]> gitweb.michael.orlitzky.com - mailshears.git/blobdiff - lib/common/postfixadmin_db_plugin.rb
Way too many changes to mention. The 'rm' mode works now.
[mailshears.git] / lib / common / postfixadmin_db_plugin.rb
diff --git a/lib/common/postfixadmin_db_plugin.rb b/lib/common/postfixadmin_db_plugin.rb
new file mode 100644 (file)
index 0000000..936672e
--- /dev/null
@@ -0,0 +1,91 @@
+require 'common/plugin'
+require 'pg'
+
+module PostfixadminDbPlugin
+  # Code that all PostfixadminDb plugins (Prune, Rm, Mv...) will
+  # share.  That is, we implement the Plugin interface.
+  include Plugin
+
+  def initialize()
+
+    cfg = Configuration.new()
+    @db_host = cfg.dbhost
+    @db_port = cfg.dbport
+    @db_opts = cfg.dbopts
+    @db_tty  = cfg.dbtty
+    @db_name = cfg.dbname
+    @db_user = cfg.dbuser
+    @db_pass = cfg.dbpass
+  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 get_domains_from_db()
+    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)
+    end
+
+    return domains
+  end
+
+
+  def get_accounts_from_db()
+    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)
+    end
+
+    return accounts
+  end
+
+
+end