ircbot 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/bin/ircbot +44 -3
- data/config/sama-zu.yml +8 -0
- data/config/wota.yml +8 -0
- data/config/yml.erb +8 -0
- data/lib/ircbot/client/config/generator.rb +65 -0
- data/lib/ircbot/client/plugins.rb +2 -1
- data/lib/ircbot/plugins.rb +14 -28
- data/lib/ircbot/version.rb +1 -1
- data/lib/ircbot.rb +1 -0
- data/plugins/irc.rb +7 -0
- data/plugins/plugins.rb +2 -2
- data/plugins/which.rb +27 -0
- data/spec/plugins_spec.rb +3 -1
- metadata +7 -2
data/Rakefile
CHANGED
@@ -29,7 +29,7 @@ spec = Gem::Specification.new do |s|
|
|
29
29
|
s.require_path = 'lib'
|
30
30
|
s.add_dependency('extlib', '>= 0.9.14')
|
31
31
|
s.add_dependency('net-irc', '>= 0.0.9')
|
32
|
-
s.files = %w(MIT-LICENSE README Rakefile) + Dir.glob("{lib,spec,plugins}/**/*")
|
32
|
+
s.files = %w(MIT-LICENSE README Rakefile) + Dir.glob("{lib,spec,plugins,config}/**/*")
|
33
33
|
end
|
34
34
|
|
35
35
|
Rake::GemPackageTask.new(spec) do |pkg|
|
data/bin/ircbot
CHANGED
@@ -4,19 +4,60 @@ require 'ircbot'
|
|
4
4
|
|
5
5
|
config = nil
|
6
6
|
plugin = Pathname(Dir.getwd) + "plugins"
|
7
|
+
create = false
|
7
8
|
|
8
9
|
while (arg = ARGV.shift)
|
9
10
|
case arg
|
10
11
|
when /^-f$/ ; config = ARGV.shift
|
11
|
-
when /^-c$/ ;
|
12
|
+
when /^-c$/ ; create = true
|
12
13
|
else
|
13
14
|
puts "invalid argument: `#{arg}'"
|
14
15
|
exit
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
config = Pathname(config.to_s)
|
20
|
+
|
21
|
+
|
22
|
+
######################################################################
|
23
|
+
### Create config file
|
24
|
+
|
25
|
+
if create
|
26
|
+
require 'ircbot/client/config/generator'
|
27
|
+
nick = config.basename(".*").to_s
|
28
|
+
generator = Ircbot::Client::Config::Generator.new(:nick => nick)
|
29
|
+
|
30
|
+
if nick.empty?
|
31
|
+
nick = generator.nick
|
32
|
+
config = Pathname("#{nick}.yml")
|
33
|
+
end
|
34
|
+
|
35
|
+
unless config.extname == ".yml"
|
36
|
+
config = Pathname(config.to_s + ".yml")
|
37
|
+
end
|
38
|
+
|
39
|
+
if config.exist?
|
40
|
+
puts "ERROR: Config file (#{config}) already exists."
|
41
|
+
puts "Delete it first, or specify another filename by -f option."
|
42
|
+
exit
|
43
|
+
end
|
44
|
+
|
45
|
+
config.open("w+") do |f|
|
46
|
+
f.print generator.execute
|
47
|
+
end
|
48
|
+
puts "Created #{config}. Run following command.\nircbot -f #{config}"
|
49
|
+
exit
|
50
|
+
end
|
51
|
+
|
52
|
+
######################################################################
|
53
|
+
### Run bot
|
54
|
+
|
55
|
+
unless config.extname == ".yml"
|
56
|
+
config = Pathname(config.to_s + ".yml")
|
57
|
+
end
|
58
|
+
|
59
|
+
unless config.exist?
|
60
|
+
raise "Specify your config file\nusage: #{$0} -f xxx.yml"
|
20
61
|
end
|
21
62
|
|
22
63
|
Ircbot.push_path(:plugin, plugin)
|
data/config/sama-zu.yml
ADDED
data/config/wota.yml
ADDED
data/config/yml.erb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
module Ircbot
|
4
|
+
class Client
|
5
|
+
class Config
|
6
|
+
class Generator
|
7
|
+
attr_reader :nick
|
8
|
+
|
9
|
+
def initialize(options = {})
|
10
|
+
@nick = options[:nick]
|
11
|
+
@code = options[:code] || read_template
|
12
|
+
|
13
|
+
@nick = current_user + "_bot" if @nick.to_s.empty?
|
14
|
+
end
|
15
|
+
|
16
|
+
def execute
|
17
|
+
require 'erb'
|
18
|
+
erb = ERB.new(@code)
|
19
|
+
erb.result(binding)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def read_template
|
24
|
+
path = Ircbot.paths[:config].first + "yml.erb"
|
25
|
+
return path.read{}
|
26
|
+
end
|
27
|
+
|
28
|
+
def current_user
|
29
|
+
require 'etc'
|
30
|
+
Etc.getlogin
|
31
|
+
rescue Exception
|
32
|
+
"unknown"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
def channels
|
36
|
+
case (val = super)
|
37
|
+
when Array
|
38
|
+
val
|
39
|
+
else
|
40
|
+
val.to_s.split
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
######################################################################
|
46
|
+
### Event
|
47
|
+
|
48
|
+
def on_rpl_welcome(m)
|
49
|
+
super
|
50
|
+
|
51
|
+
config.channels.each do |channel|
|
52
|
+
post JOIN, channel
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
######################################################################
|
57
|
+
### Command
|
58
|
+
|
59
|
+
def broadcast(text)
|
60
|
+
config.channels.each do |channel|
|
61
|
+
privmsg channel, text
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -49,7 +49,8 @@ module Ircbot
|
|
49
49
|
m.reply(self, reply) if opts[:reply]
|
50
50
|
rescue Exception => e
|
51
51
|
type = (e.class == RuntimeError) ? 'Error' : "#{e.class}"
|
52
|
-
|
52
|
+
text = (type == e.message) ? '' : e.message
|
53
|
+
m.reply(self, "#{type}: #{text}")
|
53
54
|
plugins_rescue_action type, plugin, e
|
54
55
|
end
|
55
56
|
|
data/lib/ircbot/plugins.rb
CHANGED
@@ -27,14 +27,21 @@ module Ircbot
|
|
27
27
|
######################################################################
|
28
28
|
### Accessors
|
29
29
|
|
30
|
-
def plugin!(
|
31
|
-
|
30
|
+
def plugin!(plugin)
|
31
|
+
case plugin
|
32
|
+
when String, Symbol
|
33
|
+
@plugins[plugin.to_s] or raise PluginNotFound, plugin.to_s
|
34
|
+
when Plugin
|
35
|
+
plugin
|
36
|
+
else
|
37
|
+
raise PluginNotFound, "#{plugin.class}(#{plugin})"
|
38
|
+
end
|
32
39
|
end
|
40
|
+
alias :[] :plugin!
|
33
41
|
|
34
42
|
def plugin(name)
|
35
43
|
plugin!(name) rescue nil
|
36
44
|
end
|
37
|
-
alias :[] :plugin!
|
38
45
|
|
39
46
|
def bot
|
40
47
|
client
|
@@ -44,15 +51,11 @@ module Ircbot
|
|
44
51
|
### Operations
|
45
52
|
|
46
53
|
def start(plugin)
|
47
|
-
|
54
|
+
plugin!(plugin).running = true
|
48
55
|
end
|
49
56
|
|
50
57
|
def stop(plugin)
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
def load(plugin)
|
55
|
-
load_plugins(plugin)
|
58
|
+
plugin!(plugin).running = false
|
56
59
|
end
|
57
60
|
|
58
61
|
def delete(plugin)
|
@@ -83,9 +86,7 @@ module Ircbot
|
|
83
86
|
when String, Symbol
|
84
87
|
begin
|
85
88
|
name = plugin.to_s
|
86
|
-
|
87
|
-
self << load_plugin(name)
|
88
|
-
end
|
89
|
+
self << load(name)
|
89
90
|
rescue Exception => e
|
90
91
|
broadcast "Plugin error(#{name}): #{e}[#{e.class}]"
|
91
92
|
end
|
@@ -96,7 +97,7 @@ module Ircbot
|
|
96
97
|
end
|
97
98
|
alias :<< :load_plugins
|
98
99
|
|
99
|
-
def
|
100
|
+
def load(name)
|
100
101
|
path = Ircbot.glob_for(:plugin, name).first or
|
101
102
|
raise PluginNotFound, name.to_s
|
102
103
|
|
@@ -106,10 +107,6 @@ module Ircbot
|
|
106
107
|
class_name = Extlib::Inflection.camelize(name) + "Plugin"
|
107
108
|
return Object.const_get(class_name).new
|
108
109
|
|
109
|
-
# Object.subclasses_of(Plugin).each do |k|
|
110
|
-
# return k.new if Extlib::Inflection.demodulize(k.name) =~ /^#{class_name}(Plugin)?$/
|
111
|
-
# end
|
112
|
-
|
113
110
|
rescue NameError => e
|
114
111
|
raise LoadError, "Expected #{path} to define #{class_name} (#{e})"
|
115
112
|
end
|
@@ -123,17 +120,6 @@ module Ircbot
|
|
123
120
|
end
|
124
121
|
|
125
122
|
private
|
126
|
-
def find_plugin!(plugin)
|
127
|
-
case plugin
|
128
|
-
when String, Symbol
|
129
|
-
@plugins[plugin.to_s] or raise PluginNotFound, plugin.to_s
|
130
|
-
when Plugin
|
131
|
-
plugin
|
132
|
-
else
|
133
|
-
raise PluginNotFound, "#{plugin.class}(#{plugin})"
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
123
|
def broadcast(text)
|
138
124
|
p text
|
139
125
|
end
|
data/lib/ircbot/version.rb
CHANGED
data/lib/ircbot.rb
CHANGED
@@ -18,6 +18,7 @@ require "ircbot/framework"
|
|
18
18
|
require "ircbot/version"
|
19
19
|
|
20
20
|
Ircbot.push_path(:plugin, Ircbot.system_root + 'plugins')
|
21
|
+
Ircbot.push_path(:config, Ircbot.system_root + 'config')
|
21
22
|
Ircbot.toplevel_binding = binding
|
22
23
|
|
23
24
|
######################################################################
|
data/plugins/irc.rb
CHANGED
@@ -4,6 +4,13 @@ require 'rubygems'
|
|
4
4
|
require 'ircbot'
|
5
5
|
|
6
6
|
class IrcPlugin < Ircbot::Plugin
|
7
|
+
def help
|
8
|
+
<<-EOF
|
9
|
+
[Irc plugin] make bot send irc native commands
|
10
|
+
ex) send "!JOIN #test" message to #{config.nick}
|
11
|
+
EOF
|
12
|
+
end
|
13
|
+
|
7
14
|
def reply(text)
|
8
15
|
if direct?
|
9
16
|
if text[0] == ?!
|
data/plugins/plugins.rb
CHANGED
@@ -20,11 +20,11 @@ commands: load, start, stop, delete
|
|
20
20
|
|
21
21
|
case command.to_s
|
22
22
|
when "load", "register"
|
23
|
-
plugins
|
23
|
+
plugins.load arg
|
24
24
|
throw :halt, "Loaded #{arg}"
|
25
25
|
|
26
26
|
when "delete", "remove"
|
27
|
-
plugins.
|
27
|
+
plugins.delete arg
|
28
28
|
throw :halt, "Removed #{arg}"
|
29
29
|
|
30
30
|
when "start"
|
data/plugins/which.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'ircbot'
|
6
|
+
require 'chawan'
|
7
|
+
|
8
|
+
class WhichPlugin < Ircbot::Plugin
|
9
|
+
def reply(text)
|
10
|
+
case text
|
11
|
+
when /どっち.*(?|\?)/
|
12
|
+
nouns = Chawan.parse($`).compact.noun
|
13
|
+
noun = nouns[rand(nouns.size)]
|
14
|
+
return "#{noun}に大決定や!"
|
15
|
+
else
|
16
|
+
return nil
|
17
|
+
end
|
18
|
+
rescue Chawan::CannotAnalyze
|
19
|
+
return nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
if $0 == __FILE__
|
24
|
+
plugin = WhichPlugin.new
|
25
|
+
puts plugin.reply("AKBとハロはどっちがいい?")
|
26
|
+
puts plugin.reply("どっち?")
|
27
|
+
end
|
data/spec/plugins_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ircbot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maiha
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-10 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -50,6 +50,7 @@ files:
|
|
50
50
|
- lib/ircbot/client/config.rb
|
51
51
|
- lib/ircbot/client/config/channels.rb
|
52
52
|
- lib/ircbot/client/config/plugins.rb
|
53
|
+
- lib/ircbot/client/config/generator.rb
|
53
54
|
- lib/ircbot/client/encoding.rb
|
54
55
|
- lib/ircbot/client/commands.rb
|
55
56
|
- lib/ircbot/client/plugins.rb
|
@@ -70,7 +71,11 @@ files:
|
|
70
71
|
- spec/plugins_spec.rb
|
71
72
|
- plugins/echo.rb
|
72
73
|
- plugins/plugins.rb
|
74
|
+
- plugins/which.rb
|
73
75
|
- plugins/irc.rb
|
76
|
+
- config/wota.yml
|
77
|
+
- config/sama-zu.yml
|
78
|
+
- config/yml.erb
|
74
79
|
has_rdoc: true
|
75
80
|
homepage: http://github.com/maiha/ircbot
|
76
81
|
licenses: []
|