Exemplo n.º 1
0
Arquivo: bot.py Projeto: code2u/jsb
 def startshell(self, connect=True):
     """ start the console bot. """
     self.start(False)
     time.sleep(0.1)
     self.dostart()
     while not self.stopped: 
         try: 
             input = console.raw_input("> ")
             event = ConsoleEvent()
             event.parse(self, input, console)
             event.showall = True
             if False and input.startswith('#'):
                 try:
                     env = {"bot": self, "event": event}
                     env.update(locals())
                     env.update(globals())
                     console.locals.update(env)
                     console.runsource(input[1:])
                     continue
                 except Exception, ex:
                     handle_exception()
                     continue
             self.put(event)
             if self.shouldwait: waitforqueue(event.resqueue)
             time.sleep(0.2)
         except NoInput: continue
         except (KeyboardInterrupt, EOFError): break
         except Exception, ex: handle_exception()
Exemplo n.º 2
0
Arquivo: grep.py Projeto: code2u/jsb
def handle_grep(bot, ievent):
    """ no arguments - grep the result list, use this command in a pipeline. """
    if not ievent.inqueue: ievent.reply('use grep in a pipeline') ; return
    if not ievent.rest: ievent.reply('grep <txt>') ; return
    try: (options, rest) = getopt.getopt(ievent.args, 'riv')
    except getopt.GetoptError, ex: ievent.reply(str(ex)) ; return
    result = waitforqueue(ievent.inqueue, 3000)
    if not result: ievent.reply('no data to grep on: %s' % ievent.txt) ; return
    doregex = False
    docasein = False
    doinvert = False
    for i, j in options:
        if i == '-r': doregex = True
        if i == '-i': docasein = True
        if i == '-v': doinvert = True
    res = []
    if doregex:
        try:
            if docasein: reg = re.compile(' '.join(rest), re.I)
            else: reg = re.compile(' '.join(rest))
        except Exception, ex:
            ievent.reply("can't compile regex: %s" % str(ex))
            return
        if doinvert:
            for i in result:
                if not re.search(reg, i): res.append(i)
        else:
            for i in result:
                if re.search(reg, i): res.append(i)
Exemplo n.º 3
0
def handle_count(bot, ievent):
    """ show nr of elements in result list. """
    if not ievent.inqueue:
        ievent.reply("use count in a pipeline")
        return
    result = waitforqueue(ievent.inqueue, 5000)
    ievent.reply(str(len(result)))
Exemplo n.º 4
0
def handle_reverse(bot, ievent):
    """ arguments: [<string>] - reverse string or use in a pipeline. """
    if ievent.rest: result = ievent.rest
    else: result = waitforqueue(ievent.inqueue, 5000)
    if not result: ievent.reply("reverse what?") ; return
    if type(result) == types.ListType: ievent.reply("results: ", result[::-1])
    else: ievent.reply(result[::-1])
Exemplo n.º 5
0
def fleet_cmnd(bot, ievent):
    """ co cmnd on fleet bot(s). """
    try:
        (name, cmndtxt) = ievent.rest.split(' ', 1)
    except ValueError:
        ievent.missing("<name> <cmndstring>")
        return
    fleet = getfleet()
    if name == "all": do = fleet.list()
    else: do = [
        name,
    ]
    for botname in do:
        bot = fleet.byname(botname)
        if not bot:
            ievent.reply("%s bot is not in fleet" % botname)
            return
        result = bot.docmnd(ievent.userhost,
                            ievent.channel,
                            cmndtxt,
                            wait=1,
                            nooutput=True)
        if result: res = waitforqueue(result.outqueue, 60000)
        else: ievent.reply("no result")
        ievent.reply("[%s] %s" % (botname, ", ".join(res)))
    ievent.reply("done")
Exemplo n.º 6
0
def handle_reverse(bot, ievent):
    """ reverse string or pipelined list. """
    if not ievent.rest and ievent.inqueue: result = waitforqueue(ievent.inqueue, 5000)
    elif not ievent.rest: ievent.missing('<text to reverse>') ; return
    else: result = ievent.rest
    if type(result) == types.ListType: ievent.reply("results: ", result[::-1])
    else: ievent.reply(result[::-1])
Exemplo n.º 7
0
Arquivo: choice.py Projeto: code2u/jsb
def handle_choice(bot, ievent):
    """ arguments: [<space seperated strings>] - make a random choice out of different words or list elements. when used in a pipeline will choose from that. """ 
    result = []
    if ievent.args: result = ievent.args
    elif ievent.inqueue: result = waitforqueue(ievent.inqueue, 3000)
    else: ievent.missing('<space seperated list>') ; return
    if result: ievent.reply(random.choice(result))
    else: ievent.reply('nothing to choose from: %s' % ievent.txt)
