annotate mod_pastebin/mod_pastebin.lua @ 5:9c1c6c5344dc

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