0.3->0.4
[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 t_concat = table.concat;
15
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;
21
22 module:add_feature("jabber:iq:roster");
23
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
32                                                         roster:tag("item", {
33                                                                 jid = jid,
34                                                                 subscription = session.roster[jid].subscription,
35                                                                 ask = session.roster[jid].ask,
36                                                                 name = session.roster[jid].name,
37                                                         });
38                                                         for group in pairs(session.roster[jid].groups) do
39                                                                 roster:tag("group"):text(group):up();
40                                                         end
41                                                         roster:up(); -- move out from item
42                                                 end
43                                         end
44                                         session.send(roster);
45                                         session.interested = true; -- resource is interested in roster updates
46                                         return true;
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];
61                                                                         if r_item then
62                                                                                 local success, err_type, err_cond, err_msg = rm_remove_from_roster(session, item.attr.jid);
63                                                                                 if success then
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);
72                                                                                         end
73                                                                                 else
74                                                                                         session.send(st.error_reply(stanza, err_type, err_cond, err_msg));
75                                                                                 end
76                                                                         else
77                                                                                 session.send(st.error_reply(stanza, "modify", "item-not-found"));
78                                                                         end
79                                                                 else
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;
85                                                                         else
86                                                                                 r_item.subscription = "none";
87                                                                         end
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;
93                                                                                         end
94                                                                                 end
95                                                                         end
96                                                                         local success, err_type, err_cond, err_msg = rm_add_to_roster(session, item.attr.jid, r_item);
97                                                                         if success then
98                                                                                 session.send(st.reply(stanza));
99                                                                                 rm_roster_push(from_node, from_host, item.attr.jid);
100                                                                         else
101                                                                                 session.send(st.error_reply(stanza, err_type, err_cond, err_msg));
102                                                                         end
103                                                                 end
104                                                         else
105                                                                 session.send(st.error_reply(stanza, "cancel", "not-allowed"));
106                                                         end
107                                                 else
108                                                         session.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME what's the correct error?
109                                                 end
110                                         else
111                                                 session.send(st.error_reply(stanza, "modify", "bad-request"));
112                                         end
113                                         return true;
114                                 end
115                         end
116                 end);