Mercurial > prosody-modules
annotate mod_ogp/mod_ogp.lua @ 4254:a4e182d7ff0a
mod_ogp: Improve parsing patterns
| author | Seve Ferrer <seve@delape.net> |
|---|---|
| date | Wed, 18 Nov 2020 11:16:11 +0100 |
| parents | 1327e1e1c94e |
| children | 38da10e4b593 |
| rev | line source |
|---|---|
| 4252 | 1 local mod_muc = module:depends("muc") |
| 2 local http = require "net.http" | |
| 3 local st = require "util.stanza" | |
| 4 | |
|
4254
a4e182d7ff0a
mod_ogp: Improve parsing patterns
Seve Ferrer <seve@delape.net>
parents:
4252
diff
changeset
|
5 local ogp_pattern = [[<meta property=["']?(og:.-)["']? content=%s*["']?(.-)["']?%s-/?>]] |
|
a4e182d7ff0a
mod_ogp: Improve parsing patterns
Seve Ferrer <seve@delape.net>
parents:
4252
diff
changeset
|
6 local ogp_pattern2 = [[<meta content=%s*["']?(.-)["']? property=["']?(og:.-)["']?%s-/?>]] |
| 4252 | 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) |
