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()
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)
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)))
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])
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")
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])
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)
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)
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])
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)
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)
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)
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:])
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')
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')
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:])
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")
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)
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)
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()
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
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)
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
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)
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)
## 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: