]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/mv/plugins/postfixadmin.rb
9317b4b7d12d6c0dd64702793a072bc951e23178
[mailshears.git] / lib / mv / plugins / postfixadmin.rb
1 require 'pg'
2
3 require 'common/postfixadmin_plugin'
4 require 'mv/mv_plugin'
5
6 class PostfixadminMv
7
8 include PostfixadminPlugin
9 include MvPlugin
10
11 def mv_user(src, dst)
12 # It's obviously an error if the source user does not exist.
13 raise NonexistentUserError.new(src.to_s()) if not user_exists(src)
14
15 # And if the destination domain does not exist.
16 if not domain_exists(dst.domain())
17 raise NonexistentDomainError.new(dst.domain.to_s())
18 end
19
20 # But the destination *user* should *not* exist.
21 # And it's an error if the destination user exists already.
22 raise UserAlreadyExistsError.new(dst.to_s()) if user_exists(dst)
23
24 mailbox_query = 'UPDATE mailbox SET '
25 mailbox_query += ' username=$1,'
26 mailbox_query += ' domain=$2,'
27 mailbox_query += " maildir=CONCAT($2, '/', $3, '/'),"
28 mailbox_query += ' local_part=$3 '
29 mailbox_query += 'WHERE username=$4;'
30
31 alias_query1 = 'UPDATE alias SET '
32 alias_query1 += ' address=$1,'
33 alias_query1 += ' domain=$2,'
34 alias_query1 += ' goto=REPLACE(goto, $4, $1) '
35 alias_query1 += 'WHERE address=$4;'
36
37 alias_query2 = 'UPDATE alias SET '
38 alias_query2 += 'goto=REPLACE(goto, $4, $1);'
39
40 sql_queries = [mailbox_query, alias_query1, alias_query2]
41
42 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
43 @db_name, @db_user, @db_pass)
44
45 sql_queries.each do |sql_query|
46 varchar = 1043 # from pg_type.h
47 params = [{:value => dst.to_s(), :type => varchar},
48 {:value => dst.domainpart(), :type => varchar},
49 {:value => dst.localpart(), :type => varchar},
50 {:value => src.to_s(), :type => varchar}]
51
52 connection.query(sql_query, params)
53 end
54
55 connection.close()
56 end
57
58 end