]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/plugin.rb
Rename plugin_class to plugin_module.
[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 def Plugin.included(c)
6 # Callback, called whenever another class or module includes this
7 # one. The parameter given is the name of the class or module
8 # that included us.
9 @includers ||= []
10 @includers << c
11 end
12
13 def Plugin.includers
14 return @includers
15 end
16
17 def runner()
18 # The Runner associated with this plugin.
19 raise NotImplementedError
20 end
21
22 def dummy_runner()
23 # The RummyRunner associated with this plugin.
24 raise NotImplementedError
25 end
26
27 def describe_domain(domain)
28 # Provide a "description" of the domain. This is output along
29 # with the domain name and can be anything of use to the system
30 # administrator.
31 raise NotImplementedError
32 end
33
34 def describe_user(user)
35 # Provide a "description" of the user. This is output along
36 # with the domain name and can be anything of use to the system
37 # administrator.
38 raise NotImplementedError
39 end
40
41 def list_users()
42 # Return a list of all users managed by this plugin.
43 raise NotImplementedError
44 end
45
46 def user_exists(username)
47 # Does the given username exist for this plugin? We use a naive
48 # implementation here based on list_users() which is required to
49 # exist above. Plugins can override this with something fast.
50 users = list_users()
51 return users.include?(username)
52 end
53
54 def list_domains_users(domains)
55 # Get all usernames belonging to the given domains. If a username
56 # ends in @example.com, it belongs to the domain example.com
57 #
58 # This uses a naive loop, but relies only on the existence of a
59 # list_users() method which is guaranteed above. More efficient
60 # implementations can usually be made within the plugin.
61 domains_users = []
62
63 usernames = list_users();
64 domains.each do |d|
65 matches = usernames.select do |username|
66 username =~ /@#{d}$/
67 end
68
69 domains_users += matches
70 end
71
72 return domains_users
73 end
74
75 end