X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mailshears.git;a=blobdiff_plain;f=lib%2Frm%2Fplugins%2Fpostfixadmin.rb;fp=lib%2Frm%2Fplugins%2Fpostfixadmin.rb;h=46eda4736878c25c35e435aee446a89d77be5fe3;hp=c8e44abd2d9140b5a75d969078e31b6eb836aa02;hb=a2d3dd9ee4838fb99557718b4bcdc11c8d1372fd;hpb=ef77e919fa61bb5ba7924d49a171dc3a05410a33 diff --git a/lib/rm/plugins/postfixadmin.rb b/lib/rm/plugins/postfixadmin.rb index c8e44ab..46eda47 100644 --- a/lib/rm/plugins/postfixadmin.rb +++ b/lib/rm/plugins/postfixadmin.rb @@ -31,10 +31,16 @@ class PostfixadminRm # But aliases don't need to point to a single user! If our user # was part of a multi-recipient alias, we want to remove our user - # from the alias and leave the other recipients. If you're - # wondering about the leftover double-commas, look towards the end - # of the function. - sql_queries << "UPDATE alias SET goto=REPLACE(goto, $1, '');" + # from the alias and leave the other recipients. + # + # We want to delete the comma that precedes/follows the address, + # too. Since the address to be replaced can appear at either the + # beginning or the end of the list (as well as in the middle), we + # have to try to fix both cases: comma before, and comma after. + comma_before = "CONCAT(',', $1)" + comma_after = "CONCAT($1, ',')" + sql_queries << "UPDATE alias SET goto=REPLACE(goto, #{comma_before}, '');" + sql_queries << "UPDATE alias SET goto=REPLACE(goto, #{comma_after}, '');" sql_queries << 'DELETE FROM mailbox WHERE username = $1;' sql_queries << 'DELETE FROM quota WHERE username = $1;' @@ -48,15 +54,10 @@ class PostfixadminRm begin sql_queries.each do |sql_query| - connection.query(sql_query, [user.to_s()]) + varchar = 1043 # from pg_type.h + params = [{:value => user.to_s(), :type => varchar}] + connection.query(sql_query, params) end - - # The earlier alias update query will leave things like - # "foo@example.com,,bar@example.com" in the "goto" column. Now - # we fix it. We don't do it in the loop because query() craps - # out on the superfluous parameter. - sql_query = "UPDATE alias SET goto=REPLACE(goto, ',,', ',');" - connection.query(sql_query) ensure # Make sure the connection gets closed even if a query explodes. connection.close()