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