mod_pep_vcard_avatar/mod_pep_vcard_avatar.lua
changeset 2866 933049a60ce6
parent 2210 724809023c92
child 2868 6f3859233515
equal deleted inserted replaced
2865:afeb06e4cdea 2866:933049a60ce6
     1 -- Prosody IM
     1 -- Prosody IM
     2 -- Copyright (C) 2008-2014 Matthew Wild
     2 -- Copyright (C) 2008-2014 Matthew Wild
     3 -- Copyright (C) 2008-2014 Waqas Hussain
     3 -- Copyright (C) 2008-2014 Waqas Hussain
     4 -- Copyright (C) 2014 Kim Alvefur
     4 -- Copyright (C) 2014 Kim Alvefur
       
     5 -- Copyright (c) 2018 Daniel Gultsch
     5 --
     6 --
     6 -- This project is MIT/X11 licensed. Please see the
     7 -- This project is MIT/X11 licensed. Please see the
     7 -- COPYING file in the source package for more information.
     8 -- COPYING file in the source package for more information.
     8 --
     9 --
     9 
    10 
   141 		return on_publish_nick(event);
   142 		return on_publish_nick(event);
   142 	end
   143 	end
   143 end
   144 end
   144 
   145 
   145 module:hook("pep-publish-item", on_publish, 1);
   146 module:hook("pep-publish-item", on_publish, 1);
       
   147 
       
   148 local function calculate_avatar_hash(username)
       
   149 	local vcard = get_vcard(username)
       
   150 	local photo = vcard and vcard:get_child("PHOTO")
       
   151 	if photo then
       
   152 		local photo_type = photo:get_child_text("TYPE");
       
   153 		local photo_b64 = photo:get_child_text("BINVAL");
       
   154 		local photo_raw = photo_b64 and base64.decode(photo_b64);
       
   155 		if photo_raw and photo_type then
       
   156 			return sha1(photo_raw, true);
       
   157 		end
       
   158 	end
       
   159 	return ""
       
   160 end
       
   161 
       
   162 local function get_avatar_hash(username, host)
       
   163 	local session = prosody.hosts[host] and prosody.hosts[host].sessions[username]
       
   164 	if not session then
       
   165 		return nil
       
   166 	end
       
   167 	if not session.avatar_hash then
       
   168 		session.avatar_hash  = calculate_avatar_hash(username)
       
   169 	end
       
   170 	return session.avatar_hash
       
   171 end
       
   172 
       
   173 local function on_send_presence(event)
       
   174 	local stanza, session = event.stanza, event.origin
       
   175 	if stanza.attr.type then
       
   176 		return
       
   177 	end
       
   178 	local hash = get_avatar_hash(session.username, session.host)
       
   179 	if hash and hash ~= "" then
       
   180 		local x_vcard_update = stanza:get_child("x","vcard-temp:x:update")
       
   181 		if not x_vcard_update then
       
   182 			x_vcard_update = st.stanza("x",{xmlns="vcard-temp:x:update"})
       
   183 			stanza:add_child(x_vcard_update)
       
   184 		end
       
   185 		local photo = x_vcard_update:get_child("photo")
       
   186 		if not photo then
       
   187 			photo = st.stanza("photo")
       
   188 			photo:text(hash)
       
   189 			x_vcard_update:add_child(photo)
       
   190 		elseif photo:get_text() then
       
   191 			photo:text(hash)
       
   192 		end
       
   193 	end
       
   194 end
       
   195 
       
   196 module:hook("pre-presence/full", on_send_presence)
       
   197 module:hook("pre-presence/bare", on_send_presence)
       
   198 module:hook("pre-presence/host", on_send_presence)