root/data/rbot/plugins/seen.rb @ d29df50ddaf32536b105decefb135a0b86ee937f

Revision d29df50ddaf32536b105decefb135a0b86ee937f, 2.8 KB (checked in by Giuseppe Bilotta <giuseppe.bilotta@…>, 4 years ago)

Modernize/optimize/cleanup a bunch of plugins

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)]

  • Property mode set to 100644
Line 
1Saw = Struct.new("Saw", :nick, :time, :type, :where, :message)
2
3class SeenPlugin < Plugin
4  def help(plugin, topic="")
5    "seen <nick> => have you seen, or when did you last see <nick>"
6  end
7 
8  def privmsg(m)
9    unless(m.params =~ /^(\S)+$/)
10      m.reply "incorrect usage: " + help(m.plugin)
11      return
12    end
13
14    m.params.gsub!(/\?$/, "")
15
16    if @registry.has_key?(m.params)
17      m.reply seen(@registry[m.params])
18    else
19      m.reply "nope!"
20    end
21  end
22
23  def listen(m)
24    return if m.sourcenick.nil?
25    # keep database up to date with who last said what
26    if m.kind_of?(PrivMessage)
27      return if m.private?
28      if m.action?
29        @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "ACTION", 
30                                          m.target, m.message.dup)
31      else
32        @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "PUBLIC",
33                                          m.target, m.message.dup)
34      end
35    elsif m.kind_of?(QuitMessage)
36      return if m.address?
37      @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "QUIT", 
38                                        nil, m.message.dup)
39    elsif m.kind_of?(NickMessage)
40      return if m.address?
41      @registry[m.message] = Saw.new(m.sourcenick.dup, Time.new, "NICK", 
42                                        nil, m.message.dup)
43      @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "NICK", 
44                                        nil, m.message.dup)
45    elsif m.kind_of?(PartMessage)
46      return if m.address?
47      @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "PART", 
48                                        m.target, m.message.dup)
49    elsif m.kind_of?(JoinMessage)
50      return if m.address?
51      @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "JOIN", 
52                                        m.target, m.message.dup)
53    elsif m.kind_of?(TopicMessage)
54      return if m.address?
55      @registry[m.sourcenick] = Saw.new(m.sourcenick.dup, Time.new, "TOPIC", 
56                                        m.target, m.message.dup)
57    end
58  end
59 
60  def seen(saw)
61    ret = "#{saw.nick} was last seen "
62    ago = Time.new - saw.time
63   
64    if (ago.to_i == 0)
65      ret += "just now, "
66    else
67      ret += Utils.secs_to_string(ago) + " ago, "
68    end
69
70    case saw.type.to_sym
71    when :PUBLIC
72      ret += "saying #{saw.message}"
73    when :ACTION
74      ret += "doing #{saw.nick} #{saw.message}"
75    when :NICK
76      ret += "changing nick from #{saw.nick} to #{saw.message}"
77    when :PART
78      ret += "leaving #{saw.where}"
79    when :JOIN
80      ret += "joining #{saw.where}"
81    when :QUIT
82      ret += "quitting IRC (#{saw.message})"
83    when :TOPIC
84      ret += "changing the topic of #{saw.where} to #{saw.message}"
85    end
86  end
87 
88end
89plugin = SeenPlugin.new
90plugin.register("seen")
Note: See TracBrowser for help on using the browser.