forked from bear/relengbot
/
rbot.py
executable file
·165 lines (127 loc) · 5.06 KB
/
rbot.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
#!/usr/bin/env python
""" RelEng IRC Bot
:copyright: (c) 2012 by Mozilla
:license: MPLv2
Assumes Python v2.6+
Usage
-c --config Configuration file (json format)
default: ./rbot.cfg
-d --debug Turn on debug logging
default: False
-l --logpath Path where the log file output is written
default: None
-b --background Fork to a daemon process
default: False
Authors:
bear Mike Taylor <bear@mozilla.com>
"""
import sys, os
import imp
import logging
from multiprocessing import Process, Queue, get_logger, log_to_stderr
from Queue import Empty
from irc import initOptions, initLogs, rbot
log = get_logger()
ircQueue = Queue()
ircModules = {}
ircCommands = {}
ircFilters = []
def loadModules(options):
filenames = []
for filename in os.listdir(options.modules):
if filename.endswith('.py') and not filename.startswith('_'):
filenames.append(os.path.join(options.modules, filename))
for filename in filenames:
fname = os.path.basename(filename)[:-3]
q = Queue()
p = Process(target=handleModule, name=fname, args=(fname, filename, q, ircQueue, options))
ircModules[fname] = { 'process': p,
'queue': q,
}
p.start()
def handleModule(moduleName, filename, qMsg, qIRC, options):
log.info('initializing modue %s [%s]' % (moduleName, filename))
try:
commands = {}
filters = []
module = imp.load_source(moduleName, filename)
if hasattr(module, 'setup'):
log.info('calling setup for %s' % moduleName)
module.setup(self)
for item, obj in vars(module).iteritems():
if hasattr(obj, 'commands'):
for cmd in obj.commands:
log.info('registering command %s' % cmd)
qIRC.put(('command', moduleName, cmd))
commands[cmd] = obj
if hasattr(obj, 'filters'):
for cmd in obj.filters:
log.info('registering filter %s' % cmd)
qIRC.put(('filter', moduleName))
filters.append(obj)
except:
module = None
log.error('Unable to load module %s' % moduleName, exc_info=True)
qIRC.put(('module', 'remove', moduleName))
if module is not None:
while True:
try:
item = qMsg.get(False)
except Empty:
item = None
if item is not None:
cmd, msg, sender, channel, private = item
log.info('processing %s' % cmd)
if cmd == 'filter':
for func in filters:
func(msg, sender, channel, private, qIRC)
else:
commands[cmd](msg, sender, channel, private, qIRC)
def processMessage(msg, sender, channel, private, irc):
log.info(u'%s:%s %s' % (channel, sender, msg))
args = msg.split(' ', 1)
cmd = None
if args[0].startswith(irc.trigger):
cmd = args[0][1:]
body = ' '.join(args[1:])
else:
if (len(args) > 1) and (args[0] == irc.nickname):
cmd = args[1]
body = ' '.join(args[2:])
if cmd is not None:
if cmd in ircCommands:
mod = ircCommands[cmd]
log.info('send msg to module %s for command %s' % (mod, cmd))
ircModules[mod]['queue'].put((cmd, ' '.join(args[1:]), sender, channel, private))
else:
for mod in ircFilters:
log.info('send msg to module %s for filter' % mod)
ircModules[mod]['queue'].put(('filter', msg, sender, channel, private))
_defaultOptions = { 'config': ('-c', '--config', './rbot.cfg', 'Configuration file'),
'debug': ('-d', '--debug', True, 'Enable Debug', 'b'),
'background': ('-b', '--background', False, 'daemonize ourselves', 'b'),
'logpath': ('-l', '--logpath', None, 'Path where log file is to be written'),
'modules': ('-m', '--modules', './modules', 'Path where bot modules are found'),
}
if __name__ == "__main__":
options = initOptions(_defaultOptions)
initLogs(options)
log.info('Starting')
loadModules(options)
ircBot = rbot(options, cb=processMessage)
ircBot.start()
while ircBot.active:
ircBot.process()
try:
msg = ircQueue.get(False)
except Empty:
msg = None
if msg is not None:
if msg[0] == 'irc':
ircBot.tell(msg[1], msg[2])
elif msg[0] == 'command':
log.info('registering %s %s' % (msg[0], msg[2]))
ircCommands[msg[2]] = msg[1]
elif msg[0] == 'filter':
log.info('registering %s %s' % (msg[0], msg[1]))
ircFilters.append(msg[1])