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