]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - contrib/plugins/ri.rb
initial import of rbot
[user/henk/code/ruby/rbot.git] / contrib / plugins / ri.rb
1 #  Author:  Michael Brailsford  <brailsmt@yahoo.com>
2 #           aka  brailsmt
3 #  Purpose: To respond to requests for information from the ri command line
4 #  utility.
5
6 class RiPlugin < Plugin
7
8         @@handlers = {
9                 "ri" => "ri_handler",
10                 "msgri" => "msgri_handler"
11         }
12
13         #{{{
14         def initialize
15                 super
16                 @cache = Hash.new
17         end
18         #}}}
19         #{{{
20         def privmsg(m)
21                 if not m.params
22                         m.reply "uhmm... whatever"
23                         return
24                 end
25
26                 meth = self.method(@@handlers[m.plugin])
27                 meth.call(m)
28         end
29         #}}}
30         #{{{
31         def cleanup
32                 @cache = nil
33         end
34         #}}}
35         #{{{
36         def ri_handler(m)
37                 response = ""
38                 if @cache[m.params]
39                         response = @cache[m.params]
40                 else
41                         IO.popen("-") {|p|
42                                 if(p)
43                                         response = p.readlines.join "\n"
44                                         @cache[m.params] = response
45                                 else
46                                         $stderr = $stdout
47                                         exec("ri", m.params)
48                                 end
49                         }
50                         @cache[m.params] = response
51                 end
52
53                 @bot.say m.sourcenick, response
54                 m.reply "Finished \"ri #{m.params}\"" 
55         end
56         #}}}
57         #{{{
58         def msgri_handler(m)
59                 response = ""
60                 tell_nick, query = m.params.split()
61                 if @cache[query]
62                         response = @cache[query]
63                 else
64                         IO.popen("-") {|p|
65                                 if(p)
66                                         response = p.readlines.join "\n"
67                                         @cache[m.params] = response
68                                 else
69                                         $stderr = $stdout
70                                         exec("ri", query)
71                                 end
72                         }
73                         @cache[query] = response
74                 end
75
76                 @bot.say tell_nick, response
77                 m.reply "Finished telling #{tell_nick} about \"ri #{query}\"" 
78         end
79         #}}}
80 end
81 plugin = RiPlugin.new
82 plugin.register("ri")
83 plugin.register("msgri")