annotate mod_storage_gdbm/mod_storage_gdbm.lua @ 6299:5cf5ee23b361

mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs. diff --git a/mod_voipms/README.md b/mod_voipms/README.md new file mode 100644 --- /dev/null +++ b/mod_voipms/README.md @@ -0,0 +1,51 @@ +--- +labels: +- 'Stage-Alpha' +- 'Type-Web' +summary: Send and receive SMS/MMS via VoIP.ms APIs. +rockspec: + build: + modules: + mod_voipms: mod_voipms.lua +... + +Introduction +============ + +This is a Prosody module to map JIDs to DIDs on VoIP.ms and support sending/receiving SMS/MMS. + +Configuration +============= + +| option | type | default | +|-----------------------|--------|---------| +| voipms\_api\_username | string | nil | +| voipms\_api\_password | string | nil | +| voipms\_query\_key | string | nil | +| voipms\_jid\_map | table | nil | +``` +VirtualHost "sms.example.com" +modules_enabled = { + "voipms"; +} +voipms_api_username = john@example.com -- E-mail registered with VoIP.ms +voipms_api_password = abcd1234 -- API password configured in VoIP.ms +voipms_query_key = some_query_key -- query param 'key' part of your URL callback +voipms_jid_map = { + ["your_jid@your_domain.com"] = "+1234567890" +} +``` + +HTTP +==== + +The module is served on Prosody's default HTTP ports at the path /voipms. More details on configuring HTTP modules in Prosody can be found in the HTTP documentation. + +VoIP.ms Webhook URL +=================== + +This module receives the VoIP.ms Webhook URL (POST) at the /voipms endpoint. It uses the sendSMS/sendMMS GET methods against the VoIP.ms APIs. This is an example webhook to use in VoIP.ms: + +``` +https://sms.example.com/voipms?key=some_query_key +``` diff --git a/mod_voipms/mod_voipms.lua b/mod_voipms/mod_voipms.lua new file mode 100644 --- /dev/null +++ b/mod_voipms/mod_voipms.lua @@ -0,0 +1,166 @@ +local http = require "net.http" +local json = require "util.json" +local st = require "util.stanza" + +local api_username = module:get_option_string("voipms_api_username") +local api_password = module:get_option_string("voipms_api_password") +local query_key = module:get_option("voipms_query_key") +local jid_map = module:get_option("voipms_jid_map") or {} +local rest_endpoint = "https://voip.ms/api/v1/rest.php" + +if not api_username or not api_password or not query_key then + module:log("error", "Missing required config values (voipms_api_username, voipms_api_password, voipms_query_key)") + return +end + +module:depends("http") + +local function normalize_number(num) + if not num then return nil end + if num:sub(1, 1) ~= "+" then + return "+1" .. num + end + return num +end + +local function extract_query_key(event) + return (event.request.url.query or ""):match("key=([^&]+)") +end + +module:provides("http", { + route = { + ["POST"] = function(event) + local req = event.request + local body = req.body or "" + + if extract_query_key(event) ~= query_key then + module:log("warn", "Unauthorized webhook: missing or invalid key") + return { status_code = 403 } + end + + local json_payload, err = json.decode(body) + if not json_payload then + module:log("warn", "Invalid JSON: %s", err or "unknown error") + return { status_code = 400 } + end + + local payload = json_payload.data and json_payload.data.payload + if not payload then + module:log("warn", "Missing payload in JSON") + return { status_code = 400 } + end + + local from = payload.from and payload.from.phone_number + local to_list = payload.to or {} + local to = #to_list > 0 and to_list[1].phone_number or nil + + if not from or not to then + module:log("warn", "Missing phone numbers (from: %s, to: %s)", tostring(from), tostring(to)) + return { status_code = 400 } + end + + local normalized_to = normalize_number(to) + local target_jid = nil + + for jid, did in pairs(jid_map) do + if normalize_number(did) == normalized_to then + target_jid = jid + break + end + end + + if not target_jid then + module:log("warn", "No JID mapping for DID %s", normalized_to) + return { status_code = 404 } + end + + local normalized_from = normalize_number(from) + local message_text = payload.text or "" + + local message = st.message({ + from = normalized_from .. "@" .. module.host, + to = target_jid, + type = "chat" + }):tag("body"):text(message_text):up() + + if payload.media and #payload.media > 0 then + for _, media_item in ipairs(payload.media) do + if media_item.url then + message_text = message_text .. "\n" .. media_item.url + message:tag("x", { xmlns = "jabber:x:oob" }) + :tag("url"):text(media_item.url):up():up() + end + end + end + + module:send(message) + module:log("info", "Delivered SMS from %s to %s", normalized_from, target_jid) + + return { status_code = 204 } + end + } +}) + +module:hook("message/bare", function(event) + local stanza = event.stanza + if stanza.attr.type ~= "chat" then return end + + local from_jid = stanza.attr.from + local to_jid = stanza.attr.to + local body = stanza:get_child_text("body") + if not body or body == "" then return end + + local from_number = jid_map[from_jid] + if not from_number then + module:log("warn", "No DID mapping for JID %s", from_jid) + return + end + + local to_number = to_jid:match("^([^@]+)") + if not to_number then + module:log("warn", "Malformed JID in message.to: %s", to_jid) + return + end + + to_number = normalize_number(to_number) + + local media_urls = {} + for line in body:gmatch("[^\r\n]+") do + if line:match("^https?://") then + table.insert(media_urls, line) + end + end + + local method = (#media_urls > 0) and "sendMMS" or "sendSMS" + local query = { + api_username = api_username, + api_password = api_password, + method = method, + did = from_number, + dst = to_number, + message = body + } + + if method == "sendMMS" then + for i, url in ipairs(media_urls) do + query["media_url[" .. (i - 1) .. "]"] = url + end + end + + local query_str = http.formencode(query) + + http.request(rest_endpoint .. "?" .. query_str, { + method = "GET"; + }, function(response_body, code) + if code == 200 then + local resp, err = json.decode(response_body) + if not resp or resp.status ~= "success" then + module:log("error", "Failed to send %s: %s", method, err or (resp and resp.status) or "unknown") + else + module:log("info", "Sent %s from %s to %s", method, from_number, to_number) + end + else + module:log("error", "HTTP error sending %s: code %s", method, tostring(code)) + end + end) +end)
author Chaz <menel@snikket.de>
date Mon, 21 Jul 2025 23:57:37 +0200
parents 95ad6e68e203
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- mod_storage_gdbm
1629
36eb0dbea7ba mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents: 1628
diff changeset
2 -- Copyright (C) 2014-2015 Kim Alvefur
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 --
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 -- This file is MIT/X11 licensed.
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 --
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 -- Depends on lgdbm:
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 -- http://webserver2.tecgraf.puc-rio.br/~lhf/ftp/lua/#lgdbm
1653
1fe899527ee5 mod_storage_gdbm: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1635
diff changeset
8 --
1fe899527ee5 mod_storage_gdbm: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1635
diff changeset
9 -- luacheck: globals open purge
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local gdbm = require"gdbm";
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local path = require"util.paths";
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local lfs = require"lfs";
1629
36eb0dbea7ba mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents: 1628
diff changeset
14 local st = require"util.stanza";
1596
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
15 local uuid = require"util.uuid".generate;
1629
36eb0dbea7ba mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents: 1628
diff changeset
16
1593
d9f3c66ea938 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents: 1570
diff changeset
17 local serialization = require"util.serialization";
d9f3c66ea938 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents: 1570
diff changeset
18 local serialize = serialization.serialize;
d9f3c66ea938 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents: 1570
diff changeset
19 local deserialize = serialization.deserialize;
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20
1629
36eb0dbea7ba mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents: 1628
diff changeset
21 local g_set, g_get, g_del = gdbm.replace, gdbm.fetch, gdbm.delete;
36eb0dbea7ba mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents: 1628
diff changeset
22 local g_first, g_next = gdbm.firstkey, gdbm.nextkey;
36eb0dbea7ba mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents: 1628
diff changeset
23
1628
5e4b37b9cde1 mod_storage_gdbm: Fix traceback if query is nil or no metadata exists
Kim Alvefur <zash@zash.se>
parents: 1604
diff changeset
24 local empty = {};
5e4b37b9cde1 mod_storage_gdbm: Fix traceback if query is nil or no metadata exists
Kim Alvefur <zash@zash.se>
parents: 1604
diff changeset
25
1596
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
26 local function id(v) return v; end
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
27
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
28 local function is_stanza(s)
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
29 return getmetatable(s) == st.stanza_mt;
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
30 end
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
31
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 local base_path = path.resolve_relative_path(prosody.paths.data, module.host);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 lfs.mkdir(base_path);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 local cache = {};
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36
1595
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
37 local keyval = {};
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
38 local keyval_mt = { __index = keyval, suffix = ".db" };
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
1595
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
40 function keyval:set(user, value)
1630
0fcd63818aa1 mod_storage_gdbm: Make write operation delete data when passed 'nil' as value instead of storing a literal "nil" string
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
41 if type(value) == "table" and next(value) == nil then
0fcd63818aa1 mod_storage_gdbm: Make write operation delete data when passed 'nil' as value instead of storing a literal "nil" string
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
42 value = nil;
0fcd63818aa1 mod_storage_gdbm: Make write operation delete data when passed 'nil' as value instead of storing a literal "nil" string
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
43 end
0fcd63818aa1 mod_storage_gdbm: Make write operation delete data when passed 'nil' as value instead of storing a literal "nil" string
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
44 if value ~= nil then
0fcd63818aa1 mod_storage_gdbm: Make write operation delete data when passed 'nil' as value instead of storing a literal "nil" string
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
45 value = serialize(value);
0fcd63818aa1 mod_storage_gdbm: Make write operation delete data when passed 'nil' as value instead of storing a literal "nil" string
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
46 end
0fcd63818aa1 mod_storage_gdbm: Make write operation delete data when passed 'nil' as value instead of storing a literal "nil" string
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
47 local ok, err = (value and g_set or g_del)(self._db, user or "@", value);
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 if not ok then return nil, err; end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 return true;
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51
1595
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
52 function keyval:get(user)
1629
36eb0dbea7ba mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents: 1628
diff changeset
53 local data, err = g_get(self._db, user or "@");
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 if not data then return nil, err; end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 return deserialize(data);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57
1633
1d2dc6c74581 mod_storage_gdbm: Add user iterator
Kim Alvefur <zash@zash.se>
parents: 1632
diff changeset
58 local function g_keys(db, key)
1d2dc6c74581 mod_storage_gdbm: Add user iterator
Kim Alvefur <zash@zash.se>
parents: 1632
diff changeset
59 return (key == nil and g_first or g_next)(db, key);
1d2dc6c74581 mod_storage_gdbm: Add user iterator
Kim Alvefur <zash@zash.se>
parents: 1632
diff changeset
60 end
1d2dc6c74581 mod_storage_gdbm: Add user iterator
Kim Alvefur <zash@zash.se>
parents: 1632
diff changeset
61
1d2dc6c74581 mod_storage_gdbm: Add user iterator
Kim Alvefur <zash@zash.se>
parents: 1632
diff changeset
62 function keyval:users()
1d2dc6c74581 mod_storage_gdbm: Add user iterator
Kim Alvefur <zash@zash.se>
parents: 1632
diff changeset
63 return g_keys, self._db, nil;
1d2dc6c74581 mod_storage_gdbm: Add user iterator
Kim Alvefur <zash@zash.se>
parents: 1632
diff changeset
64 end
1d2dc6c74581 mod_storage_gdbm: Add user iterator
Kim Alvefur <zash@zash.se>
parents: 1632
diff changeset
65
1596
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
66 local archive = {};
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
67 local archive_mt = { __index = archive, suffix = ".adb" };
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
68
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
69 archive.get = keyval.get;
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
70 archive.set = keyval.set;
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
71
1753
54c8a0cb2996 mod_storage_(archive-capable): Change order of arguments to :append to be the same as return values from :find iterator (see prosody 41725f3df3cc)
Kim Alvefur <zash@zash.se>
parents: 1653
diff changeset
72 function archive:append(username, key, value, when, with)
54c8a0cb2996 mod_storage_(archive-capable): Change order of arguments to :append to be the same as return values from :find iterator (see prosody 41725f3df3cc)
Kim Alvefur <zash@zash.se>
parents: 1653
diff changeset
73 if type(when) ~= "number" then
1760
e72f9eac51c8 mod_storage_(various): Order swapping in 54c8a0cb2996 was backwards
Kim Alvefur <zash@zash.se>
parents: 1756
diff changeset
74 when, with, value = value, when, with;
1753
54c8a0cb2996 mod_storage_(archive-capable): Change order of arguments to :append to be the same as return values from :find iterator (see prosody 41725f3df3cc)
Kim Alvefur <zash@zash.se>
parents: 1653
diff changeset
75 end
54c8a0cb2996 mod_storage_(archive-capable): Change order of arguments to :append to be the same as return values from :find iterator (see prosody 41725f3df3cc)
Kim Alvefur <zash@zash.se>
parents: 1653
diff changeset
76
1596
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
77 key = key or uuid();
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
78 local meta = self:get(username);
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
79 if not meta then
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
80 meta = {};
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
81 end
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
82 local i = meta[key] or #meta+1;
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
83 local type;
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
84 if is_stanza(value) then
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
85 type, value = "stanza", st.preserialize(value);
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
86 end
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
87 meta[i] = { key = key, when = when, with = with, type = type };
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
88 meta[key] = i;
1632
050d101af207 mod_storage_gdbm: Prefix archive item keys with username to prevent collisions
Kim Alvefur <zash@zash.se>
parents: 1631
diff changeset
89 local prefix = (username or "@") .. "#";
050d101af207 mod_storage_gdbm: Prefix archive item keys with username to prevent collisions
Kim Alvefur <zash@zash.se>
parents: 1631
diff changeset
90 local ok, err = self:set(prefix..key, value);
1596
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
91 if not ok then return nil, err; end
1631
98427ed3d53f mod_storage_gdbm: Update archive metadata only if data write succeeds
Kim Alvefur <zash@zash.se>
parents: 1630
diff changeset
92 ok, err = self:set(username, meta);
1596
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
93 if not ok then return nil, err; end
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
94 return key;
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
95 end
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
96
1653
1fe899527ee5 mod_storage_gdbm: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1635
diff changeset
97 local deserialize_map = {
1596
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
98 stanza = st.deserialize;
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
99 };
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
100
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
101 function archive:find(username, query)
1635
b877b75eb973 mod_storage_gdbm: Fix name of empty table
Kim Alvefur <zash@zash.se>
parents: 1634
diff changeset
102 query = query or empty;
1628
5e4b37b9cde1 mod_storage_gdbm: Fix traceback if query is nil or no metadata exists
Kim Alvefur <zash@zash.se>
parents: 1604
diff changeset
103 local meta = self:get(username) or empty;
1632
050d101af207 mod_storage_gdbm: Prefix archive item keys with username to prevent collisions
Kim Alvefur <zash@zash.se>
parents: 1631
diff changeset
104 local prefix = (username or "@") .. "#";
1968
95ad6e68e203 mod_storage_gdbm: Refactor for readability (bye single-char variable names)
Kim Alvefur <zash@zash.se>
parents: 1760
diff changeset
105 local reverse = query.reverse;
95ad6e68e203 mod_storage_gdbm: Refactor for readability (bye single-char variable names)
Kim Alvefur <zash@zash.se>
parents: 1760
diff changeset
106 local step = reverse and -1 or 1;
95ad6e68e203 mod_storage_gdbm: Refactor for readability (bye single-char variable names)
Kim Alvefur <zash@zash.se>
parents: 1760
diff changeset
107 local first = meta[query.after];
95ad6e68e203 mod_storage_gdbm: Refactor for readability (bye single-char variable names)
Kim Alvefur <zash@zash.se>
parents: 1760
diff changeset
108 local last = meta[query.before];
95ad6e68e203 mod_storage_gdbm: Refactor for readability (bye single-char variable names)
Kim Alvefur <zash@zash.se>
parents: 1760
diff changeset
109 -- we don't want to include the actual 'after' or 'before' item
95ad6e68e203 mod_storage_gdbm: Refactor for readability (bye single-char variable names)
Kim Alvefur <zash@zash.se>
parents: 1760
diff changeset
110 if first then first = first + 1; end
95ad6e68e203 mod_storage_gdbm: Refactor for readability (bye single-char variable names)
Kim Alvefur <zash@zash.se>
parents: 1760
diff changeset
111 if last then last = last - 1; end
95ad6e68e203 mod_storage_gdbm: Refactor for readability (bye single-char variable names)
Kim Alvefur <zash@zash.se>
parents: 1760
diff changeset
112 first, last = first or 1, last or #meta;
95ad6e68e203 mod_storage_gdbm: Refactor for readability (bye single-char variable names)
Kim Alvefur <zash@zash.se>
parents: 1760
diff changeset
113 if reverse then first, last = last, first; end
1603
1fbec16996f5 mod_storage_gdbm: Add support for 'limit' query parameter
Kim Alvefur <zash@zash.se>
parents: 1602
diff changeset
114 local limit = query.limit;
1968
95ad6e68e203 mod_storage_gdbm: Refactor for readability (bye single-char variable names)
Kim Alvefur <zash@zash.se>
parents: 1760
diff changeset
115 local count = 0;
1596
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
116 return function ()
1968
95ad6e68e203 mod_storage_gdbm: Refactor for readability (bye single-char variable names)
Kim Alvefur <zash@zash.se>
parents: 1760
diff changeset
117 if limit and count >= limit then return end
1596
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
118 local item, value;
1968
95ad6e68e203 mod_storage_gdbm: Refactor for readability (bye single-char variable names)
Kim Alvefur <zash@zash.se>
parents: 1760
diff changeset
119 for i = first, last, step do
1596
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
120 item = meta[i];
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
121 if (not query.with or item.with == query.with)
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
122 and (not query.start or item.when >= query.start)
1604
53052a610c67 mod_storage_gdbm: Fix comparison of 'end'
Kim Alvefur <zash@zash.se>
parents: 1603
diff changeset
123 and (not query["end"] or item.when <= query["end"]) then
1968
95ad6e68e203 mod_storage_gdbm: Refactor for readability (bye single-char variable names)
Kim Alvefur <zash@zash.se>
parents: 1760
diff changeset
124 first = i + step; count = count + 1;
1632
050d101af207 mod_storage_gdbm: Prefix archive item keys with username to prevent collisions
Kim Alvefur <zash@zash.se>
parents: 1631
diff changeset
125 value = self:get(prefix..item.key);
1653
1fe899527ee5 mod_storage_gdbm: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1635
diff changeset
126 return item.key, (deserialize_map[item.type] or id)(value), item.when, item.with;
1596
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
127 end
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
128 end
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
129 end
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
130 end
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
131
1595
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
132 local drivers = {
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
133 keyval = keyval_mt;
1596
b362e6c00fd1 mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents: 1595
diff changeset
134 archive = archive_mt;
1595
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
135 }
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
136
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 function open(_, store, typ)
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 typ = typ or "keyval";
1595
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
139 local driver_mt = drivers[typ];
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
140 if not driver_mt then
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 return nil, "unsupported-store";
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143
1595
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
144 local db_path = path.join(base_path, store) .. driver_mt.suffix;
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 local db = cache[db_path];
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 if not db then
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 db = assert(gdbm.open(db_path, "c"));
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 cache[db_path] = db;
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 end
1754
c04d10557bbc mod_storage_gdbm: Fix typo in unused field on store instance
Kim Alvefur <zash@zash.se>
parents: 1753
diff changeset
151 return setmetatable({ _db = db; _path = db_path; store = store, type = typ }, driver_mt);
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153
1634
25441bd8b344 mod_storage_gdbm: Add purge action
Kim Alvefur <zash@zash.se>
parents: 1633
diff changeset
154 function purge(_, user)
25441bd8b344 mod_storage_gdbm: Add purge action
Kim Alvefur <zash@zash.se>
parents: 1633
diff changeset
155 for dir in lfs.dir(base_path) do
25441bd8b344 mod_storage_gdbm: Add purge action
Kim Alvefur <zash@zash.se>
parents: 1633
diff changeset
156 local name, ext = dir:match("^(.-)%.a?db$");
25441bd8b344 mod_storage_gdbm: Add purge action
Kim Alvefur <zash@zash.se>
parents: 1633
diff changeset
157 if ext == ".db" then
25441bd8b344 mod_storage_gdbm: Add purge action
Kim Alvefur <zash@zash.se>
parents: 1633
diff changeset
158 open(_, name, "keyval"):set(user, nil);
25441bd8b344 mod_storage_gdbm: Add purge action
Kim Alvefur <zash@zash.se>
parents: 1633
diff changeset
159 elseif ext == ".adb" then
25441bd8b344 mod_storage_gdbm: Add purge action
Kim Alvefur <zash@zash.se>
parents: 1633
diff changeset
160 open(_, name, "archive"):delete(user);
25441bd8b344 mod_storage_gdbm: Add purge action
Kim Alvefur <zash@zash.se>
parents: 1633
diff changeset
161 end
25441bd8b344 mod_storage_gdbm: Add purge action
Kim Alvefur <zash@zash.se>
parents: 1633
diff changeset
162 end
25441bd8b344 mod_storage_gdbm: Add purge action
Kim Alvefur <zash@zash.se>
parents: 1633
diff changeset
163 return true;
25441bd8b344 mod_storage_gdbm: Add purge action
Kim Alvefur <zash@zash.se>
parents: 1633
diff changeset
164 end
25441bd8b344 mod_storage_gdbm: Add purge action
Kim Alvefur <zash@zash.se>
parents: 1633
diff changeset
165
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 function module.unload()
1653
1fe899527ee5 mod_storage_gdbm: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1635
diff changeset
167 for db_path, db in pairs(cache) do
1fe899527ee5 mod_storage_gdbm: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1635
diff changeset
168 module:log("debug", "Closing db at %q", db_path);
1756
c619425dafe7 mod_storage_gdbm: Reorganize (vacuum) databases on module unload
Kim Alvefur <zash@zash.se>
parents: 1754
diff changeset
169 gdbm.reorganize(db);
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 gdbm.sync(db);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 gdbm.close(db);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 module:provides"storage";