Exemplo n.º 8
0
def handle_choice(bot, ievent):
    """ make a random choice out of different words or list elements. """
    result = []
    if ievent.args: result = ievent.args
    elif ievent.inqueue: result = waitforqueue(ievent.inqueue, 3000)
    else:
        ievent.missing('<space seperated list>')
        return
    if result: ievent.reply(random.choice(result))
    else: ievent.reply('nothing to choose from: %s' % ievent.txt)
Exemplo n.º 9
0
def handle_reverse(bot, ievent):
    """ reverse string or pipelined list. """
    if not ievent.rest and ievent.inqueue:
        result = waitforqueue(ievent.inqueue, 5000)
    elif not ievent.rest:
        ievent.missing('<text to reverse>')
        return
    else:
        result = ievent.rest
    if type(result) == types.ListType: ievent.reply("results: ", result[::-1])
    else: ievent.reply(result[::-1])
Exemplo n.º 10
0
def handle_uniq(bot, ievent):
    """ uniq the result list """
    if not ievent.inqueue:
        ievent.reply('use uniq in a pipeline')
        return
    result = waitforqueue(ievent.inqueue, 3000)
    if not result:
        ievent.reply('no data')
        return
    result = list(result)
    if not result: ievent.reply('no result')
    else: ievent.reply(result)
Exemplo n.º 11
0
def handle_uniq(bot, ievent):
    """ uniq the result list """
    if not ievent.inqueue:
        ievent.reply('use uniq in a pipeline')
        return
    result = waitforqueue(ievent.inqueue, 3000)
    if not result:
        ievent.reply('no data')
        return
    result = list(result)
    if not result: ievent.reply('no result')
    else: ievent.reply(result)
Exemplo n.º 12
0
Arquivo: uniq.py Projeto: code2u/jsb
def handle_uniq(bot, ievent):
    """ no arguments - uniq the result list, use this command in a pipeline. """
    if not ievent.inqueue:
        ievent.reply('use uniq in a pipeline')
        return
    result = waitforqueue(ievent.inqueue, 3000)
    if not result:
        ievent.reply('no data')
        return
    result = list(result)
    if not result: ievent.reply('no result')
    else: ievent.reply("result: ", result)
Exemplo n.º 13
0
 def wait(self, nr=1000):
     nr = int(nr)
     result = []
     #if self.nodispatch: return
     if not self.busy: self.startout()
     self.finished.acquire()
     while nr > 0 and (self.busy and not self.dostop): self.finished.wait(0.1) ; nr -= 100
     self.finished.release()
     if self.wait and self.thread: logging.warn("joining thread %s" % self.thread) ; self.thread.join(nr/1000)
     if not "TICK" in self.cbtype: logging.info(self.busy)
     if not self.resqueue: res = waitforqueue(self.resqueue, nr)
     else: res = self.resqueue
     return list(res)
Exemplo n.º 14
0
Arquivo: tail.py Projeto: code2u/jsb
def handle_tail(bot, ievent):
    """ no arguments - show last <nr> elements, use this command in a pipeline. """
    if not ievent.inqueue:
        ievent.reply("use tail in a pipeline")
        return
    try: nr = int(ievent.args[0])
    except (ValueError, IndexError):
        ievent.reply('tail <nr>')
        return
    result = waitforqueue(ievent.inqueue, 3000)
    if not result:
        ievent.reply('no data to tail')
        return
    ievent.reply('results: ', result[-nr:])
Exemplo n.º 15
0
Arquivo: to.py Projeto: code2u/jsb
def handle_to(bot, ievent):
    """ arguments: <nick> - direct output to <nick>, use this command in a pipeline. """
    if not ievent.inqueue: ievent.reply('use to in a pipeline') ; return
    try: nick = ievent.args[0]
    except IndexError: ievent.reply('to <nick>') ; return
    if nick == 'me': nick = ievent.nick
    if not getwho(bot, nick): ievent.reply("don't know %s" % nick) ; return
    result = waitforqueue(ievent.inqueue, 5000)
    if result:
        bot.say(nick, "%s sends you this:" % ievent.nick)
        bot.say(nick, " ".join(result))
        if len(result) == 1: ievent.reply('1 element sent')
        else: ievent.reply('%s elements sent' % len(result))
    else: ievent.reply('nothing to send')
