diff options
author | mrDoctorWho <mrdoctorwho@gmail.com> | 2015-04-24 12:51:30 +0300 |
---|---|---|
committer | mrDoctorWho <mrdoctorwho@gmail.com> | 2015-04-24 12:51:30 +0300 |
commit | 0515686a9cea8bbe48a5967e097f936143a6d601 (patch) | |
tree | 392a46a2c09dd980c6c6cbec5e7872aa3642516b | |
parent | a03e783128e67169a724ec6ed720ff6765a1c0e8 (diff) |
Oh, I forgot to add these
-rw-r--r-- | extensions/user_activity.py | 13 | ||||
-rw-r--r-- | library/forms.py | 34 | ||||
-rw-r--r-- | library/rostermanager.py | 57 |
3 files changed, 98 insertions, 6 deletions
diff --git a/extensions/user_activity.py b/extensions/user_activity.py index db13fc5..0ca6a01 100644 --- a/extensions/user_activity.py +++ b/extensions/user_activity.py @@ -40,17 +40,18 @@ def user_activity_remove(): for (jid, date) in users: if (time.time() - date) >= LA: if jid not in Transport: - import shutil runDatabaseQuery("delete from users where jid=?", (jid,), set=True) runDatabaseQuery("delete from last_activity where jid=?", (jid,)) - try: - shutil.rmtree("%s/%s" % (settingsDir, jid)) - except Exception: - crashLog("remove_inactive") + settings = "%s/%s" % (settingsDir, jid) + if os.path.exists(settings): + import shutil + shutil.rmtree(settings) logger.info("user_activity: user has been removed from " \ "the database because of inactivity more than %s (jid: %s)" % (LA, jid)) else: - sendMessage(Component, jid, TransportID, _("Your last activity was more than %s seconds ago. Relogin or you'll be exterminated.") % LA, LA) + sendMessage(Component, jid, TransportID, + _("Your last activity was more than %s seconds ago." + " Relogin or you'll be exterminated.") % LA, LA) utils.runThread(user_activity_remove, delay=(60*60*24)) # A dirty hack to add seen users in stats diff --git a/library/forms.py b/library/forms.py new file mode 100644 index 0000000..2791969 --- /dev/null +++ b/library/forms.py @@ -0,0 +1,34 @@ +# coding: utf-8 + +from __main__ import URL_ACCEPT_APP, VK_ACCESS, _ + +URL_ACCEPT_APP = URL_ACCEPT_APP % VK_ACCESS + +class Forms: + + @classmethod + def getSimpleForm(cls): + form = [] + form.append({"var": "link", "type": "text-single", + "label": _("Autorization page"), "value": URL_ACCEPT_APP}) + form.append({"var": "password", "type": "text-private", + "label": _("Access-token"), + "desc": _("Enter the access token")}) + return form + + @classmethod + def getComlicatedForm(cls): + form = cls.getSimpleForm() + # This is why we have a fixed order + form[1]["label"] = _("Password/Access-token") + form[1]["desc"] = _("Enter the token or password") + + form.insert(1, {"var": "phone", "type": "text-single", + "label": _("Phone number"), "value": "+", + "desc": _("Enter phone number in format +71234567890")}) + form.insert(2, {"var": "use_password", "type": "boolean", + "label": _("Get access-token automatically"), + "desc": _("Tries to get access-token automatically. (It's recommended to use a token)")}) + return form + + diff --git a/library/rostermanager.py b/library/rostermanager.py new file mode 100644 index 0000000..fd92a86 --- /dev/null +++ b/library/rostermanager.py @@ -0,0 +1,57 @@ +# coding: utf-8 +# This file is a part of VK4XMPP transport +# © simpleApps, 2015. + +findListByID = lambda id, list: [key for key in list if key["lid"] == id] + +from defaults import IDENTIFIER +from __main__ import TransportID, vk2xmpp, sender, Component, sendPresence +import xmpp +import utils + +class Roster: + + @staticmethod + def getNode(jid, name=IDENTIFIER["name"], action="add"): + return xmpp.Node("item", {"action": action, "jid": jid, "name": name}) + + @classmethod + @utils.threaded + def manageRoster(cls, user, jid, dist={}, action="add"): + lists = user.vk.getLists() + iq = xmpp.Iq("set", to=jid, frm=TransportID) + node = xmpp.Node("x", {"xmlns": xmpp.NS_ROSTERX}) + items = [cls.getNode(TransportID, action=action)] + dist = dist or user.friends + for uid, value in dist.iteritems(): + item = cls.getNode(vk2xmpp(uid), value["name"], action) + if lists and value["lists"]: + list = findListByID(value["lists"][0], lists) + if list: + item.setTagData("group", list[0]["name"]) + items.append(item) + node.setPayload(items) + iq.addChild(node=node) + sender(Component, iq, cb=user.markRosterSet) + + @classmethod + def checkRosterx(cls, user, resource): + """ + Checks if the client supports XEP-0144: Roster Item Exchange + If it doesn't, or it didn't answer us, then transport will use old method + """ + jid = "%s/%s" % (user.source, resource) + 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): + 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) + + 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 |