Class: IrcCat::HttpServer

Inherits:
Object
  • Object
show all
Defined in:
lib/irccat/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/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

#appObject



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_passObject



58
59
60
# File 'lib/irccat/http_server.rb', line 58

def auth_pass
  @config["pass"]
end

#auth_userObject



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"
    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"]

    if @config['message_param']
      message = request.params[@config['message_param']]
    else
      message = request.params["message"]
    end

    message.gsub!(/\s+/,' ')

    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)
    elsif channels = @config['channels']
      channels.each do |channel|
        @bot.say(channel, message)
      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]
      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



32
33
34
# File 'lib/irccat/http_server.rb', line 32

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

#requires_auth?Boolean

Returns:

  • (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

#runObject



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.message
        puts e.backtace
        sleep 1
        retry
      end
    }
  else
    raise "Could not find a valid Rack handler for #{handler_name.inspect}"
  end
end