Annotate

mod_log_messages_sql/mod_log_messages_sql.lua @ 2268:56ea6ff796ba

mod_spam_reporting: Add README
author Kim Alvefur <zash@zash.se>
date Tue, 09 Aug 2016 19:52:01 +0200
parent 1639:398c4aaccf6d
child 2407:be08b65f2855
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
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 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
15 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
16 local connection;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 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
18 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
19
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 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
21 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
22 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
23 return true;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 else
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 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
26 connection = nil;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 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
30 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
31 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
32 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
33 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
34 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
35 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
36 );
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 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
38 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
39 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
40 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
41 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 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
43 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
44 connection = dbh;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 return connection;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 end
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 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
51 local ok;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 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
53 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
54 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
55 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
56 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
57 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
58 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 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
60 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
61 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
62 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 params = params or { driver = "SQLite3" };
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 953
diff changeset
65
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 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
67 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
68 end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 953
diff changeset
69
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 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
71
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 assert(connect());
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 953
diff changeset
73
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 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
77 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
78 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
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 -- 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
81 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
82 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
83 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
84 -- run query
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 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
86 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
87 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
88
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 return stmt;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 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
92 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
93 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
94 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
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 sql.rollback(...)
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 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
98 return ...;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 function sql.commit(...)
1639
398c4aaccf6d mod_log_messages_sql: Better logging on error
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
101 local ok, err = connection:commit();
398c4aaccf6d mod_log_messages_sql: Better logging on error
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
102 if not ok then
398c4aaccf6d mod_log_messages_sql: Better logging on error
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
103 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
104 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
105 end
953
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 return ...;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 -- Handle messages
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 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
113 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
114 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
115 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
116 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
117
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 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
119 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
120 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 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
122
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 -- 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
124 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
125 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
126 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
127 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
128 return;
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 -- 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
130 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 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
133 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
134 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
135 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
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 id = uuid();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 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
139 -- 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
140 local ok, err = setsql([[
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 INSERT INTO `prosodyarchive`
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 (`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
143 VALUES (?, ?, ?, ?, ?, ?, ?);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 ]], 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
145 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
146 sql.commit();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 else
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 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
149 sql.rollback();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 end
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 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
154 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
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
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 -- 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
158 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
159 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
160 -- 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
161 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
162 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
163
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 -- 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
165 -- >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
166 function create_sql()
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 local stm = getsql[[
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 CREATE TABLE `prosodyarchive` (
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 `host` TEXT,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 `user` TEXT,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 `store` TEXT,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 `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
173 `when` INTEGER,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 `with` TEXT,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 `resource` TEXT,
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 `stanza` TEXT
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 );
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 CREATE INDEX `hus` ON `prosodyarchive` (`host`, `user`, `store`);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 CREATE INDEX `with` ON `prosodyarchive` (`with`);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 CREATE INDEX `thetime` ON `prosodyarchive` (`when`);
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 ]];
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 stm:execute();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 sql.commit();
2c38d7d8b332 mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 end