UPDATE:
I've updated the perl script to allow user input of the save hook.
Also importantly I forgot to mention in the article originally that you must add the string '#SAVE AND FCC HOOKS' at the place in your ~/.muttrc file where you want the save hooks to be saved!
This is a recipe for automatically creating save-hooks in the Mutt mail client. The recipe allows you to bind the function to a key - I use 'esc H' - so when you receive mail say from 'munk @ munk.me.uk', you hit 'esc H' and mutt will create a save-hook that saves any mail from that address to 'munk.me.uk'. That way when you hit 's' to save the mail to a folder, mutt will automatically prompt you to save the mail to 'munk.me.uk'.
The recipe involves editing the ~/.muttrc file and the creation of a perl script to do the work. It also requires formail (part of procmail iirc). The script is based on an original script by
Eric Smith on the mutt mailing list.
First add this to your ~/.muttrc file:
CODE:
# Notes:
#
# Modify the paths to the 'hookgen.pl' script as needed
# - it should be the location you decide to save the perl script as.
#
# Modify the temp file path if you want.
# /tmp should be fine though unless you have a busy server and don't want to
# risk others seeing who you receive mail from.
#
# The file after the ':source' command should be the file that you want to contain the save hooks in.
# In my setup I have all my hooks in a separate file, ~/.mutt/mutt.hooks, which is sourced from my main
# ~/.muttrc file.
#
# There are 3 bindings, one for each mutt 'view':
macro index \eH "<pipe-message>formail -X \
From:>/tmp/hookgen.pl_cache\n<shell-escape>/home/munk/bin/perl/mutt/hookgen.pl\n:source \
~/.mutt/mutt.hooks\n" "Edit mutt hooks file and reload it"
macro pager \eH \
"<pipe-message>formail -X \
From:>/tmp/hookgen.pl_cache\n<shell-escape>/home/munk/bin/perl/mutt/hookgen.pl\n:source \
~/.mutt/mutt.hooks\n" "Edit mutt hooks file and reload it"
This presumes you're using the key sequence 'ESC H' - the escape key followed by a capital H - to bind the function. Feel free to use another keystroke.
At the point in your ~/.muttrc file that you want the save-hook saving, add the following:
CODE:
'#SAVE AND FCC HOOKS'
Now create and save the following perl script for the above macro binding to use. Please note I didn't originally write this script, I based it on a script by Eric Smith in the URL below. You might want to read the original mail because he uses a slightly different approach. You can debug the script on the commandline by uncommenting the line starting '#$debug'.
Here's the script:
CODE:
#!/usr/bin/perl -w
# Original idea with thanks to Eric Smith.
# see here: http://marc.10east.com/?l=mutt-users&m=104422326212956&w=2
# Overview:
# This script allows you to create save hooks in mutt. Add the following to
# your ~/.muttrc file:
#
# macro index \eH "<pipe-message>formail -X From:>/tmp/hookgen.pl_cache\n \
# <shell-escape>/home/munk/bin/perl/mutt/hookgen.pl\n:source ~/.mutt/mutt.hooks\n" \
# "Edit mutt hooks file and reload it"
# macro pager \eH # "<pipe-message>formail -X # From:>/tmp/hookgen.pl_cache\n \
# <shell-escape>/home/munk/bin/perl/mutt/hookgen.pl\n:source ~/.mutt/mutt.hooks\n" \
# "Edit mutt hooks file and reload it"
#
# substituting:
# /tmp/hookgen.pl_cache for the location you want to store the 'From' header
# temporarily
#
# /home/munk/bin/perl/hookgen.pl for the location of this script.
#
# ~/.mutt/mutt.hooks for the file you want to save the hooks into, normally this will be your ~/.muttrc
#
# IMPORTANTLY: add the following in the file you want to save the hooks to:
# '#SAVE AND FCC HOOKS' (everything inside the quotes)
use strict;
my $cmd = undef;
my $regexp = undef;
my $folder = undef;
my $alt_hook = undef;
my $domain = undef;
my $save_hook = undef;
my $debug = undef;
# set this to the name of the temp file used in the muttrc bind directive for
# this macro:
my $tmp_file = '/tmp/hookgen.pl_cache';
# set this to the file you want to save the hooks to - make sure the hook file
# has the text '#SAVE AND FCC HOOKS' in it!:
my $hook_file = '/home/munk/.mutt/mutt.hooks';
# To debug the script, uncomment the following line:
# $debug = 'From: "John Smith" <john.smith@example.com>';
`echo '$debug' > $tmp_file` if $debug;
open CACHE, "</tmp/hookgen.pl_cache" or die "Cannot open /tmp/hookgen.pl_cache $!";
while (<CACHE>) {
if(/^from:/i){
chomp;
/.*\@([^>]*)/i;
$domain = $1;
}
}
$regexp = $folder = $domain;
close CACHE;
$save_hook="save-hook '~f $regexp' +/$folder";
while(){
print "Save hook to be added:\n\n$save_hook\n\n";
print "Hit enter to save, enter an alternative save-hook, or hit ctrl-c to exit:\n";
chomp($alt_hook = <STDIN>);
if ($alt_hook eq ""){
last;
} else {
$save_hook = $alt_hook ;
}
}
$cmd = 'perl -pi -e "s@(?<=#SAVE AND FCC HOOKS)@\n' . $save_hook . '@" ' . $hook_file;
if($debug){
print "Running in debug mode:\n\n";
print "Would have saved this hook:\n\n$save_hook\n\n";
print "to this hook file:\n\n$hook_file\n";
print "Using this command:\n\n$cmd\n";
} else {
system qq|$cmd|;
}