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.Exchange;
020    import org.apache.camel.Processor;
021    import org.apache.camel.impl.DefaultConsumer;
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.schwering.irc.lib.IRCConnection;
025    import org.schwering.irc.lib.IRCEventAdapter;
026    import org.schwering.irc.lib.IRCModeParser;
027    import org.schwering.irc.lib.IRCUser;
028    
029    public class IrcConsumer extends DefaultConsumer {
030        private static final transient Log LOG = LogFactory.getLog(IrcConsumer.class);
031    
032        private final IrcConfiguration configuration;
033        private final IrcEndpoint endpoint;
034        private final IRCConnection connection;
035        private FilteredIRCEventAdapter listener;
036    
037        public IrcConsumer(IrcEndpoint endpoint, Processor processor, IRCConnection connection) {
038            super(endpoint, processor);
039            this.endpoint = endpoint;
040            this.connection = connection;
041            this.configuration = endpoint.getConfiguration();
042        }
043    
044        @Override
045        protected void doStop() throws Exception {
046            if (connection != null) {
047                for (String channel : endpoint.getConfiguration().getChannels()) {
048                    if (LOG.isDebugEnabled()) {
049                        LOG.debug("Parting: " + channel);
050                    }
051                    connection.doPart(channel);
052                }
053                connection.removeIRCEventListener(listener);
054            }
055            super.doStop();
056        }
057    
058        @Override
059        protected void doStart() throws Exception {
060            super.doStart();
061    
062            listener = new FilteredIRCEventAdapter();
063            connection.addIRCEventListener(listener);
064    
065            for (String channel : endpoint.getConfiguration().getChannels()) {
066                if (LOG.isDebugEnabled()) {
067                    LOG.debug("Joining: " + channel + " using " + connection.getClass().getName());
068                }
069                connection.doJoin(channel);
070            }
071        }
072    
073        public IRCConnection getConnection() {
074            return connection;
075        }
076    
077        class FilteredIRCEventAdapter extends IRCEventAdapter {
078    
079            @Override
080            public void onNick(IRCUser user, String newNick) {
081                if (configuration.isOnNick()) {
082                    Exchange exchange = endpoint.createOnNickExchange(user, newNick);
083                    try {
084                        getProcessor().process(exchange);
085                    } catch (Exception e) {
086                        handleException(e);
087                    }
088                }
089            }
090    
091            @Override
092            public void onQuit(IRCUser user, String msg) {
093                if (configuration.isOnQuit()) {
094                    Exchange exchange = endpoint.createOnQuitExchange(user, msg);
095                    try {
096                        getProcessor().process(exchange);
097                    } catch (Exception e) {
098                        handleException(e);
099                    }
100                }
101            }
102    
103            @Override
104            public void onJoin(String channel, IRCUser user) {
105                if (configuration.isOnJoin()) {
106                    Exchange exchange = endpoint.createOnJoinExchange(channel, user);
107                    try {
108                        getProcessor().process(exchange);
109                    } catch (Exception e) {
110                        handleException(e);
111                    }
112                }
113            }
114    
115            @Override
116            public void onKick(String channel, IRCUser user, String passiveNick, String msg) {
117                if (configuration.isOnKick()) {
118                    Exchange exchange = endpoint.createOnKickExchange(channel, user, passiveNick, msg);
119                    try {
120                        getProcessor().process(exchange);
121                    } catch (Exception e) {
122                        handleException(e);
123                    }
124                }
125            }
126    
127            @Override
128            public void onMode(String channel, IRCUser user, IRCModeParser modeParser) {
129                if (configuration.isOnMode()) {
130                    Exchange exchange = endpoint.createOnModeExchange(channel, user, modeParser);
131                    try {
132                        getProcessor().process(exchange);
133                    } catch (Exception e) {
134                        handleException(e);
135                    }
136                }
137            }
138    
139            @Override
140            public void onPart(String channel, IRCUser user, String msg) {
141                if (configuration.isOnPart()) {
142                    Exchange exchange = endpoint.createOnPartExchange(channel, user, msg);
143                    try {
144                        getProcessor().process(exchange);
145                    } catch (Exception e) {
146                        handleException(e);
147                    }
148                }
149            }
150    
151            @Override
152            public void onReply(int num, String value, String msg) {
153                if (configuration.isOnReply()) {
154                    Exchange exchange = endpoint.createOnReplyExchange(num, value, msg);
155                    try {
156                        getProcessor().process(exchange);
157                    } catch (Exception e) {
158                        handleException(e);
159                    }
160                }
161            }
162    
163            @Override
164            public void onTopic(String channel, IRCUser user, String topic) {
165                if (configuration.isOnTopic()) {
166                    Exchange exchange = endpoint.createOnTopicExchange(channel, user, topic);
167                    try {
168                        getProcessor().process(exchange);
169                    } catch (Exception e) {
170                        handleException(e);
171                    }
172                }
173            }
174    
175            @Override
176            public void onPrivmsg(String target, IRCUser user, String msg) {
177                if (configuration.isOnPrivmsg()) {
178                    Exchange exchange = endpoint.createOnPrivmsgExchange(target, user, msg);
179                    try {
180                        getProcessor().process(exchange);
181                    } catch (Exception e) {
182                        handleException(e);
183                    }
184                }
185            }
186        }
187    }