lart plugin: replace "me" with sourcenick
[rbot] / 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 = "#{@bot.botclass}/lart/larts"
45       @oldpraise = "#{@bot.botclass}/lart/praise"
46     end
47
48     @lartfile.replace "#{@bot.botclass}/lart/larts-#{lang}"
49     @praisefile.replace "#{@bot.botclass}/lart/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("#{@bot.botclass}/lart") if not FileTest.directory? "#{@bot.botclass}/lart"
76     # TODO implement safe saving here too
77     Utils.safe_save(@lartfile) { |file|
78       file.puts @larts
79     }
80     Utils.safe_save(@praisefile) { |file|
81       file.puts @praises
82     }
83     @changed = false
84   end
85
86   def help(plugin, topic="")
87     "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."
88   end
89
90   def handle_lart(m, params)
91     lart = @larts[get_msg_idx(@larts.length)]
92     if not lart
93       m.reply "I dunno any larts"
94       return
95     end
96     who = params[:who].to_s
97     reason = params[:why]
98     if who == "me"
99       who = m.sourcenick
100     end
101     if who == @bot.nick
102       who = m.sourcenick
103       reason = "for trying to make me lart myself"
104     end
105     lart = replace_who lart, who
106     lart << " #{reason}" unless reason.empty?
107
108     m.act lart
109   end
110
111   def handle_praise(m, params)
112     praise = @praises[get_msg_idx(@praises.length)]
113     if not praise
114       m.reply "I dunno any praises"
115       return
116     end
117     who = params[:who].to_s
118     reason = params[:why]
119     if who == m.sourcenick || who == "me"
120       params[:who] = m.sourcenick
121       params[:why] = "for praising himself"
122       handle_lart(m, params)
123       return
124     end
125     praise = replace_who praise, who
126     praise << " #{reason}" unless reason.empty?
127
128     m.act praise
129   end
130
131   def handle_addlart(m, params)
132     @larts << params[:lart].to_s
133     @changed = true
134     m.okay
135   end
136
137   def handle_rmlart(m, params)
138     @larts.delete params[:lart].to_s
139     @changed = true
140     m.okay
141   end
142
143   def handle_listlart(m, params)
144     rx = Regexp.new(params[:lart].to_s, true)
145     list = @larts.grep(rx)
146     unless list.empty?
147       m.reply list.join(" | "), :split_at => /\s+\|\s+/
148     else
149       m.reply "no lart found matching #{params[:lart]}"
150     end
151   end
152
153   def handle_addpraise(m, params)
154     @praises << params[:praise].to_s
155     @changed = true
156     m.okay
157   end
158
159   def handle_rmpraise(m, params)
160     @praises.delete params[:praise].to_s
161     @changed = true
162     m.okay
163   end
164
165   def handle_listpraise(m, params)
166     rx = Regexp.new(params[:praise].to_s, true)
167     list = @praises.grep(rx)
168     unless list.empty?
169       m.reply list.join(" | "), :split_at => /\s+\|\s+/
170     else
171       m.reply "no praise found matching #{params[:praise]}"
172     end
173   end
174
175   #  The following are utils for larts/praises
176   def replace_who(msg, nick)
177     msg.gsub(/<who>/i, "#{nick}")
178   end
179
180   def get_msg_idx(max)
181     idx = rand(max)
182   end
183
184 end
185
186 plugin = LartPlugin.new
187
188 plugin.map "lart *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_lart
189 plugin.map "praise *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_praise
190
191 plugin.map "addlart *lart", :action => :handle_addlart
192 plugin.map "addpraise *praise", :action => :handle_addpraise
193
194 plugin.map "rmlart *lart", :action => :handle_rmlart
195 plugin.map "rmpraise *praise", :action => :handle_rmpraise
196
197 plugin.map "listlart *lart", :action => :handle_listlart
198 plugin.map "listpraise *praise", :action => :handle_listpraise