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 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$/ ; cpi = ARGV.shift
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
- unless config
19
- raise "Specify your config file\nusage: #{$0} -f config/xxx.dat"
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)
@@ -0,0 +1,8 @@
1
+ host: localhost
2
+ port: 6667
3
+ nick: sama-zu
4
+ user: samazu
5
+ real: sama~zu
6
+ channels: "#test"
7
+ plugins: irc plugins
8
+ multiline: true
data/config/wota.yml ADDED
@@ -0,0 +1,8 @@
1
+ host: irc.haun.org
2
+ port: 6667
3
+ nick: sama-zu
4
+ user: samazu
5
+ real: sama~zu
6
+ channels: "#wota"
7
+ plugins: irc plugins which
8
+ multiline: true
data/config/yml.erb ADDED
@@ -0,0 +1,8 @@
1
+ host: localhost
2
+ port: 6666
3
+ nick: <%= nick %>
4
+ user: <%= nick %>
5
+ real: <%= nick %>
6
+ channels: "#test"
7
+ plugins: irc plugins
8
+ multiline: true
@@ -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
- m.reply(self, "#{type}: #{e.message}")
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
 
@@ -27,14 +27,21 @@ module Ircbot
27
27
  ######################################################################
28
28
  ### Accessors
29
29
 
30
- def plugin!(name)
31
- find_plugin!(name)
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
- find_plugin(plugin).running = true
54
+ plugin!(plugin).running = true
48
55
  end
49
56
 
50
57
  def stop(plugin)
51
- find_plugin(plugin).running = false
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
- unless @plugins[name]
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 load_plugin(name)
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
@@ -1,4 +1,4 @@
1
1
  module Ircbot
2
- VERSION = '0.1.0' unless defined?(Ircbot::VERSION)
2
+ VERSION = '0.1.1' unless defined?(Ircbot::VERSION)
3
3
  HOMEPAGE = "http://github.com/maiha/ircbot"
4
4
  end
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 << Array(arg.split)
23
+ plugins.load arg
24
24
  throw :halt, "Loaded #{arg}"
25
25
 
26
26
  when "delete", "remove"
27
- plugins.remove arg
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
@@ -11,7 +11,9 @@ describe Ircbot::Plugins do
11
11
  provide :plugins
12
12
  provide :active
13
13
  provide :load_plugins
14
- provide :load_plugin
14
+ provide :load
15
+ provide :plugin!
16
+ provide :plugin
15
17
  provide :[]
16
18
  provide :<<
17
19
  provide :bot
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.0
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-09 00:00:00 +09:00
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: []