]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/utils/wordlist.rb
23e141ac3b32497941b09f5fb5a7471e992ce176
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / wordlist.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: rbot wordlist provider
5 #
6 # Author:: Raine Virta <rane@kapsi.fi>
7
8 require "find"
9
10 module ::Irc
11 class Bot
12 class Wordlist
13   def self.wordlist_base
14     @@wordlist_base ||= File.join(Utils.bot.botclass, 'wordlists')
15   end
16
17   def self.get(path, options={})
18     opts = { :spaces => false }.merge(options)
19
20     wordlist_path = File.join(wordlist_base, path)
21     raise "wordlist not found: #{wordlist_path}" unless File.exist?(wordlist_path)
22
23     # Location is a directory -> combine all lists beneath it
24     wordlist = if File.directory?(wordlist_path)
25       wordlists = []
26       Find.find(wordlist_path) do |path|
27         next if path == wordlist_path
28         wordlists << path unless File.directory?(path)
29       end
30
31       wordlists.map { |list| File.readlines(list) }.flatten
32     else
33       File.readlines(wordlist_path)
34     end
35
36     wordlist.map! { |l| l.strip }
37     wordlist.reject do |word|
38       word =~ /\s/ && !opts[:spaces] ||
39       word.empty?
40     end
41   end
42 end
43 end
44 end