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