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_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 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 iscommand(bot, ievent): """ check to see if ievent is a command """ if not ievent.txt: return 0 try: cc = bot.channels[ievent.channel]['cc'] except (TypeError, KeyError): cc = None txt = "" if cc and ievent.txt[0] == cc: txt = ievent.txt[1:] if ievent.txt.startswith(bot.nick + ':') or \ ievent.txt.startswith(bot.nick + ','): txt = ievent.txt[len(bot.nick)+1:] oldtxt = ievent.txt ievent.txt = txt result = plugins.woulddispatch(bot, ievent) ievent.txt = oldtxt return result
def iscommand(bot, ievent): """ check to see if ievent is a command """ if not ievent.txt: return 0 try: cc = bot.channels[ievent.channel]['cc'] except (TypeError, KeyError): cc = None txt = "" if cc and ievent.txt[0] == cc: txt = ievent.txt[1:] if ievent.txt.startswith(bot.nick + ':') or \ ievent.txt.startswith(bot.nick + ','): txt = ievent.txt[len(bot.nick) + 1:] oldtxt = ievent.txt ievent.txt = txt result = plugins.woulddispatch(bot, ievent) ievent.txt = oldtxt return 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 handle_at(bot, ievent): """ start a job at a certain time. """ if len(ievent.args) < 2: ievent.missing('<time> <command>') return nevent = Ircevent() nevent.copyin(ievent) nevent.txt = ' '.join(ievent.args[1:]) nevent.origtxt = u'!' + nevent.txt if plugins.woulddispatch(bot, nevent): try: when = int(ievent.args[0]) except ValueError, e: when = ievent.args[0] try: AtJob(when, bot, nevent) except JobError: ievent.reply('wrong date/time') return ievent.reply('job scheduled')
def messageHandler(self, conn, msg): """ message handler. """ rlog(5, self.name, 'incoming: %s' % str(msg)) if self.test: return if 'jabber:x:delay' in str(msg): return m = Jabbermsg(msg) m.toirc(self) if m.groupchat and m.getSubject(): self.topiccheck(m) nm = Jabbermsg(msg) nm.copyin(m) print nm jcallbacks.check(self, nm) return self.privwait.check(m) if m.isresponse: return if not m.txt: return if self.me in m.userhost: return 0 if m.groupchat and self.nick == m.resource: return 0 go = 0 try: cc = self.channels[m.channel]['cc'] except (TypeError, KeyError): cc = config['defaultcc'] or '!' try: channick = self.channels[m.channel]['nick'] except (TypeError, KeyError): channick = self.nick if m.msg and not config['noccinmsg']: go = 1 elif m.txt[0] in cc: go = 1 elif m.txt.startswith("%s: " % channick): m.txt = m.txt.replace("%s: " % channick, "") go = 1 elif m.txt.startswith("%s, " % channick): m.txt = m.txt.replace("%s, " % channick, "") go = 1 if m.txt[0] in cc: m.txt = m.txt[1:] if go: try: if plugins.woulddispatch(self, m): m.usercmnd = True plugins.trydispatch(self, m) except: handle_exception() try: nm = Jabbermsg(msg) nm.copyin(m) jcallbacks.check(self, nm) if nm.getType() == 'error': err = nm.getErrorCode() if err: rlog(10, self.name + '.error', "%s => %s: %s" % (nm.getFrom(),\ err, nm.getError())) rlog(10, self.name + '.error', str(nm)) self.errorwait.check(nm) try: method = getattr(self, 'handle_' + err) # try to call method try: method(nm) except: handle_exception() except AttributeError: # no command method to handle event pass except Exception, ex: handle_exception()
def messageHandler(self, conn, msg): """ message handler. """ rlog(5, self.name, 'incoming: %s' % str(msg)) if self.test: return if 'jabber:x:delay' in str(msg): return m = Jabbermsg(msg) m.toirc(self) if m.groupchat and m.getSubject(): self.topiccheck(m) nm = Jabbermsg(msg) nm.copyin(m) print nm jcallbacks.check(self, nm) return self.privwait.check(m) if m.isresponse: return if not m.txt: return if self.me in m.userhost: return 0 if m.groupchat and self.nick == m.resource: return 0 go = 0 try: cc = self.channels[m.channel]['cc'] except (TypeError, KeyError): cc = config['defaultcc'] or '!' try: channick = self.channels[m.channel]['nick'] except (TypeError, KeyError): channick = self.nick if m.msg and not config['noccinmsg']: go = 1 elif m.txt[0] in cc: go = 1 elif m.txt.startswith("%s: " % channick): m.txt = m.txt.replace("%s: " % channick, "") go = 1 elif m.txt.startswith("%s, " % channick): m.txt = m.txt.replace("%s, " % channick, "") go = 1 if m.txt[0] in cc: m.txt = m.txt[1:] if go: try: if plugins.woulddispatch(self, m): m.usercmnd = True plugins.trydispatch(self, m) except: handle_exception() try: nm = Jabbermsg(msg) nm.copyin(m) jcallbacks.check(self, nm) if nm.getType() == 'error': err = nm.getErrorCode() if err: rlog(10, self.name + '.error', "%s => %s: %s" % (nm.getFrom(),\ err, nm.getError())) rlog(10, self.name + '.error', str(nm)) self.errorwait.check(nm) try: method = getattr(self,'handle_' + err) # try to call method try: method(nm) except: handle_exception() except AttributeError: # no command method to handle event pass except Exception, ex: handle_exception()