]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/lart.rb
17294055fecd56d3fc24b5e072141164cc8052d3
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / lart.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: lart/praise plugin for rbot
5 #
6 # Author::    Michael Brailsford  <brailsmt@yahoo.com> aka brailsmt
7 # Author::    Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
8 #
9 # Copyright:: (C) 2002 Michael Brailsford.  All rights reserved.
10 # Copyright:: (C) 2006 Giuseppe Bilotta.  All rights reserved.
11 #
12 # License::  This plugin is licensed under the BSD license.  The terms of
13 #            which follow.
14 #
15 # Redistribution and use in source and binary forms, with or without
16 # modification, are permitted provided that the following conditions
17 # are met:
18 #
19 # 1. Redistributions of source code must retain the above copyright notice,
20 #    this list of conditions and the following disclaimer.
21 #
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.
25 #
26 # Purpose::   Provide for humorous larts and praises
27
28 class LartPlugin < Plugin
29
30   def initialize
31     @larts = nil
32     @praises = nil
33
34     super
35     # after intialization #set_language is called with the language set in the bot configuration
36     # this loads the larts/praises from the registry
37   end
38
39   def load_static_files(path, suffix)
40     entries = {}
41     Dir.glob("#{path}/#{suffix}-*").each { |filename|
42       language = filename[filename.rindex('-')+1..-1]
43       entries[language] = File.readlines(filename).map(&:chomp)
44     }
45     entries
46   end
47
48   def set_language(lang)
49     save
50
51     @lang = lang
52     @larts = @registry[:larts]
53     @praises = @registry[:praises]
54
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}"
58
59       @larts = load_static_files(datafile, 'larts')
60       @praises = load_static_files(datafile, 'praises')
61     end
62
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}"
67
68       initial_path = File.join(plugin_path, 'lart')
69       @larts = load_static_files(initial_path, 'larts')
70       @praises = load_static_files(initial_path, 'praises')
71     end
72   end
73
74   def larts
75     @larts[@lang]
76   end
77
78   def praises
79     @praises[@lang]
80   end
81
82   def save
83     return unless @larts
84
85     @registry[:larts] = @larts
86     @registry[:praises] = @praises
87
88     @registry.flush
89   end
90
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."
93   end
94
95   def handle_lart(m, params)
96     lart = larts[get_msg_idx(larts.length)]
97     if not lart
98       m.reply "I dunno any larts"
99       return
100     end
101     who = params[:who].to_s
102     reason = params[:why]
103     if who == "me"
104       who = m.sourcenick
105     end
106     if who == @bot.nick
107       who = m.sourcenick
108       reason = "for trying to make me lart myself"
109     end
110     lart = replace_who lart, who
111     lart << " #{reason}" unless reason.empty?
112
113     m.act lart
114   end
115
116   def handle_praise(m, params)
117     praise = praises[get_msg_idx(praises.length)]
118     if not praise
119       m.reply "I dunno any praises"
120       return
121     end
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)
128       return
129     end
130     praise = replace_who praise, who
131     praise << " #{reason}" unless reason.empty?
132
133     m.act praise
134   end
135
136   def handle_addlart(m, params)
137     @larts[@lang] << params[:lart].to_s
138     @changed = true
139     m.okay
140   end
141
142   def handle_rmlart(m, params)
143     @larts[@lang].delete params[:lart].to_s
144     @changed = true
145     m.okay
146   end
147
148   def handle_listlart(m, params)
149     rx = Regexp.new(params[:lart].to_s, true)
150     list = larts.grep(rx)
151     unless list.empty?
152       m.reply list.join(" | "), :split_at => /\s+\|\s+/
153     else
154       m.reply "no lart found matching #{params[:lart]}"
155     end
156   end
157
158   def handle_addpraise(m, params)
159     @praises[@lang] << params[:praise].to_s
160     @changed = true
161     m.okay
162   end
163
164   def handle_rmpraise(m, params)
165     @praises[@lang].delete params[:praise].to_s
166     @changed = true
167     m.okay
168   end
169
170   def handle_listpraise(m, params)
171     rx = Regexp.new(params[:praise].to_s, true)
172     list = praises.grep(rx)
173     unless list.empty?
174       m.reply list.join(" | "), :split_at => /\s+\|\s+/
175     else
176       m.reply "no praise found matching #{params[:praise]}"
177     end
178   end
179
180   #  The following are utils for larts/praises
181   def replace_who(msg, nick)
182     msg.gsub(/<who>/i, "#{nick}")
183   end
184
185   def get_msg_idx(max)
186     rand(max)
187   end
188
189 end
190
191 plugin = LartPlugin.new
192
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
195
196 plugin.map "addlart *lart", :action => :handle_addlart
197 plugin.map "addpraise *praise", :action => :handle_addpraise
198
199 plugin.map "rmlart *lart", :action => :handle_rmlart
200 plugin.map "rmpraise *praise", :action => :handle_rmpraise
201
202 plugin.map "listlart *lart", :action => :handle_listlart
203 plugin.map "listpraise *praise", :action => :handle_listpraise