annotate mod_log_messages_sql/mod_log_messages_sql.lua @ 5536:96dec7681af8

mod_firewall: Update user marks to store instantly via map store The original approach was to keep marks in memory only, and persist them at shutdown. That saves I/O, at the cost of potentially losing marks on an unclean shutdown. This change persists marks instantly, which may have some performance overhead but should be more "correct". It also splits the marking/unmarking into an event which may be watched or even fired by other modules.
author Matthew Wild <mwild1@gmail.com>
date Thu, 08 Jun 2023 16:20:42 +0100
parents 9d132153d786
children
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