diff options
author | John Smith <mrdoctorwho@gmail.com> | 2017-01-22 08:37:29 +0300 |
---|---|---|
committer | John Smith <mrdoctorwho@gmail.com> | 2017-01-22 08:37:29 +0300 |
commit | 1a7cbfd6f2914f43e1caea6998930088b736cb2d (patch) | |
tree | 867b23bcd906a3dcba29c10a2ed5b69ec7a3882e | |
parent | f8fbc733c0179be5f69552479af2dc516a7b100e (diff) |
Fix AttributeError in captcha_forms (#148)
-rw-r--r-- | extensions/captcha_forms.py | 52 | ||||
-rw-r--r-- | gateway.py | 8 |
2 files changed, 33 insertions, 27 deletions
diff --git a/extensions/captcha_forms.py b/extensions/captcha_forms.py index b701dec..9f5018a 100644 --- a/extensions/captcha_forms.py +++ b/extensions/captcha_forms.py @@ -3,50 +3,56 @@ # © simpleApps, 2014 — 2015. from hashlib import sha1 +import xmpp """ Implements XEP-0158: CAPTCHA Forms """ -def sendCaptcha(vk, url): - logger.debug("VK: sending message with captcha to %s", vk.source) +def sendCaptcha(user, captcha): + """ + Send a captcha to the user + Args: + user: the user's jid + captcha: captcha dictionary ({"url": "https://vk.com/...", "sid": "10"}) + """ + url = captcha.get("img") + sid = captcha.get("sid") + logger.debug("VK: sending message with captchaXMPP (jid: %s)", user) body = _("WARNING: VK has sent you a CAPTCHA." " Please, follow the link: %s and enter the text shown on the image to the chat." - " Example: !captcha my_captcha_key." + " Example: !captchaXMPP my_captcha_key." "\nWarning: don't use Firefox to open the link.") % url - msg = xmpp.Message(vk.source, body, "chat", frm=TransportID) - msg.setID(vk.engine.captcha["sid"]) + msg = xmpp.Message(user, body, "chat", frm=TransportID) x = msg.setTag("x", namespace=xmpp.NS_OOB) x.setTagData("url", url) - captcha = msg.setTag("captcha", namespace=xmpp.NS_CAPTCHA) + captchaNode = msg.setTag("captchaXMPP", namespace=xmpp.NS_CAPTCHA) image = utils.getLinkData(url, False) if image: hash = sha1(image).hexdigest() encoded = image.encode("base64") - form = utils.buildDataForm(type="form", fields=[ - {"var": "FORM_TYPE", "value": xmpp.NS_CAPTCHA, "type": "hidden"}, - {"var": "from", "value": TransportID, "type": "hidden"}, - {"var": "challenge", "value": msg.getID(), "type": "hidden"}, - {"var": "ocr", "label": _("Enter shown text"), - "payload": [xmpp.Node("required"), - xmpp.Node("media", {"xmlns": xmpp.NS_MEDIA}, - [xmpp.Node("uri", {"type": "image/jpg"}, - ["cid:sha1+%s@bob.xmpp.org" % hash] - ) - ]) - ]} - ] - ) - captcha.addChild(node=form) + payload = [xmpp.Node("required"), + xmpp.Node("media", {"xmlns": xmpp.NS_MEDIA}, + [xmpp.Node("uri", {"type": "image/jpg"}, + ["cid:sha1+%s@bob.xmpp.org" % hash])])] # feel yourself like a erlang programmer + fields = [{"var": "FORM_TYPE", "value": xmpp.NS_CAPTCHA, "type": "hidden"}] + fields.append({"var": "from", "value": TransportID, "type": "hidden"}) + fields.append({"var": "challenge", "value": msg.getID(), "type": "hidden"}) + fields.append({"var": "ocr", "label": _("Enter shown text"), "payload": payload}) + form = utils.buildDataForm(type="form", fields=fields) + captchaNode.addChild(node=form) oob = msg.setTag("data", {"cid": "sha1+%s@bob.xmpp.org" % hash, "type": "image/jpg", "max-age": "0"}, xmpp.NS_URN_OOB) oob.setData(encoded) + else: + logger.warning("unable to get the image from %s (jid: %s)", url, user) + msg.setID(sid) sender(Component, msg) - sendPresence(vk.source, TransportID, show="xa", reason=body, hash=USER_CAPS_HASH) + sendPresence(user, TransportID, show="xa", reason=body, hash=USER_CAPS_HASH) TransportFeatures.update({xmpp.NS_OOB, xmpp.NS_MEDIA, xmpp.NS_CAPTCHA, xmpp.NS_URN_OOB}) -registerHandler("evt04", sendCaptcha)
\ No newline at end of file +registerHandler("evt04", sendCaptcha) @@ -350,7 +350,7 @@ class VK(object): raise except api.CaptchaNeeded as e: - executeHandlers("evt04", (self, self.engine.captcha["img"])) + executeHandlers("evt04", (self.source, self.engine.captcha)) self.online = False except api.NetworkNotFound as e: @@ -364,7 +364,7 @@ class VK(object): except api.VkApiError as e: # There are several types of VkApiError - # But the user defenitely must be removed. + # But the user definitely must be removed. # The question is: how? # Should we completely exterminate them or just remove? roster = False @@ -556,7 +556,7 @@ class User(object): def __init__(self, source=""): self.friends = {} self.typing = {} - self.source = source + self.source = source # TODO: rename to jid self.lastMsgID = 0 self.rosterSet = None @@ -603,7 +603,7 @@ class User(object): except api.CaptchaNeeded: self.sendSubPresence() logger.error("User: running captcha challenge (jid: %s)", self.source) - executeHandlers("evt04", (self, vk.engine.captcha["img"])) + executeHandlers("evt04", (self.source, self.vk.engine.captcha)) return True else: logger.debug("User seems to be authenticated (jid: %s)", self.source) |