summaryrefslogtreecommitdiff
path: root/contrib/plugins/stats.rb
blob: 4cafbbe973c848605fdbdc8bc882ef26feb3c5cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#  Author:    Michael Brailsford  <brailsmt@yahoo.com>
#             aka brailsmt
#  Purpose:   Provides the ability to track various tokens that are spoken in a
#             channel.
#  Copyright: 2002 Michael Brailsford.  All rights reserved.
#  License:   This plugin is licensed under the BSD license.  The terms of
#             which follow.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#
#  1. Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer.
#
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.

class StatsPlugin < Plugin

	@@commands = { 
		"stats" => "handle_stats",
		"track" => "handle_track",
		"untrack" => "handle_untrack", 
		"listtokens" => "handle_listtokens",
		"rmabuser" => "handle_rmabuser"
	}

	#{{{
	def initialize
		super
		@listen = true
		@channels = Hash.new
		#check to see if a stats token file already exists for this channel...
		Dir["#{@bot.botclass}/stats/*"].each { |fname|
			channel = File.basename fname
			tokens = Hash.new
			IO.foreach(fname) { |line|
				if line =~ /^(\S+)\s*<=>(.*)/
					tokens[$1] = parse_token_stats $2
				end
			}
			@channels[channel] = tokens
		}
	end
	#}}}
	#{{{
	def cleanup
		@channels = nil
	end
	#}}}
	#{{{
	def help(plugin, topic="")
		"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"
	end
	#}}}
	#{{{
	def privmsg(m)
		if not m.params and not m.plugin =~ /listtokens/
			m.reply "What a crazy fool!  Did you mean |help stats?"
			return
		end
		
		meth = self.method(@@commands[m.plugin])
		meth.call(m)
	end
	#}}}
	#{{{
	def save
		Dir.mkdir("#{@bot.botclass}/stats") if not FileTest.directory?("#{@bot.botclass}/stats")
		#save the tokens to a file...
		@channels.each_pair { |channel, tokens|
			if not tokens.empty?
				File.open("#{@bot.botclass}/stats/#{channel}", "w") { |f|
					tokens.each { |token, datahash|
						f.puts "#{token} <=> #{datahash_to_s(datahash)}"
					}
				}
			else
				File.delete "#{@bot.botclass}/stats/#{channel}"
			end
		}
	end
	#}}}
	#{{{
	def listen(m)
		if not m.private?
			tokens = @channels[m.target]
			if not @@commands[m.plugin]
				tokens.each_pair { |key, hsh|
					if not m.message.scan(/#{Regexp.escape(key)}/).empty?
						if hsh[m.sourcenick]
							hsh[m.sourcenick] += 1
						else
							hsh[m.sourcenick] = 1
						end
					end
				}
			end
		end
#This is the old code	{{{
#		if not m.private?
#			tokens = @channels[m.target]
#			hsh = tokens[m.message]
#			if hsh
#				if hsh[m.sourcenick]
#					hsh[m.sourcenick] += 1
#				else
#					hsh[m.sourcenick] = 1
#				end
#			end
#		end	}}}
	end
	#}}}
	#The following are helper functions for the plugin	{{{
		def datahash_to_s(dhash)
			rv = ""
			dhash.each { |key, val|
				rv << "#{key}:#{val} "
			}
			rv.chomp
		end

		def parse_token_stats(stats)
			rv = Hash.new
			stats.split(" ").each { |nickstat|
				nick, stat = nickstat.split ":"
				rv[nick] = stat.to_i
			}
			rv
		end
		#}}}
	#The following are handler methods for dealing with each command from IRC	{{{
	#{{{
	def handle_stats(m)
		if not m.private?
			total = 0
			tokens = @channels[m.target]
			hsh = tokens[m.params]
			msg1 = ""
			if not hsh.empty?
				sorted = hsh.sort { |i, j| j[1] <=> i[1] }
				sorted.each { |a|
					total += a[1]
				}

				msg = "Stats for #{m.params}.  Said #{total} times.  The top sayers are "
				if sorted[0..2]
					msg << "#{sorted[0].join ':'}" if sorted[0]
					msg << ", #{sorted[1].join ':'}" if sorted[1]
					msg << ", and #{sorted[2].join ':'}" if sorted[2]
					msg << "."

					msg1 << "#{m.sourcenick} has said it "
					if hsh[m.sourcenick]
						msg1 << "#{hsh[m.sourcenick]} times."
					else
						msg1 << "0 times."
					end
				else
					msg << "#{m.params} has not been said yet!"
				end
				@bot.action m.replyto, msg
				@bot.action m.replyto, msg1 if msg1
			else
				m.reply "#{m.params} is not currently being tracked."
			end
		end
	end
	#}}}
	#{{{
	def handle_track(m)
		if not m.private?
			if @channels[m.target]
				tokens = @channels[m.target]
			else
				tokens = Hash.new
				@channels[m.target] = tokens
			end
			tokens[m.params] = Hash.new
			m.reply "now tracking #{m.params}"
		end
	end
	#}}}
	#{{{
	def handle_untrack(m)
		if not m.private?
			toks = @channels[m.target]
			if toks.has_key? m.params
				toks.delete m.params
				m.reply "no longer tracking #{m.params}"
			else
				m.reply "Are your signals crossed?  Since when have I tracked that?"
			end
		end

		toks = nil
	end
	#}}}
	#{{{
	def handle_listtokens(m)
		if not m.private? and not @channels.empty?
			tokens = @channels[m.target]
			unless tokens.empty?
				toks = ""
				tokens.each_key { |k|
					toks << "#{k} "
				}
				@bot.action m.replyto, "is currently keeping stats for:  #{toks}"
			else
				@bot.action m.replyto, "is not currently keeping stats for anything"
			end
		elsif not m.private?
			@bot.action m.replyto, "is not currently keeping stats for anything"
		end
	end
	#}}}
	#{{{
	def handle_rmabuser(m)
		m.reply "This feature has not yet been implemented"
	end
	#}}}
	#}}}

end
plugin = StatsPlugin.new
plugin.register("stats")
plugin.register("track")
plugin.register("untrack")
plugin.register("listtokens")
#plugin.register("rmabuser")