Mercurial > prosody-modules
comparison mod_fallback_vcard/mod_fallback_vcard.lua @ 1899:ceb594a14a18
mod_fallback_vcard: Generates missing vcards from a template
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 08 Oct 2015 21:01:48 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1898:d85ddd3e588a | 1899:ceb594a14a18 |
|---|---|
| 1 local datamanager = require "util.datamanager"; | |
| 2 local usermanager = require "core.usermanager"; | |
| 3 local st = require "util.stanza"; | |
| 4 local host = module.host; | |
| 5 local jid_split = require "util.jid".split; | |
| 6 | |
| 7 local orgname = module:get_option_string("default_vcard_orgname"); | |
| 8 local orgmail = module:get_option_boolean("default_vcard_orgmail"); | |
| 9 | |
| 10 module:hook("iq/bare/vcard-temp:vCard", function(event) | |
| 11 local session, stanza = event.origin, event.stanza; | |
| 12 local to = stanza.attr.to; | |
| 13 local username = jid_split(to); | |
| 14 if not username then return end | |
| 15 | |
| 16 local vcard = datamanager.load(username, host, "vcard"); | |
| 17 local data = datamanager.load(username, host, "account_details"); | |
| 18 local exists = usermanager.user_exists(username, host); | |
| 19 module:log("debug", "has %s: %s", "vcard", tostring(vcard)); | |
| 20 module:log("debug", "has %s: %s", "data", tostring(data)); | |
| 21 module:log("debug", "has %s: %s", "exists", tostring(exists)); | |
| 22 data = data or {}; | |
| 23 | |
| 24 if not(vcard) and data and exists then | |
| 25 -- MAYBE | |
| 26 -- first .. " " .. last | |
| 27 -- first, last = name:match("^(%w+) (%w+)$") | |
| 28 local vcard = st.reply(stanza):tag("vCard", { xmlns = "vcard-temp" }) | |
| 29 :tag("VERSION"):text("3.0"):up() | |
| 30 :tag("N") | |
| 31 :tag("FAMILY"):text(data.last or ""):up() | |
| 32 :tag("GIVEN"):text(data.first or ""):up() | |
| 33 :up() | |
| 34 :tag("FN"):text(data.name or ""):up() | |
| 35 :tag("NICKNAME"):text(data.nick or username):up() | |
| 36 :tag("JABBERID"):text(username.."@"..host):up(); | |
| 37 if orgmail then | |
| 38 vcard:tag("EMAIL"):tag("USERID"):text(username.."@"..host):up():up(); | |
| 39 elseif data.email then | |
| 40 vcard:tag("EMAIL"):tag("USERID"):text(data.email):up():up(); | |
| 41 end | |
| 42 if orgname then | |
| 43 vcard:tag("ORG"):tag("ORGNAME"):text(orgname):up():up(); | |
| 44 end | |
| 45 session.send(vcard); | |
| 46 return true; | |
| 47 end | |
| 48 end, 1); |
