]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
plugin(lart): refactor to use registry to persist
authorMatthias Hecker <mail@apoc.cc>
Thu, 16 Apr 2020 21:59:08 +0000 (23:59 +0200)
committerMatthias Hecker <mail@apoc.cc>
Thu, 16 Apr 2020 21:59:08 +0000 (23:59 +0200)
see #42

13 files changed:
data/rbot/plugins/lart.rb
data/rbot/plugins/lart/larts-english [new file with mode: 0644]
data/rbot/plugins/lart/larts-italian [new file with mode: 0644]
data/rbot/plugins/lart/larts-japanese [new file with mode: 0644]
data/rbot/plugins/lart/praises-english [new file with mode: 0644]
data/rbot/plugins/lart/praises-italian [new file with mode: 0644]
data/rbot/plugins/lart/praises-japanese [new file with mode: 0644]
data/rbot/templates/lart/larts-english [deleted file]
data/rbot/templates/lart/larts-italian [deleted file]
data/rbot/templates/lart/larts-japanese [deleted file]
data/rbot/templates/lart/praises-english [deleted file]
data/rbot/templates/lart/praises-italian [deleted file]
data/rbot/templates/lart/praises-japanese [deleted file]

index aeaad7333246153b731d6e613f1e60ac0ba1d32a..cb8e57b72444a0daa4f911697aecb726c3cf8d66 100644 (file)
 class LartPlugin < Plugin
 
   def initialize
-    @larts = Array.new
-    @praises = Array.new
-    @lartfile = ""
-    @praisefile = ""
-    @changed = false
+    @larts = nil
+    @praises = nil
+
     super
+    # after intialization #set_language is called with the language set in the bot configuration
+    # this loads the larts/praises from the registry
+  end
+
+  def load_static_files(path, suffix)
+    entries = {}
+    Dir.glob("#{path}/#{suffix}-*").each { |filename|
+      language = filename[filename.rindex('-')+1..-1]
+      entries[language] = File.readlines(filename).map(&:chomp)
+    }
+    entries
   end
 
   def set_language(lang)
     save
 
-    # We may be on an old installation, so on the first run read non-language-specific larts
-    unless defined?(@oldlart)
-      @oldlart = datafile 'larts'
-      @oldpraise = datafile 'praise'
-    end
+    @lang = lang
+    @larts = @registry[:larts]
+    @praises = @registry[:praises]
+
+    # for migrations try to read lart from bot data first (this is usually in ~/.rbot/lart:
+    if not @larts or not @praises
+      log "migrate existing larts or praises from #{datafile}"
 
-    @lartfile.replace(datafile("larts-#{lang}"))
-    @praisefile.replace(datafile("praises-#{lang}"))
-    @larts.clear
-    @praises.clear
-    if File.exists? @lartfile
-      IO.foreach(@lartfile) { |line|
-        @larts << line.chomp
-      }
-    elsif File.exists? @oldlart
-      IO.foreach(@oldlart) { |line|
-        @larts << line.chomp
-      }
+      @larts = load_static_files(datafile, 'larts')
+      @praises = load_static_files(datafile, 'praises')
     end
-    if File.exists? @praisefile
-      IO.foreach(@praisefile) { |line|
-        @praises << line.chomp
-      }
-    elsif File.exists? @oldpraise
-      IO.foreach(@oldpraise) { |line|
-        @praises << line.chomp
-      }
+
+    # without migrations, the initial data files we load from is located in the plugin directory
+    # that is the directory in which the plugin file itself is located (.../data/rbot/plugins/ usually)
+    if not @larts or not @praises
+      log "load initial larts and praises from #{plugin_path}"
+
+      initial_path = File.join(plugin_path, 'lart')
+      @larts = load_static_files(initial_path, 'larts')
+      @praises = load_static_files(initial_path, 'praises')
     end
-    @changed = false
+  end
+
+  def larts
+    @larts[@lang]
+  end
+
+  def praises
+    @praises[@lang]
   end
 
   def save
-    return unless @changed
-    Dir.mkdir(datafile) unless FileTest.directory? datafile
-    Utils.safe_save(@lartfile) { |file|
-      file.puts @larts
-    }
-    Utils.safe_save(@praisefile) { |file|
-      file.puts @praises
-    }
-    @changed = false
+    @registry[:larts] = @larts
+    @registry[:praises] = @praises
+    @registry.flush
   end
 
   def help(plugin, topic="")
@@ -87,7 +90,7 @@ class LartPlugin < Plugin
   end
 
   def handle_lart(m, params)
