X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;ds=sidebyside;f=data%2Frbot%2Fplugins%2Fnote.rb;h=e0a7be293a05a192375e4aed7e03cebe72ad347c;hb=d19058b6c071d754a6cc8143acd7e2c50ae12d93;hp=16c98e78152291ec1e62547e1df81a015b04adef;hpb=05fd07d54769b435e837d7dd9ec4dfc1f6a6a5c2;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/note.rb b/data/rbot/plugins/note.rb index 16c98e78..e0a7be29 100644 --- a/data/rbot/plugins/note.rb +++ b/data/rbot/plugins/note.rb @@ -1,28 +1,64 @@ -Note = Struct.new('Note', :time, :from, :private, :text) +#++ +# +# :title: Note plugin for rbot +# +# Author:: dmitry kim +# +# Copyright:: (C) 200?-2009 dmitry 'jsn' kim +# +# License:: MIT license class NotePlugin < Plugin - def help(plugin, topic="") - "note => stores a note () for " + + 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 listen(m) + def help(plugin, topic='') + 'note => stores a note () for ' + end + + def message(m) begin - return if !m.kind_of?(PrivMessage) || !@registry.has_key?(m.sourcenick) + nick = m.sourcenick.downcase + # Keys are case insensitive to avoid storing a message + # for instead of or visa-versa. + return unless @registry.has_key? nick pub = [] priv = [] - @registry[m.sourcenick].each do |n| - s = "[#{n.time.strftime('%H:%M')}] <#{n.from}> #{n.text}" - (n.private ? priv : pub).push(s) + @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 - if !pub.empty? + unless pub.empty? @bot.say m.replyto, "#{m.sourcenick}, you have notes! " + pub.join(' ') end - - if !priv.empty? - @bot.say m.sourcenick, "you have notes! " + priv.join(' ') + unless priv.empty? + @bot.say m.sourcenick, 'you have notes! ' + priv.join(' ') end - @registry.delete(m.sourcenick) + @registry.delete nick rescue Exception => e m.reply e.message end @@ -30,17 +66,17 @@ class NotePlugin < Plugin def note(m, params) begin - q = @registry[params[:nick]] || Array.new - s = params[:string].join(' ') - raise 'cowardly discarding the empty note' if s.empty? || !s =~ /\S/ - q.push Note.new(Time.now, m.sourcenick, - m.private?, params[:string].join(' ')) - @registry[params[:nick]] = q + 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 -plugin = NotePlugin.new -plugin.map 'note :nick *string' + +NotePlugin.new.map 'note :nick *string'