Exemplo n.º 16
0
def handle_to(bot, ievent):
    """ direct pipeline output to <nick>. """
    if not ievent.inqueue: ievent.reply('use to in a pipeline') ; return
    try: nick = ievent.args[0]
    except IndexError: ievent.reply('to <nick>') ; return
    if nick == 'me': nick = ievent.nick
    if not getwho(bot, nick): ievent.reply("don't know %s" % nick) ; return
    result = waitforqueue(ievent.inqueue, 5000)
    if result:
        bot.say(nick, "%s sends you this:" % ievent.nick)
        bot.say(nick, " ".join(result))
        if len(result) == 1: ievent.reply('1 element sent')
        else: ievent.reply('%s elements sent' % len(result))
    else: ievent.reply('nothing to send')
Exemplo n.º 17
0
def handle_tail(bot, ievent):
    """ used in a pipeline .. show last <nr> elements. """
    if not ievent.inqueue:
        ievent.reply("use tail in a pipeline")
        return
    try:
        nr = int(ievent.args[0])
    except (ValueError, IndexError):
        ievent.reply('tail <nr>')
        return
    result = waitforqueue(ievent.inqueue, 3000)
    if not result:
        ievent.reply('no data to tail')
        return
    ievent.reply('results: ', result[-nr:])
Exemplo n.º 18
0
def fleet_cmnd(bot, ievent):
    """ co cmnd on fleet bot(s). """
    try:
        (name, cmndtxt) = ievent.rest.split(' ', 1)
    except ValueError: ievent.missing("<name> <cmndstring>") ; return
    fleet = getfleet()
    if name == "all": do = fleet.list()
    else: do = [name, ]
    for botname in do:
        bot = fleet.byname(botname)
        if not bot: ievent.reply("%s bot is not in fleet" % botname) ; return
        result = bot.docmnd(ievent.userhost, ievent.channel, cmndtxt, wait=1, nooutput=True)
        if result: res = waitforqueue(result.outqueue, 60000)
        else: ievent.reply("no result")
        ievent.reply("[%s] %s" % (botname, ", ".join(res)))
    ievent.reply("done")
Exemplo n.º 19
0
 def wait(self, nr=1000):
     nr = int(nr)
     result = []
     #if self.nodispatch: return
     if not self.busy: self.startout()
     self.finished.acquire()
     if self.threaded and self.untildone:
         logging.info("waiting until done")
         while 1: self.finished.wait(0.1)
     else:
         while nr > 0 and (self.busy and not self.dostop): self.finished.wait(0.1) ; nr -= 100
     self.finished.release()
     if self.wait and self.thread: logging.warn("joining thread %s" % self.thread) ; self.thread.join(nr/1000)
     if not "TICK" in self.cbtype: logging.info(self.busy)
     if not self.resqueue: res = waitforqueue(self.resqueue, nr)
     else: res = self.resqueue
     return list(res)
Exemplo n.º 20
0
 def gettopic(self, channel, event=None):
     """ get topic data. """
     q = Queue.Queue()
     i332 = waiter.register("332", queue=q)
     i333 = waiter.register("333", queue=q)
     self.putonqueue(7, None, 'TOPIC %s' % channel)
     res = waitforqueue(q, 5000)
     who = what = when = None
     for r in res:
         if not r.postfix: continue
         try:
             if r.cmnd == "332": what = r.txt ; waiter.ready(i332) ; continue
             waiter.ready(i333)
             splitted = r.postfix.split()
             who = splitted[2]
             when = float(splitted[3])
         except (IndexError, ValueError): continue
         return (what, who, when)
Exemplo n.º 21
0
 def gettopic(self, channel, event=None):
     """ get topic data. """
     q = Queue.Queue()
     i332 = waiter.register("332", queue=q)
     i333 = waiter.register("333", queue=q)
     self.putonqueue(7, None, 'TOPIC %s' % channel)
     res = waitforqueue(q, 5000)
     who = what = when = None
     for r in res:
         if not r.postfix: continue
         try:
             if r.cmnd == "332": what = r.txt ; waiter.ready(i332) ; continue
             waiter.ready(i333)
             splitted = r.postfix.split()
             who = splitted[2]
             when = float(splitted[3])
         except (IndexError, ValueError): continue
         return (what, who, when)
Exemplo n.º 22
0
def handle_feedback(bot, event):
    """ 
        arguments: <feedbacktxt> - give feedback to [email protected], this needs a jabber server to be able to send the feedback.
        the feedback command can be used in a pipeline.
    
    """
    if not event.rest and event.inqueue:
        payload = waitforqueue(event.inqueue, 2000)
    else:
        payload = event.rest
    fleet = getfleet()
    feedbackbot = fleet.getfirstjabber()
    if not feedbackbot:
        event.reply("can't find an xmpp bot to send the feedback with")
        return
    event.reply("sending to [email protected]")
    feedbackbot.say("*****@*****.**", "%s send you this: %s" % (event.userhost, payload), event=event)
    event.done()
