示例#1
0
def savecmndtable(modname=None, saveperms=True):
    """ save command -> plugin list to db backend. """
    global cmndtable
    if not cmndtable.data: cmndtable.data = {}
    if modname: target = LazyDict(cmndtable.data)
    else: target = LazyDict()
    global cmndperms
    #if not cmndperms.data: cmndperms.data = {}
    from jsb.lib.commands import cmnds
    assert cmnds
    if cmnds.subs:
        for name, clist in cmnds.subs.iteritems():
            if name:
                if clist and len(clist) == 1: target[name] = clist[0].modname
    for cmndname, c in cmnds.iteritems():
        if modname and c.modname != modname or cmndname == "subs": continue
        if cmndname and c:
            target[cmndname] = c.modname
            cmndperms[cmndname] = c.perms
    logging.warn("saving command table")
    assert cmndtable
    assert target
    cmndtable.data = target
    cmndtable.save()
    if saveperms:
        logging.warn("saving command perms")
        cmndperms.save()
示例#2
0
 def dispatch(self, dom):
     """ dispatch a dom to the appropiate handler. """
     res = LazyDict()
     parentname = dom.getName()
     data = dom.getData()
     if data:
         self.final[parentname] = data
         if parentname == 'body': self.final['txt'] = data
     attrs = dom.getAttributes()
     ns = dom.getNamespace()
     res[parentname] = LazyDict()
     res[parentname]['data'] = data
     res[parentname].update(attrs) 
     if ns: res[parentname]['xmlns'] = ns
     for child in dom.getChildren():  
         name = child.getName()
         data = child.getData()
         if data: self.final[name] = data
         attrs = child.getAttributes()
         ns = child.getNamespace()
         res[parentname][name] = LazyDict()
         res[parentname][name]['data'] = data
         res[parentname][name].update(attrs) 
         self.final.update(attrs)
         if ns: res[parentname][name]['xmlns'] = ns
     self.subelements.append(res)
示例#3
0
 def __init__(self, name=None):
     if not self.cfg: raise Exception("sxmpp - config is not set")
     self.cfg.name = name or self.cfg.name
     if not self.cfg.name: raise Exception("bot name is not set in config file %s" % self.cfg.filename)
     self.connection = None
     self.target = None
     self.streamid = None
     self.encoding = "utf-8"
     self.stopped = False
     self.failure = ""
     self.result = LazyDict()
     self.final = LazyDict()
     self.subelements = []
     self.reslist = []
     self.cur = u""
     self.tags = []
     self.features = []
     self.handlers = LazyDict()
     self.addHandler('error', self.handle_error)
     self.addHandler('proceed', self.handle_proceed)
     self.addHandler('message', self.handle_message)
     self.addHandler('presence', self.handle_presence)
     self.addHandler('iq', self.handle_iq)
     self.addHandler('stream', self.handle_stream)
     self.addHandler('stream:stream', self.handle_stream)
     self.addHandler('stream:error', self.handle_streamerror)
     self.addHandler('stream:features', self.handle_streamfeatures)
     self.addHandler('challenge', self.handle_challenge)
     self.addHandler('failure', self.handle_failure)
     self.addHandler('success', self.handle_success)
示例#4
0
文件: web.py 项目: melmothx/jsonbot
def mini(response, input={}):
    """ display start html so that bot output can follow. """
    inputdict = LazyDict({'version': getversion()})
    if input: inputdict.update(input)
    temp = os.path.join(os.getcwd(), 'templates/mini.html')
    outstr = template.render(temp)
    response.out.write(outstr)
示例#5
0
class Plugins(LazyDict):
    """ the plugins object contains all the plugins. """

    loading = LazyDict()

    def size(self):
        return len(self)

    def exit(self):
        todo = cpy(self)
        for plugname in todo:
            self.unload(plugname)

    def reloadfile(self, filename, force=True):
        mlist = filename.split(os.sep)
        mod = []
        for m in mlist[::-1]:
            mod.insert(0, m)
            if m == "myplugs": break
        modname = ".".join(mod)[:-3]
        if not isenabled(modname):
            logging.warn("%s is not enabled. not loading" % modname)
            return
        from boot import plugblacklist
        if modname in plugblacklist.data:
            logging.warn("%s is in blacklist .. not loading." % modname)
            return
        logging.debug("using %s" % modname)
        logging.info("reloading %s" % filename)
        try:
            self.reload(modname, force)
        except RequireError, ex:
            logging.info(str(ex))
