Change modules to use the new add_feature module API method.
[prosody.git] / plugins / mod_roster.lua
1 -- Prosody IM v0.1
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21
22 local st = require "util.stanza"
23
24 local jid_split = require "util.jid".split;
25 local t_concat = table.concat;
26
27 local handle_outbound_presence_subscriptions_and_probes = require "core.presencemanager".handle_outbound_presence_subscriptions_and_probes;
28 local rm_remove_from_roster = require "core.rostermanager".remove_from_roster;
29 local rm_add_to_roster = require "core.rostermanager".add_to_roster;
30 local rm_roster_push = require "core.rostermanager".roster_push;
31
32 module:add_feature("jabber:iq:roster");
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                                         for jid in pairs(session.roster) do
41                                                 if jid ~= "pending" then
42                                                         roster:tag("item", {
43                                                                 jid = jid,
44                                                                 subscription = session.roster[jid].subscription,
45                                                                 ask = session.roster[jid].ask,
46                                                                 name = session.roster[jid].name,
47                                                         });
48                                                         for group in pairs(session.roster[jid].groups) do
49                                                                 roster:tag("group"):text(group):up();
50                                                         end
51                                                         roster:up(); -- move out from item
52                                                 end
53                                         end
54                                         session.send(roster);
55                                         session.interested = true; -- resource is interested in roster updates
56                                         return true;
57                                 elseif stanza.attr.type == "set" then
58                                         local query = stanza.tags[1];
59                                         if #query.tags == 1 and query.tags[1].name == "item"
60                                                         and query.tags[1].attr.xmlns == "jabber:iq:roster" and query.tags[1].attr.jid
61                                                         and query.tags[1].attr.jid ~= "pending" then
62                                                 local item = query.tags[1];
63                                                 local from_node, from_host = jid_split(stanza.attr.from);
64                                                 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
65                                                 local node, host, resource = jid_split(item.attr.jid);
66                                                 local to_bare = node and (node.."@"..host) or host; -- bare JID
67                                                 if not resource and host then
68                                                         if item.attr.jid ~= from_node.."@"..from_host then
69                                                                 if item.attr.subscription == "remove" then
70                                                                         local r_item = session.roster[item.attr.jid];
71                                                                         if r_item then
72                                                                                 local success, err_type, err_cond, err_msg = rm_remove_from_roster(session, item.attr.jid);
73                                                                                 if success then
74                                                                                         session.send(st.reply(stanza));
75                                                                                         rm_roster_push(from_node, from_host, item.attr.jid);
76                                                                                         if r_item.subscription == "both" or r_item.subscription == "from" then
77                                                                                                 handle_outbound_presence_subscriptions_and_probes(session,
78                                                                                                         st.presence({type="unsubscribed"}), from_bare, to_bare);
79                                                                                         elseif r_item.subscription == "both" or r_item.subscription == "to" then
80                                                                                                 handle_outbound_presence_subscriptions_and_probes(session,
81                                                                                                         st.presence({type="unsubscribe"}), from_bare, to_bare);
82                                                                                         end
83                                                                                 else
84                                                                                         session.send(st.error_reply(stanza, err_type, err_cond, err_msg));
85                                                                                 end
86                                                                         else
87                                                                                 session.send(st.error_reply(stanza, "modify", "item-not-found"));
88                                                                         end
89                                                                 else
90                                                                         local r_item = {name = item.attr.name, groups = {}};
91                                                                         if r_item.name == "" then r_item.name = nil; end
92                                                                         if session.roster[item.attr.jid] then
93                                                                                 r_item.subscription = session.roster[item.attr.jid].subscription;
94                                                                                 r_item.ask = session.roster[item.attr.jid].ask;
95                                                                         else
96                                                                                 r_item.subscription = "none";
97                                                                         end
98                                                                         for _, child in ipairs(item) do 
99                                                                                 if child.name == "group" then
100                                                                                         local text = t_concat(child);
101                                                                                         if text and text ~= "" then
102                                                                                                 r_item.groups[text] = true;
103                                                                                         end
104                                                                                 end
105                                                                         end
106                                                                         local success, err_type, err_cond, err_msg = rm_add_to_roster(session, item.attr.jid, r_item);
107                                                                         if success then
108                                                                                 session.send(st.reply(stanza));
109                                                                                 rm_roster_push(from_node, from_host, item.attr.jid);
110                                                                         else
111                                                                                 session.send(st.error_reply(stanza, err_type, err_cond, err_msg));
112                                                                         end
113                                                                 end
114                                                         else
115                                                                 session.send(st.error_reply(stanza, "cancel", "not-allowed"));
116                                                         end
117                                                 else
118                                                         session.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME what's the correct error?
119                                                 end
120                                         else
121                                                 session.send(st.error_reply(stanza, "modify", "bad-request"));
122                                         end
123                                         return true;
124                                 end
125                         end
126                 end);