module ::Irc
class Bot
class Wordlist
- def self.wordlist_base
- @@wordlist_base ||= File.join(Utils.bot.botclass, 'wordlists')
- end
-
- def self.get(path, options={})
+ def self.get(bot, where, options={})
+ wordlist_base = bot.path('wordlists')
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
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(bot, options={})
+ wordlist_base = bot.path('wordlists')
+ pattern = options[:pattern] || "**"
+ # refuse patterns that contain ../
+ return [] if pattern =~ /\.\.\//
+ striplen = wordlist_base.length+1
+ Dir.glob(File.join(wordlist_base, pattern)).map { |name|
+ name[striplen..-1]
+ }
+ end
+
+ def self.exist?(bot, path)
+ wordlist_base = bot.path('wordlists')
+ fn = path.to_s
+ # refuse to check outside of the wordlist base directory
+ return false if fn =~ /\.\.\//
+ File.exist?(File.join(wordlist_base, fn))
+ end
+
+end
end
end
-end
\ No newline at end of file