]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
refactor: wordlist shouldn't use bot singleton #35
authorMatthias Hecker <mail@apoc.cc>
Wed, 15 Apr 2020 18:26:54 +0000 (20:26 +0200)
committerMatthias Hecker <mail@apoc.cc>
Wed, 15 Apr 2020 18:26:54 +0000 (20:26 +0200)
also related to #41 and #6

data/rbot/plugins/games/azgame.rb
data/rbot/plugins/games/hangman.rb
data/rbot/plugins/games/shiritori.rb
lib/rbot/core/utils/wordlist.rb [changed mode: 0755->0644]
lib/rbot/core/wordlist_ui.rb

index ca67881cb79424c3959ba89deb8eaf61a0e97d03..b4dbabc1b73a03573c8883112b89c3e8246119df 100644 (file)
@@ -157,9 +157,9 @@ class AzGamePlugin < Plugin
     lang = params[:lang]
     addlang = params[:addlang]
     autoadd = @autoadd_base + addlang.to_s
-    if Wordlist.exist?(lang)
+    if Wordlist.exist?(@bot, lang)
       # wordlists are assumed to be UTF-8, but we need to strip the BOM, if present
-      words = Wordlist.get(lang)
+      words = Wordlist.get(@bot, lang)
       if addlang and File.exist?(autoadd)
         word += File.readlines(autoadd).map {|line| line.sub("\xef\xbb\xbf",'').strip}
       end
@@ -600,7 +600,7 @@ class AzGamePlugin < Plugin
       return _("az => start a game if none is running, show the current word range otherwise; you can say 'az <language>' if you want to play in a language different from the current bot default")
     end
     langs = @rules.keys
-    wls = Wordlist.list
+    wls = Wordlist.list(@bot)
     return [
       _("az topics: play, rules, cancel, manage, check"),
       _("available languages: %{langs}") % { :langs => langs.join(", ") },
index 0177a3481177f0699c315fe2e5aec47ab36c3d86..890f821acfe1b123a71df7423bf42a2b66dca522 100644 (file)
@@ -253,7 +253,7 @@ class HangmanPlugin < Plugin
       params[:word].join(" ")
     elsif params[:wordlist]
       begin
-        wordlist = Wordlist.get(params[:wordlist].join("/"), :spaces => true)
+        wordlist = Wordlist.get(@bot, params[:wordlist].join("/"), :spaces => true)
       rescue
         raise _("no such wordlist")
       end
index 3cb3c7b5c3056e7f68e273142043f86b6451b46b..9ca23f9a264af5e299e54c2cf55041c07f8e7c8a 100644 (file)
@@ -389,9 +389,8 @@ class ShiritoriPlugin < Plugin
     unless ruleset.has_key?(:words)
       if ruleset.has_key?(:wordlist_file)
         begin
-          ruleset[:words] =
-            File.new(datafile(ruleset[:wordlist_file])).grep(
-              ruleset[:listen]) {|l| ruleset[:normalize].call l.chomp}
+          ruleset[:words] = Wordlist.get(@bot, ruleset[:wordlist_file], :spaces => true)
+            .grep(ruleset[:listen]) {|l| ruleset[:normalize].call l.chomp}
         rescue
           raise "unable to load word list"
         end
old mode 100755 (executable)
new mode 100644 (file)
index 81d7d77..339a321
@@ -10,11 +10,8 @@ require "find"
 module ::Irc
 class Bot
 class Wordlist
-  def self.wordlist_base
-    @@wordlist_base ||= Utils.bot.path 'wordlists'
-  end
-
-  def self.get(where, options={})
+  def self.get(bot, where, options={})
+    wordlist_base = bot.path('wordlists')
     opts = { :spaces => false }.merge(options)
 
     wordlist_path = File.join(wordlist_base, where)
@@ -44,21 +41,23 @@ class Wordlist
   # 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={})
+  def self.list(bot, options={})
+    wordlist_base = bot.path('wordlists')
     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|
+    striplen = wordlist_base.length+1
+    Dir.glob(File.join(wordlist_base, pattern)).map { |name|
       name[striplen..-1]
     }
   end
 
-  def self.exist?(path)
+  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(self.wordlist_base, fn))
+    File.exist?(File.join(wordlist_base, fn))
   end
 
 end
index 20ccc228b03d3dbad7c7325cc0daa100d65cc2d4..8042089a754fc89184b52da77bf370090d39734a 100644 (file)
@@ -11,12 +11,14 @@ class WordlistModule < CoreBotModule
   end
 
   def do_list(m, p)
-    found = Wordlist.list(p)
+    found = Wordlist.list(@bot, p)
     if found.empty?
-      m.reply _("no wordlist found")
+      m.reply _("no wordlists found in %{path}") % {
+        path: @bot.path('wordlists')
+      }
     else
       m.reply _("Wordlists: %{found}") % {
-        :found => found.sort.join(', ')
+        found: found.sort.join(', ')
       }
     end
   end