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