File: lr_getbody

package info (click to toggle)
lire 20020214-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,180 kB
  • ctags: 1,245
  • sloc: perl: 11,637; xml: 5,725; sh: 3,458; makefile: 1,008
file content (184 lines) | stat: -rwxr-xr-x 5,443 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#! /bin/sh -e

#
# $Id: lr_getbody,v 1.55 2001/10/27 23:42:20 flacoste Exp $
#
# Copyright (C) 2000-2001 Stichting LogReport Foundation LogReport@LogReport.org
# 
#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.
# 
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
# 
#     You should have received a copy of the GNU General Public License
#     along with this program (see COPYING); if not, check with
#     http://www.gnu.org/copyleft/gpl.html or write to the Free Software 
#     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
#

#
# lr_getbody - get body from mail on stdin to file. echo filename, a space,
#      and sender address to stdout. sender address gets extracted from
#      message headers
#

PROGRAM=lr_getbody

identifier="none"
file=
while getopts :f:i: a
do
    case $a in
        f) file=$OPTARG
            ;;
        i) identifier=$OPTARG
            ;;
        *) echo >&2 "$tag err usage $PROGRAM -f file -i identifier"
           exit 1
            ;;
    esac
done

tag="all all $identifier $PROGRAM"

echo >&2 "$tag info started with '$*'"

if test "$identifier" = "none"
then
    echo >&2 "$tag err aborted, no identifier found (use -i)"
    exit 1
fi

if test ! -f "$file"
then
    echo >&2 "$tag err aborted, no readable inputfile specified (use -f)"
    exit 1
fi

if test -z "$TMPDIR"
then
    echo >&2 "$tag err TMPDIR not set. did you source etc/defaults?"
    exit 1
fi

if test ! -d "$TMPDIR"
then
    if test -f "$TMPDIR"
    then
        echo >&2 "$tag err $TMPDIR is a file, it should be a dir, aborting"
        exit 1
    else
        echo >&2 "$tag info creating $TMPDIR"
        mkdir $TMPDIR
    fi
fi

#
# we try to implement RFC 822, 4.4.4 policy
#

echo >&2 "$tag info extracting sender, running lr_smtpfield"

REPLYTO=`lr_smtpfield Reply-To < $file || true`
FROM=`lr_smtpfield From < $file || true`
SENDER=`lr_smtpfield Sender < $file || true`

SUBJECTFILE=$TMPDIR/${PROGRAM}_${identifier}_$$.subject
if lr_smtpfield Subject < $file > $SUBJECTFILE
then
    echo >&2 "$tag info subjectfile $SUBJECTFILE"
else
    echo >&2 "$tag err cannot run lr_smtpfield Subject < $file > $SUBJECTFILE"
    echo >&2 "$tag notice keeping $SUBJECTFILE for debug"
    exit 1
fi

if [ ! -z "$REPLYTO" ]; then
    SUBMITTER="$REPLYTO"
elif [ ! -z "$FROM" ]; then
    SUBMITTER="$FROM"
elif [ ! -z "$SENDER" ]; then
    SUBMITTER="$SENDER"
else
    echo >&2 "$tag err sender unknown"
    exit 1
fi

# strip leading < and trailing >
# BEWARE! there exist unix vendors who still ship a 
# /bin/sh which
# does not know ${a%.*}. Sun is one of those.
#

# this removes embedded newlines, and leadind and trailing whitespace
# quote single quote
SUBMITTER=`echo $SUBMITTER | sed -e "s|'|'\"'\"'|g"`
echo >&2 "$tag info submitter $SUBMITTER"

# make it possible to eval what we're gonna print to stdout.
# SUBMITTER=`echo $SUBMITTER | sed 's/(/\\\(/g'`
# SUBMITTER=`echo $SUBMITTER | sed 's/)/\\\)/g'`

# Extract the body from the mail message.
# Use munpack if it is a MIME message.
# BUT : We don't use munpack if it is a single part text message
# since munpack doesn't work with single part text message.
CONTENT_TYPE=`lr_smtpfield Content-Type < $file || true`
echo >&2 "$tag info content-type $CONTENT_TYPE"
ENCODING=`lr_smtpfield Content-Transfer-Encoding < $file | grep -i "quoted" || true`
echo >&2 "$tag info content-transfer-encoding $ENCODING"
IS_TEXT=`echo $CONTENT_TYPE | grep -i "text" || true`
if test -n "$ENCODING" || test -n "$CONTENT_TYPE" -a -z "$IS_TEXT" 
then
    echo >&2 "$tag info $file is an encoded MIME message"
    cd $TMPDIR
    echo >&2 "$tag info munpacking $file"
    # FIXME: When there are multiple parts, we only take the 
    # last one. This seems logical, but may not always work.
    # munpack is from mpack ( ftp://ftp.andrew.cmu.edu/pub/mpack/ )
    munpack -t -q $file | 
        while read a b
        do
            extension=`echo $a | sed -e 's/.*\.//'`
            BODYFILE=$TMPDIR/${PROGRAM}_${identifier}_$$.$extension
            mv $TMPDIR/$a $BODYFILE
            echo >&2 "$tag info bodyfile $BODYFILE"
            echo "${PROGRAM}_BODYFILE='$BODYFILE'"
            echo "${PROGRAM}_SUBJECTFILE='$SUBJECTFILE'"
            echo "${PROGRAM}_SUBMITTER='$SUBMITTER'"

            base=`basename $a .$extension`
            rm -f $TMPDIR/$base.desc
        done
else
    echo >&2 "$tag info $file is plain message"
    BODYFILE=$TMPDIR/${PROGRAM}_${identifier}_$$.txt
    body="no"
    cat $file |
        while read line
        do
            if [ -z "$line" ]
            then
                body="yes"
            elif [ "$body" = "yes" ]
            then
                echo $line >> $BODYFILE
            fi
        done

    # just in case body was empty: still create the file
    touch $BODYFILE

    echo "${PROGRAM}_BODYFILE='$BODYFILE'"
    echo "${PROGRAM}_SUBJECTFILE='$SUBJECTFILE'"
    echo "${PROGRAM}_SUBMITTER='$SUBMITTER'"
    echo >&2 "$tag info bodyfile $BODYFILE"
fi

echo >&2 "$tag info stopped"