Class: IrcCat::HttpServer
- Inherits:
-
Object
- Object
- IrcCat::HttpServer
- Defined in:
- lib/irccat/http_server.rb
Defined Under Namespace
Classes: BadRequest, Unauthorized
Class Method Summary collapse
Instance Method Summary collapse
- #app ⇒ Object
- #auth_pass ⇒ Object
- #auth_user ⇒ Object
- #call(env) ⇒ Object
- #handler_name ⇒ Object
-
#initialize(bot, config) ⇒ HttpServer
constructor
A new instance of HttpServer.
- #requires_auth? ⇒ Boolean
- #run ⇒ Object
Constructor Details
#initialize(bot, config) ⇒ HttpServer
Returns a new instance of HttpServer.
10 11 12 |
# File 'lib/irccat/http_server.rb', line 10 def initialize(bot, config) @bot, @config = bot, config end |
Class Method Details
.run(bot, config) ⇒ Object
6 7 8 |
# File 'lib/irccat/http_server.rb', line 6 def self.run(bot, config) new(bot, config).run end |
Instance Method Details
#app ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/irccat/http_server.rb', line 36 def app endpoint = self builder = Rack::Builder.new if requires_auth? builder.use Rack::Auth::Basic, "irccat" do |user,pass| auth_user == user && auth_pass == pass end end if prefix = @config["prefix"] builder.map prefix do run endpoint end else builder.run endpoint end builder end |
#auth_pass ⇒ Object
58 59 60 |
# File 'lib/irccat/http_server.rb', line 58 def auth_pass @config["pass"] end |
#auth_user ⇒ Object
54 55 56 |
# File 'lib/irccat/http_server.rb', line 54 def auth_user @config["user"] end |
#call(env) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/irccat/http_server.rb', line 73 def call(env) request = Rack::Request.new(env) case request.path_info when "/" [200, {"Content-Type" => "text/plain"}, "OK"] when "/topic" = request.params["message"] if channel = request.params["channel"] @bot.join_channel("##{channel}", request.params["key"]) @bot.topic("##{channel}", ) else raise "Need a channel" end [200, {"Content-Type" => "text/plain"}, "OK"] when "/send" raise "Send support is not enabled" unless @config["send"] if @config['message_param'] = request.params[@config['message_param']] else = request.params["message"] end .gsub!(/\s+/,' ') if channel = request.params["channel"] @bot.join_channel("##{channel}", request.params["key"]) @bot.say("##{channel}", ) elsif nick = request.params["nick"] @bot.say(nick, ) elsif channels = @config['channels'] channels.each do |channel| @bot.say(channel, ) end else raise "Unkown action" end [200, {"Content-Type" => "text/plain"}, "OK"] when "/github" raise "GitHub support is not enabled" unless @config["github"] data = JSON.parse(request.POST['payload']) repository = data['repository']['name'] data['commits'].each do |commit| topic = commit['message'].split("\n").first ref = commit['id'][0,7] = commit['author']['name'] @bot.announce "#{topic} - #{ref} - #{}" end [200, {"Content-Type" => "text/plain"}, "OK"] else [404, {}, ""] end rescue Unauthorized [401, {'WWW-Authenticate' => %(Basic realm="IrcCat")}, 'Authorization Required'] rescue BadRequest [400, {}, 'Bad Request'] rescue Exception => e puts "Got an error: #{e.class}, #{e.}" e.backtrace.each do |b| puts "- #{b}" end [503, {}, "ERR"] end |
#handler_name ⇒ Object
32 33 34 |
# File 'lib/irccat/http_server.rb', line 32 def handler_name @config["server"] || "mongrel" end |
#requires_auth? ⇒ Boolean
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/irccat/http_server.rb', line 62 def requires_auth? if auth_user if auth_pass if auth_pass.empty? raise "Empty HTTP password in config" end true end end end |
#run ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/irccat/http_server.rb', line 14 def run host, port = @config['host'], @config['port'] if handler = Rack::Handler.get(handler_name) Thread.new { begin handler.run(app, :Host => host, :Port => port) rescue Exception => e puts e. puts e.backtace sleep 1 retry end } else raise "Could not find a valid Rack handler for #{handler_name.inspect}" end end |