]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/lart.rb
Try to read old language-agnostic lart/praises if language-specific ones are not...
[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 = nil
43     @praisefile = nil
44     super
45
46   end
47
48   def set_language(lang)
49     save
50     @lartfile = "#{@bot.botclass}/lart/larts-#{lang}"
51     @praisefile = "#{@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 = "#{@bot.botclass}/lart/larts"
54     @bupraise = "#{@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   end
76
77   def cleanup
78   end
79
80   def save
81     return if @lartfile.nil? and @praisefile.nil?
82     Dir.mkdir("#{@bot.botclass}/lart") if not FileTest.directory? "#{@bot.botclass}/lart"
83     # TODO implement safe saving here too
84     Utils.safe_save(@lartfile) { |file|
85       file.puts @larts
86     }
87     Utils.safe_save(@praisefile) { |file|
88       file.puts @praises
89     }
90   end
91
92   def privmsg(m)
93     if not m.params
94       m.reply "What a crazy fool!  Did you mean |help stats?"
95       return
96     end
97
98     meth = self.method(@@handlers[m.plugin])
99     meth.call(m) if(@bot.auth.allow?(m.plugin, m.source, m.replyto))
100   end
101
102   def help(plugin, topic="")
103     "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."
104   end
105
106   # The following are command handler
107
108   def handle_lart(m)
109     for_idx = m.params =~ /\s+\bfor\b/
110     if for_idx
111       nick = m.params[0, for_idx]
112     else
113       nick = m.params
114     end
115     lart = @larts[get_msg_idx(@larts.length)]
116     if lart == nil
117       m.reply "I dunno any larts"
118       return
119     end
120     if nick == @bot.nick
121       lart = replace_who lart, m.sourcenick
122       lart << " for trying to make me lart myself"
123     else
124       lart = replace_who lart, nick
125       lart << m.params[for_idx, m.params.length] if for_idx
126     end
127
128     @bot.action m.replyto, lart
129   end
130
131   def handle_praise(m)
132     for_idx = m.params =~ /\s+\bfor\b/
133     if for_idx
134       nick = m.params[0, for_idx]
135     else
136       nick = m.params
137     end
138     praise = @praises[get_msg_idx(@praises.length)]
139     if not praise
140       m.reply "I dunno any praises"
141       return
142     end
143
144     if nick == m.sourcenick
145       praise = @larts[get_msg_idx(@larts.length)]
146       praise = replace_who praise, nick
147     else
148       praise = replace_who praise, nick
149       praise << m.params.gsub("#{nick}", "")
150     end
151
152     @bot.action m.replyto, praise
153   end
154
155   def handle_addlart(m)
156     @larts << m.params
157     m.okay
158   end
159
160   def handle_rmlart(m)
161     @larts.delete m.params
162     m.okay
163   end
164
165   def handle_addpraise(m)
166     @praises << m.params
167     m.okay
168   end
169
170   def handle_rmpraise(m)
171     @praises.delete m.params
172     m.okay
173   end
174
175   #  The following are utils for larts/praises
176   def replace_who(msg, nick)
177     msg.gsub(/<who>/i, "#{nick}")
178   end
179
180   def get_msg_idx(max)
181     idx = rand(max)
182   end
183
184 end
185 plugin = LartPlugin.new
186 plugin.register("lart")
187 plugin.register("praise")
188
189 plugin.register("addlart")
190 plugin.register("addpraise")
191
192 plugin.register("rmlart")
193 plugin.register("rmpraise")