]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/lart.rb
initial import of rbot
[user/henk/code/ruby/rbot.git] / rbot / plugins / lart.rb
1 #  Author:     Michael Brailsford  <brailsmt@yahoo.com>
2 #              aka brailsmt
3 #  Purpose:        Provide for humorous larts and praises
4 #  Copyright:  2002 Michael Brailsford.  All rights reserved.
5 #  License:    This plugin is licensed under the BSD license.  The terms of
6 #              which follow.
7 #
8 #  Redistribution and use in source and binary forms, with or without
9 #  modification, are permitted provided that the following conditions
10 #  are met:
11 #
12 #  1. Redistributions of source code must retain the above copyright notice,
13 #     this list of conditions and the following disclaimer.
14 #
15 #  2. Redistributions in binary form must reproduce the above copyright
16 #     notice, this list of conditions and the following disclaimer in the
17 #     documentation and/or other materials provided with the distribution.
18
19 class LartPlugin < Plugin
20
21         # Keep a 1:1 relation between commands and handlers
22         @@handlers = {
23                 "lart" => "handle_lart",
24                 "praise" => "handle_praise",
25                 "addlart" => "handle_addlart",
26                 "rmlart" => "handle_rmlart",
27                 "addpraise" => "handle_addpraise",
28                 "rmpraise" => "handle_rmpraise"
29         }
30
31         #{{{
32         def initialize
33                 super
34                 @larts = Array.new
35                 @praises = Array.new
36                 #read in the lart and praise files
37                 if File.exists? "#{@bot.botclass}/lart/larts"
38                         IO.foreach("#{@bot.botclass}/lart/larts") { |line|
39                                 @larts << line.chomp
40                         }
41                 end
42                 if File.exists? "#{@bot.botclass}/lart/praises"
43                         IO.foreach("#{@bot.botclass}/lart/praises") { |line|
44                                 @praises << line.chomp
45                         }
46                 end
47         end
48         #}}}
49         #{{{
50         def cleanup
51         end
52         #}}}
53         #{{{
54         def save
55                 Dir.mkdir("#{@bot.botclass}/lart") if not FileTest.directory? "#{@bot.botclass}/lart"
56                 File.open("#{@bot.botclass}/lart/larts", "w") { |file|
57                         file.puts @larts
58                 }
59                 File.open("#{@bot.botclass}/lart/praises", "w") { |file|
60                         file.puts @praises
61                 }
62         end
63         #}}}
64         #{{{
65         def privmsg(m)
66                 if not m.params
67                         m.reply "What a crazy fool!  Did you mean |help stats?"
68                         return
69                 end
70
71                 meth = self.method(@@handlers[m.plugin])
72                 meth.call(m) if(@bot.auth.allow?(m.plugin, m.source, m.replyto))
73         end
74         #}}}
75         #{{{
76         def help(plugin, topic="")
77                 "Lart:  The lart plugin allows you to punish/praise someone in the channel.  You can also add new punishments and new praises as well as delete them.  For the curious, LART is an acronym for Luser Attitude Readjustment Tool.\nUsage:  punish/lart <nick> <reason>  --  punishes <nick> for <reason>.  The reason is optional.\n        praise <nick> <reason>  --  praises <nick> for <reason>.  The reason is optional.\n        mod[lart|punish|praise] [add|remove]  --  Add or remove a lart or praise."
78         end
79         #}}}
80         # The following are command handlers    {{{
81         #{{{
82         def handle_lart(m)
83                 for_idx = m.params =~ /\s+\bfor\b/
84                 if for_idx
85                         nick = m.params[0, for_idx]
86                 else
87                         nick = m.params
88                 end
89                 lart = @larts[get_msg_idx(@larts.length)]
90                 if lart == nil
91                         m.reply "I dunno any larts"
92                         return
93                 end
94                 if nick == @bot.nick
95                         lart = replace_who lart, m.sourcenick
96                         lart << " for trying to make me lart myself"
97                 else
98                         lart = replace_who lart, nick
99                         lart << m.params[for_idx, m.params.length] if for_idx
100                 end
101
102                 @bot.action m.replyto, lart
103         end
104         #}}}
105         #{{{
106         def handle_praise(m)
107                 for_idx = m.params =~ /\s+\bfor\b/
108                 if for_idx
109                         nick = m.params[0, for_idx]
110                 else
111                         nick = m.params
112                 end
113                 praise = @praises[get_msg_idx(@praises.length)]
114                 if not praise
115                         m.reply "I dunno any praises"
116                         return
117                 end
118
119                 if nick == m.sourcenick
120                         praise = @larts[get_msg_idx(@larts.length)]
121                         praise = replace_who praise, nick
122                 else
123                         praise = replace_who praise, nick
124                         praise << m.params.gsub(/#{nick}/, "")
125                 end
126
127                 @bot.action m.replyto, praise
128         end
129         #}}}
130         #{{{
131         def handle_addlart(m)
132                 @larts << m.params
133                 @bot.okay m.replyto
134         end
135         #}}}
136         #{{{
137         def handle_rmlart(m)
138                 @larts.delete m.params
139                 @bot.okay m.replyto
140         end
141         #}}}
142         #{{{
143         def handle_addpraise(m)
144                 @praises << m.params
145                 @bot.okay m.replyto
146         end
147         #}}}
148         #{{{
149         def handle_rmpraise(m)
150                 @praises.delete m.params
151                 @bot.okay m.replyto
152         end
153         #}}}
154         #}}}
155
156         #  The following are utils for larts/praises    {{{
157         #{{{
158         def replace_who(msg, nick)
159                 msg.gsub(/<who>/i, "#{nick}")
160         end
161         #}}}
162         #{{{
163         def get_msg_idx(max)
164                 idx = rand(max)
165         end
166         #}}}
167         #}}}
168 end
169 plugin = LartPlugin.new
170 plugin.register("lart")
171 plugin.register("praise")
172
173 plugin.register("addlart")
174 plugin.register("addpraise")
175
176 plugin.register("rmlart")
177 plugin.register("rmpraise")