-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
60 lines (49 loc) · 1.85 KB
/
bot.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
import re
import socket
import time
import trackeback
from connection import Connection
from logging import Logging
class Bot(object):
ping_pattern = re.compile('^PING (?P<payload>.*)')
chanmsg_pattern = re.compile(':(?P<nick>.*?)!\S+\s+?PRIVMSG\s+#(?P<channel>[-\w]+)\s+:(?P<message>[^\n\r]+)')
def __init__(self, server, ident, channel, path):
self._dispatch_table = (
(self.ping_pattern, self.handle_ping),
(self.chanmsg_pattern, self.handle_chanmsg))
self._logger = Logging(path)
self.server = server
self.ident = ident
self.channel = channel
self.start()
def start(self):
self._connection = Connection(self.server)
self.register_connection(self.ident)
self.join_channel(self.channel)
def loop(self):
while True:
try:
line = self._connection.read()
except socket.error as se:
trackeback.print_exc()
print "Caught exception. Will reconnect."
del(self._connection)
time.sleep(60)
self.start()
continue
for pattern, handler in self._dispatch_table:
match = pattern.match(line)
if match:
handler(**match.groupdict())
def handle_ping(self, payload):
self._connection.send("PONG " + payload)
def handle_chanmsg(self, nick, channel, message):
self._logger.write(nick + ": " + message)
def register_connection(self, ident):
nick, passw = ident
self._connection.send("PASS " + passw)
self._connection.send("NICK " + nick)
self._connection.send("USER " + nick + " 0 * :" + nick)
def join_channel(self, channel):
chan, passw = channel
self._connection.send("JOIN " + chan + " " + passw)