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
100 reason = "for trying to make me lart myself"
102 lart = replace_who lart, who
103 lart << " #{reason}" unless reason.empty?
108 def handle_praise(m, params)
109 praise = @praises[get_msg_idx(@praises.length)]
111 m.reply "I dunno any praises"
114 who = params[:who].to_s
115 reason = params[:why]
116 if who == m.sourcenick
117 params[:why] = "for praising himself"
118 handle_lart(m, params)
121 praise = replace_who praise, who
122 praise << " #{reason}" unless reason.empty?
127 def handle_addlart(m, params)
128 @larts << params[:lart].to_s
133 def handle_rmlart(m, params)
134 @larts.delete params[:lart].to_s
139 def handle_listlart(m, params)
140 rx = Regexp.new(params[:lart].to_s, true)
141 list = @larts.grep(rx)
143 m.reply list.join(" | "), :split_at => /\s+\|\s+/
145 m.reply "no lart found matching #{params[:lart]}"
149 def handle_addpraise(m, params)
150 @praises << params[:praise].to_s
155 def handle_rmpraise(m, params)
156 @praises.delete params[:praise].to_s
161 def handle_listpraise(m, params)
162 rx = Regexp.new(params[:praise].to_s, true)
163 list = @praises.grep(rx)
165 m.reply list.join(" | "), :split_at => /\s+\|\s+/
167 m.reply "no praise found matching #{params[:praise]}"
171 # The following are utils for larts/praises
172 def replace_who(msg, nick)
173 msg.gsub(/<who>/i, "#{nick}")
182 plugin = LartPlugin.new
184 plugin.map "lart *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_lart
185 plugin.map "praise *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_praise
187 plugin.map "addlart *lart", :action => :handle_addlart
188 plugin.map "addpraise *praise", :action => :handle_addpraise
190 plugin.map "rmlart *lart", :action => :handle_rmlart
191 plugin.map "rmpraise *praise", :action => :handle_rmpraise
193 plugin.map "listlart *lart", :action => :handle_listlart
194 plugin.map "listpraise *praise", :action => :handle_listpraise