Mercurial > prosody-modules
annotate mod_log_messages_sql/mod_log_messages_sql.lua @ 1324:853a382c9bd6
mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 28 Feb 2014 15:36:06 +0100 |
parents | 2c38d7d8b332 |
children | 7dbde05b48a9 |
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" }; |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 |
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 |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 |
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()); |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 |
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 |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 |
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(...) |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 if not connection:commit() then return nil, "SQL commit failed"; end |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 return ...; |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 end |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 end |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 -- Handle messages |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 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
|
109 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
|
110 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
|
111 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
|
112 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
|
113 |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 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
|
115 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
|
116 end |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 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
|
118 |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 -- 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
|
120 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
|
121 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
|
122 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
|
123 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
|
124 return; |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 -- 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
|
126 end |
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 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
|
129 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
|
130 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
|
131 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
|
132 |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 --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
|
134 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
|
135 -- 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
|
136 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
|
137 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
|
138 (`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
|
139 VALUES (?, ?, ?, ?, ?, ?, ?); |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 ]], 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
|
141 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
|
142 sql.commit(); |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 else |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 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
|
145 sql.rollback(); |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 end |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 end |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 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
|
150 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
|
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 -- 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
|
154 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
|
155 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
|
156 -- 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
|
157 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
|
158 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
|
159 |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 -- 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
|
161 -- >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
|
162 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
|
163 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
|
164 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
|
165 `host` TEXT, |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 `user` TEXT, |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 `store` TEXT, |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 `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
|
169 `when` INTEGER, |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 `with` TEXT, |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 `resource` TEXT, |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 `stanza` TEXT |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 ); |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 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
|
175 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
|
176 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
|
177 ]]; |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 stm:execute(); |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 sql.commit(); |
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 end |