diff options
| author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-04-20 04:23:20 -0700 | 
|---|---|---|
| committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-04-20 04:23:20 -0700 | 
| commit | 7f134adccb20abe773234acecf526c9951b418e8 (patch) | |
| tree | eeb4b4f96a963591f244c8f661b84dd194cf1484 | |
| parent | 7e7aa3278a9e478b02eb152575fb00ee0802ac81 (diff) | |
| download | dinobot-7f134adccb20abe773234acecf526c9951b418e8.tar.gz dinobot-7f134adccb20abe773234acecf526c9951b418e8.tar.xz | |
Improved namespace and directory structure.
| -rw-r--r-- | admin.rb | 77 | ||||
| -rw-r--r-- | core/irc.rb | 67 | ||||
| -rw-r--r-- | core/logger.rb | 50 | ||||
| -rw-r--r-- | dinobot.rb | 17 | ||||
| -rw-r--r-- | irc.rb | 65 | ||||
| -rw-r--r-- | logger.rb | 48 | ||||
| -rw-r--r-- | module.rb | 23 | ||||
| -rw-r--r-- | module/admin.rb | 79 | ||||
| -rw-r--r-- | module/module.rb | 25 | ||||
| -rw-r--r-- | module/test.rb | 46 | ||||
| -rw-r--r-- | test.rb | 44 | 
11 files changed, 276 insertions, 265 deletions
| diff --git a/admin.rb b/admin.rb deleted file mode 100644 index c505ef0..0000000 --- a/admin.rb +++ /dev/null @@ -1,77 +0,0 @@ -require_relative 'module' - -module Dinobot -  class Admin < Module -    def initialize(bot) -      super - -      @commands << :join << :part << :load << :unload -      @commands << :listadmins << :listmodules << :listchannels - -      @admins = Array.new -    end - -    def add(user) -      @admins << user unless @admins.include?(user) -    end - -    def remove(user) -      @admins.delete(user) -    end - -    def is_admin?(user) -      # FIXME: Using hostname for testing purposes. Need better solution. -      @admins.include?(user.sub(/.+@/, '')) -    end - -    def join(user, channel, argument) -      return unless is_admin?(user) - -      [[:join, argument.strip]] -    end - -    def part(user, channel, argument) -      return unless is_admin?(user) - -      [[:part, argument.strip]] -    end - -    def listadmins(user, channel, argument) -      return unless is_admin?(user) - -      [[:say, channel, @admins.join(' ')]] -    end - -    def load(user, channel, argument) -      return unless is_admin?(user) - -      argument.split.each do |x| -        @bot.load_module x.intern -      end - -      nil -    end - -    def unload(user, channel, argument) -      return unless is_admin?(user) - -      argument.split.each do |x| -        @bot.unload_module x.intern -      end - -      nil -    end - -    def listmodules(user, channel, argument) -      return unless is_admin?(user) - -      [[:say, channel, "Modules: #{@bot.modules.keys.sort.join(' ')}"]] -    end - -    def listchannels(user, channel, argument) -      return unless is_admin?(user) - -      [[:say, channel, "Channels: #{@bot.channels.sort.join(' ')}"]] -    end -  end -end diff --git a/core/irc.rb b/core/irc.rb new file mode 100644 index 0000000..779b37b --- /dev/null +++ b/core/irc.rb @@ -0,0 +1,67 @@ +require 'socket' + +require_relative 'logger' + +module Dinobot +  module Core +    class IRC +      def initialize(server, port, nick, pass=nil) +        @server = server +        @port = port +        @nick = nick +        @pass = pass + +        @socket = nil +        @logger = Dinobot::Core::Logger.instance +      end + +      def connect +        @logger.info "Connecting to #{@server}:#{@port}." + +        @socket = TCPSocket.new(@server, @port) + +        puts "PASS #{@pass}" if @pass +        puts "NICK #{@nick}" +        puts "USER #{@nick} 0 * :#{@nick}" +      end + +      def disconnect +        @socket.close +      end + +      def connected? +        !(@socket.nil? || @socket.closed?) +      end + +      def gets +        str = @socket.gets + +        @logger.in str.inspect + +        str +      end + +      def puts(str) +        @logger.out str.inspect + +        @socket.puts str +      end + +      def pong(message) +        puts "PONG #{message}" +      end + +      def join(channel) +        puts "JOIN #{channel}" +      end + +      def part(channel) +        puts "PART #{channel}" +      end + +      def privmsg(channel, message) +        puts "PRIVMSG #{channel} :#{message}" +      end +    end +  end +end diff --git a/core/logger.rb b/core/logger.rb new file mode 100644 index 0000000..d73cd15 --- /dev/null +++ b/core/logger.rb @@ -0,0 +1,50 @@ +module Dinobot +  module Core +    class Logger +      @@instance = nil +      @@mutex = Mutex.new + +      def in(*lines) +        str = lines.join("\n") + +        puts str.gsub(/^/, "\e[32m<<\e[0m ") +      end + +      def out(*lines) +        str = lines.join("\n") + +        puts str.gsub(/^/, "\e[36m>>\e[0m ") +      end + +      def error(*lines) +        str = lines.join("\n") + +        puts str.gsub(/^/, "\e[31m!!\e[0m ") +      end + +      def info(*lines) +        str = lines.join("\n") + +        puts str.gsub(/^/, "\e[33m==\e[0m ") +      end + +      def indent(*lines) +        str = lines.join("\n") + +        puts str.gsub(/^/, '   ') +      end + +      class << self +        def instance +          return @@instance if @@instance + +          @@mutex.synchronize do +            @@instance ||= new +          end +        end +      end + +      private_class_method :allocate, :new +    end +  end +end @@ -1,7 +1,7 @@  require 'timeout' -require_relative 'irc' -require_relative 'logger' +require_relative 'core/irc' +require_relative 'core/logger'  module Dinobot    class Bot @@ -16,8 +16,8 @@ module Dinobot        @trigger = '!' -      @irc = Dinobot::IRC.new(@server, @port, @nick, @pass) -      @logger = Dinobot::Logger.instance +      @irc = Dinobot::Core::IRC.new(@server, @port, @nick, @pass) +      @logger = Dinobot::Core::Logger.instance        @modules = Hash.new        @channels = Array.new @@ -61,9 +61,10 @@ module Dinobot        @logger.info "Loading module: #{mod}"        begin -        load "#{mod}.rb" +        load "module/#{mod}.rb" -        m = Dinobot.const_get(Dinobot.constants.find { |x| x.downcase == mod }) +        m = Dinobot::Module.const_get( +          Dinobot::Module.constants.find { |x| x.downcase == mod })          @modules[mod] = m.new(self)          @logger.info "Loaded module: #{mod} (#{m})" @@ -80,8 +81,8 @@ module Dinobot          raise 'module not loaded' unless @modules.has_key?(mod)          @modules.delete(mod) -        m = Dinobot.send(:remove_const, -          Dinobot.constants.find { |x| x.downcase == mod }) +        m = Dinobot::Module.send(:remove_const, +          Dinobot::Module.constants.find { |x| x.downcase == mod })          @logger.info "Unloaded module: #{mod} (#{m})"        rescue => e @@ -1,65 +0,0 @@ -require 'socket' - -require_relative 'logger' - -module Dinobot -  class IRC -    def initialize(server, port, nick, pass=nil) -      @server = server -      @port = port -      @nick = nick -      @pass = pass - -      @socket = nil -      @logger = Dinobot::Logger.instance -    end - -    def connect -      @logger.info "Connecting to #{@server}:#{@port}." - -      @socket = TCPSocket.new(@server, @port) - -      puts "PASS #{@pass}" if @pass -      puts "NICK #{@nick}" -      puts "USER #{@nick} 0 * :#{@nick}" -    end - -    def disconnect -      @socket.close -    end - -    def connected? -      !(@socket.nil? || @socket.closed?) -    end - -    def gets -      str = @socket.gets - -      @logger.in str.inspect - -      str -    end - -    def puts(str) -      @logger.out str.inspect - -      @socket.puts str -    end - -    def pong(message) -      puts "PONG #{message}" -    end - -    def join(channel) -      puts "JOIN #{channel}" -    end - -    def part(channel) -      puts "PART #{channel}" -    end - -    def privmsg(channel, message) -      puts "PRIVMSG #{channel} :#{message}" -    end -  end -end diff --git a/logger.rb b/logger.rb deleted file mode 100644 index f84c157..0000000 --- a/logger.rb +++ /dev/null @@ -1,48 +0,0 @@ -module Dinobot -  class Logger -    @@instance = nil -    @@mutex = Mutex.new - -    def in(*lines) -      str = lines.join("\n") - -      puts str.gsub(/^/, "\e[32m<<\e[0m ") -    end - -    def out(*lines) -      str = lines.join("\n") - -      puts str.gsub(/^/, "\e[36m>>\e[0m ") -    end - -    def error(*lines) -      str = lines.join("\n") - -      puts str.gsub(/^/, "\e[31m!!\e[0m ") -    end - -    def info(*lines) -      str = lines.join("\n") - -      puts str.gsub(/^/, "\e[33m==\e[0m ") -    end - -    def indent(*lines) -      str = lines.join("\n") - -      puts str.gsub(/^/, '   ') -    end - -    class << self -      def instance -        return @@instance if @@instance - -        @@mutex.synchronize do -          @@instance ||= new -        end -      end -    end - -    private_class_method :allocate, :new -  end -end diff --git a/module.rb b/module.rb deleted file mode 100644 index 082fc83..0000000 --- a/module.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Dinobot -  class Module -    attr_accessor :commands - -    def initialize(bot) -      @bot = bot - -      @commands = [:commands] -    end - -    def call(user, channel, message) -      command, argument = message.split(' ', 3)[1..2] - -      if @commands.map { |x| x.to_s }.include?(command) -        send(command, user, channel, argument) -      end -    end - -    def commands(user, channel, argument) -      [[:say, channel, "Commands: #{@commands.sort.join(' ')}"]] -    end -  end -end diff --git a/module/admin.rb b/module/admin.rb new file mode 100644 index 0000000..d59fd49 --- /dev/null +++ b/module/admin.rb @@ -0,0 +1,79 @@ +require_relative 'module' + +module Dinobot +  module Module +    class Admin < Module +      def initialize(bot) +        super + +        @commands << :join << :part << :load << :unload +        @commands << :listadmins << :listmodules << :listchannels + +        @admins = Array.new +      end + +      def add(user) +        @admins << user unless @admins.include?(user) +      end + +      def remove(user) +        @admins.delete(user) +      end + +      def is_admin?(user) +        # FIXME: Using hostname for testing purposes. Need better solution. +        @admins.include?(user.sub(/.+@/, '')) +      end + +      def join(user, channel, argument) +        return unless is_admin?(user) + +        [[:join, argument.strip]] +      end + +      def part(user, channel, argument) +        return unless is_admin?(user) + +        [[:part, argument.strip]] +      end + +      def listadmins(user, channel, argument) +        return unless is_admin?(user) + +        [[:say, channel, @admins.join(' ')]] +      end + +      def load(user, channel, argument) +        return unless is_admin?(user) + +        argument.split.each do |x| +          @bot.load_module x.intern +        end + +        nil +      end + +      def unload(user, channel, argument) +        return unless is_admin?(user) + +        argument.split.each do |x| +          @bot.unload_module x.intern +        end + +        nil +      end + +      def listmodules(user, channel, argument) +        return unless is_admin?(user) + +        [[:say, channel, "Modules: #{@bot.modules.keys.sort.join(' ')}"]] +      end + +      def listchannels(user, channel, argument) +        return unless is_admin?(user) + +        [[:say, channel, "Channels: #{@bot.channels.sort.join(' ')}"]] +      end +    end +  end +end diff --git a/module/module.rb b/module/module.rb new file mode 100644 index 0000000..9d72b09 --- /dev/null +++ b/module/module.rb @@ -0,0 +1,25 @@ +module Dinobot +  module Module +    class Module +      attr_accessor :commands + +      def initialize(bot) +        @bot = bot + +        @commands = [:commands] +      end + +      def call(user, channel, message) +        command, argument = message.split(' ', 3)[1..2] + +        if @commands.map { |x| x.to_s }.include?(command) +          send(command, user, channel, argument) +        end +      end + +      def commands(user, channel, argument) +        [[:say, channel, "Commands: #{@commands.sort.join(' ')}"]] +      end +    end +  end +end diff --git a/module/test.rb b/module/test.rb new file mode 100644 index 0000000..e1d8358 --- /dev/null +++ b/module/test.rb @@ -0,0 +1,46 @@ +require_relative 'module' + +module Dinobot +  module Module +    class Test < Module +      def initialize(bot) +        super + +        @commands << :echo << :ping << :x3 << :fooify +        @commands << :error << :timeout << :wrongreturn << :invalidmethods +      end + +      def echo(user, channel, argument) +        [[:say, channel, argument]] +      end + +      def ping(user, channel, argument) +        [[:say, channel, 'pong']] +      end + +      def x3(user, channel, argument) +        [[:say, channel, argument]] * 3 +      end + +      def fooify(user, channel, argument) +        [[:say, channel, 'foo' + argument]] +      end + +      def error(user, channel, argument) +        x +      end + +      def timeout(user, channel, argument) +        sleep 60 +      end + +      def wrongreturn(user, channel, argument) +        0 +      end + +      def invalidmethods(user, channel, argument) +        [[:say, channel]] +      end +    end +  end +end diff --git a/test.rb b/test.rb deleted file mode 100644 index a54683e..0000000 --- a/test.rb +++ /dev/null @@ -1,44 +0,0 @@ -require_relative 'module' - -module Dinobot -  class Test < Module -    def initialize(bot) -      super - -      @commands << :echo << :ping << :x3 << :fooify -      @commands << :error << :timeout << :wrongreturn << :invalidmethods -    end - -    def echo(user, channel, argument) -      [[:say, channel, argument]] -    end - -    def ping(user, channel, argument) -      [[:say, channel, 'pong']] -    end - -    def x3(user, channel, argument) -      [[:say, channel, argument]] * 3 -    end - -    def fooify(user, channel, argument) -      [[:say, channel, 'foo' + argument]] -    end - -    def error(user, channel, argument) -      x -    end - -    def timeout(user, channel, argument) -      sleep 60 -    end - -    def wrongreturn(user, channel, argument) -      0 -    end - -    def invalidmethods(user, channel, argument) -      [[:say, channel]] -    end -  end -end | 
