Mercurial > prosody-modules
annotate mod_storage_muconference_readonly/mod_storage_muconference_readonly.lua @ 4421:94805a7e7b30
mod_invites: rework CLI parsing to support groups
To make this sensible, the code had to move from rather simple
parsing to something which looks more like getopt or your typical
shell script.
author | Jonas Schäfer <jonas@wielicki.name> |
---|---|
date | Sun, 31 Jan 2021 19:16:36 +0100 |
parents | a0727d23ee65 |
children |
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() |
2245
a0727d23ee65
mod_storage_muconference_readonly: Use the correct store name, and add description.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2222
diff
changeset
|
40 if store == "config" then |
2222
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, |
2245
a0727d23ee65
mod_storage_muconference_readonly: Use the correct store name, and add description.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2222
diff
changeset
|
73 description = desc, |
2222
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 subject = subject, |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 password = secret, |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 hidden = hidden, |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
77 }, |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
78 _affiliations = affiliations, |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
79 }; |
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 end |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
82 |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
83 --- Key/value store API (default store type) |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
84 |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
85 local keyval_store = {}; |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
86 keyval_store.__index = keyval_store; |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
87 function keyval_store:get(roomname) |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
88 room, store = roomname, self.store; |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
89 local ok, result = engine:transaction(keyval_store_get); |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
90 if not ok then |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
91 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
|
92 return nil, result; |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
93 end |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
94 return result; |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
95 end |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
96 |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
97 function keyval_store:users() |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
98 local host_length = host:len() + 1; |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
99 local ok, result = engine:transaction(function() |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
100 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
|
101 end); |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
102 if not ok then return ok, result end |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
103 return iterator(result); |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
104 end |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
105 |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
106 local stores = { |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
107 keyval = keyval_store; |
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 |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
110 --- Implement storage driver API |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
111 |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
112 -- 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
|
113 |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
114 local driver = {}; |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
115 |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
116 function driver:open(store, typ) |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
117 local store_mt = stores[typ or "keyval"]; |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
118 if store_mt then |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
119 return setmetatable({ store = store }, store_mt); |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
120 end |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
121 return nil, "unsupported-store"; |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
122 end |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
123 |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
124 function driver:stores(roomname) |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
125 local query = "SELECT 'config'"; |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
126 if roomname == true or not roomname then |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
127 roomname = ""; |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
128 end |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
129 local ok, result = engine:transaction(function() |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
130 return engine:select(query, host, roomname); |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
131 end); |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
132 if not ok then return ok, result end |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
133 return iterator(result); |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
134 end |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
135 |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
136 --- Initialization |
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 |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
139 local function normalize_params(params) |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
140 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
|
141 return params; |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
142 end |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
143 |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
144 function module.load() |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
145 if prosody.prosodyctl then return; end |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
146 local engines = module:shared("/*/sql/connections"); |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
147 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
|
148 engine = engines[sql.db2uri(params)]; |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
149 if not engine then |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
150 module:log("debug", "Creating new engine"); |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
151 engine = sql:create_engine(params); |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
152 engines[sql.db2uri(params)] = engine; |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
153 end |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
154 |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
155 module:provides("storage", driver); |
51596d73157e
mod_storage_muconference_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
156 end |