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)
103 m.reply "Topic separator set to #{getsep(ch)}"
107 raise unless ch.class <= Irc::Channel
111 if @registry.has_key?(k)
118 topic = ch.topic.text
119 topicarray = topic.split(/\s+#{Regexp.escape(oldsep)}\s*/)
121 if sep != oldsep and topicarray.length > 0
122 newtopic = topicarray.join(" #{sep} ")
123 @bot.topic ch, newtopic
126 data[:separator] = sep
131 raise unless ch.class <= Irc::Channel
135 if @registry.has_key?(k)
136 if @registry[k].has_key?(:separator)
137 return @registry[k][:separator]
143 def topicaddat(m, channel, num, txt)
144 sep = getsep(channel)
145 topic = channel.topic.to_s
146 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
147 topicarray.insert(num, txt)
148 newtopic = topicarray.join(" #{sep} ")
149 @bot.topic channel, newtopic
152 def topicappend(m, ch, txt)
153 topicaddat(m, ch, -1, txt)
156 def topicprepend(m, ch, txt)
157 topicaddat(m, ch, 0, txt)
160 def topicdel(m, channel, num)
161 sep = getsep(channel)
162 topic = channel.topic.to_s
163 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
164 topicarray.delete_at(num)
165 newtopic = topicarray.join(" #{sep} ")
166 @bot.topic channel, newtopic
169 def learntopic(m, channel)
170 return if !@bot.auth.allow?("learntopic", m.source, m.replyto)
171 topic = channel.topic.to_s
173 if @registry.has_key?(k)
183 def replacetopic(m, channel, num, txt)
184 return if !@bot.auth.allow?("topic", m.source, m.replyto)
185 sep = getsep(channel)
186 topic = @bot.channels[channel].topic.to_s
187 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
188 topicarray[num] = txt
189 newtopic = topicarray.join(" #{sep} ")
190 @bot.topic channel, newtopic
193 def restoretopic(m, channel)
194 return if !@bot.auth.allow?("restoretopic", m.source, m.replyto)
196 if @registry.has_key?(k) && @registry[k].has_key?(:topic)
197 topic = @registry[k][:topic]
198 @bot.topic channel, topic
200 m.reply "I don't remember any topic for this channel"
204 def topicset(m, channel, text)
205 return if !@bot.auth.allow?("topic", m.source, m.replyto)
206 @bot.topic channel, text
210 plugin = TopicPlugin.new
212 plugin.map 'topic :command *text', :action => 'handletopic', :public => true, :private => false
213 plugin.map 'topic :channel :command *text', :action => 'handletopic', :public => false, :private => true
215 plugin.default_auth('*', false)