]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
giuseppe.bilotta's new topic plugin, as well as a tiny patch to the quotes plugin.
authorChris Gahan <chris@ill-logic.com>
Thu, 1 Jun 2006 06:20:41 +0000 (06:20 +0000)
committerChris Gahan <chris@ill-logic.com>
Thu, 1 Jun 2006 06:20:41 +0000 (06:20 +0000)
data/rbot/plugins/quotes.rb
data/rbot/plugins/topic.rb [new file with mode: 0755]

index 6094737c1143403928e6d532c8e19ce25128299a..10743dfa5aea4c3ece0405ffee5a475c4a03842e 100644 (file)
@@ -49,6 +49,7 @@ class QuotePlugin < Plugin
     return false unless(@lists[channel].length > 0)
     if(@lists[channel][num])
       @lists[channel][num] = nil
+      @lists[channel].pop if num == @lists[channel].length - 1
       return true
     end
     return false
diff --git a/data/rbot/plugins/topic.rb b/data/rbot/plugins/topic.rb
new file mode 100755 (executable)
index 0000000..075b694
--- /dev/null
@@ -0,0 +1,145 @@
+# 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)/
+  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>"
+      else
+       return "topic commands: addtopic, prependtopic, addtopicat, deltopic, learntopic, resumetopic, settopic"
+      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
+  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
+      end
+    end
+  end
+
+  def topicaddat(m, num, txt)
+    channel = m.channel.downcase
+    topic = @bot.channels[m.channel].topic.to_s
+    topicarray = topic.split(SEPARATOR)
+    topicarray.insert(num, txt)
+    newtopic = topicarray.join(SEPARATOR)
+    @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
+  end
+
+  def topicdel(m, num)
+    channel = m.channel.downcase
+    topic = @bot.channels[m.channel].topic.to_s
+    topicarray = topic.split(SEPARATOR)
+    topicarray.delete_at(num)
+    newtopic = topicarray.join(SEPARATOR)
+    @bot.topic channel, newtopic
+  end
+
+  def learntopic(m, param)
+    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"
+  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}"
+      @bot.topic channel, topic
+    else
+      @bot.say channel, "Non ricordo nulla"
+    end
+  end
+
+  def settopic(m, param)
+    return if !@bot.auth.allow?("topic", m.source, m.replyto)
+    channel = m.channel.downcase
+    debug "Channel: #{channel}"
+    @bot.topic channel, param[:text].to_s
+  end
+
+end
+plugin = TopicPlugin.new
+plugin.register 'topic'
+plugin.map 'settopic *text', :action => 'settopic'
+plugin.map 'resumetopic'
+plugin.map 'learntopic'
+