Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 160 lines (114 sloc) 4.321 kB
42c0855 @honza Add some docs
authored
1 import sys
ea52d53 @honza Add greeter. Add room env.
authored
2 import os
42c0855 @honza Add some docs
authored
3 from twisted.words.protocols import irc
4 from twisted.internet import reactor, protocol
5 from twisted.python import log
ea52d53 @honza Add greeter. Add room env.
authored
6
09c6120 @sjl Add the Volunteer matcher to the list.
sjl authored
7 from matchers import (
8caeca8 @honza Add points matcher
authored
8 BrbMatcher, SifterMatcher, GifterMatcher, JenkinsMatcher, VolunteerMatcher,
db61c54 @honza Add silly matcher
authored
9 TimezoneMatcher, PointsMatcher, SillyMatcher
09c6120 @sjl Add the Volunteer matcher to the list.
sjl authored
10 )
ea52d53 @honza Add greeter. Add room env.
authored
11
f59adb9 @honza Add matchers directory
authored
12 IGNORED_USERS = os.environ.get('IGNORED', '').split(',')
c54f7c8 @honza Add twisted bot
authored
13
14
05864c0 @honza Add brain and simple responding logic
authored
15 class Brain(object):
42c0855 @honza Add some docs
authored
16 """
17 Check an incoming message against registered matchers and let the matchers
18 speak into the channel.
19 """
05864c0 @honza Add brain and simple responding logic
authored
20
fb41f67 @honza Refactor matchers
authored
21 def __init__(self, bot, matchers):
05864c0 @honza Add brain and simple responding logic
authored
22 self.bot = bot
fb41f67 @honza Refactor matchers
authored
23 self.channel = None
24 self.matchers = []
25 map(self.register, matchers)
26
27 def register(self, matcher):
28 matcher.brain = self
29 matcher.brain = self
30 self.matchers.append(matcher)
31
32 def set_channel(self, channel):
33 for matcher in self.matchers:
34 matcher.channel = channel
35 self.channel = channel
05864c0 @honza Add brain and simple responding logic
authored
36
37 def handle(self, channel, message, user=None):
fb41f67 @honza Refactor matchers
authored
38 for matcher in self.matchers:
39 matcher.respond(message, user)
05864c0 @honza Add brain and simple responding logic
authored
40
41
c54f7c8 @honza Add twisted bot
authored
42 class LogBot(irc.IRCClient):
43 """A logging IRC bot."""
8e4656e @ehazlett added gifter
ehazlett authored
44
2e8e20f @honza Change nickname
authored
45 nickname = "nigel_bot"
8e4656e @ehazlett added gifter
ehazlett authored
46
c54f7c8 @honza Add twisted bot
authored
47 def connectionMade(self):
48 irc.IRCClient.connectionMade(self)
49
50 def connectionLost(self, reason):
51 irc.IRCClient.connectionLost(self, reason)
52
53 # callbacks for events
54
55 def signedOn(self):
56 """Called when bot has succesfully signed on to server."""
57 self.join(self.factory.channel)
58
59 def privmsg(self, user, channel, msg):
60 """This will get called when the bot receives a message."""
61 user = user.split('!', 1)[0]
6c4bea3 @honza Add support for ignoring users. Don't break on match.
authored
62
fb41f67 @honza Refactor matchers
authored
63 if not self.brain.channel:
64 self.brain.set_channel(channel)
65
6c4bea3 @honza Add support for ignoring users. Don't break on match.
authored
66 if user in IGNORED_USERS:
67 print 'ignoring message from:', user
68 return
8e4656e @ehazlett added gifter
ehazlett authored
69
c54f7c8 @honza Add twisted bot
authored
70 # Check to see if they're sending me a private message
71 if channel == self.nickname:
72 msg = "It isn't nice to whisper! Play nice with the group."
73 self.msg(user, msg)
74 return
75
76 # Otherwise check to see if it is a message directed at me
77 if msg.startswith(self.nickname + ":"):
42c0855 @honza Add some docs
authored
78 msg = msg.replace(self.nickname + ':', '')
79 msg = msg.strip()
05864c0 @honza Add brain and simple responding logic
authored
80 self.brain.handle(channel, msg, user)
81 return
82
fb41f67 @honza Refactor matchers
authored
83 self.brain.handle(channel, msg, user)
c54f7c8 @honza Add twisted bot
authored
84
0351e53 @honza Add name collision override
authored
85 def alterCollidedNick(self, nickname):
86 """
87 Generate an altered version of a nickname that caused a collision in an
88 effort to create an unused related name for subsequent registration.
89
90 By default it uses _.
91 """
92 return nickname + '____'
93
c54f7c8 @honza Add twisted bot
authored
94
95 class LogBotFactory(protocol.ClientFactory):
96 """A factory for LogBots.
97
98 A new protocol instance will be created each time we connect to the server.
99 """
100
bb4c086 @honza Remove logging
authored
101 def __init__(self, channel):
c54f7c8 @honza Add twisted bot
authored
102 self.channel = channel
103
104 def buildProtocol(self, addr):
105 p = LogBot()
248af9a @honza Add jenkins matcher
authored
106 p.brain = Brain(p, [BrbMatcher(), SifterMatcher(), GifterMatcher(),
8caeca8 @honza Add points matcher
authored
107 JenkinsMatcher(), VolunteerMatcher(), TimezoneMatcher(),
db61c54 @honza Add silly matcher
authored
108 PointsMatcher(), SillyMatcher()])
c54f7c8 @honza Add twisted bot
authored
109 p.factory = self
110 return p
111
112 def clientConnectionLost(self, connector, reason):
113 """If we get disconnected, reconnect to server."""
114 connector.connect()
115
116 def clientConnectionFailed(self, connector, reason):
117 print "connection failed:", reason
118 reactor.stop()
119
120
fa853e1 @honza Add fake bot
authored
121 class FakeBot(object):
122
123 def msg(self, channel, message):
124 print message
125
126
c54f7c8 @honza Add twisted bot
authored
127 if __name__ == '__main__':
fa853e1 @honza Add fake bot
authored
128 argv = sys.argv
129
130 if len(argv) > 1:
131 if argv[1] == '-s':
132 message = argv[2]
133
134 bot = FakeBot()
135
136 brain = Brain(bot, [BrbMatcher(), SifterMatcher(), GifterMatcher(),
8caeca8 @honza Add points matcher
authored
137 JenkinsMatcher(), VolunteerMatcher(), TimezoneMatcher(),
db61c54 @honza Add silly matcher
authored
138 PointsMatcher(), SillyMatcher()])
fa853e1 @honza Add fake bot
authored
139
140 brain.set_channel(None)
141
8e09350 @honza Better jenkins matcher
authored
142 brain.handle('', message, 'le-user')
fa853e1 @honza Add fake bot
authored
143
144 sys.exit(1)
145
c54f7c8 @honza Add twisted bot
authored
146 # initialize logging
147 log.startLogging(sys.stdout)
8e4656e @ehazlett added gifter
ehazlett authored
148
c54f7c8 @honza Add twisted bot
authored
149 # create factory protocol and application
ea52d53 @honza Add greeter. Add room env.
authored
150 room = os.environ.get('ROOM')
151 if not room:
152 room = sys.argv[1]
153 f = LogBotFactory(room)
c54f7c8 @honza Add twisted bot
authored
154
155 # connect factory to this host and port
156 reactor.connectTCP("irc.freenode.net", 6667, f)
157
158 # run bot
159 reactor.run()
Something went wrong with that request. Please try again.