Skip to content

Instantly share code, notes, and snippets.

@metalaureate
Created March 21, 2015 16:25
Show Gist options
  • Save metalaureate/071148513466e24224f2 to your computer and use it in GitHub Desktop.
Save metalaureate/071148513466e24224f2 to your computer and use it in GitHub Desktop.
Modifies https://code.google.com/p/prosody-modules/wiki/mod_offline_email and adds vCard email forwading.
local jid_bare = require "util.jid".bare;
local jid_split = require "util.jid".split;
local os_time = os.time;
local t_concat = table.concat;
local smtp = require "socket.smtp";
local vcards = module:open_store("vcard");
local smtp_server = module:get_option_string("smtp_server", "localhost");
local smtp_user = module:get_option_string("smtp_username");
local smtp_pass = module:get_option_string("smtp_password");
local smtp_daemon = module:get_option_string("smtp_daemon");
local st = require "util.stanza";
local smtp_address = module:get_option("smtp_from") or ((smtp_user or "xmpp") .. "@" .. (smtp_server or module.host));
local queue_offline_emails = module:get_option("queue_offline_emails");
if queue_offline_emails == true then queue_offline_emails = 300; end
local send_message_as_email;
module:hook("message/offline/handle", function(event)
local stanza = event.stanza;
local text = stanza:get_child_text("body");
if text then
local username = jid_split(stanza.attr.to);
module:log("info", "vCard for %s", username);
local email; -- nil by default
local vCardRaw = vcards:get(username);
if vCardRaw then
-- note: it is not safe to do this in an open xmpp network
local vcard = st.deserialize(vCardRaw);
email = vcard:get_child("EMAIL");
email = email and email:get_child_text("USERID");
text = text .. "\n\n /to:" .. jid_bare(stanza.attr.to) .. "/from:" .. jid_bare(stanza.attr.from) .. "/";
if email then
return send_message_as_email(email, smtp_daemon, text);
end
end
end
end, 1);
function send_message_as_email(address, from_address, message_text, subject)
module:log("info", "Forwarding offline message to %s via email", address);
local rcpt = "<" .. address .. ">";
local mesgt = {
headers = {
to = address;
subject = subject or ("Offline message from " .. jid_bare(from_address));
};
body = message_text;
};
local ok, err = smtp.send {
from = smtp_address,
rcpt = rcpt,
source = smtp.message(mesgt),
server = smtp_server,
user = smtp_user,
password = smtp_pass
};
if not ok then
module:log("error", "Failed to deliver to %s: %s", tostring(address), tostring(err));
return;
end
return true;
end
if queue_offline_emails then
local queues = {};
local real_send_message_as_email = send_message_as_email;
function send_message_as_email(address, from_address, message_text)
local pair_key = address .. "\0" .. from_address;
local queue = queues[pair_key];
if not queue then
queue = { from = smtp_address, to = address, messages = {} };
queues[pair_key] = queue;
module:add_timer(queue_offline_emails + 5, function()
module:log("info", "Checking on %s", from_address);
local current_time = os_time();
local diff = current_time - queue.last_message_time;
if diff > queue_offline_emails then
module:log("info", "Enough silence, sending...");
real_send_message_as_email(address, from_address, t_concat(queue.messages, "\n"), "You have " .. #queue.messages .. " offline message" .. (#queue.messages == 1 and "" or "s") .. " from " .. from_address)
else
module:log("info", "Next check in %d", queue_offline_emails - diff + 5);
return queue_offline_emails - diff + 5;
end
end);
end
queue.last_message_time = os_time();
local messages = queue.messages;
messages[#messages + 1] = message_text;
return true;
end
end
@tristan-k
Copy link

I installed this version, but it isnt working. There are no emails send to users with a vCard email-address.

@tristan-k
Copy link

I'm using prosody trunk (version 0.10). Here is the error log:

@400000005610796f2f99823c jabber.tristank.de:offline_email          info    vCard  for guest
@400000005610796f2f998624 jabber.tristank.de:offline_email          info    Forwarding offline message to [email protected] via email
@400000005610796f2f99a564 c2s24297d0                                error   Traceback[c2s]: ...sody-modules/mod_offline_email/mod_offline_email.lua:48: attempt to concatenate a nil value
@400000005610796f2f99ad34 stack traceback:
@400000005610796f2f99ad34   ...sody-modules/mod_offline_email/mod_offline_email.lua:48: in function <...sody-modules/mod_offline_email/mod_offline_email.lua:41>
@400000005610796f2f99c4a4   (tail call): ?
@400000005610796f2f99c4a4   ...home/tristank/.toast/armed/lib/prosody/util/events.lua:73: in function <...home/tristank/.toast/armed/lib/prosody/util/events.lua:69>
@400000005610796f2f99c88c   (tail call): ?
@400000005610796f2f99cc74   (tail call): ?
@400000005610796f2f99cc74   ...cke/.toast/armed/lib/prosody/modules/mod_message.lua:53: in function <...cke/.toast/armed/lib/prosody/modules/mod_message.lua:18>
@400000005610796f2f99e3e4   (tail call): ?
@400000005610796f2f99e3e4   ...home/tristank/.toast/armed/lib/prosody/util/events.lua:73: in function <...home/tristank/.toast/armed/lib/prosody/util/events.lua:69>
@400000005610796f2f99e7cc   (tail call): ?
@400000005610796f2f99e7cc   ...ocke/.toast/armed/lib/prosody/core/stanza_router.lua:187: in function 'core_post_stanza'
@400000005610796f2f99ff3c   ...ocke/.toast/armed/lib/prosody/core/stanza_router.lua:135: in function 'core_process_stanza'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment