]>
gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/plugin.rb
1 require 'common/domain'
4 # All plugins should include this module. It defines the basic
5 # operations that all plugins are supposed to support.
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.
14 # Callback, called whenever another class or module includes this
15 # one. The parameter given is the name of the class or module
27 # The Runner associated with this plugin.
28 raise NotImplementedError
32 # The DummyRunner associated with this plugin.
33 raise NotImplementedError
37 includers().each
do |includer
|
38 plugin
= includer
.new(cfg
)
40 if cfg
.i_mean_business
then
41 runner
= runner().new()
43 runner
= dummy_runner().new()
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(cfg
, plugin
, *args
)
55 # A generic version of describe_user/describe_domain that
56 # dispatches base on the class of the target.
58 if user_exists(target
) then
59 return describe_user(target
)
61 return 'User not found'
63 elsif target
.is_a
?(Domain
)
64 if domain_exists(target
) then
65 return describe_domain(target
)
67 return 'Domain not found'
70 raise NotImplementedError
74 def describe_domain(domain
)
75 # Provide a "description" of the domain. This is output along with
76 # the domain name and can be anything of use to the system
77 # administrator. The default doesn't do anything useful and should
82 def describe_user(user
)
83 # Provide a "description" of the user. This is output along
84 # with the domain name and can be anything of use to the system
85 # administrator. The default doesn't do anything useful and should
91 # Return a list of all users managed by this plugin.
92 raise NotImplementedError
96 # Compute the domains from a list of users. Obviously much worse
97 # than getting the domains the "smart" way, if such a way exists.
99 domains
= users
.map
{ |u
| u
.domain() }
100 return domains
.uniq()
103 def user_exists(user
)
104 # Does the given username exist for this plugin? We use a naive
105 # implementation here based on list_users() which is required to
106 # exist above. Plugins can override this with something fast.
108 return users
.include?(user
)
111 def domain_exists(domain
)
112 # Does the given domain exist for this plugin? We use a naive
113 # implementation here based on list_domains() which is required to
114 # exist above. Plugins can override this with something fast.
115 domains
= list_domains()
116 return domains
.include?(domain
)
119 def list_domains_users(domains
)
120 # Get all users belonging to the given domains. If a user has
121 # domainpart "example.com" then it belongs to the domain
124 # This uses a naive loop, but relies only on the existence of a
125 # list_users() method which is guaranteed above. More efficient
126 # implementations can usually be made within the plugin.
129 users
= list_users();
131 matches
= users
.select
do |user
|
132 user
.domainpart() == d
.to_s()
135 domains_users +
= matches