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