Software / code / prosody-modules
Annotate
mod_reminders/mod_reminders.lua @ 6301:fa45ae704b79
mod_cloud_notify: Update Readme
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,109 +1,106 @@
----
-labels:
-- 'Stage-Beta'
-summary: 'XEP-0357: Cloud push notifications'
----
+# Introduction
-Introduction
-============
+This module enables support for sending "push notifications" to clients
+that need it, typically those running on certain mobile devices.
-This module enables support for sending "push notifications" to clients that
-need it, typically those running on certain mobile devices.
+As well as this module, your client must support push notifications (the
+apps that need it generally do, of course) and the app developer's push
+gateway must be reachable from your Prosody server (this happens over a
+normal XMPP server-to-server 's2s' connection).
-As well as this module, your client must support push notifications (the apps
-that need it generally do, of course) and the app developer's push gateway
-must be reachable from your Prosody server (this happens over a normal XMPP
-server-to-server 's2s' connection).
-
-Details
-=======
+# Details
Some platforms, notably Apple's iOS and many versions of Android, impose
-limits that prevent applications from running or accessing the network in the
-background. This makes it difficult or impossible for an XMPP application to
-remain reliably connected to a server to receive messages.
-
-In order for messaging and other apps to receive notifications, the OS vendors
-run proprietary servers that their OS maintains a permanent connection to in
-the background. Then they provide APIs to application developers that allow
-sending notifications to specific devices via those servers.
+limits that prevent applications from running or accessing the network
+in the background. This makes it difficult or impossible for an XMPP
+application to remain reliably connected to a server to receive
+messages.
-When you connect to your server with an app that requires push notifications,
-it will use this module to set up a "push registration". When you receive
-a message but your device is not connected to the server, this module will
-generate a notification and send it to the push gateway operated by your
-application's developers). Their gateway will then connect to your device's
-OS vendor and ask them to forward the notification to your device. When your
-device receives the notification, it will display it or wake up the app so it
-can connect to XMPP and receive any pending messages.
+In order for messaging and other apps to receive notifications, the OS
+vendors run proprietary servers that their OS maintains a permanent
+connection to in the background. Then they provide APIs to application
+developers that allow sending notifications to specific devices via
+those servers.
-This protocol is described for developers in [XEP-0357: Push Notifications].
+When you connect to your server with an app that requires push
+notifications, it will use this module to set up a "push registration".
+When you receive a message but your device is not connected to the
+server, this module will generate a notification and send it to the push
+gateway operated by your application's developers). Their gateway will
+then connect to your device's OS vendor and ask them to forward the
+notification to your device. When your device receives the notification,
+it will display it or wake up the app so it can connect to XMPP and
+receive any pending messages.
-For this module to work reliably, you must have [mod_smacks], [mod_mam] and
-[mod_carbons] also enabled on your server.
+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.
-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].
+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\].
-Configuration
-=============
+# Configuration
- Option Default Description
- ------------------------------------ ----------------- -------------------------------------------------------------------------------------------------------------------
- `push_notification_important_body` `New Message!` The body text to use when the stanza is important (see above), no message body is sent if this is empty
- `push_max_errors` `16` How much persistent push errors are tolerated before notifications for the identifier in question are disabled
- `push_max_devices` `5` The number of allowed devices per user (the oldest devices are automatically removed if this threshold is reached)
- `push_max_hibernation_timeout` `259200` (72h) Number of seconds to extend the smacks timeout if no push was triggered yet (default: 72 hours)
- `push_notification_with_body` (\*) `false` Whether or not to send the real message body to remote pubsub node. Without end-to-end encryption, enabling this may expose your message contents to your client developers and OS vendor. Not recommended.
- `push_notification_with_sender` (\*) `false` Whether or not to send the real message sender to remote pubsub node. Enabling this may expose your contacts to your client developers and OS vendor. Not recommended.
+ Option Default Description
+ -------------------------------------- ---------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ `push_notification_important_body` `New Message!` The body text to use when the stanza is important (see above), no message body is sent if this is empty
+ `push_max_errors` `16` How much persistent push errors are tolerated before notifications for the identifier in question are disabled
+ `push_max_devices` `5` The number of allowed devices per user (the oldest devices are automatically removed if this threshold is reached)
+ `push_max_hibernation_timeout` `259200` (72h) Number of seconds to extend the smacks timeout if no push was triggered yet (default: 72 hours)
+ `push_notification_with_body` (\*) `false` Whether or not to send the real message body to remote pubsub node. Without end-to-end encryption, enabling this may expose your message contents to your client developers and OS vendor. Not recommended.
+ `push_notification_with_sender` (\*) `false` Whether or not to send the real message sender to remote pubsub node. Enabling this may expose your contacts to your client developers and OS vendor. Not recommended.
-(\*) There are privacy implications for enabling these options.
+(\*) There are privacy implications for enabling these options.[^1]
-Internal design notes
-=====================
+# 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 outlined [here](//mail.jabber.org/pipermail/standards/2016-February/030925.html) are all honored[^2].
+App servers are notified about offline messages, messages stored by
+\[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:
-`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 delayed by a certain amount of seconds
-configurable with the [mod_smacks] setting `smacks_max_ack_delay`.
+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
+delayed by a certain amount of seconds configurable with the
+\[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 read timeout or
-connection close didn't already happen) but also aren't responding to acknowledgement
-request in a timely manner. This setting thus allows conversations to be smoother
-under such circumstances.
+The `smacks_max_ack_delay` setting allows to send out notifications to
+clients which aren't already in smacks hibernation state (because the
+read timeout or connection close didn't already happen) but also aren't
+responding to acknowledgement request in a timely manner. This setting
+thus allows conversations to be smoother under such circumstances.
-The new event `cloud-notify-ping` can be used by any module to send out a cloud
-notification to either all registered endpoints for the given user or only the endpoints
-given in the event data.
+The new event `cloud-notify-ping` can be used by any module to send out
+a cloud notification to either all registered endpoints for the given
+user or only the endpoints given in the event data.
-The config setting `push_notification_important_body` can be used to specify an alternative
-body text to send to the remote pubsub node if the stanza is encrypted or has a body.
-This way the real contents of the message aren't revealed to the push appserver but it
-can still see that the push is important.
-This is used by Chatsecure on iOS to send out high priority pushes in those cases for example.
+The config setting `push_notification_important_body` can be used to
+specify an alternative body text to send to the remote pubsub node if
+the stanza is encrypted or has a body. This way the real contents of the
+message aren't revealed to the push appserver but it can still see that
+the push is important. This is used by Chatsecure on iOS to send out
+high priority pushes in those cases for example.
-Compatibility
-=============
-
-**Note:** This module should be used with Lua 5.2 and higher. Using it with
-Lua 5.1 may cause push notifications to not be sent to some clients.
+# Compatibility
------- -----------------------------------------------------------------------------
- trunk Works
- 0.12 Works
- 0.11 Works
- 0.10 Works
- 0.9 Support dropped, use last supported version [675726ab06d3](//hg.prosody.im/prosody-modules/raw-file/675726ab06d3/mod_cloud_notify/mod_cloud_notify.lua)
------- -----------------------------------------------------------------------------
+**Note:** This module should be used with Lua 5.2 and higher. Using it
+with Lua 5.1 may cause push notifications to not be sent to some
+clients.
+ ------- -----------------------------------------------------------------
+ trunk Works as of 25-06-13
+ 13 Works
+ 0.12 Works
+ ------- -----------------------------------------------------------------
-[^1]: The service which is expected to forward notifications to something like Google Cloud Messaging or Apple Notification Service
-[^2]: [business_rules.markdown](//hg.prosody.im/prosody-modules/file/tip/mod_cloud_notify/business_rules.markdown)
+[^1]: The service which is expected to forward notifications to
+ something like Google Cloud Messaging or Apple Notification Service
+
+[^2]: [business_rules.md](//hg.prosody.im/prosody-modules/file/tip/mod_cloud_notify/business_rules.md)
| author | Menel <menel@snikket.de> |
|---|---|
| date | Fri, 13 Jun 2025 10:36:52 +0200 |
| parent | 3921:9eabd68b8e48 |
| rev | line source |
|---|---|
|
3917
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
1 -- mod_reminders |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
2 -- |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
3 -- Copyright (C) 2020 Marcos de Vera Piquero <marcos@tenak.net> |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
4 -- |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
5 -- This file is MIT/X11 licensed. |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
6 -- |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
7 -- A module to support ProtoXEP: Reminders |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
8 -- |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
9 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
10 local id = require "util.id" |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
11 local datetime = require"util.datetime"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
12 local errors = require"util.error"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
13 local jid = require"util.jid"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
14 local st = require"util.stanza"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
15 local os_time = os.time; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
16 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
17 local xmlns_reminders = "urn:xmpp:reminders:0"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
18 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
19 local reminders_store = module:open_store(xmlns_reminders, "keyval"); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
20 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
21 local reminders_errors = { |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
22 missing_fields = { |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
23 type = "modify"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
24 condition = "bad-request"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
25 text = "Missing required value for date or text"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
26 }; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
27 invalid_dateformat = { |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
28 type = "modify"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
29 condition = "bad-request"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
30 text = "Invalid date format"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
31 }; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
32 past_date = { |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
33 type = "modify"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
34 condition = "gone"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
35 text = "Reminder date is in the past"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
36 }; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
37 store_error = { |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
38 type = "cancel"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
39 condition = "internal-server-error"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
40 text = "Unable to persist data"; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
41 }; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
42 }; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
43 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
44 local function reminder_error (name) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
45 return errors.new(name, nil, reminders_errors); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
46 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
47 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
48 local function store_reminder (reminder) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
49 -- pushes the reminder to the store, and nothing else |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
50 return reminders_store:set(reminder.id, reminder); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
51 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
52 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
53 local function delete_reminder (reminder_id) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
54 -- empties the store for the given reminder_id |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
55 return reminders_store:set(reminder_id, nil); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
56 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
57 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
58 local function get_reminder (reminder_id) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
59 return reminders_store:get(reminder_id); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
60 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
61 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
62 local function send_reminder (reminder) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
63 -- actually delivers the <message /> with the reminder to the user |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
64 local bare = jid.bare(reminder.jid); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
65 module:log("debug", "Sending reminder %s to %s", reminder.id, bare); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
66 local message = st.message({ from = "localhost"; to = bare; id = id.short() }) |
|
3921
9eabd68b8e48
mod_reminders: add id to sent reminder
marc0s <marcos@tenak.net>
parents:
3917
diff
changeset
|
67 :tag("reminder", {id = reminder.id; xmlns = xmlns_reminders}) |
|
3917
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
68 :add_child(reminder.text) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
69 :tag("date"):text(datetime.datetime(reminder.date)):up(); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
70 module:send(message); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
71 return delete_reminder(reminder.id) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
72 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
73 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
74 local function schedule_reminder (reminder) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
75 -- schedule a module:add_timer for the given reminder |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
76 module:log("debug", "Scheduling reminder to datetime %s", reminder.date); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
77 local now = os_time(); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
78 local when = reminder.date; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
79 local delay = when - now; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
80 module:log("debug", "Reminder text: %s", reminder.text); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
81 local function callback () |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
82 send_reminder(reminder) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
83 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
84 module:add_timer(delay, callback); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
85 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
86 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
87 local function process_reminders_store () |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
88 -- retrieve all reminders in the store and schedule them |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
89 for reminder_id in reminders_store:users() do |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
90 module:log("debug", "Found stored reminder %s", reminder_id); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
91 local reminder = get_reminder(reminder_id); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
92 if reminder.date and reminder.text then |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
93 local text = st.deserialize(reminder.text) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
94 module:log("debug", "Read reminder %s", reminder.id); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
95 -- cleanup missed reminders |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
96 if reminder.date < os_time() then |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
97 module:log("debug", "Deleting outdated reminder %s", reminder.id) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
98 delete_reminder(reminder.id) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
99 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
100 schedule_reminder({ |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
101 date = reminder.date; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
102 id = reminder.id; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
103 jid = reminder.jid; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
104 text = text; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
105 }) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
106 else |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
107 delete_reminder(reminder_id); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
108 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
109 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
110 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
111 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
112 local function create_reminder (jid, reminder) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
113 local date = reminder:get_child("date"); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
114 local text = reminder:get_child("text"); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
115 if date == nil or text == nil then |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
116 return nil, reminder_error("missing_fields") |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
117 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
118 local now = os_time(); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
119 local _, parsed_date = pcall(datetime.parse, date:get_text()); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
120 if parsed_date == nil then |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
121 return nil, reminder_error("invalid_dateformat") |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
122 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
123 if parsed_date < now then |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
124 return nil, reminder_error("past_date"), nil |
|
3921
9eabd68b8e48
mod_reminders: add id to sent reminder
marc0s <marcos@tenak.net>
parents:
3917
diff
changeset
|
125 end |
|
9eabd68b8e48
mod_reminders: add id to sent reminder
marc0s <marcos@tenak.net>
parents:
3917
diff
changeset
|
126 local reminder_id = id.medium(); |
|
9eabd68b8e48
mod_reminders: add id to sent reminder
marc0s <marcos@tenak.net>
parents:
3917
diff
changeset
|
127 local rem = st.stanza("reminder", {xmlns = xmlns_reminders; id = reminder_id}); |
|
3917
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
128 local data = { |
|
3921
9eabd68b8e48
mod_reminders: add id to sent reminder
marc0s <marcos@tenak.net>
parents:
3917
diff
changeset
|
129 id = reminder_id; |
|
3917
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
130 jid = jid; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
131 text = text; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
132 date = parsed_date; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
133 } |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
134 local stored = store_reminder(data); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
135 if not stored then |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
136 return nil, reminder_error("store_error") |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
137 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
138 schedule_reminder(data); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
139 return rem |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
140 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
141 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
142 local function handle_set (event) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
143 local origin, stanza = event.origin, event.stanza |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
144 local reminder = stanza:get_child("reminder", xmlns_reminders); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
145 if reminder.attr.id ~= nil and reminder:get_child("date") == nil then |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
146 -- delete existing reminder |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
147 local ok = delete_reminder(reminder.attr.id); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
148 if ok then |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
149 module:log("debug", "reminder %s deleted", reminder.attr.id); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
150 origin.send(st.reply(stanza):add_child(reminder)); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
151 else |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
152 module:log("debug", "failed to delete reminder %s", reminder.attr.id); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
153 origin.send(st.error_reply(stanza, "cancel", "internal-server-error")); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
154 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
155 return true; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
156 else |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
157 -- create new reminder |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
158 local jid = stanza.attr.from |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
159 local created, err = create_reminder(jid, reminder); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
160 if err ~= nil then |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
161 origin.send(st.error_reply(stanza, err)) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
162 return true; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
163 else |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
164 origin.send(st.reply(stanza):add_child(created)) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
165 return true; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
166 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
167 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
168 origin.send(st.error_reply(stanza, "modify", "bad-request")); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
169 return true; |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
170 end |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
171 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
172 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
173 -- load saved reminders and set timers |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
174 process_reminders_store(); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
175 |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
176 module:hook("iq-set/host/"..xmlns_reminders..":reminder", handle_set) |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
177 module:add_feature(xmlns_reminders); |
|
3e19c25ff93e
mod_reminders: Initial commit for supporting Reminders ProtoXEP
marc0s <marcos.devera@quobis.com>
parents:
diff
changeset
|
178 module:log("debug", "Module loaded"); |