]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/lart.rb
plugin(lart): refactor to use registry to persist
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / lart.rb
index 9b627fc5ad836c469a93e1e51e26bd1bb578aad1..cb8e57b72444a0daa4f911697aecb726c3cf8d66 100644 (file)
@@ -1,81 +1,88 @@
-#  Original Author:
-#               Michael Brailsford  <brailsmt@yahoo.com>
-#               aka brailsmt
-#  Author:      Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
-#  Purpose:     Provide for humorous larts and praises
-#  Original Copyright:
-#               2002 Michael Brailsford.  All rights reserved.
-#  Copyright:   2006 Giuseppe Bilotta.  All rights reserved.
-#  License:     This plugin is licensed under the BSD license.  The terms of
-#               which follow.
+#-- vim:sw=2:et
+#++
 #
-#  Redistribution and use in source and binary forms, with or without
-#  modification, are permitted provided that the following conditions
-#  are met:
+# :title: lart/praise plugin for rbot
 #
-#  1. Redistributions of source code must retain the above copyright notice,
-#     this list of conditions and the following disclaimer.
+# Author::    Michael Brailsford  <brailsmt@yahoo.com> aka brailsmt
+# Author::    Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
 #
-#  2. Redistributions in binary form must reproduce the above copyright
-#     notice, this list of conditions and the following disclaimer in the
-#     documentation and/or other materials provided with the distribution.
+# Copyright:: (C) 2002 Michael Brailsford.  All rights reserved.
+# Copyright:: (C) 2006 Giuseppe Bilotta.  All rights reserved.
+#
+# License::  This plugin is licensed under the BSD license.  The terms of
+#            which follow.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+#    this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# Purpose::   Provide for humorous larts and praises
 
 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 = "#{@bot.botclass}/lart/larts"
-      @oldpraise = "#{@bot.botclass}/lart/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 "#{@bot.botclass}/lart/larts-#{lang}"
-    @praisefile.replace "#{@bot.botclass}/lart/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("#{@bot.botclass}/lart") if not FileTest.directory? "#{@bot.botclass}/lart"
-    # TODO implement safe saving here too
-    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="")
@@ -83,13 +90,16 @@ 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
     end
     who = params[:who].to_s
     reason = params[:why]
+    if who == "me"
+      who = m.sourcenick
+    end
     if who == @bot.nick
       who = m.sourcenick
       reason = "for trying to make me lart myself"
@@ -101,14 +111,15 @@ 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
     end
     who = params[:who].to_s
     reason = params[:why]
-    if who == m.sourcenick
+    if who == m.sourcenick || who == "me"
+      params[:who] = m.sourcenick
       params[:why] = "for praising himself"
       handle_lart(m, params)
       return
@@ -120,36 +131,56 @@ class LartPlugin < Plugin
   end
 
   def handle_addlart(m, params)
-    @larts << params[:lart]
+    @larts[@lang] << params[:lart].to_s
     @changed = true
     m.okay
   end
 
   def handle_rmlart(m, params)
-    @larts.delete params[:lart]
+    @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)
+    unless list.empty?
+      m.reply list.join(" | "), :split_at => /\s+\|\s+/
+    else
+      m.reply "no lart found matching #{params[:lart]}"
+    end
+  end
+
   def handle_addpraise(m, params)
-    @praises << params[:praise]
+    @praises[@lang] << params[:praise].to_s
     @changed = true
     m.okay
   end
 
   def handle_rmpraise(m, params)
-    @praises.delete params[:praise]
+    @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)
+    unless list.empty?
+      m.reply list.join(" | "), :split_at => /\s+\|\s+/
+    else
+      m.reply "no praise found matching #{params[:praise]}"
+    end
+  end
+
   #  The following are utils for larts/praises
   def replace_who(msg, nick)
     msg.gsub(/<who>/i, "#{nick}")
   end
 
   def get_msg_idx(max)
-    idx = rand(max)
+    rand(max)
   end
 
 end
@@ -164,3 +195,6 @@ plugin.map "addpraise *praise", :action => :handle_addpraise
 
 plugin.map "rmlart *lart", :action => :handle_rmlart
 plugin.map "rmpraise *praise", :action => :handle_rmpraise
+
+plugin.map "listlart *lart", :action => :handle_listlart
+plugin.map "listpraise *praise", :action => :handle_listpraise