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.RuntimeCamelException;
021    import org.apache.camel.impl.DefaultProducer;
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 IrcProducer extends DefaultProducer {
030    
031        public static final String[] COMMANDS = new String[]{"AWAY", "INVITE", "ISON", "JOIN", "KICK", "LIST", "NAMES",
032            "PRIVMSG", "MODE", "NICK", "NOTICE", "PART", "PONG", "QUIT", "TOPIC", "WHO", "WHOIS", "WHOWAS", "USERHOST"};
033    
034        private static final transient Log LOG = LogFactory.getLog(IrcProducer.class);
035    
036        private IRCConnection connection;
037        private IrcEndpoint endpoint;
038        private IRCEventAdapter listener;
039    
040        public IrcProducer(IrcEndpoint endpoint, IRCConnection connection) {
041            super(endpoint);
042            this.endpoint = endpoint;
043            this.connection = connection;
044        }
045    
046        public void process(Exchange exchange) throws Exception {
047            final String msg = exchange.getIn().getBody(String.class);
048            final String targetChannel = exchange.getIn().getHeader(IrcConstants.IRC_TARGET, String.class);
049    
050            if (!connection.isConnected()) {
051                throw new RuntimeCamelException("Lost connection to " + connection.getHost());
052            }
053    
054            if (isMessageACommand(msg)) {
055                if (LOG.isDebugEnabled()) {
056                    LOG.debug("Sending command: " + msg);
057                }
058                connection.send(msg);
059            } else if (targetChannel != null) {
060                if (LOG.isDebugEnabled()) {
061                    LOG.debug("Sending to: " + targetChannel + " message: " + msg);
062                }
063                connection.doPrivmsg(targetChannel, msg);
064            } else {
065                for (String channel : endpoint.getConfiguration().getChannels()) {
066                    if (LOG.isDebugEnabled()) {
067                        LOG.debug("Sending to: " + channel + " message: " + msg);
068                    }
069                    connection.doPrivmsg(channel, msg);
070                }
071            }
072        }
073    
074        @Override
075        protected void doStart() throws Exception {
076            super.doStart();
077            listener = getListener();
078            connection.addIRCEventListener(listener);
079            endpoint.joinChannels();
080        }
081    
082    
083        @Override
084        protected void doStop() throws Exception {
085            if (connection != null) {
086                for (String channel : endpoint.getConfiguration().getChannels()) {
087                    if (LOG.isDebugEnabled()) {
088                        LOG.debug("Parting: " + channel);
089                    }
090                    connection.doPart(channel);
091                }
092                connection.removeIRCEventListener(listener);
093            }
094            super.doStop();
095        }
096    
097        protected boolean isMessageACommand(String msg) {
098            for (String command : COMMANDS) {
099                if (msg.startsWith(command)) {
100                    return true;
101                }
102            }
103            return false;
104        }
105    
106        public IRCEventAdapter getListener() {
107            if (listener == null) {
108                listener = new FilteredIRCEventAdapter();
109            }
110            return listener;
111        }
112    
113        public void setListener(IRCEventAdapter listener) {
114            this.listener = listener;
115        }
116    
117        class FilteredIRCEventAdapter extends IRCEventAdapter {
118    
119            @Override
120            public void onKick(String channel, IRCUser user, String passiveNick, String msg) {
121    
122                // check to see if I got kick and if so rejoin if autoRejoin is on
123                if (passiveNick.equals(connection.getNick()) && endpoint.getConfiguration().isAutoRejoin()) {
124                    endpoint.joinChannel(channel);
125                }
126            }
127    
128    
129            @Override
130            public void onError(int num, String msg) {
131                IrcProducer.this.endpoint.handleIrcError(num, msg);
132            }
133    
134        }
135    
136    }