]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/keywords.rb
Initial factorization of botconfig into kernel functionality and a coremodule
[user/henk/code/ruby/rbot.git] / lib / rbot / keywords.rb
index 8065e8b807f13229bf12ee92b9dd47d8a72b3357..1604faf753d8a503bb5cded859688c8811ac7f84 100644 (file)
@@ -11,10 +11,10 @@ module Irc
 
     # type of keyword (e.g. "is" or "are")
     attr_reader :type
-    
+
     # type::   type of keyword (e.g "is" or "are")
     # values:: array of values
-    # 
+    #
     # create a keyword of type +type+ with values +values+
     def initialize(type, values)
       @type = type.downcase
@@ -73,7 +73,7 @@ module Irc
     end
   end
 
-  # keywords class. 
+  # keywords class.
   #
   # Handles all that stuff like "bot: foo is bar", "bot: foo?"
   #
@@ -87,7 +87,7 @@ module Irc
     BotConfig.register BotConfigBooleanValue.new('keyword.address',
       :default => true,
       :desc => "Should the bot require that keyword lookups are addressed to it? If not, the bot will attempt to lookup foo if someone says 'foo?' in channel")
-    
+
     # create a new Keywords instance, associated to bot +bot+
     def initialize(bot)
       @bot = bot
@@ -96,10 +96,10 @@ module Irc
       @keywords = DBTree.new bot, "keyword"
 
       scan
-      
+
       # import old format keywords into DBHash
       if(File.exist?("#{@bot.botclass}/keywords.rbot"))
-        puts "auto importing old keywords.rbot"
+        log "auto importing old keywords.rbot"
         IO.foreach("#{@bot.botclass}/keywords.rbot") do |line|
           if(line =~ /^(.*?)\s*<=(is|are)?=?>\s*(.*)$/)
             lhs = $1
@@ -128,15 +128,13 @@ module Irc
       # first scan for old DBHash files, and convert them
       Dir["#{@bot.botclass}/keywords/*"].each {|f|
         next unless f =~ /\.db$/
-        puts "upgrading keyword db #{f} (rbot 0.9.5 or prior) database format"
+        log "upgrading keyword db #{f} (rbot 0.9.5 or prior) database format"
         newname = f.gsub(/\.db$/, ".kdb")
         old = BDB::Hash.open f, nil, 
-                             "r+", 0600, "set_pagesize" => 1024,
-                             "set_cachesize" => [0, 32 * 1024, 0]
-        new = BDB::CIBtree.open newname, nil, 
-                                BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
-                                0600, "set_pagesize" => 1024,
-                                "set_cachesize" => [0, 32 * 1024, 0]
+                             "r+", 0600
+        new = BDB::CIBtree.open(newname, nil, 
+                                BDB::CREATE | BDB::EXCL,
+                                0600)
         old.each {|k,v|
           new[k] = v
         }
@@ -158,7 +156,7 @@ module Irc
       Dir["#{@bot.botclass}/keywords/*"].each {|f|
         next if f =~ /\.kdb$/
         next if f =~ /CVS$/
-        puts "auto converting keywords from #{f}"
+        log "auto converting keywords from #{f}"
         key = File.basename(f)
         unless @statickeywords.has_key?(key)
           @statickeywords[key] = DBHash.new @bot, "#{f}.db", true
@@ -184,14 +182,12 @@ module Irc
     # upgrade data files found in old rbot formats to current
     def upgrade_data
       if File.exist?("#{@bot.botclass}/keywords.db")
-        puts "upgrading old keywords (rbot 0.9.5 or prior) database format"
+        log "upgrading old keywords (rbot 0.9.5 or prior) database format"
         old = BDB::Hash.open "#{@bot.botclass}/keywords.db", nil, 
-                             "r+", 0600, "set_pagesize" => 1024,
-                             "set_cachesize" => [0, 32 * 1024, 0]
+                             "r+", 0600
         new = BDB::CIBtree.open "#{@bot.botclass}/keyword.db", nil, 
-                                BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
-                                0600, "set_pagesize" => 1024,
-                                "set_cachesize" => [0, 32 * 1024, 0]
+                                BDB::CREATE | BDB::EXCL,
+                                0600
         old.each {|k,v|
           new[k] = v
         }
@@ -250,6 +246,7 @@ module Irc
     # 
     # handle a message asking about a keyword
     def keyword(m, key, dunno=true)
+      return if key.nil?
        unless(kw = self[key])
          m.reply @bot.lang.get("dunno") if (dunno)
          return
@@ -386,38 +383,45 @@ module Irc
           full = false
           full = true if str.gsub!(/--full\s+/, "")
 
-          re = Regexp.new(str, Regexp::IGNORECASE)
-          if(@bot.auth.allow?("keyword", m.source, m.replyto))
-            matches = Array.new
-            @keywords.each {|k,v|
-              kw = Keyword.restore(v)
-              if re.match(k) || (full && re.match(kw.desc))
-                matches << [k,kw]
+          begin
+            re = Regexp.new(str, Regexp::IGNORECASE)
+            if(@bot.auth.allow?("keyword", m.source, m.replyto))
+              matches = Array.new
+              @keywords.each {|k,v|
+                kw = Keyword.restore(v)
+                if re.match(k) || (full && re.match(kw.desc))
+                  matches << [k,kw]
+                end
+              }
+              if all
+                @statickeywords.each {|k,v|
+                  v.each {|kk,vv|
+                    kw = Keyword.restore(vv)
+                    if re.match(kk) || (full && re.match(kw.desc))
+                      matches << [kk,kw]
+                    end
+                  }
+                }
               end
-            }
-            if all
-              @statickeywords.each {|k,v|
-                v.each {|kk,vv|
-                  kw = Keyword.restore(vv)
-                  if re.match(kk) || (full && re.match(kw.desc))
-                    matches << [kk,kw]
-                  end
+              if matches.length == 1
+                rkw = matches[0]
+                m.reply "#{rkw[0]} #{rkw[1].type} #{rkw[1].desc}"
+              elsif matches.length > 0
+                i = 0
+                matches.each {|rkw|
+                  m.reply "[#{i+1}/#{matches.length}] #{rkw[0]} #{rkw[1].type} #{rkw[1].desc}"
+                  i += 1
+                  break if i == 3
                 }
-              }
-            end
-            if matches.length == 1
-              rkw = matches[0]
-              m.reply "#{rkw[0]} #{rkw[1].type} #{rkw[1].desc}"
-            elsif matches.length > 0
-              i = 0
-              matches.each {|rkw|
-                m.reply "[#{i+1}/#{matches.length}] #{rkw[0]} #{rkw[1].type} #{rkw[1].desc}"
-                i += 1
-                break if i == 3
-              }
-            else
-              m.reply "no keywords match #{str}"
+              else
+                m.reply "no keywords match #{str}"
+              end
             end
+          rescue RegexpError => e
+            m.reply "no keywords match #{str}: #{e}"
+          rescue
+            debug e.inspect
+            m.reply "no keywords match #{str}: an error occurred"
           end
         end
       else