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 restore => resets the topic to the latest remembered one"
29 return "topic set <text> => sets the topic to <text>"
31 return "topic add(at)|prepend|del|sep(arator)|learn|restore|set: " + \
32 "manipulate the topic of the current channel; use topic <#channel> <command> " + \
33 "for private addressing"
38 def handletopic(m, param)
39 return unless m.kind_of?(PrivMessage)
41 ch = m.channel.downcase
43 ch = param[:channel].downcase
46 txt = param[:text].join(" ")
49 topicappend(m, ch, txt)
51 topicprepend(m, ch, txt)
53 if txt =~ /\s*(-?\d+)\s+(.*)\s*/
57 topicaddat(m, ch, num, txt)
60 if txt =~ /\s*(-?\d+)\s*/
74 m.reply 'unknown command'
78 def topicsep(m, ch, txt)
85 m.reply "Topic separator set to #{getsep(ch)}"
89 if @registry.has_key?(ch)
94 data[:separator] = sep
99 if @registry.has_key?(ch)
100 if @registry[ch].has_key?(:separator)
101 return @registry[ch][:separator]
107 def topicaddat(m, channel, num, txt)
108 sep = getsep(channel)
109 topic = @bot.channels[channel].topic.to_s
110 topicarray = topic.split(/\s+#{sep}\s*/)
111 topicarray.insert(num, txt)
112 newtopic = topicarray.join(" #{sep} ")
113 @bot.topic channel, newtopic
116 def topicappend(m, ch, txt)
117 topicaddat(m, ch, -1, txt)
120 def topicprepend(m, ch, txt)
121 topicaddat(m, ch, 0, txt)
124 def topicdel(m, channel, num)
125 sep = getsep(channel)
126 topic = @bot.channels[channel].topic.to_s
127 topicarray = topic.split(/\s+#{sep}\s*/)
128 topicarray.delete_at(num)
129 newtopic = topicarray.join(" #{sep} ")
130 @bot.topic channel, newtopic
133 def learntopic(m, channel)
134 return if !@bot.auth.allow?("learntopic", m.source, m.replyto)
135 topic = @bot.channels[channel].topic.to_s
136 if @registry.has_key?(channel)
137 data = @registry[channel]
142 @registry[channel] = data
146 def restoretopic(m, channel)
147 return if !@bot.auth.allow?("restoretopic", m.source, m.replyto)
148 if @registry.has_key?(channel) && @registry[channel].has_key?(:topic)
149 topic = @registry[channel][:topic]
150 @bot.topic channel, topic
152 m.reply "I don't remember any topic for this channel"
156 def topicset(m, channel, text)
157 return if !@bot.auth.allow?("topic", m.source, m.replyto)
158 @bot.topic channel, text
162 plugin = TopicPlugin.new
163 plugin.map 'topic :command *text', :action => 'handletopic', :public => true, :private => false
164 plugin.map 'topic :channel :command *text', :action => 'handletopic', :public => false, :private => true