Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
63 lines (57 sloc) 2.63 KB
local lume = require("lume")
local http = require("http")
local irc = require("irc")
local json = require("json")
local server_port = process.env["PORT"] or 5000
local server = process.env["IRC_HOST"] or "irc.freenode.net"
local nick = assert(process.env["IRC_NICK"], "No IRC_NICK set.")
local channel = assert(process.env["IRC_CHANNEL"], "No IRC_CHANNEL set.")
local conn = irc:new(server, nick, {auto_connect=true, auto_join={channel}})
local say = function(...) conn:say(channel, string.format(...)) end
conn:on("connecting", function() print("Connecting to "..conn.server.."...") end)
conn:on("connect", function(_, server, nick) print("Connected to "..server.." as "..nick) end)
conn:on("connecterror", function(reason) print("Could not connect to "..conn.server..": "..reason) end)
conn:on("disconnect", function(reason) print("Disconnected from "..conn.server..": "..reason) end)
local responses = {
push = function(json, attrs)
for _, c in ipairs(json.commits) do
say("Commit by %s | %s | %s", c.author.name, c.message, c.url)
end
end,
tag_push = function(json, attrs)
say("Tagged %s as %s", json.after, json.ref)
end,
issue = function(json, attrs)
say("Issue %s by %s: %s | %s",
attrs.action, json.user.username, attrs.title, attrs.url)
end,
merge_request = function(json, attrs)
say("Merge request %s by %s: %s | %s",
attrs.action, json.user.username, attrs.title, attrs.url)
end,
note = function(json, attrs)
say("%s comment by %s: %s | %s",
attrs.noteable_type, json.user.username, attrs.note, attrs.url)
end,
}
http.createServer(function(req, res)
local chunks = {}
req:on('data', function (chunk, len) table.insert(chunks, chunk) end)
req:on('end', function ()
local body = table.concat(chunks)
local json, pos, err = json.decode(body)
if(not conn.connected) then
res:writeHead(200, {["Content-Type"] = "text/plain",})
res:finish("Oops; not connected yet.")
print("Oops; not connected yet.")
elseif(not responses[json.object_kind]) then
res:writeHead(200, {["Content-Type"] = "text/plain",})
res:finish("Oops; unknown object_kind: " .. json.object_kind)
print("Oops; unknown object_kind: " .. json.object_kind)
else
responses[json.object_kind](json, json.object_attributes)
res:writeHead(200, {["Content-Type"] = "text/plain",})
res:finish("OK")
end
end)
end):listen(server_port)