4 # :title: Figlet plugin
6 class FigletPlugin < Plugin
9 Config.register Config::StringValue.new('figlet.path',
11 :desc => _('Path to the figlet program'),
12 :on_change => Proc.new { |bot, v| bot.plugins['figlet'].test_figlet })
14 Config.register Config::StringValue.new('figlet.font',
15 :default => 'smslant',
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 })
20 Config.register Config::StringValue.new('toilet.path',
22 :desc => _('Path to the toilet program'),
23 :on_change => Proc.new { |bot, v| bot.plugins['figlet'].test_toilet })
25 Config.register Config::StringValue.new('toilet.font',
26 :default => 'smslant',
27 :desc => _('toilet font to use'),
28 :validate => Proc.new { |v| v !~ /\s|`/ },
29 :on_change => Proc.new { |bot, v| bot.plugins['figlet'].test_toilet })
31 Config.register Config::ArrayValue.new('toilet.filters',
33 :desc => _('toilet filters to use (e.g. gay, metal)'),
34 :validate => Proc.new { |v| v !~ /\s|`/ },
35 :on_change => Proc.new { |bot, v| bot.plugins['figlet'].test_toilet })
38 @bot.config['figlet.path']
42 @bot.config['toilet.path']
46 @bot.config['figlet.font']
50 @bot.config['toilet.font']
54 @bot.config['toilet.filters']
57 attr_reader :has, :params
60 #check that figlet is present
61 @has[:figlet] = Utils.try_exec("#{figlet_path} -v")
63 # check that figlet actually has the font installed
64 @has[:figlet_font] = Utils.try_exec("#{figlet_path} -f #{figlet_font} test test test")
66 # set the commandline params
67 @params[:figlet] = ['-k', '-w', MAX_WIDTH.to_s, '-C', 'utf8']
69 # add the font from DEFAULT_FONTS to the cmdline (if figlet has that font)
70 @params[:figlet] += ['-f', figlet_font] if @has[:figlet_font]
74 #check that toilet is present
75 @has[:toilet] = Utils.try_exec("#{toilet_path} -v")
77 # check that toilet actually has the font installed
78 @has[:toilet_font] = Utils.try_exec("#{toilet_path} -f #{toilet_font} test test test")
80 # set the commandline params
81 @params[:toilet] = ['-k', '-w', MAX_WIDTH.to_s, '-E', 'utf8', '--irc']
83 # add the font from DEFAULT_FONTS to the cmdline (if toilet has that font)
84 @params[:toilet] += ['-f', toilet_font] if @has[:toilet_font]
86 # add the filters, if any
87 toilet_filters.each { |f| @params[:toilet] += ['-F', f.dup] }
96 # test for figlet and font presence
102 def help(plugin, topic="")
103 "figlet|toilet <message> => print using figlet or toilet"
106 def figlet(m, params)
107 key = params[:plugin] || m.plugin.intern
109 m.reply("%{cmd} couldn't be found. if it's installed, you should set the %{cmd}.path config key to its path" % {
115 message = params[:message].to_s
117 m.reply "the message can't start with a - sign"
121 # collect the parameters to pass to safe_exec
122 exec_params = [send(:"#{key}_path")] + @params[key] + [message]
125 m.reply strip_first_last_empty_line(Utils.safe_exec(*exec_params)), :max_lines => 0
127 alias :toilet :figlet
131 def strip_first_last_empty_line(txt)
132 txt.gsub(/\A(?:^\s*\r?\n)+/m,'').rstrip
137 plugin = FigletPlugin.new
138 plugin.map "figlet *message"
139 plugin.map "toilet *message"