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 add(at)|prepend|del(ete)|replace|sep(arator)|learn|restore|clear|set: " + \
36 "manipulate the topic of the current channel; use topic <#channel> <command> " + \
37 "for private addressing"
42 def handletopic(m, param)
43 return unless m.kind_of?(PrivMessage)
47 ch = m.server.get_channel(param[:channel])
49 m.reply("I am not in channel #{param[:channel]}")
54 txt = param[:text].to_s
58 topicappend(m, ch, txt)
60 topicprepend(m, ch, txt)
62 if txt =~ /\s*(-?\d+)\s+(.*)\s*/
66 topicaddat(m, ch, num, txt)
69 if txt =~ /\s*(-?\d+)\s*/
83 if txt =~ /\s*(-?\d+)\s+(.*)\s*/
87 replacetopic(m, ch, num, txt)
92 m.reply 'unknown command'
96 def topicsep(m, ch, txt)
97 return if !@bot.auth.allow?("topic::edit::separator", m.source, m.replyto)
104 m.reply "Topic separator set to #{getsep(ch)}"
108 raise unless ch.class <= Irc::Channel
112 if @registry.has_key?(k)
119 topic = ch.topic.text
120 topicarray = topic.split(/\s+#{Regexp.escape(oldsep)}\s*/)
122 if sep != oldsep and topicarray.length > 0
123 newtopic = topicarray.join(" #{sep} ")
124 @bot.topic ch, newtopic
127 data[:separator] = sep
132 raise unless ch.class <= Irc::Channel
136 if @registry.has_key?(k)
137 if @registry[k].has_key?(:separator)
138 return @registry[k][:separator]
144 def topicaddat(m, channel, num, txt)
145 return if !@bot.auth.allow?("topic::edit::add", m.source, m.replyto)
146 sep = getsep(channel)
147 topic = channel.topic.to_s
148 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
149 topicarray.insert(num, txt)
150 newtopic = topicarray.join(" #{sep} ")
151 @bot.topic channel, newtopic
154 def topicappend(m, ch, txt)
155 topicaddat(m, ch, -1, txt)
158 def topicprepend(m, ch, txt)
159 topicaddat(m, ch, 0, txt)
162 def topicdel(m, channel, num)
163 return if !@bot.auth.allow?("topic::edit::del", m.source, m.replyto)
164 sep = getsep(channel)
165 topic = channel.topic.to_s
166 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
167 topicarray.delete_at(num)
168 newtopic = topicarray.join(" #{sep} ")
169 @bot.topic channel, newtopic
172 def learntopic(m, channel)
173 return if !@bot.auth.allow?("topic::store::store", m.source, m.replyto)
174 topic = channel.topic.to_s
176 if @registry.has_key?(k)
186 def replacetopic(m, channel, num, txt)
187 return if !@bot.auth.allow?("topic::edit::replace", m.source, m.replyto)
188 sep = getsep(channel)
189 topic = channel.topic.to_s
190 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
191 topicarray[num] = txt
192 newtopic = topicarray.join(" #{sep} ")
193 @bot.topic channel, newtopic
196 def restoretopic(m, channel)
197 return if !@bot.auth.allow?("topic::store::restore", m.source, m.replyto)
198 return if !@bot.auth.allow?("restoretopic", m.source, m.replyto)
200 if @registry.has_key?(k) && @registry[k].has_key?(:topic)
201 topic = @registry[k][:topic]
202 topicset(m, channel, topic)
204 m.reply "I don't remember any topic for this channel"
208 def topicset(m, channel, text)
209 return if !@bot.auth.allow?("topic::edit::replace", m.source, m.replyto)
210 @bot.topic channel, text
214 plugin = TopicPlugin.new
216 plugin.map 'topic :command *text', :action => 'handletopic', :public => true, :private => false
217 plugin.map 'topic :channel :command *text', :action => 'handletopic', :public => false, :private => true
219 plugin.default_auth('*', false)