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 if (LOG.isDebugEnabled()) { 055 LOG.debug("Sending command: " + msg); 056 } 057 connection.send(msg); 058 } else if (targetChannel != null) { 059 if (LOG.isDebugEnabled()) { 060 LOG.debug("Sending to: " + targetChannel + " message: " + msg); 061 } 062 connection.doPrivmsg(targetChannel, msg); 063 } else { 064 for (String channel : endpoint.getConfiguration().getChannels()) { 065 if (LOG.isDebugEnabled()) { 066 LOG.debug("Sending to: " + channel + " message: " + msg); 067 } 068 connection.doPrivmsg(channel, msg); 069 } 070 } 071 } 072 073 @Override 074 protected void doStart() throws Exception { 075 super.doStart(); 076 listener = getListener(); 077 connection.addIRCEventListener(listener); 078 endpoint.joinChannels(); 079 } 080 081 082 @Override 083 protected void doStop() throws Exception { 084 if (connection != null) { 085 for (String channel : endpoint.getConfiguration().getChannels()) { 086 if (LOG.isDebugEnabled()) { 087 LOG.debug("Parting: " + channel); 088 } 089 connection.doPart(channel); 090 } 091 connection.removeIRCEventListener(listener); 092 } 093 super.doStop(); 094 } 095 096 protected boolean isMessageACommand(String msg) { 097 for (String command : COMMANDS) { 098 if (msg.startsWith(command)) { 099 return true; 100 } 101 } 102 return false; 103 } 104 105 public IRCEventAdapter getListener() { 106 if (listener == null) { 107 listener = new FilteredIRCEventAdapter(); 108 } 109 return listener; 110 } 111 112 public void setListener(IRCEventAdapter listener) { 113 this.listener = listener; 114 } 115 116 class FilteredIRCEventAdapter extends IRCEventAdapter { 117 118 @Override 119 public void onKick(String channel, IRCUser user, String passiveNick, String msg) { 120 121 // check to see if I got kick and if so rejoin if autoRejoin is on 122 if (passiveNick.equals(connection.getNick()) && endpoint.getConfiguration().isAutoRejoin()) { 123 endpoint.joinChannel(channel); 124 } 125 } 126 127 128 @Override 129 public void onError(int num, String msg) { 130 IrcProducer.this.endpoint.handleIrcError(num, msg); 131 } 132 133 } 134 135 }