]> gitweb.michael.orlitzky.com - mailshears.git/blobdiff - lib/common/postfixadmin_plugin.rb
Overhaul everything to get consistent error reports.
[mailshears.git] / lib / common / postfixadmin_plugin.rb
index 5556d5a7c23a15866a0a7fb17e6cbb77f2a21f1d..60880d9e7bad17cf2298b4b0c9498d1007258039 100644 (file)
@@ -1,4 +1,6 @@
+require 'common/domain'
 require 'common/plugin'
+require 'common/user'
 require 'pg'
 
 module PostfixadminPlugin
@@ -19,13 +21,13 @@ module PostfixadminPlugin
 
   def describe_user(user)
     # There's no other unique identifier in PostfixAdmin
-    return user
+    return user.to_s()
   end
 
 
   def describe_domain(domain)
     # There's no other unique identifier in PostfixAdmin
-    return domain
+    return domain.to_s()
   end
 
 
@@ -53,7 +55,16 @@ module PostfixadminPlugin
       raise DatabaseError.new(e)
     end
 
-    return domains
+    return domains.map{ |d| Domain.new(d) }
+  end
+
+
+  def domain_exists(domain)
+    # Does the given domain exist in Postfixadmin? We use a naive
+    # implementation here based on list_domains(). This isn't in our
+    # superclass because not all plugins have a concept of domains.
+    domains = list_domains()
+    return domains.include?(domain)
   end
 
 
@@ -70,8 +81,6 @@ module PostfixadminPlugin
                                   @db_user,
                                   @db_pass)
 
-      # If address = goto, then the alias basically says, "really
-      # deliver to that address; it's not an alias."
       sql_query = 'SELECT username FROM mailbox;'
       connection.query(sql_query) do |result|
         users = result.field_values('username')
@@ -82,7 +91,7 @@ module PostfixadminPlugin
       raise DatabaseError.new(e)
     end
 
-    return users
+    return users.map{ |u| User.new(u) }
   end
 
 
@@ -101,7 +110,7 @@ module PostfixadminPlugin
 
       sql_query  = 'SELECT username FROM mailbox WHERE domain IN $1;'
 
-      connection.query(sql_query, [domains]) do |result|
+      connection.query(sql_query, domains.map{|d| d.to_s()}) do |result|
         usernames = result.field_values('username')
       end
 
@@ -111,7 +120,38 @@ module PostfixadminPlugin
       raise DatabaseError.new(e)
     end
 
-    return usernames
+    return usernames.map{ |u| User.new(u) }
+  end
+
+
+  def list_aliases()
+    #
+    # Get a list of all aliases, useful for testing.
+    #
+    aliases = []
+
+    # Just assume PostgreSQL for now.
+    begin
+      connection = PGconn.connect(@db_host,
+                                  @db_port,
+                                  @db_opts,
+                                  @db_tty,
+                                  @db_name,
+                                  @db_user,
+                                  @db_pass)
+
+      sql_query = 'SELECT address,goto FROM alias;'
+      results = connection.query(sql_query)
+      results.each do |row|
+        aliases << row # row should be a hash
+      end
+      connection.close()
+    rescue PGError => e
+      # But pretend like we're database-agnostic in case we ever are.
+      raise DatabaseError.new(e)
+    end
+
+    return aliases
   end
 
 end