]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/topic.rb
make sure @commands is never nil in script.rb
[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.downcase
46     else
47       ch = param[:channel].downcase
48     end
49     cmd = param[:command]
50     txt = param[:text].join(" ")
51
52     unless @bot.channels.has_key?(ch)
53       m.reply "I am not in channel #{ch}"
54       return
55     end
56
57     case cmd
58     when /^a(dd|ppend)$/
59       topicappend(m, ch, txt)
60     when 'prepend'
61       topicprepend(m, ch, txt)
62     when 'addat'
63       if txt =~ /\s*(-?\d+)\s+(.*)\s*/
64         num = $1.to_i - 1
65         num += 1 if num < 0
66         txt = $2
67         topicaddat(m, ch, num, txt)
68       end
69     when /^del(ete)?$/
70       if txt =~ /\s*(-?\d+)\s*/
71         num=$1.to_i - 1
72         num += 1 if num < 0
73         topicdel(m, ch, num)
74       end
75     when 'set'
76       topicset(m, ch, txt)
77     when 'clear'
78       topicset(m, ch, '')
79     when /^sep(arator)?$/
80       topicsep(m, ch, txt)
81     when 'learn'
82       learntopic(m, ch)
83     when 'replace'
84       if txt =~ /\s*(-?\d+)\s+(.*)\s*/
85         num = $1.to_i - 1
86         num += 1 if num < 0
87         txt = $2
88         replacetopic(m, ch, num, txt)
89       end
90     when 'restore'
91       restoretopic(m, ch)
92     else
93       m.reply 'unknown command'
94     end
95   end
96
97   def topicsep(m, ch, txt)
98     if txt
99       sep = txt.strip
100       if sep != ""
101         setsep(ch, sep)
102       end
103     end
104     m.reply "Topic separator set to #{getsep(ch)}"
105   end
106
107   def setsep(ch, sep)
108     if @registry.has_key?(ch)
109       data = @registry[ch]
110     else
111       data = Hash.new
112     end
113
114     oldsep = getsep(ch)
115     topic = @bot.channels[ch].topic.to_s
116     topicarray = topic.split(/\s+#{Regexp.escape(oldsep)}\s*/)
117
118     if sep != oldsep and topicarray.length > 0
119       newtopic = topicarray.join(" #{sep} ")
120       @bot.topic ch, newtopic
121     end
122
123     data[:separator] = sep
124     @registry[ch] = data
125   end
126
127   def getsep(ch)
128     if @registry.has_key?(ch)
129       if @registry[ch].has_key?(:separator)
130         return @registry[ch][:separator]
131       end
132     end
133     return @separator
134   end
135
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
143   end
144
145   def topicappend(m, ch, txt)
146     topicaddat(m, ch, -1, txt)
147   end
148
149   def topicprepend(m, ch, txt)
150     topicaddat(m, ch, 0, txt)
151   end
152
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
160   end
161
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]
167     else
168       data = Hash.new
169     end
170     data[:topic] = topic
171     @registry[channel] = data
172     m.okay
173   end
174
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
183   end
184   
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
190     else
191       m.reply "I don't remember any topic for this channel"
192     end
193   end
194
195   def topicset(m, channel, text)
196     return if !@bot.auth.allow?("topic", m.source, m.replyto)
197     @bot.topic channel, text
198   end
199
200 end
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
204