]> diplodocus.org Git - nmh/blob - etc/rmmproc.messageid
Remove unused NCWD and NPWD #defines.
[nmh] / etc / rmmproc.messageid
1 #! /bin/sh
2 ##
3 # rmmproc.messageid -- tries to back up each message/file to a file
4 # that's 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 messages, the current directory is the message folder
11 # and the script arguments are the message filenames. If called on
12 # files, the arguments are the full paths to the files.
13 #
14 # The backup directory will be:
15 # 1) If input is messages, the folder of the messages.
16 # 2) If input is files, the directories of the files.
17 # 3) If input is standard input (-), the user's MHPATH directory.
18 #
19 # Each backup filename will be:
20 # 1) Message-ID with all / and \ converted to periods.
21 # Message-IDs should not contain \, but some filesystems can't
22 # handle them. Message-IDs should be unique, so there should be
23 # no need to backup a file with the same name, especially if the
24 # result of malicious action. If the backup file already
25 # exists, use 2).
26 # NOTE: conversion of some characters in the filename could
27 # result in an unintended name collision. If that is a concern,
28 # a program that uses mkstemp(3) to create a temporary file might
29 # be the basis for a remedy.
30 # 2) Concatenation of BACKUP_PREFIX and input filename.
31 # NOTE: if a file of that name already exists in the destination
32 # directory, it will be overwritten by the mv below.
33 ##
34
35 for i in "$@"; do
36 if [ "$i" = - ]; then
37 #### Input is stdin; put backup in user's MHPATH directory.
38 dir=`mhparam path`
39 #### If Path is relative, prepend home directory.
40 [ `dirname "$dir"` = . ] && dir="$HOME/$dir"
41 else
42 dir=`dirname "$i"`
43 fi
44
45 #### Extract first Message-Id, remove <>, substitute / and \.
46 messageid=`sed -n '
47 /^$/q; /^[Mm][Ee][Ss][Ss][Aa][Gg][Ee]-[Ii][Dd]:/!d
48 s/>.*//; s/.*<//; y#/\\\#..#; p
49 ' "$i"`
50
51 if [ "$messageid" -a ! -f "$dir/$messageid" ]; then
52 filename="$dir/$messageid"
53 else
54 #### `mhparam sbackup` is usually ',' but depends on configuration.
55 filename="$dir"/`mhparam sbackup``basename "$i"`
56 fi
57
58 mv "$i" "$filename" || status=1
59 done
60
61 exit $status