From f35ae6761d9656ec51bd9176a83320c5dcf44278 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 14 Apr 2014 02:06:53 -0700 Subject: Add support for modules. --- dinobot.rb | 32 ++++++++++++++++++++++++++++++++ echo.rb | 15 +++++++++++++++ module.rb | 22 ++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 echo.rb create mode 100644 module.rb diff --git a/dinobot.rb b/dinobot.rb index f88b81b..464f299 100644 --- a/dinobot.rb +++ b/dinobot.rb @@ -55,6 +55,27 @@ module Dinobot def parse_line(str) out str.sub('PING', 'PONG') if str =~ /^PING / + + if str =~ /(\S+) PRIVMSG (\S+) :(.*)/ + user, channel, message = str.scan(/(\S+) PRIVMSG (\S+) :(.*)/).first + + if @modules.has_key?(message.split.first.sub(/^#{Regexp.escape(@trigger)}/, '').downcase.intern) + ret = @modules[message.split.first.sub(/^#{Regexp.escape(@trigger)}/, '').downcase.intern].call(user, channel, message) + + exec_commands(ret) + end + end + end + + def exec_commands(commands) + commands.each do |command| + case command.first + when :say + send(*command) if command.length == 3 + when :join, :part + send(*command) if command.length == 2 + end + end end def out(str) @@ -80,5 +101,16 @@ module Dinobot out "PART #{channel}" end + + def load_module(mod) + file = Dir.entries(File.dirname(__FILE__)).find { |x| x == mod.to_s.downcase + ".rb" } + + if file + puts "== Loading #{mod}." + + load file + @modules[mod.downcase] = eval("Dinobot::#{mod}").new + end + end end end diff --git a/echo.rb b/echo.rb new file mode 100644 index 0000000..4911ad0 --- /dev/null +++ b/echo.rb @@ -0,0 +1,15 @@ +require_relative 'module' + +module Dinobot + class Echo < Module + def initialize + super + + @commands << :echo + end + + def echo(user, channel, message) + [[:say, channel, message]] + end + end +end diff --git a/module.rb b/module.rb new file mode 100644 index 0000000..f0c415a --- /dev/null +++ b/module.rb @@ -0,0 +1,22 @@ +module Dinobot + class Module + attr_accessor :commands + + def initialize + @commands = [:commands] + end + + def call(user, channel, message) + message = message.split(' ', 2).last + command = message.split.first + + if @commands.include?(command.intern) + send(command, user, channel, message) + end + end + + def commands(user, channel, message) + [[:say, channel, "Commands: #{@commands.sort.join(' ')}"]] + end + end +end -- cgit v1.2.3-70-g09d2