summaryrefslogtreecommitdiff
path: root/lib/MaildirIndexer/Server.rakumod
blob: 07dfb4a2728df742265c2b2132e13d6e45588e1b (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
use v6.d;
unit class MaildirIndexer::Server;
use MaildirIndexer::LogTimelineSchema;
use MaildirIndexer::Parser;
use MaildirIndexer::Store;
 
has $.port = 9000;
has MaildirIndexer::Store $.store is required;
 
method serve() {
    my $listener = IO::Socket::Async.listen(
        '127.0.0.1',
        $.port,
        :enc<utf8-c8>,
    );
 
    react {
        whenever signal(SIGINT{ exit }
        whenever signal(SIGHUP{
            $.store.dump();
        }
        whenever $listener -> $conn {
            MaildirIndexer::LogTimelineSchema::Server::Serve.log: {
                LEAVE { $conn.close }
                with parse-email($conn-> $email {
                    with $.store.mailbox-for-email($email-> $mailbox {
                        await $conn.print("$mailbox\x0d\x0a");
                    }
                }
            }
        }
    }
}