]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/figlet.rb
bans plugin: badword checks on plain message
[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 => '/usr/bin/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 => '/usr/bin/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   def test_figlet
58     #check that figlet is present
59     @has[:figlet] = File.exist?(figlet_path)
60
61     # check that figlet actually has the font installed
62     @has[:figlet_font] = !!system("#{figlet_path} -f #{figlet_font} test test test")
63
64     # set the commandline params
65     @params[:figlet] = ['-k', '-w', MAX_WIDTH.to_s, '-C', 'utf8']
66
67     # add the font from DEFAULT_FONTS to the cmdline (if figlet has that font)
68     @params[:figlet] += ['-f', figlet_font] if @has[:figlet_font]
69   end
70
71   def test_toilet
72     #check that toilet is present
73     @has[:toilet] = File.exist?(toilet_path)
74
75     # check that toilet actually has the font installed
76     @has[:toilet_font] = !!system("#{toilet_path} -f #{toilet_font} test test test")
77
78     # set the commandline params
79     @params[:toilet] = ['-k', '-w', MAX_WIDTH.to_s, '-E', 'utf8', '--irc']
80
81     # add the font from DEFAULT_FONTS to the cmdline (if toilet has that font)
82     @params[:toilet] += ['-f', toilet_font] if @has[:toilet_font]
83
84     # add the filters, if any
85     toilet_filters.each { |f| @params[:toilet] += ['-F', f.dup] }
86   end
87
88   def initialize
89     super
90
91     @has = {}
92     @params = {}
93
94     # test for figlet and font presence
95     test_figlet
96     # ditto for toilet
97     test_toilet
98   end
99
100   def help(plugin, topic="")
101     "figlet|toilet <message> => print using figlet or toilet"
102   end
103
104   def figlet(m, params)
105     key = m.plugin.intern
106     unless @has[key]
107       m.reply("%{cmd} couldn't be found. if it's installed, you should set the %{cmd}.path config key to its path" % {
108         :cmd => key
109       })
110       return
111     end
112
113     message = params[:message].to_s
114     if message =~ /^-/
115       m.reply "the message can't start with a - sign"
116       return
117     end
118
119     # collect the parameters to pass to safe_exec
120     exec_params = [send(:"#{m.plugin}_path")] + @params[key] + [message]
121
122     # run the program
123     m.reply Utils.safe_exec(*exec_params), :max_lines => 0
124   end
125   alias :toilet :figlet
126
127 end
128
129 plugin = FigletPlugin.new
130 plugin.map "figlet *message"
131 plugin.map "toilet *message"