Example #1
0
 def __init__(self, id, botname=None, type="notset", needexist=False):
     if not id: raise NoChannelSet()
     if not botname: Persist.__init__(self, getdatadir() + os.sep + 'channels' + os.sep + stripname(id), needexist=needexist)
     else: Persist.__init__(self, getdatadir() + os.sep + 'fleet' + os.sep + stripname(botname) + os.sep + 'channels' + os.sep + stripname(id), needexist=needexist)
     self.id = id
     self.type = type
     self.lastmodified = time.time()
     self.data.id = id
     self.data.enable = self.data.enable or False
     self.data.ops = self.data.ops or []
     self.data.taglist = self.data.taglist or []
     self.data.silentcommands = self.data.silentcommands or []
     self.data.allowcommands = self.data.allowcommands or []
     self.data.feeds = self.data.feeds or []
     self.data.forwards = self.data.forwards or []
     self.data.allowwatch = self.data.allowwatch or []
     self.data.watched = self.data.watched or []
     self.data.passwords = self.data.passwords or {}
     self.data.cc = self.data.cc or ""
     self.data.nick = self.data.nick or "jsb"
     self.data.key = self.data.key or ""
     self.data.denyplug = self.data.denyplug or []
     self.data.createdfrom = whichmodule()
     self.data.cacheindex = 0
     self.data.tokens = self.data.tokens or []
     self.data.webchannels = self.data.webchannels or []
Example #2
0
 def __init__(self, name):
     self.name = name
     if not os.path.exists(getdatadir() + os.sep + 'gatekeeper'):
         os.mkdir(getdatadir() + os.sep + 'gatekeeper')
     Persist.__init__(self,
                      getdatadir() + os.sep + 'gatekeeper' + os.sep + name)
     self.data.whitelist = self.data.whitelist or []
Example #3
0
 def __init__(self, name):
     self.name = name
     try: import waveapi
     except:
         if not os.path.exists(getdatadir() + os.sep +'gatekeeper'): os.mkdir(getdatadir() + os.sep + 'gatekeeper')
     Persist.__init__(self, getdatadir() + os.sep + 'gatekeeper' + os.sep + name)
     self.data.whitelist = self.data.whitelist or []
Example #4
0
 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()
Example #5
0
 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()
Example #6
0
 def __init__(self, fname):
     Persist.__init__(self, fname)
     if type(self.data) == types.ListType:
         tmp = {}
         for i in range(self.data):
             tmp[i] = self.data[i]
         self.data = tmp                
     for i in self.data:
         z = Alarmitem(d=self.data[i])
         periodical.addjob(z.time - time.time(), 1, self.alarmsay, z.nick, z)
Example #7
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
Example #8
0
 def __init__(self, fname):
     Persist.__init__(self, fname)
     if type(self.data) == types.ListType:
         tmp = {}
         for i in range(self.data):
             tmp[i] = self.data[i]
         self.data = tmp
     for i in self.data:
         z = Alarmitem(d=self.data[i])
         periodical.addjob(z.time - time.time(), 1, self.alarmsay, z.nick,
                           z)
Example #9
0
 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()
Example #10
0
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
Example #11
0
 def __init__(self, name, url="", owner="", itemslist=['title', 'link'], watchchannels=[], running=1):
     filebase = getdatadir() + os.sep + 'plugs' + os.sep + 'jsb.plugs.wave.hubbub' + os.sep + name
     Persist.__init__(self, filebase + os.sep + name + '.core')
     if not self.data: self.data = {}
     self.data = LazyDict(self.data)
     self.data['name'] = self.data.name or str(name)
     self.data['url'] = self.data.url or str(url)
     self.data['owner'] = self.data.owner or str(owner)
     self.data['watchchannels'] = self.data.watchchannels or list(watchchannels)
     self.data['running'] = self.data.running or running
     self.itemslists = Pdol(filebase + os.sep + name + '.itemslists')
     self.markup = Pdod(filebase + os.sep + name + '.markup')
