Example #1
0
 def handle_incoming(self): 
     """ handle incoming data. """
     logging.info("rest.client - %s - incoming: %s" % (self.url, self.buffer))
     if not self.reading_headers:
         result = RestResult(self.url, self.name)
         if self.status >= 400:
             logging.warn('rest.client - %s - error status: %s' % (self.url, self.status))
             result.error = self.status
             result.data = None
         elif self.error:
             result.error = self.error
             result.data = None
         elif self.buffer == "":
             result.data = ""
             result.error = None
         else:
             try:
                 res = json.loads(self.buffer)
                 if not res:
                     self.buffer = ''
                     return
                 result.data = res
                 result.error = None
             except ValueError, ex:
                 logging.info("rest.client - %s - can't decode %s" % (self.url, self.buffer))
                 result.error = str(ex)
             except Exception, ex:
                 logging.error("rest.client - %s - %s" % (self.url, exceptionmsg()))
                 result.error = exceptionmsg()                
                 result.data = None
Example #2
0
 def handle_incoming(self):
     """ handle incoming data. """
     logging.info("rest.client - %s - incoming: %s" %
                  (self.url, self.buffer))
     if not self.reading_headers:
         result = RestResult(self.url, self.name)
         if self.status >= 400:
             logging.warn('rest.client - %s - error status: %s' %
                          (self.url, self.status))
             result.error = self.status
             result.data = None
         elif self.error:
             result.error = self.error
             result.data = None
         elif self.buffer == "":
             result.data = ""
             result.error = None
         else:
             try:
                 res = json.loads(self.buffer)
                 if not res:
                     self.buffer = ''
                     return
                 result.data = res
                 result.error = None
             except ValueError, ex:
                 logging.info("rest.client - %s - can't decode %s" %
                              (self.url, self.buffer))
                 result.error = str(ex)
             except Exception, ex:
                 logging.error("rest.client - %s - %s" %
                               (self.url, exceptionmsg()))
                 result.error = exceptionmsg()
                 result.data = None
Example #3
0
 def handle_error(self):
     """ take care of errors. """
     exctype, excvalue, tb = sys.exc_info()
     if exctype == socket.error:
         try:
             errno, errtxt = excvalue
             if errno in [11, 35, 9]:
                 logging.error("res.client - %s - %s %s" %
                               (self.url, errno, errtxt))
                 return
         except ValueError:
             pass
         self.error = str(excvalue)
     else:
         logging.error("%s - %s" % (self.name, exceptionmsg()))
         self.error = exceptionmsg()
     self.buffer = ''
     result = RestResult(self.url, self.name)
     result.error = self.error
     result.data = None
     for cb in self.callbacks:
         try:
             cb(self, result)
             logging.info('rest.client - %s - called callback %s' %
                          (url, str(cb)))
         except Exception, ex:
             handle_exception()
Example #4
0
def handle_reload(bot, ievent):
    """ reload list of plugins. """
    try:
        pluglist = ievent.args
    except IndexError:
        ievent.missing('<list plugins>')
        return
    reloaded = []
    errors = []
    for plug in pluglist:
        modname = bot.plugs.getmodule(plug)
        if not modname:
            errors.append("can't find %s plugin" % plug)
            continue
        try:
            loaded = bot.plugs.reload(modname, force=True, showerror=True)
            for plug in loaded:
                reloaded.append(plug)
                logging.warn("reload - %s reloaded" % plug)
        except NoSuchPlugin:
            errors.append("can't find %s plugin" % plug)
            continue
        except Exception, ex:
            if 'No module named' in str(ex) and plug in str(ex):
                logging.debug('reload - %s - %s' % (modname, str(ex)))
                continue
            errors.append(exceptionmsg())
Example #5
0
 def handle_error(self, request, addr):
     """ log the error """
     ip = request.ip
     exctype, excvalue, tb = sys.exc_info()
     if exctype == socket.timeout:
         logging.warn('rest.server - %s - socket timeout' % (ip, ))
         return
     if exctype == socket.error:
         logging.warn('rest.server - %s - socket error: %s' % (ip, excvalue))
         return
     exceptstr = exceptionmsg()
     logging.warn('rest.server - %s - error %s %s => %s' % (ip, exctype, excvalue, exceptstr))
Example #6
0
 def handle_error(self):
     """ take care of errors. """
     exctype, excvalue, tb = sys.exc_info()
     if exctype == socket.error:
         try:
             errno, errtxt = excvalue
             if errno in [11, 35, 9]:
                 logging.error("res.client - %s - %s %s" % (self.url, errno, errtxt))
                 return
         except ValueError: pass
         self.error = str(excvalue)
     else:
         logging.error("%s - %s" % (self.name, exceptionmsg()))
         self.error = exceptionmsg()
     self.buffer = ''
     result = RestResult(self.url, self.name)
     result.error = self.error
     result.data = None
     for cb in self.callbacks:
         try:
             cb(self, result)
             logging.info('rest.client - %s - called callback %s' % (url, str(cb)))
         except Exception, ex: handle_exception()
     self.close()
