1 # Author: Michael Brailsford <brailsmt@yahoo.com>
3 # Purpose: Provides the ability to track various tokens that are spoken in a
5 # Copyright: 2002 Michael Brailsford. All rights reserved.
6 # License: This plugin is licensed under the BSD license. The terms of
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
13 # 1. Redistributions of source code must retain the above copyright notice,
14 # this list of conditions and the following disclaimer.
16 # 2. Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
20 class StatsPlugin < Plugin
23 "stats" => "handle_stats",
24 "track" => "handle_track",
25 "untrack" => "handle_untrack",
26 "listtokens" => "handle_listtokens",
27 "rmabuser" => "handle_rmabuser"
35 #check to see if a stats token file already exists for this channel...
36 Dir["#{@bot.botclass}/stats/*"].each { |fname|
37 channel = File.basename fname
39 IO.foreach(fname) { |line|
40 if line =~ /^(\S+)\s*<=>(.*)/
41 tokens[$1] = parse_token_stats $2
44 @channels[channel] = tokens
54 def help(plugin, topic="")
55 "Stats: The stats plugin tracks various tokens from users in the channel. The tokens are only tracked if it is the only thing on a line.\nUsage: stats <token> -- lists the stats for <token>\n [un]track <token> -- Adds or deletes <token> from the list of tokens\n listtokens -- lists the tokens that are currently being tracked"
60 if not m.params and not m.plugin =~ /listtokens/
61 m.reply "What a crazy fool! Did you mean |help stats?"
65 meth = self.method(@@commands[m.plugin])
71 Dir.mkdir("#{@bot.botclass}/stats") if not FileTest.directory?("#{@bot.botclass}/stats")
72 #save the tokens to a file...
73 @channels.each_pair { |channel, tokens|
75 File.open("#{@bot.botclass}/stats/#{channel}", "w") { |f|
76 tokens.each { |token, datahash|
77 f.puts "#{token} <=> #{datahash_to_s(datahash)}"
81 File.delete "#{@bot.botclass}/stats/#{channel}"
89 tokens = @channels[m.target]
90 if not @@commands[m.plugin]
91 tokens.each_pair { |key, hsh|
92 if not m.message.scan(/#{Regexp.escape(key)}/).empty?
94 hsh[m.sourcenick] += 1
102 #This is the old code {{{
104 # tokens = @channels[m.target]
105 # hsh = tokens[m.message]
107 # if hsh[m.sourcenick]
108 # hsh[m.sourcenick] += 1
110 # hsh[m.sourcenick] = 1
116 #The following are helper functions for the plugin {{{
117 def datahash_to_s(dhash)
119 dhash.each { |key, val|
120 rv << "#{key}:#{val} "
125 def parse_token_stats(stats)
127 stats.split(" ").each { |nickstat|
128 nick, stat = nickstat.split ":"
134 #The following are handler methods for dealing with each command from IRC {{{
139 tokens = @channels[m.target]
140 hsh = tokens[m.params]
143 sorted = hsh.sort { |i, j| j[1] <=> i[1] }
148 msg = "Stats for #{m.params}. Said #{total} times. The top sayers are "
150 msg << "#{sorted[0].join ':'}" if sorted[0]
151 msg << ", #{sorted[1].join ':'}" if sorted[1]
152 msg << ", and #{sorted[2].join ':'}" if sorted[2]
155 msg1 << "#{m.sourcenick} has said it "
157 msg1 << "#{hsh[m.sourcenick]} times."
162 msg << "#{m.params} has not been said yet!"
164 @bot.action m.replyto, msg
165 @bot.action m.replyto, msg1 if msg1
167 m.reply "#{m.params} is not currently being tracked."
175 if @channels[m.target]
176 tokens = @channels[m.target]
179 @channels[m.target] = tokens
181 tokens[m.params] = Hash.new
182 m.reply "now tracking #{m.params}"
187 def handle_untrack(m)
189 toks = @channels[m.target]
190 if toks.has_key? m.params
192 m.reply "no longer tracking #{m.params}"
194 m.reply "Are your signals crossed? Since when have I tracked that?"
202 def handle_listtokens(m)
203 if not m.private? and not @channels.empty?
204 tokens = @channels[m.target]
207 tokens.each_key { |k|
210 @bot.action m.replyto, "is currently keeping stats for: #{toks}"
212 @bot.action m.replyto, "is not currently keeping stats for anything"
215 @bot.action m.replyto, "is not currently keeping stats for anything"
220 def handle_rmabuser(m)
221 m.reply "This feature has not yet been implemented"
227 plugin = StatsPlugin.new
228 plugin.register("stats")
229 plugin.register("track")
230 plugin.register("untrack")
231 plugin.register("listtokens")
232 #plugin.register("rmabuser")