File: ImapClone.pm

package info (click to toggle)
cyrus-imapd 3.6.1-4%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,688 kB
  • sloc: ansic: 255,928; perl: 97,730; javascript: 9,266; sh: 5,537; yacc: 2,651; cpp: 2,128; makefile: 2,099; lex: 660; xml: 621; python: 388; awk: 303; asm: 262
file content (489 lines) | stat: -rw-r--r-- 14,687 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/usr/bin/perl -w
#  Copyright (c) 2017 Fastmail.  All rights reserved.
#
# Author: Bron Gondwana <brong@fastmail.fm>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#
# 3. The name "Carnegie Mellon University" must not be used to
#    endorse or promote products derived from this software without
#    prior written permission. For permission or any other legal
#    details, please contact
#      Office of Technology Transfer
#      Carnegie Mellon University
#      5000 Forbes Avenue
#      Pittsburgh, PA  15213-3890
#      (412) 268-4387, fax: (412) 268-7395
#      tech-transfer@andrew.cmu.edu
#
# 4. Redistributions of any form whatsoever must retain the following
#    acknowledgment:
#    "This product includes software developed by Computing Services
#     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
#
# CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
# FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
# AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
# OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


package Cyrus::ImapClone;

use strict;
use warnings;
use Date::Parse;
use Cyrus::SyncProto;
use Mail::IMAPTalk;
use Data::Dumper;
use Digest::SHA qw(sha1_hex);
use Tie::DataUUID qw($uuid);
use IO::Socket::SSL;
use JSON::XS;
use IO::File;

=pod
=head1 NAME

Cyrus::ImapClone  - A pure perl interface to clone Cyrus Mailbox.

=head1 EXAMPLES

=cut
=head1 PUBLIC API
=over
=item Cyrus::ImapClone->new()
=cut

sub new {
  my $class = shift;
  my %args = @_;

  my $syncssl = $args{syncssl};
  my $st = Mail::IMAPTalk->new(
    Server => $args{synchost},
    Port => $args{syncport},
    Username => $args{syncuser},
    Password => $args{syncpass},
    AuthzUser => $args{syncauthz},
    UseSSL => $syncssl,
    UseBlocking => $syncssl,
    UseCompress => 1,
  );
  die "Failed to setup sync talk" unless $st;
  my $sp = Cyrus::SyncProto->new($st);
  if ($args{syncwipe}) {
    $sp->dlwrite('APPLY', 'UNUSER', $args{synctarget});
    $st->logout();
    return;
  }
  my $userdata = $sp->dlwrite('GET', 'USER', $args{synctarget});

  my $usessl = $args{imapssl};
  my $it = Mail::IMAPTalk->new(
    Server => $args{imaphost},
    Port => $args{imapport},
    Username => $args{imapuser},
    Password => $args{imappass},
    AuthzUser => $args{imapauthz},
    SSL_verify_mode => SSL_VERIFY_NONE,
    UseSSL => $usessl,
    UseBlocking => $usessl,
    UseCompress => 1,
  );

  return bless {
    syncer => $sp,
    synctalk => $st,
    imaptalk => $it,
    userdata => $userdata,
    targetuser => $args{synctarget},
  }, ref($class) || $class;
}

sub done {
  my $Self = shift;
  eval { $Self->{imaptalk}->logout() };
  eval { $Self->{synctalk}->logout() };
}

sub DESTROY {
  my $Self = shift;
  $Self->done();
}

sub batchfillrecords {
  my $Self = shift;
  my $mboxname = shift;
  my $records = shift;

  my %todo = %$records;
  my $total = scalar keys %todo;

  # batch in units of max 10 megabytes plus 1 message
  while (%todo) {
    my $size = 0;
    my %batch;
    foreach my $uid (sort {$a <=> $b} keys %todo) {
      $batch{$uid} = delete $todo{$uid};
      $size += $batch{$uid}{SIZE};
      last if $size > 1024 * 1024 * 10; # 10 megabytes
    }
    $Self->fillrecords($mboxname, \%batch);
    last unless %todo;
    print "Batching - still " . scalar(keys %todo) . "/$total to go for $mboxname\n" if $Self->{verbose};
  }
}

