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 java.util.List; 020 021 import org.apache.camel.Exchange; 022 import org.apache.camel.impl.DefaultProducer; 023 import org.apache.camel.util.ObjectHelper; 024 import org.apache.commons.logging.Log; 025 import org.apache.commons.logging.LogFactory; 026 import org.schwering.irc.lib.IRCConnection; 027 028 public class IrcProducer extends DefaultProducer { 029 030 public static final String[] COMMANDS = new String[]{"AWAY", "INVITE", "ISON", "JOIN", "KICK", "LIST", "NAMES", 031 "PRIVMSG", "MODE", "NICK", "NOTICE", "PART", "PONG", "QUIT", "TOPIC", "WHO", "WHOIS", "WHOWAS", "USERHOST"}; 032 033 private static final transient Log LOG = LogFactory.getLog(IrcProducer.class); 034 035 private IRCConnection connection; 036 private IrcEndpoint endpoint; 037 038 public IrcProducer(IrcEndpoint endpoint, IRCConnection connection) { 039 super(endpoint); 040 this.endpoint = endpoint; 041 this.connection = connection; 042 } 043 044 public void process(Exchange exchange) throws Exception { 045 final String msg = exchange.getIn().getBody(String.class); 046 final String targetChannel = exchange.getIn().getHeader(IrcConstants.IRC_TARGET, String.class); 047 if (isMessageACommand(msg)) { 048 if (LOG.isDebugEnabled()) { 049 LOG.debug("Sending command: " + msg); 050 } 051 connection.send(msg); 052 } else if (targetChannel != null) { 053 if (LOG.isDebugEnabled()) { 054 LOG.debug("Sending to: " + targetChannel + " message: " + msg); 055 } 056 connection.doPrivmsg(targetChannel, msg); 057 } else { 058 for (String channel : endpoint.getConfiguration().getChannels()) { 059 if (LOG.isDebugEnabled()) { 060 LOG.debug("Sending to: " + channel + " message: " + msg); 061 } 062 connection.doPrivmsg(channel, msg); 063 } 064 } 065 } 066 067 @Override 068 protected void doStart() throws Exception { 069 super.doStart(); 070 071 List<String> channels = endpoint.getConfiguration().getChannels(); 072 for (String channel : endpoint.getConfiguration().getChannels()) { 073 074 // find key for channel 075 int ndx = channels.indexOf(channel); 076 String key = null; 077 if (ndx >= 0) { 078 List<String> keys = endpoint.getConfiguration().getKeys(); 079 if (keys.size() > 0 && ndx < keys.size()) { 080 key = keys.get(ndx); 081 } 082 } 083 084 if (ObjectHelper.isNotEmpty(key)) { 085 if (LOG.isDebugEnabled()) { 086 LOG.debug("Joining: " + channel + " using " + connection.getClass().getName() + " with key " + key); 087 } 088 connection.doJoin(channel, key); 089 } else { 090 if (LOG.isDebugEnabled()) { 091 LOG.debug("Joining: " + channel + " using " + connection.getClass().getName()); 092 } 093 connection.doJoin(channel); 094 } 095 } 096 } 097 098 @Override 099 protected void doStop() throws Exception { 100 if (connection != null) { 101 for (String channel : endpoint.getConfiguration().getChannels()) { 102 if (LOG.isDebugEnabled()) { 103 LOG.debug("Parting: " + channel); 104 } 105 connection.doPart(channel); 106 } 107 } 108 super.doStop(); 109 } 110 111 protected boolean isMessageACommand(String msg) { 112 for (String command : COMMANDS) { 113 if (msg.startsWith(command)) { 114 return true; 115 } 116 } 117 return false; 118 } 119 120 }