| 1 | Quote = Struct.new("Quote", "num", "date", "source", "quote") |
|---|
| 2 | |
|---|
| 3 | class QuotePlugin < Plugin |
|---|
| 4 | def initialize |
|---|
| 5 | super |
|---|
| 6 | @lists = Hash.new |
|---|
| 7 | Dir["#{@bot.botclass}/quotes/*"].each {|f| |
|---|
| 8 | channel = File.basename(f) |
|---|
| 9 | @lists[channel] = Array.new if(!@lists.has_key?(channel)) |
|---|
| 10 | IO.foreach(f) {|line| |
|---|
| 11 | if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/) |
|---|
| 12 | num = $1.to_i |
|---|
| 13 | @lists[channel][num] = Quote.new(num, $2, $3, $4) |
|---|
| 14 | end |
|---|
| 15 | } |
|---|
| 16 | } |
|---|
| 17 | end |
|---|
| 18 | def save |
|---|
| 19 | Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes")) |
|---|
| 20 | @lists.each {|channel, quotes| |
|---|
| 21 | File.open("#{@bot.botclass}/quotes/#{channel}", "w") {|file| |
|---|
| 22 | quotes.compact.each {|q| |
|---|
| 23 | file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}" |
|---|
| 24 | } |
|---|
| 25 | } |
|---|
| 26 | } |
|---|
| 27 | end |
|---|
| 28 | def addquote(source, channel, quote) |
|---|
| 29 | @lists[channel] = Array.new if(!@lists.has_key?(channel)) |
|---|
| 30 | num = @lists[channel].length |
|---|
| 31 | @lists[channel][num] = Quote.new(num, Time.new, source, quote) |
|---|
| 32 | return num |
|---|
| 33 | end |
|---|
| 34 | def getquote(source, channel, num=nil) |
|---|
| 35 | return nil unless(@lists.has_key?(channel)) |
|---|
| 36 | return nil unless(@lists[channel].length > 0) |
|---|
| 37 | if(num) |
|---|
| 38 | if(@lists[channel][num]) |
|---|
| 39 | return @lists[channel][num], @lists[channel].length - 1 |
|---|
| 40 | end |
|---|
| 41 | else |
|---|
| 42 | # random quote |
|---|
| 43 | return @lists[channel].compact[rand(@lists[channel].nitems)], |
|---|
| 44 | @lists[channel].length - 1 |
|---|
| 45 | end |
|---|
| 46 | end |
|---|
| 47 | def delquote(channel, num) |
|---|
| 48 | return false unless(@lists.has_key?(channel)) |
|---|
| 49 | return false unless(@lists[channel].length > 0) |
|---|
| 50 | if(@lists[channel][num]) |
|---|
| 51 | @lists[channel][num] = nil |
|---|
| 52 | return true |
|---|
| 53 | end |
|---|
| 54 | return false |
|---|
| 55 | end |
|---|
| 56 | def countquote(source, channel=nil, regexp=nil) |
|---|
| 57 | unless(channel) |
|---|
| 58 | total=0 |
|---|
| 59 | @lists.each_value {|l| |
|---|
| 60 | total += l.compact.length |
|---|
| 61 | } |
|---|
| 62 | return total |
|---|
| 63 | end |
|---|
| 64 | return 0 unless(@lists.has_key?(channel)) |
|---|
| 65 | return 0 unless(@lists[channel].length > 0) |
|---|
| 66 | if(regexp) |
|---|
| 67 | matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/ } |
|---|
| 68 | else |
|---|
| 69 | matches = @lists[channel].compact |
|---|
| 70 | end |
|---|
| 71 | return matches.length |
|---|
| 72 | end |
|---|
| 73 | def searchquote(source, channel, regexp) |
|---|
| 74 | return nil unless(@lists.has_key?(channel)) |
|---|
| 75 | return nil unless(@lists[channel].length > 0) |
|---|
| 76 | matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/ } |
|---|
| 77 | if(matches.length > 0) |
|---|
| 78 | return matches[rand(matches.length)], @lists[channel].length - 1 |
|---|
| 79 | else |
|---|
| 80 | return nil |
|---|
| 81 | end |
|---|
| 82 | end |
|---|
| 83 | def help(plugin, topic="") |
|---|
| 84 | case topic |
|---|
| 85 | when "addquote" |
|---|
| 86 | 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" |
|---|
| 87 | when "delquote" |
|---|
| 88 | 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" |
|---|
| 89 | when "getquote" |
|---|
| 90 | 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" |
|---|
| 91 | when "searchquote" |
|---|
| 92 | 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" |
|---|
| 93 | when "topicquote" |
|---|
| 94 | 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" |
|---|
| 95 | when "countquote" |
|---|
| 96 | 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" |
|---|
| 97 | when "whoquote" |
|---|
| 98 | return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately" |
|---|
| 99 | when "whenquote" |
|---|
| 100 | return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately" |
|---|
| 101 | else |
|---|
| 102 | return "Quote module (Quote storage and retrieval) topics: addquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote" |
|---|
| 103 | end |
|---|
| 104 | end |
|---|
| 105 | def listen(m) |
|---|
| 106 | return unless(m.kind_of? PrivMessage) |
|---|
| 107 | |
|---|
| 108 | command = m.message.dup |
|---|
| 109 | if(m.address? && m.private?) |
|---|
| 110 | case command |
|---|
| 111 | when (/^addquote\s+(#\S+)\s+(.*)/) |
|---|
| 112 | channel = $1 |
|---|
| 113 | quote = $2 |
|---|
| 114 | if(@bot.auth.allow?("addquote", m.source, m.replyto)) |
|---|
| 115 | if(channel =~ /^#/) |
|---|
| 116 | num = addquote(m.source, channel, quote) |
|---|
| 117 | m.reply "added the quote (##{num})" |
|---|
| 118 | end |
|---|
| 119 | end |
|---|
| 120 | when (/^getquote\s+(#\S+)$/) |
|---|
| 121 | channel = $1 |
|---|
| 122 | if(@bot.auth.allow?("getquote", m.source, m.replyto)) |
|---|
| 123 | quote, total = getquote(m.source, channel) |
|---|
| 124 | if(quote) |
|---|
| 125 | m.reply "[#{quote.num}] #{quote.quote}" |
|---|
| 126 | else |
|---|
| 127 | m.reply "quote not found!" |
|---|
| 128 | end |
|---|
| 129 | end |
|---|
| 130 | when (/^getquote\s+(#\S+)\s+(\d+)$/) |
|---|
| 131 | channel = $1 |
|---|
| 132 | num = $2.to_i |
|---|
| 133 | if(@bot.auth.allow?("getquote", m.source, m.replyto)) |
|---|
| 134 | quote, total = getquote(m.source, channel, num) |
|---|
| 135 | if(quote) |
|---|
| 136 | m.reply "[#{quote.num}] #{quote.quote}" |
|---|
| 137 | else |
|---|
| 138 | m.reply "quote not found!" |
|---|
| 139 | end |
|---|
| 140 | end |
|---|
| 141 | when (/^whoquote\s+(#\S+)\s+(\d+)$/) |
|---|
| 142 | channel = $1 |
|---|
| 143 | num = $2.to_i |
|---|
| 144 | if(@bot.auth.allow?("getquote", m.source, m.replyto)) |
|---|
| 145 | quote, total = getquote(m.source, channel, num) |
|---|
| 146 | if(quote) |
|---|
| 147 | m.reply "quote #{quote.num} added by #{quote.source}" |
|---|
| 148 | else |
|---|
| 149 | m.reply "quote not found!" |
|---|
| 150 | end |
|---|
| 151 | end |
|---|
| 152 | when (/^whenquote\s+(#\S+)\s+(\d+)$/) |
|---|
| 153 | channel = $1 |
|---|
| 154 | num = $2.to_i |
|---|
| 155 | if(@bot.auth.allow?("getquote", m.source, m.replyto)) |
|---|
| 156 | quote, total = getquote(m.source, channel, num) |
|---|
| 157 | if(quote) |
|---|
| 158 | m.reply "quote #{quote.num} added on #{quote.date}" |
|---|
| 159 | else |
|---|
| 160 | m.reply "quote not found!" |
|---|
| 161 | end |
|---|
| 162 | end |
|---|
| 163 | when (/^topicquote\s+(#\S+)$/) |
|---|
| 164 | channel = $1 |
|---|
| 165 | if(@bot.auth.allow?("topicquote", m.source, m.replyto)) |
|---|
| 166 | quote, total = getquote(m.source, channel) |
|---|
| 167 | if(quote) |
|---|
| 168 | @bot.topic channel, "[#{quote.num}] #{quote.quote}" |
|---|
| 169 | else |
|---|
| 170 | m.reply "quote not found!" |
|---|
| 171 | end |
|---|
| 172 | end |
|---|
| 173 | when (/^topicquote\s+(#\S+)\s+(\d+)$/) |
|---|
| 174 | channel = $1 |
|---|
| 175 | num = $2.to_i |
|---|
| 176 | if(@bot.auth.allow?("topicquote", m.source, m.replyto)) |
|---|
| 177 | quote, total = getquote(m.source, channel, num) |
|---|
| 178 | if(quote) |
|---|
| 179 | @bot.topic channel, "[#{quote.num}] #{quote.quote}" |
|---|
| 180 | else |
|---|
| 181 | m.reply "quote not found!" |
|---|
| 182 | end |
|---|
| 183 | end |
|---|
| 184 | when (/^delquote\s+(#\S+)\s+(\d+)$/) |
|---|
| 185 | channel = $1 |
|---|
| 186 | num = $2.to_i |
|---|
| 187 | if(@bot.auth.allow?("delquote", m.source, m.replyto)) |
|---|
| 188 | if(delquote(channel, num)) |
|---|
| 189 | @bot.okay m.replyto |
|---|
| 190 | else |
|---|
| 191 | m.reply "quote not found!" |
|---|
| 192 | end |
|---|
| 193 | end |
|---|
| 194 | when (/^searchquote\s+(#\S+)\s+(.*)$/) |
|---|
| 195 | channel = $1 |
|---|
| 196 | reg = $2 |
|---|
| 197 | if(@bot.auth.allow?("getquote", m.source, m.replyto)) |
|---|
| 198 | quote, total = searchquote(m.source, channel, reg) |
|---|
| 199 | if(quote) |
|---|
| 200 | m.reply "[#{quote.num}] #{quote.quote}" |
|---|
| 201 | else |
|---|
| 202 | m.reply "quote not found!" |
|---|
| 203 | end |
|---|
| 204 | end |
|---|
| 205 | when (/^countquote$/) |
|---|
| 206 | if(@bot.auth.allow?("getquote", m.source, m.replyto)) |
|---|
| 207 | total = countquote(m.source) |
|---|
| 208 | m.reply "#{total} quotes" |
|---|
| 209 | end |
|---|
| 210 | when (/^countquote\s+(#\S+)\s*(.*)$/) |
|---|
| 211 | channel = $1 |
|---|
| 212 | reg = $2 |
|---|
| 213 | if(@bot.auth.allow?("getquote", m.source, m.replyto)) |
|---|
| 214 | total = countquote(m.source, channel, reg) |
|---|
| 215 | if(reg.length > 0) |
|---|
| 216 | m.reply "#{total} quotes match: #{reg}" |
|---|
| 217 | else |
|---|
| 218 | m.reply "#{total} quotes" |
|---|
| 219 | end |
|---|
| 220 | end |
|---|
| 221 | end |
|---|
| 222 | elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, ""))) |
|---|
| 223 | case command |
|---|
| 224 | when (/^addquote\s+(.+)/) |
|---|
| 225 | if(@bot.auth.allow?("addquote", m.source, m.replyto)) |
|---|
| 226 | num = addquote(m.source, m.target, $1) |
|---|
| 227 | m.reply "added the quote (##{num})" |
|---|
| 228 | end |
|---|
| 229 | when (/^getquote$/) |
|---|
| 230 | if(@bot.auth.allow?("getquote", m.source, m.replyto)) |
|---|
| 231 | quote, total = getquote(m.source, m.target) |
|---|
| 232 | if(quote) |
|---|
| 233 | m.reply "[#{quote.num}] #{quote.quote}" |
|---|
| 234 | else |
|---|
| 235 | m.reply "no quotes found!" |
|---|
| 236 | end |
|---|
| 237 | end |
|---|
| 238 | when (/^getquote\s+(\d+)$/) |
|---|
| 239 | num = $1.to_i |
|---|
| 240 | if(@bot.auth.allow?("getquote", m.source, m.replyto)) |
|---|
| 241 | quote, total = getquote(m.source, m.target, num) |
|---|
| 242 | if(quote) |
|---|
| 243 | m.reply "[#{quote.num}] #{quote.quote}" |
|---|
| 244 | else |
|---|
| 245 | m.reply "quote not found!" |
|---|
| 246 | end |
|---|
| 247 | end |
|---|
| 248 | when (/^whenquote\s+(\d+)$/) |
|---|
| 249 | num = $1.to_i |
|---|
| 250 | if(@bot.auth.allow?("getquote", m.source, m.replyto)) |
|---|
| 251 | quote, total = getquote(m.source, m.target, num) |
|---|
| 252 | if(quote) |
|---|
| 253 | m.reply "quote #{quote.num} added on #{quote.date}" |
|---|
| 254 | else |
|---|
| 255 | m.reply "quote not found!" |
|---|
| 256 | end |
|---|
| 257 | end |
|---|
| 258 | when (/^whoquote\s+(\d+)$/) |
|---|
| 259 | num = $1.to_i |
|---|
| 260 | if(@bot.auth.allow?("getquote", m.source, m.replyto)) |
|---|
| 261 | quote, total = getquote(m.source, m.target, num) |
|---|
| 262 | if(quote) |
|---|
| 263 | m.reply "quote #{quote.num} added by #{quote.source}" |
|---|
| 264 | else |
|---|
| 265 | m.reply "quote not found!" |
|---|
| 266 | end |
|---|
| 267 | end |
|---|
| 268 | when (/^topicquote$/) |
|---|
| 269 | if(@bot.auth.allow?("topicquote", m.source, m.replyto)) |
|---|
| 270 | quote, total = getquote(m.source, m.target) |
|---|
| 271 | if(quote) |
|---|
| 272 | @bot.topic m.target, "[#{quote.num}] #{quote.quote}" |
|---|
| 273 | else |
|---|
| 274 | m.reply "no quotes found!" |
|---|
| 275 | end |
|---|
| 276 | end |
|---|
| 277 | when (/^topicquote\s+(\d+)$/) |
|---|
| 278 | num = $1.to_i |
|---|
| 279 | if(@bot.auth.allow?("topicquote", m.source, m.replyto)) |
|---|
| 280 | quote, total = getquote(m.source, m.target, num) |
|---|
| 281 | if(quote) |
|---|
| 282 | @bot.topic m.target, "[#{quote.num}] #{quote.quote}" |
|---|
| 283 | else |
|---|
| 284 | m.reply "quote not found!" |
|---|
| 285 | end |
|---|
| 286 | end |
|---|
| 287 | when (/^delquote\s+(\d+)$/) |
|---|
| 288 | num = $1.to_i |
|---|
| 289 | if(@bot.auth.allow?("delquote", m.source, m.replyto)) |
|---|
| 290 | if(delquote(m.target, num)) |
|---|
| 291 | @bot.okay m.replyto |
|---|
| 292 | else |
|---|
| 293 | m.reply "quote not found!" |
|---|
| 294 | end |
|---|
| 295 | end |
|---|
| 296 | when (/^searchquote\s+(.*)$/) |
|---|
| 297 | reg = $1 |
|---|
| 298 | if(@bot.auth.allow?("getquote", m.source, m.replyto)) |
|---|
| 299 | quote, total = searchquote(m.source, m.target, reg) |
|---|
| 300 | if(quote) |
|---|
| 301 | m.reply "[#{quote.num}] #{quote.quote}" |
|---|
| 302 | else |
|---|
| 303 | m.reply "quote not found!" |
|---|
| 304 | end |
|---|
| 305 | end |
|---|
| 306 | when (/^countquote(?:\s+(.*))?$/) |
|---|
| 307 | reg = $1 |
|---|
| 308 | if(@bot.auth.allow?("getquote", m.source, m.replyto)) |
|---|
| 309 | total = countquote(m.source, m.target, reg) |
|---|
| 310 | if(reg && reg.length > 0) |
|---|
| 311 | m.reply "#{total} quotes match: #{reg}" |
|---|
| 312 | else |
|---|
| 313 | m.reply "#{total} quotes" |
|---|
| 314 | end |
|---|
| 315 | end |
|---|
| 316 | end |
|---|
| 317 | end |
|---|
| 318 | end |
|---|
| 319 | end |
|---|
| 320 | plugin = QuotePlugin.new |
|---|
| 321 | plugin.register("quotes") |
|---|