Exemplo n.º 23
0
 def cmnd(self, event, name, cmnd):
     """ do command on a bot. """
     bot = self.byname(name)
     if not bot: return 0
     from jsb.lib.eventbase import EventBase
     j = plugs.clonedevent(bot, event)
     j.onlyqueues = True
     j.txt = cmnd
     q = Queue.Queue()
     j.queues = [q]
     j.speed = 3
     plugs.trydispatch(bot, j)
     result = waitforqueue(q, 3000)
     if not result: return
     res = ["[%s]" % bot.botname, ]
     res += result
     event.reply(res)
     return res
Exemplo n.º 24
0
def handle_feedback(bot, event):
    """ 
        arguments: <feedbacktxt> - give feedback to [email protected], this needs a jabber server to be able to send the feedback.
        the feedback command can be used in a pipeline.
    
    """
    if not event.rest and event.inqueue:
        payload = waitforqueue(event.inqueue, 2000)
    else:
        payload = event.rest
    fleet = getfleet()
    feedbackbot = fleet.getfirstjabber()
    if not feedbackbot:
        event.reply("can't find an xmpp bot to send the feedback with")
        return
    event.reply("sending to [email protected]")
    feedbackbot.say("*****@*****.**",
                    "%s send you this: %s" % (event.userhost, payload),
                    event=event)
    event.done()
Exemplo n.º 25
0
 def waitfor(self, millisec=10000):
     """ wait for the event to finish. """
     logging.warn("eventbase - waiting for %s" % self.txt)
     self.finished.wait(5)
     return waitforqueue(self.resqueue, millisec)
Exemplo n.º 26
0
     ievent.sock = sock
     ievent.speed = 1
     ievent.isdcc = True
     ievent.msg = True
     ievent.bind(self)
     logging.debug("%s - dcc - constructed event" % self.name)
     if ievent.txt[0] == "!":
         self.doevent(ievent)
         continue
     elif ievent.txt[0] == "@":
         partyline.say_broadcast_notself(ievent.nick, "[%s] %s" % (ievent.nick, ievent.txt))
         q = Queue.Queue()
         ievent.queues = [q]
         ievent.txt = ievent.txt[1:]
         self.doevent(ievent)
         result = waitforqueue(q, 3000)
         if result:
             for i in result:
                 partyline.say_broadcast("[bot] %s" % i)
         continue
     else:
         partyline.say_broadcast_notself(ievent.nick, "[%s] %s" % (ievent.nick, ievent.txt))
     self.privwait.check(ievent)
 except socket.error, ex:
     try:
         (errno, errstr) = ex
     except:
         errno = 0
         errstr = str(ex)
     if errno == 35 or errno == 11:
         continue
Exemplo n.º 27
0

def handle_not(bot, ievent):
    """ negative grep. """
    if not ievent.inqueue:
        ievent.reply('use not in a pipeline')
        return
    if not ievent.rest:
        ievent.reply('not <txt>')
        return
    try:
        (options, rest) = getopt.getopt(ievent.args, 'r')
    except getopt.GetoptError, ex:
        ievent.reply(str(ex))
        return
    result = waitforqueue(ievent.inqueue, 3000)
    if not result:
        ievent.reply('no data to grep on')
        return
    doregex = False
    for i, j in options:
        if i == '-r': doregex = True
    res = []
    if doregex:
        try:
            reg = re.compile(' '.join(rest))
        except Exception, ex:
            ievent.reply("can't compile regex: %s" % str(ex))
            return
        for i in result:
            if not re.search(reg, i): res.append(i)
Exemplo n.º 28
0
 def waitfor(self, millisec=10000):
     """ wait for the event to finish. """
     logging.warn("eventbase - waiting for %s" % self.txt)
     self.finished.wait(5)
     return waitforqueue(self.resqueue , millisec)
Exemplo n.º 29
0
Arquivo: not.py Projeto: code2u/jsb
## not command

def handle_not(bot, ievent):
    """ no arguments - negative grep, use this command in a pipeline. """
    if not ievent.inqueue:
        ievent.reply('use not in a pipeline')
        return
    if not ievent.rest:
        ievent.reply('not <txt>')
        return
    try: (options, rest) = getopt.getopt(ievent.args, 'r')
    except getopt.GetoptError, ex:
        ievent.reply(str(ex))
        return
    result = waitforqueue(ievent.inqueue, 3000)
    if not result:
        ievent.reply('no data to grep on')
        return
    doregex = False
    for i, j in options:
        if i == '-r': doregex = True
    res = []
    if doregex:
        try: reg = re.compile(' '.join(rest))
        except Exception, ex:
            ievent.reply("can't compile regex: %s" % str(ex))
            return
        for i in result:
            if not re.search(reg, i): res.append(i)
    else: