-
Notifications
You must be signed in to change notification settings - Fork 1
/
standupbot.py
194 lines (150 loc) · 5.37 KB
/
standupbot.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
from twisted.python import log
import json
import time
import sys
import os
from commands import Commander, schedule_standup
class JSONStore(dict):
"""
Blocking and crappy
"""
def __init__(self, path):
self._path = path
self._cache = {}
self._fill_cache()
def __getitem__(self, name):
return self._cache[name]
def __setitem__(self, name, value):
self._cache[name] = value
self._flush_cache()
def __delitem__(self, name):
del self._cache[name]
self._flush_cache()
def __contains__(self, name):
return self._cache.__contains__(name)
def flush(self):
self._flush_cache()
def _open(self, mode):
return os.fdopen(os.open(self._path, os.O_RDWR | os.O_CREAT), mode)
def _fill_cache(self):
log.msg('Loading conf from "%s"' % (self._path))
f = None
try:
f = self._open('r')
conf = f.read()
self._cache = json.loads(conf)
except Exception as e:
log.msg('Loading conf from "%s" failed: %s' % (self._path, e))
if reactor.running:
reactor.stop()
finally:
if f:
f.close()
def _flush_cache(self):
log.msg('Flushing conf to "%s"' % (self._path))
f = None
try:
conf = json.dumps(self._cache, indent=4)
f = self._open('w')
f.truncate()
f.write(conf)
except Exception as e:
log.msg('Flushing conf to "%s" failed: %s' % (self._path, e))
if reactor.running:
reactor.stop()
finally:
if f:
f.close()
class StandupBot(irc.IRCClient):
@property
def config(self):
return self.factory.config
def sendmsg(self, channel, message):
self.msg(channel.encode('utf8') if type(message) is unicode else channel,
message.encode('utf8') if type(message) is unicode else message)
def connectionMade(self):
irc.IRCClient.connectionMade(self)
log.msg("[connected at %s]" % time.asctime(time.localtime(time.time())))
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
log.msg("[disconnected at %s]" % time.asctime(time.localtime(time.time())))
def signedOn(self):
"""Called when bot has succesfully signed on to server."""
for channel, data in self.config['channels'].iteritems():
self.join(str(channel))
schedule_standup(self, channel)
def joined(self, channel):
"""This will get called when the bot joins the channel."""
log.msg("[I have joined %s]" % channel)
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
user = user.split('!', 1)[0]
log.msg("<%s> %s" % (user, msg))
if channel == self.nickname:
self.msg(user, 'PRVMSG not supported, talk in the channel')
return # don't support PM
if msg.startswith(self.nickname + ":"):
msg = msg[len(self.nickname + ":"):]
msg = msg.strip()
log.msg('parsing command from %s: %s' % (user, msg))
msg = msg.decode('utf8')
self.commander.run_command(self, user, channel, msg)
def action(self, user, channel, msg):
"""This will get called when the bot sees someone do an action."""
user = user.split('!', 1)[0]
log.msg("* %s %s" % (user, msg))
def irc_NICK(self, prefix, params):
"""Called when an IRC user changes their nickname."""
old_nick = prefix.split('!')[0]
new_nick = params[0]
log.msg("%s is now known as %s" % (old_nick, new_nick))
def alterCollidedNick(self, nickname):
"""
Generate an altered version of a nickname that caused a collision in an
effort to create an unused related name for subsequent registration.
"""
return self.nickname + '^'
class StandupBotFactory(protocol.ClientFactory):
def __init__(self, config):
self.config = config
def buildProtocol(self, addr):
p = StandupBot()
p.factory = self
p.nickname = str(self.config['nick'])
p.commander = Commander()
return p
def clientConnectionLost(self, connector, reason):
"""If we get disconnected, reconnect to server."""
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "connection failed:", reason
reactor.stop()
def _validate_config(config):
valid = True
msg = []
if 'host' not in config:
valid = False
msg.append('host')
if 'port' not in config:
valid = False
msg.append('port')
if 'nick' not in config:
valid = False
msg.append('nick')
return valid, msg
if __name__ == '__main__':
# logging
log.startLogging(sys.stdout)
# load config
config = JSONStore('./standupbot-config.json')
valid, msg = _validate_config(config)
if not valid:
log.msg('Invalid config - missing fields: %s' % (', '.join(msg)))
sys.exit(1)
# setup bot
f = StandupBotFactory(config)
reactor.connectTCP(str(config['host']), config['port'], f)
# run bot
reactor.run()