]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/lart.rb
Clean up logging from the plugins
[user/henk/code/ruby/rbot.git] / data / 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   def name
32     "lart"
33   end
34
35         #{{{
36         def initialize
37                 super
38                 @larts = Array.new
39                 @praises = Array.new
40                 #read in the lart and praise files
41                 if File.exists? "#{@bot.botclass}/lart/larts"
42                         IO.foreach("#{@bot.botclass}/lart/larts") { |line|
43                                 @larts << line.chomp
44                         }
45                 end
46                 if File.exists? "#{@bot.botclass}/lart/praises"
47                         IO.foreach("#{@bot.botclass}/lart/praises") { |line|
48                                 @praises << line.chomp
49                         }
50                 end
51         end
52         #}}}
53         #{{{
54         def cleanup
55         end
56         #}}}
57         #{{{
58         def save
59                 Dir.mkdir("#{@bot.botclass}/lart") if not FileTest.directory? "#{@bot.botclass}/lart"
60                 # TODO implement safe saving here too
61                 File.open("#{@bot.botclass}/lart/larts", "w") { |file|
62                         file.puts @larts
63                 }
64                 File.open("#{@bot.botclass}/lart/praises", "w") { |file|
65                         file.puts @praises
66                 }
67         end
68         #}}}
69         #{{{
70         def privmsg(m)
71                 if not m.params
72                         m.reply "What a crazy fool!  Did you mean |help stats?"
73                         return
74                 end
75
76                 meth = self.method(@@handlers[m.plugin])
77                 meth.call(m) if(@bot.auth.allow?(m.plugin, m.source, m.replyto))
78         end
79         #}}}
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         # The following are command handlers    {{{
86         #{{{
87         def handle_lart(m)
88                 for_idx = m.params =~ /\s+\bfor\b/
89                 if for_idx
90                         nick = m.params[0, for_idx]
91                 else
92                         nick = m.params
93                 end
94                 lart = @larts[get_msg_idx(@larts.length)]
95                 if lart == nil
96                         m.reply "I dunno any larts"
97                         return
98                 end
99                 if nick == @bot.nick
100                         lart = replace_who lart, m.sourcenick
101                         lart << " for trying to make me lart myself"
102                 else
103                         lart = replace_who lart, nick
104                         lart << m.params[for_idx, m.params.length] if for_idx
105                 end
106
107                 @bot.action m.replyto, lart
108         end
109         #}}}
110         #{{{
111         def handle_praise(m)
112                 for_idx = m.params =~ /\s+\bfor\b/
113                 if for_idx
114                         nick = m.params[0, for_idx]
115                 else
116                         nick = m.params
117                 end
118                 praise = @praises[get_msg_idx(@praises.length)]
119                 if not praise
120                         m.reply "I dunno any praises"
121                         return
122                 end
123
124                 if nick == m.sourcenick
125                         praise = @larts[get_msg_idx(@larts.length)]
126                         praise = replace_who praise, nick
127                 else
128                         praise = replace_who praise, nick
129                         praise << m.params.gsub(/#{nick}/, "")
130                 end
131
132                 @bot.action m.replyto, praise
133         end
134         #}}}
135         #{{{
136         def handle_addlart(m)
137                 @larts << m.params
138                 m.okay
139         end
140         #}}}
141         #{{{
142         def handle_rmlart(m)
143                 @larts.delete m.params
144                 m.okay
145         end
146         #}}}
147         #{{{
148         def handle_addpraise(m)
149                 @praises << m.params
150                 m.okay
151         end
152         #}}}
153         #{{{
154         def handle_rmpraise(m)
155                 @praises.delete m.params
156                 m.okay
157         end
158         #}}}
159         #}}}
160
161         #  The following are utils for larts/praises    {{{
162         #{{{
163         def replace_who(msg, nick)
164                 msg.gsub(/<who>/i, "#{nick}")
165         end
166         #}}}
167         #{{{
168         def get_msg_idx(max)
169                 idx = rand(max)
170         end
171         #}}}
172         #}}}
173 end
174 plugin = LartPlugin.new
175 plugin.register("lart")
176 plugin.register("praise")
177
178 plugin.register("addlart")
179 plugin.register("addpraise")
180
181 plugin.register("rmlart")
182 plugin.register("rmpraise")