Subject: Re: Subject as Foldername

From: [email-masked] (Hans-Albert Schneider)
Date: Fri, 27 May 1994 17:12:10 +0200

On May 25,  2:50pm, David Barron wrote
on subject "Subject as Foldername"
} X-Mailing-List: <[email-masked]> archive/latest/1420
} 
} I've been asking this question all over, now it's your turn.  I'd like 
} to find a way to use part of the Subject as a Folder name.  I've worked 
} out how to pull out the part I need, but am having no luck with the 
} proper recipe.
} 
} Example:
} If I get mail with this condition, Subject: Tracking Entry 4187575
} I would like it to go to $MAILDIR/Track/4187575
} 
} I want to be able to pull out the last part of the subject and use it as 
} the foldername.

Well, my solution is not *exactly* what you requested, but the
result is the same. I am subscribed to several mailing lists that
number their mails. And I wanted procmail to sort these Mails to
special directories (each message with its sequence number as the
filename) so they do not fill my mailbox, and I can read them
when I have time to do so.

To achieve this, I use a shell script that gets the header from
which it should extract the number (in case of the procmail list,
this would be 'X-Mailing-List:'), and a directory where the mail
should be stored by the number that terminates that header line.
It prints the name of the resulting file to stdout where the
recipe catches it for the log file.  The script is called by the
following recipe:

:
^X-Mailing-List:.*procmail
{
	:0
	last=| storeListMail LISTS/Procmail/New X-Mailing-List:

	# Arguments to storeListMail are:
	# 1 - Target directory (absolute pathname or relative to
	# current, i.e., $MAILDIR)
	# 2 - Name of header field that gives the sequence
	# number. The number must be the last thing on that line.
	# If this does not fit exactly (e.g., not always a
	# number), then try to fiddle with the line reading
	# 'number=`formail ...'.

	LOG="moved to $last
"

	:0
	/dev/null
}

(This stored your question to $MAILDIR/LISTS/Procmail/New/1420.)
Using the above recipe with

	last=| storeListMail Track Subject:

and appropriate pattern(s) should do what you want.

While we are at it: A bit more complicated is the SunFlash list.
This I want to be stored by volume and issue:

:
^TOsunflash
{
	:0
	last=|storeVolumeIssue Sunflash \
		'Subject: SunFlash: \([0-9]*\)\..*' \
		'Subject: SunFlash: [0-9]*\.\([0-9]*\).*'

	# Arguments to storeVolumeIssue are:
	# 1 - Target directory (absolute pathname or relative to
	# current, i.e., $MAILDIR)
	# 2 - Pattern specifying the volume
	# 3 - Pattern specifying the issue
	# The patterns are used with sed -n -e "s/$PATTERN/\1/p".

	LOG="moved to $last
"

	:0
	/dev/null
}

I have appended these two shell scripts as a SHAR archive. I
think I have mailed them before; maybe they are something for the
goodie colletion somebody suggested to be established under the
procmail archive server.

The first argument to both scripts is the directory where the
message should be stored (say DIR). storeListMail stores to
$MAILDIR/DIR/number, whereas storeVolumeIssue stores to
$MAILDIR/DIR/volume/issue.

Here are the scripts:

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then feed it
# into a shell via "sh file" or similar.  To overwrite existing files,
# type "sh file -c".
# The tool that generated this appeared in the comp.sources.unix newsgroup;
# send mail to [email-masked] if you want that tool.
# Contents:  storeListMail storeVolumeIssue
# Wrapped by has@borel on Fri May 27 16:22:49 1994
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
echo If this archive is complete, you will see the following message:
echo '          "shar: End of archive."'
if test -f 'storeListMail' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'storeListMail'\"
else
  echo shar: Extracting \"'storeListMail'\" \(557 characters\)
  sed "s/^X//" >'storeListMail' <<'END_OF_FILE'
X#!/bin/sh
X
X# Store a Mail message coming through a list
X# that has assigned a number to it by that number.
X
X# Arguments:
X#	Directory to store the mails in:
Xdirectory=$1
X#	Which header line contains the number?
Xheader=$2
X
X
Xtmp=$HOME/tmp/mail.$$
X
Xcat - > $tmp
X
Xnumber=`formail < $tmp -x "$header" | sed -e 's/.*[^0-9]\([0-9]*\)$/\1/'`
X
Xif [ -z "$number" ]
Xthen
X	number=$$
X	echo "$$: no number found -- using $number" >&2
Xfi
X
Xfile=$directory/$number
Xif [ -f $file ]
Xthen
X	echo "$$: $file already exists" >&2
X	file=$file.$$
Xfi
X
Xmv $tmp $file
Xecho $file
X
Xexit 0
END_OF_FILE
  if test 557 -ne `wc -c <'storeListMail'`; then
    echo shar: \"'storeListMail'\" unpacked with wrong size!
  fi
  chmod +x 'storeListMail'
  # end of 'storeListMail'
fi
if test -f 'storeVolumeIssue' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'storeVolumeIssue'\"
else
  echo shar: Extracting \"'storeVolumeIssue'\" \(901 characters\)
  sed "s/^X//" >'storeVolumeIssue' <<'END_OF_FILE'
X#!/bin/sh
X
X# Store a mail message having volume/isuue numbers by those numbers.
X
X# Arguments:
X#	Directory to store the mails in:
Xdirectory=$1
X#	Patterns to find out volume and issue numbers. Will be fed to sed.
Xvol_pat=$2
Xiss_pat=$3
X
Xtmp=$HOME/tmp/mail.$$
X
Xtee $tmp | sed -e "1,/^\$/ w $tmp.hdr" > /dev/null
X
X# echo "Volume matched by: $vol_pat" >&2
X# echo "Issue  matched by: $iss_pat" >&2
X
Xvolume=`sed -n -e "s/$vol_pat/\1/p" < $tmp.hdr`
Xissue=`sed -n -e "s/$iss_pat/\1/p" < $tmp.hdr`
X
Xif [ -z "$volume" ]
Xthen
X	volume=novol
X	echo "$$: no volume number found -- using $volume" >&2
Xfi
X
Xif [ -z "$issue" ]
Xthen
X	issue=$$
X	echo "$$: no issue number found -- using $issue" >&2
Xfi
X
Xif [ ! -d $directory/$volume ]
Xthen
X	mkdir -p $directory/$volume
Xfi
X
Xfile=$directory/$volume/$issue
Xif [ -f $file ]
Xthen
X	echo "$$: $file already exists" >&2
X	file=$file.$$
Xfi
X
Xmv $tmp $file
Xecho $file
X
Xrm $tmp.hdr
Xexit 0
END_OF_FILE
  if test 901 -ne `wc -c <'storeVolumeIssue'`; then
    echo shar: \"'storeVolumeIssue'\" unpacked with wrong size!
  fi
  chmod +x 'storeVolumeIssue'
  # end of 'storeVolumeIssue'
fi
echo shar: End of archive.
exit 0

--
Hans-Albert Schneider		<[email-masked]>
Siemens AG				phone:	(+49) 89 636 45445
Corporate Research and Development	fax:	(+49) 89 636 42284
Munich, Germany
 -- To get my PGP key, send me a mail with subject "send key" --