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<IrcExchange> {
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            configuration = endpoint.getConfiguration();
041        }
042    
043        @Override
044        protected void doStop() throws Exception {
045            String target = endpoint.getConfiguration().getTarget();
046            connection.doPart(target);
047            connection.removeIRCEventListener(listener);
048    
049            super.doStop();
050        }
051    
052        @Override
053        protected void doStart() throws Exception {
054            super.doStart();
055    
056            String target = endpoint.getConfiguration().getTarget();
057            connection.addIRCEventListener(new FilteredIRCEventAdapter(target));
058    
059            LOG.debug("joining: " + target);
060            connection.doJoin(target);
061        }
062    
063        public IRCConnection getConnection() {
064            return connection;
065        }
066    
067        class FilteredIRCEventAdapter extends IRCEventAdapter {
068            final String target;
069    
070            public FilteredIRCEventAdapter(String target) {
071                this.target = target;
072            }
073    
074            @Override
075            public void onNick(IRCUser user, String newNick) {
076                if (configuration.isOnNick()) {
077                    IrcExchange exchange = endpoint.createOnNickExchange(user, newNick);
078                    try {
079                        getProcessor().process(exchange);
080                    } catch (Exception e) {
081                        // TODO: what should we do when a processing failure
082                        // occurs??
083                        e.printStackTrace();
084                    }
085                }
086            }
087    
088            @Override
089            public void onQuit(IRCUser user, String msg) {
090                if (configuration.isOnQuit()) {
091                    IrcExchange exchange = endpoint.createOnQuitExchange(user, msg);
092                    try {
093                        getProcessor().process(exchange);
094                    } catch (Exception e) {
095                        // TODO: what should we do when a processing failure
096                        // occurs??
097                        e.printStackTrace();
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                            // TODO: what should we do when a processing failure
111                            // occurs??
112                            e.printStackTrace();
113                        }
114                    }
115                }
116            }
117    
118            @Override
119            public void onKick(String channel, IRCUser user, String passiveNick, String msg) {
120                if (configuration.isOnKick()) {
121                    if (channel.equals(configuration.getTarget())) {
122                        IrcExchange exchange = endpoint.createOnKickExchange(channel, user, passiveNick, msg);
123                        try {
124                            getProcessor().process(exchange);
125                        } catch (Exception e) {
126                            // TODO: what should we do when a processing failure
127                            // occurs??
128                            e.printStackTrace();
129                        }
130                    }
131                }
132            }
133    
134            @Override
135            public void onMode(String channel, IRCUser user, IRCModeParser modeParser) {
136                if (configuration.isOnMode()) {
137                    if (channel.equals(configuration.getTarget())) {
138                        IrcExchange exchange = endpoint.createOnModeExchange(channel, user, modeParser);
139                        try {
140                            getProcessor().process(exchange);
141                        } catch (Exception e) {
142                            // TODO: what should we do when a processing failure
143                            // occurs??
144                            e.printStackTrace();
145                        }
146                    }
147                }
148            }
149    
150            @Override
151            public void onPart(String channel, IRCUser user, String msg) {
152                if (configuration.isOnPart()) {
153                    if (channel.equals(configuration.getTarget())) {
154                        IrcExchange exchange = endpoint.createOnPartExchange(channel, user, msg);
155                        try {
156                            getProcessor().process(exchange);
157                        } catch (Exception e) {
158                            // TODO: what should we do when a processing failure
159                            // occurs??
160                            e.printStackTrace();
161                        }
162                    }
163                }
164            }
165    
166            @Override
167            public void onTopic(String channel, IRCUser user, String topic) {
168                if (configuration.isOnTopic()) {
169                    if (channel.equals(configuration.getTarget())) {
170                        IrcExchange exchange = endpoint.createOnTopicExchange(channel, user, topic);
171                        try {
172                            getProcessor().process(exchange);
173                        } catch (Exception e) {
174                            // TODO: what should we do when a processing failure
175                            // occurs??
176                            e.printStackTrace();
177                        }
178                    }
179                }
180            }
181    
182            @Override
183            public void onPrivmsg(String target, IRCUser user, String msg) {
184                if (configuration.isOnPrivmsg()) {
185                    if (target.equals(configuration.getTarget())) {
186                        IrcExchange exchange = endpoint.createOnPrivmsgExchange(target, user, msg);
187                        try {
188                            getProcessor().process(exchange);
189                        } catch (Exception e) {
190                            // TODO: what should we do when a processing failure
191                            // occurs??
192                            e.printStackTrace();
193                        }
194                    }
195                }
196            }
197        }
198    }