| 1 |
;;; mew-decode.el --- MIME syntax decoder for Mew |
| 2 |
|
| 3 |
;; Author: Kazu Yamamoto <[email protected]> |
| 4 |
;; Created: Oct 2, 1996 |
| 5 |
|
| 6 |
;;; Code: |
| 7 |
|
| 8 |
(require 'mew) |
| 9 |
|
| 10 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 11 |
;;; |
| 12 |
;;; Decode info |
| 13 |
;;; |
| 14 |
|
| 15 |
;; limit: |
| 16 |
;; Depth of RFC 822 messages. |
| 17 |
;; The cnt starts with 0. |
| 18 |
;; Decode if cnt <= limit |
| 19 |
;; nil means that limit is 0. |
| 20 |
;; |
| 21 |
;; decode-text: |
| 22 |
;; If t, decode a text MIME body. |
| 23 |
;; Otherwise, it is treated as 'no-cs-conv |
| 24 |
;; |
| 25 |
;; decode-binary: |
| 26 |
;; If t, decode a binary (non-text) MIME body. |
| 27 |
;; |
| 28 |
;; use-alt: |
| 29 |
;; If t, one part in Multipart/Alternative is used. |
| 30 |
;; Otherwise, it is treated as Multipart/Mixed. |
| 31 |
|
| 32 |
(defvar mew-dinfo-list '("limit" "decode-text" "decode-binary" "use-alt" "encap-html")) |
| 33 |
|
| 34 |
(mew-blinfo-defun 'mew-dinfo mew-dinfo-list) |
| 35 |
|
| 36 |
(defun mew-dinfo-set (lim dt db alt &optional encap) |
| 37 |
(mew-dinfo-set-limit lim) |
| 38 |
(mew-dinfo-set-decode-text dt) |
| 39 |
(mew-dinfo-set-decode-binary db) |
| 40 |
(mew-dinfo-set-use-alt alt) |
| 41 |
(mew-dinfo-set-encap-html encap)) |
| 42 |
|
| 43 |
(defun mew-cache-dinfo-get-use-alt (buf) |
| 44 |
(when buf |
| 45 |
(with-current-buffer buf |
| 46 |
(mew-dinfo-get-use-alt)))) |
| 47 |
|
| 48 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 49 |
;;; |
| 50 |
;;; Decode switch |
| 51 |
;;; |
| 52 |
|
| 53 |
(defvar mew-prog-mime-decode-switch |
| 54 |
`((,mew-b64 "-d" "-b") |
| 55 |
(,mew-qp "-d" "-q") |
| 56 |
(,mew-xg "-d" "-g") |
| 57 |
(,mew-xuu "-d" "-u"))) |
| 58 |
|
| 59 |
(defvar mew-prog-mime-decode-text-switch |
| 60 |
`((,mew-b64 "-d" "-b" "-t") |
| 61 |
(,mew-qp "-d" "-q") |
| 62 |
(,mew-xg "-d" "-g" "-t") |
| 63 |
(,mew-xuu "-d" "-u"))) |
| 64 |
|
| 65 |
(defun mew-prog-mime-decode-get-opt (cte switch) |
| 66 |
(cdr (mew-assoc-case-equal cte switch 0))) |
| 67 |
|
| 68 |
(defvar mew-decode-multipart-encrypted-switch |
| 69 |
`((,mew-ct-pge mew-pgp-decrypt mew-pgp-ver mew-prog-pgp))) |
| 70 |
|
| 71 |
(defvar mew-decode-multipart-signed-switch |
| 72 |
`((,mew-ct-pgs mew-pgp-verify mew-pgp-ver mew-prog-pgp) |
| 73 |
(,mew-ct-sms mew-smime-detach-verify mew-smime-ver mew-prog-smime) |
| 74 |
(,mew-ct-xsms mew-smime-detach-verify mew-smime-ver mew-prog-smime))) |
| 75 |
|
| 76 |
;; |
| 77 |
|
| 78 |
(defun mew-decode-get-security-func (proto switch) |
| 79 |
(nth 1 (mew-assoc-case-equal proto switch 0))) |
| 80 |
|
| 81 |
(defun mew-decode-get-security-existence (proto switch) |
| 82 |
(symbol-value (nth 2 (mew-assoc-case-equal proto switch 0)))) |
| 83 |
|
| 84 |
(defun mew-decode-get-security-prog (proto switch) |
| 85 |
(symbol-value (nth 3 (mew-assoc-case-equal proto switch 0)))) |
| 86 |
|
| 87 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 88 |
;;; |
| 89 |
;;; MIME decoder |
| 90 |
;;; |
| 91 |
|
| 92 |
(defun mew-decode-error (error-msg) |
| 93 |
(mew-xinfo-set-decode-err error-msg) |
| 94 |
(error error-msg)) |
| 95 |
|
| 96 |
(defun mew-decode-error2 (error-msg) |
| 97 |
(mew-xinfo-set-decode-err error-msg)) |
| 98 |
|
| 99 |
;;; |
| 100 |
;;; Decoding a header |
| 101 |
;;; |
| 102 |
|
| 103 |
(defmacro mew-decode-narrow-to-header (&rest body) |
| 104 |
(declare (debug (&rest form))) |
| 105 |
`(progn |
| 106 |
(if (re-search-forward mew-eoh nil t) |
| 107 |
(beginning-of-line) |
| 108 |
(goto-char (point-max)) |
| 109 |
(insert "\n")) |
| 110 |
(when (and (integerp mew-header-max-length) |
| 111 |
(> (count-lines (point-min) (point)) mew-header-max-length)) |
| 112 |
(mew-xinfo-set-action |
| 113 |
(mew-substitute-for-summary |
| 114 |
"Too long header. To see the message, type '\\[mew-summary-analyze-again]'")) |
| 115 |
(error "")) |
| 116 |
(save-restriction |
| 117 |
(narrow-to-region (point-min) (point)) |
| 118 |
(goto-char (point-min)) |
| 119 |
,@body))) |
| 120 |
|
| 121 |
(defun mew-summary-toggle-header-veil () |
| 122 |
"If 'mew-use-header-veil' is non-nil, field lines of To: and Cc: |
| 123 |
over 'mew-header-veil-count' are covered with invisible veils. |
| 124 |
This commands toggles visibility of these lines." |
| 125 |
(interactive) |
| 126 |
(when (and mew-use-header-veil (get-buffer (mew-buffer-message))) |
| 127 |
(mew-summary-msg-or-part |
| 128 |
(with-current-buffer (mew-buffer-message) |
| 129 |
(when (mew-msghdr-p) |
| 130 |
(let* ((win (get-buffer-window (current-buffer))) |
| 131 |
(pos (window-start win))) |
| 132 |
(mew-toggle-header-veil (mew-minfo-get-veil-to)) |
| 133 |
(mew-toggle-header-veil (mew-minfo-get-veil-cc)) |
| 134 |
(set-window-start win pos))))))) |
| 135 |
|
| 136 |
(defun mew-header-veil-put (key ov) |
| 137 |
(let ((case-fold-search t) |
| 138 |
(regex (concat "^" key))) |
| 139 |
(goto-char (point-min)) |
| 140 |
(when (re-search-forward regex nil t) |
| 141 |
(beginning-of-line) |
| 142 |
(let ((start (point)) end) |
| 143 |
(forward-line) |
| 144 |
(mew-header-goto-next) |
| 145 |
(setq end (point)) |
| 146 |
(when (> (count-lines start end) mew-header-veil-count) |
| 147 |
(goto-char start) |
| 148 |
(forward-line mew-header-veil-count) |
| 149 |
(move-overlay ov (1- (point)) (1- end))))))) |
| 150 |
|
| 151 |
(defun mew-header-veil () |
| 152 |
(when mew-use-header-veil |
| 153 |
(unless (mew-minfo-get-veil-to) |
| 154 |
(mew-minfo-set-veil-to (mew-header-veil-make))) |
| 155 |
(unless (mew-minfo-get-veil-cc) |
| 156 |
(mew-minfo-set-veil-cc (mew-header-veil-make))) |
| 157 |
(mew-header-veil-put "To:" (mew-minfo-get-veil-to)) |
| 158 |
(mew-header-veil-put "Cc:" (mew-minfo-get-veil-cc)))) |
| 159 |
|
| 160 |
(defun mew-header-arrange (beg end) |
| 161 |
(let (vispos) |
| 162 |
(save-restriction |
| 163 |
(narrow-to-region beg end) |
| 164 |
(mew-elet |
| 165 |
(let (ch-beg ch-end vs-beg vs-end contents cbeg) |
| 166 |
(setq ch-beg (next-single-property-change (point-min) 'mew-noncontents)) |
| 167 |
(when ch-beg |
| 168 |
(setq vs-beg (next-single-property-change (point-min) 'mew-visible)) |
| 169 |
(cond |
| 170 |
(vs-beg |
| 171 |
(setq vs-end (next-single-property-change vs-beg 'mew-visible))) |
| 172 |
(mew-field-other-visible |
| 173 |
(setq vs-beg (next-single-property-change (point-min) 'mew-others)) |
| 174 |
(if vs-beg |
| 175 |
(setq vs-end (next-single-property-change vs-beg 'mew-others)) |
| 176 |
(setq vs-beg ch-beg) |
| 177 |
(setq vs-end ch-beg))) |
| 178 |
(t |
| 179 |
(setq vs-beg ch-beg) |
| 180 |
(setq vs-end ch-beg))) |
| 181 |
(setq ch-end (point-max)) |
| 182 |
(unless vs-end (setq vs-end (point-max))) |
| 183 |
(mew-decode-header-property-region ch-beg ch-end) |
| 184 |
;; This must be "buffer-substring". |
| 185 |
(setq contents (buffer-substring ch-beg ch-end)) |
| 186 |
(delete-region ch-beg ch-end) |
| 187 |
(cond |
| 188 |
((mew-nspec-visiblep (mew-nspec-by-key "Content-")) |
| 189 |
;; visible |
| 190 |
(goto-char vs-end) |
| 191 |
(setq cbeg (point)) |
| 192 |
(insert contents) |
| 193 |
(put-text-property cbeg (point) 'mew-visible t)) ;; used later |
| 194 |
(t |
| 195 |
;; invisible |
| 196 |
(goto-char vs-beg) |
| 197 |
(setq cbeg (point)) |
| 198 |
(insert contents))) |
| 199 |
(goto-char (point-max))) |
| 200 |
(mew-decode-syntax-insert-privacy) |
| 201 |
(mew-decode-syntax-insert-warning) |
| 202 |
(save-excursion (mew-highlight-x-face (point-min) (point-max))) |
| 203 |
(setq vispos (if (get-text-property (point-min) 'mew-visible) |
| 204 |
(point-min) |
| 205 |
(or (next-single-property-change (point-min) 'mew-visible) |
| 206 |
(point-max))))))) |
| 207 |
(mew-header-veil) |
| 208 |
(mew-header-goto-end) |
| 209 |
(if (eobp) |
| 210 |
(mew-header-set "\n") ;; analyzed |
| 211 |
(mew-header-set nil)) ;; asis |
| 212 |
;; Emacs 21.3.50 or later need this. |
| 213 |
(set-window-start (selected-window) vispos))) |
| 214 |
|
| 215 |
(defun mew-decode-header-property-region (BEG END) |
| 216 |
;; see also mew-highlight-header |
| 217 |
(when mew-use-highlight-header |
| 218 |
(mew-elet |
| 219 |
(let (key beg med nspec key-face val-face) |
| 220 |
(save-restriction |
| 221 |
(narrow-to-region BEG END) |
| 222 |
(goto-char (point-min)) |
| 223 |
(while (not (eobp)) |
| 224 |
(if (not (looking-at mew-keyval)) |
| 225 |
(forward-line) |
| 226 |
(setq key (mew-match-string 1)) |
| 227 |
(setq beg (match-beginning 0)) |
| 228 |
(setq med (match-end 0)) |
| 229 |
(forward-line) |
| 230 |
(setq nspec (mew-nspec-by-key key)) |
| 231 |
(setq key-face (mew-key-face key nspec)) |
| 232 |
(setq val-face (mew-val-face key nspec)) |
| 233 |
(put-text-property beg med 'face key-face) |
| 234 |
(put-text-property med (1- (point)) 'face val-face) |
| 235 |
(while (looking-at mew-lwsp+) |
| 236 |
(forward-line) |
| 237 |
(put-text-property (match-end 0) (1- (point)) |
| 238 |
'face val-face))))))))) |
| 239 |
|
| 240 |
(defun mew-decode-rfc822-header (&optional cnt) |
| 241 |
"A function to handle RFC822 header. |
| 242 |
Called on the beginning of the header in the narrowed region. |
| 243 |
- Decode and highlight RFC822 fields excluding MIME fields. |
| 244 |
- Delete X-Mew: fields. |
| 245 |
- Arrange decoded-RFC822-fields, mew-mv:, MIME fields in order. |
| 246 |
The cursor moves between mew-mv: and MIME fields. |
| 247 |
Return the existence MIME-Version: and the value of Subject:." |
| 248 |
(let* ((case-fold-search t) |
| 249 |
(visibles (make-list (length mew-field-spec) nil)) |
| 250 |
(prop mew-use-highlight-header) |
| 251 |
key beg med subj from contents others |
| 252 |
key-face val-face N nspec visiblep |
| 253 |
mimep mimep2 |
| 254 |
size ext ext-str ext-face) |
| 255 |
(mew-decode-narrow-to-header |
| 256 |
(while (not (eobp)) |
| 257 |
(if (not (looking-at mew-keyval)) |
| 258 |
(forward-line) |
| 259 |
(setq key (mew-capitalize (mew-match-string 1))) |
| 260 |
(setq beg (match-beginning 0)) |
| 261 |
(setq med (match-end 0)) |
| 262 |
(forward-line) |
| 263 |
(mew-header-goto-next) |
| 264 |
(setq nspec (mew-nspec-by-key key)) |
| 265 |
(setq N (mew-nspec-n nspec)) |
| 266 |
(setq visiblep (mew-nspec-visiblep nspec)) |
| 267 |
(if (fboundp visiblep) |
| 268 |
(setq visiblep (funcall visiblep (mew-buffer-substring med (1- (point)))))) |
| 269 |
(cond |
| 270 |
((string= key mew-x-mew:) |
| 271 |
;; deleting X-Mew: on the RFC822 header |
| 272 |
(delete-region beg (point))) |
| 273 |
((string= key mew-x-mew-uidl:) |
| 274 |
(setq size (mew-scan-uid-size (mew-buffer-substring med (1- (point))))) |
| 275 |
(when (and (eq cnt 1) (mew-msg-truncatedp size)) |
| 276 |
(mew-xinfo-set-action |
| 277 |
(mew-substitute-for-summary |
| 278 |
"Too large, truncated (the 'T' mark). To get the entire message, type '\\[mew-summary-retrieve-message]'")))) |
| 279 |
((string-match "^Content-" key) |
| 280 |
;; Due to PGP/MIME, properties are not put here. |
| 281 |
:; This must be "buffer-substring". |
| 282 |
(setq mimep2 t) |
| 283 |
(setq contents (cons (buffer-substring beg (point)) contents)) |
| 284 |
(delete-region beg (point))) |
| 285 |
(t |
| 286 |
;; Let's decode the field anyway. |
| 287 |
;; If a user want to preserve this RFC 822 header, he |
| 288 |
;; set the limit, so never reaches here. |
| 289 |
;; Moreover this function may be called with |
| 290 |
;; mew-dinfo-set unset. |
| 291 |
(mew-header-decode-region key med (point)) |
| 292 |
(cond |
| 293 |
((string= key mew-from:) |
| 294 |
(setq from (mew-addrstr-parse-address |
| 295 |
(mew-buffer-substring med (1- (point)))))) |
| 296 |
((string= key mew-subj:) |
| 297 |
(setq subj (mew-buffer-substring med (1- (point))))) |
| 298 |
((string= key mew-mv:) |
| 299 |
;; MIME-Version: |
| 300 |
(setq mimep (string-match |
| 301 |
mew-mv:-num |
| 302 |
(or (mew-addrstr-parse-value |
| 303 |
(mew-buffer-substring med (point))) |
| 304 |
""))))) |
| 305 |
(when prop |
| 306 |
(setq key-face (mew-key-face key nspec)) |
| 307 |
(setq val-face (mew-val-face key nspec)) |
| 308 |
(put-text-property beg med 'face key-face) |
| 309 |
(goto-char med) |
| 310 |
(forward-line) |
| 311 |
(put-text-property med (1- (point)) 'face val-face) |
| 312 |
(while (looking-at mew-lwsp+) |
| 313 |
(forward-line) |
| 314 |
(put-text-property (match-end 0) (1- (point)) |
| 315 |
'face val-face)) |
| 316 |
(when (setq ext (mew-nspec-extraface nspec)) |
| 317 |
(save-restriction |
| 318 |
(narrow-to-region med (point)) |
| 319 |
(while ext ;; cannot use dolist |
| 320 |
(goto-char (point-min)) |
| 321 |
(setq ext-str (car ext)) |
| 322 |
(setq ext (cdr ext)) |
| 323 |
(setq ext-face (car ext)) |
| 324 |
(setq ext (cdr ext)) |
| 325 |
(while (re-search-forward ext-str nil t) |
| 326 |
(put-text-property (match-beginning 0) (match-end 0) 'face ext-face))) |
| 327 |
(goto-char (point-max))))) |
| 328 |
(cond |
| 329 |
((null nspec) ;; others |
| 330 |
;; This must be "buffer-substring". |
| 331 |
(setq others (cons (buffer-substring beg (point)) others)) |
| 332 |
(delete-region beg (point))) |
| 333 |
(visiblep |
| 334 |
;; This must be "buffer-substring". |
| 335 |
(setcar (nthcdr N visibles) |
| 336 |
(concat (nth N visibles) |
| 337 |
(buffer-substring beg (point)))) |
| 338 |
(delete-region beg (point))) |
| 339 |
(t ;; invisible |
| 340 |
()))))))) |
| 341 |
(put-text-property (point-min) (point) 'mew-invisible t) |
| 342 |
(put-text-property (point-min) (point) 'mew-noncontents t) |
| 343 |
(unless mew-field-other-visible |
| 344 |
(setq beg (point)) |
| 345 |
(mapc 'insert (nreverse others)) |
| 346 |
(put-text-property beg (point) 'mew-others t) |
| 347 |
(put-text-property beg (point) 'mew-noncontents t)) |
| 348 |
(setq beg (point)) |
| 349 |
(mapc (lambda (x) (and (stringp x) (insert x))) visibles) |
| 350 |
;; for recenter in Message mode |
| 351 |
(put-text-property beg (point) 'mew-visible t) |
| 352 |
(put-text-property beg (point) 'mew-noncontents t) |
| 353 |
(mew-rear-nonsticky beg (point)) |
| 354 |
(when mew-field-other-visible |
| 355 |
(setq beg (point)) |
| 356 |
(mapc 'insert (nreverse others)) |
| 357 |
(put-text-property beg (point) 'mew-others t) |
| 358 |
(put-text-property beg (point) 'mew-noncontents t) |
| 359 |
(mew-rear-nonsticky beg (point))) |
| 360 |
;; the beginning of the content header |
| 361 |
(save-excursion (mapc 'insert (nreverse contents))) |
| 362 |
;; 'mew-contents does not work due to PGP/MIME |
| 363 |
(if (and (null mimep) mimep2) |
| 364 |
(mew-xinfo-set-warning |
| 365 |
(cons "No MIME-Version\n" (mew-xinfo-get-warning)))) |
| 366 |
(list (or mimep mimep2) subj from))) |
| 367 |
|
| 368 |
(defun mew-decode-mime-header (&optional dct) |
| 369 |
"A function to handle content header. |
| 370 |
Called on the beginning of the content header in the narrowed region |
| 371 |
Return a part syntax after moving the beginning of the content body." |
| 372 |
(let ((case-fold-search t) |
| 373 |
(vec (make-vector (length mew-mime-fields) nil)) |
| 374 |
key med attr n act value syntax) |
| 375 |
(mew-decode-narrow-to-header |
| 376 |
(while (not (eobp)) |
| 377 |
(if (not (looking-at mew-keyval)) |
| 378 |
(forward-line) |
| 379 |
(setq key (mew-capitalize (mew-match-string 1))) |
| 380 |
(setq med (match-end 0)) |
| 381 |
(forward-line) |
| 382 |
(mew-header-goto-next) |
| 383 |
(setq attr (assoc key mew-mime-fields)) |
| 384 |
(when attr |
| 385 |
(mew-set '(nil n act) attr) |
| 386 |
(cond |
| 387 |
((eq act 'analyze) |
| 388 |
(setq value (mew-param-decode |
| 389 |
(mew-buffer-substring med (1- (point)))))) |
| 390 |
((eq act 'extract) |
| 391 |
(setq value (mew-addrstr-parse-value |
| 392 |
(mew-buffer-substring med (1- (point)))))) |
| 393 |
((eq act 'decode) |
| 394 |
;; If a user want to preserve this MIME header, he |
| 395 |
;; set the limit, so never reaches here. |
| 396 |
(mew-header-decode-region key med (point)) |
| 397 |
;; mew-header-decode-region goes to the max point in |
| 398 |
;; the narrowed region. So, this must be (point). |
| 399 |
(setq value (mew-buffer-substring med (1- (point))))) |
| 400 |
((eq act 'id) |
| 401 |
(setq value (mew-buffer-substring med (1- (point)))) |
| 402 |
(setq value (mew-idstr-get-first-id value)))) |
| 403 |
(aset vec n value))))) |
| 404 |
(if (eobp) |
| 405 |
(insert "\n") |
| 406 |
(forward-line)) |
| 407 |
;; the beginning of the content body |
| 408 |
(setq syntax (vconcat (list 'single (point) nil nil) vec)) |
| 409 |
(or (mew-syntax-get-ct syntax) |
| 410 |
(mew-syntax-set-ct syntax (or dct mew-type-txt))) |
| 411 |
syntax)) |
| 412 |
|
| 413 |
;;; |
| 414 |
;;; Decoding a message |
| 415 |
;;; |
| 416 |
|
| 417 |
(defun mew-decode () |
| 418 |
"Decode a message. Execute mew-dinfo-set before calling this." |
| 419 |
;; in cache buffer |
| 420 |
(mew-set-buffer-multibyte t) |
| 421 |
;; |
| 422 |
;; Illegal messages may not have end-of-header. |
| 423 |
;; Truncated messages may not have end-of-header. |
| 424 |
;; |
| 425 |
(goto-char (point-min)) ;; just in case |
| 426 |
(unless (re-search-forward mew-eoh nil t) |
| 427 |
(goto-char (point-max)) |
| 428 |
(if (not (bolp)) (insert "\n")) |
| 429 |
(insert "\n")) |
| 430 |
(goto-char (point-min)) |
| 431 |
;; |
| 432 |
(if (mew-debug 'decode) |
| 433 |
(let ((debug-on-error t)) |
| 434 |
(setq mew-decode-syntax |
| 435 |
(mew-decode-message (mew-decode-syntax-rfc822-head) 0))) |
| 436 |
(condition-case nil |
| 437 |
(setq mew-decode-syntax |
| 438 |
(mew-decode-message |
| 439 |
;; Call internalform with VIRTUAL content header |
| 440 |
;; CT: message/rfc822 (virtual) |
| 441 |
;; |
| 442 |
;; Header(RFC822 header + content header) |
| 443 |
;; |
| 444 |
;; Body(content body) |
| 445 |
(mew-decode-syntax-rfc822-head) 0)) |
| 446 |
(error |
| 447 |
(widen) |
| 448 |
(mew-header-goto-body) |
| 449 |
;; min, point - 1, point, point-max |
| 450 |
(setq mew-decode-syntax (mew-decode-syntax-rfc822)))))) |
| 451 |
|
| 452 |
(defun mew-decode-message (syntax cnt) |
| 453 |
;; Called on the beginning of the RFC822 header in the narrowed region |
| 454 |
;; hbeg is certainly the beginning of the VIRTUAL content body(i.e. min). |
| 455 |
;; hend will have to set to the end of PHYSICAL content header(i.e. end) |
| 456 |
;; after analyzing the physical content header and body since CD:'s |
| 457 |
;; length in the physical content header will change(no need to say |
| 458 |
;; about the end of the physical content header). |
| 459 |
;; |
| 460 |
;; Content-Type: Message/Rfc822 == virtual content header |
| 461 |
;; |
| 462 |
;;(min)Decoded RFC822 fields == virtual content body |
| 463 |
;; MIME-Version: 1.0 |
| 464 |
;;(cur)MIME fields == physical content header |
| 465 |
;;(end) |
| 466 |
;; Content-Body == physical content body |
| 467 |
;;(max) |
| 468 |
(setq cnt (1+ cnt)) |
| 469 |
(cond |
| 470 |
((and (mew-dinfo-get-limit) (> cnt (mew-dinfo-get-limit))) |
| 471 |
;; Don't analyze recursively. |
| 472 |
;; We need to analyze the header and the body since a user |
| 473 |
;; may want to save the body only. |
| 474 |
;; the beginning of the meaningless physical content header |
| 475 |
(if (re-search-forward mew-eoh nil t) |
| 476 |
(forward-line) |
| 477 |
(mew-decode-error "No end-of-header(null line) in RFC822 message")) |
| 478 |
;; the beginning of the BODY(i.e. the physical content body) |
| 479 |
(mew-syntax-set-key syntax 'message) |
| 480 |
(mew-syntax-set-end syntax (1- (point))) |
| 481 |
;; (point-min), (point) - 1, (point), (point-max) |
| 482 |
(mew-decode-syntax-rfc822 syntax)) ;; return value |
| 483 |
(t |
| 484 |
;; Analyze recursively. |
| 485 |
(let* ((msf (mew-decode-rfc822-header cnt)) ;; on the physical |
| 486 |
(mimep (nth 0 msf)) |
| 487 |
(subj (nth 1 msf)) |
| 488 |
(mew-inherit-decode-signer (nth 2 msf)) ;; signature verification |
| 489 |
part) |
| 490 |
;; the beginning of the physical content header (cur) |
| 491 |
(cond |
| 492 |
(mimep ;; MIME |
| 493 |
(save-restriction |
| 494 |
(narrow-to-region (point) (point-max)) |
| 495 |
(setq part (mew-decode-singlepart cnt nil 'message)) |
| 496 |
;; hend is always 1 char smaller than the beginning of |
| 497 |
;; the physical content body. |
| 498 |
;; If multipart/alternative mew-syntax-get-begin2 gets |
| 499 |
;; correct value like other cases. |
| 500 |
(mew-syntax-set-key syntax 'message) |
| 501 |
(mew-syntax-set-end syntax (1- (mew-syntax-get-begin2 part))) |
| 502 |
(or (mew-syntax-get-cd syntax) (mew-syntax-set-cd syntax subj)) |
| 503 |
(mew-syntax-cat syntax part))) ;; return value |
| 504 |
(t ;; RFC822 |
| 505 |
;; the beginning of the meaningless physical content header |
| 506 |
(if (re-search-forward mew-eoh nil t) |
| 507 |
(forward-line) |
| 508 |
(mew-decode-error "No end-of-header(null line) in RFC822 message")) |
| 509 |
;; the beginning of the BODY(i.e. the physical content body) |
| 510 |
(if (eq (mew-dinfo-get-decode-text) t) ;; not 'no-cs-conv |
| 511 |
(mew-cs-decode-region (point) (point-max) mew-cs-autoconv)) |
| 512 |
(mew-syntax-set-key syntax 'message) |
| 513 |
(mew-syntax-set-end syntax (1- (point))) |
| 514 |
(or (mew-syntax-get-cd syntax) (mew-syntax-set-cd syntax subj)) |
| 515 |
;; (point-min), (point) - 1, (point), (point-max) |
| 516 |
(mew-highlight-body-region (point) (point-max)) |
| 517 |
(mew-decode-syntax-rfc822 |
| 518 |
syntax (not (mew-xinfo-get-text-body))))))))) ;; return value |
| 519 |
|
| 520 |
;;; |
| 521 |
;;; Decoding singlepart |
| 522 |
;;; |
| 523 |
|
| 524 |
(defun mew-decode-mime-body (textp ct cte &optional charset) |
| 525 |
;; ((point), (point-max)) (not point-min) |
| 526 |
(let* ((linebasep (mew-ct-linebasep ct)) |
| 527 |
(beg (point)) |
| 528 |
opt file decoded switch) |
| 529 |
(unless (or (null cte) (mew-cte-composite-p cte)) |
| 530 |
(when (and (mew-case-equal cte mew-b64) (fboundp 'base64-decode-region)) |
| 531 |
(condition-case nil |
| 532 |
(setq decoded (base64-decode-region beg (point-max))) |
| 533 |
(error (setq decoded nil))) |
| 534 |
(when (and decoded linebasep) |
| 535 |
(goto-char beg) |
| 536 |
(mew-crlf-to-lf))) |
| 537 |
;; base64-decode-region may fail if garbage exists. |
| 538 |
(cond |
| 539 |
(decoded |
| 540 |
()) |
| 541 |
((mew-which-exec mew-prog-mime-decode) |
| 542 |
(setq switch (if linebasep |
| 543 |
mew-prog-mime-decode-text-switch |
| 544 |
mew-prog-mime-decode-switch)) |
| 545 |
(setq opt (mew-prog-mime-decode-get-opt cte switch)) |
| 546 |
(if (null opt) |
| 547 |
;; Treated as Application/Octet-Stream. |
| 548 |
;; Never reach here when decoding. |
| 549 |
(mew-decode-error (concat "Unknown CTE: " cte)) |
| 550 |
(setq file (mew-make-temp-name)) |
| 551 |
(mew-frwlet mew-cs-dummy (if linebasep |
| 552 |
mew-cs-text-for-write |
| 553 |
mew-cs-text-for-net) |
| 554 |
;; Due to broken MUAs, we need to write CRLF. |
| 555 |
;; NEVER use call-process-region for privacy reasons |
| 556 |
(write-region beg (point-max) file nil 'no-msg)) |
| 557 |
(delete-region beg (point-max)) |
| 558 |
(mew-piolet mew-cs-binary mew-cs-dummy |
| 559 |
;; mew-prog-mime-decode converts CRLF to LF, so |
| 560 |
;; read input as binary. |
| 561 |
(apply 'call-process mew-prog-mime-decode file '(t nil) nil opt)) |
| 562 |
(mew-delete-file file))) |
| 563 |
(t |
| 564 |
(mew-decode-error (concat mew-prog-mime-decode " does not exist"))))) |
| 565 |
;; charset conversion |
| 566 |
(if (and textp (eq (mew-dinfo-get-decode-text) t)) ;; not 'no-cs-conv |
| 567 |
(mew-decode-charset-conv charset beg)))) |
| 568 |
|
| 569 |
(defun mew-decode-charset-conv (charset beg) |
| 570 |
(let* ((fromcs (and charset (mew-charset-to-cs charset)))) |
| 571 |
;; fromcs is nil if charset is "us-ascii" |
| 572 |
;; (mew-coding-system-p nil) returns t. |
| 573 |
(unless (mew-coding-system-p fromcs) |
| 574 |
(mew-decode-warning-body 'unknown-charset charset)) |
| 575 |
(when (and mew-use-autoconv-when-unknown |
| 576 |
(not (mew-coding-system-p fromcs))) |
| 577 |
(setq fromcs mew-cs-autoconv)) |
| 578 |
(when (and mew-decode-broken |
| 579 |
(not (eq fromcs mew-cs-autoconv)) |
| 580 |
(or (null charset) |
| 581 |
(mew-case-equal charset mew-us-ascii))) ;; anyway |
| 582 |
(goto-char beg) |
| 583 |
(when (re-search-forward mew-regex-esc-or-nonascii nil t) |
| 584 |
(mew-decode-warning-body 'no-charset charset) |
| 585 |
(setq fromcs mew-cs-autoconv)) |
| 586 |
(goto-char beg)) |
| 587 |
(when (and mew-decode-broken |
| 588 |
(not (eq fromcs mew-cs-autoconv)) |
| 589 |
(or (null charset) |
| 590 |
(mew-case-equal charset mew-us-ascii) |
| 591 |
(mew-case-equal charset mew-utf-8))) |
| 592 |
(goto-char beg) |
| 593 |
(when (re-search-forward mew-regex-singlebyte-nonascii nil t) |
| 594 |
(mew-decode-warning-body 'no-charset charset) |
| 595 |
(setq fromcs mew-cs-autoconv)) |
| 596 |
(goto-char beg)) |
| 597 |
(if (mew-coding-system-p fromcs) |
| 598 |
(mew-cs-decode-region beg (point-max) fromcs)))) |
| 599 |
|
| 600 |
(defun mew-decode-singlepart (cnt &optional dct parent) |
| 601 |
;; Called on the beginning of the content header in the narrowed region |
| 602 |
(let* ((case-fold-search t) (begin (point)) |
| 603 |
(syntax (mew-decode-mime-header dct)) |
| 604 |
(ctl (mew-syntax-get-ct syntax)) |
| 605 |
(ct (mew-syntax-get-value ctl 'cap)) |
| 606 |
(textp (mew-ct-textp ct)) |
| 607 |
(cte (or (mew-syntax-get-cte syntax) mew-7bit)) |
| 608 |
(multi-err t) (encap nil) type hend format delsp) |
| 609 |
;; the beginning of the content body |
| 610 |
(cond |
| 611 |
((not (mew-cte-p cte)) |
| 612 |
(mew-syntax-set-ct syntax mew-type-apo) |
| 613 |
(if (eq parent 'message) (setq encap t))) |
| 614 |
((mew-ct-messagep ct) |
| 615 |
(if (not (mew-cte-composite-p cte)) |
| 616 |
(mew-syntax-set-ct syntax mew-type-apo) |
| 617 |
(cond |
| 618 |
((string= mew-ct-msg ct) |
| 619 |
(if (eq parent 'message) (setq encap t)) |
| 620 |
(save-restriction |
| 621 |
(narrow-to-region (point) (point-max)) |
| 622 |
(setq syntax (mew-decode-message syntax cnt)))) |
| 623 |
((string= mew-ct-ext ct) |
| 624 |
(let* ((at (mew-syntax-get-param ctl "access-type")) |
| 625 |
(func (mew-ext-include-get-func at))) |
| 626 |
(when (and func (fboundp func)) |
| 627 |
(save-excursion |
| 628 |
(goto-char (point-max)) ;; phantom body |
| 629 |
(funcall func ctl)) |
| 630 |
(delete-region begin (point)) |
| 631 |
(setq syntax (mew-decode-singlepart cnt))))) |
| 632 |
((string= mew-ct-sts ct) |
| 633 |
;; do nothing |
| 634 |
) |
| 635 |
(t |
| 636 |
;; xxx how about message/partial? |
| 637 |
(mew-syntax-set-ct syntax mew-type-apo) |
| 638 |
)))) |
| 639 |
((or (string= ct mew-ct-smm) (string= ct mew-ct-xsmm)) |
| 640 |
(mew-decode-mime-body nil ct cte) |
| 641 |
(mew-syntax-set-end syntax (point-max)) |
| 642 |
(setq syntax (mew-decode-smime syntax cnt))) |
| 643 |
;; Multipart, decoding is not required |
| 644 |
((mew-ct-multipartp ct) |
| 645 |
(if (mew-cte-composite-p cte) |
| 646 |
(setq multi-err nil) |
| 647 |
(mew-decode-warning-body 'multi cte) |
| 648 |
(if mew-decode-broken (setq multi-err nil))) |
| 649 |
(if multi-err |
| 650 |
(mew-syntax-set-ct syntax mew-type-apo) |
| 651 |
(cond |
| 652 |
((string= mew-ct-mld ct) |
| 653 |
;; semantics into digest |
| 654 |
(setq syntax (mew-decode-multipart syntax cnt mew-type-msg))) |
| 655 |
((string= mew-ct-mls ct) |
| 656 |
(setq syntax (mew-decode-multipart-signed syntax cnt))) |
| 657 |
((string= mew-ct-mle ct) |
| 658 |
;; xxx making use of password cache? |
| 659 |
(if mew-inherit-prefetching |
| 660 |
(signal 'quit "") |
| 661 |
(setq syntax (mew-decode-multipart-encrypted syntax cnt)))) |
| 662 |
(t |
| 663 |
(setq syntax (mew-decode-multipart syntax cnt nil)))))) |
| 664 |
;; Others |
| 665 |
((string= mew-ct-trh ct) |
| 666 |
(save-restriction |
| 667 |
;; Eliminating the content the header so that Rfc822 header |
| 668 |
;; comes the beginning. |
| 669 |
(narrow-to-region (point) (point-max)) |
| 670 |
(mew-decode-rfc822-header))) |
| 671 |
(t |
| 672 |
(if (and (eq parent 'message) |
| 673 |
(or (not (mew-xinfo-get-text-body)) |
| 674 |
(if (mew-dinfo-get-encap-html) |
| 675 |
(not (string= ct mew-ct-txt)) |
| 676 |
(not textp)))) |
| 677 |
(setq encap t)) |
| 678 |
;; even if cte is nil, call mew-decode-mime-body for charset conversion |
| 679 |
(if textp |
| 680 |
(if (mew-dinfo-get-decode-text) |
| 681 |
(let ((charset (mew-syntax-get-param ctl "charset"))) |
| 682 |
(mew-decode-mime-body t ct cte charset))) |
| 683 |
(if (mew-dinfo-get-decode-binary) |
| 684 |
(mew-decode-mime-body nil ct cte))))) |
| 685 |
;; ct may be changed to apo |
| 686 |
;; or single in multipart/alternative |
| 687 |
;; or single in multipart/security |
| 688 |
(setq type (mew-syntax-get-value (mew-syntax-get-ct syntax) 'cap)) |
| 689 |
(setq hend (mew-syntax-get-end syntax)) |
| 690 |
(unless hend |
| 691 |
(mew-syntax-set-end syntax (point-max))) |
| 692 |
(when (and (string= type mew-ct-txt) |
| 693 |
(setq format (mew-syntax-get-param ctl "format")) |
| 694 |
(mew-case-equal format "flowed")) |
| 695 |
(setq delsp (mew-syntax-get-param ctl "delsp")) |
| 696 |
(if (and delsp (mew-case-equal delsp "yes")) |
| 697 |
(setq delsp t) |
| 698 |
(setq delsp nil)) |
| 699 |
(mew-decode-flowed (mew-syntax-get-begin syntax) (point-max) delsp) |
| 700 |
(setq hend nil)) |
| 701 |
;; highlight in the cache buffers only |
| 702 |
(when (and (string= type mew-ct-txt) |
| 703 |
(string-match mew-buffer-cache-prefix (buffer-name))) |
| 704 |
(mew-highlight-body-region (mew-syntax-get-begin syntax) (point-max))) |
| 705 |
(unless hend |
| 706 |
(mew-syntax-set-end syntax (point-max))) ;; ajusting |
| 707 |
(if encap |
| 708 |
;; Mew allows text/plain and multipart/* for body. |
| 709 |
;; If other CT: is embedded under message, it should be |
| 710 |
;; encapsulated in multipart/mixed. |
| 711 |
(let ((head (mew-decode-syntax-multi-head))) |
| 712 |
;; begin for multipart syntax is important because |
| 713 |
;; the begin will be used by the parent to set hend |
| 714 |
(mew-syntax-set-begin head (mew-syntax-get-begin syntax)) |
| 715 |
(mew-syntax-set-end head (point-max)) |
| 716 |
(mew-syntax-cat head syntax)) ;; return value |
| 717 |
;; return value |
| 718 |
syntax))) |
| 719 |
|
| 720 |
(defun mew-decode-flowed-level () |
| 721 |
(cond |
| 722 |
((looking-at "^$") -1) |
| 723 |
((looking-at "^>*") |
| 724 |
(let ((level (- (match-end 0) (match-beginning 0)))) |
| 725 |
(goto-char (match-end 0)) |
| 726 |
;; we don't remove space-stuff after > |
| 727 |
(if (and (/= level 0) (looking-at " ")) (forward-char)) |
| 728 |
level)))) |
| 729 |
|
| 730 |
(defconst mew-flowed-quoted ?>) |
| 731 |
(defconst mew-flowed-stuffed mew-sp) |
| 732 |
(defconst mew-flowed-break mew-sp) |
| 733 |
(defconst mew-flowed-netnews "-- ") |
| 734 |
(defvar mew-flowed-broken-list '("----- Original Message ----- ")) ;; OE |
| 735 |
|
| 736 |
(defun mew-decode-flowed (beg end delsp) |
| 737 |
"Decoding wrapped lines encoded with RFC 3676" |
| 738 |
(save-restriction |
| 739 |
(narrow-to-region beg end) |
| 740 |
(goto-char (point-min)) |
| 741 |
(let (level prev-level softbreak) |
| 742 |
(setq level (mew-decode-flowed-level)) |
| 743 |
(if (= level 0) (mew-decode-flowed-remove-stuffed)) |
| 744 |
(setq softbreak (mew-decode-flowed-soft-breakp delsp)) |
| 745 |
(while (= (forward-line) 0) |
| 746 |
(setq prev-level level) |
| 747 |
(setq level (mew-decode-flowed-level)) |
| 748 |
(cond |
| 749 |
((and (= prev-level level) softbreak) |
| 750 |
(mew-decode-flowed-remove-quoted-stuffed softbreak)) |
| 751 |
(t |
| 752 |
(if (= level 0) (mew-decode-flowed-remove-stuffed)))) |
| 753 |
(setq softbreak (mew-decode-flowed-soft-breakp delsp)))))) |
| 754 |
|
| 755 |
(defun mew-decode-flowed-remove-quoted-stuffed (beg) |
| 756 |
(delete-region beg (point))) |
| 757 |
|
| 758 |
(defun mew-decode-flowed-remove-stuffed () |
| 759 |
(if (and (char-after) (char-equal (char-after) mew-flowed-stuffed)) |
| 760 |
(delete-char 1))) |
| 761 |
|
| 762 |
(defun mew-decode-flowed-soft-breakp (delsp) |
| 763 |
(let ((beg (point)) line) |
| 764 |
(end-of-line) |
| 765 |
(setq line (mew-buffer-substring beg (point))) |
| 766 |
(if (string= line mew-flowed-netnews) |
| 767 |
nil |
| 768 |
(if (and mew-decode-broken (member line mew-flowed-broken-list)) |
| 769 |
(progn |
| 770 |
(mew-xinfo-set-warning |
| 771 |
(cons "Illegal flowed lines\n" (mew-xinfo-get-warning))) |
| 772 |
nil) |
| 773 |
(if (and (char-before) (char-equal (char-before) mew-flowed-break)) |
| 774 |
(if delsp (1- (point)) (point)) |
| 775 |
nil))))) |
| 776 |
|
| 777 |
;;; |
| 778 |
;;; Decoding multipart |
| 779 |
;;; |
| 780 |
|
| 781 |
(defun mew-decode-multipart-boundary-regex (boundary) |
| 782 |
;; for broken MUAs which uses non-ASCII boundaries |
| 783 |
;; This works only on Emacs 21. |
| 784 |
(let ((mboundary (mew-set-string-multibyte boundary))) |
| 785 |
;; Some MUAs use invalid character like "<>". |
| 786 |
(when (or (string-match "[!-&*;<>@[-^`{-~]" mboundary) |
| 787 |
(not (string= mboundary boundary))) |
| 788 |
(if mew-decode-broken |
| 789 |
(mew-decode-warning-params "boundary" 'char) |
| 790 |
(mew-decode-error "Boundary has invalid character"))) |
| 791 |
(concat "^--" (regexp-quote mboundary) "\\(--\\|\\)[ \t]*$"))) |
| 792 |
|
| 793 |
(defun mew-decode-multipart-end-boundary-regex (boundary) |
| 794 |
(let ((mboundary (mew-set-string-multibyte boundary))) |
| 795 |
(concat "^--" (regexp-quote mboundary) "--[ \t]*$"))) |
| 796 |
|
| 797 |
(defun mew-decode-multipart-boundary-cont () |
| 798 |
(string= (mew-match-string 1) "")) |
| 799 |
|
| 800 |
(defun mew-decode-multipart-boundary-end () |
| 801 |
(string= (mew-match-string 1) "--")) |
| 802 |
|
| 803 |
(defun mew-decode-multipart-beginning-of-first-part (bregex ct) |
| 804 |
;; RFC 2046 says, "An exact match of the entire candidate |
| 805 |
;; line is not required; it is sufficient that the boundary |
| 806 |
;; appear in its entirety following the CRLF". |
| 807 |
(unless (and (re-search-forward bregex nil t) |
| 808 |
(mew-decode-multipart-boundary-cont)) |
| 809 |
(mew-decode-error (format "No first boundary for %s" ct))) |
| 810 |
(forward-line)) ;; the beginning of the first part |
| 811 |
|
| 812 |
(defun mew-decode-multipart-beginning-of-part (break) |
| 813 |
(if break |
| 814 |
() ;; close-delimiter need not be followed by CRLF |
| 815 |
(forward-line))) ;; the beginning of the next part |
| 816 |
|
| 817 |
(defun mew-decode-multipart-end-of-part (break) |
| 818 |
(unless break (forward-line -1)) |
| 819 |
(beginning-of-line) |
| 820 |
(forward-char -1) ;; skip the preceding CRLF |
| 821 |
;; the end of the part |
| 822 |
) |
| 823 |
|
| 824 |
(defun mew-decode-multipart-singlepart (break start cnt dct) |
| 825 |
(save-excursion |
| 826 |
(mew-decode-multipart-end-of-part break) |
| 827 |
(save-restriction |
| 828 |
(narrow-to-region start (point)) |
| 829 |
(goto-char (point-min)) |
| 830 |
;; the beginning of the part |
| 831 |
(mew-decode-singlepart cnt dct nil)))) |
| 832 |
|
| 833 |
(defun mew-decode-multipart-duplicate-boundary (start cnt dct err boundary bregex) |
| 834 |
(if (not (string-match "^No first boundary" (nth 1 err))) |
| 835 |
(error "") |
| 836 |
;; duplicate boundary error recovering, sigh |
| 837 |
(widen) ;; breaking save-restriction |
| 838 |
(goto-char start) ;; breaking save-excursion |
| 839 |
(let ((eregex (mew-decode-multipart-end-boundary-regex boundary)) |
| 840 |
break part) |
| 841 |
(if (not (and (re-search-forward eregex nil t) |
| 842 |
(forward-line) |
| 843 |
(re-search-forward bregex nil t))) |
| 844 |
(error "") |
| 845 |
(mew-xinfo-set-warning |
| 846 |
(cons "Duplicate boundary\n" (mew-xinfo-get-warning))) |
| 847 |
(mew-xinfo-set-decode-err nil) |
| 848 |
(setq break (mew-decode-multipart-boundary-end)) |
| 849 |
(mew-decode-multipart-beginning-of-part break) |
| 850 |
(setq part (mew-decode-multipart-singlepart break start cnt dct)) |
| 851 |
(list part break))))) |
| 852 |
|
| 853 |
(defun mew-decode-multipart-alternative-choice (part prefpart lastpref lastatpref) |
| 854 |
(let ((pref (or (mew-multipart-alternative-preference part) |
| 855 |
(length mew-mime-multipart-alternative-list))) |
| 856 |
(atpref (mew-mime-external-body-preference part))) |
| 857 |
;; returns '(prefpart lastpref lastatpref) |
| 858 |
(cond |
| 859 |
((or (null prefpart) (<= pref lastpref)) |
| 860 |
(list part pref atpref)) |
| 861 |
((and (= pref lastpref) atpref |
| 862 |
(or (null lastatpref) (<= atpref lastatpref))) |
| 863 |
;; If external-body and internal-body are alternative, |
| 864 |
;; what should we do? |
| 865 |
(list part lastpref atpref)) |
| 866 |
(t |
| 867 |
(list prefpart lastpref lastatpref))))) |
| 868 |
|
| 869 |
(defun mew-decode-multipart-alternative-part (prefpart syntax) |
| 870 |
(let* ((beg (mew-syntax-get-begin prefpart)) |
| 871 |
(ctl (mew-syntax-get-ct prefpart)) |
| 872 |
(ct (mew-syntax-get-value ctl 'cap)) |
| 873 |
(multibeg (mew-syntax-get-begin syntax))) |
| 874 |
(mew-syntax-set-begin prefpart (cons beg multibeg)) |
| 875 |
(mew-xinfo-set-info |
| 876 |
(cons |
| 877 |
(format "%s in Multipart/Alternative as a singlepart\n" ct) |
| 878 |
(mew-xinfo-get-info))) |
| 879 |
prefpart)) |
| 880 |
|
| 881 |
(defun mew-decode-multipart (syntax cnt &optional dct) |
| 882 |
(let* ((case-fold-search nil) ;; boundary is case sensitive |
| 883 |
(ctl (mew-syntax-get-ct syntax)) |
| 884 |
(ct (mew-syntax-get-value ctl 'cap)) |
| 885 |
(boundary (mew-syntax-get-param ctl "boundary")) |
| 886 |
(count 0) |
| 887 |
(parts []) part |
| 888 |
(use-alt (and (mew-dinfo-get-use-alt) (string= ct mew-ct-mla))) |
| 889 |
prefpart lastpref lastatpref |
| 890 |
bregex start break) |
| 891 |
(unless boundary |
| 892 |
(mew-decode-error "No boundary parameter for multipart")) |
| 893 |
(mew-syntax-set-key syntax 'multi) |
| 894 |
(setq bregex (mew-decode-multipart-boundary-regex boundary)) |
| 895 |
(mew-decode-multipart-beginning-of-first-part bregex ct) |
| 896 |
(setq start (point)) ;; the beginning of the first part |
| 897 |
(catch 'multipart |
| 898 |
(while (re-search-forward bregex nil t) |
| 899 |
(setq break (mew-decode-multipart-boundary-end)) |
| 900 |
(mew-decode-multipart-beginning-of-part break) |
| 901 |
(condition-case err |
| 902 |
(setq part (mew-decode-multipart-singlepart break start cnt dct)) |
| 903 |
(error |
| 904 |
;; rescue duplicate boundary |
| 905 |
(let ((pb (mew-decode-multipart-duplicate-boundary start cnt dct err boundary bregex))) |
| 906 |
(mew-set '(part break) pb)))) |
| 907 |
(setq count (1+ count)) |
| 908 |
(setq start (point)) ;; the beginning of the part |
| 909 |
(if use-alt |
| 910 |
(let ((pll (mew-decode-multipart-alternative-choice part prefpart lastpref lastatpref))) |
| 911 |
(mew-set '(prefpart lastpref lastatpref) pll)) |
| 912 |
(setq parts (vconcat parts (vector part)))) |
| 913 |
(when break |
| 914 |
(if use-alt |
| 915 |
(throw 'multipart (mew-decode-multipart-alternative-part prefpart syntax)) |
| 916 |
(throw 'multipart (vconcat syntax parts))))) |
| 917 |
;; Let's try to decode the first part even if boundary is not found. |
| 918 |
;; This should save truncated messages. |
| 919 |
(when (= count 0) |
| 920 |
(save-restriction |
| 921 |
(narrow-to-region start (point-max)) |
| 922 |
(goto-char (point-min)) |
| 923 |
(mew-decode-singlepart cnt dct nil))) |
| 924 |
(mew-decode-error (format "No last boundary for %s" ct))))) |
| 925 |
|
| 926 |
;;; |
| 927 |
;;; Privacy services |
| 928 |
;;; |
| 929 |
|
| 930 |
(mew-defstruct privacy-dinfo ct proto result) |
| 931 |
|
| 932 |
(defun mew-decode-multipart-encrypted (syntax cnt) |
| 933 |
;; called in narrowed region |
| 934 |
;; |
| 935 |
;; CT: M/E; proto; bound; |
| 936 |
;; |
| 937 |
;;(cur)--bound |
| 938 |
;; (the key part) |
| 939 |
;; --bound |
| 940 |
;; (the encrypted part) |
| 941 |
;; --bound-- |
| 942 |
(let* ((case-fold-search nil) ;; boundary is case sensitive |
| 943 |
(ctl (mew-syntax-get-ct syntax)) |
| 944 |
(boundary (mew-syntax-get-param ctl "boundary")) |
| 945 |
(switch mew-decode-multipart-encrypted-switch) |
| 946 |
file1 file2 file3 syntax1 syntax3 func unknown existp proto |
| 947 |
start result file3result privacy bregex) |
| 948 |
(unless boundary |
| 949 |
(mew-decode-error "No boundary parameter for multipart")) |
| 950 |
(setq bregex (mew-decode-multipart-boundary-regex boundary)) |
| 951 |
;; |
| 952 |
(unless (and (re-search-forward bregex nil t) |
| 953 |
(mew-decode-multipart-boundary-cont)) |
| 954 |
(mew-decode-error "No first boundary for Multipart/Encrypted")) |
| 955 |
(forward-line) ;; the beginning of the key part |
| 956 |
(setq start (point)) |
| 957 |
;; |
| 958 |
(unless (and (re-search-forward bregex nil t) |
| 959 |
(mew-decode-multipart-boundary-cont)) |
| 960 |
(mew-decode-error "No second boundary for Multipart/Encrypted")) |
| 961 |
(beginning-of-line) |
| 962 |
(setq syntax1 (mew-decode-security-singlepart start (1- (point)))) |
| 963 |
(setq proto (mew-syntax-get-value (mew-syntax-get-ct syntax1) 'cap)) |
| 964 |
(setq func (mew-decode-get-security-func proto switch)) |
| 965 |
(setq existp (mew-decode-get-security-existence proto switch)) |
| 966 |
(if func |
| 967 |
(if existp (setq file1 (mew-save-decode-form syntax1))) |
| 968 |
(setq unknown t)) |
| 969 |
(forward-line) ;; the beginning of the encrypted part |
| 970 |
(setq start (point)) |
| 971 |
;; |
| 972 |
(unless (and (re-search-forward bregex nil t) |
| 973 |
(mew-decode-multipart-boundary-end)) |
| 974 |
(mew-decode-error "No third boundary for Multipart/Encrypted")) |
| 975 |
(beginning-of-line) |
| 976 |
(if (and func existp) |
| 977 |
(setq file2 (mew-save-decode-form |
| 978 |
(mew-decode-security-singlepart start (1- (point)))))) |
| 979 |
;; |
| 980 |
(delete-region (point-min) (point-max)) |
| 981 |
;; |
| 982 |
;; Call protocol function |
| 983 |
(cond |
| 984 |
(unknown |
| 985 |
(setq result (concat "unknown protocol " proto))) |
| 986 |
((not existp) |
| 987 |
(setq result (concat (mew-decode-get-security-prog proto switch) |
| 988 |
" does not exist"))) |
| 989 |
(t |
| 990 |
(setq file3result (funcall func file1 file2)) |
| 991 |
(mew-set '(file3 result) file3result))) |
| 992 |
;; |
| 993 |
(if (and func existp (file-exists-p file3)) |
| 994 |
(mew-flet |
| 995 |
(mew-insert-file-contents file3) |
| 996 |
(put-text-property (point-min) (point-max) 'mew-noncontents nil) |
| 997 |
;; because of RICH functionality of RFC1847... Gee dirty! |
| 998 |
(mew-decode-crlf-magic)) |
| 999 |
(insert "\n") ;; CT: text/plain; charset=us-ascii |
| 1000 |
(insert "Multipart/Encrypted could not be decrypted.\n") |
| 1001 |
(mew-xinfo-set-not-decrypted t)) |
| 1002 |
;; Throw away garbage |
| 1003 |
(mew-delete-file file1) |
| 1004 |
(mew-delete-file file2) |
| 1005 |
(mew-delete-file file3) |
| 1006 |
;; Analyze the decrypted part |
| 1007 |
(goto-char (point-min)) |
| 1008 |
(setq syntax3 (mew-decode-singlepart cnt nil nil)) |
| 1009 |
(setq privacy (mew-syntax-get-privacy syntax3)) |
| 1010 |
(if privacy (setq result (concat result "\n\t"))) |
| 1011 |
(mew-syntax-set-privacy |
| 1012 |
syntax3 (cons (mew-make-privacy-dinfo :ct mew-ct-mle :proto proto :result result) privacy)) |
| 1013 |
syntax3)) |
| 1014 |
|
| 1015 |
(defun mew-decode-multipart-signed (syntax cnt) |
| 1016 |
;; called in narrowed region |
| 1017 |
;; |
| 1018 |
;; CT: M/S; proto; bound; micalg; |
| 1019 |
;; |
| 1020 |
;;(cur)--bound |
| 1021 |
;; (the signed part) |
| 1022 |
;; --bound |
| 1023 |
;; (the key part) |
| 1024 |
;; --bound-- |
| 1025 |
(let* ((case-fold-search nil) ;; boundary is case sensitive |
| 1026 |
(ctl (mew-syntax-get-ct syntax)) |
| 1027 |
(boundary (mew-syntax-get-param ctl "boundary")) |
| 1028 |
(switch mew-decode-multipart-signed-switch) |
| 1029 |
file1 file2 syntax2 syntax3 func unknown existp proto |
| 1030 |
end1 start2 result privacy bregex) |
| 1031 |
(unless boundary |
| 1032 |
(mew-decode-error "No boundary parameter for multipart")) |
| 1033 |
(setq bregex (mew-decode-multipart-boundary-regex boundary)) |
| 1034 |
;; |
| 1035 |
(unless (and (re-search-forward bregex nil t) |
| 1036 |
(mew-decode-multipart-boundary-cont)) |
| 1037 |
(mew-decode-error2 "No first boundary for Multipart/Signed")) |
| 1038 |
(forward-line) ;; the beginning of the signed part |
| 1039 |
(delete-region (point-min) (point)) ;; deleting content-header |
| 1040 |
(goto-char (point-min)) ;; just in case |
| 1041 |
;; |
| 1042 |
(unless (and (re-search-forward bregex nil t) |
| 1043 |
(mew-decode-multipart-boundary-cont)) |
| 1044 |
(mew-decode-error2 "No second boundary for Multipart/Signed")) |
| 1045 |
(beginning-of-line) |
| 1046 |
(setq end1 (1- (point))) ;; the end of the signed part |
| 1047 |
(forward-line) ;; the beginning of the key part |
| 1048 |
(setq start2 (point)) |
| 1049 |
;; |
| 1050 |
(unless (and (re-search-forward bregex nil t) |
| 1051 |
(mew-decode-multipart-boundary-end)) |
| 1052 |
(mew-decode-error2 "No third boundary for Multipart/Signed")) |
| 1053 |
(beginning-of-line) ;; the end of the encrypted part + 1 |
| 1054 |
(setq syntax2 (mew-decode-security-singlepart start2 (1- (point)))) |
| 1055 |
(setq proto (mew-syntax-get-value (mew-syntax-get-ct syntax2) 'cap)) |
| 1056 |
(setq func (mew-decode-get-security-func proto switch)) |
| 1057 |
(setq existp (mew-decode-get-security-existence proto switch)) |
| 1058 |
(if func |
| 1059 |
(when existp |
| 1060 |
(setq file1 (mew-save-transfer-form (point-min) end1 'retain)) |
| 1061 |
(setq file2 (mew-save-decode-form syntax2 (concat file1 ".sig")))) |
| 1062 |
(setq unknown t)) |
| 1063 |
;; |
| 1064 |
(delete-region end1 (point-max)) |
| 1065 |
;; Now the signed part only |
| 1066 |
;; Call protocol function |
| 1067 |
(cond |
| 1068 |
(unknown |
| 1069 |
(setq result (concat "unknown protocol " proto))) |
| 1070 |
((not existp) |
| 1071 |
(setq result (concat (mew-decode-get-security-prog proto switch) |
| 1072 |
" does not exist"))) |
| 1073 |
(t |
| 1074 |
(setq result (funcall func file1 file2)))) |
| 1075 |
;; Throw away garbage |
| 1076 |
(mew-delete-file file1) |
| 1077 |
(mew-delete-file file2) |
| 1078 |
;; Analyze the signed part |
| 1079 |
(goto-char (point-min)) |
| 1080 |
(setq syntax3 (mew-decode-singlepart cnt nil nil)) |
| 1081 |
(setq privacy (mew-syntax-get-privacy syntax3)) |
| 1082 |
(if privacy (setq result (concat result "\n\t"))) |
| 1083 |
(mew-syntax-set-privacy |
| 1084 |
syntax3 (cons (mew-make-privacy-dinfo :ct mew-ct-mls :proto proto :result result) privacy)) |
| 1085 |
syntax3)) |
| 1086 |
|
| 1087 |
(defun mew-decode-security-singlepart (beg end) |
| 1088 |
(save-excursion |
| 1089 |
(save-restriction |
| 1090 |
(narrow-to-region beg end) |
| 1091 |
(goto-char (point-min)) |
| 1092 |
(mew-decode-singlepart 0)))) ;; 0 is dummy |
| 1093 |
|
| 1094 |
(defun mew-save-decode-form (syntax &optional file) |
| 1095 |
(mew-flet |
| 1096 |
(unless file (setq file (mew-make-temp-name))) |
| 1097 |
(write-region (mew-syntax-get-begin syntax) |
| 1098 |
(mew-syntax-get-end syntax) |
| 1099 |
file nil 'no-msg) |
| 1100 |
file)) |
| 1101 |
|
| 1102 |
(defun mew-decode-crlf-magic () |
| 1103 |
(let ((case-fold-search t) |
| 1104 |
(cte mew-7bit) |
| 1105 |
key start match) |
| 1106 |
(save-excursion |
| 1107 |
(goto-char (point-min)) |
| 1108 |
(catch 'header |
| 1109 |
(while (re-search-forward |
| 1110 |
"^\r?$\\|^Content-Transfer-Encoding:[ \t]*" nil t) |
| 1111 |
(setq key (mew-match-string 0)) |
| 1112 |
(setq start (match-end 0)) |
| 1113 |
(when (string-match "^\r?$" key) |
| 1114 |
(save-restriction |
| 1115 |
(if (string-match mew-bin cte) ;; cte may include \r |
| 1116 |
(narrow-to-region (point-min) (1+ start)) |
| 1117 |
(narrow-to-region (point-min) (point-max))) |
| 1118 |
(goto-char (point-min)) |
| 1119 |
(mew-crlf-to-lf)) |
| 1120 |
(throw 'header nil)) |
| 1121 |
(forward-line) |
| 1122 |
(mew-header-goto-next) |
| 1123 |
(setq match (mew-buffer-substring start (1- (point)))) |
| 1124 |
(setq cte (mew-addrstr-parse-value match))))))) |
| 1125 |
|
| 1126 |
(provide 'mew-decode) |
| 1127 |
|
| 1128 |
;;; Copyright Notice: |
| 1129 |
|
| 1130 |
;; Copyright (C) 1996-2015 Mew developing team. |
| 1131 |
;; All rights reserved. |
| 1132 |
|
| 1133 |
;; Redistribution and use in source and binary forms, with or without |
| 1134 |
;; modification, are permitted provided that the following conditions |
| 1135 |
;; are met: |
| 1136 |
;; |
| 1137 |
;; 1. Redistributions of source code must retain the above copyright |
| 1138 |
;; notice, this list of conditions and the following disclaimer. |
| 1139 |
;; 2. Redistributions in binary form must reproduce the above copyright |
| 1140 |
;; notice, this list of conditions and the following disclaimer in the |
| 1141 |
;; documentation and/or other materials provided with the distribution. |
| 1142 |
;; 3. Neither the name of the team nor the names of its contributors |
| 1143 |
;; may be used to endorse or promote products derived from this software |
| 1144 |
;; without specific prior written permission. |
| 1145 |
;; |
| 1146 |
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND |
| 1147 |
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 1148 |
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 1149 |
;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE |
| 1150 |
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 1151 |
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 1152 |
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 1153 |
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 1154 |
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 1155 |
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 1156 |
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 1157 |
|
| 1158 |
;;; mew-decode.el ends here |