X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fsalut.rb;h=4582e5549aea4ca936253cc5427b01dc223a1d6a;hb=aaf9333016f3aea00bc7ebebe7e6a54f19cf3770;hp=1a301b438e13619011b75ba1e387bd733f525407;hpb=40e622c4ae1ccf8b9dfcec133d5f33f1d8b1217c;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/salut.rb b/data/rbot/plugins/salut.rb index 1a301b43..4582e554 100644 --- a/data/rbot/plugins/salut.rb +++ b/data/rbot/plugins/salut.rb @@ -1,5 +1,8 @@ # vim: set sw=2 et: # 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" @@ -18,6 +21,12 @@ class SalutPlugin < Plugin :desc => "Check for a salutation in all languages and not just in the one defined by core.language", :on_change => Proc.new {|bot, v| bot.plugins['salut'].reload} ) + BotConfig.register BotConfigBooleanValue.new('salut.address_only', + :default => true, + :desc => "When set to true, the bot will only reply to salutations directed at him", + :on_change => Proc.new {|bot, v| bot.plugins['salut'].reload} + ) + def initialize @salutations = Hash.new @@ -25,6 +34,7 @@ class SalutPlugin < Plugin @main_lang_str = nil @main_lang = nil @all_langs = true + @changed = false super reload end @@ -35,12 +45,15 @@ class SalutPlugin < Plugin def create_match @match = Hash.new + ar_dest = Array.new ar_in = Array.new ar_out = Array.new ar_both = Array.new @salutations.each { |lang, hash| hash.each { |situation, array| case situation.to_s + when /^generic-dest$/ + ar_dest += array when /in$/ ar_in += array when /out$/ @@ -60,19 +73,31 @@ class SalutPlugin < Plugin Regexp.escape(txt) }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_both.empty? debug "Matches: #{@match.inspect}" + @match[:dest] = Regexp.new("\\b(?:" + ar_dest.uniq.map { |txt| + Regexp.escape(txt) + }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_dest.empty? + @punct = /\s*[.,:!;?]?\s*/ # Punctuation end def listen(m) + return unless m.kind_of?(PrivMessage) + to_me = m.address? || m.message =~ /#{Regexp.escape(@bot.nick)}/i + if @bot.config['salut.address_only'] + 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]}/ + if m.message =~ @match[k] salut = k break end } return unless salut + # If the bot wasn't addressed, we continue only the match was exact + # (apart from space and punctuation) or if @match[:dest] matches too + return unless to_me or m.message =~ @match[:dest] or m.message =~ /^#{@punct}#{@match[salut]}#{@punct}$/ h = Time.new.hour case h when 4...12 @@ -100,7 +125,7 @@ class SalutPlugin < Plugin rep_ar += @salutations[@main_lang].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 @@ -134,9 +159,14 @@ class SalutPlugin < Plugin @salutations[@main_lang] = load_lang(@main_lang_str) end create_match + @changed = false end def load_lang(lang) + dir = "#{@bot.botclass}/salut" + if not File.exist?(dir) + Dir.mkdir(dir) + end file = "#{@bot.botclass}/salut/salut-#{lang}" if File.exist?(file) begin @@ -155,15 +185,17 @@ class SalutPlugin < Plugin def save return if @salutations.empty? + return unless @changed @salutations.each { |lang, val| l = lang.to_s save_lang(lang, val) } + @changed = false end def save_lang(lang, val) - file = "#{@bot.botclass}/salut/salut-#{lang}" - Utils.safe_save(file) { |file| + fn = "#{@bot.botclass}/salut/salut-#{lang}" + Utils.safe_save(fn) { |file| file.puts val.to_yaml } end