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
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')
83 @registry[:larts] = @larts
84 @registry[:praises] = @praises
88 def help(plugin, topic="")
89 "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 def handle_lart(m, params)
93 lart = larts[get_msg_idx(larts.length)]
95 m.reply "I dunno any larts"
98 who = params[:who].to_s
105 reason = "for trying to make me lart myself"
107 lart = replace_who lart, who
108 lart << " #{reason}" unless reason.empty?
113 def handle_praise(m, params)
114 praise = praises[get_msg_idx(praises.length)]
116 m.reply "I dunno any praises"
119 who = params[:who].to_s
120 reason = params[:why]
121 if who == m.sourcenick || who == "me"
122 params[:who] = m.sourcenick
123 params[:why] = "for praising himself"
124 handle_lart(m, params)
127 praise = replace_who praise, who
128 praise << " #{reason}" unless reason.empty?
133 def handle_addlart(m, params)
134 @larts[@lang] << params[:lart].to_s
139 def handle_rmlart(m, params)
140 @larts[@lang].delete params[:lart].to_s
145 def handle_listlart(m, params)
146 rx = Regexp.new(params[:lart].to_s, true)
147 list = larts.grep(rx)
149 m.reply list.join(" | "), :split_at => /\s+\|\s+/
151 m.reply "no lart found matching #{params[:lart]}"
155 def handle_addpraise(m, params)
156 @praises[@lang] << params[:praise].to_s
161 def handle_rmpraise(m, params)
162 @praises[@lang].delete params[:praise].to_s
167 def handle_listpraise(m, params)
168 rx = Regexp.new(params[:praise].to_s, true)
169 list = praises.grep(rx)
171 m.reply list.join(" | "), :split_at => /\s+\|\s+/
173 m.reply "no praise found matching #{params[:praise]}"
177 # The following are utils for larts/praises
178 def replace_who(msg, nick)
179 msg.gsub(/<who>/i, "#{nick}")
188 plugin = LartPlugin.new
190 plugin.map "lart *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_lart
191 plugin.map "praise *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_praise
193 plugin.map "addlart *lart", :action => :handle_addlart
194 plugin.map "addpraise *praise", :action => :handle_addpraise
196 plugin.map "rmlart *lart", :action => :handle_rmlart
197 plugin.map "rmpraise *praise", :action => :handle_rmpraise
199 plugin.map "listlart *lart", :action => :handle_listlart
200 plugin.map "listpraise *praise", :action => :handle_listpraise