From 8fd8c27562184a18000d32d96921fbb0ca8045be Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 21 Apr 2014 13:02:19 -0700 Subject: Add some basic command aliasing support. --- dinobot.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'dinobot.rb') diff --git a/dinobot.rb b/dinobot.rb index 0826a51..752ca7e 100644 --- a/dinobot.rb +++ b/dinobot.rb @@ -4,6 +4,7 @@ require_relative 'core/config' require_relative 'core/irc' require_relative 'core/logger' require_relative 'core/messageinfo' +require_relative 'core/store' module Dinobot class Bot @@ -19,6 +20,7 @@ module Dinobot @irc = Dinobot::Core::IRC.new(@server, @port, @nick, @pass) @config = Dinobot::Core::Config.instance @logger = Dinobot::Core::Logger.instance + @aliases = Dinobot::Core::Store.new('data/aliases') @modules = Hash.new @channels = Array.new @@ -95,6 +97,20 @@ module Dinobot end end + def add_alias(from, to) + @aliases.data[:global] = Hash.new unless @aliases.data[:global] + + @aliases.data[:global][from] = to + @aliases.save + end + + def remove_alias(from) + return unless @aliases.data[:global] + + @aliases.data[:global].delete(from) + @aliases.save + end + private def process_in_new_thread(str) @@ -130,6 +146,13 @@ module Dinobot end def exec_command(m, command) + # FIXME: Improve and add debug output. + if @aliases.data[:global] + @aliases.data[:global].each do |k, v| + command.sub!(/\A#{Regexp.escape(k)}\b/, v) + end + end + command, remainder = command.split(' | ', 2) mod = command.scan(/\A\S+/).first.downcase -- cgit v1.2.3-70-g09d2