summaryrefslogtreecommitdiff
path: root/lib
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 /lib
parentlet's use Perl 6! (diff)
downloadMaildirIndexer-db72db7fd0c10131dfc3c4a49e3e2c77aae96b25.tar.gz
MaildirIndexer-db72db7fd0c10131dfc3c4a49e3e2c77aae96b25.tar.bz2
MaildirIndexer-db72db7fd0c10131dfc3c4a49e3e2c77aae96b25.zip
move stuff into modules
Diffstat (limited to 'lib')
-rw-r--r--lib/MaildirIndexer/Parser.pm671
-rw-r--r--lib/MaildirIndexer/ScanDir.pm634
2 files changed, 105 insertions, 0 deletions
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);
+ }
+}