|
| 1 | +#!/usr/bin/env python2.7 |
| 2 | +# |
| 3 | +# |
| 4 | + |
| 5 | +""" Console Bot. """ |
| 6 | + |
| 7 | + |
| 8 | +#import warnings |
| 9 | +#warnings.simplefilter("ignore") |
| 10 | + |
| 11 | +## bootstrap |
| 12 | + |
| 13 | +import os, sys |
| 14 | +sys.path.insert(0, os.getcwd()) |
| 15 | +sys.path.insert(0, os.getcwd() + os.sep + "..") |
| 16 | + |
| 17 | + |
| 18 | +## etc |
| 19 | + |
| 20 | +from jsb.version import getversion |
| 21 | + |
| 22 | +from jsb.utils.log import setloglevel |
| 23 | + |
| 24 | +## command line parsing |
| 25 | + |
| 26 | +from optparse import OptionParser |
| 27 | +parser = OptionParser(usage='usage: %prog [options] <appid>', version='%prog ' + getversion()) |
| 28 | +parser.add_option('-d', '--datadir', type='string', default=False, dest='datadir', help="datadir to use") |
| 29 | +parser.add_option('-c', '--channel', type='string', default=False, dest='channel', help="channel to operate on") |
| 30 | +parser.add_option('-n', '--name', type='string', default=False, dest='name', help="name of the console bot") |
| 31 | +parser.add_option('-l', '--loglevel', type='string', default=False, dest='loglevel', help="logging level") |
| 32 | +parser.add_option('', '--nocbs', type='string', default=False, dest='nocbs', help="callback types to exclude from running") |
| 33 | +parser.add_option('-f', '--fast', action="store_true", default=False, dest='fast', help="boot fast (dont load myplugs)") |
| 34 | +parser.add_option('', '--nocolors', action="store_true", default=False, dest='nocolors', help="enable the use of colors") |
| 35 | +parser.add_option('', '--fleet', action="store_true", default=False, dest='fleet', help="start the fleet") |
| 36 | +parser.add_option('', '--nourl', action="store_true", default=False, dest='nourl', help="disable geturl functionality") |
| 37 | +parser.add_option('', '--lazy', action="store_true", default=False, dest='lazy', help="enable lazy importing") |
| 38 | + |
| 39 | +opts, args = parser.parse_args() |
| 40 | +opts.args = args |
| 41 | + |
| 42 | +if not opts.args: print getversion('CONSOLE') |
| 43 | +if opts.lazy: |
| 44 | + from jsb.contrib import lazyimport |
| 45 | + lazyimport.install() |
| 46 | +if opts.datadir: |
| 47 | + if not os.path.isdir(opts.datadir): os.mkdir(opts.datadir) |
| 48 | + from jsb.lib.datadir import setdatadir |
| 49 | + setdatadir(opts.datadir) |
| 50 | + |
| 51 | +## make config from cmndline options |
| 52 | + |
| 53 | +from jsb.utils.log import setloglevel |
| 54 | +setloglevel(opts.loglevel or "error", not opts.nocolors) |
| 55 | + |
| 56 | +if opts.nourl: |
| 57 | + from jsb.utils.url import url_disable |
| 58 | + url_disable() |
| 59 | + |
| 60 | +from jsb.lib.boot import boot |
| 61 | +if opts.fast: boot(opts.datadir, fast=True) |
| 62 | +else: boot(opts.datadir) |
| 63 | + |
| 64 | +## jsb imports |
| 65 | + |
| 66 | +from jsb.utils.exception import handle_exception |
| 67 | +from jsb.utils.generic import waitforqueue, waitevents |
| 68 | +from jsb.drivers.console.bot import ConsoleBot |
| 69 | +from jsb.lib.config import Config |
| 70 | +from jsb.lib.errors import NoOwnerSet, NoSuchCommand |
| 71 | +from jsb.lib.commands import cmnds |
| 72 | +from jsb.lib.exit import globalshutdown |
| 73 | +from jsb.lib.fleet import getfleet |
| 74 | +from jsb.lib.threads import start_new_thread |
| 75 | +from jsb.lib.datadir import getdatadir |
| 76 | + |
| 77 | +## basic imports |
| 78 | + |
| 79 | +import getpass |
| 80 | +import time |
| 81 | +import logging |
| 82 | + |
| 83 | +## start the bot |
| 84 | +name = opts.name or "default-console" |
| 85 | +cfg = Config("fleet" + os.sep + name + os.sep + 'config') |
| 86 | +if not cfg.owner: cfg.owner = [] |
| 87 | +userid = getpass.getuser() + '@' + cfg.uuid |
| 88 | + |
| 89 | +if userid not in cfg.owner: cfg.owner.append(userid) ; cfg.save() |
| 90 | + |
| 91 | +try: |
| 92 | + bot = ConsoleBot(cfg, nocbs=opts.nocbs) |
| 93 | +except NoOwnerSet: |
| 94 | + print "the owner is not set in %s" % cfg.cfile |
| 95 | + os._exit(1) |
| 96 | + |
| 97 | +if opts.args: |
| 98 | + cmndstring = ";" |
| 99 | + for cmnd in opts.args: |
| 100 | + if "|" in cmnd: break |
| 101 | + cmndstring += u"%s " % unicode(cmnd) |
| 102 | + event = bot.make_event(userid, opts.channel or userid, cmndstring.strip(), showall=True) |
| 103 | + event.directoutput = True |
| 104 | + event.nodispatch = False |
| 105 | + try: event.execute(True) |
| 106 | + except NoSuchCommand, ex: print "no %s command found." % str(ex).strip() |
| 107 | + print "" |
| 108 | + try: |
| 109 | + sys.stdout.close() |
| 110 | + except: pass |
| 111 | + os._exit(0) |
| 112 | + |
| 113 | +print "datadir is %s" % getdatadir() |
| 114 | +bot.start(False) |
| 115 | +if bot.maincfg.dbenable: print "dbtype is %s" % bot.maincfg.dbtype |
| 116 | +fleet = getfleet() |
| 117 | +fleet.addbot(bot) |
| 118 | + |
| 119 | +if opts.fleet: |
| 120 | + bots = fleet.loadall() |
| 121 | + logging.error("starting fleet %s" % fleet.list()) |
| 122 | + start_new_thread(fleet.startall, ()) |
| 123 | + |
| 124 | +bot.startshell() |
| 125 | +globalshutdown() |
0 commit comments