annotate mod_mam_muc_sql/mod_mam_muc_sql.lua @ 1510:094e9d5a4d94

mod_block_p2pft: Plugin for forcing local clients to use approved file transfer proxies
author Kim Alvefur <zash@zash.se>
date Mon, 22 Sep 2014 18:17:48 +0200
parents 7dbde05b48a9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
821
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- XEP-0313: Message Archive Management for Prosody
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- Copyright (C) 2011-2012 Kim Alvefur
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 --
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 -- This file is MIT/X11 licensed.
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local xmlns_mam = "urn:xmpp:mam:tmp";
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local xmlns_delay = "urn:xmpp:delay";
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local xmlns_forward = "urn:xmpp:forward:0";
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local st = require "util.stanza";
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local rsm = module:require "mod_mam/rsm";
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local jid_bare = require "util.jid".bare;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local jid_split = require "util.jid".split;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local jid_prep = require "util.jid".prep;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local host = module.host;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local serialize, deserialize = require"util.json".encode, require"util.json".decode;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local unpack = unpack;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local tostring = tostring;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local time_now = os.time;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local t_insert = table.insert;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local m_min = math.min;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 --local rooms_to_archive = module:get_option_set("rooms_to_archive",{});
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local sql, setsql, getsql = {};
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 do -- SQL stuff
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 local dburi;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local connection;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 local connections = module:shared "/*/sql/connection-cache";
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 local build_url = require"socket.url".build;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 local resolve_relative_path = require "core.configmanager".resolve_relative_path;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local params = module:get_option("mam_sql", module:get_option("sql"));
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local function db2uri(params)
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 return build_url{
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 scheme = params.driver,
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 user = params.username,
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 password = params.password,
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 host = params.host,
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 port = params.port,
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 path = params.database,
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 };
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 local function test_connection()
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 if not connection then return nil; end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 if connection:ping() then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 return true;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 else
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 module:log("debug", "Database connection closed");
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 connection = nil;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 connections[dburi] = nil;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 local function connect()
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 if not test_connection() then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 prosody.unlock_globals();
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 local dbh, err = DBI.Connect(
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 params.driver, params.database,
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 params.username, params.password,
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 params.host, params.port
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 );
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 prosody.lock_globals();
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 if not dbh then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 module:log("debug", "Database connection failed: %s", tostring(err));
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 return nil, err;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 module:log("debug", "Successfully connected to database");
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 dbh:autocommit(false); -- don't commit automatically
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 connection = dbh;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 connections[dburi] = dbh;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 return connection;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 do -- process options to get a db connection
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 local ok;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 prosody.unlock_globals();
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 ok, DBI = pcall(require, "DBI");
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 if not ok then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 package.loaded["DBI"] = {};
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 module:log("error", "Failed to load the LuaDBI library for accessing SQL databases: %s", DBI);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 module:log("error", "More information on installing LuaDBI can be found at http://prosody.im/doc/depends#luadbi");
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 prosody.lock_globals();
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 if not ok or not DBI.Connect then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 return; -- Halt loading of this module
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 params = params or { driver = "SQLite3" };
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 821
diff changeset
94
821
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 if params.driver == "SQLite3" then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite");
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 821
diff changeset
98
821
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 assert(params.driver and params.database, "Both the SQL driver and the database need to be specified");
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 dburi = db2uri(params);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 connection = connections[dburi];
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 821
diff changeset
103
821
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 assert(connect());
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 821
diff changeset
105
821
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 function getsql(sql, ...)
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 if params.driver == "PostgreSQL" then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 sql = sql:gsub("`", "\"");
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 -- do prepared statement stuff
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 local stmt, err = connection:prepare(sql);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 if not stmt and not test_connection() then error("connection failed"); end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 -- run query
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 local ok, err = stmt:execute(...);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 if not ok and not test_connection() then error("connection failed"); end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 if not ok then return nil, err; end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 821
diff changeset
120
821
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 return stmt;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 function setsql(sql, ...)
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 local stmt, err = getsql(sql, ...);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 if not stmt then return stmt, err; end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 return stmt:affected();
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 function sql.rollback(...)
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 if connection then connection:rollback(); end -- FIXME check for rollback error?
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 return ...;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 function sql.commit(...)
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 if not connection:commit() then return nil, "SQL commit failed"; end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 return ...;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 local archive_store = "archive2";
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 -- Handle archive queries
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 module:hook("iq/bare/"..xmlns_mam..":query", function(event)
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 local origin, stanza = event.origin, event.stanza;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 local room = jid_split(stanza.attr.to);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 local query = stanza.tags[1];
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 local room_obj = hosts[module.host].modules.muc.rooms[jid_bare(stanza.attr.to)];
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 if not room_obj then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 return -- FIXME not found
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 local from = jid_bare(stanza.attr.from);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 if room_obj._affiliations[from] == "outcast"
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 or room_obj._data.members_only and not room_obj._affiliations[from] then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 return -- FIXME unauth
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 if stanza.attr.type == "get" then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 local qid = query.attr.queryid;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 -- Search query parameters
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 local qwith = query:get_child_text("with");
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 local qstart = query:get_child_text("start");
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 local qend = query:get_child_text("end");
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 local qset = rsm.get(query);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 module:log("debug", "Archive query, id %s with %s from %s until %s)",
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 tostring(qid), qwith or "anyone", qstart or "the dawn of time", qend or "now");
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 if qstart or qend then -- Validate timestamps
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 local vstart, vend = (qstart and timestamp_parse(qstart)), (qend and timestamp_parse(qend))
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 if (qstart and not vstart) or (qend and not vend) then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid timestamp"))
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 return true
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 qstart, qend = vstart, vend;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 local qres;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 if qwith then -- Validate the 'with' jid
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 local pwith = qwith and jid_prep(qwith);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 if pwith and not qwith then -- it failed prepping
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid JID"))
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 return true
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 local _, _, resource = jid_split(qwith);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 qwith = jid_bare(pwith);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 qres = resource;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 -- RSM stuff
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 local qmax = m_min(qset and qset.max or default_max_items, max_max_items);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 local first, last;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 local n = 0;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 local sql_query = ([[
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 SELECT `id`, `when`, `stanza`
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 FROM `prosodyarchive`
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 WHERE `host` = ? AND `user` = ? AND `store` = ?
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 AND `when` BETWEEN ? AND ?
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 %s
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 AND `id` > ?
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 LIMIT ?;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 ]]):format(qres and [[AND `resource` = ?]] or "")
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 local p = {
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 host, room, archive_store,
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 qstart or 0, qend or time_now(),
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 qset and tonumber(qset.after) or 0,
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 qmax
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 };
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 if qres then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 t_insert(p, 6, qres);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 module:log("debug", "getsql(sql_query, "..table.concat(p, ", "));
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 local data, err = getsql(sql_query, unpack(p));
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 if not data then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error loading archive: "..tostring(err)));
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 return true
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 for item in data:rows() do
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 local id, when, orig_stanza = unpack(item);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 --module:log("debug", "id is %s", id);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 local fwd_st = st.message{ to = stanza.attr.from, from = stanza.attr.to }
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id }):up()
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 :tag("forwarded", { xmlns = xmlns_forward })
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 :tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up();
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 orig_stanza = st.deserialize(deserialize(orig_stanza));
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 orig_stanza.attr.xmlns = "jabber:client";
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 fwd_st:add_child(orig_stanza);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 origin.send(fwd_st);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 last = id;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234 n = n + 1;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235 if not first then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 first = id;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 -- That's all folks!
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 module:log("debug", "Archive query %s completed", tostring(qid));
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242 local reply = st.reply(stanza);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243 if last then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244 -- This is a bit redundant, isn't it?
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 reply:query(xmlns_mam):add_child(rsm.generate{first = first, last = last, count = n});
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 origin.send(reply);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 return true
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250 end);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 -- Handle messages
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
253 local function message_handler(event)
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254 local origin, stanza = event.origin, event.stanza;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 local orig_type = stanza.attr.type or "normal";
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 local orig_to = stanza.attr.to;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 local orig_from = stanza.attr.from;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
259 -- Still needed?
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260 if not orig_from then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
261 orig_from = origin.full_jid;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 -- Only store groupchat messages
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 if not (orig_type == "groupchat" and (stanza:get_child("body") or stanza:get_child("subject"))) then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266 return;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269 local room = jid_split(orig_to);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270 local room_obj = hosts[host].modules.muc.rooms[orig_to]
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 if not room_obj then return end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273 local when = time_now();
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 local stanza = st.clone(stanza); -- Private copy
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275 --stanza.attr.to = nil;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276 local nick = room_obj._jid_nick[orig_from];
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277 if not nick then return end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 stanza.attr.from = nick;
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279 local _, _, nick = jid_split(nick);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 -- And stash it
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 local ok, err = setsql([[
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 INSERT INTO `prosodyarchive`
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 (`host`, `user`, `store`, `when`, `resource`, `stanza`)
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 VALUES (?, ?, ?, ?, ?, ?);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 ]], host, room, archive_store, when, nick, serialize(st.preserialize(stanza)))
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
286 if ok then
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287 sql.commit();
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
288 else
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 module:log("error", "SQL error: %s", err);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 sql.rollback();
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
292 --[[ This was dropped from the spec
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 821
diff changeset
293 if ok then
821
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 stanza:tag("archived", { xmlns = xmlns_mam, by = host, id = id }):up();
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
295 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
296 --]]
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 end
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299 module:hook("message/bare", message_handler, 2);
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300
87e847280aef mod_mam_muc_sql: SQL variant of mod_mam_muc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 module:add_feature(xmlns_mam);