Debian Codegraph @

mew-encode.el

raw
1 ;;; mew-encode.el --- MIME syntax encoder for Mew
2
3 ;; Author: Kazu Yamamoto <[email protected]>
4 ;; Created: Oct 2, 1996
5
6 ;;; Code:
7
8 (require 'mew)
9
10 (defvar mew-prog-mime-encode-switch
11 `((,mew-b64 "-b")
12 (,mew-qp "-q")
13 (,mew-xg "-g")))
14
15 (defvar mew-prog-mime-encode-text-switch
16 `((,mew-b64 "-b" "-t")
17 (,mew-qp "-q")
18 (,mew-xg "-g""-t")))
19
20 (defun mew-prog-mime-encode-get-opt (cte switch)
21 (cdr (mew-assoc-case-equal cte switch 0)))
22
23 (defvar mew-encode-multipart-encrypted-switch
24 `((,mew-ct-pge mew-pgp-encrypt)))
25
26 (defvar mew-encode-multipart-signed-switch
27 `((,mew-ct-pgs mew-pgp-sign mew-pgp-canonicalize)
28 (,mew-ct-sms mew-smime-detach-sign)))
29
30 ;;
31
32 (defun mew-encode-get-security-func (proto switch)
33 (nth 1 (mew-assoc-case-equal proto switch 0)))
34
35 (defun mew-encode-get-canonicalize-func (proto switch)
36 (nth 2 (mew-assoc-case-equal proto switch 0)))
37
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 ;;;
40 ;;; MIME encoder
41 ;;;
42
43 (defun mew-encode-error (error-msg)
44 (mew-tinfo-set-encode-err error-msg)
45 (error ""))
46
47 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48 ;;;
49 ;;; RFC 2822 comments
50 ;;;
51
52 ;; RFC 2822 defines the limitations of the number of fields.
53 ;;
54 ;; * Overriding
55 ;; Date: (exactly once)
56 ;; Message-Id:
57 ;;
58 ;; * Put multiple fields to a single field
59 ;; From: (exactly once)
60 ;; Reply-To:
61 ;; To:
62 ;; Cc:
63 ;;
64 ;; * Error and undo
65 ;; Sender: (if exist, exactly one address)
66 ;;
67 ;; In-Reply-To:
68 ;; References:
69 ;; Subject:
70 ;;
71
72 (defconst mew-multiple-field-error-list
73 (list mew-subj: mew-in-reply-to: mew-references:))
74
75 (defconst mew-multiple-field-combine-list
76 (list mew-from: mew-reply-to: mew-to: mew-cc:))
77
78 (defun mew-header-count-field (field)
79 (let ((case-fold-search t)
80 (regex (format "^%s" field))
81 (i 0))
82 (goto-char (point-min))
83 (while (re-search-forward regex nil t)
84 (setq i (1+ i))
85 (forward-line))
86 i))
87
88 (defun mew-header-combine-field (field)
89 (let ((case-fold-search t)
90 (regex (format "^%s" field))
91 beg med here value)
92 (goto-char (point-min))
93 (when (re-search-forward regex nil t)
94 (forward-line)
95 (mew-header-goto-next)
96 (setq here (1- (point)))
97 (while (re-search-forward regex nil t)
98 (setq beg (match-beginning 0))
99 (setq med (match-end 0))
100 (forward-line)
101 (mew-header-goto-next)
102 (setq value (mew-buffer-substring med (1- (point))))
103 (delete-region beg (point))
104 (save-excursion
105 (goto-char here)
106 (insert ",\n\t" value)
107 (setq here (point)))))))
108
109 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
110 ;;;
111 ;;; Header sanity check
112 ;;;
113
114 (defun mew-encode-remove-invalid-fields ()
115 (when (mew-header-end)
116 (save-excursion
117 (save-restriction
118 (narrow-to-region (point-min) (mew-header-end))
119 (dolist (field mew-multiple-field-error-list)
120 (if (> (mew-header-count-field field) 1)
121 (mew-encode-error (format "Multiple %s!" field))))
122 (mapcar 'mew-header-combine-field mew-multiple-field-combine-list)))))
123
124 (defun mew-encode-ask-subject ()
125 (when (and mew-ask-subject (not (mew-header-existp mew-subj:)))
126 ;; Subject: does not exist
127 (let ((subj (read-string (concat mew-subj: " "))))
128 (mew-header-replace-value mew-subj: subj))))
129
130 (defun mew-encode-fcc-folder-check (fld)
131 (let ((folder (mew-canonicalize-folder fld)))
132 (when (and mew-ask-fcc
133 (mew-folder-localp folder)
134 (not (file-directory-p (mew-expand-folder folder))))
135 (if (y-or-n-p (format "%s does not exist. Create it? " folder))
136 (mew-local-folder-check folder)
137 (mew-encode-error "Edit Fcc:")))
138 folder))
139
140 (defun mew-encode-ask-fcc (resentp)
141 (let* ((target (if resentp mew-resent-fcc: mew-fcc:))
142 (folders (mew-header-get-value target)))
143 (when folders
144 (mapcar
145 'mew-encode-fcc-folder-check
146 (mew-addrstr-parse-value-list2 folders)))))
147
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149 ;;;
150 ;;; To:, Cc:
151 ;;;
152
153 (defvar mew-draft-append-domain-p t)
154
155 (defun mew-draft-append-domain (addr)
156 (if (string-match "@" addr)
157 addr
158 (if mew-draft-append-domain-p
159 (concat addr "@" (mew-mail-domain (mew-tinfo-get-case)))
160 (throw 'jump addr))))
161
162 (defun mew-encode-canonicalize-address (resentp)
163 (catch 'jump
164 (let ((case-fold-search t)
165 (regex (mew-make-field-regex
166 (if resentp mew-resent-dest:-list mew-destination:-list)))
167 key start vals val addrs addr ret insl ins prefix suffix)
168 (save-excursion
169 (save-restriction
170 (narrow-to-region (point-min) (mew-header-end))
171 (goto-char (point-min))
172 (while (re-search-forward regex nil t)
173 (setq key (match-string 1))
174 (setq start (match-end 0))
175 (forward-line)
176 (while (looking-at mew-lwsp)
177 (delete-char -1)
178 (forward-line))
179 (setq val (mew-buffer-substring start (1- (point))))
180 (delete-region start (1- (point)))
181 (backward-char 1)
182 ;; single quote is allowed for local-part
183 (setq vals (mapcar 'mew-chop (mew-split-quoted val ?, ?: ?\; 'no-single)))
184 (dolist (val vals)
185 (setq ins nil addrs nil)
186 (cond
187 ;; phrase:addr1,addr2;
188 ((string-match "[^:]+:[^;]+;" val)
189 (let ((mew-alias-expand-prefix nil))
190 (setq addrs (mew-alias-expand val mew-addrbook-alist 0))
191 (setq addrs (mapcar 'mew-draft-append-domain addrs))
192 (setq ins (list (concat mew-alias-expand-prefix ":;")))))
193 ;; Name <addr>
194 ((and (setq addr (mew-addrstr-parse-address val))
195 (string-match (concat "\\(.*<\\)" (regexp-quote addr) "\\(>.*\\)")
196 val))
197 (setq prefix (match-string 1 val))
198 (setq suffix (match-string 2 val))
199 (setq addr (mew-draft-append-domain addr))
200 (setq addrs (list addr))
201 (setq ins (list (concat prefix addr suffix))))
202 ;; addr
203 (t
204 (let ((mew-alias-expand-prefix nil))
205 (setq addrs (mew-alias-expand val mew-addrbook-alist 0))
206 (setq addrs (mapcar 'mew-draft-append-domain addrs))
207 (if mew-alias-expand-prefix
208 (setq ins (list (concat mew-alias-expand-prefix ":;")))
209 (setq ins (copy-sequence addrs))))))
210 (setq insl (nconc insl ins))
211 (unless (string-match "bcc" key) ;; Removing Bcc:
212 (setq ret (nconc ret addrs)))) ;; end of dolist
213 (insert " " (mapconcat 'identity insl ", "))
214 (setq insl nil)
215 (forward-line))))
216 ret)))
217
218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
219 ;;;
220 ;;; Dcc:, Bcc:
221 ;;;
222
223 (defun mew-encode-delete-dcc (resentp)
224 (let* ((target (list (if resentp mew-resent-dcc: mew-dcc:)))
225 (dcc (mew-header-parse-address-list target)))
226 ;; Dcc: is already contained in recipients.
227 ;; So, just delete it.
228 ;; Delete {Resent-,}Dcc: anyway.
229 (mew-header-delete-lines (list mew-dcc: mew-resent-dcc:))
230 dcc))
231
232 (defun mew-encode-delete-bcc (resentp)
233 (let* ((target (list (if resentp mew-resent-bcc: mew-bcc:)))
234 (bcc (mew-header-parse-address-list target)))
235 (mew-header-delete-lines (list mew-bcc: mew-resent-bcc:)) ;; anyway
236 bcc))
237
238 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
239 ;;;
240 ;;; Message-Id:, Date:
241 ;;;
242
243 (defun mew-encode-id-date (_pnm msgid &optional resentp)
244 (let ((time (current-time)))
245 (cond
246 (resentp
247 (mew-header-delete-lines (list mew-resent-date: mew-resent-message-id:))
248 (save-excursion
249 (goto-char (point-min))
250 (mew-header-insert mew-resent-date: (mew-time-ctz-to-rfc time))
251 (mew-header-insert mew-resent-message-id: msgid)))
252 (t
253 (mew-header-delete-lines (list mew-date: mew-message-id:))
254 (save-excursion
255 (goto-char (point-min))
256 (mew-header-insert mew-date: (mew-time-ctz-to-rfc time))
257 (mew-header-insert mew-message-id: msgid))))
258 (list msgid (mew-time-ctz-to-logtime time))))
259
260 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
261 ;;;
262 ;;; From:, Sender:
263 ;;;
264
265 (defun mew-encode-check-sender (resentp)
266 (if (< 1 (length (mew-addrstr-parse-address-list
267 (mew-header-get-value
268 (if resentp mew-resent-sender: mew-sender:)))))
269 (mew-encode-error "Sender: must contain one address!")))
270
271 (defun mew-encode-from (case resentp)
272 (unless (mew-header-existp mew-from:)
273 (let ((from (mew-from case)))
274 (save-excursion
275 (goto-char (point-min))
276 (if resentp
277 (mew-header-insert mew-resent-from: from)
278 (mew-header-insert mew-from: from))))))
279
280 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
281 ;;;
282 ;;; Learning aliases
283 ;;;
284
285 ;; xxx Bcc: and/or Dcc:?
286 (defun mew-encode-learn-aliases (resentp)
287 (if (and mew-use-auto-alias mew-addrbook-append-domain-p)
288 ;; If mew-addrbook-append-domain-p is nil, automatic
289 ;; short names would be conflicted to local users.
290 (mapcar 'mew-addrbook-alias-add
291 (mew-header-parse-address-list
292 (if resentp
293 (list mew-resent-to: mew-resent-cc:)
294 (list mew-to: mew-cc:))))))
295
296 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
297 ;;;
298 ;;; Misc
299 ;;;
300
301 (defun mew-encode-set-privacy (pnm privacy case)
302 (unless (or (mew-syntax-get-privacy mew-encode-syntax) ;; specified
303 ;; encryption previously failed, so bypass.
304 (and (null privacy) (mew-tinfo-get-privacy-err)))
305 (let (type)
306 (cond
307 (privacy
308 (setq type privacy))
309 ((mew-tinfo-get-privacy-type)
310 (setq type (mew-tinfo-get-privacy-type)))
311 ((and (mew-tinfo-get-encrypted-p) (mew-protect-privacy-encrypted case))
312 (setq type (mew-protect-privacy-encrypted-type case)))
313 ((mew-protect-privacy-always case)
314 (setq type (mew-protect-privacy-always-type case))))
315 (mew-syntax-set-privacy
316 mew-encode-syntax
317 (mew-pcdb-ct (mew-pcdb-by-service type)))
318 ;; recipients are ignored when signing
319 (mew-syntax-set-decrypters
320 mew-encode-syntax (mew-smtp-get-recipients pnm)))))
321
322 (defun mew-encode-make-header (&optional addsep resentp)
323 (unless (mew-header-existp mew-mv:)
324 (goto-char (mew-header-end))
325 (mew-header-insert mew-mv: mew-mv:-num))
326 (mew-header-encode-region (point-min) (mew-header-end) resentp)
327 (cond
328 (addsep ;; reedit
329 ;; To:
330 ;; Content-*
331 ;; ----
332 (mew-header-clear) ;; mew-in-header-p() returns nil
333 ;; To:
334 ;; Content-*
335 (insert "\n"))
336 (t
337 ;; To:
338 ;; ----
339 ;; Content-*
340 (mew-header-clear) ;; mew-in-header-p returns nil
341 ;; To:
342 ;; Content-*
343 ))
344 (mew-header-goto-end)
345 (mew-highlight-header-region (point-min) (point)))
346
347 (defun mew-encode-save-draft ()
348 (mew-frwlet mew-cs-dummy mew-cs-text-for-write
349 (write-region (point-min) (point-max) (buffer-file-name) nil 'no-msg)
350 (clear-visited-file-modtime)
351 (mew-delete-file buffer-auto-save-file-name)
352 (set-buffer-modified-p nil)))
353
354 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
355 ;;;
356 ;;; Encoding a message
357 ;;;
358
359 (defun mew-smtp-encode (pnm case resentp fcc &optional privacy signer headerp)
360 (let ((ret t))
361 (if (mew-debug 'encode)
362 (let ((debug-on-error t))
363 (mew-smtp-encode-message pnm case resentp fcc privacy signer headerp))
364 (condition-case nil
365 (mew-smtp-encode-message pnm case resentp fcc privacy signer headerp)
366 (error
367 (mew-encode-undo)
368 (message "%s" (mew-tinfo-get-encode-err))
369 (setq ret nil))
370 (quit
371 (mew-encode-undo)
372 (message "quit")
373 (setq ret nil))))
374 ret))
375
376 (defun mew-smtp-encode-message (pnm case resentp fcc &optional privacy signer headerp)
377 (mew-set-buffer-multibyte t)
378 (if (buffer-modified-p) (save-buffer)) ;; to make backup
379 (widen)
380 (let (multip recipients msgid logtime)
381 (mew-smtp-set-raw-header
382 pnm (mew-buffer-substring (point-min) (mew-header-end)))
383 (unless headerp
384 ;; Let's backup
385 (if (mew-attach-p)
386 (progn
387 (setq multip t)
388 (mew-attach-clear))
389 (unless mew-encode-syntax
390 (setq mew-encode-syntax
391 (mew-encode-syntax-single "text.txt" (list mew-ct-txt)))))
392 (mew-encode-make-backup))
393 ;; Errors can be caused from here.
394 (goto-char (point-min))
395 (mew-encode-remove-invalid-fields)
396 ;; Destination check
397 (setq recipients (mew-encode-canonicalize-address resentp))
398 ;; Bcc: is not included.
399 (mew-smtp-set-recipients pnm recipients)
400 (mew-smtp-set-orig-recipients pnm recipients)
401 (cond
402 ((null recipients)
403 (mew-encode-error "No recipient!"))
404 ((stringp recipients)
405 (mew-encode-error (format "'%s' is not in the right form!" recipients))))
406 ;; Header modifications which are not remained.
407 (mew-header-delete-lines (list mew-fcc: mew-resent-fcc:)) ;; anyway
408 (mew-encode-delete-dcc resentp)
409 (mew-smtp-set-bcc pnm (mew-encode-delete-bcc resentp))
410 (mew-smtp-set-fcc pnm fcc)
411 ;;
412 (mew-encode-check-sender resentp)
413 (mew-encode-from case resentp)
414 ;;
415 (mew-set '(msgid logtime) (mew-encode-id-date pnm (mew-smtp-message-id case) resentp))
416 (mew-smtp-set-msgid pnm msgid)
417 (mew-smtp-set-logtime pnm logtime)
418 ;;
419 (goto-char (mew-header-end))
420 (forward-line) ;; necessary for PGP
421 ;;
422 (message "Making a message...")
423 ;; save syntax before setting privacy
424 (unless headerp
425 (mew-encode-set-privacy pnm privacy case)
426 (let (mew-inherit-encode-pgp-signer
427 mew-inherit-encode-smime-signer)
428 (setq mew-inherit-encode-pgp-signer
429 (or signer
430 (mew-pgp-signer (mew-tinfo-get-case))
431 (mew-get-my-address)))
432 (setq mew-inherit-encode-smime-signer
433 (or signer
434 (mew-smime-signer (mew-tinfo-get-case))
435 (mew-get-my-address)))
436 (goto-char (mew-header-end)) ;; due to invalid null lines in the header
437 (forward-line)
438 (if multip
439 (mew-encode-make-multi)
440 (mew-encode-make-single))))
441 (mew-encode-make-header headerp resentp)
442 ;; Learn aliases after no error occurred
443 (mew-encode-learn-aliases resentp)
444 (mew-encode-save-draft)
445 (mew-overlay-delete-buffer)
446 (message "Making a message...done")))
447
448 (defun mew-nntp2-encode (pnm case fcc &optional privacy signer headerp)
449 (let ((ret t))
450 (if (mew-debug 'encode)
451 (let ((debug-on-error t))
452 (mew-nntp2-encode-message pnm case fcc privacy signer headerp))
453 (condition-case nil
454 (mew-nntp2-encode-message pnm case fcc privacy signer headerp)
455 (error
456 (mew-encode-undo)
457 (message "%s" (mew-tinfo-get-encode-err))
458 (setq ret nil))
459 (quit
460 (mew-encode-undo)
461 (message "quit")
462 (setq ret nil))))
463 ret))
464
465 (defun mew-nntp2-encode-message (pnm case fcc &optional privacy signer headerp)
466 (mew-set-buffer-multibyte t)
467 (if (buffer-modified-p) (save-buffer)) ;; to make backup
468 (widen)
469 (let (multip newsgroups msgid logtime)
470 (mew-nntp2-set-raw-header
471 pnm (mew-buffer-substring (point-min) (mew-header-end)))
472 ;; Let's backup
473 (unless headerp
474 (if (mew-attach-p)
475 (progn
476 (setq multip t)
477 (mew-attach-clear))
478 (setq mew-encode-syntax
479 (mew-encode-syntax-single "text.txt" (list mew-ct-txt))))
480 (mew-encode-make-backup))
481 ;; Errors can be caused from here.
482 (goto-char (point-min))
483 (mew-encode-remove-invalid-fields)
484 ;; Newsgroups check
485 (setq newsgroups (mew-header-get-value mew-newsgroups:))
486 (mew-nntp2-set-newsgroups pnm newsgroups)
487 (unless newsgroups (mew-encode-error "No newsgroups!"))
488 ;; Header modifications which are not remained.
489 (mew-header-delete-lines (list mew-fcc: mew-resent-fcc:)) ;; anyway
490 (mew-nntp2-set-fcc pnm fcc)
491 ;;
492 (mew-encode-from case nil)
493 ;;
494 (mew-set '(msgid logtime) (mew-encode-id-date pnm (mew-nntp-message-id case)))
495 (mew-nntp2-set-msgid pnm msgid)
496 (mew-nntp2-set-logtime pnm logtime)
497 ;;
498 (goto-char (mew-header-end))
499 (forward-line) ;; necessary for PGP
500 ;;
501 (message "Making a message...")
502 ;; save syntax before setting privacy
503 (unless headerp
504 (mew-encode-set-privacy pnm privacy case)
505 (let (mew-inherit-encode-pgp-signer
506 mew-inherit-encode-smime-signer)
507 (setq mew-inherit-encode-pgp-signer
508 (or signer
509 (mew-pgp-signer (mew-tinfo-get-case))
510 (mew-get-my-address)))
511 (setq mew-inherit-encode-smime-signer
512 (or signer
513 (mew-smime-signer (mew-tinfo-get-case))
514 (mew-get-my-address)))
515 (goto-char (mew-header-end)) ;; due to invalid null lines in the header
516 (forward-line)
517 (if multip
518 (mew-encode-make-multi)
519 (mew-encode-make-single))))
520 (mew-encode-make-header nil)
521 (mew-encode-save-draft)
522 (mew-overlay-delete-buffer)
523 (message "Making a message...done")))
524
525 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
526 ;;;
527 ;;; Encoding singlepart
528 ;;;
529
530 (defun mew-encode-make-single ()
531 ;; Just after the header
532 ;; Using Multipart/Signed if the body is a single part.
533 (if (equal (mew-syntax-get-privacy mew-encode-syntax)
534 `((,mew-ct-smm ,mew-ct-smm-sig)))
535 (mew-syntax-set-privacy mew-encode-syntax `((,mew-ct-mls ,mew-ct-sms))))
536 (mew-encode-singlepart mew-encode-syntax nil nil t 'cover))
537
538 (defun mew-encode-charset-8bitp (charset)
539 (let ((case-fold-search t))
540 (if (or (mew-case-equal mew-us-ascii charset)
541 (string-match "^iso-2022" charset))
542 nil
543 t)))
544
545 (defun mew-encode-file-8bitp (file)
546 (if (and (mew-which-exec mew-prog-8bit) (file-readable-p file))
547 (eq (call-process mew-prog-8bit file nil nil "-8") 1)))
548
549 (defun mew-encode-limit-7bitp (privacy)
550 ;; Only parts to be signed FIRST are limited to 7bit.
551 (and privacy (mew-case-equal mew-ct-mls (car (car privacy)))))
552
553 (defvar mew-encode-max-line-length 990)
554 ;; RFC 2822 defines 1000 but Sendmail uses 990
555
556 (defvar mew-ask-encoding t)
557 (defvar mew-default-encoding mew-b64)
558
559 (defvar mew-draft-keep-text-charset nil)
560
561 (defun mew-encode-mime-body (ctl cte file no-encoding)
562 ;; If file is 't', target is buffered.
563 ;; text should be buffered
564 ;; - specified charset is a rare case
565 ;; - copy overhead may be small
566 (let* ((ct (mew-syntax-get-value ctl 'cap))
567 (textp (mew-ct-textp ct))
568 (charset (if textp (mew-syntax-get-param ctl "charset")))
569 (icharset (if textp (mew-syntax-get-param ctl "icharset")))
570 (linebasep (or textp (mew-ct-linebasep ct)))
571 (switch (if linebasep
572 mew-prog-mime-encode-text-switch
573 mew-prog-mime-encode-switch))
574 (beg (point))
575 buffer cs opt last flowed delsp flowed-delsp)
576 (if (and (stringp file)
577 (setq buffer (get-file-buffer file))
578 (buffer-modified-p buffer))
579 (with-current-buffer buffer
580 (save-buffer)))
581 (when textp
582 (when (and (stringp file) (file-readable-p file))
583 (when icharset
584 (setq cs (mew-charset-to-cs icharset))
585 (unless (mew-coding-system-p cs)
586 (mew-encode-error
587 (format "Unknown coding system %s in the body"
588 (symbol-name cs)))))
589 ;; If a user want to keep text charset,
590 (when (and mew-draft-keep-text-charset (null charset))
591 (let ((cs (mew-cs-strip-lineinfo
592 (mew-file-guess-coding-system file))))
593 (if cs (setq charset (mew-cs-to-charset cs)))))
594 (mew-frwlet (or cs mew-cs-autoconv) mew-cs-dummy
595 (mew-insert-file-contents file)))
596 ;; A user may specify charset to convey JIS X 0201 Katakana.
597 ;; So, we need to avoid the sanity check.
598 (unless charset
599 (if mew-use-charset-sanity-check
600 (mew-charset-sanity-check beg (point-max)))
601 (setq charset (mew-charset-guess-region beg (point-max))))
602 (setq cs (mew-charset-to-cs charset))
603 (when (string= mew-ct-txt ct)
604 (cond
605 ((mew-tinfo-get-flowed)
606 (setq flowed t)
607 (if (string= (mew-tinfo-get-flowed) "yes")
608 (setq delsp t)))
609 ((mew-tinfo-get-use-flowed)
610 (setq flowed-delsp (mew-encode-flowed beg (point-max) charset))
611 (mew-set '(flowed delsp) flowed-delsp))))
612 (unless (mew-coding-system-p cs)
613 (mew-encode-error
614 (format "Unknown coding system %s in the body" (symbol-name cs))))
615 (mew-cs-encode-region beg (point-max) cs))
616 ;; decide cte
617 (cond
618 (textp
619 (cond
620 ((and cte (or (mew-case-equal cte mew-b64) (mew-case-equal cte mew-qp)))
621 ;; user specified and in 7bit domain, so do nothing.
622 )
623 (t
624 (if (or (null cte) (mew-case-equal cte mew-bin))
625 ;; retain the user-specified cte
626 (setq cte (mew-charset-to-cte charset)))
627 (if (or (null cte) (mew-case-equal cte mew-bin))
628 ;; unknown charset
629 (setq cte mew-default-encoding))
630 (cond
631 (no-encoding
632 (if (mew-encode-charset-8bitp charset)
633 (setq cte mew-8bit)
634 (setq cte mew-7bit)))
635 (mew-inherit-7bit
636 (if (mew-case-equal cte mew-8bit)
637 (setq cte mew-default-encoding)))
638 (mew-use-8bit
639 (if (mew-encode-charset-8bitp charset)
640 (setq cte mew-8bit))))
641 (when (and (not no-encoding)
642 (or (mew-case-equal cte mew-7bit)
643 (mew-case-equal cte mew-8bit)))
644 (save-excursion
645 (goto-char beg)
646 (mew-set-buffer-multibyte nil)
647 (catch 'long-line
648 (while t
649 (setq last (point))
650 (if (/= (forward-line) 0) (end-of-line))
651 (when (>= (- (point) last) mew-encode-max-line-length) ;; including LF
652 (if (not mew-ask-encoding)
653 (setq cte mew-default-encoding)
654 (setq cte (mew-input-encoding)))
655 (throw 'long-line nil))
656 (if (eobp) (throw 'long-line nil))))
657 (mew-set-buffer-multibyte t))))))
658 ((string= ct mew-ct-msg)
659 (if (mew-encode-file-8bitp file)
660 (setq cte mew-8bit)
661 (setq cte mew-7bit)))
662 (t
663 (if (and no-encoding cte (mew-case-equal cte mew-b64))
664 (setq cte mew-bin))
665 ;; There are 7bit ascii bodies such as
666 ;; application/pgp-encrypted and message/external-body.
667 ;; If 7bit or 8bit, it should be linebase.
668 (if (null cte) (setq cte mew-7bit))))
669 (cond
670 ((mew-case-equal cte mew-bin)
671 (mew-frwlet mew-cs-binary mew-cs-dummy
672 (mew-insert-file-contents file)))
673 ((or (mew-case-equal cte mew-7bit) (mew-case-equal cte mew-8bit))
674 ;; Certainly linebase here.
675 (unless textp
676 (mew-frwlet mew-cs-text-for-read mew-cs-dummy
677 (mew-insert-file-contents file)))
678 (when (and (string= ct mew-ct-msg)
679 (or mew-inherit-7bit (not mew-use-8bit))
680 (mew-case-equal cte mew-8bit))
681 (save-restriction
682 (setq cte mew-7bit)
683 (narrow-to-region beg (point-max))
684 (mew-convert-message))))
685 ((and (mew-case-equal cte mew-b64) (fboundp 'base64-encode-region))
686 (unless textp
687 (mew-frwlet (if linebasep mew-cs-text-for-read mew-cs-binary) mew-cs-dummy
688 (mew-insert-file-contents file)))
689 (when linebasep
690 (goto-char beg)
691 (mew-lf-to-crlf))
692 (base64-encode-region beg (point-max))
693 (goto-char (point-max))
694 (insert "\n"))
695 ((mew-which-exec mew-prog-mime-encode)
696 (setq opt (mew-prog-mime-encode-get-opt cte switch))
697 (if (null opt)
698 (mew-encode-error (concat "Unknown CTE: " cte))
699 (when textp
700 (setq file (mew-make-temp-name))
701 (mew-frwlet mew-cs-dummy mew-cs-text-for-write
702 ;; NEVER use call-process-region for privacy reasons
703 (write-region beg (point-max) file nil 'no-msg))
704 (delete-region beg (point-max)))
705 (mew-piolet mew-cs-text-for-read mew-cs-dummy
706 (apply 'call-process mew-prog-mime-encode file t nil opt))
707 (if textp (mew-delete-file file))))
708 (t
709 (mew-encode-error (concat mew-prog-mime-encode " does not exist"))))
710 (list cte charset flowed delsp)))
711
712 (defun mew-encode-no-mime-encoding (privacy)
713 (let ((first (car privacy)))
714 (and (listp first)
715 (or (string= (car first) mew-ct-mle)
716 (string= (car first) mew-ct-smm)))))
717
718 (defun mew-broken-name (cdpl)
719 (let ((mew-encode-word-max-length 1000) ;; xxx
720 (file (nth 1 (assoc "filename" cdpl)))
721 name)
722 (when (and file (string-match mew-regex-nonascii file))
723 (setq name (car (mew-header-encode-string file)))
724 ;; name must not be double-quoted here.
725 ;; mew-heaer-insert will do this later
726 (list "name" name))))
727
728 (defun mew-encode-flowed (beg end charset)
729 "Encoding lines with RFC 3676"
730 (let (flowed delsp column)
731 (save-excursion
732 (goto-char beg)
733 (save-restriction
734 (narrow-to-region beg end)
735 (when (mew-encode-flowed-check)
736 (setq flowed t)
737 (setq delsp (mew-charset-to-delsp charset))
738 (setq column mew-flowed-fold-length)
739 (if delsp (setq column (1- column)))
740 (while (not (eobp))
741 (mew-encode-flowed-line column delsp)
742 (forward-line)))))
743 (list flowed delsp)))
744
745 (defun mew-encode-flowed-remove-trailing-sp ()
746 (while (and (not (bobp)) (= (char-before) mew-sp))
747 (delete-char -1)))
748
749 (defun mew-encode-flowed-line (column delsp)
750 (let (prefix beg)
751 (looking-at "^>*")
752 (setq prefix (mew-match-string 0))
753 (goto-char (match-end 0))
754 (if (looking-at " ")
755 (if (string= prefix "")
756 (insert mew-flowed-stuffed)
757 (progn
758 (setq prefix (format "%s%c" prefix mew-flowed-stuffed))
759 (forward-char)))
760 (when (looking-at "From ")
761 (insert mew-flowed-stuffed)
762 (setq prefix (format "%s%c" prefix mew-flowed-stuffed))))
763 (setq beg (point))
764 (save-excursion
765 (end-of-line)
766 (unless (and (= (- (point) beg) 3)
767 (string= (mew-buffer-substring beg (point)) "-- "))
768 (mew-encode-flowed-remove-trailing-sp)))
769 (move-to-column column)
770 (catch 'loop
771 (while (not (eolp))
772 (cond
773 (delsp
774 (while (> (current-column) column)
775 (backward-char 1))
776 (insert " \n"))
777 (t
778 (if (search-backward " " beg t)
779 (forward-char)
780 (unless (search-forward " " (save-excursion (end-of-line) (point)) t)
781 (throw 'loop (end-of-line))))
782 (insert "\n")))
783 (insert prefix)
784 (setq beg (point))
785 (move-to-column column)))))
786
787 (defun mew-encode-flowed-check ()
788 (catch 'loop
789 (save-excursion
790 (save-restriction
791 (narrow-to-region (point-min) (min (point-max) mew-file-max-size))
792 (goto-char (point-min))
793 (while (not (eobp))
794 (end-of-line)
795 (if (> (current-column) mew-flowed-fold-threshold)
796 (throw 'loop t))
797 (forward-line))))))
798
799 (defun mew-encode-singlepart (syntax &optional path depth buffered coverp)
800 ;; path is nil if called make-single or security multipart
801 ;; buffered is t if called make-single
802 (run-hook-with-args 'mew-encode-singlepart-hook
803 syntax path depth buffered)
804 (let* ((file (expand-file-name (mew-syntax-get-file syntax) path))
805 (ctl (mew-syntax-get-ct syntax))
806 (ct (mew-syntax-get-value ctl 'cap))
807 (cte (mew-syntax-get-cte syntax))
808 (cd (mew-syntax-get-cd syntax))
809 (cdpl (mew-syntax-get-cdp syntax))
810 (privacy (mew-syntax-get-privacy syntax))
811 (no-encoding (mew-encode-no-mime-encoding privacy))
812 (mew-inherit-7bit (mew-encode-limit-7bitp privacy))
813 (beg (point))
814 mret charset bodybeg cst ask-cst broken-name flowed delsp)
815 (setq mret (mew-encode-mime-body ctl cte (or buffered file) no-encoding))
816 (goto-char beg)
817 (mew-set '(cte charset flowed delsp) mret)
818 (setq ctl (mew-delete "icharset" ctl))
819 (when charset
820 (setq ctl (mew-syntax-get-params ctl))
821 (setq ctl (mew-delete "charset" ctl))
822 (setq ctl (cons ct (cons (list "charset" charset) ctl))))
823 (when flowed
824 (setq ctl (nconc ctl (list (list "format" "flowed"))))
825 (if delsp (setq ctl (nconc ctl (list (list "delsp" "yes"))))))
826 ;;
827 (setq ctl (mew-delete "name" ctl))
828 (setq broken-name (mew-broken-name cdpl))
829 (if broken-name (setq ctl (nconc ctl (list broken-name))))
830 ;;
831 (mew-header-insert mew-ct: ctl)
832 (mew-header-insert mew-cte: cte)
833 (and cd (mew-header-insert mew-cd: cd))
834 (and cdpl (mew-header-insert mew-cdp: cdpl))
835 (insert "\n")
836 ;; header "\n" (cur) [text]
837 (setq bodybeg (point))
838 (goto-char (point-max))
839 (when (and (string= ct mew-ct-msg) mew-field-delete-for-forwarding)
840 (save-restriction
841 (narrow-to-region bodybeg (point-max))
842 (mew-header-delete-lines mew-field-delete-common)
843 (mew-header-delete-lines mew-field-delete-for-forwarding)))
844 (when privacy
845 (mew-encode-security-multipart
846 beg privacy depth (mew-syntax-get-decrypters syntax) cte))
847 (goto-char (point-max))
848 (when (and coverp (setq cst charset))
849 (cond
850 ((null mew-ask-charset)
851 ;; not ask
852 )
853 ((eq mew-ask-charset t)
854 (if (mew-member-case-equal cst mew-cs-m17n-list)
855 (setq ask-cst t)))
856 ((listp mew-ask-charset)
857 (unless (mew-member-case-equal cst mew-ask-charset)
858 (setq ask-cst t))))
859 (if (and ask-cst
860 (not (y-or-n-p (format "%s is used. OK? " cst))))
861 (mew-encode-error "Modify body")))
862 (mew-case-equal cte mew-8bit)))
863
864 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
865 ;;;
866 ;;; Encoding multipart
867 ;;;
868
869 (defun mew-encode-make-multi ()
870 ;; Just after the header
871 (let (beg path multip buffered privacy decrypters first ct)
872 (if (mew-encode-syntax-have-one-part)
873 ;; A user may want to do any MIME stuff to the body
874 (progn
875 (setq mew-encode-syntax (mew-syntax-get-part mew-encode-syntax))
876 (setq buffered t))
877 ;; See if a cover page is empty or not
878 (setq beg (point))
879 (save-excursion
880 (while (and (looking-at "^$") (not (eobp)))
881 (forward-line))
882 (unless (eobp)
883 ;; The cover page exists.
884 (setq buffered t)))
885 (setq path (mew-expand-folder mew-attach-folder))
886 (if buffered
887 ;; The cover page exists.
888 (setq multip t)
889 ;; The cover page does not exist.
890 (delete-region beg (point-max))
891 ;; Remove the cover page entry from the syntax.
892 (setq mew-encode-syntax
893 (mew-syntax-remove-entry mew-encode-syntax '(1)))
894 ;; After removing the over page, see if this message has
895 ;; only one text part.
896 (if (not (mew-encode-syntax-have-one-part))
897 (setq multip t)
898 (setq first (mew-syntax-get-part mew-encode-syntax))
899 (setq ct (mew-syntax-get-value (mew-syntax-get-ct first) 'cap))
900 (if (not (mew-ct-textp ct))
901 (setq multip t)
902 (setq path (expand-file-name
903 (mew-syntax-get-file mew-encode-syntax) path))
904 (setq privacy (mew-syntax-get-privacy mew-encode-syntax))
905 (setq decrypters (mew-syntax-get-decrypters mew-encode-syntax))
906 (setq mew-encode-syntax (mew-syntax-get-part mew-encode-syntax))
907 (mew-syntax-set-privacy mew-encode-syntax privacy)
908 (mew-syntax-set-decrypters mew-encode-syntax decrypters)))))
909 (if multip
910 (mew-encode-multipart mew-encode-syntax path 0 buffered)
911 (mew-encode-singlepart mew-encode-syntax path nil nil 'cover))))
912
913 (defvar mew-default-boundary "--%s(%s_%s)--")
914
915 (defun mew-boundary-get (&optional string)
916 ;; boundary is less than or equal to 70
917 (unless string (setq string "Next_Part"))
918 (format mew-default-boundary
919 string
920 (mew-replace-character
921 (mew-replace-character (current-time-string) mew-sp ?_) ?: ?_)
922 (mew-random-string 3 t)))
923
924 (defun mew-encode-multipart (syntax path depth &optional buffered cte)
925 (let* ((boundary
926 (mew-boundary-get ;; 0 is nil for Next_Part
927 (if (> depth 0) (format "BOUNDARY%s" (number-to-string depth)))))
928 (fullname (expand-file-name (mew-syntax-get-file syntax) path))
929 (ctl (mew-syntax-get-ct syntax))
930 (ct (mew-syntax-get-value ctl 'cap))
931 (cd (mew-syntax-get-cd syntax))
932 (privacy (mew-syntax-get-privacy syntax))
933 (mew-inherit-7bit (mew-encode-limit-7bitp privacy))
934 (len (length syntax))
935 (beg (point))
936 (cnt mew-syntax-magic)
937 (8bit-cnt 0)
938 8bitp cte-pos cover)
939 (mew-header-insert mew-ct: (list ct (list "boundary" boundary)))
940 (setq cte-pos (point))
941 (and cd (mew-header-insert mew-cd: cd))
942 (while (< cnt len)
943 (insert "\n--" boundary "\n")
944 (if (mew-syntax-multipart-p (aref syntax cnt))
945 (setq 8bitp (mew-encode-multipart
946 (aref syntax cnt)
947 fullname
948 (1+ depth)))
949 (if (and (= depth 0) (= cnt mew-syntax-magic ))
950 (setq cover t)
951 (setq cover nil))
952 (setq 8bitp (mew-encode-singlepart
953 (aref syntax cnt)
954 fullname
955 (1+ depth)
956 (if (eq cnt mew-syntax-magic) buffered nil)
957 cover)))
958 (if 8bitp (setq 8bit-cnt (1+ 8bit-cnt)))
959 (setq cnt (1+ cnt)))
960 (insert "\n--" boundary "--\n")
961 (save-excursion
962 (goto-char cte-pos)
963 (mew-header-insert mew-cte: (if (> 8bit-cnt 0) mew-8bit mew-7bit)))
964 (when privacy
965 (mew-encode-security-multipart
966 beg privacy depth (mew-syntax-get-decrypters syntax) cte))
967 (goto-char (point-max))
968 (> 8bit-cnt 0)))
969
970 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
971 ;;;
972 ;;; Privacy services
973 ;;;
974
975 (defun mew-encode-security-multipart (beg privacy depth decrypters cte)
976 (save-restriction
977 (narrow-to-region beg (point-max))
978 (let (proto ct)
979 (dolist (ent privacy)
980 (goto-char (point-min))
981 (mew-set '(ct proto) ent)
982 (setq ct (mew-capitalize ct))
983 (cond
984 ((string= ct mew-ct-mle)
985 (mew-encode-multipart-encrypted ct proto depth decrypters cte))
986 ((string= ct mew-ct-mls)
987 (mew-encode-multipart-signed ct proto depth))
988 ((string= ct mew-ct-smm)
989 (mew-encode-smime proto cte decrypters)))))))
990
991 (defun mew-security-multipart-boundary (depth)
992 (if depth
993 (mew-boundary-get (format "Security_Multipart%s" (number-to-string depth)))
994 (mew-boundary-get "Security_Multipart")))
995
996 (defun mew-save-transfer-form (beg end retain &optional cte)
997 ;; called in the narrowed region
998 (let ((sbeg beg) (send end) (draft-buf (current-buffer))
999 (ocs mew-cs-text-for-net)
1000 tmpbuf file)
1001 (if retain
1002 (progn
1003 (setq tmpbuf (generate-new-buffer mew-buffer-prefix))
1004 (set-buffer tmpbuf)
1005 (mew-erase-buffer)
1006 (mew-insert-buffer-substring draft-buf beg end)
1007 (setq sbeg (point-min) send (point-max))))
1008 (goto-char sbeg) ;; just in case
1009 (if (and cte (mew-case-equal cte mew-bin)
1010 (re-search-forward mew-eoh))
1011 (progn
1012 (setq ocs mew-cs-binary)
1013 (forward-line)
1014 (save-restriction
1015 (narrow-to-region sbeg (point))
1016 (goto-char sbeg)
1017 (mew-lf-to-crlf)))
1018 (unless mew-cs-text-for-net
1019 (goto-char sbeg) ;; just in case
1020 (mew-lf-to-crlf)))
1021 (setq send (point-max))
1022 (setq file (mew-make-temp-name))
1023 (mew-frwlet mew-cs-dummy ocs
1024 (write-region sbeg send file nil 'no-msg))
1025 (if retain
1026 (mew-remove-buffer tmpbuf)
1027 (delete-region sbeg send))
1028 (set-buffer draft-buf)
1029 file)) ;; return value
1030
1031 (defun mew-encode-multipart-encrypted (ct proto depth decrypters cte)
1032 ;; called in the narrowed region
1033 (let* ((boundary (mew-security-multipart-boundary depth))
1034 (switch mew-encode-multipart-encrypted-switch) ;; save length
1035 (func (mew-encode-get-security-func proto switch))
1036 file1 file2 file3 cte2 cte3 fc errmsg)
1037 ;; Write the part converting line breaks.
1038 (setq file1 (mew-save-transfer-form (point-min) (point-max) nil cte))
1039 ;; The narrowed region stores nothing
1040 ;; Call the protocol function
1041 (condition-case nil
1042 (setq fc (funcall func file1 decrypters))
1043 (error
1044 (mew-delete-file file1)
1045 (mew-encode-error
1046 (format "unknown error for %s. Check %s, anyway"
1047 mew-ct-mle mew-temp-dir))))
1048 (mew-set '(file2 cte2 file3 cte3 errmsg) fc)
1049 (if errmsg
1050 (progn
1051 (mew-delete-file file1)
1052 (mew-delete-file file2)
1053 (mew-delete-file file3)
1054 (mew-tinfo-set-privacy-err t)
1055 (mew-tinfo-set-privacy-type nil)
1056 (mew-draft-mode-name)
1057 (mew-encode-error errmsg))
1058 ;; Create multipart content-header
1059 (mew-header-insert mew-ct: (list ct
1060 (list "protocol" proto)
1061 (list "boundary" boundary)))
1062 (mew-header-insert mew-cte: mew-7bit)
1063 (insert (format "\n--%s\n" boundary))
1064 ;; Insert control keys
1065 (mew-encode-singlepart
1066 (mew-encode-syntax-single file2 (list proto) cte2))
1067 (insert (format "\n--%s\n" boundary))
1068 ;; Insert encrypted body
1069 (mew-encode-singlepart
1070 (mew-encode-syntax-single file3 mew-type-apo cte3))
1071 (insert (format "\n--%s--\n" boundary))
1072 ;; Throw away the garbage
1073 (mew-delete-file file1)
1074 (mew-delete-file file2)
1075 (mew-delete-file file3))))
1076
1077 (defun mew-encode-multipart-signed (ct proto depth)
1078 ;; called in the narrowed region
1079 (let* ((boundary (mew-security-multipart-boundary depth))
1080 (switch mew-encode-multipart-signed-switch) ;; save length
1081 (func (mew-encode-get-security-func proto switch))
1082 (canon-func (mew-encode-get-canonicalize-func proto switch))
1083 file1 file2 micalg cte2 fmc errmsg ct2 cdp2)
1084 (if (fboundp canon-func) (funcall canon-func))
1085 (setq file1 (mew-save-transfer-form (point-min) (point-max) 'retain))
1086 ;; The narrowed region still the ORIGINAL part (i.e. line breaks are LF)
1087 ;; Call the protocol function
1088 (condition-case nil
1089 (setq fmc (funcall func file1))
1090 (error
1091 (mew-delete-file file1)
1092 (mew-encode-error
1093 (format "unknown error for %s. Check %s, anyway"
1094 mew-ct-mls mew-temp-dir))))
1095 (mew-set '(file2 cte2 micalg errmsg ct2 cdp2) fmc)
1096 (if errmsg
1097 (progn
1098 (mew-delete-file file1)
1099 (mew-delete-file file2)
1100 (mew-tinfo-set-privacy-err t)
1101 (mew-tinfo-set-privacy-type nil)
1102 (mew-draft-mode-name)
1103 (mew-encode-error errmsg))
1104 (goto-char (point-min))
1105 ;; Before the signed part
1106 ;; Create multipart content-header
1107 (mew-header-insert mew-ct: (list ct
1108 (list "protocol" proto)
1109 (list "micalg" micalg)
1110 (list "boundary" boundary)))
1111 (mew-header-insert mew-cte: mew-7bit)
1112 (insert (format "\n--%s\n" boundary))
1113 (goto-char (point-max))
1114 ;; After the signed part
1115 (insert (format "\n--%s\n" boundary))
1116 (mew-encode-singlepart
1117 (mew-encode-syntax-single file2 (or ct2 (list proto)) cte2 nil nil cdp2))
1118 (insert (format "\n--%s--\n" boundary))
1119 ;; Throw away the garbage
1120 (mew-delete-file file1)
1121 (mew-delete-file file2))))
1122
1123 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1124 ;;;
1125 ;;; 8bit to 7bit conversion for Multipart/Security
1126 ;;;
1127
1128 (defun mew-convert-mime-body (beg end cte linebasep)
1129 (let* ((switch (if linebasep
1130 mew-prog-mime-encode-text-switch
1131 mew-prog-mime-encode-switch))
1132 file opt)
1133 (save-restriction
1134 (narrow-to-region beg end)
1135 (cond
1136 ((and (mew-case-equal cte mew-b64) (fboundp 'base64-encode-region))
1137 (when linebasep
1138 (goto-char (point-min))
1139 (mew-lf-to-crlf))
1140 (base64-encode-region (point-min) (point-max))
1141 (goto-char (point-max))
1142 (insert "\n"))
1143 ((mew-which-exec mew-prog-mime-encode)
1144 (setq opt (mew-prog-mime-encode-get-opt cte switch))
1145 (if (null opt)
1146 (mew-encode-error (concat "Unknown CTE: " cte))
1147 (setq file (mew-make-temp-name))
1148 (mew-frwlet mew-cs-dummy (if linebasep mew-cs-text-for-write mew-cs-binary)
1149 ;; NEVER use call-process-region for privacy reasons
1150 (write-region (point-min) (point-max) file nil 'no-msg)
1151 (delete-region (point-min) (point-max)))
1152 (mew-piolet mew-cs-text-for-read mew-cs-dummy
1153 (apply 'call-process mew-prog-mime-encode file t nil opt))
1154 (mew-delete-file file)))
1155 (t
1156 (mew-encode-error (concat mew-prog-mime-encode " does not exist")))))))
1157
1158 (defun mew-convert-message ()
1159 ;; called on the beginning of a header
1160 (let ((case-fold-search t)
1161 (buf (current-buffer))
1162 hd-end mimep charset cte body-beg body-end)
1163 (unless (re-search-forward mew-eoh nil t)
1164 (goto-char (point-max)))
1165 (setq hd-end (point-marker))
1166 (goto-char (point-min))
1167 (if (re-search-forward (concat "^" mew-mv:) (marker-position hd-end) t)
1168 (setq mimep t))
1169 (cond
1170 (mimep
1171 (goto-char (point-min))
1172 (mew-convert-singlepart)
1173 (mew-header-delete-lines (list mew-mv:))
1174 (goto-char hd-end)
1175 (mew-header-insert mew-mv: (concat mew-mv:-num " " mew-field-comment)))
1176 (t
1177 (goto-char hd-end)
1178 (forward-line)
1179 (setq body-beg (point))
1180 (setq body-end (point-max))
1181 (with-temp-buffer
1182 (mew-insert-buffer-substring buf body-beg body-end)
1183 (mew-cs-decode-region (point-min) (point-max) mew-cs-autoconv)
1184 (setq charset (mew-charset-guess-region (point-min) (point-max))))
1185 (when (mew-encode-charset-8bitp charset)
1186 (setq cte (mew-charset-to-cte charset))
1187 (if (null cte) (setq cte mew-b64))
1188 (mew-convert-mime-body body-beg (point-max) cte 'linebasep)
1189 (goto-char (point-min))
1190 (mew-header-delete-lines (list "Content-"))
1191 (goto-char hd-end)
1192 (mew-header-insert mew-mv: mew-mv:-num)
1193 (mew-header-insert mew-ct: (list mew-ct-txt (list "charset" charset)))
1194 (mew-header-insert mew-cte: cte))))))
1195
1196 (defun mew-convert-singlepart (&optional dctl)
1197 ;; called on the beginning of a content-header
1198 (let ((case-fold-search t)
1199 cthd-end ctbody-beg
1200 ct-val cte-val
1201 ctl ct cte charset)
1202 (unless (re-search-forward mew-eoh nil t)
1203 (goto-char (point-max)))
1204 (setq cthd-end (point-marker))
1205 (forward-line)
1206 (setq ctbody-beg (point))
1207 (goto-char (point-min))
1208 (if (not (re-search-forward (concat "^" mew-cte: "[ \t]*") nil t))
1209 (setq cte mew-7bit)
1210 (setq cte-val (point-marker))
1211 (forward-line)
1212 (mew-header-goto-next)
1213 (setq cte (mew-addrstr-parse-value
1214 (mew-buffer-substring cte-val (1- (point))))))
1215 (when (or (mew-case-equal cte mew-8bit) (mew-case-equal cte mew-bin))
1216 (goto-char (point-min))
1217 (if (not (re-search-forward (concat "^" mew-ct: "[ \t]*") nil t))
1218 (setq ctl (or dctl mew-type-txt))
1219 (setq ct-val (point))
1220 (forward-line)
1221 (mew-header-goto-next)
1222 (setq ctl (mew-param-decode
1223 (mew-buffer-substring ct-val (1- (point)))))
1224 (setq ct (mew-syntax-get-value ctl 'cap))
1225 (cond
1226 ((mew-ct-multipartp ct)
1227 (if (or (string= ct mew-ct-mle) (string= ct mew-ct-mls))
1228 ()
1229 (mew-convert-multipart ctl))
1230 (setq cte mew-7bit))
1231 ((mew-ct-messagep ct)
1232 (if (string= ct mew-ct-msg)
1233 (mew-convert-message))
1234 (setq cte mew-7bit))
1235 ((mew-ct-textp ct)
1236 (setq charset (mew-syntax-get-param ctl "charset"))
1237 (setq cte (mew-charset-to-cte charset))
1238 (if (or (null cte)
1239 (mew-case-equal cte mew-8bit)
1240 (mew-case-equal cte mew-bin))
1241 (setq cte mew-b64))
1242 (unless (mew-case-equal cte mew-7bit)
1243 (mew-convert-mime-body ctbody-beg (point-max) cte 'linebasep)))
1244 (t
1245 ;; rare case
1246 (setq cte (mew-ctdb-cte (mew-ctdb-by-ct ct)))
1247 (if (or (null cte)
1248 (mew-case-equal cte mew-8bit)
1249 (mew-case-equal cte mew-bin))
1250 (setq cte mew-b64))
1251 (unless (mew-case-equal cte mew-7bit)
1252 (mew-convert-mime-body
1253 ctbody-beg (point-max) cte (mew-ct-linebasep ct))))
1254 (goto-char (point-min)))
1255 (mew-header-delete-lines (list mew-cte:))
1256 (goto-char cthd-end)
1257 (mew-header-insert mew-cte: (concat cte " " mew-field-comment))))))
1258
1259 (defun mew-convert-multipart (ctl)
1260 (let* ((case-fold-search nil) ;; boundary is case sensitive
1261 (ct (mew-syntax-get-value ctl 'cap))
1262 (dctl (if (string= ct mew-ct-mld) mew-type-msg))
1263 (boundary (regexp-quote (mew-syntax-get-param ctl "boundary")))
1264 obound ebound bregex start break)
1265 (unless boundary
1266 (mew-encode-error "No boundary parameter for multipart"))
1267 (setq obound (concat "--" boundary))
1268 (setq ebound (concat "--" boundary "--"))
1269 (setq bregex (concat "^--" boundary "\\(\\|--\\)$"))
1270 (unless (re-search-forward (concat "^" obound "$") nil t)
1271 (mew-encode-error (format "No first boundary for %s" ct)))
1272 (forward-line)
1273 (setq start (point)) ;; the beginning of the part
1274 (catch 'multipart
1275 (while t
1276 (unless (re-search-forward bregex nil t)
1277 (mew-encode-error (format "No last boundary for %s" ct)))
1278 (setq break (string= (regexp-quote (match-string 0)) ebound))
1279 (forward-line) ;; the beginning of the next part
1280 (save-excursion
1281 (forward-line -1)
1282 (beginning-of-line) ;; just in case
1283 (forward-char -1) ;; skip the preceding CRLF
1284 ;; the end of the part
1285 (save-restriction
1286 (narrow-to-region start (point))
1287 (goto-char (point-min))
1288 ;; the beginning of the part
1289 (mew-convert-singlepart dctl)))
1290 (setq start (point)) ;; the beginning of the part
1291 (if break (throw 'multipart nil))))))
1292
1293 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1294 ;;;
1295 ;;; Backup and undo
1296 ;;;
1297
1298 (defun mew-encode-make-backup ()
1299 (let* ((file (buffer-file-name))
1300 (back (mew-prepend-prefix file mew-backup-prefix))
1301 (info (concat file mew-draft-info-suffix)))
1302 (mew-frwlet mew-cs-dummy mew-cs-m17n
1303 (write-region (point-min) (point-max) back nil 'no-msg))
1304 (mew-lisp-save
1305 info
1306 (list
1307 (cons "Syntax:" mew-encode-syntax)
1308 (cons "Case:" (mew-tinfo-get-case))
1309 (cons "Flowed:" (mew-tinfo-get-flowed))
1310 (cons "Use-Flowed:" (mew-tinfo-get-use-flowed))
1311 (cons "Message:" (mew-tinfo-get-hdr-file))) ;; Header mode
1312 'nobackup)
1313 nil)) ;; to save
1314
1315 (defun mew-encode-load-syntax ()
1316 (let* ((file (buffer-file-name))
1317 (info (concat file mew-draft-info-suffix))
1318 syntax)
1319 (when (file-exists-p info)
1320 (setq syntax (mew-lisp-load info))
1321 (setq mew-encode-syntax (cdr (assoc "Syntax:" syntax)))
1322 (if (and mew-encode-syntax
1323 (mew-syntax-singlepart-p mew-encode-syntax))
1324 (setq mew-encode-syntax nil))
1325 (mew-tinfo-set-case (cdr (assoc "Case:" syntax)))
1326 (mew-tinfo-set-flowed (cdr (assoc "Flowed:" syntax)))
1327 (mew-tinfo-set-flowed (cdr (assoc "Use-Flowed:" syntax)))
1328 (mew-tinfo-set-hdr-file (cdr (assoc "Message:" syntax))) ;; Header mode
1329 t)))
1330
1331 (defun mew-encode-insert-backup ()
1332 (let* ((file (buffer-file-name))
1333 (back (mew-prepend-prefix file mew-backup-prefix)))
1334 (when (file-exists-p back)
1335 (mew-frwlet mew-cs-m17n mew-cs-dummy
1336 (mew-insert-file-contents back)))))
1337
1338 (defun mew-encode-undo ()
1339 "Get back to the draft before making MIME message."
1340 (interactive)
1341 (mew-elet
1342 (if (not (mew-encode-load-syntax))
1343 (message "Cannot undo")
1344 (mew-erase-buffer)
1345 (if (not (mew-encode-insert-backup))
1346 (message "Cannot undo")
1347 (mew-header-clear) ;; erase the old header separator
1348 (mew-header-prepared)
1349 (if mew-encode-syntax (mew-draft-prepare-attachments))
1350 (mew-draft-toolbar-update)
1351 (setq buffer-undo-list nil)))))
1352
1353 (provide 'mew-encode)
1354
1355 ;;; Copyright Notice:
1356
1357 ;; Copyright (C) 1996-2015 Mew developing team.
1358 ;; All rights reserved.
1359
1360 ;; Redistribution and use in source and binary forms, with or without
1361 ;; modification, are permitted provided that the following conditions
1362 ;; are met:
1363 ;;
1364 ;; 1. Redistributions of source code must retain the above copyright
1365 ;; notice, this list of conditions and the following disclaimer.
1366 ;; 2. Redistributions in binary form must reproduce the above copyright
1367 ;; notice, this list of conditions and the following disclaimer in the
1368 ;; documentation and/or other materials provided with the distribution.
1369 ;; 3. Neither the name of the team nor the names of its contributors
1370 ;; may be used to endorse or promote products derived from this software
1371 ;; without specific prior written permission.
1372 ;;
1373 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
1374 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1375 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1376 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
1377 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1378 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1379 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
1380 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
1381 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
1382 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
1383 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1384
1385 ;;; mew-encode.el ends here