From 7b5c1abf0bac5147efa468f50a384310b4376a7e Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 9 Nov 2018 16:18:11 +0000 Subject: feature parity! --- bayes | 15 +++++++++++++++ lib/MaildirIndexer/Parser.pm6 | 13 +++++++++++++ lib/MaildirIndexer/Store.pm6 | 6 ++++++ 3 files changed, 34 insertions(+) diff --git a/bayes b/bayes index 0843976..e3814fc 100644 --- a/bayes +++ b/bayes @@ -3,6 +3,7 @@ use v6.d.PREVIEW; use lib 'lib'; use MaildirIndexer::ScanDir; use MaildirIndexer::Store; +use MaildirIndexer::Parser; sub MAIN($maildir) { my $store = MaildirIndexer::Store.new; @@ -23,10 +24,24 @@ sub MAIN($maildir) { } } + my $listener = IO::Socket::Async.listen( + '127.0.0.1', + 9000, + :enc, + ); + react { whenever signal(SIGINT) { exit } whenever signal(SIGHUP) { $store.dump(); } + whenever $listener -> $conn { + LEAVE { $conn.close } + with parse-email($conn) -> $email { + with $store.mailbox-for-email($email) -> $mailbox { + await $conn.print("$mailbox\x0d\x0a"); + } + } + } } } diff --git a/lib/MaildirIndexer/Parser.pm6 b/lib/MaildirIndexer/Parser.pm6 index c2425fe..7b80ecf 100644 --- a/lib/MaildirIndexer/Parser.pm6 +++ b/lib/MaildirIndexer/Parser.pm6 @@ -58,6 +58,7 @@ class Message-actions { multi parse-email(IO::Path $p) is export { return parse-email($p.slurp(:enc)); } + multi parse-email(IO::Path $p, :$headers-only!) is export { return parse-email( $p.lines( @@ -67,6 +68,18 @@ multi parse-email(IO::Path $p, :$headers-only!) is export { )[0], ); } + +multi parse-email(IO::Socket::Async $s) is export { + my $string; + react { + whenever $s.Supply(:enc) { + $string ~= $_; + done if $string ~~ /@separators/; + } + } + return parse-email($string); +} + multi parse-email(Str $email-str) is export { with Message.parse($email-str,:actions(Message-actions.new)) { return .made; diff --git a/lib/MaildirIndexer/Store.pm6 b/lib/MaildirIndexer/Store.pm6 index 5bacbe9..7447643 100644 --- a/lib/MaildirIndexer/Store.pm6 +++ b/lib/MaildirIndexer/Store.pm6 @@ -37,6 +37,12 @@ method del-file(IO $file) { return; } +method mailbox-for-email(MaildirIndexer::Email $email) { + my $id = $email.message-id or return Nil; + with %!mailboxes-for-id{$id} { return .[*-1] } + return Nil; +} + sub mailbox-from-path(Str() $path) { $path ~~ m{'/' (<-[/]>+?) '/' [cur|new|tmp] '/'} and return ~$/[0]; return Nil; -- cgit v1.2.3