]> git.g-eek.se Git - interimap.git/commitdiff
Fix bug in synchronizing the subscription list.
authorGuilhem Moulin <guilhem@fripost.org>
Sun, 26 Jul 2015 04:58:27 +0000 (06:58 +0200)
committerGuilhem Moulin <guilhem@fripost.org>
Sun, 26 Jul 2015 04:58:27 +0000 (06:58 +0200)
imapsync

index 540797ee2104035edc0cf7844356281fe3379f5d..52d0157221ed77dc2049d751cea29682f2549007 100755 (executable)
--- a/imapsync
+++ b/imapsync
@@ -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;
     }
 }