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