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.
164 (let* ((login (or user-mail-address (user-login-name)))
165 (fullname (user-full-name)))
166 (cond ((eq mail-from-style 'angles)
167 (insert "From: " fullname)
168 (let ((fullname-start (+ (point-min) (length "From: ")))
169 (fullname-end (point-marker)))
170 (goto-char fullname-start)
171 ;; Look for a character that cannot appear unquoted
172 ;; according to RFC 822.
173 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
176 ;; Quote fullname, escaping specials.
177 (goto-char fullname-start)
179 (while (re-search-forward "[\"\\]"
181 (replace-match "\\\\\\&" t))
183 (insert " <" login ">\n"))
184 ((eq mail-from-style 'parens)
185 (insert "From: " login " (")
186 (let ((fullname-start (point)))
188 (let ((fullname-end (point-marker)))
189 (goto-char fullname-start)
190 ;; RFC 822 says \ and nonmatching parentheses
191 ;; must be escaped in comments.
192 ;; Escape every instance of ()\ ...
193 (while (re-search-forward "[()\\]" fullname-end 1)
194 (replace-match "\\\\\\&" t))
195 ;; ... then undo escaping of matching parentheses,
196 ;; including matching nested parentheses.
197 (goto-char fullname-start)
198 (while (re-search-forward
199 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
201 (replace-match "\\1(\\3)" t)
202 (goto-char fullname-start))))
204 ((not mail-from-style)
205 (insert "From: " login "\n")))))
207 (defun wl-draft-insert-x-face-field ()
208 "Insert X-Face header."
210 (if (not (file-exists-p wl-x-face-file))
211 (error "File %s does not exist" wl-x-face-file)
212 (goto-char (point-min))
213 (search-forward mail-header-separator nil t)
215 (wl-draft-insert-x-face-field-here)
216 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
218 (defun wl-draft-insert-x-face-field-here ()
219 "Insert X-Face field at point."
220 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
221 (when (string-match "^\\(X-Face:\\)?[ \t\n]*" x-face-string)
222 (setq x-face-string (substring x-face-string (match-end 0))))
223 (insert "X-Face: " x-face-string))
224 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
226 ;; Insert X-Face-Version: field
227 (when (and (fboundp 'x-face-insert-version-header)
228 (boundp 'x-face-add-x-face-version-header)
229 x-face-add-x-face-version-header)
230 (x-face-insert-version-header)))
232 (defun wl-draft-setup ()
233 (let ((field wl-draft-fields)
237 (list (cons (concat (car field) " ")
238 (concat (car field) " ")))))
239 (setq field (cdr field)))
241 (cons (cons (concat wl-draft-mime-bcc-field-name ": ")
242 (concat wl-draft-mime-bcc-field-name ": "))
244 (setq wl-draft-field-completion-list cl)
245 (setq wl-address-complete-header-regexp
247 (append wl-address-complete-header-list
248 (list (concat wl-draft-mime-bcc-field-name ":")))))))
250 (defun wl-draft-make-mail-followup-to (recipients)
251 (if (elmo-list-member
252 (or wl-user-mail-address-list
253 (list (wl-address-header-extract-address wl-from)))
255 (let ((rlist (elmo-list-delete
256 (or wl-user-mail-address-list
257 (list (wl-address-header-extract-address wl-from)))
261 (lambda (item) (string= (downcase elem)
264 (if (elmo-list-member rlist (mapcar 'downcase
265 wl-subscribed-mailing-list))
267 (append rlist (list (wl-address-header-extract-address
271 (defun wl-draft-delete-myself-from-cc (to cc)
272 (let ((myself (or wl-user-mail-address-list
273 (list (wl-address-header-extract-address wl-from)))))
274 (cond (wl-draft-always-delete-myself ; always-delete option
275 (elmo-list-delete myself cc
278 (lambda (item) (string= (downcase elem)
281 ((elmo-list-member (append to cc) ; subscribed mailing-list
282 (mapcar 'downcase wl-subscribed-mailing-list))
283 (elmo-list-delete myself cc
286 (lambda (item) (string= (downcase elem)
291 (defun wl-draft-forward (original-subject summary-buf)
292 (let (references parent-folder)
293 (with-current-buffer summary-buf
294 (setq parent-folder (wl-summary-buffer-folder-name)))
295 (with-current-buffer (wl-message-get-original-buffer)
296 (setq references (nconc
297 (std11-field-bodies '("References" "In-Reply-To"))
298 (list (std11-field-body "Message-Id"))))
299 (setq references (delq nil references)
300 references (mapconcat 'identity references " ")
301 references (wl-draft-parse-msg-id-list-string references)
302 references (wl-delete-duplicates references)
303 references (when references
304 (mapconcat 'identity references "\n\t"))))
305 (and wl-draft-use-frame
306 (get-buffer-window summary-buf)
307 (select-window (get-buffer-window summary-buf)))
308 (wl-draft (list (cons 'To "")
310 (concat wl-forward-subject-prefix original-subject))
311 (cons 'References references))
312 nil nil nil nil parent-folder))
313 (goto-char (point-max))
314 (wl-draft-insert-message)
315 (mail-position-on-field "To"))
317 (defun wl-draft-strip-subject-re (subject)
318 "Remove \"Re:\" from subject lines. Shamelessly copied from Gnus."
319 (if (string-match wl-subject-prefix-regexp subject)
320 (substring subject (match-end 0))
323 (defun wl-draft-reply-list-symbol (with-arg)
324 "Return symbol `wl-draft-reply-*-argument-list' match condition.
325 Check WITH-ARG and From: field."
326 (if (wl-address-user-mail-address-p (or (elmo-field-body "From") ""))
328 'wl-draft-reply-myself-with-argument-list
329 'wl-draft-reply-myself-without-argument-list)
331 'wl-draft-reply-with-argument-list
332 'wl-draft-reply-without-argument-list)))
334 (defun wl-draft-reply (buf with-arg summary-buf &optional number)
335 "Reply to BUF buffer message.
336 Reply to author if WITH-ARG is non-nil."
339 to mail-followup-to cc subject in-reply-to references newsgroups
340 to-alist cc-alist decoder parent-folder)
341 (when (buffer-live-p summary-buf)
342 (with-current-buffer summary-buf
343 (setq parent-folder (wl-summary-buffer-folder-name))))
344 (set-buffer (or buf mime-mother-buffer))
345 (setq r-list (symbol-value (wl-draft-reply-list-symbol with-arg)))
348 (when (let ((condition (car (car r-list))))
349 (cond ((stringp condition)
350 (std11-field-body condition))
354 (if (not (std11-field-body (car condition)))
356 (setq condition (cdr condition)))
359 (funcall condition))))
360 (let ((r-to-list (nth 0 (cdr (car r-list))))
361 (r-cc-list (nth 1 (cdr (car r-list))))
362 (r-ng-list (nth 2 (cdr (car r-list)))))
363 (when (and (member "Followup-To" r-ng-list)
364 (string= (std11-field-body "Followup-To") "poster"))
365 (setq r-to-list (cons "From" r-to-list))
366 (setq r-ng-list (delete "Followup-To"
367 (copy-sequence r-ng-list))))
368 (if (and r-to-list (symbolp r-to-list))
369 (setq to (wl-concat-list (funcall r-to-list) ","))
370 (setq to (wl-concat-list (cons to
371 (elmo-multiple-fields-body-list
374 (if (and r-cc-list (symbolp r-cc-list))
375 (setq cc (wl-concat-list (funcall r-to-list) ","))
376 (setq cc (wl-concat-list (cons cc
377 (elmo-multiple-fields-body-list
380 (if (and r-ng-list (symbolp r-ng-list))
381 (setq newsgroups (wl-concat-list (funcall r-ng-list) ","))
382 (setq newsgroups (wl-concat-list (cons newsgroups
387 (setq r-list (cdr r-list)))
388 (error "No match field: check your `%s'"
389 (symbol-name (wl-draft-reply-list-symbol with-arg))))
390 (setq subject (std11-field-body "Subject"))
391 (setq to (wl-parse-addresses to)
392 cc (wl-parse-addresses cc))
393 (with-temp-buffer ; to keep raw buffer unibyte.
394 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
395 (setq decoder (mime-find-field-decoder 'Subject 'plain))
396 (setq subject (if (and subject decoder)
397 (funcall decoder subject) subject))
401 (setq decoder (mime-find-field-decoder 'To 'plain))
402 (cons (nth 1 (std11-extract-address-components addr))
403 (if decoder (funcall decoder addr) addr)))
408 (setq decoder (mime-find-field-decoder 'Cc 'plain))
409 (cons (nth 1 (std11-extract-address-components addr))
410 (if decoder (funcall decoder addr) addr)))
412 (and wl-reply-subject-prefix
413 (setq subject (concat wl-reply-subject-prefix
414 (wl-draft-strip-subject-re
416 (setq in-reply-to (std11-field-body "Message-Id"))
417 (setq references (nconc
418 (std11-field-bodies '("References" "In-Reply-To"))
420 (setq to (delq nil (mapcar 'car to-alist)))
421 (setq cc (delq nil (mapcar 'car cc-alist)))
422 ;; if subscribed mailing list is contained in cc or to
423 ;; and myself is contained in cc,
424 ;; delete myself from cc.
425 (setq cc (wl-draft-delete-myself-from-cc to cc))
426 (when wl-insert-mail-followup-to
427 (setq mail-followup-to
428 (wl-draft-make-mail-followup-to (append to cc)))
429 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
430 (with-temp-buffer ; to keep raw buffer unibyte.
431 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
432 (setq newsgroups (wl-parse newsgroups
433 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
434 newsgroups (wl-delete-duplicates newsgroups)
439 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
440 (if decoder (funcall decoder grp) grp))
442 (setq to (wl-delete-duplicates to nil t))
443 (setq cc (wl-delete-duplicates
444 (append (wl-delete-duplicates cc nil t)
445 to (copy-sequence to))
447 (and to (setq to (mapconcat
449 (if wl-draft-reply-use-address-with-full-name
450 (or (cdr (assoc addr to-alist)) addr)
453 (and cc (setq cc (mapconcat
455 (if wl-draft-reply-use-address-with-full-name
456 (or (cdr (assoc addr cc-alist)) addr)
459 (and mail-followup-to
460 (setq mail-followup-to
463 (if wl-draft-reply-use-address-with-full-name
464 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
466 mail-followup-to ",\n\t")))
467 (and (null to) (setq to cc cc nil))
468 (setq references (delq nil references)
469 references (mapconcat 'identity references " ")
470 references (wl-draft-parse-msg-id-list-string references)
471 references (wl-delete-duplicates references)
472 references (if references
473 (mapconcat 'identity references "\n\t")))
474 (and wl-draft-use-frame
475 (get-buffer-window summary-buf)
476 (select-window (get-buffer-window summary-buf)))
477 (wl-draft (list (cons 'To to)
479 (cons 'Newsgroups newsgroups)
480 (cons 'Subject subject)
481 (cons 'In-Reply-To in-reply-to)
482 (cons 'References references)
483 (cons 'Mail-Followup-To mail-followup-to))
484 nil nil nil nil parent-folder)
485 (setq wl-draft-parent-number number)
486 (setq wl-draft-reply-buffer buf)
487 (setq wl-draft-config-variables
488 (append wl-draft-reply-saved-variables
489 wl-draft-config-variables)))
490 (run-hooks 'wl-reply-hook))
492 (defun wl-draft-add-references ()
493 (wl-draft-add-in-reply-to "References"))
495 (defun wl-draft-add-in-reply-to (&optional alt-field)
496 (let* ((mes-id (save-excursion
497 (set-buffer mail-reply-buffer)
498 (std11-field-body "message-id")))
499 (field (or alt-field "In-Reply-To"))
500 (ref (std11-field-body field))
501 (ref-list nil) (st nil))
502 (when (and mes-id ref)
503 (while (string-match "<[^>]+>" ref st)
505 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
508 (member mes-id ref-list))
512 (when (mail-position-on-field field)
514 (while (looking-at "^[ \t]")
516 (setq mes-id (concat "\t" mes-id "\n")))
520 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
521 &optional ignored-fields)
524 (narrow-to-region (point)(point))
526 (with-current-buffer mail-reply-buffer
528 (decode-mime-charset-region (point-min) (point-max)
530 (buffer-substring-no-properties
531 (point-min) (point-max))))
533 (goto-char (point-min))
534 (wl-draft-delete-fields ignored-fields))
535 (goto-char (point-max))
536 (push-mark (point) nil t)
537 (goto-char (point-min)))
539 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
540 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
541 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
542 (run-hooks 'wl-draft-cited-hook)
543 (when (if wl-draft-add-references
544 (wl-draft-add-references)
545 (if wl-draft-add-in-reply-to
546 (wl-draft-add-in-reply-to)))
547 (wl-highlight-headers 'for-draft)) ; highlight when added References:
548 (when wl-highlight-body-too
549 (wl-highlight-body-region beg (point-max)))))
551 (defun wl-message-news-p ()
552 "If exist valid Newsgroups field, return non-nil."
553 (std11-field-body "Newsgroups"))
555 (defun wl-message-field-exists-p (field)
556 "If FIELD exist and FIELD value is not empty, return non-nil."
557 (let ((value (std11-field-body field)))
559 (not (string= value "")))))
561 (defun wl-message-mail-p ()
562 "If exist To, Cc or Bcc field, return non-nil."
563 (or (wl-message-field-exists-p "To")
564 (wl-message-field-exists-p "Resent-to")
565 (wl-message-field-exists-p "Cc")
566 (wl-message-field-exists-p "Bcc")
567 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
568 ;;; This may be needed..
569 ;;; (wl-message-field-exists-p "Fcc")
572 (defun wl-draft-edit-string (string)
573 (let ((cur-buf (current-buffer))
574 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
575 to subject in-reply-to cc references newsgroups mail-followup-to
576 content-type content-transfer-encoding from
581 (setq to (std11-field-body "To"))
584 (decode-mime-charset-string
587 (setq subject (std11-field-body "Subject"))
588 (setq subject (and subject
590 (decode-mime-charset-string
593 (setq from (std11-field-body "From")
596 (decode-mime-charset-string
599 (setq in-reply-to (std11-field-body "In-Reply-To"))
600 (setq cc (std11-field-body "Cc"))
603 (decode-mime-charset-string
606 (setq references (std11-field-body "References"))
607 (setq newsgroups (std11-field-body "Newsgroups"))
608 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
609 (setq content-type (std11-field-body "Content-Type"))
610 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
611 (goto-char (point-min))
612 (or (re-search-forward "\n\n" nil t)
613 (search-forward (concat mail-header-separator "\n") nil t))
619 (nth 1 (std11-extract-address-components from))
620 wl-user-mail-address-list)
624 (cons 'Subject subject)
625 (cons 'Newsgroups newsgroups)
626 (cons 'Mail-Followup-To mail-followup-to)
627 (cons 'In-Reply-To in-reply-to)
628 (cons 'References references))
629 content-type content-transfer-encoding
630 (buffer-substring (point) (point-max))
632 (and to (mail-position-on-field "To"))
633 (delete-other-windows)
634 (kill-buffer tmp-buf)))
635 (run-hooks 'wl-draft-reedit-hook))
637 (defun wl-draft-insert-current-message (dummy)
639 (let (original-buffer
641 mail-citation-hook mail-yank-hooks
642 wl-draft-add-references wl-draft-add-in-reply-to
643 wl-draft-cite-function)
644 (with-current-buffer wl-draft-buffer-cur-summary-buffer
645 (with-current-buffer wl-message-buffer
646 (setq original-buffer (wl-message-get-original-buffer))
648 (with-current-buffer original-buffer
650 (error "No current message"))))
651 (setq mail-reply-buffer original-buffer)
652 (wl-draft-yank-from-mail-reply-buffer
654 wl-ignored-forwarded-headers)))
656 (defun wl-draft-insert-get-message (dummy)
657 (let ((fld (completing-read
659 (if (memq 'read-folder wl-use-folder-petname)
660 (wl-folder-get-entity-with-petname)
661 wl-folder-entity-hashtb)
662 nil nil wl-default-spec
663 'wl-read-folder-hist))
664 (number (call-interactively
665 (function (lambda (num)
666 (interactive "nNumber: ")
668 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
669 mail-citation-hook mail-yank-hooks
670 wl-draft-cite-function)
673 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
676 (elmo-make-fetch-strategy 'entire)
677 nil mail-reply-buffer)
678 (wl-draft-yank-from-mail-reply-buffer nil))
679 (kill-buffer mail-reply-buffer))))
682 ;; default body citation func
684 (defun wl-default-draft-cite ()
685 (let ((mail-yank-ignored-headers "[^:]+:")
686 (mail-yank-prefix "> ")
687 date from cite-title)
689 (if (< (mark t) (point))
690 (exchange-point-and-mark))
691 (narrow-to-region (point)(point-max))
692 (setq date (std11-field-body "date")
693 from (std11-field-body "from")))
695 (insert (format "At %s,\n%s wrote:\n"
696 (or date "some time ago")
697 (if wl-default-draft-cite-decorate-author
698 (funcall wl-summary-from-function
701 (mail-indent-citation)))
703 (defvar wl-draft-buffer nil "Draft buffer to yank content")
704 (defun wl-draft-yank-to-draft-buffer (buffer)
705 "Yank BUFFER content to `wl-draft-buffer'."
706 (set-buffer wl-draft-buffer)
707 (let ((mail-reply-buffer buffer))
708 (wl-draft-yank-from-mail-reply-buffer nil)
709 (kill-buffer buffer)))
711 (defun wl-draft-yank-original (&optional arg)
712 "Yank original message."
715 (let (buf mail-reply-buffer)
719 (setq buf (current-buffer)))
720 (setq mail-reply-buffer buf)
721 (wl-draft-yank-from-mail-reply-buffer nil))
722 (wl-draft-yank-current-message-entity)))
724 (defun wl-draft-hide (editing-buffer)
725 "Hide the editing draft buffer if possible."
726 (when (and editing-buffer
727 (buffer-live-p editing-buffer))
728 (set-buffer editing-buffer)
729 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
730 fld-buf sum-win fld-win)
731 (if (and wl-draft-use-frame
732 (> (length (visible-frame-list)) 1))
738 ;; stay folder window if required
739 (when wl-stay-folder-window
740 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
741 (if (setq fld-win (get-buffer-window fld-buf))
742 (select-window fld-win)
743 (if wl-draft-resume-folder-window ;; resume folder window
744 (switch-to-buffer fld-buf)))))
745 (if (buffer-live-p sum-buf)
746 (if (setq sum-win (get-buffer-window sum-buf t))
747 ;; if Summary is on the frame, select it.
748 (select-window sum-win)
749 ;; if summary is not on the frame, switch to it.
750 (if (and wl-stay-folder-window
751 (or wl-draft-resume-folder-window fld-win))
752 (wl-folder-select-buffer sum-buf)
753 (switch-to-buffer sum-buf))))))))
755 (defun wl-draft-delete (editing-buffer)
756 "kill the editing draft buffer and delete the file corresponds to it."
759 (set-buffer editing-buffer)
760 (if wl-draft-buffer-file-name
762 (if (file-exists-p wl-draft-buffer-file-name)
763 (delete-file wl-draft-buffer-file-name))
764 (let ((msg (and wl-draft-buffer-file-name
765 (string-match "[0-9]+$" wl-draft-buffer-file-name)
767 (match-string 0 wl-draft-buffer-file-name)))))
768 (wl-draft-config-info-operation msg 'delete))))
769 (set-buffer-modified-p nil) ; force kill
770 (kill-buffer editing-buffer))))
772 (defun wl-draft-kill (&optional force-kill)
773 "Kill current draft buffer and quit editing."
776 (when (and (or (eq major-mode 'wl-draft-mode)
777 (eq major-mode 'mail-mode))
779 (y-or-n-p "Kill Current Draft? ")))
780 (let ((cur-buf (current-buffer)))
781 (when (and wl-draft-parent-number
782 (not (string= wl-draft-parent-folder "")))
783 (let* ((number wl-draft-parent-number)
784 (folder-name wl-draft-parent-folder)
785 (folder (wl-folder-get-elmo-folder folder-name))
787 (if (and (setq buffer (wl-summary-get-buffer folder-name))
788 (with-current-buffer buffer
789 (string= (wl-summary-buffer-folder-name)
791 (with-current-buffer buffer
792 (elmo-folder-unmark-answered folder (list number))
793 (wl-summary-jump-to-msg number)
794 (wl-summary-update-mark number))
795 (elmo-folder-open folder 'load-msgdb)
796 (elmo-folder-unmark-answered folder (list number))
797 (elmo-folder-close folder))))
798 (wl-draft-hide cur-buf)
799 (wl-draft-delete cur-buf)))
802 (defun wl-draft-fcc ()
803 "Add a new Fcc field, with file name completion."
805 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
806 (mail-position-on-field "to"))
809 ;; Imported from message.el.
810 (defun wl-draft-elide-region (b e)
811 "Elide the text in the region.
812 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
816 (insert wl-draft-elide-ellipsis))
818 ;; Imported from message.el.
819 (defun wl-draft-beginning-of-line (&optional n)
820 "Move point to beginning of header value or to beginning of line."
822 (let ((zrs 'zmacs-region-stays))
823 (when (and (interactive-p) (boundp zrs))
825 (if (wl-draft-point-in-header-p)
826 (let* ((here (point))
827 (bol (progn (beginning-of-line n) (point)))
828 (eol (line-end-position))
829 (eoh (and (looking-at "[^ \t]")
830 (re-search-forward ": *" eol t))))
831 (if (and eoh (or (> here eoh) (= here bol)))
834 (beginning-of-line n)))
836 (defun wl-draft-point-in-header-p ()
837 "Return t if point is in the header."
840 (goto-char (point-min))
841 (not (re-search-forward
842 (concat "^" (regexp-quote mail-header-separator) "\n")
845 ;; function for wl-sent-message-via
847 (defmacro wl-draft-sent-message-p (type)
848 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
850 (defmacro wl-draft-set-sent-message (type result &optional server-port)
851 (` (let ((element (assq (, type) wl-sent-message-via)))
853 (unless (eq (nth 1 element) (, result))
854 (setcdr element (list (, result) (, server-port)))
855 (setq wl-sent-message-modified t))
856 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
857 (setq wl-sent-message-modified t)))))
859 (defun wl-draft-sent-message-results ()
860 (let ((results wl-sent-message-via)
861 unplugged-via sent-via)
863 (cond ((eq (nth 1 (car results)) 'unplugged)
864 (push (caar results) unplugged-via))
865 ((eq (nth 1 (car results)) 'sent)
866 (push (caar results) sent-via)))
867 (setq results (cdr results)))
868 (list unplugged-via sent-via)))
870 (defun wl-draft-write-sendlog (status proto server to id)
871 "Write send log file, if `wl-draft-sendlog' is non-nil."
872 (when wl-draft-sendlog
874 (let* ((filename (expand-file-name wl-draft-sendlog-filename
875 elmo-msgdb-directory))
876 (filesize (nth 7 (file-attributes filename)))
877 (server (if server (concat " server=" server) ""))
879 ((memq proto '(fcc queue))
880 (format " folder=\"%s\"" to))
882 (format " ng=<%s>" to))
887 (mapcar '(lambda(x) (format "<%s>" x)) to)
890 (id (if id (concat " id=" id) ""))
891 (time (format-time-string "%Y/%m/%d %T")))
892 (insert (format "%s proto=%s stat=%s%s%s%s\n"
893 time proto status server to id))
894 (if (and wl-draft-sendlog-max-size filesize
895 (> filesize wl-draft-sendlog-max-size))
896 (rename-file filename (concat filename ".old") t))
897 (if (file-writable-p filename)
898 (write-region-as-binary (point-min) (point-max)
900 (message "%s is not writable." filename))))))
902 (defun wl-draft-get-header-delimiter (&optional delete)
903 ;; If DELETE is non-nil, replace the header delimiter with a blank line
905 (goto-char (point-min))
906 (when (re-search-forward
907 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
911 (setq delimline (point-marker)))
914 (defun wl-draft-send-mail-with-qmail ()
915 "Pass the prepared message buffer to qmail-inject.
916 Refer to the documentation for the variable `send-mail-function'
917 to find out how to use this."
918 (if (and wl-draft-qmail-send-plugged
919 (not (elmo-plugged-p)))
920 (wl-draft-set-sent-message 'mail 'unplugged)
922 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
923 (let ((id (std11-field-body "Message-ID"))
924 (to (std11-field-body "To")))
928 'call-process-region 1 (point-max) wl-qmail-inject-program
930 wl-qmail-inject-args))
931 ;; qmail-inject doesn't say anything on it's stdout/stderr,
932 ;; we have to look at the retval instead
934 (wl-draft-set-sent-message 'mail 'sent)
935 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
936 (1 (error "`qmail-inject' reported permanent failure"))
937 (111 (error "`qmail-inject' reported transient failure"))
938 ;; should never happen
939 (t (error "`qmail-inject' reported unknown failure"))))))
941 (defun wl-draft-parse-msg-id-list-string (string)
942 "Get msg-id list from STRING."
944 (dolist (parsed-id (std11-parse-msg-ids-string string))
945 (when (eq (car parsed-id) 'msg-id)
946 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
948 (nreverse msg-id-list)))
950 (defun wl-draft-eword-encode-address-list (string &optional column)
951 "Encode header field STRING as list of address, and return the result.
952 Cause an error when STRING contains invalid address.
953 Optional argument COLUMN is start-position of the field."
954 (car (eword-encode-rword-list
955 (or column eword-encode-default-start-column)
956 (eword-encode-addresses-to-rword-list
957 (wl-draft-std11-parse-addresses (std11-lexical-analyze string))))))
959 (defun wl-draft-std11-parse-addresses (lal)
960 (let ((ret (std11-parse-address lal)))
961 (when (and (not (and (eq (length lal) 1)
962 (eq (car (car lal)) 'spaces)))
964 (error "Error while parsing address"))
966 (let ((dest (list (car ret))))
968 (while (and (setq ret (std11-parse-ascii-token lal))
969 (string-equal (cdr (assq 'specials (car ret))) ",")
970 (setq ret (std11-parse-address (cdr ret)))
972 (setq dest (cons (car ret) dest))
973 (setq lal (cdr ret)))
974 (while (eq 'spaces (car (car lal)))
975 (setq lal (cdr lal)))
976 (if lal (error "Error while parsing address"))
979 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
980 "Get mailbox list of FIELD from current buffer.
981 The buffer is expected to be narrowed to just the headers of the message.
982 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
983 from current buffer."
985 (let ((case-fold-search t)
986 (inhibit-read-only t)
988 mailbox-list beg seq has-group-list)
989 (goto-char (point-min))
990 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
993 (re-search-forward "^[^ \t]" nil 'move)
995 (skip-chars-backward "\n")
996 (setq seq (std11-lexical-analyze
997 (buffer-substring-no-properties beg (point))))
998 (setq addresses (wl-draft-std11-parse-addresses seq))
1000 (cond ((eq (car (car addresses)) 'group)
1001 (setq has-group-list t)
1005 'std11-address-string
1006 (nth 2 (car addresses))))))
1007 ((eq (car (car addresses)) 'mailbox)
1008 (setq address (nth 1 (car addresses)))
1012 (std11-addr-to-string
1013 (if (eq (car address) 'phrase-route-addr)
1015 (cdr address))))))))
1016 (setq addresses (cdr addresses)))
1017 (when (and remove-group-list has-group-list)
1018 (delete-region beg (point))
1019 (insert (wl-address-string-without-group-list-contents seq))))
1022 (defun wl-draft-deduce-address-list (buffer header-start header-end)
1023 "Get address list suitable for smtp RCPT TO:<address>.
1024 Group list content is removed if `wl-draft-remove-group-list-contents' is
1026 (let ((fields (if (and wl-draft-doing-mime-bcc
1027 wl-draft-disable-bcc-for-mime-bcc)
1029 '("to" "cc" "bcc")))
1030 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
1031 (case-fold-search t)
1035 (narrow-to-region header-start header-end)
1036 (goto-char (point-min))
1038 (if (re-search-forward "^resent-to[\t ]*:" nil t)
1039 (setq fields resent-fields)))
1043 (wl-draft-parse-mailbox-list
1045 wl-draft-remove-group-list-contents)))
1046 (setq fields (cdr fields)))
1052 (defun wl-draft-send-mail-with-smtp ()
1053 "Send the prepared message buffer with SMTP."
1055 (let* ((errbuf (if mail-interactive
1056 (generate-new-buffer " smtp errors")
1058 (case-fold-search t)
1059 (default-case-fold-search t)
1060 (sender (or wl-envelope-from
1061 (wl-address-header-extract-address wl-from)))
1062 (delimline (save-excursion
1063 (goto-char (point-min))
1065 (concat "^" (regexp-quote mail-header-separator)
1069 (or wl-smtp-posting-server smtp-server "localhost"))
1070 (smtp-service (or wl-smtp-posting-port smtp-service))
1071 (smtp-local-domain (or smtp-local-domain wl-local-domain))
1072 (id (std11-field-body "message-id"))
1074 (if (not (elmo-plugged-p smtp-server smtp-service))
1075 (wl-draft-set-sent-message 'mail 'unplugged
1076 (cons smtp-server smtp-service))
1079 ;; Instead of `smtp-deduce-address-list'.
1080 (setq recipients (wl-draft-deduce-address-list
1081 (current-buffer) (point-min) delimline))
1082 (unless recipients (error "No recipients"))
1083 ;; Insert an extra newline if we need it to work around
1084 ;; Sun's bug that swallows newlines.
1085 (goto-char (1+ delimline))
1086 (if (eval mail-mailer-swallows-blank-line)
1088 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1089 (if mail-interactive
1093 (wl-draft-delete-field "bcc" delimline)
1094 (wl-draft-delete-field "resent-bcc" delimline)
1095 (let (process-connection-type)
1098 (wl-smtp-extension-bind
1100 (smtp-send-buffer sender recipients (current-buffer))
1102 (wl-draft-write-sendlog 'failed 'smtp smtp-server
1104 (if (and (eq (car err) 'smtp-response-error)
1105 (/= (nth 1 err) 334))
1107 (wl-smtp-password-key
1109 (car smtp-sasl-mechanisms)
1111 (signal (car err) (cdr err)))))
1112 (wl-draft-set-sent-message 'mail 'sent)
1113 (wl-draft-write-sendlog
1114 'ok 'smtp smtp-server recipients id)))))
1115 (if (bufferp errbuf)
1116 (kill-buffer errbuf))))))
1118 (defun wl-draft-send-mail-with-pop-before-smtp ()
1119 "Send the prepared message buffer with POP-before-SMTP."
1120 (require 'elmo-pop3)
1124 :user (or wl-pop-before-smtp-user
1125 elmo-pop3-default-user)
1126 :server (or wl-pop-before-smtp-server
1127 elmo-pop3-default-server)
1128 :port (or wl-pop-before-smtp-port
1129 elmo-pop3-default-port)
1130 :auth (or wl-pop-before-smtp-authenticate-type
1131 elmo-pop3-default-authenticate-type)
1132 :stream-type (or wl-pop-before-smtp-stream-type
1133 elmo-pop3-default-stream-type))))
1134 (condition-case error
1136 (elmo-pop3-get-session session)
1137 (when session (elmo-network-close-session session)))
1139 (elmo-network-close-session session)
1140 (unless (string= (nth 1 error) "Unplugged")
1141 (signal (car error)(cdr error))))))
1142 (wl-draft-send-mail-with-smtp))
1144 (defun wl-draft-insert-required-fields (&optional force-msgid)
1145 "Insert Message-ID, Date, and From field.
1146 If FORCE-MSGID, insert message-id regardless of `wl-insert-message-id'."
1147 ;; Insert Message-Id field...
1148 (goto-char (point-min))
1149 (when (and (or force-msgid
1150 wl-insert-message-id)
1151 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1152 (insert (concat "Message-ID: "
1153 (funcall wl-message-id-function)
1155 ;; Insert date field.
1156 (goto-char (point-min))
1157 (or (re-search-forward "^Date[ \t]*:" nil t)
1158 (wl-draft-insert-date-field))
1159 ;; Insert from field.
1160 (goto-char (point-min))
1161 (or (re-search-forward "^From[ \t]*:" nil t)
1162 (wl-draft-insert-from-field)))
1164 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1165 "Send the message in the current buffer."
1167 (std11-narrow-to-header mail-header-separator)
1168 (wl-draft-insert-required-fields)
1169 ;; Delete null fields.
1170 (goto-char (point-min))
1171 (while (re-search-forward "^[^ \t\n:]+:[ \t]*\n" nil t)
1173 ;; ignore any blank lines in the header
1174 (while (re-search-forward "\n\n\n*" nil t)
1175 (replace-match "\n")))
1176 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1177 (wl-draft-dispatch-message)
1178 (when kill-when-done
1179 ;; hide editing-buffer.
1180 (wl-draft-hide editing-buffer)
1181 ;; delete editing-buffer and its file.
1182 (wl-draft-delete editing-buffer)))
1184 (defun wl-draft-dispatch-message (&optional mes-string)
1185 "Send the message in the current buffer. Not modified the header fields."
1186 (let (delimline mime-bcc)
1187 (if (and wl-draft-verbose-send mes-string)
1188 (message mes-string))
1190 (setq delimline (wl-draft-get-header-delimiter t))
1191 (unless wl-draft-fcc-list
1192 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1194 (setq wl-sent-message-modified nil)
1197 (if (and (wl-message-mail-p)
1198 (not (wl-draft-sent-message-p 'mail)))
1199 (if (or (not (or wl-draft-force-queuing
1200 wl-draft-force-queuing-mail))
1201 (memq 'mail wl-sent-message-queued))
1203 (setq mime-bcc (wl-draft-mime-bcc-field))
1204 (funcall wl-draft-send-mail-function)
1205 (when (not (zerop (length mime-bcc)))
1206 (wl-draft-do-mime-bcc mime-bcc)))
1207 (push 'mail wl-sent-message-queued)
1208 (wl-draft-set-sent-message 'mail 'unplugged)))
1209 (if (and (wl-message-news-p)
1210 (not (wl-draft-sent-message-p 'news))
1211 (not (wl-message-field-exists-p "Resent-to")))
1212 (if (or (not (or wl-draft-force-queuing
1213 wl-draft-force-queuing-news))
1214 (memq 'news wl-sent-message-queued))
1215 (funcall wl-draft-send-news-function)
1216 (push 'news wl-sent-message-queued)
1217 (wl-draft-set-sent-message 'news 'unplugged))))
1218 (let* ((status (wl-draft-sent-message-results))
1219 (unplugged-via (car status))
1220 (sent-via (nth 1 status)))
1221 ;; If one sent, process fcc folder.
1222 (if (and sent-via wl-draft-fcc-list)
1224 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1226 (setq wl-draft-fcc-list nil)))
1227 (if wl-draft-use-cache
1228 (let ((id (std11-field-body "Message-ID"))
1229 (elmo-enable-disconnected-operation t))
1230 (elmo-file-cache-save (elmo-file-cache-get-path id)
1232 ;; If one unplugged, append queue.
1233 (when (and unplugged-via
1234 wl-sent-message-modified)
1235 (if wl-draft-enable-queuing
1237 (wl-draft-queue-append wl-sent-message-via)
1238 (setq wl-sent-message-modified 'requeue))
1239 (error "Unplugged")))
1240 (when wl-draft-verbose-send
1241 (if (and unplugged-via sent-via);; combined message
1243 (setq wl-draft-verbose-msg
1244 (format "Sending%s and Queuing%s..."
1245 sent-via unplugged-via))
1246 (message (concat wl-draft-verbose-msg "done")))
1248 (message (concat mes-string
1249 (if sent-via "done" "failed")))))))))
1250 (not wl-sent-message-modified)) ;; return value
1252 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1253 "Force send current buffer as raw message."
1256 (let (wl-interactive-send
1257 ;;; wl-draft-verbose-send
1258 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1259 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1262 (wl-draft-send kill-when-done mes-string))))
1264 (defun wl-draft-clone-local-variables ()
1265 (let ((locals (buffer-local-variables))
1268 (when (and (consp (car locals))
1270 (string-match wl-draft-clone-local-variable-regexp
1271 (symbol-name (car (car locals)))))
1272 (wl-append result (list (car (car locals)))))
1273 (setq locals (cdr locals)))
1276 (defun wl-draft-send (&optional kill-when-done mes-string)
1277 "Send current draft message.
1278 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1280 ;; Don't call this explicitly.
1281 ;; Added to 'wl-draft-send-hook (by teranisi)
1282 ;; (wl-draft-config-exec)
1283 (run-hooks 'wl-draft-send-hook)
1284 (when (or (not wl-interactive-send)
1285 (y-or-n-p "Do you really want to send current draft? "))
1286 (let ((send-mail-function 'wl-draft-raw-send)
1287 (editing-buffer (current-buffer))
1288 (sending-buffer (wl-draft-generate-clone-buffer
1289 " *wl-draft-sending-buffer*"
1290 (append wl-draft-config-variables
1291 (wl-draft-clone-local-variables))))
1292 (wl-draft-verbose-msg nil)
1295 (save-excursion (set-buffer sending-buffer)
1296 (if (and (not (wl-message-mail-p))
1297 (not (wl-message-news-p)))
1298 (error "No recipient is specified"))
1299 (expand-abbrev) ; for mail-abbrevs
1300 (let ((mime-header-encode-method-alist
1302 '((wl-draft-eword-encode-address-list
1303 . (To Cc Bcc Resent-To Resent-Cc Bcc Resent-Bcc)))
1304 (if (boundp 'mime-header-encode-method-alist)
1305 (symbol-value 'mime-header-encode-method-alist)))))
1306 (run-hooks 'mail-send-hook) ; translate buffer
1309 (if wl-draft-verbose-send
1310 (message (or mes-string "Sending...")))
1311 (funcall wl-draft-send-function editing-buffer kill-when-done)
1312 ;; Now perform actions on successful sending.
1313 (while mail-send-actions
1315 (apply (car (car mail-send-actions))
1316 (cdr (car mail-send-actions)))
1318 (setq mail-send-actions (cdr mail-send-actions)))
1319 (if wl-draft-verbose-send
1320 (message (concat (or wl-draft-verbose-msg
1321 mes-string "Sending...")
1323 ;; kill sending buffer, anyway.
1324 (and (buffer-live-p sending-buffer)
1325 (kill-buffer sending-buffer))))))
1327 (defun wl-draft-mime-bcc-field ()
1328 "Return the MIME-Bcc field body. The field is deleted."
1329 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1330 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1332 (defun wl-draft-do-mime-bcc (field-body)
1333 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1334 (let ((orig-from (mime-decode-field-body (std11-field-body "from")
1336 (orig-subj (mime-decode-field-body (or (std11-field-body "subject")
1339 (recipients (wl-parse-addresses field-body))
1340 (draft-buffer (current-buffer))
1342 (save-window-excursion
1343 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1344 (not (zerop (length field-body))))
1345 (let ((wl-draft-doing-mime-bcc t))
1346 (dolist (recipient recipients)
1347 (wl-draft-create-buffer)
1348 (wl-draft-create-contents
1349 (append `((From . ,orig-from)
1351 (Subject . ,(concat "A blind carbon copy ("
1354 (wl-draft-default-headers)))
1355 (wl-draft-insert-mail-header-separator)
1356 (wl-draft-prepare-edit)
1357 (goto-char (point-max))
1358 (insert (or wl-draft-mime-bcc-body
1359 "This is a blind carbon copy.")
1361 (mime-edit-insert-tag "message" "rfc822")
1362 (insert-buffer draft-buffer)
1363 (let (wl-interactive-send)
1364 (wl-draft-send 'kill-when-done))))))))
1366 ;; Derived from `message-save-drafts' in T-gnus.
1367 (defun wl-draft-save ()
1368 "Save current draft."
1370 (if (buffer-modified-p)
1372 (message "Saving %s..." wl-draft-buffer-file-name)
1373 (let ((msg (buffer-substring-no-properties (point-min) (point-max))))
1374 (with-temp-file wl-draft-buffer-file-name
1376 ;; If no header separator, insert it.
1378 (goto-char (point-min))
1379 (unless (re-search-forward
1380 (concat "^" (regexp-quote mail-header-separator) "$")
1382 (goto-char (point-min))
1383 (if (re-search-forward "\n\n" nil t)
1384 (replace-match (concat "\n" mail-header-separator "\n"))
1385 (goto-char (point-max))
1386 (insert (if (eq (char-before) ?\n) "" "\n")
1387 mail-header-separator "\n"))))
1388 (let ((mime-header-encode-method-alist
1389 '((eword-encode-unstructured-field-body))))
1390 (mime-edit-translate-buffer))
1391 (wl-draft-get-header-delimiter t)))
1392 (set-buffer-modified-p nil)
1393 (wl-draft-config-info-operation
1394 (and (string-match "[0-9]+$" wl-draft-buffer-file-name)
1396 (match-string 0 wl-draft-buffer-file-name)))
1398 (message "Saving %s...done" wl-draft-buffer-file-name))
1399 (message "(No changes need to be saved)")))
1401 (defun wl-draft-mimic-kill-buffer ()
1402 "Kill the current (draft) buffer with query."
1404 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1407 (if (or (not bufname)
1408 (string-equal bufname "")
1409 (string-equal bufname (buffer-name)))
1410 (let ((bufname (current-buffer)))
1411 (when (or (not (buffer-modified-p))
1413 (format "Buffer %s modified; kill anyway? " bufname)))
1414 (set-buffer-modified-p nil)
1415 (wl-draft-hide bufname)
1416 (kill-buffer bufname)))
1417 (kill-buffer bufname))))
1419 (defun wl-draft-save-and-exit ()
1420 "Save current draft and exit current draft mode."
1423 (let ((editing-buffer (current-buffer)))
1424 (wl-draft-hide editing-buffer)
1425 (kill-buffer editing-buffer)))
1427 (defun wl-draft-send-and-exit ()
1428 "Send current draft message and kill it."
1432 (defun wl-draft-send-from-toolbar ()
1434 (let ((wl-interactive-send t))
1435 (wl-draft-send-and-exit)))
1437 (defun wl-draft-delete-field (field &optional delimline replace)
1438 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1440 (defun wl-draft-delete-fields (field &optional delimline replace)
1443 (goto-char (point-min))
1444 (if (search-forward "\n\n" nil t)
1445 (setq delimline (point))
1446 (setq delimline (point-max))))
1447 (narrow-to-region (point-min) delimline)
1448 (goto-char (point-min))
1449 (let ((regexp (concat "^" field ":"))
1450 (case-fold-search t))
1452 (if (looking-at regexp)
1458 (if (re-search-forward "^[^ \t]" nil t)
1459 (goto-char (match-beginning 0))
1462 (insert (concat field ": " replace "\n"))))
1464 (if (re-search-forward "^[^ \t]" nil t)
1465 (goto-char (match-beginning 0))
1468 (defun wl-draft-get-fcc-list (header-end)
1469 (if (and wl-draft-doing-mime-bcc
1470 wl-draft-disable-fcc-for-mime-bcc)
1472 (wl-draft-delete-field "fcc")
1475 (case-fold-search t))
1476 (or (markerp header-end) (error "HEADER-END must be a marker"))
1478 (goto-char (point-min))
1479 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1484 (buffer-substring-no-properties
1488 (skip-chars-backward " \t")
1491 (dolist (folder fcc-list)
1492 (wl-folder-confirm-existence
1493 (wl-folder-get-elmo-folder (eword-decode-string folder)))))
1494 (delete-region (match-beginning 0)
1495 (progn (forward-line 1) (point)))))
1498 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1499 (let ((send-mail-buffer (current-buffer))
1500 (tembuf (generate-new-buffer " fcc output"))
1501 (case-fold-search t)
1503 (or (markerp header-end) (error "HEADER-END must be a marker"))
1506 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1509 ;; insert just the headers to avoid moving the gap more than
1510 ;; necessary (the message body could be arbitrarily huge.)
1511 (insert-buffer-substring send-mail-buffer 1 header-end)
1512 (wl-draft-insert-required-fields t)
1513 (goto-char (point-max))
1514 (insert-buffer-substring send-mail-buffer header-end)
1515 (let ((id (std11-field-body "Message-ID"))
1516 (elmo-enable-disconnected-operation t)
1519 (unless (or cache-saved
1520 (elmo-folder-plugged-p
1521 (wl-folder-get-elmo-folder (car fcc-list))))
1522 (elmo-file-cache-save id nil) ;; for disconnected operation
1523 (setq cache-saved t))
1524 (if (elmo-folder-append-buffer
1525 (wl-folder-get-elmo-folder
1526 (eword-decode-string (car fcc-list)))
1527 (not wl-fcc-force-as-read))
1528 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1529 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1530 (setq fcc-list (cdr fcc-list)))))
1531 (kill-buffer tembuf)))
1533 (defun wl-draft-on-field-p ()
1536 (goto-char (point-min))
1537 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1544 (if (or (looking-at ".*,[ \t]?$")
1545 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1547 (let ((pos (point)))
1550 (if (looking-at "^[ \t]")
1552 (if (re-search-forward ":" pos t) nil t)))))))
1556 (defun wl-draft (&optional header-alist
1557 content-type content-transfer-encoding
1560 "Write and send mail/news message with Wanderlust."
1567 (wl-plugged-init t))
1569 (wl-init)) ; returns immediately if already initialized.
1572 (let (buf-name header-alist-internal)
1574 (wl-draft-create-buffer
1576 (eq this-command 'wl-draft)
1577 (eq this-command 'wl-summary-write)
1578 (eq this-command 'wl-summary-write-current-folder)
1579 (eq this-command 'wl-folder-write-current-folder))
1582 (unless (cdr (assq 'From header-alist))
1584 (append (list (cons 'From wl-from)) header-alist)))
1585 (unless (cdr (assq 'To header-alist))
1590 (if (assq 'To header-alist)
1591 (setcdr (assq 'To header-alist) to)
1593 (append header-alist
1594 (list (cons 'To to))))))))
1595 (unless (cdr (assq 'Subject header-alist))
1596 (if (assq 'Subject header-alist)
1597 (setcdr (assq 'Subject header-alist) "")
1599 (append header-alist (list (cons 'Subject ""))))))
1600 (setq header-alist (append header-alist
1601 (wl-draft-default-headers)
1602 wl-draft-additional-header-alist
1603 (if body (list "" (cons 'Body body)))))
1604 (wl-draft-create-contents header-alist)
1606 (wl-draft-decode-body
1607 content-type content-transfer-encoding))
1608 (wl-draft-insert-mail-header-separator)
1609 (wl-draft-prepare-edit)
1611 (run-hooks 'wl-mail-setup-hook))
1612 (goto-char (point-min))
1613 (wl-user-agent-compose-internal) ;; user-agent
1616 (string= (cdr (assq 'To header-alist)) ""))
1617 (mail-position-on-field "To"))
1619 (goto-char (point-max))))
1622 (defun wl-draft-create-buffer (&optional full parent-folder)
1623 (let* ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1624 (parent-folder (or parent-folder (wl-summary-buffer-folder-name)))
1625 (summary-buf (wl-summary-get-buffer parent-folder))
1626 buf-name file-name num change-major-mode-hook)
1627 (if (not (elmo-folder-message-file-p draft-folder))
1628 (error "%s folder cannot be used for draft folder" wl-draft-folder))
1629 (setq num (elmo-max-of-list
1630 (or (elmo-folder-list-messages draft-folder) '(0))))
1631 (setq num (+ 1 num))
1632 ;; To get unused buffer name.
1633 (while (get-buffer (concat wl-draft-folder "/" (int-to-string num)))
1634 (setq num (+ 1 num)))
1635 (setq buf-name (find-file-noselect
1637 (elmo-message-file-name
1638 (wl-folder-get-elmo-folder wl-draft-folder)
1640 (if wl-draft-use-frame
1641 (switch-to-buffer-other-frame buf-name)
1642 (switch-to-buffer buf-name))
1643 (set-buffer buf-name)
1644 (if (not (string-match (regexp-quote wl-draft-folder)
1646 (rename-buffer (concat wl-draft-folder "/" (int-to-string num))))
1647 (if (or (eq wl-draft-reply-buffer-style 'full)
1649 (delete-other-windows))
1652 (make-local-variable 'truncate-partial-width-windows)
1653 (setq truncate-partial-width-windows nil)
1654 (setq truncate-lines wl-draft-truncate-lines)
1655 (setq wl-sent-message-via nil)
1656 (setq wl-sent-message-queued nil)
1657 (setq wl-draft-buffer-file-name file-name)
1658 (setq wl-draft-config-exec-flag t)
1659 (setq wl-draft-parent-folder (or parent-folder ""))
1660 (or (eq this-command 'wl-folder-write-current-folder)
1661 (setq wl-draft-buffer-cur-summary-buffer summary-buf))
1664 (defun wl-draft-create-contents (header-alist)
1665 "header-alist' sample
1666 '(function ;; funcall
1667 string ;; insert string
1668 (symbol . string) ;; insert symbol-value: string
1669 (symbol . function) ;; (funcall) and if it returns string,
1670 ;; insert symbol-value: string
1671 (symbol . nil) ;; do nothing
1675 (unless (eq major-mode 'wl-draft-mode)
1676 (error "wl-draft-create-header must be use in wl-draft-mode."))
1677 (let ((halist header-alist)
1682 ((functionp (car halist)) (funcall (car halist)))
1684 ((stringp (car halist)) (insert (car halist) "\n"))
1686 ((consp (car halist))
1687 (setq field (car (car halist)))
1688 (setq value (cdr (car halist)))
1692 ((eq field 'Body) ; body
1694 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1696 (let ((value-return (funcall value)))
1697 (when (stringp value-return)
1698 (insert (symbol-name field) ": " value-return "\n"))))
1707 (setq halist (cdr halist)))))
1709 (defun wl-draft-prepare-edit ()
1710 (unless (eq major-mode 'wl-draft-mode)
1711 (error "wl-draft-create-header must be use in wl-draft-mode."))
1712 (let (change-major-mode-hook)
1713 (wl-draft-editor-mode)
1714 (add-hook 'local-write-file-hooks 'wl-draft-save)
1715 (wl-draft-overload-functions)
1716 (wl-highlight-headers 'for-draft)
1718 (clear-visited-file-modtime)))
1720 (defun wl-draft-decode-header ()
1722 (std11-narrow-to-header)
1723 (wl-draft-decode-message-in-buffer)
1726 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1729 (std11-field-body "content-type")))
1730 (content-transfer-encoding
1731 (or content-transfer-encoding
1732 (std11-field-body "content-transfer-encoding")))
1735 (std11-narrow-to-header)
1736 (wl-draft-delete-field "content-type")
1737 (wl-draft-delete-field "content-transfer-encoding")
1738 (goto-char (point-max))
1739 (setq delimline (point-marker))
1741 (narrow-to-region delimline (point-max))
1742 (goto-char (point-min))
1744 (insert "Content-type: " content-type "\n"))
1745 (when content-transfer-encoding
1746 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1747 (wl-draft-decode-message-in-buffer)
1748 (goto-char (point-min))
1749 (unless (re-search-forward "^$" (point-at-eol) t)
1754 ;;; subroutine for wl-draft-create-contents
1755 ;;; must be used in wl-draft-mode
1756 (defun wl-draft-check-new-line ()
1757 (if (not (= (preceding-char) ?\n))
1760 (defsubst wl-draft-trim-ccs (cc)
1766 (null (and wl-draft-delete-myself-from-bcc-fcc
1768 (mapcar 'wl-address-header-extract-address
1770 (wl-parse-addresses (std11-field-body "To"))
1771 (wl-parse-addresses (std11-field-body "Cc"))))
1772 (mapcar 'downcase wl-subscribed-mailing-list)))))
1776 (defsubst wl-draft-default-headers ()
1778 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1779 (wl-address-header-extract-address
1781 (cons 'User-Agent wl-generate-mailer-string-function)
1782 (cons 'Reply-To mail-default-reply-to)
1783 (cons 'Bcc (function
1786 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1787 (cons 'Fcc (function
1789 (wl-draft-trim-ccs wl-fcc))))
1790 (cons 'Organization wl-organization)
1791 (and wl-auto-insert-x-face
1792 (file-exists-p wl-x-face-file)
1793 'wl-draft-insert-x-face-field-here) ;; allow nil
1794 mail-default-headers
1795 ;; check \n at th end of line for `mail-default-headers'
1796 'wl-draft-check-new-line
1799 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1802 (goto-char delimline)
1803 (goto-char (point-min))
1804 (if (search-forward "\n\n" nil t)
1805 (delete-backward-char 1)
1806 (goto-char (point-max))))
1807 (wl-draft-check-new-line)
1808 (put-text-property (point)
1810 (insert mail-header-separator "\n")
1812 'category 'mail-header-separator)))
1816 (defun wl-draft-elmo-nntp-send ()
1817 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1818 (elmo-nntp-default-user
1819 (or wl-nntp-posting-user elmo-nntp-default-user))
1820 (elmo-nntp-default-server
1821 (or wl-nntp-posting-server elmo-nntp-default-server))
1822 (elmo-nntp-default-port
1823 (or wl-nntp-posting-port elmo-nntp-default-port))
1824 (elmo-nntp-default-stream-type
1825 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1826 (elmo-nntp-default-function wl-nntp-posting-function)
1828 (if (setq condition (cdr (elmo-string-matched-assoc
1829 (std11-field-body "Newsgroups")
1830 wl-nntp-posting-config-alist)))
1831 (if (stringp condition)
1832 (setq elmo-nntp-default-server condition)
1833 (while (car condition)
1834 (set (intern (format "elmo-nntp-default-%s"
1835 (symbol-name (caar condition))))
1837 (setq condition (cdr condition)))))
1838 (unless elmo-nntp-default-function
1839 (error "wl-draft-nntp-send: posting-function is nil."))
1840 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1841 (wl-draft-set-sent-message 'news 'unplugged
1842 (cons elmo-nntp-default-server
1843 elmo-nntp-default-port))
1844 (funcall elmo-nntp-default-function
1845 elmo-nntp-default-server (current-buffer))
1846 (wl-draft-set-sent-message 'news 'sent)
1847 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1848 (std11-field-body "Newsgroups")
1849 (std11-field-body "Message-ID")))))
1851 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1852 "Generate clone of current buffer named NAME."
1853 (let ((editing-buffer (current-buffer)))
1855 (set-buffer (generate-new-buffer name))
1858 (wl-draft-editor-mode)
1859 (insert-buffer editing-buffer)
1861 (while local-variables
1862 (make-local-variable (car local-variables))
1863 (set (car local-variables)
1865 (set-buffer editing-buffer)
1866 (symbol-value (car local-variables))))
1867 (setq local-variables (cdr local-variables)))
1870 (defun wl-draft-reedit (number)
1871 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1873 buffer file-name change-major-mode-hook)
1874 (setq file-name (elmo-message-file-name draft-folder number))
1875 (unless (file-exists-p file-name)
1876 (error "File %s does not exist" file-name))
1877 (if (setq buffer (get-buffer
1878 (concat wl-draft-folder "/"
1879 (number-to-string number))))
1881 (if wl-draft-use-frame
1882 (switch-to-buffer-other-frame buffer)
1883 (switch-to-buffer buffer))
1884 (set-buffer buffer))
1885 (setq buffer (get-buffer-create (number-to-string number)))
1886 (if wl-draft-use-frame
1887 (switch-to-buffer-other-frame buffer)
1888 (switch-to-buffer buffer))
1890 (setq wl-draft-parent-folder "")
1891 (insert-file-contents-as-binary file-name)
1892 (let((mime-edit-again-ignored-field-regexp
1893 "^\\(Content-.*\\|Mime-Version\\):"))
1894 (wl-draft-decode-message-in-buffer))
1895 (wl-draft-insert-mail-header-separator)
1896 (if (not (string-match (regexp-quote wl-draft-folder)
1898 (rename-buffer (concat wl-draft-folder "/" (buffer-name))))
1901 (setq buffer-file-name file-name)
1902 (make-local-variable 'truncate-partial-width-windows)
1903 (setq truncate-partial-width-windows nil)
1904 (setq truncate-lines wl-draft-truncate-lines)
1905 (setq wl-sent-message-via nil)
1906 (setq wl-sent-message-queued nil)
1907 (setq wl-draft-buffer-file-name file-name)
1908 (wl-draft-config-info-operation number 'load)
1909 (goto-char (point-min))
1910 (wl-draft-overload-functions)
1911 (wl-draft-editor-mode)
1912 (add-hook 'local-write-file-hooks 'wl-draft-save)
1913 (wl-highlight-headers 'for-draft)
1914 (run-hooks 'wl-draft-reedit-hook)
1915 (goto-char (point-max))
1918 (defmacro wl-draft-body-goto-top ()
1920 (goto-char (point-min))
1921 (if (re-search-forward mail-header-separator nil t)
1923 (goto-char (point-max))))))
1925 (defmacro wl-draft-body-goto-bottom ()
1926 (` (goto-char (point-max))))
1928 (defmacro wl-draft-config-body-goto-header ()
1930 (goto-char (point-min))
1931 (if (re-search-forward mail-header-separator nil t)
1933 (goto-char (point-max))))))
1935 (defsubst wl-draft-config-sub-eval-insert (content &optional newline)
1936 (let (content-value)
1938 (stringp (setq content-value (eval content))))
1939 (insert content-value)
1940 (if newline (insert "\n")))))
1942 (defun wl-draft-config-sub-body (content)
1943 (wl-draft-body-goto-top)
1944 (delete-region (point) (point-max))
1945 (wl-draft-config-sub-eval-insert content))
1947 (defun wl-draft-config-sub-top (content)
1948 (wl-draft-body-goto-top)
1949 (wl-draft-config-sub-eval-insert content))
1951 (defun wl-draft-config-sub-bottom (content)
1952 (wl-draft-body-goto-bottom)
1953 (wl-draft-config-sub-eval-insert content))
1955 (defun wl-draft-config-sub-header (content)
1956 (wl-draft-config-body-goto-header)
1957 (wl-draft-config-sub-eval-insert content 'newline))
1959 (defun wl-draft-config-sub-header-top (content)
1960 (goto-char (point-min))
1961 (wl-draft-config-sub-eval-insert content 'newline))
1963 (defun wl-draft-config-sub-part-top (content)
1964 (goto-char (mime-edit-content-beginning))
1965 (wl-draft-config-sub-eval-insert content 'newline))
1967 (defun wl-draft-config-sub-part-bottom (content)
1968 (goto-char (mime-edit-content-end))
1969 (wl-draft-config-sub-eval-insert content 'newline))
1971 (defsubst wl-draft-config-sub-file (content)
1972 (let ((coding-system-for-read wl-cs-autoconv)
1973 (file (expand-file-name (eval content))))
1974 (if (file-exists-p file)
1975 (insert-file-contents file)
1976 (error "%s: no exists file" file))))
1978 (defun wl-draft-config-sub-body-file (content)
1979 (wl-draft-body-goto-top)
1980 (delete-region (point) (point-max))
1981 (wl-draft-config-sub-file content))
1983 (defun wl-draft-config-sub-top-file (content)
1984 (wl-draft-body-goto-top)
1985 (wl-draft-config-sub-file content))
1987 (defun wl-draft-config-sub-bottom-file (content)
1988 (wl-draft-body-goto-bottom)
1989 (wl-draft-config-sub-file content))
1991 (defun wl-draft-config-sub-header-file (content)
1992 (wl-draft-config-body-goto-header)
1993 (wl-draft-config-sub-file content))
1995 (defun wl-draft-config-sub-template (content)
1996 (setq wl-draft-config-variables
1997 (wl-template-insert (eval content))))
1999 (defun wl-draft-config-sub-x-face (content)
2000 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
2001 (fboundp 'x-face-insert)) ; x-face.el is installed.
2002 (x-face-insert content)
2003 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
2005 (defsubst wl-draft-config-sub-func (field content)
2007 (if (setq func (assq field wl-draft-config-sub-func-alist))
2008 (let (wl-draft-config-variables)
2009 (funcall (cdr func) content)
2010 ;; for wl-draft-config-sub-template
2011 (cons t wl-draft-config-variables)))))
2013 (defsubst wl-draft-config-exec-sub (clist)
2014 (let (config local-variables)
2016 (setq config (car clist))
2021 (let ((field (car config))
2022 (content (cdr config))
2026 (wl-draft-replace-field field (eval content) t))
2027 ((setq ret-val (wl-draft-config-sub-func field content))
2028 (if (cdr ret-val) ;; for wl-draft-config-sub-template
2029 (wl-append local-variables (cdr ret-val))))
2030 ((boundp field) ;; variable
2031 (make-local-variable field)
2032 (set field (eval content))
2033 (wl-append local-variables (list field)))
2035 (error "%s: not variable" field)))))
2037 (error "%s: not supported type" config)))
2038 (setq clist (cdr clist)))
2041 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
2042 "Change headers in draft preparation time."
2044 (unless wl-draft-reedit
2047 (and (boundp 'wl-draft-prepared-config-alist)
2048 wl-draft-prepared-config-alist) ;; For compatible.
2049 wl-draft-config-alist)))
2051 (wl-draft-config-exec config-alist reply-buf)))))
2053 (defun wl-draft-config-exec (&optional config-alist reply-buf)
2054 "Change headers according to the value of `wl-draft-config-alist'.
2055 Automatically applied in draft sending time."
2057 (let ((case-fold-search t)
2058 (alist (or config-alist wl-draft-config-alist))
2059 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2060 wl-draft-reply-buffer)))
2061 (local-variables wl-draft-config-variables)
2063 (when (and (or (interactive-p)
2064 wl-draft-config-exec-flag)
2069 (setq key (caar alist)
2076 (set-buffer reply-buf)
2078 (std11-narrow-to-header)
2079 (goto-char (point-min))
2080 (re-search-forward (car clist) nil t))))
2081 (wl-draft-config-exec-sub (cdr clist))
2084 (when (save-restriction
2085 (std11-narrow-to-header mail-header-separator)
2086 (goto-char (point-min))
2087 (re-search-forward key nil t))
2088 (wl-append local-variables
2089 (wl-draft-config-exec-sub clist))
2092 (wl-append local-variables
2093 (wl-draft-config-exec-sub clist))
2095 (if (and found wl-draft-config-matchone)
2097 (setq alist (cdr alist))))
2099 (setq wl-draft-config-exec-flag nil))
2100 (run-hooks 'wl-draft-config-exec-hook)
2101 (put-text-property (point-min)(point-max) 'face nil)
2102 (wl-highlight-message (point-min)(point-max) t)
2103 (setq wl-draft-config-variables
2104 (elmo-uniq-list local-variables))))))
2106 (defun wl-draft-replace-field (field content &optional add)
2109 (let ((case-fold-search t)
2110 (inhibit-read-only t) ;; added by teranisi.
2112 (std11-narrow-to-header mail-header-separator)
2113 (goto-char (point-min))
2114 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2119 (re-search-forward "^[^ \t]" nil 'move)
2121 (skip-chars-backward "\n")
2122 (delete-region beg (point))
2123 (insert " " content))
2128 (re-search-forward "^[^ \t]" nil 'move)
2130 (delete-region beg (point)))
2131 (when (and add content)
2133 (goto-char (point-max))
2134 (insert (concat field ": " content "\n"))))))))
2136 (defun wl-draft-config-info-operation (msg operation)
2137 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-folder-get-elmo-folder
2141 (format "%s-%d" wl-draft-config-save-filename msg)
2143 element alist variable)
2145 ((eq operation 'save)
2146 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2147 (while (setq variable (pop variables))
2148 (when (boundp variable)
2150 (list (cons variable (eval variable))))))
2151 (elmo-object-save filename alist)))
2152 ((eq operation 'load)
2153 (setq alist (elmo-object-load filename))
2154 (while (setq element (pop alist))
2155 (set (make-local-variable (car element)) (cdr element))
2156 (wl-append wl-draft-config-variables (list (car element)))))
2157 ((eq operation 'delete)
2158 (if (file-exists-p filename)
2159 (delete-file filename))))))
2161 (defun wl-draft-queue-info-operation (msg operation
2162 &optional add-sent-message-via)
2163 (let* ((msgdb-dir (elmo-folder-msgdb-path
2164 (wl-folder-get-elmo-folder wl-queue-folder)))
2167 (format "%s-%d" wl-draft-queue-save-filename msg)
2169 element alist variable)
2171 ((eq operation 'save)
2172 (let ((variables (elmo-uniq-list
2173 (append wl-draft-queue-save-variables
2174 wl-draft-config-variables
2175 (list 'wl-draft-fcc-list)))))
2176 (if add-sent-message-via
2178 (push 'wl-sent-message-queued variables)
2179 (push 'wl-sent-message-via variables)))
2180 (while (setq variable (pop variables))
2181 (when (boundp variable)
2183 (list (cons variable (eval variable))))))
2184 (elmo-object-save filename alist)))
2185 ((eq operation 'load)
2186 (setq alist (elmo-object-load filename))
2187 (while (setq element (pop alist))
2188 (set (make-local-variable (car element)) (cdr element))))
2189 ((eq operation 'get-sent-via)
2190 (setq alist (elmo-object-load filename))
2191 (cdr (assq 'wl-sent-message-via alist)))
2192 ((eq operation 'delete)
2193 (if (file-exists-p filename)
2194 (delete-file filename))))))
2196 (defun wl-draft-queue-append (wl-sent-message-via)
2197 (if wl-draft-verbose-send
2198 (message "Queuing..."))
2199 (let ((send-buffer (current-buffer))
2200 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2201 (message-id (std11-field-body "Message-ID")))
2202 (if (elmo-folder-append-buffer folder t)
2204 (wl-draft-queue-info-operation
2205 (car (elmo-folder-status folder))
2206 'save wl-sent-message-via)
2207 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2208 (when wl-draft-verbose-send
2209 (setq wl-draft-verbose-msg "Queuing...")
2210 (message "Queuing...done")))
2211 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2212 (error "Queuing failed"))))
2214 (defun wl-draft-queue-flush ()
2215 "Flush draft queue."
2217 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2219 (elmo-folder-open-internal queue-folder)
2220 (elmo-folder-list-messages queue-folder)))
2223 (wl-draft-queue-flushing t)
2224 msgs failure len buffer msgid sent-via)
2225 ;; get plugged send message
2227 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2230 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2231 (or (not (nth 2 (car sent-via)))
2233 (car (nth 2 (car sent-via)))
2234 (cdr (nth 2 (car sent-via))))))
2235 (wl-append msgs (list (car msgs2)))
2237 (setq sent-via (cdr sent-via))))
2238 (setq msgs2 (cdr msgs2)))
2239 (when (> (setq len (length msgs)) 0)
2240 (if (elmo-y-or-n-p (format
2241 "%d message(s) are in the sending queue. Send now? "
2243 (not elmo-dop-flush-confirm) t)
2246 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2249 ;; reset buffer local variables
2250 (kill-all-local-variables)
2254 (setq wl-sent-message-via nil)
2255 (wl-draft-queue-info-operation (car msgs) 'load)
2256 (elmo-message-fetch queue-folder
2258 (elmo-make-fetch-strategy 'entire)
2259 nil (current-buffer))
2261 (setq failure (funcall
2262 wl-draft-queue-flush-send-function
2263 (format "Sending (%d/%d)..." i len)))
2264 ;;; (wl-draft-raw-send nil nil
2265 ;;; (format "Sending (%d/%d)..." i len))
2267 (elmo-display-error err t)
2271 (if (eq wl-sent-message-modified 'requeue)
2273 (elmo-folder-delete-messages
2274 queue-folder (cons (car msgs) nil))
2275 (wl-draft-queue-info-operation (car msgs) 'delete))
2277 (elmo-folder-delete-messages
2278 queue-folder (cons (car msgs) nil))
2279 (wl-draft-queue-info-operation (car msgs) 'delete)
2280 (setq performed (+ 1 performed))))
2281 (setq msgs (cdr msgs)))
2282 (kill-buffer buffer)
2283 (message "%d message(s) are sent." performed)))
2284 (message "%d message(s) are remained to be sent." len))
2285 (elmo-folder-close queue-folder)
2288 (defun wl-jump-to-draft-buffer (&optional arg)
2289 "Jump to the draft if exists."
2292 (wl-jump-to-draft-folder)
2293 (let ((draft-bufs (wl-collect-draft))
2297 (message "No draft buffer exist."))
2300 (sort (mapcar 'buffer-name draft-bufs)
2301 (function (lambda (a b)
2302 (not (string< a b))))))
2303 (if (setq buf (cdr (member (buffer-name)
2305 (setq buf (car buf))
2306 (setq buf (car draft-bufs)))
2307 (switch-to-buffer buf))))))
2309 (defun wl-jump-to-draft-folder ()
2310 (let ((msgs (reverse (elmo-folder-list-messages (wl-folder-get-elmo-folder
2312 (mybuf (buffer-name))
2315 (message "No draft message exist.")
2316 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2317 (setq msg (cadr (memq
2318 (string-to-int (substring mybuf (match-end 0)))
2320 (or msg (setq msg (car msgs)))
2321 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2322 (switch-to-buffer buf)
2323 (wl-draft-reedit msg)))))
2325 (defun wl-draft-highlight-and-recenter (&optional n)
2327 (when wl-highlight-body-too
2328 (let ((modified (buffer-modified-p)))
2331 (put-text-property (point-min) (point-max) 'face nil)
2332 (wl-highlight-message (point-min) (point-max) t))
2333 (set-buffer-modified-p modified))))
2336 ;;;; user-agent support by Sen Nagata
2338 ;; this appears to be necessarily global...
2339 (defvar wl-user-agent-compose-p nil)
2340 (defvar wl-user-agent-headers-and-body-alist nil)
2342 ;; this should be a generic function for mail-mode -- i wish there was
2343 ;; something like it in sendmail.el
2344 (defun wl-user-agent-insert-header (header-name header-value)
2345 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2346 ;; it seems like overriding existing headers is acceptable -- should
2347 ;; we provide an option?
2349 ;; plan was: unfold header (might be folded), remove existing value, insert
2351 ;; wl doesn't seem to fold header lines yet anyway :-)
2353 (let ((kill-whole-line t)
2355 (mail-position-on-field (capitalize header-name))
2356 (setq end-of-line (point))
2358 (re-search-forward ":" end-of-line)
2359 (insert (concat " " header-value "\n"))
2362 ;; this should be a generic function for mail-mode -- i wish there was
2363 ;; something like it in sendmail.el
2365 ;; ** haven't dealt w/ case where the body is already set **
2366 (defun wl-user-agent-insert-body (body-text)
2367 "Insert a body of text, BODY-TEXT, into a message."
2368 ;; code defensively... :-P
2369 (goto-char (point-min))
2370 (search-forward mail-header-separator)
2375 (defun wl-user-agent-compose (&optional to subject other-headers continue
2376 switch-function yank-action
2378 "Support the `compose-mail' interface for wl.
2379 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2380 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2381 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2383 (unless (featurep 'wl)
2385 ;; protect these -- to and subject get bound at some point, so it looks
2386 ;; to be necessary to protect the values used w/in
2387 (let ((wl-user-agent-headers-and-body-alist other-headers)
2388 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2389 (wl-draft-reply-buffer-style 'split))
2390 (when (eq switch-function 'switch-to-buffer-other-window)
2391 (when (one-window-p t)
2392 (if (window-minibuffer-p) (other-window 1))
2396 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2399 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2402 (setq wl-user-agent-headers-and-body-alist
2403 (cons (cons "to" to)
2404 wl-user-agent-headers-and-body-alist))))
2406 (if (wl-string-match-assoc "subject"
2407 wl-user-agent-headers-and-body-alist
2410 (wl-string-match-assoc "subject"
2411 wl-user-agent-headers-and-body-alist
2414 (setq wl-user-agent-headers-and-body-alist
2415 (cons (cons "subject" subject)
2416 wl-user-agent-headers-and-body-alist))))
2417 ;; i think this is what we want to use...
2420 ;; tell the hook-function to do its stuff
2421 (setq wl-user-agent-compose-p t)
2422 ;; because to get the hooks working, wl-draft has to think it has
2423 ;; been called interactively
2424 (call-interactively 'wl-draft))
2425 (setq wl-user-agent-compose-p nil))))
2427 (defun wl-user-agent-compose-internal ()
2428 "Manipulate headers and/or a body of a draft message."
2429 ;; being called from wl-user-agent-compose?
2430 (if wl-user-agent-compose-p
2433 (let ((headers wl-user-agent-headers-and-body-alist)
2434 (case-fold-search t))
2437 (if (not (string-match "^body$" (car (car headers))))
2438 (wl-user-agent-insert-header
2439 (car (car headers)) (cdr (car headers)))
2441 (setq headers (cdr headers))))
2442 ;; highlight headers (from wl-draft in wl-draft.el)
2443 (wl-highlight-headers 'for-draft)
2445 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2447 (wl-user-agent-insert-body
2448 (cdr (wl-string-match-assoc
2450 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2454 (product-provide (provide 'wl-draft) (require 'wl-version))
2456 ;;; wl-draft.el ends here