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