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