annotate mod_pubsub_googlecode/mod_pubsub_googlecode.lua @ 1345:c60e9943dcb9

Fix problem handling form input
author Luis G.F <luisgf@gmail.com>
date Thu, 13 Mar 2014 14:58:47 +0000
parents 7dbde05b48a9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
946
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 module:depends("http");
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local st = require "util.stanza";
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local json = require "util.json";
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local formdecode = require "net.http".formdecode;
1109
d988f2db9773 mod_pubsub_googlecode: Import hmac from util.hashes
Kim Alvefur <zash@zash.se>
parents: 951
diff changeset
6 local hmac_md5 = require "util.hashes".hmac_md5;
946
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local st = require "util.stanza";
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local json = require "util.json";
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local datetime = require "util.datetime".datetime;
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local pubsub_service = module:depends("pubsub").service;
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local node = module:get_option_string("googlecode_node", "googlecode");
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local auth_key = module:get_option_string("googlecode_auth_key");
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 if not auth_key then
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 module:log("warn", "Specify googlecode_auth_key to prevent commit spoofing!");
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 function handle_POST(event)
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local request = event.request;
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local body = request.body;
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1109
diff changeset
24
946
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 if auth_key then
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local digest_header = request.headers["google-code-project-hosting-hook-hmac"];
1109
d988f2db9773 mod_pubsub_googlecode: Import hmac from util.hashes
Kim Alvefur <zash@zash.se>
parents: 951
diff changeset
27 local digest = hmac_md5(auth_key, body, true);
946
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 if digest ~= digest_header then
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 module:log("warn", "Commit POST failed authentication check, sender gave %s, we got %s, body was:\n%s", tostring(digest_header), tostring(digest), tostring(body));
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 return "No thanks.";
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1109
diff changeset
33
946
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local data = json.decode(body);
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1109
diff changeset
35
946
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local project = data.project_name or "somewhere";
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 for _, rev in ipairs(data.revisions) do
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 if rev.url:match("^http://wiki.") then
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local what;
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 for _, page in ipairs(rev.added) do
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 what = page:match("^/(.-)%.wiki");
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if what then break; end
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 if not what then
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 for _, page in ipairs(rev.modified) do
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 what = page:match("^/(.-)%.wiki");
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if what then break; end
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 rev.message = "wiki ("..(what or "unknown page").."): "..rev.message;
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1109
diff changeset
52
951
ef54ae817689 mod_pubsub_googlecode: Split name and email when possible
Matthew Wild <mwild1@gmail.com>
parents: 948
diff changeset
53 local name = rev.author;
ef54ae817689 mod_pubsub_googlecode: Split name and email when possible
Matthew Wild <mwild1@gmail.com>
parents: 948
diff changeset
54 local email = name:match("<([^>]+)>$");
ef54ae817689 mod_pubsub_googlecode: Split name and email when possible
Matthew Wild <mwild1@gmail.com>
parents: 948
diff changeset
55 if email then
ef54ae817689 mod_pubsub_googlecode: Split name and email when possible
Matthew Wild <mwild1@gmail.com>
parents: 948
diff changeset
56 name = name:gsub("%s*<[^>]+>$", "");
ef54ae817689 mod_pubsub_googlecode: Split name and email when possible
Matthew Wild <mwild1@gmail.com>
parents: 948
diff changeset
57 end
946
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local ok, err = pubsub_service:publish(node, true, project,
948
79b4a1db7a57 mod_pubsub_googlecode: Remove quotes around 'project'... it's a variable :)
Matthew Wild <mwild1@gmail.com>
parents: 946
diff changeset
60 st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = project })
946
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 :tag("entry", { xmlns = "http://www.w3.org/2005/Atom" })
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 :tag("id"):text(tostring(rev.revision)):up()
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 :tag("title"):text(rev.message):up()
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 :tag("link", { rel = "alternate", href = rev.url }):up()
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 :tag("published"):text(datetime(rev.timestamp)):up()
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 :tag("author")
951
ef54ae817689 mod_pubsub_googlecode: Split name and email when possible
Matthew Wild <mwild1@gmail.com>
parents: 948
diff changeset
67 :tag("name"):text(name):up()
ef54ae817689 mod_pubsub_googlecode: Split name and email when possible
Matthew Wild <mwild1@gmail.com>
parents: 948
diff changeset
68 :tag("email"):text(email):up()
946
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 :up()
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 );
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 module:log("debug", "Handled POST: \n%s\n", tostring(body));
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 return "Thank you Google!";
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 module:provides("http", {
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 route = {
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 POST = handle_POST;
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 };
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 });
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 function module.load()
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 if not pubsub_service.nodes[node] then
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 local ok, err = pubsub_service:create(node, true);
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 if not ok then
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 module:log("error", "Error creating node: %s", err);
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 else
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 module:log("debug", "Node %q created", node);
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
2c5430ff1c11 mod_pubsub_googlecode: Module to receive post-commit webhook requests from Google Code Hosting
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 end