Mercurial > prosody-modules
comparison mod_pastebin/mod_pastebin.lua @ 3028:ded630a87563
mod_pastebin: Replace tags using :maptags API instead of hacky direct manipulation
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sun, 20 May 2018 17:49:00 +0200 |
| parents | ce34cbc10b5b |
| children | 7878dc2dbf59 |
comparison
equal
deleted
inserted
replaced
| 3027:ce34cbc10b5b | 3028:ded630a87563 |
|---|---|
| 69 --module:log("debug", "Received request, replying: %s", pastes[pasteid].text); | 69 --module:log("debug", "Received request, replying: %s", pastes[pasteid].text); |
| 70 | 70 |
| 71 return pastes[pasteid]; | 71 return pastes[pasteid]; |
| 72 end | 72 end |
| 73 | 73 |
| 74 local function replace_tag(s, replacement) | |
| 75 local once = false; | |
| 76 s:maptags(function (tag) | |
| 77 if tag.name == replacement.name and tag.attr.xmlns == replacement.attr.xmlns then | |
| 78 if not once then | |
| 79 once = true; | |
| 80 return replacement; | |
| 81 else | |
| 82 return nil; | |
| 83 end | |
| 84 end | |
| 85 return tag; | |
| 86 end); | |
| 87 if not once then | |
| 88 s:add_child(replacement); | |
| 89 end | |
| 90 end | |
| 91 | |
| 74 function check_message(data) | 92 function check_message(data) |
| 75 local origin, stanza = data.origin, data.stanza; | 93 local origin, stanza = data.origin, data.stanza; |
| 76 | 94 |
| 77 -- Only check for MUC presence when loaded on a component. | 95 -- Only check for MUC presence when loaded on a component. |
| 78 if is_component then | 96 if is_component then |
| 81 | 99 |
| 82 local nick = room._jid_nick[stanza.attr.from]; | 100 local nick = room._jid_nick[stanza.attr.from]; |
| 83 if not nick then return; end | 101 if not nick then return; end |
| 84 end | 102 end |
| 85 | 103 |
| 86 local body, bodyindex, htmlindex; | 104 local body = stanza:get_child_text(); |
| 87 for k,v in ipairs(stanza) do | |
| 88 if v.name == "body" then | |
| 89 body, bodyindex = v, k; | |
| 90 elseif v.name == "html" and v.attr.xmlns == xmlns_xhtmlim then | |
| 91 htmlindex = k; | |
| 92 end | |
| 93 end | |
| 94 | 105 |
| 95 if not body then return; end | 106 if not body then return; end |
| 96 body = body:get_text(); | |
| 97 | 107 |
| 98 --module:log("debug", "Body(%s) length: %d", type(body), #(body or "")); | 108 --module:log("debug", "Body(%s) length: %d", type(body), #(body or "")); |
| 99 | 109 |
| 100 if body and ( | 110 if body and ( |
| 101 ((#body > length_threshold) | 111 ((#body > length_threshold) |
| 110 module:log("debug", "Pasted message as %s", url); | 120 module:log("debug", "Pasted message as %s", url); |
| 111 --module:log("debug", " stanza[bodyindex] = %q", tostring( stanza[bodyindex])); | 121 --module:log("debug", " stanza[bodyindex] = %q", tostring( stanza[bodyindex])); |
| 112 local summary = (body:sub(1, max_summary_length):gsub(utf8_pattern, drop_invalid_utf8) or ""):match("[^\n]+") or ""; | 122 local summary = (body:sub(1, max_summary_length):gsub(utf8_pattern, drop_invalid_utf8) or ""):match("[^\n]+") or ""; |
| 113 summary = summary:match("^%s*(.-)%s*$"); | 123 summary = summary:match("^%s*(.-)%s*$"); |
| 114 local summary_prefixed = summary:match("[,:]$"); | 124 local summary_prefixed = summary:match("[,:]$"); |
| 115 stanza[bodyindex][1] = (summary_prefixed and (summary.." ") or "")..url; | 125 replace_tag(stanza, st.stanza("body"):text(summary)); |
| 116 | 126 |
| 117 if html_preview then | 127 if html_preview then |
| 118 local line_count = select(2, body:gsub("\n", "%0")) + 1; | 128 local line_count = select(2, body:gsub("\n", "%0")) + 1; |
| 119 local link_text = ("[view %spaste (%d line%s)]"):format(summary_prefixed and "" or "rest of ", line_count, line_count == 1 and "" or "s"); | 129 local link_text = ("[view %spaste (%d line%s)]"):format(summary_prefixed and "" or "rest of ", line_count, line_count == 1 and "" or "s"); |
| 120 local html = st.stanza("html", { xmlns = xmlns_xhtmlim }):tag("body", { xmlns = xmlns_xhtml }); | 130 local html = st.stanza("html", { xmlns = xmlns_xhtmlim }):tag("body", { xmlns = xmlns_xhtml }); |
| 121 html:tag("p"):text(summary.." "):up(); | 131 html:tag("p"):text(summary.." "):up(); |
| 122 html:tag("a", { href = url }):text(link_text):up(); | 132 html:tag("a", { href = url }):text(link_text):up(); |
| 123 stanza[htmlindex or #stanza+1] = html; | 133 replace_tag(stanza, html); |
| 124 end | 134 end |
| 125 end | 135 end |
| 126 end | 136 end |
| 127 | 137 |
| 128 module:hook("message/bare", check_message); | 138 module:hook("message/bare", check_message); |
