Annotate

mod_storage_muconference_readonly/mod_storage_muconference_readonly.lua @ 2232:836b36d8bfd2

mod_blocking/README: Use magic autolinking
author Kim Alvefur <zash@zash.se>
date Fri, 08 Jul 2016 22:35:13 +0200
parent 2222:51596d73157e
child 2245:a0727d23ee65
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2222
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 -- luacheck: ignore 212/self
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 local sql = require "util.sql";
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 local xml_parse = require "util.xml".parse;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 local resolve_relative_path = require "util.paths".resolve_relative_path;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 local stanza_preserialize = require "util.stanza".preserialize;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 local unpack = unpack
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 local function iterator(result)
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 return function(result_)
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 local row = result_();
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 if row ~= nil then
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 return unpack(row);
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 end, result, nil;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 local default_params = { driver = "MySQL" };
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 local engine;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 local host = module.host;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 local room, store;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 local function get_best_affiliation(a, b)
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 if a == 'owner' or b == 'owner' then
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 return 'owner';
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 elseif a == 'administrator' or b == 'administrator' then
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 return 'administrator';
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 elseif a == 'outcast' or b == 'outcast' then
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 return 'outcast';
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 elseif a == 'member' or b == 'member' then
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 return 'member';
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 assert(false);
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 local function keyval_store_get()
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 if store == "muc" then
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 local room_jid = room.."@"..host;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 local result;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 for row in engine:select("SELECT `name`,`desc`,`topic`,`public`,`secret` FROM `rooms` WHERE `jid`=? LIMIT 1", room_jid or "") do result = row end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 local name = result[1];
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 local desc = result[2];
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 local subject = result[3];
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 local public = result[4];
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 local hidden = public == 0 and true or nil;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 local secret = result[5];
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 if secret == '' then secret = nil end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 local affiliations = {};
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 for row in engine:select("SELECT `jid_user`,`affil` FROM `rooms_lists` WHERE `jid_room`=?", room_jid or "") do
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 local jid_user = row[1];
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 local affil = row[2];
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 -- mu-conference has a bug where full JIDs get stored…
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 local bare_jid = jid_user:gsub('/.*', '');
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 local old_affil = affiliations[bare_jid];
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 -- mu-conference has a bug where it can record multiple affiliations…
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 if old_affil ~= nil and old_affil ~= affil then
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 affil = get_best_affiliation(old_affil, affil);
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 -- terminology is clearly “admin”, not “administrator”.
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 if affil == 'administrator' then
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 affil = 'admin';
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 affiliations[bare_jid] = affil;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 return {
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 jid = room_jid,
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 _data = {
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 persistent = true,
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 name = name,
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 subject = subject,
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 password = secret,
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 hidden = hidden,
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 },
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 _affiliations = affiliations,
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 };
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 --- Key/value store API (default store type)
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 local keyval_store = {};
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 keyval_store.__index = keyval_store;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 function keyval_store:get(roomname)
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 room, store = roomname, self.store;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 local ok, result = engine:transaction(keyval_store_get);
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 if not ok then
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 module:log("error", "Unable to read from database %s store for %s: %s", store, roomname or "<host>", result);
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 return nil, result;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 return result;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 function keyval_store:users()
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 local host_length = host:len() + 1;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 local ok, result = engine:transaction(function()
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 return engine:select("SELECT SUBSTRING_INDEX(jid, '@', 1) FROM `rooms`");
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 end);
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 if not ok then return ok, result end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 return iterator(result);
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 local stores = {
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 keyval = keyval_store;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 };
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 --- Implement storage driver API
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 -- FIXME: Some of these operations need to operate on the archive store(s) too
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 local driver = {};
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115 function driver:open(store, typ)
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 local store_mt = stores[typ or "keyval"];
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117 if store_mt then
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 return setmetatable({ store = store }, store_mt);
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 return nil, "unsupported-store";
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 function driver:stores(roomname)
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 local query = "SELECT 'config'";
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 if roomname == true or not roomname then
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 roomname = "";
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 local ok, result = engine:transaction(function()
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 return engine:select(query, host, roomname);
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130 end);
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 if not ok then return ok, result end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 return iterator(result);
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 --- Initialization
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138 local function normalize_params(params)
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 assert(params.driver and params.database, "Configuration error: Both the SQL driver and the database need to be specified");
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140 return params;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 function module.load()
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 if prosody.prosodyctl then return; end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 local engines = module:shared("/*/sql/connections");
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 local params = normalize_params(module:get_option("sql", default_params));
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 engine = engines[sql.db2uri(params)];
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 if not engine then
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 module:log("debug", "Creating new engine");
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 engine = sql:create_engine(params);
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151 engines[sql.db2uri(params)] = engine;
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 end
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 module:provides("storage", driver);
51596d73157e mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 end