-    lart = @larts[get_msg_idx(@larts.length)]
+    lart = larts[get_msg_idx(larts.length)]
     if not lart
       m.reply "I dunno any larts"
       return
@@ -108,7 +111,7 @@ class LartPlugin < Plugin
   end
 
   def handle_praise(m, params)
-    praise = @praises[get_msg_idx(@praises.length)]
+    praise = praises[get_msg_idx(praises.length)]
     if not praise
       m.reply "I dunno any praises"
       return
@@ -128,20 +131,20 @@ class LartPlugin < Plugin
   end
 
   def handle_addlart(m, params)
-    @larts << params[:lart].to_s
+    @larts[@lang] << params[:lart].to_s
     @changed = true
     m.okay
   end
 
   def handle_rmlart(m, params)
-    @larts.delete params[:lart].to_s
+    @larts[@lang].delete params[:lart].to_s
     @changed = true
     m.okay
   end
 
   def handle_listlart(m, params)
     rx = Regexp.new(params[:lart].to_s, true)
-    list = @larts.grep(rx)
+    list = larts.grep(rx)
     unless list.empty?
       m.reply list.join(" | "), :split_at => /\s+\|\s+/
     else
@@ -150,20 +153,20 @@ class LartPlugin < Plugin
   end
 
   def handle_addpraise(m, params)
-    @praises << params[:praise].to_s
+    @praises[@lang] << params[:praise].to_s
     @changed = true
     m.okay
   end
 
   def handle_rmpraise(m, params)
-    @praises.delete params[:praise].to_s
+    @praises[@lang].delete params[:praise].to_s
     @changed = true
     m.okay
   end
 
   def handle_listpraise(m, params)
     rx = Regexp.new(params[:praise].to_s, true)
-    list = @praises.grep(rx)
+    list = praises.grep(rx)
     unless list.empty?
       m.reply list.join(" | "), :split_at => /\s+\|\s+/
     else
@@ -177,7 +180,7 @@ class LartPlugin < Plugin
   end
 
   def get_msg_idx(max)
-    idx = rand(max)
+    rand(max)
   end
 
 end
