]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/figlet.rb
9d843ce95a056ed7aea3623a1598849352224c98
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / figlet.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Figlet plugin
5
6 class FigletPlugin < Plugin
7   DEFAULT_FONTS = ['rectangles', 'smslant']
8   MAX_WIDTH=68
9
10   Config.register Config::StringValue.new('figlet.path',
11      :default => '/usr/bin/figlet',
12      :desc => _('Path to the figlet program'),
13      :on_change => Proc.new { |bot, v| bot.plugins['figlet'].test_figlet })
14
15   def figlet_path
16     @bot.config['figlet.path']
17   end
18
19   attr_reader :has_figlet
20   attr_accessor :figlet_font
21
22   def test_figlet
23     #check that figlet is present
24     @has_figlet = File.exist?(figlet_path)
25
26     # check that figlet actually has the font installed
27     @figlet_font = nil
28     for fontname in DEFAULT_FONTS
29       # check if figlet can render this font properly
30       if system("#{figlet_path} -f #{fontname} test test test")
31         @figlet_font = fontname
32         break
33       end
34     end
35   end
36
37   def initialize
38     super
39
40
41     # test for figlet and font presence
42     test_figlet
43
44     # set the commandline params
45     @figlet_params = ['-k', '-w', MAX_WIDTH.to_s, '-C', 'utf8']
46
47     # add the font from DEFAULT_FONTS to the cmdline (if figlet has that font)
48     @figlet_params += ['-f', @figlet_font] if @figlet_font
49
50   end
51
52   def help(plugin, topic="")
53     "figlet <message> => print using figlet"
54   end
55
56   def figlet(m, params)
57     unless @has_figlet
58       m.reply "figlet couldn't be found. if it's installed, you should set the figlet.path config key to its path"
59       return
60     end
61
62     message = params[:message].to_s
63     if message =~ /^-/
64       m.reply "the message can't start with a - sign"
65       return
66     end
67
68     # collect the parameters to pass to safe_exec
69     exec_params = [figlet_path] + @figlet_params + [message]
70
71     # run figlet
72     m.reply Utils.safe_exec(*exec_params), :max_lines => 0
73   end
74
75 end
76
77 plugin = FigletPlugin.new
78 plugin.map "figlet *message"