]> gitweb.michael.orlitzky.com - mailshears.git/blobdiff - lib/common/plugin.rb
Overhaul everything to get consistent error reports.
[mailshears.git] / lib / common / plugin.rb
index 272f0df56749419e7f5dc14843232544ac5ee0e8..c6f750bc3f4d4c2f20e1c11c31f372e849afd711 100644 (file)
@@ -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
 # 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()
     end
 
     def dummy_runner()
-      # The RummyRunner associated with this plugin.
+      # The DummyRunner associated with this plugin.
       raise NotImplementedError
     end
 
       raise NotImplementedError
     end
 
@@ -48,6 +51,18 @@ module Plugin
     end
   end
 
     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
   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
 
     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()
     # 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)
   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 = []
 
     #
     # 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|
     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
       end
 
       domains_users += matches