X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fsalut.rb;h=feff2dd63c9efe880d5acf0b3f2d0129cf143e99;hb=edd1cf77be07ae507014574141e920ad23eb164d;hp=a74504f6e6cc44314b4c6ef256ab3727ea679ec4;hpb=ac72e77fcb2c9ea9bbae67476b2d66458586926a;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/salut.rb b/data/rbot/plugins/salut.rb index a74504f6..feff2dd6 100644 --- a/data/rbot/plugins/salut.rb +++ b/data/rbot/plugins/salut.rb @@ -1,19 +1,17 @@ -# vim: set sw=2 et: +#-- vim:sw=2:et +#++ +# +# :title: Salutations plugin for rbot +# +# Author:: Giuseppe "Oblomov" Bilotta +# Copyright:: (C) 2006-2007 Giuseppe Bilotta +# License:: GPL v2 +# # Salutations plugin: respond to salutations -# TODO allow online editing of salutations -# TODO *REMEMBER* to set @changed to true after edit -# TODO or changes won't be saved - -unless Array.respond_to?(:pick_one) - debug "Defining the pick_one method for Array" - class ::Array - def pick_one - return nil if self.empty? - self[rand(self.length)] - end - end -end - +# +# TODO:: allow online editing of salutations +# +# TODO:: *REMEMBER* to set @changed to true after edit or changes won't be saved class SalutPlugin < Plugin BotConfig.register BotConfigBooleanValue.new('salut.all_languages', @@ -30,7 +28,8 @@ class SalutPlugin < Plugin def initialize @salutations = Hash.new - @match = nil + @match = Hash.new + @match_langs = Array.new @main_lang_str = nil @main_lang = nil @all_langs = true @@ -44,13 +43,20 @@ class SalutPlugin < Plugin end def create_match - @match = Hash.new + @match.clear + ar_dest = Array.new ar_in = Array.new ar_out = Array.new ar_both = Array.new @salutations.each { |lang, hash| + ar_dest.clear + ar_in.clear + ar_out.clear + ar_both.clear hash.each { |situation, array| case situation.to_s + when /^generic-dest$/ + ar_dest += array when /in$/ ar_in += array when /out$/ @@ -59,34 +65,55 @@ class SalutPlugin < Plugin ar_both += array end } + @match[lang] = Hash.new + @match[lang][:in] = Regexp.new("\\b(?:" + ar_in.uniq.map { |txt| + Regexp.escape(txt) + }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_in.empty? + @match[lang][:out] = Regexp.new("\\b(?:" + ar_out.uniq.map { |txt| + Regexp.escape(txt) + }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_out.empty? + @match[lang][:both] = Regexp.new("\\b(?:" + ar_both.uniq.map { |txt| + Regexp.escape(txt) + }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_both.empty? + @match[lang][:dest] = Regexp.new("\\b(?:" + ar_dest.uniq.map { |txt| + Regexp.escape(txt) + }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_dest.empty? + } + @punct = /\s*[.,:!;?]?\s*/ # Punctuation + + # Languages to match for, in order + @match_langs.clear + @match_langs << @main_lang if @match.key?(@main_lang) + @match_langs << :english if @match.key?(:english) + @match.each_key { |key| + @match_langs << key } - @match[:in] = Regexp.new("\\b(?:" + ar_in.uniq.map { |txt| - Regexp.escape(txt) - }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_in.empty? - @match[:out] = Regexp.new("\\b(?:" + ar_out.uniq.map { |txt| - Regexp.escape(txt) - }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_out.empty? - @match[:both] = Regexp.new("\\b(?:" + ar_both.uniq.map { |txt| - Regexp.escape(txt) - }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_both.empty? - debug "Matches: #{@match.inspect}" + @match_langs.uniq! end def listen(m) + return if @match.empty? return unless m.kind_of?(PrivMessage) + return if m.address? and m.plugin == 'config' + to_me = m.address? || m.message =~ /#{Regexp.escape(@bot.nick)}/i if @bot.config['salut.address_only'] - return unless m.address? or m.message =~ /#{Regexp.escape(@bot.nick)}/ + return unless to_me end salut = nil - [:both, :in, :out].each { |k| - next unless @match[k] - debug "Checking salutations #{k} (#{@match[k].inspect})" - if m.message =~ /^#{@match[k]}/ - salut = k - break - end + @match_langs.each { |lang| + [:both, :in, :out].each { |k| + next unless @match[lang][k] + if m.message =~ @match[lang][k] + salut = [@match[lang][k], lang, k] + break + end + } + break if salut } return unless salut + # If the bot wasn't addressed, we continue only if the match was exact + # (apart from space and punctuation) or if @match[:dest] matches too + return unless to_me or m.message =~ /^#{@punct}#{salut.first}#{@punct}$/ or m.message =~ @match[salut[1]][:dest] h = Time.new.hour case h when 4...12 @@ -98,8 +125,12 @@ class SalutPlugin < Plugin end end - def salut_reply(m, k, time) - debug "Replying to #{k} in the #{time}" + def salut_reply(m, salut, time) + lang = salut[1] + k = salut[2] + debug "Replying to #{salut.first} (#{lang} #{k}) in the #{time}" + # salut_ar = @salutations[@main_lang].update @salutations[:english].update @salutations[lang] + salut_ar = @salutations[lang] case k when :both sfx = "" @@ -108,13 +139,13 @@ class SalutPlugin < Plugin end debug "Building array ..." rep_ar = Array.new - rep_ar += @salutations[@main_lang].fetch("#{time}#{sfx}".to_sym, []) - rep_ar += @salutations[@main_lang].fetch("#{time}".to_sym, []) unless sfx.empty? - rep_ar += @salutations[@main_lang].fetch("generic#{sfx}".to_sym, []) - rep_ar += @salutations[@main_lang].fetch("generic".to_sym, []) unless sfx.empty? + rep_ar += salut_ar.fetch("#{time}#{sfx}".to_sym, []) + rep_ar += salut_ar.fetch("#{time}".to_sym, []) unless sfx.empty? + rep_ar += salut_ar.fetch("generic#{sfx}".to_sym, []) + rep_ar += salut_ar.fetch("generic".to_sym, []) unless sfx.empty? debug "Choosing reply in #{rep_ar.inspect} ..." if rep_ar.empty? - if m.public? and (m.address? or m =~ /#{Regexp.escape(@bot.nick)}/) + if m.public? # and (m.address? or m =~ /#{Regexp.escape(@bot.nick)}/) choice = @bot.lang.get("hello_X") % m.sourcenick else choice = @bot.lang.get("hello") % m.sourcenick @@ -179,6 +210,7 @@ class SalutPlugin < Plugin l = lang.to_s save_lang(lang, val) } + @changed = false end def save_lang(lang, val)