2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 local st = require "util.stanza"
13 local jid_split = require "util.jid".split;
14 local t_concat = table.concat;
16 local handle_presence = require "core.presencemanager".handle_presence;
17 local rm_remove_from_roster = require "core.rostermanager".remove_from_roster;
18 local rm_add_to_roster = require "core.rostermanager".add_to_roster;
19 local rm_roster_push = require "core.rostermanager".roster_push;
20 local core_route_stanza = core_route_stanza;
22 module:add_feature("jabber:iq:roster");
24 module:add_iq_handler("c2s", "jabber:iq:roster",
25 function (session, stanza)
26 if stanza.tags[1].name == "query" then
27 if stanza.attr.type == "get" then
28 local roster = st.reply(stanza)
29 :query("jabber:iq:roster");
30 for jid in pairs(session.roster) do
31 if jid ~= "pending" then
34 subscription = session.roster[jid].subscription,
35 ask = session.roster[jid].ask,
36 name = session.roster[jid].name,
38 for group in pairs(session.roster[jid].groups) do
39 roster:tag("group"):text(group):up();
41 roster:up(); -- move out from item
45 session.interested = true; -- resource is interested in roster updates
47 elseif stanza.attr.type == "set" then
48 local query = stanza.tags[1];
49 if #query.tags == 1 and query.tags[1].name == "item"
50 and query.tags[1].attr.xmlns == "jabber:iq:roster" and query.tags[1].attr.jid
51 and query.tags[1].attr.jid ~= "pending" then
52 local item = query.tags[1];
53 local from_node, from_host = jid_split(stanza.attr.from);
54 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
55 local node, host, resource = jid_split(item.attr.jid);
56 local to_bare = node and (node.."@"..host) or host; -- bare JID
57 if not resource and host then
58 if item.attr.jid ~= from_node.."@"..from_host then
59 if item.attr.subscription == "remove" then
60 local r_item = session.roster[item.attr.jid];
62 local success, err_type, err_cond, err_msg = rm_remove_from_roster(session, item.attr.jid);
64 session.send(st.reply(stanza));
65 rm_roster_push(from_node, from_host, item.attr.jid);
66 if r_item.subscription == "both" or r_item.subscription == "from" then
67 handle_presence(session, st.presence({type="unsubscribed"}), from_bare, to_bare,
68 core_route_stanza, false);
69 elseif r_item.subscription == "both" or r_item.subscription == "to" then
70 handle_presence(session, st.presence({type="unsubscribe"}), from_bare, to_bare,
71 core_route_stanza, false);
74 session.send(st.error_reply(stanza, err_type, err_cond, err_msg));
77 session.send(st.error_reply(stanza, "modify", "item-not-found"));
80 local r_item = {name = item.attr.name, groups = {}};
81 if r_item.name == "" then r_item.name = nil; end
82 if session.roster[item.attr.jid] then
83 r_item.subscription = session.roster[item.attr.jid].subscription;
84 r_item.ask = session.roster[item.attr.jid].ask;
86 r_item.subscription = "none";
88 for _, child in ipairs(item) do
89 if child.name == "group" then
90 local text = t_concat(child);
91 if text and text ~= "" then
92 r_item.groups[text] = true;
96 local success, err_type, err_cond, err_msg = rm_add_to_roster(session, item.attr.jid, r_item);
98 session.send(st.reply(stanza));
99 rm_roster_push(from_node, from_host, item.attr.jid);
101 session.send(st.error_reply(stanza, err_type, err_cond, err_msg));
105 session.send(st.error_reply(stanza, "cancel", "not-allowed"));
108 session.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME what's the correct error?
111 session.send(st.error_reply(stanza, "modify", "bad-request"));