This repository has been archived by the owner on Feb 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
irc.py
93 lines (65 loc) · 2.76 KB
/
irc.py
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
import logging
import socket
import pydle
import config
BotBase = pydle.featurize(pydle.features.RFC1459Support, pydle.features.TLSSupport)
logger = logging.getLogger("IRC")
logger.setLevel(logging.DEBUG)
cfg = config.init()
class IRC(BotBase):
tracking = None
RECONNECT_MAX_ATTEMPTS = 100
# temp fix until pydle handles connect failures
def connect(self, *args, **kwargs):
try:
super().connect(*args, **kwargs)
except socket.error:
self.on_disconnect(expected=False)
def set_tracker(self, track):
self.tracking = track
def on_connect(self):
logger.info("Connected to: %s, joining %s", self.tracking['irc_host'], self.tracking['irc_channel'])
nick_pass = cfg["{}.nick_pass".format(self.tracking['name'].lower())]
if nick_pass is not None and len(nick_pass) > 1:
self.rawmsg('NICKSERV', 'IDENTIFY', nick_pass)
self.join(self.tracking['irc_channel'])
def on_raw(self, message):
super().on_raw(message)
if cfg["{}.nick".format(self.tracking['name'].lower())] in message._raw and '+r' in message._raw:
logger.debug("Identified with NICKSERV - joining %s", self.tracking['irc_channel'])
self.join(self.tracking['irc_channel'])
def on_raw_900(self, message):
logger.debug("Identified with NICKSERV - joining %s", self.tracking['irc_channel'])
self.join(self.tracking['irc_channel'])
def on_message(self, source, target, message):
if source[0] != '#':
logger.debug("%s sent us a message: %s", target, message)
else:
self.tracking['plugin'].parse(message)
def on_invite(self, channel, by):
if channel == self.tracking['irc_channel']:
self.join(self.tracking['irc_channel'])
pool = pydle.ClientPool()
clients = []
def start(trackers):
global cfg, pool, clients
for tracker in trackers.loaded:
logger.info("Pooling server: %s:%d %s", tracker['irc_host'], tracker['irc_port'], tracker['irc_channel'])
nick = cfg["{}.nick".format(tracker['name'].lower())]
client = IRC(nick)
client.set_tracker(tracker)
clients.append(client)
try:
pool.connect(client, hostname=tracker['irc_host'], port=tracker['irc_port'],
tls=tracker['irc_tls'], tls_verify=tracker['irc_tls_verify'])
except Exception as ex:
logger.exception("Error while connecting to: %s", tracker['irc_host'])
try:
pool.handle_forever()
except Exception as ex:
logger.exception("Exception pool.handle_forever:")
def stop():
global pool
for tracker in clients:
logger.debug("Removing tracker: %s", tracker.tracking['name'])
pool.disconnect(tracker)