]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/karma.rb
initial import of rbot
[user/henk/code/ruby/rbot.git] / rbot / plugins / karma.rb
1 class KarmaPlugin < Plugin
2   def initialize
3     super
4
5     # this plugin only wants to store ints!
6     class << @registry
7       def store(val)
8         val.to_i
9       end
10       def restore(val)
11         val.to_i
12       end
13     end
14     @registry.set_default(0)
15
16     # import if old file format found
17     if(File.exist?("#{@bot.botclass}/karma.rbot"))
18       puts "importing old karma data"
19       IO.foreach("#{@bot.botclass}/karma.rbot") do |line|
20         if(line =~ /^(\S+)<=>([\d-]+)$/)
21           item = $1
22           karma = $2.to_i
23           @registry[item] = karma
24         end
25       end
26       File.delete("#{@bot.botclass}/karma.rbot")
27     end
28
29   end
30   def help(plugin, topic="")
31     "karma module: <thing>++/<thing>-- => increase/decrease karma for <thing>, karma for <thing>? => show karma for <thing>. Karma is a community rating system - only in-channel messages can affect karma and you cannot adjust your own."
32   end
33   def listen(m)
34     if(m.kind_of?(PrivMessage) && m.public?)
35       # in channel message, the kind we are interested in
36       if(m.message =~ /(\+\+|--)/)
37         string = m.message.sub(/\W(--|\+\+)(\(.*?\)|[^(++)(\-\-)\s]+)/, "\2\1")
38         seen = Hash.new
39         while(string.sub!(/(\(.*?\)|[^(++)(\-\-)\s]+)(\+\+|--)/, ""))
40           key = $1
41           change = $2
42           next if seen[key]
43           seen[key] = true
44
45           key.sub!(/^\((.*)\)$/, "\1")
46           key.gsub!(/\s+/, " ")
47           next unless(key.length > 0)
48           next if(key == m.sourcenick)
49           if(change == "++")
50             @registry[key] += 1
51           elsif(change == "--")
52             @registry[key] -= 1
53           end
54         end
55       end
56     end
57   end
58   def privmsg(m)
59     unless(m.params)
60       m.reply "incorrect usage: " + m.plugin
61       return
62     end
63     if(m.params =~ /^(?:for\s+)?(\S+?)\??$/)
64       thing = $1
65       karma = @registry[thing]
66       if(karma != 0)
67         m.reply "karma for #{thing}: #{@registry[thing]}"
68       else
69         m.reply "#{thing} has neutral karma"
70       end
71     end
72   end
73 end
74 plugin = KarmaPlugin.new
75 plugin.register("karma")