]> gitweb.michael.orlitzky.com - mailshears.git/commitdiff
Add a preliminary PostfixadminMv.
authorMichael Orlitzky <michael@orlitzky.com>
Wed, 17 Sep 2014 00:53:14 +0000 (20:53 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Wed, 17 Sep 2014 00:53:14 +0000 (20:53 -0400)
lib/mv/plugins/postfixadmin.rb [new file with mode: 0644]

diff --git a/lib/mv/plugins/postfixadmin.rb b/lib/mv/plugins/postfixadmin.rb
new file mode 100644 (file)
index 0000000..223c716
--- /dev/null
@@ -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