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 IRCEventAdapter 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 listener = getListener(); 062 connection.addIRCEventListener(listener); 063 endpoint.joinChannels(); 064 } 065 066 public IRCConnection getConnection() { 067 return connection; 068 } 069 070 public IRCEventAdapter getListener() { 071 if (listener == null) { 072 listener = new FilteredIRCEventAdapter(); 073 } 074 return listener; 075 } 076 077 public void setListener(IRCEventAdapter listener) { 078 this.listener = listener; 079 } 080 081 class FilteredIRCEventAdapter extends IRCEventAdapter { 082 083 @Override 084 public void onNick(IRCUser user, String newNick) { 085 if (configuration.isOnNick()) { 086 Exchange exchange = endpoint.createOnNickExchange(user, newNick); 087 try { 088 getProcessor().process(exchange); 089 } catch (Exception e) { 090 handleException(e); 091 } 092 } 093 } 094 095 @Override 096 public void onQuit(IRCUser user, String msg) { 097 if (configuration.isOnQuit()) { 098 Exchange exchange = endpoint.createOnQuitExchange(user, msg); 099 try { 100 getProcessor().process(exchange); 101 } catch (Exception e) { 102 handleException(e); 103 } 104 } 105 } 106 107 @Override 108 public void onJoin(String channel, IRCUser user) { 109 if (configuration.isOnJoin()) { 110 Exchange exchange = endpoint.createOnJoinExchange(channel, user); 111 try { 112 getProcessor().process(exchange); 113 } catch (Exception e) { 114 handleException(e); 115 } 116 } 117 } 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()) && configuration.isAutoRejoin()) { 124 endpoint.joinChannel(channel); 125 } 126 127 if (configuration.isOnKick()) { 128 Exchange exchange = endpoint.createOnKickExchange(channel, user, passiveNick, msg); 129 try { 130 getProcessor().process(exchange); 131 } catch (Exception e) { 132 handleException(e); 133 } 134 } 135 } 136 137 @Override 138 public void onMode(String channel, IRCUser user, IRCModeParser modeParser) { 139 if (configuration.isOnMode()) { 140 Exchange exchange = endpoint.createOnModeExchange(channel, user, modeParser); 141 try { 142 getProcessor().process(exchange); 143 } catch (Exception e) { 144 handleException(e); 145 } 146 } 147 } 148 149 @Override 150 public void onPart(String channel, IRCUser user, String msg) { 151 if (configuration.isOnPart()) { 152 Exchange exchange = endpoint.createOnPartExchange(channel, user, msg); 153 try { 154 getProcessor().process(exchange); 155 } catch (Exception e) { 156 handleException(e); 157 } 158 } 159 } 160 161 @Override 162 public void onReply(int num, String value, String msg) { 163 if (configuration.isOnReply()) { 164 Exchange exchange = endpoint.createOnReplyExchange(num, value, msg); 165 try { 166 getProcessor().process(exchange); 167 } catch (Exception e) { 168 handleException(e); 169 } 170 } 171 } 172 173 @Override 174 public void onTopic(String channel, IRCUser user, String topic) { 175 if (configuration.isOnTopic()) { 176 Exchange exchange = endpoint.createOnTopicExchange(channel, user, topic); 177 try { 178 getProcessor().process(exchange); 179 } catch (Exception e) { 180 handleException(e); 181 } 182 } 183 } 184 185 @Override 186 public void onPrivmsg(String target, IRCUser user, String msg) { 187 if (configuration.isOnPrivmsg()) { 188 Exchange exchange = endpoint.createOnPrivmsgExchange(target, user, msg); 189 try { 190 getProcessor().process(exchange); 191 } catch (Exception e) { 192 handleException(e); 193 } 194 } 195 } 196 197 @Override 198 public void onError(int num, String msg) { 199 } 200 201 } 202 203 }