comparison mod_pep_vcard_avatar/mod_pep_vcard_avatar.lua @ 2150:c1ecc3bc88fa

mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
author Kim Alvefur <zash@zash.se>
date Sun, 03 Apr 2016 00:19:51 +0200
parents d9e91240a2dd
children 96aca307964b
comparison
equal deleted inserted replaced
2149:d9e91240a2dd 2150:c1ecc3bc88fa
25 end 25 end
26 if not vcard then 26 if not vcard then
27 vcard = st.stanza("vCard", { xmlns = "vcard-temp" }); 27 vcard = st.stanza("vCard", { xmlns = "vcard-temp" });
28 end 28 end
29 return vcard, err; 29 return vcard, err;
30 end
31
32 local function replace_tag(s, replacement)
33 local once = false;
34 s:maptags(function (tag)
35 if tag.name == replacement.name and tag.attr.xmlns == replacement.attr.xmlns then
36 if not once then
37 once = true;
38 return replacement;
39 else
40 return nil;
41 end
42 end
43 return tag;
44 end);
45 if not once then
46 s:add_child(replacement);
47 end
30 end 48 end
31 49
32 local function set_vcard(username, vcard) 50 local function set_vcard(username, vcard)
33 if vcard then 51 if vcard then
34 vcard = st.preserialize(st.clone(vcard)); 52 vcard = st.preserialize(st.clone(vcard));
93 if not pep_photo then 111 if not pep_photo then
94 module:log("error", "No photo found"); 112 module:log("error", "No photo found");
95 return; 113 return;
96 end -- Publishing in the wrong order? 114 end -- Publishing in the wrong order?
97 local vcard = get_vcard(username); 115 local vcard = get_vcard(username);
98 local old_photo = vcard:get_child("PHOTO"); 116 local new_photo = st.stanza("PHOTO", { xmlns = "vcard-temp" })
99 if old_photo then 117 :tag("TYPE"):text(metadat.attr.type):up()
100 local photo_type = old_photo:get_child("TYPE"); 118 :tag("BINVAL"):text(pep_photo:get_child_text("data", "urn:xmpp:avatar:data"));
101 photo_type[1] = metadata.attr.type; 119
102 local photo_b64 = old_photo:get_child("BINVAL"); 120 replace_tag(vcard, new_photo);
103 photo_b64[1] = pep_photo:get_child_text("data", "urn:xmpp:avatar:data");
104 end
105 set_vcard(username, vcard); 121 set_vcard(username, vcard);
106 end 122 end
107 123
108 -- PEP Nickname -> vCard 124 -- PEP Nickname -> vCard
109 local function on_publish_nick(event) 125 local function on_publish_nick(event)
110 local username = event.session.username; 126 local username = event.session.username;
111 local vcard = get_vcard(username); 127 local vcard = get_vcard(username);
112 local new_nick = event.item:get_child_text("nick", "http://jabber.org/protocol/nick"); 128 local new_nick = st.stanza("NICKNAME", { xmlns = "vcard-temp" })
113 local old_nick = vcard:get_child("NICKNAME"); 129 :text(event.item:get_child_text("nick", "http://jabber.org/protocol/nick"));
114 if not old_nick then 130 replace_tag(vcard, new_nick);
115 vcard:tag("NICKNAME"):text(new_nick);
116 elseif old_nick[1] ~= new_nick then
117 old_nick[1] = new_nick;
118 else
119 return -- no change
120 end
121 set_vcard(username, vcard); 131 set_vcard(username, vcard);
122 end 132 end
123 133
124 local function on_publish(event) 134 local function on_publish(event)
125 if event.actor == true then return end -- Not from a client 135 if event.actor == true then return end -- Not from a client