summaryrefslogtreecommitdiff
path: root/lib/MaildirIndexer/Store.pm6
blob: e54fa8f5a537c904ef2951335d13646ecdd8d476 (plain)
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
use v6.d.PREVIEW;
unit class MaildirIndexer::Store;
use MaildirIndexer::Parser;
 
has %!id-for-file;
has %!mailboxes-for-id;
has $!lock = Lock.new;
 
method dump() {
    $!lock.protect: {
        say "{.key} → {.value}" for %!id-for-file;
        say "{.key} ⇒ {.value.perl}" for %!mailboxes-for-id;
    }
}
 
method add-file(IO $file{
    my $email = parse-email($file,:headers-only);
    my $id = $email.message-id or return;
    my $mailbox = mailbox-from-path($file.pathor return;
    $!lock.protect: {
        %!id-for-file{ $file.path } = $id;
        %!mailboxes-for-id{ $id }.push($mailbox);
    };
    return;
}
 
method del-file(IO $file{
    my $mailbox = mailbox-from-path($file.pathor return;
    $!lock.protect: {
        my $id = %!id-for-file{ $file.path }:delete;
        with %!mailboxes-for-id{ $id } {
            with .grep($mailbox):k -> $pos {
                .splice($pos,1);
            }
        }
    }
    return;
}
 
method mailbox-for-email(MaildirIndexer::Email $email{
    for |$email.refs() -> $ref {
        with %!mailboxes-for-id{$ref} { return .[*-1}
    }
    return Nil;
}
 
sub mailbox-from-path(Str() $path) {
    $path ~~ m{'/' (<-[/]>+?'/' [cur|new|tmp] '/'} and return ~$/[0];
    return Nil;
}