X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mailshears.git;a=blobdiff_plain;f=lib%2Fcommon%2Fpostfixadmin_db.rb;fp=lib%2Fcommon%2Fpostfixadmin_db.rb;h=0249ea07949b384a5438f34c9c5f7c27e582d7e0;hp=0000000000000000000000000000000000000000;hb=ec3c9099a29f40d95f055ea0f7fb25a99d913de3;hpb=c6cab6b71770d14dad1115db90a00b990c44a58d diff --git a/lib/common/postfixadmin_db.rb b/lib/common/postfixadmin_db.rb new file mode 100644 index 0000000..0249ea0 --- /dev/null +++ b/lib/common/postfixadmin_db.rb @@ -0,0 +1,79 @@ +require 'pg' + +class PostfixadminDb + + def initialize(db_host, + db_port, + db_opts, + db_tty, + db_name, + db_user, + db_pass) + + @db_host = db_host + @db_port = db_port + @db_opts = db_opts + @db_tty = db_tty + @db_name = db_name + @db_user = db_user + @db_pass = db_pass + 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