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 FilteredIRCEventAdapter 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 String target = endpoint.getConfiguration().getTarget(); 048 connection.doPart(target); 049 connection.removeIRCEventListener(listener); 050 } 051 super.doStop(); 052 } 053 054 @Override 055 protected void doStart() throws Exception { 056 super.doStart(); 057 058 String target = endpoint.getConfiguration().getTarget(); 059 listener = new FilteredIRCEventAdapter(target); 060 connection.addIRCEventListener(listener); 061 062 if (LOG.isDebugEnabled()) { 063 LOG.debug("Joining: " + target + " using " + connection.getClass().getName()); 064 } 065 connection.doJoin(target); 066 } 067 068 public IRCConnection getConnection() { 069 return connection; 070 } 071 072 class FilteredIRCEventAdapter extends IRCEventAdapter { 073 final String target; 074 075 public FilteredIRCEventAdapter(String target) { 076 this.target = target; 077 } 078 079 @Override 080 public void onNick(IRCUser user, String newNick) { 081 if (configuration.isOnNick()) { 082 Exchange exchange = endpoint.createOnNickExchange(user, newNick); 083 try { 084 getProcessor().process(exchange); 085 } catch (Exception e) { 086 handleException(e); 087 } 088 } 089 } 090 091 @Override 092 public void onQuit(IRCUser user, String msg) { 093 if (configuration.isOnQuit()) { 094 Exchange exchange = endpoint.createOnQuitExchange(user, msg); 095 try { 096 getProcessor().process(exchange); 097 } catch (Exception e) { 098 handleException(e); 099 } 100 } 101 } 102 103 @Override 104 public void onJoin(String channel, IRCUser user) { 105 if (configuration.isOnJoin()) { 106 if (channel.equals(configuration.getTarget())) { 107 Exchange exchange = endpoint.createOnJoinExchange(channel, user); 108 try { 109 getProcessor().process(exchange); 110 } catch (Exception e) { 111 handleException(e); 112 } 113 } 114 } 115 } 116 117 @Override 118 public void onKick(String channel, IRCUser user, String passiveNick, String msg) { 119 if (configuration.isOnKick()) { 120 if (channel.equals(configuration.getTarget())) { 121 Exchange exchange = endpoint.createOnKickExchange(channel, user, passiveNick, msg); 122 try { 123 getProcessor().process(exchange); 124 } catch (Exception e) { 125 handleException(e); 126 } 127 } 128 } 129 } 130 131 @Override 132 public void onMode(String channel, IRCUser user, IRCModeParser modeParser) { 133 if (configuration.isOnMode()) { 134 if (channel.equals(configuration.getTarget())) { 135 Exchange exchange = endpoint.createOnModeExchange(channel, user, modeParser); 136 try { 137 getProcessor().process(exchange); 138 } catch (Exception e) { 139 handleException(e); 140 } 141 } 142 } 143 } 144 145 @Override 146 public void onPart(String channel, IRCUser user, String msg) { 147 if (configuration.isOnPart()) { 148 if (channel.equals(configuration.getTarget())) { 149 Exchange exchange = endpoint.createOnPartExchange(channel, user, msg); 150 try { 151 getProcessor().process(exchange); 152 } catch (Exception e) { 153 handleException(e); 154 } 155 } 156 } 157 } 158 159 @Override 160 public void onTopic(String channel, IRCUser user, String topic) { 161 if (configuration.isOnTopic()) { 162 if (channel.equals(configuration.getTarget())) { 163 Exchange exchange = endpoint.createOnTopicExchange(channel, user, topic); 164 try { 165 getProcessor().process(exchange); 166 } catch (Exception e) { 167 handleException(e); 168 } 169 } 170 } 171 } 172 173 @Override 174 public void onPrivmsg(String target, IRCUser user, String msg) { 175 if (configuration.isOnPrivmsg()) { 176 if (target.equals(configuration.getTarget()) || target.equals(configuration.getNickname())) { 177 Exchange exchange = endpoint.createOnPrivmsgExchange(target, user, msg); 178 try { 179 getProcessor().process(exchange); 180 } catch (Exception e) { 181 handleException(e); 182 } 183 } 184 } 185 } 186 } 187 }