]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/note.rb
note: store nicks case-insensitive to avoid lost messages
[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       nick = m.sourcenick.downcase
21       # Keys are case insensitive to avoid storing a message
22       # for <person> instead of <Person> or visa-versa.
23       return unless @registry.has_key? nick
24       pub = []
25       priv = []
26       @registry[nick].each do |n|
27         s = "[#{n.time.strftime('%H:%M')}] <#{n.from}> #{n.text}"
28         (n.private ? priv : pub).push s
29       end
30       if !pub.empty?
31         @bot.say m.replyto, "#{m.sourcenick}, you have notes! " +
32           pub.join(' ')
33       end
34
35       if !priv.empty?
36         @bot.say m.sourcenick, "you have notes! " + priv.join(' ')
37       end
38       @registry.delete nick
39     rescue Exception => e
40       m.reply e.message
41     end
42   end
43
44   def note(m, params)
45     begin
46       nick = params[:nick].downcase
47       q = @registry[nick] || Array.new
48       s = params[:string].to_s.strip
49       raise 'cowardly discarding the empty note' if s.empty?
50       q.push Note.new(Time.now, m.sourcenick, m.private?, s)
51       @registry[nick] = q
52       m.okay
53     rescue Exception => e
54       m.reply "error: #{e.message}"
55     end
56   end
57 end
58
59 NotePlugin.new.map 'note :nick *string'