def parse_captcha_iq(self, jid, node): if node['type'] != 'set': return print "parse captcha" nick, password, cid, cookie, prs = self.captcha_cache.get( jid, [None, None, None, None, None]) if not cid or not cookie or not prs: return self.captcha_cache.pop(jid) try: captcha = node.getTag("captcha", {"xmlns": "urn:xmpp:captcha"}) #print unicode(captcha) captcha = captcha.getTag("x").getTag("field", { "var": "ocr" }).getTag("value").getData() except: captcha = "" print "c =", captcha cookie, result = self.chat.login(nick, password, captcha=captcha, cookie=cookie) if result['status'] != 'ok': send(gen_error(prs, "406", "modify", "not-acceptable")) send(gen_error(prs, "401", "auth", "not-authorized")) if not self.has_jabber_clients(): kill_room(self.name) raise NodeProcessed #self.cookies[jid] = cookie self.clients[jid] = nick self.send_last20(jid) self.set_prs_status(jid, prs)
def parse_presence(node): if node['type'] == 'error': return jid, resource = jid_split(node['to']) #node['to'] = jid name = jid[:jid.find("@")].encode("utf-8") room = rooms.get(name) if room: #joined = rooms[name].clients.has_key(node['from']) joined = rooms[name].has_jabber_clients(node['from']) else: joined = False if not joined and not resource: return passwd = node.getTag("x", {"xmlns": "http://jabber.org/protocol/muc"}) if passwd: passwd = passwd.getTag("password") if passwd: passwd = passwd.getData() if not room: if node['type'] == 'unavailable': return try: room = create_room(name) except chatovod.ChatovodError: send(gen_error(node, "404", "cancel", "remote-server-not-found")) return if node['type'] == 'unavailable': room.leave(node['from'], node) if not room.has_jabber_clients(): kill_room(name) else: room.join(node['from'], resource, prs=node, password=passwd)
def parse_node(node): if node.getName() == "iq": if node['to'] == host: parse_my_iq(node) else: parse_iq(node) if node['type'] in ('get', 'set'): send(gen_error(node, "501", "cancel", "feature-not-implemented")) elif node.getName() == "presence": if node['to'] == host: pass else: parse_presence(node) elif node.getName() == "message": if node['to'] == host: pass else: parse_message(node)
def process(): node = read() try: parse_node(node) except NodeProcessed: pass except KeyboardInterrupt: raise except: try: #print unicode(node) #print #print unicode(gen_error(node)) send(gen_error(node)) except: traceback.print_exc() raise
def join(self, jid, nick, prs, password=None): if self.clients.get(jid) == nick: self.set_prs_status(jid, prs) return if self.clients.get( jid): # or (nick != u"anonymous" and not password): send(gen_error(prs, "409", "cancel", "conflict")) return if self.chat_users.get(nick) and self.clients.get(jid) != nick: send(gen_error(prs, "409", "cancel", "conflict")) return if nick == u"anonymous": self.clients[jid] = nick self.send_last20(jid) prs = Node( "presence", { "type": "available", "from": self.name + u"@" + host + u"/" + nick, "to": jid }) x = prs.addChild("x") x.setNamespace("http://jabber.org/protocol/muc#user") x.addChild("item", {"affiliation": "none", "role": "visitor"}) send(prs) return cookie, result = self.chat.login(nick, password if password else "") if result['status'] == 'ok': self.clients[jid] = nick self.send_last20(jid) self.set_prs_status(jid, prs) return elif result['status'] == 'needpassword': send(gen_error(prs, "401", "auth", "not-authorized")) return captcha = self.chat.getcaptcha(cookie=cookie) chash = sha1(captcha).hexdigest() cid = "sha1+" + chash + "@bob.xmpp.org" cid = str(random.randrange(1000000, 10000000)) self.captcha_cache[jid] = [nick, password, cid, cookie, prs] msg = Node("message", { "from": self.name + u"@" + host, "to": jid, "id": cid }) msg.addChild("body").addData(u"Увы, капча") c = msg.addChild("captcha") c.setNamespace("urn:xmpp:captcha") x = c.addChild("x", {"type": "form"}) x.setNamespace("jabber:x:data") x.addChild("field", { "type": "hidden", "var": "FORM_TYPE" }).addChild("value").addData("urn:xmpp:captcha") x.addChild("field", { "type": "hidden", "var": "from" }).addChild("value").addData(self.name + u"@" + host) x.addChild("field", { "type": "hidden", "var": "challenge" }).addChild("value").addData(cid) x.addChild("field", {"type": "hidden", "var": "sid"}).addChild("value") l = x.addChild("field", { "label": u"Введите увиденный текст", "var": "ocr" }) l.addChild("required") m = l.addChild("media") m.setNamespace("urn:xmpp:media-element") m.addChild("uri", {"type": "image/png"}).addData(cid) d = msg.addChild("data", { "cid": cid, "type": "image/png", "mag-age": "0" }) d.setNamespace("urn:xmpp:bob") d.addData(b64encode(captcha)) send(msg)