From f64d7df93fa4d21ec2ea8cfa08ed9f58af23df9b Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Fri, 24 Jul 2015 18:02:21 +0200 Subject: Pass messages by reference. --- imapsync | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'imapsync') diff --git a/imapsync b/imapsync index 6fb82ab..4cbc503 100755 --- a/imapsync +++ b/imapsync @@ -579,19 +579,19 @@ sub fix_missing($$$@) { my $target = $name eq 'local' ? $rIMAP : $lIMAP; my $attrs = join ' ', qw/MODSEQ FLAGS INTERNALDATE ENVELOPE BODY.PEEK[]/; - $source->fetch(compact_set(@set), "($attrs)", sub(%) { - my %mail = @_; - return unless exists $mail{RFC822}; # not for us + $source->fetch(compact_set(@set), "($attrs)", sub($) { + my $mail = shift; + return unless exists $mail->{RFC822}; # not for us - my $from = first { defined $_ and @$_ } @{$mail{ENVELOPE}}[2,3,4]; + my $suid = $mail->{UID}; + my $from = first { defined $_ and @$_ } @{$mail->{ENVELOPE}}[2,3,4]; $from = (defined $from and @$from) ? $from->[0]->[2].'@'.$from->[0]->[3] : ''; - print STDERR "$name($mailbox): UID $mail{UID} from <$from> ($mail{INTERNALDATE})\n" unless $CONFIG{quiet}; + print STDERR "$name($mailbox): UID $suid from <$from> ($mail->{INTERNALDATE})\n" unless $CONFIG{quiet}; # don't bother checking for MULTIAPPEND, @set is probably rather small - my @mail = ($mail{RFC822}, [ grep {lc $_ ne '\recent'} @{$mail{FLAGS}} ], $mail{INTERNALDATE}); - my ($uid) = $target->append($mailbox, @mail); + my ($tuid) = $target->append($mailbox, $mail); - my ($lUID, $rUID) = $name eq 'local' ? ($mail{UID}, $uid) : ($uid, $mail{UID}); + my ($lUID, $rUID) = $name eq 'local' ? ($suid, $tuid) : ($tuid, $suid); print STDERR "$name($mailbox): Adding mapping (lUID,rUID) = ($lUID,$rUID)\n"; $STH_INSERT_MAPPING->execute($idx, $lUID, $rUID); }); @@ -821,29 +821,33 @@ sub sync_messages($$) { # don't fetch again the messages we've just added my @ignore = $source eq 'local' ? keys %mapping : values %mapping; - ($source eq 'local' ? $lIMAP : $rIMAP)->pull_new_messages(sub(%) { - my %mail = @_; - return unless exists $mail{RFC822}; # not for us + ($source eq 'local' ? $lIMAP : $rIMAP)->pull_new_messages(sub($) { + my $mail = shift; + return unless exists $mail->{RFC822}; # not for us - my @mail = ($mail{RFC822}, [ grep {lc $_ ne '\recent'} @{$mail{FLAGS}} ], $mail{INTERNALDATE}); - push @sUID, $mail{UID}; + my $length = length $mail->{RFC822}; + my $suid = $mail->{UID}; + if ($length == 0) { + warn "$source($mailbox): WARNING: Ignoring new 0-length message (UID $suid)\n"; + return; + } # use MULTIAPPEND if possible (RFC 3502) to save round-trips $multiappend //= !$target->incapable('MULTIAPPEND'); + push @sUID, $suid; if (!$multiappend) { - my ($uid) = $target->append($mailbox, @mail); - push @tUID, $uid; + push @tUID, $target->append($mailbox, $mail); } else { # proceed by batch of 1MB to save roundtrips without blowing up the memory - if (@newmails and $buffer + length($mail{RFC822}) > 1048576) { + if (@newmails and $buffer + $length > 1048576) { push @tUID, $target->append($mailbox, @newmails); @newmails = (); $buffer = 0; } - push @newmails, @mail; - $buffer += length $mail{RFC822}; + push @newmails, $mail; + $buffer += $length; } }, @ignore); push @tUID, $target->append($mailbox, @newmails) if @newmails; -- cgit v1.2.3