2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 local st = require "util.stanza"
24 local jid_split = require "util.jid".split;
25 local t_concat = table.concat;
27 local handle_outbound_presence_subscriptions_and_probes = require "core.presencemanager".handle_outbound_presence_subscriptions_and_probes;
28 local rm_remove_from_roster = require "core.rostermanager".remove_from_roster;
29 local rm_add_to_roster = require "core.rostermanager".add_to_roster;
30 local rm_roster_push = require "core.rostermanager".roster_push;
32 module:add_feature("jabber:iq:roster");
34 module:add_iq_handler("c2s", "jabber:iq:roster",
35 function (session, stanza)
36 if stanza.tags[1].name == "query" then
37 if stanza.attr.type == "get" then
38 local roster = st.reply(stanza)
39 :query("jabber:iq:roster");
40 for jid in pairs(session.roster) do
41 if jid ~= "pending" then
44 subscription = session.roster[jid].subscription,
45 ask = session.roster[jid].ask,
46 name = session.roster[jid].name,
48 for group in pairs(session.roster[jid].groups) do
49 roster:tag("group"):text(group):up();
51 roster:up(); -- move out from item
55 session.interested = true; -- resource is interested in roster updates
57 elseif stanza.attr.type == "set" then
58 local query = stanza.tags[1];
59 if #query.tags == 1 and query.tags[1].name == "item"
60 and query.tags[1].attr.xmlns == "jabber:iq:roster" and query.tags[1].attr.jid
61 and query.tags[1].attr.jid ~= "pending" then
62 local item = query.tags[1];
63 local from_node, from_host = jid_split(stanza.attr.from);
64 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
65 local node, host, resource = jid_split(item.attr.jid);
66 local to_bare = node and (node.."@"..host) or host; -- bare JID
67 if not resource and host then
68 if item.attr.jid ~= from_node.."@"..from_host then
69 if item.attr.subscription == "remove" then
70 local r_item = session.roster[item.attr.jid];
72 local success, err_type, err_cond, err_msg = rm_remove_from_roster(session, item.attr.jid);
74 session.send(st.reply(stanza));
75 rm_roster_push(from_node, from_host, item.attr.jid);
76 if r_item.subscription == "both" or r_item.subscription == "from" then
77 handle_outbound_presence_subscriptions_and_probes(session,
78 st.presence({type="unsubscribed"}), from_bare, to_bare);
79 elseif r_item.subscription == "both" or r_item.subscription == "to" then
80 handle_outbound_presence_subscriptions_and_probes(session,
81 st.presence({type="unsubscribe"}), from_bare, to_bare);
84 session.send(st.error_reply(stanza, err_type, err_cond, err_msg));
87 session.send(st.error_reply(stanza, "modify", "item-not-found"));
90 local r_item = {name = item.attr.name, groups = {}};
91 if r_item.name == "" then r_item.name = nil; end
92 if session.roster[item.attr.jid] then
93 r_item.subscription = session.roster[item.attr.jid].subscription;
94 r_item.ask = session.roster[item.attr.jid].ask;
96 r_item.subscription = "none";
98 for _, child in ipairs(item) do
99 if child.name == "group" then
100 local text = t_concat(child);
101 if text and text ~= "" then
102 r_item.groups[text] = true;
106 local success, err_type, err_cond, err_msg = rm_add_to_roster(session, item.attr.jid, r_item);
108 session.send(st.reply(stanza));
109 rm_roster_push(from_node, from_host, item.attr.jid);
111 session.send(st.error_reply(stanza, err_type, err_cond, err_msg));
115 session.send(st.error_reply(stanza, "cancel", "not-allowed"));
118 session.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME what's the correct error?
121 session.send(st.error_reply(stanza, "modify", "bad-request"));