]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/lart.rb
Fix #57
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / lart.rb
1 #  Author:     Michael Brailsford  <brailsmt@yahoo.com>
2 #              aka brailsmt
3 #  Purpose:        Provide for humorous larts and praises
4 #  Copyright:  2002 Michael Brailsford.  All rights reserved.
5 #  License:    This plugin is licensed under the BSD license.  The terms of
6 #              which follow.
7 #
8 #  Redistribution and use in source and binary forms, with or without
9 #  modification, are permitted provided that the following conditions
10 #  are met:
11 #
12 #  1. Redistributions of source code must retain the above copyright notice,
13 #     this list of conditions and the following disclaimer.
14 #
15 #  2. Redistributions in binary form must reproduce the above copyright
16 #     notice, this list of conditions and the following disclaimer in the
17 #     documentation and/or other materials provided with the distribution.
18
19 class LartPlugin < Plugin
20
21   # Keep a 1:1 relation between commands and handlers
22   @@handlers = {
23     "lart" => "handle_lart",
24     "praise" => "handle_praise",
25     "addlart" => "handle_addlart",
26     "rmlart" => "handle_rmlart",
27     "addpraise" => "handle_addpraise",
28     "rmpraise" => "handle_rmpraise"
29   }
30
31   def name
32     "lart"
33   end
34
35   def initialize
36     super
37     @larts = Array.new
38     @praises = Array.new
39     #read in the lart and praise files
40     if File.exists? "#{@bot.botclass}/lart/larts"
41       IO.foreach("#{@bot.botclass}/lart/larts") { |line|
42         @larts << line.chomp
43       }
44     end
45     if File.exists? "#{@bot.botclass}/lart/praises"
46       IO.foreach("#{@bot.botclass}/lart/praises") { |line|
47         @praises << line.chomp
48       }
49     end
50   end
51
52   def cleanup
53   end
54
55   def save
56     Dir.mkdir("#{@bot.botclass}/lart") if not FileTest.directory? "#{@bot.botclass}/lart"
57     # TODO implement safe saving here too
58     File.open("#{@bot.botclass}/lart/larts", "w") { |file|
59       file.puts @larts
60     }
61     File.open("#{@bot.botclass}/lart/praises", "w") { |file|
62       file.puts @praises
63     }
64   end
65
66   def privmsg(m)
67     if not m.params
68       m.reply "What a crazy fool!  Did you mean |help stats?"
69       return
70     end
71
72     meth = self.method(@@handlers[m.plugin])
73     meth.call(m) if(@bot.auth.allow?(m.plugin, m.source, m.replyto))
74   end
75
76   def help(plugin, topic="")
77     "Lart: The lart plugin allows you to lart/praise someone in the channel. You can also add new larts and new praises as well as delete them. For the curious, LART is an acronym for Luser Attitude Readjustment Tool. Usage: lart <who> [<reason>] -- larts <who> for <reason>. praise <who> [<reason>] -- praises <who> for <reason>. [add|rm][lart|praise] -- Add or remove a lart or praise."
78   end
79
80   # The following are command handler
81
82   def handle_lart(m)
83     for_idx = m.params =~ /\s+\bfor\b/
84     if for_idx
85       nick = m.params[0, for_idx]
86     else
87       nick = m.params
88     end
89     lart = @larts[get_msg_idx(@larts.length)]
90     if lart == nil
91       m.reply "I dunno any larts"
92       return
93     end
94     if nick == @bot.nick
95       lart = replace_who lart, m.sourcenick
96       lart << " for trying to make me lart myself"
97     else
98       lart = replace_who lart, nick
99       lart << m.params[for_idx, m.params.length] if for_idx
100     end
101
102     @bot.action m.replyto, lart
103   end
104
105   def handle_praise(m)
106     for_idx = m.params =~ /\s+\bfor\b/
107     if for_idx
108       nick = m.params[0, for_idx]
109     else
110       nick = m.params
111     end
112     praise = @praises[get_msg_idx(@praises.length)]
113     if not praise
114       m.reply "I dunno any praises"
115       return
116     end
117
118     if nick == m.sourcenick
119       praise = @larts[get_msg_idx(@larts.length)]
120       praise = replace_who praise, nick
121     else
122       praise = replace_who praise, nick
123       praise << m.params.gsub("#{nick}", "")
124     end
125
126     @bot.action m.replyto, praise
127   end
128
129   def handle_addlart(m)
130     @larts << m.params
131     m.okay
132   end
133
134   def handle_rmlart(m)
135     @larts.delete m.params
136     m.okay
137   end
138
139   def handle_addpraise(m)
140     @praises << m.params
141     m.okay
142   end
143
144   def handle_rmpraise(m)
145     @praises.delete m.params
146     m.okay
147   end
148
149   #  The following are utils for larts/praises
150   def replace_who(msg, nick)
151     msg.gsub(/<who>/i, "#{nick}")
152   end
153
154   def get_msg_idx(max)
155     idx = rand(max)
156   end
157
158 end
159 plugin = LartPlugin.new
160 plugin.register("lart")
161 plugin.register("praise")
162
163 plugin.register("addlart")
164 plugin.register("addpraise")
165
166 plugin.register("rmlart")
167 plugin.register("rmpraise")