This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
75 lines (59 loc) · 2.28 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Copyright 2011, Kristofer Hallin ([email protected])
#
# Mermaid, IRC bot written by Kristofer Hallin
#
import os
import sys
import log
import irclib
import module
import title
class Bot(object):
def __init__(self, nick, server, port, channel):
self.log = log.Logger("bot")
self.nick = nick
self.server = server
self.port = port
self.channel = channel
self.module = module.Module()
self.title = title.Title()
def on_connect(self, connection, event):
self.log.debug("Connected, joining channel")
return connection.join(self.channel)
def on_disconnect(self, connection, event):
self.log.debug("Disconnected")
return connection.connect(self.server, self.port, self.nick)
def on_event(self, connection, event):
self.log.debug("Got event, parsing")
arg = event.arguments()[0]
argp = arg.partition(' ')
nickname = event.source().split('!')[0]
if len(argp[2]) == 0:
argument = ""
else:
argument = argp[2]
ret = self.title.parse_title(argp[0])
if ret != None:
return connection.privmsg(self.channel, ret)
try:
ret = self.module.actions.get(argp[0],
self.module.action_fallback)(nickname, argument)
except Exception as e:
self.log.debug("Module " + argp[0] + " threw exception: %s\n" % (e))
return connection.privmsg(self.channel, "Module threw exception")
if ret != None:
self.log.debug("To channel: " + ret)
connection.privmsg(self.channel, ret)
def start(self):
self.irc.process_forever()
def create(self):
self.irc = irclib.IRC()
self.log.debug("Connecting")
connection = self.irc.server().connect(self.server,
self.port,
self.nick)
connection.add_global_handler("welcome", self.on_connect)
connection.add_global_handler("disconnect", self.on_disconnect)
connection.add_global_handler("pubmsg", self.on_event)
return connection