4 # :title: lart/praise plugin for rbot
6 # Author:: Michael Brailsford <brailsmt@yahoo.com> aka brailsmt
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
9 # Copyright:: (C) 2002 Michael Brailsford. All rights reserved.
10 # Copyright:: (C) 2006 Giuseppe Bilotta. All rights reserved.
12 # License:: This plugin is licensed under the BSD license. The terms of
15 # Redistribution and use in source and binary forms, with or without
16 # modification, are permitted provided that the following conditions
19 # 1. Redistributions of source code must retain the above copyright notice,
20 # this list of conditions and the following disclaimer.
22 # 2. Redistributions in binary form must reproduce the above copyright
23 # notice, this list of conditions and the following disclaimer in the
24 # documentation and/or other materials provided with the distribution.
26 # Purpose:: Provide for humorous larts and praises
28 class LartPlugin < Plugin
39 def set_language(lang)
42 # We may be on an old installation, so on the first run read non-language-specific larts
43 unless defined?(@oldlart)
44 @oldlart = "#{@bot.botclass}/lart/larts"
45 @oldpraise = "#{@bot.botclass}/lart/praise"
48 @lartfile.replace "#{@bot.botclass}/lart/larts-#{lang}"
49 @praisefile.replace "#{@bot.botclass}/lart/praises-#{lang}"
52 if File.exists? @lartfile
53 IO.foreach(@lartfile) { |line|
56 elsif File.exists? @oldlart
57 IO.foreach(@oldlart) { |line|
61 if File.exists? @praisefile
62 IO.foreach(@praisefile) { |line|
63 @praises << line.chomp
65 elsif File.exists? @oldpraise
66 IO.foreach(@oldpraise) { |line|
67 @praises << line.chomp
74 return unless @changed
75 Dir.mkdir("#{@bot.botclass}/lart") if not FileTest.directory? "#{@bot.botclass}/lart"
76 # TODO implement safe saving here too
77 Utils.safe_save(@lartfile) { |file|
80 Utils.safe_save(@praisefile) { |file|
86 def help(plugin, topic="")
87 "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."
90 def handle_lart(m, params)
91 lart = @larts[get_msg_idx(@larts.length)]
93 m.reply "I dunno any larts"
96 who = params[:who].to_s
103 reason = "for trying to make me lart myself"
105 lart = replace_who lart, who
106 lart << " #{reason}" unless reason.empty?
111 def handle_praise(m, params)
112 praise = @praises[get_msg_idx(@praises.length)]
114 m.reply "I dunno any praises"
117 who = params[:who].to_s
118 reason = params[:why]
119 if who == m.sourcenick || who == "me"
120 params[:who] = m.sourcenick
121 params[:why] = "for praising himself"
122 handle_lart(m, params)
125 praise = replace_who praise, who
126 praise << " #{reason}" unless reason.empty?
131 def handle_addlart(m, params)
132 @larts << params[:lart].to_s
137 def handle_rmlart(m, params)
138 @larts.delete params[:lart].to_s
143 def handle_listlart(m, params)
144 rx = Regexp.new(params[:lart].to_s, true)
145 list = @larts.grep(rx)
147 m.reply list.join(" | "), :split_at => /\s+\|\s+/
149 m.reply "no lart found matching #{params[:lart]}"
153 def handle_addpraise(m, params)
154 @praises << params[:praise].to_s
159 def handle_rmpraise(m, params)
160 @praises.delete params[:praise].to_s
165 def handle_listpraise(m, params)
166 rx = Regexp.new(params[:praise].to_s, true)
167 list = @praises.grep(rx)
169 m.reply list.join(" | "), :split_at => /\s+\|\s+/
171 m.reply "no praise found matching #{params[:praise]}"
175 # The following are utils for larts/praises
176 def replace_who(msg, nick)
177 msg.gsub(/<who>/i, "#{nick}")
186 plugin = LartPlugin.new
188 plugin.map "lart *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_lart
189 plugin.map "praise *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_praise
191 plugin.map "addlart *lart", :action => :handle_addlart
192 plugin.map "addpraise *praise", :action => :handle_addpraise
194 plugin.map "rmlart *lart", :action => :handle_rmlart
195 plugin.map "rmpraise *praise", :action => :handle_rmpraise
197 plugin.map "listlart *lart", :action => :handle_listlart
198 plugin.map "listpraise *praise", :action => :handle_listpraise