]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/figlet.rb
plugin(points): new message parser, see #34
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / figlet.rb
index 040dee25af183d0a56e0180544c7d29bc03607ba..700730f416b6621177c45cceda4d50167bdfecea 100644 (file)
 # :title: Figlet plugin
 
 class FigletPlugin < Plugin
-  DEFAULT_FONTS = ['rectangles', 'smslant']
   MAX_WIDTH=68
 
   Config.register Config::StringValue.new('figlet.path',
-     :default => '/usr/bin/figlet',
+     :default => 'figlet',
      :desc => _('Path to the figlet program'),
      :on_change => Proc.new { |bot, v| bot.plugins['figlet'].test_figlet })
 
+  Config.register Config::StringValue.new('figlet.font',
+     :default => 'smslant',
+     :desc => _('figlet font to use'),
+     :validate => Proc.new { |v| v !~ /\s|`/ },
+     :on_change => Proc.new { |bot, v| bot.plugins['figlet'].test_figlet })
+
+  Config.register Config::StringValue.new('toilet.path',
+     :default => 'toilet',
+     :desc => _('Path to the toilet program'),
+     :on_change => Proc.new { |bot, v| bot.plugins['figlet'].test_toilet })
+
+  Config.register Config::StringValue.new('toilet.font',
+     :default => 'smslant',
+     :desc => _('toilet font to use'),
+     :validate => Proc.new { |v| v !~ /\s|`/ },
+     :on_change => Proc.new { |bot, v| bot.plugins['figlet'].test_toilet })
+
+  Config.register Config::ArrayValue.new('toilet.filters',
+     :default => [],
+     :desc => _('toilet filters to use (e.g. gay, metal)'),
+     :validate => Proc.new { |v| v !~ /\s|`/ },
+     :on_change => Proc.new { |bot, v| bot.plugins['figlet'].test_toilet })
+
   def figlet_path
     @bot.config['figlet.path']
   end
 
-  attr_reader :has_figlet
-  attr_accessor :figlet_font
+  def toilet_path
+    @bot.config['toilet.path']
+  end
+
+  def figlet_font
+    @bot.config['figlet.font']
+  end
+
+  def toilet_font
+    @bot.config['toilet.font']
+  end
+
+  def toilet_filters
+    @bot.config['toilet.filters']
+  end
+
+  attr_reader :has, :params
 
   def test_figlet
     #check that figlet is present
-    @has_figlet = File.exist?(figlet_path)
+    @has[:figlet] = Utils.try_exec("#{figlet_path} -v")
 
     # check that figlet actually has the font installed
-    @figlet_font = nil
-    for fontname in DEFAULT_FONTS
-      # check if figlet can render this font properly
-      if system("#{figlet_path} -f #{fontname} test test test")
-        @figlet_font = fontname
-        break
-      end
-    end
+    @has[:figlet_font] = Utils.try_exec("#{figlet_path} -f #{figlet_font} test test test")
+
+    # set the commandline params
+    @params[:figlet] = ['-k', '-w', MAX_WIDTH.to_s, '-C', 'utf8']
+
+    # add the font from DEFAULT_FONTS to the cmdline (if figlet has that font)
+    @params[:figlet] += ['-f', figlet_font] if @has[:figlet_font]
+  end
+
+  def test_toilet
+    #check that toilet is present
+    @has[:toilet] = Utils.try_exec("#{toilet_path} -v")
+
+    # check that toilet actually has the font installed
+    @has[:toilet_font] = Utils.try_exec("#{toilet_path} -f #{toilet_font} test test test")
+
+    # set the commandline params
+    @params[:toilet] = ['-k', '-w', MAX_WIDTH.to_s, '-E', 'utf8', '--irc']
+
+    # add the font from DEFAULT_FONTS to the cmdline (if toilet has that font)
+    @params[:toilet] += ['-f', toilet_font] if @has[:toilet_font]
+
+    # add the filters, if any
+    toilet_filters.each { |f| @params[:toilet] += ['-F', f.dup] }
   end
 
   def initialize
     super
 
+    @has = {}
+    @params = {}
 
     # test for figlet and font presence
     test_figlet
-
-    # set the commandline params
-    @figlet_params = ['-k', '-w', MAX_WIDTH.to_s]
-
-    # add the font from DEFAULT_FONTS to the cmdline (if figlet has that font)
-    @figlet_params += ['-f', @figlet_font] if @figlet_font
-
+    # ditto for toilet
+    test_toilet
   end
 
   def help(plugin, topic="")
-    "figlet <message> => print using figlet"
+    "figlet|toilet <message> => print using figlet or toilet"
   end
 
   def figlet(m, params)
-    unless @has_figlet
-      m.reply "figlet couldn't be found. if it's installed, you should set the figlet.path config key to its path"
+    key = params[:plugin] || m.plugin.intern
+    unless @has[key]
+      m.reply("%{cmd} couldn't be found. if it's installed, you should set the %{cmd}.path config key to its path" % {
+        :cmd => key
+      })
       return
     end
 
@@ -66,13 +119,21 @@ class FigletPlugin < Plugin
     end
 
     # collect the parameters to pass to safe_exec
-    exec_params = [figlet_path] + @figlet_params + [message]
+    exec_params = [send(:"#{key}_path")] + @params[key] + [message]
+
+    # run the program
+    m.reply strip_first_last_empty_line(Utils.safe_exec(*exec_params)), :max_lines => 0, :nick => false
+  end
+  alias :toilet :figlet
+
+  private
 
-    # run figlet
-    m.reply Utils.safe_exec(*exec_params)
+  def strip_first_last_empty_line(txt)
+    txt.gsub(/\A(?:^\s*\r?\n)+/m,'').rstrip
   end
 
 end
 
 plugin = FigletPlugin.new
 plugin.map "figlet *message"
+plugin.map "toilet *message"