annotate mod_offline_email/mod_offline_email.lua @ 6258:06fbbd45ba75

mod_cloud_notify: Readme: fix links and labels that were removed in the last commit diff --git a/mod_cloud_notify/README.md b/mod_cloud_notify/README.md --- a/mod_cloud_notify/README.md +++ b/mod_cloud_notify/README.md @@ -1,3 +1,9 @@ +---- +-labels: +-- 'Stage-Beta' +-summary: 'XEP-0357: Cloud push notifications' +---- + # Introduction This module enables support for sending "push notifications" to clients @@ -32,15 +38,15 @@ notification to your device. When your d it will display it or wake up the app so it can connect to XMPP and receive any pending messages. -This protocol is described for developers in \[XEP-0357: Push -Notifications\]. +This protocol is described for developers in [XEP-0357: Push +Notifications]. -For this module to work reliably, you must have \[mod_smacks\], -\[mod_mam\] and \[mod_carbons\] also enabled on your server. +For this module to work reliably, you must have [mod_smacks], +[mod_mam] and [mod_carbons] also enabled on your server. Some clients, notably Siskin and Snikket iOS need some additional extensions that are not currently defined in a standard XEP. To support -these clients, see \[mod_cloud_notify_extensions\]. +these clients, see [mod_cloud_notify_extensions]. # Configuration @@ -58,18 +64,18 @@ these clients, see \[mod_cloud_notify_ex # Internal design notes App servers are notified about offline messages, messages stored by -\[mod_mam\] or messages waiting in the smacks queue. The business rules +[mod_mam] or messages waiting in the smacks queue. The business rules outlined [here](//mail.jabber.org/pipermail/standards/2016-February/030925.html) are all honored[^2]. -To cooperate with \[mod_smacks\] this module consumes some events: +To cooperate with [mod_smacks] this module consumes some events: `smacks-ack-delayed`, `smacks-hibernation-start` and `smacks-hibernation-end`. These events allow this module to send out notifications for messages received while the session is hibernated by -\[mod_smacks\] or even when smacks acknowledgements for messages are +[mod_smacks] or even when smacks acknowledgements for messages are delayed by a certain amount of seconds configurable with the -\[mod_smacks\] setting `smacks_max_ack_delay`. +[mod_smacks] setting `smacks_max_ack_delay`. The `smacks_max_ack_delay` setting allows to send out notifications to clients which aren't already in smacks hibernation state (because the
author Menel <menel@snikket.de>
date Fri, 13 Jun 2025 10:44:37 +0200
parents ee724c7fa8e0
children
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 = {
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