sub fillrecords {
  my $Self = shift;
  my $mboxname = shift;
  my $records = shift;

  # XXX - smaller batch to control memory usage?

  my $imap = $Self->{imaptalk};
  my $fetch = $imap->fetch([sort {$a <=> $b} keys %$records], '(rfc822)');
  my @apply;
  foreach my $uid (sort {$a <=> $b} keys %$records) {
    die "MISSING $uid" unless $fetch->{$uid};
    die "SIZE MISSMATCH $uid" unless $records->{$uid}{SIZE} == length($fetch->{$uid}{rfc822});
    $records->{$uid}{GUID} = sha1_hex($fetch->{$uid}{rfc822});
  }

  # let's try to reserve first
  my @names = map { $_->{MBOXNAME} } @{$Self->{userdata}{MAILBOX}};
  my %guids = map { $_->{GUID} => 1 } values %$records;
  my $res = $Self->{syncer}->dlwrite('APPLY', 'RESERVE', {PARTITION => 'default', MBOXNAME => \@names, GUID => [sort keys %guids]});
  my %missing = map { $_ => 1 } @{$res->{MISSING}[0]};

  return unless %missing;

  foreach my $uid (sort {$a <=> $b} keys %$records) {
    next unless $missing{$records->{$uid}{GUID}};
    push @apply, \['default', $records->{$uid}{GUID}, $records->{$uid}{SIZE}, $fetch->{$uid}{rfc822}];
  }

  return unless @apply;

  $Self->{syncer}->dlwrite('APPLY', 'MESSAGE', \@apply);
}

