]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/figlet.rb
figlet plugin: utf-8
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / figlet.rb
index f82288eb65356cb06dd9ec7cb57d073d713781e6..9d843ce95a056ed7aea3623a1598849352224c98 100644 (file)
@@ -1,24 +1,78 @@
-#DEFAULT_FONT="smslant"
-DEFAULT_FONT="rectangles"
-MAX_WIDTH=68
+#-- vim:sw=2:et
+#++
+#
+# :title: Figlet plugin
 
 class FigletPlugin < Plugin
+  DEFAULT_FONTS = ['rectangles', 'smslant']
+  MAX_WIDTH=68
+
+  Config.register Config::StringValue.new('figlet.path',
+     :default => '/usr/bin/figlet',
+     :desc => _('Path to the figlet program'),
+     :on_change => Proc.new { |bot, v| bot.plugins['figlet'].test_figlet })
+
+  def figlet_path
+    @bot.config['figlet.path']
+  end
+
+  attr_reader :has_figlet
+  attr_accessor :figlet_font
+
+  def test_figlet
+    #check that figlet is present
+    @has_figlet = File.exist?(figlet_path)
+
+    # 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
+  end
+
+  def initialize
+    super
+
+
+    # test for figlet and font presence
+    test_figlet
+
+    # set the commandline params
+    @figlet_params = ['-k', '-w', MAX_WIDTH.to_s, '-C', 'utf8']
+
+    # add the font from DEFAULT_FONTS to the cmdline (if figlet has that font)
+    @figlet_params += ['-f', @figlet_font] if @figlet_font
+
+  end
+
   def help(plugin, topic="")
-    "figlet [<message>] => print using figlet"
+    "figlet <message> => print using figlet"
   end
-  def privmsg(m)
-         case m.params
-         when nil
-                 m.reply "incorrect usage: " + help(m.plugin)
-                 return
-         when (/^-/)
-                 m.reply "incorrect usage: " + help(m.plugin)
-                 return
-         else
-                 m.reply Utils.safe_exec("/usr/bin/figlet", "-k", "-w", "#{MAX_WIDTH}", "-f", DEFAULT_FONT, m.params)
-                 return
-         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"
+      return
+    end
+
+    message = params[:message].to_s
+    if message =~ /^-/
+      m.reply "the message can't start with a - sign"
+      return
+    end
+
+    # collect the parameters to pass to safe_exec
+    exec_params = [figlet_path] + @figlet_params + [message]
+
+    # run figlet
+    m.reply Utils.safe_exec(*exec_params), :max_lines => 0
   end
+
 end
+
 plugin = FigletPlugin.new
-plugin.register("figlet")
+plugin.map "figlet *message"