annotate mod_muc_cache_media/mod_muc_cache_media.lua @ 6513:5fb466693e85

mod_storage_xmlarchive: Ensure list index files are removed using os.remove() on the list files leaves the new index files behind since util.datamanager does not know they are removed. Thanks Link Mauve
author Kim Alvefur <zash@zash.se>
date Tue, 07 Apr 2026 21:10:54 +0200
parents 0a9b516e388e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
1 local http = require "net.http"
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
2 local resolver = require "net.resolvers.basic"
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
3 local socket_url = require "socket.url"
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
4 local st = require "util.stanza"
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
5 local async = require "util.async"
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
6 local promise = require "util.promise"
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
7 local mime = require "mime"
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
8 local lfs = require "lfs"
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
9 local hashes = require "util.hashes"
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
10 local jid = require "util.jid"
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
11
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
12 local storage_path = module:get_option_string("muc_media_store_path", "/var/lib/prosody/muc-media")
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
13 local public_base_url = module:get_option_string("muc_media_public_base")
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
14 local max_size = module:get_option_number("muc_media_max_size", 10 * 1024 * 1024)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
15
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
16 lfs.mkdir(storage_path)
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
17 lfs.mkdir(storage_path.."/sha-256")
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
18 lfs.mkdir(storage_path.."/sha3-256")
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
19
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
20 local function ipv4_to_number(ip)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
21 local a,b,c,d = ip:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
22 if not a then return nil end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
23 return (tonumber(a) << 24) | (tonumber(b) << 16) | (tonumber(c) << 8) | tonumber(d)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
24 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
25
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
26 local function ipv4_in_range(ip, base, mask) return (ip & mask) == base end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
27
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
28 local function is_private_ipv4(ip)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
29 local n = ipv4_to_number(ip)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
30 if not n then return false end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
31 if ipv4_in_range(n, 0x0A000000, 0xFF000000) then return true end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
32 if ipv4_in_range(n, 0xAC100000, 0xFFF00000) then return true end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
33 if ipv4_in_range(n, 0xC0A80000, 0xFFFF0000) then return true end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
34 if ipv4_in_range(n, 0x7F000000, 0xFF000000) then return true end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
35 if ipv4_in_range(n, 0xA9FE0000, 0xFFFF0000) then return true end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
36 if ipv4_in_range(n, 0x00000000, 0xFF000000) then return true end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
37 if ipv4_in_range(n, 0xE0000000, 0xF0000000) then return true end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
38 return false
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
39 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
40
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
41 local function is_private_ipv6(ip)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
42 ip = ip:lower()
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
43 if ip == "::1" then return true end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
44 if ip:match("^fc") or ip:match("^fd") then return true end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
45 if ip:match("^fe8") or ip:match("^fe9") or ip:match("^fea") or ip:match("^feb") then return true end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
46 if ip == "::" then return true end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
47 return false
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
48 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
49
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
50 local function sha256_file(path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
51 local f = io.open(path,"rb")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
52 if not f then return nil end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
53 -- TODO: incremental hash
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
54 local digest = hashes.sha256(f:read("*all"))
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
55 f:close()
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
56 return mime.b64(digest)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
57 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
58
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
59 local function sha3_256_file(path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
60 local f = io.open(path,"rb")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
61 if not f then return nil end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
62 -- TODO: incremental hash
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
63 local digest = hashes.sha3_256(f:read("*all"))
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
64 f:close()
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
65 return mime.b64(digest)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
66 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
67
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
68 local function file_size(path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
69 return lfs.attributes(path,"size") or 0
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
70 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
71
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
72 local function filename_from_url(url)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
73 return url:match(".*/([^/?]+)$") or "file"
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
74 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
75
6419
233691533318 mod_muc_cache_media: use raw strings not patterns
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6418
diff changeset
76 local function pattern_from_string(s)
233691533318 mod_muc_cache_media: use raw strings not patterns
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6418
diff changeset
77 return s:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", function(c) return "%" .. c end)
233691533318 mod_muc_cache_media: use raw strings not patterns
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6418
diff changeset
78 end
233691533318 mod_muc_cache_media: use raw strings not patterns
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6418
diff changeset
79
6414
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
80 local function secure_download_to_disk(url)
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
81 local parsed = socket_url.parse(url)
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
82 if not parsed or (parsed.scheme~="http" and parsed.scheme~="https") then return promise.reject("invalid scheme") end
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
83
6414
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
84 local tmpname = storage_path.."/tmp_"..tostring(math.random(1e9))
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
85 local file = io.open(tmpname,"wb")
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
86 if not file then return promise.reject("cannot open temp file") end
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
87
6414
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
88 return promise.new(function(resolve, reject)
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
89 http.request(url,{
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
90 method = "GET";
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
91 body_size_limit = max_size;
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
92 streaming_handler = function(r) return file, function() file:close(); file = nil end end;
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
93 target_filter = function(net, ip, port, extra)
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
94 return (net == "tcp4" and not is_private_ipv4(ip)) or (net == "tcp6" and not is_private_ipv6(ip))
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
95 end;
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
96 }, function(_, code, response)
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
97 if code ~= 200 then
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
98 if file then file:close() end
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
99 os.remove(tmpname)
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
100 reject("bad status")
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
101 return
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
102 end
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
103 if file then
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
104 file:write(response.body)
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
105 file:close()
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
106 end
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
107 resolve({ tmpname, response.headers["content-type"] })
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
108 end)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
109 end)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
110 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
111
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
112 module:hook("muc-broadcast-message", function(event)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
113 local stanza = event.stanza
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
114 if stanza.attr.type ~= "groupchat" then return end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
115
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
116 local url_map = {}
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
117
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
118 -- Collect OOB URLs
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
119 for oob in stanza:childtags("x","jabber:x:oob") do
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
120 local url_tag = oob:get_child("url")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
121 if url_tag then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
122 url_map[url_tag:get_text()] = {}
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
123 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
124 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
125
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
126 -- Collect SIMS URLs
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
127 for ref in stanza:childtags("reference","urn:xmpp:reference:0") do
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
128 for ms in ref:childtags("media-sharing","urn:xmpp:sims:1") do
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
129 local sources = ms:get_child("sources")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
130 if sources then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
131 for sref in sources:childtags("reference","urn:xmpp:reference:0") do
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
132 local uri = sref.attr.uri
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
133 if uri then url_map[uri] = {} end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
134 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
135 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
136 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
137 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
138
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
139 -- Cache each URL
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
140 for original_url,_ in pairs(url_map) do
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
141 local result, err = async.wait_for(secure_download_to_disk(original_url))
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
142 local tmp_path, content_type = result and result[1], result and result[2]
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
143 if not err and tmp_path then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
144 local sha2 = sha256_file(tmp_path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
145 local sha3 = sha3_256_file(tmp_path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
146 local size = file_size(tmp_path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
147 local name = filename_from_url(original_url)
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
148 if sha2 and sha3 then
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
149 local final_name = "sha3-256/"..sha3:gsub("+","-"):gsub("/","_"):gsub("=","")
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
150 local final_path = storage_path.."/"..final_name
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
151 if not lfs.attributes(final_path) then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
152 os.rename(tmp_path, final_path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
153 else
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
154 lfs.touch(final_path)
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
155 os.remove(tmp_path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
156 end
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
157 local sha2_path = storage_path.."/sha-256/"..sha2:gsub("+","-"):gsub("/","_"):gsub("=","")
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
158 if not lfs.attributes(sha2_path) then
6418
98e06fc5680f mod_muc_cache_media: relative link is better
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6417
diff changeset
159 lfs.link("../"..final_name, sha2_path, true)
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
160 end
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
161 url_map[original_url] = { local_url = public_base_url..final_name.."?ct="..content_type, path=final_path, sha2=sha2, sha3=sha3, size=size, name=name, content_type=content_type }
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
162 else
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
163 os.remove(tmp_path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
164 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
165 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
166 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
167
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
168 -- Rewrite URLs and enrich/create SIMS
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
169 local original_body = stanza:get_child_text("body")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
170 for original_url,data in pairs(url_map) do
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
171 if data.local_url then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
172
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
173 for oob in stanza:childtags("x","jabber:x:oob") do
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
174 local url_tag = oob:get_child("url")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
175 if url_tag and url_tag:get_text() == original_url then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
176 oob:maptags(function(el)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
177 if el.name == "url" then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
178 return st.stanza("url"):text(data.local_url)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
179 else
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
180 return el
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
181 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
182 end)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
183 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
184 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
185
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
186 stanza:maptags(function(el)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
187 if el.name == "body" then
6419
233691533318 mod_muc_cache_media: use raw strings not patterns
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6418
diff changeset
188 return st.stanza("body"):text(el:get_text():gsub(pattern_from_string(original_url), data.local_url))
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
189 elseif el.name == "fallback" and el.attr.xmlns == "urn:xmpp:fallback:0" and (el.attr["for"] == "jabber:x:oob" or el.attr["for"] == "urn:xmpp:sims:1") then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
190 return el:maptags(function(subel)
6419
233691533318 mod_muc_cache_media: use raw strings not patterns
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6418
diff changeset
191 local s, e = string.find(original_body, original_url, 1, true)
6491
0a9b516e388e mod_muc_cache_media: tonumber(nil) is nil
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6419
diff changeset
192 if s and subel.name == "body" and subel.attr.start and math.abs(s - tonumber(subel.attr.start)) < 5 then
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
193 subel.attr["end"] = tostring((tonumber(subel.attr["end"]) - #original_url) + #data.local_url)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
194 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
195 return subel
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
196 end)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
197 else
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
198 return el
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
199 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
200 end)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
201
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
202 local found = false
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
203 -- Enrich existing SIMS references
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
204 for ref in stanza:childtags("reference","urn:xmpp:reference:0") do
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
205 for ms in ref:childtags("media-sharing","urn:xmpp:sims:1") do
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
206 local sources = ms:get_child("sources")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
207 if sources then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
208 for sref in sources:childtags("reference","urn:xmpp:reference:0") do
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
209 if sref.attr.uri == original_url then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
210 found = true
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
211 sref.attr.uri = data.local_url
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
212 local file_tag = ms:get_child("file","urn:xmpp:jingle:apps:file-transfer:5")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
213 if not file_tag then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
214 file_tag = st.stanza("file",{ xmlns="urn:xmpp:jingle:apps:file-transfer:5" })
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
215 ms:add_child(file_tag)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
216 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
217 file_tag:maptags(function(el)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
218 if el.name == "size" or el.name == "hash" then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
219 return nil
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
220 else
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
221 return el
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
222 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
223 end)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
224 -- Fill name
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
225 if not file_tag:get_child("name") then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
226 file_tag:add_child(st.stanza("name"):text(data.name))
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
227 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
228 -- Fill size
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
229 if not file_tag:get_child("size") then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
230 file_tag:add_child(st.stanza("size"):text(tostring(data.size)))
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
231 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
232 -- Fill SHA256 hash
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
233 local sha2_tag = nil
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
234 for htag in file_tag:childtags("hash","urn:xmpp:hashes:2") do
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
235 if htag.attr.algo=="sha-256" then sha2_tag = htag end
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
236 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
237 if not sha2_tag then
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
238 file_tag:add_child(st.stanza("hash",{ xmlns="urn:xmpp:hashes:2", algo="sha-256" }):text(data.sha2))
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
239 end
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
240 -- Fill SHA3-256 hash
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
241 local sha3_tag = nil
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
242 for htag in file_tag:childtags("hash","urn:xmpp:hashes:2") do
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
243 if htag.attr.algo=="sha3-256" then sha3_tag = htag end
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
244 end
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
245 if not sha3_tag then
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
246 file_tag:add_child(st.stanza("hash",{ xmlns="urn:xmpp:hashes:2", algo="sha3-256" }):text(data.sha3))
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
247 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
248 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
249 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
250 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
251 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
252 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
253 -- Create new SIMS if none existed
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
254 if not found then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
255 local ref = st.stanza("reference",{ xmlns="urn:xmpp:reference:0", type="data" })
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
256 local ms = st.stanza("media-sharing",{ xmlns="urn:xmpp:sims:1" })
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
257 local file_tag = st.stanza("file",{ xmlns="urn:xmpp:jingle:apps:file-transfer:5" })
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
258 file_tag:add_child(st.stanza("media-type"):text(data.content_type or "application/octet-stream"))
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
259 file_tag:add_child(st.stanza("name"):text(data.name))
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
260 file_tag:add_child(st.stanza("size"):text(tostring(data.size)))
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
261 file_tag:add_child(st.stanza("hash",{ xmlns="urn:xmpp:hashes:2", algo="sha-256" }):text(data.sha2))
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
262 file_tag:add_child(st.stanza("hash",{ xmlns="urn:xmpp:hashes:2", algo="sha3-256" }):text(data.sha3))
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
263 ms:add_child(file_tag)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
264 local sources = st.stanza("sources")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
265 sources:add_child(st.stanza("reference",{ xmlns="urn:xmpp:reference:0", type="data", uri=data.local_url }))
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
266 ms:add_child(sources)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
267 ref:add_child(ms)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
268 stanza:add_child(ref)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
269 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
270 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
271 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
272 end, 100)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
273
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
274 local archive = module:open_store("muc_log", "archive");
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
275
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
276 module:hook("muc-moderate-message", function(event)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
277 local room = event.room
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
278 local stanza_id = event.stanza_id
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
279 if not room or not stanza_id then return end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
280
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
281 local room_node = jid.split(room.jid);
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
282
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
283 -- Find message by stanza_id
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
284 local query = {
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
285 with = "message<groupchat";
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
286 ids = { stanza_id };
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
287 }
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
288
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
289 local items, err = archive:find(room_node, query)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
290 if not items or err then return end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
291
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
292 for id, stanza, when in items do
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
293 -- Extract SIMS hashes
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
294 for ref in stanza:childtags("reference", "urn:xmpp:reference:0") do
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
295 for ms in ref:childtags("media-sharing", "urn:xmpp:sims:1") do
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
296 for file_tag in ms:childtags("file", "urn:xmpp:jingle:apps:file-transfer:5") do
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
297 for hash_tag in file_tag:childtags("hash", "urn:xmpp:hashes:2") do
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
298 if hash_tag.attr.algo == "sha3-256" then
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
299 local sha3 = hash_tag:get_text()
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
300 if sha3 then
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
301 local file = sha3:gsub("+","-"):gsub("/","_"):gsub("=","")
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
302 local path = storage_path .. "/sha3-256/" .. file
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
303 if lfs.attributes(path) then
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
304 os.remove(path)
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
305 module:log("debug",
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
306 "Deleted cached media %s for moderated message %s",
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
307 file, stanza_id
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
308 )
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
309 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
310 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
311 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
312 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
313 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
314 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
315 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
316 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
317 end)