annotate mod_mam_sql/mod_mam_sql.lua @ 1009:fcba646eb20a

mod_s2s_log_certs: Log certificate status and fingerprints
author Kim Alvefur <zash@zash.se>
date Thu, 09 May 2013 13:37:55 +0200
parents 1e0d273bcb75
children 04bf76c3e4c6
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
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 -- do prepared statement stuff
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 local stmt, err = connection:prepare(sql);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 if not stmt and not test_connection() then error("connection failed"); end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 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
121 -- run query
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 local ok, err = stmt:execute(...);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 if not ok and not test_connection() then error("connection failed"); end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 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
125
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 return stmt;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 function setsql(sql, ...)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 local stmt, err = getsql(sql, ...);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 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
131 return stmt:affected();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 function sql.rollback(...)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 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
135 return ...;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 function sql.commit(...)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 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
139 return ...;
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
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 -- 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
145 local default_attrs = {
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 always = true, [true] = "always",
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 never = false, [false] = "never",
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 roster = "roster",
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 }
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 do
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 local prefs_format = {
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 [false] = "roster",
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 -- default ::= true | false | "roster"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 -- 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
156 ["romeo@montague.net"] = true, -- always
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 ["montague@montague.net"] = false, -- newer
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 };
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 local archive_store = "archive2";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 local prefs_store = archive_store .. "_prefs";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 local function get_prefs(user)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 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
165 { [false] = global_default_policy };
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 local function set_prefs(user, prefs)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 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
169 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170
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 -- Handle prefs.
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 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
174 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
175 local user = origin.username;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 if stanza.attr.type == "get" then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 local prefs = get_prefs(user);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 local default = prefs[false];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 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
180 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
181 local always = st.stanza("always");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 local never = st.stanza("never");
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 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
184 if k then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 (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
186 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 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
189 origin.send(reply);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 else -- type == "set"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 local prefs = {};
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 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
194 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
195 if new_default then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 prefs[false] = default_attrs[new_default];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 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
200 if always then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 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
202 local jid = rule:get_text();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 prefs[jid] = true;
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 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 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
208 if never then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 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
210 local jid = rule:get_text();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 prefs[jid] = false;
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 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 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
216 if not ok then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 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
218 else
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 origin.send(st.reply(stanza));
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 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 end);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 -- Handle archive queries
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 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
227 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
228 local query = stanza.tags[1];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 if stanza.attr.type == "get" then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 local qid = query.attr.queryid;
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 -- Search query parameters
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 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
234 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
235 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
236 local qset = rsm.get(query);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 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
238 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
239
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 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
241 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
242 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
243 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
244 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 qstart, qend = vstart, vend;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 local qres;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250 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
251 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
252 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
253 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
254 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 local _, _, resource = jid_split(qwith);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 qwith = jid_bare(pwith);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258 qres = resource;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
259 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
261 -- RSM stuff
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 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
263 local last;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 local sql_query = ([[
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266 SELECT `id`, `when`, `stanza`
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 FROM `prosodyarchive`
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 WHERE `host` = ? AND `user` = ? AND `store` = ?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269 AND `when` BETWEEN ? AND ?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270 %s %s
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 AND `id` > ?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272 LIMIT ?;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273 ]]):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
274
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275 local p = {
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276 host, origin.username, archive_store,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277 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
278 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
279 qmax
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 };
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 if qwith then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 if qres then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 t_insert(p, 6, qres);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 t_insert(p, 6, qwith);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
286 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287 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
288 if not data then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 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
290 return true
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
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 for item in data:rows() do
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 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
295 --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
296
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 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
298 :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
299 :tag("forwarded", { xmlns = xmlns_forward })
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300 :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
301 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
302 orig_stanza.attr.xmlns = "jabber:client";
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303 fwd_st:add_child(orig_stanza);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
304 origin.send(fwd_st);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
305 last = id;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
306 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
307 -- That's all folks!
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
308 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
309
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
310 local reply = st.reply(stanza);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311 if last then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312 -- 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
313 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
314 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
315 origin.send(reply);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
316 return true
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
317 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
318 end);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
319
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320 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
321 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
322 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
323 return roster[who];
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
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326 local function shall_store(user, who)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327 -- TODO Cache this?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
328 local prefs = get_prefs(user);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
329 local rule = prefs[who];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
330 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
331 if rule ~= nil then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
332 return rule;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
333 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
334 local default = prefs[false];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 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
336 if default == nil then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337 default = global_default_policy;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
338 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
339 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
340 if default == "roster" then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
341 return has_in_roster(user, who);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343 return default;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
345 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
346
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
347 -- Handle messages
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348 local function message_handler(event, c2s)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
349 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
350 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
351 local orig_to = stanza.attr.to;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
352 local orig_from = stanza.attr.from;
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 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
355 orig_from = origin.full_jid;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
356 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
357 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
358
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
359 -- 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
360 if orig_type == "error"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 or orig_type == "headline"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
362 or orig_type == "groupchat"
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
363 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
364 return;
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
365 -- TODO Maybe headlines should be configurable?
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
366 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
367
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
368 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
369 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
370 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
371 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
372
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
373 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
374 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
375
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
376 --local id = uuid();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
377 local when = time_now();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
378 -- And stash it
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
379 local ok, err = setsql([[
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
380 INSERT INTO `prosodyarchive`
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
381 (`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
382 VALUES (?, ?, ?, ?, ?, ?, ?);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
383 ]], 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
384 if ok then
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
385 sql.commit();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
386 else
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
387 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
388 sql.rollback();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
389 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
390 --[[ This was dropped from the spec
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 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
393 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
394 --]]
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
395 else
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
396 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
397 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
398 end
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
399
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
400 local function c2s_message_handler(event)
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
401 return message_handler(event, true);
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
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
404 -- Stanzas sent by local clients
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
405 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
406 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
407 -- Stanszas to local clients
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
408 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
409 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
410
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
411 module:add_feature(xmlns_mam);
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 -- In the telnet console, run:
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
414 -- >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
415 function create_sql()
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
416 local stm = getsql[[
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
417 CREATE TABLE `prosodyarchive` (
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
418 `host` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
419 `user` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
420 `store` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
421 `id` INTEGER PRIMARY KEY AUTOINCREMENT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
422 `when` INTEGER,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
423 `with` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
424 `resource` TEXT,
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
425 `stanza` TEXT
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
426 );
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
427 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
428 CREATE INDEX `with` ON `prosodyarchive` (`with`);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
429 CREATE INDEX `thetime` ON `prosodyarchive` (`when`);
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
430 ]];
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
431 stm:execute();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
432 sql.commit();
1e0d273bcb75 mod_mam_sql: Fork of mod_mam using SQL.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
433 end