1 """ |
|
2 Twisted application |
|
3 """ |
|
4 |
|
5 import sys |
|
6 |
|
7 from twisted.application import internet, service |
|
8 from twisted.internet import reactor, endpoints, defer |
|
9 from twisted.python import usage, log |
|
10 |
|
11 import urlparse |
|
12 |
|
13 import pvl.irker.irk |
|
14 import pvl.irker.irc |
|
15 |
|
16 def myusername (): |
|
17 """ |
|
18 Return username current process is running under. |
|
19 """ |
|
20 |
|
21 import os, pwd |
|
22 |
|
23 return pwd.getpwuid(os.getuid()).pw_name |
|
24 |
|
25 |
|
26 class Options (usage.Options) : |
|
27 optParameters = [ |
|
28 [ 'irc-nickname', 'n', pvl.irker.irc.IRCFactory.NICKNAME, "Default IRC nickname" ], |
|
29 [ 'irc-username', 'u', myusername(), "IRC username (default: system user)" ], |
|
30 ] |
|
31 |
|
32 def __init__ (self) : |
|
33 usage.Options.__init__(self) |
|
34 |
|
35 self.listen_tcp = [] |
|
36 self.connect = [] |
|
37 self.target = [] |
|
38 self.privmsg = [] |
|
39 |
|
40 def opt_listen_tcp (self, listen) : |
|
41 """ |
|
42 Twisted endpoint. |
|
43 """ |
|
44 |
|
45 if ':' in listen : |
|
46 host, port = listen.split(':') |
|
47 else : |
|
48 host, port = '', listen |
|
49 |
|
50 port = int(port) |
|
51 |
|
52 self.listen_tcp.append((host, port)) |
|
53 |
|
54 def opt_connect (self, connect) : |
|
55 """ |
|
56 Connect to given target. |
|
57 """ |
|
58 |
|
59 self.connect.append(urlparse.urlparse(connect)) |
|
60 |
|
61 def opt_target (self, target) : |
|
62 """ |
|
63 Join given target. |
|
64 """ |
|
65 |
|
66 self.target.append(urlparse.urlparse(target)) |
|
67 |
|
68 def opt_privmsg (self, privmsg) : |
|
69 """ |
|
70 Send message to targets |
|
71 """ |
|
72 |
|
73 self.privmsg.append(privmsg) |
|
74 |
|
75 @defer.inlineCallbacks |
|
76 def connect (irc, connect) : |
|
77 """ |
|
78 Connect to given urls. |
|
79 """ |
|
80 |
|
81 try : |
|
82 clients = yield defer.gatherResults([irc.client(url) for url in connect]) |
|
83 |
|
84 except Exception as ex : |
|
85 log.err(ex) |
|
86 return |
|
87 |
|
88 for client in clients : |
|
89 log.msg('--connect', client) |
|
90 |
|
91 @defer.inlineCallbacks |
|
92 def target (irc, target, privmsg) : |
|
93 """ |
|
94 Connect to given urls. |
|
95 """ |
|
96 |
|
97 try : |
|
98 targets = yield defer.gatherResults([irc.target(url) for url in target]) |
|
99 |
|
100 except Exception as ex : |
|
101 log.err(ex) |
|
102 return |
|
103 |
|
104 for target in targets : |
|
105 log.msg('--target', target) |
|
106 |
|
107 target.privmsg(*privmsg) |
|
108 |
|
109 def makeService (options) : |
|
110 """ |
|
111 Return a Service for running irk -> irc. |
|
112 """ |
|
113 |
|
114 s = service.MultiService() |
|
115 |
|
116 # IRC |
|
117 irc = pvl.irker.irc.IRCFactory(options['irc-nickname'], options['irc-username']) |
|
118 |
|
119 connect(irc, options.connect) |
|
120 target(irc, options.target, options.privmsg) |
|
121 |
|
122 # IRK |
|
123 irk = pvl.irker.irk.IrkFactory(irc) |
|
124 |
|
125 for host, port in options.listen_tcp : |
|
126 ss = internet.TCPServer(port, irk, interface=host) |
|
127 |
|
128 log.msg("--listen", port) |
|
129 |
|
130 ss.setServiceParent(s) |
|
131 |
|
132 # return the service collection |
|
133 return s |
|
134 |
|
135 def main (args) : |
|
136 options = Options() |
|
137 options.parseOptions(args) |
|
138 |
|
139 # logging |
|
140 log.startLogging(sys.stderr, setStdout=False) |
|
141 |
|
142 # connect |
|
143 irc = pvl.irker.irc.IRCFactory(options['irc-nickname'], options['irc-username']) |
|
144 |
|
145 connect(irc, options.connect) |
|
146 target(irc, options.target, options.privmsg) |
|
147 |
|
148 # listen |
|
149 irk = pvl.irker.irk.IrkFactory(irc) |
|
150 |
|
151 for host, port in options.listen_tcp : |
|
152 endpoint = endpoints.TCP4ServerEndpoint(reactor, port, interface=host) |
|
153 |
|
154 log.msg("listen:", endpoint) |
|
155 |
|
156 endpoint.listen(irk) |
|
157 |
|
158 # go |
|
159 reactor.run() |
|
160 |
|
161 return 0 |
|
162 |
|