sub syncmailbox {
  my $Self = shift;
  my $mboxname = shift;
  my $existing = shift;

  if ($existing) {
    my $status = $Self->{imaptalk}->status($Self->_sync_to_imap($mboxname), "(HIGHESTMODSEQ UIDVALIDITY)");
    die "UIDVALIDITY CHANGED" if ($existing->{UIDVALIDITY} != $status->{uidvalidity});
    return if ($existing->{HIGHESTMODSEQ} == $status->{highestmodseq});
  }

  $Self->{imaptalk}->examine($Self->_sync_to_imap($mboxname));
  my $imap = $Self->{imaptalk};
  my %idata = (
    UIDVALIDITY => $imap->get_response_code('uidvalidity') + 0,
    LAST_UID => $imap->get_response_code('uidnext') - 1,
    HIGHESTMODSEQ => $imap->get_response_code('highestmodseq') || 1,
    EXISTS => $imap->get_response_code('exists') || 0,
  );

  # basic sanity checks
  die "UIDVALIDITY CHANGED" if ($existing and $existing->{UIDVALIDITY} != $idata{UIDVALIDITY});
  return if ($existing and $existing->{HIGHESTMODSEQ} == $idata{HIGHESTMODSEQ});

  my $sdata = $Self->readup($mboxname, $existing);

  # basic sanity checks again with latest data
  die "UIDVALIDITY CHANGED " . Dumper($sdata) if ($sdata and $sdata->{UIDVALIDITY} != $idata{UIDVALIDITY});
  return if ($existing and $existing->{HIGHESTMODSEQ} == $idata{HIGHESTMODSEQ});

  # sanity range checks
  die "FUTURE CHANGED MODSEQ $sdata->{HIGHESTMODSEQ} > $idata{HIGHESTMODSEQ}" if ($sdata and $sdata->{HIGHESTMODSEQ} > $idata{HIGHESTMODSEQ});
  die "FUTURE CHANGED UIDS $sdata->{LAST_UID} > $idata{LAST_UID}" if ($sdata and $sdata->{LAST_UID} > $idata{LAST_UID});

  my $time = time();

  unless ($sdata) {
    print "NEW MAILBOX $mboxname: $idata{EXISTS}\n" if $Self->{verbose};

    my %mb = (
      ACL => Cyrus::SyncProto::user_acl($Self->{targetuser}),
      HIGHESTMODSEQ => 0,
      LAST_APPENDDATE => 0,
      LAST_UID => 0,
      MBOXNAME => $mboxname,
      OPTIONS => 'P',
      PARTITION => 'default',
      POP3_LAST_LOGIN => 0,
      POP3_SHOW_AFTER => 0,
      QUOTAROOT => $Self->_imap_to_sync('INBOX'),
      RECENTTIME => 0,
      RECENTUID => 0,
      RECORD => [],
      SYNC_CRC => 0,
      SYNC_ANNOT_CRC => 0,
      UIDVALIDITY => $idata{UIDVALIDITY},
      UNIQUEID => $uuid,
    );

    push @{$Self->{userdata}{MAILBOX}}, \%mb;

    $sdata = { %mb, RECORD => [] };
  }

  my $recentuid = $idata{LAST_UID};
  my @applyrecords;

  # clever logic here..
  if ($sdata->{LAST_UID}) {
    # re-fetch flags only
    my $end = $sdata->{LAST_UID};
    my $fetch = $imap->fetch("1:$end", "(uid flags modseq)", "(changedsince $sdata->{HIGHESTMODSEQ})");
    foreach my $record (grep { _notexpunged($_) } @{$sdata->{RECORD}}) {
      my $uid = $record->{UID};
      next unless $fetch->{$uid};
      my @flags = @{$fetch->{$uid}{flags}};
      if (grep { lc $_ eq '\\recent' } @flags) {
        $recentuid = $uid if $recentuid > $uid;
      }

      # update the record and the CRC
      $sdata->{SYNC_CRC} ^= $Self->{syncer}->record_crc($record);
      $record->{FLAGS} = _cleanflags(@flags);
      $record->{MODSEQ} = $fetch->{$uid}{modseq}[0];
      $record->{LAST_UPDATED} = $time;
      $sdata->{SYNC_CRC} ^= $Self->{syncer}->record_crc($record);
      push @applyrecords, $record;
    }
  }

  my $first = $sdata->{LAST_UID} + 1;
  my $last = $idata{LAST_UID};
  if ($last >= $first) {
    my $fetch = $imap->fetch("$first:$last", "(uid flags modseq internaldate rfc822.size)");
    my %records;
    foreach my $uid (sort {$a <=> $b} keys %$fetch) {
      my @flags = @{$fetch->{$uid}{flags}};
      if (grep { lc $_ eq '\\recent' } @flags) {
        $recentuid = $uid if $recentuid > $uid;
      }

      $records{$uid} = {
        # ANNOTATIONS => [],
        FLAGS => _cleanflags(@flags),
        # GUID to be filled
        INTERNALDATE => _mkunixtime($fetch->{$uid}{internaldate}),
        LAST_UPDATED => $time,
        MODSEQ => $fetch->{$uid}{modseq}[0],
        SIZE => $fetch->{$uid}{'rfc822.size'},
        UID => $uid,
      };
    }
    $Self->batchfillrecords($mboxname, \%records);
    foreach my $uid (sort {$a <=> $b} keys %records) {
      push @applyrecords, $records{$uid};
      push @{$sdata->{RECORD}}, $records{$uid};
      $sdata->{SYNC_CRC} ^= $Self->{syncer}->record_crc($records{$uid});
    }
  }

  if ($idata{EXISTS} != scalar(grep { _notexpunged($_) } @{$sdata->{RECORD}})) {
    # we need to expunge something - let's see what..
    print "DOING EXPUNGE CHECK FOR $mboxname\n" if $Self->{verbose};
    my $uids = $imap->search('uid', "1:$last");
    my %exists = map { $_ => 1 } @$uids;
    foreach my $record (grep { _notexpunged($_) } @{$sdata->{RECORD}}) {
      next if $exists{$record->{UID}};
      # update the record and the CRC
      $sdata->{SYNC_CRC} ^= $Self->{syncer}->record_crc($record);
      push @{$record->{FLAGS}}, "\\Expunged";
      $record->{MODSEQ} = $idata{HIGHESTMODSEQ};
      $record->{LAST_UPDATED} = $time;
      push @applyrecords, $record;
    }
  }

  $sdata->{HIGHESTMODSEQ} = $idata{HIGHESTMODSEQ};
  $sdata->{LAST_UID} = $idata{LAST_UID};
  $sdata->{RECENTTIME} = $time;
  $sdata->{RECENTUID} = $recentuid;

  $Self->{syncer}->dlwrite('APPLY', 'MAILBOX', {%$sdata, RECORD => [sort { $a->{UID} <=> $b->{UID} } @applyrecords]});

  $Self->writedown($sdata);
}

sub readup {
  my $Self = shift;
  my $mboxname = shift;
  my $existing = shift;

  if ($existing and $Self->{cachedir} and $Self->cachepath($existing->{UNIQUEID})) {
    my $file = IO::File->new($Self->cachepath($existing->{UNIQUEID}), "r");
    my $data = eval { $file->getline() };
    my $perl = eval { decode_json($data) };
    if ($perl and $perl->{UNIQUEID} eq $existing->{UNIQUEID} and $perl->{HIGHESTMODSEQ} eq $existing->{HIGHESTMODSEQ} and $perl->{UIDVALIDITY} eq $existing->{UIDVALIDITY} and $perl->{LAST_UID} eq $existing->{LAST_UID}) {
      print "READING $mboxname FROM CACHE\n" if $Self->{verbose};
      return $perl;
    }
    else {
      use Data::Dumper;
      my %check =  map { $_ => $perl->{$_} } qw(UNIQUEID HIGHESTMODSEQ UIDVALIDITY LAST_UID);
      print "INVALID $mboxname CACHE: " . Dumper(\%check, $existing) if $Self->{verbose};
    }
  }

  my $res = eval { $Self->{syncer}->dlwrite('GET', 'FULLMAILBOX', $mboxname)->{MAILBOX}[0] };

  $Self->writedown($res) if $res;

  return $res;
}

