Files
vnctalk-prosody/vnctalk/mod_vnc_vcard_fallback.lua

107 lines
3.3 KiB
Lua

local datamanager = require "util.datamanager";
local usermanager = require "core.usermanager";
local st = require "util.stanza";
local host = module.host;
local jid_split = require "util.jid".split;
local orgname = module:get_option_string("default_vcard_orgname");
local orgmail = module:get_option_boolean("default_vcard_orgmail");
local function trim(s)
if s and (type(s) == "string") then
return (s:gsub("^%s*(.-)%s*$", "%1"))
else
return s
end
end
local function collapseSpaces(s)
if s and (type(s) == "string") then
return (s:gsub("%s+", " "))
else
return s
end
end
local function prettyUsername(userName)
userName = string.upper(string.sub(userName, 1, 1))..string.sub(userName, 2, -1) -- upcase first char in any case
if string.find(userName, "%.") -- contains dot
then
local i = 1
while true do
local j = string.find(userName, "%.", i)
if j == nil or #userName == i then break end
-- replace dot with space and upcase next char
userName = string.sub(userName, 1, j-1).." "..string.upper(string.sub(userName, j+1, j+1))..string.sub(userName, j+2, -1)
if #userName <= j then break end
i = j
end
end
return trim(collapseSpaces(userName));
end
module:hook("iq/bare/vcard-temp:vCard", function(event)
local session, stanza = event.origin, event.stanza;
local to = stanza.attr.to;
local from = stanza.attr.from;
local username = "";
if (stanza.attr.to == nil) then
-- module:log("info", "query for vcard from stanza.attr.from: %s", stanza.attr.from);
username = jid_split(from);
else
-- module:log("info", "query for vcard for stanza.attr.to: %s", stanza.attr.to);
username = jid_split(to);
end
-- return
if not username then return end
local vcard = datamanager.load(username, host, "vcard");
local exists = usermanager.user_exists(username, host);
-- module:log("info", "query for vcard for user: %s host %s", username, host);
module:log("debug", "has %s: %s", "vcard", tostring(vcard));
module:log("debug", "has %s: %s", "exists", tostring(exists));
-- data = data or {};
if vcard then
module:log("debug", "found existing vcard for %s", username);
end
local firstraw = "";
local lastraw = "";
local dotpos = string.find(username, "%.");
if dotpos ~= nil then
firstraw = prettyUsername(string.sub(username, 0, dotpos-1));
lastraw = prettyUsername(string.sub(username, dotpos+1));
module:log("info","found raw firstname / lastname from username: %s %s", firstraw, lastraw);
else
firstraw = prettyUsername(username);
module:log("info", "found raw firstname from username: %s", firstraw);
end
local nickname = prettyUsername(username);
if not(vcard) and exists then
-- MAYBE
-- first .. " " .. last
-- first, last = name:match("^(%w+) (%w+)$")
local vcard = st.reply(stanza):tag("vCard", { xmlns = "vcard-temp" })
:tag("VERSION"):text("3.0"):up()
:tag("N")
:tag("FAMILY"):text(lastraw):up()
:tag("GIVEN"):text(firstraw):up()
:up()
:tag("FN"):text(prettyUsername(username) or ""):up()
:tag("NICKNAME"):text(prettyUsername(username)):up()
-- :tag("JABBERID"):text(username.."@"..host):up();
if orgname then
vcard:tag("ORG"):tag("ORGNAME"):text(orgname):up():up();
end
session.send(vcard);
return true;
else
module:log("debug", "not modifying vcard");
end
end, 1);