]> granicus.if.org Git - neomutt/commitdiff
contrib: add mairix_filter.pl
authorIan Zimmerman <itz@no-use.mooo.com>
Sun, 16 Dec 2018 00:02:48 +0000 (16:02 -0800)
committerRichard Russon <rich@flatcap.org>
Sun, 16 Dec 2018 14:16:34 +0000 (14:16 +0000)
Sample implementation of external search filter (mairix)

contrib/Makefile.autosetup
contrib/mairix_filter.pl [new file with mode: 0755]

index 15c08a1fd92ed8b7e65df5e419c8cfb1e69bcebe..4f3c0b84e03cedfd4e61fdb72b7f1b8dad41a6bf 100644 (file)
@@ -1,6 +1,6 @@
 SAMPLES=       colors.default colors.linux gpg.rc Mush.rc Pine.rc \
                sample.mailcap sample.neomuttrc sample.neomuttrc-tlr smime.rc \
-               smime_keys_test.pl Tin.rc
+               smime_keys_test.pl Tin.rc mairix_filter.pl
 
 CONTRIB_DIRS=  colorschemes hcache-bench keybase logo lua vim-keys
 
diff --git a/contrib/mairix_filter.pl b/contrib/mairix_filter.pl
new file mode 100755 (executable)
index 0000000..57105ea
--- /dev/null
@@ -0,0 +1,44 @@
+#! /usr/bin/env perl
+
+use strict;
+
+use Cwd qw(abs_path);
+
+my $indexer = $ENV{'MAIL_INDEXER'} || 'mairix';
+my $folder = shift(@ARGV) || '/';
+my $folder_dir = ($folder eq '/' ? '/' abs_path($folder) . '/');
+
+sub starts_with {
+  my ($hay, $needle) = @_;
+  my $needlen = length($needle);
+  return (substr($hay, 0, $needlen) eq $needle);
+}
+
+sub snag_msgid {
+  open (my $mail, "<$_[0]");
+  if (!$mail) {
+    warn("failed to open $_[0]: $!");
+    return undef;
+  }
+  my $msgid = undef;
+ HEADERS:
+  for (<$mail>) {
+    chomp; last HEADERS unless $_;
+    $msgid = $1, last HEADERS if m(^message-id:(.*))i;
+  }
+  $mail->close;
+  return $msgid;
+}
+
+open(my $fh, "$indexer " . join(' ', @ARGV) . '|')
+  or die("failed to run $indexer: $!");
+LINES:
+for (<$fh>) {
+  chomp;
+  my $realmsg = abs_path($_);
+  next LINES unless starts_with($realmsg, $folder_dir);
+  my $msgid = snag_msgid($realmsg);
+  printf("%s\n", $msgid) if $msgid;
+}
+my $closed = $fh->close;
+die("failed to close pipe from $indexer: $!") unless $closed || $! == 0;