Skip to content

Instantly share code, notes, and snippets.

@TauPan
Created April 30, 2013 12:40
Show Gist options
  • Save TauPan/5488456 to your computer and use it in GitHub Desktop.
Save TauPan/5488456 to your computer and use it in GitHub Desktop.
mutt2remember.pl a script to help integration of emacs-org-mode and mutt
#!/usr/bin/perl
# $Id: mutt2remember.pl,v 1.5 2008/09/27 11:25:41 friedel Exp friedel $
# Variations on a theme given by Russell Adams http://lists.gnu.org/archive/html/emacs-orgmode/2008-09/msg00300.html
$ENV{TMPDIR} = $ENV{TMP};
# Install:
# ========
# 1.) put the following in your muttrc:
# folder-hook <<folder-pattern> `mutt2remember.pl folder-hook "<<archive-folder>>" "<<from-address>>" "<<signature-file>>"`
# for any folder you want to configure... of course the simplest base-use-case would be:
# folder-hook . `mutt2remember.pl folder-hook "\$record" "\$from" ~/.signature`
# This needs to be last, if you have multiple folder hooks like this.
# mutt2remember.pl will try its best to correctly quote the arguments for your muttrc
# but I don't really get mutt's quoting rules,
# see "2. Syntax of Initialization Files" in manual.txt.
# 2) put this file into your $HOME/bin and make it executable.
# 3) make sure org-protocol.el is loaded in your org config
# 4) press <f9>n in the pager or index to annotate a mail url,
# press <f9>t to *remember* it
# 5) follow the generated link in emacs to open the mail in mutt.
use strict;
use warnings;
use URI::Escape qw/ uri_escape uri_escape_utf8 /;
use File::Temp qw/ mkstemp /;
use Encode qw/encode decode/;
my $action=$ARGV[0];
my $folder=$ARGV[1];
if ($action eq "capture" or $action eq "remember" or $action eq "store-link") {
my ( $Subject , $From , $MID );
while (<STDIN>) {
chomp;
if (/^Subject: /i) {
( $Subject ) = $_ =~ m/^Subject: (.*)$/;
}
if (/^From: /i) {
( $From ) = $_ =~ m/^From: (.*)$/;
}
if (/^Message-ID:\s*/i) {
( $MID ) = $_ =~ m/^Message-ID:\s*<(.*)>\s*$/i;
}
if (/^$/) {
last; # Header ends on first blank line
}
}
$From = uri_escape_utf8(decode('MIME-Header', $From));
$Subject = uri_escape_utf8(decode('MIME-Header', $Subject));
$folder =~ tr/=/+/;
my $uri = "mutt:"
. $folder
. " "
. $MID;
$uri = uri_escape_utf8($uri);
my $key = "";
if ($action eq "capture" or $action eq "remember") {
$key ="/t/";
}
my $Link = "org-protocol://" . $action . "://" . $key . $uri . "/Mail From $From: $Subject";
system ("em", $Link);
} elsif ($action eq "open") {
my @args;
if ($#ARGV < 2) {
# The open command gives us all arguments in one, since org-links
# just take on argument. This is why we have to split off the last
# portion as message-id and assume the previous part is the folder.
# This way we can handle google mail's "All Mails" folder correctly.
@args=split(' ', $ARGV[1]);
} else {
@args = @ARGV[1..$#ARGV];
}
$folder=join(' ', @args[0..($#args - 1)]);
my $msgid=$args[$#args];
my ($tmp, $tmpfile) = mkstemp(($ENV{TMP} or "/tmp") . "/mutt2rememberXXXXXXXX");
$msgid = quotemeta($msgid); # escape regex meta characters, e.g. outlook uses '$'
$msgid =~ s/\\/\\\\/g; # mutt needs double backslashes
printf $tmp "push \"<search>~i\'%s\'<enter><display-message>\"", $msgid;
flush $tmp;
close $tmp;
system("mutt", "-f", $folder, "-e", "source $tmpfile");
close $tmp;
unlink $tmpfile;
} elsif ($action eq "notmuch") {
my $MID;
while (<STDIN>) {
chomp;
if (/^Message-ID:\s*/i) {
( $MID ) = $_ =~ m/^Message-ID:\s*<(.*)>\s*$/i;
}
}
system ("em", "-e", "(org-notmuch-open \"id:$MID\")")
} elsif ($action eq "folder-hook") {
my $fromaddress = $ARGV[2];
my $sigfile = $ARGV[3];
printf '"set my_from=\"%s\"; ' .
'set signature=\"%s\"; ' .
'set from=\"\$my_from\"; ' .
'set my_archive_folder=\"%s\"; ' .
'set record=\"\$my_archive_folder\"; ' .
'set my_pipe_decode=\$pipe_decode; ' .
'set my_wait_key=\$wait_key; ' .
'macro index,pager <f9>t \"' .
'<enter-command>set pipe_decode=no; set wait_key=no<enter>' .
'<pipe-message>mutt2remember.pl capture \\\\\\"\$my_archive_folder\\\\\\"<enter>' .
'<enter-command>set wait_key=\$my_wait_key ; set pipe_decode=\$my_pipe_decode<enter>\" ' .
'\'capture mail in emacs\'; ' .
'macro index,pager <f9>n \"<enter-command>set pipe_decode=no; set wait_key=no<enter><pipe-message>mutt2remember.pl store-link \\\\\\"\$my_archive_folder\\\\\\"<enter><enter-command>set wait_key=\$my_wait_key ; set pipe_decode=\$my_pipe_decode<enter>\" \'copy url to mail in emacs\'; ' .
'macro index,pager <f9>a <change-folder><<enter>; ' .
'save-hook . \$my_archive_folder"',$fromaddress, $sigfile, $folder;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment