forked from errbotio/errbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errBot.py
271 lines (238 loc) · 10.6 KB
/
errBot.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/python
# -*- coding: utf-8 -*-
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import inspect
import logging
import os
import shelve
import shutil
import subprocess
from tarfile import TarFile
from urllib2 import urlopen
from config import BOT_DATA_DIR, BOT_ADMINS
from jabberbot import JabberBot, botcmd
from plugin_manager import get_all_active_plugin_names, activate_plugin, deactivate_plugin, activate_all_plugins, deactivate_all_plugins, update_plugin_places, init_plugin_manager
from utils import get_jid_from_message, PLUGINS_SUBDIR, human_name_for_git_url
from repos import KNOWN_PUBLIC_REPOS
PLUGIN_DIR = BOT_DATA_DIR + os.sep + PLUGINS_SUBDIR
def get_class_that_defined_method(meth):
obj = meth.im_self
for cls in inspect.getmro(meth.im_class):
if meth.__name__ in cls.__dict__: return cls
return None
def admin_only(mess):
if mess.getType() == 'groupchat':
raise Exception('You cannot administer the bot from a chatroom, message the bot directly')
usr = get_jid_from_message(mess)
if usr not in BOT_ADMINS:
raise Exception('You cannot administer the bot from this user %s.' % usr)
class ErrBot(JabberBot):
""" Commands related to the bot administration """
MSG_ERROR_OCCURRED = 'Computer says nooo. '
MSG_UNKNOWN_COMMAND = 'Unknown command: "%(command)s". '
shelf = shelve.DbfilenameShelf(BOT_DATA_DIR + os.sep + 'core.db')
def add_repo(self, name, url):
repos = self.shelf.get('repos', {})
repos[name] = url
self.shelf['repos'] = repos
self.shelf.sync()
# this will load the plugins the admin has setup at runtime
def update_dynamic_plugins(self):
update_plugin_places([PLUGIN_DIR + os.sep + d for d in self.shelf.get('repos', {}).keys()])
def __init__(self, username, password, res=None, debug=False,
privatedomain=False, acceptownmsgs=False, handlers=None):
JabberBot.__init__(self, username, password, res, debug, privatedomain, acceptownmsgs, handlers)
self.update_dynamic_plugins()
def callback_message(self, conn, mess):
for cls in ErrBot.__bases__:
if hasattr(cls, 'callback_message'):
try:
cls.callback_message(self, conn, mess)
except:
logging.exception("Probably a type error")
def activate_non_started_plugins(self):
logging.info('Activating all the plugins...')
activate_all_plugins()
logging.info('Refreshing command list...')
self.refresh_command_list()
def signal_connect_to_all_plugins(self):
for cls in ErrBot.__bases__:
if hasattr(cls, 'callback_connect'):
try:
cls.callback_connect(self)
except:
logging.exception("callback_connect failed for %s" % cls)
def connect(self):
if not self.conn:
self.conn = JabberBot.connect(self)
self.activate_non_started_plugins()
logging.info('Notifying connection to all the plugins...')
self.signal_connect_to_all_plugins()
logging.info('Plugin activation done.')
return self.conn
def shutdown(self):
logging.info('Shutting down... deactivating all the plugins.')
deactivate_all_plugins()
self.shelf.close()
logging.info('Bye.')
@botcmd
def status(self, mess, args):
""" If I am alive I should be able to respond to this one
"""
return 'I am alive with those plugins :\n' + '\n'.join(get_all_active_plugin_names())
@botcmd
def restart(self, mess, args):
""" restart the bot """
admin_only(mess)
self.quit(-1337)
return "I'm restarting..."
@botcmd
def load(self, mess, args):
"""load a plugin"""
admin_only(mess)
result = activate_plugin(args)
self.refresh_command_list()
return result
@botcmd
def unload(self, mess, args):
"""unload a plugin"""
admin_only(mess)
result = deactivate_plugin(args)
self.refresh_command_list()
return result
@botcmd
def reload(self, mess, args):
"""reload a plugin"""
admin_only(mess)
result = deactivate_plugin(args) + " / " + activate_plugin(args)
self.refresh_command_list()
return result
@botcmd
def install(self, mess, args):
""" install a plugin repository from the given source or a known public repo (see !repos to find those).
for example from a known repo : !install err-codebot
for example a git url : git@github.com:gbin/plugin.git
or an url towards a tar.gz archive : http://www.gootz.net/plugin-latest.tar.gz
"""
admin_only(mess)
if not args.strip():
return "You should have an urls/git repo argument"
if args in KNOWN_PUBLIC_REPOS:
args = KNOWN_PUBLIC_REPOS[args][0] # replace it by the url
if args.endswith('tar.gz'):
tar = TarFile(fileobj=urlopen(args))
tar.extractall(path= PLUGIN_DIR)
human_name = args.split('/')[-1][:-7]
else:
human_name = human_name_for_git_url(args)
p = subprocess.Popen(['git', 'clone', args, human_name], cwd = PLUGIN_DIR, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
feedback = p.stdout.read()
error_feedback = p.stderr.read()
if p.wait():
return "Could not load this plugin : \n%s\n---\n%s" % (feedback, error_feedback)
self.add_repo(human_name, args)
self.send(mess.getFrom(), "A new plugin repository named %s has been installed correctly from %s. Refreshing the plugins commands..." % (human_name, args), message_type=mess.getType())
self.update_dynamic_plugins()
self.activate_non_started_plugins()
return "Plugin reload done."
@botcmd
def uninstall(self, mess, args):
""" uninstall a plugin repository by name.
"""
admin_only(mess)
if not args.strip():
return "You should have a repo name as argument"
repos = self.shelf.get('repos', {})
if not repos.has_key(args):
return "This repo is not installed check with !repos the list of installed ones"
shutil.rmtree(PLUGIN_DIR + os.sep + args)
repos.pop(args)
self.shelf['repos'] = repos
self.shelf.sync()
self.quit(-1337)
return "Done, restarting"
@botcmd
def repos(self, mess, args):
""" list the current active plugin repositories
"""
max_width = max([len(name) for name,(_,_) in KNOWN_PUBLIC_REPOS.iteritems()])
answer = 'Public repos : \n' + '\n'.join(['%s %s'%(name.ljust(max_width), desc) for name,(url,desc) in KNOWN_PUBLIC_REPOS.iteritems()])
answer += '\n' + '-'* 40 + '\n\nInstalled repos :\n'
repos = self.shelf.get('repos', {})
if not len(repos):
answer += 'No plugin repo has been installed, use !install to add one.'
return answer
max_width = max([len(item[0]) for item in repos.iteritems()])
answer+= '\n'.join(['%s -> %s' % (item[0].ljust(max_width), item[1]) for item in repos.iteritems()])
return answer
@botcmd
def help(self, mess, args):
""" Returns a help string listing available options.
Automatically assigned to the "help" command."""
if not args:
description = 'Available commands:'
clazz_commands = {}
for (name, command) in self.commands.iteritems():
clazz = get_class_that_defined_method(command)
commands = clazz_commands.get(clazz, [])
commands.append((name, command))
clazz_commands[clazz] = commands
usage = ''
for clazz in sorted(clazz_commands):
usage += '\n\n%s: %s\n' % (clazz.__name__, clazz.__doc__ or '')
usage += '\n'.join(sorted([
'\t!%s: %s' % (name, (command.__doc__ or\
'(undocumented)').strip().split('\n', 1)[0])
for (name, command) in clazz_commands[clazz] if name != 'help' and not command._jabberbot_command_hidden
]))
usage += '\n\n'
else:
super(ErrBot, self).help(mess,args)
top = self.top_of_help_message()
bottom = self.bottom_of_help_message()
return ''.join(filter(None, [top, description, usage, bottom]))
@botcmd
def update(self, mess, args):
""" update the bot and/or plugins
use : !update all
to update everything
or : !update core
to update only the core
or : !update repo_name repo_name ...
to update selectively some repos
"""
admin_only(mess)
directories = set()
args = args.split(' ')
repos = self.shelf.get('repos', {})
if 'all' in args or 'core' in args:
directories.add(os.path.dirname(__file__))
if 'all' in args:
directories.update([PLUGIN_DIR+os.sep+name for name in repos])
else:
directories.update([PLUGIN_DIR+os.sep+name for name in set(args).intersection(set(repos))])
for d in directories:
self.send(mess.getFrom(), "I am updating %s ..." % d , message_type=mess.getType())
p = subprocess.Popen(['git', 'pull'], cwd=d, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
feedback = p.stdout.read() + '\n' + '-'*50 + '\n'
err = p.stderr.read().strip()
if err:
feedback += err + '\n' + '-'*50 + '\n'
if p.wait():
self.send(mess.getFrom(), "Update of %s failed...\n\n%s\n\n resuming..." % (d,feedback) , message_type=mess.getType())
else:
self.send(mess.getFrom(), "Update of %s succeeded...\n\n%s\n\n" % (d,feedback) , message_type=mess.getType())
self.quit(-1337)
return "Done, restarting"