예제 #1
0
파일: exit.py 프로젝트: Petraea/jsonbot
def globalshutdown(exit=True):
    """ shutdown the bot. """
    try:
        try: sys.stdout.write("\n")
        except: pass
        logging.error('shutting down'.upper())
        from fleet import getfleet
        fleet = getfleet()
        if fleet:
            logging.warn('shutting down fleet')
            fleet.exit()
        logging.warn('shutting down plugins')
        from jsb.lib.plugins import plugs
        plugs.exit()
        logging.warn("shutting down runners")
        cmndrunner.stop()
        callbackrunner.stop()
        waitrunner.stop()
        mainsink.stop()
        logging.warn("cleaning up any open files")
        while cleanup(): time.sleep(1)
        try: os.remove('jsb.pid')
        except: pass
        killmcdaemon()
        logging.warn('done')
        print ""
        if exit: os._exit(0)
    except Exception, ex:
        print str(ex)
        if exit: os._exit(1)
예제 #2
0
 def start(self, connect=True, join=True):
     """ start the mainloop of the bot. """
     if self.started: logging.warn("%s - already started" % self.cfg.name) ; return
     tickloop.start(self)
     self.stopped = False
     self.stopreadloop = False
     self.stopoutloop = False
     self.stopeventloop = False
     self.status = "start"
     if not self.eventlooprunning: start_new_thread(self._eventloop, ())
     start_new_thread(self._outloop, ())
     if connect:
         if not self.connect() : return False
         start_new_thread(self._readloop, ())
         self.connectok.wait()
         if self.stopped: logging.warn("bot is stopped") ; return True
         if self.connectok.isSet():
             logging.warn('%s - logged on !' % self.cfg.name)
             if join: start_new_thread(self.joinchannels, ())
             if self.type in ["sxmpp", "xmpp", "sleek"]:
                 start_new_thread(self._keepalive, ())
                 if self.cfg.keepchannelsalive: start_new_thread(self._keepchannelsalive, ())
         elif self.type not in ["console", "base"]: logging.warn("%s - failed to logon - connectok is not set" % self.cfg.name)
     fleet = getfleet()
     fleet.addbot(self)
     self.status == "started"
     self.started = True
     self.dostart(self.cfg.name, self.type)
     return True
예제 #3
0
파일: exit.py 프로젝트: code2u/jsb
def globalshutdown():
    """ shutdown the bot. """
    try:
        logging.warn('shutting down'.upper())
        sys.stdout.write("\n")
        from fleet import getfleet
        fleet = getfleet()
        if fleet:
            logging.warn('shutting down fleet')
            fleet.exit()
        logging.warn('shutting down plugins')
        from jsb.lib.plugins import plugs
        plugs.exit()
        logging.warn("shutting down runners")
        cmndrunner.stop()
        callbackrunner.stop()
        waitrunner.stop()
        logging.warn("cleaning up any open files")
        while cleanup(): time.sleep(1)
        try: os.remove('jsb.pid')
        except: pass
        killmcdaemon()
        time.sleep(0.5)
        logging.warn('done')
        os._exit(0)
    except Exception, ex: handle_exception() ; os._exit(1)
예제 #4
0
 def boot(self, *args, **kwargs):
     if not self.cfg.type: self.cfg.type = self.type ; self.cfg.save()
     fleet = getfleet()
     fleet.addbot(self)
     fleet.addnametype(self.cfg.name, self.type)
     while 1:
         try:
             self.started = False
             if self.start(): break
         except Exception, ex:
             logging.error(str(ex))
             logging.error("sleeping 15 seconds")
             time.sleep(15)       
예제 #5
0
 def boot(self):
     logging.warn("booting %s bot" % self.cfg.name)
     if not self.cfg.type: self.cfg.type = self.type ; self.cfg.save()
     fleet = getfleet()
     fleet.addbot(self)
     fleet.addnametype(self.cfg.name, self.type)
     while 1:
         try:
             #self.exit(close=False, save=False)
             self.started = False
             if self.start(): break
         except Exception, ex:
             logging.error(str(ex))
             logging.error("sleeping 15 seconds")
             time.sleep(15)       
예제 #6
0
 def boot(self, *args, **kwargs):
     if not self.cfg.type:
         self.cfg.type = self.type
         self.cfg.save()
     fleet = getfleet()
     fleet.addbot(self)
     fleet.addnametype(self.cfg.name, self.type)
     while 1:
         try:
             self.started = False
             if self.start(): break
         except Exception, ex:
             logging.error(str(ex))
             logging.error("sleeping 15 seconds")
             time.sleep(15)
예제 #7
0
 def boot(self):
     logging.warn("booting %s bot" % self.cfg.name)
     if not self.cfg.type:
         self.cfg.type = self.type
         self.cfg.save()
     fleet = getfleet()
     fleet.addbot(self)
     fleet.addnametype(self.cfg.name, self.type)
     while 1:
         try:
             #self.exit(close=False, save=False)
             self.started = False
             if self.start(): break
         except Exception, ex:
             logging.error(str(ex))
             logging.error("sleeping 15 seconds")
             time.sleep(15)
