root/lang/ruby/chokan/branches/citrus/plugins/http.rb @ 6404

Revision 6398, 3.1 kB (checked in by cho45, 5 years ago)

lang/ruby/chokan/branches/citrus/plugins/http/default.rb,
lang/ruby/chokan/branches/citrus/plugins/http.rb:

テストのアップデート。image_size は gem になってたのでコメント追記

  • Property svn:executable set to *
Line 
1#!/usr/bin/env ruby
2
3
4require "pathname"
5require "uri"
6
7class HTTP < Citrus::Plugin
8
9        attr_reader :handlers
10
11        def initialize(*args)
12                super
13
14                @handlers_dir = Pathname.new(@core.config.general["plugin_dir"]) + "http"
15                Pathname.glob(@handlers_dir + "*.rb") do |f|
16                        eval(f.read)
17                end
18
19                plugin = self
20                Handler.__send__(:define_method, :log) do |*args|
21                        plugin.log(*args)
22                end
23
24                @handlers = Handler.handlers.map do |h|
25                        name = h.name.sub(/^.+::/, "")
26                        h.new(self)
27                end
28
29                @handlers << "Default"
30        end
31
32        def on_privmsg(prefix, channel, message)
33                message.scan(URI.regexp(["http"])).each do
34                        uri = Regexp.last_match.to_s
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(config)
72                        @config = config
73                end
74
75                def process(uri)
76                end
77        end
78end
79
80tests 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                        } })
128                end
129
130                it "should reply correctly" do
131                        Thread.abort_on_exception = true
132                        Thread.critical = true
133                        @socket.string = ""
134                        @plugin.on_privmsg(@prefix, "#test", "http://foo/")
135                        Thread.pass
136                        @socket.string.should == "NOTICE #test :foo foo\r\n"
137
138
139                        @socket.string = ""
140                        @plugin.on_privmsg(@prefix, "#test", "http://bar/")
141                        Thread.pass
142                        @socket.string.should == "NOTICE #test :bar bar\r\n"
143                        Thread.critical = false
144
145                        @socket.string = ""
146                        @plugin.on_privmsg(@prefix, "#test", "http://example.com/")
147                        true while @socket.string.empty?
148                        @socket.string.should == "NOTICE #test :Example Web Page [text/html; charset=UTF-8]\r\n"
149
150                        @socket.string = ""
151                        @plugin.on_privmsg(@prefix, "#test", "http://localhost/")
152                        true while @socket.string.empty?
153                        @socket.string.should == "NOTICE #test :#<Net::HTTP::Paranoid::NotAllowedHostError: localhost is blocked by Paranoid because it is LAN.>\r\n"
154                end
155        end
156
157end
158
Note: See TracBrowser for help on using the browser.