File: pm-jamime-decode.rc

package info (click to toggle)
procmail-lib 1%3A2009.1202-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 2,372 kB
  • ctags: 541
  • sloc: perl: 294; makefile: 196; sh: 4
file content (290 lines) | stat: -rw-r--r-- 9,129 bytes parent folder | download | duplicates (5)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# pm-jamime-decode.rc -- decode MIME body contents; quoted-printable, base64
#
#   File id
#
#       Copyright (C) 1997-2010 Jari Aalto
#
#       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 at
#       <http://www.gnu.org/copyleft/gpl.html>.
#
#   Documentation
#
#       The original father of the decoding scheme used here was
#       presented by Peter Galbraith <galbraith@mixing.qc.dfo.ca> in
#       procmail mailing list somewhere at the end of 1997.
#
#       This subroutine supposes that the header has MIME header
#       Content-Type: text/plain and performs quoted-printable or
#       base64 decoding on the whole message. Note, that if you
#       receive messages that have many mime attachments, then this
#       recipe is not suitable for it.
#
#       Procmail is *not* designed to handle mime attachments and this
#       recipe only applies to whole _body_.
#
#       The `pm-jamime-*.rc' is really stretching the limits and any
#       serious work should be delegated to other tools. Alternatives:
#
#       o   A Perl MIME module which will allow you to manipulate MIME
#           body parts rather elegantly. See
#           http://www.perl.com/CPAN-local/authors/Eryq/ for MIME-tools.
#       o   mimedecode at ftp://ftp.dde.dk/pub/mimedecode.c
#
#   Notes
#
#       Perl or Python is not used, because both are CPU intensive. It
#       would be too expansive for accounts or environments receiving
#       hundreds of mails per day (like from several mailing lists).
#
#       RFC 2047 gives possiblity to use MIME iso-8859-1 extensions
#       for mail headers.
#
#           Subject: Re: [PIC]: RSA =?iso-8859-1?Q?encryption=B7=B7?=
#           Subject: =?iso-8859-1?Q?=5BEE=5D:TV_&_video_IC=B4s_!!?=
#
#       There is also base64 possibility (although rare):
#
#           Subject: =?iso-8859-1?B?zvLi5fI6ICAgICAgTVBMQUIzLjQw?=
#
#       In worst possible case there is even multiple ISO encoded
#       strings in subject. Yes, this is valid, the continued line
#       includes spaces at front to keep it with original just like
#       in `Received:' headers. This subroutine will not touch headers
#       that have multiple ISO tags - procmail is too limited for that.
#
#           Subject: AW: Re: AW: neue =3D?ISO-8859-1?Q?M=3DF6glichkeiten_=3D28was_=3D=C4hn?=3D
#              =3D?ISO-8859-1?Q?lichkeiten_von_=3DDCbungen=3D29?=3D
#
#   Required settings
#
#       Variable `PMSRC' must point to source directory of procmail code.
#       This subroutine will include
#
#       o   pm-javar.rc, pm-jamime.rc
#       o   Programs `$MIME_BIN', `$MIME_BIN_QP' and `$MIME_BIN_64'
#           must have been installed (see pm-javar.rc).
#
#   Call arguments (variables to set before calling)
#
#       o   `JA_MIME_DECODE_TREAT_SUBJECT', default "yes". Decode
#           Subject header by removing mime.
#       o   `JA_MIME_DECODE_TREAT_FROM', default "no". Decode
#           From header by removing mime.
#       o   `JA_MIME_DECODE_TREAT_BODY', default "no". Decode
#           body of message by removing quoted-printable from a
#           message that contains only one part. Messages with multiple
#           parts are not handled.
#
#   Return values
#
#       o    `PM_JAMIME_COMPLEX_SUBJECT' is set to "yes".
#            This flag is set to indicate that some other program
#            should handle the message. If Subject header contains ISO
#            encoding several times, it cannot be handled by this module.
#
#   Examples
#
#       Instead of testing the existence of text/plain in the body,
#       you can force decoding by settings JA_MIME_DECODE_REGEXP to
#       ".*".
#
#           RC_MIME_DECODE = $PMSRC/pm-jamime-decode.rc
#
#           :0
#           * condition
#           {
#               JA_MIME_DECODE_REGEXP = ".*"
#           }
#
#           INCLUDERC = $RC_MIME_DECODE     # call subroutine.
#
#
#   Change Log (none)


dummy = "
========================================================================
pm-jamime-decode.rc: init:
"

:0
* !  WSPC ?? [ ]
{
    INCLUDERC = $PMSRC/pm-javar.rc
}

:0
* !  MIME_VER ?? [0-9]
{
    INCLUDERC = $PMSRC/pm-jamime.rc
}

# .......................................................... &public ...
# User configurable sections

JA_MIME_DECODE_TREAT_SUBJECT = "yes"   # Set to "no"  to disable
JA_MIME_DECODE_TREAT_FROM    = "no"    # Set to "yes" to enable
JA_MIME_DECODE_TREAT_BODY    = "no"    # Set to "yes" to enable

