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)
45 ch = m.channel.downcase
47 ch = param[:channel].downcase
50 txt = param[:text].join(" ")
52 unless @bot.channels.has_key?(ch)
53 m.reply "I am not in channel #{ch}"
59 topicappend(m, ch, txt)
61 topicprepend(m, ch, txt)
63 if txt =~ /\s*(-?\d+)\s+(.*)\s*/
67 topicaddat(m, ch, num, txt)
70 if txt =~ /\s*(-?\d+)\s*/
84 if txt =~ /\s*(-?\d+)\s+(.*)\s*/
88 replacetopic(m, ch, num, txt)
93 m.reply 'unknown command'
97 def topicsep(m, ch, txt)
104 m.reply "Topic separator set to #{getsep(ch)}"
108 if @registry.has_key?(ch)
115 topic = @bot.channels[ch].topic.to_s
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 if @registry.has_key?(ch)
129 if @registry[ch].has_key?(:separator)
130 return @registry[ch][:separator]
136 def topicaddat(m, channel, num, txt)
137 sep = getsep(channel)
138 topic = @bot.channels[channel].topic.to_s
139 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
140 topicarray.insert(num, txt)
141 newtopic = topicarray.join(" #{sep} ")
142 @bot.topic channel, newtopic
145 def topicappend(m, ch, txt)
146 topicaddat(m, ch, -1, txt)
149 def topicprepend(m, ch, txt)
150 topicaddat(m, ch, 0, txt)
153 def topicdel(m, channel, num)
154 sep = getsep(channel)
155 topic = @bot.channels[channel].topic.to_s
156 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
157 topicarray.delete_at(num)
158 newtopic = topicarray.join(" #{sep} ")
159 @bot.topic channel, newtopic
162 def learntopic(m, channel)
163 return if !@bot.auth.allow?("learntopic", m.source, m.replyto)
164 topic = @bot.channels[channel].topic.to_s
165 if @registry.has_key?(channel)
166 data = @registry[channel]
171 @registry[channel] = data
175 def replacetopic(m, channel, num, txt)
176 return if !@bot.auth.allow?("topic", m.source, m.replyto)
177 sep = getsep(channel)
178 topic = @bot.channels[channel].topic.to_s
179 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
180 topicarray[num] = txt
181 newtopic = topicarray.join(" #{sep} ")
182 @bot.topic channel, newtopic
185 def restoretopic(m, channel)
186 return if !@bot.auth.allow?("restoretopic", m.source, m.replyto)
187 if @registry.has_key?(channel) && @registry[channel].has_key?(:topic)
188 topic = @registry[channel][:topic]
189 @bot.topic channel, topic
191 m.reply "I don't remember any topic for this channel"
195 def topicset(m, channel, text)
196 return if !@bot.auth.allow?("topic", m.source, m.replyto)
197 @bot.topic channel, text
201 plugin = TopicPlugin.new
202 plugin.map 'topic :command *text', :action => 'handletopic', :public => true, :private => false
203 plugin.map 'topic :channel :command *text', :action => 'handletopic', :public => false, :private => true