]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
Modernize/optimize/cleanup a bunch of plugins
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Wed, 14 Feb 2007 22:00:08 +0000 (22:00 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Wed, 14 Feb 2007 22:00:08 +0000 (22:00 +0000)
Remove some unnecessary plugin.register() calls, replace other by plugin.map() calls.

Also use e.g. Array#pick_one instead of ar[rand(ar.length)]

data/rbot/plugins/autorejoin.rb
data/rbot/plugins/debugger.rb
data/rbot/plugins/excuse.rb
data/rbot/plugins/figlet.rb
data/rbot/plugins/grouphug.rb
data/rbot/plugins/imdb.rb
data/rbot/plugins/insult.rb
data/rbot/plugins/roshambo.rb
data/rbot/plugins/script.rb
data/rbot/plugins/seen.rb

index f057a6598163514b19a480746ec6494823d95dd0..4b6572747c7ef4fbeb15718a531f648d1c1b00bc 100644 (file)
@@ -19,4 +19,3 @@ class AutoRejoinPlugin < Plugin
 end
 
 plugin = AutoRejoinPlugin.new
-plugin.register("autorejoin")
index f0ff0c03fdb61b9bfa512a4256372a47c7125d41..2851d6a9ffd97aa1efbd78a8b847540e95534b78 100644 (file)
@@ -120,7 +120,7 @@ end
 
 
 plugin = DebugPlugin.new
-plugin.register( "debug" )
+
 plugin.default_auth( 'start', false )
 plugin.default_auth( 'stop', false )
 plugin.default_auth( 'dumpstrings', false )
index 38e85ad658fb44a36f6f7150ace92ebbcebc39c4..ad0e8334e58f7ad3d5889847f4502ceb59131ae4 100644 (file)
@@ -459,12 +459,12 @@ class ExcusePlugin < Plugin
   def help(plugin, topic="")
     "excuse => supply a random excuse"
   end
-  def privmsg(m)
-    excuse = @@excuses[rand(@@excuses.length)]
-    m.reply excuse
+
+  def excuse(m, params)
+    m.reply @@excuses.pick_one
   end
 end
 
 plugin = ExcusePlugin.new
-plugin.register("excuse")
+plugin.map "excuse"
 
index f82288eb65356cb06dd9ec7cb57d073d713781e6..598adfaffab91d10c609a96e026091f22a0ae14c 100644 (file)
@@ -4,21 +4,19 @@ MAX_WIDTH=68
 
 class FigletPlugin < Plugin
   def help(plugin, topic="")
-    "figlet [<message>] => print using figlet"
+    "figlet <message> => print using figlet"
   end
-  def privmsg(m)
-         case m.params
-         when nil
-                 m.reply "incorrect usage: " + help(m.plugin)
-                 return
-         when (/^-/)
-                 m.reply "incorrect usage: " + help(m.plugin)
-                 return
-         else
-                 m.reply Utils.safe_exec("/usr/bin/figlet", "-k", "-w", "#{MAX_WIDTH}", "-f", DEFAULT_FONT, m.params)
-                 return
-         end
+
+  def figlet(m, params)
+    message = params[:message].to_s
+    if message =~ /^-/
+      m.reply "the message can't start with a - sign"
+      return
+    end
+    m.reply Utils.safe_exec("/usr/bin/figlet", "-k", "-w", "#{MAX_WIDTH}", "-f", DEFAULT_FONT, message)
+    return
   end
 end
+
 plugin = FigletPlugin.new
-plugin.register("figlet")
+plugin.map "figlet *message"
index 53fc7f0a761a118ee4f41160bc852b111578bc29..87a7e32c1895e24c7ac8e173a16619cda2d7a99c 100644 (file)
@@ -6,31 +6,30 @@ require "net/http"
 
 
 class GrouphugPlugin < Plugin
-    def help( plugin, topic="" )
-        "Grouphug plugin. Confess! Usage: 'confess' for random confession, 'confess <number>' for specific one."
+  def help( plugin, topic="" )
+    return "Grouphug plugin. Confess! Usage: 'confess' for random confession, 'confess <number>' for specific one."
+  end
+
+  def confess(m, params)
+    path = "random"
+    path = "confessions/#{params[:num]}" if params[:num]
+    begin
+      data = bot.httputil.get_cached(URI.parse("http://grouphug.us/#{path}"))
+
+      reg = Regexp.new( '(<td class="conf-text")(.*?)(<p>)(.*?)(</p>)', Regexp::MULTILINE )
+      confession = reg.match( data )[4].ircify_html
+      confession = "no confession ##{params[:num]} found" if confession.empty? and params[:num]
+
+      m.reply confession
+    rescue
+      m.reply "failed to connect to grouphug.us"
     end
-
-    def privmsg( m )
-        path = "/random"
-        path = "/confessions/#{m.params()}" if m.params()
-        begin
-          data = bot.httputil.get_cached(URI.parse("http://grouphug.us/#{path}"))
-
-          reg = Regexp.new( '(<td class="conf-text")(.*?)(<p>)(.*?)(</p>)', Regexp::MULTILINE )
-          confession = reg.match( data )[4]
-          confession.gsub!( /<.*?>/, "" ) # Remove html tags
-          confession.gsub!( "\t", "" ) # Remove tab characters
-
-          @bot.say(m.replyto, confession)
-        rescue
-          m.reply "failed to connect to grouphug.us"
-        end
-   end
+  end
 end
 
 
 plugin = GrouphugPlugin.new
 
-plugin.register("grouphug")
-plugin.register("confess")
+plugin.map "grouphug [:num]", :action => :confess, :requirements => { :num => /\d+/ }
+plugin.map "confess [:num]", :action => :confess, :requirements => { :num => /\d+/ }
 
index 432d0efc47ea4d87f537f6d431a9f3abb7073e0a..48c49dc32d94c297eb13682a3e3d73cad3284b1a 100644 (file)
@@ -70,16 +70,12 @@ class ImdbPlugin < Plugin
     "imdb <string> => search http://www.imdb.org for <string>"
   end
 
-  def privmsg(m)
-    unless(m.params && m.params.length > 0)
-      m.reply "incorrect usage: " + help(m.plugin)
-      return
-    end
-
+  def imdb(m, params)
+    what = params[:what].to_s
     i = Imdb.new(@bot)
-    info = i.info(m.params)
+    info = i.info(what)
     if !info
-      m.reply "Nothing found for #{m.params}"
+      m.reply "Nothing found for #{what}"
       return nil
     end
     m.reply "#{info[1]} : #{info[0]}"
@@ -88,5 +84,5 @@ class ImdbPlugin < Plugin
 end
 
 plugin = ImdbPlugin.new
-plugin.register("imdb")
+plugin.map "imdb *what"
 
index f7217756b8635dca11cb8c3be4fedf7aee52d4f5..0c44965442a427392940db0901971304253866f5 100644 (file)
@@ -208,58 +208,53 @@ class InsultPlugin < Plugin
 ]
   
   def help(plugin, topic="")
