Annotate

mod_mam_sql/mod_mam_sql.lua @ 1206:04bf76c3e4c6

mod_mam_sql: Add better error handling
author Rob Hoelz <rob@hoelz.ro>
date Mon, 02 Sep 2013 19:55:51 +0200
parent 819:1e0d273bcb75
child 1207:c6b8ae5a8369
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
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 local function test_connection()
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 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
54 if connection:ping() then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 return true;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 else
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 module:log("debug", "Database connection closed");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 connection = nil;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 connections[dburi] = nil;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local function connect()
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 if not test_connection() then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 prosody.unlock_globals();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 local dbh, err = DBI.Connect(
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 params.driver, params.database,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 params.username, params.password,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 params.host, params.port
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 );
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 prosody.lock_globals();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 if not dbh then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 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
73 return nil, err;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 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
76 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
77 connection = dbh;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 connections[dburi] = dbh;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 return connection;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 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
85 local ok;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 prosody.unlock_globals();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 ok, DBI = pcall(require, "DBI");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 if not ok then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 package.loaded["DBI"] = {};
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 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
91 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
92 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 prosody.lock_globals();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 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
95 return; -- Halt loading of this module
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 params = params or { driver = "SQLite3" };
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 if params.driver == "SQLite3" then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 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
102 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 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
105
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 dburi = db2uri(params);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 connection = connections[dburi];
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 assert(connect());
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 function getsql(sql, ...)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 if params.driver == "PostgreSQL" then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 sql = sql:gsub("`", "\"");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 end
1206
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
117 if not connection then
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
118 return nil, 'connection failed';
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
119 end
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 -- do prepared statement stuff
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 local stmt, err = connection:prepare(sql);
1206
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
122 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
123 return nil, "connection failed";
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
124 end
819
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 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
126 -- run query
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 local ok, err = stmt:execute(...);
1206
04bf76c3e4c6 mod_mam_sql: Add better error handling
Rob Hoelz <rob@hoelz.ro>
parents: 819
diff changeset
128 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
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 ok then return nil, err; end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 return stmt;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 function setsql(sql, ...)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 local stmt, err = getsql(sql, ...);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 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
138 return stmt:affected();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 function sql.rollback(...)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 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
142 return ...;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 function sql.commit(...)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 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
146 return ...;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148
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
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 -- 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
152 local default_attrs = {
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 always = true, [true] = "always",
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 never = false, [false] = "never",
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 roster = "roster",
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
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 do
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 local prefs_format = {
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 [false] = "roster",
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 -- default ::= true | false | "roster"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 -- 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
163 ["romeo@montague.net"] = true, -- always
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 ["montague@montague.net"] = false, -- newer
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 };
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 local archive_store = "archive2";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 local prefs_store = archive_store .. "_prefs";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 local function get_prefs(user)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 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
172 { [false] = global_default_policy };
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 local function set_prefs(user, prefs)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 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
176 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 -- Handle prefs.
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 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
181 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
182 local user = origin.username;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 if stanza.attr.type == "get" then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 local prefs = get_prefs(user);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 local default = prefs[false];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 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
187 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
188 local always = st.stanza("always");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 local never = st.stanza("never");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 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
191 if k then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 (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
193 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 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
196 origin.send(reply);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 else -- type == "set"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 local prefs = {};
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 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
201 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
202 if new_default then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 prefs[false] = default_attrs[new_default];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 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
207 if always then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 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
209 local jid = rule:get_text();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 prefs[jid] = true;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 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
215 if never then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 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
217 local jid = rule:get_text();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 prefs[jid] = false;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 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
223 if not ok then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 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
225 else
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 origin.send(st.reply(stanza));
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 end);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 -- Handle archive queries
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 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
234 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
235 local query = stanza.tags[1];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 if stanza.attr.type == "get" then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 local qid = query.attr.queryid;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 -- Search query parameters
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 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
241 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
242 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
243 local qset = rsm.get(query);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244 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
245 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
246
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 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
248 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
249 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
250 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
251 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
253 qstart, qend = vstart, vend;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 local qres;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 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
258 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
259 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
260 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
261 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263 local _, _, resource = jid_split(qwith);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 qwith = jid_bare(pwith);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 qres = resource;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 -- RSM stuff
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269 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
270 local last;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272 local sql_query = ([[
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273 SELECT `id`, `when`, `stanza`
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 FROM `prosodyarchive`
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275 WHERE `host` = ? AND `user` = ? AND `store` = ?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276 AND `when` BETWEEN ? AND ?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277 %s %s
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 AND `id` > ?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279 LIMIT ?;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 ]]):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
281
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 local p = {
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 host, origin.username, archive_store,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 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
285 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
286 qmax
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 if qwith then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 if qres then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 t_insert(p, 6, qres);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
292 t_insert(p, 6, qwith);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 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
295 if not data then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
296 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
297 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300 for item in data:rows() do
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 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
302 --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
303
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
304 local fwd_st = st.message{ to = origin.full_jid }
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
305 :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id }):up()
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
306 :tag("forwarded", { xmlns = xmlns_forward })
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
307 :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
308 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
309 orig_stanza.attr.xmlns = "jabber:client";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
310 fwd_st:add_child(orig_stanza);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311 origin.send(fwd_st);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312 last = id;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
313 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
314 -- That's all folks!
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
315 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
316
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
317 local reply = st.reply(stanza);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
318 if last then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
319 -- 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
320 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
321 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322 origin.send(reply);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
323 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325 end);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327 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
328 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
329 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
330 return roster[who];
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 shall_store(user, who)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
334 -- TODO Cache this?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 local prefs = get_prefs(user);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
336 local rule = prefs[who];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337 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
338 if rule ~= nil then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
339 return rule;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
340 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
341 local default = prefs[false];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342 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
343 if default == nil then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344 default = global_default_policy;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
345 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
346 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
347 if default == "roster" then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348 return has_in_roster(user, who);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
349 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
350 return default;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
351 end
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
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354 -- Handle messages
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355 local function message_handler(event, c2s)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
356 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
357 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
358 local orig_to = stanza.attr.to;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
359 local orig_from = stanza.attr.from;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
360
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 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
362 orig_from = origin.full_jid;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
363 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
364 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
365
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
366 -- 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
367 if orig_type == "error"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
368 or orig_type == "headline"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
369 or orig_type == "groupchat"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
370 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
371 return;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
372 -- TODO Maybe headlines should be configurable?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
373 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
374
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
375 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
376 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
377 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
378 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
379
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
380 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
381 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
382
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
383 --local id = uuid();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
384 local when = time_now();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
385 -- And stash it
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
386 local ok, err = setsql([[
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
387 INSERT INTO `prosodyarchive`
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
388 (`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
389 VALUES (?, ?, ?, ?, ?, ?, ?);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
390 ]], 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
391 if ok then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
392 sql.commit();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
393 else
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
394 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
395 sql.rollback();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
396 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
397 --[[ This was dropped from the spec
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
398 if ok then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
399 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
400 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
401 --]]
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
402 else
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
403 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
404 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
405 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
406
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
407 local function c2s_message_handler(event)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
408 return message_handler(event, true);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
409 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
410
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
411 -- Stanzas sent by local clients
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
412 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
413 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
414 -- Stanszas to local clients
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
415 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
416 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
417
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
418 module:add_feature(xmlns_mam);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
419
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
420 -- In the telnet console, run:
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
421 -- >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
422 function create_sql()
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
423 local stm = getsql[[
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
424 CREATE TABLE `prosodyarchive` (
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
425 `host` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
426 `user` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
427 `store` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
428 `id` INTEGER PRIMARY KEY AUTOINCREMENT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
429 `when` INTEGER,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
430 `with` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
431 `resource` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
432 `stanza` TEXT
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
433 );
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
434 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
435 CREATE INDEX `with` ON `prosodyarchive` (`with`);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
436 CREATE INDEX `thetime` ON `prosodyarchive` (`when`);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
437 ]];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
438 stm:execute();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
439 sql.commit();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
440 end