X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fcore%2Futils%2Fwordlist.rb;h=81d7d775ef329f4058531cdb8444365a2d87a0a4;hb=bf9734ff89a238c5a63015b68eabd8d0ef9d1308;hp=23e141ac3b32497941b09f5fb5a7471e992ce176;hpb=0bf6eabdddddafecc42d56eaae1e0db35413390f;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/core/utils/wordlist.rb b/lib/rbot/core/utils/wordlist.rb index 23e141ac..81d7d775 100755 --- a/lib/rbot/core/utils/wordlist.rb +++ b/lib/rbot/core/utils/wordlist.rb @@ -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,12 +33,34 @@ 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? end end + + # Return an array with the list of available wordlists. + # Available options: + # pattern:: pattern that should be matched by the wordlist filename + def self.list(options={}) + pattern = options[:pattern] || "**" + # refuse patterns that contain ../ + return [] if pattern =~ /\.\.\// + striplen = self.wordlist_base.length+1 + Dir.glob(File.join(self.wordlist_base, pattern)).map { |name| + 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 -end \ No newline at end of file