Example #12
0
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
Example #13
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
Example #14
0
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")
Example #15
0
 def __init__(self, id, botname=None, type="notset"):
     if not id: raise NoChannelSet()
     if not botname: Persist.__init__(self, getdatadir() + os.sep + 'channels' + os.sep + stripname(id))
     else: Persist.__init__(self, getdatadir() + os.sep + 'fleet' + os.sep + botname + os.sep + 'channels' + os.sep + stripname(id))
     self.id = id
     self.type = type
     self.lastmodified = time.time()
     self.data.id = id
     self.data.silentcommands = self.data.silentcommands or []
     self.data.allowcommands = self.data.allowcommands or []
     self.data.feeds = self.data.feeds or []
     self.data.forwards = self.data.forwards or []
     self.data.allowwatch = self.data.allowwatch or []
     self.data.watched = self.data.watched or []
     self.data.passwords = self.data.passwords or {}
     self.data.cc = self.data.cc or "!"
     self.data.nick = self.data.nick or "jsb"
     self.data.key = self.data.key or ""
     self.data.denyplug = self.data.denyplug or []
     self.data.createdfrom = whichmodule()
     self.data.cacheindex = 0
     self.data.tokens = self.data.tokens or []
     self.data.webchannels = self.data.webchannels or []
     logging.debug("channelbase - created channel %s" % id)
Example #16
0
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")
Example #17
0
 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)
Example #18
0
File: alarm.py Project: code2u/jsb
 def __init__(self, fname):
     Persist.__init__(self, fname)
     if not self.data: self.data = []
     for i in self.data:
         z = Alarmitem(d=i)
         periodical.addjob(z.time - time.time(), 1, self.alarmsay, z.nick, z)
Example #19
0
def getaliases():
    from jsb.lib.persist import Persist
    p = Persist(getdatadir() + os.sep + "aliases")
    if not p.data: p.data = {}
    return p
Example #20
0
 def __init__(self, fname):
     Persist.__init__(self, fname)
     if not self.data: self.data = {}
Example #21
0
 def __init__(self, keyname):
     Persist.__init__(
         self,
         getdatadir() + os.sep + 'keys' + os.sep + 'fish' + os.sep +
         stripname(keyname))
Example #22
0
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")
Example #23
0
 def save(self):
     Persist.save(self)
Example #24
0
 def __init__(self, url, txt=None, *args, **kwargs):
     Persist.__init__(self, getdatadir() + os.sep + "spider" + os.sep + "data" + os.sep + stripname(url), *args, **kwargs)
     self.data.url = url
     self.data.txt = txt or self.data.txt or ""
Example #25
0
 def __init__(self, filename):
     Persist.__init__(self, filename)
     if not self.data: self.data = LazyDict()
Example #26
0
 def __init__(self, url):
     self.scantime = 0
     self.url = Url(url)
     self.fname = getdatadir() + os.sep + 'spider' + os.sep + 'stats' + os.sep + stripname(url)
     Persist.__init__(self, self.fname)
Example #27
0
 def save(self):
     Persist.save(self)
Example #28
0
def boot(ddir=None, force=False, encoding="utf-8", umask=None, saveperms=True, fast=False, clear=False):
    """ initialize the bot. """
    try:
        if os.getuid() == 0:
            print "don't run the bot as root"
            os._exit(1)
    except AttributeError:
        pass
    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:
        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
    global timestamps
    global plugwhitelist
    global plugblacklist
    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 timestamps:
        timestamps = Persist(rundir + os.sep + "timestamps")
    if clear:
        timestamps.data = {}
    if not plugwhitelist:
        plugwhitelist = Persist(rundir + os.sep + "plugwhitelist")
    if not plugwhitelist.data or clear:
        plugwhitelist.data = []
    if not plugblacklist:
        plugblacklist = Persist(rundir + os.sep + "plugblacklist")
    if not plugblacklist.data or clear:
        plugblacklist.data = []
    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.common", "myplugs.gae", "jsb.plugs.myplugs.gae", "jsb.plugs.myplugs.common"], force=True
                )
            else:
                plugs.loadall(
                    ["myplugs.common", "myplugs.socket", "jsb.plugs.myplugs.socket", "jsb.plugs.myplugs.common"],
                    force=True,
                )
        else:
            logging.error("skipped loading of myplugs")
    try:
        changed = checktimestamps()
        if changed:
            logging.warn("boot - files changed %s" % str(changed))
            for plugfile in changed:
                plugs.reloadfile(plugfile, force=True)
    except Exception, ex:
        logging.error("boot - can't read myplugs dir.")
Example #29
0
 def __init__(self, filename):
     Persist.__init__(self, filename)
     if not self.data: self.data = LazyDict()
