001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.camel.component.irc;
019
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<IrcExchange> {
030 private static final transient Log log = LogFactory.getLog(IrcConsumer.class);
031 final private IrcEndpoint endpoint;
032 final private IRCConnection connection;
033 final IrcConfiguration configuration;
034 private FilteredIRCEventAdapter listener = null;
035
036 public IrcConsumer(IrcEndpoint endpoint, Processor processor, IRCConnection connection) {
037 super(endpoint, processor);
038 this.endpoint = endpoint;
039 this.connection = connection;
040 configuration = endpoint.getConfiguration();
041 }
042
043 @Override
044 protected void doStop() throws Exception {
045 String target = endpoint.getConfiguration().getTarget();
046 connection.doPart(target);
047 connection.removeIRCEventListener(listener);
048
049 super.doStop();
050 }
051
052 @Override
053 protected void doStart() throws Exception {
054 super.doStart();
055
056 String target = endpoint.getConfiguration().getTarget();
057 connection.addIRCEventListener(new FilteredIRCEventAdapter(target));
058
059 log.debug("joining: " + target);
060 connection.doJoin(target);
061 }
062
063 public IRCConnection getConnection() {
064 return connection;
065 }
066
067 class FilteredIRCEventAdapter extends IRCEventAdapter {
068 final String target;
069
070 public FilteredIRCEventAdapter(String target) {
071 this.target = target;
072 }
073
074 @Override
075 public void onNick(IRCUser user, String newNick) {
076 if (configuration.isOnNick()) {
077 IrcExchange exchange = endpoint.createOnNickExchange(user, newNick);
078 try {
079 getProcessor().process(exchange);
080 }
081 catch (Exception e) {
082 // TODO: what should we do when a processing failure occurs??
083 e.printStackTrace();
084 }
085 }
086 }
087
088 @Override
089 public void onQuit(IRCUser user, String msg) {
090 if (configuration.isOnQuit()) {
091 IrcExchange exchange = endpoint.createOnQuitExchange(user, msg);
092 try {
093 getProcessor().process(exchange);
094 }
095 catch (Exception e) {
096 // TODO: what should we do when a processing failure occurs??
097 e.printStackTrace();
098 }
099 }
100 }
101
102 @Override
103 public void onJoin(String channel, IRCUser user) {
104 if (configuration.isOnJoin()) {
105 if (channel.equals(configuration.getTarget())) {
106 IrcExchange exchange = endpoint.createOnJoinExchange(channel, user);
107 try {
108 getProcessor().process(exchange);
109 }
110 catch (Exception e) {
111 // TODO: what should we do when a processing failure occurs??
112 e.printStackTrace();
113 }
114 }
115 }
116 }
117
118 @Override
119 public void onKick(String channel, IRCUser user, String passiveNick, String msg) {
120 if (configuration.isOnKick()) {
121 if (channel.equals(configuration.getTarget())) {
122 IrcExchange exchange = endpoint.createOnKickExchange(channel, user, passiveNick, msg);
123 try {
124 getProcessor().process(exchange);
125 }
126 catch (Exception e) {
127 // TODO: what should we do when a processing failure occurs??
128 e.printStackTrace();
129 }
130 }
131 }
132 }
133
134 @Override
135 public void onMode(String channel, IRCUser user, IRCModeParser modeParser) {
136 if (configuration.isOnMode()) {
137 if (channel.equals(configuration.getTarget())) {
138 IrcExchange exchange = endpoint.createOnModeExchange(channel, user, modeParser);
139 try {
140 getProcessor().process(exchange);
141 }
142 catch (Exception e) {
143 // TODO: what should we do when a processing failure occurs??
144 e.printStackTrace();
145 }
146 }
147 }
148 }
149
150 @Override
151 public void onPart(String channel, IRCUser user, String msg) {
152 if (configuration.isOnPart()) {
153 if (channel.equals(configuration.getTarget())) {
154 IrcExchange exchange = endpoint.createOnPartExchange(channel, user, msg);
155 try {
156 getProcessor().process(exchange);
157 }
158 catch (Exception e) {
159 // TODO: what should we do when a processing failure occurs??
160 e.printStackTrace();
161 }
162 }
163 }
164 }
165
166 @Override
167 public void onTopic(String channel, IRCUser user, String topic) {
168 if (configuration.isOnTopic()) {
169 if (channel.equals(configuration.getTarget())) {
170 IrcExchange exchange = endpoint.createOnTopicExchange(channel, user, topic);
171 try {
172 getProcessor().process(exchange);
173 }
174 catch (Exception e) {
175 // TODO: what should we do when a processing failure occurs??
176 e.printStackTrace();
177 }
178 }
179 }
180 }
181
182 @Override
183 public void onPrivmsg(String target, IRCUser user, String msg) {
184 if (configuration.isOnPrivmsg()) {
185 if (target.equals(configuration.getTarget())) {
186 IrcExchange exchange = endpoint.createOnPrivmsgExchange(target, user, msg);
187 try {
188 getProcessor().process(exchange);
189 }
190 catch (Exception e) {
191 // TODO: what should we do when a processing failure occurs??
192 e.printStackTrace();
193 }
194 }
195 }
196 }
197 }
198 }