]> diplodocus.org Git - nmh/blob - etc/rmmproc.messageid
Document argsplit changes in mh-profile man page.
[nmh] / etc / rmmproc.messageid
1 #! /bin/sh
2 ##
3 # rmmproc.messageid -- tries to back up a message/file to a file that's
4 # named based on Message ID
5 #
6 # This code is Copyright (c) 2013, by the authors of nmh.
7 # See the COPYRIGHT file in the root directory of the nmh
8 # distribution for complete copyright information.
9 #
10 # If called on a message, the current directory is the
11 # message folder and $1 is the message filename.
12 # If called on a file, $1 is the full path to the file.
13 #
14 # The backup directory will be:
15 # 1) If input is a message, the folder of the message.
16 # 2) If input is a file, the directory of the file.
17 # 3) If input is standard input (-), the user's MHPATH directory.
18 #
19 # The backup filename will be:
20 # 1) Message-ID with all / and \ converted to periods.
21 # Message-IDs should not contain \, but some
22 # filesystems can't handle them. Message-IDs should
23 # be unique, so there should be no need to backup a
24 # file with the same name, especially if the result of
25 # malicious action. If the backup file already
26 # exists, use 2).
27 # NOTE: conversion of some characters in the filename
28 # could result in an unintended name collision. If
29 # that is a concern, a program that uses mktemp(3) to
30 # create a temporary file might be the basis for a
31 # remedy.
32 #
33 # 2) Concatenation of BACKUP_PREFIX and input filename.
34 # NOTE: if a file of that name already exists in the
35 # destination directory, it will be overwritten by the
36 # mv below.
37 #
38 ##
39
40 if [ "$1" = - ]; then
41 #### Input is stdin; put backup in user's MHPATH directory.
42 dir=`mhparam path`
43 #### If Path is relative, prepend home directory.
44 [ `dirname "$dir"` = . ] && dir="$HOME/$dir"
45 else
46 dir=`dirname "$1"`
47 fi
48
49 #### Extract first Message-Id, remove <>, substitute / and \.
50 messageid=`sed -n '
51 /^$/q; /^[Mm][Ee][Ss][Ss][Aa][Gg][Ee]-[Ii][Dd]:/!d
52 s/>.*//; s/.*<//; y#/\\\#..#; p
53 ' "$1"`
54
55 if [ "$messageid" -a ! -f "$dir/$messageid" ]; then
56 filename="$dir/$messageid"
57 else
58 filename="$dir"/`mhparam sbackup``basename "$1"`
59 fi
60
61 #### If unable to backup the message, exit with status 1.
62 mv "$1" "$filename"