Class: IrcCat::HttpServer

Inherits:
Object
  • Object
show all
Defined in:
lib/irc_cat/http_server.rb

Defined Under Namespace

Classes: BadRequest, Unauthorized

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, config) ⇒ HttpServer

Returns a new instance of HttpServer.



10
11
12
# File 'lib/irc_cat/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/irc_cat/http_server.rb', line 6

def self.run(bot, config)
  new(bot, config).run
end

Instance Method Details

#appObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/irc_cat/http_server.rb', line 29

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_passObject



51
52
53
# File 'lib/irc_cat/http_server.rb', line 51

def auth_pass
  @config["pass"]
end

#auth_userObject



47
48
49
# File 'lib/irc_cat/http_server.rb', line 47

def auth_user
  @config["user"]
end

#call(env) ⇒ Object



66
67
68
69
70
71
72
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
# File 'lib/irc_cat/http_server.rb', line 66

def call(env)
  request = Rack::Request.new(env)

  case request.path_info
  when "/"
    [200, {"Content-Type" => "text/plain"}, "OK"]
  when "/topic"
    message = request.params["message"]
    if channel = request.params["channel"]
      @bot.join_channel("##{channel}", request.params["key"])
      @bot.topic("##{channel}", message)
    else
      raise "Need a channel"
    end

    [200, {"Content-Type" => "text/plain"}, "OK"]
  when "/send"
    raise "Send support is not enabled" unless @config["send"]

    message = request.params["message"]
    if channel = request.params["channel"]
      @bot.join_channel("##{channel}", request.params["key"])
      @bot.say("##{channel}", message)
    elsif nick = request.params["nick"]
      @bot.say(nick, message)
    else
      raise "Unknown 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]
      author = commit['author']['name']
      @bot.announce "#{topic} - #{ref} - #{author}"
    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.message}"
  e.backtrace.each do |b|
    puts "- #{b}"
  end
  [503, {}, "ERR"]
end

#handler_nameObject



25
26
27
# File 'lib/irc_cat/http_server.rb', line 25

def handler_name
  @config["server"] || "mongrel"
end

#requires_auth?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
# File 'lib/irc_cat/http_server.rb', line 55

def requires_auth?
  if auth_user
    if auth_pass
      if auth_pass.empty?
        raise "Empty HTTP password in config"
      end
      true
    end
  end
end

#runObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/irc_cat/http_server.rb', line 14

def run
  host, port = @config['host'], @config['port']
  if handler = Rack::Handler.get(handler_name)
    Thread.new {
      handler.run(app, :Host => host, :Port => port)
    }
  else
    raise "Could not find a valid Rack handler for #{handler_name.inspect}"
  end
end