X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mailshears.git;a=blobdiff_plain;f=lib%2Fcommon%2Fplugin.rb;h=702b621ff5da9c1ee18ccbd0fd29352950a63386;hp=77d80c469e7b1e94f794b84b0c532f632765f77e;hb=1ee3ee0afbca846fa83ae741bbc5a101cc4a4cb6;hpb=bd2dabf89ab277fbe315b05e6dfa839afb5ce5ef diff --git a/lib/common/plugin.rb b/lib/common/plugin.rb index 77d80c4..702b621 100644 --- a/lib/common/plugin.rb +++ b/lib/common/plugin.rb @@ -1,31 +1,90 @@ +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 - def Plugin.included(c) - # Callback, called whenever another class or module includes this - # one. The parameter given is the name of the class or module - # that included us. - @includers ||= [] - @includers << c + module Run + # Module methods, meant to be extended. Includers can explicitly + # extend this to get a run() method defined in terms of their own + # runner() and dummy_runner() methods. + + def included(c) + # Callback, called whenever another class or module includes this + # one. The parameter given is the name of the class or module + # that included us. + @includers ||= [] + @includers << c + end + + def includers + @includers ||= [] + return @includers + end + + def runner() + # The Runner associated with this plugin. + raise NotImplementedError + end + + def dummy_runner() + # The DummyRunner associated with this plugin. + raise NotImplementedError + end + + def run(cfg, *args) + includers().each do |includer| + plugin = includer.new(cfg) + + if cfg.i_mean_business then + runner = runner().new() + else + runner = dummy_runner().new() + end + + # The splat passes the correct (we hope) number of arguments to the + # appropriate runner. The Rm(Dummy)Runner have splats on their + # *target arguments as well, to turn ARGV back into an array. + runner.run(plugin, *args) + end + end end - def Plugin.includers - return @includers + 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) + if user_exists(target) then + return describe_user(target) + else + return 'User not found' + end + elsif target.is_a?(Domain) + if domain_exists(target) then + return describe_domain(target) + else + return 'Domain not found' + end + 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 - # administrator. - raise NotImplementedError + # Provide a "description" of the domain. This is output along with + # the domain name and can be anything of use to the system + # administrator. The default doesn't do anything useful and should + # be overridden. + return domain.to_s() end - def describe_account(account) - # Provide a "description" of the account. This is output along + def describe_user(user) + # Provide a "description" of the user. This is output along # with the domain name and can be anything of use to the system - # administrator. - raise NotImplementedError + # administrator. The default doesn't do anything useful and should + # be overridden. + return user.to_s() end def list_users() @@ -33,27 +92,44 @@ module Plugin raise NotImplementedError end - def user_exists(username) + def list_domains() + # Compute the domains from a list of users. Obviously much worse + # than getting the domains the "smart" way, if such a way exists. + users = list_users() + domains = users.map{ |u| u.domain() } + return domains.uniq() + end + + 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 domain_exists(domain) + # Does the given domain exist for this plugin? We use a naive + # implementation here based on list_domains() which is required to + # exist above. Plugins can override this with something fast. + domains = list_domains() + return domains.include?(domain) 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