Exemple #1
0
 def __init__(self, cfg=None, usersin=None, plugs=None, botname=None, nick=None, bottype=None, nocbs=None, *args, **kwargs):
     logging.debug("type is %s" % str(type(self)))
     if cfg: self.cfg = cfg ; botname = botname or self.cfg.name
     if not botname: botname = u"default-%s" % str(type(self)).split('.')[-1][:-2]
     if not botname: raise Exception("can't determine  botname")
     self.fleetdir = u'fleet' + os.sep + stripname(botname)
     if not self.cfg: self.cfg = Config(self.fleetdir + os.sep + u'config')
     self.cfg.name = botname or self.cfg.name
     if not self.cfg.name: raise Exception("name is not set in %s config file" % self.fleetdir)
     logging.debug("name is %s" % self.cfg.name)
     LazyDict.__init__(self)
     logging.debug("created bot with config %s" % self.cfg.tojson(full=True))
     self.ecounter = 0
     self.ids = []
     self.aliases = getaliases()
     self.reconnectcount = 0
     self.plugs = coreplugs
     self.gatekeeper = GateKeeper(self.cfg.name)
     self.gatekeeper.allow(self.user or self.jid or self.cfg.server or self.cfg.name)
     self.starttime = time.time()
     self.type = bottype or "base"
     self.status = "init"
     self.networkname = self.cfg.networkname or self.cfg.name or ""
     from jsb.lib.datadir import getdatadir
     datadir = getdatadir()
     self.datadir = datadir + os.sep + self.fleetdir
     self.maincfg = getmainconfig()
     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 = self.maincfg.owner
     self.users = usersin or getusers()
     logging.debug(u"owner is %s" % self.owner)
     self.users.make_owner(self.owner)
     self.outcache = outcache
     self.userhosts = LazyDict()
     self.nicks = LazyDict()
     self.connectok = threading.Event()
     self.reconnectcount = 0
     self.cfg.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.outputlock = thread.allocate_lock()
     try:
         self.outqueue = Queue.PriorityQueue()
         self.eventqueue = Queue.PriorityQueue()
     except AttributeError:
         self.outqueue = Queue.Queue()
         self.eventqueue = Queue.Queue()
     self.laterqueue = Queue.Queue()
     self.encoding = self.cfg.encoding or "utf-8"
     self.cmndperms = getcmndperms()
     self.outputmorphs = outputmorphs
     self.inputmorphs = inputmorphs
     try:
         if nocbs: self.nocbs = nocbs.split(",")
     except ValueError: logging.error("cannot determine %s nocbs argument" % self.nocbs)
     self.lastiter = 0
Exemple #2
0
 def makebot(self, type, name, config={}, domain="", showerror=False):
     """ create a bot .. use configuration if provided. """
     if not name: logging.error(" name is not correct: %s" % name) ; return
     if not type: type = self.data.types.get(name)
     if not type: logging.error("no type found for %s bot" % name) ; return 
     if config: logging.debug('making %s (%s) bot - %s - from: %s' % (type, name, config.tojson(), whichmodule()))
     bot = None
     if not config: cfg = Config('fleet' + os.sep + stripname(name) + os.sep + 'config')
     else: cfg = config 
     cfg.init()
     if not cfg.name: cfg['name'] = name
     cfg['botname'] = cfg['name']
     if cfg.disable:
         logging.info("%s bot is disabled. see %s" % (name, cfg.cfile))
         return 
     if not cfg.type and type:
         logging.debug("%s - setting type to %s" % (cfg.cfile, type))
         cfg.type = type
     if not cfg['type']:
         try:
             self.data['names'].remove(name)
             self.save()
         except ValueError: pass
         raise Exception("no bot type specified")
     if not cfg.owner:
         logging.debug("%s - owner not set .. using global config." % cfg.name) 
         cfg.owner = getmainconfig().owner
     if not cfg.domain and domain: cfg.domain = domain
     if not cfg: raise Exception("can't make config for %s" % name)
     #cfg.save()
     bot = BotFactory().create(type, cfg)
     return bot
Exemple #3
0
 def addguest(self, userhost, username=None):
     if username and self.byname(username): username = userhost
     if not self.getname(userhost):
         if getmainconfig().guestasuser:
             return self.add(username, [
                 userhost,
             ], [
                 "USER",
             ])
         else:
             return self.add(username, [
                 userhost,
             ], [
                 "GUEST",
             ])
     else:
         logging.warn("%s is already in database" % userhost)
Exemple #4
0
 def joinchannels(self):
     """ join channels. """
     time.sleep(getmainconfig().waitforjoin or 1)
     target = self.cfg.channels
     try:
         for i in self.state['joinedchannels']:
             if i not in target: target.append(i)
     except: pass
     if not target: target = self.state['joinedchannels']
     for i in target:
         try:
             logging.debug("%s - joining %s" % (self.cfg.name, i))
             channel = ChannelBase(i, self.cfg.name)
             if channel: key = channel.data.key
             else: key = None
             if channel.data.nick: self.ids.append("%s/%s" % (i, channel.data.nick))
             start_new_thread(self.join, (i, key))
         except Exception, ex:
             logging.warn('%s - failed to join %s: %s' % (self.cfg.name, i, str(ex)))
             handle_exception()
         time.sleep(3)
Exemple #5
0
 def joinchannels(self):
     """ join channels. """
     time.sleep(getmainconfig().waitforjoin or 1)
     target = self.cfg.channels
     try:
         for i in self.state['joinedchannels']:
             if i not in target: target.append(i)
     except:
         pass
     if not target: target = self.state['joinedchannels']
     for i in target:
         try:
             logging.debug("%s - joining %s" % (self.cfg.name, i))
             channel = ChannelBase(i, self.cfg.name)
             if channel: key = channel.data.key
             else: key = None
             if channel.data.nick:
                 self.ids.append("%s/%s" % (i, channel.data.nick))
             start_new_thread(self.join, (i, key))
         except Exception, ex:
             logging.warn('%s - failed to join %s: %s' %
                          (self.cfg.name, i, str(ex)))
             handle_exception()
         time.sleep(3)
