Mercurial > prosody-modules
annotate mod_ogp/mod_ogp.lua @ 4309:e8b9228b5265
mod_rest: Optimize stanza to JSON mapping
From O(#field_mappings ^ #s.tags) to O(#s.tags)
Haven't actually benchmarked...
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 16 Dec 2020 22:07:09 +0100 |
parents | 38da10e4b593 |
children | dbfda7f5522d |
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 = "" | |
4255
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
33 |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
34 local meta_pattern = [[<meta (.-)/?>]] |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
35 for match in response_body:gmatch(meta_pattern) do |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
36 local property = match:match([[property=%s*["']?(og:.-)["']?%s]]) |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
37 if not property then |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
38 property = match:match([[property=["']?(og:.-)["']$]]) |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
39 end |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
40 |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
41 local content = match:match([[content=%s*["'](.-)["']%s]]) |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
42 if not content then |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
43 content = match:match([[content=["']?(.-)["']$]]) |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
44 end |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
45 if not content then |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
46 content = match:match([[content=(.-) property]]) |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
47 end |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
48 if not content then |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
49 content = match:match([[content=(.-)$]]) |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
50 end |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
51 |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
52 if property and content then |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
53 module:log("info", property .. "\t" .. content) |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
54 fastening:tag( |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
55 "meta", |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
56 { |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
57 xmlns = "http://www.w3.org/1999/xhtml", |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
58 property = property, |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
59 content = content |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
60 } |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
61 ):up() |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
62 found_metadata = true |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
63 message_body = message_body .. property .. "\t" .. content .. "\n" |
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
64 end |
4252 | 65 end |
4255
38da10e4b593
mod_ogp: Update parsing logic to catch more cases
Seve Ferrer <seve@delape.net>
parents:
4254
diff
changeset
|
66 |
4252 | 67 |
68 if found_metadata then | |
69 mod_muc.get_room_from_jid(room.jid):broadcast_message(fastening) | |
70 end | |
71 module:log("info", tostring(fastening)) | |
72 end | |
73 ) | |
74 end | |
75 | |
76 module:hook("muc-occupant-groupchat", ogp_handler) |