Annotate

mod_log_messages_sql/mod_log_messages_sql.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 2408:9d132153d786
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- Based on mod_mam_sql
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- Copyright (C) 2011-2012 Kim Alvefur
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 --
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 -- This file is MIT/X11 licensed.
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local st = require "util.stanza";
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local jid_bare = require "util.jid".bare;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local jid_split = require "util.jid".split;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local serialize = require"util.json".encode, require"util.json".decode;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local tostring = tostring;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local time_now = os.time;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
2407
be08b65f2855 mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents: 1639
diff changeset
14
be08b65f2855 mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents: 1639
diff changeset
15 local table_name = module:get_option("message_log_sql_table", pcall(require, "util.cache") and "messages" or "prosodyarchive");
be08b65f2855 mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents: 1639
diff changeset
16
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local sql, setsql, getsql = {};
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 do -- SQL stuff
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local connection;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local resolve_relative_path = require "core.configmanager".resolve_relative_path;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local params = module:get_option("message_log_sql", module:get_option("sql"));
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
2407
be08b65f2855 mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents: 1639
diff changeset
23
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local function test_connection()
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 if not connection then return nil; end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 if connection:ping() then
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 return true;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 else
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 module:log("debug", "Database connection closed");
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 connection = nil;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 local function connect()
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 if not test_connection() then
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 prosody.unlock_globals();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local dbh, err = DBI.Connect(
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 params.driver, params.database,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 params.username, params.password,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 params.host, params.port
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 );
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 prosody.lock_globals();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 if not dbh then
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 module:log("debug", "Database connection failed: %s", tostring(err));
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 return nil, err;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 module:log("debug", "Successfully connected to database");
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 dbh:autocommit(false); -- don't commit automatically
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 connection = dbh;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 return connection;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53
2407
be08b65f2855 mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents: 1639
diff changeset
54
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 do -- process options to get a db connection
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 local ok;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 prosody.unlock_globals();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 ok, DBI = pcall(require, "DBI");
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 if not ok then
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 package.loaded["DBI"] = {};
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 module:log("error", "Failed to load the LuaDBI library for accessing SQL databases: %s", DBI);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 module:log("error", "More information on installing LuaDBI can be found at http://prosody.im/doc/depends#luadbi");
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 prosody.lock_globals();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 if not ok or not DBI.Connect then
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 return; -- Halt loading of this module
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 params = params or { driver = "SQLite3" };
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 953
diff changeset
70
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 if params.driver == "SQLite3" then
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite");
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 953
diff changeset
74
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 assert(params.driver and params.database, "Both the SQL driver and the database need to be specified");
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 assert(connect());
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 953
diff changeset
78
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 function getsql(sql, ...)
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 if params.driver == "PostgreSQL" then
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 sql = sql:gsub("`", "\"");
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 -- do prepared statement stuff
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 local stmt, err = connection:prepare(sql);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 if not stmt and not test_connection() then error("connection failed"); end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 -- run query
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 local ok, err = stmt:execute(...);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 if not ok and not test_connection() then error("connection failed"); end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 if not ok then return nil, err; end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 953
diff changeset
93
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 return stmt;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 function setsql(sql, ...)
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 local stmt, err = getsql(sql, ...);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 if not stmt then return stmt, err; end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 return stmt:affected();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 function sql.rollback(...)
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 if connection then connection:rollback(); end -- FIXME check for rollback error?
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 return ...;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 function sql.commit(...)
1639
398c4aaccf6d mod_log_messages_sql: Better logging on error
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
106 local ok, err = connection:commit();
398c4aaccf6d mod_log_messages_sql: Better logging on error
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
107 if not ok then
398c4aaccf6d mod_log_messages_sql: Better logging on error
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
108 module:log("error", "SQL commit failed: %s", tostring(err));
398c4aaccf6d mod_log_messages_sql: Better logging on error
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
109 return nil, "SQL commit failed: "..tostring(err);
398c4aaccf6d mod_log_messages_sql: Better logging on error
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
110 end
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 return ...;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 -- Handle messages
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 local function message_handler(event, c2s)
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 local origin, stanza = event.origin, event.stanza;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 local orig_type = stanza.attr.type or "normal";
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 local orig_to = stanza.attr.to;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 local orig_from = stanza.attr.from;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 if not orig_from and c2s then
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 orig_from = origin.full_jid;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 orig_to = orig_to or orig_from; -- Weird corner cases
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 -- Don't store messages of these types
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 if orig_type == "error"
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 or orig_type == "headline"
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 or orig_type == "groupchat"
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 or not stanza:get_child("body") then
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 return;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 -- TODO Maybe headlines should be configurable?
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 local store_user, store_host = jid_split(c2s and orig_from or orig_to);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 local target_jid = c2s and orig_to or orig_from;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 local target_bare = jid_bare(target_jid);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 local _, _, target_resource = jid_split(target_jid);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 --local id = uuid();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 local when = time_now();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 -- And stash it
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 local ok, err = setsql([[
2407
be08b65f2855 mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents: 1639
diff changeset
146 INSERT INTO `]]..table_name..[[`
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 (`host`, `user`, `store`, `when`, `with`, `resource`, `stanza`)
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 VALUES (?, ?, ?, ?, ?, ?, ?);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 ]], store_host, store_user, "message_log", when, target_bare, target_resource, serialize(st.preserialize(stanza)))
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 if ok then
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 sql.commit();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 else
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 module:log("error", "SQL error: %s", err);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 sql.rollback();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 local function c2s_message_handler(event)
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 return message_handler(event, true);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 -- Stanzas sent by local clients
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 module:hook("pre-message/bare", c2s_message_handler, 2);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 module:hook("pre-message/full", c2s_message_handler, 2);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 -- Stanszas to local clients
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 module:hook("message/bare", message_handler, 2);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 module:hook("message/full", message_handler, 2);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 -- In the telnet console, run:
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 -- >hosts["this host"].modules.mam_sql.environment.create_sql()
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 function create_sql()
2408
9d132153d786 mod_log_messages: Add assert() to detect errors creating SQL table
Matthew Wild <mwild1@gmail.com>
parents: 2407
diff changeset
172 local stm = assert(getsql([[
2407
be08b65f2855 mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents: 1639
diff changeset
173 CREATE TABLE `]]..table_name..[[` (
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 `host` TEXT,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 `user` TEXT,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 `store` TEXT,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 `id` INTEGER PRIMARY KEY AUTOINCREMENT,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 `when` INTEGER,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 `with` TEXT,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 `resource` TEXT,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 `stanza` TEXT
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 );
2407
be08b65f2855 mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents: 1639
diff changeset
183 CREATE INDEX `hus` ON `]]..table_name..[[` (`host`, `user`, `store`);
be08b65f2855 mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents: 1639
diff changeset
184 CREATE INDEX `with` ON `]]..table_name..[[` (`with`);
be08b65f2855 mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents: 1639
diff changeset
185 CREATE INDEX `thetime` ON `]]..table_name..[[` (`when`);
2408
9d132153d786 mod_log_messages: Add assert() to detect errors creating SQL table
Matthew Wild <mwild1@gmail.com>
parents: 2407
diff changeset
186 ]]));
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 stm:execute();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 sql.commit();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 end