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