554ef2e59044a85cf2a37918880792c331356498
[prosody.git] / plugins / mod_roster.lua
1 -- Prosody IM v0.4
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 handle_presence = require "core.presencemanager".handle_presence;
19 local rm_remove_from_roster = require "core.rostermanager".remove_from_roster;
20 local rm_add_to_roster = require "core.rostermanager".add_to_roster;
21 local rm_roster_push = require "core.rostermanager".roster_push;
22 local core_route_stanza = core_route_stanza;
23
24 module:add_feature("jabber:iq:roster");
25
26 local rosterver_stream_feature = st.stanza("ver", {xmlns="urn:xmpp:features:rosterver"}):tag("optional"):up();
27 module:add_event_hook("stream-features", 
28                 function (session, features)                                                                                            
29                         if session.username then
30                                 features:add_child(rosterver_stream_feature);
31                         end
32                 end);
33
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                                         
41                                         local ver = stanza.tags[1].attr.ver
42                                         
43                                         if (not ver) or tonumber(ver) ~= (session.roster[false].version or 1) then
44                                                 -- Client does not support versioning, or has stale roster
45                                                 for jid in pairs(session.roster) do
46                                                         if jid ~= "pending" and jid then
47                                                                 roster:tag("item", {
48                                                                         jid = jid,
49                                                                         subscription = session.roster[jid].subscription,
50                                                                         ask = session.roster[jid].ask,
51                                                                         name = session.roster[jid].name,
52                                                                 });
53                                                                 for group in pairs(session.roster[jid].groups) do
54                                                                         roster:tag("group"):text(group):up();
55                                                                 end
56                                                                 roster:up(); -- move out from item
57                                                         end
58                                                 end
59                                                 roster.tags[1].attr.ver = tostring(session.roster[false].version or "1");
60                                         end
61                                         session.send(roster);
62                                         session.interested = true; -- resource is interested in roster updates
63                                         return true;
64                                 elseif stanza.attr.type == "set" then
65                                         local query = stanza.tags[1];
66                                         if #query.tags == 1 and query.tags[1].name == "item"
67                                                         and query.tags[1].attr.xmlns == "jabber:iq:roster" and query.tags[1].attr.jid 
68                                                         -- Protection against overwriting roster.pending, until we move it
69                                                         and query.tags[1].attr.jid ~= "pending" then
70                                                 local item = query.tags[1];
71                                                 local from_node, from_host = jid_split(stanza.attr.from);
72                                                 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
73                                                 local jid = jid_prep(item.attr.jid);
74                                                 local node, host, resource = jid_split(jid);
75                                                 if not resource and host then
76                                                         if jid ~= from_node.."@"..from_host then
77                                                                 if item.attr.subscription == "remove" then
78                                                                         local r_item = session.roster[jid];
79                                                                         if r_item then
80                                                                                 local success, err_type, err_cond, err_msg = rm_remove_from_roster(session, jid);
81                                                                                 if success then
82                                                                                         session.send(st.reply(stanza));
83                                                                                         rm_roster_push(from_node, from_host, jid);
84                                                                                         local to_bare = node and (node.."@"..host) or host; -- bare JID
85                                                                                         if r_item.subscription == "both" or r_item.subscription == "from" then
86                                                                                                 handle_presence(session, st.presence({type="unsubscribed"}), from_bare, to_bare,
87                                                                                                         core_route_stanza, false);
88                                                                                         elseif r_item.subscription == "both" or r_item.subscription == "to" then
89                                                                                                 handle_presence(session, st.presence({type="unsubscribe"}), from_bare, to_bare,
90                                                                                                         core_route_stanza, false);
91                                                                                         end
92                                                                                 else
93                                                                                         session.send(st.error_reply(stanza, err_type, err_cond, err_msg));
94                                                                                 end
95                                                                         else
96                                                                                 session.send(st.error_reply(stanza, "modify", "item-not-found"));
97                                                                         end
98                                                                 else
99                                                                         local r_item = {name = item.attr.name, groups = {}};
100                                                                         if r_item.name == "" then r_item.name = nil; end
101                                                                         if session.roster[jid] then
102                                                                                 r_item.subscription = session.roster[jid].subscription;
103                                                                                 r_item.ask = session.roster[jid].ask;
104                                                                         else
105                                                                                 r_item.subscription = "none";
106                                                                         end
107                                                                         for _, child in ipairs(item) do 
108                                                                                 if child.name == "group" then
109                                                                                         local text = t_concat(child);
110                                                                                         if text and text ~= "" then
111                                                                                                 r_item.groups[text] = true;
112                                                                                         end
113                                                                                 end
114                                                                         end
115                                                                         local success, err_type, err_cond, err_msg = rm_add_to_roster(session, jid, r_item);
116                                                                         if success then
117                                                                                 session.send(st.reply(stanza));
118                                                                                 rm_roster_push(from_node, from_host, jid);
119                                                                         else
120                                                                                 session.send(st.error_reply(stanza, err_type, err_cond, err_msg));
121                                                                         end
122                                                                 end
123                                                         else
124                                                                 session.send(st.error_reply(stanza, "cancel", "not-allowed"));
125                                                         end
126                                                 else
127                                                         session.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME what's the correct error?
128                                                 end
129                                         else
130                                                 session.send(st.error_reply(stanza, "modify", "bad-request"));
131                                         end
132                                         return true;
133                                 end
134                         end
135                 end);