Files
vnctalk-prosody/vnctalk/mod_vcard_muc/mod_vcard_muc.lua
T

175 lines
4.9 KiB
Lua

-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
-- Copyright (C) 2018 Michel Le Bihan
--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
local st = require "util.stanza"
local jid_split = require "util.jid".split;
local base64 = require"util.encodings".base64;
local sha1 = require"util.hashes".sha1;
local http = require "net.http";
local mod_muc = module:depends"muc";
local vcards = module:open_store();
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(_room_jid, _photo)
local to_url = avatar_upload_url.._room_jid;
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", _room_jid);
end
module:add_feature("vcard-temp");
local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
function (jid)
local rooms = rawget(mod_muc, "rooms");
return rooms[jid];
end
local function broadcast_presence(room_jid, to)
local room = get_room_from_jid(room_jid);
local room_node = jid_split(room_jid);
local vcard = st.deserialize(vcards:get(room_node));
local photo_hash;
if vcard then
local photo = vcard:get_child("PHOTO");
if photo then
local photo_b64 = photo:get_child_text("BINVAL");
local photo_raw = photo_b64 and base64.decode(photo_b64);
photo_hash = sha1(photo_raw, true);
end
end
local presence_vcard = st.presence({to = to, from = room_jid})
:tag("x", { xmlns = "vcard-temp:x:update" })
:tag("photo"):text(photo_hash):up();
if to == nil then
room:broadcast_message(presence_vcard);
else
module:send(presence_vcard);
end
end
local function handle_vcard(event)
local session, stanza = event.origin, event.stanza;
local room_jid = stanza.attr.to;
local room_node = jid_split(room_jid);
local room = get_room_from_jid(room_jid);
local from = stanza.attr.from;
local from_affiliation = room:get_affiliation(from);
if stanza.attr.type == "get" then
local vCard;
vCard = st.deserialize(vcards:get(room_node));
if vCard then
session.send(st.reply(stanza):add_child(vCard)); -- send vCard!
else
session.send(st.error_reply(stanza, "cancel", "item-not-found"));
end
else
if from_affiliation == "owner" then
if vcards:set(room_node, st.preserialize(stanza.tags[1])) then
session.send(st.reply(stanza):tag("vCard", { xmlns = "vcard-temp" }));
broadcast_presence(room_jid, nil)
local vcrd = st.deserialize(vcards:get(room_node));
if vcrd then
local photo = vcrd:get_child("PHOTO");
if photo then
local photo_b64 = photo:get_child_text("BINVAL");
local photo_raw = photo_b64 and base64.decode(photo_b64);
if room_jid then
local s = os.time();
avatar_update_store:set(room_node, "updated", s);
putGlobalAvatar(room_jid, photo_raw);
module:log("info", "setting new avatar for %s @ %s", room_jid);
end
end
end
else
-- TODO unable to write file, file may be locked, etc, what's the correct error?
session.send(st.error_reply(stanza, "wait", "internal-server-error"));
end
else
session.send(st.error_reply(stanza, "auth", "forbidden"));
end
end
return true;
end
module:hook("iq/bare/vcard-temp:vCard", handle_vcard);
module:hook("iq/host/vcard-temp:vCard", handle_vcard);
module:hook("muc-disco#info", function(event)
event.reply:tag("feature", { var = "vcard-temp" }):up();
end);
-- module:hook("muc-occupant-session-new", function(event)
-- module:log("info", "skipping vcard photo presence");
-- broadcast_presence(event.room.jid, event.jid);
-- end)