diff options
author | mrDoctorWho <mrdoctorwho@gmail.com> | 2015-04-26 20:02:00 +0300 |
---|---|---|
committer | mrDoctorWho <mrdoctorwho@gmail.com> | 2015-04-26 20:02:00 +0300 |
commit | 59d46b7276ca84533281a8e2ab8047bf02df67dd (patch) | |
tree | 331fbe2729f4197fa510219e3cadb37f47827625 | |
parent | ba4fe757ec21e59bf1f4160b7e342d44f5d0be15 (diff) |
Fix crash when user has asked for subscription
Add subscription request after transport have added users to user's roster by RosterX
Overall code cleanup
-rw-r--r-- | gateway.py | 17 | ||||
-rw-r--r-- | library/rostermanager.py | 21 | ||||
-rw-r--r-- | library/vkapi.py | 24 | ||||
-rw-r--r-- | modules/mod_prs_main.py | 2 |
4 files changed, 31 insertions, 33 deletions
@@ -225,18 +225,14 @@ class VK(object): try: int(self.method("isAppUser", force=True)) except (api.VkApiError, TypeError, AttributeError): - import traceback - traceback.print_exc() return False return True def auth(self, username=None, password=None): logger.debug("VK going to authenticate (jid: %s)", self.source) self.engine = api.APIBinding(self.token, debug=DEBUG_API) - if not self.checkToken(): raise api.TokenError("The token is invalid (jid: %s)" % self.source) - self.online = True return True @@ -799,7 +795,6 @@ def sender(cl, stanza, cb=None, args={}): try: cl.send(stanza) except Exception: - crashLog("sender") disconnectHandler(True) @@ -972,27 +967,23 @@ def disconnectHandler(crash=True): Handles disconnect And writes a crashlog if crash parameter equals True """ - logger.debug("disconnectHandler has been called!") executeHandlers("evt02") if crash: crashLog("main.disconnect") - logger.critical("Disconnecting from the server") + logger.critical("disconnecting from the server") try: Component.disconnect() except AttributeError: pass - except Exception: - crashLog("disconnect_handler") global ALIVE ALIVE = False - logger.info("Disconnected successfully!") if not Daemon: - logger.warning("The trasnport is going to be restarted!") - Print("Reconnecting...") + logger.warning("the trasnport is going to be restarted!") + Print("Restarting...") time.sleep(5) os.execl(sys.executable, sys.executable, *sys.argv) else: - logger.info("The transport is shutting down!") + logger.info("the transport is shutting down!") sys.exit(-1) diff --git a/library/rostermanager.py b/library/rostermanager.py index 75de0cb..6dc47ca 100644 --- a/library/rostermanager.py +++ b/library/rostermanager.py @@ -7,6 +7,8 @@ findListByID = lambda id, list: [key for key in list if key["lid"] == id] from __main__ import * from __main__ import _ +RosterSemaphore = threading.Semaphore() + class Roster: @staticmethod @@ -15,9 +17,9 @@ class Roster: @classmethod @utils.threaded - def manageRoster(cls, user, jid, dist={}, action="add"): + def manageRoster(cls, user, dist={}, action="add"): lists = user.vk.getLists() - iq = xmpp.Iq("set", to=jid, frm=TransportID) + iq = xmpp.Iq("set", to=user.source, frm=TransportID) node = xmpp.Node("x", {"xmlns": xmpp.NS_ROSTERX}) items = [cls.getNode(TransportID, action=action)] dist = dist or user.friends @@ -30,7 +32,10 @@ class Roster: items.append(item) node.setPayload(items) iq.addChild(node=node) - sender(Component, iq, cb=user.markRosterSet) + logger.debug("RosterManager: sending %s fellas to user (jid: %s)", len(items), user) + with RosterSemaphore: + sender(Component, iq, cb=user.markRosterSet) + user.sendSubPresence(user.friends) @classmethod def checkRosterx(cls, user, resource): @@ -42,14 +47,14 @@ class Roster: sendPresence(jid, TransportID, "subscribe", IDENTIFIER["name"]) sendPresence(jid, TransportID, nick=IDENTIFIER["name"], reason=_("You are being initialized, please wait..."), show="xa") - def answer(cl, stanza, timer): + def answer(cl, stanza): if xmpp.isResultNode(stanza): query = stanza.getTag("query") if query.getTags("feature", attrs={"var": xmpp.NS_ROSTERX}): - timer.cancel() - cls.manageRoster(user, jid, user.friends) + cls.manageRoster(user, user.friends) + else: + user.sendSubPresence(user.friends) iq = xmpp.Iq("get", to=jid, frm=TransportID) iq.addChild("query", namespace=xmpp.NS_DISCO_INFO) - timer = utils.runThread(user.sendSubPresence, (user.friends,), delay=10) - sender(Component, iq, cb=answer, args={"timer": timer})
\ No newline at end of file + sender(Component, iq, cb=answer)
\ No newline at end of file diff --git a/library/vkapi.py b/library/vkapi.py index 3618ecd..069cf5e 100644 --- a/library/vkapi.py +++ b/library/vkapi.py @@ -260,7 +260,12 @@ class PasswordLogin(RequestProcessor): body, response = self.get(url, values) if response: if "access_token" in response.url: - token = token_exp.search(response.url).group(0) + match = token_exp.search(response.url) + if match: + token = match.group(0) + else: + logger.error("token regexp doesn't match the url: %s", response.url) + raise AuthError("Something went wrong. We're so sorry.") else: postTarget = webtools.getTagArg("form method=\"post\"", "action", body, "form") @@ -289,7 +294,7 @@ class APIBinding(RequestProcessor): def method(self, method, values=None): """ - Issues the VK method + Issues a VK method Parameters: method: vk method values: method parameters @@ -400,26 +405,23 @@ class VkApiError(Exception): pass -class AuthError(VkApiError): +class CaptchaNeeded(Exception): """ - Happens when user is trying to login using password - And there's one of possible errors: captcha, - invalid password and wrong phone + Will be raised when captcha appears """ pass -class InternalServerError(VkApiError): +class AuthError(VkApiError): """ - Well, that error should be probably ignored + Happens when user is trying to login using password """ pass -class CaptchaNeeded(Exception): +class InternalServerError(VkApiError): """ - Will be raised when happens error with code 14 - To prevent captchas, you should probably send less of queries + Well, that error should be probably ignored """ pass diff --git a/modules/mod_prs_main.py b/modules/mod_prs_main.py index 015e46c..a18ec42 100644 --- a/modules/mod_prs_main.py +++ b/modules/mod_prs_main.py @@ -75,7 +75,7 @@ def presence_handler(cl, prs): id = vk2xmpp(destination) if id in user.friends: if user.friends[id]["online"]: - sendPresence(jidFrom, frm=jidTo) + sendPresence(jidFrom, jidTo) if destination == TransportID: sendPresence(jidFrom, TransportID) |