-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpyirccat.py
More file actions
376 lines (282 loc) · 10.5 KB
/
pyirccat.py
File metadata and controls
376 lines (282 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# encoding: utf8
import argparse
from Queue import Queue
import re
import socket
import sys
import threading
from time import sleep
from os import getpid
# ext. dependencies
from OpenSSL import SSL
def verify_cb(conn, cert, errnum, depth, ok):
return ok
class BindException(Exception):
'''Thrown when we cant listen on a given ip:port'''
pass
class IRCClient(object):
'''Simple IRC client - supports PRIVMSG, JOIN and QUIT commands'''
def __init__(self, host, port, channel, nick, ssl_mode=False, ssl_no_verify=False, password=None):
self.host = host
self.port = port
self.channel = "#{}".format(channel)
self.nick = nick
self.ssl_mode = ssl_mode
self.ssl_no_verify = ssl_no_verify
self.password = password
self._connected = False
self.realname = 'pyirccat'
def __repr__(self):
return '<%s(%s:%s(%s) channel=#%s nick=%s)>' % (
self.__class__.__name__, self.host, self.port,
'(SSL, %s)' % ('non-verified' if self.ssl_no_verify else 'verified') if self.ssl_mode else '(Plain)',
self.channel, self.nick,
)
def _send(self, msg, delay=True):
'''Internal send method to send a command to the irc server connection'''
if not self._connected:
return
# we delay most messages in some vein attempt
# not to flood an ircd
if delay:
sleep(0.2)
print '> %s' % msg
self._s.send('%s\n' % (msg,))
def connect(self):
'''Connect to irc server'''
if self.ssl_mode:
ctx = SSL.Context(SSL.SSLv23_METHOD)
if not self.ssl_no_verify:
ctx.set_verify(SSL.VERIFY_PEER, verify_cb)
self._s = SSL.Connection(ctx, socket.socket(socket.AF_INET, socket.SOCK_STREAM))
else:
self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# note; calling code is expected to deal with a thrown socket.error
self._s.connect((self.host, self.port))
self._connected = True
if self.password is not None:
self._send('PASS %s' % self.password)
# send nick and user commands, then join the channel and announce presence
self._send('NICK %s' % self.nick)
self._send('USER %s 8 * :%s' % (self.nick, self.realname))
self.join(self.channel)
self.privmsg(self.channel, 'Bot active')
def interact(self):
'''
Interact indefinitely with the irc server after connection
Currently essentially all this does is report data received from the server
and replies to ping requests
'''
while self._connected:
try:
d = self._s.recv(2048)
except:
break
else:
if not d:
break
for l in d.strip().split('\n'):
self.received(l)
if d.startswith('PING :'):
try:
self._send('PONG :%s' % (d.strip().split(':')[1],))
except:
print '[error PONG]'
def parse_message(self, raw):
'''Parses a message to work out if it was channel prefixed or not '''
if not raw:
return None
r = re.match(r'([#@][\w\d_\-\.]+)\s{1}(.*)', raw)
if r is None:
return self.channel, raw
# we wanted it sent to some other channel
recipient, msg = r.groups()
recipient = recipient.lstrip("@")
return recipient, msg
def received(self, msg):
print '< %s' % msg.strip()
def send(self, d):
'''Sends a message to a channel
Expected format: "#channel message here"
'''
parsed = self.parse_message(d)
if parsed is not None:
# message was valid
channel, msg = parsed
# send message to channel
self.privmsg(channel, msg)
# IRC commands
# see RFC2812: https://tools.ietf.org/html/rfc2812
def privmsg(self, channel, msg):
self._send('PRIVMSG %s :%s' % (channel, msg))
def join(self, channel):
self._send('JOIN %s' % self.channel)
def quit(self, msg=None):
self._connected = False
self._send('QUIT' if not msg else 'QUIT :%s' % (msg,))
self._s.close()
class Listener(object):
'''Listener which listens on a host and port for messages and shoves
said messages into a queue which the irc client consumes
'''
def __init__(self, bind_addr, bind_port, queue, backlog=4):
self.bind_addr = bind_addr
self.bind_port = bind_port
self.queue = queue
self.backlog = backlog
def __repr__(self):
return '<%s(%s:%s)>' % (self.__class__.__name__, self.bind_addr, self.bind_port)
def close(self):
if hasattr(self, '_s'):
self._s.close()
def listen(self):
self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self._s.bind((self.bind_addr, self.bind_port))
except socket.error:
em = 'Could not bind to host %s:%s' % (self.bind_addr, self.bind_port)
if self.bind_port < 1024:
em = '%s (port < 1024, privilege error?)' % em
raise BindException(em)
self._s.listen(self.backlog)
while True:
conn, addr = self._s.accept()
def h_cnx():
while True:
data = conn.recv(4096)
if not data:
break
# bump message onto queue
self.queue.put(data)
break
conn.close()
t = threading.Thread(target=h_cnx)
t.start()
class IRCClientWorker(threading.Thread):
def __init__(self, host, port, channel, nickname, queue, ssl_mode=False, ssl_verify=True, password=None):
threading.Thread.__init__(self)
self.host = host
self.port = port
self.channel = channel
self.nickname = nickname
self.ssl_mode = ssl_mode
self.ssl_verify = ssl_verify
self.password = password
self.queue = queue
self._stop = threading.Event()
self.process = IRCClient(
self.host, self.port, self.channel,
self.nickname, self.ssl_mode, self.ssl_verify, self.password,
)
def stop(self):
self.process.quit()
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
# connect to server
try:
self.process.connect()
except socket.error:
return
# spin the interaction off into a thread
def irc_interact():
self.process.interact()
t = threading.Thread(target=irc_interact)
t.daemon = True
t.start()
# send items in the queue off to be processed
while True and not self.stopped():
item = self.queue.get()
if item is None:
# none is a signal to quit
break
self.process.send(item)
class ListenerWorker(threading.Thread):
def __init__(self, addr, port, queue):
threading.Thread.__init__(self)
self.addr = addr
self.port = port
self.queue = queue
self._stop = threading.Event()
self.process = Listener(self.addr, self.port, self.queue)
def stop(self):
self.process.close()
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
while True and not self.stopped():
try:
self.process.listen()
except BindException as e:
print 'Error: %s' % (e,)
self.stop()
break
class MainWorker(threading.Thread):
def __init__(self, parser):
threading.Thread.__init__(self)
self.parser = parser
self._stop = threading.Event()
self.threads = []
def stop(self):
self._stop.set()
for t in self.threads:
t.stop()
def stopped(self):
return self._stop.isSet()
def run(self):
q = Queue()
t_irc = IRCClientWorker(
self.parser.host, self.parser.port, self.parser.channel,
self.parser.nickname, q, self.parser.ssl, self.parser.ssl_no_verify,
self.parser.password
)
t_listener = ListenerWorker(self.parser.bind_addr, self.parser.bind_port, q)
self.threads.append(t_irc)
self.threads.append(t_listener)
t_irc.daemon = True
t_listener.daemon = True
# start them
t_irc.start()
t_listener.start()
while True:
# if either thread quits, stop everything
sleep(0.25)
if t_listener.stopped() or t_irc.stopped():
self.stop()
def cli_args():
'''Returns a prepared ArgumentParser'''
parser = argparse.ArgumentParser(description='pyirccat - cat to irc')
parser.add_argument('-s', '--server', dest='host', required=True, type=str,
help='IRC Server hostname')
parser.add_argument('-p', '--port', dest='port', default=6667, type=int,
help='IRC Server port')
parser.add_argument('--password', dest='password', type=str, help='IRC server password')
parser.add_argument('-n', '--nickname', dest='nickname', default='pyirccat', type=str,
help='Nickname of bot')
parser.add_argument('-c', '--channel', dest='channel', required=True, type=str,
help='Channel to join, without # prefix')
parser.add_argument('-ba', '--bind-addr', dest='bind_addr', required=True, type=str,
help='IP to bind to')
parser.add_argument('-bp', '--bind-port', dest='bind_port', required=True, type=int,
help='Port to bind to')
parser.add_argument('--ssl', dest='ssl', action='store_true', help='Join server via SSL')
parser.add_argument('--ssl-no-verify', dest='ssl_no_verify', action='store_true',
help='Disable SSL cert verification')
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help='Noisy mode')
return parser
if __name__ == '__main__':
parser = cli_args().parse_args()
main_worker = MainWorker(parser)
main_worker.daemon = True
main_worker.start()
while not main_worker.stopped():
try:
sleep(0.1)
main_worker.join(1.0)
except KeyboardInterrupt:
main_worker.stop()
sys.exit(0)
# we're done.
print 'Finished!'