Ejemplo n.º 1
0
    def __init__(self, cfg):

        # set attributes based on config
        self.__dict__.update(cfg)
        # if name not set in config file use the directory name
        if not cfg.has_key('name'):
            self.name = cfg.dir.split(os.sep)[-1]
        # default nick to gozerbot
        if not cfg.has_key('nick'):
            self.nick = 'gozerbot'
        # default port to 0 (use default port)
        if not cfg.has_key('port'):
            self.port = 0
        # make sure bot name is not a directory
        if '..' in self.name or '/' in self.name:
            raise Exception('wrong bot name %s' % self.name)
        # set datadir to datadir/fleet/<botname>
        self.datadir = datadir + os.sep + 'fleet' + os.sep + self.name
        # bot state
        self.state = Pdod(self.datadir + os.sep + 'state')  # bot state
        # joined channels list .. used to join channels
        if not self.state.has_key('joinedchannels'):
            self.state['joinedchannels'] = []
        # allowed commands on the bot
        if not self.state.has_key('allowed'):
            self.state['allowed'] = []
        # channels we dont want ops in
        if not self.state.has_key('no-op'):
            self.state['no-op'] = []
        # channels we are op in
        if not self.state.has_key('opchan'):
            self.state['opchan'] = []
        self.cfg = cfg  # the bots config
        self.orignick = ""  # original nick
        self.blocking = True  # use blocking sockets
        self.lastoutput = 0  # time of last output
        self.stopped = False  # flag to set when bot is to be stopped
        self.connected = False  # conencted flag
        self.connecting = False  # connecting flag
        self.connectok = threading.Event()  # event set when bot has connected
        self.waitingforconnect = False  # flag to indicate we are waiting for connect
        self.starttime = time.time()  # start time of the bot
        self.nrevents = 0  # number of events processed
        self.gcevents = 0  # number of garbage collected events
        self.less = Less(5)  # output buffering
        self.userchannels = Dol()  # list of channels a user is in
        self.channels = Channels(self.datadir + os.sep +
                                 'channels')  # channels
        self.userhosts = PersistState(self.datadir + os.sep +
                                      'userhosts')  # userhosts cache
        self.splitted = []  # list of splitted nicks
        self.throttle = []  # list of nicks that need to be throttled
        self.jabber = False  # flag is set on jabber bots
        self.google = False  # flag is set on google bots

        # start runners
        runners_start()
Ejemplo n.º 2
0
    def __init__(self):

        # cbs are the callbacks .. 1 list per ievent.CMND
        self.cbs = Dol()
Ejemplo n.º 3
0
class Callbacks(object):

    """ dict of lists containing callbacks. """

    def __init__(self):

        # cbs are the callbacks .. 1 list per ievent.CMND
        self.cbs = Dol()

    def size(self):

        """ return number of callbacks. """

        return len(self.cbs)

    @locked
    def add(self, what, func, prereq=None, kwargs=None, threaded=False, nr=False, speed=5):

        """ add a callback. """

        what = what.upper()

        # get the plugin this callback was registered from
        plugname = calledfrom(sys._getframe(1))

        # check if plugname is in loadlist .. if not don't add callback
        if config["loadlist"] and not plugname in config["loadlist"]:
            return

        # see if kwargs is set if not init to {}
        if not kwargs:
            kwargs = {}

        # add callback to the dict of lists
        if nr != False:
            self.cbs.insert(nr, what, Callback(func, prereq, plugname, kwargs, threaded, speed))
        else:
            self.cbs.add(what, Callback(func, prereq, plugname, kwargs, threaded, speed))

        rlog(-3, "callbacks", "added %s (%s)" % (what, plugname))

    def unload(self, plugname):

        """ unload all callbacks registered in a plugin. """

        unload = []

        # look for all callbacks in a plugin
        for name, cblist in self.cbs.iteritems():
            index = 0
            for item in cblist:
                if item.plugname == plugname:
                    unload.append((name, index))
                index += 1

        # delete callbacks
        for callback in unload[::-1]:
            self.cbs.delete(callback[0], callback[1])
            rlog(1, "callbacks", "unloaded %s" % callback[0])

    def whereis(self, cmnd):

        """ show where ircevent.CMND callbacks are registered """

        result = []
        cmnd = cmnd.upper()

        # locate callbacks for CMND
        for c, callback in self.cbs.iteritems():
            if c == cmnd:
                for item in callback:
                    if not item.plugname in result:
                        result.append(item.plugname)

        return result

    def list(self):

        """ show all callbacks. """

        result = []

        # loop over callbacks and collect callback functions
        for cmnd, callbacks in self.cbs.iteritems():
            for cb in callbacks:
                result.append(getname(cb.func))

        return result

    def check(self, bot, ievent):

        """ check for callbacks to be fired. """

        # check for "ALL" callbacks
        if self.cbs.has_key("ALL"):
            for cb in self.cbs["ALL"]:
                stats.up("callbacks", "ALL")
                self.callback(cb, bot, ievent)

        cmnd = ievent.cmnd.upper()

        # check for CMND callbacks
        if self.cbs.has_key(cmnd):
            for cb in self.cbs[cmnd]:
                stats.up("callbacks", cmnd)
                self.callback(cb, bot, ievent)

    @locked
    def callback(self, cb, bot, ievent):

        """ callback cb with bot and ievent as arguments """

        try:

            # see if the callback pre requirement succeeds
            if cb.prereq:
                rlog(-10, "callback", "excecuting in loop %s" % str(cb.prereq))
                if not cb.prereq(bot, ievent):
                    return

            # check if callback function is there
            if not cb.func:
                return

            # log and stats
            rlog(0, "callback", "excecuting callback %s" % str(cb.func))
            stats.up("callbacks", getname(cb.func))
            stats.up("callbacks", cb.plugname)

            # launcn the callback .. either threaded or dispatched at runners
            if cb.threaded:
                start_new_thread(cb.func, (bot, ievent), cb.kwargs)
            else:
                cbrunners[10 - cb.speed].put("cb-%s" % cb.plugname, cb.func, bot, ievent, **cb.kwargs)

        except Exception, ex:
            handle_exception()
