| 1 | |
|---|
| 2 | require "pathname" |
|---|
| 3 | require "uri" |
|---|
| 4 | |
|---|
| 5 | class HTTP < Citrus::Plugin |
|---|
| 6 | |
|---|
| 7 | attr_reader :handlers |
|---|
| 8 | attr_reader :config |
|---|
| 9 | |
|---|
| 10 | def initialize(*args) |
|---|
| 11 | super |
|---|
| 12 | |
|---|
| 13 | @handlers_dir = Pathname.new(@core.config.general["plugin_dir"]) + "http" |
|---|
| 14 | Pathname.glob(@handlers_dir + "*.rb") do |f| |
|---|
| 15 | eval(f.read, nil, f) |
|---|
| 16 | end |
|---|
| 17 | |
|---|
| 18 | plugin = self |
|---|
| 19 | Handler.__send__(:define_method, :log) do |*args| |
|---|
| 20 | plugin.log(*args) |
|---|
| 21 | end |
|---|
| 22 | |
|---|
| 23 | @handlers = Handler.handlers.map do |h| |
|---|
| 24 | name = h.name.sub(/^.+::/, "") |
|---|
| 25 | h.new(self) |
|---|
| 26 | end |
|---|
| 27 | @handlers.sort! do |a,b| |
|---|
| 28 | 0 |
|---|
| 29 | 1 if a.class.to_s.include? 'Default' |
|---|
| 30 | end |
|---|
| 31 | end |
|---|
| 32 | |
|---|
| 33 | def on_privmsg(prefix, channel, message) |
|---|
| 34 | URI.extract(message, %w[http]) do |uri| |
|---|
| 35 | Thread.start(channel, URI(uri)) do |chan, u| |
|---|
| 36 | begin |
|---|
| 37 | response(chan, u) |
|---|
| 38 | rescue Exception => e |
|---|
| 39 | post NOTICE, chan, e.inspect |
|---|
| 40 | log e.inspect |
|---|
| 41 | e.backtrace.each do |l| |
|---|
| 42 | log l |
|---|
| 43 | end |
|---|
| 44 | end |
|---|
| 45 | end |
|---|
| 46 | end |
|---|
| 47 | end |
|---|
| 48 | |
|---|
| 49 | def response(chan, uri) |
|---|
| 50 | ret = nil |
|---|
| 51 | @handlers.each do |handler| |
|---|
| 52 | ret = handler.process(uri) |
|---|
| 53 | break if ret |
|---|
| 54 | end |
|---|
| 55 | if ret |
|---|
| 56 | post NOTICE, chan, ret |
|---|
| 57 | end |
|---|
| 58 | end |
|---|
| 59 | |
|---|
| 60 | class Handler |
|---|
| 61 | @@handlers = [] |
|---|
| 62 | |
|---|
| 63 | def self.handlers |
|---|
| 64 | @@handlers |
|---|
| 65 | end |
|---|
| 66 | |
|---|
| 67 | def self.inherited(subclass) |
|---|
| 68 | @@handlers << subclass |
|---|
| 69 | end |
|---|
| 70 | |
|---|
| 71 | def initialize(parent) |
|---|
| 72 | @parent = parent |
|---|
| 73 | end |
|---|
| 74 | |
|---|
| 75 | def process(uri) |
|---|
| 76 | end |
|---|
| 77 | end |
|---|
| 78 | end |
|---|
| 79 | |
|---|
| 80 | tests do |
|---|
| 81 | |
|---|
| 82 | describe HTTP do |
|---|
| 83 | before :all do |
|---|
| 84 | @core = DummyCore.new({}) |
|---|
| 85 | @socket = @core.socket |
|---|
| 86 | @prefix = Net::IRC::Prefix.new("foo!foo@localhsot") |
|---|
| 87 | @handlers_dir = Pathname.tempname + "http" |
|---|
| 88 | @handlers_dir.mkpath |
|---|
| 89 | |
|---|
| 90 | (@handlers_dir + "foo.rb").open("w") do |f| |
|---|
| 91 | f.puts <<-EOF |
|---|
| 92 | class Foo < Handler |
|---|
| 93 | |
|---|
| 94 | def process(uri) |
|---|
| 95 | return unless uri.host == "foo" |
|---|
| 96 | |
|---|
| 97 | "foo foo" |
|---|
| 98 | end |
|---|
| 99 | end |
|---|
| 100 | EOF |
|---|
| 101 | end |
|---|
| 102 | |
|---|
| 103 | (@handlers_dir + "bar.rb").open("w") do |f| |
|---|
| 104 | f.puts <<-EOF |
|---|
| 105 | class Bar < Handler |
|---|
| 106 | |
|---|
| 107 | def process(uri) |
|---|
| 108 | return unless uri.host == "bar" |
|---|
| 109 | |
|---|
| 110 | "bar bar" |
|---|
| 111 | end |
|---|
| 112 | end |
|---|
| 113 | EOF |
|---|
| 114 | end |
|---|
| 115 | |
|---|
| 116 | (@handlers_dir + "default.rb").open("w") do |f| |
|---|
| 117 | f.puts File.read("./plugins/http/default.rb") |
|---|
| 118 | end |
|---|
| 119 | |
|---|
| 120 | @core.config.general["plugin_dir"] = @handlers_dir.parent.to_s |
|---|
| 121 | |
|---|
| 122 | @plugin = HTTP.new(@core, { "HTTP" => { |
|---|
| 123 | "handlers" => [ |
|---|
| 124 | { "class" => "Foo" }, |
|---|
| 125 | { "class" => "Bar" }, |
|---|
| 126 | ], |
|---|
| 127 | "whitelist" => [ "localhost" ], |
|---|
| 128 | } }) |
|---|
| 129 | end |
|---|
| 130 | |
|---|
| 131 | it "should reply correctly" do |
|---|
| 132 | @socket.clear |
|---|
| 133 | @plugin.on_privmsg(@prefix, "#test", "http://foo/") |
|---|
| 134 | @socket.pop.to_s.should == "NOTICE #test :foo foo\r\n" |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | @socket.clear |
|---|
| 138 | @plugin.on_privmsg(@prefix, "#test", "http://bar/") |
|---|
| 139 | @socket.pop.to_s.should == "NOTICE #test :bar bar\r\n" |
|---|
| 140 | |
|---|
| 141 | @socket.clear |
|---|
| 142 | @plugin.on_privmsg(@prefix, "#test", "http://example.com/") |
|---|
| 143 | @socket.pop.to_s.should == "NOTICE #test :Example Web Page [text/html; charset=UTF-8]\r\n" |
|---|
| 144 | |
|---|
| 145 | @socket.clear |
|---|
| 146 | @plugin.on_privmsg(@prefix, "#test", "http://www.google.co.jp/intl/ja/about.html") |
|---|
| 147 | @socket.pop.to_s.should == "NOTICE #test :Google について [text/html]\r\n" |
|---|
| 148 | |
|---|
| 149 | @socket.clear |
|---|
| 150 | @plugin.on_privmsg(@prefix, "#test", "http://192.168.0.1/") |
|---|
| 151 | @socket.pop.to_s.should == "NOTICE #test :#<Net::HTTP::Paranoid::NotAllowedHostError: 192.168.0.1 is not allowed host>\r\n" |
|---|
| 152 | end |
|---|
| 153 | end |
|---|
| 154 | |
|---|
| 155 | end |
|---|
| 156 | |
|---|