1 ;;; wl-draft.el --- Message draft mode for Wanderlust.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright (C) 1998,1999,2000 Masahiro MURATA <muse@ba2.so-net.ne.jp>
6 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
7 ;; Masahiro MURATA <muse@ba2.so-net.ne.jp>
8 ;; Keywords: mail, net news
10 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
35 (require 'wl-template)
37 (condition-case nil (require 'timezone) (error nil))
39 (require 'eword-encode)
42 (defvar x-face-add-x-face-version-header)
43 (defvar mail-reply-buffer)
44 (defvar mail-from-style)
50 (defalias-maybe 'x-face-insert 'ignore)
51 (defalias-maybe 'x-face-insert-version-header 'ignore)
52 (defalias-maybe 'wl-init 'ignore)
53 (defalias-maybe 'wl-draft-mode 'ignore))
56 (autoload 'wl-addrmgr "wl-addrmgr"))
58 (autoload 'open-ssl-stream "ssl")
60 (defvar wl-draft-buffer-message-number nil)
61 (defvar wl-draft-field-completion-list nil)
62 (defvar wl-draft-verbose-send t)
63 (defvar wl-draft-verbose-msg nil)
64 (defvar wl-draft-queue-flushing nil)
65 (defvar wl-draft-config-variables nil)
66 (defvar wl-draft-config-exec-flag t)
67 (defvar wl-draft-buffer-cur-summary-buffer nil)
68 (defvar wl-draft-clone-local-variable-regexp "^\\(wl\\|mime\\)")
69 (defvar wl-draft-sendlog-filename "sendlog")
70 (defvar wl-draft-queue-save-filename "qinfo")
71 (defvar wl-draft-config-save-filename "config")
72 (defvar wl-draft-queue-flush-send-function 'wl-draft-dispatch-message)
73 (defvar wl-sent-message-via nil)
74 (defvar wl-sent-message-modified nil)
75 (defvar wl-sent-message-queued nil)
76 (defvar wl-draft-fcc-list nil)
77 (defvar wl-draft-reedit nil)
78 (defvar wl-draft-reply-buffer nil)
79 (defvar wl-draft-forward nil)
80 (defvar wl-draft-doing-mime-bcc nil)
82 (defvar wl-draft-parent-folder nil
83 "Folder name of the summary in which current draft is invoked.
84 This variable is local in each draft buffer.
85 You can refer its value in `wl-draft-config-alist'.
88 \(setq wl-draft-config-alist
89 '(((string-match \".*@domain1$\" wl-draft-parent-folder)
90 (\"From\" . \"user@domain1\"))
91 ((string-match \".*@domain2$\" wl-draft-parent-folder)
92 (\"From\" . \"user@domain2\"))))")
94 (defvar wl-draft-parent-number nil)
96 (defconst wl-draft-reply-saved-variables
97 '(wl-draft-parent-folder
98 wl-draft-parent-number))
100 (defvar wl-draft-config-sub-func-alist
101 '((body . wl-draft-config-sub-body)
102 (top . wl-draft-config-sub-top)
103 (bottom . wl-draft-config-sub-bottom)
104 (header . wl-draft-config-sub-header)
105 (header-top . wl-draft-config-sub-header-top)
106 (header-bottom . wl-draft-config-sub-header)
107 (part-top . wl-draft-config-sub-part-top)
108 (part-bottom . wl-draft-config-sub-part-bottom)
109 (body-file . wl-draft-config-sub-body-file)
110 (top-file . wl-draft-config-sub-top-file)
111 (bottom-file . wl-draft-config-sub-bottom-file)
112 (header-file . wl-draft-config-sub-header-file)
113 (template . wl-draft-config-sub-template)
114 (x-face . wl-draft-config-sub-x-face)))
116 (make-variable-buffer-local 'wl-draft-buffer-message-number)
117 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
118 (make-variable-buffer-local 'wl-draft-config-variables)
119 (make-variable-buffer-local 'wl-draft-config-exec-flag)
120 (make-variable-buffer-local 'wl-sent-message-via)
121 (make-variable-buffer-local 'wl-sent-message-queued)
122 (make-variable-buffer-local 'wl-draft-fcc-list)
123 (make-variable-buffer-local 'wl-draft-reply-buffer)
124 (make-variable-buffer-local 'wl-draft-parent-folder)
125 (make-variable-buffer-local 'wl-draft-parent-number)
127 (defvar wl-draft-folder-internal nil
128 "Internal variable for caching `opened' draft folder.")
130 (defsubst wl-smtp-password-key (user mechanism server)
131 (format "SMTP:%s/%s@%s"
132 user mechanism server))
134 (defmacro wl-smtp-extension-bind (&rest body)
135 (` (let* ((smtp-sasl-mechanisms
136 (if wl-smtp-authenticate-type
138 (if (listp wl-smtp-authenticate-type)
139 wl-smtp-authenticate-type
140 (list wl-smtp-authenticate-type)))))
141 (smtp-use-sasl (and smtp-sasl-mechanisms t))
142 (smtp-use-starttls (eq wl-smtp-connection-type 'starttls))
143 (smtp-open-connection-function
144 (if (eq wl-smtp-connection-type 'ssl)
146 smtp-open-connection-function))
148 (if (eq wl-smtp-connection-type 'ssl)
151 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
152 (if (and (string= (car smtp-sasl-mechanisms) "DIGEST-MD5")
154 (string-match "^\\([^@]*\\)@\\([^@]*\\)"
155 wl-smtp-posting-user))
156 (setq smtp-sasl-user-name (match-string 1 wl-smtp-posting-user)
157 smtp-sasl-properties (list 'realm
158 (match-string 2 wl-smtp-posting-user)))
159 (setq smtp-sasl-user-name wl-smtp-posting-user
160 smtp-sasl-properties nil))
161 (setq sasl-read-passphrase
165 (wl-smtp-password-key
167 (car smtp-sasl-mechanisms)
171 (defun wl-draft-insert-date-field ()
173 (insert "Date: " (wl-make-date-string) "\n"))
175 (defun wl-draft-insert-from-field ()
177 ;; Put the "From:" field in unless for some odd reason
178 ;; they put one in themselves.
181 (setq from (wl-draft-eword-encode-address-list wl-from))
182 (error (error "Please look at `wl-from' again")))
183 (insert "From: " from "\n")))
185 (defun wl-draft-insert-x-face-field ()
186 "Insert X-Face header."
188 (if (not (file-exists-p wl-x-face-file))
189 (error "File %s does not exist" wl-x-face-file)
190 (goto-char (point-min))
191 (search-forward mail-header-separator nil t)
193 (wl-draft-insert-x-face-field-here)
194 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
196 (defun wl-draft-insert-x-face-field-here ()
197 "Insert X-Face field at point."
198 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
199 (when (string-match "^\\(X-Face:\\)?[ \t\n]*" x-face-string)
200 (setq x-face-string (substring x-face-string (match-end 0))))
201 (insert "X-Face: " x-face-string))
202 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
204 ;; Insert X-Face-Version: field
205 (when (and (fboundp 'x-face-insert-version-header)
206 (boundp 'x-face-add-x-face-version-header)
207 x-face-add-x-face-version-header)
208 (x-face-insert-version-header)))
210 (defun wl-draft-setup ()
211 (let ((field wl-draft-fields)
215 (list (cons (concat (car field) " ")
216 (concat (car field) " ")))))
217 (setq field (cdr field)))
219 (cons (cons (concat wl-draft-mime-bcc-field-name ": ")
220 (concat wl-draft-mime-bcc-field-name ": "))
222 (setq wl-draft-field-completion-list cl)
223 (setq wl-address-complete-header-regexp
225 (append wl-address-complete-header-list
226 (list (concat wl-draft-mime-bcc-field-name ":")))))))
228 (defun wl-draft-make-mail-followup-to (recipients)
229 (let ((rlist (wl-address-delete-user-mail-addresses recipients)))
230 (if (elmo-list-member rlist (mapcar 'downcase
231 wl-subscribed-mailing-list))
233 (append rlist (list (wl-address-header-extract-address
236 (defun wl-draft-delete-myself-from-cc (to cc)
237 (cond (wl-draft-always-delete-myself ; always-delete option
238 (wl-address-delete-user-mail-addresses cc))
239 ((elmo-list-member (append to cc) ; subscribed mailing-list
240 (mapcar 'downcase wl-subscribed-mailing-list))
241 (wl-address-delete-user-mail-addresses cc))
244 (defsubst wl-draft-strip-subject-regexp (subject regexp)
245 "Remove REGEXP from SUBJECT string."
246 (if (string-match regexp subject)
247 (substring subject (match-end 0))
250 (defun wl-draft-forward-make-subject (original-subject)
251 "Generate subject string for forwarding."
252 (cond ((functionp wl-forward-subject-prefix)
253 (concat (funcall wl-forward-subject-prefix)
255 ((stringp wl-forward-subject-prefix)
256 (concat wl-forward-subject-prefix
257 (wl-draft-strip-subject-regexp
258 (or original-subject "")
259 wl-subject-forward-prefix-regexp)))
260 (t original-subject)))
262 (defun wl-draft-reply-make-subject (original-subject)
263 "Generate subject string for replying."
264 (cond ((functionp wl-reply-subject-prefix)
265 (concat (funcall wl-reply-subject-prefix)
267 ((stringp wl-reply-subject-prefix)
268 (concat wl-reply-subject-prefix
269 (wl-draft-strip-subject-regexp
270 (or original-subject "")
271 wl-subject-re-prefix-regexp)))
272 (t original-subject)))
274 (defun wl-draft-forward (original-subject summary-buf &optional number)
275 (let (references parent-folder subject)
276 (with-current-buffer summary-buf
277 (setq parent-folder (wl-summary-buffer-folder-name)))
278 (setq subject (wl-draft-forward-make-subject original-subject))
279 (with-current-buffer (wl-message-get-original-buffer)
280 (setq references (nconc
281 (std11-field-bodies '("References" "In-Reply-To"))
282 (list (std11-field-body "Message-Id"))))
283 (setq references (delq nil references)
284 references (mapconcat 'identity references " ")
285 references (wl-draft-parse-msg-id-list-string references)
286 references (wl-delete-duplicates references)
287 references (when references
288 (mapconcat 'identity references "\n\t"))))
289 (and wl-draft-use-frame
290 (get-buffer-window summary-buf)
291 (select-window (get-buffer-window summary-buf)))
292 (wl-draft (list (cons 'To "")
293 (cons 'Subject subject)
294 (cons 'References references))
295 nil nil nil nil parent-folder))
296 (setq wl-draft-parent-number number)
297 (goto-char (point-max))
298 (wl-draft-insert-message)
299 (mail-position-on-field "To")
300 (run-hooks 'wl-draft-forward-hook))
302 (defun wl-draft-self-reply-p ()
303 "Return t when From address in the current message is user's self one or not."
304 (wl-address-user-mail-address-p (or (elmo-field-body "From") "")))
306 (defun wl-draft-reply (buf with-arg summary-buf &optional number)
307 "Reply to BUF buffer message.
308 Reply to author if WITH-ARG is non-nil."
311 to mail-followup-to cc subject in-reply-to references newsgroups
312 to-alist cc-alist decoder parent-folder)
313 (when (buffer-live-p summary-buf)
314 (with-current-buffer summary-buf
315 (setq parent-folder (wl-summary-buffer-folder-name))))
316 (set-buffer (or buf mime-mother-buffer))
317 (setq r-list (if with-arg wl-draft-reply-with-argument-list
318 wl-draft-reply-without-argument-list))
321 (when (let ((condition (car (car r-list))))
322 (cond ((stringp condition)
323 (std11-field-body condition))
328 ((stringp (car condition))
329 (or (std11-field-body (car condition))
331 ((symbolp (car condition))
332 (or (funcall (car condition))
336 (setq condition (cdr condition)))
339 (funcall condition))))
340 (let ((r-to-list (nth 0 (cdr (car r-list))))
341 (r-cc-list (nth 1 (cdr (car r-list))))
342 (r-ng-list (nth 2 (cdr (car r-list)))))
343 (when (and (member "Followup-To" r-ng-list)
344 (string= (std11-field-body "Followup-To") "poster"))
345 (setq r-to-list (cons "From" r-to-list))
346 (setq r-ng-list (delete "Followup-To"
347 (copy-sequence r-ng-list))))
348 (if (and r-to-list (symbolp r-to-list))
349 (setq to (wl-concat-list (funcall r-to-list) ","))
350 (setq to (wl-concat-list (cons to
351 (elmo-multiple-fields-body-list
354 (if (and r-cc-list (symbolp r-cc-list))
355 (setq cc (wl-concat-list (funcall r-cc-list) ","))
356 (setq cc (wl-concat-list (cons cc
357 (elmo-multiple-fields-body-list
360 (if (and r-ng-list (symbolp r-ng-list))
361 (setq newsgroups (wl-concat-list (funcall r-ng-list) ","))
362 (setq newsgroups (wl-concat-list (cons newsgroups
367 (setq r-list (cdr r-list)))
368 (error "No match field: check your `wl-draft-reply-%s-argument-list'"
369 (if with-arg "with" "without")))
370 (setq subject (std11-field-body "Subject"))
371 (setq to (wl-parse-addresses to)
372 cc (wl-parse-addresses cc))
373 (with-temp-buffer ; to keep raw buffer unibyte.
374 (set-buffer-multibyte default-enable-multibyte-characters)
375 (setq decoder (mime-find-field-decoder 'Subject 'plain))
376 (setq subject (wl-draft-reply-make-subject
377 (if (and subject decoder)
378 (funcall decoder subject) subject)))
382 (setq decoder (mime-find-field-decoder 'To 'plain))
383 (cons (nth 1 (std11-extract-address-components addr))
384 (if decoder (funcall decoder addr) addr)))
389 (setq decoder (mime-find-field-decoder 'Cc 'plain))
390 (cons (nth 1 (std11-extract-address-components addr))
391 (if decoder (funcall decoder addr) addr)))
393 (setq in-reply-to (std11-field-body "Message-Id"))
394 (setq references (nconc
395 (std11-field-bodies '("References" "In-Reply-To"))
397 (setq to (delq nil (mapcar 'car to-alist)))
398 (setq cc (delq nil (mapcar 'car cc-alist)))
399 ;; if subscribed mailing list is contained in cc or to
400 ;; and myself is contained in cc,
401 ;; delete myself from cc.
402 (setq cc (wl-draft-delete-myself-from-cc to cc))
403 (when wl-insert-mail-followup-to
404 (setq mail-followup-to
405 (wl-draft-make-mail-followup-to (append to cc)))
406 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
407 (with-temp-buffer ; to keep raw buffer unibyte.
408 (set-buffer-multibyte default-enable-multibyte-characters)
409 (setq newsgroups (wl-parse newsgroups
410 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
411 newsgroups (wl-delete-duplicates newsgroups)
416 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
417 (if decoder (funcall decoder grp) grp))
419 (setq to (wl-delete-duplicates to nil t))
420 (setq cc (wl-delete-duplicates
421 (append (wl-delete-duplicates cc nil t)
422 to (copy-sequence to))
424 (and to (setq to (mapconcat
426 (if wl-draft-reply-use-address-with-full-name
427 (or (cdr (assoc addr to-alist)) addr)
430 (and cc (setq cc (mapconcat
432 (if wl-draft-reply-use-address-with-full-name
433 (or (cdr (assoc addr cc-alist)) addr)
436 (and mail-followup-to
437 (setq mail-followup-to
440 (if wl-draft-reply-use-address-with-full-name
441 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
443 mail-followup-to ",\n\t")))
444 (and (null to) (setq to cc cc nil))
445 (setq references (delq nil references)
446 references (mapconcat 'identity references " ")
447 references (wl-draft-parse-msg-id-list-string references)
448 references (wl-delete-duplicates references)
449 references (if references
450 (mapconcat 'identity references "\n\t")))
451 (and wl-draft-use-frame
452 (get-buffer-window summary-buf)
453 (select-window (get-buffer-window summary-buf)))
454 (wl-draft (list (cons 'To to)
456 (cons 'Newsgroups newsgroups)
457 (cons 'Subject subject)
458 (cons 'In-Reply-To in-reply-to)
459 (cons 'References references)
460 (cons 'Mail-Followup-To mail-followup-to))
461 nil nil nil nil parent-folder)
462 (setq wl-draft-parent-number number)
463 (setq wl-draft-reply-buffer buf)
464 (setq wl-draft-config-variables
465 (append wl-draft-reply-saved-variables
466 wl-draft-config-variables)))
467 (run-hooks 'wl-draft-reply-hook))
469 (defun wl-draft-reply-position (position)
470 (cond ((eq position 'body)
471 (wl-draft-body-goto-top))
472 ((eq position 'bottom)
473 (wl-draft-body-goto-bottom))
475 (goto-char (point-min)))
476 ((and (stringp position)
477 (std11-field-body position))
478 (progn (mail-position-on-field position)
479 (wl-draft-beginning-of-line)))
481 (while (car position)
482 (wl-draft-reply-position (car position))
483 (setq position (cdr position))))))
485 (defun wl-draft-add-references ()
486 (wl-draft-add-in-reply-to "References"))
488 (defun wl-draft-add-in-reply-to (&optional alt-field)
489 (let* ((mes-id (save-excursion
490 (set-buffer mail-reply-buffer)
491 (std11-field-body "message-id")))
492 (field (or alt-field "In-Reply-To"))
493 (ref (std11-field-body field))
494 (ref-list nil) (st nil))
495 (when (and mes-id ref)
496 (while (string-match "<[^>]+>" ref st)
498 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
501 (member mes-id ref-list))
505 (when (mail-position-on-field field)
507 (while (looking-at "^[ \t]")
509 (setq mes-id (concat "\t" mes-id "\n")))
513 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
514 &optional ignored-fields)
517 (narrow-to-region (point)(point))
520 (with-current-buffer mail-reply-buffer
522 (decode-mime-charset-region (point-min) (point-max)
524 (buffer-substring-no-properties
525 (point-min) (point-max)))))
527 (goto-char (point-min))
528 (wl-draft-delete-fields ignored-fields))
529 (goto-char (point-max))
530 (push-mark (point) nil t)
531 (goto-char (point-min)))
533 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
534 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
535 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
536 (run-hooks 'wl-draft-cited-hook)
537 (when (if wl-draft-add-references
538 (wl-draft-add-references)
539 (if wl-draft-add-in-reply-to
540 (wl-draft-add-in-reply-to)))
541 (wl-highlight-headers 'for-draft)) ; highlight when added References:
542 (when wl-highlight-body-too
543 (wl-highlight-body-region beg (point-max)))))
545 (defun wl-message-news-p ()
546 "If exist valid Newsgroups field, return non-nil."
547 (std11-field-body "Newsgroups"))
549 (defun wl-message-field-exists-p (field)
550 "If FIELD exist and FIELD value is not empty, return non-nil."
551 (let ((value (std11-field-body field)))
553 (not (string= value "")))))
555 (defun wl-message-mail-p ()
556 "If exist To, Cc or Bcc field, return non-nil."
557 (or (wl-message-field-exists-p "To")
558 (wl-message-field-exists-p "Resent-to")
559 (wl-message-field-exists-p "Cc")
560 (wl-message-field-exists-p "Bcc")
561 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
562 ;;; This may be needed..
563 ;;; (wl-message-field-exists-p "Fcc")
566 (defun wl-draft-edit-string (string)
567 (let ((cur-buf (current-buffer))
568 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
569 to subject in-reply-to cc references newsgroups mail-followup-to
570 content-type content-transfer-encoding from
575 (setq to (std11-field-body "To"))
578 (decode-mime-charset-string
581 (setq subject (std11-field-body "Subject"))
582 (setq subject (and subject
584 (decode-mime-charset-string
587 (setq from (std11-field-body "From")
590 (decode-mime-charset-string
593 (setq in-reply-to (std11-field-body "In-Reply-To"))
594 (setq cc (std11-field-body "Cc"))
597 (decode-mime-charset-string
600 (setq references (std11-field-body "References"))
601 (setq newsgroups (std11-field-body "Newsgroups"))
602 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
603 (setq content-type (std11-field-body "Content-Type"))
604 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
605 (goto-char (point-min))
606 (or (re-search-forward "\n\n" nil t)
607 (search-forward (concat mail-header-separator "\n") nil t))
612 (if (wl-address-user-mail-address-p from) from))
615 (cons 'Subject subject)
616 (cons 'Newsgroups newsgroups)
617 (cons 'Mail-Followup-To mail-followup-to)
618 (cons 'In-Reply-To in-reply-to)
619 (cons 'References references))
620 content-type content-transfer-encoding
621 (buffer-substring (point) (point-max))
623 (kill-buffer tmp-buf))
624 ;; Set cursor point to the top.
625 (goto-char (point-min))
626 (search-forward (concat mail-header-separator "\n") nil t)
627 (run-hooks 'wl-draft-reedit-hook)
628 (and to (mail-position-on-field "To"))))
630 (defun wl-draft-insert-current-message (dummy)
632 (let (original-buffer
634 mail-citation-hook mail-yank-hooks
635 wl-draft-add-references wl-draft-add-in-reply-to
636 wl-draft-cite-function)
637 (if (and wl-draft-buffer-cur-summary-buffer
638 (with-current-buffer wl-draft-buffer-cur-summary-buffer
639 (and wl-message-buffer
640 (with-current-buffer wl-message-buffer
641 (setq original-buffer (wl-message-get-original-buffer))
642 (not (zerop (with-current-buffer original-buffer
645 (setq mail-reply-buffer original-buffer)
646 (wl-draft-yank-from-mail-reply-buffer
648 wl-ignored-forwarded-headers))
649 (when (string= (mime-make-tag "message" "rfc822")
650 (buffer-substring-no-properties (point-at-bol 0)(point-at-eol 0)))
651 (delete-region (point-at-bol 0) (1+ (point-at-eol 0))))
652 (error "No current message"))))
654 (defun wl-draft-insert-get-message (dummy)
655 (let ((fld (completing-read
657 (if (memq 'read-folder wl-use-folder-petname)
658 (wl-folder-get-entity-with-petname)
659 wl-folder-entity-hashtb)
660 nil nil wl-default-spec
661 'wl-read-folder-history))
662 (number (call-interactively
663 (function (lambda (num)
664 (interactive "nNumber: ")
666 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
667 mail-citation-hook mail-yank-hooks
668 wl-draft-cite-function)
671 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
674 (elmo-make-fetch-strategy 'entire)
675 nil mail-reply-buffer)
676 (wl-draft-yank-from-mail-reply-buffer nil))
677 (kill-buffer mail-reply-buffer))))
680 ;; default body citation func
682 (defun wl-default-draft-cite ()
683 (let ((mail-yank-ignored-headers "[^:]+:")
684 (mail-yank-prefix "> ")
685 date from cite-title)
687 (if (< (mark t) (point))
688 (exchange-point-and-mark))
689 (narrow-to-region (point)(point-max))
690 (setq date (std11-field-body "date")
691 from (std11-field-body "from")))
693 (insert (format "At %s,\n%s wrote:\n"
694 (or date "some time ago")
695 (if wl-default-draft-cite-decorate-author
696 (funcall wl-summary-from-function
699 (mail-indent-citation)))
701 (defvar wl-draft-buffer nil "Draft buffer to yank content.")
702 (defun wl-draft-yank-to-draft-buffer (buffer)
703 "Yank BUFFER content to `wl-draft-buffer'."
704 (set-buffer wl-draft-buffer)
705 (let ((mail-reply-buffer buffer))
706 (wl-draft-yank-from-mail-reply-buffer nil)
707 (kill-buffer buffer)))
709 (defun wl-draft-yank-original (&optional arg)
710 "Yank original message."
713 (let (buf mail-reply-buffer)
717 (setq buf (current-buffer)))
718 (setq mail-reply-buffer buf)
719 (wl-draft-yank-from-mail-reply-buffer nil))
720 (wl-draft-yank-current-message-entity)))
722 (defun wl-draft-hide (editing-buffer)
723 "Hide the editing draft buffer if possible."
724 (when (and editing-buffer
725 (buffer-live-p editing-buffer)
726 (get-buffer-window editing-buffer))
727 (select-window (get-buffer-window editing-buffer))
728 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
729 fld-buf sum-win fld-win)
730 (if (and wl-draft-use-frame
731 (> (length (visible-frame-list)) 1))
737 ;; stay folder window if required
738 (when wl-stay-folder-window
739 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
740 (if (setq fld-win (get-buffer-window fld-buf))
741 (select-window fld-win)
742 (if wl-draft-resume-folder-window ;; resume folder window
743 (switch-to-buffer fld-buf)))))
744 (if (buffer-live-p sum-buf)
745 (if (setq sum-win (get-buffer-window sum-buf t))
746 ;; if Summary is on the frame, select it.
747 (select-window sum-win)
748 ;; if summary is not on the frame, switch to it.
749 (if (and wl-stay-folder-window
750 (or wl-draft-resume-folder-window fld-win))
751 (wl-folder-select-buffer sum-buf)
752 (switch-to-buffer sum-buf))))))))
754 (defun wl-draft-delete (editing-buffer)
755 "Kill the editing draft buffer and delete the file corresponds to it."
758 (set-buffer editing-buffer)
759 (when wl-draft-buffer-message-number
760 (elmo-folder-delete-messages (wl-draft-get-folder)
762 wl-draft-buffer-message-number))
763 (wl-draft-config-info-operation wl-draft-buffer-message-number
765 (set-buffer-modified-p nil) ; force kill
766 (kill-buffer editing-buffer))))
768 (defun wl-draft-kill (&optional force-kill)
769 "Kill current draft buffer and quit editing."
772 (when (and (or (eq major-mode 'wl-draft-mode)
773 (eq major-mode 'mail-mode))
775 (yes-or-no-p "Kill Current Draft? ")))
776 (let ((cur-buf (current-buffer)))
777 (when (and wl-draft-parent-number
778 (not (string= wl-draft-parent-folder "")))
779 (let* ((number wl-draft-parent-number)
780 (folder-name wl-draft-parent-folder)
781 (folder (wl-folder-get-elmo-folder folder-name))
783 (if (and (setq buffer (wl-summary-get-buffer folder-name))
784 (with-current-buffer buffer
785 (string= (wl-summary-buffer-folder-name)
787 (with-current-buffer buffer
788 (dolist (flag wl-draft-kill-flags)
789 (elmo-folder-unset-flag folder (list number) flag))
790 (when (wl-summary-jump-to-msg number)
791 (wl-summary-update-persistent-mark)))
792 (elmo-folder-open folder 'load-msgdb)
793 (elmo-folder-unset-flag folder (list number) 'answered)
794 (elmo-folder-close folder))))
795 (wl-draft-hide cur-buf)
796 (wl-draft-delete cur-buf)))
799 (defun wl-draft-fcc ()
800 "Add a new Fcc field, with file name completion."
802 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
803 (mail-position-on-field "to"))
806 ;; Imported from message.el.
807 (defun wl-draft-elide-region (b e)
808 "Elide the text in the region.
809 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
813 (insert wl-draft-elide-ellipsis))
815 ;; Imported from message.el.
816 (defun wl-draft-beginning-of-line (&optional n)
817 "Move point to beginning of header value or to beginning of line."
819 (let ((zrs 'zmacs-region-stays))
820 (when (and (interactive-p) (boundp zrs))
822 (if (wl-draft-point-in-header-p)
823 (let* ((here (point))
824 (bol (progn (beginning-of-line n) (point)))
825 (eol (line-end-position))
826 (eoh (and (looking-at "[^ \t]")
827 (re-search-forward ": *" eol t))))
828 (if (and eoh (or (> here eoh) (= here bol)))
831 (beginning-of-line n)))
833 (defun wl-draft-point-in-header-p ()
834 "Return t if point is in the header."
837 (goto-char (point-min))
838 (not (re-search-forward
839 (concat "^" (regexp-quote mail-header-separator) "\n")
842 ;; function for wl-sent-message-via
844 (defmacro wl-draft-sent-message-p (type)
845 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
847 (defmacro wl-draft-set-sent-message (type result &optional server-port)
848 (` (let ((element (assq (, type) wl-sent-message-via)))
850 (unless (eq (nth 1 element) (, result))
851 (setcdr element (list (, result) (, server-port)))
852 (setq wl-sent-message-modified t))
853 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
854 (setq wl-sent-message-modified t)))))
856 (defun wl-draft-sent-message-results ()
857 (let ((results wl-sent-message-via)
858 unplugged-via sent-via)
860 (cond ((eq (nth 1 (car results)) 'unplugged)
861 (push (caar results) unplugged-via))
862 ((eq (nth 1 (car results)) 'sent)
863 (push (caar results) sent-via)))
864 (setq results (cdr results)))
865 (list unplugged-via sent-via)))
867 (defun wl-draft-write-sendlog (status proto server to id)
868 "Write send log file, if `wl-draft-sendlog' is non-nil."
869 (when wl-draft-sendlog
871 (let* ((filename (expand-file-name wl-draft-sendlog-filename
872 elmo-msgdb-directory))
873 (filesize (nth 7 (file-attributes filename)))
874 (server (if server (concat " server=" server) ""))
876 ((memq proto '(fcc queue))
877 (format " folder=\"%s\"" to))
879 (format " ng=<%s>" to))
884 (mapcar '(lambda(x) (format "<%s>" x)) to)
887 (id (if id (concat " id=" id) ""))
888 (time (format-time-string "%Y/%m/%d %T")))
889 (insert (format "%s proto=%s stat=%s%s%s%s\n"
890 time proto status server to id))
891 (if (and wl-draft-sendlog-max-size filesize
892 (> filesize wl-draft-sendlog-max-size))
893 (rename-file filename (concat filename ".old") t))
894 (if (file-writable-p filename)
895 (write-region-as-binary (point-min) (point-max)
897 (message "%s is not writable." filename))))))
899 (defun wl-draft-get-header-delimiter (&optional delete)
900 ;; If DELETE is non-nil, replace the header delimiter with a blank line
902 (goto-char (point-min))
903 (when (re-search-forward
904 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
908 (setq delimline (point-marker)))
911 (defun wl-draft-send-mail-with-qmail ()
912 "Pass the prepared message buffer to qmail-inject.
913 Refer to the documentation for the variable `send-mail-function'
914 to find out how to use this."
915 (if (and wl-draft-qmail-send-plugged
916 (not (elmo-plugged-p)))
917 (wl-draft-set-sent-message 'mail 'unplugged)
919 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
920 (let ((id (std11-field-body "Message-ID"))
921 (to (std11-field-body "To")))
925 'call-process-region 1 (point-max) wl-qmail-inject-program
927 wl-qmail-inject-args))
928 ;; qmail-inject doesn't say anything on it's stdout/stderr,
929 ;; we have to look at the retval instead
931 (wl-draft-set-sent-message 'mail 'sent)
932 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
933 (1 (error "`qmail-inject' reported permanent failure"))
934 (111 (error "`qmail-inject' reported transient failure"))
935 ;; should never happen
936 (t (error "`qmail-inject' reported unknown failure"))))))
938 (defun wl-draft-parse-msg-id-list-string (string)
939 "Get msg-id list from STRING."
941 (dolist (parsed-id (std11-parse-msg-ids-string string))
942 (when (eq (car parsed-id) 'msg-id)
943 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
945 (nreverse msg-id-list)))
947 (defun wl-draft-eword-encode-address-list (string &optional column)
948 "Encode header field STRING as list of address, and return the result.
949 Cause an error when STRING contains invalid address.
950 Optional argument COLUMN is start-position of the field."
951 (car (eword-encode-rword-list
952 (or column eword-encode-default-start-column)
953 (eword-encode-addresses-to-rword-list
954 (wl-draft-std11-parse-addresses (std11-lexical-analyze string))))))
956 (defun wl-draft-std11-parse-addresses (lal)
957 (let ((ret (std11-parse-address lal)))
958 (when (and (not (and (eq (length lal) 1)
959 (eq (car (car lal)) 'spaces)))
961 (error "Error while parsing address"))
963 (let ((dest (list (car ret))))
965 (while (and (setq ret (std11-parse-ascii-token lal))
966 (string-equal (cdr (assq 'specials (car ret))) ",")
967 (setq ret (std11-parse-address (cdr ret)))
969 (setq dest (cons (car ret) dest))
970 (setq lal (cdr ret)))
971 (while (eq 'spaces (car (car lal)))
972 (setq lal (cdr lal)))
973 (if lal (error "Error while parsing address"))
976 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
977 "Get mailbox list of FIELD from current buffer.
978 The buffer is expected to be narrowed to just the headers of the message.
979 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
980 from current buffer."
982 (let ((case-fold-search t)
983 (inhibit-read-only t)
985 mailbox-list beg seq has-group-list)
986 (goto-char (point-min))
987 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
990 (re-search-forward "^[^ \t]" nil 'move)
992 (skip-chars-backward "\n")
993 (setq seq (std11-lexical-analyze
994 (buffer-substring-no-properties beg (point))))
995 (setq addresses (wl-draft-std11-parse-addresses seq))
997 (cond ((eq (car (car addresses)) 'group)
998 (setq has-group-list t)
1002 'std11-address-string
1003 (nth 2 (car addresses))))))
1004 ((eq (car (car addresses)) 'mailbox)
1005 (setq address (nth 1 (car addresses)))
1009 (std11-addr-to-string
1010 (if (eq (car address) 'phrase-route-addr)
1012 (cdr address))))))))
1013 (setq addresses (cdr addresses)))
1014 (when (and remove-group-list has-group-list)
1015 (delete-region beg (point))
1016 (insert (wl-address-string-without-group-list-contents seq))))
1019 (defun wl-draft-deduce-address-list (buffer header-start header-end)
1020 "Get address list suitable for smtp RCPT TO:<address>.
1021 Group list content is removed if `wl-draft-remove-group-list-contents' is
1023 (let ((fields (if (and wl-draft-doing-mime-bcc
1024 wl-draft-disable-bcc-for-mime-bcc)
1026 '("to" "cc" "bcc")))
1027 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
1028 (case-fold-search t)
1032 (narrow-to-region header-start header-end)
1033 (goto-char (point-min))
1035 (if (re-search-forward "^resent-to[\t ]*:" nil t)
1036 (setq fields resent-fields)))
1040 (wl-draft-parse-mailbox-list
1042 wl-draft-remove-group-list-contents)))
1043 (setq fields (cdr fields)))
1049 (defun wl-draft-send-mail-with-smtp ()
1050 "Send the prepared message buffer with SMTP."
1052 (let* ((errbuf (if mail-interactive
1053 (generate-new-buffer " smtp errors")
1055 (case-fold-search t)
1056 (default-case-fold-search t)
1057 (sender (or wl-envelope-from
1058 (wl-address-header-extract-address wl-from)))
1059 (delimline (save-excursion
1060 (goto-char (point-min))
1062 (concat "^" (regexp-quote mail-header-separator)
1066 (or wl-smtp-posting-server smtp-server "localhost"))
1067 (smtp-service (or wl-smtp-posting-port smtp-service))
1068 (smtp-local-domain (or smtp-local-domain wl-local-domain))
1069 (id (std11-field-body "message-id"))
1071 (if (not (elmo-plugged-p smtp-server smtp-service))
1072 (wl-draft-set-sent-message 'mail 'unplugged
1073 (cons smtp-server smtp-service))
1076 ;; Instead of `smtp-deduce-address-list'.
1077 (setq recipients (wl-draft-deduce-address-list
1078 (current-buffer) (point-min) delimline))
1079 (unless recipients (error "No recipients"))
1080 ;; Insert an extra newline if we need it to work around
1081 ;; Sun's bug that swallows newlines.
1082 (goto-char (1+ delimline))
1083 (if (eval mail-mailer-swallows-blank-line)
1085 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1086 (if mail-interactive
1090 (wl-draft-delete-field "bcc" delimline)
1091 (wl-draft-delete-field "resent-bcc" delimline)
1092 (let (process-connection-type)
1095 (wl-smtp-extension-bind
1097 (smtp-send-buffer sender recipients (current-buffer))
1099 (wl-draft-write-sendlog 'failed 'smtp smtp-server
1101 (if (and (eq (car err) 'smtp-response-error)
1102 (= (nth 1 err) 535))
1104 (wl-smtp-password-key
1106 (car smtp-sasl-mechanisms)
1108 (signal (car err) (cdr err)))
1110 (wl-draft-write-sendlog 'uncertain 'smtp smtp-server
1112 (signal (car err) (cdr err)))))
1113 (wl-draft-set-sent-message 'mail 'sent)
1114 (wl-draft-write-sendlog
1115 'ok 'smtp smtp-server recipients id)))))
1116 (if (bufferp errbuf)
1117 (kill-buffer errbuf))))))
1119 (defun wl-draft-send-mail-with-pop-before-smtp ()
1120 "Send the prepared message buffer with POP-before-SMTP."
1121 (require 'elmo-pop3)
1125 :user (or wl-pop-before-smtp-user
1126 elmo-pop3-default-user)
1127 :server (or wl-pop-before-smtp-server
1128 elmo-pop3-default-server)
1129 :port (or wl-pop-before-smtp-port
1130 elmo-pop3-default-port)
1131 :auth (or wl-pop-before-smtp-authenticate-type
1132 elmo-pop3-default-authenticate-type)
1133 :stream-type (elmo-get-network-stream-type
1134 (or wl-pop-before-smtp-stream-type
1135 elmo-pop3-default-stream-type))))
1137 (condition-case error
1139 (setq session (elmo-pop3-get-session folder))
1140 (when session (elmo-network-close-session session)))
1142 (unless (string= (nth 1 error) "Unplugged")
1143 (signal (car error) (cdr error))))))
1144 (wl-draft-send-mail-with-smtp))
1146 (defun wl-draft-insert-required-fields (&optional force-msgid)
1147 "Insert Message-ID, Date, and From field.
1148 If FORCE-MSGID, insert message-id regardless of `wl-insert-message-id'."
1149 ;; Insert Message-Id field...
1150 (goto-char (point-min))
1151 (when (and (or force-msgid
1152 wl-insert-message-id)
1153 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1154 (insert (concat "Message-ID: "
1155 (funcall wl-message-id-function)
1157 ;; Insert date field.
1158 (goto-char (point-min))
1159 (or (re-search-forward "^Date[ \t]*:" nil t)
1160 (wl-draft-insert-date-field))
1161 ;; Insert from field.
1162 (goto-char (point-min))
1163 (or (re-search-forward "^From[ \t]*:" nil t)
1164 (wl-draft-insert-from-field)))
1166 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1167 "Send the message in the current buffer."
1169 (narrow-to-region (goto-char (point-min))
1170 (if (re-search-forward
1172 "^" (regexp-quote mail-header-separator) "$")
1176 (wl-draft-insert-required-fields)
1177 ;; ignore any blank lines in the header
1178 (while (progn (goto-char (point-min))
1179 (re-search-forward "\n[ \t]*\n\n*" nil t))
1180 (replace-match "\n"))
1181 (goto-char (point-min))
1182 (while (re-search-forward
1183 "^[^ \t\n:]+:[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n"
1185 (when (string= "" (match-string 1))
1186 (replace-match ""))))
1187 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1188 (wl-draft-dispatch-message)
1189 (when kill-when-done
1190 ;; hide editing-buffer.
1191 (wl-draft-hide editing-buffer)
1192 ;; delete editing-buffer and its file.
1193 (wl-draft-delete editing-buffer)))
1195 (defun wl-draft-dispatch-message (&optional mes-string)
1196 "Send the message in the current buffer. Not modified the header fields."
1197 (let (delimline mime-bcc)
1198 (if (and wl-draft-verbose-send mes-string)
1199 (message "%s" mes-string))
1201 (setq delimline (wl-draft-get-header-delimiter t))
1202 (unless wl-draft-fcc-list
1203 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1205 (setq wl-sent-message-modified nil)
1208 (if (and (wl-message-mail-p)
1209 (not (wl-draft-sent-message-p 'mail)))
1210 (if (or (not (or wl-draft-force-queuing
1211 wl-draft-force-queuing-mail))
1212 (memq 'mail wl-sent-message-queued))
1214 (setq mime-bcc (wl-draft-mime-bcc-field))
1215 (funcall wl-draft-send-mail-function)
1216 (when (not (zerop (length mime-bcc)))
1217 (wl-draft-do-mime-bcc mime-bcc)))
1218 (push 'mail wl-sent-message-queued)
1219 (wl-draft-set-sent-message 'mail 'unplugged)))
1220 (if (and (wl-message-news-p)
1221 (not (wl-draft-sent-message-p 'news))
1222 (not (wl-message-field-exists-p "Resent-to")))
1223 (if (or (not (or wl-draft-force-queuing
1224 wl-draft-force-queuing-news))
1225 (memq 'news wl-sent-message-queued))
1226 (funcall wl-draft-send-news-function)
1227 (push 'news wl-sent-message-queued)
1228 (wl-draft-set-sent-message 'news 'unplugged))))
1229 (let* ((status (wl-draft-sent-message-results))
1230 (unplugged-via (car status))
1231 (sent-via (nth 1 status)))
1232 ;; If one sent, process fcc folder.
1233 (if (and sent-via wl-draft-fcc-list)
1235 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1237 (setq wl-draft-fcc-list nil)))
1238 (if wl-draft-use-cache
1239 (let ((id (std11-field-body "Message-ID"))
1240 (elmo-enable-disconnected-operation t))
1241 (elmo-file-cache-save (elmo-file-cache-get-path id)
1243 ;; If one unplugged, append queue.
1244 (when (and unplugged-via
1245 wl-sent-message-modified)
1246 (if wl-draft-enable-queuing
1248 (wl-draft-queue-append wl-sent-message-via)
1249 (setq wl-sent-message-modified 'requeue))
1250 (error "Unplugged")))
1251 (when wl-draft-verbose-send
1252 (if (and unplugged-via sent-via);; combined message
1254 (setq wl-draft-verbose-msg
1255 (format "Sending%s and Queuing%s..."
1256 sent-via unplugged-via))
1257 (message "%sdone" wl-draft-verbose-msg))
1261 (if sent-via "done" "failed"))))))))
1262 (not wl-sent-message-modified)) ;; return value
1264 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1265 "Force send current buffer as raw message."
1268 (let (wl-interactive-send
1269 ;;; wl-draft-verbose-send
1270 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1271 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1274 (wl-draft-send kill-when-done mes-string))))
1276 (defun wl-draft-clone-local-variables ()
1277 (let ((locals (buffer-local-variables))
1280 (when (and (consp (car locals))
1282 (string-match wl-draft-clone-local-variable-regexp
1283 (symbol-name (car (car locals)))))
1284 (wl-append result (list (car (car locals)))))
1285 (setq locals (cdr locals)))
1288 (defcustom wl-draft-send-confirm-with-preview t
1289 "Non-nil to invoke preview through confirmation of sending.
1290 This variable is valid when `wl-interactive-send' has non-nil value."
1294 (defun wl-draft-send-confirm ()
1297 (condition-case quit
1299 (when wl-draft-send-confirm-with-preview
1300 (wl-draft-preview-message))
1302 (goto-char (point-min)) ; to show recipients in header
1306 (message "Send current draft? <y/n/j(down)/k(up)> ")
1307 (setq answer (let ((cursor-in-echo-area t)) (read-char)))
1326 (throw 'done nil)))))))
1328 (when wl-draft-send-confirm-with-preview
1329 (mime-preview-quit)))))
1331 (defun wl-draft-send (&optional kill-when-done mes-string)
1332 "Send current draft message.
1333 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1335 ;; Don't call this explicitly.
1336 ;; Added to 'wl-draft-send-hook (by teranisi)
1337 ;; (wl-draft-config-exec)
1338 (run-hooks 'wl-draft-send-hook)
1339 (when (or (not wl-interactive-send)
1340 (wl-draft-send-confirm))
1341 (let ((send-mail-function 'wl-draft-raw-send)
1342 (editing-buffer (current-buffer))
1343 (sending-buffer (wl-draft-generate-clone-buffer
1344 " *wl-draft-sending-buffer*"
1345 (append wl-draft-config-variables
1346 (wl-draft-clone-local-variables))))
1347 (wl-draft-verbose-msg nil)
1351 (set-buffer sending-buffer)
1352 (if (and (not (wl-message-mail-p))
1353 (not (wl-message-news-p)))
1354 (error "No recipient is specified"))
1355 (expand-abbrev) ; for mail-abbrevs
1356 (let ((mime-header-encode-method-alist
1358 '((wl-draft-eword-encode-address-list
1359 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
1360 (if (boundp 'mime-header-encode-method-alist)
1361 (symbol-value 'mime-header-encode-method-alist)))))
1362 (run-hooks 'mail-send-hook) ; translate buffer
1365 (if wl-draft-verbose-send
1366 (message "%s" (or mes-string "Sending...")))
1367 (funcall wl-draft-send-function editing-buffer kill-when-done)
1368 ;; Now perform actions on successful sending.
1369 (while mail-send-actions
1371 (apply (car (car mail-send-actions))
1372 (cdr (car mail-send-actions)))
1374 (setq mail-send-actions (cdr mail-send-actions)))
1375 (if wl-draft-verbose-send
1377 (or wl-draft-verbose-msg
1380 ;; kill sending buffer, anyway.
1381 (and (buffer-live-p sending-buffer)
1382 (kill-buffer sending-buffer))))))
1384 (defun wl-draft-mime-bcc-field ()
1385 "Return the MIME-Bcc field body. The field is deleted."
1386 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1387 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1389 (defun wl-draft-do-mime-bcc (field-body)
1390 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1391 (let ((orig-from (mime-decode-field-body (std11-field-body "from")
1393 (orig-subj (mime-decode-field-body (or (std11-field-body "subject")
1396 (recipients (wl-parse-addresses field-body))
1397 (draft-buffer (current-buffer))
1399 (save-window-excursion
1400 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1401 (not (zerop (length field-body))))
1402 (let ((wl-draft-doing-mime-bcc t))
1403 (dolist (recipient recipients)
1404 (wl-draft-create-buffer)
1405 (wl-draft-create-contents
1406 (append `((From . ,orig-from)
1408 (Subject . ,(concat "A blind carbon copy ("
1411 (wl-draft-default-headers)))
1412 (wl-draft-insert-mail-header-separator)
1413 (wl-draft-prepare-edit)
1414 (goto-char (point-max))
1415 (insert (or wl-draft-mime-bcc-body
1416 "This is a blind carbon copy.")
1418 (mime-edit-insert-tag "message" "rfc822")
1419 (insert-buffer draft-buffer)
1420 (let (wl-interactive-send)
1421 (wl-draft-send 'kill-when-done))))))))
1423 (defun wl-draft-save ()
1424 "Save current draft."
1426 (if (buffer-modified-p)
1428 (message "Saving...")
1429 (let ((msg (buffer-substring-no-properties (point-min) (point-max)))
1431 (when wl-draft-buffer-message-number
1432 (elmo-folder-delete-messages (wl-draft-get-folder)
1433 (list wl-draft-buffer-message-number))
1434 (wl-draft-config-info-operation wl-draft-buffer-message-number
1436 (elmo-folder-check (wl-draft-get-folder))
1437 ;; If no header separator, insert it.
1440 (goto-char (point-min))
1441 (unless (re-search-forward
1442 (concat "^" (regexp-quote mail-header-separator) "$")
1444 (goto-char (point-min))
1445 (if (re-search-forward "\n\n" nil t)
1446 (replace-match (concat "\n" mail-header-separator "\n"))
1447 (goto-char (point-max))
1448 (insert (if (eq (char-before) ?\n) "" "\n")
1449 mail-header-separator "\n")))
1450 (let ((mime-header-encode-method-alist
1452 '((eword-encode-unstructured-field-body
1453 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
1454 (if (boundp 'mime-header-encode-method-alist)
1455 (symbol-value 'mime-header-encode-method-alist)))))
1456 (mime-edit-translate-buffer))
1457 (wl-draft-get-header-delimiter t)
1459 (elmo-folder-next-message-number (wl-draft-get-folder)))
1460 (elmo-folder-append-buffer (wl-draft-get-folder)))
1461 (elmo-folder-check (wl-draft-get-folder))
1462 (elmo-folder-commit (wl-draft-get-folder))
1463 (setq wl-draft-buffer-message-number next-number)
1464 (rename-buffer (format "%s/%d" wl-draft-folder next-number))
1465 (setq buffer-file-name (buffer-name))
1466 (set-buffer-modified-p nil)
1467 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save)
1468 (message "Saving...done")))
1469 (message "(No changes need to be saved)")))
1471 (defun wl-draft-mimic-kill-buffer ()
1472 "Kill the current (draft) buffer with query."
1474 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1477 (if (or (not bufname)
1478 (string-equal bufname "")
1479 (string-equal bufname (buffer-name)))
1480 (let ((bufname (current-buffer)))
1481 (when (or (not (buffer-modified-p))
1483 (format "Buffer %s modified; kill anyway? " bufname)))
1484 (set-buffer-modified-p nil)
1485 (wl-draft-hide bufname)
1486 (kill-buffer bufname)))
1487 (kill-buffer bufname))))
1489 (defun wl-draft-save-and-exit ()
1490 "Save current draft and exit current draft mode."
1493 (let ((editing-buffer (current-buffer)))
1494 (wl-draft-hide editing-buffer)
1495 (kill-buffer editing-buffer)))
1497 (defun wl-draft-send-and-exit ()
1498 "Send current draft message and kill it."
1502 (defun wl-draft-send-from-toolbar ()
1504 (let ((wl-interactive-send t))
1505 (wl-draft-send-and-exit)))
1507 (defun wl-draft-delete-field (field &optional delimline replace)
1508 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1510 (defun wl-draft-delete-fields (field &optional delimline replace)
1513 (goto-char (point-min))
1514 (if (search-forward "\n\n" nil t)
1515 (setq delimline (point))
1516 (setq delimline (point-max))))
1517 (narrow-to-region (point-min) delimline)
1518 (goto-char (point-min))
1519 (let ((regexp (concat "^" field ":"))
1520 (case-fold-search t))
1522 (if (looking-at regexp)
1528 (if (re-search-forward "^[^ \t]" nil t)
1529 (goto-char (match-beginning 0))
1532 (insert (concat field ": " replace "\n"))))
1534 (if (re-search-forward "^[^ \t]" nil t)
1535 (goto-char (match-beginning 0))
1538 (defun wl-draft-get-fcc-list (header-end)
1539 (if (and wl-draft-doing-mime-bcc
1540 wl-draft-disable-fcc-for-mime-bcc)
1542 (wl-draft-delete-field "fcc")
1545 (case-fold-search t))
1546 (or (markerp header-end) (error "HEADER-END must be a marker"))
1548 (goto-char (point-min))
1549 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1554 (buffer-substring-no-properties
1558 (skip-chars-backward " \t")
1561 (dolist (folder fcc-list)
1562 (wl-folder-confirm-existence
1563 (wl-folder-get-elmo-folder (eword-decode-string folder)))))
1564 (delete-region (match-beginning 0)
1565 (progn (forward-line 1) (point)))))
1568 (defcustom wl-draft-fcc-append-read-folder-history t
1569 "Non-nil to append fcc'ed folder to `wl-read-folder-history'."
1573 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1574 (let ((send-mail-buffer (current-buffer))
1575 (tembuf (generate-new-buffer " fcc output"))
1576 (case-fold-search t)
1578 (or (markerp header-end) (error "HEADER-END must be a marker"))
1581 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1584 ;; insert just the headers to avoid moving the gap more than
1585 ;; necessary (the message body could be arbitrarily huge.)
1586 (insert-buffer-substring send-mail-buffer 1 header-end)
1587 (wl-draft-insert-required-fields t)
1588 (goto-char (point-max))
1589 (insert-buffer-substring send-mail-buffer header-end)
1590 (let ((id (std11-field-body "Message-ID"))
1591 (elmo-enable-disconnected-operation t)
1594 (unless (or cache-saved
1595 (elmo-folder-plugged-p
1596 (wl-folder-get-elmo-folder (car fcc-list))))
1597 (elmo-file-cache-save id nil) ;; for disconnected operation
1598 (setq cache-saved t))
1599 (if (elmo-folder-append-buffer
1600 (wl-folder-get-elmo-folder
1601 (eword-decode-string (car fcc-list)))
1602 (and wl-fcc-force-as-read '(read)))
1603 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1604 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1605 (if (and wl-draft-fcc-append-read-folder-history
1606 (boundp 'wl-read-folder-history))
1607 (or (equal (car fcc-list) (car wl-read-folder-history))
1608 (setq wl-read-folder-history
1609 (append (list (car fcc-list)) wl-read-folder-history))))
1610 (setq fcc-list (cdr fcc-list)))))
1611 (kill-buffer tembuf)))
1613 (defun wl-draft-on-field-p ()
1616 (goto-char (point-min))
1617 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1624 (if (or (looking-at ".*,[ \t]?$")
1625 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1627 (let ((pos (point)))
1630 (if (looking-at "^[ \t]")
1632 (if (re-search-forward ":" pos t) nil t)))))))
1636 (defun wl-draft (&optional header-alist
1637 content-type content-transfer-encoding
1640 "Write and send mail/news message with Wanderlust."
1647 (wl-plugged-init t))
1649 (wl-init)) ; returns immediately if already initialized.
1652 (let (buffer header-alist-internal)
1653 (setq buffer (wl-draft-create-buffer parent-folder))
1654 (unless (cdr (assq 'From header-alist))
1656 (append (list (cons 'From wl-from)) header-alist)))
1657 (unless (cdr (assq 'To header-alist))
1662 (if (assq 'To header-alist)
1663 (setcdr (assq 'To header-alist) to)
1665 (append header-alist
1666 (list (cons 'To to))))))))
1667 (unless (cdr (assq 'Subject header-alist))
1668 (if (assq 'Subject header-alist)
1669 (setcdr (assq 'Subject header-alist) "")
1671 (append header-alist (list (cons 'Subject ""))))))
1672 (setq header-alist (append header-alist
1673 (wl-draft-default-headers)
1674 wl-draft-additional-header-alist
1675 (if body (list "" (cons 'Body body)))))
1676 (wl-draft-create-contents header-alist)
1678 (wl-draft-decode-body
1679 content-type content-transfer-encoding))
1680 (wl-draft-insert-mail-header-separator)
1681 (wl-draft-prepare-edit)
1683 (run-hooks 'wl-mail-setup-hook))
1684 (goto-char (point-min))
1685 (setq buffer-undo-list nil)
1686 (wl-user-agent-compose-internal) ;; user-agent
1689 (string= (cdr (assq 'To header-alist)) ""))
1690 (mail-position-on-field "To"))
1692 (goto-char (point-max))))
1695 (defun wl-draft-create-buffer (&optional parent-folder)
1696 (let* ((draft-folder (wl-draft-get-folder))
1697 (parent-folder (or parent-folder (wl-summary-buffer-folder-name)))
1698 (summary-buf (wl-summary-get-buffer parent-folder))
1700 (or (eq this-command 'wl-summary-reply)
1701 (eq this-command 'wl-summary-reply-with-citation)
1702 (eq this-command 'wl-summary-forward)
1703 (eq this-command 'wl-summary-target-mark-forward)
1704 (eq this-command 'wl-summary-target-mark-reply-with-citation)))
1705 (buffer (generate-new-buffer "*draft*")) ; Just for initial name.
1706 change-major-mode-hook)
1708 ;; switch-buffer according to draft buffer style.
1709 (if wl-draft-use-frame
1710 (switch-to-buffer-other-frame buffer)
1711 (if reply-or-forward
1712 (case wl-draft-reply-buffer-style
1714 (split-window-vertically)
1716 (switch-to-buffer buffer))
1718 (switch-to-buffer buffer))
1720 (delete-other-windows)
1721 (switch-to-buffer buffer))
1723 (if (functionp wl-draft-reply-buffer-style)
1724 (funcall wl-draft-reply-buffer-style buffer)
1725 (error "Invalid value for wl-draft-reply-buffer-style"))))
1726 (case wl-draft-buffer-style
1728 (when (eq major-mode 'wl-summary-mode)
1729 (wl-summary-toggle-disp-msg 'off))
1730 (split-window-vertically)
1732 (switch-to-buffer buffer))
1734 (switch-to-buffer buffer))
1736 (delete-other-windows)
1737 (switch-to-buffer buffer))
1738 (t (if (functionp wl-draft-buffer-style)
1739 (funcall wl-draft-buffer-style buffer)
1740 (error "Invalid value for wl-draft-buffer-style"))))))
1743 (set-buffer-multibyte t) ; draft buffer is always multibyte.
1744 (make-local-variable 'truncate-partial-width-windows)
1745 (setq truncate-partial-width-windows nil)
1746 (setq truncate-lines wl-draft-truncate-lines)
1747 (setq wl-sent-message-via nil)
1748 (setq wl-sent-message-queued nil)
1749 (setq wl-draft-config-exec-flag t)
1750 (setq wl-draft-parent-folder (or parent-folder ""))
1751 (or (eq this-command 'wl-folder-write-current-folder)
1752 (setq wl-draft-buffer-cur-summary-buffer summary-buf))
1755 (defun wl-draft-create-contents (header-alist)
1756 "header-alist' sample
1757 '(function ;; funcall
1758 string ;; insert string
1759 (symbol . string) ;; insert symbol-value: string
1760 (symbol . function) ;; (funcall) and if it returns string,
1761 ;; insert symbol-value: string
1762 (symbol . nil) ;; do nothing
1765 (unless (eq major-mode 'wl-draft-mode)
1766 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1767 (let ((halist header-alist)
1772 ((functionp (car halist)) (funcall (car halist)))
1774 ((stringp (car halist)) (insert (car halist) "\n"))
1776 ((consp (car halist))
1777 (setq field (car (car halist)))
1778 (setq value (cdr (car halist)))
1782 ((eq field 'Body) ; body
1784 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1786 (let ((value-return (funcall value)))
1787 (when (stringp value-return)
1788 (insert (symbol-name field) ": " value-return "\n"))))
1797 (setq halist (cdr halist)))))
1799 (defun wl-draft-prepare-edit ()
1800 (unless (eq major-mode 'wl-draft-mode)
1801 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1802 (let (change-major-mode-hook)
1803 (wl-draft-editor-mode)
1804 (static-when (boundp 'auto-save-file-name-transforms)
1805 (make-local-variable 'auto-save-file-name-transforms)
1806 (setq auto-save-file-name-transforms
1807 (cons (list (concat (regexp-quote wl-draft-folder)
1809 (concat (expand-file-name
1811 (elmo-folder-msgdb-path
1812 (wl-draft-get-folder)))
1814 auto-save-file-name-transforms)))
1815 (when wl-draft-write-file-function
1816 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
1817 (wl-draft-overload-functions)
1818 (wl-highlight-headers 'for-draft)
1820 (clear-visited-file-modtime)))
1822 (defun wl-draft-decode-header ()
1824 (std11-narrow-to-header)
1825 (wl-draft-decode-message-in-buffer)
1828 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1831 (std11-field-body "content-type")))
1832 (content-transfer-encoding
1833 (or content-transfer-encoding
1834 (std11-field-body "content-transfer-encoding")))
1837 (std11-narrow-to-header)
1838 (wl-draft-delete-field "content-type")
1839 (wl-draft-delete-field "content-transfer-encoding")
1840 (goto-char (point-max))
1841 (setq delimline (point-marker))
1843 (narrow-to-region delimline (point-max))
1844 (goto-char (point-min))
1846 (insert "Content-type: " content-type "\n"))
1847 (when content-transfer-encoding
1848 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1849 (wl-draft-decode-message-in-buffer)
1850 (goto-char (point-min))
1851 (unless (re-search-forward "^$" (point-at-eol) t)
1856 ;;; subroutine for wl-draft-create-contents
1857 ;;; must be used in wl-draft-mode
1858 (defun wl-draft-check-new-line ()
1859 (if (not (= (preceding-char) ?\n))
1862 (defsubst wl-draft-trim-ccs (cc)
1868 (null (and wl-draft-delete-myself-from-bcc-fcc
1870 (mapcar 'wl-address-header-extract-address
1872 (wl-parse-addresses (std11-field-body "To"))
1873 (wl-parse-addresses (std11-field-body "Cc"))))
1874 (mapcar 'downcase wl-subscribed-mailing-list)))))
1878 (defsubst wl-draft-default-headers ()
1880 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1881 (wl-address-header-extract-address
1883 (cons 'User-Agent wl-generate-mailer-string-function)
1884 (cons 'Reply-To mail-default-reply-to)
1885 (cons 'Bcc (function
1888 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1889 (cons 'Fcc (function
1891 (wl-draft-trim-ccs wl-fcc))))
1892 (cons 'Organization wl-organization)
1893 (and wl-auto-insert-x-face
1894 (file-exists-p wl-x-face-file)
1895 'wl-draft-insert-x-face-field-here) ;; allow nil
1896 mail-default-headers
1897 ;; check \n at th end of line for `mail-default-headers'
1898 'wl-draft-check-new-line
1901 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1904 (goto-char delimline)
1905 (goto-char (point-min))
1906 (if (search-forward "\n\n" nil t)
1907 (delete-backward-char 1)
1908 (goto-char (point-max))))
1909 (wl-draft-check-new-line)
1910 (put-text-property (point)
1912 (insert mail-header-separator "\n")
1914 'category 'mail-header-separator)
1919 (defun wl-draft-elmo-nntp-send ()
1920 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1921 (elmo-nntp-default-user
1922 (or wl-nntp-posting-user elmo-nntp-default-user))
1923 (elmo-nntp-default-server
1924 (or wl-nntp-posting-server elmo-nntp-default-server))
1925 (elmo-nntp-default-port
1926 (or wl-nntp-posting-port elmo-nntp-default-port))
1927 (elmo-nntp-default-stream-type
1928 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1929 (elmo-nntp-default-function wl-nntp-posting-function)
1931 (if (setq condition (cdr (elmo-string-matched-assoc
1932 (std11-field-body "Newsgroups")
1933 wl-nntp-posting-config-alist)))
1934 (if (stringp condition)
1935 (setq elmo-nntp-default-server condition)
1936 (while (car condition)
1937 (set (intern (format "elmo-nntp-default-%s"
1938 (symbol-name (caar condition))))
1940 (setq condition (cdr condition)))))
1941 (unless elmo-nntp-default-function
1942 (error "wl-draft-nntp-send: posting-function is nil"))
1943 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1944 (wl-draft-set-sent-message 'news 'unplugged
1945 (cons elmo-nntp-default-server
1946 elmo-nntp-default-port))
1947 (funcall elmo-nntp-default-function
1948 elmo-nntp-default-server (current-buffer))
1949 (wl-draft-set-sent-message 'news 'sent)
1950 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1951 (std11-field-body "Newsgroups")
1952 (std11-field-body "Message-ID")))))
1954 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1955 "Generate clone of current buffer named NAME."
1956 (let ((editing-buffer (current-buffer)))
1958 (set-buffer (generate-new-buffer name))
1961 (wl-draft-editor-mode)
1962 (insert-buffer editing-buffer)
1964 (while local-variables
1965 (make-local-variable (car local-variables))
1966 (set (car local-variables)
1968 (set-buffer editing-buffer)
1969 (symbol-value (car local-variables))))
1970 (setq local-variables (cdr local-variables)))
1973 (defun wl-draft-remove-text-plain-tag ()
1974 "Remove text/plain tag of mime-edit."
1975 (when (string= (mime-make-text-tag "plain")
1976 (buffer-substring-no-properties (point-at-bol)(point-at-eol)))
1977 (delete-region (point-at-bol)(1+ (point-at-eol)))))
1979 (defun wl-draft-reedit (number)
1980 (let ((draft-folder (wl-draft-get-folder))
1983 buffer change-major-mode-hook body-top)
1984 (setq buffer (get-buffer-create (format "%s/%d" wl-draft-folder
1986 (if wl-draft-use-frame
1987 (switch-to-buffer-other-frame buffer)
1988 (switch-to-buffer buffer))
1990 (elmo-message-fetch draft-folder number (elmo-make-fetch-strategy 'entire)
1991 nil (current-buffer))
1992 (elmo-delete-cr-buffer)
1993 (let ((mime-edit-again-ignored-field-regexp
1994 "^\\(Content-.*\\|Mime-Version\\):"))
1995 (wl-draft-decode-message-in-buffer))
1996 (setq body-top (wl-draft-insert-mail-header-separator))
1999 (make-local-variable 'truncate-partial-width-windows)
2000 (setq truncate-partial-width-windows nil)
2001 (setq truncate-lines wl-draft-truncate-lines)
2002 (setq wl-sent-message-via nil)
2003 (setq wl-sent-message-queued nil)
2004 (wl-draft-config-info-operation number 'load)
2005 (goto-char (point-min))
2006 (wl-draft-overload-functions)
2007 (wl-draft-editor-mode)
2008 (static-when (boundp 'auto-save-file-name-transforms)
2009 (make-local-variable 'auto-save-file-name-transforms)
2010 (setq auto-save-file-name-transforms
2011 (cons (list (concat (regexp-quote wl-draft-folder)
2013 (concat (expand-file-name
2015 (elmo-folder-msgdb-path
2016 (wl-draft-get-folder)))
2018 auto-save-file-name-transforms)))
2019 (setq buffer-file-name (buffer-name)
2020 wl-draft-parent-folder ""
2021 wl-draft-buffer-message-number number)
2022 (when wl-draft-write-file-function
2023 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
2024 (wl-highlight-headers 'for-draft)
2025 (goto-char body-top)
2026 (run-hooks 'wl-draft-reedit-hook)
2027 (goto-char (point-max))
2030 (defmacro wl-draft-body-goto-top ()
2032 (goto-char (point-min))
2033 (if (re-search-forward mail-header-separator nil t)
2035 (goto-char (point-max))))))
2037 (defmacro wl-draft-body-goto-bottom ()
2038 (` (goto-char (point-max))))
2040 (defmacro wl-draft-config-body-goto-header ()
2042 (goto-char (point-min))
2043 (if (re-search-forward mail-header-separator nil t)
2045 (goto-char (point-max))))))
2047 (defsubst wl-draft-config-sub-eval-insert (content &optional newline)
2048 (let (content-value)
2050 (stringp (setq content-value (eval content))))
2051 (insert content-value)
2052 (if newline (insert "\n")))))
2054 (defun wl-draft-config-sub-body (content)
2055 (wl-draft-body-goto-top)
2056 (delete-region (point) (point-max))
2057 (wl-draft-config-sub-eval-insert content))
2059 (defun wl-draft-config-sub-top (content)
2060 (wl-draft-body-goto-top)
2061 (wl-draft-config-sub-eval-insert content))
2063 (defun wl-draft-config-sub-bottom (content)
2064 (wl-draft-body-goto-bottom)
2065 (wl-draft-config-sub-eval-insert content))
2067 (defun wl-draft-config-sub-header (content)
2068 (wl-draft-config-body-goto-header)
2069 (wl-draft-config-sub-eval-insert content 'newline))
2071 (defun wl-draft-config-sub-header-top (content)
2072 (goto-char (point-min))
2073 (wl-draft-config-sub-eval-insert content 'newline))
2075 (defun wl-draft-config-sub-part-top (content)
2076 (goto-char (mime-edit-content-beginning))
2077 (wl-draft-config-sub-eval-insert content 'newline))
2079 (defun wl-draft-config-sub-part-bottom (content)
2080 (goto-char (mime-edit-content-end))
2081 (wl-draft-config-sub-eval-insert content 'newline))
2083 (defsubst wl-draft-config-sub-file (content)
2084 (let ((coding-system-for-read wl-cs-autoconv)
2085 (file (expand-file-name (eval content))))
2086 (if (file-exists-p file)
2087 (insert-file-contents file)
2088 (error "%s: no exists file" file))))
2090 (defun wl-draft-config-sub-body-file (content)
2091 (wl-draft-body-goto-top)
2092 (delete-region (point) (point-max))
2093 (wl-draft-config-sub-file content))
2095 (defun wl-draft-config-sub-top-file (content)
2096 (wl-draft-body-goto-top)
2097 (wl-draft-config-sub-file content))
2099 (defun wl-draft-config-sub-bottom-file (content)
2100 (wl-draft-body-goto-bottom)
2101 (wl-draft-config-sub-file content))
2103 (defun wl-draft-config-sub-header-file (content)
2104 (wl-draft-config-body-goto-header)
2105 (wl-draft-config-sub-file content))
2107 (defun wl-draft-config-sub-template (content)
2108 (setq wl-draft-config-variables
2109 (wl-template-insert (eval content))))
2111 (defun wl-draft-config-sub-x-face (content)
2112 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
2113 (fboundp 'x-face-insert)) ; x-face.el is installed.
2114 (x-face-insert content)
2115 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
2117 (defsubst wl-draft-config-sub-func (field content)
2119 (if (setq func (assq field wl-draft-config-sub-func-alist))
2120 (let (wl-draft-config-variables)
2121 (funcall (cdr func) content)
2122 ;; for wl-draft-config-sub-template
2123 (cons t wl-draft-config-variables)))))
2125 (defsubst wl-draft-config-exec-sub (clist)
2126 (let (config local-variables)
2128 (setq config (car clist))
2133 (let ((field (car config))
2134 (content (cdr config))
2138 (wl-draft-replace-field field (eval content) t))
2139 ((setq ret-val (wl-draft-config-sub-func field content))
2140 (if (cdr ret-val) ;; for wl-draft-config-sub-template
2141 (wl-append local-variables (cdr ret-val))))
2142 ((boundp field) ;; variable
2143 (make-local-variable field)
2144 (set field (eval content))
2145 (wl-append local-variables (list field)))
2147 (error "%s: not variable" field)))))
2149 (error "%s: not supported type" config)))
2150 (setq clist (cdr clist)))
2153 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
2154 "Change headers in draft preparation time."
2156 (unless wl-draft-reedit
2159 (and (boundp 'wl-draft-prepared-config-alist)
2160 wl-draft-prepared-config-alist) ;; For compatible.
2161 wl-draft-config-alist)))
2163 (wl-draft-config-exec config-alist reply-buf)))))
2165 (defun wl-draft-config-exec (&optional config-alist reply-buf)
2166 "Change headers according to the value of `wl-draft-config-alist'.
2167 Automatically applied in draft sending time."
2169 (let ((case-fold-search t)
2170 (alist (or config-alist wl-draft-config-alist))
2171 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2172 wl-draft-reply-buffer)))
2173 (local-variables wl-draft-config-variables)
2175 (when (and (or (interactive-p)
2176 wl-draft-config-exec-flag)
2181 (setq key (caar alist)
2188 (set-buffer reply-buf)
2190 (std11-narrow-to-header)
2191 (goto-char (point-min))
2192 (re-search-forward (car clist) nil t))))
2193 (wl-draft-config-exec-sub (cdr clist))
2196 (when (save-restriction
2197 (std11-narrow-to-header mail-header-separator)
2198 (goto-char (point-min))
2199 (re-search-forward key nil t))
2200 (wl-append local-variables
2201 (wl-draft-config-exec-sub clist))
2204 (wl-append local-variables
2205 (wl-draft-config-exec-sub clist))
2207 (if (and found wl-draft-config-matchone)
2209 (setq alist (cdr alist)))))
2211 (setq wl-draft-config-exec-flag nil))
2212 (run-hooks 'wl-draft-config-exec-hook)
2213 (put-text-property (point-min)(point-max) 'face nil)
2214 (wl-highlight-message (point-min)(point-max) t)
2215 (setq wl-draft-config-variables
2216 (elmo-uniq-list local-variables)))))
2218 (defun wl-draft-replace-field (field content &optional add)
2221 (let ((case-fold-search t)
2222 (inhibit-read-only t) ;; added by teranisi.
2224 (std11-narrow-to-header mail-header-separator)
2225 (goto-char (point-min))
2226 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2231 (re-search-forward "^[^ \t]" nil 'move)
2233 (skip-chars-backward "\n")
2234 (delete-region beg (point))
2235 (insert " " content))
2240 (re-search-forward "^[^ \t]" nil 'move)
2242 (delete-region beg (point)))
2243 (when (and add content)
2245 (goto-char (point-max))
2246 (insert (concat field ": " content "\n"))))))))
2248 (defun wl-draft-config-info-operation (msg operation)
2249 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-draft-get-folder)))
2252 (format "%s-%d" wl-draft-config-save-filename msg)
2254 element alist variable)
2256 ((eq operation 'save)
2257 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2258 (while (setq variable (pop variables))
2259 (when (boundp variable)
2261 (list (cons variable (eval variable))))))
2262 (elmo-object-save filename alist)))
2263 ((eq operation 'load)
2264 (setq alist (elmo-object-load filename))
2265 (while (setq element (pop alist))
2266 (set (make-local-variable (car element)) (cdr element))
2267 (wl-append wl-draft-config-variables (list (car element)))))
2268 ((eq operation 'delete)
2269 (if (file-exists-p filename)
2270 (delete-file filename))))))
2272 (defun wl-draft-queue-info-operation (msg operation
2273 &optional add-sent-message-via)
2274 (let* ((msgdb-dir (elmo-folder-msgdb-path
2275 (wl-folder-get-elmo-folder wl-queue-folder)))
2278 (format "%s-%d" wl-draft-queue-save-filename msg)
2280 element alist variable)
2282 ((eq operation 'save)
2283 (let ((variables (elmo-uniq-list
2284 (append wl-draft-queue-save-variables
2285 wl-draft-config-variables
2286 (list 'wl-draft-fcc-list)))))
2287 (if add-sent-message-via
2289 (push 'wl-sent-message-queued variables)
2290 (push 'wl-sent-message-via variables)))
2291 (while (setq variable (pop variables))
2292 (when (boundp variable)
2294 (list (cons variable (eval variable))))))
2295 (elmo-object-save filename alist)))
2296 ((eq operation 'load)
2297 (setq alist (elmo-object-load filename))
2298 (while (setq element (pop alist))
2299 (set (make-local-variable (car element)) (cdr element))))
2300 ((eq operation 'get-sent-via)
2301 (setq alist (elmo-object-load filename))
2302 (cdr (assq 'wl-sent-message-via alist)))
2303 ((eq operation 'delete)
2304 (if (file-exists-p filename)
2305 (delete-file filename))))))
2307 (defun wl-draft-queue-append (wl-sent-message-via)
2308 (if wl-draft-verbose-send
2309 (message "Queuing..."))
2310 (let ((send-buffer (current-buffer))
2311 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2312 (message-id (std11-field-body "Message-ID")))
2313 (if (elmo-folder-append-buffer folder)
2315 (wl-draft-queue-info-operation
2316 (car (elmo-folder-status folder))
2317 'save wl-sent-message-via)
2318 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2319 (when wl-draft-verbose-send
2320 (setq wl-draft-verbose-msg "Queuing...")
2321 (message "Queuing...done")))
2322 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2323 (error "Queuing failed"))))
2325 (defun wl-draft-queue-flush ()
2326 "Flush draft queue."
2328 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2330 (elmo-folder-open-internal queue-folder)
2331 (elmo-folder-list-messages queue-folder)))
2334 (wl-draft-queue-flushing t)
2335 msgs failure len buffer msgid sent-via)
2336 ;; get plugged send message
2338 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2341 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2342 (or (not (nth 2 (car sent-via)))
2344 (car (nth 2 (car sent-via)))
2345 (cdr (nth 2 (car sent-via))))))
2346 (wl-append msgs (list (car msgs2)))
2348 (setq sent-via (cdr sent-via))))
2349 (setq msgs2 (cdr msgs2)))
2350 (when (> (setq len (length msgs)) 0)
2351 (if (elmo-y-or-n-p (format
2352 "%d message(s) are in the sending queue. Send now? "
2354 (not elmo-dop-flush-confirm) t)
2357 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2360 ;; reset buffer local variables
2361 (kill-all-local-variables)
2365 (setq wl-sent-message-via nil)
2366 (wl-draft-queue-info-operation (car msgs) 'load)
2367 (elmo-message-fetch queue-folder
2369 (elmo-make-fetch-strategy 'entire)
2370 nil (current-buffer))
2372 (setq failure (funcall
2373 wl-draft-queue-flush-send-function
2374 (format "Sending (%d/%d)..." i len)))
2375 ;;; (wl-draft-raw-send nil nil
2376 ;;; (format "Sending (%d/%d)..." i len))
2378 (elmo-display-error err t)
2382 (if (eq wl-sent-message-modified 'requeue)
2384 (elmo-folder-delete-messages
2385 queue-folder (cons (car msgs) nil))
2386 (wl-draft-queue-info-operation (car msgs) 'delete))
2388 (elmo-folder-delete-messages
2389 queue-folder (cons (car msgs) nil))
2390 (wl-draft-queue-info-operation (car msgs) 'delete)
2391 (setq performed (+ 1 performed))))
2392 (setq msgs (cdr msgs)))
2393 (kill-buffer buffer)
2394 (message "%d message(s) are sent." performed)))
2395 (message "%d message(s) are remained to be sent." len))
2396 (elmo-folder-close queue-folder)
2399 (defun wl-jump-to-draft-buffer (&optional arg)
2400 "Jump to the draft if exists."
2403 (wl-jump-to-draft-folder)
2404 (let ((draft-bufs (wl-collect-draft))
2408 (message "No draft buffer exist."))
2411 (sort (mapcar 'buffer-name draft-bufs)
2412 (function (lambda (a b)
2413 (not (string< a b))))))
2414 (if (setq buf (cdr (member (buffer-name)
2416 (setq buf (car buf))
2417 (setq buf (car draft-bufs)))
2418 (switch-to-buffer buf))))))
2420 (defun wl-jump-to-draft-folder ()
2421 (let ((msgs (reverse (elmo-folder-list-messages (wl-draft-get-folder))))
2422 (mybuf (buffer-name))
2425 (message "No draft message exist.")
2426 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2427 (setq msg (cadr (memq
2428 (string-to-int (substring mybuf (match-end 0)))
2430 (or msg (setq msg (car msgs)))
2431 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2432 (switch-to-buffer buf)
2433 (wl-draft-reedit msg)))))
2435 (defun wl-draft-highlight-and-recenter (&optional n)
2437 (when wl-highlight-body-too
2438 (let ((modified (buffer-modified-p)))
2441 (put-text-property (point-min) (point-max) 'face nil)
2442 (wl-highlight-message (point-min) (point-max) t))
2443 (set-buffer-modified-p modified))))
2444 (static-when (featurep 'xemacs)
2445 ;; Cope with one of many XEmacs bugs that `recenter' takes
2446 ;; a long time if there are a lot of invisible text lines.
2450 ;; insert element from history
2451 (defvar wl-draft-current-history-position nil)
2452 (defvar wl-draft-history-backup-word "")
2454 (defun wl-draft-previous-history-element (n)
2456 (let (bol history beg end prev new)
2457 (when (and (not (wl-draft-on-field-p))
2460 (goto-char (point-min))
2461 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
2465 (while (and (looking-at "^[ \t]")
2466 (not (= (point) (point-min))))
2469 ((looking-at wl-folder-complete-header-regexp)
2470 (and (boundp 'wl-read-folder-history)
2471 (setq history wl-read-folder-history)))
2472 ;; ((looking-at wl-address-complete-header-regexp)
2473 ;; (setq history .....))
2477 (setq bol (save-excursion (beginning-of-line) (point)))
2478 (cond ((and (or (eq last-command 'wl-draft-previous-history-element)
2479 (eq last-command 'wl-draft-next-history-element))
2480 wl-draft-current-history-position)
2482 (or (search-backward-regexp ",[ \t]*\\(.*\\)" bol t)
2483 (search-backward-regexp "^[ \t]\\(.*\\)" bol t)
2484 (search-backward-regexp "^[^ \t]*: \\(.*\\)" bol t))
2485 (setq prev (match-string 1))
2486 (goto-char (match-beginning 1))
2489 (>= (+ n wl-draft-current-history-position) 0))
2491 (<= (+ n wl-draft-current-history-position)
2494 (setq wl-draft-current-history-position
2495 (+ n wl-draft-current-history-position))
2497 (nth wl-draft-current-history-position
2498 (append (list wl-draft-history-backup-word)
2500 (delete-region beg end)
2504 (message "End of history; no next item"))
2506 (message "Beginning of history; no preceding item")))))
2509 (or (search-backward-regexp ",[ \t]*\\(.*\\)" bol t)
2510 (search-backward-regexp "^[ \t]\\(.*\\)" bol t)
2511 (search-backward-regexp "^[^ \t]*: \\(.*\\)" bol t)))
2513 (setq wl-draft-current-history-position 1)
2514 (setq wl-draft-history-backup-word (match-string 1))
2515 (delete-region (match-beginning 1) (match-end 1))
2516 (insert (car history)))
2518 (setq wl-draft-current-history-position nil))))))
2520 (defun wl-draft-next-history-element (n)
2522 (wl-draft-previous-history-element (- n)))
2524 ;;;; user-agent support by Sen Nagata
2526 ;; this appears to be necessarily global...
2527 (defvar wl-user-agent-compose-p nil)
2528 (defvar wl-user-agent-headers-and-body-alist nil)
2530 ;; this should be a generic function for mail-mode -- i wish there was
2531 ;; something like it in sendmail.el
2532 (defun wl-user-agent-insert-header (header-name header-value)
2533 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2534 ;; it seems like overriding existing headers is acceptable -- should
2535 ;; we provide an option?
2537 ;; plan was: unfold header (might be folded), remove existing value, insert
2539 ;; wl doesn't seem to fold header lines yet anyway :-)
2541 (let ((kill-whole-line t)
2543 (mail-position-on-field (capitalize header-name))
2544 (setq end-of-line (point))
2546 (re-search-forward ":" end-of-line)
2547 (insert (concat " " header-value "\n"))
2550 ;; this should be a generic function for mail-mode -- i wish there was
2551 ;; something like it in sendmail.el
2553 ;; ** haven't dealt w/ case where the body is already set **
2554 (defun wl-user-agent-insert-body (body-text)
2555 "Insert a body of text, BODY-TEXT, into a message."
2556 ;; code defensively... :-P
2557 (goto-char (point-min))
2558 (search-forward mail-header-separator)
2563 (defun wl-user-agent-compose (&optional to subject other-headers continue
2564 switch-function yank-action
2566 "Support the `compose-mail' interface for wl.
2567 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2568 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2569 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2571 (unless (featurep 'wl)
2574 (setq switch-function 'keep))
2575 ;; protect these -- to and subject get bound at some point, so it looks
2576 ;; to be necessary to protect the values used w/in
2577 (let ((wl-user-agent-headers-and-body-alist other-headers)
2578 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2579 (wl-draft-buffer-style switch-function))
2581 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2584 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2587 (setq wl-user-agent-headers-and-body-alist
2588 (cons (cons "to" to)
2589 wl-user-agent-headers-and-body-alist))))
2591 (if (wl-string-match-assoc "subject"
2592 wl-user-agent-headers-and-body-alist
2595 (wl-string-match-assoc "subject"
2596 wl-user-agent-headers-and-body-alist
2599 (setq wl-user-agent-headers-and-body-alist
2600 (cons (cons "subject" subject)
2601 wl-user-agent-headers-and-body-alist))))
2602 ;; i think this is what we want to use...
2605 ;; tell the hook-function to do its stuff
2606 (setq wl-user-agent-compose-p t)
2607 ;; because to get the hooks working, wl-draft has to think it has
2608 ;; been called interactively
2609 (call-interactively 'wl-draft))
2610 (setq wl-user-agent-compose-p nil))))
2612 (defun wl-user-agent-compose-internal ()
2613 "Manipulate headers and/or a body of a draft message."
2614 ;; being called from wl-user-agent-compose?
2615 (if wl-user-agent-compose-p
2618 (let ((headers wl-user-agent-headers-and-body-alist)
2619 (case-fold-search t))
2622 (if (not (string-match "^body$" (car (car headers))))
2623 (wl-user-agent-insert-header
2624 (car (car headers)) (cdr (car headers)))
2626 (setq headers (cdr headers))))
2627 ;; highlight headers (from wl-draft in wl-draft.el)
2628 (wl-highlight-headers 'for-draft)
2630 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2632 (wl-user-agent-insert-body
2633 (cdr (wl-string-match-assoc
2635 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2639 (product-provide (provide 'wl-draft) (require 'wl-version))
2641 ;;; wl-draft.el ends here