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