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