/usr/share/pyshared/jsb/lib/commands.py is in jsonbot 0.84.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 269 270 271 272 273 274 275 276 277 278 279 280 281 | # jsb/commands.py
#
#
"""
the commands module provides the infrastructure to dispatch commands.
commands are the first word of a line.
"""
## jsb imports
from threads import start_new_thread, start_bot_command
from jsb.utils.xmpp import stripped
from jsb.utils.trace import calledfrom, whichmodule
from jsb.utils.exception import handle_exception
from jsb.utils.lazydict import LazyDict
from errors import NoSuchCommand, NoSuchUser
from persiststate import UserState
from runner import cmndrunner
from boot import getcmndperms
from floodcontrol import floodcontrol
from aliases import getaliases, aliascheck
## basic imports
import logging
import sys
import types
import os
import copy
import time
import re
## defines
cpy = copy.deepcopy
## Command class
class Command(LazyDict):
""" a command object. """
def __init__(self, modname, cmnd, func, perms=[], threaded=False, wait=False, orig=None, how=None, speed=None):
LazyDict.__init__(self)
if not modname: raise Exception("modname is not set - %s" % cmnd)
self.modname = cpy(modname)
self.plugname = self.modname.split('.')[-1]
self.cmnd = cpy(cmnd)
self.orig = cpy(orig)
self.func = func
if type(perms) == types.StringType: perms = [perms, ]
self.perms = cpy(perms)
self.plugin = self.plugname
self.threaded = cpy(threaded)
self.wait = cpy(wait)
self.enable = True
self.how = how or "overwrite"
self.regex = None
self.speed = speed
class Commands(LazyDict):
"""
the commands object holds all commands of the bot.
"""
regex = []
def add(self, cmnd, func, perms, threaded=False, wait=False, orig=None, how=None, speed=None, regex=False, *args, **kwargs):
""" add a command. """
modname = calledfrom(sys._getframe())
try: prev = self[cmnd]
except KeyError: prev = None
target = Command(modname, cmnd, func, perms, threaded, wait, orig, how, speed=speed)
if regex:
logging.info("regex command detected - %s" % cmnd)
self.regex.append(target)
target.regex = cmnd
return self
self[cmnd] = target
try:
p = cmnd.split('-')[0]
if not self.pre: self.pre = LazyDict()
if self.pre.has_key(p):
if not self.pre[p]: self.pre[p] = []
if prev in self.pre[p]: self.pre[p].remove(prev)
if target not in self.pre[p]: self.pre[p].append(target)
else: self.pre[p] = [target, ]
except IndexError: pass
return self
def checkre(self, bot, event):
for r in self.regex:
s = re.search(r.cmnd, event.stripcc().strip())
if s:
logging.info("regex matches %s" % r.cmnd)
event.groups = list(s.groups())
return r
def wouldmatchre(self, bot, event, cmnd=""):
groups = self.checkre(bot, event)
if groups: return group
def woulddispatch(self, bot, event):
"""
dispatch an event if cmnd exists and user is allowed to exec this
command.
"""
event.bind(bot)
try:
cmnd = event.stripcc().split()[0]
if event.execstr and not cmnd: cmnd = event.execstr.split()[0]
if not cmnd: cmnd = event.txt.split()[0]
except Exception, ex: logging.debug("can't determine command") ; return None
try:
a = event.chan.data.aliases[cmnd]
if a: cmnd = a.split()[0]
except (KeyError, TypeError):
try:
a = getaliases()[cmnd]
if a: cmnd = a.split()[0]
except (KeyError, TypeError):
if not self.has_key(cmnd):
try:
from boot import shorttable
if shorttable.data.has_key(cmnd):
cmndlist = shorttable.data[cmnd]
if len(cmndlist) == 1: cmnd = cmndlist[0]
else: event.reply("choose one of: ", cmndlist) ; return
except Exception, ex: handle_exception()
logging.info("trying for %s" % cmnd)
result = None
try:
result = self[cmnd]
except KeyError: pass
logging.debug("woulddispatch result: %s" % result)
if result: event.bloh() ; event.makeargs()
return result
def dispatch(self, bot, event, direct=False):
"""
dispatch an event if cmnd exists and user is allowed to exec this
command.
"""
if event.nodispatch: logging.info("nodispatch is set on event") ; return
if event.groupchat and bot.cfg.fulljids: id = event.auth
elif event.groupchat: id = event.auth = event.userhost
else: id = event.auth
if not event.user: event.bind(bot)
if not event.user: raise NoSuchUser(event.userhost)
self.reloadcheck(bot, event)
c = self.woulddispatch(bot, event)
if not c: c = self.checkre(bot, event)
if not c: raise NoSuchCommand(event.usercmnd)
if c.modname in bot.plugs.loading and bot.plugs.loading[c.modname]: event.reply("%s is loading" % c.modname) ; return
if bot.cmndperms and bot.cmndperms[c.cmnd]: perms = bot.cmndperms[c.cmnd]
else: perms = c.perms
if bot.allowall: return self.doit(bot, event, c, direct)
elif not bot.users or bot.users.allowed(id, perms, bot=bot): return self.doit(bot, event, c, direct)
elif bot.users.allowed(id, perms, bot=bot): return self.doit(bot, event, c, direct)
return event
def doit(self, bot, event, target, direct=False):
""" do the dispatching. """
if not target.enable: return
if target.modname in event.chan.data.denyplug:
logging.warn("%s is denied in channel %s - %s" % (target.plugname, event.channel, event.userhost))
return
id = event.auth or event.userhost
event.iscommand = True
event.how = event.how or target.how or "overwrite"
aliascheck(event)
logging.warning('dispatching %s (%s)' % (event.usercmnd, bot.cfg.name))
try:
if bot.isgae:
if not event.notask and (target.threaded or event.threaded) and not event.nothreads:
logging.warn("LAUNCHING AS TASK")
from jsb.drivers.gae.tasks import start_botevent
if target.threaded == "backend": start_botevent(bot, event, "backend")
else: start_botevent(bot, event, target.speed or event.speed)
event.reply("task started for %s" % event.auth)
else: target.func(bot, event)
else:
if direct or event.direct: target.func(bot, event)
elif target.threaded and not event.nothreads:
logging.warning("launching thread for %s (%s)" % (event.usercmnd, bot.cfg.name))
t = start_bot_command(target.func, (bot, event))
event.thread = t
else: event.dontclose = False; cmndrunner.put(target.speed or event.speed, target.modname, target.func, bot, event)
except Exception, ex:
logging.error('%s - error executing %s' % (whichmodule(), str(target.func)))
raise
return event
def unload(self, modname):
""" remove modname registered commands from store. """
delete = []
for name, cmnd in self.iteritems():
if not cmnd: continue
if cmnd.modname == modname: delete.append(cmnd)
for cmnd in delete: cmnd.enable = False
return self
def apropos(self, search):
""" search existing commands for search term. """
result = []
from boot import getcmndtable
for name, plugname in getcmndtable().iteritems():
if search in name: result.append(name)
return result
def perms(self, cmnd):
""" show what permissions are needed to execute cmnd. """
try: return self[cmnd].perms
except KeyError: return []
def whereis(self, cmnd):
""" return plugin name in which command is implemented. """
from boot import getcmndtable
try: return getcmndtable()[cmnd]
except KeyError: return ""
def gethelp(self, cmnd):
""" get the docstring of a command. used for help. """
try: return self[cmnd].func.__doc__
except KeyError: pass
def reloadcheck(self, bot, event, target=None):
"""
check if event requires a plugin to be reloaded. if so
reload the plugin.
"""
from boot import getcmndtable
from boot import plugblacklist
plugloaded = None
plugin = None
target = target or event.usercmnd.lower()
from jsb.lib.aliases import getaliases
aliases = getaliases()
try: target = aliases[target]
except KeyError:
try: target = event.chan.data.aliases[target]
except (AttributeError, KeyError, TypeError): pass
if not getcmndtable().has_key(target):
try:
from boot import shorttable
if shorttable.data.has_key(target):
cmndlist = shorttable.data[target]
if len(cmndlist) == 1: target = cmndlist[0]
except Exception, ex: handle_exception()
if target: target = target.split()[0]
logging.debug("checking for reload of %s" % target)
try:
plugin = getcmndtable()[target]
except KeyError:
try:
from boot import retable
for regex, mod in retable.data.iteritems():
if re.search(regex, event.stripcc() or event.txt): plugin = mod ; break
except Exception, ex: handle_exception()
logging.info("plugin is %s" % plugin)
if not plugin: logging.debug("can't find plugin to reload for %s" % target) ; return
if plugin in bot.plugs: logging.info(" %s already loaded" % plugin) ; return plugloaded
elif plugin in plugblacklist.data: return plugloaded
elif bot.cfg.loadlist and plugin not in bot.cfg.loadlist: logging.warn("plugin %s is blacklisted" % plugin) ; return plugloaded
logging.info("loaded %s on demand" % plugin)
plugloaded = bot.plugs.reload(plugin)
return plugloaded
## global commands
cmnds = Commands()
def size():
return len(cmnds)
|