limit limitFreq to per channel
[lusty.git] / core.py
blobf31f608bb81a6fd86490087018442b52340a5b7c
1 # lusty is srid's pet IRC bot
2 # cuddle, but not annoy her.
3 # $ python lusty.py channel channel.log
5 from twisted.words.protocols import irc
6 from twisted.internet import reactor, protocol
7 from twisted.python import log
9 import time, sys
11 import plugins
14 class IRCBot(irc.IRCClient):
15 """An IRC bot."""
17 nickname = "lusty"
18 realname = "Lusty"
20 def connectionMade(self):
21 irc.IRCClient.connectionMade(self)
22 log.msg("[connected at %s]" %
23 time.asctime(time.localtime(time.time())))
25 def connectionLost(self, reason):
26 irc.IRCClient.connectionLost(self, reason)
27 log.msg("[disconnected at %s]" %
28 time.asctime(time.localtime(time.time())))
31 # callbacks for events
33 def signedOn(self):
34 for channel in self.factory.channels:
35 self.join(channel)
37 def joined(self, channel):
38 log.msg("[JOIN %s]" % channel)
40 def privmsg(self, user, channel, msg):
41 """This will get called when the bot receives a message."""
42 user = user.split('!', 1)[0]
43 log.msg("<%s> %s" % (user, msg))
45 plugins.handleMsg(self, user, channel, msg)
46 return
48 # Check to see if they're sending me a private message
49 if channel == self.nickname:
50 msg = "It isn't nice to whisper! Play nice with the group."
51 self.msg(user, msg)
52 log.msg('pwned!!', user, msg)
53 return
55 matchedRest = util.prefixMatch(msg, [self.nickname + ':', ','])
56 if matchedRest is not None:
57 if matchedRest == 'reload':
58 reload(commands)
59 log.msg('reloaded commands.py')
60 return
61 else:
62 respChannel, responses = commands.handleCommand(
63 user, channel, matchedRest)
64 else:
65 respChannel, responses = commands.handleGeneric(user, channel, msg)
67 for r in responses:
68 self.msg(respChannel, r)
69 log.msg("<%s> %s" % (self.nickname, r))
70 if not responses:
71 log.msg('No bot response for %s' % msg)
74 def action(self, user, channel, msg):
75 """This will get called when the bot sees someone do an action."""
76 user = user.split('!', 1)[0]
77 log.msg("* %s %s" % (user, msg))
79 def irc_NICK(self, prefix, params):
80 """Called when an IRC user changes their nickname."""
81 old_nick = prefix.split('!')[0]
82 new_nick = params[0]
83 log.msg("%s NICK %s" % (old_nick, new_nick))
86 class IRCBotFactory(protocol.ClientFactory):
88 protocol = IRCBot
90 def __init__(self, channels):
91 self.channels = channels
93 def clientConnectionLost(self, connector, reason):
94 connector.connect() # reconnect
96 def clientConnectionFailed(self, connector, reason):
97 print "connection failed:", reason
98 reactor.stop()
101 if __name__ == '__main__':
102 channels = sys.argv[1:]
103 print channels
105 log.startLogging(sys.stdout)
106 f = IRCBotFactory(channels)
107 reactor.connectTCP("irc.freenode.net", 6666, f)
108 reactor.run()