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 java.util.List; 020 021 import org.apache.camel.Exchange; 022 import org.apache.camel.Processor; 023 import org.apache.camel.impl.DefaultConsumer; 024 import org.apache.camel.util.ObjectHelper; 025 import org.apache.commons.logging.Log; 026 import org.apache.commons.logging.LogFactory; 027 import org.schwering.irc.lib.IRCConnection; 028 import org.schwering.irc.lib.IRCEventAdapter; 029 import org.schwering.irc.lib.IRCModeParser; 030 import org.schwering.irc.lib.IRCUser; 031 032 public class IrcConsumer extends DefaultConsumer { 033 private static final transient Log LOG = LogFactory.getLog(IrcConsumer.class); 034 035 private final IrcConfiguration configuration; 036 private final IrcEndpoint endpoint; 037 private final IRCConnection connection; 038 private FilteredIRCEventAdapter listener; 039 040 public IrcConsumer(IrcEndpoint endpoint, Processor processor, IRCConnection connection) { 041 super(endpoint, processor); 042 this.endpoint = endpoint; 043 this.connection = connection; 044 this.configuration = endpoint.getConfiguration(); 045 } 046 047 @Override 048 protected void doStop() throws Exception { 049 if (connection != null) { 050 for (String channel : endpoint.getConfiguration().getChannels()) { 051 if (LOG.isDebugEnabled()) { 052 LOG.debug("Parting: " + channel); 053 } 054 connection.doPart(channel); 055 } 056 connection.removeIRCEventListener(listener); 057 } 058 super.doStop(); 059 } 060 061 @Override 062 protected void doStart() throws Exception { 063 super.doStart(); 064 065 listener = new FilteredIRCEventAdapter(); 066 connection.addIRCEventListener(listener); 067 068 List<String> channels = endpoint.getConfiguration().getChannels(); 069 for (String channel : channels) { 070 071 // find key for channel 072 int ndx = channels.indexOf(channel); 073 String key = null; 074 if (ndx >= 0) { 075 List<String> keys = endpoint.getConfiguration().getKeys(); 076 if (keys.size() > 0 && ndx < keys.size()) { 077 key = keys.get(ndx); 078 } 079 } 080 081 if (ObjectHelper.isNotEmpty(key)) { 082 if (LOG.isDebugEnabled()) { 083 LOG.debug("Joining: " + channel + " using " + connection.getClass().getName() + " with key " + key); 084 } 085 connection.doJoin(channel, key); 086 } else { 087 if (LOG.isDebugEnabled()) { 088 LOG.debug("Joining: " + channel + " using " + connection.getClass().getName()); 089 } 090 connection.doJoin(channel); 091 } 092 } 093 } 094 095 public IRCConnection getConnection() { 096 return connection; 097 } 098 099 class FilteredIRCEventAdapter extends IRCEventAdapter { 100 101 @Override 102 public void onNick(IRCUser user, String newNick) { 103 if (configuration.isOnNick()) { 104 Exchange exchange = endpoint.createOnNickExchange(user, newNick); 105 try { 106 getProcessor().process(exchange); 107 } catch (Exception e) { 108 handleException(e); 109 } 110 } 111 } 112 113 @Override 114 public void onQuit(IRCUser user, String msg) { 115 if (configuration.isOnQuit()) { 116 Exchange exchange = endpoint.createOnQuitExchange(user, msg); 117 try { 118 getProcessor().process(exchange); 119 } catch (Exception e) { 120 handleException(e); 121 } 122 } 123 } 124 125 @Override 126 public void onJoin(String channel, IRCUser user) { 127 if (configuration.isOnJoin()) { 128 Exchange exchange = endpoint.createOnJoinExchange(channel, user); 129 try { 130 getProcessor().process(exchange); 131 } catch (Exception e) { 132 handleException(e); 133 } 134 } 135 } 136 137 @Override 138 public void onKick(String channel, IRCUser user, String passiveNick, String msg) { 139 if (configuration.isOnKick()) { 140 Exchange exchange = endpoint.createOnKickExchange(channel, user, passiveNick, msg); 141 try { 142 getProcessor().process(exchange); 143 } catch (Exception e) { 144 handleException(e); 145 } 146 } 147 } 148 149 @Override 150 public void onMode(String channel, IRCUser user, IRCModeParser modeParser) { 151 if (configuration.isOnMode()) { 152 Exchange exchange = endpoint.createOnModeExchange(channel, user, modeParser); 153 try { 154 getProcessor().process(exchange); 155 } catch (Exception e) { 156 handleException(e); 157 } 158 } 159 } 160 161 @Override 162 public void onPart(String channel, IRCUser user, String msg) { 163 if (configuration.isOnPart()) { 164 Exchange exchange = endpoint.createOnPartExchange(channel, user, msg); 165 try { 166 getProcessor().process(exchange); 167 } catch (Exception e) { 168 handleException(e); 169 } 170 } 171 } 172 173 @Override 174 public void onReply(int num, String value, String msg) { 175 if (configuration.isOnReply()) { 176 Exchange exchange = endpoint.createOnReplyExchange(num, value, msg); 177 try { 178 getProcessor().process(exchange); 179 } catch (Exception e) { 180 handleException(e); 181 } 182 } 183 } 184 185 @Override 186 public void onTopic(String channel, IRCUser user, String topic) { 187 if (configuration.isOnTopic()) { 188 Exchange exchange = endpoint.createOnTopicExchange(channel, user, topic); 189 try { 190 getProcessor().process(exchange); 191 } catch (Exception e) { 192 handleException(e); 193 } 194 } 195 } 196 197 @Override 198 public void onPrivmsg(String target, IRCUser user, String msg) { 199 if (configuration.isOnPrivmsg()) { 200 Exchange exchange = endpoint.createOnPrivmsgExchange(target, user, msg); 201 try { 202 getProcessor().process(exchange); 203 } catch (Exception e) { 204 handleException(e); 205 } 206 } 207 } 208 } 209 210 }