Exemple #6
0
 def addguest(self, userhost):
     if not self.getname(userhost):
         if getmainconfig().guestasuser: self.add(userhost, [userhost, ], ["USER",])
         else: self.add(userhost, [userhost, ], ["GUEST",])
Exemple #7
0
 def __init__(self,
              cfg=None,
              usersin=None,
              plugs=None,
              botname=None,
              nick=None,
              bottype=None,
              nocbs=None,
              *args,
              **kwargs):
     logging.debug("type is %s" % str(type(self)))
     if cfg:
         self.cfg = cfg
         botname = botname or self.cfg.name
     if not botname:
         botname = u"default-%s" % str(type(self)).split('.')[-1][:-2]
     if not botname: raise Exception("can't determine  botname")
     self.fleetdir = u'fleet' + os.sep + stripname(botname)
     if not self.cfg: self.cfg = Config(self.fleetdir + os.sep + u'config')
     self.cfg.name = botname or self.cfg.name
     if not self.cfg.name:
         raise Exception("name is not set in %s config file" %
                         self.fleetdir)
     logging.debug("name is %s" % self.cfg.name)
     LazyDict.__init__(self)
     logging.debug("created bot with config %s" %
                   self.cfg.tojson(full=True))
     self.ecounter = 0
     self.ids = []
     self.aliases = getaliases()
     self.reconnectcount = 0
     self.plugs = coreplugs
     self.gatekeeper = GateKeeper(self.cfg.name)
     self.gatekeeper.allow(self.user or self.jid or self.cfg.server
                           or self.cfg.name)
     self.starttime = time.time()
     self.type = bottype or "base"
     self.status = "init"
     self.networkname = self.cfg.networkname or self.cfg.name or ""
     from jsb.lib.datadir import getdatadir
     datadir = getdatadir()
     self.datadir = datadir + os.sep + self.fleetdir
     self.maincfg = getmainconfig()
     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 = self.maincfg.owner
     self.users = usersin or getusers()
     logging.debug(u"owner is %s" % self.owner)
     self.users.make_owner(self.owner)
     self.outcache = outcache
     self.userhosts = LazyDict()
     self.nicks = LazyDict()
     self.connectok = threading.Event()
     self.reconnectcount = 0
     self.cfg.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.outputlock = thread.allocate_lock()
     try:
         self.outqueue = Queue.PriorityQueue()
         self.eventqueue = Queue.PriorityQueue()
     except AttributeError:
         self.outqueue = Queue.Queue()
         self.eventqueue = Queue.Queue()
     self.laterqueue = Queue.Queue()
     self.encoding = self.cfg.encoding or "utf-8"
     self.cmndperms = getcmndperms()
     self.outputmorphs = outputmorphs
     self.inputmorphs = inputmorphs
     try:
         if nocbs: self.nocbs = nocbs.split(",")
     except ValueError:
         logging.error("cannot determine %s nocbs argument" % self.nocbs)
     self.lastiter = 0
Exemple #8
0
 def addguest(self, userhost, username=None):
     if username and self.byname(username): username = userhost
     if not self.getname(userhost):
         if getmainconfig().guestasuser: return self.add(username, [userhost, ], ["USER",])
         else: return self.add(username, [userhost, ], ["GUEST",])
     else: logging.warn("%s is already in database" % userhost)
Exemple #9
0
 def __init__(self, cfg=None, usersin=None, plugs=None, botname=None, nick=None, *args, **kwargs):
     logging.debug("type is %s" % str(type(self)))
     if cfg: cfg = LazyDict(cfg)
     if cfg and not botname: botname = cfg.name
     if not botname: botname = u"default-%s" % str(type(self)).split('.')[-1][:-2]
     if not botname: raise Exception("can't determine type")
     self.fleetdir = u'fleet' + os.sep + stripname(botname)
     self.cfg = Config(self.fleetdir + os.sep + u'config')
     if cfg: self.cfg.merge(cfg)
     self.cfg.name = botname
     if not self.cfg.name: raise Exception(" name is not set in %s config file" % self.fleetdir)
     logging.debug("name is %s" % self.cfg.name)
     LazyDict.__init__(self)
     self.ignore = []
     self.ids = []
     self.started = False
     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.cfg.name)
     self.gatekeeper.allow(self.user or self.jid or self.cfg.server or self.cfg.name)
     self.closed = False
     try:
         import waveapi
         self.isgae = True
         logging.debug("bot is a GAE bot (%s)" % self.cfg.name)
     except ImportError:
         self.isgae = False
         logging.debug("bot is a shell bot (%s)" % self.cfg.name)
     self.starttime = time.time()
     self.type = "base"
     self.status = "init"
     self.networkname = self.cfg.networkname or self.cfg.name 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.owner = self.cfg.owner
     if not self.owner:
         logging.debug(u"owner is not set in %s - using mainconfig" % self.cfg.cfile)
         self.owner = getmainconfig().owner
     self.setusers(usersin)
     logging.debug(u"owner is %s" % self.owner)
     self.users.make_owner(self.owner)
     self.outcache = outcache
     self.userhosts = LazyDict()
     self.connectok = threading.Event()
     self.reconnectcount = 0
     self.cfg.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
     if not self.isgae:
         defaultrunner.start()
         callbackrunner.start()
         waitrunner.start()
         tickloop.start(self)