]> gitweb.michael.orlitzky.com - mailshears.git/blobdiff - lib/common/postfixadmin_plugin.rb
Stop pretending that we'll ever work with another DBMS.
[mailshears.git] / lib / common / postfixadmin_plugin.rb
index 7a1985202cc343af0301518dc9a9e94abf03c799..3a5494bced822593e7a63f7a65c4422a02f3fb75 100644 (file)
@@ -1,4 +1,6 @@
+require 'common/domain'
 require 'common/plugin'
+require 'common/user'
 require 'pg'
 
 module PostfixadminPlugin
@@ -17,101 +19,78 @@ module PostfixadminPlugin
   end
 
 
-  def describe_account(account)
-    # There's no other unique identifier in PostfixAdmin
-    return account
-  end
-
-
-  def describe_domain(domain)
-    # There's no other unique identifier in PostfixAdmin
-    return domain
-  end
-
-
   def list_domains()
     domains = []
 
-    # Just assume PostgreSQL for now.
-    begin
-      connection = PGconn.connect(@db_host,
-                                  @db_port,
-                                  @db_opts,
-                                  @db_tty,
-                                  @db_name,
-                                  @db_user,
-                                  @db_pass)
-
-      # 'ALL' is a magic domain, and we don't want it.
-      sql_query = "SELECT domain FROM domain WHERE domain <> 'ALL';"
-      connection.query(sql_query) do |result|
-        domains = result.field_values('domain')
-      end
-      connection.close()
-    rescue PGError => e
-      # But pretend like we're database-agnostic in case we ever are.
-      raise DatabaseError.new(e)
+    connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+                                @db_name, @db_user, @db_pass)
+
+    # 'ALL' is a magic domain, and we don't want it.
+    sql_query = "SELECT domain FROM domain WHERE domain <> 'ALL';"
+    connection.query(sql_query) do |result|
+      domains = result.field_values('domain')
     end
 
-    return domains
+    connection.close()
+
+    return domains.map{ |d| Domain.new(d) }
   end
 
 
+
   def list_users()
-    accounts = []
-
-    # Just assume PostgreSQL for now.
-    begin
-      connection = PGconn.connect(@db_host,
-                                  @db_port,
-                                  @db_opts,
-                                  @db_tty,
-                                  @db_name,
-                                  @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|
-        accounts = result.field_values('username')
-      end
-      connection.close()
-    rescue PGError => e
-      # But pretend like we're database-agnostic in case we ever are.
-      raise DatabaseError.new(e)
+    users = []
+
+    connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+                                @db_name, @db_user, @db_pass)
+
+    sql_query = 'SELECT username FROM mailbox;'
+    connection.query(sql_query) do |result|
+      users = result.field_values('username')
     end
 
-    return accounts
+    connection.close()
+
+    return users.map{ |u| User.new(u) }
   end
 
 
   def list_domains_users(domains)
     usernames = []
 
-    # 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 username FROM mailbox WHERE domain IN $1;'
-
-      connection.query(sql_query, [domains]) do |result|
-        usernames = result.field_values('username')
-      end
-
-      connection.close()
-    rescue PGError => e
-      # Pretend like we're database-agnostic in case we ever are.
-      raise DatabaseError.new(e)
+    connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+                                @db_name, @db_user, @db_pass)
+
+    sql_query  = 'SELECT username FROM mailbox WHERE domain IN $1;'
+
+    connection.query(sql_query, domains.map{|d| d.to_s()}) do |result|
+      usernames = result.field_values('username')
     end
 
-    return usernames
+    connection.close()
+
+    return usernames.map{ |u| User.new(u) }
+  end
+
+
+  def list_aliases()
+    #
+    # Get a list of all aliases, useful for testing.
+    #
+    aliases = []
+
+    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()
+
+    return aliases
   end
 
 end