118 lines
3.4 KiB
Lua
118 lines
3.4 KiB
Lua
--
|
|
|
|
local st = require "util.stanza"
|
|
local jid = require "util.jid";
|
|
local base64 = require"util.encodings".base64;
|
|
local sha1 = require"util.hashes".sha1;
|
|
local http = require "net.http";
|
|
local mm = require "core.modulemanager";
|
|
|
|
local my_host = module:get_host();
|
|
local my_host_type = module:get_host_type();
|
|
|
|
local avatar_upload_url = module:get_option("avatar_upload_url", "");
|
|
local avatar_upload_user = module:get_option("avatar_upload_user", "");
|
|
local avatar_upload_pass = module:get_option("avatar_upload_pass", "");
|
|
|
|
local avatar_update_store = module:open_store("avatarupdate", "map");
|
|
|
|
|
|
local function dumpTable(t, depth)
|
|
local result = "";
|
|
if (not depth) then depth = 0 end;
|
|
|
|
local prefix = string.rep(" ", depth);
|
|
|
|
if (type(t) ~= "table") then
|
|
result = result.." "..tostring(t);
|
|
else
|
|
for key,value in pairs(t) do
|
|
result = result..prefix..tostring(key).." => "..dumpTable(value, depth+1).."\n"
|
|
end
|
|
end
|
|
|
|
return result;
|
|
end
|
|
|
|
|
|
|
|
local function putGlobalAvatar(_userName, _host, _photo)
|
|
local to_url = avatar_upload_url.._userName.."@".._host;
|
|
module:log("info", "upload target URL: %s", to_url);
|
|
local httpRequestOptions = {
|
|
method = "PUT",
|
|
body = _photo,
|
|
headers = {
|
|
["Content-Type"] = "image/png"
|
|
}
|
|
}
|
|
if avatar_upload_user ~= "" then
|
|
httpRequestOptions = {
|
|
method = "PUT",
|
|
body = _photo,
|
|
headers = {
|
|
["Content-Type"] = "image/png",
|
|
["Authorization"] = "Basic "..base64.encode(avatar_upload_user..":"..avatar_upload_pass)
|
|
}
|
|
}
|
|
end
|
|
local function cb(content_, code_, request_, response_)
|
|
content, code, request, response = content_, code_, request_, response_;
|
|
module:log("info", "response code is: %s", code);
|
|
--done();
|
|
end
|
|
|
|
http.request(to_url, httpRequestOptions, cb);
|
|
|
|
module:log("info", "avatar upload for %s", _userName);
|
|
end
|
|
|
|
module:depends"vcard";
|
|
local vcard_storage = module:open_store("vcard");
|
|
|
|
local function get_vcard(username)
|
|
local vcard, err = vcard_storage:get(username);
|
|
if vcard then
|
|
vcard = st.deserialize(vcard);
|
|
end
|
|
if not vcard then
|
|
vcard = st.stanza("vCard", { xmlns = "vcard-temp" });
|
|
end
|
|
return vcard, err;
|
|
end
|
|
|
|
|
|
-- vCard -> upload
|
|
local function update_avt(session, vcard)
|
|
if not vcard then return end
|
|
local nickname = vcard:get_child_text("NICKNAME");
|
|
|
|
local photo = vcard:get_child("PHOTO");
|
|
if photo then
|
|
local photo_type = photo:get_child_text("TYPE");
|
|
local photo_b64 = photo:get_child_text("BINVAL");
|
|
local photo_raw = photo_b64 and base64.decode(photo_b64);
|
|
if photo_raw and photo_type then -- Else invalid data or encoding
|
|
|
|
module:log("info", "avatar update for session.username %s", session.username);
|
|
module:log("info", "avatar update for session.host %s", session.host);
|
|
local s = os.time();
|
|
avatar_update_store:set(session.username, "updated", s);
|
|
if avatar_upload_url ~= "" then
|
|
putGlobalAvatar(session.username, session.host, photo_raw);
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
local function handle_vcard(event)
|
|
module.log("info","handle_vcard called 1 - handling update from ");
|
|
local session, stanza = event.origin, event.stanza;
|
|
if not stanza.attr.to and stanza.attr.type == "set" then
|
|
module.log("info","handle_vcard called 2 - handling update from ");
|
|
return update_avt(session, stanza:get_child("vCard", "vcard-temp"));
|
|
end
|
|
end
|
|
|
|
module:hook("iq/bare/vcard-temp:vCard", handle_vcard, 1);
|