Comparison

mod_ogp/mod_ogp.lua @ 4252:1327e1e1c94e

mod_ogp: Add module
author Seve Ferrer <seve@delape.net>
date Sun, 15 Nov 2020 19:07:34 +0100
child 4254:a4e182d7ff0a
comparison
equal deleted inserted replaced
4251:d33b480befcb 4252:1327e1e1c94e
1 local mod_muc = module:depends("muc")
2 local http = require "net.http"
3 local st = require "util.stanza"
4
5 local ogp_pattern = [[<meta property=["'](og:.-)["'] content=["'](.-)["'].->]]
6 local ogp_pattern2 = [[<meta content=["'](.-)["'] property=["'](og:.-)["'].->]]
7 local url_pattern = [[https?://%S+]]
8
9 local function ogp_handler(event)
10 local room, stanza = event.room, st.clone(event.stanza)
11 local body = stanza:get_child_text("body")
12 if not body then return; end
13
14 local url = body:match(url_pattern)
15 if not url then return; end
16
17 local origin_id = stanza:find("{urn:xmpp:sid:0}origin-id@id")
18 if not origin_id then return; end
19
20 http.request(
21 url,
22 nil,
23 function(response_body, response_code, _)
24 if response_code ~= 200 then
25 return
26 end
27
28 local to = room.jid
29 local from = room and room.jid or module.host
30 local fastening = st.message({to = to, from = from}):tag("apply-to", {xmlns = "urn:xmpp:fasten:0", id = origin_id})
31 local found_metadata = false
32 local message_body = ""
33 for property, content in response_body:gmatch(ogp_pattern) do
34 module:log("info", property .. "\t" .. content)
35 fastening:tag(
36 "meta",
37 {
38 xmlns = "http://www.w3.org/1999/xhtml",
39 property = property,
40 content = content
41 }
42 ):up()
43 found_metadata = true
44 message_body = message_body .. property .. "\t" .. content .. "\n"
45 end
46 for content, property in response_body:gmatch(ogp_pattern2) do
47 module:log("info", property .. "\t" .. content)
48 fastening:tag(
49 "meta",
50 {
51 xmlns = "http://www.w3.org/1999/xhtml",
52 property = property,
53 content = content
54 }
55 ):up()
56 found_metadata = true
57 message_body = message_body .. property .. "\t" .. content .. "\n"
58 end
59
60 if found_metadata then
61 mod_muc.get_room_from_jid(room.jid):broadcast_message(fastening)
62 end
63 module:log("info", tostring(fastening))
64 end
65 )
66 end
67
68 module:hook("muc-occupant-groupchat", ogp_handler)