def post(self): # stanza = self.get_argument('stanza') from_addr = self.get_argument('from') to_addr = self.get_argument('to').lower() body = self.get_argument('body') logging.info('got message %s from %s to %s' % (body, from_addr, to_addr)) # message = xmpp.Message({'from' : from_addr, 'to' : to_addr, 'body' : body}) token = to_addr.split('@',1)[0] try: jid = lib.lookup_token(token) except: logging.info('no jid found for %s' % token) return self.finish('RECEIVE HOOK NOT FOUND') if not jid: logging.info('jid requested, but not found for %s' % token) return self.finish('RECEIVE HOOK NOT FOUND') for receive_hook in jid.receivehook_set: if receive_hook.endpoint == 'http://example.com/api/receive_endpoint': # skip default continue if not receive_hook.active: continue if lib.match_command(receive_hook.command, body): data = urllib.urlencode({'from':from_addr, 'body': _utf8(body), 'partychat-hook':jid.token, 'on-behalf-of':_utf8(jid.user.nickname())}) try: urlfetch.fetch( receive_hook.endpoint, method='POST', payload=data, headers = {'Content-Type' : 'application/x-www-form-urlencoded'}, follow_redirects=False ) logging.info('send to %s' % receive_hook.endpoint) except: logging.exception('failed writing to %s for %s with data %s' % (receive_hook.endpoint, receive_hook.token, data)) else: logging.info('no command match for %s against %s' % (repr(receive_hook.command), repr(body))) self.finish('DONE')
def match_command(command, body): if command == '*' or \ body.startswith(command) or \ re.findall('^\[[^\]]+\]\s%s' % re.escape(command), _utf8(body)): return True return False