From 14b2b331c639f4484b377966e5e1a04c15082404 Mon Sep 17 00:00:00 2001 From: Matthias Hecker Date: Thu, 16 Apr 2020 23:59:08 +0200 Subject: plugin(lart): refactor to use registry to persist see #42 --- data/rbot/plugins/lart.rb | 101 +++++++++++++++--------------- data/rbot/plugins/lart/larts-english | 98 +++++++++++++++++++++++++++++ data/rbot/plugins/lart/larts-italian | 25 ++++++++ data/rbot/plugins/lart/larts-japanese | 10 +++ data/rbot/plugins/lart/praises-english | 5 ++ data/rbot/plugins/lart/praises-italian | 4 ++ data/rbot/plugins/lart/praises-japanese | 3 + data/rbot/templates/lart/larts-english | 98 ----------------------------- data/rbot/templates/lart/larts-italian | 25 -------- data/rbot/templates/lart/larts-japanese | 10 --- data/rbot/templates/lart/praises-english | 5 -- data/rbot/templates/lart/praises-italian | 4 -- data/rbot/templates/lart/praises-japanese | 3 - 13 files changed, 197 insertions(+), 194 deletions(-) create mode 100644 data/rbot/plugins/lart/larts-english create mode 100644 data/rbot/plugins/lart/larts-italian create mode 100644 data/rbot/plugins/lart/larts-japanese create mode 100644 data/rbot/plugins/lart/praises-english create mode 100644 data/rbot/plugins/lart/praises-italian create mode 100644 data/rbot/plugins/lart/praises-japanese delete mode 100644 data/rbot/templates/lart/larts-english delete mode 100644 data/rbot/templates/lart/larts-italian delete mode 100644 data/rbot/templates/lart/larts-japanese delete mode 100644 data/rbot/templates/lart/praises-english delete mode 100644 data/rbot/templates/lart/praises-italian delete mode 100644 data/rbot/templates/lart/praises-japanese diff --git a/data/rbot/plugins/lart.rb b/data/rbot/plugins/lart.rb index aeaad733..cb8e57b7 100644 --- a/data/rbot/plugins/lart.rb +++ b/data/rbot/plugins/lart.rb @@ -28,58 +28,61 @@ 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 index 00000000..c3a25880 --- /dev/null +++ b/data/rbot/plugins/lart/larts-english @@ -0,0 +1,98 @@ +swaps 's shampoo with nair +installs windows on 's machine +forces to use perl for 3 weeks +registers 's name with 50 known spammers +resizes 's terminal to 40x24 +takes 's tea +dispenses 's email address to a few hundred 'bulk mailing services' +pokes in the eye +beats senseless with a 50lb Unix manual +cats /dev/urandom into 's ear +signs up for AOL +enrolls in Visual Basic 101 +sporks +drops a truckload of VAXen on +judo chops +resizes 's terminal to 40x24 +formats 's harddrive to fat12 +rm -rf's +stabs +steals 's mojo +strangles with a doohicky mouse cord +whacks with the cluebat +sells on E-Bay +uses as a biological warfare study +uses the "Customer Appreciation Bat" on +reads some vogon poetry +puts in the Total Perspective Vortex +casts into the fires of Mt. Doom +gives a melvin +turns over to Agent Smith to be "bugged" +takes away 's internet connection +pushes past the Shoe Event Horizon +counts "1, 2, 5... er... 3!" and hurls the Holy Handgrenade Of Antioch at +puts in a nest of camel spiders +makes read slashdot at -1 +puts "alias vim=emacs" in 's /etc/profile +uninstalls ld from 's system +locks in the Chateau d'If +signs up for getting hit on the head lessons +makes try to set up a Lexmark printer +fills 's Visene eyedrop bottle with lime juice +casts into the fires of Mt. Doom. +gives a Chicago Steamer +rips off 's arm, and uses it to beat them to death +pierces 's nose with a rusty paper hole puncher +pokes with a rusty nail +puts sugar between 's bedsheets +pours sand into 's oatmeal +mixes epoxy into 's toothpaste +puts Icy-Hot in 's lip balm +straps 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 was talking smack +forces to use a Commodore 64 for all their word processing +smacks in the face with a burlap sack full of broken glass +puts in a room with several heavily armed manic depressives +makes watch reruns of "Blue's Clues" +puts lye in 's coffee +tattoos the Windows symbol on 's ass +lets Borg have his way with +signs up for line dancing classes at the local senior center +wakes out of a sound sleep with some brand new nipple piercings +gives a 2 guage Prince Albert +forces to eat all their veggies +covers 's toilet paper with lemon-pepper +fills 's ketchup bottle with Dave's Insanity sauce +forces to stare at an incredibly frustrating and seemingly neverending IRC political debate +knocks two of 's teeth out with a 2x4 +removes debian from 's system +uses 's debian cds for skeet shooting practice +gives 's phone number to Borg +posts 's IP and root password on alt.2600 +forces to use words like "irregardless" and "administrate" (thereby sounding like a real dumbass)" +tickles until they wet their pants and pass out +replaces 's KY with elmer's clear wood glue +replaces 's TUMS with alka-seltzer tablets +squeezes habanero pepper juice into 's tub of vaseline for +submits 's photo to the people at SA for photoshopping +Forces to learn the Win32 API +gives an atomic wedgie +ties to a chair and forces them to listen to 'N Sync at full blast +forces to use emacs for text editing +frowns at really really hard +jabs a hot car lighter into 's eye sockets +forces to browse the web with IE +takes out at the knees with a broken pool cue +forces to listen to only emo music +signs up for the Iowa State Ferret Legging Championship +attempts to hotswap 's RAM +donkey punches +puts track spikes into 's side +replaces 's Astroglide with JB Weld +replaces 's hypertension pills with rat poison pellets +replaces s jock itch cream with Nair +does the Australian Death Grip on +dances upon the grave of 's ancestors. +farts in 's general direction +flogs with stinging neddle +hands a poison ivy joint diff --git a/data/rbot/plugins/lart/larts-italian b/data/rbot/plugins/lart/larts-italian new file mode 100644 index 00000000..bc4b4e05 --- /dev/null +++ b/data/rbot/plugins/lart/larts-italian @@ -0,0 +1,25 @@ +scambia lo shampoo di con crema depilatoria +installa Windows 3.1 sul computer di +costringe a programmare in Perl per 3 settimane +manda l'indirizzo email di a 50 noti spammer +restringe il terminale di a 40x24 +ruba il the di +picchia a sangue sulle gengive con una mazza ferrata arrugginita +vende su eBay +ficca un peperoncino su per il culo di +fa a fettine +punzecchia la bambola voodoo di +riformatta il disco fisso di +brucia la collezione di fumetti di +brucia la biblioteca di +brucia la macchina di +mette zucchero nel serbatoio della moto di +legge a un po' di poesia Vogon +svuota il frigorifero di +picchia in faccia con la 'Ricerca' di Proust +usa come cavia in esperimenti di guerra batteriologica +pubblica su internet la foto di sedut@ al cesso +scrive il numero di telefono di su tutti i cessi pubblici +scollega la connessione ad internet di +strappa le braccia di e le usa per picchiarl@ a morte +nasconde un nido di vipere nel cuscino di diff --git a/data/rbot/plugins/lart/larts-japanese b/data/rbot/plugins/lart/larts-japanese new file mode 100644 index 00000000..27982595 --- /dev/null +++ b/data/rbot/plugins/lart/larts-japanese @@ -0,0 +1,10 @@ +のパソコンにWindowsをインスタールする +に3週間Perlを使わせる +を叩き付ける +の手を剥して、その手でに打つ +Ebayでを売る +のコーヒーに灰汁を入れる +をはらきりさせる +を刺身に作る +に心臓麻痺させる +の名前をデスノートに書く diff --git a/data/rbot/plugins/lart/praises-english b/data/rbot/plugins/lart/praises-english new file mode 100644 index 00000000..3d89df74 --- /dev/null +++ b/data/rbot/plugins/lart/praises-english @@ -0,0 +1,5 @@ +hugs +gives some love +gives a cookie +slaps heartily on the back +tickles diff --git a/data/rbot/plugins/lart/praises-italian b/data/rbot/plugins/lart/praises-italian new file mode 100644 index 00000000..9467cf95 --- /dev/null +++ b/data/rbot/plugins/lart/praises-italian @@ -0,0 +1,4 @@ +abbraccia +adora +invita ad una cena luculliana +da' un'amichevole pacca sulle spalle a diff --git a/data/rbot/plugins/lart/praises-japanese b/data/rbot/plugins/lart/praises-japanese new file mode 100644 index 00000000..12c0abc3 --- /dev/null +++ b/data/rbot/plugins/lart/praises-japanese @@ -0,0 +1,3 @@ +とチュウする +にクキーあげる +ダイスキ! diff --git a/data/rbot/templates/lart/larts-english b/data/rbot/templates/lart/larts-english deleted file mode 100644 index c3a25880..00000000 --- a/data/rbot/templates/lart/larts-english +++ /dev/null @@ -1,98 +0,0 @@ -swaps 's shampoo with nair -installs windows on 's machine -forces to use perl for 3 weeks -registers 's name with 50 known spammers -resizes 's terminal to 40x24 -takes 's tea -dispenses 's email address to a few hundred 'bulk mailing services' -pokes in the eye -beats senseless with a 50lb Unix manual -cats /dev/urandom into 's ear -signs up for AOL -enrolls in Visual Basic 101 -sporks -drops a truckload of VAXen on -judo chops -resizes 's terminal to 40x24 -formats 's harddrive to fat12 -rm -rf's -stabs -steals 's mojo -strangles with a doohicky mouse cord -whacks with the cluebat -sells on E-Bay -uses as a biological warfare study -uses the "Customer Appreciation Bat" on -reads some vogon poetry -puts in the Total Perspective Vortex -casts into the fires of Mt. Doom -gives a melvin -turns over to Agent Smith to be "bugged" -takes away 's internet connection -pushes past the Shoe Event Horizon -counts "1, 2, 5... er... 3!" and hurls the Holy Handgrenade Of Antioch at -puts in a nest of camel spiders -makes read slashdot at -1 -puts "alias vim=emacs" in 's /etc/profile -uninstalls ld from 's system -locks in the Chateau d'If -signs up for getting hit on the head lessons -makes try to set up a Lexmark printer -fills 's Visene eyedrop bottle with lime juice -casts into the fires of Mt. Doom. -gives a Chicago Steamer -rips off 's arm, and uses it to beat them to death -pierces 's nose with a rusty paper hole puncher -pokes with a rusty nail -puts sugar between 's bedsheets -pours sand into 's oatmeal -mixes epoxy into 's toothpaste -puts Icy-Hot in 's lip balm -straps 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 was talking smack -forces to use a Commodore 64 for all their word processing -smacks in the face with a burlap sack full of broken glass -puts in a room with several heavily armed manic depressives -makes watch reruns of "Blue's Clues" -puts lye in 's coffee -tattoos the Windows symbol on 's ass -lets Borg have his way with -signs up for line dancing classes at the local senior center -wakes out of a sound sleep with some brand new nipple piercings -gives a 2 guage Prince Albert -forces to eat all their veggies -covers 's toilet paper with lemon-pepper -fills 's ketchup bottle with Dave's Insanity sauce -forces to stare at an incredibly frustrating and seemingly neverending IRC political debate -knocks two of 's teeth out with a 2x4 -removes debian from 's system -uses 's debian cds for skeet shooting practice -gives 's phone number to Borg -posts 's IP and root password on alt.2600 -forces to use words like "irregardless" and "administrate" (thereby sounding like a real dumbass)" -tickles until they wet their pants and pass out -replaces 's KY with elmer's clear wood glue -replaces 's TUMS with alka-seltzer tablets -squeezes habanero pepper juice into 's tub of vaseline for -submits 's photo to the people at SA for photoshopping -Forces to learn the Win32 API -gives an atomic wedgie -ties to a chair and forces them to listen to 'N Sync at full blast -forces to use emacs for text editing -frowns at really really hard -jabs a hot car lighter into 's eye sockets -forces to browse the web with IE -takes out at the knees with a broken pool cue -forces to listen to only emo music -signs up for the Iowa State Ferret Legging Championship -attempts to hotswap 's RAM -donkey punches -puts track spikes into 's side -replaces 's Astroglide with JB Weld -replaces 's hypertension pills with rat poison pellets -replaces s jock itch cream with Nair -does the Australian Death Grip on -dances upon the grave of 's ancestors. -farts in 's general direction -flogs with stinging neddle -hands a poison ivy joint diff --git a/data/rbot/templates/lart/larts-italian b/data/rbot/templates/lart/larts-italian deleted file mode 100644 index bc4b4e05..00000000 --- a/data/rbot/templates/lart/larts-italian +++ /dev/null @@ -1,25 +0,0 @@ -scambia lo shampoo di con crema depilatoria -installa Windows 3.1 sul computer di -costringe a programmare in Perl per 3 settimane -manda l'indirizzo email di a 50 noti spammer -restringe il terminale di a 40x24 -ruba il the di -picchia a sangue sulle gengive con una mazza ferrata arrugginita -vende su eBay -ficca un peperoncino su per il culo di -fa a fettine -punzecchia la bambola voodoo di -riformatta il disco fisso di -brucia la collezione di fumetti di -brucia la biblioteca di -brucia la macchina di -mette zucchero nel serbatoio della moto di -legge a un po' di poesia Vogon -svuota il frigorifero di -picchia in faccia con la 'Ricerca' di Proust -usa come cavia in esperimenti di guerra batteriologica -pubblica su internet la foto di sedut@ al cesso -scrive il numero di telefono di su tutti i cessi pubblici -scollega la connessione ad internet di -strappa le braccia di e le usa per picchiarl@ a morte -nasconde un nido di vipere nel cuscino di diff --git a/data/rbot/templates/lart/larts-japanese b/data/rbot/templates/lart/larts-japanese deleted file mode 100644 index 27982595..00000000 --- a/data/rbot/templates/lart/larts-japanese +++ /dev/null @@ -1,10 +0,0 @@ -のパソコンにWindowsをインスタールする -に3週間Perlを使わせる -を叩き付ける -の手を剥して、その手でに打つ -Ebayでを売る -のコーヒーに灰汁を入れる -をはらきりさせる -を刺身に作る -に心臓麻痺させる -の名前をデスノートに書く diff --git a/data/rbot/templates/lart/praises-english b/data/rbot/templates/lart/praises-english deleted file mode 100644 index 3d89df74..00000000 --- a/data/rbot/templates/lart/praises-english +++ /dev/null @@ -1,5 +0,0 @@ -hugs -gives some love -gives a cookie -slaps heartily on the back -tickles diff --git a/data/rbot/templates/lart/praises-italian b/data/rbot/templates/lart/praises-italian deleted file mode 100644 index 9467cf95..00000000 --- a/data/rbot/templates/lart/praises-italian +++ /dev/null @@ -1,4 +0,0 @@ -abbraccia -adora -invita ad una cena luculliana -da' un'amichevole pacca sulle spalle a diff --git a/data/rbot/templates/lart/praises-japanese b/data/rbot/templates/lart/praises-japanese deleted file mode 100644 index 12c0abc3..00000000 --- a/data/rbot/templates/lart/praises-japanese +++ /dev/null @@ -1,3 +0,0 @@ -とチュウする -にクキーあげる -ダイスキ! -- cgit v1.2.3