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