-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpluginDriver.py
More file actions
135 lines (106 loc) · 4.13 KB
/
pluginDriver.py
File metadata and controls
135 lines (106 loc) · 4.13 KB
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
# pluginDriver.py
# (C) 2013 jtRIPper
#
# 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 1, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
import sys
import os
import re
import pickle
import importlib
class pluginDriver:
def __init__(self):
self.plugins = {}
self.auto_run = {}
def unload_plugin(self, plugin_name, bot=None, buffer=None):
if plugin_name in self.plugins:
if plugin_name in self.auto_run:
self.auto_run.pop(plugin_name)
if hasattr(self.plugins[plugin_name], 'unload'):
self.plugins[plugin_name].unload()
del sys.modules['plugins.' + plugin_name]
del self.plugins[plugin_name]
if bot:
bot.msg(buffer.to, "Unloaded: %s." % plugin)
else:
if bot:
bot.msg(buffer.to, "Module not loaded.")
def load_plugin(self, plugin_name, bot=None, buffer=None):
if plugin_name == '__init__':
return
if os.path.isfile("plugins/%s.py" % (plugin_name)):
if plugin_name in self.plugins:
self.unload_plugin(plugin_name)
plugin = importlib.import_module('plugins.' + plugin_name)
plugin = getattr(plugin, plugin_name)()
self.plugins[plugin_name] = plugin
if hasattr(plugin, "autorun"):
self.auto_run[plugin_name] = getattr(plugin, "autorun")
if bot:
bot.msg(buffer.to, "Loaded: " + plugin_name)
else:
if bot:
bot.msg(buffer.to, "Plugin does not exist.")
def load_plugins(self):
for plugin in os.listdir('plugins'):
if not re.search(".py$", plugin):
continue
self.load_plugin(re.sub("\.py$", "", plugin))
def unload_plugins(self):
plugins = [ plugin for plugin in self.plugins ]
for plugin in plugins:
self.unload_plugin(plugin)
def meta_commands(self, buffer, auth_level, args, command, bot):
if auth_level != 10:
return
if args[0] == "plugin.load" or args[0] == "plugin.reload":
self.load_plugin(args[1], bot, buffer)
elif args[0] == "plugin.unload":
self.unload_plugin(args[1], bot, buffer)
elif args[0] == "plugin.list":
bot.msg(buffer.to, "Loaded plugins:")
for plugin in self.plugins:
bot.msg(buffer.to, " * %s" % plugin)
elif args[0] == "auth.levels":
bot.msg(buffer.to, "Auth levels:")
for level in bot.auth.auth_levels:
bot.msg(buffer.to, " * %s -- %d" % (level, bot.auth.auth_levels[level]))
elif args[0] == "auth.level":
if len(args) == 2:
if bot.auth.auth_levels.has_key(args[1].lower()):
bot.msg(buffer.to, "%s is level %s." % (args[1], bot.auth.auth_levels[args[1].lower()]))
else:
bot.msg(buffer.to, "%s not set." % (args[1]))
elif len(args) == 3:
bot.auth.auth_levels[args[1].lower()] = int(args[2])
bot.msg(buffer.to, "Set %s to level %s." % (args[1], args[2]))
pickle.dump(bot.auth.auth_levels, open("data/auth_%s.p" % bot.hostname, "w"))
def run_command(self, bot, buffer, auth_level, sock):
if not buffer.msg:
return
for plugin in self.auto_run:
self.auto_run[plugin](bot, sock, auth_level, buffer)
args = buffer.msg.split()
command = args[0].split(".")
if len(command) != 2:
return
if command[0] not in self.plugins:
self.meta_commands(buffer, auth_level, args, command, sock)
return
if command[1] not in self.plugins[command[0]].allowed_functions:
self.plugins[command[0]].help(bot, sock, buffer)
return
if self.plugins[command[0]].allowed_functions[command[1]] <= auth_level:
getattr(self.plugins[command[0]], command[1])(bot, sock, buffer)
return