4 # :title: Topic manipulation plugin for rbot
6 # Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2006-2007 Giuseppe Bilotta
10 # Add a bunch of topic manipulation features
12 class TopicPlugin < Plugin
15 @separator = "|" # default separator
18 def help(plugin, topic="")
23 return "topic add <text> => add <text> at the end the topic"
25 return "topic prepend <text> => add <text> at the beginning of the topic"
27 return "topic addat <num> <text> => add <text> at position <num> of the topic"
29 return "topic del <num> => remove section <num> from the topic"
31 return "topic replace <num> <text> => Replaces section <num> with <text>"
32 when "sep", "separator"
33 return "topic sep(arator) [<text>] => get or set the topic section separator"
35 return "topic learn => remembers the topic for later"
37 return "topic restore => resets the topic to the latest remembered one"
39 return "topic clear => clears the topic"
41 return "topic set <text> => sets the topic to <text>"
43 return "topic undo => undoes the latest change to the topic"
45 return "topic add(at)|prepend|del(ete)|replace|sep(arator)|learn|restore|clear|set|undo: " + \
46 "manipulate the topic of the current channel; use topic <#channel> <command> " + \
47 "for private addressing"
52 def handletopic(m, param)
53 return unless m.kind_of?(PrivMessage)
57 ch = m.server.get_channel(param[:channel])
59 m.reply("I am not in channel #{param[:channel]}")
64 txt = param[:text].to_s
68 topicappend(m, ch, txt)
70 topicprepend(m, ch, txt)
72 if txt =~ /\s*(-?\d+)\s+(.*)\s*/
76 topicaddat(m, ch, num, txt)
79 if txt =~ /\s*(-?\d+)\s*/
93 if txt =~ /\s*(-?\d+)\s+(.*)\s*/
97 replacetopic(m, ch, num, txt)
104 m.reply 'unknown command'
108 def topicsep(m, ch, txt)
109 return if !@bot.auth.allow?("topic::edit::separator", m.source, m.replyto)
116 m.reply "Topic separator set to #{getsep(ch)}"
120 raise unless ch.class <= Irc::Channel
124 if @registry.has_key?(k)
131 topic = ch.topic.text
132 topicarray = topic.split(/\s+#{Regexp.escape(oldsep)}\s*/)
134 if sep != oldsep and topicarray.length > 0
135 newtopic = topicarray.join(" #{sep} ")
136 @bot.topic ch, newtopic if newtopic != topic
139 data[:separator] = sep
144 raise unless ch.class <= Irc::Channel
148 if @registry.has_key?(k)
149 if @registry[k].has_key?(:separator)
150 return @registry[k][:separator]
156 def topicaddat(m, ch, num, txt)
157 return if !@bot.auth.allow?("topic::edit::add", m.source, m.replyto)
159 topic = ch.topic.text
160 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
161 topicarray.insert(num, txt)
162 newtopic = topicarray.join(" #{sep} ")
163 changetopic(m, ch, newtopic)
166 def topicappend(m, ch, txt)
167 topicaddat(m, ch, -1, txt)
170 def topicprepend(m, ch, txt)
171 topicaddat(m, ch, 0, txt)
174 def topicdel(m, ch, num)
175 return if !@bot.auth.allow?("topic::edit::del", m.source, m.replyto)
177 topic = ch.topic.text
178 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
179 topicarray.delete_at(num)
180 newtopic = topicarray.join(" #{sep} ")
181 changetopic(m, ch, newtopic)
184 def learntopic(m, ch)
185 return if !@bot.auth.allow?("topic::store::store", m.source, m.replyto)
186 topic = ch.topic.text
188 if @registry.has_key?(k)
198 def replacetopic(m, ch, num, txt)
199 return if !@bot.auth.allow?("topic::edit::replace", m.source, m.replyto)
201 topic = ch.topic.text
202 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
203 topicarray[num] = txt
204 newtopic = topicarray.join(" #{sep} ")
205 changetopic(m, ch, newtopic)
208 def restoretopic(m, ch)
209 return if !@bot.auth.allow?("topic::store::restore", m.source, m.replyto)
211 if @registry.has_key?(k) && @registry[k].has_key?(:topic)
212 topic = @registry[k][:topic]
213 topicset(m, ch, topic)
215 m.reply "I don't remember any topic for #{ch}"
219 def topicset(m, ch, text)
220 return if !@bot.auth.allow?("topic::edit::replace", m.source, m.replyto)
221 changetopic(m, ch, text)
224 # This method changes the topic on channel +ch+ to +text+, storing
225 # the previous topic for undo
226 def changetopic(m, ch, text)
228 if @registry.has_key?(k)
234 data[:oldtopic] = ch.topic.text
242 if @registry.has_key?(k)
244 if data.has_key?(:oldtopic)
245 changetopic(m, ch, data[:oldtopic].dup)
250 m.reply "No recent changes were recorded for #{ch}"
254 plugin = TopicPlugin.new
256 plugin.map 'topic :command [*text]', :action => 'handletopic', :public => true, :private => false
257 plugin.map 'topic :channel :command [*text]', :action => 'handletopic', :public => false, :private => true
259 plugin.default_auth('*', false)