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