Exemple #1
0
def do_opts(type="console", args=[], *argslist, **kwargs):
    from tl.version import getversion
    if not args: args = sys.argv
    if type != "console": print("TIMELINE %s" % getversion(type.upper()))
    cfg = None
    if type == "irc": target = api_opts + bot_opts + irc_opts
    elif type == "xmpp": target = api_opts + bot_opts + xmpp_opts
    elif type == "fleet": target = api_opts + fleet_opts
    elif type == "init": target = []
    elif type == "console": target = bot_opts + console_opts
    else: target = []
    opts =  make_opts(args, target, *argslist, **kwargs)
    if type == "console": ll = "error"
    else: ll = "warn" 
    if opts.datadir: setdatadir(opts.datadir)
    else: setdatadir(homedir + os.sep + ".tl")
    setloglevel(opts.loglevel or ll)
    if opts.bork: tl.utils.exception.bork = True ; logging.warn("bork mode enabled")
    if opts.nourl: tl.utils.url.enabled = False ; logging.warn("url fetching disabled")
    if opts.nocolors: tl.utils.log.docolor = False ; logging.warn("colors in logging is disabled")
    from tl.lib.users import getusers
    u = getusers()
    if opts.owner and u: u.make_owner(opts.owner)
    from tl.lib.config import getmainconfig
    maincfg = getmainconfig(opts.datadir)
    if type == "irc": cfg = makeircconfig(opts, opts.name)
    elif type == "xmpp" or type == "xmpp": cfg = makexmppconfig(opts, opts.name)
    elif type == "console": cfg = makeconsoleconfig(opts, opts.name)
    else: cfg = makedefaultconfig(opts, opts.name) 
    if opts.anon: cfg.auto_register = True ; cfg.guestasuser = True
    else: cfg.auto_register = False
    if maincfg.dbtype: logging.warn("database type is %s" % maincfg.dbtype)
    return (opts, cfg)
Exemple #2
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 tl.lib.datadir import getdatadir, setdatadir
    if ddir: setdatadir(ddir)
    origdir = ddir 
    ddir = getdatadir()
    if not ddir: logging.error("can't determine datadir to boot from") ; raise Exception("can't determine datadir")
    logging.warn("starting !!")
    if not ddir in sys.path: sys.path.append(ddir)
    makedirs(ddir)
    try:
        rundir = ddir + os.sep + "run"
        k = open(rundir + os.sep + 'tl.pid','w')
        k.write(str(os.getpid()))
        k.close()
    except IOError: pass
    try:
        if not umask: checkpermissions(getdatadir(), 0o700) 
        else: checkpermissions(getdatadir(), umask)  
    except: handle_exception()
    from tl.lib.plugins import plugs
    global loaded
    global cmndtable
    global retable
    global plugins
    global callbacktable
    global shorttable
    global cmndperms
    global timestamps
    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 plugins: plugins = Persist(rundir + os.sep + 'plugins')
    if clear: plugins.data = {}
    if not plugins.data.available: plugins.data.available = []
    if not plugins.data.refused: plugins.data.refused = []
    if not plugins.data.allowed: plugins.data.allowed = []
    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 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)
    packages = find_packages(ddir + os.sep + "myplugs")
    for p in packages:
        if p not in plugin_packages: plugin_packages.append(p)
    packages = find_packages("tl" + os.sep + "plugs")
    for p in packages:
        if p not in plugin_packages: plugin_packages.append(p)
    if os.path.isdir('tl'):
        gotlocal = True
        changed = scandir('tl-myplugs')
        if changed:
            logging.warn("tl-myplugs has changed -=- %s" % ", ".join(changed))
            dosave = True
    for plug in default_plugins: plugs.reload(plug, showerror=True, force=True)
    changed = scandir(ddir + os.sep + 'myplugs')
    if changed:
        logging.warn("myplugs has changed -=- %s" % ', '.join(changed))
        dosave = True
    configchanges = checkconfig()
    if configchanges:
        logging.info("there are configuration changes: %s" % ', '.join(configchanges))
        for f in configchanges:
            if 'mainconfig' in f: force = True ; dosave = True
    if os.path.isdir('tl'):
        coreplugs = scandir("tl" + os.sep + "plugs")
        if coreplugs:
            logging.warn("core changed -=- %s" % ", ".join(coreplugs))
            dosave = True
    try:
        from tl.db import getmaindb
        from tl.db.tables import tablestxt
        db = getmaindb()
        if db: db.define(tablestxt)
    except Exception as ex: logging.warn("could not initialize database %s" % str(ex))
    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)
        saveplugins()
        savecallbacktable()
        savealiases()
    logging.warn(getfullversion(getdatadir()))
    logging.warn("READY")