From: Ian Zimmerman Date: Sun, 16 Dec 2018 00:02:48 +0000 (-0800) Subject: contrib: add mairix_filter.pl X-Git-Tag: 2019-10-25~434 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e31a2226216452bf707ec131eb0575a741b565a8;p=neomutt contrib: add mairix_filter.pl Sample implementation of external search filter (mairix) --- diff --git a/contrib/Makefile.autosetup b/contrib/Makefile.autosetup index 15c08a1fd..4f3c0b84e 100644 --- a/contrib/Makefile.autosetup +++ b/contrib/Makefile.autosetup @@ -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 index 000000000..57105ea27 --- /dev/null +++ b/contrib/mairix_filter.pl @@ -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;