示例#6
0
def handle_forget(bot, event):
    """" arguments: <item> and <matchstring> - set an information item. """
    if not event.rest:
        event.missing("<item> and <match>")
        return
    try:
        (what, match) = event.rest.split(" and ", 2)
    except ValueError:
        what = event.rest
        match = None
    what = what.lower()
    items = GlobalPersist("learndb")
    got = False
    if not items.data: items.data = LazyDict()
    if items.data.has_key(what):
        if match == None:
            del items.data[what]
            got = True
        else:
            for i in range(len(items.data[what])):
                if match in items.data[what][i]:
                    del items.data[what][i]
                    got = True
                    break
    if got:
        items.save()
        event.reply("item removed from global database")
    else:
        event.reply("no %s item in global database" % what)
示例#7
0
文件: users.py 项目: Petraea/jsonbot
 def __init__(self, ddir=None, filename=None):
     self.datadir = ddir or getdatadir()
     self.filename = filename or 'mainusers'
     self.udir = self.datadir + os.sep + self.filename
     Persist.__init__(self, self.udir)
     if not self.data: self.data = LazyDict()
     self.data.names = self.data.names or {}
     logging.warn("users object created from %s" % self.udir)
示例#8
0
def savecmndtable(modname=None, saveperms=True):
    """ save command -> plugin list to db backend. """
    global cmndtable
    if not cmndtable.data: cmndtable.data = {}
    if modname: target = LazyDict(cmndtable.data)
    else: target = LazyDict()
    global shorttable
    if not shorttable.data: shorttable.data = {}
    if modname: short = LazyDict(shorttable.data)
    else: short = LazyDict()
    global cmndperms
    from jsb.lib.commands import cmnds
    assert cmnds
    for cmndname, c in cmnds.iteritems():
        if modname and c.modname != modname or cmndname == "subs": continue
        if cmndname and c:
            target[cmndname] = c.modname
            cmndperms[cmndname] = c.perms
            try:
                s = cmndname.split("-")[1]
                if not target.has_key(s):
                    if not short.has_key(s): short[s] = [
                            cmndname,
                    ]
                    if cmndname not in short[s]: short[s].append(cmndname)
            except (ValueError, IndexError):
                pass
    logging.warn("saving command table")
    assert cmndtable
    assert target
    cmndtable.data = target
    cmndtable.save()
    logging.warn("saving short table")
    assert shorttable
    assert short
    shorttable.data = short
    shorttable.save()
    logging.warn("saving RE table")
    for command in cmnds.regex:
        retable.data[command.regex] = command.modname
    assert retable
    retable.save()
    if saveperms:
        logging.warn("saving command perms")
        cmndperms.save()
示例#9
0
class Plugins(LazyDict):

    """ the plugins object contains all the plugins. """

    loading = LazyDict()

    def size(self): return len(self)

    def exit(self):
        todo = cpy(self)
        for plugname in todo:
            self.unload(plugname)         

    def reloadfile(self, filename, force=True):
        mlist = filename.split(os.sep)
        mod = []
        for m in mlist[::-1]:
            mod.insert(0, m)
            if m == "myplugs": break 
        modname = ".".join(mod)[:-3]
        if not isenabled(modname): logging.warn("%s is not enabled. not loading" % modname) ; return
        from boot import plugblacklist
        if modname in plugblacklist.data: logging.warn("%s is in blacklist .. not loading." % modname) ; return
        logging.debug("using %s" % modname)
        logging.info("reloading %s" % filename)
        try: self.reload(modname, force)
        except RequireError, ex: logging.info(str(ex))
   
    def loadall(self, paths=[], force=True):
        """
            load all plugins from given paths, if force is true .. 
            otherwise load all plugins for default_plugins list.

        """
        if not paths: paths = plugin_packages
        imp = None
        old = self.keys()
        new = []
        logging.warn("loading all from paths %s" % paths)
        for module in paths:
            try: imp = _import(module)
            except ImportError, ex:
                #handle_exception()
                logging.warn("no %s plugin package found - %s" % (module, str(ex)))
                continue
            except Exception, ex: handle_exception()
            logging.debug("got plugin package %s" % module)
            try:
                for plug in imp.__plugs__:
                    if "gae" in plug: continue
                    mod = "%s.%s" % (module, plug)
                    if not isenabled(mod): logging.warn("%s is not enabled. not loading" % mod) ; continue
                    try: self.reload(mod, force=force, showerror=True)
                    except RequireError, ex: logging.info(str(ex)) ; continue
                    except KeyError: logging.debug("failed to load plugin package %s" % module) ; continue
                    except Exception, ex: handle_exception() ; continue
                    new.append(mod)
