Mercurial > prosody-modules
annotate mod_offline_email/mod_offline_email.lua @ 5401:c8d04ac200fc
mod_http_oauth2: Reject loopback URIs as client_uri
This really should be a proper website with info, https://localhost is
not good enough. Ideally we'd validate that it's got proper DNS and is
actually reachable, but triggering HTTP or even DNS lookups seems like
it would carry abuse potential that would best to avoid.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 02 May 2023 16:20:55 +0200 |
parents | ee724c7fa8e0 |
children |
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 = { |
4231
ee724c7fa8e0
mod_offline_email: explicitly set charset to utf-8 to override mailclients default settings
Vladimir D. Seleznev <vseleznv@altlinux.org>
parents:
3405
diff
changeset
|
35 ["content-type"] = 'text/plain; charset=utf-8'; |
1285
f1a0a0754b87
mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents:
0
diff
changeset
|
36 to = address; |
f1a0a0754b87
mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents:
0
diff
changeset
|
37 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
|
38 }; |
1285
f1a0a0754b87
mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents:
0
diff
changeset
|
39 body = message_text; |
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 |
f1a0a0754b87
mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents:
0
diff
changeset
|
42 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
|
43 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
|
44 |
0
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 if not ok then |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 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
|
47 return; |
0
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 end |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 return true; |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 end |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 if queue_offline_emails then |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 local queues = {}; |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 local real_send_message_as_email = send_message_as_email; |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 function send_message_as_email(address, from_address, message_text) |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 local pair_key = address.."\0"..from_address; |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 local queue = queues[pair_key]; |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 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
|
59 queue = { from = smtp_address, to = address, messages = {} }; |
0
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 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
|
61 |
f1a0a0754b87
mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents:
0
diff
changeset
|
62 module:add_timer(queue_offline_emails+5, function () |
0
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 module:log("info", "Checking on %s", from_address); |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 local current_time = os_time(); |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 local diff = current_time - queue.last_message_time; |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 if diff > queue_offline_emails then |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 module:log("info", "Enough silence, sending..."); |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 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
|
69 else |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 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
|
71 return queue_offline_emails - diff + 5; |
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); |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 end |
1285
f1a0a0754b87
mod_offline_email: Much cleanup. Very update to newer APIs. Wow.
Kim Alvefur <zash@zash.se>
parents:
0
diff
changeset
|
75 |
0
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 queue.last_message_time = os_time(); |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 local messages = queue.messages; |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 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
|
80 return true; |
0
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 end |
010452cfaf53
mod_offline_email: Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 end |