X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Ftopic.rb;h=672bf82a1aee0d5d8a894b93d1cfe82864207b8c;hb=3d8bdf551aebdd4fa7ddb10fa8e824232dd4f82b;hp=a3d325241370060f06adccfe664d1e2e9a8571e1;hpb=8fa912740704cedf2e742b6bdca68df32e83da9e;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/topic.rb b/data/rbot/plugins/topic.rb old mode 100755 new mode 100644 index a3d32524..672bf82a --- a/data/rbot/plugins/topic.rb +++ b/data/rbot/plugins/topic.rb @@ -1,4 +1,12 @@ -# Author: Giuseppe "Oblomov" Bilotta +#-- vim:sw=2:et +#++ +# +# :title: Topic manipulation plugin for rbot +# +# Author:: Giuseppe Bilotta +# Copyright:: (C) 2006-2007 Giuseppe Bilotta +# License:: GPL v2 +# # Add a bunch of topic manipulation features class TopicPlugin < Plugin @@ -31,8 +39,10 @@ class TopicPlugin < Plugin return "topic clear => clears the topic" when "set" return "topic set => sets the topic to " + when "undo" + return "topic undo => undoes the latest change to the topic" else - return "topic add(at)|prepend|del(ete)|replace|sep(arator)|learn|restore|clear|set: " + \ + return "topic add(at)|prepend|del(ete)|replace|sep(arator)|learn|restore|clear|set|undo: " + \ "manipulate the topic of the current channel; use topic <#channel> " + \ "for private addressing" end @@ -42,17 +52,16 @@ class TopicPlugin < Plugin def handletopic(m, param) return unless m.kind_of?(PrivMessage) if m.public? - ch = m.channel.downcase + ch = m.channel else - ch = param[:channel].downcase + ch = m.server.get_channel(param[:channel]) + unless ch + m.reply("I am not in channel #{param[:channel]}") + return + end end cmd = param[:command] - txt = param[:text].join(" ") - - unless @bot.channels.has_key?(ch) - m.reply "I am not in channel #{ch}" - return - end + txt = param[:text].to_s case cmd when /^a(dd|ppend)$/ @@ -89,12 +98,15 @@ class TopicPlugin < Plugin end when 'restore' restoretopic(m, ch) + when 'undo' + undotopic(m, ch) else m.reply 'unknown command' end end def topicsep(m, ch, txt) + return if !@bot.auth.allow?("topic::edit::separator", m.source, m.replyto) if txt sep = txt.strip if sep != "" @@ -105,41 +117,50 @@ class TopicPlugin < Plugin end def setsep(ch, sep) - if @registry.has_key?(ch) - data = @registry[ch] + raise unless ch.class <= Irc::Channel + # TODO multiserver + k = ch.downcase + + if @registry.has_key?(k) + data = @registry[k] else data = Hash.new end oldsep = getsep(ch) - topic = @bot.channels[ch].topic.to_s + topic = ch.topic.text topicarray = topic.split(/\s+#{Regexp.escape(oldsep)}\s*/) if sep != oldsep and topicarray.length > 0 newtopic = topicarray.join(" #{sep} ") - @bot.topic ch, newtopic + @bot.topic ch, newtopic if newtopic != topic end data[:separator] = sep - @registry[ch] = data + @registry[k] = data end def getsep(ch) - if @registry.has_key?(ch) - if @registry[ch].has_key?(:separator) - return @registry[ch][:separator] + raise unless ch.class <= Irc::Channel + # TODO multiserver + k = ch.downcase + + if @registry.has_key?(k) + if @registry[k].has_key?(:separator) + return @registry[k][:separator] end end return @separator end - def topicaddat(m, channel, num, txt) - sep = getsep(channel) - topic = @bot.channels[channel].topic.to_s + def topicaddat(m, ch, num, txt) + return if !@bot.auth.allow?("topic::edit::add", m.source, m.replyto) + sep = getsep(ch) + topic = ch.topic.text topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/) topicarray.insert(num, txt) newtopic = topicarray.join(" #{sep} ") - @bot.topic channel, newtopic + changetopic(m, ch, newtopic) end def topicappend(m, ch, txt) @@ -150,55 +171,91 @@ class TopicPlugin < Plugin topicaddat(m, ch, 0, txt) end - def topicdel(m, channel, num) - sep = getsep(channel) - topic = @bot.channels[channel].topic.to_s + def topicdel(m, ch, num) + return if !@bot.auth.allow?("topic::edit::del", m.source, m.replyto) + sep = getsep(ch) + topic = ch.topic.text topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/) topicarray.delete_at(num) newtopic = topicarray.join(" #{sep} ") - @bot.topic channel, newtopic + changetopic(m, ch, newtopic) end - def learntopic(m, channel) - return if !@bot.auth.allow?("learntopic", m.source, m.replyto) - topic = @bot.channels[channel].topic.to_s - if @registry.has_key?(channel) - data = @registry[channel] + def learntopic(m, ch) + return if !@bot.auth.allow?("topic::store::store", m.source, m.replyto) + topic = ch.topic.text + k = ch.downcase + if @registry.has_key?(k) + data = @registry[k] else data = Hash.new end data[:topic] = topic - @registry[channel] = data + @registry[k] = data m.okay end - def replacetopic(m, channel, num, txt) - return if !@bot.auth.allow?("topic", m.source, m.replyto) - sep = getsep(channel) - topic = @bot.channels[channel].topic.to_s + def replacetopic(m, ch, num, txt) + return if !@bot.auth.allow?("topic::edit::replace", m.source, m.replyto) + sep = getsep(ch) + topic = ch.topic.text topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/) topicarray[num] = txt newtopic = topicarray.join(" #{sep} ") - @bot.topic channel, newtopic - end - - 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 + changetopic(m, ch, newtopic) + end + + def restoretopic(m, ch) + return if !@bot.auth.allow?("topic::store::restore", m.source, m.replyto) + k = ch.downcase + if @registry.has_key?(k) && @registry[k].has_key?(:topic) + topic = @registry[k][:topic] + topicset(m, ch, topic) + else + m.reply "I don't remember any topic for #{ch}" + end + end + + def topicset(m, ch, text) + return if !@bot.auth.allow?("topic::edit::replace", m.source, m.replyto) + changetopic(m, ch, text) + end + + # This method changes the topic on channel +ch+ to +text+, storing + # the previous topic for undo + def changetopic(m, ch, text) + k = ch.downcase + if @registry.has_key?(k) + data = @registry[k] else - m.reply "I don't remember any topic for this channel" + data = Hash.new end + + data[:oldtopic] = ch.topic.text + @registry[k] = data + + @bot.topic ch, text end - def topicset(m, channel, text) - return if !@bot.auth.allow?("topic", m.source, m.replyto) - @bot.topic channel, text + def undotopic(m, ch) + k = ch.downcase + if @registry.has_key?(k) + data = @registry[k] + if data.has_key?(:oldtopic) + changetopic(m, ch, data[:oldtopic].dup) + return + end + end + + m.reply "No recent changes were recorded for #{ch}" end end plugin = TopicPlugin.new -plugin.map 'topic :command *text', :action => 'handletopic', :public => true, :private => false -plugin.map 'topic :channel :command *text', :action => 'handletopic', :public => false, :private => true + +plugin.map 'topic :command [*text]', :action => 'handletopic', :public => true, :private => false +plugin.map 'topic :channel :command [*text]', :action => 'handletopic', :public => false, :private => true + +plugin.default_auth('*', false) +