annotate mod_mam_sql/mod_mam_sql.lua @ 1623:2c39af0fb93b

mod_measure_memory: Module for polling memory useage from Lua, meminfo() and /proc depending on availability
author Kim Alvefur <zash@zash.se>
date Tue, 10 Mar 2015 16:34:40 +0100
parents 6e1facedcb74
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- XEP-0313: Message Archive Management for Prosody
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- Copyright (C) 2011-2012 Kim Alvefur
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 --
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 -- This file is MIT/X11 licensed.
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local xmlns_mam = "urn:xmpp:mam:tmp";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local xmlns_delay = "urn:xmpp:delay";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local xmlns_forward = "urn:xmpp:forward:0";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local st = require "util.stanza";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local rsm = module:require "mod_mam/rsm";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local jid_bare = require "util.jid".bare;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local jid_split = require "util.jid".split;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local jid_prep = require "util.jid".prep;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local host = module.host;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local dm_load = require "util.datamanager".load;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local dm_store = require "util.datamanager".store;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local rm_load_roster = require "core.rostermanager".load_roster;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local serialize, deserialize = require"util.json".encode, require"util.json".decode;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local unpack = unpack;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local tostring = tostring;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local time_now = os.time;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local t_insert = table.insert;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 local m_min = math.min;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 local global_default_policy = module:get_option("default_archive_policy", false);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 -- TODO Should be possible to enforce it too
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 local sql, setsql, getsql = {};
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 do -- SQL stuff
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local dburi;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 local connection;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local connections = module:shared "/*/sql/connection-cache";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 local build_url = require"socket.url".build;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 local resolve_relative_path = require "core.configmanager".resolve_relative_path;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 local params = module:get_option("mam_sql", module:get_option("sql"));
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 local function db2uri(params)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 return build_url{
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 scheme = params.driver,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 user = params.username,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 password = params.password,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 host = params.host,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 port = params.port,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 path = params.database,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 };
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51
1207
c6b8ae5a8369 mod_mam_sql: Reconnect if the connection should be dropped
Rob Hoelz <rob@hoelz.ro>
parents: 1206
diff changeset
52 local connect
c6b8ae5a8369 mod_mam_sql: Reconnect if the connection should be dropped
Rob Hoelz <rob@hoelz.ro>
parents: 1206
diff changeset
53
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 local function test_connection()
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 if not connection then return nil; end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 if connection:ping() then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 return true;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 else
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 module:log("debug", "Database connection closed");
1207
c6b8ae5a8369 mod_mam_sql: Reconnect if the connection should be dropped
Rob Hoelz <rob@hoelz.ro>
parents: 1206
diff changeset
60 module:log("debug", "Attempting to reconnect");
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 connection = nil;
1207
c6b8ae5a8369 mod_mam_sql: Reconnect if the connection should be dropped
Rob Hoelz <rob@hoelz.ro>
parents: 1206
diff changeset
62 return connect();
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 end
1207
c6b8ae5a8369 mod_mam_sql: Reconnect if the connection should be dropped
Rob Hoelz <rob@hoelz.ro>
parents: 1206
diff changeset
65 function connect()
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 if not test_connection() then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 prosody.unlock_globals();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 local dbh, err = DBI.Connect(
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 params.driver, params.database,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 params.username, params.password,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 params.host, params.port
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 );
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 prosody.lock_globals();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 if not dbh then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 module:log("debug", "Database connection failed: %s", tostring(err));
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 return nil, err;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 module:log("debug", "Successfully connected to database");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 dbh:autocommit(false); -- don't commit automatically
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 connection = dbh;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 connections[dburi] = dbh;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 return connection;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 do -- process options to get a db connection
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 local ok;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 prosody.unlock_globals();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 ok, DBI = pcall(require, "DBI");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 if not ok then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 package.loaded["DBI"] = {};
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 module:log("error", "Failed to load the LuaDBI library for accessing SQL databases: %s", DBI);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 module:log("error", "More information on installing LuaDBI can be found at http://prosody.im/doc/depends#luadbi");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 prosody.lock_globals();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 if not ok or not DBI.Connect then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 return; -- Halt loading of this module
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 params = params or { driver = "SQLite3" };
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1207
diff changeset
102
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 if params.driver == "SQLite3" then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1207
diff changeset
106
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 assert(params.driver and params.database, "Both the SQL driver and the database need to be specified");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 dburi = db2uri(params);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 connection = connections[dburi];
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1207
diff changeset
111
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 assert(connect());
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1207
diff changeset
113
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 function getsql(sql, ...)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 if params.driver == "PostgreSQL" then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 sql = sql:gsub("`", "\"");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 end
1206
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
120 if not connection then
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
121 return nil, 'connection failed';
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
122 end
1207
c6b8ae5a8369 mod_mam_sql: Reconnect if the connection should be dropped
Rob Hoelz <rob@hoelz.ro>
parents: 1206
diff changeset
123 if not test_connection() then
c6b8ae5a8369 mod_mam_sql: Reconnect if the connection should be dropped
Rob Hoelz <rob@hoelz.ro>
parents: 1206
diff changeset
124 return nil, 'connection failed';
c6b8ae5a8369 mod_mam_sql: Reconnect if the connection should be dropped
Rob Hoelz <rob@hoelz.ro>
parents: 1206
diff changeset
125 end
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 -- do prepared statement stuff
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 local stmt, err = connection:prepare(sql);
1206
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
128 if not stmt and not test_connection() then
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
129 return nil, "connection failed";
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
130 end
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 -- run query
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 local ok, err = stmt:execute(...);
1206
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
134 if not ok and not test_connection() then
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
135 return nil, "connection failed";
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
136 end
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 if not ok then return nil, err; end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1207
diff changeset
138
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 return stmt;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 function setsql(sql, ...)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 local stmt, err = getsql(sql, ...);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 if not stmt then return stmt, err; end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 return stmt:affected();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 function sql.rollback(...)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 if connection then connection:rollback(); end -- FIXME check for rollback error?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 return ...;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 function sql.commit(...)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 if not connection:commit() then return nil, "SQL commit failed"; end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 return ...;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 -- For translating preference names from string to boolean and back
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 local default_attrs = {
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 always = true, [true] = "always",
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 never = false, [false] = "never",
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 roster = "roster",
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 }
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 do
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 local prefs_format = {
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 [false] = "roster",
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 -- default ::= true | false | "roster"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 -- true = always, false = never, nil = global default
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 ["romeo@montague.net"] = true, -- always
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 ["montague@montague.net"] = false, -- newer
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 };
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 local archive_store = "archive2";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 local prefs_store = archive_store .. "_prefs";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 local function get_prefs(user)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 return dm_load(user, host, prefs_store) or
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 { [false] = global_default_policy };
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 local function set_prefs(user, prefs)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 return dm_store(user, host, prefs_store, prefs);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 -- Handle prefs.
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 module:hook("iq/self/"..xmlns_mam..":prefs", function(event)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 local origin, stanza = event.origin, event.stanza;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 local user = origin.username;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 if stanza.attr.type == "get" then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 local prefs = get_prefs(user);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 local default = prefs[false];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 default = default ~= nil and default_attrs[default] or global_default_policy;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 local reply = st.reply(stanza):tag("prefs", { xmlns = xmlns_mam, default = default })
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 local always = st.stanza("always");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 local never = st.stanza("never");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 for k,v in pairs(prefs) do
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 if k then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 (v and always or never):tag("jid"):text(k):up();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 reply:add_child(always):add_child(never);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 origin.send(reply);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 else -- type == "set"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 local prefs = {};
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 local new_prefs = stanza:get_child("prefs", xmlns_mam);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 local new_default = new_prefs.attr.default;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 if new_default then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 prefs[false] = default_attrs[new_default];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 local always = new_prefs:get_child("always");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 if always then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 for rule in always:childtags("jid") do
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 local jid = rule:get_text();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 prefs[jid] = true;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 local never = new_prefs:get_child("never");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 if never then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 for rule in never:childtags("jid") do
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 local jid = rule:get_text();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 prefs[jid] = false;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 local ok, err = set_prefs(user, prefs);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 if not ok then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error storing preferences: "..tostring(err)));
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 else
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 origin.send(st.reply(stanza));
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 end);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 -- Handle archive queries
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 module:hook("iq/self/"..xmlns_mam..":query", function(event)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 local origin, stanza = event.origin, event.stanza;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241 local query = stanza.tags[1];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242 if stanza.attr.type == "get" then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243 local qid = query.attr.queryid;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 -- Search query parameters
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 local qwith = query:get_child_text("with");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 local qstart = query:get_child_text("start");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 local qend = query:get_child_text("end");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 local qset = rsm.get(query);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250 module:log("debug", "Archive query, id %s with %s from %s until %s)",
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251 tostring(qid), qwith or "anyone", qstart or "the dawn of time", qend or "now");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
253 if qstart or qend then -- Validate timestamps
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254 local vstart, vend = (qstart and timestamp_parse(qstart)), (qend and timestamp_parse(qend))
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 if (qstart and not vstart) or (qend and not vend) then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid timestamp"))
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
259 qstart, qend = vstart, vend;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
261
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 local qres;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263 if qwith then -- Validate the 'with' jid
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 local pwith = qwith and jid_prep(qwith);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 if pwith and not qwith then -- it failed prepping
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid JID"))
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269 local _, _, resource = jid_split(qwith);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270 qwith = jid_bare(pwith);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 qres = resource;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 -- RSM stuff
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275 local qmax = m_min(qset and qset.max or default_max_items, max_max_items);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276 local last;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 local sql_query = ([[
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279 SELECT `id`, `when`, `stanza`
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 FROM `prosodyarchive`
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 WHERE `host` = ? AND `user` = ? AND `store` = ?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 AND `when` BETWEEN ? AND ?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 %s %s
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 AND `id` > ?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 LIMIT ?;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
286 ]]):format(qwith and [[AND `with` = ?]] or "", qres and [[AND `resource` = ?]] or "")
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
288 local p = {
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 host, origin.username, archive_store,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 qstart or 0, qend or time_now(),
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291 qset and tonumber(qset.after) or 0,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
292 qmax
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 };
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 if qwith then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
295 if qres then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
296 t_insert(p, 6, qres);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298 t_insert(p, 6, qwith);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300 local data, err = getsql(sql_query, unpack(p));
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 if not data then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
302 origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error loading archive: "..tostring(err)));
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
304 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
305
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
306 for item in data:rows() do
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
307 local id, when, orig_stanza = unpack(item);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
308 --module:log("debug", "id is %s", id);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
309
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
310 local fwd_st = st.message{ to = origin.full_jid }
1388
6e1facedcb74 mam_sql: Make forwarded a child element of result, rather than a sibling
Rob Hoelz <rob@hoelz.ro>
parents: 1343
diff changeset
311 :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id })
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312 :tag("forwarded", { xmlns = xmlns_forward })
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
313 :tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
314 orig_stanza = st.deserialize(deserialize(orig_stanza));
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
315 orig_stanza.attr.xmlns = "jabber:client";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
316 fwd_st:add_child(orig_stanza);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
317 origin.send(fwd_st);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
318 last = id;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
319 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320 -- That's all folks!
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321 module:log("debug", "Archive query %s completed", tostring(qid));
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
323 local reply = st.reply(stanza);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 if last then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325 -- This is a bit redundant, isn't it?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326 reply:query(xmlns_mam):add_child(rsm.generate{last = last});
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
328 origin.send(reply);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
329 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
330 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
331 end);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
332
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
333 local function has_in_roster(user, who)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
334 local roster = rm_load_roster(user, host);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 module:log("debug", "%s has %s in roster? %s", user, who, roster[who] and "yes" or "no");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
336 return roster[who];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
338
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
339 local function shall_store(user, who)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
340 -- TODO Cache this?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
341 local prefs = get_prefs(user);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342 local rule = prefs[who];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343 module:log("debug", "%s's rule for %s is %s", user, who, tostring(rule))
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344 if rule ~= nil then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
345 return rule;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
346 else -- Below could be done by a metatable
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
347 local default = prefs[false];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348 module:log("debug", "%s's default rule is %s", user, tostring(default))
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
349 if default == nil then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
350 default = global_default_policy;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
351 module:log("debug", "Using global default rule, %s", tostring(default))
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
352 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
353 if default == "roster" then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354 return has_in_roster(user, who);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
356 return default;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
357 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
358 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
359
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
360 -- Handle messages
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 local function message_handler(event, c2s)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
362 local origin, stanza = event.origin, event.stanza;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
363 local orig_type = stanza.attr.type or "normal";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
364 local orig_to = stanza.attr.to;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
365 local orig_from = stanza.attr.from;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
366
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
367 if not orig_from and c2s then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
368 orig_from = origin.full_jid;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
369 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
370 orig_to = orig_to or orig_from; -- Weird corner cases
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
371
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
372 -- Don't store messages of these types
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
373 if orig_type == "error"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
374 or orig_type == "headline"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
375 or orig_type == "groupchat"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
376 or not stanza:get_child("body") then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
377 return;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
378 -- TODO Maybe headlines should be configurable?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
379 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
380
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
381 local store_user, store_host = jid_split(c2s and orig_from or orig_to);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
382 local target_jid = c2s and orig_to or orig_from;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
383 local target_bare = jid_bare(target_jid);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
384 local _, _, target_resource = jid_split(target_jid);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
385
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
386 if shall_store(store_user, target_bare) then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
387 module:log("debug", "Archiving stanza: %s", stanza:top_tag());
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
388
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
389 --local id = uuid();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
390 local when = time_now();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
391 -- And stash it
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
392 local ok, err = setsql([[
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
393 INSERT INTO `prosodyarchive`
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
394 (`host`, `user`, `store`, `when`, `with`, `resource`, `stanza`)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
395 VALUES (?, ?, ?, ?, ?, ?, ?);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
396 ]], store_host, store_user, archive_store, when, target_bare, target_resource, serialize(st.preserialize(stanza)))
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
397 if ok then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
398 sql.commit();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
399 else
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
400 module:log("error", "SQL error: %s", err);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
401 sql.rollback();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
402 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
403 --[[ This was dropped from the spec
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1207
diff changeset
404 if ok then
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
405 stanza:tag("archived", { xmlns = xmlns_mam, by = host, id = id }):up();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
406 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
407 --]]
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
408 else
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
409 module:log("debug", "Not archiving stanza: %s", stanza:top_tag());
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
410 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
411 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
412
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
413 local function c2s_message_handler(event)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
414 return message_handler(event, true);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
415 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
416
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
417 -- Stanzas sent by local clients
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
418 module:hook("pre-message/bare", c2s_message_handler, 2);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
419 module:hook("pre-message/full", c2s_message_handler, 2);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
420 -- Stanszas to local clients
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
421 module:hook("message/bare", message_handler, 2);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
422 module:hook("message/full", message_handler, 2);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
423
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
424 module:add_feature(xmlns_mam);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
425
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
426 -- In the telnet console, run:
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
427 -- >hosts["this host"].modules.mam_sql.environment.create_sql()
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
428 function create_sql()
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
429 local stm = getsql[[
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
430 CREATE TABLE `prosodyarchive` (
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
431 `host` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
432 `user` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
433 `store` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
434 `id` INTEGER PRIMARY KEY AUTOINCREMENT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
435 `when` INTEGER,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
436 `with` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
437 `resource` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
438 `stanza` TEXT
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
439 );
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
440 CREATE INDEX `hus` ON `prosodyarchive` (`host`, `user`, `store`);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
441 CREATE INDEX `with` ON `prosodyarchive` (`with`);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
442 CREATE INDEX `thetime` ON `prosodyarchive` (`when`);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
443 ]];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
444 stm:execute();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
445 sql.commit();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
446 end