comparison mod_ogp/mod_ogp.lua @ 4482:21698b960bd6

mod_ogp: Add the ability to detect and process multiple URLs in a body
author JC Brand <jc@opkode.com>
date Tue, 02 Mar 2021 12:04:14 +0100
parents dbfda7f5522d
children c4f11a4b5ac7
comparison
equal deleted inserted replaced
4481:e453eaf1589e 4482:21698b960bd6
3 local st = require "util.stanza" 3 local st = require "util.stanza"
4 local url_pattern = [[https?://%S+]] 4 local url_pattern = [[https?://%S+]]
5 local xmlns_fasten = "urn:xmpp:fasten:0"; 5 local xmlns_fasten = "urn:xmpp:fasten:0";
6 local xmlns_xhtml = "http://www.w3.org/1999/xhtml"; 6 local xmlns_xhtml = "http://www.w3.org/1999/xhtml";
7 7
8 local function ogp_handler(event)
9 local room, stanza = event.room, st.clone(event.stanza)
10 local body = stanza:get_child_text("body")
11 if not body then return; end
12 8
13 local url = body:match(url_pattern) 9 local function fetch_ogp_data(room, url, origin_id)
14 if not url then return; end 10 if not url then return; end
15
16 local origin_id = stanza:find("{urn:xmpp:sid:0}origin-id@id")
17 if not origin_id then return; end
18 11
19 http.request( 12 http.request(
20 url, 13 url,
21 nil, 14 nil,
22 function(response_body, response_code, _) 15 function(response_body, response_code, _)
69 module:log("info", tostring(fastening)) 62 module:log("info", tostring(fastening))
70 end 63 end
71 ) 64 )
72 end 65 end
73 66
67 local function ogp_handler(event)
68 local room, stanza = event.room, st.clone(event.stanza)
69 local body = stanza:get_child_text("body")
70
71 if not body then return; end
72
73 local origin_id = stanza:find("{urn:xmpp:sid:0}origin-id@id")
74 if not origin_id then return; end
75
76 for url in body:gmatch(url_pattern) do
77 fetch_ogp_data(room, url, origin_id);
78 end
79 end
80
74 module:hook("muc-occupant-groupchat", ogp_handler) 81 module:hook("muc-occupant-groupchat", ogp_handler)
75 82
76 83
77 module:hook("muc-message-is-historic", function (event) 84 module:hook("muc-message-is-historic", function (event)
78 local fastening = event.stanza:get_child('apply-to', xmlns_fasten) 85 local fastening = event.stanza:get_child('apply-to', xmlns_fasten)