Mercurial > prosody-modules
annotate mod_pastebin/mod_pastebin.lua @ 23:92b1e6592d36
mod_pastebin: Allow per-host pastebin_threshold
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 02 Oct 2009 13:52:37 +0100 |
parents | 4f18696f043a |
children | 72bcc0475e2f |
rev | line source |
---|---|
5 | 1 |
2 local st = require "util.stanza"; | |
3 local httpserver = require "net.httpserver"; | |
4 local uuid_new = require "util.uuid".generate; | |
12
316e8437f233
mod_pastebin: Allow configurable message length threshold
Matthew Wild <mwild1@gmail.com>
parents:
5
diff
changeset
|
5 local os_time = os.time; |
5 | 6 |
23
92b1e6592d36
mod_pastebin: Allow per-host pastebin_threshold
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
7 local length_threshold = config.get(module.host, "core", "pastebin_threshold") or 500; |
5 | 8 |
21
4f18696f043a
mod_pastebin: Small fix to read the pastebin URL from the config
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
9 local base_url = config.get(module.host, "core", "pastebin_url"); |
5 | 10 |
11 local pastes = {}; | |
12 | |
13 local xmlns_xhtmlim = "http://jabber.org/protocol/xhtml-im"; | |
14 local xmlns_xhtml = "http://www.w3.org/1999/xhtml"; | |
15 | |
16 local function pastebin_message(text) | |
17 local uuid = uuid_new(); | |
18 pastes[uuid] = { text = text, time = os_time() }; | |
19 return base_url..uuid; | |
20 end | |
21 | |
22 function handle_request(method, body, request) | |
23 local pasteid = request.url.path:match("[^/]+$"); | |
24 if not pasteid or not pastes[pasteid] then | |
25 return "Invalid paste id, perhaps it expired?"; | |
26 end | |
27 | |
28 --module:log("debug", "Received request, replying: %s", pastes[pasteid].text); | |
29 | |
30 return pastes[pasteid].text; | |
31 end | |
32 | |
33 function check_message(data) | |
34 local origin, stanza = data.origin, data.stanza; | |
35 | |
36 local body, bodyindex, htmlindex; | |
37 for k,v in ipairs(stanza) do | |
38 if v.name == "body" then | |
39 body, bodyindex = v, k; | |
40 elseif v.name == "html" and v.attr.xmlns == xmlns_xhtml then | |
41 htmlindex = k; | |
42 end | |
43 end | |
44 | |
45 if not body then return; end | |
46 body = body:get_text(); | |
47 | |
48 module:log("debug", "Body(%s) length: %d", type(body), #(body or "")); | |
49 | |
12
316e8437f233
mod_pastebin: Allow configurable message length threshold
Matthew Wild <mwild1@gmail.com>
parents:
5
diff
changeset
|
50 if body and #body > length_threshold then |
5 | 51 local url = pastebin_message(body); |
52 module:log("debug", "Pasted message as %s", url); | |
53 --module:log("debug", " stanza[bodyindex] = %q", tostring( stanza[bodyindex])); | |
54 stanza[bodyindex][1] = url; | |
55 local html = st.stanza("html", { xmlns = xmlns_xhtmlim }):tag("body", { xmlns = xmlns_xhtml }); | |
56 html:tag("p"):text(body:sub(1,150)):up(); | |
57 html:tag("a", { href = url }):text("[...]"):up(); | |
58 stanza[htmlindex or #stanza+1] = html; | |
59 end | |
60 end | |
61 | |
62 module:hook("message/bare", check_message); | |
63 | |
64 local ports = config.get(module.host, "core", "pastebin_ports") or { 5280 }; | |
65 for _, options in ipairs(ports) do | |
66 local port, base, ssl, interface = 5280, "pastebin", false, nil; | |
67 if type(options) == "number" then | |
68 port = options; | |
69 elseif type(options) == "table" then | |
70 port, base, ssl, interface = options.port or 5280, options.path or "pastebin", options.ssl or false, options.interface; | |
71 elseif type(options) == "string" then | |
72 base = options; | |
73 end | |
74 | |
75 base_url = base_url or ("http://"..module:get_host()..(port ~= 80 and (":"..port) or "").."/"..base.."/"); | |
76 | |
77 httpserver.new{ port = port, base = base, handler = handle_request, ssl = ssl } | |
78 end |