Mercurial > prosody-modules
annotate mod_pastebin/mod_pastebin.lua @ 153:31e24026e4fd
mod_tcpproxy: Handle gracefully invalid base64 data, fixes #2 (thanks dersd)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 19 May 2010 14:02:19 +0100 |
parents | 1fc4e8dc66a6 |
children | b51741b7e86d |
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; |
24
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
6 local t_insert, t_remove = table.insert, table.remove; |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
7 local add_task = require "util.timer".add_task; |
5 | 8 |
23
92b1e6592d36
mod_pastebin: Allow per-host pastebin_threshold
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
9 local length_threshold = config.get(module.host, "core", "pastebin_threshold") or 500; |
5 | 10 |
21
4f18696f043a
mod_pastebin: Small fix to read the pastebin URL from the config
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
11 local base_url = config.get(module.host, "core", "pastebin_url"); |
5 | 12 |
24
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
13 -- Seconds a paste should live for in seconds (config is in hours), default 24 hours |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
14 local expire_after = math.floor((config.get(module.host, "core", "pastebin_expire_after") or 24) * 3600); |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
15 |
5 | 16 local pastes = {}; |
76
1fc4e8dc66a6
mod_pastebin: Send Content-Type header to specify plain UTF-8 text
Matthew Wild <mwild1@gmail.com>
parents:
75
diff
changeset
|
17 local default_headers = { ["Content-Type"] = "text/plain; charset=utf-8" }; |
5 | 18 |
19 local xmlns_xhtmlim = "http://jabber.org/protocol/xhtml-im"; | |
20 local xmlns_xhtml = "http://www.w3.org/1999/xhtml"; | |
21 | |
75
3c7189e26848
mod_pastebin: Rename pastebin_message() to pastebin_text() and make it global so it can be called by other plugins
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
22 function pastebin_text(text) |
5 | 23 local uuid = uuid_new(); |
76
1fc4e8dc66a6
mod_pastebin: Send Content-Type header to specify plain UTF-8 text
Matthew Wild <mwild1@gmail.com>
parents:
75
diff
changeset
|
24 pastes[uuid] = { body = text, time = os_time(), headers = default_headers }; |
24
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
25 pastes[#pastes+1] = uuid; |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
26 if not pastes[2] then -- No other pastes, give the timer a kick |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
27 add_task(expire_after, expire_pastes); |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
28 end |
5 | 29 return base_url..uuid; |
30 end | |
31 | |
32 function handle_request(method, body, request) | |
33 local pasteid = request.url.path:match("[^/]+$"); | |
34 if not pasteid or not pastes[pasteid] then | |
35 return "Invalid paste id, perhaps it expired?"; | |
36 end | |
37 | |
38 --module:log("debug", "Received request, replying: %s", pastes[pasteid].text); | |
39 | |
76
1fc4e8dc66a6
mod_pastebin: Send Content-Type header to specify plain UTF-8 text
Matthew Wild <mwild1@gmail.com>
parents:
75
diff
changeset
|
40 return pastes[pasteid]; |
5 | 41 end |
42 | |
43 function check_message(data) | |
44 local origin, stanza = data.origin, data.stanza; | |
45 | |
46 local body, bodyindex, htmlindex; | |
47 for k,v in ipairs(stanza) do | |
48 if v.name == "body" then | |
49 body, bodyindex = v, k; | |
71
3c18c2d03bc2
mod_pastebin: Fix finding of XHTML content.
Paul Aurich <paul@darkrain42.org>
parents:
25
diff
changeset
|
50 elseif v.name == "html" and v.attr.xmlns == xmlns_xhtmlim then |
5 | 51 htmlindex = k; |
52 end | |
53 end | |
54 | |
55 if not body then return; end | |
56 body = body:get_text(); | |
57 | |
25
ea59a8d98b03
mod_pastebin: Comment some debug logging on every message
Matthew Wild <mwild1@gmail.com>
parents:
24
diff
changeset
|
58 --module:log("debug", "Body(%s) length: %d", type(body), #(body or "")); |
5 | 59 |
12
316e8437f233
mod_pastebin: Allow configurable message length threshold
Matthew Wild <mwild1@gmail.com>
parents:
5
diff
changeset
|
60 if body and #body > length_threshold then |
75
3c7189e26848
mod_pastebin: Rename pastebin_message() to pastebin_text() and make it global so it can be called by other plugins
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
61 local url = pastebin_text(body); |
5 | 62 module:log("debug", "Pasted message as %s", url); |
63 --module:log("debug", " stanza[bodyindex] = %q", tostring( stanza[bodyindex])); | |
64 stanza[bodyindex][1] = url; | |
65 local html = st.stanza("html", { xmlns = xmlns_xhtmlim }):tag("body", { xmlns = xmlns_xhtml }); | |
66 html:tag("p"):text(body:sub(1,150)):up(); | |
67 html:tag("a", { href = url }):text("[...]"):up(); | |
68 stanza[htmlindex or #stanza+1] = html; | |
69 end | |
70 end | |
71 | |
72 module:hook("message/bare", check_message); | |
73 | |
24
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
74 function expire_pastes(time) |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
75 time = time or os_time(); -- COMPAT with 0.5 |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
76 if pastes[1] then |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
77 pastes[pastes[1]] = nil; |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
78 t_remove(pastes, 1); |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
79 if pastes[1] then |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
80 return (expire_after - (time - pastes[pastes[1]].time)) + 1; |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
81 end |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
82 end |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
83 end |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
84 |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
85 |
5 | 86 local ports = config.get(module.host, "core", "pastebin_ports") or { 5280 }; |
87 for _, options in ipairs(ports) do | |
88 local port, base, ssl, interface = 5280, "pastebin", false, nil; | |
89 if type(options) == "number" then | |
90 port = options; | |
91 elseif type(options) == "table" then | |
92 port, base, ssl, interface = options.port or 5280, options.path or "pastebin", options.ssl or false, options.interface; | |
93 elseif type(options) == "string" then | |
94 base = options; | |
95 end | |
96 | |
97 base_url = base_url or ("http://"..module:get_host()..(port ~= 80 and (":"..port) or "").."/"..base.."/"); | |
98 | |
99 httpserver.new{ port = port, base = base, handler = handle_request, ssl = ssl } | |
100 end |