Fix nil error when only postfixadmin is enabled.
[mailshears.git] / lib / common / plugin.rb
1 # All plugins should include this module. It defines the basic
2 # operations that all plugins are supposed to support.
3 module Plugin
4
5 module Run
6 # Module methods, meant to be extended. Includers can explicitly
7 # extend this to get a run() method defined in terms of their own
8 # runner() and dummy_runner() methods.
9
10 def included(c)
11 # Callback, called whenever another class or module includes this
12 # one. The parameter given is the name of the class or module
13 # that included us.
14 @includers ||= []
15 @includers << c
16 end
17
18 def includers
19 @includers ||= []
20 return @includers
21 end
22
23 def runner()
24 # The Runner associated with this plugin.
25 raise NotImplementedError
26 end
27
28 def dummy_runner()
29 # The RummyRunner associated with this plugin.
30 raise NotImplementedError
31 end
32
33 def run(cfg, *args)
34 includers().each do |includer|
35 plugin = includer.new(cfg)
36
37 if cfg.i_mean_business then
38 runner = runner().new()
39 else
40 runner = dummy_runner().new()
41 end
42
43 # The splat passes the correct (we hope) number of arguments to the
44 # appropriate runner. The Rm(Dummy)Runner have splats on their
45 # *target arguments as well, to turn ARGV back into an array.
46 runner.run(plugin, *args)
47 end
48 end
49 end
50
51 def describe_domain(domain)
52 # Provide a "description" of the domain. This is output along
53 # with the domain name and can be anything of use to the system
54 # administrator.
55 raise NotImplementedError
56 end
57
58 def describe_user(user)
59 # Provide a "description" of the user. This is output along
60 # with the domain name and can be anything of use to the system
61 # administrator.
62 raise NotImplementedError
63 end
64
65 def list_users()
66 # Return a list of all users managed by this plugin.
67 raise NotImplementedError
68 end
69
70 def user_exists(username)
71 # Does the given username exist for this plugin? We use a naive
72 # implementation here based on list_users() which is required to
73 # exist above. Plugins can override this with something fast.
74 users = list_users()
75 return users.include?(username)
76 end
77
78 def list_domains_users(domains)
79 # Get all usernames belonging to the given domains. If a username
80 # ends in @example.com, it belongs to the domain example.com
81 #
82 # This uses a naive loop, but relies only on the existence of a
83 # list_users() method which is guaranteed above. More efficient
84 # implementations can usually be made within the plugin.
85 domains_users = []
86
87 usernames = list_users();
88 domains.each do |d|
89 matches = usernames.select do |username|
90 username =~ /@#{d}$/
91 end
92
93 domains_users += matches
94 end
95
96 return domains_users
97 end
98
99 end