diff --git a/README.md b/README.md index ea0092490be8e713b3350a6dce3247d9eebffd53..77b81fa433fbcb86747b68a517f1d5c87d31399b 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,14 @@ # PlayBot +## ATTENTION + +Cette branche a pour but la réécriture du PlayBot en ruby. Avec cette réécriture, un système de plugin et des tests unitaires seront de la partie. + +Les dépendances nécéssaires sont : + * net-yail + * rspec + + ## Description diff --git a/lib/playbot.rb b/lib/playbot.rb index c39a383e2317ed85e3fa8f158cb7f4885255785c..3f9203bfe08d40aae8fff42dc238e88f46fca224 100644 --- a/lib/playbot.rb +++ b/lib/playbot.rb @@ -1,15 +1,20 @@ -#!/usr/bin/ruby - - require 'rubygems' require 'net/yail/irc_bot' +require 'site_plugin' + +# +# Add plugins folder to LOAD_PATH and subsequently require all plugins. +# +dir = '../plugins' +$LOAD_PATH << dir +Dir[File.join(dir, '*.rb')].each {|file| require File.basename(file) } class PlayBot < IRCBot BOTNAME = 'PlayBot' public - # Star a new instance + # Start a new instance # # Options: # * <tt>:address</tt>: irc server @@ -39,7 +44,7 @@ class PlayBot < IRCBot # This metod is called by IRCBot#connect_socket def add_custom_handlers() @irc.hearing_welcome self.method(:_in_welcome) - #@irc.on_msg self.method(:_in_msg) + @irc.on_msg self.method(:_in_msg) end private @@ -57,4 +62,9 @@ class PlayBot < IRCBot msg('NickServ', "identify #{nick_passwd}") end + + def _in_msg(event) + # we don't care of private messages + return if event.pm? + end end diff --git a/lib/site_plugin.rb b/lib/site_plugin.rb new file mode 100644 index 0000000000000000000000000000000000000000..c59d975f5322f22970fe8dd826896411ed6901d1 --- /dev/null +++ b/lib/site_plugin.rb @@ -0,0 +1,15 @@ +class SitePlugin + @@repository = [] + + def self.repository + @@repository + end + + def self.inherited(target) + @@repository << target + end + + def self.for_site(site) + @@repository.find {|handler| handler.can_handle? site } + end +end diff --git a/playbot.rb b/playbot.rb deleted file mode 100755 index 76db536195d6ad6d2bf5699035fc9385e28fd45c..0000000000000000000000000000000000000000 --- a/playbot.rb +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/ruby - - -require 'rubygems' -require 'net/yail/irc_bot' - - -class PlayBot < IRCBot - BOTNAME = 'PlayBot' - - public - # Star a new instance - # - # Options: - # * <tt>:address</tt>: irc server - # * <tt>:port</tt>: port number, default to 6667 - # * <tt>:nicknames</tt>: array of nicknames to cycle through - # * <tt>:nick_passwd</tt>: password for the first nick of :nicknames - # if we are not connected with this nick, we will use ghost and take this nick - # * <tt>:channels</tt>: the channels we are going to connect to - # * <tt>:admin</tt>: the nick of the user who can command the bot - def initialize(options = {}) - @admin = options.delete(:master) - (!@admin) ? raise "You must provide an admin !" - - if options[:nick_passwd] - @nick = options[:nicknames].first - @nick_paswd = options.delete[:nick_passwd] - end - - options[:username] = BOTNAME - options[:realname] = BOTNAME - - self.connect_socket - self.start_listening - end - - # This metod is called by IRCBot#connect_socket - def add_custom_handlers() - @irc.on_welcome self.method(:_in_welcome) - @irc.on_msg self.method(:_in_msg) - @irc.saying_join self.method(:_out_join) - end - - private - # Welcome event handler - # - # We use it to identify us against NickServ - def _in_welcome(event) - return if !@nick - - if self.bot_name != @nick - msg('NickServ', "ghost #{nick} #{nick_passwd}") - sleep 30 - nick @nick - end - - msg('NickServ', "identify #{nick_passwd}") - end -end diff --git a/spec/site_plugin_spec.rb b/spec/site_plugin_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..612cf3133433eb4b83040587ec620dc13d8b1ecc --- /dev/null +++ b/spec/site_plugin_spec.rb @@ -0,0 +1,28 @@ +$LOAD_PATH << '../lib' +require 'site_plugin' + +describe SitePlugin do + describe 'inherance' do + it 'add the class to the repository' do + class TestPlugin1 < SitePlugin + def self.can_handle?(site) + false + end + end + + SitePlugin.repository.empty?.should be_false + end + end + + describe '.for_site' do + it 'return plugin that can handle a given site"' do + class TestPlugin2 < SitePlugin + def self.can_handle?(site) + site =~ /test/ + end + end + + SitePlugin.for_site('OfCourseICanHandle_test_').nil?.should be_false + end + end +end