Merge waqas with Tobias. Eww.
[prosody.git] / plugins / mod_roster.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10
11 local st = require "util.stanza"
12
13 local jid_split = require "util.jid".split;
14 local jid_prep = require "util.jid".prep;
15 local t_concat = table.concat;
16 local tostring = tostring;
17
18 local rm_remove_from_roster = require "core.rostermanager".remove_from_roster;
19 local rm_add_to_roster = require "core.rostermanager".add_to_roster;
20 local rm_roster_push = require "core.rostermanager".roster_push;
21 local core_post_stanza = core_post_stanza;
22
23 module:add_feature("jabber:iq:roster");
24
25 local rosterver_stream_feature = st.stanza("ver", {xmlns="urn:xmpp:features:rosterver"}):tag("optional"):up();
26 module:add_event_hook("stream-features", 
27                 function (session, features)
28                         if session.username then
29                                 features:add_child(rosterver_stream_feature);
30                         end
31                 end);
32
33 module:add_iq_handler("c2s", "jabber:iq:roster", 
34                 function (session, stanza)
35                         if stanza.tags[1].name == "query" then
36                                 if stanza.attr.type == "get" then
37                                         local roster = st.reply(stanza);
38                                         
39                                         local ver = stanza.tags[1].attr.ver
40                                         
41                                         if (not ver) or tonumber(ver) ~= (session.roster[false].version or 1) then
42                                                 roster:query("jabber:iq:roster");
43                                                 -- Client does not support versioning, or has stale roster
44                                                 for jid in pairs(session.roster) do
45                                                         if jid ~= "pending" and jid then
46                                                                 roster:tag("item", {
47                                                                         jid = jid,
48                                                                         subscription = session.roster[jid].subscription,
49                                                                         ask = session.roster[jid].ask,
50                                                                         name = session.roster[jid].name,
51                                                                 });
52                                                                 for group in pairs(session.roster[jid].groups) do
53                                                                         roster:tag("group"):text(group):up();
54                                                                 end
55                                                                 roster:up(); -- move out from item
56                                                         end
57                                                 end
58                                                 roster.tags[1].attr.ver = tostring(session.roster[false].version or "1");
59                                         end
60                                         session.send(roster);
61                                         session.interested = true; -- resource is interested in roster updates
62                                         return true;
63                                 elseif stanza.attr.type == "set" then
64                                         local query = stanza.tags[1];
65                                         if #query.tags == 1 and query.tags[1].name == "item"
66                                                         and query.tags[1].attr.xmlns == "jabber:iq:roster" and query.tags[1].attr.jid 
67                                                         -- Protection against overwriting roster.pending, until we move it
68                                                         and query.tags[1].attr.jid ~= "pending" then
69                                                 local item = query.tags[1];
70                                                 local from_node, from_host = jid_split(stanza.attr.from);
71                                                 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
72                                                 local jid = jid_prep(item.attr.jid);
73                                                 local node, host, resource = jid_split(jid);
74                                                 if not resource and host then
75                                                         if jid ~= from_node.."@"..from_host then
76                                                                 if item.attr.subscription == "remove" then
77                                                                         local r_item = session.roster[jid];
78                                                                         if r_item then
79                                                                                 local success, err_type, err_cond, err_msg = rm_remove_from_roster(session, jid);
80                                                                                 if success then
81                                                                                         session.send(st.reply(stanza));
82                                                                                         rm_roster_push(from_node, from_host, jid);
83                                                                                         local to_bare = node and (node.."@"..host) or host; -- bare JID
84                                                                                         if r_item.subscription == "both" or r_item.subscription == "from" then
85                                                                                                 core_post_stanza(session, st.presence({type="unsubscribed", from=session.full_jid, to=to_bare}));
86                                                                                         elseif r_item.subscription == "both" or r_item.subscription == "to" then
87                                                                                                 core_post_stanza(session, st.presence({type="unsubscribe", from=session.full_jid, to=to_bare}));
88                                                                                         end
89                                                                                 else
90                                                                                         session.send(st.error_reply(stanza, err_type, err_cond, err_msg));
91                                                                                 end
92                                                                         else
93                                                                                 session.send(st.error_reply(stanza, "modify", "item-not-found"));
94                                                                         end
95                                                                 else
96                                                                         local r_item = {name = item.attr.name, groups = {}};
97                                                                         if r_item.name == "" then r_item.name = nil; end
98                                                                         if session.roster[jid] then
99                                                                                 r_item.subscription = session.roster[jid].subscription;
100                                                                                 r_item.ask = session.roster[jid].ask;
101                                                                         else
102                                                                                 r_item.subscription = "none";
103                                                                         end
104                                                                         for _, child in ipairs(item) do 
105                                                                                 if child.name == "group" then
106                                                                                         local text = t_concat(child);
107                                                                                         if text and text ~= "" then
108                                                                                                 r_item.groups[text] = true;
109                                                                                         end
110                                                                                 end
111                                                                         end
112                                                                         local success, err_type, err_cond, err_msg = rm_add_to_roster(session, jid, r_item);
113                                                                         if success then
114                                                                                 -- Ok, send success
115                                                                                 session.send(st.reply(stanza));
116                                                                                 -- and push change to all resources
117                                                                                 rm_roster_push(from_node, from_host, jid);
118                                                                         else
119                                                                                 -- Adding to roster failed
120                                                                                 session.send(st.error_reply(stanza, err_type, err_cond, err_msg));
121                                                                         end
122                                                                 end
123                                                         else
124                                                                 -- Trying to add self to roster
125                                                                 session.send(st.error_reply(stanza, "cancel", "not-allowed"));
126                                                         end
127                                                 else
128                                                         -- Invalid JID added to roster
129                                                         session.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME what's the correct error?
130                                                 end
131                                         else
132                                                 -- Roster set didn't include a single item, or its name wasn't  'item'
133                                                 session.send(st.error_reply(stanza, "modify", "bad-request"));
134                                         end
135                                         return true;
136                                 end
137                         end
138                 end);