annotate mod_storage_ejabberdsql_readonly/mod_storage_ejabberdsql_readonly.lua @ 4565:3b2ae854842c

mod_muc_bot: Save occupant to room This has some side-effects: Firstly, the bot shows up in occupant list, which is nice. Secondly, the bot starts receiving messages from the room which might be wanted, but it would be better to join the room for real in this case.
author Kim Alvefur <zash@zash.se>
date Sat, 10 Apr 2021 19:23:25 +0200
parents e0663dcd934d
children 7c264a2cb970
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2223
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 -- luacheck: ignore 212/self
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 local sql = require "util.sql";
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 local xml_parse = require "util.xml".parse;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 local resolve_relative_path = require "util.paths".resolve_relative_path;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 local stanza_preserialize = require "util.stanza".preserialize;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 local unpack = unpack
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 local function iterator(result)
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 return function(result_)
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 local row = result_();
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 if row ~= nil then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 return unpack(row);
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 end, result, nil;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 local default_params = { driver = "SQLite3" };
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 local engine;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 local host = module.host;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 local user, store;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 local function keyval_store_get()
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 if store == "accounts" then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 --for row in engine:select("SELECT `password`,`created_at` FROM `users` WHERE `username`=?", user or "") do
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 local result;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 for row in engine:select("SELECT `password` FROM `users` WHERE `username`=? LIMIT 1", user or "") do result = row end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 local password = result[1];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 --local created_at = result[2];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 return { password = password };
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 elseif store == "roster" then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 local roster = {};
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 local pending = nil;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 --for row in engine:select("SELECT `jid`,`nick`,`subscription`,`ask`,`askmessage`,`server`,`subscribe`,`type`,`created_at` FROM `rosterusers` WHERE `username`=?", user or "") do
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 for row in engine:select("SELECT `jid`,`nick`,`subscription`,`ask` FROM `rosterusers` WHERE `username`=?", user or "") do
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 local contact = row[1];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 local name = row[2];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 if name == "" then name = nil; end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 local subscription = row[3];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 if subscription == "N" then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 subscription = "none"
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 elseif subscription == "B" then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 subscription = "both"
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 elseif subscription == "F" then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 subscription = "from"
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 elseif subscription == "T" then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 subscription = "to"
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 else error("Unknown subscription type: "..subscription) end;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 local ask = row[4];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 if ask == "N" then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 ask = nil;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 elseif ask == "O" then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 ask = "subscribe";
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 elseif ask == "I" then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 if pending == nil then pending = {} end;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 pending[contact] = true;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 ask = nil;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 elseif ask == "B" then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 if pending == nil then pending = {} end;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 pending[contact] = true;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 ask = "subscribe";
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 else error("Unknown ask type: "..ask); end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 --local askmessage = row[5];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 --local server = row[6];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 --local subscribe = row[7];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 --local type = row[8];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 --local created_at = row[9];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 local groups = {};
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 for row in engine:select("SELECT `grp` FROM `rostergroups` WHERE `username`=? AND `jid`=?", user or "", contact) do
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 local group = row[1];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 groups[group] = true;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 roster[contact] = { name = name, ask = ask, subscription = subscription, groups = groups };
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 return roster;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 elseif store == "vcard" then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 local result = nil;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 for row in engine:select("SELECT `vcard` FROM `vcard` WHERE `username`=? LIMIT 1", user or "") do result = row end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 if not result then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 return nil;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 local data, err = xml_parse(result[1]);
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 if data then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 return stanza_preserialize(data);
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 elseif store == "private" then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 local private = nil;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 local result;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 for row in engine:select("SELECT `namespace`,`data` FROM `private_storage` WHERE `username`=?", user or "") do
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 if private == nil then private = {} end;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 local namespace = row[1];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 local data, err = xml_parse(row[2]);
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 if data then
2244
e0663dcd934d mod_storage_ejabberd_readonly: Fix the key for the private store, it’s node:namespace but just namespace in Ejabberd
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2223
diff changeset
103 assert(namespace == data.attr.xmlns, "Wrong namespace in private data!");
e0663dcd934d mod_storage_ejabberd_readonly: Fix the key for the private store, it’s node:namespace but just namespace in Ejabberd
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2223
diff changeset
104 local key = data.name..":"..data.attr.xmlns;
e0663dcd934d mod_storage_ejabberd_readonly: Fix the key for the private store, it’s node:namespace but just namespace in Ejabberd
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2223
diff changeset
105 private[key] = stanza_preserialize(data);
2223
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 return private;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 --- Key/value store API (default store type)
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 local keyval_store = {};
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115 keyval_store.__index = keyval_store;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 function keyval_store:get(username)
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117 user, store = username, self.store;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 local ok, result = engine:transaction(keyval_store_get);
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 if not ok then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 module:log("error", "Unable to read from database %s store for %s: %s", store, username or "<host>", result);
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 return nil, result;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 return result;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 function keyval_store:users()
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 local ok, result = engine:transaction(function()
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 return engine:select("SELECT `username` FROM `users`");
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 end);
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130 if not ok then return ok, result end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 return iterator(result);
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134 local stores = {
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 keyval = keyval_store;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136 };
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138 --- Implement storage driver API
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140 -- FIXME: Some of these operations need to operate on the archive store(s) too
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 local driver = {};
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 function driver:open(store, typ)
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 local store_mt = stores[typ or "keyval"];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 if store_mt then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 return setmetatable({ store = store }, store_mt);
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 return nil, "unsupported-store";
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 function driver:stores(username)
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 local query = "SELECT 'accounts', 'roster', 'vcard', 'private'";
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 if username == true or not username then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 username = "";
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 local ok, result = engine:transaction(function()
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158 return engine:select(query, host, username);
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 end);
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 if not ok then return ok, result end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 return iterator(result);
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164 --- Initialization
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
166
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 local function normalize_params(params)
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 if params.driver == "SQLite3" then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 if params.database ~= ":memory:" then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite");
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173 assert(params.driver and params.database, "Configuration error: Both the SQL driver and the database need to be specified");
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174 return params;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 function module.load()
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178 if prosody.prosodyctl then return; end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 local engines = module:shared("/*/sql/connections");
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
180 local params = normalize_params(module:get_option("sql", default_params));
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
181 engine = engines[sql.db2uri(params)];
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
182 if not engine then
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
183 module:log("debug", "Creating new engine");
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
184 engine = sql:create_engine(params);
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
185 engines[sql.db2uri(params)] = engine;
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
186 end
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
187
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
188 module:provides("storage", driver);
c3ad652cb71f mod_storage_ejabberdsql_readonly: Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189 end