Example #7
0
class Dispatch_Handler(RequestHandler):
    """ the bots remote command dispatcher. """
    def options(self):
        self.response.headers.add_header('Content-Type',
                                         'application/x-www-form-urlencoded')
        #self.response.headers.add_header("Cache-Control", "private")
        self.response.headers.add_header("Server", getversion())
        self.response.headers.add_header("Public", "*")
        self.response.headers.add_header('Accept', '*')
        self.response.headers.add_header('Access-Control-Allow-Origin',
                                         self.request.headers['Origin'])
        self.response.out.write("Allow: *")
        self.response.out.write('Access-Control-Allow-Origin: *')
        logging.warn("dispatch - options response send to %s - %s" %
                     (self.request.remote_addr, str(self.request.headers)))

    def post(self):
        """ this is where the command get disaptched. """
        starttime = time.time()
        try:
            logging.warn(
                "DISPATCH incoming: %s - %s" %
                (self.request.get('content'), self.request.remote_addr))
            if not gusers.get_current_user():
                logging.warn(
                    "denied access for %s - %s" %
                    (self.request.remote_addr, self.request.get('content')))
                self.response.out.write("acess denied .. plz login")
                self.response.set_status(400)
                return
            event = WebEvent(bot=bot).parse(self.response, self.request)
            event.cbtype = "DISPATCH"
            event.type = "DISPATCH"
            (userhost, user, u, nick) = checkuser(self.response, self.request,
                                                  event)
            bot.gatekeeper.allow(userhost)
            event.bind(bot)
            bot.doevent(event)
        except NoSuchCommand:
            self.response.out.write("no such command: %s" % event.usercmnd)
        except google.appengine.runtime.DeadlineExceededError, ex:
            self.response.out.write("the command took too long to finish: %s" %
                                    str(time.time() - starttime))
        except Exception, ex:
            self.response.out.write("the command had an eror: %s" %
                                    exceptionmsg())
            handle_exception()
Example #8
0
File: plug.py Project: code2u/jsb
def handle_plugreload(bot, ievent):
    """ arguments: <list of plugnames> - reload list of plugins. """
    try: pluglist = ievent.args
    except IndexError:
        ievent.missing('<list of plugnames>')
        return
    reloaded = []
    errors = []
    for plug in pluglist:
        modname = bot.plugs.getmodule(plug)
        if not modname: errors.append("can't find %s plugin" % plug) ; continue
        try:
            loaded = bot.plugs.reload(modname, force=True, showerror=True)
            for plug in loaded:
                reloaded.append(plug)
                logging.warn("%s reloaded" % plug) 
        except NoSuchPlugin: errors.append("can't find %s plugin" % plug) ; continue
        except Exception, ex:
            if 'No module named' in str(ex) and plug in str(ex):
                logging.debug('%s - %s' % (modname, str(ex)))
                continue
            errors.append(exceptionmsg())
Example #9
0
                plug)
            continue
        try:
            loaded = bot.plugs.reload(modname, force=True, showerror=True)
            for plug in loaded:
                reloaded.append(plug)
                logging.warn("%s reloaded" % plug)
        except RequireError, ex:
            errors.append(str(ex))
            continue
        except NoSuchPlugin:
            errors.append("can't find %s plugin" % plug)
            continue
        except Exception, ex:
            if 'No module named' in str(ex) and plug in str(ex):
                logging.info('%s - %s' % (modname, str(ex)))
                continue
            errors.append(exceptionmsg())
    for modname in reloaded:
        if modname: update_mod(modname)
    if reloaded: ievent.reply('reloaded: ', reloaded)
    else: ievent.reply("nothing to reload")
    if errors:
        t = ievent.jid or ievent.nick or ievent.channel
        bot.say(t, 'errors: ', errors)
        ievent.reply("there were errors (send to %s)" % t)


cmnds.add('plug-reload', handle_plugreload, 'OPER')
examples.add('plug-reload', 'plug-reload <plugin>', 'plug-reload core')
Example #10
0
        starttime = time.time()
        try:
            if not getmainconfig().demo: self.response.set_status(404) ; return
            logging.warn("DEMO incoming: %s - %s" % (self.request.get('content'), self.request.remote_addr))
            event = WebEvent(bot=bot).parse(self.response, self.request)
            event.cbtype = "DISPATCH"
            event.type = "DISPATCH"
            bot.gatekeeper.allow(event.userhost)
            event.bind(bot)
            bot.doevent(event)
        except NoSuchCommand:
            self.response.out.write("no such command: %s" % event.usercmnd)
        except google.appengine.runtime.DeadlineExceededError, ex:
            self.response.out.write("the command took too long to finish: %s" % str(time.time()-starttime))
        except Exception, ex:
            self.response.out.write("the command had an eror: %s" % exceptionmsg())
            handle_exception()

# the application 

application = WSGIApplication([Route('/demo', Demo_Handler),
                               Route('/demo/', Demo_Handler) ], debug=True)

def main():
    global bot
    global application
    try: application.run()
    except google.appengine.runtime.DeadlineExceededError:
        pass
    except Exception, ex:
        logging.error("demo - %s" % str(ex))