-
Notifications
You must be signed in to change notification settings - Fork 0
/
JlewBot.py
99 lines (78 loc) · 3.54 KB
/
JlewBot.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
94
95
96
97
98
99
# JlewBot is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# JlewBot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with JlewBot. If not, see <http://www.gnu.org/licenses/>.
from twisted.words.protocols.irc import IRCClient
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.internet import reactor
class JlewBot(IRCClient):
bot_name = "JlewBot"
channel = "#jlew-test"
versionNum = 1
sourceURL = "http://gitorious.com/~jlew"
lineRate = 1
def signedOn(self):
"""Called when bot has succesfully signed on to server."""
self.join(self.factory.channel)
self.factory.add_bot(self)
def joined(self, channel):
"""This will get called when the bot joins the channel."""
print "Joined %s" % channel
def left(self, channel):
"""This will get called when the bot leaves the channel."""
print "Left %s" % channel
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
user = user.split('!', 1)[0]
# Check to see if they're sending me a private message
if channel == self.nickname:
responder = lambda x: self.msg(user, x, length=400)
msg = msg.split(" ", 1)
if len(msg) == 1:
self.factory.handle_command(responder, user, channel, msg[0], "")
elif len(msg) == 2:
self.factory.handle_command(responder, user, channel, msg[0], msg[1])
# Message said in channel to the bot
elif msg.startswith(self.nickname):
responder = lambda x: self.msg(channel, "%s: %s" % (user, x))
msg = msg.split(" ", 2)
if len(msg) == 2:
self.factory.handle_command(responder, user, channel, msg[1], "")
elif len(msg) == 3:
self.factory.handle_command(responder, user, channel, msg[1], msg[2])
class JlewBotFactory(ReconnectingClientFactory):
active_bot = None
def __init__(self, protocol=JlewBot):
self.protocol = protocol
self.channel = protocol.channel
self.registered_commands = {}
self.default_cmd_handler = self.__default_cmd
IRCClient.nickname = protocol.bot_name
IRCClient.realname = protocol.bot_name
def add_bot(self, bot):
self.active_bot = bot
def register_command(self, key, func):
self.registered_commands[key] = func
def handle_command(self, responder, user, channel, command, msg):
cb = self.registered_commands.get(command, self.default_cmd_handler)
cb(responder, user, channel, command, msg)
def __default_cmd(self, responder, user, channel, command, msg):
if command == "help":
responder("Available Commands: %s" % ', '.join(self.registered_commands.keys()))
else:
responder("Command not recognized")
if __name__ == '__main__':
# create factory protocol and application
f = JlewBotFactory("#jlew-test")
# connect factory to this host and port
reactor.connectTCP("irc.freenode.net", 6667, f)
# run bot
reactor.run()