From: Michael Orlitzky Date: Wed, 17 Sep 2014 00:53:14 +0000 (-0400) Subject: Add a preliminary PostfixadminMv. X-Git-Tag: 0.0.1~60 X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=7e5de74d94665d7d4d32945df389c5d827119f95;p=mailshears.git Add a preliminary PostfixadminMv. --- 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