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
35 # after intialization #set_language is called with the language set in the bot configuration
36 # this loads the larts/praises from the registry
39 def load_static_files(path, suffix)
41 Dir.glob("#{path}/#{suffix}-*").each { |filename|
42 language = filename[filename.rindex('-')+1..-1]
43 entries[language] = File.readlines(filename).map(&:chomp)
48 def set_language(lang)
52 @larts = @registry[:larts]
53 @praises = @registry[:praises]
55 # for migrations try to read lart from bot data first (this is usually in ~/.rbot/lart:
56 if (not @larts or not @praises) and Dir.exists? datafile
57 log "migrate existing larts or praises from #{datafile}"
59 @larts = load_static_files(datafile, 'larts')
60 @praises = load_static_files(datafile, 'praises')
63 # without migrations, the initial data files we load from is located in the plugin directory
64 # that is the directory in which the plugin file itself is located (.../data/rbot/plugins/ usually)
65 if not @larts or not @praises
66 log "load initial larts and praises from #{plugin_path}"
68 initial_path = File.join(plugin_path, 'lart')
69 @larts = load_static_files(initial_path, 'larts')
70 @praises = load_static_files(initial_path, 'praises')
85 @registry[:larts] = @larts
86 @registry[:praises] = @praises
91 def help(plugin, topic="")
92 "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."
95 def handle_lart(m, params)
96 lart = larts[get_msg_idx(larts.length)]
98 m.reply "I dunno any larts"
101 who = params[:who].to_s
102 reason = params[:why]
108 reason = "for trying to make me lart myself"
110 lart = replace_who lart, who
111 lart << " #{reason}" unless reason.empty?
116 def handle_praise(m, params)
117 praise = praises[get_msg_idx(praises.length)]
119 m.reply "I dunno any praises"
122 who = params[:who].to_s
123 reason = params[:why]
124 if who == m.sourcenick || who == "me"
125 params[:who] = m.sourcenick
126 params[:why] = "for praising himself"
127 handle_lart(m, params)
130 praise = replace_who praise, who
131 praise << " #{reason}" unless reason.empty?
136 def handle_addlart(m, params)
137 @larts[@lang] << params[:lart].to_s
142 def handle_rmlart(m, params)
143 @larts[@lang].delete params[:lart].to_s
148 def handle_listlart(m, params)
149 rx = Regexp.new(params[:lart].to_s, true)
150 list = larts.grep(rx)
152 m.reply list.join(" | "), :split_at => /\s+\|\s+/
154 m.reply "no lart found matching #{params[:lart]}"
158 def handle_addpraise(m, params)
159 @praises[@lang] << params[:praise].to_s
164 def handle_rmpraise(m, params)
165 @praises[@lang].delete params[:praise].to_s
170 def handle_listpraise(m, params)
171 rx = Regexp.new(params[:praise].to_s, true)
172 list = praises.grep(rx)
174 m.reply list.join(" | "), :split_at => /\s+\|\s+/
176 m.reply "no praise found matching #{params[:praise]}"
180 # The following are utils for larts/praises
181 def replace_who(msg, nick)
182 msg.gsub(/<who>/i, "#{nick}")
191 plugin = LartPlugin.new
193 plugin.map "lart *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_lart
194 plugin.map "praise *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_praise
196 plugin.map "addlart *lart", :action => :handle_addlart
197 plugin.map "addpraise *praise", :action => :handle_addpraise
199 plugin.map "rmlart *lart", :action => :handle_rmlart
200 plugin.map "rmpraise *praise", :action => :handle_rmpraise
202 plugin.map "listlart *lart", :action => :handle_listlart
203 plugin.map "listpraise *praise", :action => :handle_listpraise