-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshow_from
executable file
·121 lines (105 loc) · 3.52 KB
/
show_from
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
#!/bin/sh
## $Id$
##
progname=`basename "$0"`
flag_debug=0
help(){
echo "usage: ${progname} messageID"
}
## input: encoded string, e.g., "=?iso-2022-jp?B?.....?="
## output: decoded string
## iso-8859-12 is officially abandoned (see http://en.wikipedia.org/wiki/ISO/IEC_8859)
decode(){
charset=
BorQ="B"
content=
charset=`echo "$1" | sed 's/^\([^?][^?]*\)?\(.\)?\(.*\)$/\1/'`
BorQ=`echo "$1" | sed 's/^\([^?][^?]*\)?\(.\)?\(.*\)$/\2/' | tr 'BQ' 'bq'`
content=`echo "$1" | sed 's/^\([^?][^?]*\)?\(.\)?\(.*\)$/\3/'`
if [ ${flag_debug} -eq 1 ]; then
(echo "decode: charset=$charset" 1>&2 )
(echo "decode: BorQ=$BorQ" 1>&2 )
(echo "decode: content=$content" 1>&2)
fi
if [ "${charset}" = "iso-2022-jp" -a "${BorQ}" = "b" ]; then
echo "${content}" | base64 -d | iconv -f iso-2022-jp-2 -t "${MM_CHARSET}" -c
elif [ "${charset}" = "iso-2022-jp" -a "${BorQ}" = "q" ]; then
echo "${content}" | qprint -d | iconv -f iso-2022-jp-2 -t "${MM_CHARSET}" -c
elif [ "${BorQ}" = "b" ]; then
echo "${content}" | base64 -d | iconv -f "${charset}" -t "${MM_CHARSET}" -c
elif [ "${BorQ}" = "q" ]; then
echo "${content}" | qprint -d | iconv -f "${charset}" -t "${MM_CHARSET}" -c
fi
return 0
}
## main
case "$1" in
-h*)
help
exit 0
break
;;
-debug)
flag_debug=1
;;
esac
TMPFILE1=`mktemp /tmp/XXXXXXXXXX` || exit 10
TMPFILE2=`mktemp /tmp/XXXXXXXXXX` || exit 20
sed '/^$/,$d' | sed -n '/^[fF][rR][oO][mM]:/,$p' > "${TMPFILE1}"
FIRST=`head -1 "${TMPFILE1}" | sed 's/^[fF][rR][oO][mM]:[ ]*//'`
REMAIN=`tail -n +2 < "${TMPFILE1}" | sed '/^[^ ]/,$d' | sed 's/^[ ]*//'`
echo "${FIRST}${REMAIN}" | tr -d '\n' > "${TMPFILE1}"
if [ ${flag_debug} -eq 1 ]; then
cat "${TMPFILE1}" ; echo "\n****************"
fi
cat "${TMPFILE1}" | sed 's/ /\\040/g' |\
sed -e 's/=?[iI][sS][oO]-2022-[jJ][pP]?\([bBqQ]\)?\([^?][^?]*\)?=/\
ENCODED: iso-2022-jp?\1?\2\
/g' \
-e 's/=?[eE][uU][cC][-_][jJ][pP]?\([bBqQ]\)?\([^?][^?]*\)?=/\
ENCODED: euc-jp?\1?\2\
/g' \
-e 's/=?[sS][hH][iI][fF][tT][-_][jJ][iI][sS]?\([bBqQ]\)?\([^?][^?]*\)?=/\
ENCODED: shift-jis?\1?\2\
/g' \
-e 's/=?[uU][tT][fF]-8?\([bBqQ]\)?\([^?][^?]*\)?=/\
ENCODED: utf-8?\1?\2\
/g' \
-e 's/=?[gG][bB]2312?\([bBqQ]\)?\([^?][^?]*\)?=/\
ENCODED: gb2312?\1?\2\
/g' \
-e 's/=?[eE][uU][cC][-_][kK][rR]?\([bBqQ]\)?\([^?][^?]*\)?=/\
ENCODED: euc-kr?\1?\2\
/g' \
-e 's/=?[iI][sS][oO][-_]8859[-_]\([1-9][0-5]*\)?\([bBqQ]\)?\([^?][^?]*\)?=/\
ENCODED: iso-8859-\1?\2?\3\
/g' \
-e 's/=?[wW][iI][nN][dD][oO][wW][sS][-_]\([0-9][0-9][0-9][0-9]\)?\([bBqQ]\)?\([^?][^?]*\)?=/\
ENCODED: windows-\1?\2?\3\
/g' \
-e 's/=?[cC][pP]\([0-9][0-9][0-9][0-9]\)?\([bBqQ]\)?\([^?][^?]*\)?=/\
ENCODED: cp\1?\2?\3\
/g' \
-e 's/=?[kK][oO][iI]8[-_][r]?\([bBqQ]\)?\([^?][^?]*\)?=/\
ENCODED: koi8-r?\1?\2\
/g' \
-e 's/=?[uU][sS][-_][aA][sS][cC][iI][iI]?\([bBqQ]\)?\([^?][^?]*\)?=/\
ENCODED: us-ascii?\1?\2\
/g' \
> "${TMPFILE2}"
echo "" >> "${TMPFILE2}"
LINE=
cat "${TMPFILE2}" |\
while read LINE; do
if [ ${flag_debug} -eq 1 ]; then
(echo "==>${LINE}<==" 1>&2)
fi
(echo "${LINE}" | egrep -q '^ENCODED: ') \
&& (arg=`echo "${LINE}" | sed 's/^ENCODED: *//'` && decode "$arg" ) \
|| (echo "${LINE}" | sed 's/\040/ /g')
done | tr -d '\n'
if [ ${flag_debug} -eq 1 ]; then
echo "working files ${TMPFILE1} and ${TMPFILE2} left un-removed..." 1>&2
else
rm "${TMPFILE1}" "${TMPFILE2}"
fi