Ejemplo n.º 1
0
    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.º 2
0
    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.º 3
0
    def handle(self, *args, **kwargs):
        """ fire monitor callbacks. """

        for i in self.outs:

            # check if precondition is met
            try:
                if i[2]:
                    stats.up('monitors', thr.getname(str(i[2])))
                    rlog(-10, 'jabbermonitor',
                         'checking inloop %s' % str(i[2]))
                    doit = i[2](*args, **kwargs)
                else:
                    doit = 1
            except Exception, ex:
                handle_exception()
                doit = 0

            if doit:
                # run monitor callback in its own thread
                rlog(0, 'jabbermonitor', 'excecuting jabbermonitor callback \
%s' % i[0])
                stats.up('monitors', thr.getname(str(i[1])))
                if not i[3]:
                    cbrunners[5].put("monitor-%s" % i[0], i[1], *args)
                else:
                    thr.start_new_thread(i[1], args, kwargs)
Ejemplo n.º 4
0
    def handle(self, *args, **kwargs):

        """ fire monitor callbacks. """

        for i in self.outs:

            # check if precondition is met
            try:
                if i[2]:
                    stats.up('monitors', thr.getname(str(i[2])))
                    rlog(-10, 'jabbermonitor', 'checking inloop %s' % str(i[2]))
                    doit = i[2](*args, **kwargs)
                else:
                    doit = 1
            except Exception, ex:
                handle_exception()
                doit = 0

            if doit:
                # run monitor callback in its own thread
                rlog(0, 'jabbermonitor', 'excecuting jabbermonitor callback \
%s' % i[0])
                stats.up('monitors', thr.getname(str(i[1])))
                if not i[3]:
                    cbrunners[5].put("monitor-%s" % i[0], i[1], *args)
                else:
                    thr.start_new_thread(i[1], args, kwargs)
Ejemplo n.º 5
0
    def start(self):

        """ start the eventhandler thread. """

        self.stopped = False
        if not self.running:
            start_new_thread(self.handleloop, ())
            self.running = True
Ejemplo n.º 6
0
    def dispatch(self, com, txt):
        """ dispatch command. """

        if com.threaded:
            start_new_thread(com.func, (txt, ))
        else:
            cmndrunners[10 - com.speed].put(com.name, com.func, txt)
        return 1
Ejemplo n.º 7
0
    def dispatch(self, com, txt):

        """ dispatch command. """

        if com.threaded:
            start_new_thread(com.func, (txt, ))
        else:
            cmndrunners[10-com.speed].put(com.name, com.func, txt)
        return 1
Ejemplo n.º 8
0
    def checkloop(self):

        """ main loop of the periodical scheduler. """

        while self.run:
            for job in self.jobs:
                if job.next <= time.time():
                    thr.start_new_thread(self.runjob, (job, ))
            time.sleep(self.SLEEPTIME)
Ejemplo n.º 9
0
    def register(self, catch, queue, timeout=15):
        """ register wait for privmsg. """

        rlog(1, 'jabberwait', 'registering for %s' % catch)
        self.ticket += 1
        self.waitlist.append((catch, queue, self.ticket))
        if timeout:
            thr.start_new_thread(self.dotimeout, (timeout, self.ticket))
        return self.ticket
Ejemplo n.º 10
0
    def register(self, cmnd, catch, queue, timeout=15):
        """ register wait for cmnd. """

        rlog(1, 'wait', 'registering for cmnd ' + cmnd)
        self.ticket += 1
        self.waitlist.insert(0, (cmnd, catch, queue, self.ticket))
        if timeout:
            # start timeout thread
            thr.start_new_thread(self.dotimeout, (timeout, self.ticket))
        return self.ticket
Ejemplo n.º 11
0
    def register(self, catch, queue, timeout=15):

        """ register wait for privmsg. """

        rlog(1, 'jabberwait', 'registering for %s' % catch)
        self.ticket += 1
        self.waitlist.append((catch, queue, self.ticket))
        if timeout:
            thr.start_new_thread(self.dotimeout, (timeout, self.ticket))
        return self.ticket
Ejemplo n.º 12
0
    def register(self, cmnd, catch, queue, timeout=15):

        """ register wait for cmnd. """

        rlog(1, 'wait', 'registering for cmnd ' + cmnd)
        self.ticket += 1
        self.waitlist.insert(0, (cmnd, catch, queue, self.ticket))
        if timeout:
            # start timeout thread
            thr.start_new_thread(self.dotimeout, (timeout, self.ticket))
        return self.ticket
