root/lang/ruby/citrus/trunk/plugins/system.rb

Revision 28104, 4.3 kB (checked in by cho45, 14 months ago)

config の設定されたやつからの invite はうけるように

Line 
1
2class System < Citrus::Plugin
3        def initialize(*args)
4                super
5
6                if @config['operator'].kind_of?(String)
7                        @config['operator'] = Regexp.new(@config['operator'])
8                end
9        end
10
11        def on_invite(prefix, nick, channel)
12                return unless @config['operator'] === prefix
13                join(channel)
14        end
15
16        def on_privmsg(prefix, channel, message)
17                return unless @config['operator'] === prefix
18                case message
19                when /^reload(?:\s+([a-z]+))?$/i
20                        log "#{prefix}: call reloading"
21                        begin
22                                @core.reload_config
23                        rescue => e
24                                log e.message
25                        end
26                        name = Regexp.last_match[1]
27                        log name
28                        if name
29                                begin
30                                        instances = [@core.reload_plugin(name)]
31                                rescue Citrus::Plugins::UnknownPlugin => e
32                                        notice channel, e.message
33                                        instances = []
34                                end
35                        else
36                                instances = @core.reload_plugins.values
37                        end
38                        if instances.empty?
39                                notice channel, "No plugins to reload."
40                        else
41                                reloaded = instances.map {|i|
42                                        i.class.name.sub(/^.+::/, "")
43                                }.join(" ")
44                                notice channel, "Reloaded: " + reloaded
45                        end
46
47                when /^chokan: join to (\S+)(?: (\S+))?/
48                        chan, pass = Regexp.last_match.captures
49                        log "Joining to '#{chan}' with '#{pass}'"
50                        join(chan.to_s, pass.to_s)
51
52                when "chokan: part"
53                        part(channel, "lambda....")
54
55                when "operator?"
56                        notice channel, "You are an operator for me."
57
58                when "Gem.clear_paths"
59                        r = Gem.clear_paths
60                        notice channel, "Gem.clear_paths #{r.inspect}"
61                end
62        end
63end
64
65tests do
66
67        describe System do
68                before do
69                        @core     = DummyCore.new({
70                                "plugins" => { "Foo" => nil, "Bar" => nil }
71                        })
72                        @pdir = Pathname.new(@core.config.general["plugin_dir"])
73
74                        %w(Foo Bar).each do |name|
75                                (@pdir + "#{name.downcase}.rb").open("w") do |f|
76                                        f << <<-EOS.unindent
77                                                require "thread"
78                                                class #{name}
79                                                        include Net::IRC
80                                                        include Constants
81
82                                                        attr_reader :config
83
84                                                        def initialize(core, config)
85                                                                @core, @config = core, config[self.class.name.sub(/.+::/, "")] || {}
86                                                                @messages = {}
87                                                        end
88
89                                                        def method_missing(method, *args)
90                                                                @messages[method] = args
91                                                        end
92
93                                                        def m
94                                                                @messages
95                                                        end
96                                                end
97                                        EOS
98                                end
99                        end
100
101                        @socket   = @core.socket
102                        @prefix   = Net::IRC::Prefix.new("foo!foo@localhost")
103                        @prefixop = Net::IRC::Prefix.new("foo!bar@localhost")
104
105                        @plugin = System.new(@core, { "System" => {
106                                "operator" => "foo!bar@localhost",
107                        } })
108
109                        @core.init_plugins
110                end
111
112                it "should response to operator" do
113                        @socket.clear
114                        @plugin.on_privmsg(@prefix, "#test", "operator?")
115                        @socket.should be_empty
116
117                        @socket.clear
118                        @plugin.on_privmsg(@prefixop, "#test", "operator?")
119                        @socket.pop.to_s.should == "NOTICE #test :You are an operator for me.\r\n"
120
121                        @plugin = System.new(@core, { "System" => {
122                                "operator" => "foo!bar@.+",
123                        } })
124
125                        @socket.clear
126                        @plugin.on_privmsg(@prefixop, "#test", "operator?")
127                        @socket.pop.to_s.should == "NOTICE #test :You are an operator for me.\r\n"
128
129                        @plugin = System.new(@core, { "System" => {
130                                "operator" => /foo!bar@.+/,
131                        } })
132
133                        @socket.clear
134                        @plugin.on_privmsg(@prefixop, "#test", "operator?")
135                        @socket.pop.to_s.should == "NOTICE #test :You are an operator for me.\r\n"
136                end
137
138                it "can reload_plugins" do
139                        @socket.clear
140                        @plugin.on_privmsg(@prefixop, "#test", "reload")
141                        @socket.pop.to_s.should match(/^NOTICE #test /)
142
143                        @socket.clear
144                        @plugin.on_privmsg(@prefixop, "#test", "reload Foo")
145                        @socket.pop.to_s.should match(/^NOTICE #test /)
146
147                        @socket.clear
148                        @plugin.on_privmsg(@prefixop, "#test", "reload Unknown")
149                        @socket.pop.to_s.should match(/^NOTICE #test /)
150
151                        def @core.reload_config
152                                raise "config error"
153                        end
154
155                        @socket.clear
156                        @plugin.on_privmsg(@prefixop, "#test", "reload")
157                        @socket.pop.to_s.should match(/^NOTICE #test /)
158                end
159
160                it "can operate join/part" do
161                        @socket.clear
162                        @plugin.on_privmsg(@prefixop, "#test", "chokan: part")
163                        @socket.pop.to_s.should match(/^PART #test /)
164
165                        @socket.clear
166                        @plugin.on_privmsg(@prefixop, "#test", "chokan: join to #foobar")
167                        @socket.pop.to_s.should match(/^JOIN #foobar /)
168
169                        @socket.clear
170                        @plugin.on_privmsg(@prefixop, "#test", "chokan: join to #foobar password")
171                        @socket.pop.to_s.should match(/^JOIN #foobar password/)
172                end
173
174                it "can Gem.clear_paths" do
175                        @socket.clear
176                        @plugin.on_privmsg(@prefixop, "#test", "Gem.clear_paths")
177                        @socket.pop.to_s.should match(/^NOTICE #test :Gem.clear_paths/)
178                end
179        end
180
181end
182
Note: See TracBrowser for help on using the browser.