Annotate

plugins/roster.lua @ 271:c9d8628a61cc

plugins.roster: Change variable name to what's used in XMPP-IM
author Kim Alvefur <zash@zash.se>
date Sun, 01 Jan 2012 22:17:39 +0100
parent 250:a5ac643a7fd6
child 276:3d8442f7f034
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
250
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 217
diff changeset
1 local verse = require "verse";
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 217
diff changeset
2 local bare_jid = require "util.jid".bare;
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 217
diff changeset
3
82
f306c2f8bb88 plugins.roster: Unfinished roster plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local xmlns_roster = "jabber:iq:roster";
217
60db4e738910 plugins.roster: Check stream features for roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 213
diff changeset
5 local xmlns_rosterver = "urn:xmpp:features:rosterver";
192
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
6 local t_insert = table.insert;
82
f306c2f8bb88 plugins.roster: Unfinished roster plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
f306c2f8bb88 plugins.roster: Unfinished roster plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 function verse.plugins.roster(stream)
217
60db4e738910 plugins.roster: Check stream features for roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 213
diff changeset
9 local ver_supported = false;
192
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
10 local roster = {
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
11 items = {};
210
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
12 ver = "";
192
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
13 -- TODO:
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
14 -- groups = {};
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
15 };
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
16 stream.roster = roster;
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
17
217
60db4e738910 plugins.roster: Check stream features for roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 213
diff changeset
18 stream:hook("stream-features", function(features_stanza)
60db4e738910 plugins.roster: Check stream features for roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 213
diff changeset
19 if features_stanza:get_child("ver", xmlns_rosterver) then
60db4e738910 plugins.roster: Check stream features for roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 213
diff changeset
20 ver_supported = true;
60db4e738910 plugins.roster: Check stream features for roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 213
diff changeset
21 end
60db4e738910 plugins.roster: Check stream features for roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 213
diff changeset
22 end);
60db4e738910 plugins.roster: Check stream features for roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 213
diff changeset
23
192
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
24 local function item_lua2xml(item_table)
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
25 local xml_item = verse.stanza("item", { xmlns = xmlns_roster });
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
26 for k, v in pairs(item_table) do
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
27 if k ~= "groups" then
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
28 xml_item.attr[k] = v;
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
29 else
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
30 for i = 1,#v do
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
31 xml_item:tag("group"):text(v[i]):up();
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
32 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
33 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
34 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
35 return xml_item;
82
f306c2f8bb88 plugins.roster: Unfinished roster plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
192
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
37
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
38 local function item_xml2lua(xml_item)
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
39 local item_table = { };
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
40 local groups = {};
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
41 item_table.groups = groups;
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
42 local jid = xml_item.attr.jid;
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
43
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
44 for k, v in pairs(xml_item.attr) do
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
45 if k ~= "xmlns" then
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
46 item_table[k] = v
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
47 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
48 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
49
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
50 for group in xml_item:childtags("group") do
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
51 t_insert(groups, group:get_text())
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
52 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
53 return item_table;
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
54 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
55
210
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
56 function roster:load(r)
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
57 roster.ver, roster.items = r.ver, r.items;
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
58 end
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
59
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
60 function roster:dump()
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
61 return {
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
62 ver = roster.ver,
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
63 items = roster.items,
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
64 };
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
65 end
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
66
192
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
67 -- should this be add_contact(item, callback) instead?
271
c9d8628a61cc plugins.roster: Change variable name to what's used in XMPP-IM
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
68 function roster:add_contact(jid, name, groups, callback)
c9d8628a61cc plugins.roster: Change variable name to what's used in XMPP-IM
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
69 local item = { jid = jid, name = name, groups = groups };
192
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
70 local stanza = verse.iq({ type = "set" })
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
71 :tag("query", { xmlns = xmlns_roster })
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
72 :add_child(item_lua2xml(item));
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
73 stream:send_iq(stanza, function (reply)
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
74 if not callback then return end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
75 if reply.attr.type == "result" then
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
76 callback(true);
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
77 else
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
78 type, condition, text = reply:get_error();
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
79 callback(nil, { type, condition, text });
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
80 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
81 end);
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
82 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
83 -- What about subscriptions?
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
84
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
85 function roster:delete_contact(jid, callback)
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
86 jid = (type(jid) == "table" and jid.jid) or jid;
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
87 local item = { jid = jid, subscription = "remove" }
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
88 if not roster.items[jid] then return false, "item-not-found"; end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
89 stream:send_iq(verse.iq({ type = "set" })
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
90 :tag("query", { xmlns = xmlns_roster })
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
91 :add_child(item_lua2xml(item)),
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
92 function (reply)
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
93 if not callback then return end
213
aa3088108021 plugins.roster: Fix wrong name of reply variable
Kim Alvefur <zash@zash.se>
parents: 210
diff changeset
94 if reply.attr.type == "result" then
192
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
95 callback(true);
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
96 else
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
97 type, condition, text = reply:get_error();
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
98 callback(nil, { type, condition, text });
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
99 end
82
f306c2f8bb88 plugins.roster: Unfinished roster plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end);
f306c2f8bb88 plugins.roster: Unfinished roster plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end
192
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
102
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
103 local function add_item(item) -- Takes one roster <item/>
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
104 local roster_item = item_xml2lua(item);
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
105 roster.items[roster_item.jid] = roster_item;
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
106 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
107
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
108 -- Private low level
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
109 local function delete_item(jid)
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
110 local deleted_item = roster.items[jid];
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
111 roster.items[jid] = nil;
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
112 return deleted_item;
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
113 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
114
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
115 function roster:fetch(callback)
217
60db4e738910 plugins.roster: Check stream features for roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 213
diff changeset
116 stream:send_iq(verse.iq({type="get"}):tag("query", { xmlns = xmlns_roster, ver = ver_supported and roster.ver or nil }),
192
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
117 function (result)
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
118 if result.attr.type == "result" then
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
119 local query = result:get_child("query", xmlns_roster);
210
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
120 if query then
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
121 roster.items = {};
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
122 for item in query:childtags("item") do
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
123 add_item(item)
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
124 end
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
125 roster.ver = query.attr.ver or "";
192
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
126 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
127 callback(roster);
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
128 else
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
129 type, condition, text = stanza:get_error();
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
130 callback(nil, { type, condition, text }); --FIXME
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
131 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
132 end);
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
133 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
134
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
135 stream:hook("iq/"..xmlns_roster, function(stanza)
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
136 local type, from = stanza.attr.type, stanza.attr.from;
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
137 if type == "set" and (not from or from == bare_jid(stream.jid)) then
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
138 local query = stanza:get_child("query", xmlns_roster);
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
139 local item = query and query:get_child("item");
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
140 if item then
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
141 local event, target;
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
142 local jid = item.attr.jid;
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
143 if item.attr.subscription == "remove" then
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
144 event = "removed"
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
145 target = delete_item(jid);
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
146 else
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
147 event = roster.items[jid] and "changed" or "added";
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
148 add_item(item)
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
149 target = roster.items[jid];
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
150 end
210
118da85cb3ce plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents: 209
diff changeset
151 roster.ver = query.attr.ver;
209
48a404e083c4 plugins.roster: Don't signal roster events if there was no such item
Kim Alvefur <zash@zash.se>
parents: 192
diff changeset
152 if target then
48a404e083c4 plugins.roster: Don't signal roster events if there was no such item
Kim Alvefur <zash@zash.se>
parents: 192
diff changeset
153 stream:event("roster/item-"..event, target);
48a404e083c4 plugins.roster: Don't signal roster events if there was no such item
Kim Alvefur <zash@zash.se>
parents: 192
diff changeset
154 end
192
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
155 -- TODO else return error? Events?
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
156 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
157 stream:send(verse.reply(stanza))
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
158 return true;
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
159 end
d20feb8034f5 plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents: 82
diff changeset
160 end);
82
f306c2f8bb88 plugins.roster: Unfinished roster plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 end