]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/figlet.rb
spell plugin: command line option
[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   MAX_WIDTH=68
8
9   Config.register Config::StringValue.new('figlet.path',
10      :default => '/usr/bin/figlet',
11      :desc => _('Path to the figlet program'),
12      :on_change => Proc.new { |bot, v| bot.plugins['figlet'].test_figlet })
13
14   Config.register Config::StringValue.new('figlet.font',
15      :default => 'rectangles',
16      :desc => _('figlet font to use'),
17      :validate => Proc.new { |v| v !~ /\s|`/ },
18      :on_change => Proc.new { |bot, v| bot.plugins['figlet'].test_figlet })
19
20   def figlet_path
21     @bot.config['figlet.path']
22   end
23
24   def figlet_font
25     @bot.config['figlet.font']
26   end
27
28   attr_reader :has_figlet
29   attr_reader :has_font
30
31   def test_figlet
32     #check that figlet is present
33     @has_figlet = File.exist?(figlet_path)
34
35     # check that figlet actually has the font installed
36     @has_font = !!system("#{figlet_path} -f #{figlet_font} test test test")
37
38     # set the commandline params
39     @figlet_params = ['-k', '-w', MAX_WIDTH.to_s, '-C', 'utf8']
40
41     # add the font from DEFAULT_FONTS to the cmdline (if figlet has that font)
42     @figlet_params += ['-f', figlet_font] if @has_font
43   end
44
45   def initialize
46     super
47
48     # test for figlet and font presence
49     test_figlet
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"