summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/note.rb
blob: e0a7be293a05a192375e4aed7e03cebe72ad347c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#++
#
# :title: Note plugin for rbot
#
# Author:: dmitry kim <dmitry dot kim at gmail dot com>
#
# Copyright:: (C) 200?-2009 dmitry 'jsn' kim
#
# License:: MIT license

class NotePlugin < Plugin

  Note = Struct.new('Note', :time, :from, :private, :text)

  Config.register Config::BooleanValue.new 'note.private_message',
    :default => false,
    :desc => 'Send all notes in private messages instead of channel messages.'

  def initialize
    super
    return if @registry.length < 1
    debug 'Checking registry for old-formatted notes...'
    n = 0
    @registry.keys.each do |key|
      unless key == key.downcase
        @registry[key.downcase] = @registry[key] + (@registry[key.downcase] || [])
        @registry.delete key
        n += 1
      end
    end
    debug "#{n} entries converted and merged."
  end

  def help(plugin, topic='')
    'note <nick> <string> => stores a note (<string>) for <nick>'
  end

  def message(m)
    begin
      nick = m.sourcenick.downcase
      # Keys are case insensitive to avoid storing a message
      # for <person> instead of <Person> or visa-versa.
      return unless @registry.has_key? nick
      pub = []
      priv = []
      @registry[nick].each do |n|
        s = "[#{n.time.strftime('%b-%e %H:%M')}] <#{n.from}> #{n.text}"
        if n.private or @bot.config['note.private_message']
          priv << s
        else
          pub << s
        end
      end
      unless pub.empty?
        @bot.say m.replyto, "#{m.sourcenick}, you have notes! " +
          pub.join(' ')
      end
      unless priv.empty?
        @bot.say m.sourcenick, 'you have notes! ' + priv.join(' ')
      end
      @registry.delete nick
    rescue Exception => e
      m.reply e.message
    end
  end

  def note(m, params)
    begin
      nick = params[:nick].downcase
      q = @registry[nick] || Array.new
      s = params[:string].to_s.strip
      raise 'cowardly discarding the empty note' if s.empty?
      q.push Note.new(Time.now, m.sourcenick, m.private?, s)
      @registry[nick] = q
      m.okay
    rescue Exception => e
      m.reply "error: #{e.message}"
    end
  end
end

NotePlugin.new.map 'note :nick *string'