001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.irc;
018    
019    import org.apache.camel.Processor;
020    import org.apache.camel.impl.DefaultConsumer;
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    import org.schwering.irc.lib.IRCConnection;
024    import org.schwering.irc.lib.IRCEventAdapter;
025    import org.schwering.irc.lib.IRCModeParser;
026    import org.schwering.irc.lib.IRCUser;
027    
028    public class IrcConsumer extends DefaultConsumer {
029        private static final transient Log LOG = LogFactory.getLog(IrcConsumer.class);
030        
031        private final IrcConfiguration configuration;
032        private final IrcEndpoint endpoint;
033        private final IRCConnection connection;
034        private FilteredIRCEventAdapter listener;
035    
036        public IrcConsumer(IrcEndpoint endpoint, Processor processor, IRCConnection connection) {
037            super(endpoint, processor);
038            this.endpoint = endpoint;
039            this.connection = connection;
040            this.configuration = endpoint.getConfiguration();
041        }
042    
043        @Override
044        protected void doStop() throws Exception {
045            if (connection != null) {
046                String target = endpoint.getConfiguration().getTarget();
047                connection.doPart(target);
048                connection.removeIRCEventListener(listener);
049            }
050            super.doStop();
051        }
052    
053        @Override
054        protected void doStart() throws Exception {
055            super.doStart();
056    
057            String target = endpoint.getConfiguration().getTarget();
058            listener = new FilteredIRCEventAdapter(target);
059            connection.addIRCEventListener(listener);
060    
061            if (LOG.isDebugEnabled()) {
062                LOG.debug("Joining: " + target);
063            }
064            connection.doJoin(target);
065        }
066    
067        public IRCConnection getConnection() {
068            return connection;
069        }
070    
071        class FilteredIRCEventAdapter extends IRCEventAdapter {
072            final String target;
073    
074            public FilteredIRCEventAdapter(String target) {
075                this.target = target;
076            }
077    
078            @Override
079            public void onNick(IRCUser user, String newNick) {
080                if (configuration.isOnNick()) {
081                    IrcExchange exchange = endpoint.createOnNickExchange(user, newNick);
082                    try {
083                        getProcessor().process(exchange);
084                    } catch (Exception e) {
085                        handleException(e);
086                    }
087                }
088            }
089    
090            @Override
091            public void onQuit(IRCUser user, String msg) {
092                if (configuration.isOnQuit()) {
093                    IrcExchange exchange = endpoint.createOnQuitExchange(user, msg);
094                    try {
095                        getProcessor().process(exchange);
096                    } catch (Exception e) {
097                        handleException(e);
098                    }
099                }
100            }
101    
102            @Override
103            public void onJoin(String channel, IRCUser user) {
104                if (configuration.isOnJoin()) {
105                    if (channel.equals(configuration.getTarget())) {
106                        IrcExchange exchange = endpoint.createOnJoinExchange(channel, user);
107                        try {
108                            getProcessor().process(exchange);
109                        } catch (Exception e) {
110                            handleException(e);
111                        }
112                    }
113                }
114            }
115    
116            @Override
117            public void onKick(String channel, IRCUser user, String passiveNick, String msg) {
118                if (configuration.isOnKick()) {
119                    if (channel.equals(configuration.getTarget())) {
120                        IrcExchange exchange = endpoint.createOnKickExchange(channel, user, passiveNick, msg);
121                        try {
122                            getProcessor().process(exchange);
123                        } catch (Exception e) {
124                            handleException(e);
125                        }
126                    }
127                }
128            }
129    
130            @Override
131            public void onMode(String channel, IRCUser user, IRCModeParser modeParser) {
132                if (configuration.isOnMode()) {
133                    if (channel.equals(configuration.getTarget())) {
134                        IrcExchange exchange = endpoint.createOnModeExchange(channel, user, modeParser);
135                        try {
136                            getProcessor().process(exchange);
137                        } catch (Exception e) {
138                            handleException(e);
139                        }
140                    }
141                }
142            }
143    
144            @Override
145            public void onPart(String channel, IRCUser user, String msg) {
146                if (configuration.isOnPart()) {
147                    if (channel.equals(configuration.getTarget())) {
148                        IrcExchange exchange = endpoint.createOnPartExchange(channel, user, msg);
149                        try {
150                            getProcessor().process(exchange);
151                        } catch (Exception e) {
152                            handleException(e);
153                        }
154                    }
155                }
156            }
157    
158            @Override
159            public void onTopic(String channel, IRCUser user, String topic) {
160                if (configuration.isOnTopic()) {
161                    if (channel.equals(configuration.getTarget())) {
162                        IrcExchange exchange = endpoint.createOnTopicExchange(channel, user, topic);
163                        try {
164                            getProcessor().process(exchange);
165                        } catch (Exception e) {
166                            handleException(e);
167                        }
168                    }
169                }
170            }
171    
172            @Override
173            public void onPrivmsg(String target, IRCUser user, String msg) {
174                if (configuration.isOnPrivmsg()) {
175                    if (target.equals(configuration.getTarget())) {
176                        IrcExchange exchange = endpoint.createOnPrivmsgExchange(target, user, msg);
177                        try {
178                            getProcessor().process(exchange);
179                        } catch (Exception e) {
180                            handleException(e);
181                        }
182                    }
183                }
184            }
185        }
186    }