annotate mod_profile/mod_profile.lua @ 1743:8fbe16c83807

mod_profile: Don't use import()
author Kim Alvefur <zash@zash.se>
date Mon, 18 May 2015 02:55:39 +0200
parents 48d95ab404c7
children fbef84033fae
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- mod_profile
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local st = require"util.stanza";
1743
8fbe16c83807 mod_profile: Don't use import()
Kim Alvefur <zash@zash.se>
parents: 1516
diff changeset
4 local jid_split = require"util.jid".split;
8fbe16c83807 mod_profile: Don't use import()
Kim Alvefur <zash@zash.se>
parents: 1516
diff changeset
5 local jid_bare = require"util.jid".bare;
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local is_admin = require"core.usermanager".is_admin;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local vcard = require"util.vcard";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local base64 = require"util.encodings".base64;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local sha1 = require"util.hashes".sha1;
1446
843769eb40c3 mod_profile: Don't include photo in vCard4 version (use XEP-0084 instead)
Kim Alvefur <zash@zash.se>
parents: 1442
diff changeset
10 local t_insert, t_remove = table.insert, table.remove;
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local pep_plus;
1420
808950ab007b mod_profile: Integrate with mod_pep_plus by default
Kim Alvefur <zash@zash.se>
parents: 1419
diff changeset
13 if module:get_host_type() == "local" and module:get_option_boolean("vcard_to_pep", true) then
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 pep_plus = module:depends"pep_plus";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local storage = module:open_store();
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local legacy_storage = module:open_store("vcard");
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local function get_item(vcard, name)
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local item;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 for i=1, #vcard do
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 item=vcard[i];
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 if item.name == name then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 return item, i;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local magic_mime = {
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 ["\137PNG\r\n\026\n"] = "image/png";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 ["\255\216"] = "image/jpeg";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 ["GIF87a"] = "image/gif";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 ["GIF89a"] = "image/gif";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 ["<?xml"] = "image/svg+xml";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 }
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 local function identify(data)
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 for magic, mime in pairs(magic_mime) do
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 if data:sub(1, #magic) == magic then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 return mime;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 return "application/octet-stream";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
1515
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
46 local function update_pep(username, data, pep)
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
47 pep = pep or pep_plus.get_pep_service(username.."@"..module.host);
1446
843769eb40c3 mod_profile: Don't include photo in vCard4 version (use XEP-0084 instead)
Kim Alvefur <zash@zash.se>
parents: 1442
diff changeset
48 local photo, p = get_item(data, "PHOTO");
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 if vcard.to_vcard4 then
1446
843769eb40c3 mod_profile: Don't include photo in vCard4 version (use XEP-0084 instead)
Kim Alvefur <zash@zash.se>
parents: 1442
diff changeset
50 if p then t_remove(data, p); end
1422
c3882787ae06 mod_profile: Purge nodes before publishing new items
Kim Alvefur <zash@zash.se>
parents: 1420
diff changeset
51 pep:purge("urn:xmpp:vcard4", true);
1424
9892a537e7fc mod_profile: Add id to item tag too.
Kim Alvefur <zash@zash.se>
parents: 1423
diff changeset
52 pep:publish("urn:xmpp:vcard4", true, "current", st.stanza("item", {id="current"})
9892a537e7fc mod_profile: Add id to item tag too.
Kim Alvefur <zash@zash.se>
parents: 1423
diff changeset
53 :add_child(vcard.to_vcard4(data)));
1446
843769eb40c3 mod_profile: Don't include photo in vCard4 version (use XEP-0084 instead)
Kim Alvefur <zash@zash.se>
parents: 1442
diff changeset
54 if p then t_insert(data, p, photo); end
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 local nickname = get_item(data, "NICKNAME");
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 if nickname and nickname[1] then
1422
c3882787ae06 mod_profile: Purge nodes before publishing new items
Kim Alvefur <zash@zash.se>
parents: 1420
diff changeset
59 pep:purge("http://jabber.org/protocol/nick", true);
1424
9892a537e7fc mod_profile: Add id to item tag too.
Kim Alvefur <zash@zash.se>
parents: 1423
diff changeset
60 pep:publish("http://jabber.org/protocol/nick", true, "current", st.stanza("item", {id="current"})
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 :tag("nick", { xmlns="http://jabber.org/protocol/nick" }):text(nickname[1]));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 if photo and photo[1] then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 local photo_raw = base64.decode(photo[1]);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 local photo_hash = sha1(photo_raw, true);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67
1422
c3882787ae06 mod_profile: Purge nodes before publishing new items
Kim Alvefur <zash@zash.se>
parents: 1420
diff changeset
68 pep:purge("urn:xmpp:avatar:metadata", true);
c3882787ae06 mod_profile: Purge nodes before publishing new items
Kim Alvefur <zash@zash.se>
parents: 1420
diff changeset
69 pep:purge("urn:xmpp:avatar:data", true);
1503
c4c9ccc6e6c9 mod_profile: Set metadata nodes ItemID to the same hash as the data node
Kim Alvefur <zash@zash.se>
parents: 1499
diff changeset
70 pep:publish("urn:xmpp:avatar:metadata", true, photo_hash, st.stanza("item", {id=photo_hash})
1469
019746bba318 mod_profile: Add the missing <info> child element and move info attributes there
Kim Alvefur <zash@zash.se>
parents: 1446
diff changeset
71 :tag("metadata", { xmlns="urn:xmpp:avatar:metadata" })
019746bba318 mod_profile: Add the missing <info> child element and move info attributes there
Kim Alvefur <zash@zash.se>
parents: 1446
diff changeset
72 :tag("info", {
019746bba318 mod_profile: Add the missing <info> child element and move info attributes there
Kim Alvefur <zash@zash.se>
parents: 1446
diff changeset
73 bytes = tostring(#photo_raw),
019746bba318 mod_profile: Add the missing <info> child element and move info attributes there
Kim Alvefur <zash@zash.se>
parents: 1446
diff changeset
74 id = photo_hash,
019746bba318 mod_profile: Add the missing <info> child element and move info attributes there
Kim Alvefur <zash@zash.se>
parents: 1446
diff changeset
75 type = identify(photo_raw),
019746bba318 mod_profile: Add the missing <info> child element and move info attributes there
Kim Alvefur <zash@zash.se>
parents: 1446
diff changeset
76 }));
1470
b291a9423e0f mod_profile: Fix wrong use of "current" as id for avatar data
Kim Alvefur <zash@zash.se>
parents: 1469
diff changeset
77 pep:publish("urn:xmpp:avatar:data", true, photo_hash, st.stanza("item", {id=photo_hash})
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 :tag("data", { xmlns="urn:xmpp:avatar:data" }):text(photo[1]));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 -- The "temporary" vCard XEP-0054 part
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 module:add_feature("vcard-temp");
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 local function handle_get(event)
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 local origin, stanza = event.origin, event.stanza;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 local username = origin.username;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 local to = stanza.attr.to;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 if to then username = jid_split(to); end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 local data = storage:get(username);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 if not data then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 data = legacy_storage:get(username);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 data = data and st.deserialize(data);
1441
07c9306c2c1f mod_profile: Don't pass old vcard data trough vcard lib
Kim Alvefur <zash@zash.se>
parents: 1424
diff changeset
94 if data then
07c9306c2c1f mod_profile: Don't pass old vcard data trough vcard lib
Kim Alvefur <zash@zash.se>
parents: 1424
diff changeset
95 return origin.send(st.reply(stanza):add_child(data));
07c9306c2c1f mod_profile: Don't pass old vcard data trough vcard lib
Kim Alvefur <zash@zash.se>
parents: 1424
diff changeset
96 end
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 if not data then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 return origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 return origin.send(st.reply(stanza):add_child(vcard.to_xep54(data)));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 local function handle_set(event)
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 local origin, stanza = event.origin, event.stanza;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 local data = vcard.from_xep54(stanza.tags[1]);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 local username = origin.username;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 local to = stanza.attr.to;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 if to then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 if not is_admin(jid_bare(stanza.attr.from), module.host) then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 return origin.send(st.error_reply(stanza, "auth", "forbidden"));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 username = jid_split(to);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 local ok, err = storage:set(username, data);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 if not ok then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 return origin.send(st.error_reply(stanza, "cancel", "internal-server-error", err));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 if pep_plus and username then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 update_pep(username, data);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 return origin.send(st.reply(stanza));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 module:hook("iq-get/bare/vcard-temp:vCard", handle_get);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 module:hook("iq-get/host/vcard-temp:vCard", handle_get);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 module:hook("iq-set/bare/vcard-temp:vCard", handle_set);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 module:hook("iq-set/host/vcard-temp:vCard", handle_set);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132
1516
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
133 local function on_publish(event)
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
134 if event.actor == true then return end -- Not from a client
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
135 local node, item = event.node, event.item;
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
136 local username = jid_split(event.actor);
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
137 local data = storage:get(username) or {};
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
138 if node == "urn:xmpp:avatar:data" then
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
139 local new_photo = item:get_child_text("data", "urn:xmpp:avatar:data");
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
140 new_photo = new_photo and { name = "PHOTO"; ENCODING = { "b" }; new_photo } or nil;
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
141 local _, i = get_item(data, "PHOTO")
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
142 if new_photo then
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
143 data[i or #data+1] = new_photo;
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
144 elseif i then
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
145 table.remove(data, i);
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
146 end
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
147 elseif node == "http://jabber.org/protocol/nick" then
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
148 local new_nick = item:get_child_text("nick", "http://jabber.org/protocol/nick");
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
149 new_nick = new_nick and new_nick ~= "" and { name = "NICKNAME"; new_nick } or nil;
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
150 local _, i = get_item(data, "NICKNAME")
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
151 if new_nick then
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
152 data[i or #data+1] = new_nick;
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
153 elseif i then
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
154 table.remove(data, i);
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
155 end
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
156 else
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
157 return;
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
158 end
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
159 storage:set(username, data);
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
160 end
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
161
1515
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
162 local function pep_service_added(event)
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
163 local item = event.item;
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
164 local service, username = item.service, jid_split(item.jid);
1516
48d95ab404c7 mod_profile: Save photo and nickname from PEP to vCard
Kim Alvefur <zash@zash.se>
parents: 1515
diff changeset
165 service.events.add_handler("item-published", on_publish);
1515
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
166 local data = storage:get(username);
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
167 if data then
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
168 update_pep(username, data, service);
1442
253e374824a8 mod_profile: Load profile into PEP on initial presence
Kim Alvefur <zash@zash.se>
parents: 1441
diff changeset
169 end
1515
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
170 end
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
171
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
172 local function pep_service_removed()
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
173 -- This would happen when mod_pep_plus gets unloaded, but this module gets unloaded before that
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
174 end
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
175
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
176 function module.load()
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
177 module:handle_items("pep-service", pep_service_added, pep_service_removed, true);
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
178 end
1442
253e374824a8 mod_profile: Load profile into PEP on initial presence
Kim Alvefur <zash@zash.se>
parents: 1441
diff changeset
179
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 -- The vCard4 part
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 if vcard.to_vcard4 then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 module:add_feature("urn:ietf:params:xml:ns:vcard-4.0");
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 module:hook("iq-get/bare/urn:ietf:params:xml:ns:vcard-4.0:vcard", function(event)
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 local origin, stanza = event.origin, event.stanza;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 local username = jid_split(stanza.attr.to) or origin.username;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 local data = storage:get(username);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 if not data then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 return origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 return origin.send(st.reply(stanza):add_child(vcard.to_vcard4(data)));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 end);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 if vcard.from_vcard4 then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 module:hook("iq-set/self/urn:ietf:params:xml:ns:vcard-4.0:vcard", function(event)
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 local origin, stanza = event.origin, event.stanza;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 local ok, err = storage:set(origin.username, vcard.from_vcard4(stanza.tags[1]));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 if not ok then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 return origin.send(st.error_reply(stanza, "cancel", "internal-server-error", err));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 return origin.send(st.reply(stanza));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 end);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 else
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 module:hook("iq-set/self/urn:ietf:params:xml:ns:vcard-4.0:vcard", function(event)
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 local origin, stanza = event.origin, event.stanza;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 return origin.send(st.error_reply(stanza, "cancel", "feature-not-implemented"));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 end);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210