]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/keywords.rb
bans plugin: fix badword regular expression creation
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / keywords.rb
index 69f3533708f9b7e1007d2f3ae99806a9afbff567..255d7fe6886a2c56fcc6bd90f021a65cf23cc181 100644 (file)
@@ -283,21 +283,29 @@ class Keywords < Plugin
   # like "foo is bar" or "foo is also qux"
   def keyword_command(m, lhs, mhs, rhs, quiet = false)
     debug "got keyword command #{lhs}, #{mhs}, #{rhs}"
+    return if lhs.strip.empty?
 
+    overwrite = false
+    overwrite = true if(lhs.gsub!(/^no,\s*/, ""))
+    also = false
     also = true if(rhs.gsub!(/^also\s+/, ""))
 
     values = rhs.split(/\s+\|\s+/)
     lhs = Keyword.unescape lhs
 
-    if(also && has_key?(lhs))
+    if(overwrite || also || !has_key?(lhs))
+      if(also && has_key?(lhs))
+        kw = self[lhs]
+        kw << values
+        @keywords[lhs] = kw.dump
+      else
+        @keywords[lhs] = Keyword.new(mhs, values).dump
+      end
+      m.okay if !quiet
+    elsif(has_key?(lhs))
       kw = self[lhs]
-      kw << values
-      @keywords[lhs] = kw.dump
-    else
-      @keywords[lhs] = Keyword.new(mhs, values).dump
+      m.reply "but #{lhs} #{kw.type} #{kw.desc}" if kw && !quiet
     end
-
-    @bot.okay m.target if !quiet
   end
 
   # return help string for Keywords with option topic +topic+
@@ -334,6 +342,8 @@ class Keywords < Plugin
       'forget <keyword> => forget a keyword'
     when "tell"
       'tell <nick> about <keyword> => tell somebody about a keyword'
+    when "learn"
+      'learn that <keyword> is/are <definition> => define a keyword, definition can contain "|" to separate multiple randomly chosen replies'
     else
       'keyword module (fact learning and regurgitation) topics: lookup, set, forget, tell, search, listen, address, <reply>, <action>, <who>, <topic>'
     end
@@ -470,15 +480,19 @@ class Keywords < Plugin
       else
         m.reply "wrong 'tell' syntax"
       end
+    when "learn"
+      if m.params =~ /^that\s+(.+?)\s+(is|are)\s+(.+)$/
+        keyword_command(m, $1, $2, $3) if @bot.auth.allow?('keycmd', m.source, m.replyto)
+      else
+        m.reply "wrong 'learn' syntax"
+      end
     end
   end
 
-  def listen(m)
-    return if m.address?
-    # in channel message, not to me
+  def unreplied(m)
     # TODO option to do if(m.message =~ /^(.*)$/, ie try any line as a
     # keyword lookup.
-    if !@bot.config["keyword.address"] && m.message =~ /^(.*\S)\s*\?\s*$/
+    if m.message =~ /^(.*\S)\s*\?\s*$/ and (m.address? or not @bot.config["keyword.address"])
       keyword_lookup m, $1, true if @bot.auth.allow?("keyword", m.source)
     elsif @bot.config["keyword.listen"] && (m.message =~ /^(.*?)\s+(is|are)\s+(.*)$/)
       # TODO MUCH more selective on what's allowed here
@@ -491,4 +505,5 @@ plugin = Keywords.new
 plugin.register 'keyword'
 plugin.register 'forget'
 plugin.register 'tell'
+plugin.register 'learn'