Ejemplo n.º 4
0
    def __init__(self):

        # cbs are the callbacks .. 1 list per ievent.CMND
        self.cbs = Dol()
Ejemplo n.º 5
0
class Callbacks(object):
    """ dict of lists containing callbacks. """
    def __init__(self):

        # cbs are the callbacks .. 1 list per ievent.CMND
        self.cbs = Dol()

    def size(self):
        """ return number of callbacks. """

        return len(self.cbs)

    @locked
    def add(self,
            what,
            func,
            prereq=None,
            kwargs=None,
            threaded=False,
            nr=False,
            speed=5):
        """ add a callback. """

        what = what.upper()

        # get the plugin this callback was registered from
        plugname = calledfrom(sys._getframe(1))

        # check if plugname is in loadlist .. if not don't add callback
        if config['loadlist'] and not plugname in config['loadlist']:
            return

        # see if kwargs is set if not init to {}
        if not kwargs:
            kwargs = {}

        # add callback to the dict of lists
        if nr != False:
            self.cbs.insert(
                nr, what,
                Callback(func, prereq, plugname, kwargs, threaded, speed))
        else:
            self.cbs.add(
                what, Callback(func, prereq, plugname, kwargs, threaded,
                               speed))

        rlog(-3, 'callbacks', 'added %s (%s)' % (what, plugname))

    def unload(self, plugname):
        """ unload all callbacks registered in a plugin. """

        unload = []

        # look for all callbacks in a plugin
        for name, cblist in self.cbs.iteritems():
            index = 0
            for item in cblist:
                if item.plugname == plugname:
                    unload.append((name, index))
                index += 1

        # delete callbacks
        for callback in unload[::-1]:
            self.cbs.delete(callback[0], callback[1])
            rlog(1, 'callbacks', 'unloaded %s' % callback[0])

    def whereis(self, cmnd):
        """ show where ircevent.CMND callbacks are registered """

        result = []
        cmnd = cmnd.upper()

        # locate callbacks for CMND
        for c, callback in self.cbs.iteritems():
            if c == cmnd:
                for item in callback:
                    if not item.plugname in result:
                        result.append(item.plugname)

        return result

    def list(self):
        """ show all callbacks. """

        result = []

        # loop over callbacks and collect callback functions
        for cmnd, callbacks in self.cbs.iteritems():
            for cb in callbacks:
                result.append(getname(cb.func))

        return result

    def check(self, bot, ievent):
        """ check for callbacks to be fired. """

        # check for "ALL" callbacks
        if self.cbs.has_key('ALL'):
            for cb in self.cbs['ALL']:
                stats.up('callbacks', 'ALL')
                self.callback(cb, bot, ievent)

        cmnd = ievent.cmnd.upper()

        # check for CMND callbacks
        if self.cbs.has_key(cmnd):
            for cb in self.cbs[cmnd]:
                stats.up('callbacks', cmnd)
                self.callback(cb, bot, ievent)

    @locked
    def callback(self, cb, bot, ievent):
        """ callback cb with bot and ievent as arguments """

        try:

            # see if the callback pre requirement succeeds
            if cb.prereq:
                rlog(-10, 'callback', 'excecuting in loop %s' % str(cb.prereq))
                if not cb.prereq(bot, ievent):
                    return

            # check if callback function is there
            if not cb.func:
                return

            # log and stats
            rlog(0, 'callback', 'excecuting callback %s' % str(cb.func))
            stats.up('callbacks', getname(cb.func))
            stats.up('callbacks', cb.plugname)

            # launcn the callback .. either threaded or dispatched at runners
            if cb.threaded:
                start_new_thread(cb.func, (bot, ievent), cb.kwargs)
            else:
                cbrunners[10 - cb.speed].put("cb-%s" % cb.plugname, cb.func,
                                             bot, ievent, **cb.kwargs)

        except Exception, ex:
            handle_exception()