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.schwering.irc.lib.IRCConnection;
023 import org.schwering.irc.lib.IRCEventAdapter;
024 import org.schwering.irc.lib.IRCModeParser;
025 import org.schwering.irc.lib.IRCUser;
026 import org.slf4j.Logger;
027 import org.slf4j.LoggerFactory;
028
029 public class IrcConsumer extends DefaultConsumer {
030 private static final transient Logger LOG = LoggerFactory.getLogger(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 LOG.debug("Parting: {}", channel);
049 connection.doPart(channel);
050 }
051 connection.removeIRCEventListener(listener);
052 }
053 super.doStop();
054 }
055
056 @Override
057 protected void doStart() throws Exception {
058 super.doStart();
059 listener = getListener();
060 connection.addIRCEventListener(listener);
061 endpoint.joinChannels();
062 }
063
064 public IRCConnection getConnection() {
065 return connection;
066 }
067
068 public IRCEventAdapter getListener() {
069 if (listener == null) {
070 listener = new FilteredIRCEventAdapter();
071 }
072 return listener;
073 }
074
075 public void setListener(IRCEventAdapter listener) {
076 this.listener = listener;
077 }
078
079 class FilteredIRCEventAdapter extends IRCEventAdapter {
080
081 @Override
082 public void onNick(IRCUser user, String newNick) {
083 if (configuration.isOnNick()) {
084 Exchange exchange = endpoint.createOnNickExchange(user, newNick);
085 try {
086 getProcessor().process(exchange);
087 } catch (Exception e) {
088 handleException(e);
089 }
090 }
091 }
092
093 @Override
094 public void onQuit(IRCUser user, String msg) {
095 if (configuration.isOnQuit()) {
096 Exchange exchange = endpoint.createOnQuitExchange(user, msg);
097 try {
098 getProcessor().process(exchange);
099 } catch (Exception e) {
100 handleException(e);
101 }
102 }
103 }
104
105 @Override
106 public void onJoin(String channel, IRCUser user) {
107 if (configuration.isOnJoin()) {
108 Exchange exchange = endpoint.createOnJoinExchange(channel, user);
109 try {
110 getProcessor().process(exchange);
111 } catch (Exception e) {
112 handleException(e);
113 }
114 }
115 }
116
117 @Override
118 public void onKick(String channel, IRCUser user, String passiveNick, String msg) {
119
120 // check to see if I got kick and if so rejoin if autoRejoin is on
121 if (passiveNick.equals(connection.getNick()) && configuration.isAutoRejoin()) {
122 endpoint.joinChannel(channel);
123 }
124
125 if (configuration.isOnKick()) {
126 Exchange exchange = endpoint.createOnKickExchange(channel, user, passiveNick, msg);
127 try {
128 getProcessor().process(exchange);
129 } catch (Exception e) {
130 handleException(e);
131 }
132 }
133 }
134
135 @Override
136 public void onMode(String channel, IRCUser user, IRCModeParser modeParser) {
137 if (configuration.isOnMode()) {
138 Exchange exchange = endpoint.createOnModeExchange(channel, user, modeParser);
139 try {
140 getProcessor().process(exchange);
141 } catch (Exception e) {
142 handleException(e);
143 }
144 }
145 }
146
147 @Override
148 public void onPart(String channel, IRCUser user, String msg) {
149 if (configuration.isOnPart()) {
150 Exchange exchange = endpoint.createOnPartExchange(channel, user, msg);
151 try {
152 getProcessor().process(exchange);
153 } catch (Exception e) {
154 handleException(e);
155 }
156 }
157 }
158
159 @Override
160 public void onReply(int num, String value, String msg) {
161 if (configuration.isOnReply()) {
162 Exchange exchange = endpoint.createOnReplyExchange(num, value, msg);
163 try {
164 getProcessor().process(exchange);
165 } catch (Exception e) {
166 handleException(e);
167 }
168 }
169 }
170
171 @Override
172 public void onTopic(String channel, IRCUser user, String topic) {
173 if (configuration.isOnTopic()) {
174 Exchange exchange = endpoint.createOnTopicExchange(channel, user, topic);
175 try {
176 getProcessor().process(exchange);
177 } catch (Exception e) {
178 handleException(e);
179 }
180 }
181 }
182
183 @Override
184 public void onPrivmsg(String target, IRCUser user, String msg) {
185 if (configuration.isOnPrivmsg()) {
186 Exchange exchange = endpoint.createOnPrivmsgExchange(target, user, msg);
187 try {
188 getProcessor().process(exchange);
189 } catch (Exception e) {
190 handleException(e);
191 }
192 }
193 }
194
195 @Override
196 public void onError(int num, String msg) {
197 }
198
199 }
200
201 }