X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mailshears.git;a=blobdiff_plain;f=lib%2Frm%2Fplugins%2Fpostfixadmin_db.rb;fp=lib%2Frm%2Fplugins%2Fpostfixadmin_db.rb;h=0000000000000000000000000000000000000000;hp=ca91b8beb553f545b00f8de85c5cc475bef594fa;hb=7f8654ed6582062a295e1be75ae70e99de41b323;hpb=bd2dabf89ab277fbe315b05e6dfa839afb5ce5ef diff --git a/lib/rm/plugins/postfixadmin_db.rb b/lib/rm/plugins/postfixadmin_db.rb deleted file mode 100644 index ca91b8b..0000000 --- a/lib/rm/plugins/postfixadmin_db.rb +++ /dev/null @@ -1,123 +0,0 @@ -require 'pg' - -require 'common/postfixadmin_db_plugin' -require 'rm/rm_plugin' - -class PostfixadminDbRm - - include PostfixadminDbPlugin - include RmPlugin - - - def delete_account(account) - raise NonexistentAccountError.new(account) if not user_exists(account) - - sql_queries = ['DELETE FROM alias WHERE address = $1;'] - # Wipe out any aliases pointed at our account. - sql_queries << "UPDATE alias SET goto=REPLACE(goto, $1, '');" - sql_queries << 'DELETE FROM mailbox WHERE username = $1;' - sql_queries << 'DELETE FROM quota WHERE username = $1;' - sql_queries << 'DELETE FROM quota2 WHERE username = $1;' - sql_queries << 'DELETE FROM vacation WHERE email = $1;' - - # Should be handled by a trigger, according to PostfixAdmin code. - sql_queries << 'DELETE FROM vacation_notification WHERE on_vacation = $1;' - - begin - connection = PGconn.connect(@db_host, - @db_port, - @db_opts, - @db_tty, - @db_name, - @db_user, - @db_pass) - - sql_queries.each do |sql_query| - connection.query(sql_query, [account]) - end - - # The earlier alias update query will leave things like - # "foo@example.com,,bar@example.com" in the "goto" column. Now - # we fix it. We don't do it in the loop because query() craps - # out on the superfluous parameter. - sql_query = "UPDATE alias SET goto=REPLACE(goto, ',,', ',');" - connection.query(sql_query) - - connection.close() - - rescue PGError => e - # Pretend like we're database-agnostic in case we ever are. - raise DatabaseError.new(e) - end - end - - - def delete_domain(domain) - raise NonexistentDomainError.new(domain) if not domain_exists(domain) - - sql_queries = ['DELETE FROM domain_admins WHERE domain = $1;'] - sql_queries << 'DELETE FROM alias WHERE domain = $1;' - sql_queries << 'DELETE FROM mailbox WHERE domain = $1;' - sql_queries << 'DELETE FROM alias_domain WHERE alias_domain = $1;' - sql_queries << 'DELETE FROM log WHERE domain = $1;' - sql_queries << 'DELETE FROM vacation WHERE domain = $1;' - sql_queries << 'DELETE FROM domain WHERE domain = $1;' - - begin - connection = PGconn.connect(@db_host, - @db_port, - @db_opts, - @db_tty, - @db_name, - @db_user, - @db_pass) - - sql_queries.each do |sql_query| - connection.query(sql_query, [domain]) - end - - connection.close() - - rescue PGError => e - # Pretend like we're database-agnostic in case we ever are. - raise DatabaseError.new(e) - end - - end - - - protected; - - def domain_exists(domain) - count = 0 - - # 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 COUNT(domain) as count FROM domain WHERE domain = $1;' - connection.query(sql_query, [domain]) do |result| - return false if result.ntuples() < 1 - begin - count = result.getvalue(0,0).to_i() - return false if count.nil? - rescue StandardError - return false - end - end - connection.close() - rescue PGError => e - # But pretend like we're database-agnostic in case we ever are. - raise DatabaseError.new(e) - end - - return (count > 0) - end - -end