From 77a8d4daa59b668473794d083e3bbc89eab94648 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 14 Apr 2014 17:17:09 -0700 Subject: Recursive handling of piped commands. --- dinobot.rb | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'dinobot.rb') diff --git a/dinobot.rb b/dinobot.rb index 03f6291..16b8573 100644 --- a/dinobot.rb +++ b/dinobot.rb @@ -70,33 +70,35 @@ module Dinobot message.sub!(@trigger, '') - commands = nil - mt = message.split(' | ') + commands = parse_command(user, channel, message) - mt.each_with_index do |m, i| - mod = m.split.first.downcase.intern - next unless @modules.has_key?(mod) + exec_commands(commands) if commands.is_a?(Array) + end + end - if i.zero? - commands = @modules[mod].call(user, channel, m) - else - return unless commands.is_a?(Array) + def parse_command(user, channel, message, pc=nil) + mod = message.split.first.downcase.intern - nc = [] + m = message.split(' | ', 2) - commands.each do |cmd| - if cmd.first == :say - nc.concat(@modules[mod].call(user, cmd[1], "#{m} #{cmd[2]}")) - else - nc << cmd - end - end + if pc.nil? + commands = @modules[mod].call(user, channel, m.first) + else + commands = [] - commands = nc + pc.each do |c| + if c.first == :say + commands.concat(@modules[mod].call(user, c[1], "#{m.first} #{c[2]}")) + else + commands << c end end + end - exec_commands(commands) if commands.is_a?(Array) + if m.length == 2 + parse_command(user, channel, m.last, commands) + else + commands end end -- cgit v1.2.3-70-g09d2