Example #30
0
class Config(LazyDict):

    """ 
        config class is a dict containing json strings. is writable to file 
        and human editable.

    """

    def __init__(self, filename, verbose=False, input={}, ddir=None, nolog=False, *args, **kw):
        assert filename
        LazyDict.__init__(self, input, *args, **kw)
        a = "%s%s" % (os.sep, os.sep)
        filename = filename.replace(a, os.sep)
        self.origname = filename
        self.origdir = ddir or getdatadir()
        self.setcfile(ddir or getdatadir(), filename)
        self.jsondb = None
        if not self._comments: self._comments = {}
        logging.info("fromfile - %s from %s" % (self.origname, whichmodule(2)))
        self.fromfile(self.cfile)
        self.origdir = ddir or getdatadir()
        self.setcfile(ddir or getdatadir(), filename)
        self.init()
        if self.owner: logging.info("owner is %s" % self.owner)
        if not self.has_key("uuid"): self.setuuid()
        if not self.has_key("cfile"): self.cfile = self.setcfile(self.origdir, self.origname) 
        assert self.cfile
        if not self.origdir in self.cfile: raise WrongFileName("%s not in %s" % (self.origdir, self.cfile))

    def setcfile(self, ddir, filename):
        self.filename = filename or 'mainconfig'
        self.datadir = ddir or getdatadir()
        self.dir = self.datadir + os.sep + 'config'
        self.cfile = self.dir + os.sep + filename

    def setuuid(self, save=True):
        logging.debug("setting uuid")
        self.uuid = str(uuid.uuid4())
        if save: self.save()

    def __deepcopy__(self, a):
        """ accessor function. """
        cfg = Config(self.filename, input=self, nolog=True)
        return cfg

    def __getitem__(self, item):
        """ accessor function. """
        if not self.has_key(item): return None
        else: return LazyDict.__getitem__(self, item)

    def merge(self, cfg):
        """ merge in another cfg. """
        self.update(cfg)

    def set(self, item, value):
        """ set item to value. """
        LazyDict.__setitem__(self, item, value)

    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 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 fromfile(self, filename=None):
        """ read config object from filename. """
        curline = ""
        fname = filename or self.cfile
        if not fname: raise Exception(" %s - %s" % (self.cfile, self.dump()))
        if not os.path.exists(fname): logging.warn("config file %s doesn't exist yet" % fname) ; return False 
        comment = ""
        for line in open(fname, 'r'):
            curline = line
            curline = curline.strip()
            if curline == "": continue
            if curline.startswith('#'): comment = curline; continue
            if True:
                try:
                    key, value = curline.split('=', 1)
                    kkey = key.strip()
                    self[kkey] = json.loads(unicode(value.strip()))
                    if comment: self._comments[kkey] = comment 
                    comment = ""
                except ValueError: logging.error("skipping line - unable to parse: %s" % line)
        #self.cfile = fname
        return

    def tofile(self, filename=None, stdout=False):
        """ save config object to file. """
        if not filename: filename = self.cfile
        if not filename: raise Exception("no cfile found  - %s" % whichmodule(3))
        logging.warn("saving %s" %  self.origname)
        if filename.startswith(os.sep): d = [os.sep,]
        else: d = []
        for p in filename.split(os.sep)[:-1]:
            if not p: continue
            d.append(p)
            ddir = os.sep.join(d)
            if not os.path.isdir(ddir):
                logging.debug("persist - creating %s dir" % ddir)
                try: os.mkdir(ddir)
                except OSError, ex:
                    logging.error("persist - not saving - failed to make %s - %s" % (ddir, str(ex)))
                    return
        written = []
        curitem = None
        later = []
        try:
            if stdout: configtmp = sys.stdout
            else: configtmp = open(filename + '.tmp', 'w')
            configtmp.write('# ===========================================================\n#\n')
            configtmp.write("# JSONBOT CONFIGURATION FILE - %s\n" % filename)
            configtmp.write("#\n")
            configtmp.write('# last changed on %s\n#\n' % time.ctime(time.time()))
            configtmp.write("# This file contains configration data for the JSONBOT.\n")
            configtmp.write('# Variables are defined by "name = json value" pairs.\n')
            configtmp.write('# Make sure to use " in strings.\n#\n')
            configtmp.write('# The bot can edit this file!.\n#\n')
            configtmp.write('# ===========================================================\n\n')
            teller = 0
            keywords = self.keys()
            keywords.sort()
            for keyword in keywords:
                value = self[keyword]
                if keyword in written: continue
                if keyword in ['origdir', 'origname', 'issaved', 'blacklist', 'whitelist', 'followlist', 'uuid', 'whitelist', 'datadir', 'name', 'createdfrom', 'cfile', 'filename', 'dir', 'isdb']: later.append(keyword) ; continue
                if keyword == 'jsondb': continue
                if keyword == 'optionslist': continue
                if keyword == 'gatekeeper': continue
                if keyword == "_comments": continue
                if self._comments and self._comments.has_key(keyword):
                    configtmp.write(self._comments[keyword] + u"\n")
                curitem = keyword
                try: configtmp.write('%s = %s\n' % (keyword, json.dumps(value)))
                except TypeError: logging.error("%s - can't serialize %s" % (filename, keyword)) ; continue
                teller += 1
                #configtmp.write("\n")
            configtmp.write('\n\n# ============================================================\n#\n')
            configtmp.write("# bot generated stuff.\n#\n")
            configtmp.write('# ============================================================\n\n')
            for keyword in later:
                if self._comments and self._comments.has_key(keyword):
                    configtmp.write(self._comments[keyword] + u"\n")
                curitem = keyword
                value = self[keyword]
                try: configtmp.write(keyword + " = " + json.dumps(value) + "\n")
                except TypeError: logging.error("%s - can't serialize %s" % (filename, keyword)) ; continue
                teller += 1
                #configtmp.write("\n")
            if not "mainconfig" in filename and self._comments:
                try:
                    configtmp.write('\n\n# ============================================================\n#\n')
                    configtmp.write("# possible other config variables.\n#\n")
                    configtmp.write('# ============================================================\n\n')
                    items = self._comments.keys()
                    keys = self.keys()
                    do = []
                    for var in items:
                        if var not in keys: do.append(var)
                    do.sort()
                    for var in do:
                         configtmp.write(u"# %s -=- %s\n" % (var, self._comments[var]))
                    configtmp.write("\n\n")
                except Exception, ex: handle_exception()
            else: configtmp.write("\n\n# jsonbot can run multiple bots at once. see %s/config/fleet for their configurations.\n\n" % self.origdir)
            if not stdout: 
                configtmp.close()
                os.rename(filename + '.tmp', filename)
            return teller

        except Exception, ex:
            handle_exception()
            logging.error("ERROR WRITING %s CONFIG FILE: %s .. %s" % (self.cfile, str(ex), curitem))
