#!/usr/bin/perl
######################################################################
# mutt2org.pl
#
# Read an email on STDIN and fetch headers to create an org-mode
# link which executes mairix to find the correct message.
#
# Later when the link is visited, mairix will find the message by id.
# Then the user can goto their search folder in mutt to view it.
#
# Licensed GPL v2: Russell Adams
#
# The following are excerpts from .muttrc to speed up mairix related
# activities.
#
# Given that the mairix folder is "=._Search/", the following allows
# M-` to jump directly to the search folder.
#
# ~/.muttrc:
# # Quick access to mairix search folder
# macro pager \e\` "=._Search/\n"
# macro index \e\` "=._Search/\n"
#
# Bind M-o to this perl script without pause in the index and pager
# allowing for quick paste into Org.
#
# ~/.muttrc:
# macro index \eo "\
# unset wait_key\n\
# ~/.dotfiles/mutt/mutt2org.pl\n\
# set wait_key\n\
# " "Create Org link in X clipboard"
#
# macro pager \eo "\
# unset wait_key\n\
# ~/.dotfiles/mutt/mutt2org.pl\n\
# set wait_key\n\
# " "Create Org link in X clipboard"
#
######################################################################
use strict;
use warnings;
use 5.010;
my ( $Subject , $From , $MID );
while () {
chomp;
given ($_) {
when (/^Subject: /) { ( $Subject ) = $_ =~ m/^Subject: (.*)$/; };
when (/^From: /) { ( $From ) = $_ =~ m/^From: (.*)$/; };
when (/^Message-ID:\s*/) { ( $MID ) = $_ =~ m/^Message-ID:\s*<(.*)>\s*$/; };
when (/^$/) { break; }; # Header ends on first blank line
}
};
# Compose link
my $Link = "[[shell:mairix \"m:$MID\"][$From / $Subject]]";
# Output to xclip, avoiding shell escape headaches with exec.
open(PIPE, "|/usr/bin/xclip");
say PIPE "$Link";
close(PIPE);