sub writedown {
  my $Self = shift;
  my $data = shift;
  return unless $Self->{cachedir};
  my @records = sort { $a->{UID} <=> $b->{UID} } @{$data->{RECORD}};
  $data->{RECORD} = \@records;
  eval {
    my $file = IO::File->new($Self->cachepath($data->{UNIQUEID}, 1), 'w');
    $file->print(encode_json($data));
  };
}

sub cachepath {
  my $Self = shift;
  my $uniqueid = shift;
  my $make = shift;
  return unless $Self->{cachedir};
  my $dir = "$Self->{cachedir}/$Self->{targetuser}";
  my $path = "$dir/$uniqueid.cache";
  return (-f $path ? $path : undef) unless $make;
  mkdir $dir unless -d $dir;
  return $path;
}

sub _notexpunged {
  my $record = shift;
  my @expunged = grep { lc $_ eq '\\expunged' } @{$record->{FLAGS}};
  return not scalar @expunged;
}

sub _cleanflags {
  my @flags = @_;
  my @clean = grep { lc $_ ne '\\recent' } @flags;
  return \@clean;
}

sub _mkunixtime {
  my $time = shift;
  return str2time($time);
}

sub syncmailboxes {
  my $Self = shift;
  my $userdata = $Self->{userdata};

  my $list = $Self->{imaptalk}->list('INBOX', '*');
  my %mbox = map { $Self->_imap_to_sync($_->[2]) => 1 } @$list;

  foreach my $mailbox (@{$userdata->{MAILBOX}}) {
    if (delete $mbox{$mailbox->{MBOXNAME}}) {
      $Self->syncmailbox($mailbox->{MBOXNAME}, $mailbox);
    } else {
      $Self->{syncer}->apply_unmailbox($mailbox->{MBOXNAME});
    }
  }
  foreach my $new (sort keys %mbox) {
    $Self->syncmailbox($new);
  }
}

sub syncsubs {
  my $Self = shift;
  my $userdata = $Self->{userdata};
  my $lsub = $Self->{imaptalk}->lsub('INBOX', '*');
  my %sub = map { $Self->_imap_to_sync($_->[2]) => 1 } @$lsub;

  foreach my $existing (@{$userdata->{LSUB}[0]}) {
    next if delete $sub{$existing};
    $Self->{syncer}->apply_unsub($existing, $Self->{targetuser});
  }
  foreach my $new (keys %sub) {
    $Self->{syncer}->apply_sub($new, $Self->{targetuser});
  }
}

sub syncquota {
  my $Self = shift;
  my $userdata = $Self->{userdata};
  my $quota = $Self->{imaptalk}->getquotaroot('INBOX');
  my $name = $quota->{quotaroot}[1];
  my $amount = $quota->{$name}[2];
  my $existing = $Self->{userdata}{QUOTA}[0];
  if ($existing and not $amount) {
    $Self->{syncer}->dlwrite('APPLY', 'UNQUOTA', $existing->{ROOT});
    return;
  }
  return if not $amount;
  return if ($existing and $amount == $existing->{STORAGE});
  $Self->{syncer}->dlwrite('APPLY', 'QUOTA', { ROOT => $Self->_imap_to_sync('INBOX'), STORAGE => $amount });
}

sub syncuser {
  my $Self = shift;
  $Self->syncmailboxes();
  $Self->syncsubs();
  $Self->syncquota();
  return 1;
}

sub _imap_to_sync {
  my $Self = shift;
  my $name = shift;
  my ($l, $d) = _splituser($Self->{targetuser});
  my $res = '';
  $res = "$d!" if $d;
  $res .= "user.$l";
  $name =~ s/^INBOX//i;
  return "$res$name";
}

sub _sync_to_imap {
  my $Self = shift;
  my $name = shift;
  $name =~ s/^(.*\!)//;
  $name =~ s/^user\.[^.]+//;
  return "INBOX$name";
}

sub _splituser {
  my $user = shift;
  return split /\@/, $user;
}

=back
=head1 AUTHOR AND COPYRIGHT

Bron Gondwana <brong@fastmail.fm> - Copyright 2017 FastMail

Licenced under the same terms as Cyrus IMAPd.

=cut

1;