]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/seen.rb
Fri Jul 29 13:07:56 BST 2005 Tom Gilbert <tom@linuxbrit.co.uk>
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / seen.rb
1 Saw = Struct.new("Saw", :nick, :time, :type, :where, :message)
2
3 class SeenPlugin < Plugin
4   # turn a number of seconds into a human readable string, e.g
5   # 2 days, 3 hours, 18 minutes, 10 seconds
6   def secs_to_string(secs)
7     ret = ""
8     days = (secs / (60 * 60 * 24)).to_i
9     secs = secs % (60 * 60 * 24)
10     hours = (secs / (60 * 60)).to_i
11     secs = (secs % (60 * 60))
12     mins = (secs / 60).to_i
13     secs = (secs % 60).to_i
14     ret += "#{days} days, " if days > 0
15     ret += "#{hours} hours, " if hours > 0 || days > 0
16     ret += "#{mins} minutes and " if mins > 0 || hours > 0 || days > 0
17     ret += "#{secs} seconds"
18     return ret
19   end
20
21   def help(plugin, topic="")
22     "seen <nick> => have you seen, or when did you last see <nick>"
23   end
24   
25   def privmsg(m)
26     unless(m.params =~ /^(\S)+$/)
27       m.reply "incorrect usage: " + help(m.plugin)
28       return
29     end
30
31     m.params.gsub!(/\?$/, "")
32
33     if @registry.has_key?(m.params)
34       m.reply seen(@registry[m.params])
35     else
36       m.reply "nope!"
37     end
38   end
39
40   def listen(m)
41     # keep database up to date with who last said what
42     if m.kind_of?(PrivMessage)
43       return if m.private?
44       if m.action?
45         @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "ACTION", 
46                                           m.target, m.message.dup)
47       else
48         @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "PUBLIC",
49                                           m.target, m.message.dup)
50       end
51     elsif m.kind_of?(QuitMessage)
52       return if m.address?
53       @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "QUIT", 
54                                         nil, m.message.dup)
55     elsif m.kind_of?(NickMessage)
56       return if m.address?
57       @registry[m.message] = Saw.new(m.sourcenick.dup, Time.new, "NICK", 
58                                         nil, m.message.dup)
59       @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "NICK", 
60                                         nil, m.message.dup)
61     elsif m.kind_of?(PartMessage)
62       return if m.address?
63       @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "PART", 
64                                         m.target, m.message.dup)
65     elsif m.kind_of?(JoinMessage)
66       return if m.address?
67       @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "JOIN", 
68                                         m.target, m.message.dup)
69     elsif m.kind_of?(TopicMessage)
70       return if m.address?
71       @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "TOPIC", 
72                                         m.target, m.message.dup)
73     end
74   end
75   
76   def seen(saw)
77     ret = "#{saw.nick} was last seen "
78     ago = Time.new - saw.time
79     
80     if (ago.to_i == 0)
81       ret += "just now, "
82     else
83       ret += secs_to_string(ago) + " ago, "
84     end
85
86     case saw.type
87     when "PUBLIC"
88       ret += "saying #{saw.message}"
89     when "ACTION"
90       ret += "doing #{saw.nick} #{saw.message}"
91     when "NICK"
92       ret += "changing nick from #{saw.nick} to #{saw.message}"
93     when "PART"
94       ret += "leaving #{saw.where}"
95     when "JOIN"
96       ret += "joining #{saw.where}"
97     when "QUIT"
98       ret += "quiting IRC (#{saw.message})"
99     when "TOPIC"
100       ret += "changing the topic of #{saw.where} to #{saw.message}"
101     end
102   end
103   
104 end
105 plugin = SeenPlugin.new
106 plugin.register("seen")