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 sep(arator) [<text>] => get or set the topic section separator"
25 return "topic learn => remembers the topic for later"
27 return "topic replace <num> <text> => Replaces section <num> with <text>"
29 return "topic restore => resets the topic to the latest remembered one"
31 return "topic set <text> => sets the topic to <text>"
33 return "topic add(at)|prepend|del|sep(arator)|learn|replace|restore|set: " + \
34 "manipulate the topic of the current channel; use topic <#channel> <command> " + \
35 "for private addressing"
40 def handletopic(m, param)
41 return unless m.kind_of?(PrivMessage)
43 ch = m.channel.downcase
45 ch = param[:channel].downcase
48 txt = param[:text].join(" ")
51 topicappend(m, ch, txt)
53 topicprepend(m, ch, txt)
55 if txt =~ /\s*(-?\d+)\s+(.*)\s*/
59 topicaddat(m, ch, num, txt)
62 if txt =~ /\s*(-?\d+)\s*/
74 if txt =~ /\s*(-?\d+)\s+(.*)\s*/
78 replacetopic(m, ch, num, txt)
83 m.reply 'unknown command'
87 def topicsep(m, ch, txt)
94 m.reply "Topic separator set to #{getsep(ch)}"
98 if @registry.has_key?(ch)
103 data[:separator] = sep
108 if @registry.has_key?(ch)
109 if @registry[ch].has_key?(:separator)
110 return @registry[ch][:separator]
116 def topicaddat(m, channel, num, txt)
117 sep = getsep(channel)
118 topic = @bot.channels[channel].topic.to_s
119 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
120 topicarray.insert(num, txt)
121 newtopic = topicarray.join(" #{sep} ")
122 @bot.topic channel, newtopic
125 def topicappend(m, ch, txt)
126 topicaddat(m, ch, -1, txt)
129 def topicprepend(m, ch, txt)
130 topicaddat(m, ch, 0, txt)
133 def topicdel(m, channel, num)
134 sep = getsep(channel)
135 topic = @bot.channels[channel].topic.to_s
136 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
137 topicarray.delete_at(num)
138 newtopic = topicarray.join(" #{sep} ")
139 @bot.topic channel, newtopic
142 def learntopic(m, channel)
143 return if !@bot.auth.allow?("learntopic", m.source, m.replyto)
144 topic = @bot.channels[channel].topic.to_s
145 if @registry.has_key?(channel)
146 data = @registry[channel]
151 @registry[channel] = data
155 def replacetopic(m, channel, num, txt)
156 return if !@bot.auth.allow?("topic", m.source, m.replyto)
157 sep = getsep(channel)
158 topic = @bot.channels[channel].topic.to_s
159 topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
160 topicarray[num] = txt
161 newtopic = topicarray.join(" #{sep} ")
162 @bot.topic channel, newtopic
165 def restoretopic(m, channel)
166 return if !@bot.auth.allow?("restoretopic", m.source, m.replyto)
167 if @registry.has_key?(channel) && @registry[channel].has_key?(:topic)
168 topic = @registry[channel][:topic]
169 @bot.topic channel, topic
171 m.reply "I don't remember any topic for this channel"
175 def topicset(m, channel, text)
176 return if !@bot.auth.allow?("topic", m.source, m.replyto)
177 @bot.topic channel, text
181 plugin = TopicPlugin.new
182 plugin.map 'topic :command *text', :action => 'handletopic', :public => true, :private => false
183 plugin.map 'topic :channel :command *text', :action => 'handletopic', :public => false, :private => true