-    if(plugin == "insult")
-      return "insult me|<person> => insult you or <person>"
-    elsif(plugin == "msginsult")
-      return "msginsult <nick> => insult <nick> via /msg"
-    else
-      return "insult module topics: msginsult, insult"
-    end
+    return "[msg]insult me|<person> => insult you or <person>. msginsult insults in private"
   end
-  def name
-    "insult"
-  end
-  def privmsg(m)
-    suffix=""
-    unless(m.params)
-      m.reply "incorrect usage: " + help(m.plugin)
-      return
-    end
-    msgto = m.channel
-    if(m.plugin =~ /^msginsult$/)
-      prefix = "you are "
-      if (m.params =~ /^#/)
-        prefix += "all "
-      end
-      msgto = m.params
-      suffix = " (from #{m.sourcenick})"
-    elsif(m.params =~ /^me$/)
+
+  def insult(m, params)
+    who = params[:who]
+    who = m.sourcenick if ["me", @bot.nick].include?(who)
+
+    priv = params[:priv] || m.plugin == "msginsult"
+
+    case who
+    when /^#/
+      prefix = "you are all "
+    when m.sourcenick
       prefix = "you are "
     else
-      who = m.params
-      if (who == @bot.nick)
-        who = m.sourcenick
-      end
-      prefix = "#{who} is "
+      prefix = priv ? "you are " : "#{who} is "
     end
+
+    suffix = ""
+
+    if priv
+      msgto = who
+      suffix = " (from #{m.sourcenick})" unless who == m.sourcenick
+    else
+      msgto = m.channel
+    end
+
     insult = generate_insult
     @bot.say msgto, prefix + insult + suffix
   end
+
   def generate_insult
-    adj = @@adj[rand(@@adj.length)]
+    adj = @@adj.pick_one
     adj2 = ""
     loop do
-      adj2 = @@adj[rand(@@adj.length)]
+      adj2 = @@adj.pick_one
       break if adj2 != adj
     end
-    amt = @@amt[rand(@@amt.length)]
-    noun = @@noun[rand(@@noun.length)]
+    amt = @@amt.pick_one
+    noun = @@noun.pick_one
     start = "a "
-    start = "an " if ['a','e','i','o','u'].include?(adj[0].chr)
+    start = "an " if ['a','e','i','o','u'].include?(adj[0,1])
     "#{start}#{adj} #{amt} of #{adj2} #{noun}"
   end
 end
+
 plugin = InsultPlugin.new
-plugin.register("insult")
-plugin.register("msginsult")
+plugin.map "insult :who [:priv]", :action => :insult, :requirements => { :priv => /in private/ }
+plugin.map "msginsult :who", :action => :insult
 
index 4f20fb155ca64f74d61e6766ff5ea353ce1ef176..7f776c398bee9292cb947ad33f3dc4ad78f89ba2 100644 (file)
@@ -1,54 +1,56 @@
+#-- vim:sw=2:et
+#++
 # Play the game of roshambo (rock-paper-scissors)
 # Copyright (C) 2004 Hans Fugal
 # Distributed under the same license as rbot itself
+
 require 'time'
+
 class RoshamboPlugin < Plugin
+
   def initialize
     super 
     @scoreboard = {}
+    @plays = [:rock, :paper, :scissor]
+    @beats = { :rock => :scissors, :paper => :rock, :scissors => :paper}
   end
+
   def help(plugin, topic="")
-    "roshambo <rock|paper|scissors> => play roshambo"
+    "roshambo <rock|paper|scissors> or rps <rock|paper|scissors> => play roshambo"
   end
-  def privmsg(m)
+
+  def rps(m, params)
     # simultaneity
-    choice = choose
+    choice = @plays.pick_one
 
     # init scoreboard
-    if (not @scoreboard.has_key?(m.sourcenick) or (Time.now - @scoreboard[m.sourcenick]['timestamp']) > 3600)
-      @scoreboard[m.sourcenick] = {'me'=>0,'you'=>0,'timestamp'=>Time.now}
+    if not @scoreboard.has_key?(m.sourcenick) or (Time.now - @scoreboard[m.sourcenick]['timestamp']) > 3600
+      @scoreboard[m.sourcenick] = { 'me' => 0, 'you' => 0, 'timestamp' => Time.now }
     end
-    case m.params
-    when 'rock','paper','scissors'
-      s = score(choice,m.params)
-      @scoreboard[m.sourcenick]['timestamp'] = Time.now
-      myscore=@scoreboard[m.sourcenick]['me']
-      yourscore=@scoreboard[m.sourcenick]['you']
-      case s
-      when 1
-       yourscore=@scoreboard[m.sourcenick]['you'] += 1
-       m.reply "#{choice}. You win. Score: me #{myscore} you #{yourscore}"
-      when 0
-       m.reply "#{choice}. We tie. Score: me #{myscore} you #{yourscore}"
-      when -1
-       myscore=@scoreboard[m.sourcenick]['me'] += 1
-       m.reply "#{choice}! I win! Score: me #{myscore} you #{yourscore}"
-      end
-    else
-      m.reply "incorrect usage: " + help(m.plugin)
+    play = params[:play].to_sym
+    s = score(choice, play)
+    @scoreboard[m.sourcenick]['timestamp'] = Time.now
+    myscore=@scoreboard[m.sourcenick]['me']
+    yourscore=@scoreboard[m.sourcenick]['you']
+    case s
+    when 1
+      yourscore = @scoreboard[m.sourcenick]['you'] += 1
+      m.reply "#{choice}. You win. Score: me #{myscore} you #{yourscore}"
+    when 0
+      m.reply "#{choice}. We tie. Score: me #{myscore} you #{yourscore}"
+    when -1
+      myscore = @scoreboard[m.sourcenick]['me'] += 1
+      m.reply "#{choice}! I win! Score: me #{myscore} you #{yourscore}"
     end
   end
-      
-  def choose
-    ['rock','paper','scissors'][rand(3)]
-  end
-  def score(a,b)
-    beats = {'rock'=>'scissors', 'paper'=>'rock', 'scissors'=>'paper'}
-    return -1 if beats[a] == b
-    return 1 if beats[b] == a
+
+  def score(a, b)
+    return -1 if @beats[a] == b
+    return 1 if @beats[b] == a
     return 0
   end
 end
+
 plugin = RoshamboPlugin.new
-plugin.register("roshambo")
-plugin.register("rps")
+plugin.map "roshambo :play", :action => :rps, :requirements => { :play => /rock|paper|scissors/ }
+plugin.map "rps :play", :action => :rps, :requirements => { :play => /rock|paper|scissors/ }
index 4509c71b08dabc0d08243b4aba66cc663ff62f88..3976517f7d873154f248026849ef3813e2f263b8 100644 (file)
@@ -158,7 +158,7 @@ end
 
 
 plugin = ScriptPlugin.new
-plugin.register( "script" )
+
 plugin.default_auth( 'edit', false )
 plugin.default_auth( 'eval', false )
 plugin.default_auth( 'echo', false )
index aec5a064fa8ebbd7c990119a71ad87a17ccbecb9..a697d19b06722743884fe0411a84d177dc10a86e 100644 (file)
@@ -67,20 +67,20 @@ class SeenPlugin < Plugin
       ret += Utils.secs_to_string(ago) + " ago, "
     end
 
-    case saw.type
-    when "PUBLIC"
+    case saw.type.to_sym
+    when :PUBLIC
       ret += "saying #{saw.message}"
-    when "ACTION"
+    when :ACTION
       ret += "doing #{saw.nick} #{saw.message}"
-    when "NICK"
+    when :NICK
       ret += "changing nick from #{saw.nick} to #{saw.message}"
-    when "PART"
+    when :PART
       ret += "leaving #{saw.where}"
-    when "JOIN"
+    when :JOIN
       ret += "joining #{saw.where}"
-    when "QUIT"
+    when :QUIT
       ret += "quitting IRC (#{saw.message})"
-    when "TOPIC"
+    when :TOPIC
       ret += "changing the topic of #{saw.where} to #{saw.message}"
     end
   end