]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/lart.rb
Oops, forgot to reset @changed after save in salut
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / lart.rb
1 #  Original Author:
2 #               Michael Brailsford  <brailsmt@yahoo.com>
3 #               aka brailsmt
4 #  Author:      Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
5 #  Purpose:     Provide for humorous larts and praises
6 #  Original Copyright:
7 #               2002 Michael Brailsford.  All rights reserved.
8 #  Copyright:   2006 Giuseppe Bilotta.  All rights reserved.
9 #  License:     This plugin is licensed under the BSD license.  The terms of
10 #               which follow.
11 #
12 #  Redistribution and use in source and binary forms, with or without
13 #  modification, are permitted provided that the following conditions
14 #  are met:
15 #
16 #  1. Redistributions of source code must retain the above copyright notice,
17 #     this list of conditions and the following disclaimer.
18 #
19 #  2. Redistributions in binary form must reproduce the above copyright
20 #     notice, this list of conditions and the following disclaimer in the
21 #     documentation and/or other materials provided with the distribution.
22
23 class LartPlugin < Plugin
24
25   # Keep a 1:1 relation between commands and handlers
26   @@handlers = {
27     "lart" => "handle_lart",
28     "praise" => "handle_praise",
29     "addlart" => "handle_addlart",
30     "rmlart" => "handle_rmlart",
31     "addpraise" => "handle_addpraise",
32     "rmpraise" => "handle_rmpraise"
33   }
34
35   def name
36     "lart"
37   end
38
39   def initialize
40     @larts = Array.new
41     @praises = Array.new
42     @lartfile = ""
43     @praisefile = ""
44     @changed = false
45     super
46   end
47
48   def set_language(lang)
49     save
50     @lartfile.replace "#{@bot.botclass}/lart/larts-#{lang}"
51     @praisefile.replace "#{@bot.botclass}/lart/praises-#{lang}"
52     # We may be on an old installation, so on the first run read non-language-specific larts
53     @bulart.replace "#{@bot.botclass}/lart/larts"
54     @bupraise.replace "#{@bot.botclass}/lart/praise"
55     @larts.clear
56     @praises.clear
57     if File.exists? @lartfile
58       IO.foreach(@lartfile) { |line|
59         @larts << line.chomp
60       }
61     elsif File.exists? @bulart
62       IO.foreach(@bulart) { |line|
63         @larts << line.chomp
64       }
65     end
66     if File.exists? @praisefile
67       IO.foreach(@praisefile) { |line|
68         @praises << line.chomp
69       }
70     elsif File.exists? @bupraise
71       IO.foreach(@bupraise) { |line|
72         @praises << line.chomp
73       }
74     end
75     @changed = false
76   end
77
78   def cleanup
79   end
80
81   def save
82     return unless @changed
83     Dir.mkdir("#{@bot.botclass}/lart") if not FileTest.directory? "#{@bot.botclass}/lart"
84     # TODO implement safe saving here too
85     Utils.safe_save(@lartfile) { |file|
86       file.puts @larts
87     }
88     Utils.safe_save(@praisefile) { |file|
89       file.puts @praises
90     }
91     @changed = false
92   end
93
94   def privmsg(m)
95     if not m.params
96       m.reply "What a crazy fool!  Did you mean |help stats?"
97       return
98     end
99
100     meth = self.method(@@handlers[m.plugin])
101     meth.call(m) if(@bot.auth.allow?(m.plugin, m.source, m.replyto))
102   end
103
104   def help(plugin, topic="")
105     "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."
106   end
107
108   # The following are command handler
109
110   def handle_lart(m)
111     for_idx = m.params =~ /\s+\bfor\b/
112     if for_idx
113       nick = m.params[0, for_idx]
114     else
115       nick = m.params
116     end
117     lart = @larts[get_msg_idx(@larts.length)]
118     if lart == nil
119       m.reply "I dunno any larts"
120       return
121     end
122     if nick == @bot.nick
123       lart = replace_who lart, m.sourcenick
124       lart << " for trying to make me lart myself"
125     else
126       lart = replace_who lart, nick
127       lart << m.params[for_idx, m.params.length] if for_idx
128     end
129
130     @bot.action m.replyto, lart
131   end
132
133   def handle_praise(m)
134     for_idx = m.params =~ /\s+\bfor\b/
135     if for_idx
136       nick = m.params[0, for_idx]
137     else
138       nick = m.params
139     end
140     praise = @praises[get_msg_idx(@praises.length)]
141     if not praise
142       m.reply "I dunno any praises"
143       return
144     end
145
146     if nick == m.sourcenick
147       praise = @larts[get_msg_idx(@larts.length)]
148       praise = replace_who praise, nick
149     else
150       praise = replace_who praise, nick
151       praise << m.params.gsub("#{nick}", "")
152     end
153
154     @bot.action m.replyto, praise
155   end
156
157   def handle_addlart(m)
158     @larts << m.params
159     @changed = true
160     m.okay
161   end
162
163   def handle_rmlart(m)
164     @larts.delete m.params
165     @changed = true
166     m.okay
167   end
168
169   def handle_addpraise(m)
170     @praises << m.params
171     @changed = true
172     m.okay
173   end
174
175   def handle_rmpraise(m)
176     @praises.delete m.params
177     @changed = true
178     m.okay
179   end
180
181   #  The following are utils for larts/praises
182   def replace_who(msg, nick)
183     msg.gsub(/<who>/i, "#{nick}")
184   end
185
186   def get_msg_idx(max)
187     idx = rand(max)
188   end
189
190 end
191 plugin = LartPlugin.new
192 plugin.register("lart")
193 plugin.register("praise")
194
195 plugin.register("addlart")
196 plugin.register("addpraise")
197
198 plugin.register("rmlart")
199 plugin.register("rmpraise")