diff --git a/data/rbot/plugins/lart/larts-english b/data/rbot/plugins/lart/larts-english
new file mode 100644 (file)
index 0000000..c3a2588
--- /dev/null
@@ -0,0 +1,98 @@
+swaps <who>'s shampoo with nair
+installs windows on <who>'s machine
+forces <who> to use perl for 3 weeks
+registers <who>'s name with 50 known spammers
+resizes <who>'s terminal to 40x24
+takes <who>'s tea
+dispenses <who>'s email address to a few hundred 'bulk mailing services'
+pokes <who> in the eye
+beats <who> senseless with a 50lb Unix manual
+cats /dev/urandom into <who>'s ear
+signs <who> up for AOL
+enrolls <who> in Visual Basic 101
+sporks <who>
+drops a truckload of VAXen on <who>
+judo chops <who>
+resizes <who>'s terminal to 40x24
+formats <who>'s harddrive to fat12
+rm -rf's <who>
+stabs <who>
+steals <who>'s mojo
+strangles <who> with a doohicky mouse cord
+whacks <who> with the cluebat
+sells <who> on E-Bay
+uses <who> as a biological warfare study
+uses the "Customer Appreciation Bat" on <who>
+reads <who> some vogon poetry
+puts <who> in the Total Perspective Vortex
+casts <who> into the fires of Mt. Doom
+gives <who> a melvin
+turns over <who> to Agent Smith to be "bugged"
+takes away <who>'s internet connection
+pushes <who> past the Shoe Event Horizon
+counts "1, 2, 5... er... 3!" and hurls the Holy Handgrenade Of Antioch at <who>
+puts <who> in a nest of camel spiders
+makes <who> read slashdot at -1
+puts "alias vim=emacs" in <who>'s /etc/profile
+uninstalls ld from <who>'s system
+locks <who> in the Chateau d'If
+signs <who> up for getting hit on the head lessons
+makes <who> try to set up a Lexmark printer
+fills <who>'s Visene eyedrop bottle with lime juice
+casts <who> into the fires of Mt. Doom.
+gives <who> a Chicago Steamer
+rips off <who>'s arm, and uses it to beat them to death
+pierces <who>'s nose with a rusty paper hole puncher
+pokes <who> with a rusty nail
+puts sugar between <who>'s bedsheets
+pours sand into <who>'s oatmeal
+mixes epoxy into <who>'s toothpaste
+puts Icy-Hot in <who>'s lip balm
+straps <who> to a chair, and plays a endless low bitrate MP3 loop of "the world's most annoying sound" from "Dumb and Dumber"
+tells Dr. Dre that <who> was talking smack
+forces <who> to use a Commodore 64 for all their word processing
+smacks <who> in the face with a burlap sack full of broken glass
+puts <who> in a room with several heavily armed manic depressives
+makes <who> watch reruns of "Blue's Clues"
+puts lye in <who>'s coffee
+tattoos the Windows symbol on <who>'s ass
+lets Borg have his way with <who>
+signs <who> up for line dancing classes at the local senior center
+wakes <who> out of a sound sleep with some brand new nipple piercings
+gives <who> a 2 guage Prince Albert
+forces <who> to eat all their veggies
+covers <who>'s toilet paper with lemon-pepper
+fills <who>'s ketchup bottle with Dave's Insanity sauce
+forces <who> to stare at an incredibly frustrating and seemingly neverending IRC political debate
+knocks two of <who>'s teeth out with a 2x4
+removes debian from <who>'s system
+uses <who>'s debian cds for skeet shooting practice
+gives <who>'s phone number to Borg
+posts <who>'s IP and root password on alt.2600
+forces <who> to use words like "irregardless" and "administrate" (thereby sounding like a real dumbass)"
+tickles <who> until they wet their pants and pass out
+replaces <who>'s KY with elmer's clear wood glue
+replaces <who>'s TUMS with alka-seltzer tablets
+squeezes habanero pepper juice into <who>'s tub of vaseline for <who>
+submits <who>'s photo to the people at SA for photoshopping
+Forces <who> to learn the Win32 API
+gives <who> an atomic wedgie
+ties <who> to a chair and forces them to listen to 'N Sync at full blast
+forces <who> to use emacs for text editing
+frowns at <who> really really hard
+jabs a hot car lighter into <who>'s eye sockets
+forces <who> to browse the web with IE
+takes <who> out at the knees with a broken pool cue
+forces <who> to listen to only emo music
+signs <who> up for the Iowa State Ferret Legging Championship
+attempts to hotswap <who>'s RAM
+donkey punches <who>
+puts track spikes into <who>'s side
+replaces <who>'s Astroglide with JB Weld
+replaces <who>'s hypertension pills with rat poison pellets
+replaces <who>s jock itch cream with Nair
+does the Australian Death Grip on <who>
+dances upon the grave of <who>'s ancestors.
+farts in <who>'s general direction
+flogs <who> with stinging neddle
+hands <who> a poison ivy joint
diff --git a/data/rbot/plugins/lart/larts-italian b/data/rbot/plugins/lart/larts-italian
new file mode 100644 (file)
index 0000000..bc4b4e0
--- /dev/null
@@ -0,0 +1,25 @@
+scambia lo shampoo di <who> con crema depilatoria
+installa Windows 3.1 sul computer di <who>
+costringe <who> a programmare in Perl per 3 settimane
+manda l'indirizzo email di <who> a 50 noti spammer
+restringe il terminale di <who> a 40x24
+ruba il the di <who>
+picchia <who> a sangue sulle gengive con una mazza ferrata arrugginita
+vende <who> su eBay
+ficca un peperoncino su per il culo di <who>
+fa <who> a fettine
+punzecchia la bambola voodoo di <who>
+riformatta il disco fisso di <who>
+brucia la collezione di fumetti di <who>
+brucia la biblioteca di <who>
+brucia la macchina di <who>
+mette zucchero nel serbatoio della moto di <who>
+legge a <who> un po' di poesia Vogon
+svuota il frigorifero di <who>
+picchia <who> in faccia con la 'Ricerca' di Proust 
+usa <who> come cavia in esperimenti di guerra batteriologica
+pubblica su internet la foto di <who> sedut@ al cesso
+scrive il numero di telefono di <who> su tutti i cessi pubblici
+scollega la connessione ad internet di <who>
+strappa le braccia di <who> e le usa per picchiarl@ a morte
+nasconde un nido di vipere nel cuscino di <who>
diff --git a/data/rbot/plugins/lart/larts-japanese b/data/rbot/plugins/lart/larts-japanese
new file mode 100644 (file)
index 0000000..2798259
--- /dev/null
@@ -0,0 +1,10 @@
+<who>のパソコンにWindowsをインスタールする
+<who>に3週間Perlを使わせる
+<who>を叩き付ける
+<who>の手を剥して、その手で<who>に打つ
+Ebayで<who>を売る
+<who>のコーヒーに灰汁を入れる
+<who>をはらきりさせる
+<who>を刺身に作る
+<who>に心臓麻痺させる
+<who>の名前をデスノートに書く
diff --git a/data/rbot/plugins/lart/praises-english b/data/rbot/plugins/lart/praises-english
new file mode 100644 (file)
index 0000000..3d89df7
--- /dev/null
@@ -0,0 +1,5 @@
+hugs <who>
+gives <who> some love
+gives <who> a cookie
+slaps <who> heartily on the back
+tickles <who>
diff --git a/data/rbot/plugins/lart/praises-italian b/data/rbot/plugins/lart/praises-italian
new file mode 100644 (file)
index 0000000..9467cf9
--- /dev/null
@@ -0,0 +1,4 @@
+abbraccia <who>
+adora <who>
+invita <who> ad una cena luculliana
+da' un'amichevole pacca sulle spalle a <who>
diff --git a/data/rbot/plugins/lart/praises-japanese b/data/rbot/plugins/lart/praises-japanese
new file mode 100644 (file)
index 0000000..12c0abc
--- /dev/null
@@ -0,0 +1,3 @@
+<who>とチュウする
+<who>にクキーあげる
+<who>ダイスキ!
diff --git a/data/rbot/templates/lart/larts-english b/data/rbot/templates/lart/larts-english
deleted file mode 100644 (file)
index c3a2588..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-swaps <who>'s shampoo with nair
-installs windows on <who>'s machine
-forces <who> to use perl for 3 weeks
-registers <who>'s name with 50 known spammers
-resizes <who>'s terminal to 40x24
-takes <who>'s tea
-dispenses <who>'s email address to a few hundred 'bulk mailing services'
-pokes <who> in the eye
-beats <who> senseless with a 50lb Unix manual
-cats /dev/urandom into <who>'s ear
-signs <who> up for AOL
-enrolls <who> in Visual Basic 101
-sporks <who>
-drops a truckload of VAXen on <who>
-judo chops <who>
-resizes <who>'s terminal to 40x24
-formats <who>'s harddrive to fat12
-rm -rf's <who>
-stabs <who>
-steals <who>'s mojo
-strangles <who> with a doohicky mouse cord
-whacks <who> with the cluebat
-sells <who> on E-Bay
-uses <who> as a biological warfare study
-uses the "Customer Appreciation Bat" on <who>
-reads <who> some vogon poetry
-puts <who> in the Total Perspective Vortex
-casts <who> into the fires of Mt. Doom
-gives <who> a melvin
-turns over <who> to Agent Smith to be "bugged"
-takes away <who>'s internet connection
-pushes <who> past the Shoe Event Horizon
-counts "1, 2, 5... er... 3!" and hurls the Holy Handgrenade Of Antioch at <who>
-puts <who> in a nest of camel spiders
-makes <who> read slashdot at -1
-puts "alias vim=emacs" in <who>'s /etc/profile
-uninstalls ld from <who>'s system
-locks <who> in the Chateau d'If
-signs <who> up for getting hit on the head lessons
-makes <who> try to set up a Lexmark printer
-fills <who>'s Visene eyedrop bottle with lime juice
-casts <who> into the fires of Mt. Doom.
-gives <who> a Chicago Steamer
-rips off <who>'s arm, and uses it to beat them to death
-pierces <who>'s nose with a rusty paper hole puncher
-pokes <who> with a rusty nail
-puts sugar between <who>'s bedsheets
-pours sand into <who>'s oatmeal
-mixes epoxy into <who>'s toothpaste
-puts Icy-Hot in <who>'s lip balm
-straps <who> to a chair, and plays a endless low bitrate MP3 loop of "the world's most annoying sound" from "Dumb and Dumber"
-tells Dr. Dre that <who> was talking smack
-forces <who> to use a Commodore 64 for all their word processing
-smacks <who> in the face with a burlap sack full of broken glass
-puts <who> in a room with several heavily armed manic depressives
-makes <who> watch reruns of "Blue's Clues"
-puts lye in <who>'s coffee
-tattoos the Windows symbol on <who>'s ass
-lets Borg have his way with <who>
-signs <who> up for line dancing classes at the local senior center
-wakes <who> out of a sound sleep with some brand new nipple piercings
-gives <who> a 2 guage Prince Albert
-forces <who> to eat all their veggies
-covers <who>'s toilet paper with lemon-pepper
-fills <who>'s ketchup bottle with Dave's Insanity sauce
-forces <who> to stare at an incredibly frustrating and seemingly neverending IRC political debate
-knocks two of <who>'s teeth out with a 2x4
-removes debian from <who>'s system
-uses <who>'s debian cds for skeet shooting practice
-gives <who>'s phone number to Borg
-posts <who>'s IP and root password on alt.2600
-forces <who> to use words like "irregardless" and "administrate" (thereby sounding like a real dumbass)"
-tickles <who> until they wet their pants and pass out
-replaces <who>'s KY with elmer's clear wood glue
-replaces <who>'s TUMS with alka-seltzer tablets
-squeezes habanero pepper juice into <who>'s tub of vaseline for <who>
-submits <who>'s photo to the people at SA for photoshopping
-Forces <who> to learn the Win32 API
-gives <who> an atomic wedgie
-ties <who> to a chair and forces them to listen to 'N Sync at full blast
-forces <who> to use emacs for text editing
-frowns at <who> really really hard
-jabs a hot car lighter into <who>'s eye sockets
-forces <who> to browse the web with IE
-takes <who> out at the knees with a broken pool cue
-forces <who> to listen to only emo music
-signs <who> up for the Iowa State Ferret Legging Championship
-attempts to hotswap <who>'s RAM
-donkey punches <who>
-puts track spikes into <who>'s side
-replaces <who>'s Astroglide with JB Weld
-replaces <who>'s hypertension pills with rat poison pellets
-replaces <who>s jock itch cream with Nair
-does the Australian Death Grip on <who>
-dances upon the grave of <who>'s ancestors.
-farts in <who>'s general direction
-flogs <who> with stinging neddle
-hands <who> a poison ivy joint
diff --git a/data/rbot/templates/lart/larts-italian b/data/rbot/templates/lart/larts-italian
deleted file mode 100644 (file)
index bc4b4e0..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-scambia lo shampoo di <who> con crema depilatoria
-installa Windows 3.1 sul computer di <who>
-costringe <who> a programmare in Perl per 3 settimane
-manda l'indirizzo email di <who> a 50 noti spammer
-restringe il terminale di <who> a 40x24
-ruba il the di <who>
-picchia <who> a sangue sulle gengive con una mazza ferrata arrugginita
-vende <who> su eBay
-ficca un peperoncino su per il culo di <who>
-fa <who> a fettine
-punzecchia la bambola voodoo di <who>
-riformatta il disco fisso di <who>
-brucia la collezione di fumetti di <who>
-brucia la biblioteca di <who>
-brucia la macchina di <who>
-mette zucchero nel serbatoio della moto di <who>
-legge a <who> un po' di poesia Vogon
-svuota il frigorifero di <who>
-picchia <who> in faccia con la 'Ricerca' di Proust 
-usa <who> come cavia in esperimenti di guerra batteriologica
-pubblica su internet la foto di <who> sedut@ al cesso
-scrive il numero di telefono di <who> su tutti i cessi pubblici
-scollega la connessione ad internet di <who>
-strappa le braccia di <who> e le usa per picchiarl@ a morte
-nasconde un nido di vipere nel cuscino di <who>
diff --git a/data/rbot/templates/lart/larts-japanese b/data/rbot/templates/lart/larts-japanese
deleted file mode 100644 (file)
index 2798259..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-<who>のパソコンにWindowsをインスタールする
-<who>に3週間Perlを使わせる
-<who>を叩き付ける
-<who>の手を剥して、その手で<who>に打つ
-Ebayで<who>を売る
-<who>のコーヒーに灰汁を入れる
-<who>をはらきりさせる
-<who>を刺身に作る
-<who>に心臓麻痺させる
-<who>の名前をデスノートに書く
diff --git a/data/rbot/templates/lart/praises-english b/data/rbot/templates/lart/praises-english
deleted file mode 100644 (file)
index 3d89df7..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-hugs <who>
-gives <who> some love
-gives <who> a cookie
-slaps <who> heartily on the back
-tickles <who>
diff --git a/data/rbot/templates/lart/praises-italian b/data/rbot/templates/lart/praises-italian
deleted file mode 100644 (file)
index 9467cf9..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-abbraccia <who>
-adora <who>
-invita <who> ad una cena luculliana
-da' un'amichevole pacca sulle spalle a <who>
diff --git a/data/rbot/templates/lart/praises-japanese b/data/rbot/templates/lart/praises-japanese
deleted file mode 100644 (file)
index 12c0abc..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-<who>とチュウする
-<who>にクキーあげる
-<who>ダイスキ!