]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/lart.rb
cb8e57b72444a0daa4f911697aecb726c3cf8d66
[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
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     @registry[:larts] = @larts
84     @registry[:praises] = @praises
85     @registry.flush
86   end
87
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."
90   end
91
92   def handle_lart(m, params)
93     lart = larts[get_msg_idx(larts.length)]
94     if not lart
95       m.reply "I dunno any larts"
96       return
97     end
98     who = params[:who].to_s
99     reason = params[:why]
100     if who == "me"
101       who = m.sourcenick
102     end
103     if who == @bot.nick
104       who = m.sourcenick
105       reason = "for trying to make me lart myself"
106     end
107     lart = replace_who lart, who
108     lart << " #{reason}" unless reason.empty?
109
110     m.act lart
111   end
112
113   def handle_praise(m, params)
114     praise = praises[get_msg_idx(praises.length)]
115     if not praise
116       m.reply "I dunno any praises"
117       return
118     end
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)
125       return
126     end
127     praise = replace_who praise, who
128     praise << " #{reason}" unless reason.empty?
129
130     m.act praise
131   end
132
133   def handle_addlart(m, params)
134     @larts[@lang] << params[:lart].to_s
135     @changed = true
136     m.okay
137   end
138
139   def handle_rmlart(m, params)
140     @larts[@lang].delete params[:lart].to_s
141     @changed = true
142     m.okay
143   end
144
145   def handle_listlart(m, params)
146     rx = Regexp.new(params[:lart].to_s, true)
147     list = larts.grep(rx)
148     unless list.empty?
149       m.reply list.join(" | "), :split_at => /\s+\|\s+/
150     else
151       m.reply "no lart found matching #{params[:lart]}"
152     end
153   end
154
155   def handle_addpraise(m, params)
156     @praises[@lang] << params[:praise].to_s
157     @changed = true
158     m.okay
159   end
160
161   def handle_rmpraise(m, params)
162     @praises[@lang].delete params[:praise].to_s
163     @changed = true
164     m.okay
165   end
166
167   def handle_listpraise(m, params)
168     rx = Regexp.new(params[:praise].to_s, true)
169     list = praises.grep(rx)
170     unless list.empty?
171       m.reply list.join(" | "), :split_at => /\s+\|\s+/
172     else
173       m.reply "no praise found matching #{params[:praise]}"
174     end
175   end
176
177   #  The following are utils for larts/praises
178   def replace_who(msg, nick)
179     msg.gsub(/<who>/i, "#{nick}")
180   end
181
182   def get_msg_idx(max)
183     rand(max)
184   end
185
186 end
187
188 plugin = LartPlugin.new
189
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
192
193 plugin.map "addlart *lart", :action => :handle_addlart
194 plugin.map "addpraise *praise", :action => :handle_addpraise
195
196 plugin.map "rmlart *lart", :action => :handle_rmlart
197 plugin.map "rmpraise *praise", :action => :handle_rmpraise
198
199 plugin.map "listlart *lart", :action => :handle_listlart
200 plugin.map "listpraise *praise", :action => :handle_listpraise