X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mailshears.git;a=blobdiff_plain;f=lib%2Fcommon%2Fplugin.rb;h=c6f750bc3f4d4c2f20e1c11c31f372e849afd711;hp=272f0df56749419e7f5dc14843232544ac5ee0e8;hb=f819b178c5c1cb8adda0182c610e5c52fad8bea7;hpb=00237603494fde1d1f62e8a4e68326e00abb5fa1 diff --git a/lib/common/plugin.rb b/lib/common/plugin.rb index 272f0df..c6f750b 100644 --- a/lib/common/plugin.rb +++ b/lib/common/plugin.rb @@ -1,3 +1,6 @@ +require 'common/domain' +require 'common/user' + # All plugins should include this module. It defines the basic # operations that all plugins are supposed to support. module Plugin @@ -26,7 +29,7 @@ module Plugin end def dummy_runner() - # The RummyRunner associated with this plugin. + # The DummyRunner associated with this plugin. raise NotImplementedError end @@ -48,6 +51,18 @@ module Plugin end end + def describe(target) + # A generic version of describe_user/describe_domain that + # dispatches base on the class of the target. + if target.is_a?(User) + return describe_user(target) + elsif target.is_a?(Domain) + return describe_domain(target) + else + raise NotImplementedError + end + end + def describe_domain(domain) # Provide a "description" of the domain. This is output along # with the domain name and can be anything of use to the system @@ -67,27 +82,28 @@ module Plugin raise NotImplementedError end - def user_exists(username) + def user_exists(user) # Does the given username exist for this plugin? We use a naive # implementation here based on list_users() which is required to # exist above. Plugins can override this with something fast. users = list_users() - return users.include?(username) + return users.include?(user) end def list_domains_users(domains) - # Get all usernames belonging to the given domains. If a username - # ends in @example.com, it belongs to the domain example.com + # Get all users belonging to the given domains. If a user has + # domainpart "example.com" then it belongs to the domain + # "example.com". # # This uses a naive loop, but relies only on the existence of a # list_users() method which is guaranteed above. More efficient # implementations can usually be made within the plugin. domains_users = [] - usernames = list_users(); + users = list_users(); domains.each do |d| - matches = usernames.select do |username| - username =~ /@#{d}$/ + matches = users.select do |user| + user.domainpart() == d.to_s() end domains_users += matches