X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fseen.rb;h=0fc6f0760f4dfcc91e8ac622f3e5f0fac3764c5c;hb=682e7f1708b9534385467b3d707433cb256b6405;hp=094a9b3db66149d29f436eae33279f474566aafc;hpb=bca160cbbf64aeb56fb6729f2ea4d77e5821a31c;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/seen.rb b/data/rbot/plugins/seen.rb index 094a9b3d..0fc6f076 100644 --- a/data/rbot/plugins/seen.rb +++ b/data/rbot/plugins/seen.rb @@ -1,10 +1,21 @@ -Saw = Struct.new("Saw", :nick, :time, :type, :where, :message) unless defined?(Saw) +#-- vim:sw=2:et +#++ +# +# :title: Seen Plugin +# +# Keep a database of who last said/did what + +define_structure :Saw, :nick, :time, :type, :where, :message class SeenPlugin < Plugin + Config.register Config::IntegerValue.new('seen.max_results', + :default => 3, :validate => Proc.new{|v| v >= 0}, + :desc => _("Maximum number of seen users to return in search (0 = no limit).")) + def help(plugin, topic="") - "seen => have you seen, or when did you last see " + _("seen => have you seen, or when did you last see ") end - + def privmsg(m) unless(m.params =~ /^(\S)+$/) m.reply "incorrect usage: " + help(m.plugin) @@ -16,75 +27,159 @@ class SeenPlugin < Plugin if @registry.has_key?(m.params) m.reply seen(@registry[m.params]) else - m.reply "nope!" + rx = Regexp.new(m.params, true) + num_matched = 0 + @registry.each {|nick, saw| + if nick.match(rx) + m.reply seen(saw) + num_matched += 1 + break if num_matched == @bot.config['seen.max_results'] + end + } + + m.reply _("nope!") if num_matched.zero? end end def listen(m) - return if m.sourcenick.nil? + 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' + store m, 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", - nil, m.message.dup) - elsif m.kind_of?(NickMessage) + store m, Saw.new(m.sourcenick.dup, now, "QUIT", + nil, m.message.dup) + 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) + store m, Saw.new(m.oldnick, now, "NICK", nil, m.newnick) + 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) + store m, Saw.new(m.sourcenick.dup, Time.new, "PART", + 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? - @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "TOPIC", - m.target, m.message.dup) + store m, Saw.new(m.sourcenick.dup, Time.new, "JOIN", + m.target.to_s, m.message.dup) + when TopicMessage + return if m.address? or m.info_or_set == :info + store m, Saw.new(m.sourcenick.dup, Time.new, "TOPIC", + m.target.to_s, m.message.dup) end end - - def seen(saw) - ret = "#{saw.nick} was last seen " + + def seen(reg) + saw = case reg + when Struct::Saw + reg # for backwards compatibility + when Array + reg.last + end + + if reg.kind_of? Array + before = reg.first + end + + formats = { + :normal => _("%{nick} was last seen %{when}, %{doing}"), + :with_before => _("%{nick} was last seen %{when}, %{doing} and %{time} before %{did_before}") + } + + if before && [:PART, :QUIT].include?(saw.type.to_sym) && + [:PUBLIC, :ACTION].include?(before.type.to_sym) + did_before = case before.type.to_sym + when :PUBLIC + _("saying \"%{message}\"") + when :ACTION + _("doing *%{nick} %{message}*") + end % { + :nick => saw.nick, + :message => before.message + } + + format = :with_before + + time_diff = saw.time - before.time + if time_diff < 300 + time_before = _("a moment") + elsif time_diff < 3600 + time_before = _("a while") + else + format = :normal + end + else + format = :normal + end + + nick = saw.nick ago = Time.new - saw.time - + if (ago.to_i == 0) - ret += "just now, " + time = _("just now") + else + time = _("%{time} ago") % { :time => Utils.secs_to_string(ago) } + end + + doing = case saw.type.to_sym + when :PUBLIC + _("saying \"%{message}\"") + when :ACTION + _("doing *%{message}*") + when :NICK + _("changing nick from %{nick} to %{message}") + when :PART + if saw.message.empty? + _("leaving %{where}") + else + _("leaving %{where} (%{message})") + end + when :JOIN + _("joining %{where}") + when :QUIT + _("quitting IRC (%{message})") + when :TOPIC + _("changing the topic of %{where} to \"%{message}\"") + end % { :message => saw.message, :where => saw.where, :nick => saw.nick } + + case format + when :normal + formats[:normal] % { + :nick => saw.nick, + :when => time, + :doing => doing, + } + when :with_before + formats[:with_before] % { + :nick => saw.nick, + :when => time, + :doing => doing, + :time => time_before, + :did_before => did_before + } + end + end + + def store(m, saw) + reg = @registry[saw.nick] + + if reg && reg.is_a?(Array) + reg.shift if reg.size > 1 + reg.push(saw) else - ret += Utils.secs_to_string(ago) + " ago, " + reg = [saw] 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 += "quitting IRC (#{saw.message})" - when "TOPIC" - ret += "changing the topic of #{saw.where} to #{saw.message}" + if m.is_a? NickMessage + @registry[m.newnick] = reg end + + @registry[saw.nick] = reg end - end plugin = SeenPlugin.new plugin.register("seen")