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