]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/utils/wordlist.rb
httputil: remove obsolete version_1_2 declaration
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / wordlist.rb
index 63cc99a7c8808e04200f3b0b863e3ee8950d5f8f..81d7d775ef329f4058531cdb8444365a2d87a0a4 100755 (executable)
@@ -11,13 +11,13 @@ module ::Irc
 class Bot
 class Wordlist
   def self.wordlist_base
-    @@wordlist_base ||= File.join(Utils.bot.botclass, 'wordlists')
+    @@wordlist_base ||= Utils.bot.path 'wordlists'
   end
 
-  def self.get(path, options={})
+  def self.get(where, options={})
     opts = { :spaces => false }.merge(options)
 
-    wordlist_path = File.join(wordlist_base, path)
+    wordlist_path = File.join(wordlist_base, where)
     raise "wordlist not found: #{wordlist_path}" unless File.exist?(wordlist_path)
 
     # Location is a directory -> combine all lists beneath it
@@ -33,7 +33,8 @@ class Wordlist
       File.readlines(wordlist_path)
     end
 
-    wordlist.map! { |l| l.strip }
+    # wordlists are assumed to be UTF-8, but we need to strip the BOM, if present
+    wordlist.map! { |l| l.sub("\xef\xbb\xbf",'').strip }
     wordlist.reject do |word|
       word =~ /\s/ && !opts[:spaces] ||
       word.empty?
@@ -52,6 +53,14 @@ class Wordlist
       name[striplen..-1]
     }
   end
+
+  def self.exist?(path)
+    fn = path.to_s
+    # refuse to check outside of the wordlist base directory
+    return false if fn =~ /\.\.\//
+    File.exist?(File.join(self.wordlist_base, fn))
+  end
+
 end
 end
 end