Mercurial > prosody-modules
annotate mod_pastebin/mod_pastebin.lua @ 50:a96d3f37d845
mod_muclogging: with http_server part for viewing
author | Thilo Cestonaro <thilo@cestona.ro> |
---|---|
date | Sun, 18 Oct 2009 22:39:36 +0200 |
parents | ea59a8d98b03 |
children | 3c18c2d03bc2 |
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 = {}; |
17 | |
18 local xmlns_xhtmlim = "http://jabber.org/protocol/xhtml-im"; | |
19 local xmlns_xhtml = "http://www.w3.org/1999/xhtml"; | |
20 | |
21 local function pastebin_message(text) | |
22 local uuid = uuid_new(); | |
23 pastes[uuid] = { text = text, 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
|
24 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
|
25 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
|
26 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
|
27 end |
5 | 28 return base_url..uuid; |
29 end | |
30 | |
31 function handle_request(method, body, request) | |
32 local pasteid = request.url.path:match("[^/]+$"); | |
33 if not pasteid or not pastes[pasteid] then | |
34 return "Invalid paste id, perhaps it expired?"; | |
35 end | |
36 | |
37 --module:log("debug", "Received request, replying: %s", pastes[pasteid].text); | |
38 | |
39 return pastes[pasteid].text; | |
40 end | |
41 | |
42 function check_message(data) | |
43 local origin, stanza = data.origin, data.stanza; | |
44 | |
45 local body, bodyindex, htmlindex; | |
46 for k,v in ipairs(stanza) do | |
47 if v.name == "body" then | |
48 body, bodyindex = v, k; | |
49 elseif v.name == "html" and v.attr.xmlns == xmlns_xhtml then | |
50 htmlindex = k; | |
51 end | |
52 end | |
53 | |
54 if not body then return; end | |
55 body = body:get_text(); | |
56 | |
25
ea59a8d98b03
mod_pastebin: Comment some debug logging on every message
Matthew Wild <mwild1@gmail.com>
parents:
24
diff
changeset
|
57 --module:log("debug", "Body(%s) length: %d", type(body), #(body or "")); |
5 | 58 |
12
316e8437f233
mod_pastebin: Allow configurable message length threshold
Matthew Wild <mwild1@gmail.com>
parents:
5
diff
changeset
|
59 if body and #body > length_threshold then |
5 | 60 local url = pastebin_message(body); |
61 module:log("debug", "Pasted message as %s", url); | |
62 --module:log("debug", " stanza[bodyindex] = %q", tostring( stanza[bodyindex])); | |
63 stanza[bodyindex][1] = url; | |
64 local html = st.stanza("html", { xmlns = xmlns_xhtmlim }):tag("body", { xmlns = xmlns_xhtml }); | |
65 html:tag("p"):text(body:sub(1,150)):up(); | |
66 html:tag("a", { href = url }):text("[...]"):up(); | |
67 stanza[htmlindex or #stanza+1] = html; | |
68 end | |
69 end | |
70 | |
71 module:hook("message/bare", check_message); | |
72 | |
24
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
73 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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 end |
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 |
72bcc0475e2f
mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
84 |
5 | 85 local ports = config.get(module.host, "core", "pastebin_ports") or { 5280 }; |
86 for _, options in ipairs(ports) do | |
87 local port, base, ssl, interface = 5280, "pastebin", false, nil; | |
88 if type(options) == "number" then | |
89 port = options; | |
90 elseif type(options) == "table" then | |
91 port, base, ssl, interface = options.port or 5280, options.path or "pastebin", options.ssl or false, options.interface; | |
92 elseif type(options) == "string" then | |
93 base = options; | |
94 end | |
95 | |
96 base_url = base_url or ("http://"..module:get_host()..(port ~= 80 and (":"..port) or "").."/"..base.."/"); | |
97 | |
98 httpserver.new{ port = port, base = base, handler = handle_request, ssl = ssl } | |
99 end |