def handle_chanupgrade(bot, event): """ no arguments - upgrade the channel. """ prevchan = event.channel # 0.4.1 if prevchan.startswith("-"): prevchan[0] = "+" prevchan = prevchan.replace("@", "+") prev = Persist(getdatadir() + os.sep + "channels" + os.sep + prevchan) if prev.data: event.chan.data.update(prev.data) ; event.chan.save() ; event.reply("done") else: prevchan = event.channel prevchan = prevchan.replace("-", "#") prevchan = prevchan.replace("+", "@") prev = Persist(getdatadir() + os.sep + "channels" + os.sep + prevchan) if prev.data: event.chan.data.update(prev.data) ; event.chan.save() ; event.reply("done") else: event.reply("can't find previous channel data")
def convertpickle(src, target): import gozerbot.compat.persist p = gozerbot.compat.persist.Persist(src) if p and p.data: pers = Persist(target) if not pers.data: pers.data = {} pers.data.update(p.data) try: pers.save() except TypeError: pers2 = Persist(target) if not pers2.data: pers2.data = {} for item, value in p.data.iteritems(): pers2.data[jsonstring(item)] = value pers2.save()
def todb(self): """ save config to database. """ cp = dict(self) del cp['jsondb'] if not self.jsondb: from jsb.lib.persist import Persist self.jsondb = Persist(self.cfile) self.jsondb.data = cp self.jsondb.save()
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
def savealiases(ddir=None): """ return global aliases. """ global aliases if aliases: logging.warn("saving aliases") 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") p.data = aliases p.save() return aliases
def save(self, attributes=[]): target = {} if attributes: for key in attributes: target[key] = self[key] else: target = cpy(self) targetfile = getdatadir() + os.sep + "containers" + os.sep + str( self.createtime) + "_" + stripname(self.origin) p = Persist(targetfile) p.data = getjson().dumps(target) p.save()
def handle_search(bot, event): if not event.options: event.makeoptions() all = event.options.all res = [] target = event.args if not target: event.missing("<search words seperated by space>") ; return coll = PersistCollection(getdatadir() + os.sep + 'spider' + os.sep + "data") files = coll.filenames(target) if files: for f in files: try: res.append(Persist(f).data.url) except AttributeError, ex: continue objs = coll.search('txt', event.rest) if not objs: objs = coll.objects().values() stats = makestats(objs, target, res) urls = stats_response(stats, target) res.extend(urls) if res: if len(res) < 4 or all: event.reply("found %s urls: " % len(res), res, dot=" -or- ") else: event.reply("found %s urls, use --all for more: " % len(res), res[:3], dot=" -or- ") else: event.reply("no urls found")
def boot(ddir=None, force=False, encoding="utf-8", umask=None, saveperms=True, fast=False, clear=False, loadall=False): """ initialize the bot. """ global plugin_packages try: if os.getuid() == 0: print "don't run the bot as root" os._exit(1) except AttributeError: pass from jsb.lib.datadir import getdatadir, setdatadir if ddir: setdatadir(ddir) origdir = ddir ddir = ddir or getdatadir() if not ddir: logging.error("can't determine datadir to boot from") raise Exception("can't determine datadir") if not ddir in sys.path: sys.path.append(ddir) makedirs(ddir) if os.path.isdir("/var/run/jsb") and botuser() == "jsb": rundir = "/var/run/jsb" else: rundir = ddir + os.sep + "run" try: k = open(rundir + os.sep + 'jsb.pid', 'w') k.write(str(os.getpid())) k.close() except IOError: pass try: reload(sys) sys.setdefaultencoding(encoding) except (AttributeError, IOError): handle_exception() try: if not umask: checkpermissions(getdatadir(), 0700) else: checkpermissions(getdatadir(), umask) except: handle_exception() from jsb.lib.plugins import plugs global loaded global cmndtable global retable global pluginlist global callbacktable global shorttable global cmndperms global timestamps global plugwhitelist global plugblacklist if not retable: retable = Persist(rundir + os.sep + 'retable') if clear: retable.data = {} if not cmndtable: cmndtable = Persist(rundir + os.sep + 'cmndtable') if clear: cmndtable.data = {} if not pluginlist: pluginlist = Persist(rundir + os.sep + 'pluginlist') if clear: pluginlist.data = [] if not callbacktable: callbacktable = Persist(rundir + os.sep + 'callbacktable') if clear: callbacktable.data = {} if not shorttable: shorttable = Persist(rundir + os.sep + 'shorttable') if clear: shorttable.data = {} if not timestamps: timestamps = Persist(rundir + os.sep + 'timestamps') if not plugwhitelist: plugwhitelist = Persist(rundir + os.sep + 'plugwhitelist') if not plugwhitelist.data: plugwhitelist.data = [] if not plugblacklist: plugblacklist = Persist(rundir + os.sep + 'plugblacklist') if not plugblacklist.data: plugblacklist.data = [] if not cmndperms: cmndperms = Config('cmndperms', ddir=ddir) changed = [] gotlocal = False dosave = clear or False maincfg = getmainconfig(ddir=ddir) logging.warn("mainconfig used is %s" % maincfg.cfile) if os.path.isdir('jsb'): gotlocal = True packages = find_packages("jsb" + os.sep + "plugs") pluglist = [x for x in packages if not 'db' in x] for p in pluglist: if p not in plugin_packages: plugin_packages.append(p) for plug in default_plugins: plugs.reload(plug, showerror=True, force=True) changed = scandir(getdatadir() + os.sep + 'myplugs', dbenable=maincfg.dbenable) if changed: logging.debug("myplugs has changed -=- %s" % str(changed)) for plugfile in changed: if "gae" in plugfile: continue try: plugs.reloadfile(plugfile, force=True) except Exception, ex: handle_exception() dosave = True
def boot(ddir=None, force=False, encoding="utf-8", umask=None, saveperms=True, fast=False): """ initialize the bot. """ logging.info("booting ..") from jsb.lib.datadir import getdatadir, setdatadir if ddir: setdatadir(ddir) origdir = ddir ddir = ddir or getdatadir() if not ddir: logging.error("can't determine datadir to boot from") os._exit(1) if not ddir in sys.path: sys.path.append(ddir) makedirs(ddir) if os.path.isdir("/var/run/jsb") and botuser() == "jsb": rundir = "/var/run/jsb" else: rundir = ddir + os.sep + "run" try: if os.getuid() == 0: print "don't run the bot as root" os._exit(1) except AttributeError: pass try: k = open(rundir + os.sep + 'jsb.pid', 'w') k.write(str(os.getpid())) k.close() except IOError: pass try: if not ongae: reload(sys) sys.setdefaultencoding(encoding) except (AttributeError, IOError): pass try: if not umask: checkpermissions(getdatadir(), 0700) else: checkpermissions(getdatadir(), umask) except: handle_exception() global loaded global cmndtable global pluginlist global callbacktable global cmndperms global plugcommands if not cmndtable: cmndtable = Persist(rundir + os.sep + 'cmndtable') if not pluginlist: pluginlist = Persist(rundir + os.sep + 'pluginlist') if not callbacktable: callbacktable = Persist(rundir + os.sep + 'callbacktable') if not cmndperms: cmndperms = Config('cmndperms', ddir=ddir) from jsb.lib.plugins import plugs if not cmndtable.data or force: plugs.loadall(plugin_packages, force=True) loaded = True savecmndtable(saveperms=saveperms) if not pluginlist.data or force: if not loaded: plugs.loadall(plugin_packages, force=True) loaded = True savepluginlist() if not callbacktable.data or force: if not loaded: plugs.loadall(plugin_packages, force=True) loaded = True savecallbacktable() if not loaded: logging.info("boot - plugins not loaded .. loading defaults") for plug in default_plugins: plugs.reload(plug, showerror=True, force=True) if not fast: if ongae: plugs.loadall(["myplugs.gae", "jsb.plugs.myplugs"], force=True) else: plugs.loadall(["myplugs.socket", "jsb.plugs.myplugs"], force=True) else: logging.error("skipped loading of myplugs") logging.warn("boot - done")
def fromdb(self): """ read config from database. """ from jsb.lib.persist import Persist tmp = Persist(self.cfile) logging.debug("fromdb - %s - %s" % (self.cfile, tmp.data.tojson())) self.update(tmp.data)
def getaliases(): from jsb.lib.persist import Persist p = Persist(getdatadir() + os.sep + "aliases") if not p.data: p.data = {} return p