annotate mod_reminders/mod_reminders.lua @ 5466:398d936e77fb

mod_http_oauth2: Add support for the OpenID 'login_hint' parameter This allows the client to suggest to the authorization screen which user is trying to login, so they don't have to fill that in twice if they already did so at the client.
author Kim Alvefur <zash@zash.se>
date Wed, 17 May 2023 18:49:22 +0200
parents 9eabd68b8e48
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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");