]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/lart.rb
plugin(oxford): moved to lexico.com, closes #13
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / lart.rb
index 69a9e8cc26839e176f8be3e370ae7bd2064a483a..aeaad7333246153b731d6e613f1e60ac0ba1d32a 100644 (file)
@@ -1,41 +1,32 @@
-#  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
 
-  # Keep a 1:1 relation between commands and handlers
-  @@handlers = {
-    "lart" => "handle_lart",
-    "praise" => "handle_praise",
-    "addlart" => "handle_addlart",
-    "rmlart" => "handle_rmlart",
-    "addpraise" => "handle_addpraise",
-    "rmpraise" => "handle_rmpraise"
-  }
-
-  def name
-    "lart"
-  end
-
   def initialize
     @larts = Array.new
     @praises = Array.new
@@ -47,19 +38,23 @@ class LartPlugin < Plugin
 
   def set_language(lang)
     save
-    @lartfile.replace "#{@bot.botclass}/lart/larts-#{lang}"
-    @praisefile.replace "#{@bot.botclass}/lart/praises-#{lang}"
+
     # We may be on an old installation, so on the first run read non-language-specific larts
-    @bulart.replace "#{@bot.botclass}/lart/larts"
-    @bupraise.replace "#{@bot.botclass}/lart/praise"
+    unless defined?(@oldlart)
+      @oldlart = datafile 'larts'
+      @oldpraise = datafile 'praise'
+    end
+
+    @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? @bulart
-      IO.foreach(@bulart) { |line|
+    elsif File.exists? @oldlart
+      IO.foreach(@oldlart) { |line|
         @larts << line.chomp
       }
     end
@@ -67,21 +62,17 @@ class LartPlugin < Plugin
       IO.foreach(@praisefile) { |line|
         @praises << line.chomp
       }
-    elsif File.exists? @bupraise
-      IO.foreach(@bupraise) { |line|
+    elsif File.exists? @oldpraise
+      IO.foreach(@oldpraise) { |line|
         @praises << line.chomp
       }
     end
     @changed = false
   end
 
-  def cleanup
-  end
-
   def save
     return unless @changed
-    Dir.mkdir("#{@bot.botclass}/lart") if not FileTest.directory? "#{@bot.botclass}/lart"
-    # TODO implement safe saving here too
+    Dir.mkdir(datafile) unless FileTest.directory? datafile
     Utils.safe_save(@lartfile) { |file|
       file.puts @larts
     }
@@ -91,93 +82,95 @@ class LartPlugin < Plugin
     @changed = false
   end
 
-  def privmsg(m)
-    if not m.params
-      m.reply "What a crazy fool!  Did you mean |help stats?"
-      return
-    end
-
-    meth = self.method(@@handlers[m.plugin])
-    meth.call(m) if(@bot.auth.allow?(m.plugin, m.source, m.replyto))
-  end
-
   def help(plugin, topic="")
     "Lart: The lart plugin allows you to lart/praise someone in the channel. You can also add new larts and new praises as well as delete them. For the curious, LART is an acronym for Luser Attitude Readjustment Tool. Usage: lart <who> [<reason>] -- larts <who> for <reason>. praise <who> [<reason>] -- praises <who> for <reason>. [add|rm][lart|praise] -- Add or remove a lart or praise."
   end
 
-  # The following are command handler
-
-  def handle_lart(m)
-    for_idx = m.params =~ /\s+\bfor\b/
-    if for_idx
-      nick = m.params[0, for_idx]
-    else
-      nick = m.params
-    end
+  def handle_lart(m, params)
     lart = @larts[get_msg_idx(@larts.length)]
-    if lart == nil
+    if not lart
       m.reply "I dunno any larts"
       return
     end
-    if nick == @bot.nick
-      lart = replace_who lart, m.sourcenick
-      lart << " for trying to make me lart myself"
-    else
-      lart = replace_who lart, nick
-      lart << m.params[for_idx, m.params.length] if for_idx
+    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"
     end
+    lart = replace_who lart, who
+    lart << " #{reason}" unless reason.empty?
 
-    @bot.action m.replyto, lart
+    m.act lart
   end
 
-  def handle_praise(m)
-    for_idx = m.params =~ /\s+\bfor\b/
-    if for_idx
-      nick = m.params[0, for_idx]
-    else
-      nick = m.params
-    end
+  def handle_praise(m, params)
     praise = @praises[get_msg_idx(@praises.length)]
     if not praise
       m.reply "I dunno any praises"
       return
     end
-
-    if nick == m.sourcenick
-      praise = @larts[get_msg_idx(@larts.length)]
-      praise = replace_who praise, nick
-    else
-      praise = replace_who praise, nick
-      praise << m.params.gsub("#{nick}", "")
+    who = params[:who].to_s
+    reason = params[:why]
+    if who == m.sourcenick || who == "me"
+      params[:who] = m.sourcenick
+      params[:why] = "for praising himself"
+      handle_lart(m, params)
+      return
     end
+    praise = replace_who praise, who
+    praise << " #{reason}" unless reason.empty?
 
-    @bot.action m.replyto, praise
+    m.act praise
   end
 
-  def handle_addlart(m)
-    @larts << m.params
+  def handle_addlart(m, params)
+    @larts << params[:lart].to_s
     @changed = true
     m.okay
   end
 
-  def handle_rmlart(m)
-    @larts.delete m.params
+  def handle_rmlart(m, params)
+    @larts.delete params[:lart].to_s
     @changed = true
     m.okay
   end
 
-  def handle_addpraise(m)
-    @praises << m.params
+  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].to_s
     @changed = true
     m.okay
   end
 
-  def handle_rmpraise(m)
-    @praises.delete m.params
+  def handle_rmpraise(m, params)
+    @praises.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}")
@@ -188,12 +181,17 @@ class LartPlugin < Plugin
   end
 
 end
+
 plugin = LartPlugin.new
-plugin.register("lart")
-plugin.register("praise")
 
-plugin.register("addlart")
-plugin.register("addpraise")
+plugin.map "lart *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_lart
+plugin.map "praise *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_praise
+
+plugin.map "addlart *lart", :action => :handle_addlart
+plugin.map "addpraise *praise", :action => :handle_addpraise
+
+plugin.map "rmlart *lart", :action => :handle_rmlart
+plugin.map "rmpraise *praise", :action => :handle_rmpraise
 
-plugin.register("rmlart")
-plugin.register("rmpraise")
+plugin.map "listlart *lart", :action => :handle_listlart
+plugin.map "listpraise *praise", :action => :handle_listpraise