]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/note.rb
Ruby 1.9 cleanup: variables warnings
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / note.rb
1 #++
2 #
3 # :title: Note plugin for rbot
4 #
5 # Author:: dmitry kim <dmitry dot kim at gmail dot com>
6 #
7 # Copyright:: (C) 200?-2009 dmitry 'jsn' kim
8 #
9 # License:: MIT license
10
11 class NotePlugin < Plugin
12   Note = Struct.new('Note', :time, :from, :private, :text)
13
14   def help(plugin, topic="")
15     "note <nick> <string> => stores a note (<string>) for <nick>"
16   end
17
18   def message(m)
19     begin
20       return unless @registry.has_key? m.sourcenick
21       pub = []
22       priv = []
23       @registry[m.sourcenick].each do |n|
24         s = "[#{n.time.strftime('%H:%M')}] <#{n.from}> #{n.text}"
25         (n.private ? priv : pub).push s
26       end
27       if !pub.empty?
28         @bot.say m.replyto, "#{m.sourcenick}, you have notes! " +
29           pub.join(' ')
30       end
31
32       if !priv.empty?
33         @bot.say m.sourcenick, "you have notes! " + priv.join(' ')
34       end
35       @registry.delete m.sourcenick
36     rescue Exception => e
37       m.reply e.message
38     end
39   end
40
41   def note(m, params)
42     begin
43       q = @registry[params[:nick]] || Array.new
44       s = params[:string].to_s.strip
45       raise 'cowardly discarding the empty note' if s.empty?
46       q.push Note.new(Time.now, m.sourcenick, m.private?, s)
47       @registry[params[:nick]] = q
48       m.okay
49     rescue Exception => e
50       m.reply "error: #{e.message}"
51     end
52   end
53 end
54
55 NotePlugin.new.map 'note :nick *string'