]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/quotes.rb
+ @bot.path and datafile methods
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / quotes.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Quotes plugin
5 #
6 # TODO:: switch to db
7
8 define_structure :Quote, :num, :date, :source, :quote
9
10 class QuotePlugin < Plugin
11   def dirname
12     'quotes'
13   end
14
15   def initialize
16     super
17     @lists = Hash.new
18     @changed = Hash.new
19     Dir[datafile '*'].each {|f|
20       next if File.directory?(f)
21       channel = File.basename(f)
22       @lists[channel] = Array.new if(!@lists.has_key?(channel))
23       IO.foreach(f) {|line|
24         if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/)
25           num = $1.to_i
26           @lists[channel][num] = Quote.new(num, $2, $3, $4)
27         end
28       }
29       @changed[channel] = false
30     }
31   end
32
33   def save
34     Dir.mkdir(datafile) unless FileTest.directory? datafile
35     @lists.each {|channel, quotes|
36       begin
37         if @changed[channel]
38           debug "Writing new quotefile for channel #{channel} ..."
39           Utils.safe_save(datafile channel) {|file|
40             quotes.compact.each {|q| 
41               file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
42             }
43           }
44           @changed[channel] = false
45         else
46           debug "Not writing quotefile for channel #{channel} (unchanged)"
47         end
48       rescue => e
49         error "failed to write quotefile for channel #{channel}!\n#{$!}"
50         error "#{e.class}: #{e}"
51         error e.backtrace.join("\n")
52       end
53     }
54   end
55
56   def cleanup
57     @lists.clear
58     @changed.clear
59     super
60   end
61
62   def lastquote(channel)
63     @lists[channel].length-1
64   end
65
66   def addquote(source, channel, quote)
67     @lists[channel] = Array.new if(!@lists.has_key?(channel))
68     num = @lists[channel].length 
69     @lists[channel][num] = Quote.new(num, Time.new, source.fullform, quote)
70     @changed[channel] = true
71     return num
72   end
73
74   def getquote(source, channel, num=nil)
75     return nil unless(@lists.has_key?(channel))
76     return nil unless(@lists[channel].length > 0)
77     if(num)
78       if(@lists[channel][num])
79         return @lists[channel][num], @lists[channel].length - 1
80       end
81     else
82       # random quote
83       return @lists[channel].compact[rand(@lists[channel].nitems)],
84       @lists[channel].length - 1
85     end
86   end
87
88   def delquote(channel, num)
89     return false unless(@lists.has_key?(channel))
90     return false unless(@lists[channel].length > 0)
91     if(@lists[channel][num])
92       @lists[channel][num] = nil
93       @lists[channel].pop if num == @lists[channel].length - 1
94       @changed[channel] = true
95       return true
96     end
97     return false
98   end
99
100   def countquote(source, channel=nil, regexp=nil)
101     unless(channel)
102       total=0
103       @lists.each_value {|l|
104         total += l.compact.length
105       }
106       return total
107     end
108     return 0 unless(@lists.has_key?(channel))
109     return 0 unless(@lists[channel].length > 0)
110     if(regexp)
111       matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
112     else
113       matches = @lists[channel].compact
114     end
115     return matches.length
116   end
117
118   def searchquote(source, channel, regexp)
119     return nil unless(@lists.has_key?(channel))
120     return nil unless(@lists[channel].length > 0)
121     matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
122     if(matches.length > 0)
123       return matches[rand(matches.length)], @lists[channel].length - 1
124     else
125       return nil
126     end
127   end
128
129   def help(plugin, topic="")
130     case plugin
131     when "addquote"
132       _("addquote [<channel>] <quote> => Add quote <quote> for channel <channel>. You only need to supply <channel> if you are addressing %{nick} privately.") % { :nick => @bot.nick }
133     when "delquote"
134       _("delquote [<channel>] <num> => delete quote from <channel> with number <num>. You only need to supply <channel> if you are addressing %{nick} privately.") % { :nick => @bot.nick }
135     when "getquote"
136       _("getquote [<channel>] [<num>] => get quote from <channel> with number <num>. You only need to supply <channel> if you are addressing %{nick} privately. Without <num>, a random quote will be returned.") % { :nick => @bot.nick }
137     when "searchquote"
138       _("searchquote [<channel>] <regexp> => search for quote from <channel> that matches <regexp>. You only need to supply <channel> if you are addressing %{nick} privately.") % { :nick => @bot.nick }
139     when "topicquote"
140       _("topicquote [<channel>] [<num>] => set topic to quote from <channel> with number <num>. You only need to supply <channel> if you are addressing %{nick} privately. Without <num>, a random quote will be set.") % { :nick => @bot.nick }
141     when "countquote"
142       _("countquote [<channel>] <regexp> => count quotes from <channel> that match <regexp>. You only need to supply <channel> if you are addressing %{nick} privately.") % { :nick => @bot.nick }
143     when "whoquote"
144       _("whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing %{nick} privately") % { :nick => @bot.nick }
145     when "whenquote"
146       _("whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing %{nick} privately") % { :nick => @bot.nick }
147     when "lastquote"
148       _("lastquote [<channel>] => show the last quote in a given channel. You only need to supply <channel> if you are addressing %{nick} privately") % { :nick => @bot.nick }
149     else
150       _("Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote, lastquote") % { :nick => @bot.nick }
151     end
152   end
153
154   def cmd_addquote(m, p)
155     channel = p[:channel] || m.channel.to_s
156     quote = p[:quote].to_s
157     num = addquote(m.source, channel, quote)
158     m.reply _("added the quote (#%{num})") % { :num => num }
159   end
160
161   def cmd_delquote(m, p)
162     channel = p[:channel] || m.channel.to_s
163     num = p[:num].to_i
164     if delquote(channel, num)
165       m.okay
166     else
167       m.reply _("quote not found!")
168     end
169   end
170
171   def cmd_getquote(m, p)
172     channel = p[:channel] || m.channel.to_s
173     num = p[:num] ? p[:num].to_i : nil
174     quote, total = getquote(m.source, channel, num)
175     if quote
176       m.reply _("[%{num}] %{quote}") % {
177         :num => quote.num,
178         :quote => quote.quote
179       }
180     else
181       m.reply _("quote not found!")
182     end
183   end
184
185   def cmd_whoquote(m, p)
186     channel = p[:channel] || m.channel.to_s
187     num = p[:num] ? p[:num].to_i : nil
188     quote, total = getquote(m.source, channel, num)
189     if quote
190       m.reply _("quote %{num} added by %{source}") % {
191         :num => quote.num,
192         :source => quote.source
193       }
194     else
195       m.reply _("quote not found!")
196     end
197   end
198
199   def cmd_whenquote(m, p)
200     channel = p[:channel] || m.channel.to_s
201     num = p[:num] ? p[:num].to_i : nil
202     quote, total = getquote(m.source, channel, num)
203     if quote
204       m.reply _("quote %{num} added on %{date}") % {
205         :num => quote.num,
206         :date => quote.date
207       }
208     else
209       m.reply _("quote not found!")
210     end
211   end
212
213   def cmd_searchquote(m, p)
214     channel = p[:channel] || m.channel.to_s
215     reg = p[:reg].to_s
216     quote, total = searchquote(m.source, channel, reg)
217     if quote
218       m.reply _("[%{num}] %{quote}") % {
219         :num => quote.num,
220         :quote => quote.quote
221       }
222     else
223       m.reply _("quote not found!")
224     end
225   end
226
227   def cmd_countquote(m, p)
228     channel = p[:channel] || m.channel.to_s
229     reg = p[:reg] ? p[:reg].to_s : nil
230     total = countquote(m.source, channel, reg)
231     if reg.length > 0
232       m.reply _("%{total} quotes matching %{reg}") % {
233         :total => total,
234         :reg => reg
235       }
236     else
237       m.reply _("%{total} quotes") % { :total => total }
238     end
239   end
240
241   def cmd_topicquote(m, p)
242     channel = p[:channel] || m.channel.to_s
243     num = p[:num] ? p[:num].to_i : nil
244     quote, total = getquote(m.source, channel, num)
245     if quote
246       @bot.topic channel, _("[%{num}] %{quote}") % {
247         :num => quote.num,
248         :quote => quote.quote
249       }
250     else
251       m.reply _("quote not found!")
252     end
253   end
254
255   def cmd_lastquote(m, p)
256     channel = p[:channel] || m.channel.to_s
257     quote, total = getquote(m.source, channel, lastquote(channel))
258     if quote
259       m.reply _("[%{num}] %{quote}") % {
260         :num => quote.num,
261         :quote => quote.quote
262       }
263     else
264       m.reply _("quote not found!")
265     end
266   end
267 end
268
269 plugin = QuotePlugin.new
270 plugin.register("quotes")
271
272 plugin.default_auth('other::edit', false) # Prevent random people from editing other channels quote lists by default
273 plugin.default_auth('other::view', true) # But allow them to view them
274
275 plugin.map "addquote :channel *quote", :action => :cmd_addquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::edit::add!'
276 plugin.map "delquote :channel :num", :action => :cmd_delquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::edit::del!'
277 plugin.map "getquote :channel [:num]", :action => :cmd_getquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::get!'
278 plugin.map "whoquote :channel :num", :action => :cmd_whoquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::who!'
279 plugin.map "whenquote :channel :num", :action => :cmd_whenquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::when!'
280 plugin.map "searchquote :channel *reg", :action => :cmd_searchquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::search!'
281 plugin.map "countquote :channel [*reg]", :action => :cmd_countquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::count!'
282 plugin.map "topicquote :channel [:num]", :action => :cmd_topicquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::topic!'
283 plugin.map "lastquote :channel", :action => :cmd_lastquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::last!'
284
285 plugin.default_auth('edit', false) # Prevent random people from removing quotes
286 plugin.default_auth('edit::add', true) # But allow them to add them
287
288 plugin.map "addquote *quote", :action => :cmd_addquote, :private => false, :auth_path => '!quote::edit::add!'
289 plugin.map "delquote :num", :action => :cmd_delquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::edit::del!'
290 plugin.map "getquote [:num]", :action => :cmd_getquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::get!'
291 plugin.map "whoquote :num", :action => :cmd_whoquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::who!'
292 plugin.map "whenquote :num", :action => :cmd_whenquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::when!'
293 plugin.map "searchquote *reg", :action => :cmd_searchquote, :private => false, :auth_path => '!quote::view::search!'
294 plugin.map "countquote [*reg]", :action => :cmd_countquote, :private => false, :auth_path => '!quote::view::count!'
295 plugin.map "topicquote [:num]", :action => :cmd_topicquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::topic!'
296 plugin.map "lastquote", :action => :cmd_lastquote, :private => false, :auth_path => '!quote::view::last!'
297