1 # Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
2 # Add a bunch of topic manipulation features
4 class TopicPlugin < Plugin
7 @separator = "|" # default separator
10 def help(plugin, topic="")
15 return "topic add <text> => add <text> at the end the topic"
17 return "topic prepend <text> => add <text> at the beginning of the topic"
19 return "topic addat <num> <text> => add <text> at position <num> of the topic"
21 return "topic del <num> => remove section <num> from the topic"
23 return "topic replace <num> <text> => Replaces section <num> with <text>"
24 when "sep", "separator"
25 return "topic sep(arator) [<text>] => get or set the topic section separator"
27 return "topic learn => remembers the topic for later"
29 return "topic restore => resets the topic to the latest remembered one"
31 return "topic clear => clears the topic"
33 return "topic set <text> => sets the topic to <text>"
35 return "topic undo => undoes the latest change to the topic"
37 return "topic add(at)|prepend|del(ete)|replace|sep(arator)|learn|restore|clear|set|undo: " + \
38 "manipulate the topic of the current channel; use topic <#channel> <command> " + \
39 "for private addressing"
44 def handletopic(m, param)
45 return unless m.kind_of?(PrivMessage)
49 ch = m.server.get_channel(param[:channel])
51 m.reply("I am not in channel #{param[:channel]}")
56 txt = param[:text].to_s
60 topicappend(m, ch, txt)
62 topicprepend(m, ch, txt)
64 if txt =~ /\s*(-?\d+)\s+(.*)\s*/
68 topicaddat(m, ch, num, txt)
71 if txt =~ /\s*(-?\d+)\s*/
85 if txt =~ /\s*(-?\d+)\s+(.*)\s*/
89 replacetopic(m, ch, num, txt)
96 m.reply 'unknown command'
100 def topicsep(m, ch, txt)
101 return if !@bot.auth.allow?("topic::edit::separator", m.source, m.replyto)
108 m.reply "Topic separator set to #{getsep(ch)}"
112 raise unless ch.class <= Irc::Channel
116 if @registry.has_key?(k)
123 topic = ch.topic.text
124 topicarray = topic.split(/\s+#{Regexp.escape(oldsep)}\s*/)
126 if sep != oldsep and topicarray.length > 0
127 newtopic = topicarray.join(" #{sep} ")
128 @bot.topic ch, newtopic if newtopic != topic
131 data[:separator] = sep
136 raise unless ch.class <= Irc::Channel
140 if @registry.has_key?(k)
141 if @registry[k].has_key?(:separator)
142 return @registry[k][:separator]
148 def topicaddat(m, ch, num, txt)
149 return if !@bot.auth.allow?("topic::edit::add", m.source, m.replyto)
151 topic = ch.topic.text
152 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
153 topicarray.insert(num, txt)
154 newtopic = topicarray.join(" #{sep} ")
155 changetopic(m, ch, newtopic)
158 def topicappend(m, ch, txt)
159 topicaddat(m, ch, -1, txt)
162 def topicprepend(m, ch, txt)
163 topicaddat(m, ch, 0, txt)
166 def topicdel(m, ch, num)
167 return if !@bot.auth.allow?("topic::edit::del", m.source, m.replyto)
169 topic = ch.topic.text
170 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
171 topicarray.delete_at(num)
172 newtopic = topicarray.join(" #{sep} ")
173 changetopic(m, ch, newtopic)
176 def learntopic(m, ch)
177 return if !@bot.auth.allow?("topic::store::store", m.source, m.replyto)
178 topic = ch.topic.text
180 if @registry.has_key?(k)
190 def replacetopic(m, ch, num, txt)
191 return if !@bot.auth.allow?("topic::edit::replace", m.source, m.replyto)
193 topic = ch.topic.text
194 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
195 topicarray[num] = txt
196 newtopic = topicarray.join(" #{sep} ")
197 changetopic(m, ch, newtopic)
200 def restoretopic(m, ch)
201 return if !@bot.auth.allow?("topic::store::restore", m.source, m.replyto)
203 if @registry.has_key?(k) && @registry[k].has_key?(:topic)
204 topic = @registry[k][:topic]
205 topicset(m, ch, topic)
207 m.reply "I don't remember any topic for #{ch}"
211 def topicset(m, ch, text)
212 return if !@bot.auth.allow?("topic::edit::replace", m.source, m.replyto)
213 changetopic(m, ch, text)
216 # This method changes the topic on channel +ch+ to +text+, storing
217 # the previous topic for undo
218 def changetopic(m, ch, text)
220 if @registry.has_key?(k)
226 data[:oldtopic] = ch.topic.text
234 if @registry.has_key?(k)
236 if data.has_key?(:oldtopic)
237 changetopic(m, ch, data[:oldtopic].dup)
242 m.reply "No recent changes were recorded for #{ch}"
246 plugin = TopicPlugin.new
248 plugin.map 'topic :command [*text]', :action => 'handletopic', :public => true, :private => false
249 plugin.map 'topic :channel :command [*text]', :action => 'handletopic', :public => false, :private => true
251 plugin.default_auth('*', false)