Ejemplo n.º 13
0
    def start(self, botlist=[], enable=False):

        """ startup the bots. """

        # scan the fleet datadir for bots
        dirs = []
        got = []
        for bot in botlist:
            dirs.append(self.datadir + os.sep + bot)

        if not dirs:
            dirs = glob.glob(self.datadir + os.sep + "*")

        for fleetdir in dirs:

            if fleetdir.endswith('fleet'):
                continue

            rlog(10, 'fleet', 'found bot: ' + fleetdir)
            cfg = Config(fleetdir, 'config')

            if not cfg:
                rlog(10, 'fleet', "can't read %s config file" % fleetdir)
                continue

            name = fleetdir.split(os.sep)[-1]

            if not name:
                rlog(10, 'fleet', "can't read botname from %s config file" % \
fleetdir)
                continue

            if not enable and not cfg['enable']:
                rlog(10, 'fleet', '%s bot is disabled' % name)
                continue
            else:
                rlog(10, 'fleet', '%s bot is enabled' % name)

            if not name in fleetdir:
                rlog(10, 'fleet', 'bot name in config file doesnt match dir name')
                continue

            bot = self.makebot(name, cfg)

            if bot:
                start_new_thread(bot.connectwithjoin, ())
                self.addbot(bot)
                got.append(bot)

        return got

        # set startok event
        self.startok.set()
Ejemplo n.º 14
0
    def start(self, botlist=[], enable=False):
        """ startup the bots. """

        # scan the fleet datadir for bots
        dirs = []
        got = []
        for bot in botlist:
            dirs.append(self.datadir + os.sep + bot)

        if not dirs:
            dirs = glob.glob(self.datadir + os.sep + "*")

        for fleetdir in dirs:

            if fleetdir.endswith('fleet'):
                continue

            rlog(10, 'fleet', 'found bot: ' + fleetdir)
            cfg = Config(fleetdir, 'config')

            if not cfg:
                rlog(10, 'fleet', "can't read %s config file" % fleetdir)
                continue

            name = fleetdir.split(os.sep)[-1]

            if not name:
                rlog(10, 'fleet', "can't read botname from %s config file" % \
fleetdir)
                continue

            if not enable and not cfg['enable']:
                rlog(10, 'fleet', '%s bot is disabled' % name)
                continue
            else:
                rlog(10, 'fleet', '%s bot is enabled' % name)

            if not name in fleetdir:
                rlog(10, 'fleet',
                     'bot name in config file doesnt match dir name')
                continue

            bot = self.makebot(name, cfg)

            if bot:
                start_new_thread(bot.connectwithjoin, ())
                self.addbot(bot)
                got.append(bot)

        return got

        # set startok event
        self.startok.set()
Ejemplo n.º 15
0
    def dispatch(self, callback, txt):

        """ dispatch callback on txt. """

        try:
            result = re.search(callback.compiled, txt)
            if result:
                if callback.threaded:
                    thr.start_new_thread(callback.func, (txt, result.groups()))
                else:
                    cmndrunners.put(callback.plugname, callback.func, txt, \
result.groups())
                    return 1
        except Exception, ex:
            handle_exception()
Ejemplo n.º 16
0
    def cmndall(self, event, cmnd):
        """ do a command on all bots. """

        threads = []
        for i in self.bots:
            thread = start_new_thread(self.cmnd, (event, i.name, cmnd))
            threads.append(thread)
        for i in threads:
            i.join(10)
Ejemplo n.º 17
0
    def resume(self, sessionfile):
        """ resume bot from session file. """

        # read JSON session file
        session = load(open(sessionfile))

        #  resume bots in session file
        for name in session['bots'].keys():
            reto = None
            if name == session['name']:
                reto = session['channel']
            start_new_thread(self.resumebot,
                             (name, session['bots'][name], reto))

        # allow 5 seconds for bots to resurrect
        time.sleep(5)

        # set start event
        self.startok.set()
Ejemplo n.º 18
0
    def cmndall(self, event, cmnd):

        """ do a command on all bots. """

        threads = []
        for i in self.bots:
            thread = start_new_thread(self.cmnd, (event, i.name, cmnd))
            threads.append(thread)
        for i in threads:
            i.join(10)
Ejemplo n.º 19
0
    def resume(self, sessionfile):

        """ resume bot from session file. """

        # read JSON session file
        session = load(open(sessionfile))

        #  resume bots in session file
        for name in session['bots'].keys():
            reto = None
            if name == session['name']:
                reto = session['channel']
            start_new_thread(self.resumebot, (name, session['bots'][name], reto))

        # allow 5 seconds for bots to resurrect
        time.sleep(5)

        # set start event
        self.startok.set()
Ejemplo n.º 20
0
    def cmnd(self, event, name, cmnd):
 
        """ do command on a bot by name. """

        name = name.lower()
        bot = self.byname(name)
        if not bot:
            return 0
        from gozerbot.eventbase import EventBase
        j = EventBase(event)
        j.txt = cmnd
        q = Queue.Queue()
        j.queues = [q]
        j.speed = 3
        start_new_thread(plugins.trydispatch, (bot, j))
        result = waitforqueue(q)
        if not result:
            return
        res = ["[%s]" % bot.name, ]
        res += result
        event.reply(res)
