annotate mod_profile/mod_profile.lua @ 1420:808950ab007b

mod_profile: Integrate with mod_pep_plus by default
author Kim Alvefur <zash@zash.se>
date Thu, 29 May 2014 00:58:33 +0200
parents 877dc87539ff
children c3882787ae06
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";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local jid_split, jid_bare = import("util.jid", "split", "bare");
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 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
6 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
7 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
8 local sha1 = require"util.hashes".sha1;
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
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local pep_plus;
1420
808950ab007b mod_profile: Integrate with mod_pep_plus by default
Kim Alvefur <zash@zash.se>
parents: 1419
diff changeset
11 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
12 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
13 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
14
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 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
16 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
17
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 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
19 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
20 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
21 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
22 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
23 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
24 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
25 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
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
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 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
29 ["\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
30 ["\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
31 ["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
32 ["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
33 ["<?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
34 }
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 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
36 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
37 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
38 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
39 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
40 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
41 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
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
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 local function 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
45 local pep = pep_plus.get_pep_service(username.."@"..module.host);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 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
47 pep:publish("urn:xmpp:vcard4", true, "", st.stanza("item"):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
48 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
49
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 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
51 if nickname and nickname[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
52 pep:publish("http://jabber.org/protocol/nick", true, "", st.stanza("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
53 :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
54 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
55
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 local photo = get_item(data, "PHOTO");
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 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
58 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
59 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
60
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 pep:publish("urn:xmpp:avatar:metadata", true, "", st.stanza("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
62 :tag("metadata", {
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 xmlns="urn:xmpp:avatar:metadata",
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 bytes = tostring(#photo_raw),
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 id = photo_hash,
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 type = identify(photo_raw),
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 }));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 pep:publish("urn:xmpp:avatar:data", true, photo_hash, st.stanza("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
69 :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
70 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
71 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
72
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 -- 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
74 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
75
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 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
77 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
78 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
79 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
80 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
81 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
82 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
83 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
84 data = data and st.deserialize(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
85 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
86 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
87 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
88 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
89 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
90 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
91
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 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
93 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
94 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
95 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
96 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
97 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
98 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
99 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
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 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
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 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
104 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
105 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
106 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
107
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 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
109 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
110 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
111
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 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
113 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
114
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 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
116 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
117
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 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
119 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
120
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 -- 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
122 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
123 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
124
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 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
126 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
127 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
128 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
129 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
130 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
131 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
132 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
133 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
134
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 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
136 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
137 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
138 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
139 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
140 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
141 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
142 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
143 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
144 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
145 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
146 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
147 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
148 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
149 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
150 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
151