1 # Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
2 # Add a bunch of topic manipulation features
3 # NOTE: topic separator is defined by the global symbol SEPARATOR
7 class TopicPlugin < Plugin
10 @addtopic = /(?:topic(?:append|add)|(?:append|add)topic)/
11 @prependtopic = /(?:topicprepend|prependtopic)/
12 @addtopicat = /(?:(?:addtopic|topicadd)at)/
13 @deltopic = /(?:deltopic|topicdel)/
16 def help(plugin, topic="")
21 return "#{topic} <text> => add <text> at the end the topic"
23 return "#{topic} <text> => add <text> at the beginning of the topic"
25 return "#{topic} <num> <text> => add <text> at position <num> of the topic"
27 return "#{topic} <num> => remove section <num> from the topic"
29 return "learntopic => remembers the topic for later"
31 return "resumetopic => resets the topic to the latest remembered one"
33 return "settopic <text> => sets the topic to <text>"
35 return "topic commands: addtopic, prependtopic, addtopicat, deltopic, learntopic, resumetopic, settopic"
38 return "learntopic => remembers the topic for later"
40 return "resumetopic => resets the topic to the latest remembered one"
42 return "settopic <text> => sets the topic to <text>"
47 return unless m.kind_of?(PrivMessage) && m.public?
48 command = m.message.dup
50 if m.address? || command.gsub!(/^!/, "")
52 when /^#{@addtopic}\s+(.*)\s*/
55 if @bot.auth.allow?("topic", m.source, m.replyto)
58 when /^#{@prependtopic}\s+(.*)\s*/
61 if @bot.auth.allow?("topic", m.source, m.replyto)
64 when /^#{@addtopicat}\s+(-?\d+)\s+(.*)\s*/
69 if @bot.auth.allow?("topic", m.source, m.replyto)
70 topicaddat(m, num, txt)
72 when /^#{@deltopic}\s+(-?\d+)\s*/
76 if @bot.auth.allow?("topic", m.source, m.replyto)
83 def topicaddat(m, num, txt)
84 channel = m.channel.downcase
85 topic = @bot.channels[m.channel].topic.to_s
86 topicarray = topic.split(SEPARATOR)
87 topicarray.insert(num, txt)
88 newtopic = topicarray.join(SEPARATOR)
89 @bot.topic channel, newtopic
92 def topicappend(m, txt)
93 channel = m.channel.downcase
94 topic = @bot.channels[m.channel].topic.to_s
95 topicarray = topic.split(SEPARATOR)
97 newtopic = topicarray.join(SEPARATOR)
98 @bot.topic channel, newtopic
102 channel = m.channel.downcase
103 topic = @bot.channels[m.channel].topic.to_s
104 topicarray = topic.split(SEPARATOR)
105 topicarray.delete_at(num)
106 newtopic = topicarray.join(SEPARATOR)
107 @bot.topic channel, newtopic
110 def learntopic(m, param)
111 return if !@bot.auth.allow?("learntopic", m.source, m.replyto)
112 channel = m.channel.downcase
114 topic = @bot.channels[m.channel].topic.to_s
115 @registry[channel] = topic
116 @bot.say channel, "Ok"
119 def resumetopic(m, param)
120 return if !@bot.auth.allow?("resumetopic", m.source, m.replyto)
121 channel = m.channel.downcase
122 debug "Channel: #{channel}"
123 if @registry.has_key?(channel)
124 topic = @registry[channel]
125 debug "Channel: #{channel}, topic: #{topic}"
126 @bot.topic channel, topic
128 @bot.say channel, "Non ricordo nulla"
132 def settopic(m, param)
133 return if !@bot.auth.allow?("topic", m.source, m.replyto)
134 channel = m.channel.downcase
135 debug "Channel: #{channel}"
136 @bot.topic channel, param[:text].to_s
140 plugin = TopicPlugin.new
141 plugin.register 'topic'
142 plugin.map 'settopic *text', :action => 'settopic'
143 plugin.map 'resumetopic'
144 plugin.map 'learntopic'