X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mailshears.git;a=blobdiff_plain;f=lib%2Fmv%2Fplugins%2Fpostfixadmin.rb;h=9317b4b7d12d6c0dd64702793a072bc951e23178;hp=223c716b082ae8d089973d5316234d07f003d3ee;hb=a66e4a7e39518629981fa134456dc2ae6c461faf;hpb=7e5de74d94665d7d4d32945df389c5d827119f95 diff --git a/lib/mv/plugins/postfixadmin.rb b/lib/mv/plugins/postfixadmin.rb index 223c716..9317b4b 100644 --- a/lib/mv/plugins/postfixadmin.rb +++ b/lib/mv/plugins/postfixadmin.rb @@ -8,54 +8,51 @@ class PostfixadminMv include PostfixadminPlugin include MvPlugin + def mv_user(src, dst) + # It's obviously an error if the source user does not exist. + raise NonexistentUserError.new(src.to_s()) if not user_exists(src) - 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) + # And if the destination domain does not exist. + if not domain_exists(dst.domain()) + raise NonexistentDomainError.new(dst.domain.to_s()) end - end + # But the destination *user* should *not* exist. + # And it's an error if the destination user exists already. + raise UserAlreadyExistsError.new(dst.to_s()) if user_exists(dst) + + mailbox_query = 'UPDATE mailbox SET ' + mailbox_query += ' username=$1,' + mailbox_query += ' domain=$2,' + mailbox_query += " maildir=CONCAT($2, '/', $3, '/')," + mailbox_query += ' local_part=$3 ' + mailbox_query += 'WHERE username=$4;' + + alias_query1 = 'UPDATE alias SET ' + alias_query1 += ' address=$1,' + alias_query1 += ' domain=$2,' + alias_query1 += ' goto=REPLACE(goto, $4, $1) ' + alias_query1 += 'WHERE address=$4;' + + alias_query2 = 'UPDATE alias SET ' + alias_query2 += 'goto=REPLACE(goto, $4, $1);' + + sql_queries = [mailbox_query, alias_query1, alias_query2] + + connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty, + @db_name, @db_user, @db_pass) + + sql_queries.each do |sql_query| + varchar = 1043 # from pg_type.h + params = [{:value => dst.to_s(), :type => varchar}, + {:value => dst.domainpart(), :type => varchar}, + {:value => dst.localpart(), :type => varchar}, + {:value => src.to_s(), :type => varchar}] + + connection.query(sql_query, params) + end - def mv_domain(domain_from, domain_to) - raise NotImplementedError + connection.close() end end