General fixes for s2s, to make it more robust (I hope), sending data to remote hosts...
[prosody.git] / plugins / mod_roster.lua
1
2 local st = require "util.stanza"
3 local send = require "core.sessionmanager".send_to_session
4
5 local jid_split = require "util.jid".split;
6 local t_concat = table.concat;
7
8 local rm_remove_from_roster = require "core.rostermanager".remove_from_roster;
9 local rm_add_to_roster = require "core.rostermanager".add_to_roster;
10 local rm_roster_push = require "core.rostermanager".roster_push;
11
12 add_iq_handler("c2s", "jabber:iq:roster", 
13                 function (session, stanza)
14                         if stanza.tags[1].name == "query" then
15                                 if stanza.attr.type == "get" then
16                                         local roster = st.reply(stanza)
17                                                                 :query("jabber:iq:roster");
18                                         for jid in pairs(session.roster) do
19                                                 if jid ~= "pending" then
20                                                         roster:tag("item", {
21                                                                 jid = jid,
22                                                                 subscription = session.roster[jid].subscription,
23                                                                 ask = session.roster[jid].ask,
24                                                                 name = session.roster[jid].name,
25                                                         });
26                                                         for group in pairs(session.roster[jid].groups) do
27                                                                 roster:tag("group"):text(group):up();
28                                                         end
29                                                 end
30                                         end
31                                         send(session, roster);
32                                         session.interested = true; -- resource is interested in roster updates
33                                         return true;
34                                 elseif stanza.attr.type == "set" then
35                                         local query = stanza.tags[1];
36                                         if #query.tags == 1 and query.tags[1].name == "item"
37                                                         and query.tags[1].attr.xmlns == "jabber:iq:roster" and query.tags[1].attr.jid
38                                                         and query.tags[1].attr.jid ~= "pending" then
39                                                 local item = query.tags[1];
40                                                 local from_node, from_host = jid_split(stanza.attr.from);
41                                                 local node, host, resource = jid_split(item.attr.jid);
42                                                 if not resource then
43                                                         if item.attr.jid ~= from_node.."@"..from_host then
44                                                                 if item.attr.subscription == "remove" then
45                                                                         if session.roster[item.attr.jid] then
46                                                                                 local success, err_type, err_cond, err_msg = rm_remove_from_roster(session, item.attr.jid);
47                                                                                 if success then
48                                                                                         send(session, st.reply(stanza));
49                                                                                         rm_roster_push(from_node, from_host, item.attr.jid);
50                                                                                 else
51                                                                                         send(session, st.error_reply(stanza, err_type, err_cond, err_msg));
52                                                                                 end
53                                                                         else
54                                                                                 send(session, st.error_reply(stanza, "modify", "item-not-found"));
55                                                                         end
56                                                                 else
57                                                                         local r_item = {name = item.attr.name, groups = {}};
58                                                                         if r_item.name == "" then r_item.name = nil; end
59                                                                         if session.roster[item.attr.jid] then
60                                                                                 r_item.subscription = session.roster[item.attr.jid].subscription;
61                                                                                 r_item.ask = session.roster[item.attr.jid].ask;
62                                                                         else
63                                                                                 r_item.subscription = "none";
64                                                                         end
65                                                                         for _, child in ipairs(item) do 
66                                                                                 if child.name == "group" then
67                                                                                         local text = t_concat(child);
68                                                                                         if text and text ~= "" then
69                                                                                                 r_item.groups[text] = true;
70                                                                                         end
71                                                                                 end
72                                                                         end
73                                                                         local success, err_type, err_cond, err_msg = rm_add_to_roster(session, item.attr.jid, r_item);
74                                                                         if success then
75                                                                                 send(session, st.reply(stanza));
76                                                                                 rm_roster_push(from_node, from_host, item.attr.jid);
77                                                                         else
78                                                                                 send(session, st.error_reply(stanza, err_type, err_cond, err_msg));
79                                                                         end
80                                                                 end
81                                                         else
82                                                                 send(session, st.error_reply(stanza, "cancel", "not-allowed"));
83                                                         end
84                                                 else
85                                                         send(session, st.error_reply(stanza, "modify", "bad-request")); -- FIXME what's the correct error?
86                                                 end
87                                         else
88                                                 send(session, st.error_reply(stanza, "modify", "bad-request"));
89                                         end
90                                         return true;
91                                 end
92                         end
93                 end);