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])
48 return m.reply "I am not in channel #{ch}" unless ch
51 txt = param[:text].join(" ")
55 topicappend(m, ch, txt)
57 topicprepend(m, ch, txt)
59 if txt =~ /\s*(-?\d+)\s+(.*)\s*/
63 topicaddat(m, ch, num, txt)
66 if txt =~ /\s*(-?\d+)\s*/
80 if txt =~ /\s*(-?\d+)\s+(.*)\s*/
84 replacetopic(m, ch, num, txt)
89 m.reply 'unknown command'
93 def topicsep(m, ch, txt)
100 m.reply "Topic separator set to #{getsep(ch)}"
104 raise unless ch.class <= Irc::Channel
108 if @registry.has_key?(k)
115 topic = ch.topic.text
116 topicarray = topic.split(/\s+#{Regexp.escape(oldsep)}\s*/)
118 if sep != oldsep and topicarray.length > 0
119 newtopic = topicarray.join(" #{sep} ")
120 @bot.topic ch, newtopic
123 data[:separator] = sep
128 raise unless ch.class <= Irc::Channel
132 if @registry.has_key?(k)
133 if @registry[k].has_key?(:separator)
134 return @registry[k][:separator]
140 def topicaddat(m, channel, num, txt)
141 sep = getsep(channel)
142 topic = channel.topic.to_s
143 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
144 topicarray.insert(num, txt)
145 newtopic = topicarray.join(" #{sep} ")
146 @bot.topic channel, newtopic
149 def topicappend(m, ch, txt)
150 topicaddat(m, ch, -1, txt)
153 def topicprepend(m, ch, txt)
154 topicaddat(m, ch, 0, txt)
157 def topicdel(m, channel, num)
158 sep = getsep(channel)
159 topic = channel.topic.to_s
160 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
161 topicarray.delete_at(num)
162 newtopic = topicarray.join(" #{sep} ")
163 @bot.topic channel, newtopic
166 def learntopic(m, channel)
167 return if !@bot.auth.allow?("learntopic", m.source, m.replyto)
168 topic = channel.topic.to_s
170 if @registry.has_key?(k)
180 def replacetopic(m, channel, num, txt)
181 return if !@bot.auth.allow?("topic", m.source, m.replyto)
182 sep = getsep(channel)
183 topic = @bot.channels[channel].topic.to_s
184 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
185 topicarray[num] = txt
186 newtopic = topicarray.join(" #{sep} ")
187 @bot.topic channel, newtopic
190 def restoretopic(m, channel)
191 return if !@bot.auth.allow?("restoretopic", m.source, m.replyto)
193 if @registry.has_key?(k) && @registry[k].has_key?(:topic)
194 topic = @registry[k][:topic]
195 @bot.topic channel, topic
197 m.reply "I don't remember any topic for this channel"
201 def topicset(m, channel, text)
202 return if !@bot.auth.allow?("topic", m.source, m.replyto)
203 @bot.topic channel, text
207 plugin = TopicPlugin.new
208 plugin.map 'topic :command *text', :action => 'handletopic', :public => true, :private => false
209 plugin.map 'topic :channel :command *text', :action => 'handletopic', :public => false, :private => true