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