]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/lart.rb
multiple plugins: Changes to remove parenthesize warnings.
[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 = Array.new
32     @praises = Array.new
33     @lartfile = ""
34     @praisefile = ""
35     @changed = false
36     super
37   end
38
39   def set_language(lang)
40     save
41
42     # We may be on an old installation, so on the first run read non-language-specific larts
43     unless defined?(@oldlart)
44       @oldlart = datafile 'larts'
45       @oldpraise = datafile 'praise'
46     end
47
48     @lartfile.replace(datafile("larts-#{lang}"))
49     @praisefile.replace(datafile("praises-#{lang}"))
50     @larts.clear
51     @praises.clear
52     if File.exists? @lartfile
53       IO.foreach(@lartfile) { |line|
54         @larts << line.chomp
55       }
56     elsif File.exists? @oldlart
57       IO.foreach(@oldlart) { |line|
58         @larts << line.chomp
59       }
60     end
61     if File.exists? @praisefile
62       IO.foreach(@praisefile) { |line|
63         @praises << line.chomp
64       }
65     elsif File.exists? @oldpraise
66       IO.foreach(@oldpraise) { |line|
67         @praises << line.chomp
68       }
69     end
70     @changed = false
71   end
72
73   def save
74     return unless @changed
75     Dir.mkdir(datafile) unless FileTest.directory? datafile
76     Utils.safe_save(@lartfile) { |file|
77       file.puts @larts
78     }
79     Utils.safe_save(@praisefile) { |file|
80       file.puts @praises
81     }
82     @changed = false
83   end
84
85   def help(plugin, topic="")
86     "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."
87   end
88
89   def handle_lart(m, params)
90     lart = @larts[get_msg_idx(@larts.length)]
91     if not lart
92       m.reply "I dunno any larts"
93       return
94     end
95     who = params[:who].to_s
96     reason = params[:why]
97     if who == "me"
98       who = m.sourcenick
99     end
100     if who == @bot.nick
101       who = m.sourcenick
102       reason = "for trying to make me lart myself"
103     end
104     lart = replace_who lart, who
105     lart << " #{reason}" unless reason.empty?
106
107     m.act lart
108   end
109
110   def handle_praise(m, params)
111     praise = @praises[get_msg_idx(@praises.length)]
112     if not praise
113       m.reply "I dunno any praises"
114       return
115     end
116     who = params[:who].to_s
117     reason = params[:why]
118     if who == m.sourcenick || who == "me"
119       params[:who] = m.sourcenick
120       params[:why] = "for praising himself"
121       handle_lart(m, params)
122       return
123     end
124     praise = replace_who praise, who
125     praise << " #{reason}" unless reason.empty?
126
127     m.act praise
128   end
129
130   def handle_addlart(m, params)
131     @larts << params[:lart].to_s
132     @changed = true
133     m.okay
134   end
135
136   def handle_rmlart(m, params)
137     @larts.delete params[:lart].to_s
138     @changed = true
139     m.okay
140   end
141
142   def handle_listlart(m, params)
143     rx = Regexp.new(params[:lart].to_s, true)
144     list = @larts.grep(rx)
145     unless list.empty?
146       m.reply list.join(" | "), :split_at => /\s+\|\s+/
147     else
148       m.reply "no lart found matching #{params[:lart]}"
149     end
150   end
151
152   def handle_addpraise(m, params)
153     @praises << params[:praise].to_s
154     @changed = true
155     m.okay
156   end
157
158   def handle_rmpraise(m, params)
159     @praises.delete params[:praise].to_s
160     @changed = true
161     m.okay
162   end
163
164   def handle_listpraise(m, params)
165     rx = Regexp.new(params[:praise].to_s, true)
166     list = @praises.grep(rx)
167     unless list.empty?
168       m.reply list.join(" | "), :split_at => /\s+\|\s+/
169     else
170       m.reply "no praise found matching #{params[:praise]}"
171     end
172   end
173
174   #  The following are utils for larts/praises
175   def replace_who(msg, nick)
176     msg.gsub(/<who>/i, "#{nick}")
177   end
178
179   def get_msg_idx(max)
180     idx = rand(max)
181   end
182
183 end
184
185 plugin = LartPlugin.new
186
187 plugin.map "lart *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_lart
188 plugin.map "praise *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_praise
189
190 plugin.map "addlart *lart", :action => :handle_addlart
191 plugin.map "addpraise *praise", :action => :handle_addpraise
192
193 plugin.map "rmlart *lart", :action => :handle_rmlart
194 plugin.map "rmpraise *praise", :action => :handle_rmpraise
195
196 plugin.map "listlart *lart", :action => :handle_listlart
197 plugin.map "listpraise *praise", :action => :handle_listpraise