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