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
14 class IRCBot(irc
.IRCClient
):
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
34 for channel
in self
.factory
.channels
:
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
)
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."
52 log
.msg('pwned!!', user
, msg
)
55 matchedRest
= util
.prefixMatch(msg
, [self
.nickname
+ ':', ','])
56 if matchedRest
is not None:
57 if matchedRest
== 'reload':
59 log
.msg('reloaded commands.py')
62 respChannel
, responses
= commands
.handleCommand(
63 user
, channel
, matchedRest
)
65 respChannel
, responses
= commands
.handleGeneric(user
, channel
, msg
)
68 self
.msg(respChannel
, r
)
69 log
.msg("<%s> %s" % (self
.nickname
, r
))
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]
83 log
.msg("%s NICK %s" % (old_nick
, new_nick
))
86 class IRCBotFactory(protocol
.ClientFactory
):
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
101 if __name__
== '__main__':
102 channels
= sys
.argv
[1:]
105 log
.startLogging(sys
.stdout
)
106 f
= IRCBotFactory(channels
)
107 reactor
.connectTCP("irc.freenode.net", 6667, f
)