示例#10
0
def handle_overflowsearch(bot, event):
    result = []
    res = search(event.rest, 10)
    nritems = 10
    for aa in res['questions'][:nritems]:
        a = LazyDict(aa)
        if not a.accepted_answer_id: continue
        url = "http://stackoverflow.com/questions/%s" % a.accepted_answer_id
        result.append("%s - %s - %s" % (a.title, ";".join(a.tags), url))
    if result: event.reply("results: ", result, dot=" -=- ")
    else: event.reply("no result found")
示例#11
0
def getaliases(ddir=None, force=True):
    """ return global aliases. """
    global aliases
    if not aliases or force:
        from jsb.lib.persist import Persist
        from jsb.utils.lazydict import LazyDict
        d = ddir or getdatadir()
        p = Persist(d + os.sep + "run" + os.sep + "aliases")
        if not p.data: p.data = LazyDict()
        aliases = p.data
    return aliases
示例#12
0
def savecallbacktable(modname=None):
    """ save command -> plugin list to db backend. """
    if modname: logging.warn("boot - module name is %s" % modname)
    global callbacktable
    assert callbacktable
    if not callbacktable.data: callbacktable.data = {}
    if modname: target = LazyDict(callbacktable.data)
    else: target = LazyDict()
    from jsb.lib.callbacks import first_callbacks, callbacks, last_callbacks, remote_callbacks
    for cb in [first_callbacks, callbacks, last_callbacks, remote_callbacks]:
        for type, cbs in cb.cbs.iteritems():
            for c in cbs:
                if modname and c.modname != modname: continue
                if not target.has_key(type): target[type] = []
                if not c.modname in target[type]:
                    target[type].append(c.modname)
    logging.warn("saving callback table")
    assert callbacktable
    assert target
    callbacktable.data = target
    callbacktable.save()
示例#13
0
文件: core.py 项目: melmothx/jsonbot
 def __init__(self, host, port, name='sxmpp'):
     self.name = name
     self.connection = None
     self.encoding = "utf-8"
     self.stop = False
     self.result = LazyDict()
     self.final = LazyDict()
     self.subelements = []
     self.reslist = []
     self.cur = u""
     self.tags = []
     self.host = host
     self.port = port
     self.handlers = LazyDict()
     self.addHandler('proceed', self.handle_proceed)
     self.addHandler('message', self.handle_message)
     self.addHandler('presence', self.handle_presence)
     self.addHandler('iq', self.handle_iq)
     self.addHandler('stream', self.handle_stream)
     self.addHandler('stream:stream', self.handle_stream)
     self.addHandler('stream:error', self.handle_streamerror)
     self.addHandler('stream:features', self.handle_streamfeatures)
示例#14
0
 def add(self, cmnd, func, perms, threaded=False, wait=False, orig=None, how=None, *args, **kwargs):
     """ add a command. """
     modname = calledfrom(sys._getframe())
     target = Command(modname, cmnd, func, perms, threaded, wait, orig, how)
     self[cmnd] = target
     try:
         c = cmnd.split('-')[1]
         if not self.subs: self.subs = LazyDict()
         if self.subs.has_key(c):
             if not self.subs[c]: self.subs[c] = []
             if target not in self.subs[c]: self.subs[c].append(target)
         else: self.subs[c] = [target, ]
     except IndexError: pass
     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 target not in self.pre[p]: self.pre[p].append(target)
         else: self.pre[p] = [target, ]
     except IndexError: pass
     return self
示例#15
0
def boteventcb(inputdict, request, response):
    #logging.warn(inputdict)
    #logging.warn(dir(request))
    #logging.warn(dir(response))
    body = request.body
    #logging.warn(body)
    payload = json.loads(body)
    try:
        botjson = payload['bot']
        logging.warn(botjson)
        cfg = LazyDict(json.loads(botjson))
        #logging.warn(str(cfg))
        bot = BotFactory().create(cfg.type, cfg)
        logging.warn("created bot: %s" % bot.tojson(full=True))
        eventjson = payload['event']
        #logging.warn(eventjson)
        event = EventBase()
        event.update(LazyDict(json.loads(eventjson)))
        logging.warn("created event: %s" % event.tojson(full=True))
        event.notask = True
        bot.doevent(event)
    except Exception, ex:
        handle_exception()
