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.RuntimeCamelException;
021 import org.apache.camel.impl.DefaultProducer;
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.IRCEventListener;
026
027 public class IrcProducer extends DefaultProducer<IrcExchange> {
028
029 public static final String[] COMMANDS = new String[] {"AWAY", "INVITE", "ISON", "JOIN", "KICK", "LIST", "NAMES", "PRIVMSG", "MODE", "NICK", "NOTICE", "PART", "PONG", "QUIT", "TOPIC", "WHO",
030 "WHOIS", "WHOWAS", "USERHOST"};
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 try {
045 final String msg = exchange.getIn().getBody(String.class);
046 if (isMessageACommand(msg)) {
047 connection.send(msg);
048 } else {
049 final String target = endpoint.getConfiguration().getTarget();
050
051 if (LOG.isDebugEnabled()) {
052 LOG.debug("sending to: " + target + " message: " + msg);
053 }
054
055 connection.doPrivmsg(target, msg);
056 }
057 } catch (Exception e) {
058 throw new RuntimeCamelException(e);
059 }
060 }
061
062 @Override
063 protected void doStart() throws Exception {
064 super.doStart();
065
066 ircErrorLogger = createIrcErrorLogger();
067 connection.addIRCEventListener(ircErrorLogger);
068
069 final String target = endpoint.getConfiguration().getTarget();
070
071 LOG.debug("joining: " + target);
072 connection.doJoin(target);
073 }
074
075 @Override
076 protected void doStop() throws Exception {
077 super.doStop();
078 if (connection != null) {
079 connection.removeIRCEventListener(ircErrorLogger);
080 }
081 }
082
083 protected boolean isMessageACommand(String msg) {
084 for (String command : COMMANDS) {
085 if (msg.startsWith(command)) {
086 return true;
087 }
088 }
089 return false;
090 }
091
092 protected IRCEventListener createIrcErrorLogger() {
093 return new IrcErrorLogger(LOG);
094 }
095
096 }