]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/lart.rb
lart pluing: use plugin.map instead of plugin.register
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / lart.rb
1 #  Original Author:
2 #               Michael Brailsford  <brailsmt@yahoo.com>
3 #               aka brailsmt
4 #  Author:      Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
5 #  Purpose:     Provide for humorous larts and praises
6 #  Original Copyright:
7 #               2002 Michael Brailsford.  All rights reserved.
8 #  Copyright:   2006 Giuseppe Bilotta.  All rights reserved.
9 #  License:     This plugin is licensed under the BSD license.  The terms of
10 #               which follow.
11 #
12 #  Redistribution and use in source and binary forms, with or without
13 #  modification, are permitted provided that the following conditions
14 #  are met:
15 #
16 #  1. Redistributions of source code must retain the above copyright notice,
17 #     this list of conditions and the following disclaimer.
18 #
19 #  2. Redistributions in binary form must reproduce the above copyright
20 #     notice, this list of conditions and the following disclaimer in the
21 #     documentation and/or other materials provided with the distribution.
22
23 class LartPlugin < Plugin
24
25   def initialize
26     @larts = Array.new
27     @praises = Array.new
28     @lartfile = ""
29     @praisefile = ""
30     @changed = false
31     super
32   end
33
34   def set_language(lang)
35     save
36
37     # We may be on an old installation, so on the first run read non-language-specific larts
38     unless defined?(@oldlart)
39       @oldlart = "#{@bot.botclass}/lart/larts"
40       @oldpraise = "#{@bot.botclass}/lart/praise"
41     end
42
43     @lartfile.replace "#{@bot.botclass}/lart/larts-#{lang}"
44     @praisefile.replace "#{@bot.botclass}/lart/praises-#{lang}"
45     @larts.clear
46     @praises.clear
47     if File.exists? @lartfile
48       IO.foreach(@lartfile) { |line|
49         @larts << line.chomp
50       }
51     elsif File.exists? @oldlart
52       IO.foreach(@oldlart) { |line|
53         @larts << line.chomp
54       }
55     end
56     if File.exists? @praisefile
57       IO.foreach(@praisefile) { |line|
58         @praises << line.chomp
59       }
60     elsif File.exists? @oldpraise
61       IO.foreach(@oldpraise) { |line|
62         @praises << line.chomp
63       }
64     end
65     @changed = false
66   end
67
68   def save
69     return unless @changed
70     Dir.mkdir("#{@bot.botclass}/lart") if not FileTest.directory? "#{@bot.botclass}/lart"
71     # TODO implement safe saving here too
72     Utils.safe_save(@lartfile) { |file|
73       file.puts @larts
74     }
75     Utils.safe_save(@praisefile) { |file|
76       file.puts @praises
77     }
78     @changed = false
79   end
80
81   def help(plugin, topic="")
82     "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."
83   end
84
85   def handle_lart(m, params)
86     lart = @larts[get_msg_idx(@larts.length)]
87     if not lart
88       m.reply "I dunno any larts"
89       return
90     end
91     who = params[:who].to_s
92     reason = params[:why]
93     if who == @bot.nick
94       who = m.sourcenick
95       reason = "for trying to make me lart myself"
96     end
97     lart = replace_who lart, who
98     lart << " #{reason}" unless reason.empty?
99
100     m.act lart
101   end
102
103   def handle_praise(m, params)
104     praise = @praises[get_msg_idx(@praises.length)]
105     if not praise
106       m.reply "I dunno any praises"
107       return
108     end
109     who = params[:who].to_s
110     reason = params[:why]
111     if who == m.sourcenick
112       params[:why] = "for praising himself"
113       handle_lart(m, params)
114       return
115     end
116     praise = replace_who praise, who
117     praise << " #{reason}" unless reason.empty?
118
119     m.act praise
120   end
121
122   def handle_addlart(m, params)
123     @larts << params[:lart]
124     @changed = true
125     m.okay
126   end
127
128   def handle_rmlart(m, params)
129     @larts.delete params[:lart]
130     @changed = true
131     m.okay
132   end
133
134   def handle_addpraise(m, params)
135     @praises << params[:praise]
136     @changed = true
137     m.okay
138   end
139
140   def handle_rmpraise(m, params)
141     @praises.delete params[:praise]
142     @changed = true
143     m.okay
144   end
145
146   #  The following are utils for larts/praises
147   def replace_who(msg, nick)
148     msg.gsub(/<who>/i, "#{nick}")
149   end
150
151   def get_msg_idx(max)
152     idx = rand(max)
153   end
154
155 end
156
157 plugin = LartPlugin.new
158
159 plugin.map "lart *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_lart
160 plugin.map "praise *who [*why]", :requirements => { :why => /(?:for|because)\s+.*/ }, :action => :handle_praise
161
162 plugin.map "addlart *lart", :action => :handle_addlart
163 plugin.map "addpraise *praise", :action => :handle_addpraise
164
165 plugin.map "rmlart *lart", :action => :handle_rmlart
166 plugin.map "rmpraise *praise", :action => :handle_rmpraise