- # Wipe out any aliases pointed at our user.
- sql_queries << "UPDATE alias SET goto=REPLACE(goto, $1, '');"
+
+ # Also delete aliases that point SOLELY TO our user.
+ sql_queries << "DELETE FROM alias WHERE goto = $1;"
+
+ # 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.
+ #
+ # 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}, '');"
+