]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/figlet.rb
plugin(points): new message parser, see #34
[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 => '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 => '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 })
19
20   Config.register Config::StringValue.new('toilet.path',
21      :default => 'toilet',
22      :desc => _('Path to the toilet program'),
23      :on_change => Proc.new { |bot, v| bot.plugins['figlet'].test_toilet })
24
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 })
30
31   Config.register Config::ArrayValue.new('toilet.filters',
32      :default => [],
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 })
36
37   def figlet_path
38     @bot.config['figlet.path']
39   end
40
41   def toilet_path
42     @bot.config['toilet.path']
43   end
44
45   def figlet_font
46     @bot.config['figlet.font']
47   end
48
49   def toilet_font
50     @bot.config['toilet.font']
51   end
52
53   def toilet_filters
54     @bot.config['toilet.filters']
55   end
56
57   attr_reader :has, :params
58
59   def test_figlet
60     #check that figlet is present
61     @has[:figlet] = Utils.try_exec("#{figlet_path} -v")
62
63     # check that figlet actually has the font installed
64     @has[:figlet_font] = Utils.try_exec("#{figlet_path} -f #{figlet_font} test test test")
65
66     # set the commandline params
67     @params[:figlet] = ['-k', '-w', MAX_WIDTH.to_s, '-C', 'utf8']
68
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]
71   end
72
73   def test_toilet
74     #check that toilet is present
75     @has[:toilet] = Utils.try_exec("#{toilet_path} -v")
76
77     # check that toilet actually has the font installed
78     @has[:toilet_font] = Utils.try_exec("#{toilet_path} -f #{toilet_font} test test test")
79
80     # set the commandline params
81     @params[:toilet] = ['-k', '-w', MAX_WIDTH.to_s, '-E', 'utf8', '--irc']
82
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]
85
86     # add the filters, if any
87     toilet_filters.each { |f| @params[:toilet] += ['-F', f.dup] }
88   end
89
90   def initialize
91     super
92
93     @has = {}
94     @params = {}
95
96     # test for figlet and font presence
97     test_figlet
98     # ditto for toilet
99     test_toilet
100   end
101
102   def help(plugin, topic="")
103     "figlet|toilet <message> => print using figlet or toilet"
104   end
105
106   def figlet(m, params)
107     key = params[:plugin] || m.plugin.intern
108     unless @has[key]
109       m.reply("%{cmd} couldn't be found. if it's installed, you should set the %{cmd}.path config key to its path" % {
110         :cmd => key
111       })
112       return
113     end
114
115     message = params[:message].to_s
116     if message =~ /^-/
117       m.reply "the message can't start with a - sign"
118       return
119     end
120
121     # collect the parameters to pass to safe_exec
122     exec_params = [send(:"#{key}_path")] + @params[key] + [message]
123
124     # run the program
125     m.reply strip_first_last_empty_line(Utils.safe_exec(*exec_params)), :max_lines => 0, :nick => false
126   end
127   alias :toilet :figlet
128
129   private
130
131   def strip_first_last_empty_line(txt)
132     txt.gsub(/\A(?:^\s*\r?\n)+/m,'').rstrip
133   end
134
135 end
136
137 plugin = FigletPlugin.new
138 plugin.map "figlet *message"
139 plugin.map "toilet *message"