JA_MIME_DECODE_REGEXP = ${JA_MIME_DECODE_REGEXP:-\
"^Content-Type: *text/plain"}

# ........................................................... &do-it ...
#   Run conversion if it was quoted printable.
#   Also reflect correct MIME header

JA_MIME_DECODE_HEADER = ${JA_MIME_DECODE_HEADER:-\
"X-Mime-Header-Decoded"}

dummy = "pm-jamime-decode.rc: handle quoted printable"

PM_JAMIME_COMPLEX_SUBJECT       # Return value if too complex subject

jamimeHandledSubject = "no"

#  Kill variables
jamimeSubject
jamimeSubjectRest

:0
*  JA_MIME_DECODE_TREAT_SUBJECT ?? yes
* ^Subject: \/.*
{
    jamimeSubject = $MATCH
    jamimeISO     = "\?iso-8859-[1-9]\?[QB]\?.+\?="

    :0
    *$ jamimeSubject ?? $jamimeISO()\/.*
    {
        #  ".*" is actually minimum match. See pm-doc.sf.net
        jamimeSubjectRest = $MATCH
    }

    :0
    *$ jamimeSubjectRest ?? $jamimeISO()
    {
        #  This Subejct line contains ISO encoding several times.
        #  This is out of our league. Can't hanle it, so quit.

        jamimeSubject
        JAMIME_COMPLEX_SUBJECT = "yes"
    }
}

:0
* ! jamimeSubject ?? ^^^^
{
    str     = $jamimeSubject
    decoder = $MIME_BIN_QP
    type    = "quoted-printable"

    :0
    * ^Subject:.*\?B\?
    {
        type    = "base64"
        decoder = $MIME_BIN_64
    }

   #   NOTE "?" is not wildcard "as is" in sed.

    clean =                                                         \
    `   echo "$str"                                                 \
        | $SED                                                      \
          -e 's/^\(.*\)?\(iso\|ISO\)-\{0,1\}8859-[1-9]?[QB]?\(.*[^?]\)?=/\1\2/g'   \
          -e 's/=0D//g'                                             \
          -e 's/=0A//g'                                             \
          -e 's/=B7//g'                                             \
          -e 's/_/ /g'                                              \
        | $decoder
    `

    handledSubject = "yes"

    :0 fhw
    * ! clean ?? ^^^^
    | $FORMAIL                                                      \
        -i "Subject: $clean"                                        \
        -I "${JA_MIME_DECODE_HEADER}-Subject: $type"
}

#  Too bad Procmail does not have subroutines. This recipe is
#  identical to "Subject" check above.
#
#  Note, that 'From' header is different from the 'Subject' header
#  in respect to the ISO encoding. Look closely where '?=' ends:
#
#    Subject: =?ISO-8859-1?Q?=C4hnlichkeiten_von_=DCbungen?=
#    From: =?ISO-8859-1?Q?Holger_Hoffst=E4tte?= <holger@example.com>

jamimeHandledFrom = "no"

:0
*  JA_MIME_DECODE_TREAT_FROM ?? yes
*$ ^From: +()\/=\?iso-8859-[1-9]\?[QB]\?.+\?=.*
*$ ^From: +()\/.*=\?.+[>]
{
    str     = $MATCH
    decoder = $MIME_BIN_QP
    type    = "quoted-printable"

    :0
    * ^Subject:.*\?B\?
    {
        type    = "base64"
        decoder = $MIME_BIN_64
    }

    clean =                                                         \
    `   echo "$str"                                                 \
        | $SED                                                      \
          -e 's/=?\(iso\|ISO\)-\{0,1\}8859-[1-9]?[QB]?\(.*[^?]\)?=/\2/g'   \
          -e 's/=0D//g'                                             \
          -e 's/=0A//g'                                             \
          -e 's/=B7//g'                                             \
        | $decoder
    `

    jamimeHandledFrom = "yes"

    #  Make sure there is <send@example.com> before replacing anything

    :0 fhw
    * clean ?? [>]
    | $FORMAIL							    \
	-i "From: $clean"					    \
	-i "${JA_MIME_DECODE_HEADER}-From: $type"
}

# Touch only real mime messages and text/plain
# $MIME_BIN_QP does not handle separate MIME sections

:0
*    JA_MIME_DECODE_TREAT_BODY  ?? yes
*$   $JA_MIME_DECODE_REGEXP
{
    :0 fbw
    * ^Content-Transfer-Encoding: *quoted-printable
    | $MIME_BIN_QP

        :0 A fhw
        | $FORMAIL -I "Content-Transfer-Encoding: 8bit"

    :0 fbw
    * ^Content-Transfer-Encoding: *base64
    | $MIME_BIN_64

        :0 A fhw
        | $FORMAIL -I "Content-Transfer-Encoding: 8bit"
}

dummy = "pm-jamime-decode.rc: end:"

# End of file pm-jamime-decode.rc