]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/quotes.rb
quotes plugin: modernize dispatcher, add localization
[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 addquote(source, channel, quote)
60     @lists[channel] = Array.new if(!@lists.has_key?(channel))
61     num = @lists[channel].length 
62     @lists[channel][num] = Quote.new(num, Time.new, source.fullform, quote)
63     @changed[channel] = true
64     return num
65   end
66
67   def getquote(source, channel, num=nil)
68     return nil unless(@lists.has_key?(channel))
69     return nil unless(@lists[channel].length > 0)
70     if(num)
71       if(@lists[channel][num])
72         return @lists[channel][num], @lists[channel].length - 1
73       end
74     else
75       # random quote
76       return @lists[channel].compact[rand(@lists[channel].nitems)],
77       @lists[channel].length - 1
78     end
79   end
80
81   def delquote(channel, num)
82     return false unless(@lists.has_key?(channel))
83     return false unless(@lists[channel].length > 0)
84     if(@lists[channel][num])
85       @lists[channel][num] = nil
86       @lists[channel].pop if num == @lists[channel].length - 1
87       @changed[channel] = true
88       return true
89     end
90     return false
91   end
92
93   def countquote(source, channel=nil, regexp=nil)
94     unless(channel)
95       total=0
96       @lists.each_value {|l|
97         total += l.compact.length
98       }
99       return total
100     end
101     return 0 unless(@lists.has_key?(channel))
102     return 0 unless(@lists[channel].length > 0)
103     if(regexp)
104       matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
105     else
106       matches = @lists[channel].compact
107     end
108     return matches.length
109   end
110
111   def searchquote(source, channel, regexp)
112     return nil unless(@lists.has_key?(channel))
113     return nil unless(@lists[channel].length > 0)
114     matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
115     if(matches.length > 0)
116       return matches[rand(matches.length)], @lists[channel].length - 1
117     else
118       return nil
119     end
120   end
121
122   def help(plugin, topic="")
123     case topic
124     when "addquote"
125       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"
126     when "delquote"
127       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"
128     when "getquote"
129       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"
130     when "searchquote"
131       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"
132     when "topicquote"
133       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"
134     when "countquote"
135       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"
136     when "whoquote"
137       return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
138     when "whenquote"
139       return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
140     else
141       return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
142     end
143   end
144
145   def cmd_addquote(m, p)
146     channel = p[:channel] || m.channel.to_s
147     quote = p[:quote].to_s
148     num = addquote(m.source, channel, quote)
149     m.reply _("added the quote (#%{num})") % { :num => num }
150   end
151
152   def cmd_delquote(m, p)
153     channel = p[:channel] || m.channel.to_s
154     num = p[:num].to_i
155     if delquote(channel, num)
156       m.okay
157     else
158       m.reply "quote not found!"
159     end
160   end
161
162   def cmd_getquote(m, p)
163     channel = p[:channel] || m.channel.to_s
164     num = p[:num] ? p[:num].to_i : nil
165     quote, total = getquote(m.source, channel, num)
166     if quote
167       m.reply _("[%{num}] %{quote}") % {
168         :num => quote.num,
169         :quote => quote.quote
170       }
171     else
172       m.reply _("quote not found!")
173     end
174   end
175
176   def cmd_whoquote(m, p)
177     channel = p[:channel] || m.channel.to_s
178     num = p[:num] ? p[:num].to_i : nil
179     quote, total = getquote(m.source, channel, num)
180     if quote
181       m.reply _("quote %{num} added by %{source}") % {
182         :num => quote.num,
183         :source => quote.source
184       }
185     else
186       m.reply _("quote not found!")
187     end
188   end
189
190   def cmd_whenquote(m, p)
191     channel = p[:channel] || m.channel.to_s
192     num = p[:num] ? p[:num].to_i : nil
193     quote, total = getquote(m.source, channel, num)
194     if quote
195       m.reply _("quote %{num} added on %{date}") % {
196         :num => quote.num,
197         :date => quote.date
198       }
199     else
200       m.reply _("quote not found!")
201     end
202   end
203
204   def cmd_searchquote(m, p)
205     channel = p[:channel] || m.channel.to_s
206     reg = p[:reg].to_s
207     quote, total = searchquote(m.source, channel, reg)
208     if quote
209       m.reply _("[%{num}] %{quote}") % {
210         :num => quote.num,
211         :quote => quote.quote
212       }
213     else
214       m.reply _("quote not found!")
215     end
216   end
217
218   def cmd_countquote(m, p)
219     channel = p[:channel] || m.channel.to_s
220     reg = p[:reg] ? p[:reg].to_s : nil
221     total = countquote(m.source, channel, reg)
222     if reg.length > 0
223       m.reply _("%{total} quotes matching %{reg}") % {
224         :total => total,
225         :reg => reg
226       }
227     else
228       m.reply _("%{total} quotes") % { :total => total }
229     end
230   end
231
232   def cmd_topicquote(m, p)
233     channel = p[:channel] || m.channel.to_s
234     num = p[:num] ? p[:num].to_i : nil
235     quote, total = getquote(m.source, channel, num)
236     if quote
237       @bot.topic channel, _("[%{num}] %{quote}") % {
238         :num => quote.num,
239         :quote => quote.quote
240       }
241     else
242       m.reply _("quote not found!")
243     end
244   end
245 end
246
247 plugin = QuotePlugin.new
248 plugin.register("quotes")
249
250 plugin.default_auth('edit', false) # Prevent random people from removing quotes
251 plugin.default_auth('edit::add', true) # But allow them to add them
252
253 plugin.map "addquote *quote", :action => :cmd_addquote, :private => false, :auth_path => '!quote::edit::add!'
254 plugin.map "delquote :num", :action => :cmd_delquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::edit::del!'
255 plugin.map "getquote [:num]", :action => :cmd_getquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::get!'
256 plugin.map "whoquote :num", :action => :cmd_whoquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::who!'
257 plugin.map "whenquote :num", :action => :cmd_whenquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::when!'
258 plugin.map "searchquote *reg", :action => :cmd_searchquote, :private => false, :auth_path => '!quote::view::search!'
259 plugin.map "countquote [*reg]", :action => :cmd_countquote, :private => false, :auth_path => '!quote::view::count!'
260 plugin.map "topicquote [:num]", :action => :cmd_topicquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::topic!'
261
262 plugin.default_auth('other::edit', false) # Prevent random people from editing other channels quote lists by default
263 plugin.default_auth('other::view', true) # But allow them to view them
264
265 plugin.map "addquote :channel *quote", :action => :cmd_addquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::edit::add!'
266 plugin.map "delquote :channel :num", :action => :cmd_delquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::edit::del!'
267 plugin.map "getquote :channel [:num]", :action => :cmd_getquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::get!'
268 plugin.map "whoquote :channel :num", :action => :cmd_whoquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::who!'
269 plugin.map "whenquote :channel :num", :action => :cmd_whenquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::when!'
270 plugin.map "searchquote :channel *reg", :action => :cmd_searchquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::search!'
271 plugin.map "countquote [:channel] [*reg]", :action => :cmd_countquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::count!'
272 plugin.map "topicquote :channel [:num]", :action => :cmd_topicquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::topic!'