| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package org.apache.camel.component.irc; |
| 19 |
|
|
| 20 |
|
import org.apache.camel.Exchange; |
| 21 |
|
import org.apache.camel.RuntimeCamelException; |
| 22 |
|
import org.apache.camel.impl.DefaultProducer; |
| 23 |
|
import org.apache.commons.logging.Log; |
| 24 |
|
import org.apache.commons.logging.LogFactory; |
| 25 |
|
import org.schwering.irc.lib.IRCConnection; |
| 26 |
|
import org.schwering.irc.lib.IRCEventListener; |
| 27 |
|
|
| 28 |
|
public class IrcProducer extends DefaultProducer<IrcExchange> { |
| 29 |
0 |
private static final transient Log log = LogFactory.getLog(IrcProducer.class); |
| 30 |
|
private IRCConnection connection; |
| 31 |
|
private IrcEndpoint endpoint; |
| 32 |
|
private IRCEventListener ircErrorLogger; |
| 33 |
|
|
| 34 |
|
public IrcProducer(IrcEndpoint endpoint, IRCConnection connection) { |
| 35 |
0 |
super(endpoint); |
| 36 |
0 |
this.endpoint = endpoint; |
| 37 |
0 |
this.connection = connection; |
| 38 |
0 |
} |
| 39 |
|
|
| 40 |
|
public void process(Exchange exchange) throws Exception { |
| 41 |
|
try { |
| 42 |
0 |
final String msg = exchange.getIn().getBody(String.class); |
| 43 |
0 |
if (isMessageACommand(msg)) { |
| 44 |
0 |
connection.send(msg); |
| 45 |
0 |
} |
| 46 |
|
else { |
| 47 |
0 |
final String target = endpoint.getConfiguration().getTarget(); |
| 48 |
|
|
| 49 |
0 |
if (log.isDebugEnabled()) { |
| 50 |
0 |
log.debug("sending to: " + target + " message: " + msg); |
| 51 |
|
} |
| 52 |
|
|
| 53 |
0 |
connection.doPrivmsg(target, msg); |
| 54 |
|
} |
| 55 |
|
} |
| 56 |
0 |
catch (Exception e) { |
| 57 |
0 |
throw new RuntimeCamelException(e); |
| 58 |
0 |
} |
| 59 |
0 |
} |
| 60 |
|
|
| 61 |
|
@Override |
| 62 |
|
protected void doStart() throws Exception { |
| 63 |
0 |
super.doStart(); |
| 64 |
|
|
| 65 |
0 |
ircErrorLogger = createIrcErrorLogger(); |
| 66 |
0 |
connection.addIRCEventListener(ircErrorLogger); |
| 67 |
|
|
| 68 |
0 |
final String target = endpoint.getConfiguration().getTarget(); |
| 69 |
|
|
| 70 |
0 |
log.debug("joining: " + target); |
| 71 |
0 |
connection.doJoin(target); |
| 72 |
0 |
} |
| 73 |
|
|
| 74 |
|
@Override |
| 75 |
|
protected void doStop() throws Exception { |
| 76 |
0 |
super.doStop(); |
| 77 |
0 |
if (connection != null) { |
| 78 |
0 |
connection.removeIRCEventListener(ircErrorLogger); |
| 79 |
|
} |
| 80 |
0 |
} |
| 81 |
|
|
| 82 |
|
protected boolean isMessageACommand(String msg) { |
| 83 |
0 |
for (String command : commands) { |
| 84 |
0 |
if (msg.startsWith(command)) { |
| 85 |
0 |
return true; |
| 86 |
|
} |
| 87 |
|
} |
| 88 |
0 |
return false; |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
protected IRCEventListener createIrcErrorLogger() { |
| 92 |
0 |
return new IrcErrorLogger(log); |
| 93 |
|
} |
| 94 |
|
|
| 95 |
0 |
public final String[] commands = new String[]{ |
| 96 |
|
"AWAY", "INVITE", "ISON", "JOIN", |
| 97 |
|
"KICK", "LIST", "NAMES", "PRIVMSG", |
| 98 |
|
"MODE", "NICK", "NOTICE", "PART", |
| 99 |
|
"PONG", "QUIT", "TOPIC", "WHO", |
| 100 |
|
"WHOIS", "WHOWAS", "USERHOST" |
| 101 |
|
}; |
| 102 |
|
} |