annotate mod_offline_email/mod_offline_email.lua @ 2856:08f6b9d37a49

mod_omemo_all_access: initial commit. disable access control for all omemo related PEP nodes
author Daniel Gultsch <daniel@gultsch.de>
date Fri, 29 Dec 2017 16:28:47 +0100
parents f1a0a0754b87
children bd71c97de1d0
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;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local smtp = require "socket.smtp";
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
7 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
8 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
9 local smtp_pass = module:get_option_string("smtp_password");
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 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
12
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local queue_offline_emails = module:get_option("queue_offline_emails");
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 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
15
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local send_message_as_email;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
18 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
19 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
20 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
21 if text then
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
22 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
23 end
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
24 end, 1);
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 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
27 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
28 local rcpt = "<"..address..">";
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
29
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
30 local mesgt = {
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
31 headers = {
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
32 to = address;
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
33 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
34 };
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
35 body = message_text;
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
36 };
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
37
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
38 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
39 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
40
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 if not ok then
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 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
43 return;
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 return true;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 if queue_offline_emails then
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local queues = {};
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local real_send_message_as_email = send_message_as_email;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 function send_message_as_email(address, from_address, message_text)
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local pair_key = address.."\0"..from_address;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 local queue = queues[pair_key];
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 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
55 queue = { from = smtp_address, to = address, messages = {} };
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 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
57
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
58 module:add_timer(queue_offline_emails+5, function ()
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 module:log("info", "Checking on %s", from_address);
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 local current_time = os_time();
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local diff = current_time - queue.last_message_time;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 if diff > queue_offline_emails then
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 module:log("info", "Enough silence, sending...");
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 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
65 else
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 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
67 return queue_offline_emails - diff + 5;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end);
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
1285
f1a0a0754b87 mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents: 0
diff changeset
71
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 queue.last_message_time = os_time();
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 local messages = queue.messages;
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 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
76 return true;
0
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
010452cfaf53 mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end