summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/figlet.rb
blob: b8561f4702ae4d26ced879a310c709aa966b98fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class FigletPlugin < Plugin
  DEFAULT_FONTS = ['rectangles', 'smslant']
  MAX_WIDTH=68

  def initialize
    super
    @figlet_path = "/usr/bin/figlet"

    # 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
    
    # 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

  end

  def help(plugin, topic="")
    "figlet <message> => print using figlet"
  end

  def figlet(m, params)
    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)
  end

end

plugin = FigletPlugin.new
plugin.map "figlet *message"