]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/quotes.rb
giuseppe.bilotta's new topic plugin, as well as a tiny patch to the quotes plugin.
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / quotes.rb
1 Quote = Struct.new("Quote", "num", "date", "source", "quote")
2
3 class QuotePlugin < Plugin
4   def initialize
5     super
6     @lists = Hash.new
7     Dir["#{@bot.botclass}/quotes/*"].each {|f|
8       channel = File.basename(f)
9       @lists[channel] = Array.new if(!@lists.has_key?(channel))
10       IO.foreach(f) {|line|
11         if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/)
12           num = $1.to_i
13           @lists[channel][num] = Quote.new(num, $2, $3, $4)
14         end
15       }
16     }
17   end
18   def save
19     Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes"))
20     @lists.each {|channel, quotes|
21       File.open("#{@bot.botclass}/quotes/#{channel}", "w") {|file|
22         quotes.compact.each {|q| 
23           file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
24         }
25       }
26     }
27   end
28   def addquote(source, channel, quote)
29     @lists[channel] = Array.new if(!@lists.has_key?(channel))
30     num = @lists[channel].length 
31     @lists[channel][num] = Quote.new(num, Time.new, source, quote)
32     return num
33   end
34   def getquote(source, channel, num=nil)
35     return nil unless(@lists.has_key?(channel))
36     return nil unless(@lists[channel].length > 0)
37     if(num)
38       if(@lists[channel][num])
39         return @lists[channel][num], @lists[channel].length - 1
40       end
41     else
42       # random quote
43       return @lists[channel].compact[rand(@lists[channel].nitems)],
44                                        @lists[channel].length - 1
45     end
46   end
47   def delquote(channel, num)
48     return false unless(@lists.has_key?(channel))
49     return false unless(@lists[channel].length > 0)
50     if(@lists[channel][num])
51       @lists[channel][num] = nil
52       @lists[channel].pop if num == @lists[channel].length - 1
53       return true
54     end
55     return false
56   end
57   def countquote(source, channel=nil, regexp=nil)
58     unless(channel)
59       total=0
60       @lists.each_value {|l|
61         total += l.compact.length
62       }
63       return total
64     end
65     return 0 unless(@lists.has_key?(channel))
66     return 0 unless(@lists[channel].length > 0)
67     if(regexp)
68       matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
69     else
70       matches = @lists[channel].compact
71     end
72     return matches.length
73   end
74   def searchquote(source, channel, regexp)
75     return nil unless(@lists.has_key?(channel))
76     return nil unless(@lists[channel].length > 0)
77     matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
78     if(matches.length > 0)
79        return matches[rand(matches.length)], @lists[channel].length - 1
80     else
81       return nil
82     end
83   end
84   def help(plugin, topic="")
85     case topic
86       when "addquote"
87         return "addquote [<channel>] <quote> => Add quote <quote> for channel <channel>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !addquote without addressing if so configured"
88       when "delquote"
89         return "delquote [<channel>] <num> => delete quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !delquote without addressing if so configured"
90       when "getquote"
91         return "getquote [<channel>] [<num>] => get quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Without <num>, a random quote will be returned. Responds to !getquote without addressing if so configured"
92       when "searchquote"
93         return "searchquote [<channel>] <regexp> => search for quote from <channel> that matches <regexp>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !searchquote without addressing if so configured"
94       when "topicquote"
95         return "topicquote [<channel>] [<num>] => set topic to quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Without <num>, a random quote will be set. Responds to !topicquote without addressing if so configured"
96       when "countquote"
97         return "countquote [<channel>] <regexp> => count quotes from <channel> that match <regexp>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !countquote without addressing if so configured"
98       when "whoquote"
99         return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
100       when "whenquote"
101         return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
102       else
103         return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
104     end
105   end
106   def listen(m)
107     return unless(m.kind_of? PrivMessage)
108
109     command = m.message.dup
110     if(m.address? && m.private?)
111       case command
112         when (/^addquote\s+(#\S+)\s+(.*)/)
113           channel = $1
114           quote = $2
115           if(@bot.auth.allow?("addquote", m.source, m.replyto))
116             if(channel =~ /^#/)
117               num = addquote(m.source, channel, quote)
118               m.reply "added the quote (##{num})"
119             end
120           end
121         when (/^getquote\s+(#\S+)$/)
122           channel = $1
123           if(@bot.auth.allow?("getquote", m.source, m.replyto))
124             quote, total = getquote(m.source, channel)
125             if(quote)
126               m.reply "[#{quote.num}] #{quote.quote}"
127             else
128               m.reply "quote not found!"
129             end
130           end
131         when (/^getquote\s+(#\S+)\s+(\d+)$/)
132           channel = $1
133           num = $2.to_i
134           if(@bot.auth.allow?("getquote", m.source, m.replyto))
135             quote, total = getquote(m.source, channel, num)
136             if(quote)
137               m.reply "[#{quote.num}] #{quote.quote}"
138             else
139               m.reply "quote not found!"
140             end
141           end
142         when (/^whoquote\s+(#\S+)\s+(\d+)$/)
143           channel = $1
144           num = $2.to_i
145           if(@bot.auth.allow?("getquote", m.source, m.replyto))
146             quote, total = getquote(m.source, channel, num)
147             if(quote)
148               m.reply "quote #{quote.num} added by #{quote.source}"
149             else
150               m.reply "quote not found!"
151             end
152           end
153         when (/^whenquote\s+(#\S+)\s+(\d+)$/)
154           channel = $1
155           num = $2.to_i
156           if(@bot.auth.allow?("getquote", m.source, m.replyto))
157             quote, total = getquote(m.source, channel, num)
158             if(quote)
159               m.reply "quote #{quote.num} added on #{quote.date}"
160             else
161               m.reply "quote not found!"
162             end
163           end
164         when (/^topicquote\s+(#\S+)$/)
165           channel = $1
166           if(@bot.auth.allow?("topicquote", m.source, m.replyto))
167             quote, total = getquote(m.source, channel)
168             if(quote)
169               @bot.topic channel, "[#{quote.num}] #{quote.quote}"
170             else
171               m.reply "quote not found!"
172             end
173           end
174         when (/^topicquote\s+(#\S+)\s+(\d+)$/)
175           channel = $1
176           num = $2.to_i
177           if(@bot.auth.allow?("topicquote", m.source, m.replyto))
178             quote, total = getquote(m.source, channel, num)
179             if(quote)
180               @bot.topic channel, "[#{quote.num}] #{quote.quote}"
181             else
182               m.reply "quote not found!"
183             end
184           end
185         when (/^delquote\s+(#\S+)\s+(\d+)$/)
186           channel = $1
187           num = $2.to_i
188           if(@bot.auth.allow?("delquote", m.source, m.replyto))
189             if(delquote(channel, num))
190               m.okay
191             else
192               m.reply "quote not found!"
193             end
194           end
195         when (/^searchquote\s+(#\S+)\s+(.*)$/)
196           channel = $1
197           reg = $2
198           if(@bot.auth.allow?("getquote", m.source, m.replyto))
199             quote, total = searchquote(m.source, channel, reg)
200             if(quote)
201               m.reply "[#{quote.num}] #{quote.quote}"
202             else
203               m.reply "quote not found!"
204             end
205           end
206         when (/^countquote$/)
207           if(@bot.auth.allow?("getquote", m.source, m.replyto))
208             total = countquote(m.source)
209             m.reply "#{total} quotes"
210           end
211         when (/^countquote\s+(#\S+)\s*(.*)$/)
212           channel = $1
213           reg = $2
214           if(@bot.auth.allow?("getquote", m.source, m.replyto))
215             total = countquote(m.source, channel, reg)
216             if(reg.length > 0)
217               m.reply "#{total} quotes match: #{reg}"
218             else
219               m.reply "#{total} quotes"
220             end
221           end
222       end
223     elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, "")))
224       case command
225         when (/^addquote\s+(.+)/)
226           if(@bot.auth.allow?("addquote", m.source, m.replyto))
227             num = addquote(m.source, m.target, $1)
228             m.reply "added the quote (##{num})"
229           end
230         when (/^getquote$/)
231           if(@bot.auth.allow?("getquote", m.source, m.replyto))
232             quote, total = getquote(m.source, m.target)
233             if(quote)
234               m.reply "[#{quote.num}] #{quote.quote}"
235             else
236               m.reply "no quotes found!"
237             end
238           end
239         when (/^getquote\s+(\d+)$/)
240           num = $1.to_i
241           if(@bot.auth.allow?("getquote", m.source, m.replyto))
242             quote, total = getquote(m.source, m.target, num)
243             if(quote)
244               m.reply "[#{quote.num}] #{quote.quote}"
245             else
246               m.reply "quote not found!"
247             end
248           end
249         when (/^whenquote\s+(\d+)$/)
250           num = $1.to_i
251           if(@bot.auth.allow?("getquote", m.source, m.replyto))
252             quote, total = getquote(m.source, m.target, num)
253             if(quote)
254               m.reply "quote #{quote.num} added on #{quote.date}"
255             else
256               m.reply "quote not found!"
257             end
258           end
259         when (/^whoquote\s+(\d+)$/)
260           num = $1.to_i
261           if(@bot.auth.allow?("getquote", m.source, m.replyto))
262             quote, total = getquote(m.source, m.target, num)
263             if(quote)
264               m.reply "quote #{quote.num} added by #{quote.source}"
265             else
266               m.reply "quote not found!"
267             end
268           end
269         when (/^topicquote$/)
270           if(@bot.auth.allow?("topicquote", m.source, m.replyto))
271             quote, total = getquote(m.source, m.target)
272             if(quote)
273               @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
274             else
275               m.reply "no quotes found!"
276             end
277           end
278         when (/^topicquote\s+(\d+)$/)
279           num = $1.to_i
280           if(@bot.auth.allow?("topicquote", m.source, m.replyto))
281             quote, total = getquote(m.source, m.target, num)
282             if(quote)
283               @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
284             else
285               m.reply "quote not found!"
286             end
287           end
288         when (/^delquote\s+(\d+)$/)
289           num = $1.to_i
290           if(@bot.auth.allow?("delquote", m.source, m.replyto))
291             if(delquote(m.target, num))
292               m.okay
293             else
294               m.reply "quote not found!"
295             end
296           end
297         when (/^searchquote\s+(.*)$/)
298           reg = $1
299           if(@bot.auth.allow?("getquote", m.source, m.replyto))
300             quote, total = searchquote(m.source, m.target, reg)
301             if(quote)
302               m.reply "[#{quote.num}] #{quote.quote}"
303             else
304               m.reply "quote not found!"
305             end
306           end
307         when (/^countquote(?:\s+(.*))?$/)
308           reg = $1
309           if(@bot.auth.allow?("getquote", m.source, m.replyto))
310             total = countquote(m.source, m.target, reg)
311             if(reg && reg.length > 0)
312               m.reply "#{total} quotes match: #{reg}"
313             else
314               m.reply "#{total} quotes"
315             end
316           end
317       end
318     end
319   end
320 end
321 plugin = QuotePlugin.new
322 plugin.register("quotes")