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))
41 (defvar x-face-add-x-face-version-header)
42 (defvar mail-reply-buffer)
43 (defvar mail-from-style)
47 (defalias-maybe 'x-face-insert 'ignore)
48 (defalias-maybe 'x-face-insert-version-header 'ignore)
49 (defalias-maybe 'wl-init 'ignore)
50 (defalias-maybe 'wl-draft-mode 'ignore))
53 (autoload 'wl-addrmgr "wl-addrmgr"))
55 (defvar wl-draft-buf-name "Draft")
56 (defvar wl-draft-buffer-file-name nil)
57 (defvar wl-draft-field-completion-list nil)
58 (defvar wl-draft-verbose-send t)
59 (defvar wl-draft-verbose-msg nil)
60 (defvar wl-draft-queue-flushing nil)
61 (defvar wl-draft-config-variables nil)
62 (defvar wl-draft-config-exec-flag t)
63 (defvar wl-draft-buffer-cur-summary-buffer nil)
64 (defvar wl-draft-clone-local-variable-regexp "^\\(wl\\|mime\\)")
65 (defvar wl-draft-sendlog-filename "sendlog")
66 (defvar wl-draft-queue-save-filename "qinfo")
67 (defvar wl-draft-config-save-filename "config")
68 (defvar wl-draft-queue-flush-send-function 'wl-draft-dispatch-message)
69 (defvar wl-sent-message-via nil)
70 (defvar wl-sent-message-modified nil)
71 (defvar wl-sent-message-queued nil)
72 (defvar wl-draft-fcc-list nil)
73 (defvar wl-draft-reedit nil)
74 (defvar wl-draft-reply-buffer nil)
75 (defvar wl-draft-forward nil)
76 (defvar wl-draft-doing-mime-bcc nil)
78 (defvar wl-draft-parent-folder nil
79 "Folder name of the summary in which current draft is invoked.
80 This variable is local in each draft buffer.
81 You can refer its value in `wl-draft-config-alist'.
84 \(setq wl-draft-config-alist
85 '(((string-match \".*@domain1$\" wl-draft-parent-folder)
86 (\"From\" . \"user@domain1\"))
87 ((string-match \".*@domain2$\" wl-draft-parent-folder)
88 (\"From\" . \"user@domain2\"))))")
90 (defvar wl-draft-parent-number nil)
92 (defconst wl-draft-reply-saved-variables
93 '(wl-draft-parent-folder
94 wl-draft-parent-number))
96 (defvar wl-draft-config-sub-func-alist
97 '((body . wl-draft-config-sub-body)
98 (top . wl-draft-config-sub-top)
99 (bottom . wl-draft-config-sub-bottom)
100 (header . wl-draft-config-sub-header)
101 (header-top . wl-draft-config-sub-header-top)
102 (header-bottom . wl-draft-config-sub-header)
103 (part-top . wl-draft-config-sub-part-top)
104 (part-bottom . wl-draft-config-sub-part-bottom)
105 (body-file . wl-draft-config-sub-body-file)
106 (top-file . wl-draft-config-sub-top-file)
107 (bottom-file . wl-draft-config-sub-bottom-file)
108 (header-file . wl-draft-config-sub-header-file)
109 (template . wl-draft-config-sub-template)
110 (x-face . wl-draft-config-sub-x-face)))
112 (make-variable-buffer-local 'wl-draft-buffer-file-name)
113 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
114 (make-variable-buffer-local 'wl-draft-config-variables)
115 (make-variable-buffer-local 'wl-draft-config-exec-flag)
116 (make-variable-buffer-local 'wl-sent-message-via)
117 (make-variable-buffer-local 'wl-sent-message-queued)
118 (make-variable-buffer-local 'wl-draft-fcc-list)
119 (make-variable-buffer-local 'wl-draft-reply-buffer)
120 (make-variable-buffer-local 'wl-draft-parent-folder)
121 (make-variable-buffer-local 'wl-draft-parent-number)
123 (defsubst wl-smtp-password-key (user mechanism server)
124 (format "SMTP:%s/%s@%s"
125 user mechanism server))
127 (defmacro wl-smtp-extension-bind (&rest body)
128 (` (let* ((smtp-sasl-mechanisms
129 (if wl-smtp-authenticate-type
131 (if (listp wl-smtp-authenticate-type)
132 wl-smtp-authenticate-type
133 (list wl-smtp-authenticate-type)))))
134 (smtp-use-sasl (and smtp-sasl-mechanisms t))
135 (smtp-use-starttls (eq wl-smtp-connection-type 'starttls))
136 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
137 (if (and (string= (car smtp-sasl-mechanisms) "DIGEST-MD5")
139 (string-match "^\\([^@]*\\)@\\([^@]*\\)"
140 wl-smtp-posting-user))
141 (setq smtp-sasl-user-name (match-string 1 wl-smtp-posting-user)
142 smtp-sasl-properties (list 'realm
143 (match-string 2 wl-smtp-posting-user)))
144 (setq smtp-sasl-user-name wl-smtp-posting-user
145 smtp-sasl-properties nil))
146 (setq sasl-read-passphrase
150 (wl-smtp-password-key
152 (car smtp-sasl-mechanisms)
156 (defun wl-draft-insert-date-field ()
158 (insert "Date: " (wl-make-date-string) "\n"))
160 (defun wl-draft-insert-from-field ()
162 ;; Put the "From:" field in unless for some odd reason
163 ;; they put one in themselves.
166 (setq from (wl-draft-eword-encode-address-list wl-from))
167 (error (error "Please look at `wl-from' again")))
168 (insert "From: " from "\n")))
170 (defun wl-draft-insert-x-face-field ()
171 "Insert X-Face header."
173 (if (not (file-exists-p wl-x-face-file))
174 (error "File %s does not exist" wl-x-face-file)
175 (goto-char (point-min))
176 (search-forward mail-header-separator nil t)
178 (wl-draft-insert-x-face-field-here)
179 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
181 (defun wl-draft-insert-x-face-field-here ()
182 "Insert X-Face field at point."
183 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
184 (when (string-match "^\\(X-Face:\\)?[ \t\n]*" x-face-string)
185 (setq x-face-string (substring x-face-string (match-end 0))))
186 (insert "X-Face: " x-face-string))
187 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
189 ;; Insert X-Face-Version: field
190 (when (and (fboundp 'x-face-insert-version-header)
191 (boundp 'x-face-add-x-face-version-header)
192 x-face-add-x-face-version-header)
193 (x-face-insert-version-header)))
195 (defun wl-draft-setup ()
196 (let ((field wl-draft-fields)
200 (list (cons (concat (car field) " ")
201 (concat (car field) " ")))))
202 (setq field (cdr field)))
204 (cons (cons (concat wl-draft-mime-bcc-field-name ": ")
205 (concat wl-draft-mime-bcc-field-name ": "))
207 (setq wl-draft-field-completion-list cl)
208 (setq wl-address-complete-header-regexp
210 (append wl-address-complete-header-list
211 (list (concat wl-draft-mime-bcc-field-name ":")))))))
213 (defun wl-draft-make-mail-followup-to (recipients)
214 (if (elmo-list-member
215 (or wl-user-mail-address-list
216 (list (wl-address-header-extract-address wl-from)))
218 (let ((rlist (elmo-list-delete
219 (or wl-user-mail-address-list
220 (list (wl-address-header-extract-address wl-from)))
224 (lambda (item) (string= (downcase elem)
227 (if (elmo-list-member rlist (mapcar 'downcase
228 wl-subscribed-mailing-list))
230 (append rlist (list (wl-address-header-extract-address
234 (defun wl-draft-delete-myself-from-cc (to cc)
235 (let ((myself (or wl-user-mail-address-list
236 (list (wl-address-header-extract-address wl-from)))))
237 (cond (wl-draft-always-delete-myself ; always-delete option
238 (elmo-list-delete myself cc
241 (lambda (item) (string= (downcase elem)
244 ((elmo-list-member (append to cc) ; subscribed mailing-list
245 (mapcar 'downcase wl-subscribed-mailing-list))
246 (elmo-list-delete myself cc
249 (lambda (item) (string= (downcase elem)
254 (defun wl-draft-forward (original-subject summary-buf)
255 (let (references parent-folder)
256 (with-current-buffer summary-buf
257 (setq parent-folder (wl-summary-buffer-folder-name)))
258 (with-current-buffer (wl-message-get-original-buffer)
259 (setq references (nconc
260 (std11-field-bodies '("References" "In-Reply-To"))
261 (list (std11-field-body "Message-Id"))))
262 (setq references (delq nil references)
263 references (mapconcat 'identity references " ")
264 references (wl-draft-parse-msg-id-list-string references)
265 references (wl-delete-duplicates references)
266 references (when references
267 (mapconcat 'identity references "\n\t"))))
268 (and wl-draft-use-frame
269 (get-buffer-window summary-buf)
270 (select-window (get-buffer-window summary-buf)))
271 (wl-draft (list (cons 'To "")
273 (concat wl-forward-subject-prefix original-subject))
274 (cons 'References references))
275 nil nil nil nil parent-folder))
276 (goto-char (point-max))
277 (wl-draft-insert-message)
278 (mail-position-on-field "To"))
280 (defun wl-draft-strip-subject-re (subject)
281 "Remove \"Re:\" from SUBJECT string. Shamelessly copied from Gnus."
282 (if (string-match wl-subject-prefix-regexp subject)
283 (substring subject (match-end 0))
286 (defun wl-draft-self-reply-p ()
287 "Return t when From address in the current message is user's self one or not."
288 (wl-address-user-mail-address-p (or (elmo-field-body "From") "")))
290 (defun wl-draft-reply (buf with-arg summary-buf &optional number)
291 "Reply to BUF buffer message.
292 Reply to author if WITH-ARG is non-nil."
295 to mail-followup-to cc subject in-reply-to references newsgroups
296 to-alist cc-alist decoder parent-folder)
297 (when (buffer-live-p summary-buf)
298 (with-current-buffer summary-buf
299 (setq parent-folder (wl-summary-buffer-folder-name))))
300 (set-buffer (or buf mime-mother-buffer))
301 (setq r-list (if with-arg wl-draft-reply-with-argument-list
302 wl-draft-reply-without-argument-list))
305 (when (let ((condition (car (car r-list))))
306 (cond ((stringp condition)
307 (std11-field-body condition))
312 ((stringp (car condition))
313 (or (std11-field-body (car condition))
315 ((symbolp (car condition))
316 (or (funcall (car condition))
320 (setq condition (cdr condition)))
323 (funcall condition))))
324 (let ((r-to-list (nth 0 (cdr (car r-list))))
325 (r-cc-list (nth 1 (cdr (car r-list))))
326 (r-ng-list (nth 2 (cdr (car r-list)))))
327 (when (and (member "Followup-To" r-ng-list)
328 (string= (std11-field-body "Followup-To") "poster"))
329 (setq r-to-list (cons "From" r-to-list))
330 (setq r-ng-list (delete "Followup-To"
331 (copy-sequence r-ng-list))))
332 (if (and r-to-list (symbolp r-to-list))
333 (setq to (wl-concat-list (funcall r-to-list) ","))
334 (setq to (wl-concat-list (cons to
335 (elmo-multiple-fields-body-list
338 (if (and r-cc-list (symbolp r-cc-list))
339 (setq cc (wl-concat-list (funcall r-cc-list) ","))
340 (setq cc (wl-concat-list (cons cc
341 (elmo-multiple-fields-body-list
344 (if (and r-ng-list (symbolp r-ng-list))
345 (setq newsgroups (wl-concat-list (funcall r-ng-list) ","))
346 (setq newsgroups (wl-concat-list (cons newsgroups
351 (setq r-list (cdr r-list)))
352 (error "No match field: check your `wl-draft-reply-%s-argument-list'"
353 (if with-arg "with" "without")))
354 (setq subject (std11-field-body "Subject"))
355 (setq to (wl-parse-addresses to)
356 cc (wl-parse-addresses cc))
357 (with-temp-buffer ; to keep raw buffer unibyte.
358 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
359 (setq decoder (mime-find-field-decoder 'Subject 'plain))
360 (setq subject (if (and subject decoder)
361 (funcall decoder subject) subject))
365 (setq decoder (mime-find-field-decoder 'To 'plain))
366 (cons (nth 1 (std11-extract-address-components addr))
367 (if decoder (funcall decoder addr) addr)))
372 (setq decoder (mime-find-field-decoder 'Cc 'plain))
373 (cons (nth 1 (std11-extract-address-components addr))
374 (if decoder (funcall decoder addr) addr)))
376 (and wl-reply-subject-prefix
377 (setq subject (concat wl-reply-subject-prefix
378 (wl-draft-strip-subject-re
380 (setq in-reply-to (std11-field-body "Message-Id"))
381 (setq references (nconc
382 (std11-field-bodies '("References" "In-Reply-To"))
384 (setq to (delq nil (mapcar 'car to-alist)))
385 (setq cc (delq nil (mapcar 'car cc-alist)))
386 ;; if subscribed mailing list is contained in cc or to
387 ;; and myself is contained in cc,
388 ;; delete myself from cc.
389 (setq cc (wl-draft-delete-myself-from-cc to cc))
390 (when wl-insert-mail-followup-to
391 (setq mail-followup-to
392 (wl-draft-make-mail-followup-to (append to cc)))
393 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
394 (with-temp-buffer ; to keep raw buffer unibyte.
395 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
396 (setq newsgroups (wl-parse newsgroups
397 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
398 newsgroups (wl-delete-duplicates newsgroups)
403 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
404 (if decoder (funcall decoder grp) grp))
406 (setq to (wl-delete-duplicates to nil t))
407 (setq cc (wl-delete-duplicates
408 (append (wl-delete-duplicates cc nil t)
409 to (copy-sequence to))
411 (and to (setq to (mapconcat
413 (if wl-draft-reply-use-address-with-full-name
414 (or (cdr (assoc addr to-alist)) addr)
417 (and cc (setq cc (mapconcat
419 (if wl-draft-reply-use-address-with-full-name
420 (or (cdr (assoc addr cc-alist)) addr)
423 (and mail-followup-to
424 (setq mail-followup-to
427 (if wl-draft-reply-use-address-with-full-name
428 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
430 mail-followup-to ",\n\t")))
431 (and (null to) (setq to cc cc nil))
432 (setq references (delq nil references)
433 references (mapconcat 'identity references " ")
434 references (wl-draft-parse-msg-id-list-string references)
435 references (wl-delete-duplicates references)
436 references (if references
437 (mapconcat 'identity references "\n\t")))
438 (and wl-draft-use-frame
439 (get-buffer-window summary-buf)
440 (select-window (get-buffer-window summary-buf)))
441 (wl-draft (list (cons 'To to)
443 (cons 'Newsgroups newsgroups)
444 (cons 'Subject subject)
445 (cons 'In-Reply-To in-reply-to)
446 (cons 'References references)
447 (cons 'Mail-Followup-To mail-followup-to))
448 nil nil nil nil parent-folder)
449 (setq wl-draft-parent-number number)
450 (setq wl-draft-reply-buffer buf)
451 (setq wl-draft-config-variables
452 (append wl-draft-reply-saved-variables
453 wl-draft-config-variables)))
454 (run-hooks 'wl-reply-hook))
456 (defun wl-draft-reply-position (position)
457 (cond ((eq position 'body)
458 (wl-draft-body-goto-top))
459 ((eq position 'bottom)
460 (wl-draft-body-goto-bottom))
462 (goto-char (point-min)))
463 ((and (stringp position)
464 (std11-field-body position))
465 (progn (mail-position-on-field position)
466 (wl-draft-beginning-of-line)))
468 (while (car position)
469 (wl-draft-reply-position (car position))
470 (setq position (cdr position))))))
472 (defun wl-draft-add-references ()
473 (wl-draft-add-in-reply-to "References"))
475 (defun wl-draft-add-in-reply-to (&optional alt-field)
476 (let* ((mes-id (save-excursion
477 (set-buffer mail-reply-buffer)
478 (std11-field-body "message-id")))
479 (field (or alt-field "In-Reply-To"))
480 (ref (std11-field-body field))
481 (ref-list nil) (st nil))
482 (when (and mes-id ref)
483 (while (string-match "<[^>]+>" ref st)
485 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
488 (member mes-id ref-list))
492 (when (mail-position-on-field field)
494 (while (looking-at "^[ \t]")
496 (setq mes-id (concat "\t" mes-id "\n")))
500 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
501 &optional ignored-fields)
504 (narrow-to-region (point)(point))
506 (with-current-buffer mail-reply-buffer
508 (decode-mime-charset-region (point-min) (point-max)
510 (buffer-substring-no-properties
511 (point-min) (point-max))))
513 (goto-char (point-min))
514 (wl-draft-delete-fields ignored-fields))
515 (goto-char (point-max))
516 (push-mark (point) nil t)
517 (goto-char (point-min)))
519 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
520 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
521 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
522 (run-hooks 'wl-draft-cited-hook)
523 (when (if wl-draft-add-references
524 (wl-draft-add-references)
525 (if wl-draft-add-in-reply-to
526 (wl-draft-add-in-reply-to)))
527 (wl-highlight-headers 'for-draft)) ; highlight when added References:
528 (when wl-highlight-body-too
529 (wl-highlight-body-region beg (point-max)))))
531 (defun wl-message-news-p ()
532 "If exist valid Newsgroups field, return non-nil."
533 (std11-field-body "Newsgroups"))
535 (defun wl-message-field-exists-p (field)
536 "If FIELD exist and FIELD value is not empty, return non-nil."
537 (let ((value (std11-field-body field)))
539 (not (string= value "")))))
541 (defun wl-message-mail-p ()
542 "If exist To, Cc or Bcc field, return non-nil."
543 (or (wl-message-field-exists-p "To")
544 (wl-message-field-exists-p "Resent-to")
545 (wl-message-field-exists-p "Cc")
546 (wl-message-field-exists-p "Bcc")
547 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
548 ;;; This may be needed..
549 ;;; (wl-message-field-exists-p "Fcc")
552 (defun wl-draft-edit-string (string)
553 (let ((cur-buf (current-buffer))
554 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
555 to subject in-reply-to cc references newsgroups mail-followup-to
556 content-type content-transfer-encoding from
561 (setq to (std11-field-body "To"))
564 (decode-mime-charset-string
567 (setq subject (std11-field-body "Subject"))
568 (setq subject (and subject
570 (decode-mime-charset-string
573 (setq from (std11-field-body "From")
576 (decode-mime-charset-string
579 (setq in-reply-to (std11-field-body "In-Reply-To"))
580 (setq cc (std11-field-body "Cc"))
583 (decode-mime-charset-string
586 (setq references (std11-field-body "References"))
587 (setq newsgroups (std11-field-body "Newsgroups"))
588 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
589 (setq content-type (std11-field-body "Content-Type"))
590 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
591 (goto-char (point-min))
592 (or (re-search-forward "\n\n" nil t)
593 (search-forward (concat mail-header-separator "\n") nil t))
599 (nth 1 (std11-extract-address-components from))
600 wl-user-mail-address-list)
604 (cons 'Subject subject)
605 (cons 'Newsgroups newsgroups)
606 (cons 'Mail-Followup-To mail-followup-to)
607 (cons 'In-Reply-To in-reply-to)
608 (cons 'References references))
609 content-type content-transfer-encoding
610 (buffer-substring (point) (point-max))
612 (kill-buffer tmp-buf))
613 ;; Set cursor point to the top.
614 (goto-char (point-min))
615 (search-forward (concat mail-header-separator "\n") nil t)
616 (run-hooks 'wl-draft-reedit-hook)
617 (and to (mail-position-on-field "To"))))
619 (defun wl-draft-insert-current-message (dummy)
621 (let (original-buffer
623 mail-citation-hook mail-yank-hooks
624 wl-draft-add-references wl-draft-add-in-reply-to
625 wl-draft-cite-function)
626 (with-current-buffer wl-draft-buffer-cur-summary-buffer
627 (with-current-buffer wl-message-buffer
628 (setq original-buffer (wl-message-get-original-buffer))
630 (with-current-buffer original-buffer
632 (error "No current message"))))
633 (setq mail-reply-buffer original-buffer)
634 (wl-draft-yank-from-mail-reply-buffer
636 wl-ignored-forwarded-headers)))
638 (defun wl-draft-insert-get-message (dummy)
639 (let ((fld (completing-read
641 (if (memq 'read-folder wl-use-folder-petname)
642 (wl-folder-get-entity-with-petname)
643 wl-folder-entity-hashtb)
644 nil nil wl-default-spec
645 'wl-read-folder-hist))
646 (number (call-interactively
647 (function (lambda (num)
648 (interactive "nNumber: ")
650 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
651 mail-citation-hook mail-yank-hooks
652 wl-draft-cite-function)
655 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
658 (elmo-make-fetch-strategy 'entire)
659 nil mail-reply-buffer)
660 (wl-draft-yank-from-mail-reply-buffer nil))
661 (kill-buffer mail-reply-buffer))))
664 ;; default body citation func
666 (defun wl-default-draft-cite ()
667 (let ((mail-yank-ignored-headers "[^:]+:")
668 (mail-yank-prefix "> ")
669 date from cite-title)
671 (if (< (mark t) (point))
672 (exchange-point-and-mark))
673 (narrow-to-region (point)(point-max))
674 (setq date (std11-field-body "date")
675 from (std11-field-body "from")))
677 (insert (format "At %s,\n%s wrote:\n"
678 (or date "some time ago")
679 (if wl-default-draft-cite-decorate-author
680 (funcall wl-summary-from-function
683 (mail-indent-citation)))
685 (defvar wl-draft-buffer nil "Draft buffer to yank content.")
686 (defun wl-draft-yank-to-draft-buffer (buffer)
687 "Yank BUFFER content to `wl-draft-buffer'."
688 (set-buffer wl-draft-buffer)
689 (let ((mail-reply-buffer buffer))
690 (wl-draft-yank-from-mail-reply-buffer nil)
691 (kill-buffer buffer)))
693 (defun wl-draft-yank-original (&optional arg)
694 "Yank original message."
697 (let (buf mail-reply-buffer)
701 (setq buf (current-buffer)))
702 (setq mail-reply-buffer buf)
703 (wl-draft-yank-from-mail-reply-buffer nil))
704 (wl-draft-yank-current-message-entity)))
706 (defun wl-draft-hide (editing-buffer)
707 "Hide the editing draft buffer if possible."
708 (when (and editing-buffer
709 (buffer-live-p editing-buffer))
710 (set-buffer editing-buffer)
711 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
712 fld-buf sum-win fld-win)
713 (if (and wl-draft-use-frame
714 (> (length (visible-frame-list)) 1))
720 ;; stay folder window if required
721 (when wl-stay-folder-window
722 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
723 (if (setq fld-win (get-buffer-window fld-buf))
724 (select-window fld-win)
725 (if wl-draft-resume-folder-window ;; resume folder window
726 (switch-to-buffer fld-buf)))))
727 (if (buffer-live-p sum-buf)
728 (if (setq sum-win (get-buffer-window sum-buf t))
729 ;; if Summary is on the frame, select it.
730 (select-window sum-win)
731 ;; if summary is not on the frame, switch to it.
732 (if (and wl-stay-folder-window
733 (or wl-draft-resume-folder-window fld-win))
734 (wl-folder-select-buffer sum-buf)
735 (switch-to-buffer sum-buf))))))))
737 (defun wl-draft-delete (editing-buffer)
738 "Kill the editing draft buffer and delete the file corresponds to it."
741 (set-buffer editing-buffer)
742 (if wl-draft-buffer-file-name
744 (if (file-exists-p wl-draft-buffer-file-name)
745 (delete-file wl-draft-buffer-file-name))
746 (let ((msg (and wl-draft-buffer-file-name
747 (string-match "[0-9]+$" wl-draft-buffer-file-name)
749 (match-string 0 wl-draft-buffer-file-name)))))
750 (wl-draft-config-info-operation msg 'delete))))
751 (set-buffer-modified-p nil) ; force kill
752 (kill-buffer editing-buffer))))
754 (defun wl-draft-kill (&optional force-kill)
755 "Kill current draft buffer and quit editing."
758 (when (and (or (eq major-mode 'wl-draft-mode)
759 (eq major-mode 'mail-mode))
761 (y-or-n-p "Kill Current Draft? ")))
762 (let ((cur-buf (current-buffer)))
763 (when (and wl-draft-parent-number
764 (not (string= wl-draft-parent-folder "")))
765 (let* ((number wl-draft-parent-number)
766 (folder-name wl-draft-parent-folder)
767 (folder (wl-folder-get-elmo-folder folder-name))
769 (if (and (setq buffer (wl-summary-get-buffer folder-name))
770 (with-current-buffer buffer
771 (string= (wl-summary-buffer-folder-name)
773 (with-current-buffer buffer
774 (elmo-folder-unmark-answered folder (list number))
775 (wl-summary-jump-to-msg number)
776 (wl-summary-update-mark number))
777 (elmo-folder-open folder 'load-msgdb)
778 (elmo-folder-unmark-answered folder (list number))
779 (elmo-folder-close folder))))
780 (wl-draft-hide cur-buf)
781 (wl-draft-delete cur-buf)))
784 (defun wl-draft-fcc ()
785 "Add a new Fcc field, with file name completion."
787 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
788 (mail-position-on-field "to"))
791 ;; Imported from message.el.
792 (defun wl-draft-elide-region (b e)
793 "Elide the text in the region.
794 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
798 (insert wl-draft-elide-ellipsis))
800 ;; Imported from message.el.
801 (defun wl-draft-beginning-of-line (&optional n)
802 "Move point to beginning of header value or to beginning of line."
804 (let ((zrs 'zmacs-region-stays))
805 (when (and (interactive-p) (boundp zrs))
807 (if (wl-draft-point-in-header-p)
808 (let* ((here (point))
809 (bol (progn (beginning-of-line n) (point)))
810 (eol (line-end-position))
811 (eoh (and (looking-at "[^ \t]")
812 (re-search-forward ": *" eol t))))
813 (if (and eoh (or (> here eoh) (= here bol)))
816 (beginning-of-line n)))
818 (defun wl-draft-point-in-header-p ()
819 "Return t if point is in the header."
822 (goto-char (point-min))
823 (not (re-search-forward
824 (concat "^" (regexp-quote mail-header-separator) "\n")
827 ;; function for wl-sent-message-via
829 (defmacro wl-draft-sent-message-p (type)
830 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
832 (defmacro wl-draft-set-sent-message (type result &optional server-port)
833 (` (let ((element (assq (, type) wl-sent-message-via)))
835 (unless (eq (nth 1 element) (, result))
836 (setcdr element (list (, result) (, server-port)))
837 (setq wl-sent-message-modified t))
838 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
839 (setq wl-sent-message-modified t)))))
841 (defun wl-draft-sent-message-results ()
842 (let ((results wl-sent-message-via)
843 unplugged-via sent-via)
845 (cond ((eq (nth 1 (car results)) 'unplugged)
846 (push (caar results) unplugged-via))
847 ((eq (nth 1 (car results)) 'sent)
848 (push (caar results) sent-via)))
849 (setq results (cdr results)))
850 (list unplugged-via sent-via)))
852 (defun wl-draft-write-sendlog (status proto server to id)
853 "Write send log file, if `wl-draft-sendlog' is non-nil."
854 (when wl-draft-sendlog
856 (let* ((filename (expand-file-name wl-draft-sendlog-filename
857 elmo-msgdb-directory))
858 (filesize (nth 7 (file-attributes filename)))
859 (server (if server (concat " server=" server) ""))
861 ((memq proto '(fcc queue))
862 (format " folder=\"%s\"" to))
864 (format " ng=<%s>" to))
869 (mapcar '(lambda(x) (format "<%s>" x)) to)
872 (id (if id (concat " id=" id) ""))
873 (time (format-time-string "%Y/%m/%d %T")))
874 (insert (format "%s proto=%s stat=%s%s%s%s\n"
875 time proto status server to id))
876 (if (and wl-draft-sendlog-max-size filesize
877 (> filesize wl-draft-sendlog-max-size))
878 (rename-file filename (concat filename ".old") t))
879 (if (file-writable-p filename)
880 (write-region-as-binary (point-min) (point-max)
882 (message "%s is not writable." filename))))))
884 (defun wl-draft-get-header-delimiter (&optional delete)
885 ;; If DELETE is non-nil, replace the header delimiter with a blank line
887 (goto-char (point-min))
888 (when (re-search-forward
889 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
893 (setq delimline (point-marker)))
896 (defun wl-draft-send-mail-with-qmail ()
897 "Pass the prepared message buffer to qmail-inject.
898 Refer to the documentation for the variable `send-mail-function'
899 to find out how to use this."
900 (if (and wl-draft-qmail-send-plugged
901 (not (elmo-plugged-p)))
902 (wl-draft-set-sent-message 'mail 'unplugged)
904 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
905 (let ((id (std11-field-body "Message-ID"))
906 (to (std11-field-body "To")))
910 'call-process-region 1 (point-max) wl-qmail-inject-program
912 wl-qmail-inject-args))
913 ;; qmail-inject doesn't say anything on it's stdout/stderr,
914 ;; we have to look at the retval instead
916 (wl-draft-set-sent-message 'mail 'sent)
917 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
918 (1 (error "`qmail-inject' reported permanent failure"))
919 (111 (error "`qmail-inject' reported transient failure"))
920 ;; should never happen
921 (t (error "`qmail-inject' reported unknown failure"))))))
923 (defun wl-draft-parse-msg-id-list-string (string)
924 "Get msg-id list from STRING."
926 (dolist (parsed-id (std11-parse-msg-ids-string string))
927 (when (eq (car parsed-id) 'msg-id)
928 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
930 (nreverse msg-id-list)))
932 (defun wl-draft-eword-encode-address-list (string &optional column)
933 "Encode header field STRING as list of address, and return the result.
934 Cause an error when STRING contains invalid address.
935 Optional argument COLUMN is start-position of the field."
936 (car (eword-encode-rword-list
937 (or column eword-encode-default-start-column)
938 (eword-encode-addresses-to-rword-list
939 (wl-draft-std11-parse-addresses (std11-lexical-analyze string))))))
941 (defun wl-draft-std11-parse-addresses (lal)
942 (let ((ret (std11-parse-address lal)))
943 (when (and (not (and (eq (length lal) 1)
944 (eq (car (car lal)) 'spaces)))
946 (error "Error while parsing address"))
948 (let ((dest (list (car ret))))
950 (while (and (setq ret (std11-parse-ascii-token lal))
951 (string-equal (cdr (assq 'specials (car ret))) ",")
952 (setq ret (std11-parse-address (cdr ret)))
954 (setq dest (cons (car ret) dest))
955 (setq lal (cdr ret)))
956 (while (eq 'spaces (car (car lal)))
957 (setq lal (cdr lal)))
958 (if lal (error "Error while parsing address"))
961 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
962 "Get mailbox list of FIELD from current buffer.
963 The buffer is expected to be narrowed to just the headers of the message.
964 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
965 from current buffer."
967 (let ((case-fold-search t)
968 (inhibit-read-only t)
970 mailbox-list beg seq has-group-list)
971 (goto-char (point-min))
972 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
975 (re-search-forward "^[^ \t]" nil 'move)
977 (skip-chars-backward "\n")
978 (setq seq (std11-lexical-analyze
979 (buffer-substring-no-properties beg (point))))
980 (setq addresses (wl-draft-std11-parse-addresses seq))
982 (cond ((eq (car (car addresses)) 'group)
983 (setq has-group-list t)
987 'std11-address-string
988 (nth 2 (car addresses))))))
989 ((eq (car (car addresses)) 'mailbox)
990 (setq address (nth 1 (car addresses)))
994 (std11-addr-to-string
995 (if (eq (car address) 'phrase-route-addr)
998 (setq addresses (cdr addresses)))
999 (when (and remove-group-list has-group-list)
1000 (delete-region beg (point))
1001 (insert (wl-address-string-without-group-list-contents seq))))
1004 (defun wl-draft-deduce-address-list (buffer header-start header-end)
1005 "Get address list suitable for smtp RCPT TO:<address>.
1006 Group list content is removed if `wl-draft-remove-group-list-contents' is
1008 (let ((fields (if (and wl-draft-doing-mime-bcc
1009 wl-draft-disable-bcc-for-mime-bcc)
1011 '("to" "cc" "bcc")))
1012 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
1013 (case-fold-search t)
1017 (narrow-to-region header-start header-end)
1018 (goto-char (point-min))
1020 (if (re-search-forward "^resent-to[\t ]*:" nil t)
1021 (setq fields resent-fields)))
1025 (wl-draft-parse-mailbox-list
1027 wl-draft-remove-group-list-contents)))
1028 (setq fields (cdr fields)))
1034 (defun wl-draft-send-mail-with-smtp ()
1035 "Send the prepared message buffer with SMTP."
1037 (let* ((errbuf (if mail-interactive
1038 (generate-new-buffer " smtp errors")
1040 (case-fold-search t)
1041 (default-case-fold-search t)
1042 (sender (or wl-envelope-from
1043 (wl-address-header-extract-address wl-from)))
1044 (delimline (save-excursion
1045 (goto-char (point-min))
1047 (concat "^" (regexp-quote mail-header-separator)
1051 (or wl-smtp-posting-server smtp-server "localhost"))
1052 (smtp-service (or wl-smtp-posting-port smtp-service))
1053 (smtp-local-domain (or smtp-local-domain wl-local-domain))
1054 (id (std11-field-body "message-id"))
1056 (if (not (elmo-plugged-p smtp-server smtp-service))
1057 (wl-draft-set-sent-message 'mail 'unplugged
1058 (cons smtp-server smtp-service))
1061 ;; Instead of `smtp-deduce-address-list'.
1062 (setq recipients (wl-draft-deduce-address-list
1063 (current-buffer) (point-min) delimline))
1064 (unless recipients (error "No recipients"))
1065 ;; Insert an extra newline if we need it to work around
1066 ;; Sun's bug that swallows newlines.
1067 (goto-char (1+ delimline))
1068 (if (eval mail-mailer-swallows-blank-line)
1070 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1071 (if mail-interactive
1075 (wl-draft-delete-field "bcc" delimline)
1076 (wl-draft-delete-field "resent-bcc" delimline)
1077 (let (process-connection-type)
1080 (wl-smtp-extension-bind
1082 (smtp-send-buffer sender recipients (current-buffer))
1084 (wl-draft-write-sendlog 'failed 'smtp smtp-server
1086 (if (and (eq (car err) 'smtp-response-error)
1087 (= (nth 1 err) 535))
1089 (wl-smtp-password-key
1091 (car smtp-sasl-mechanisms)
1093 (signal (car err) (cdr err)))))
1094 (wl-draft-set-sent-message 'mail 'sent)
1095 (wl-draft-write-sendlog
1096 'ok 'smtp smtp-server recipients id)))))
1097 (if (bufferp errbuf)
1098 (kill-buffer errbuf))))))
1100 (defun wl-draft-send-mail-with-pop-before-smtp ()
1101 "Send the prepared message buffer with POP-before-SMTP."
1102 (require 'elmo-pop3)
1106 :user (or wl-pop-before-smtp-user
1107 elmo-pop3-default-user)
1108 :server (or wl-pop-before-smtp-server
1109 elmo-pop3-default-server)
1110 :port (or wl-pop-before-smtp-port
1111 elmo-pop3-default-port)
1112 :auth (or wl-pop-before-smtp-authenticate-type
1113 elmo-pop3-default-authenticate-type)
1114 :stream-type (or wl-pop-before-smtp-stream-type
1115 elmo-pop3-default-stream-type))))
1116 (condition-case error
1118 (elmo-pop3-get-session session)
1119 (when session (elmo-network-close-session session)))
1121 (elmo-network-close-session session)
1122 (unless (string= (nth 1 error) "Unplugged")
1123 (signal (car error)(cdr error))))))
1124 (wl-draft-send-mail-with-smtp))
1126 (defun wl-draft-insert-required-fields (&optional force-msgid)
1127 "Insert Message-ID, Date, and From field.
1128 If FORCE-MSGID, insert message-id regardless of `wl-insert-message-id'."
1129 ;; Insert Message-Id field...
1130 (goto-char (point-min))
1131 (when (and (or force-msgid
1132 wl-insert-message-id)
1133 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1134 (insert (concat "Message-ID: "
1135 (funcall wl-message-id-function)
1137 ;; Insert date field.
1138 (goto-char (point-min))
1139 (or (re-search-forward "^Date[ \t]*:" nil t)
1140 (wl-draft-insert-date-field))
1141 ;; Insert from field.
1142 (goto-char (point-min))
1143 (or (re-search-forward "^From[ \t]*:" nil t)
1144 (wl-draft-insert-from-field)))
1146 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1147 "Send the message in the current buffer."
1149 (std11-narrow-to-header mail-header-separator)
1150 (wl-draft-insert-required-fields)
1151 ;; Delete null fields.
1152 (goto-char (point-min))
1153 (while (re-search-forward "^[^ \t\n:]+:[ \t]*\n" nil t)
1155 ;; ignore any blank lines in the header
1156 (while (re-search-forward "\n\n\n*" nil t)
1157 (replace-match "\n")))
1158 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1159 (wl-draft-dispatch-message)
1160 (when kill-when-done
1161 ;; hide editing-buffer.
1162 (wl-draft-hide editing-buffer)
1163 ;; delete editing-buffer and its file.
1164 (wl-draft-delete editing-buffer)))
1166 (defun wl-draft-dispatch-message (&optional mes-string)
1167 "Send the message in the current buffer. Not modified the header fields."
1168 (let (delimline mime-bcc)
1169 (if (and wl-draft-verbose-send mes-string)
1170 (message mes-string))
1172 (setq delimline (wl-draft-get-header-delimiter t))
1173 (unless wl-draft-fcc-list
1174 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1176 (setq wl-sent-message-modified nil)
1179 (if (and (wl-message-mail-p)
1180 (not (wl-draft-sent-message-p 'mail)))
1181 (if (or (not (or wl-draft-force-queuing
1182 wl-draft-force-queuing-mail))
1183 (memq 'mail wl-sent-message-queued))
1185 (setq mime-bcc (wl-draft-mime-bcc-field))
1186 (funcall wl-draft-send-mail-function)
1187 (when (not (zerop (length mime-bcc)))
1188 (wl-draft-do-mime-bcc mime-bcc)))
1189 (push 'mail wl-sent-message-queued)
1190 (wl-draft-set-sent-message 'mail 'unplugged)))
1191 (if (and (wl-message-news-p)
1192 (not (wl-draft-sent-message-p 'news))
1193 (not (wl-message-field-exists-p "Resent-to")))
1194 (if (or (not (or wl-draft-force-queuing
1195 wl-draft-force-queuing-news))
1196 (memq 'news wl-sent-message-queued))
1197 (funcall wl-draft-send-news-function)
1198 (push 'news wl-sent-message-queued)
1199 (wl-draft-set-sent-message 'news 'unplugged))))
1200 (let* ((status (wl-draft-sent-message-results))
1201 (unplugged-via (car status))
1202 (sent-via (nth 1 status)))
1203 ;; If one sent, process fcc folder.
1204 (if (and sent-via wl-draft-fcc-list)
1206 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1208 (setq wl-draft-fcc-list nil)))
1209 (if wl-draft-use-cache
1210 (let ((id (std11-field-body "Message-ID"))
1211 (elmo-enable-disconnected-operation t))
1212 (elmo-file-cache-save (elmo-file-cache-get-path id)
1214 ;; If one unplugged, append queue.
1215 (when (and unplugged-via
1216 wl-sent-message-modified)
1217 (if wl-draft-enable-queuing
1219 (wl-draft-queue-append wl-sent-message-via)
1220 (setq wl-sent-message-modified 'requeue))
1221 (error "Unplugged")))
1222 (when wl-draft-verbose-send
1223 (if (and unplugged-via sent-via);; combined message
1225 (setq wl-draft-verbose-msg
1226 (format "Sending%s and Queuing%s..."
1227 sent-via unplugged-via))
1228 (message (concat wl-draft-verbose-msg "done")))
1230 (message (concat mes-string
1231 (if sent-via "done" "failed")))))))))
1232 (not wl-sent-message-modified)) ;; return value
1234 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1235 "Force send current buffer as raw message."
1238 (let (wl-interactive-send
1239 ;;; wl-draft-verbose-send
1240 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1241 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1244 (wl-draft-send kill-when-done mes-string))))
1246 (defun wl-draft-clone-local-variables ()
1247 (let ((locals (buffer-local-variables))
1250 (when (and (consp (car locals))
1252 (string-match wl-draft-clone-local-variable-regexp
1253 (symbol-name (car (car locals)))))
1254 (wl-append result (list (car (car locals)))))
1255 (setq locals (cdr locals)))
1258 (defcustom wl-draft-send-confirm-with-preview t
1259 "Non-nil to invoke preview through confirmation of sending.
1260 This variable is valid when `wl-interactive-send' has non-nil value."
1264 (defun wl-draft-send-confirm ()
1267 (condition-case quit
1269 (when wl-draft-send-confirm-with-preview
1270 (wl-draft-preview-message))
1272 (goto-char (point-min)) ; to show recipients in header
1275 (message "Send current draft? <y/n/j(up)/k(down)> ")
1276 (setq answer (let ((cursor-in-echo-area t)) (read-char)))
1295 (throw 'done nil)))))))
1297 (when wl-draft-send-confirm-with-preview
1298 (mime-preview-quit)))))
1300 (defun wl-draft-send (&optional kill-when-done mes-string)
1301 "Send current draft message.
1302 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1304 ;; Don't call this explicitly.
1305 ;; Added to 'wl-draft-send-hook (by teranisi)
1306 ;; (wl-draft-config-exec)
1307 (run-hooks 'wl-draft-send-hook)
1308 (when (or (not wl-interactive-send)
1309 (wl-draft-send-confirm))
1310 (let ((send-mail-function 'wl-draft-raw-send)
1311 (editing-buffer (current-buffer))
1312 (sending-buffer (wl-draft-generate-clone-buffer
1313 " *wl-draft-sending-buffer*"
1314 (append wl-draft-config-variables
1315 (wl-draft-clone-local-variables))))
1316 (wl-draft-verbose-msg nil)
1319 (save-excursion (set-buffer sending-buffer)
1320 (if (and (not (wl-message-mail-p))
1321 (not (wl-message-news-p)))
1322 (error "No recipient is specified"))
1323 (expand-abbrev) ; for mail-abbrevs
1324 (let ((mime-header-encode-method-alist
1326 '((wl-draft-eword-encode-address-list
1327 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
1328 (if (boundp 'mime-header-encode-method-alist)
1329 (symbol-value 'mime-header-encode-method-alist)))))
1330 (run-hooks 'mail-send-hook) ; translate buffer
1333 (if wl-draft-verbose-send
1334 (message (or mes-string "Sending...")))
1335 (funcall wl-draft-send-function editing-buffer kill-when-done)
1336 ;; Now perform actions on successful sending.
1337 (while mail-send-actions
1339 (apply (car (car mail-send-actions))
1340 (cdr (car mail-send-actions)))
1342 (setq mail-send-actions (cdr mail-send-actions)))
1343 (if wl-draft-verbose-send
1344 (message (concat (or wl-draft-verbose-msg
1345 mes-string "Sending...")
1347 ;; kill sending buffer, anyway.
1348 (and (buffer-live-p sending-buffer)
1349 (kill-buffer sending-buffer))))))
1351 (defun wl-draft-mime-bcc-field ()
1352 "Return the MIME-Bcc field body. The field is deleted."
1353 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1354 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1356 (defun wl-draft-do-mime-bcc (field-body)
1357 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1358 (let ((orig-from (mime-decode-field-body (std11-field-body "from")
1360 (orig-subj (mime-decode-field-body (or (std11-field-body "subject")
1363 (recipients (wl-parse-addresses field-body))
1364 (draft-buffer (current-buffer))
1366 (save-window-excursion
1367 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1368 (not (zerop (length field-body))))
1369 (let ((wl-draft-doing-mime-bcc t))
1370 (dolist (recipient recipients)
1371 (wl-draft-create-buffer)
1372 (wl-draft-create-contents
1373 (append `((From . ,orig-from)
1375 (Subject . ,(concat "A blind carbon copy ("
1378 (wl-draft-default-headers)))
1379 (wl-draft-insert-mail-header-separator)
1380 (wl-draft-prepare-edit)
1381 (goto-char (point-max))
1382 (insert (or wl-draft-mime-bcc-body
1383 "This is a blind carbon copy.")
1385 (mime-edit-insert-tag "message" "rfc822")
1386 (insert-buffer draft-buffer)
1387 (let (wl-interactive-send)
1388 (wl-draft-send 'kill-when-done))))))))
1390 ;; Derived from `message-save-drafts' in T-gnus.
1391 (defun wl-draft-save ()
1392 "Save current draft."
1394 (if (buffer-modified-p)
1396 (message "Saving %s..." wl-draft-buffer-file-name)
1397 (let ((msg (buffer-substring-no-properties (point-min) (point-max))))
1398 (with-temp-file wl-draft-buffer-file-name
1400 ;; If no header separator, insert it.
1402 (goto-char (point-min))
1403 (unless (re-search-forward
1404 (concat "^" (regexp-quote mail-header-separator) "$")
1406 (goto-char (point-min))
1407 (if (re-search-forward "\n\n" nil t)
1408 (replace-match (concat "\n" mail-header-separator "\n"))
1409 (goto-char (point-max))
1410 (insert (if (eq (char-before) ?\n) "" "\n")
1411 mail-header-separator "\n"))))
1412 (let ((mime-header-encode-method-alist
1413 '((eword-encode-unstructured-field-body))))
1414 (mime-edit-translate-buffer))
1415 (wl-draft-get-header-delimiter t)))
1416 (set-buffer-modified-p nil)
1417 (wl-draft-config-info-operation
1418 (and (string-match "[0-9]+$" wl-draft-buffer-file-name)
1420 (match-string 0 wl-draft-buffer-file-name)))
1422 (message "Saving %s...done" wl-draft-buffer-file-name))
1423 (message "(No changes need to be saved)")))
1425 (defun wl-draft-mimic-kill-buffer ()
1426 "Kill the current (draft) buffer with query."
1428 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1431 (if (or (not bufname)
1432 (string-equal bufname "")
1433 (string-equal bufname (buffer-name)))
1434 (let ((bufname (current-buffer)))
1435 (when (or (not (buffer-modified-p))
1437 (format "Buffer %s modified; kill anyway? " bufname)))
1438 (set-buffer-modified-p nil)
1439 (wl-draft-hide bufname)
1440 (kill-buffer bufname)))
1441 (kill-buffer bufname))))
1443 (defun wl-draft-save-and-exit ()
1444 "Save current draft and exit current draft mode."
1447 (let ((editing-buffer (current-buffer)))
1448 (wl-draft-hide editing-buffer)
1449 (kill-buffer editing-buffer)))
1451 (defun wl-draft-send-and-exit ()
1452 "Send current draft message and kill it."
1456 (defun wl-draft-send-from-toolbar ()
1458 (let ((wl-interactive-send t))
1459 (wl-draft-send-and-exit)))
1461 (defun wl-draft-delete-field (field &optional delimline replace)
1462 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1464 (defun wl-draft-delete-fields (field &optional delimline replace)
1467 (goto-char (point-min))
1468 (if (search-forward "\n\n" nil t)
1469 (setq delimline (point))
1470 (setq delimline (point-max))))
1471 (narrow-to-region (point-min) delimline)
1472 (goto-char (point-min))
1473 (let ((regexp (concat "^" field ":"))
1474 (case-fold-search t))
1476 (if (looking-at regexp)
1482 (if (re-search-forward "^[^ \t]" nil t)
1483 (goto-char (match-beginning 0))
1486 (insert (concat field ": " replace "\n"))))
1488 (if (re-search-forward "^[^ \t]" nil t)
1489 (goto-char (match-beginning 0))
1492 (defun wl-draft-get-fcc-list (header-end)
1493 (if (and wl-draft-doing-mime-bcc
1494 wl-draft-disable-fcc-for-mime-bcc)
1496 (wl-draft-delete-field "fcc")
1499 (case-fold-search t))
1500 (or (markerp header-end) (error "HEADER-END must be a marker"))
1502 (goto-char (point-min))
1503 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1508 (buffer-substring-no-properties
1512 (skip-chars-backward " \t")
1515 (dolist (folder fcc-list)
1516 (wl-folder-confirm-existence
1517 (wl-folder-get-elmo-folder (eword-decode-string folder)))))
1518 (delete-region (match-beginning 0)
1519 (progn (forward-line 1) (point)))))
1522 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1523 (let ((send-mail-buffer (current-buffer))
1524 (tembuf (generate-new-buffer " fcc output"))
1525 (case-fold-search t)
1527 (or (markerp header-end) (error "HEADER-END must be a marker"))
1530 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1533 ;; insert just the headers to avoid moving the gap more than
1534 ;; necessary (the message body could be arbitrarily huge.)
1535 (insert-buffer-substring send-mail-buffer 1 header-end)
1536 (wl-draft-insert-required-fields t)
1537 (goto-char (point-max))
1538 (insert-buffer-substring send-mail-buffer header-end)
1539 (let ((id (std11-field-body "Message-ID"))
1540 (elmo-enable-disconnected-operation t)
1543 (unless (or cache-saved
1544 (elmo-folder-plugged-p
1545 (wl-folder-get-elmo-folder (car fcc-list))))
1546 (elmo-file-cache-save id nil) ;; for disconnected operation
1547 (setq cache-saved t))
1548 (if (elmo-folder-append-buffer
1549 (wl-folder-get-elmo-folder
1550 (eword-decode-string (car fcc-list)))
1551 (and wl-fcc-force-as-read 'read))
1552 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1553 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1554 (setq fcc-list (cdr fcc-list)))))
1555 (kill-buffer tembuf)))
1557 (defun wl-draft-on-field-p ()
1560 (goto-char (point-min))
1561 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1568 (if (or (looking-at ".*,[ \t]?$")
1569 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1571 (let ((pos (point)))
1574 (if (looking-at "^[ \t]")
1576 (if (re-search-forward ":" pos t) nil t)))))))
1580 (defun wl-draft (&optional header-alist
1581 content-type content-transfer-encoding
1584 "Write and send mail/news message with Wanderlust."
1591 (wl-plugged-init t))
1593 (wl-init)) ; returns immediately if already initialized.
1596 (let (buf-name header-alist-internal)
1598 (wl-draft-create-buffer parent-folder))
1600 (unless (cdr (assq 'From header-alist))
1602 (append (list (cons 'From wl-from)) header-alist)))
1603 (unless (cdr (assq 'To header-alist))
1608 (if (assq 'To header-alist)
1609 (setcdr (assq 'To header-alist) to)
1611 (append header-alist
1612 (list (cons 'To to))))))))
1613 (unless (cdr (assq 'Subject header-alist))
1614 (if (assq 'Subject header-alist)
1615 (setcdr (assq 'Subject header-alist) "")
1617 (append header-alist (list (cons 'Subject ""))))))
1618 (setq header-alist (append header-alist
1619 (wl-draft-default-headers)
1620 wl-draft-additional-header-alist
1621 (if body (list "" (cons 'Body body)))))
1622 (wl-draft-create-contents header-alist)
1624 (wl-draft-decode-body
1625 content-type content-transfer-encoding))
1626 (wl-draft-insert-mail-header-separator)
1627 (wl-draft-prepare-edit)
1629 (run-hooks 'wl-mail-setup-hook))
1630 (goto-char (point-min))
1631 (wl-user-agent-compose-internal) ;; user-agent
1634 (string= (cdr (assq 'To header-alist)) ""))
1635 (mail-position-on-field "To"))
1637 (goto-char (point-max))))
1640 (defun wl-draft-create-buffer (&optional parent-folder)
1641 (let* ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1642 (parent-folder (or parent-folder (wl-summary-buffer-folder-name)))
1643 (summary-buf (wl-summary-get-buffer parent-folder))
1645 (or (eq this-command 'wl-summary-reply)
1646 (eq this-command 'wl-summary-reply-with-citation)
1647 (eq this-command 'wl-summary-forward)
1648 (eq this-command 'wl-summary-target-mark-forward)
1649 (eq this-command 'wl-summary-target-mark-reply-with-citation)))
1650 buf-name file-name num change-major-mode-hook)
1651 (if (not (elmo-folder-message-file-p draft-folder))
1652 (error "%s folder cannot be used for draft folder" wl-draft-folder))
1653 (setq num (elmo-max-of-list
1654 (or (elmo-folder-list-messages draft-folder) '(0))))
1655 (setq num (+ 1 num))
1656 ;; To get unused buffer name.
1657 (while (get-buffer (concat wl-draft-folder "/" (int-to-string num)))
1658 (setq num (+ 1 num)))
1659 (setq buf-name (find-file-noselect
1661 (elmo-message-file-name
1662 (wl-folder-get-elmo-folder wl-draft-folder)
1664 ;; switch-buffer according to draft buffer style.
1665 (if wl-draft-use-frame
1666 (switch-to-buffer-other-frame buf-name)
1667 (if reply-or-forward
1668 (case wl-draft-reply-buffer-style
1670 (split-window-vertically)
1672 (switch-to-buffer buf-name))
1674 (switch-to-buffer buf-name))
1676 (delete-other-windows)
1677 (switch-to-buffer buf-name))
1679 (if (functionp wl-draft-reply-buffer-style)
1680 (funcall wl-draft-reply-buffer-style buf-name)
1681 (error "Invalid value for wl-draft-reply-buffer-style"))))
1682 (case wl-draft-buffer-style
1684 (when (eq major-mode 'wl-summary-mode)
1685 (wl-summary-toggle-disp-msg 'off))
1686 (split-window-vertically)
1688 (switch-to-buffer buf-name))
1690 (switch-to-buffer buf-name))
1692 (delete-other-windows)
1693 (switch-to-buffer buf-name))
1694 (t (if (functionp wl-draft-buffer-style)
1695 (funcall wl-draft-buffer-style buf-name)
1696 (error "Invalid value for wl-draft-buffer-style"))))))
1697 (set-buffer buf-name)
1698 (if (not (string-match (regexp-quote wl-draft-folder)
1700 (rename-buffer (concat wl-draft-folder "/" (int-to-string num))))
1703 (make-local-variable 'truncate-partial-width-windows)
1704 (setq truncate-partial-width-windows nil)
1705 (setq truncate-lines wl-draft-truncate-lines)
1706 (setq wl-sent-message-via nil)
1707 (setq wl-sent-message-queued nil)
1708 (setq wl-draft-buffer-file-name file-name)
1709 (setq wl-draft-config-exec-flag t)
1710 (setq wl-draft-parent-folder (or parent-folder ""))
1711 (or (eq this-command 'wl-folder-write-current-folder)
1712 (setq wl-draft-buffer-cur-summary-buffer summary-buf))
1715 (defun wl-draft-create-contents (header-alist)
1716 "header-alist' sample
1717 '(function ;; funcall
1718 string ;; insert string
1719 (symbol . string) ;; insert symbol-value: string
1720 (symbol . function) ;; (funcall) and if it returns string,
1721 ;; insert symbol-value: string
1722 (symbol . nil) ;; do nothing
1725 (unless (eq major-mode 'wl-draft-mode)
1726 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1727 (let ((halist header-alist)
1732 ((functionp (car halist)) (funcall (car halist)))
1734 ((stringp (car halist)) (insert (car halist) "\n"))
1736 ((consp (car halist))
1737 (setq field (car (car halist)))
1738 (setq value (cdr (car halist)))
1742 ((eq field 'Body) ; body
1744 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1746 (let ((value-return (funcall value)))
1747 (when (stringp value-return)
1748 (insert (symbol-name field) ": " value-return "\n"))))
1757 (setq halist (cdr halist)))))
1759 (defun wl-draft-prepare-edit ()
1760 (unless (eq major-mode 'wl-draft-mode)
1761 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1762 (let (change-major-mode-hook)
1763 (wl-draft-editor-mode)
1764 (when wl-draft-write-file-function
1765 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
1766 (wl-draft-overload-functions)
1767 (wl-highlight-headers 'for-draft)
1769 (clear-visited-file-modtime)))
1771 (defun wl-draft-decode-header ()
1773 (std11-narrow-to-header)
1774 (wl-draft-decode-message-in-buffer)
1777 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1780 (std11-field-body "content-type")))
1781 (content-transfer-encoding
1782 (or content-transfer-encoding
1783 (std11-field-body "content-transfer-encoding")))
1786 (std11-narrow-to-header)
1787 (wl-draft-delete-field "content-type")
1788 (wl-draft-delete-field "content-transfer-encoding")
1789 (goto-char (point-max))
1790 (setq delimline (point-marker))
1792 (narrow-to-region delimline (point-max))
1793 (goto-char (point-min))
1795 (insert "Content-type: " content-type "\n"))
1796 (when content-transfer-encoding
1797 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1798 (wl-draft-decode-message-in-buffer)
1799 (goto-char (point-min))
1800 (unless (re-search-forward "^$" (point-at-eol) t)
1805 ;;; subroutine for wl-draft-create-contents
1806 ;;; must be used in wl-draft-mode
1807 (defun wl-draft-check-new-line ()
1808 (if (not (= (preceding-char) ?\n))
1811 (defsubst wl-draft-trim-ccs (cc)
1817 (null (and wl-draft-delete-myself-from-bcc-fcc
1819 (mapcar 'wl-address-header-extract-address
1821 (wl-parse-addresses (std11-field-body "To"))
1822 (wl-parse-addresses (std11-field-body "Cc"))))
1823 (mapcar 'downcase wl-subscribed-mailing-list)))))
1827 (defsubst wl-draft-default-headers ()
1829 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1830 (wl-address-header-extract-address
1832 (cons 'User-Agent wl-generate-mailer-string-function)
1833 (cons 'Reply-To mail-default-reply-to)
1834 (cons 'Bcc (function
1837 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1838 (cons 'Fcc (function
1840 (wl-draft-trim-ccs wl-fcc))))
1841 (cons 'Organization wl-organization)
1842 (and wl-auto-insert-x-face
1843 (file-exists-p wl-x-face-file)
1844 'wl-draft-insert-x-face-field-here) ;; allow nil
1845 mail-default-headers
1846 ;; check \n at th end of line for `mail-default-headers'
1847 'wl-draft-check-new-line
1850 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1853 (goto-char delimline)
1854 (goto-char (point-min))
1855 (if (search-forward "\n\n" nil t)
1856 (delete-backward-char 1)
1857 (goto-char (point-max))))
1858 (wl-draft-check-new-line)
1859 (put-text-property (point)
1861 (insert mail-header-separator "\n")
1863 'category 'mail-header-separator)
1868 (defun wl-draft-elmo-nntp-send ()
1869 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1870 (elmo-nntp-default-user
1871 (or wl-nntp-posting-user elmo-nntp-default-user))
1872 (elmo-nntp-default-server
1873 (or wl-nntp-posting-server elmo-nntp-default-server))
1874 (elmo-nntp-default-port
1875 (or wl-nntp-posting-port elmo-nntp-default-port))
1876 (elmo-nntp-default-stream-type
1877 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1878 (elmo-nntp-default-function wl-nntp-posting-function)
1880 (if (setq condition (cdr (elmo-string-matched-assoc
1881 (std11-field-body "Newsgroups")
1882 wl-nntp-posting-config-alist)))
1883 (if (stringp condition)
1884 (setq elmo-nntp-default-server condition)
1885 (while (car condition)
1886 (set (intern (format "elmo-nntp-default-%s"
1887 (symbol-name (caar condition))))
1889 (setq condition (cdr condition)))))
1890 (unless elmo-nntp-default-function
1891 (error "wl-draft-nntp-send: posting-function is nil"))
1892 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1893 (wl-draft-set-sent-message 'news 'unplugged
1894 (cons elmo-nntp-default-server
1895 elmo-nntp-default-port))
1896 (funcall elmo-nntp-default-function
1897 elmo-nntp-default-server (current-buffer))
1898 (wl-draft-set-sent-message 'news 'sent)
1899 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1900 (std11-field-body "Newsgroups")
1901 (std11-field-body "Message-ID")))))
1903 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1904 "Generate clone of current buffer named NAME."
1905 (let ((editing-buffer (current-buffer)))
1907 (set-buffer (generate-new-buffer name))
1910 (wl-draft-editor-mode)
1911 (insert-buffer editing-buffer)
1913 (while local-variables
1914 (make-local-variable (car local-variables))
1915 (set (car local-variables)
1917 (set-buffer editing-buffer)
1918 (symbol-value (car local-variables))))
1919 (setq local-variables (cdr local-variables)))
1922 (defun wl-draft-remove-text-plain-tag ()
1923 "Remove text/plain tag of mime-edit."
1924 (when (string= (mime-make-text-tag "plain")
1925 (buffer-substring-no-properties (point-at-bol)(point-at-eol)))
1926 (delete-region (point-at-bol)(1+ (point-at-eol)))))
1928 (defun wl-draft-reedit (number)
1929 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1931 buffer file-name change-major-mode-hook body-top)
1932 (setq file-name (elmo-message-file-name draft-folder number))
1933 (unless (file-exists-p file-name)
1934 (error "File %s does not exist" file-name))
1935 (if (setq buffer (get-buffer
1936 (concat wl-draft-folder "/"
1937 (number-to-string number))))
1939 (if wl-draft-use-frame
1940 (switch-to-buffer-other-frame buffer)
1941 (switch-to-buffer buffer))
1942 (set-buffer buffer))
1943 (setq buffer (get-buffer-create (number-to-string number)))
1944 ;; switch-buffer according to draft buffer style.
1945 (if wl-draft-use-frame
1946 (switch-to-buffer-other-frame buffer)
1947 (case wl-draft-buffer-style
1949 (split-window-vertically)
1951 (switch-to-buffer buffer))
1953 (switch-to-buffer buffer))
1955 (delete-other-windows)
1956 (switch-to-buffer buffer))
1957 (t (if (functionp wl-draft-buffer-style)
1958 (funcall wl-draft-buffer-style buffer)
1959 (error "Invalid value for wl-draft-buffer-style")))))
1961 (setq wl-draft-parent-folder "")
1962 (insert-file-contents-as-binary file-name)
1963 (elmo-delete-cr-buffer)
1964 (let((mime-edit-again-ignored-field-regexp
1965 "^\\(Content-.*\\|Mime-Version\\):"))
1966 (wl-draft-decode-message-in-buffer))
1967 (setq body-top (wl-draft-insert-mail-header-separator))
1968 (if (not (string-match (regexp-quote wl-draft-folder)
1970 (rename-buffer (concat wl-draft-folder "/" (buffer-name))))
1973 (setq buffer-file-name file-name)
1974 (make-local-variable 'truncate-partial-width-windows)
1975 (setq truncate-partial-width-windows nil)
1976 (setq truncate-lines wl-draft-truncate-lines)
1977 (setq wl-sent-message-via nil)
1978 (setq wl-sent-message-queued nil)
1979 (setq wl-draft-buffer-file-name file-name)
1980 (wl-draft-config-info-operation number 'load)
1981 (goto-char (point-min))
1982 (wl-draft-overload-functions)
1983 (wl-draft-editor-mode)
1984 (when wl-draft-write-file-function
1985 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
1986 (wl-highlight-headers 'for-draft)
1987 (goto-char body-top)
1988 (run-hooks 'wl-draft-reedit-hook)
1989 (goto-char (point-max))
1992 (defmacro wl-draft-body-goto-top ()
1994 (goto-char (point-min))
1995 (if (re-search-forward mail-header-separator nil t)
1997 (goto-char (point-max))))))
1999 (defmacro wl-draft-body-goto-bottom ()
2000 (` (goto-char (point-max))))
2002 (defmacro wl-draft-config-body-goto-header ()
2004 (goto-char (point-min))
2005 (if (re-search-forward mail-header-separator nil t)
2007 (goto-char (point-max))))))
2009 (defsubst wl-draft-config-sub-eval-insert (content &optional newline)
2010 (let (content-value)
2012 (stringp (setq content-value (eval content))))
2013 (insert content-value)
2014 (if newline (insert "\n")))))
2016 (defun wl-draft-config-sub-body (content)
2017 (wl-draft-body-goto-top)
2018 (delete-region (point) (point-max))
2019 (wl-draft-config-sub-eval-insert content))
2021 (defun wl-draft-config-sub-top (content)
2022 (wl-draft-body-goto-top)
2023 (wl-draft-config-sub-eval-insert content))
2025 (defun wl-draft-config-sub-bottom (content)
2026 (wl-draft-body-goto-bottom)
2027 (wl-draft-config-sub-eval-insert content))
2029 (defun wl-draft-config-sub-header (content)
2030 (wl-draft-config-body-goto-header)
2031 (wl-draft-config-sub-eval-insert content 'newline))
2033 (defun wl-draft-config-sub-header-top (content)
2034 (goto-char (point-min))
2035 (wl-draft-config-sub-eval-insert content 'newline))
2037 (defun wl-draft-config-sub-part-top (content)
2038 (goto-char (mime-edit-content-beginning))
2039 (wl-draft-config-sub-eval-insert content 'newline))
2041 (defun wl-draft-config-sub-part-bottom (content)
2042 (goto-char (mime-edit-content-end))
2043 (wl-draft-config-sub-eval-insert content 'newline))
2045 (defsubst wl-draft-config-sub-file (content)
2046 (let ((coding-system-for-read wl-cs-autoconv)
2047 (file (expand-file-name (eval content))))
2048 (if (file-exists-p file)
2049 (insert-file-contents file)
2050 (error "%s: no exists file" file))))
2052 (defun wl-draft-config-sub-body-file (content)
2053 (wl-draft-body-goto-top)
2054 (delete-region (point) (point-max))
2055 (wl-draft-config-sub-file content))
2057 (defun wl-draft-config-sub-top-file (content)
2058 (wl-draft-body-goto-top)
2059 (wl-draft-config-sub-file content))
2061 (defun wl-draft-config-sub-bottom-file (content)
2062 (wl-draft-body-goto-bottom)
2063 (wl-draft-config-sub-file content))
2065 (defun wl-draft-config-sub-header-file (content)
2066 (wl-draft-config-body-goto-header)
2067 (wl-draft-config-sub-file content))
2069 (defun wl-draft-config-sub-template (content)
2070 (setq wl-draft-config-variables
2071 (wl-template-insert (eval content))))
2073 (defun wl-draft-config-sub-x-face (content)
2074 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
2075 (fboundp 'x-face-insert)) ; x-face.el is installed.
2076 (x-face-insert content)
2077 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
2079 (defsubst wl-draft-config-sub-func (field content)
2081 (if (setq func (assq field wl-draft-config-sub-func-alist))
2082 (let (wl-draft-config-variables)
2083 (funcall (cdr func) content)
2084 ;; for wl-draft-config-sub-template
2085 (cons t wl-draft-config-variables)))))
2087 (defsubst wl-draft-config-exec-sub (clist)
2088 (let (config local-variables)
2090 (setq config (car clist))
2095 (let ((field (car config))
2096 (content (cdr config))
2100 (wl-draft-replace-field field (eval content) t))
2101 ((setq ret-val (wl-draft-config-sub-func field content))
2102 (if (cdr ret-val) ;; for wl-draft-config-sub-template
2103 (wl-append local-variables (cdr ret-val))))
2104 ((boundp field) ;; variable
2105 (make-local-variable field)
2106 (set field (eval content))
2107 (wl-append local-variables (list field)))
2109 (error "%s: not variable" field)))))
2111 (error "%s: not supported type" config)))
2112 (setq clist (cdr clist)))
2115 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
2116 "Change headers in draft preparation time."
2118 (unless wl-draft-reedit
2121 (and (boundp 'wl-draft-prepared-config-alist)
2122 wl-draft-prepared-config-alist) ;; For compatible.
2123 wl-draft-config-alist)))
2125 (wl-draft-config-exec config-alist reply-buf)))))
2127 (defun wl-draft-config-exec (&optional config-alist reply-buf)
2128 "Change headers according to the value of `wl-draft-config-alist'.
2129 Automatically applied in draft sending time."
2131 (let ((case-fold-search t)
2132 (alist (or config-alist wl-draft-config-alist))
2133 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2134 wl-draft-reply-buffer)))
2135 (local-variables wl-draft-config-variables)
2137 (when (and (or (interactive-p)
2138 wl-draft-config-exec-flag)
2143 (setq key (caar alist)
2150 (set-buffer reply-buf)
2152 (std11-narrow-to-header)
2153 (goto-char (point-min))
2154 (re-search-forward (car clist) nil t))))
2155 (wl-draft-config-exec-sub (cdr clist))
2158 (when (save-restriction
2159 (std11-narrow-to-header mail-header-separator)
2160 (goto-char (point-min))
2161 (re-search-forward key nil t))
2162 (wl-append local-variables
2163 (wl-draft-config-exec-sub clist))
2166 (wl-append local-variables
2167 (wl-draft-config-exec-sub clist))
2169 (if (and found wl-draft-config-matchone)
2171 (setq alist (cdr alist))))
2173 (setq wl-draft-config-exec-flag nil))
2174 (run-hooks 'wl-draft-config-exec-hook)
2175 (put-text-property (point-min)(point-max) 'face nil)
2176 (wl-highlight-message (point-min)(point-max) t)
2177 (setq wl-draft-config-variables
2178 (elmo-uniq-list local-variables))))))
2180 (defun wl-draft-replace-field (field content &optional add)
2183 (let ((case-fold-search t)
2184 (inhibit-read-only t) ;; added by teranisi.
2186 (std11-narrow-to-header mail-header-separator)
2187 (goto-char (point-min))
2188 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2193 (re-search-forward "^[^ \t]" nil 'move)
2195 (skip-chars-backward "\n")
2196 (delete-region beg (point))
2197 (insert " " content))
2202 (re-search-forward "^[^ \t]" nil 'move)
2204 (delete-region beg (point)))
2205 (when (and add content)
2207 (goto-char (point-max))
2208 (insert (concat field ": " content "\n"))))))))
2210 (defun wl-draft-config-info-operation (msg operation)
2211 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-folder-get-elmo-folder
2215 (format "%s-%d" wl-draft-config-save-filename msg)
2217 element alist variable)
2219 ((eq operation 'save)
2220 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2221 (while (setq variable (pop variables))
2222 (when (boundp variable)
2224 (list (cons variable (eval variable))))))
2225 (elmo-object-save filename alist)))
2226 ((eq operation 'load)
2227 (setq alist (elmo-object-load filename))
2228 (while (setq element (pop alist))
2229 (set (make-local-variable (car element)) (cdr element))
2230 (wl-append wl-draft-config-variables (list (car element)))))
2231 ((eq operation 'delete)
2232 (if (file-exists-p filename)
2233 (delete-file filename))))))
2235 (defun wl-draft-queue-info-operation (msg operation
2236 &optional add-sent-message-via)
2237 (let* ((msgdb-dir (elmo-folder-msgdb-path
2238 (wl-folder-get-elmo-folder wl-queue-folder)))
2241 (format "%s-%d" wl-draft-queue-save-filename msg)
2243 element alist variable)
2245 ((eq operation 'save)
2246 (let ((variables (elmo-uniq-list
2247 (append wl-draft-queue-save-variables
2248 wl-draft-config-variables
2249 (list 'wl-draft-fcc-list)))))
2250 (if add-sent-message-via
2252 (push 'wl-sent-message-queued variables)
2253 (push 'wl-sent-message-via variables)))
2254 (while (setq variable (pop variables))
2255 (when (boundp variable)
2257 (list (cons variable (eval variable))))))
2258 (elmo-object-save filename alist)))
2259 ((eq operation 'load)
2260 (setq alist (elmo-object-load filename))
2261 (while (setq element (pop alist))
2262 (set (make-local-variable (car element)) (cdr element))))
2263 ((eq operation 'get-sent-via)
2264 (setq alist (elmo-object-load filename))
2265 (cdr (assq 'wl-sent-message-via alist)))
2266 ((eq operation 'delete)
2267 (if (file-exists-p filename)
2268 (delete-file filename))))))
2270 (defun wl-draft-queue-append (wl-sent-message-via)
2271 (if wl-draft-verbose-send
2272 (message "Queuing..."))
2273 (let ((send-buffer (current-buffer))
2274 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2275 (message-id (std11-field-body "Message-ID")))
2276 (if (elmo-folder-append-buffer folder)
2278 (wl-draft-queue-info-operation
2279 (car (elmo-folder-status folder))
2280 'save wl-sent-message-via)
2281 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2282 (when wl-draft-verbose-send
2283 (setq wl-draft-verbose-msg "Queuing...")
2284 (message "Queuing...done")))
2285 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2286 (error "Queuing failed"))))
2288 (defun wl-draft-queue-flush ()
2289 "Flush draft queue."
2291 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2293 (elmo-folder-open-internal queue-folder)
2294 (elmo-folder-list-messages queue-folder)))
2297 (wl-draft-queue-flushing t)
2298 msgs failure len buffer msgid sent-via)
2299 ;; get plugged send message
2301 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2304 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2305 (or (not (nth 2 (car sent-via)))
2307 (car (nth 2 (car sent-via)))
2308 (cdr (nth 2 (car sent-via))))))
2309 (wl-append msgs (list (car msgs2)))
2311 (setq sent-via (cdr sent-via))))
2312 (setq msgs2 (cdr msgs2)))
2313 (when (> (setq len (length msgs)) 0)
2314 (if (elmo-y-or-n-p (format
2315 "%d message(s) are in the sending queue. Send now? "
2317 (not elmo-dop-flush-confirm) t)
2320 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2323 ;; reset buffer local variables
2324 (kill-all-local-variables)
2328 (setq wl-sent-message-via nil)
2329 (wl-draft-queue-info-operation (car msgs) 'load)
2330 (elmo-message-fetch queue-folder
2332 (elmo-make-fetch-strategy 'entire)
2333 nil (current-buffer))
2335 (setq failure (funcall
2336 wl-draft-queue-flush-send-function
2337 (format "Sending (%d/%d)..." i len)))
2338 ;;; (wl-draft-raw-send nil nil
2339 ;;; (format "Sending (%d/%d)..." i len))
2341 (elmo-display-error err t)
2345 (if (eq wl-sent-message-modified 'requeue)
2347 (elmo-folder-delete-messages
2348 queue-folder (cons (car msgs) nil))
2349 (wl-draft-queue-info-operation (car msgs) 'delete))
2351 (elmo-folder-delete-messages
2352 queue-folder (cons (car msgs) nil))
2353 (wl-draft-queue-info-operation (car msgs) 'delete)
2354 (setq performed (+ 1 performed))))
2355 (setq msgs (cdr msgs)))
2356 (kill-buffer buffer)
2357 (message "%d message(s) are sent." performed)))
2358 (message "%d message(s) are remained to be sent." len))
2359 (elmo-folder-close queue-folder)
2362 (defun wl-jump-to-draft-buffer (&optional arg)
2363 "Jump to the draft if exists."
2366 (wl-jump-to-draft-folder)
2367 (let ((draft-bufs (wl-collect-draft))
2371 (message "No draft buffer exist."))
2374 (sort (mapcar 'buffer-name draft-bufs)
2375 (function (lambda (a b)
2376 (not (string< a b))))))
2377 (if (setq buf (cdr (member (buffer-name)
2379 (setq buf (car buf))
2380 (setq buf (car draft-bufs)))
2381 (switch-to-buffer buf))))))
2383 (defun wl-jump-to-draft-folder ()
2384 (let ((msgs (reverse (elmo-folder-list-messages (wl-folder-get-elmo-folder
2386 (mybuf (buffer-name))
2389 (message "No draft message exist.")
2390 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2391 (setq msg (cadr (memq
2392 (string-to-int (substring mybuf (match-end 0)))
2394 (or msg (setq msg (car msgs)))
2395 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2396 (switch-to-buffer buf)
2397 (wl-draft-reedit msg)))))
2399 (defun wl-draft-highlight-and-recenter (&optional n)
2401 (when wl-highlight-body-too
2402 (let ((modified (buffer-modified-p)))
2405 (put-text-property (point-min) (point-max) 'face nil)
2406 (wl-highlight-message (point-min) (point-max) t))
2407 (set-buffer-modified-p modified))))
2410 ;;;; user-agent support by Sen Nagata
2412 ;; this appears to be necessarily global...
2413 (defvar wl-user-agent-compose-p nil)
2414 (defvar wl-user-agent-headers-and-body-alist nil)
2416 ;; this should be a generic function for mail-mode -- i wish there was
2417 ;; something like it in sendmail.el
2418 (defun wl-user-agent-insert-header (header-name header-value)
2419 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2420 ;; it seems like overriding existing headers is acceptable -- should
2421 ;; we provide an option?
2423 ;; plan was: unfold header (might be folded), remove existing value, insert
2425 ;; wl doesn't seem to fold header lines yet anyway :-)
2427 (let ((kill-whole-line t)
2429 (mail-position-on-field (capitalize header-name))
2430 (setq end-of-line (point))
2432 (re-search-forward ":" end-of-line)
2433 (insert (concat " " header-value "\n"))
2436 ;; this should be a generic function for mail-mode -- i wish there was
2437 ;; something like it in sendmail.el
2439 ;; ** haven't dealt w/ case where the body is already set **
2440 (defun wl-user-agent-insert-body (body-text)
2441 "Insert a body of text, BODY-TEXT, into a message."
2442 ;; code defensively... :-P
2443 (goto-char (point-min))
2444 (search-forward mail-header-separator)
2449 (defun wl-user-agent-compose (&optional to subject other-headers continue
2450 switch-function yank-action
2452 "Support the `compose-mail' interface for wl.
2453 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2454 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2455 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2457 (unless (featurep 'wl)
2460 (setq switch-function 'keep))
2461 ;; protect these -- to and subject get bound at some point, so it looks
2462 ;; to be necessary to protect the values used w/in
2463 (let ((wl-user-agent-headers-and-body-alist other-headers)
2464 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2465 (wl-draft-buffer-style switch-function))
2467 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2470 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2473 (setq wl-user-agent-headers-and-body-alist
2474 (cons (cons "to" to)
2475 wl-user-agent-headers-and-body-alist))))
2477 (if (wl-string-match-assoc "subject"
2478 wl-user-agent-headers-and-body-alist
2481 (wl-string-match-assoc "subject"
2482 wl-user-agent-headers-and-body-alist
2485 (setq wl-user-agent-headers-and-body-alist
2486 (cons (cons "subject" subject)
2487 wl-user-agent-headers-and-body-alist))))
2488 ;; i think this is what we want to use...
2491 ;; tell the hook-function to do its stuff
2492 (setq wl-user-agent-compose-p t)
2493 ;; because to get the hooks working, wl-draft has to think it has
2494 ;; been called interactively
2495 (call-interactively 'wl-draft))
2496 (setq wl-user-agent-compose-p nil))))
2498 (defun wl-user-agent-compose-internal ()
2499 "Manipulate headers and/or a body of a draft message."
2500 ;; being called from wl-user-agent-compose?
2501 (if wl-user-agent-compose-p
2504 (let ((headers wl-user-agent-headers-and-body-alist)
2505 (case-fold-search t))
2508 (if (not (string-match "^body$" (car (car headers))))
2509 (wl-user-agent-insert-header
2510 (car (car headers)) (cdr (car headers)))
2512 (setq headers (cdr headers))))
2513 ;; highlight headers (from wl-draft in wl-draft.el)
2514 (wl-highlight-headers 'for-draft)
2516 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2518 (wl-user-agent-insert-body
2519 (cdr (wl-string-match-assoc
2521 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2525 (product-provide (provide 'wl-draft) (require 'wl-version))
2527 ;;; wl-draft.el ends here