]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/plugin.rb
Factor out plugin running into the Plugin module (along with the includers() handling).
[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 return @includers
20 end
21
22 def runner()
23 # The Runner associated with this plugin.
24 raise NotImplementedError
25 end
26
27 def dummy_runner()
28 # The RummyRunner associated with this plugin.
29 raise NotImplementedError
30 end
31
32 def run(cfg, *args)
33 includers().each do |includer|
34 plugin = includer.new(cfg)
35
36 if cfg.i_mean_business then
37 runner = runner().new()
38 else
39 runner = dummy_runner().new()
40 end
41
42 # The splat passes the correct (we hope) number of arguments to the
43 # appropriate runner. The Rm(Dummy)Runner have splats on their
44 # *target arguments as well, to turn ARGV back into an array.
45 runner.run(plugin, *args)
46 end
47 end
48 end
49
50 def describe_domain(domain)
51 # Provide a "description" of the domain. This is output along
52 # with the domain name and can be anything of use to the system
53 # administrator.
54 raise NotImplementedError
55 end
56
57 def describe_user(user)
58 # Provide a "description" of the user. This is output along
59 # with the domain name and can be anything of use to the system
60 # administrator.
61 raise NotImplementedError
62 end
63
64 def list_users()
65 # Return a list of all users managed by this plugin.
66 raise NotImplementedError
67 end
68
69 def user_exists(username)
70 # Does the given username exist for this plugin? We use a naive
71 # implementation here based on list_users() which is required to
72 # exist above. Plugins can override this with something fast.
73 users = list_users()
74 return users.include?(username)
75 end
76
77 def list_domains_users(domains)
78 # Get all usernames belonging to the given domains. If a username
79 # ends in @example.com, it belongs to the domain example.com
80 #
81 # This uses a naive loop, but relies only on the existence of a
82 # list_users() method which is guaranteed above. More efficient
83 # implementations can usually be made within the plugin.
84 domains_users = []
85
86 usernames = list_users();
87 domains.each do |d|
88 matches = usernames.select do |username|
89 username =~ /@#{d}$/
90 end
91
92 domains_users += matches
93 end
94
95 return domains_users
96 end
97
98 end