comparison mod_ogp/mod_ogp.lua @ 4459:dbfda7f5522d

mod_ogp: Make sure OGP fasten messages get archived - By adding a type of "groupchat" - By adding a hook for `muc-message-is-historic`
author JC Brand <jc@opkode.com>
date Mon, 22 Feb 2021 17:37:50 +0100
parents 38da10e4b593
children 21698b960bd6
comparison
equal deleted inserted replaced
4458:5234d0c8883d 4459:dbfda7f5522d
1 local mod_muc = module:depends("muc") 1 local mod_muc = module:depends("muc")
2 local http = require "net.http" 2 local http = require "net.http"
3 local st = require "util.stanza" 3 local st = require "util.stanza"
4
5 local ogp_pattern = [[<meta property=["']?(og:.-)["']? content=%s*["']?(.-)["']?%s-/?>]]
6 local ogp_pattern2 = [[<meta content=%s*["']?(.-)["']? property=["']?(og:.-)["']?%s-/?>]]
7 local url_pattern = [[https?://%S+]] 4 local url_pattern = [[https?://%S+]]
5 local xmlns_fasten = "urn:xmpp:fasten:0";
6 local xmlns_xhtml = "http://www.w3.org/1999/xhtml";
8 7
9 local function ogp_handler(event) 8 local function ogp_handler(event)
10 local room, stanza = event.room, st.clone(event.stanza) 9 local room, stanza = event.room, st.clone(event.stanza)
11 local body = stanza:get_child_text("body") 10 local body = stanza:get_child_text("body")
12 if not body then return; end 11 if not body then return; end
25 return 24 return
26 end 25 end
27 26
28 local to = room.jid 27 local to = room.jid
29 local from = room and room.jid or module.host 28 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}) 29 local fastening = st.message({to = to, from = from, type = 'groupchat'}):tag("apply-to", {xmlns = xmlns_fasten, id = origin_id})
31 local found_metadata = false 30 local found_metadata = false
32 local message_body = "" 31 local message_body = ""
33 32
34 local meta_pattern = [[<meta (.-)/?>]] 33 local meta_pattern = [[<meta (.-)/?>]]
35 for match in response_body:gmatch(meta_pattern) do 34 for match in response_body:gmatch(meta_pattern) do
52 if property and content then 51 if property and content then
53 module:log("info", property .. "\t" .. content) 52 module:log("info", property .. "\t" .. content)
54 fastening:tag( 53 fastening:tag(
55 "meta", 54 "meta",
56 { 55 {
57 xmlns = "http://www.w3.org/1999/xhtml", 56 xmlns = xmlns_xhtml,
58 property = property, 57 property = property,
59 content = content 58 content = content
60 } 59 }
61 ):up() 60 ):up()
62 found_metadata = true 61 found_metadata = true
63 message_body = message_body .. property .. "\t" .. content .. "\n" 62 message_body = message_body .. property .. "\t" .. content .. "\n"
64 end 63 end
65 end 64 end
66
67 65
68 if found_metadata then 66 if found_metadata then
69 mod_muc.get_room_from_jid(room.jid):broadcast_message(fastening) 67 mod_muc.get_room_from_jid(room.jid):broadcast_message(fastening)
70 end 68 end
71 module:log("info", tostring(fastening)) 69 module:log("info", tostring(fastening))
72 end 70 end
73 ) 71 )
74 end 72 end
75 73
76 module:hook("muc-occupant-groupchat", ogp_handler) 74 module:hook("muc-occupant-groupchat", ogp_handler)
75
76
77 module:hook("muc-message-is-historic", function (event)
78 local fastening = event.stanza:get_child('apply-to', xmlns_fasten)
79 if fastening and fastening:get_child('meta', xmlns_xhtml) then
80 return true
81 end
82 end);