]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/markov.rb
nickserv plugin: regexp tweaks and case insensitivity
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / markov.rb
index 8abf0e584902f32dec8711ceeaf27c9975458bca..9b83e53a56d0f6b85a1f4558431cb8045a32d331 100644 (file)
@@ -17,9 +17,13 @@ class MarkovPlugin < Plugin
     :default => 25,
     :validate => Proc.new { |v| (0..100).include? v },
     :desc => "Percentage chance of markov plugin chipping in")
-  Config.register Config::ArrayValue.new('markov.ignore_users',
+  Config.register Config::ArrayValue.new('markov.ignore',
     :default => [],
-    :desc => "Hostmasks of users to be ignored")
+    :desc => "Hostmasks and channel names markov should NOT learn from (e.g. idiot*!*@*, #privchan).")
+  Config.register Config::IntegerValue.new('markov.max_words',
+    :default => 50,
+    :validate => Proc.new { |v| (0..100).include? v },
+    :desc => "Maximum number of words the bot should put in a sentence")
 
   def initialize
     super
@@ -32,6 +36,11 @@ class MarkovPlugin < Plugin
       @bot.config['markov.probability'] = @registry['probability']
       @registry.delete('probability')
     end
+    if @bot.config['markov.ignore_users']
+      debug "moving markov.ignore_users to markov.ignore"
+      @bot.config['markov.ignore'] = @bot.config['markov.ignore_users'].dup
+      @bot.config.delete('markov.ignore_users'.to_sym)
+    end
     @learning_queue = Queue.new
     @learning_thread = Thread.new do
       while s = @learning_queue.pop
@@ -39,6 +48,7 @@ class MarkovPlugin < Plugin
         sleep 0.5
       end
     end
+    @learning_thread.priority = -1
   end
 
   def cleanup
@@ -49,7 +59,7 @@ class MarkovPlugin < Plugin
   end
 
   def generate_string(word1, word2)
-    # limit to max of 50 words
+    # limit to max of markov.max_words words
     output = word1 + " " + word2
 
     # try to avoid :nonword in the first iteration
@@ -61,7 +71,7 @@ class MarkovPlugin < Plugin
       word1, word2 = word2, word3
     end
 
-    49.times do
+    (@bot.config['markov.max_words'] - 1).times do
       wordlist = @registry["#{word1} #{word2}"]
       break if wordlist.empty?
       word3 = wordlist[rand(wordlist.length)]
@@ -95,10 +105,12 @@ class MarkovPlugin < Plugin
     end
   end
 
-  def ignore?(user=nil)
-    return false unless user
-    @bot.config['markov.ignore_users'].each do |mask|
-      return true if user.matches?(mask)
+  def ignore?(m=nil)
+    return false unless m
+    return true if m.address? or m.private?
+    @bot.config['markov.ignore'].each do |mask|
+      return true if m.channel.downcase == mask.downcase
+      return true if m.source.matches?(mask)
     end
     return false
   end
@@ -108,29 +120,29 @@ class MarkovPlugin < Plugin
     user = params[:option]
     case action
     when 'remove':
-      if @bot.config['markov.ignore_users'].include? user
-        s = @bot.config['markov.ignore_users']
+      if @bot.config['markov.ignore'].include? user
+        s = @bot.config['markov.ignore']
         s.delete user
-        @bot.config['ignore_users'] = s
+        @bot.config['ignore'] = s
         m.reply "#{user} removed"
       else
         m.reply "not found in list"
       end
     when 'add':
       if user
-        if @bot.config['markov.ignore_users'].include?(user)
+        if @bot.config['markov.ignore'].include?(user)
           m.reply "#{user} already in list"
         else
-          @bot.config['markov.ignore_users'] = @bot.config['markov.ignore_users'].push user
+          @bot.config['markov.ignore'] = @bot.config['markov.ignore'].push user
           m.reply "#{user} added to markov ignore list"
         end
       else
-        m.reply "give the name of a person to ignore"
+        m.reply "give the name of a person or channel to ignore"
       end
     when 'list':
-      m.reply "I'm ignoring #{@bot.config['markov.ignore_users'].join(", ")}"
+      m.reply "I'm ignoring #{@bot.config['markov.ignore'].join(", ")}"
     else
-      m.reply "have markov ignore the input from a hostmask.  usage: markov ignore add <mask>; markov ignore remove <mask>; markov ignore list"
+      m.reply "have markov ignore the input from a hostmask or a channel.  usage: markov ignore add <mask or channel>; markov ignore remove <mask or channel>; markov ignore list"
     end
   end
 
@@ -168,9 +180,12 @@ class MarkovPlugin < Plugin
     return unless should_talk
 
     word1, word2 = message.split(/\s+/)
+    return unless word1 and word2
     line = generate_string(word1, word2)
     return unless line
-    return if line == message
+    # we do nothing if the line we return is just an initial substring
+    # of the line we received
+    return if message.index(line) == 0
     @bot.timer.add_once(delay) {
       m.reply line
     }
@@ -205,9 +220,7 @@ class MarkovPlugin < Plugin
   end
   
   def message(m)
-    return unless m.public?
-    return if m.address?
-    return if ignore? m.source
+    return if ignore? m
 
     # in channel message, the kind we are interested in
     message = clean_str m.plainmessage