예제 #8
0
 def exit(self, stop=True, close=True, save=True, quit=False):
     """ exit the bot. """ 
     logging.warn("%s - exit" % self.cfg.name)
     if stop:
         self.stopped = True   
         self.stopreadloop = True  
         self.stopeventloop = True
         self.stopkeepalive = True
         self.connected = False
         self.started = False
         self.putonqueue(1, None, "")
         self.put(None)
     if close:
         self.shutdown()
     save and self.save()
     fleet = getfleet()
     fleet.remove(self)
     if quit and not fleet.bots: globalshutdown()
예제 #9
0
 def exit(self, stop=True, close=True, save=True, quit=False):
     """ exit the bot. """
     logging.warn("%s - exit" % self.cfg.name)
     if stop:
         self.stopped = True
         self.stopreadloop = True
         self.stopeventloop = True
         self.stopkeepalive = True
         self.connected = False
         self.started = False
         self.putonqueue(1, None, "")
         self.put(None)
     if close:
         self.shutdown()
     save and self.save()
     fleet = getfleet()
     fleet.remove(self)
     if quit and not fleet.bots: globalshutdown()
예제 #10
0
def globalshutdown():
    """ shutdown the bot. """
    logging.warn('shutting down'.upper())
    defaultrunner.stop()
    cmndrunner.stop()
    sys.stdout.write("\n")
    try:
        os.remove('jsb.pid')
    except:
        pass
    from fleet import getfleet
    fleet = getfleet()
    if fleet:
        logging.warn('shutting down fleet')
        fleet.exit()
    logging.warn('shutting down plugins')
    plugs.exit()
    logging.warn('done')
    #print "ltrs!"
    os._exit(0)
예제 #11
0
 def start(self, connect=True, join=True):
     """ start the mainloop of the bot. """
     if self.started:
         logging.warn("%s - already started" % self.cfg.name)
         return
     tickloop.start(self)
     #mainsink.start()
     self.stopped = False
     self.stopreadloop = False
     self.stopoutloop = False
     self.stopeventloop = False
     self.status = "start"
     if not self.eventlooprunning: start_new_thread(self._eventloop, ())
     start_new_thread(self._outloop, ())
     if connect:
         if not self.connect(): return False
         start_new_thread(self._readloop, ())
         self.connectok.wait()
         if self.stopped:
             logging.warn("bot is stopped")
             return True
         if self.connectok.isSet():
             logging.warn('%s - logged on !' % self.cfg.name)
             if join: start_new_thread(self.joinchannels, ())
             if self.type in ["sxmpp", "xmpp", "sleek"]:
                 start_new_thread(self._keepalive, ())
                 if self.cfg.keepchannelsalive:
                     start_new_thread(self._keepchannelsalive, ())
         elif self.type not in ["console", "base"]:
             logging.warn("%s - failed to logon - connectok is not set" %
                          self.cfg.name)
     fleet = getfleet()
     fleet.addbot(self)
     self.status == "started"
     self.started = True
     self.dostart(self.cfg.name, self.type)
     return True
