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.Exchange;
021 import org.apache.camel.RuntimeCamelException;
022 import org.apache.camel.impl.DefaultProducer;
023 import org.apache.commons.logging.Log;
024 import org.apache.commons.logging.LogFactory;
025 import org.schwering.irc.lib.IRCConnection;
026 import org.schwering.irc.lib.IRCEventListener;
027
028 public class IrcProducer extends DefaultProducer<IrcExchange> {
029 private static final transient Log log = LogFactory.getLog(IrcProducer.class);
030 private IRCConnection connection;
031 private IrcEndpoint endpoint;
032 private IRCEventListener ircErrorLogger;
033
034 public IrcProducer(IrcEndpoint endpoint, IRCConnection connection) {
035 super(endpoint);
036 this.endpoint = endpoint;
037 this.connection = connection;
038 }
039
040 public void process(Exchange exchange) throws Exception {
041 try {
042 final String msg = exchange.getIn().getBody(String.class);
043 if (isMessageACommand(msg)) {
044 connection.send(msg);
045 }
046 else {
047 final String target = endpoint.getConfiguration().getTarget();
048
049 if (log.isDebugEnabled()) {
050 log.debug("sending to: " + target + " message: " + msg);
051 }
052
053 connection.doPrivmsg(target, msg);
054 }
055 }
056 catch (Exception e) {
057 throw new RuntimeCamelException(e);
058 }
059 }
060
061 @Override
062 protected void doStart() throws Exception {
063 super.doStart();
064
065 ircErrorLogger = createIrcErrorLogger();
066 connection.addIRCEventListener(ircErrorLogger);
067
068 final String target = endpoint.getConfiguration().getTarget();
069
070 log.debug("joining: " + target);
071 connection.doJoin(target);
072 }
073
074 @Override
075 protected void doStop() throws Exception {
076 super.doStop();
077 if (connection != null) {
078 connection.removeIRCEventListener(ircErrorLogger);
079 }
080 }
081
082 protected boolean isMessageACommand(String msg) {
083 for (String command : commands) {
084 if (msg.startsWith(command)) {
085 return true;
086 }
087 }
088 return false;
089 }
090
091 protected IRCEventListener createIrcErrorLogger() {
092 return new IrcErrorLogger(log);
093 }
094
095 public final String[] commands = new String[]{
096 "AWAY", "INVITE", "ISON", "JOIN",
097 "KICK", "LIST", "NAMES", "PRIVMSG",
098 "MODE", "NICK", "NOTICE", "PART",
099 "PONG", "QUIT", "TOPIC", "WHO",
100 "WHOIS", "WHOWAS", "USERHOST"
101 };
102 }