kris 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -5,8 +5,8 @@ IRC bot library - Pluggable, and very simple
5
5
 
6
6
  [![Gem Version](https://badge.fury.io/rb/kris.png)](http://badge.fury.io/rb/kris)
7
7
  [![Dependency Status](https://gemnasium.com/Tomohiro/kris.png)](https://gemnasium.com/Tomohiro/kris)
8
+ [![Stillmaintained](http://stillmaintained.com/Tomohiro/kris.png)](http://stillmaintained.com/Tomohiro/kris)
8
9
  [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/Tomohiro/kris)
9
- [![endorse](http://api.coderwall.com/tomohiro/endorsecount.png)](http://coderwall.com/tomohiro)
10
10
 
11
11
 
12
12
  ---
@@ -16,7 +16,6 @@ Requirements
16
16
  --------------------------------------------------------------------------------
17
17
 
18
18
  - Ruby 1.9.3 or lator
19
- - Zircon
20
19
 
21
20
 
22
21
  Getting started
@@ -49,7 +48,7 @@ Install
49
48
  |-- MyBot
50
49
  `-- plugin
51
50
  |-- reply.rb
52
- `-- crawler.rb
51
+ `-- hello.rb
53
52
 
54
53
 
55
54
  #### Bootstrap Bot
@@ -67,7 +66,10 @@ Kris::Session.new(
67
66
  channel: '#my-bot-channel',
68
67
  nickname: 'MyBot',
69
68
  username: 'MyBot',
70
- realname: 'MyBot'
69
+ realname: 'MyBot',
70
+ # Optional settings is below (Not Required)
71
+ channels: %(#foo #bar #baz), # If you want to join another channels with additional
72
+ plugin_path: '/home/kris/my-bot-plugins' # If you want to specify a defferent path
71
73
  ).start
72
74
  ```
73
75
 
@@ -78,29 +80,27 @@ Response
78
80
 
79
81
  ```ruby
80
82
  class Reply < Kris::Plugin
81
- def response(message)
83
+ def on_privmsg(message)
82
84
  case message.body.downcase
83
85
  when /hello/
84
86
  reply(message.to, message.from, 'Hello!')
85
87
  end
86
88
  end
89
+
90
+ def on_topic(message)
91
+ notice(message.to, "Now topic is #{message.body}")
92
+ end
87
93
  end
88
94
  ```
89
95
 
90
- Notification
96
+ Scheduled notification
91
97
 
92
98
  ```ruby
93
- class Crawler < Kris::Plugin
99
+ class Hello < Kris::Plugin
94
100
  def notify
95
- crawl_datas do |new_data|
96
- notice('#notify-channel', new_data)
97
- end
101
+ sleep 300
102
+ notice('#notify-channel', 'Hello')
98
103
  end
99
-
100
- private
101
- def crawl_datas
102
- # do something
103
- end
104
104
  end
105
105
  ```
106
106
 
File without changes
data/example/mybot/bot CHANGED
@@ -6,6 +6,7 @@ bot = Kris::Session.new(
6
6
  :server => 'irc.freenode.net',
7
7
  :port => 6667,
8
8
  :channel => '#MyChannel',
9
+ # :channels => %w(#channel_1 #channel_2 #channel3) # If you want join multiple channels.
9
10
  :username => 'ExampleBot',
10
11
  :nickname => 'ExampleBot',
11
12
  :realname => 'ExampleBot'
@@ -1,5 +1,5 @@
1
1
  class Reply < Kris::Plugin
2
- def response(message)
2
+ def on_privmsg(message)
3
3
  case message.body.downcase
4
4
  when /hello/
5
5
  reply(message.to, message.from, 'Hello!')
data/kris.gemspec CHANGED
@@ -6,14 +6,14 @@ require 'kris/version'
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = 'kris'
8
8
  gem.version = Kris::VERSION
9
- gem.authors = ['Tomohiro, TAIRA']
9
+ gem.authors = ['Tomohiro TAIRA']
10
10
  gem.email = ['tomohiro.t@gmail.com']
11
- gem.description = 'IRC bot'
12
- gem.summary = 'IRC bot'
11
+ gem.description = 'IRC bot library - Pluggable, and very simple'
12
+ gem.summary = 'IRC bot library - Pluggable, and very simple'
13
13
  gem.homepage = 'https://github.com/Tomohiro/kris'
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
16
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ['lib']
19
19
 
data/lib/kris/plugin.rb CHANGED
@@ -1,16 +1,21 @@
1
1
  module Kris
2
2
  class Plugin
3
3
  class << self
4
- def autoload(robot)
5
- Dir.glob(File.expand_path('./plugin/*.rb')).each do |file|
4
+ def autoload(plugin_path = nil)
5
+ return unless plugin_path
6
+
7
+ plugin_files(plugin_path).each do |file|
6
8
  require file
7
- klass = eval(filename_classify(File.basename(file, '.rb'))).new(robot)
8
- yield klass
9
+ yield eval(filename_classify(file))
9
10
  end
10
11
  end
11
12
 
12
- def filename_classify(filename)
13
- filename.split('_').map(&:capitalize).join
13
+ def filename_classify(file)
14
+ File.basename(file, '.rb').split('_').map(&:capitalize).join
15
+ end
16
+
17
+ def plugin_files(plugin_path)
18
+ Dir[File.join(File.expand_path(plugin_path), '*.rb')]
14
19
  end
15
20
  end
16
21
 
@@ -19,25 +24,17 @@ module Kris
19
24
  end
20
25
 
21
26
  def boot
22
- @robot.on_privmsg do |message|
23
- response(message)
24
- end
25
-
26
- Thread.start do
27
- loop do
28
- notify
29
- sleep 1
30
- end
31
- end
27
+ load_callbacks
28
+ start_notification_scheduler
32
29
  end
33
30
 
34
31
  protected
35
- # do nothing.
36
- # override this method.
37
- def response(message); end
32
+ # Call response method is for backwords compatibility
33
+ def on_privmsg(message)
34
+ response(message) if respond_to?(:response)
35
+ end
38
36
 
39
- # do nothing.
40
- # override this method.
37
+ # Override this method if you want get scheduled notification.
41
38
  def notify; end
42
39
 
43
40
  def notice(channel, message)
@@ -49,7 +46,29 @@ module Kris
49
46
  end
50
47
 
51
48
  def reply(channel, reply_to, message)
52
- privmsg(channel, "#{reply_to}: #{message}")
49
+ @robot.privmsg(channel, ":#{reply_to}: #{message}")
50
+ end
51
+
52
+ private
53
+ def load_callbacks
54
+ Zircon::COMMAND_NAMES.each do |command|
55
+ method_name = "on_#{command.downcase}"
56
+ next unless respond_to?(method_name)
57
+
58
+ @robot.send(method_name) do |message|
59
+ send(method_name, message)
60
+ end
61
+ end
62
+ end
63
+
64
+ def start_notification_scheduler
65
+ return unless respond_to?(:notify)
66
+ Thread.start do
67
+ loop do
68
+ notify
69
+ sleep 1
70
+ end
71
+ end
53
72
  end
54
73
  end
55
74
  end
data/lib/kris/session.rb CHANGED
@@ -2,25 +2,39 @@ require 'zircon'
2
2
  require 'kris/zircon/message'
3
3
 
4
4
  module Kris
5
- class Session
5
+ class Session < Zircon
6
6
  def initialize(config)
7
- @logger = Logger.new(STDOUT)
8
- @robot = Zircon.new(config)
7
+ @logger = Logger.new(STDOUT)
8
+ @channels = config.fetch(:channels, nil)
9
+ @plugin_path = config.fetch(:plugin_path, './plugin')
10
+ super(config)
9
11
  end
10
12
 
11
13
  def start
12
14
  @logger.info('Booting Kris...')
13
-
14
- Plugin.autoload(@robot) do |plugin|
15
- @logger.info("#{plugin.class} loaded.")
16
- plugin.boot
17
- end
18
-
19
- @robot.run!
15
+ run!
20
16
  rescue => e
21
17
  @logger.error(e)
22
18
  ensure
23
- @robot.part
19
+ part
24
20
  end
21
+
22
+ protected
23
+ def run!
24
+ Plugin.autoload(@plugin_path) do |plugin|
25
+ @logger.info("#{plugin} loaded.")
26
+ plugin.new(self).boot
27
+ end
28
+
29
+ login
30
+ join_to_channels(@channels) if @channels
31
+ wait_message
32
+ end
33
+
34
+ def join_to_channels(channels)
35
+ channels.each do |channel|
36
+ join(channel)
37
+ end
38
+ end
25
39
  end
26
40
  end
data/lib/kris/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kris
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -1,5 +1,9 @@
1
1
  class Zircon
2
2
  class Message
3
+ def to
4
+ params[0].force_encoding('UTF-8')
5
+ end
6
+
3
7
  def body
4
8
  params[1].force_encoding('UTF-8')
5
9
  end
data/lib/kris.rb CHANGED
@@ -1,7 +1,8 @@
1
- require 'kris/version'
1
+ require 'rubygems'
2
2
  require 'logger'
3
3
 
4
4
  module Kris
5
- autoload :Session, 'kris/session'
6
- autoload :Plugin, 'kris/plugin'
5
+ require 'kris/version'
6
+ require 'kris/session'
7
+ require 'kris/plugin'
7
8
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kris
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - Tomohiro, TAIRA
8
+ - Tomohiro TAIRA
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-21 00:00:00.000000000 Z
12
+ date: 2013-02-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: zircon
@@ -27,7 +27,7 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
- description: IRC bot
30
+ description: IRC bot library - Pluggable, and very simple
31
31
  email:
32
32
  - tomohiro.t@gmail.com
33
33
  executables: []
@@ -40,7 +40,7 @@ files:
40
40
  - README.md
41
41
  - Rakefile
42
42
  - example/mybot/Gemfile
43
- - example/mybot/README.markdown
43
+ - example/mybot/README.md
44
44
  - example/mybot/bot
45
45
  - example/mybot/plugin/reply.rb
46
46
  - kris.gemspec
@@ -72,5 +72,5 @@ rubyforge_project:
72
72
  rubygems_version: 1.8.23
73
73
  signing_key:
74
74
  specification_version: 3
75
- summary: IRC bot
75
+ summary: IRC bot library - Pluggable, and very simple
76
76
  test_files: []