示例#16
0
文件: plus.py 项目: Petraea/jsonbot
def getplus(target):
    credentials = _import_byfile(
        "credentials",
        getdatadir() + os.sep + "config" + os.sep + "credentials.py")
    url = "https://www.googleapis.com/plus/v1/people/%s/activities/public?alt=json&pp=1&key=%s" % (
        target, credentials.googleclient_apikey)
    result = geturl2(url)
    data = json.loads(result)
    res = []
    for item in data['items']:
        i = LazyDict(item)
        res.append("%s - %s - %s" %
                   (i.actor['displayName'], i['title'], item['url']))
    return res
示例#17
0
 def add(self,
         cmnd,
         func,
         perms,
         threaded=False,
         wait=False,
         orig=None,
         how=None,
         speed=None,
         regex=False,
         needcc=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,
                      needcc=needcc)
     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
示例#18
0
文件: web.py 项目: melmothx/jsonbot
def login(response, input={}):
    """ display start html so that bot output can follow. """
    try:
        host = socket.gethostname()
    except AttributeError:
        if os.environ.get('HTTP_HOST'): host = os.environ['HTTP_HOST']
        else: host = os.environ['SERVER_NAME']
    template = LazyDict({
        'version': getversion(),
        'host': host,
        'color': Config().color or "#C54848"
    })
    if input: template.update(input)
    temp = os.path.join(os.getcwd(), 'templates/login.html')
    outstr = template.render(temp)
    response.out.write(outstr)
示例#19
0
 def resume(self, sessionfile, exclude=[]):
     """ resume bot from session file. """
     session = json.load(open(sessionfile, 'r'))
     chan = session.get("channel")
     for name, cfg in session['bots'].iteritems():
         dont = False
         for ex in exclude:
             if ex in name: dont = True
         if dont: continue
         cfg = LazyDict(cfg)
         try: 
             if not cfg.disable:
                 logging.info("resuming %s" % cfg)
                 start_new_thread(self.resumebot, (cfg, chan))
         except: handle_exception() ; return
     time.sleep(10)
     self.startok.set()
示例#20
0
def handle_learnchan(bot, event):
    """" arguments: <item> is <description> - set an information item. """
    if not event.rest:
        event.missing("<item> is <description>")
        return
    try:
        (what, description) = event.rest.split(" is ", 1)
    except ValueError:
        event.missing("<item> is <description>")
        return
    what = what.lower()
    items = PlugPersist(event.channel)
    if not items.data: items.data = LazyDict()
    if not items.data.has_key(what): items.data[what] = []
    if description not in items.data[what]:
        items.data[what].append(description)
    items.save()
    event.reply("%s item added to %s database" % (what, event.channel))
示例#21
0
def handle_forget(bot, event):
    """" set an information item. """
    if not event.rest:
        event.missing("<item> and <match>")
        return
    try:
        (what, match) = event.rest.split(" and ", 2)
    except ValueError:
        event.missing("<item> and <match>")
        return
    what = what.lower()
    items = PlugPersist(event.channel)
    if not items.data: items.data = LazyDict()
    if items.data.has_key(what):
        for i in range(len(items.data[what])):
            if match in items.data[what][i]:
                del items.data[what][i]
                items.save()
                break
    event.reply("item removed from %s database" % event.channel)
示例#22
0
文件: event.py 项目: Petraea/jsonbot
 def parsesocket(self, handler, message):
     """ parse request/response into a WebEvent. """
     self.handler = handler
     try: data = LazyDict(json.loads(message))
     except Exception, ex: logging.error("failed to parse data: %s - %s" % (message, str(ex))) ; return self
     logging.info("incoming: %s" % message)
     self.div = data.target
     self.userhost = tornado.escape.xhtml_escape(handler.current_user)
     if not self.userhost: raise Exception("no current user.")
     self.isweb = True
     input = data.cmnd
     self.how = data.how or self.how
     logging.warn("cmnd is %s - how is %s" % (input, self.how))
     self.txt = fromenc(input.strip(), self.bot.encoding)
     self.nick = self.userhost.split("@")[0]
     self.auth = fromenc(self.userhost)
     self.stripped = stripped(self.auth)
     self.channel = stripped(self.userhost)
     logging.debug(u'tornado - parsed - %s - %s' % (self.txt, self.userhost)) 
     self.prepare()
     return self
