diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2006-06-07 14:43:22 +0000 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2006-06-07 14:43:22 +0000 |
commit | 8538e56167676b9bfa600c489caf3f94b2a33e50 (patch) | |
tree | 26b6047bb0f1300ee0d8e031f57ef4c7cdb2123b /data/rbot | |
parent | e0ae5e3a02ef7c85e9af8c4f476c72f149ab8b32 (diff) |
New features (private addressing) and cleanup for the topic plugin
Diffstat (limited to 'data/rbot')
-rwxr-xr-x | data/rbot/plugins/topic.rb | 220 |
1 files changed, 120 insertions, 100 deletions
diff --git a/data/rbot/plugins/topic.rb b/data/rbot/plugins/topic.rb index 075b6942..c3e5bf24 100755 --- a/data/rbot/plugins/topic.rb +++ b/data/rbot/plugins/topic.rb @@ -1,145 +1,165 @@ # Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com> # Add a bunch of topic manipulation features -# NOTE: topic separator is defined by the global symbol SEPARATOR - -SEPARATOR=" | " class TopicPlugin < Plugin def initialize super - @addtopic = /(?:topic(?:append|add)|(?:append|add)topic)/ - @prependtopic = /(?:topicprepend|prependtopic)/ - @addtopicat = /(?:(?:addtopic|topicadd)at)/ - @deltopic = /(?:deltopic|topicdel)/ + @separator = "|" # default separator end def help(plugin, topic="") case plugin when "topic" case topic - when @addtopic - return "#{topic} <text> => add <text> at the end the topic" - when @prependtopic - return "#{topic} <text> => add <text> at the beginning of the topic" - when @addtopicat - return "#{topic} <num> <text> => add <text> at position <num> of the topic" - when @deltopic - return "#{topic} <num> => remove section <num> from the topic" - when "learntopic" - return "learntopic => remembers the topic for later" - when "resumetopic" - return "resumetopic => resets the topic to the latest remembered one" - when "settopic" - return "settopic <text> => sets the topic to <text>" + when "add" + return "topic add <text> => add <text> at the end the topic" + when "prepend" + return "topic prepend <text> => add <text> at the beginning of the topic" + when "addat" + return "topic addat <num> <text> => add <text> at position <num> of the topic" + when "del" + return "topic del <num> => remove section <num> from the topic" + when "separator" + return "topic sep(arator) [<text>] => get or set the topic section separator" + when "learn" + return "topic learn => remembers the topic for later" + when "restore" + return "topic restore => resets the topic to the latest remembered one" + when "set" + return "topic set <text> => sets the topic to <text>" else - return "topic commands: addtopic, prependtopic, addtopicat, deltopic, learntopic, resumetopic, settopic" + return "topic add(at)|prepend|del|sep(arator)|learn|restore|set: " + \ + "manipulate the topic of the current channel; use topic <#channel> <command> " + \ + "for private addressing" + end + end + end + + def handletopic(m, param) + return unless m.kind_of?(PrivMessage) + if m.public? + ch = m.channel.downcase + else + ch = param[:channel].downcase + end + cmd = param[:command] + txt = param[:text].join(" ") + case cmd + when /a(dd|ppend)/ + topicappend(m, ch, txt) + when 'prepend' + topicprepend(m, ch, txt) + when 'addat' + if txt =~ /\s*(-?\d+)\s+(.*)\s*/ + num = $1.to_i - 1 + num += 1 if num < 0 + txt = $2 + topicaddat(m, ch, num, txt) + end + when /del(ete)?/ + if txt =~ /\s*(-?\d+)\s*/ + num=$1.to_i - 1 + num += 1 if num < 0 + topicdel(m, ch, num) + end + when /set/ + topicset(m, ch, txt) + when /sep(arator)?/ + topicsep(m, ch, txt) + when /learn/ + learntopic(m, ch) + when /restore/ + restoretopic(m, ch) + else + m.reply 'unknown command' + end + end + + def topicsep(m, ch, txt) + if txt + sep = txt.strip + if sep != "" + setsep(ch, sep) end - when "learntopic" - return "learntopic => remembers the topic for later" - when "resumetopic" - return "resumetopic => resets the topic to the latest remembered one" - when "settopic" - return "settopic <text> => sets the topic to <text>" end + m.reply "Topic separator set to #{getsep(ch)}" + end + + def setsep(ch, sep) + if @registry.has_key?(ch) + data = @registry[ch] + else + data = Hash.new + end + data[:separator] = sep + @registry[ch] = data end - def listen(m) - return unless m.kind_of?(PrivMessage) && m.public? - command = m.message.dup - debug command - if m.address? || command.gsub!(/^!/, "") - case command - when /^#{@addtopic}\s+(.*)\s*/ - txt=$1 - debug txt - if @bot.auth.allow?("topic", m.source, m.replyto) - topicappend(m, txt) - end - when /^#{@prependtopic}\s+(.*)\s*/ - txt=$1 - debug txt - if @bot.auth.allow?("topic", m.source, m.replyto) - topicaddat(m, 0, txt) - end - when /^#{@addtopicat}\s+(-?\d+)\s+(.*)\s*/ - num=$1.to_i - 1 - num += 1 if num < 0 - txt=$2 - debug txt - if @bot.auth.allow?("topic", m.source, m.replyto) - topicaddat(m, num, txt) - end - when /^#{@deltopic}\s+(-?\d+)\s*/ - num=$1.to_i - 1 - num += 1 if num < 0 - debug num - if @bot.auth.allow?("topic", m.source, m.replyto) - topicdel(m, num) - end + def getsep(ch) + if @registry.has_key?(ch) + if @registry[ch].has_key?(:separator) + return @registry[ch][:separator] end end + return @separator end - def topicaddat(m, num, txt) - channel = m.channel.downcase - topic = @bot.channels[m.channel].topic.to_s - topicarray = topic.split(SEPARATOR) + def topicaddat(m, channel, num, txt) + sep = getsep(channel) + topic = @bot.channels[channel].topic.to_s + topicarray = topic.split(/\s+#{sep}\s*/) topicarray.insert(num, txt) - newtopic = topicarray.join(SEPARATOR) + newtopic = topicarray.join(" #{sep} ") @bot.topic channel, newtopic end - def topicappend(m, txt) - channel = m.channel.downcase - topic = @bot.channels[m.channel].topic.to_s - topicarray = topic.split(SEPARATOR) - topicarray << txt - newtopic = topicarray.join(SEPARATOR) - @bot.topic channel, newtopic + def topicappend(m, ch, txt) + topicaddat(m, ch, -1, txt) end - def topicdel(m, num) - channel = m.channel.downcase - topic = @bot.channels[m.channel].topic.to_s - topicarray = topic.split(SEPARATOR) + def topicprepend(m, ch, txt) + topicaddat(m, ch, 0, txt) + end + + def topicdel(m, channel, num) + sep = getsep(channel) + topic = @bot.channels[channel].topic.to_s + topicarray = topic.split(/\s+#{sep}\s*/) topicarray.delete_at(num) - newtopic = topicarray.join(SEPARATOR) + newtopic = topicarray.join(" #{sep} ") @bot.topic channel, newtopic end - def learntopic(m, param) + def learntopic(m, channel) return if !@bot.auth.allow?("learntopic", m.source, m.replyto) - channel = m.channel.downcase - debug channel - topic = @bot.channels[m.channel].topic.to_s - @registry[channel] = topic - @bot.say channel, "Ok" + topic = @bot.channels[channel].topic.to_s + if @registry.has_key?(channel) + data = @registry[channel] + else + data = Hash.new + end + data[:topic] = topic + @registry[channel] = data + m.okay end - def resumetopic(m, param) - return if !@bot.auth.allow?("resumetopic", m.source, m.replyto) - channel = m.channel.downcase - debug "Channel: #{channel}" - if @registry.has_key?(channel) - topic = @registry[channel] - debug "Channel: #{channel}, topic: #{topic}" + def restoretopic(m, channel) + return if !@bot.auth.allow?("restoretopic", m.source, m.replyto) + if @registry.has_key?(channel) && @registry[channel].has_key?(:topic) + topic = @registry[channel][:topic] @bot.topic channel, topic else - @bot.say channel, "Non ricordo nulla" + m.reply "I don't remember any topic for this channel" end end - def settopic(m, param) + def topicset(m, channel, text) return if !@bot.auth.allow?("topic", m.source, m.replyto) - channel = m.channel.downcase - debug "Channel: #{channel}" - @bot.topic channel, param[:text].to_s + @bot.topic channel, text end end plugin = TopicPlugin.new -plugin.register 'topic' -plugin.map 'settopic *text', :action => 'settopic' -plugin.map 'resumetopic' -plugin.map 'learntopic' +plugin.map 'topic :command *text', :action => 'handletopic', :public => true, :private => false +plugin.map 'topic :channel :command *text', :action => 'handletopic', :public => false, :private => true |