Ejemplo n.º 21
0
    def cmnd(self, event, name, cmnd):
        """ do command on a bot by name. """

        name = name.lower()
        bot = self.byname(name)
        if not bot:
            return 0
        from gozerbot.eventbase import EventBase
        j = EventBase(event)
        j.txt = cmnd
        q = Queue.Queue()
        j.queues = [q]
        j.speed = 3
        start_new_thread(plugins.trydispatch, (bot, j))
        result = waitforqueue(q)
        if not result:
            return
        res = [
            "[%s]" % bot.name,
        ]
        res += result
        event.reply(res)
Ejemplo n.º 22
0
    def resumebot(self, botname, data={}, printto=None):
        """ resume individual bot. """

        # see if we need to exit the old bot
        oldbot = self.byname(botname)
        if oldbot:
            oldbot.exit()

        # recreate config file of the bot
        cfg = Config(datadir + os.sep + 'fleet' + os.sep + botname, 'config')

        # make the bot and resume (IRC) or reconnect (Jabber)
        bot = self.makebot(botname, cfg)
        if bot:
            if oldbot:
                self.replace(oldbot, bot)
            else:
                self.bots.append(bot)
            if not bot.jabber:
                bot._resume(data, printto)
            else:
                start_new_thread(bot.connectwithjoin, ())
Ejemplo n.º 23
0
    def resumebot(self, botname, data={}, printto=None):

        """ resume individual bot. """

        # see if we need to exit the old bot
        oldbot = self.byname(botname)
        if oldbot:
            oldbot.exit()

        # recreate config file of the bot
        cfg = Config(datadir + os.sep + 'fleet' + os.sep + botname, 'config')

        # make the bot and resume (IRC) or reconnect (Jabber)
        bot = self.makebot(botname, cfg)
        if bot:
            if oldbot:
                self.replace(oldbot, bot)
            else:
                self.bots.append(bot)
            if not bot.jabber:
                bot._resume(data, printto)
            else:
                start_new_thread(bot.connectwithjoin, ())
Ejemplo n.º 24
0
    def exit(self):

        """ call registered plugins save. """

        self.save()
        threadlist = []

        # call shutdown on all plugins
        for name, plug in self.plugs.iteritems():
            try:
                shutdown = getattr(plug, 'shutdown')
                thread = start_new_thread(shutdown, ())
                threadlist.append((name, thread))
                try:
                    self.initcalled.remove(name)
                except ValueError:
                    pass
            except AttributeError:
                continue
            except Exception, ex:
                rlog(10, 'plugins', 'error shutting down %s: %s' % (name, str(ex)))
Ejemplo n.º 25
0
    def exit(self):
        """ call registered plugins save. """

        self.save()
        threadlist = []

        # call shutdown on all plugins
        for name, plug in self.plugs.iteritems():
            try:
                shutdown = getattr(plug, 'shutdown')
                thread = start_new_thread(shutdown, ())
                threadlist.append((name, thread))
                try:
                    self.initcalled.remove(name)
                except ValueError:
                    pass
            except AttributeError:
                continue
            except Exception, ex:
                rlog(10, 'plugins',
                     'error shutting down %s: %s' % (name, str(ex)))
Ejemplo n.º 26
0
            rlog(20, 'plugins', "no gozerplugs package found")
            plugs = None
        if plugs:
            for i in plugs.__plugs__:
                if i not in avail:
                    try:
                        self.regplugin('gozerplugs', i)
                        avail.append(i)
                    except Exception, ex:
                        handle_exception()
                else:
                    rlog(10, 'plugins', '%s already loaded' % i)

        self.ondisk.extend(avail)
        self.readoverload()
        start_new_thread(self.showregistered, ())

    def readoverload(self):
        """ see if there is a permoverload file and if so use it to overload
            permissions based on function name.
        """
        try:
            overloadfile = open(datadir + os.sep + 'permoverload', 'r')
        except IOError:
            return
        try:
            for i in overloadfile:
                i = i.strip()
                splitted = i.split(',')
                try:
                    funcname = splitted[0].strip()
Ejemplo n.º 27
0
    def start(self):

        """ start the periodical scheduler. """

        thr.start_new_thread(self.checkloop, ())
Ejemplo n.º 28
0
            rlog(20, 'plugins', "no gozerplugs package found")
            plugs = None
        if plugs:
            for i in plugs.__plugs__:
                if i not in avail:
                    try:
                        self.regplugin('gozerplugs', i)
                        avail.append(i)
                    except Exception, ex:
                        handle_exception()
                else:
                    rlog(10, 'plugins', '%s already loaded' % i)

        self.ondisk.extend(avail)
        self.readoverload()
        start_new_thread(self.showregistered, ())

    def readoverload(self):

        """ see if there is a permoverload file and if so use it to overload
            permissions based on function name.
        """
        try:
            overloadfile = open(datadir + os.sep + 'permoverload', 'r')
        except IOError:
            return
        try:
            for i in overloadfile:
                i = i.strip()
                splitted = i.split(',')
                try: