]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/topic.rb
Bug in topic plugin introduced in [440]
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / topic.rb
1 # Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
2 # Add a bunch of topic manipulation features
3
4 class TopicPlugin < Plugin
5   def initialize
6     super
7     @separator = "|" # default separator
8   end
9
10   def help(plugin, topic="")
11     case plugin
12     when "topic"
13       case topic
14       when "add"
15         return "topic add <text> => add <text> at the end the topic"
16       when "prepend"
17         return "topic prepend <text> => add <text> at the beginning of the topic"
18       when "addat"
19         return "topic addat <num> <text> => add <text> at position <num> of the topic"
20       when "del", "delete"
21         return "topic del <num> => remove section <num> from the topic"
22       when "replace"
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"
26       when "learn"
27         return "topic learn => remembers the topic for later"
28       when "restore"
29         return "topic restore => resets the topic to the latest remembered one"
30       when "clear"
31         return "topic clear => clears the topic"
32       when "set"
33         return "topic set <text> => sets the topic to <text>"
34       else
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"
38       end
39     end
40   end
41
42   def handletopic(m, param)
43     return unless m.kind_of?(PrivMessage)
44     if m.public?
45       ch = m.channel
46     else
47       ch = m.server.get_channel(param[:channel])
48       unless ch
49         m.reply("I am not in channel #{param[:channel]}")
50         return
51       end
52     end
53     cmd = param[:command]
54     txt = param[:text].join(" ")
55
56     case cmd
57     when /^a(dd|ppend)$/
58       topicappend(m, ch, txt)
59     when 'prepend'
60       topicprepend(m, ch, txt)
61     when 'addat'
62       if txt =~ /\s*(-?\d+)\s+(.*)\s*/
63         num = $1.to_i - 1
64         num += 1 if num < 0
65         txt = $2
66         topicaddat(m, ch, num, txt)
67       end
68     when /^del(ete)?$/
69       if txt =~ /\s*(-?\d+)\s*/
70         num=$1.to_i - 1
71         num += 1 if num < 0
72         topicdel(m, ch, num)
73       end
74     when 'set'
75       topicset(m, ch, txt)
76     when 'clear'
77       topicset(m, ch, '')
78     when /^sep(arator)?$/
79       topicsep(m, ch, txt)
80     when 'learn'
81       learntopic(m, ch)
82     when 'replace'
83       if txt =~ /\s*(-?\d+)\s+(.*)\s*/
84         num = $1.to_i - 1
85         num += 1 if num < 0
86         txt = $2
87         replacetopic(m, ch, num, txt)
88       end
89     when 'restore'
90       restoretopic(m, ch)
91     else
92       m.reply 'unknown command'
93     end
94   end
95
96   def topicsep(m, ch, txt)
97     if txt
98       sep = txt.strip
99       if sep != ""
100         setsep(ch, sep)
101       end
102     end
103     m.reply "Topic separator set to #{getsep(ch)}"
104   end
105
106   def setsep(ch, sep)
107     raise unless ch.class <= Irc::Channel
108     # TODO multiserver
109     k = ch.downcase
110
111     if @registry.has_key?(k)
112       data = @registry[k]
113     else
114       data = Hash.new
115     end
116
117     oldsep = getsep(ch)
118     topic = ch.topic.text
119     topicarray = topic.split(/\s+#{Regexp.escape(oldsep)}\s*/)
120
121     if sep != oldsep and topicarray.length > 0
122       newtopic = topicarray.join(" #{sep} ")
123       @bot.topic ch, newtopic
124     end
125
126     data[:separator] = sep
127     @registry[k] = data
128   end
129
130   def getsep(ch)
131     raise unless ch.class <= Irc::Channel
132     # TODO multiserver
133     k = ch.downcase
134
135     if @registry.has_key?(k)
136       if @registry[k].has_key?(:separator)
137         return @registry[k][:separator]
138       end
139     end
140     return @separator
141   end
142
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
150   end
151
152   def topicappend(m, ch, txt)
153     topicaddat(m, ch, -1, txt)
154   end
155
156   def topicprepend(m, ch, txt)
157     topicaddat(m, ch, 0, txt)
158   end
159
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
167   end
168
169   def learntopic(m, channel)
170     return if !@bot.auth.allow?("learntopic", m.source, m.replyto)
171     topic = channel.topic.to_s
172     k = channel.downcase
173     if @registry.has_key?(k)
174       data = @registry[k]
175     else
176       data = Hash.new
177     end
178     data[:topic] = topic
179     @registry[k] = data
180     m.okay
181   end
182
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
191   end
192
193   def restoretopic(m, channel)
194     return if !@bot.auth.allow?("restoretopic", m.source, m.replyto)
195     k = channel.downcase
196     if @registry.has_key?(k) && @registry[k].has_key?(:topic)
197       topic = @registry[k][:topic]
198       @bot.topic channel, topic
199     else
200       m.reply "I don't remember any topic for this channel"
201     end
202   end
203
204   def topicset(m, channel, text)
205     return if !@bot.auth.allow?("topic", m.source, m.replyto)
206     @bot.topic channel, text
207   end
208
209 end
210 plugin = TopicPlugin.new
211 plugin.map 'topic :command *text', :action => 'handletopic', :public => true, :private => false
212 plugin.map 'topic :channel :command *text', :action => 'handletopic', :public => false, :private => true
213