6 # Keep a database of who last said/did what
8 define_structure :Saw, :nick, :time, :type, :where, :message
10 class SeenPlugin < Plugin
12 MSG_PUBLIC = N_("saying \"%{message}\" in %{where}")
13 MSG_ACTION = N_("doing *%{nick} %{message}* in %{where}")
14 MSG_NICK = N_("changing nick from %{nick} to %{message}")
15 MSG_PART = N_("leaving %{where} (%{message})")
16 MSG_PART_EMPTY = N_("leaving %{where}")
17 MSG_JOIN = N_("joining %{where}")
18 MSG_QUIT = N_("quitting IRC (%{message})")
19 MSG_TOPIC = N_("changing the topic of %{where} to \"%{message}\"")
21 CHANPRIV_CHAN = N_("a private channel")
22 CHANPRIV_MSG_TOPIC = N_("changing the topic of %{where}")
24 MSGPRIV_MSG_PUBLIC = N_("speaking in %{where}")
25 MSGPRIV_MSG_ACTION = N_("doing something in %{where}")
27 FORMAT_NORMAL = N_("%{nick} was last seen %{when}, %{doing}")
28 FORMAT_WITH_BEFORE = N_("%{nick} was last seen %{when}, %{doing} and %{time} before %{did_before}")
30 Config.register Config::IntegerValue.new('seen.max_results',
31 :default => 3, :validate => Proc.new{|v| v >= 0},
32 :desc => _("Maximum number of seen users to return in search (0 = no limit)."))
34 def help(plugin, topic="")
35 _("seen <nick> => have you seen, or when did you last see <nick>")
39 unless(m.params =~ /^(\S)+$/)
40 m.reply "incorrect usage: " + help(m.plugin)
44 m.params.gsub!(/\?$/, "")
46 if @registry.has_key?(m.params)
47 m.reply seen(@registry[m.params])
49 rx = Regexp.new(m.params, true)
51 @registry.each {|nick, saw|
55 break if num_matched == @bot.config['seen.max_results']
59 m.reply _("nope!") if num_matched.zero?
64 return unless m.source
65 # keep database up to date with who last said what
70 type = m.action? ? 'ACTION' : 'PUBLIC'
71 store m, Saw.new(m.sourcenick.dup, now, type,
72 m.target.to_s, m.message.dup)
75 store m, Saw.new(m.sourcenick.dup, now, "QUIT",
79 store m, Saw.new(m.oldnick, now, "NICK", nil, m.newnick)
82 store m, Saw.new(m.sourcenick.dup, Time.new, "PART",
83 m.target.to_s, m.message.dup)
86 store m, Saw.new(m.sourcenick.dup, Time.new, "JOIN",
87 m.target.to_s, m.message.dup)
89 return if m.address? or m.info_or_set == :info
90 store m, Saw.new(m.sourcenick.dup, Time.new, "TOPIC",
91 m.target.to_s, m.message.dup)
98 reg # for backwards compatibility
103 if reg.kind_of? Array
107 # TODO: a message should not be disclosed if:
108 # - it was said in a channel that was/is invite-only, private or secret
109 # - UNLESS the requester is also in the channel now, or the request is made
112 # TODO: a channel or it's topic should not be disclosed if:
113 # - the channel was/is private or secret
114 # - UNLESS the requester is also in the channel now, or the request is made
118 # What should be displayed for channel?
119 where = chan_privacy ? _(CHANPRIV_CHAN) : saw.where
122 :normal => _(FORMAT_NORMAL),
123 :with_before => _(FORMAT_WITH_BEFORE)
126 if before && [:PART, :QUIT].include?(saw.type.to_sym) &&
127 [:PUBLIC, :ACTION].include?(before.type.to_sym)
128 did_before = case before.type.to_sym
130 _(msg_privacy ? MSGPRIV_MSG_PUBLIC : MSG_PUBLIC)
132 _(msg_privacy ? MSGPRIV_MSG_ACTION : MSG_ACTION)
135 :message => before.message,
139 format = :with_before
141 time_diff = saw.time - before.time
143 time_before = _("a moment")
144 elsif time_diff < 3600
145 time_before = _("a while")
154 ago = Time.new - saw.time
159 time = _("%{time} ago") % { :time => Utils.secs_to_string(ago) }
162 doing = case saw.type.to_sym
164 _(msg_privacy ? MSGPRIV_MSG_PUBLIC : MSG_PUBLIC)
166 _(msg_privacy ? MSGPRIV_MSG_ACTION : MSG_ACTION)
170 if saw.message.empty?
180 _(chan_privacy ? CHANPRIV_MSG_TOPIC : MSG_TOPIC)
181 end % { :message => saw.message, :where => where, :nick => saw.nick }
191 formats[:with_before] % {
195 :time => time_before,
196 :did_before => did_before
202 # TODO: we need to store the channel state INVITE/SECRET/PRIVATE here, in
203 # some symbolic form, so that we know the prior state of the channel when
204 # it comes time to display.
205 reg = @registry[saw.nick]
207 if reg && reg.is_a?(Array)
208 reg.shift if reg.size > 1
214 if m.is_a? NickMessage
215 @registry[m.newnick] = reg
218 @registry[saw.nick] = reg
221 plugin = SeenPlugin.new
222 plugin.register("seen")