[plugin] ri fixed, simple no longer available :(
[rbot] / data / rbot / plugins / ri.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: 'ri' -- ruby documentation plugin
5 #
6 # Author:: Eric Hodel <drbrain@segment7.net> (aka drbrain)
7 # Author:: Michael Brailsford  <brailsmt@yahoo.com> aka brailsmt
8 # Author:: dmitry kim <dmitry dot kim at gmail dot com>
9 # Copyright:: (C) 2007, dmitry kim
10 # Copyright:: (C) Eric Hodel
11 # Copyright:: (C) Michael Brailsford
12 # License:: MIT
13 #
14
15 class RiPlugin < Plugin
16
17   RI_COMMAND = %w{ri -f ansi -T}
18
19   Config.register Config::IntegerValue.new('ri.max_length',
20     :default => 512,
21     :desc => "Maximum length of ri entry (in bytes) which is ok to be sent to channels or other users")
22
23   def help(plugin, topic="")
24     "ri <something> => returns ruby documentation for <something>; ri [tell] <whom> [about] <something> => sends the documentation entry about <something> to <whom> using /msg"
25   end
26
27   def ri(m, params)
28     tgt = nil
29     if params[:who]
30       if m.private?
31         if params[:who] != m.sourcenick
32           m.reply '"ri tell <who>" syntax is only allowed in public channels'
33           return
34         end
35       elsif !(tgt = m.channel.users[params[:who]])
36         m.reply "sorry, i don't see user #{params[:who]} here on #{m.channel}"
37         return
38       end
39     end
40     args = RI_COMMAND.dup
41     if a = params[:something]
42       if a == '-c'
43         args.push(a)
44       else
45         args.push('--')
46         args.push(a)
47       end
48     end
49     begin
50       ret = Utils.safe_exec(*args)
51     rescue
52       return m.reply("failed to execute ri")
53     end
54     ret = ret.gsub(/\t/, "  ").split(/\n/).join(" ").gsub(/\s\s+/, '  ')
55
56     if ret.length > @bot.config['ri.max_length']
57       if !m.private? && tgt.to_s != m.sourcenick
58         return m.reply('entry is too long to send to the channel or to some other user, use /msg to ask me about it')
59       end
60     end
61     if tgt
62       @bot.say(tgt, ret)
63       m.okay
64     else
65       m.reply(ret)
66     end
67     return
68   end
69 end
70
71 plugin = RiPlugin.new
72 plugin.map 'ri :something', :requirements => {:something => /^((-c)|(\w\S+))$/}
73 plugin.map 'ri [tell] :who [about] :something',
74   :requirements => {:something => /^((-c)|(\w\S+))$/}