예제 #12
0
파일: botbase.py 프로젝트: melmothx/jsonbot
 def __init__(self, cfg=None, usersin=None, plugs=None, botname=None, nick=None, *args, **kwargs):
     if not botname and cfg and cfg.botname: botname = cfg.botname
     if botname: self.botname = botname
     else: self.botname = u"default-%s" % str(type(self)).split('.')[-1][:-2]
     logging.info("botbase - name is %s" % self.botname)
     self.fleetdir = u'fleet' + os.sep + stripname(self.botname)
     if cfg: self.cfg = Config(self.fleetdir + os.sep + u'config', input=cfg)
     else: self.cfg = Config(self.fleetdir + os.sep + u'config')
     LazyDict.__init__(self)
     self.update(self.cfg)
     self.ignore = []
     self.aliases = getaliases()
     self.curevent = None
     self.inqueue = Queue.Queue()
     self.outqueue = Queue.Queue()
     self.reconnectcount = 0
     self.stopped = False
     self.plugs = coreplugs
     self.gatekeeper = GateKeeper(self.botname)
     self.gatekeeper.allow(self.user or self.jid or self.server or self.botname)
     self.closed = False
     try:
         import waveapi
         self.isgae = True
         logging.debug("botbase - bot is a GAE bot (%s)" % self.botname)
     except ImportError:
         self.isgae = False
         logging.debug("botbase - bot is a shell bot (%s)" % self.botname)
     self.starttime = time.time()
     self.type = "base"
     self.status = "init"
     self.networkname = self.cfg.networkname or self.botname or ""
     if not self.uuid:
         if self.cfg and self.cfg.uuid: self.uuid = self.cfg.uuid
         else:
             self.uuid = self.cfg.uuid = uuid.uuid4()
             self.cfg.save()
     if self.cfg and not self.cfg.followlist: self.cfg.followlist = [] ; self.cfg.save()
     from jsb.lib.datadir import getdatadir
     datadir = getdatadir()
     self.datadir = datadir + os.sep + self.fleetdir
     self.name = self.botname
     self.owner = self.cfg.owner
     if not self.owner:
         logging.debug(u"owner is not set in %s - using mainconfig" % self.cfg.cfile)
         self.owner = Config().owner
     self.setusers(usersin)
     logging.info(u"botbase - owner is %s" % self.owner)
     self.users.make_owner(self.owner)
     self.outcache = outcache
     self.userhosts = {}
     self.connectok = threading.Event()
     if not self.nick: self.nick = (nick or self.cfg.nick or u'jsb')
     try:
         if not os.isdir(self.datadir): os.mkdir(self.datadir)
     except: pass
     self.setstate()
     self.stopreadloop = False
     self.stopoutloop = False
     self.outputlock = thread.allocate_lock()
     self.outqueues = [Queue.Queue() for i in range(10)]
     self.tickqueue = Queue.Queue()
     self.encoding = self.cfg.encoding or "utf-8"
     self.cmndperms = getcmndperms()
     self.outputmorphs = outputmorphs
     self.inputmorphs = inputmorphs
     fleet = getfleet(datadir)
     if not fleet.byname(self.name): fleet.bots.append(self) ; 
     if not self.isgae:
         defaultrunner.start()
         tickloop.start(self)
예제 #13
0
 def __init__(self,
              cfg=None,
              usersin=None,
              plugs=None,
              botname=None,
              nick=None,
              *args,
              **kwargs):
     if not botname and cfg and cfg.botname: botname = cfg.botname
     if botname: self.botname = botname
     else:
         self.botname = u"default-%s" % str(type(self)).split('.')[-1][:-2]
     logging.info("botbase - name is %s" % self.botname)
     self.fleetdir = u'fleet' + os.sep + stripname(self.botname)
     if cfg:
         self.cfg = Config(self.fleetdir + os.sep + u'config', input=cfg)
     else:
         self.cfg = Config(self.fleetdir + os.sep + u'config')
     LazyDict.__init__(self)
     self.update(self.cfg)
     self.ignore = []
     self.aliases = getaliases()
     self.curevent = None
     self.inqueue = Queue.Queue()
     self.outqueue = Queue.Queue()
     self.reconnectcount = 0
     self.stopped = False
     self.plugs = coreplugs
     self.gatekeeper = GateKeeper(self.botname)
     self.gatekeeper.allow(self.user or self.jid or self.server
                           or self.botname)
     self.closed = False
     try:
         import waveapi
         self.isgae = True
         logging.debug("botbase - bot is a GAE bot (%s)" % self.botname)
     except ImportError:
         self.isgae = False
         logging.debug("botbase - bot is a shell bot (%s)" % self.botname)
     self.starttime = time.time()
     self.type = "base"
     self.status = "init"
     self.networkname = self.cfg.networkname or self.botname or ""
     if not self.uuid:
         if self.cfg and self.cfg.uuid: self.uuid = self.cfg.uuid
         else:
             self.uuid = self.cfg.uuid = uuid.uuid4()
             self.cfg.save()
     if self.cfg and not self.cfg.followlist:
         self.cfg.followlist = []
         self.cfg.save()
     from jsb.lib.datadir import getdatadir
     datadir = getdatadir()
     self.datadir = datadir + os.sep + self.fleetdir
     self.name = self.botname
     self.owner = self.cfg.owner
     if not self.owner:
         logging.debug(u"owner is not set in %s - using mainconfig" %
                       self.cfg.cfile)
         self.owner = Config().owner
     self.setusers(usersin)
     logging.info(u"botbase - owner is %s" % self.owner)
     self.users.make_owner(self.owner)
     self.outcache = outcache
     self.userhosts = {}
     self.connectok = threading.Event()
     if not self.nick: self.nick = (nick or self.cfg.nick or u'jsb')
     try:
         if not os.isdir(self.datadir): os.mkdir(self.datadir)
     except:
         pass
     self.setstate()
     self.stopreadloop = False
     self.stopoutloop = False
     self.outputlock = thread.allocate_lock()
     self.outqueues = [Queue.Queue() for i in range(10)]
     self.tickqueue = Queue.Queue()
     self.encoding = self.cfg.encoding or "utf-8"
     self.cmndperms = getcmndperms()
     self.outputmorphs = outputmorphs
     self.inputmorphs = inputmorphs
     fleet = getfleet(datadir)
     if not fleet.byname(self.name): fleet.bots.append(self)
     if not self.isgae:
         defaultrunner.start()
         tickloop.start(self)