Comparison

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