示例#23
0
def sync():
    target = ";".join(state.data.watch)
    if not target:
        logging.warn("no channels started yet")
        return
    res = gettimeline(target)
    if not res:
        logging.warn("no result from %s" % id)
        return
    todo = []
    for r in res:
        a = LazyDict(r)
        logging.debug("got %s" % a.tojson())
        if a.creation_date not in state.data.seen:
            state.data.seen.insert(0, a.creation_date)
            todo.append(a)
        #todo.append(a)
    state.data.seen = state.data.seen[:100]
    state.save()
    logging.info("returned %s items" % len(todo))
    return todo
示例#24
0
 def get(self):
     try:
         cont = self.request.get('continue')
         logging.info('openid - login form %s' % cont)
         urlstring = self.create_openid_url(cont)
         template_values = LazyDict({
             'continue': cont,
             'urlstring': urlstring,
             'appname': getversion()
         })
         try:
             host = socket.gethostname()
         except AttributeError:
             if os.environ.get('HTTP_HOST'): host = os.environ['HTTP_HOST']
             else: host = os.environ['SERVER_NAME']
         inputdict = {'version': getversion(), 'host': host}
         template_values.update(inputdict)
         temp = os.path.join(os.getcwd(), 'templates/login.html')
         outstr = template_values.render(temp)
         self.response.out.write(outstr)
     except Exception, ex:
         handle_exception()
示例#25
0
 def parse(self, request, response):
     """ parse incoming request/response into a XMPPEvent. """
     self.copyin(LazyDict(request.POST))
     (userhost, user, u, nick) = checkuser(response, request)
     self.userhost = self['from']
     self.origin = self.channel
     if user: self.auth = user.email()
     else: self.auth = stripped(self.userhost)
     logging.info('xmpp - auth is %s' % self.auth)
     self.resource = resource(self['from'])
     self.jid = self['from']
     self.to = stripped(self['to'])
     self.channel = stripped(self.userhost)
     self.stripped = stripped(self.userhost)
     self.nick = self.stripped.split("@")[0]
     self.origin = self.channel
     input = self.body or self.stanza
     input = input.strip()
     self.origtxt = input
     self.txt = input
     self.usercmnd = self.txt.split()[0].lower()
     logging.debug(u'xmpp - in - %s - %s' % (self.userhost, self.txt))
     return self
示例#26
0
def makeoptions(ievent, options={}):
    options = LazyDict(options)
    try:
        optargs = ""
        optlist = []
        if not options.has_key('--filter'):
            options['--filter'] = ""
        if not options.has_key('--to'):
            options['--to'] = None
        if not options.has_key('--chan'):
            options['--chan'] = ievent.channel
        if not options.has_key('--how'):
            options['--how'] = "msg"
        if not options.has_key('--speed'):
            options['--speed'] = str(ievent.speed)
        else:
            options['--speed'] = str(options['--speed'])
        for i, j in options.iteritems():
            if '--' in i:
                optlist.append("%s=" % i[2:])
                if j:
                    optlist.append(j)
                continue
            if '-' in i:
                if j:
                    optargs += ":%s" % i[1:]
                else:
                    optargs += i[1:]
        args = ievent.txt.split()
        try:
            (opts, rest) = getopt.getopt(args[1:], optargs, optlist)
        except AttributeError, ex:
            print "option not allowed: %s" % str(ex), ievent.txt, options
            return 0
        except getopt.GetoptError, ex:
            return 0
示例#27
0
 def handle_streamfeatures(self, data):
     """ default stream features handler. """
     logging.debug("%s - STREAMFEATURES: %s" % (self.cfg.name, LazyDict(data).dump()))
示例#28
0
def handle_overflowanswers(bot, event):
    result = []
    for aa in getanswers(event.rest):
        a = LazyDict(aa)
        result.append("%s - %s" % (a.owner['display_name'], striphtml(a.body)))
    event.reply("answers for %s: " % event.rest, result)
示例#29
0
 def __init__(self):
     self.stats = StatDict()
     self.times = LazyDict()
     self.wait = LazyDict()
     self.warned = LazyDict()
示例#30
0
 def __init__(self, name, *args, **kwargs):
     UserState.__init__(self, name, "todo", *args, **kwargs)
     if self.data.list:
         self.data.list = [LazyDict(x) for x in self.data.list]
     else:
         self.data.list = []