Mercurial > prosody-modules
annotate mod_log_messages_sql/mod_log_messages_sql.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 | 9d132153d786 |
| children |
| rev | line source |
|---|---|
|
953
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- Based on mod_mam_sql |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 -- Copyright (C) 2011-2012 Kim Alvefur |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- This file is MIT/X11 licensed. |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local st = require "util.stanza"; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local jid_bare = require "util.jid".bare; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local jid_split = require "util.jid".split; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local serialize = require"util.json".encode, require"util.json".decode; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local tostring = tostring; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local time_now = os.time; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
|
2407
be08b65f2855
mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents:
1639
diff
changeset
|
14 |
|
be08b65f2855
mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents:
1639
diff
changeset
|
15 local table_name = module:get_option("message_log_sql_table", pcall(require, "util.cache") and "messages" or "prosodyarchive"); |
|
be08b65f2855
mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents:
1639
diff
changeset
|
16 |
|
953
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local sql, setsql, getsql = {}; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 do -- SQL stuff |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local connection; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 local resolve_relative_path = require "core.configmanager".resolve_relative_path; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 local params = module:get_option("message_log_sql", module:get_option("sql")); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 |
|
2407
be08b65f2855
mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents:
1639
diff
changeset
|
23 |
|
953
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 local function test_connection() |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 if not connection then return nil; end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 if connection:ping() then |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 return true; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 else |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 module:log("debug", "Database connection closed"); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 connection = nil; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 local function connect() |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 if not test_connection() then |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 prosody.unlock_globals(); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 local dbh, err = DBI.Connect( |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 params.driver, params.database, |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 params.username, params.password, |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 params.host, params.port |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 ); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 prosody.lock_globals(); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 if not dbh then |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 module:log("debug", "Database connection failed: %s", tostring(err)); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 return nil, err; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 module:log("debug", "Successfully connected to database"); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 dbh:autocommit(false); -- don't commit automatically |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 connection = dbh; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 return connection; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 |
|
2407
be08b65f2855
mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents:
1639
diff
changeset
|
54 |
|
953
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 do -- process options to get a db connection |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 local ok; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 prosody.unlock_globals(); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 ok, DBI = pcall(require, "DBI"); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 if not ok then |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 package.loaded["DBI"] = {}; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 module:log("error", "Failed to load the LuaDBI library for accessing SQL databases: %s", DBI); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 module:log("error", "More information on installing LuaDBI can be found at http://prosody.im/doc/depends#luadbi"); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 prosody.lock_globals(); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 if not ok or not DBI.Connect then |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 return; -- Halt loading of this module |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 params = params or { driver = "SQLite3" }; |
|
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
953
diff
changeset
|
70 |
|
953
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 if params.driver == "SQLite3" then |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite"); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 end |
|
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
953
diff
changeset
|
74 |
|
953
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 assert(params.driver and params.database, "Both the SQL driver and the database need to be specified"); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 assert(connect()); |
|
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
953
diff
changeset
|
78 |
|
953
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 function getsql(sql, ...) |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 if params.driver == "PostgreSQL" then |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 sql = sql:gsub("`", "\""); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 -- do prepared statement stuff |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 local stmt, err = connection:prepare(sql); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 if not stmt and not test_connection() then error("connection failed"); end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 -- run query |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 local ok, err = stmt:execute(...); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 if not ok and not test_connection() then error("connection failed"); end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 if not ok then return nil, err; end |
|
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
953
diff
changeset
|
93 |
|
953
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 return stmt; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 function setsql(sql, ...) |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 local stmt, err = getsql(sql, ...); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 if not stmt then return stmt, err; end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 return stmt:affected(); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 function sql.rollback(...) |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 if connection then connection:rollback(); end -- FIXME check for rollback error? |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 return ...; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 function sql.commit(...) |
|
1639
398c4aaccf6d
mod_log_messages_sql: Better logging on error
Matthew Wild <mwild1@gmail.com>
parents:
1343
diff
changeset
|
106 local ok, err = connection:commit(); |
|
398c4aaccf6d
mod_log_messages_sql: Better logging on error
Matthew Wild <mwild1@gmail.com>
parents:
1343
diff
changeset
|
107 if not ok then |
|
398c4aaccf6d
mod_log_messages_sql: Better logging on error
Matthew Wild <mwild1@gmail.com>
parents:
1343
diff
changeset
|
108 module:log("error", "SQL commit failed: %s", tostring(err)); |
|
398c4aaccf6d
mod_log_messages_sql: Better logging on error
Matthew Wild <mwild1@gmail.com>
parents:
1343
diff
changeset
|
109 return nil, "SQL commit failed: "..tostring(err); |
|
398c4aaccf6d
mod_log_messages_sql: Better logging on error
Matthew Wild <mwild1@gmail.com>
parents:
1343
diff
changeset
|
110 end |
|
953
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 return ...; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 -- Handle messages |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 local function message_handler(event, c2s) |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 local origin, stanza = event.origin, event.stanza; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 local orig_type = stanza.attr.type or "normal"; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 local orig_to = stanza.attr.to; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 local orig_from = stanza.attr.from; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 if not orig_from and c2s then |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 orig_from = origin.full_jid; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 orig_to = orig_to or orig_from; -- Weird corner cases |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 -- Don't store messages of these types |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 if orig_type == "error" |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 or orig_type == "headline" |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 or orig_type == "groupchat" |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 or not stanza:get_child("body") then |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 return; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 -- TODO Maybe headlines should be configurable? |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 local store_user, store_host = jid_split(c2s and orig_from or orig_to); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 local target_jid = c2s and orig_to or orig_from; |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 local target_bare = jid_bare(target_jid); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 local _, _, target_resource = jid_split(target_jid); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 --local id = uuid(); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 local when = time_now(); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 -- And stash it |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 local ok, err = setsql([[ |
|
2407
be08b65f2855
mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents:
1639
diff
changeset
|
146 INSERT INTO `]]..table_name..[[` |
|
953
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 (`host`, `user`, `store`, `when`, `with`, `resource`, `stanza`) |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 VALUES (?, ?, ?, ?, ?, ?, ?); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 ]], store_host, store_user, "message_log", when, target_bare, target_resource, serialize(st.preserialize(stanza))) |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 if ok then |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 sql.commit(); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 else |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 module:log("error", "SQL error: %s", err); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 sql.rollback(); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 local function c2s_message_handler(event) |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 return message_handler(event, true); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 end |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 -- Stanzas sent by local clients |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 module:hook("pre-message/bare", c2s_message_handler, 2); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 module:hook("pre-message/full", c2s_message_handler, 2); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 -- Stanszas to local clients |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 module:hook("message/bare", message_handler, 2); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 module:hook("message/full", message_handler, 2); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 -- In the telnet console, run: |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 -- >hosts["this host"].modules.mam_sql.environment.create_sql() |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 function create_sql() |
|
2408
9d132153d786
mod_log_messages: Add assert() to detect errors creating SQL table
Matthew Wild <mwild1@gmail.com>
parents:
2407
diff
changeset
|
172 local stm = assert(getsql([[ |
|
2407
be08b65f2855
mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents:
1639
diff
changeset
|
173 CREATE TABLE `]]..table_name..[[` ( |
|
953
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 `host` TEXT, |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 `user` TEXT, |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 `store` TEXT, |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 `id` INTEGER PRIMARY KEY AUTOINCREMENT, |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 `when` INTEGER, |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 `with` TEXT, |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 `resource` TEXT, |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 `stanza` TEXT |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 ); |
|
2407
be08b65f2855
mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents:
1639
diff
changeset
|
183 CREATE INDEX `hus` ON `]]..table_name..[[` (`host`, `user`, `store`); |
|
be08b65f2855
mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents:
1639
diff
changeset
|
184 CREATE INDEX `with` ON `]]..table_name..[[` (`with`); |
|
be08b65f2855
mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
Matthew Wild <mwild1@gmail.com>
parents:
1639
diff
changeset
|
185 CREATE INDEX `thetime` ON `]]..table_name..[[` (`when`); |
|
2408
9d132153d786
mod_log_messages: Add assert() to detect errors creating SQL table
Matthew Wild <mwild1@gmail.com>
parents:
2407
diff
changeset
|
186 ]])); |
|
953
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 stm:execute(); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 sql.commit(); |
|
2c38d7d8b332
mod_log_messages_sql: Fork of mod_mam_sql without the protocol bits
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 end |
