summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2018-11-09 14:41:29 +0000
committerdakkar <dakkar@thenautilus.net>2018-11-09 14:41:29 +0000
commitdb72db7fd0c10131dfc3c4a49e3e2c77aae96b25 (patch)
tree6a9e16db8a00a8133aed7928e8e1f296fff6b2bd
parentlet's use Perl 6! (diff)
downloadMaildirIndexer-db72db7fd0c10131dfc3c4a49e3e2c77aae96b25.tar.gz
MaildirIndexer-db72db7fd0c10131dfc3c4a49e3e2c77aae96b25.tar.bz2
MaildirIndexer-db72db7fd0c10131dfc3c4a49e3e2c77aae96b25.zip
move stuff into modules
-rw-r--r--bayes65
-rw-r--r--lib/MaildirIndexer/Parser.pm671
-rw-r--r--lib/MaildirIndexer/ScanDir.pm634
3 files changed, 121 insertions, 49 deletions
diff --git a/bayes b/bayes
index 37352ce..c7c81bf 100644
--- a/bayes
+++ b/bayes
@@ -1,55 +1,22 @@
#!/usr/bin/env perl6
use v6.d.PREVIEW;
+use lib 'lib';
+use MaildirIndexer::ScanDir;
+use MaildirIndexer::Parser;
-grammar Message {
- regex TOP {
- <headers>
- <separator>
- <body>
+sub MAIN($maildir) {
+ my $file-supply = scan-dir($maildir);
+ my $file-channel = $file-supply.Channel;
+ for ^10 {
+ start react {
+ whenever $file-channel -> $file {
+ if $file.e && $file.f {
+ my $email = parse-email($file,:headers-only);
+ say "{$file} - {$email<headers><Subject>}";
+ }
+ }
+ }
}
- token newline { [\x0d\x0a] | [\x0a\x0d] | \x0a | \x0d }
- token separator {
- [\x0a\x0d\x0a\x0d] | [\x0d\x0a\x0d\x0a] | \x0a ** 2 | \x0d ** 2
- }
- token body { .* }
- regex headers {
- <header>+ % <newline>
- }
- regex header {
- <name> \: \h* <value>
- || <junk>
- }
- token name {
- <-[:\s]>+
- }
- regex value {
- <line>+ % [<newline> \h+]
- }
- token line { \N* }
- token junk { \N+ }
-}
-
-class Message-actions {
- method TOP($/) {
- make %( headers => $/<headers>.made, body => $/<body>.Str );
- }
- method headers($/) {
- make %( |$/<header>».made );
- }
- method header($/) {
- make $/<junk> ?? () !! ( $/<name>.Str => $/<value>.made );
- }
- method value($/) {
- make $/<line>.join(' ')
- }
-}
-
-sub MAIN(*@files) {
- say "Starting";my $start = now;
- my @messages = @files.race(:degree(10) :batch(100)).map({
- my $email = $_.IO.slurp(:enc<utf8-c8>);
- my $match = Message.parse($email,:actions(Message-actions.new));
- });
- say "Took { now - $start } for { +@messages } messages";
+ react whenever signal(SIGINT) { exit }
}
diff --git a/lib/MaildirIndexer/Parser.pm6 b/lib/MaildirIndexer/Parser.pm6
new file mode 100644
index 0000000..90b3678
--- /dev/null
+++ b/lib/MaildirIndexer/Parser.pm6
@@ -0,0 +1,71 @@
+use v6.d.PREVIEW;
+unit module MaildirIndexer::Parser;
+
+my @separators = (
+ "\x0a\x0d\x0a\x0d",
+ "\x0d\x0a\x0d\x0a",
+ "\x0a\x0a",
+ "\x0d\x0d",
+);
+
+grammar Message {
+ regex TOP {
+ <headers>
+ <separator>
+ <body>
+ }
+
+ token newline { [\x0d\x0a] | [\x0a\x0d] | \x0a | \x0d }
+ token separator { @separators }
+
+ token body { .* }
+ regex headers {
+ <header>+ % <newline>
+ }
+ regex header {
+ <name> \: \h* <value>
+ || <junk>
+ }
+ token name {
+ <-[:\s]>+
+ }
+ regex value {
+ <line>+ % [<newline> \h+]
+ }
+ token line { \N* }
+ token junk { \N+ }
+}
+
+class Message-actions {
+ method TOP($/) {
+ make %( headers => $/<headers>.made, body => $/<body>.Str );
+ }
+ method headers($/) {
+ make %( |$/<header>».made );
+ }
+ method header($/) {
+ make $/<junk> ?? () !! ( $/<name>.Str => $/<value>.made );
+ }
+ method value($/) {
+ make $/<line>.join(' ')
+ }
+}
+
+multi parse-email(IO::Path $p) is export {
+ return parse-email($p.slurp(:enc<utf8-c8>));
+}
+multi parse-email(IO::Path $p, :$headers-only!) is export {
+ return parse-email(
+ $p.lines(
+ :enc<utf8-c8>,
+ :nl-in(@separators),
+ :!chomp,
+ )[0],
+ );
+}
+multi parse-email(Str $email-str) is export {
+ with Message.parse($email-str,:actions(Message-actions.new)) {
+ return .made;
+ }
+ return Nil;
+}
diff --git a/lib/MaildirIndexer/ScanDir.pm6 b/lib/MaildirIndexer/ScanDir.pm6
new file mode 100644
index 0000000..d3e5070
--- /dev/null
+++ b/lib/MaildirIndexer/ScanDir.pm6
@@ -0,0 +1,34 @@
+use v6.d.PREVIEW;
+unit module MaildirIndexer::ScanDir;
+
+sub scan-dir(IO() $path --> Supply) is export {
+ supply {
+ my %watched-dirs;
+
+ sub add-dir(IO::Path $dir, :$initial) {
+ %watched-dirs{$dir} = True;
+
+ CATCH { when X::IO::Dir { }; default { .perl.say } }
+
+ whenever $dir.watch {
+ my $path-io = .path.IO;
+ emit $path-io;
+ when $path-io.e && $path-io.d {
+ add-dir($path-io) unless %watched-dirs{$path-io};
+ }
+ when !$path-io.e {
+ %watched-dirs{$path-io}:delete
+ }
+ }
+
+ for $dir.dir {
+ emit $_;
+ when .e && .d {
+ add-dir($_);
+ }
+ }
+ }
+
+ add-dir($path);
+ }
+}