]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/seen.rb
rss plugin: fix watcher for empty feed
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / seen.rb
index 80d52f6595767540124a5214b1a2e90b35501707..ccfdb7261e11148cff3c20a6b00f2ea969de9524 100644 (file)
@@ -1,23 +1,13 @@
-Saw = Struct.new("Saw", :nick, :time, :type, :where, :message)
+#-- vim:sw=2:et
+#++
+#
+# :title: Seen Plugin
+#
+# Keep a database of who last said/did what
 
-class SeenPlugin < Plugin
-  # turn a number of seconds into a human readable string, e.g
-  # 2 days, 3 hours, 18 minutes, 10 seconds
-  def secs_to_string(secs)
-    ret = ""
-    days = (secs / (60 * 60 * 24)).to_i
-    secs = secs % (60 * 60 * 24)
-    hours = (secs / (60 * 60)).to_i
-    secs = (secs % (60 * 60))
-    mins = (secs / 60).to_i
-    secs = (secs % 60).to_i
-    ret += "#{days} days, " if days > 0
-    ret += "#{hours} hours, " if hours > 0 || days > 0
-    ret += "#{mins} minutes and " if mins > 0 || hours > 0 || days > 0
-    ret += "#{secs} seconds"
-    return ret
-  end
+define_structure :Saw, :nick, :time, :type, :where, :message
 
+class SeenPlugin < Plugin
   def help(plugin, topic="")
     "seen <nick> => have you seen, or when did you last see <nick>"
   end
@@ -38,38 +28,36 @@ class SeenPlugin < Plugin
   end
 
   def listen(m)
+    return unless m.source
     # keep database up to date with who last said what
-    if m.kind_of?(PrivMessage)
+    now = Time.new
+    case m
+    when PrivMessage
       return if m.private?
-      if m.action?
-        @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "ACTION", 
-                                          m.target, m.message.dup)
-      else
-        @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "PUBLIC",
-                                          m.target, m.message.dup)
-      end
-    elsif m.kind_of?(QuitMessage)
+      type = m.action? ? 'ACTION' : 'PUBLIC'
+      @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, now, type,
+                                          m.target.to_s, m.message.dup)
+    when QuitMessage
       return if m.address?
-      @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "QUIT", 
+      @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, now, "QUIT", 
                                         nil, m.message.dup)
-    elsif m.kind_of?(NickMessage)
+    when NickMessage
       return if m.address?
-      @registry[m.message] = Saw.new(m.sourcenick.dup, Time.new, "NICK", 
-                                        nil, m.message.dup)
-      @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "NICK", 
-                                        nil, m.message.dup)
-    elsif m.kind_of?(PartMessage)
+      saw = Saw.new(m.oldnick, now, "NICK", nil, m.newnick)
+      @registry[m.oldnick] = saw
+      @registry[m.newnick] = saw
+    when PartMessage
       return if m.address?
       @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "PART", 
-                                        m.target, m.message.dup)
-    elsif m.kind_of?(JoinMessage)
+                                        m.target.to_s, m.message.dup)
+    when JoinMessage
       return if m.address?
       @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "JOIN", 
-                                        m.target, m.message.dup)
-    elsif m.kind_of?(TopicMessage)
-      return if m.address?
+                                        m.target.to_s, m.message.dup)
+    when TopicMessage
+      return if m.address? or m.info_or_set == :info
       @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "TOPIC", 
-                                        m.target, m.message.dup)
+                                        m.target.to_s, m.message.dup)
     end
   end
   
@@ -78,26 +66,26 @@ class SeenPlugin < Plugin
     ago = Time.new - saw.time
     
     if (ago.to_i == 0)
-      ret += "just now, "
+      ret << "just now, "
     else
-      ret += secs_to_string(ago) + " ago, "
+      ret << Utils.secs_to_string(ago) + " ago, "
     end
 
-    case saw.type
-    when "PUBLIC"
-      ret += "saying #{saw.message}"
-    when "ACTION"
-      ret += "doing #{saw.nick} #{saw.message}"
-    when "NICK"
-      ret += "changing nick from #{saw.nick} to #{saw.message}"
-    when "PART"
-      ret += "leaving #{saw.where}"
-    when "JOIN"
-      ret += "joining #{saw.where}"
-    when "QUIT"
-      ret += "quiting IRC (#{saw.message})"
-    when "TOPIC"
-      ret += "changing the topic of #{saw.where} to #{saw.message}"
+    case saw.type.to_sym
+    when :PUBLIC
+      ret << "saying #{saw.message}"
+    when :ACTION
+      ret << "doing #{saw.nick} #{saw.message}"
+    when :NICK
+      ret << "changing nick from #{saw.nick} to #{saw.message}"
+    when :PART
+      ret << "leaving #{saw.where}"
+    when :JOIN
+      ret << "joining #{saw.where}"
+    when :QUIT
+      ret << "quitting IRC (#{saw.message})"
+    when :TOPIC
+      ret << "changing the topic of #{saw.where} to #{saw.message}"
     end
   end