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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
|
# pm-jamime-save.rc -- save message's MIME attachement (one file) to a file
#
# 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
#
# This module saves _one_ simple file attachment (MIME) from he
# message. The message must define following MIME headers. If
# "filename=" does not exists, then the message is ignored.
#
# Mime-Version: <version>
# Content-Type: <type>
# Content-Disposition: attachment; filename="file.txt"
#
# The last line can also be in separate line, provided that it
# is indented according to standard rules:
#
# Mime-Version: <version>
# Content-Type: <type>
# Content-Disposition: attachment;
# filename="file.txt"
#
# Procmail is not very suitable for saving MIME attachments and
# you should not think that this the right tool for you.
# If you receive anything more than 1 attachment, this recipe
# does nothing, because that's out of our league and you need some
# more heavy weight mime tools. E.g. Perl CPAN has MIME libraries.
#
# _Note_: when the attachment is in the body, it is simply written
# to a disk and the location in message is replaced with test:
#
# Extracted to file:/users/foo/junk/<YYYY-MM-DD-hhmm>.file.txt.
#
# The existing mime headers that surround the attachment are
# lect untouched, so don't try to press your Mail Agent's MIME buttons
# at that point. There is no such file in that spot
# if you set `JA_MIME_SAVE_DEL' to `yes'.
#
# Required settings
#
# PMSRC must point to source directory of procmail code. This
# subroutine includes library:
#
# o pm-javar.rc
# o pm-jamime.rc
# o pm-jamime-decode.rc
# o pm-jadate.rc (which will call other pm-jadate*.rc files)
#
# Call arguments (variables to set before calling)
#
# o `JA_MIME_SAVE_DIR', point this to directory where you
# want to store attachments.
# o `JA_MIME_SAVE_DECODE', set this to "yes", if you want that
# attachment is decoded before written to disk. This usually
# opens quoted printable or base64 encoding.
# o `JA_MIME_SAVE_DEL', set this to "yes", if you want to remove
# the attachment from the body of the message after it has
# been filed. Be vary careful if you use this option. If you
# keep backup cache of incoming mail, then you might try "yes".
# o `JA_MIME_SAVE_OVERWRITE', set this to "yes" if it's okay to
# overwrite to an existing filename found from attachment.
# If you get periodic attachments always with same name, then
# you would want to set this to yes.
#
# Core dump note
#
# Because procmail uses LINEBUF when filtering messages, a core
# dump may happen if the attachment being filtered is bigger than
# the LINEBUF. The current setting accepts 524K attachments, but if
# you expect to get bigger than that, you want to increase
# `JA_MIME_SAVE_LINEBUF'.
#
# Possible conflict with your awk
#
# Awk is used because it is much more system load friendly than perl.
# If you see an error message in the log file saying that awk failed:
#
# procmail: Executing awk,
# ...
# procmail: Error while writing to "awk"
# procmail: Rescue of unfiltered data succeeded
#
# it means that the system's standard awk doesn't support the
# variable passing syntax. To verify that this is the case, run
# following test:
#
# % awk '{print VAR; exit}' VAR="value" /etc/passwd
#
# The proper awk should print "value". If not, then see if you have
# `nawk' or `gawk' in your system, which should understand the
# variable passing syntax. To change the AWK, you need to set
# following variable somewhere at the top of your *.procmailrc*
#
# AWK = "gawk" # if that works better than standard "awk"
#
# Return values (none)
#
# Change Log (none)
dummy = "
========================================================================
pm-jamime-save.rc: init:"
:0
* ! WSPC ?? [ ]
{
INCLUDERC = $PMSRC/pm-javar.rc
}
:0
* ! MIME_VER ?? [0-9]
{
INCLUDERC = $RC_MIME
}
:0
* ! YYYY ?? [0-9]
{
INCLUDERC = $RC_DATE
}
:0 # if has no value, set it
*$ ! LINEBUF ?? $d
{
LINEBUF = 1024
}
jaMimeSaveLinebuf = $LINEBUF
# .......................................................... &public ...
# User configurable sections
JA_MIME_SAVE_DIR = ${JA_MIME_SAVE_DIR:-"$HOME"}
JA_MIME_SAVE_DECODE = ${JA_MIME_SAVE_DECODE:-"no"}
JA_MIME_SAVE_DEL = ${JA_MIME_SAVE_DEL:-"no"}
JA_MIME_SAVE_LINEBUF = ${JA_MIME_SAVE_LINEBUF:-524280}
JA_MIME_SAVE_OVERWRITE = ${JA_MIME_SAVE_OVERWRITE:-"no"}
# ........................................................... &do-it ...
dummy = "pm-jamime-save.rc: HEADER; $MIME_H_ATTACHMENT"
:0
* MIME_H_ATTACHMENT ?? [a-z]
{
jaMimeSaveFile = $MIME_H_ATTACHMENT
:0
* JA_MIME_SAVE_DECODE ?? yes
{
# decode regardless of body content
JA_MIME_DECODE_REGEXP = ".*"
INCLUDERC = $RC_MIME_DECODE
}
# ..................................................... filename ...
:0
* JA_MIME_SAVE_OVERWRITE ?? no
*$ ? $IS_EXIST $JA_MIME_SAVE_DIR/$jaMimeSaveFile
{
# Kill variable
jaMimeSaveDate
:0
*$ YYYY ?? $d
{
jaMimeSaveDate = "$YYYY-$MM-$DD"
:0
*$ hh ?? $d
{
jaMimeSaveDate = "$jaMimeSaveDate-$hh$mm"
}
jaMimeSaveFile = "$jaMimeSaveDate.$jaMimeSaveFile"
# Still not unique?
:0
*$ ? $IS_EXIST $JA_MIME_SAVE_DIR/$jaMimeSaveFile
{
:0 fhw
| $FORMAIL -I "X-jaMimeSave-Error: (file exists) $jaMimeSaveFile"
# kill variable to prevent next recipe from running
jaMimeSaveFile
}
}
}
# .................................................... write out ...
:0
* jaMimeSaveFile ?? [a-z]
{
:0 bwc: # do not modify body
* JA_MIME_SAVE_DEL ?? no
| $CAT > $JA_MIME_SAVE_DIR/$jaMimeSaveFile
# Write out the attachment and replace body
# with reference to the file.
:0 E
{
:0
*$ LINEBUF ?? ^^([5-9]$d$d$d$d$d|$d$d$d$d$d$d$d)^^
{
# User has set bigger linebuf than our default
}
:0 E
{
LINEBUF = $JA_MIME_SAVE_LINEBUF
}
:0 fbw:
| ( $CAT > $JA_MIME_SAVE_DIR/$jaMimeSaveFile; echo "Saved to $JA_MIME_SAVE_DIR/$jaMimeSaveFile" )
LINEBUF = $jaMimeSaveLinebuf
}
}
}
dummy = "pm-jamime-save.rc: BODY check"
:0 E
* MIME_B_ATTACHMENT ?? [a-z]
* MIME_B_ATTACHMENT_FILE_COUNT ?? ^^1^^
{
dummy = "pm-jamime-save.rc: BODY ENTERED: only some limited operations."
jaMimeSaveFile = $MIME_B_ATTACHMENT
# Try to locate the header where the content type is defined for
# file attachment. Note: there is no typo in the regexp, the
# caret(^) matches newline.
#
# Content-Type: application/octet-stream
# Content-Disposition: attachment; filename="file.txt"
# Content-Transfer-Encoding: 7bit
dummy = "pm-jamime-save.rc: Check contentType BODY 1"
:0
*$ B ?? ^Content-Type:$s+\/.*\
^Content-Disposition:.*($MIME_B_ATTACHMENT\
|^$s+.*$MIME_B_ATTACHMENT)
* MATCH ?? \/.*
{
contentType = $MATCH
}
dummy = "pm-jamime-save.rc: Check contentType BODY 2"
:0 E
*$ B ?? ^Content-Type:$s+\/.*\
^Content-Transfer-Encoding:.*\
^Content-Disposition:.*($MIME_B_ATTACHMENT\
|^$s+.*$MIME_B_ATTACHMENT)
* MATCH ?? \/.*
{
contentType = $MATCH
# The last "Content-Disposition" match allows two cases
#
# 1. Content-Disposition: attachment; filename="file.txt"
#
# 2. Content-Disposition: attachment;
# filename="file.txt"
:0
*$ B ?? ^Content-Type:.*\
^Content-Transfer-Encoding:$s\/.*\
^Content-Disposition:.*($MIME_B_ATTACHMENT\
|^$s+.*$MIME_B_ATTACHMENT)
* MATCH ?? \/.*
{
contentEncoding = $MATCH
}
}
# We expect that the headers come in this order.
# If they don't, then we can't know the encoding.
# We don't even try anything else: Procmail is not the right tool
# for complete MIME handling.
dummy = "pm-jamime-save.rc: Check contentEncoding BODY"
:0
* contentEncoding ?? ^^^^
*$ B ?? ^Content-Type:.*\
^Content-Disposition:.*\
^Content-Transfer-Encoding:$s+\/.*
{
contentEncoding = $MATCH
}
:0 E
* contentEncoding ?? ^^^^
*$ B ?? ^Content-Type:.*\
^Content-Transfer-Encoding:$s+\/.*
{
contentEncoding = $MATCH
}
:0
* contentEncoding ?? ^^^^
*$ B ?? ^Content-Type:.*\
^Content-Transfer-Encoding:$s+\/.*
{
contentEncoding = $MATCH
}
# ............................................... fix mixed-part ...
# In most typical message, sender "says" something in text/plain and
# then adds an atatchement
#
# But, due to order of the MIME headers we may have picked the
# text/plain. Change it to application/octet-stream if found.
#
# Mime-Version: 1.0
# Content-Type: multipart/mixed; boundary="----------118D218634724256"
#
# ------------118D218634724256
# Content-Type: text/plain; charset=us-ascii
# Content-Transfer-Encoding: 7bit
#
#
# ------------118D218634724256
# Content-Type: application/octet-stream; name="Ass_rake.dbf"
# Content-Transfer-Encoding: base64
# Content-Disposition: attachment; filename="Ass_rake.dbf"
dummy = "pm-jamime-save.rc: Check contentType ($contentType) text/plain => application"
:0
* contentType ?? text/
*$ B ?? ^Content-Type:$s+\/application.*
{
contentType = $MATCH
dummy = "pm-jamime-save.rc: finding Transfer-Encoding for application/*"
:0
*$ B ?? ^Content-Type:$s+application.*\
^Content-Transfer-Encoding:$s\/.*
{
contentEncoding = $MATCH
}
}
# ............................................... check filename ...
# Change filename if there is already that file
dummy = "pm-jamime-save.rc: check filename"
# Kill variable
jaMimeSaveDate
:0
*$ YYYY ?? $d
{
jaMimeSaveDate = "$YYYY-$MM-$DD"
:0
*$ hh ?? $d
{
jaMimeSaveDate = "$jaMimeSaveDate-$hh$mm"
}
jaMimeSaveFile = "$jaMimeSaveDate.$jaMimeSaveFile"
# Still not unique?
:0
*$ ? $IS_EXIST $JA_MIME_SAVE_DIR/$jaMimeSaveFile
{
:0 fhw
| $FORMAIL -I "X-jaMimeSave-Error: (file exists) $jaMimeSaveFile"
# kill variable to prevent next recipe from running
jaMimeSaveFile
}
}
# ........................................................ &save ...
# 1) Locate the positions where the attachment starts
# 2) When found, start looking for empty line which ends the mime
# headers. GO is set to 1, when attachment starts
# 3) Attachment ends when mime boundary is hit. Actually
# it ended one line before that, which was a empty line.
jaMimeSaveShellmetas = $SHELLMETAS
LINEBUF = $JA_MIME_SAVE_LINEBUF
SHELLMETAS
:0 fbw
* MIME_BOUNDARY_COUNT ?? [1-9]
* jaMimeSaveFile ?? [a-z]
| $AWK \
' BEGIN { found = 0; go = 0 } \
{ \
if ( match($0, MATCH) > 0 ) \
{ \
found++; \
} \
if ( found && match($0, "^[ \t]*$") ) \
{ \
go = 1; \
next; \
} \
if ( match($0, MIME) > 0 ) \
{ \
go = 0; \
} \
if ( go ) \
{ \
if ( DELETE == "yes") \
{ \
if ( urlFlag == 0 ) \
{ \
urlFlag = 1; \
printf "\nextracted to file:%s\n\n", FILE; \
} \
} \
else \
{ \
print; \
} \
print $0 >> FILE; \
next; \
} \
print; \
} \
' DELETE="$JA_MIME_SAVE_DEL" \
MIME="$MIME_BOUNDARY" \
COUNT="$MIME_BOUNDARY_COUNT" \
MATCH="Disposition:.*$MIME_B_ATTACHMENT" \
FILE="$JA_MIME_SAVE_DIR/$jaMimeSaveFile"
LINEBUF = $jaMimeSaveLinebuf
SHELLMETAS = $jaMimeSaveShellmetas
# ....................................................... decode ...
dummy = "pm-jamime-save.rc: Encoding($contentEncoding) of $jaMimeSaveFile"
:0
*$ jaMimeSaveFile ?? $a
*$ contentEncoding ?? $a
{
jaMimeSaveFile2 = "$jaMimeSaveFile.raw";
:0 wc
* contentEncoding ?? base64
| $CAT $JA_MIME_SAVE_DIR/$jaMimeSaveFile \
| $MIME_BIN_64 > $JA_MIME_SAVE_DIR/$jaMimeSaveFile2 \
&& $MV $JA_MIME_SAVE_DIR/$jaMimeSaveFile2 \
$JA_MIME_SAVE_DIR/$jaMimeSaveFile
:0 Ewc
* contentEncoding ?? quoted-printable
| $CAT $JA_MIME_SAVE_DIR/$jaMimeSaveFile \
| $MIME_BIN_QP > $JA_MIME_SAVE_DIR/$jaMimeSaveFile2 \
&& $MV $JA_MIME_SAVE_DIR/$jaMimeSaveFile2 \
$JA_MIME_SAVE_DIR/$jaMimeSaveFile
}
}
dummy = "pm-jamime-save.rc: end:"
# End of file pm-jamime-save.rc
|