diff options
author | mrDoctorWho <mrdoctorwho@gmail.com> | 2015-04-25 21:42:38 +0300 |
---|---|---|
committer | mrDoctorWho <mrdoctorwho@gmail.com> | 2015-04-25 21:42:38 +0300 |
commit | d33f4b449dd426ab9a32ec499a10dd7ac6b34be3 (patch) | |
tree | b1999bcca94a080045b4ed8de8c1acc3e85b1562 | |
parent | 819131477fc2f1cacecba173772dd7d0cb97c994 (diff) |
Fixes #90: properly handle CaptchaChallenge when user registers
-rw-r--r-- | extensions/avatar_hash.py | 2 | ||||
-rw-r--r-- | extensions/captcha_forms.py | 1 | ||||
-rw-r--r-- | gateway.py | 22 | ||||
-rw-r--r-- | library/defaults.py | 4 | ||||
-rw-r--r-- | library/vkapi.py | 2 | ||||
-rw-r--r-- | modules/mod_iq_register.py | 1 | ||||
-rw-r--r-- | modules/mod_prs_main.py | 41 |
7 files changed, 29 insertions, 44 deletions
diff --git a/extensions/avatar_hash.py b/extensions/avatar_hash.py index ed0ac31..e8b55ac 100644 --- a/extensions/avatar_hash.py +++ b/extensions/avatar_hash.py @@ -55,4 +55,4 @@ if isdef("ENABLE_PHOTO_HASHES") and ENABLE_PHOTO_HASHES: registerHandler("prs02", addPresenceHash) else: - del sha1, makePhotoHash, addPresenceHash
\ No newline at end of file + del makePhotoHash, addPresenceHash
\ No newline at end of file diff --git a/extensions/captcha_forms.py b/extensions/captcha_forms.py index 5173932..84cd8ac 100644 --- a/extensions/captcha_forms.py +++ b/extensions/captcha_forms.py @@ -4,7 +4,6 @@ from hashlib import sha1 - def sendCaptcha(user, url): logger.debug("VKLogin: sending message with captcha to %s" % user.source) body = _("WARNING: VK has sent you a CAPTCHA." @@ -225,6 +225,8 @@ class VK(object): try: int(self.method("isAppUser", force=True)) except (api.VkApiError, TypeError, AttributeError): + import traceback + traceback.print_exc() return False return True @@ -289,18 +291,13 @@ class VK(object): if not self.engine.captcha and (self.online or force): try: result = self.engine.method(method, args) - except (api.InternalServerError, api.AccessDenied) as e: - # To prevent returning True from checkData() + except (api.InternalServerError, api.AccessDenied): if force: raise except api.NetworkNotFound as e: self.online = False - except api.CaptchaNeeded as e: - logger.error("VK: running captcha challenge (jid: %s)", self.source) - executeHandlers("evt04", (self, self.engine.captcha["img"])) - except api.NotAllowed as e: if self.engine.lastMethod[0] == "messages.send": sendMessage(Component, self.source, @@ -348,7 +345,7 @@ class VK(object): Which will be added in the result values """ fields = fields or self.friends_fields - raw = self.method("friends.get", {"fields": str.join(chr(44), fields)}) + raw = self.method("friends.get", {"fields": str.join(chr(44), fields)}) or () friends = {} for friend in raw: uid = friend["uid"] @@ -475,12 +472,13 @@ class User(object): pwd = api.PasswordLogin(username, password).login() token = pwd.confirm() - vk = VK(token, self.source) + self.vk = vk = VK(token, self.source) try: auth = vk.auth() except api.CaptchaNeeded: self.sendSubPresence() - executeHandlers("evt04", (user, url)) + logger.error("User: running captcha challenge (jid: %s)", self.source) + executeHandlers("evt04", (self, vk.engine.captcha["img"])) return True else: logger.debug("User seems to be authenticated (jid: %s)", self.source) @@ -494,7 +492,6 @@ class User(object): self.lastMsgID, self.rosterSet), True) executeHandlers("evt07", (self,)) self.friends = vk.getFriends() - self.vk = vk return vk.online def markRosterSet(self, cl=None, stanza=None): @@ -628,8 +625,6 @@ class User(object): runDatabaseQuery("update users set lastMsgID=? where jid=?", (self.lastMsgID, self.source), True) - if not self.vk.userID: - self.vk.getUserID() def processPollResult(self, opener): """ @@ -731,9 +726,6 @@ class User(object): """ logger.debug("calling reauth for user %s", self.source) if not self.vk.online: - # Reset the token variable - # In order to prevent user deletion from the database - self.token = None self.connect() self.initialize(True) diff --git a/library/defaults.py b/library/defaults.py index 36fca2a..008118c 100644 --- a/library/defaults.py +++ b/library/defaults.py @@ -15,8 +15,8 @@ ALIVE = True # config vairables DEBUG_XMPPPY = False -DEBUG_POLL = False -DEBUG_API = False +DEBUG_POLL = None +DEBUG_API = [] STANZA_SEND_INTERVAL = 0.03125 THREAD_STACK_SIZE = 0 VK_ACCESS = 69638 diff --git a/library/vkapi.py b/library/vkapi.py index 95b0bb4..3618ecd 100644 --- a/library/vkapi.py +++ b/library/vkapi.py @@ -416,7 +416,7 @@ class InternalServerError(VkApiError): pass -class CaptchaNeeded(VkApiError): +class CaptchaNeeded(Exception): """ Will be raised when happens error with code 14 To prevent captchas, you should probably send less of queries diff --git a/modules/mod_iq_register.py b/modules/mod_iq_register.py index 906c2c1..7d76b55 100644 --- a/modules/mod_iq_register.py +++ b/modules/mod_iq_register.py @@ -35,7 +35,6 @@ def initializeUser(user, cl, iq, kwargs): import forms - def register_handler(cl, iq): jidTo = iq.getTo() jidFrom = iq.getFrom() diff --git a/modules/mod_prs_main.py b/modules/mod_prs_main.py index a0f7a23..015e46c 100644 --- a/modules/mod_prs_main.py +++ b/modules/mod_prs_main.py @@ -19,19 +19,17 @@ def initializeUser(source, resource, prs): logger.debug("User not in the transport, but a presence received. Searching in database (jid: %s)" % source) data = runDatabaseQuery("select jid,username from users where jid=?", (source,), many=False) if data: - sendPresence(source, TransportID, None, IDENTIFIER["name"], caps=True, reason=_("You are being initialized, please wait..."), show="xa") + sendPresence(source, TransportID, None, IDENTIFIER["name"], caps=True, + reason=_("You are being initialized, please wait..."), show="xa") logger.debug("User has been found in database (jid: %s)" % source) jid, phone = data Transport[jid] = user = User(jid) - try: - if user.connect(): - user.initialize(send=True, resource=resource) # probably we need to know resource a bit earlier than this time - utils.runThread(executeHandlers, ("prs01", (source, prs))) - else: - crashLog("user.connect", False) - sendMessage(Component, jid, TransportID, _("Auth failed! If this error repeated, please register again. This incident will be reported.")) - except Exception: - crashLog("prs.init") + if user.connect(): + user.initialize(send=True, resource=resource) # probably we need to know resource a bit earlier than this time + utils.runThread(executeHandlers, ("prs01", (source, prs))) + else: + sendMessage(Component, jid, TransportID, _("Auth failed! If this error repeated, please register again. This incident will be reported.")) + if source in USERS_ON_INIT: USERS_ON_INIT.remove(source) @@ -58,7 +56,7 @@ def presence_handler(cl, prs): if user.resources: user.sendOutPresence(jidFrom) if not user.resources: - sender(cl, xmpp.Presence(jidFrom, "unavailable", frm = TransportID)) + sendPresence(jidFrom, TransportID, "unavailable") if transportSettings.send_unavailable: user.sendOutPresence(jidFrom) user.vk.disconnect() @@ -68,24 +66,21 @@ def presence_handler(cl, prs): pass elif pType == "error": - eCode = prs.getErrorCode() - if eCode == "404": + if prs.getErrorCode() == "404": user.vk.disconnect() elif pType == "subscribe": + sendPresence(source, jidTo, "subscribed") + if user.friends: + id = vk2xmpp(destination) + if id in user.friends: + if user.friends[id]["online"]: + sendPresence(jidFrom, frm=jidTo) if destination == TransportID: - sender(cl, xmpp.Presence(source, "subscribed", frm=TransportID)) - sender(cl, xmpp.Presence(jidFrom, frm=TransportID)) - else: - sender(cl, xmpp.Presence(source, "subscribed", frm=jidTo)) - if user.friends: - id = vk2xmpp(destination) - if id in user.friends: - if user.friends[id]["online"]: - sender(cl, xmpp.Presence(jidFrom, frm=jidTo)) + sendPresence(jidFrom, TransportID) elif pType == "unsubscribe": - if source in Transport and destination == TransportID: + if destination == TransportID: removeUser(user, True, False) executeHandlers("evt09", (source,)) |