X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mailshears.git;a=blobdiff_plain;f=lib%2Fmv%2Fplugins%2Fpostfixadmin.rb;h=659b9b7d7166f98d2974cdc53159098cc9a9e8af;hp=3b6d1884975b939bbf7bd01f370fbde10cdf653a;hb=20b843bddcd73833d41f98ff79d92ef59bb4d81e;hpb=1d7d65c9583a879358de36ab1ac26b35a2805ba8 diff --git a/lib/mv/plugins/postfixadmin.rb b/lib/mv/plugins/postfixadmin.rb index 3b6d188..659b9b7 100644 --- a/lib/mv/plugins/postfixadmin.rb +++ b/lib/mv/plugins/postfixadmin.rb @@ -8,20 +8,19 @@ class PostfixadminMv include PostfixadminPlugin include MvPlugin - def mv_user(user_from, user_to) - if not user_exists(user_from) - raise NonexistentUserError.new("source user #{user_from} does not exist") - end - - user_to_parts = user_to.split('@') - localpart_to = user_to_parts[0] - domain_to = user_to_parts[1] + 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) - if not domain_exists(domain_to) - msg = "destination domain #{domain_to} does not exist" - raise NonexistentDomainError.new(msg) + # And if the destination domain does not exist. + if not domain_exists(dst.domain()) + raise NonexistentDomainError.new(dst.domain.to_s()) 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,' @@ -40,30 +39,18 @@ class PostfixadminMv sql_queries = [mailbox_query, alias_query1, alias_query2] - begin - connection = PGconn.connect(@db_host, - @db_port, - @db_opts, - @db_tty, - @db_name, - @db_user, - @db_pass) + 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 => user_to, :type => varchar}, - {:value => domain_to, :type => varchar}, - {:value => localpart_to, :type => varchar}, - {:value => user_from, :type => varchar}] - connection.query(sql_query, params) - end + 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.close() - - rescue PGError => e - # Pretend like we're database-agnostic in case we ever are. - raise DatabaseError.new(e) - end + connection.query(sql_query, params) + connection.close() end