X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mailshears.git;a=blobdiff_plain;f=lib%2Fcommon%2Fpostfixadmin_plugin.rb;fp=lib%2Fcommon%2Fpostfixadmin_plugin.rb;h=4bfda9dcdece7e7216856b17ff96745c031313e7;hp=0000000000000000000000000000000000000000;hb=7f8654ed6582062a295e1be75ae70e99de41b323;hpb=bd2dabf89ab277fbe315b05e6dfa839afb5ce5ef diff --git a/lib/common/postfixadmin_plugin.rb b/lib/common/postfixadmin_plugin.rb new file mode 100644 index 0000000..4bfda9d --- /dev/null +++ b/lib/common/postfixadmin_plugin.rb @@ -0,0 +1,119 @@ +require 'common/plugin' +require 'pg' + +module PostfixadminPlugin + # Code that all Postfixadmin plugins (Prune, Rm, Mv...) will + # share. That is, we implement the Plugin interface. + include Plugin + + def initialize() + + cfg = Configuration.new() + @db_host = cfg.postfixadmin_dbhost + @db_port = cfg.postfixadmin_dbport + @db_opts = cfg.postfixadmin_dbopts + @db_tty = cfg.postfixadmin_dbtty + @db_name = cfg.postfixadmin_dbname + @db_user = cfg.postfixadmin_dbuser + @db_pass = cfg.postfixadmin_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 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) + end + + return domains + 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) + end + + return accounts + 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) + end + + return usernames + end + +end