Example #31
0
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
    logging.warn("starting!")
    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 clear: timestamps.data = {}
    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
    configchanges = checkconfig()
    if configchanges:
        logging.info("there are configuration changes: %s" % str(configchanges))
        for f in configchanges:
            if 'mainconfig' in f: force = True ; dosave = True
    if os.path.isdir('jsb'):
        corechanges = scandir("jsb" + os.sep + "plugs", dbenable=maincfg.dbenable)
        if corechanges:
            logging.debug("core changed -=- %s" % str(corechanges))
            for plugfile in corechanges:
                if not maincfg.dbenable and "db" in plugfile: continue
                if "gae" in plugfile: continue
                try: plugs.reloadfile(plugfile, force=True)
                except Exception, ex: handle_exception()
            dosave = True
    if maincfg.dbenable:
        if not 'jsb.plugs.db' in plugin_packages: plugin_packages.append("jsb.plugs.db") 
        try:
            from jsb.db import getmaindb
            from jsb.db.tables import tablestxt
            db = getmaindb()
            if db: db.define(tablestxt)
        except Exception, ex: logging.warn("could not initialize database %s" % str(ex))
    else:
        logging.warn("db not enabled, set dbenable = 1 in %s to enable" % getmainconfig().cfile)
        try: plugin_packages.remove("jsb.plugs.db")
        except ValueError: pass
    if force or dosave or not cmndtable.data or len(cmndtable.data) < 100:
        logging.debug("using target %s" % str(plugin_packages))
        plugs.loadall(plugin_packages, force=True)
        savecmndtable(saveperms=saveperms)
        savepluginlist()
        savecallbacktable()
        savealiases()
    logging.warn("ready")
Example #32
0
 def __init__(self, fname):
     Persist.__init__(self, fname)
     if not self.data: self.data = {}
Example #33
0
def getaliases():
    from jsb.lib.persist import Persist
    p = Persist(getdatadir() + os.sep + "aliases")
    if not p.data: p.data = {}
    return p
Example #34
0
 def save(self):
     """ save the hubbub items data. """
     Persist.save(self)
     self.itemslists.save()
     self.markup.save()
Example #35
0
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()
Example #36
0
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
Example #37
0
 def __init__(self, keyname):
   Persist.__init__(self, getdatadir() + os.sep + 'keys' + os.sep + 'fish' + os.sep + stripname(keyname))