annotate mod_offline_email/mod_offline_email.lua @ 3718:cc6f7e2e4a59

mod_http_muc_log: Add arrow to 'latest' link like on other navigation
author Kim Alvefur <zash@zash.se>
date Sun, 13 Oct 2019 16:56:52 +0200
parents bd71c97de1d0
children ee724c7fa8e0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local jid_bare = require "util.jid".bare;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local os_time = os.time;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local t_concat = table.concat;
3405
bd71c97de1d0 mod_offline_email: Allow LuaSocket to pollute the global scope, fixes traceback (*sigh*)
Kim Alvefur <zash@zash.se>
parents: 1285
diff changeset
5
bd71c97de1d0 mod_offline_email: Allow LuaSocket to pollute the global scope, fixes traceback (*sigh*)
Kim Alvefur <zash@zash.se>
parents: 1285
diff changeset
6 prosody.unlock_globals(); -- LuaSocket wants to pollute the global scope
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local smtp = require "socket.smtp";
3405
bd71c97de1d0 mod_offline_email: Allow LuaSocket to pollute the global scope, fixes traceback (*sigh*)
Kim Alvefur <zash@zash.se>
parents: 1285
diff changeset
8 prosody.lock_globals();
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
10 local smtp_server = module:get_option_string("smtp_server", "localhost");
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
11 local smtp_user = module:get_option_string("smtp_username");
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
12 local smtp_pass = module:get_option_string("smtp_password");
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local smtp_address = module:get_option("smtp_from") or ((smtp_user or "xmpp").."@"..(smtp_server or module.host));
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local queue_offline_emails = module:get_option("queue_offline_emails");
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 if queue_offline_emails == true then queue_offline_emails = 300; end
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local send_message_as_email;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
21 module:hook("message/offline/handle", function(event)
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
22 local stanza = event.stanza;
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
23 local text = stanza:get_child_text("body");
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
24 if text then
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
25 return send_message_as_email(jid_bare(stanza.attr.to), jid_bare(stanza.attr.from), text);
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
27 end, 1);
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 function send_message_as_email(address, from_address, message_text, subject)
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
30 module:log("info", "Forwarding offline message to %s via email", address);
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
31 local rcpt = "<"..address..">";
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
32
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
33 local mesgt = {
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
34 headers = {
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
35 to = address;
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
36 subject = subject or ("Offline message from "..jid_bare(from_address));
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 };
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
38 body = message_text;
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
39 };
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
40
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
41 local ok, err = smtp.send{ from = smtp_address, rcpt = rcpt, source = smtp.message(mesgt),
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 server = smtp_server, user = smtp_user, password = smtp_pass };
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
43
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 if not ok then
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 module:log("error", "Failed to deliver to %s: %s", tostring(address), tostring(err));
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
46 return;
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return true;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 if queue_offline_emails then
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local queues = {};
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 local real_send_message_as_email = send_message_as_email;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 function send_message_as_email(address, from_address, message_text)
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local pair_key = address.."\0"..from_address;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local queue = queues[pair_key];
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 if not queue then
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
58 queue = { from = smtp_address, to = address, messages = {} };
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 queues[pair_key] = queue;
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
60
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
61 module:add_timer(queue_offline_emails+5, function ()
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 module:log("info", "Checking on %s", from_address);
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 local current_time = os_time();
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local diff = current_time - queue.last_message_time;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 if diff > queue_offline_emails then
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 module:log("info", "Enough silence, sending...");
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 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)
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 else
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 module:log("info", "Next check in %d", queue_offline_emails - diff + 5);
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 return queue_offline_emails - diff + 5;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end);
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
74
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 queue.last_message_time = os_time();
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 local messages = queue.messages;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 messages[#messages+1] = message_text;
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
79 return true;
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end