b0b57505873665a856dcf7ade0eafc4b82cf266a
[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 describe_domain(domain)
18 # Provide a "description" of the domain. This is output along
19 # with the domain name and can be anything of use to the system
20 # administrator.
21 raise NotImplementedError
22 end
23
24 def describe_user(user)
25 # Provide a "description" of the user. This is output along
26 # with the domain name and can be anything of use to the system
27 # administrator.
28 raise NotImplementedError
29 end
30
31 def list_users()
32 # Return a list of all users managed by this plugin.
33 raise NotImplementedError
34 end
35
36 def user_exists(username)
37 # Does the given username exist for this plugin? We use a naive
38 # implementation here based on list_users() which is required to
39 # exist above. Plugins can override this with something fast.
40 users = list_users()
41 return users.include?(username)
42 end
43
44 def list_domains_users(domains)
45 # Get all usernames belonging to the given domains. If a username
46 # ends in @example.com, it belongs to the domain example.com
47 #
48 # This uses a naive loop, but relies only on the existence of a
49 # list_users() method which is guaranteed above. More efficient
50 # implementations can usually be made within the plugin.
51 domains_users = []
52
53 usernames = list_users();
54 domains.each do |d|
55 matches = usernames.select do |username|
56 username =~ /@#{d}$/
57 end
58
59 domains_users += matches
60 end
61
62 return domains_users
63 end
64
65 end