def handle_dispatch(event): """ dispatch web request """ input = unquote_plus(event.path) bot = fleet.getfirstbot() ievent = Ircevent() try: what = input.split('?', 1)[1] except IndexError: return ["dispatch what ?", ] if what.startswith("command="): what = what[8:] ievent.txt = what ievent.nick = 'web' ievent.userhost = 'web@web' ievent.channel = 'web' q = Queue.Queue() ievent.queues.append(q) ievent.speed = 3 ievent.bot = bot result = [] if plugins.woulddispatch(bot, ievent): start_new_thread(plugins.trydispatch, (bot, ievent)) else: return ["can't dispatch %s" % what, ] result = waitforqueue(q, 60) if not result: return ["can't dispatch %s" % what, ] return result
def handle_dispatch(event): """ dispatch web request """ input = unquote_plus(event.path) bot = fleet.getfirstbot() ievent = Ircevent() try: what = input.split('?', 1)[1] except IndexError: return [ "dispatch what ?", ] if what.startswith("command="): what = what[8:] ievent.txt = what ievent.nick = 'web' ievent.userhost = 'web@web' ievent.channel = 'web' q = Queue.Queue() ievent.queues.append(q) ievent.speed = 3 ievent.bot = bot result = [] if plugins.woulddispatch(bot, ievent): start_new_thread(plugins.trydispatch, (bot, ievent)) else: return [ "can't dispatch %s" % what, ] result = waitforqueue(q, 60) if not result: return [ "can't dispatch %s" % what, ] return result
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, 5) if result: ievent.reply("%s sends you this:" % ievent.nick, nick=nick) ievent.reply(result, nick=nick, dot=True) 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_json(event): """ dispatch web request .. return json """ input = unquote_plus(event.path) bot = fleet.getfirstbot() ievent = Ircevent() try: what = input.split('?', 1)[1] except IndexError: return ["dispatch what ?", ] if what.startswith("command="): what = what[8:] ievent.txt = what ievent.nick = 'web' ievent.userhost = 'web@web' ievent.channel = 'web' q = Queue.Queue() ievent.queues.append(q) ievent.speed = 3 ievent.bot = bot result = [] if plugins.woulddispatch(bot, ievent): start_new_thread(plugins.trydispatch, (bot, ievent)) else: return ["can't dispatch %s" % ievent.txt, ] result = waitforqueue(q, 3) rlog(10, 'json', str(result)) try: res = dumps(result) except Exception, ex: handle_exception() res = []
def handle_pit(bot, ievent): if not ievent.inqueue: ievent.reply('use pit in a pipeline') return result = waitforqueue(ievent.inqueue, cfg.get('waitforqueue'), cfg.get('items')) if not result: ievent.reply('no data to paste') # parse options pit_parser = PitOptionParser(ievent) pit_parser.add_option("-p", "--public", dest="public", action="store_true", default=False, help="make paste public") pit_parser.add_option("-s", "--subject", dest="subject", action="store", default="", help="set subject") pit_parser.add_option("-e", "--expiry", dest="expiry", action="store", type="int", default=cfg.get('expiry'), help="expiry in seconds") pit_parser.add_option("-x", "--syntax", dest="syntax", action="store", default="", help="syntax highlighting") (options, args) = pit_parser.parse_args(ievent.rest.split()) if pit_parser.ievent.stop: return # interpret options postarray = [ ('content', '\n'.join(result)), ('nickname', ievent.nick), ('subject', options.subject), ('syntax', options.syntax), ('expiry', options.expiry), ] # public paste? if options.public: postarray.append(("obscure","n")) else: postarray.append(("obscure","y")) postdata = urllib.urlencode(postarray) req = urllib2.Request(url=cfg.get('url'), data=postdata) req.add_header('User-agent', cfg.get('useragent')) ievent.reply(urllib2.urlopen(req).read())
def handle_tr(bot, ievent): if not re_tr.match(ievent.txt.strip()): return ievent.missing('<from lang>to lang> <text>') else: if ievent.inqueue: text = ' '.join(waitforqueue(ievent.inqueue, 5)) elif '>' in ievent.args[0]: text = ' '.join(ievent.args[1:]) else: text = ' '.join(ievent.args[2:]) test_tr = re_tr.search(ievent.args[0]) if not test_tr or not trmap.has_key(test_tr.group(1).lower()) or \ not trmap.has_key(test_tr.group(2).lower()): langs = trmap.keys() langs.sort() return ievent.reply( 'invalid language combination, available languages are: %s' % ', '.join(langs)) lang1 = trmap[test_tr.group(1).lower()] lang2 = trmap[test_tr.group(2).lower()] try: result = translate.translate(lang1, lang2, text) if result: ievent.reply(html_unescape(result)) else: ievent.reply('translation failed (no result)') except TranslateLanguageException, e: ievent.reply(str(e))
def handle_tr(bot, ievent): if not re_tr.match(ievent.txt.strip()): return ievent.missing('<from lang>to lang> <text>') else: if ievent.inqueue: text = ' '.join(waitforqueue(ievent.inqueue, 5)) elif '>' in ievent.args[0]: text = ' '.join(ievent.args[1:]) else: text = ' '.join(ievent.args[2:]) test_tr = re_tr.search(ievent.args[0]) if not test_tr or not trmap.has_key(test_tr.group(1).lower()) or \ not trmap.has_key(test_tr.group(2).lower()): langs = trmap.keys() langs.sort() return ievent.reply('invalid language combination, available languages are: %s' % ', '.join(langs)) lang1 = trmap[test_tr.group(1).lower()] lang2 = trmap[test_tr.group(2).lower()] try: result = translate.translate(lang1, lang2, text) if result: ievent.reply(html_unescape(result)) else: ievent.reply('translation failed (no result)') except TranslateLanguageException, e: ievent.reply(str(e))
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, 5) ievent.reply(str(len(result)))
def handle_reverse(bot, ievent): """ reverse string or pipelined list """ if ievent.inqueue: result = waitforqueue(ievent.inqueue, 5) elif not ievent.rest: ievent.missing('<text to reverse>') return else: result = ievent.rest ievent.reply(result[::-1])
def handle_yahoo_search(bot, ievent): if ievent.inqueue: text = ' '.join(waitforqueue(ievent.inqueue, 5)) elif not ievent.args: ievent.missing('<query>') return else: text = ' '.join(ievent.args) try: search = yahoo.webSearch(text) except YahooException, e: ievent.reply(str(e)) return
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, 30) if not result: ievent.reply('no data') return result = list(sets.Set(result)) if not result: ievent.reply('no result') else: ievent.reply(result, dot=True)
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, 30) if not result: ievent.reply('no data to tail') return ievent.reply(result[-nr:])
def handle_choice(bot, ievent): """ make a random choice out of different words or list elements. """ result = [] if ievent.inqueue: result = waitforqueue(ievent.inqueue, 5) elif not ievent.args: ievent.missing('<space seperated list>') return else: result = ievent.args ievent.reply(random.choice(result))
def handle_translate(bot, ievent): if ievent.inqueue: text = ' '.join(waitforqueue(ievent.inqueue, 5)) elif len(ievent.args) < 3: ievent.missing('<from language> <to language> <text>') return else: text = ' '.join(ievent.args[2:]) try: result = translate.translate(ievent.args[0], ievent.args[1], text) if result: ievent.reply(html_unescape(result)) else: ievent.reply('translation failed (no result)') except TranslateLanguageException, e: ievent.reply(str(e))
def dispatch_POST(server, request): """ dispatch request into the cloud """ try: (host, port) = request.client_address except: return [ "can't determine host/port", ] try: input = getpostdata(request) cmnd = input['cmnd'] except KeyError: return dumps([ 'need cmnd value', ]) try: channel = input['channel'] except KeyError: channel = "#cloud" if not channel: channel = '#cloud' bot = fleet.getfirstbot() ievent = Ircevent() ievent.txt = cmnd ievent.nick = 'cloud' ievent.userhost = "cloud@%s" % host ievent.channel = channel q = Queue.Queue() ievent.queues.append(q) ievent.speed = 3 ievent.bot = bot result = [] if plugins.woulddispatch(bot, ievent): start_new_thread(plugins.trydispatch, (bot, ievent)) else: return dumps([ "can't dispatch %s" % cmnd, ]) result = waitforqueue(q, 10) if not result: return dumps([ "no result", ]) res = [] for item in result: res.append(str(item)) return dumps(res)
def handle_mail(bot, ievent): """ mail result from pipeline to the user giving the command """ if not ievent.inqueue: ievent.reply('use mail in a pipeline') return ievent.reply('waiting for input to mail') result = waitforqueue(ievent.inqueue, 10) if not result: ievent.reply('no data to mail') return username = users.getname(ievent.userhost) email = getemail(username) if email: try: sub = "output of %s" % ievent.origtxt domail(email, result, subject=sub) except Exception, ex: ievent.reply("can't send email: %s" % str(ex)) return ievent.reply('%s lines sent' % len(result))
def dispatch_POST(server, request): """ dispatch request into the cloud """ try: (host, port) = request.client_address except: return ["can't determine host/port", ] try: input = getpostdata(request) cmnd = input['cmnd'] except KeyError: return dumps(['need cmnd value', ]) try: channel = input['channel'] except KeyError: channel = "#cloud" if not channel: channel = '#cloud' bot = fleet.getfirstbot() ievent = Ircevent() ievent.txt = cmnd ievent.nick = 'cloud' ievent.userhost = "cloud@%s" % host ievent.channel = channel q = Queue.Queue() ievent.queues.append(q) ievent.speed = 3 ievent.bot = bot result = [] if plugins.woulddispatch(bot, ievent): start_new_thread(plugins.trydispatch, (bot, ievent)) else: return dumps(["can't dispatch %s" % cmnd, ]) result = waitforqueue(q, 10) if not result: return dumps(["no result", ]) res = [] for item in result: res.append(str(item)) return dumps(res)
def donick(self, nick, setorig=0, save=0, whois=0): """ change nick .. optionally set original nick and/or save to config. """ if not nick: return # disable auto 433 nick changing self.noauto433 = 1 # set up wait for NICK command and issue NICK queue = Queue.Queue() nick = nick[:16] self.wait.register('NICK', self.nick[:16], queue, 12) self._raw('NICK %s\n' % nick) result = waitforqueue(queue, 5) # reenable 433 auto nick changing self.noauto433 = 0 if not result: return 0 self.nick = nick # send whois if whois: self.whois(nick) # set original if setorig: self.orignick = nick # save nick to state and config file if save: self.state['nick'] = nick self.state.save() self.cfg.set('nick', nick) self.cfg.save() return 1
def join(self, channel, password=None, nick=None): """ join conference. """ if '#' in channel: return try: if not nick: nick = channel.split('/')[1] except IndexError: nick = self.nick channel = channel.split('/')[0] if not self.channels.has_key(channel): # init channel data self.channels.setdefault(channel, {}) # setup error wait q = Queue.Queue() self.errorwait.register("409", q, 3) self.errorwait.register("401", q, 3) self.errorwait.register("400", q, 3) # do the actual join presence = xmpp.Presence(to=channel + '/' + nick) #presence.setFrom(self.me) if password: passnode = Node('password') passnode.addData(password) presence.addChild(name='x', namespace='http://jabber.org/protocol/muc', \ payload=[passnode, ]) self.send(presence) errorobj = waitforqueue(q, 3) if errorobj: err = errorobj[0].error rlog(10, self.name, 'error joining %s: %s' % (channel, err)) if err == '409': if channel not in self.channels409: self.channels409.append(channel) return err self.timejoined[channel] = time.time() chan = self.channels[channel] # if password is provided set it chan['nick'] = nick if password: chan['key'] = password # check for control char .. if its not there init to ! if not chan.has_key('cc'): chan['cc'] = config['defaultcc'] or '!' if not chan.has_key('perms'): chan['perms'] = [] self.channels.save() if channel not in self.state['joinedchannels']: self.state['joinedchannels'].append(channel) if channel in self.channels409: self.channels409.remove(channel) self.state.save() return 1
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, 10) 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:
ievent.txt = ievent.txt[1:] plugins.trydispatch(self, ievent) continue elif ievent.txt[0] == "@": # command is broadcast so send response to the paryline # members partyline.say_broadcast_notself( ievent.nick, "[%s] %s" % (ievent.nick, ievent.txt)) # make queue and run trydispatch to see if command has # fired q = Queue.Queue() ievent.queues = [q] ievent.txt = ievent.txt[1:] plugins.trydispatch(self, ievent) # wait for result .. default timeout is 10 sec result = waitforqueue(q, 5) if result: # broadcast result for i in result: partyline.say_broadcast("[bot] %s" % i) continue else: # not a command so send txt to partyline partyline.say_broadcast_notself(ievent.nick, \ "[%s] %s" % (ievent.nick, ievent.txt)) # check PRIVMSG wait self.privwait.check(ievent) except socket.error, ex: try: (errno, errstr) = ex except:
if ievent.txt[0] == "!": ievent.txt = ievent.txt[1:] plugins.trydispatch(self, ievent) continue elif ievent.txt[0] == "@": # command is broadcast so send response to the paryline # members partyline.say_broadcast_notself(ievent.nick, "[%s] %s" % (ievent.nick, ievent.txt)) # make queue and run trydispatch to see if command has # fired q = Queue.Queue() ievent.queues = [q] ievent.txt = ievent.txt[1:] plugins.trydispatch(self, ievent) # wait for result .. default timeout is 10 sec result = waitforqueue(q, 5) if result: # broadcast result for i in result: partyline.say_broadcast("[bot] %s" % i) continue else: # not a command so send txt to partyline partyline.say_broadcast_notself(ievent.nick, \ "[%s] %s" % (ievent.nick, ievent.txt)) # check PRIVMSG wait self.privwait.check(ievent) except socket.error, ex: try: (errno, errstr) = ex except:
return a - b def handle_sort(bot, ievent): """ sort the result list """ parser = SortOptionParser() if not ievent.inqueue: if not ievent.args: ievent.missing('<input>') return try: options, result = parser.parse_args(ievent.args) except SortError, e: ievent.reply(str(e)) return else: result = waitforqueue(ievent.inqueue, 30) try: options, args = parser.parse_args(ievent.rest.split()) except SortError, e: ievent.reply(str(e)) return if not result: ievent.reply('no data to sort') return if options.unique: result = list(set(result)) if options.numeric: result.sort(numeric_compare) else: