diff options
-rwxr-xr-x | imapsync | 54 |
1 files changed, 24 insertions, 30 deletions
@@ -454,58 +454,52 @@ my @SUBSCRIPTIONS; $mailboxes{$_} = 1 foreach (keys %{$IMAP->{local}->{mailboxes}}, keys %{$IMAP->{remote}->{mailboxes}}); foreach my $mbx (keys %mailboxes) { - if (subscribed_mbx('local',$mbx) xor subscribed_mbx('remote',$mbx)) { - my ($subscribed,$unsubscribed) = subscribed_mbx('local',$mbx) ? ('local','remote') : ('remote','local'); - - $sth_search->execute($mbx); - my $row = $sth_search->fetch(); - die if defined $sth_search->fetch(); # sanity check + $sth_search->execute($mbx); + my $row = $sth_search->fetch(); + die if defined $sth_search->fetch(); # sanity check + my ($lSubscribed,$rSubscribed) = map {subscribed_mbx($_,$mbx)} qw/local remote/; + if ($lSubscribed == $rSubscribed) { + if (defined $row) { + my ($idx,$status) = @$row; + if (defined $status and $status != $lSubscribed) { + $sth_subscribe->execute($lSubscribed, $idx) or + msg('database', "WARNING: Can't (un)subscribe $mbx"); + $DBH->commit(); + } + } + } + else { + my ($subscribed,$unsubscribed) = $lSubscribed ? qw/local remote/ : qw/remote local/; if (defined $row) { my ($idx,$status) = @$row; if ($status) { # $mbx was SUBSCRIBEd before, UNSUBSCRIBE it now msg($subscribed, "Unsubscribe to mailbox $mbx"); - $sth_subscribe->execute(0,$idx); + $sth_subscribe->execute(0,$idx) or + msg('database', "WARNING: Can't unsubscribe $mbx"); $IMAP->{$subscribed}->{client}->unsubscribe($mbx); $DBH->commit(); - $IMAP->{$subscribed}->{mailboxes}->{$mbx} = - grep {lc $_ ne lc '\Subscribed'} @{$IMAP->{$subscribed}->{mailboxes}->{$mbx} // []}; + $lSubscribed = $rSubscribed = 0; } else { # $mbx was UNSUBSCRIBEd before, SUBSCRIBE it now msg($unsubscribed, "Subscribe to mailbox $mbx"); - $sth_subscribe->execute(1,$idx); + $sth_subscribe->execute(1,$idx) or + msg('database', "WARNING: Can't subscribe $mbx"); $IMAP->{$unsubscribed}->{client}->subscribe($mbx); $DBH->commit(); - $IMAP->{$unsubscribed}->{mailboxes}->{$mbx} //= []; - push @{$IMAP->{$unsubscribed}->{mailboxes}->{$mbx}}, '\Subscribed'; + $lSubscribed = $rSubscribed = 1; } } else { # $mbx is unknown; assume the user wants to SUBSCRIBE msg($unsubscribed, "Subscribe to mailbox $mbx"); $IMAP->{$unsubscribed}->{client}->subscribe($mbx); - $IMAP->{$unsubscribed}->{mailboxes}->{$mbx} //= []; - push @{$IMAP->{$unsubscribed}->{mailboxes}->{$mbx}}, '\Subscribed'; - } - } - else { - $sth_search->execute($mbx); - my $row = $sth_search->fetch(); - die if defined $sth_search->fetch(); # sanity check - - if (defined $row) { - my ($idx,$status) = @$row; - unless (defined $status and $status != 0) { - my $subscribed = subscribed_mbx('local',$mbx) ? 1 : 0; - $sth_subscribe->execute($subscribed, $idx); - $DBH->commit(); - } + $lSubscribed = $rSubscribed = 1; } } - push @SUBSCRIPTIONS, $mbx if subscribed_mbx('local', $mbx) and - subscribed_mbx('remote',$mbx); + push @SUBSCRIPTIONS, $mbx if $lSubscribed; } } |