]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/filters_ui.rb
31e35452d3d49929d116cd278d574ed0d12ae87d
[user/henk/code/ruby/rbot.git] / lib / rbot / core / filters_ui.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: filters management from IRC
5 #
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7
8 class FiltersModule < CoreBotModule
9
10   def initialize
11     super
12     @bot.clear_filters
13     @bot.register_filter(:htmlinfo) { |s| Utils.get_html_info(s.to_s, s) }
14   end
15
16   def help(plugin, topic="")
17     "filters list [<group>] => list filters (in group <group>) | filters search <pat> => list filters matching regexp <pat>"
18   end
19
20   def do_list(m, params)
21     g = params[:group]
22     ar = @bot.filter_names(g).map { |s| s.to_s }.sort!
23     if ar.empty?
24       if g
25         msg = _("no filters in group %{g}") % {:g => g}
26       else
27         msg = _("no known filters")
28       end
29     else
30       msg = _("known filters: ") << ar.join(", ")
31     end
32     m.reply msg
33   end
34
35   def do_listgroups(m, params)
36     ar = @bot.filter_groups.map { |s| s.to_s }.sort!
37     if ar.empty?
38       msg = _("no known filter groups")
39     else
40       msg = _("known filter groups: ") << ar.join(", ")
41     end
42     m.reply msg
43   end
44
45   def do_search(m, params)
46     l = @bot.filter_names.map { |s| s.to_s }
47     pat = params[:pat].to_s
48     sl = l.grep(Regexp.new(pat))
49     if sl.empty?
50       msg = _("no filters match %{pat}") % { :pat => pat }
51     else
52       msg = _("filters matching %{pat}: ") % { :pat => pat }
53       msg << sl.sort!.join(", ")
54     end
55     m.reply msg
56   end
57
58 end
59
60 plugin = FiltersModule.new
61
62 plugin.map "filters list [:group]", :action => :do_list
63 plugin.map "filters search *pat", :action => :do_search
64 plugin.map "filter groups", :action => :do_listgroups