diff options
author | mrDoctorWho <mrdoctorwho@gmail.com> | 2013-11-26 18:04:09 +0400 |
---|---|---|
committer | mrDoctorWho <mrdoctorwho@gmail.com> | 2013-11-26 18:04:09 +0400 |
commit | 19c398a81d96b4416dc0d38589d1ef08aa51fa84 (patch) | |
tree | 4c8aeb675553dbe7152f0e11de1d20f38dfffa6d /extensions | |
parent | a84ce807b3136c14dcce655e84b7881f0ae3d9dd (diff) |
Fix "wall" attachment
Some fixes in groupchats extension: now room should be members-only
Diffstat (limited to 'extensions')
-rw-r--r-- | extensions/attachments.py | 2 | ||||
-rw-r--r-- | extensions/groupchats.py | 67 |
2 files changed, 58 insertions, 11 deletions
diff --git a/extensions/attachments.py b/extensions/attachments.py index 555e857..4809211 100644 --- a/extensions/attachments.py +++ b/extensions/attachments.py @@ -15,7 +15,7 @@ def parseAttachments(self, msg): key = att.get("type") if key == "wall": att[key]["from_id"] = abs(att[key]["from_id"]) - body += "https://vk.com/feed?w=wall%(from_id)s_%(id)s" + body += "https://vk.com/feed?w=wall-%(from_id)s_%(id)s" elif key == "photo": keys = ("src_xxxbig", "src_xxbig", "src_xbig", "src_big", "src", "url", "src_small") for dKey in keys: diff --git a/extensions/groupchats.py b/extensions/groupchats.py index 6d8cb80..5b41ac9 100644 --- a/extensions/groupchats.py +++ b/extensions/groupchats.py @@ -1,10 +1,23 @@ # coding: utf-8 # This file is a part of VK4XMPP transport # © simpleApps, 2013. +# File contains parts of code from +# BlackSmith mark.1 XMPP Bot, © simpleApps 2011 — 2013. if not require("attachments") or not require("forwardMessages"): raise +def IQSender(chat, attr, data, afrls, role, jidFrom): + stanza = xmpp.Iq("set", to = chat, frm = jidFrom) + query = xmpp.Node("query") + query.setNamespace(xmpp.NS_MUC_ADMIN) + arole = query.addChild("item", {attr: data, afrls: role}) + stanza.addChild(node = query) + Sender(Component, stanza) + +def member(chat, jid, jidFrom): + IQSender(chat, "jid", jid, "affiliation", "member", jidFrom) + def inviteUser(chat, jidTo, jidFrom, name): invite = xmpp.Message(to = chat, frm = jidFrom) x = xmpp.Node("x") @@ -14,7 +27,25 @@ def inviteUser(chat, jidTo, jidFrom, name): invite.addChild(node = x) Sender(Component, invite) -def roomPresence(chat, name, jidFrom, type = None): +def groupchatSetConfig(chat, jidFrom, exterminate = False): + iq = xmpp.Iq("set", to = chat, frm = jidFrom, xmlns = "jabber:component:accept") + query = iq.addChild("query", namespace = xmpp.NS_MUC_OWNER) + if exterminate: + query.addChild("destroy") + else: + form = xmpp.DataForm("submit") + field = form.setField("FORM_TYPE") + field.setType("hidden") + field.setValue(xmpp.NS_MUC_ROOMCONFIG) + membersonly = form.setField("muc#roomconfig_membersonly") + membersonly.setType("boolean") + membersonly.setValue("1") + whois = form.setField("muc#roomconfig_whois") + whois.setValue("anyone") + query.addChild(node = form) + Sender(Component, iq) + +def groupchatPresence(chat, name, jidFrom, type = None): prs = xmpp.Presence("%s/%s" % (chat, name), type, frm = jidFrom) Sender(Component, prs) @@ -35,36 +66,44 @@ def handleChatMessages(self, msg): if msg.has_key("chat_id"): idFrom = msg["uid"] owner = msg["admin_id"] + _owner = vk2xmpp(owner) chatID = "%s_chat#%s" % (self.UserID, msg["chat_id"]) ## Maybe better to use owner id for chat chat = "%s@%s" % (chatID, ConferenceServer) users = msg["chat_active"].split(",") ## Why chat owner isn't in a chat? It may cause problems. Or not? - if not users: if chat in self.chatUsers: - roomPresence(chat, self.getUserData(owner)["name"], vk2xmpp(self.UserID), "unavailable") + groupchatPresence(chat, self.getUserData(owner)["name"], vk2xmpp(self.UserID), "unavailable") del self.chatUsers[chat] return None # Maybe true? if not chat in self.chatUsers: self.chatUsers[chat] = [] - inviteUser(chat, self.jidFrom, vk2xmpp(owner), self.getUserData(owner)["name"]) ## TODO: Handle WHO invited me. Yes, i know that it'll be never happen. But maybe someone another do it for himself? You're welcome! for usr in (owner, self.UserID): - roomPresence(chat, self.getUserData(usr)["name"], vk2xmpp(usr)) - groupchatMessage(chat, msg["title"], vk2xmpp(owner), True, msg["date"]) + groupchatPresence(chat, self.getUserData(usr)["name"], vk2xmpp(usr)) + groupchatSetConfig(chat, _owner) + member(chat, self.jidFrom, _owner) + inviteUser(chat, self.jidFrom, _owner, self.getUserData(owner)["name"]) ## TODO: Handle WHO invited me. Yes, i know that it'll be never happen. But maybe someone another do it for himself? You're welcome! + groupchatMessage(chat, msg["title"], _owner, True, msg["date"]) for user in users: ## BURN IT! if not user in self.chatUsers[chat]: self.chatUsers[chat].append(user) uName = self.getUserData(user)["name"] - roomPresence(chat, uName, vk2xmpp(user)) + user = vk2xmpp(user) + member(chat, user, _owner) + groupchatPresence(chat, uName, user) for user in self.chatUsers[chat]: ## BURN IT MORE! if not user in users: self.chatUsers[chat].remove(user) uName = self.getUserData(user)["name"] - roomPresence(chat, uName, vk2xmpp(user), "unavailable") + groupchatPresence(chat, uName, vk2xmpp(user), "unavailable") + + # This code will not work because function can be called only at message in chat + if not self.chatUsers[chat]: + groupchatSetConfig(chat, _owner, exterminate = True) # EXTERMINATE!!! - body = escapeMsg(msg["body"]) + body = escapeMsg(uHTML(msg["body"])) body += parseAttachments(self, msg) body += parseForwardMessages(self, msg) if body: @@ -86,9 +125,17 @@ def incomingGroupchatMessageHandler(msg): Class = Transport[jid] Class.msg(body, Node.split("#")[1], "chat_id") + +## TODO: +##def onShutdown(): +## for user in TransportList: +## if Transport[user].chatUsers +## + if ConferenceServer: TransportFeatures.append(xmpp.NS_GROUPCHAT) Handlers["msg01"].append(handleChatMessages) Handlers["msg02"].append(incomingGroupchatMessageHandler) +## Handlers["shutdown"] TODO else: - del incomingGroupchatMessageHandler, handleChatMessages, inviteUser, roomPresence, groupchatMessage
\ No newline at end of file + del incomingGroupchatMessageHandler, handleChatMessages, inviteUser, groupchatPresence, groupchatMessage
\ No newline at end of file |