From 7e5de74d94665d7d4d32945df389c5d827119f95 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Tue, 16 Sep 2014 20:53:14 -0400 Subject: [PATCH] Add a preliminary PostfixadminMv. --- lib/mv/plugins/postfixadmin.rb | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 lib/mv/plugins/postfixadmin.rb diff --git a/lib/mv/plugins/postfixadmin.rb b/lib/mv/plugins/postfixadmin.rb new file mode 100644 index 0000000..223c716 --- /dev/null +++ b/lib/mv/plugins/postfixadmin.rb @@ -0,0 +1,61 @@ +require 'pg' + +require 'common/postfixadmin_plugin' +require 'mv/mv_plugin' + +class PostfixadminMv + + include PostfixadminPlugin + include MvPlugin + + + def mv_user(user_from, user_to) + raise NonexistentUserError.new(user_from) if not user_exists(user_from) + + user_to_parts = user_to.split('@') + localpart_to = user_to_parts[0] + domain_to = user_to_parts[1] + + sql_queries = ['UPDATE mailbox SET username=$1, + domain=$2, + maildir=$2/$3/, + local_part=$3 + WHERE username=$4;'] + + sql_queries = ['UPDATE alias SET address=$1, + domain=$2, + goto=REPLACE(goto, $4, $1); + WHERE address=$4;'] + + sql_queries = ['UPDATE alias SET goto=REPLACE(GOTO, $4, $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, [user_to, + domain_to, + localpart_to, + user_from]) + end + + connection.close() + + rescue PGError => e + # Pretend like we're database-agnostic in case we ever are. + raise DatabaseError.new(e) + end + end + + + def mv_domain(domain_from, domain_to) + raise NotImplementedError + end + +end -- 2.44.2