annotate mod_muc_cache_media/mod_muc_cache_media.lua @ 6418:98e06fc5680f

mod_muc_cache_media: relative link is better
author Stephen Paul Weber <singpolyma@singpolyma.net>
date Tue, 24 Feb 2026 13:20:45 -0500
parents dcc8d4af8d4f
children 233691533318
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
6414
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
76 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
77 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
78 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
79
6414
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
80 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
81 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
82 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
83
6414
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
84 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
85 http.request(url,{
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
86 method = "GET";
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
87 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
88 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
89 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
90 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
91 end;
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
92 }, function(_, code, response)
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
93 if code ~= 200 then
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
94 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
95 os.remove(tmpname)
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
96 reject("bad status")
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
97 return
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
98 end
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
99 if file then
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
100 file:write(response.body)
48fca0914fcf mod_muc_cache_media: simplify code with target_filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6412
diff changeset
101 file:close()
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 resolve({ tmpname, response.headers["content-type"] })
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
104 end)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
105 end)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
106 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
107
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
108 module:hook("muc-broadcast-message", function(event)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
109 local stanza = event.stanza
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
110 if stanza.attr.type ~= "groupchat" then return 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 local url_map = {}
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
113
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
114 -- Collect OOB URLs
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
115 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
116 local url_tag = oob:get_child("url")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
117 if url_tag then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
118 url_map[url_tag:get_text()] = {}
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
119 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
120 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
121
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
122 -- Collect SIMS URLs
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
123 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
124 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
125 local sources = ms:get_child("sources")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
126 if sources then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
127 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
128 local uri = sref.attr.uri
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
129 if uri then url_map[uri] = {} end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
130 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
131 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
132 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
133 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
134
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
135 -- Cache each URL
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
136 for original_url,_ in pairs(url_map) do
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
137 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
138 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
139 if not err and tmp_path then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
140 local sha2 = sha256_file(tmp_path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
141 local sha3 = sha3_256_file(tmp_path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
142 local size = file_size(tmp_path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
143 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
144 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
145 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
146 local final_path = storage_path.."/"..final_name
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
147 if not lfs.attributes(final_path) then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
148 os.rename(tmp_path, final_path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
149 else
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
150 lfs.touch(final_path)
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
151 os.remove(tmp_path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
152 end
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
153 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
154 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
155 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
156 end
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
157 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
158 else
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
159 os.remove(tmp_path)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
160 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
161 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
162 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
163
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
164 -- Rewrite URLs and enrich/create SIMS
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
165 local original_body = stanza:get_child_text("body")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
166 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
167 if data.local_url then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
168
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
169 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
170 local url_tag = oob:get_child("url")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
171 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
172 oob:maptags(function(el)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
173 if el.name == "url" then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
174 return st.stanza("url"):text(data.local_url)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
175 else
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
176 return el
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
177 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
178 end)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
179 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
180 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
181
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
182 stanza:maptags(function(el)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
183 if el.name == "body" then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
184 return st.stanza("body"):text(el:get_text():gsub(original_url, data.local_url))
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
185 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
186 return el:maptags(function(subel)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
187 local s, e = string.find(original_body, original_url)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
188 if subel.name == "body" and math.abs(s - tonumber(subel.attr.start)) < 5 then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
189 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
190 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
191 return subel
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
192 end)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
193 else
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
194 return el
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
195 end
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
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
198 local found = false
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
199 -- Enrich existing SIMS references
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
200 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
201 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
202 local sources = ms:get_child("sources")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
203 if sources then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
204 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
205 if sref.attr.uri == original_url then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
206 found = true
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
207 sref.attr.uri = data.local_url
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
208 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
209 if not file_tag then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
210 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
211 ms:add_child(file_tag)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
212 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
213 file_tag:maptags(function(el)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
214 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
215 return nil
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
216 else
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
217 return el
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
218 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
219 end)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
220 -- Fill name
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
221 if not file_tag:get_child("name") then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
222 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
223 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
224 -- Fill size
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
225 if not file_tag:get_child("size") then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
226 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
227 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
228 -- Fill SHA256 hash
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
229 local sha2_tag = nil
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
230 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
231 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
232 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
233 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
234 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
235 end
6417
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
236 -- 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
237 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
238 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
239 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
240 end
dcc8d4af8d4f mod_muc_cache_media: move to storage that matches RFC 6920
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6415
diff changeset
241 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
242 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
243 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
244 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
245 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
246 end
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 -- Create new SIMS if none existed
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
250 if not found then
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
251 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
252 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
253 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
254 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
255 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
256 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
257 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
258 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
259 ms:add_child(file_tag)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
260 local sources = st.stanza("sources")
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
261 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
262 ms:add_child(sources)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
263 ref:add_child(ms)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
264 stanza:add_child(ref)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
265 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
266 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
267 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
268 end, 100)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
269
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
270 local archive = module:open_store("muc_log", "archive");
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
271
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
272 module:hook("muc-moderate-message", function(event)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
273 local room = event.room
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
274 local stanza_id = event.stanza_id
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
275 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
276
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
277 local room_node = jid.split(room.jid);
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
278
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
279 -- Find message by stanza_id
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
280 local query = {
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
281 with = "message<groupchat";
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
282 ids = { stanza_id };
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
283 }
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
284
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
285 local items, err = archive:find(room_node, query)
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
286 if not items or err then return end
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 for id, stanza, when in items do
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
289 -- Extract SIMS hashes
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
290 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
291 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
292 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
293 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
294 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
295 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
296 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
297 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
298 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
299 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
300 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
301 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
302 "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
303 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
304 )
6412
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
305 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
306 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
307 end
c25b98d42ed0 mod_muc_cache_media: Initial commit
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff changeset
308 end
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)