Skip to content

Commit

Permalink
Switched Send and Github class into different files. Now can disable …
Browse files Browse the repository at this point in the history
…http Send, removed http index
  • Loading branch information
hrefhref committed Feb 17, 2008
1 parent 9ece269 commit 36ea4ff
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
3 changes: 3 additions & 0 deletions config.yml
Expand Up @@ -11,7 +11,10 @@ http:
enabled: true
host: 0.0.0.0
port: '3489'
# Enable post-receive stuff from Github
github: true
# Allow send with http
send: true

# TCP Server

Expand Down
36 changes: 3 additions & 33 deletions lib/irc_cat/http_server.rb
@@ -1,43 +1,13 @@
# The HTTP Server

require 'mongrel'
require 'json'
require 'json/add/rails'
require 'lib/irc_cat/http_server/send' if @config['http']['send'] == true
require 'lib/irc_cat/http_server/github' if @config['http']['github'] == true

class Index < Mongrel::HttpHandler
def process(request, response)
response.start(200) do |head,out|
head["Content-Type"] = "text/html"
out.write("<strong>Welcome to irc_cat</strong><br /><br />This irc_cat is ready to use:) Just send a GET to : <code>/send/your message</code>.<br /><br />byekthx")
end
end
end

class Send < Mongrel::HttpHandler

def initialize(bot, config); @bot = bot; @config = config; end

def process(request, response)
response.start(200) do |head,out|
head["Content-Type"] = "text/plain"
message = CGI::unescape("#{request.params['PATH_INFO'].gsub('/','')}")
@bot.say(@config['irc']['channel'],"#{message}")
end
end
end

class Github < Mongrel::HttpHandler

def initialize(bot, config); @bot = bot; @config = config; puts "Github enabled"; end

def process(request, response)
response.start(200) do |head,out|
head["Content-Type"] = "text/plain"
post = CGI.parse(request.body.read)
json = JSON.parse(post['payload'].join(','))
json['commits'].each do |c|
@bot.say(@config['irc']['channel'],"[#{json['repository']['name']}] New commit by #{c.last['author']['name']} : #{c.last['message'].gsub(/\n(.*)/, '...')} - #{c.last['url']}")
end
end
end
end
Expand All @@ -50,7 +20,7 @@ def initialize(bot, config, ip, port)
puts "Starting HTTP (#{ip}:#{port})"
h = Mongrel::HttpServer.new(ip, port)
h.register("/", Index.new)
h.register("/send", Send.new(@bot, @config))
h.register("/send", Send.new(@bot, @config)) if @config['http']['send'] == true
h.register("/github", Github.new(@bot, @config)) if @config['http']['github'] == true
h.run.join
end
Expand Down
18 changes: 18 additions & 0 deletions lib/irc_cat/http_server/github.rb
@@ -0,0 +1,18 @@
require 'json'
require 'json/add/rails'

class Github < Mongrel::HttpHandler

def initialize(bot, config); @bot = bot; @config = config; puts "Github enabled"; end

def process(request, response)
response.start(200) do |head,out|
head["Content-Type"] = "text/plain"
post = CGI.parse(request.body.read)
json = JSON.parse(post['payload'].join(','))
json['commits'].each do |c|
@bot.say(@config['irc']['channel'],"[#{json['repository']['name']}] New commit by #{c.last['author']['name']} : #{c.last['message'].gsub(/\n(.*)/, '...')} - #{c.last['url']}")
end
end
end
end
12 changes: 12 additions & 0 deletions lib/irc_cat/http_server/send.rb
@@ -0,0 +1,12 @@
class Send < Mongrel::HttpHandler

def initialize(bot, config); @bot = bot; @config = config; end

def process(request, response)
response.start(200) do |head,out|
head["Content-Type"] = "text/plain"
message = CGI::unescape("#{request.params['PATH_INFO'].gsub('/','')}")
@bot.say(@config['irc']['channel'],"#{message}")
end
end
end

0 comments on commit 36ea4ff

Please sign in to comment.