Software /
code /
verse
Annotate
plugins/roster.lua @ 406:3c732f1d990c
plugins.presence: If a string is given as presece options, use it as status
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 04 Jun 2016 13:37:06 +0200 |
parent | 395:e86144a4eaa1 |
child | 490:6b2f31da9610 |
rev | line source |
---|---|
250 | 1 local verse = require "verse"; |
2 local bare_jid = require "util.jid".bare; | |
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 |
380 | 27 if k ~= "groups" then |
192
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 |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
43 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
|
44 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
|
45 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
|
46 end |
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 |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
49 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
|
50 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
|
51 end |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
52 return item_table; |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
53 end |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
54 |
210
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
55 function roster:load(r) |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
56 roster.ver, roster.items = r.ver, r.items; |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
57 end |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
58 |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
59 function roster:dump() |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
60 return { |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
61 ver = roster.ver, |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
62 items = roster.items, |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
63 }; |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
64 end |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
65 |
192
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
66 -- 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
|
67 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
|
68 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
|
69 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
|
70 :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
|
71 :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
|
72 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
|
73 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
|
74 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
|
75 callback(true); |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
76 else |
395 | 77 callback(nil, reply); |
192
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
78 end |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
79 end); |
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 -- What about subscriptions? |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
82 |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 :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
|
89 :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
|
90 function (reply) |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
91 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
|
92 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
|
93 callback(true); |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
94 else |
395 | 95 callback(nil, reply); |
192
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
96 end |
82
f306c2f8bb88
plugins.roster: Unfinished roster plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 end); |
f306c2f8bb88
plugins.roster: Unfinished roster plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 end |
192
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
99 |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
100 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
|
101 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
|
102 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
|
103 end |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
104 |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
105 -- Private low level |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
106 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
|
107 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
|
108 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
|
109 return deleted_item; |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
110 end |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
111 |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
112 function roster:fetch(callback) |
217
60db4e738910
plugins.roster: Check stream features for roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
213
diff
changeset
|
113 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
|
114 function (result) |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
115 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
|
116 local query = result:get_child("query", xmlns_roster); |
210
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
117 if query then |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
118 roster.items = {}; |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
119 for item in query:childtags("item") do |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
120 add_item(item) |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
121 end |
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
122 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
|
123 end |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
124 callback(roster); |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
125 else |
395 | 126 callback(nil, result); |
192
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
127 end |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
128 end); |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
129 end |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
130 |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 if item then |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
137 local event, target; |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
138 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
|
139 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
|
140 event = "removed" |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
141 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
|
142 else |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
143 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
|
144 add_item(item) |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
145 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
|
146 end |
210
118da85cb3ce
plugins.roster: Roster versioning support.
Kim Alvefur <zash@zash.se>
parents:
209
diff
changeset
|
147 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
|
148 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
|
149 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
|
150 end |
192
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
151 -- 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
|
152 end |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
153 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
|
154 return true; |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
155 end |
d20feb8034f5
plugins.roster: Add roster push handling, roster item parsing, delete_contact()
Kim Alvefur <zash@zash.se>
parents:
82
diff
changeset
|
156 end); |
82
f306c2f8bb88
plugins.roster: Unfinished roster plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 end |