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