X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mailshears.git;a=blobdiff_plain;f=lib%2Fcommon%2Fpostfixadmin_plugin.rb;h=b984f6cf66a5b2e5d66155124577f8c6a2c6eaef;hp=4bfda9dcdece7e7216856b17ff96745c031313e7;hb=650e23790019880da91c7c7248a214a13763fd3e;hpb=7f8654ed6582062a295e1be75ae70e99de41b323 diff --git a/lib/common/postfixadmin_plugin.rb b/lib/common/postfixadmin_plugin.rb index 4bfda9d..b984f6c 100644 --- a/lib/common/postfixadmin_plugin.rb +++ b/lib/common/postfixadmin_plugin.rb @@ -1,4 +1,6 @@ +require 'common/domain' require 'common/plugin' +require 'common/user' require 'pg' module PostfixadminPlugin @@ -6,9 +8,7 @@ module PostfixadminPlugin # share. That is, we implement the Plugin interface. include Plugin - def initialize() - - cfg = Configuration.new() + def initialize(cfg) @db_host = cfg.postfixadmin_dbhost @db_port = cfg.postfixadmin_dbport @db_opts = cfg.postfixadmin_dbopts @@ -19,18 +19,6 @@ 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 = [] @@ -55,12 +43,13 @@ module PostfixadminPlugin raise DatabaseError.new(e) end - return domains + return domains.map{ |d| Domain.new(d) } end + def list_users() - accounts = [] + users = [] # Just assume PostgreSQL for now. begin @@ -72,11 +61,9 @@ 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| - accounts = result.field_values('username') + users = result.field_values('username') end connection.close() rescue PGError => e @@ -84,7 +71,7 @@ module PostfixadminPlugin raise DatabaseError.new(e) end - return accounts + return users.map{ |u| User.new(u) } end @@ -103,7 +90,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 @@ -113,7 +100,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