Mercurial > prosody-modules
comparison mod_remote_roster/mod_remote_roster.lua @ 315:dead242beee3
mod_remote_roster: Implement section 2.5 of <http://jkaluza.fedorapeople.org/remote-roster.html>.
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Mon, 10 Jan 2011 17:20:34 +0500 |
| parents | df212e1fa576 |
| children | c86fc337d56f |
comparison
equal
deleted
inserted
replaced
| 314:df212e1fa576 | 315:dead242beee3 |
|---|---|
| 11 local tonumber = tonumber; | 11 local tonumber = tonumber; |
| 12 local pairs, ipairs = pairs, ipairs; | 12 local pairs, ipairs = pairs, ipairs; |
| 13 local hosts = hosts; | 13 local hosts = hosts; |
| 14 | 14 |
| 15 local load_roster = require "core.rostermanager".load_roster; | 15 local load_roster = require "core.rostermanager".load_roster; |
| 16 local save_roster = require "core.rostermanager".save_roster; | |
| 16 local rm_remove_from_roster = require "core.rostermanager".remove_from_roster; | 17 local rm_remove_from_roster = require "core.rostermanager".remove_from_roster; |
| 17 local rm_add_to_roster = require "core.rostermanager".add_to_roster; | 18 local rm_add_to_roster = require "core.rostermanager".add_to_roster; |
| 18 local rm_roster_push = require "core.rostermanager".roster_push; | 19 local rm_roster_push = require "core.rostermanager".roster_push; |
| 19 local core_post_stanza = core_post_stanza; | 20 local core_post_stanza = core_post_stanza; |
| 20 local user_exists = require "core.usermanager".user_exists; | 21 local user_exists = require "core.usermanager".user_exists; |
| 66 -- Protection against overwriting roster.pending, until we move it | 67 -- Protection against overwriting roster.pending, until we move it |
| 67 and query.tags[1].attr.jid ~= "pending" then | 68 and query.tags[1].attr.jid ~= "pending" then |
| 68 local item = query.tags[1]; | 69 local item = query.tags[1]; |
| 69 local jid = jid_prep(item.attr.jid); | 70 local jid = jid_prep(item.attr.jid); |
| 70 local node, host, resource = jid_split(jid); | 71 local node, host, resource = jid_split(jid); |
| 71 if not resource and host then | 72 if not resource and host == session.host then |
| 72 if jid ~= stanza.attr.to then | 73 if jid ~= stanza.attr.to then -- not self-jid |
| 73 if item.attr.subscription == "remove" then | 74 if item.attr.subscription == "remove" then |
| 74 local r_item = roster[jid]; | 75 local r_item = roster[jid]; |
| 75 if r_item then | 76 if r_item then |
| 76 local to_bare = node and (node.."@"..host) or host; -- bare JID | 77 local to_bare = node and (node.."@"..host) or host; -- bare JID |
| 77 if r_item.subscription == "both" or r_item.subscription == "from" or (roster.pending and roster.pending[jid]) then | 78 --if r_item.subscription == "both" or r_item.subscription == "from" or (roster.pending and roster.pending[jid]) then |
| 78 core_post_stanza(session, st.presence({type="unsubscribed", from=session.full_jid, to=to_bare})); | 79 -- core_post_stanza(hosts[module.host], st.presence({type="unsubscribed", from=stanza.attr.to, to=to_bare})); |
| 79 end | 80 --end |
| 80 if r_item.subscription == "both" or r_item.subscription == "to" or r_item.ask then | 81 --if r_item.subscription == "both" or r_item.subscription == "to" or r_item.ask then |
| 81 core_post_stanza(session, st.presence({type="unsubscribe", from=session.full_jid, to=to_bare})); | 82 -- core_post_stanza(hosts[module.host], st.presence({type="unsubscribe", from=stanza.attr.to, to=to_bare})); |
| 82 end | 83 --end |
| 83 local success, err_type, err_cond, err_msg = rm_remove_from_roster(session, jid); | 84 roster[jid] = nil; |
| 84 if success then | 85 if save_roster(from_node, from_host, roster) then |
| 85 session.send(st.reply(stanza)); | 86 session.send(st.reply(stanza)); |
| 86 rm_roster_push(from_node, from_host, jid); | 87 rm_roster_push(from_node, from_host, jid); |
| 87 else | 88 else |
| 88 session.send(st.error_reply(stanza, err_type, err_cond, err_msg)); | 89 roster[jid] = item; |
| 90 session.send(st.error_reply(stanza, "wait", "internal-server-error", "Unable to save roster")); | |
| 89 end | 91 end |
| 90 else | 92 else |
| 91 session.send(st.error_reply(stanza, "modify", "item-not-found")); | 93 session.send(st.error_reply(stanza, "modify", "item-not-found")); |
| 92 end | 94 end |
| 93 else | 95 else |
| 94 local r_item = {name = item.attr.name, groups = {}}; | 96 local r_item = {name = item.attr.name, groups = {}}; |
| 95 if r_item.name == "" then r_item.name = nil; end | 97 if r_item.name == "" then r_item.name = nil; end |
| 96 if session.roster[jid] then | 98 if roster[jid] then |
| 97 r_item.subscription = session.roster[jid].subscription; | 99 r_item.subscription = roster[jid].subscription; |
| 98 r_item.ask = session.roster[jid].ask; | 100 r_item.ask = roster[jid].ask; |
| 99 else | 101 else |
| 100 r_item.subscription = "none"; | 102 r_item.subscription = "none"; |
| 101 end | 103 end |
| 102 for _, child in ipairs(item) do | 104 for _, child in ipairs(item) do |
| 103 if child.name == "group" then | 105 if child.name == "group" then |
| 105 if text and text ~= "" then | 107 if text and text ~= "" then |
| 106 r_item.groups[text] = true; | 108 r_item.groups[text] = true; |
| 107 end | 109 end |
| 108 end | 110 end |
| 109 end | 111 end |
| 110 local success, err_type, err_cond, err_msg = rm_add_to_roster(session, jid, r_item); | 112 local olditem = roster[jid]; |
| 111 if success then -- Ok, send success | 113 roster[jid] = r_item; |
| 114 if save_roster(from_node, from_host, roster) then -- Ok, send success | |
| 112 session.send(st.reply(stanza)); | 115 session.send(st.reply(stanza)); |
| 113 -- and push change to all resources | 116 -- and push change to all resources |
| 114 rm_roster_push(from_node, from_host, jid); | 117 rm_roster_push(from_node, from_host, jid); |
| 115 else -- Adding to roster failed | 118 else -- Adding to roster failed |
| 116 session.send(st.error_reply(stanza, err_type, err_cond, err_msg)); | 119 roster[jid] = olditem; |
| 120 session.send(st.error_reply(stanza, "wait", "internal-server-error", "Unable to save roster")); | |
| 117 end | 121 end |
| 118 end | 122 end |
| 119 else -- Trying to add self to roster | 123 else -- Trying to add self to roster |
| 120 session.send(st.error_reply(stanza, "cancel", "not-allowed")); | 124 session.send(st.error_reply(stanza, "cancel", "not-allowed")); |
| 121 end | 125 end |
