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 (defvar wl-draft-config-sub-func-alist
93 '((body . wl-draft-config-sub-body)
94 (top . wl-draft-config-sub-top)
95 (bottom . wl-draft-config-sub-bottom)
96 (header . wl-draft-config-sub-header)
97 (header-top . wl-draft-config-sub-header-top)
98 (header-bottom . wl-draft-config-sub-header)
99 (part-top . wl-draft-config-sub-part-top)
100 (part-bottom . wl-draft-config-sub-part-bottom)
101 (body-file . wl-draft-config-sub-body-file)
102 (top-file . wl-draft-config-sub-top-file)
103 (bottom-file . wl-draft-config-sub-bottom-file)
104 (header-file . wl-draft-config-sub-header-file)
105 (template . wl-draft-config-sub-template)
106 (x-face . wl-draft-config-sub-x-face)))
108 (make-variable-buffer-local 'wl-draft-buffer-file-name)
109 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
110 (make-variable-buffer-local 'wl-draft-config-variables)
111 (make-variable-buffer-local 'wl-draft-config-exec-flag)
112 (make-variable-buffer-local 'wl-sent-message-via)
113 (make-variable-buffer-local 'wl-sent-message-queued)
114 (make-variable-buffer-local 'wl-draft-fcc-list)
115 (make-variable-buffer-local 'wl-draft-reply-buffer)
116 (make-variable-buffer-local 'wl-draft-parent-folder)
117 (make-variable-buffer-local 'wl-draft-parent-number)
119 (defsubst wl-smtp-password-key (user mechanism server)
120 (format "SMTP:%s/%s@%s"
121 user mechanism server))
123 (defmacro wl-smtp-extension-bind (&rest body)
124 (` (let* ((smtp-sasl-mechanisms
125 (if wl-smtp-authenticate-type
127 (if (listp wl-smtp-authenticate-type)
128 wl-smtp-authenticate-type
129 (list wl-smtp-authenticate-type)))))
130 (smtp-use-sasl (and smtp-sasl-mechanisms t))
131 (smtp-use-starttls (eq wl-smtp-connection-type 'starttls))
132 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
133 (if (and (string= (car smtp-sasl-mechanisms) "DIGEST-MD5")
135 (string-match "^\\([^@]*\\)@\\([^@]*\\)"
136 wl-smtp-posting-user))
137 (setq smtp-sasl-user-name (match-string 1 wl-smtp-posting-user)
138 smtp-sasl-properties (list 'realm
139 (match-string 2 wl-smtp-posting-user)))
140 (setq smtp-sasl-user-name wl-smtp-posting-user
141 smtp-sasl-properties nil))
142 (setq sasl-read-passphrase
146 (wl-smtp-password-key
148 (car smtp-sasl-mechanisms)
152 (defun wl-draft-insert-date-field ()
154 (insert "Date: " (wl-make-date-string) "\n"))
156 (defun wl-draft-insert-from-field ()
158 ;; Put the "From:" field in unless for some odd reason
159 ;; they put one in themselves.
160 (let* ((login (or user-mail-address (user-login-name)))
161 (fullname (user-full-name)))
162 (cond ((eq mail-from-style 'angles)
163 (insert "From: " fullname)
164 (let ((fullname-start (+ (point-min) (length "From: ")))
165 (fullname-end (point-marker)))
166 (goto-char fullname-start)
167 ;; Look for a character that cannot appear unquoted
168 ;; according to RFC 822.
169 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
172 ;; Quote fullname, escaping specials.
173 (goto-char fullname-start)
175 (while (re-search-forward "[\"\\]"
177 (replace-match "\\\\\\&" t))
179 (insert " <" login ">\n"))
180 ((eq mail-from-style 'parens)
181 (insert "From: " login " (")
182 (let ((fullname-start (point)))
184 (let ((fullname-end (point-marker)))
185 (goto-char fullname-start)
186 ;; RFC 822 says \ and nonmatching parentheses
187 ;; must be escaped in comments.
188 ;; Escape every instance of ()\ ...
189 (while (re-search-forward "[()\\]" fullname-end 1)
190 (replace-match "\\\\\\&" t))
191 ;; ... then undo escaping of matching parentheses,
192 ;; including matching nested parentheses.
193 (goto-char fullname-start)
194 (while (re-search-forward
195 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
197 (replace-match "\\1(\\3)" t)
198 (goto-char fullname-start))))
200 ((not mail-from-style)
201 (insert "From: " login "\n")))))
203 (defun wl-draft-insert-x-face-field ()
204 "Insert X-Face header."
206 (if (not (file-exists-p wl-x-face-file))
207 (error "File %s does not exist" wl-x-face-file)
208 (goto-char (point-min))
209 (search-forward mail-header-separator nil t)
211 (wl-draft-insert-x-face-field-here)
212 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
214 (defun wl-draft-insert-x-face-field-here ()
215 "Insert X-Face field at point."
216 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
217 (when (string-match "^\\(X-Face:\\)?[ \t\n]*" x-face-string)
218 (setq x-face-string (substring x-face-string (match-end 0))))
219 (insert "X-Face: " x-face-string))
220 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
222 ;; Insert X-Face-Version: field
223 (when (and (fboundp 'x-face-insert-version-header)
224 (boundp 'x-face-add-x-face-version-header)
225 x-face-add-x-face-version-header)
226 (x-face-insert-version-header)))
228 (defun wl-draft-setup ()
229 (let ((field wl-draft-fields)
233 (list (cons (concat (car field) " ")
234 (concat (car field) " ")))))
235 (setq field (cdr field)))
237 (cons (cons (concat wl-draft-mime-bcc-field-name ": ")
238 (concat wl-draft-mime-bcc-field-name ": "))
240 (setq wl-draft-field-completion-list cl)
241 (setq wl-address-complete-header-regexp
243 (append wl-address-complete-header-list
244 (list (concat wl-draft-mime-bcc-field-name ":")))))))
246 (defun wl-draft-make-mail-followup-to (recipients)
247 (if (elmo-list-member
248 (or wl-user-mail-address-list
249 (list (wl-address-header-extract-address wl-from)))
251 (let ((rlist (elmo-list-delete
252 (or wl-user-mail-address-list
253 (list (wl-address-header-extract-address wl-from)))
257 (lambda (item) (string= (downcase elem)
260 (if (elmo-list-member rlist (mapcar 'downcase
261 wl-subscribed-mailing-list))
263 (append rlist (list (wl-address-header-extract-address
267 (defun wl-draft-delete-myself-from-cc (to cc)
268 (let ((myself (or wl-user-mail-address-list
269 (list (wl-address-header-extract-address wl-from)))))
270 (cond (wl-draft-always-delete-myself ; always-delete option
271 (elmo-list-delete myself cc
274 (lambda (item) (string= (downcase elem)
277 ((elmo-list-member (append to cc) ; subscribed mailing-list
278 (mapcar 'downcase wl-subscribed-mailing-list))
279 (elmo-list-delete myself cc
282 (lambda (item) (string= (downcase elem)
287 (defun wl-draft-forward (original-subject summary-buf)
288 (let (references parent-folder)
289 (with-current-buffer summary-buf
290 (setq parent-folder (wl-summary-buffer-folder-name)))
291 (with-current-buffer (wl-message-get-original-buffer)
292 (setq references (nconc
293 (std11-field-bodies '("References" "In-Reply-To"))
294 (list (std11-field-body "Message-Id"))))
295 (setq references (delq nil references)
296 references (mapconcat 'identity references " ")
297 references (wl-draft-parse-msg-id-list-string references)
298 references (wl-delete-duplicates references)
299 references (when references
300 (mapconcat 'identity references "\n\t"))))
301 (wl-draft (list (cons 'To "")
303 (concat wl-forward-subject-prefix original-subject))
304 (cons 'References references))
305 nil nil nil nil parent-folder))
306 (goto-char (point-max))
307 (wl-draft-insert-message)
308 (mail-position-on-field "To"))
310 (defun wl-draft-strip-subject-re (subject)
311 "Remove \"Re:\" from subject lines. Shamelessly copied from Gnus."
312 (if (string-match wl-subject-prefix-regexp subject)
313 (substring subject (match-end 0))
316 (defun wl-draft-reply-list-symbol (with-arg)
317 "Return symbol `wl-draft-reply-*-argument-list' match condition.
318 Check WITH-ARG and From: field."
319 (if (wl-address-user-mail-address-p (or (elmo-field-body "From") ""))
321 'wl-draft-reply-myself-with-argument-list
322 'wl-draft-reply-myself-without-argument-list)
324 'wl-draft-reply-with-argument-list
325 'wl-draft-reply-without-argument-list)))
327 (defun wl-draft-reply (buf with-arg summary-buf &optional number)
328 "Reply to BUF buffer message.
329 Reply to author if WITH-ARG is non-nil."
332 to mail-followup-to cc subject in-reply-to references newsgroups
333 to-alist cc-alist decoder parent-folder)
334 (when (buffer-live-p summary-buf)
335 (with-current-buffer summary-buf
336 (setq parent-folder (wl-summary-buffer-folder-name))))
337 (set-buffer (or buf mime-mother-buffer))
338 (setq r-list (symbol-value (wl-draft-reply-list-symbol with-arg)))
341 (when (let ((condition (car (car r-list))))
342 (cond ((stringp condition)
343 (std11-field-body condition))
347 (if (not (std11-field-body (car condition)))
349 (setq condition (cdr condition)))
352 (funcall condition))))
353 (let ((r-to-list (nth 0 (cdr (car r-list))))
354 (r-cc-list (nth 1 (cdr (car r-list))))
355 (r-ng-list (nth 2 (cdr (car r-list)))))
356 (when (and (member "Followup-To" r-ng-list)
357 (string= (std11-field-body "Followup-To") "poster"))
358 (setq r-to-list (cons "From" r-to-list))
359 (setq r-ng-list (delete "Followup-To"
360 (copy-sequence r-ng-list))))
361 (if (and r-to-list (symbolp r-to-list))
362 (setq to (wl-concat-list (funcall r-to-list) ","))
363 (setq to (wl-concat-list (cons to
364 (elmo-multiple-fields-body-list
367 (if (and r-cc-list (symbolp r-cc-list))
368 (setq cc (wl-concat-list (funcall r-to-list) ","))
369 (setq cc (wl-concat-list (cons cc
370 (elmo-multiple-fields-body-list
373 (if (and r-ng-list (symbolp r-ng-list))
374 (setq newsgroups (wl-concat-list (funcall r-ng-list) ","))
375 (setq newsgroups (wl-concat-list (cons newsgroups
380 (setq r-list (cdr r-list)))
381 (error "No match field: check your `%s'"
382 (symbol-name (wl-draft-reply-list-symbol with-arg))))
383 (setq subject (std11-field-body "Subject"))
384 (setq to (wl-parse-addresses to)
385 cc (wl-parse-addresses cc))
386 (with-temp-buffer ; to keep raw buffer unibyte.
387 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
388 (setq decoder (mime-find-field-decoder 'Subject 'plain))
389 (setq subject (if (and subject decoder)
390 (funcall decoder subject) subject))
394 (setq decoder (mime-find-field-decoder 'To 'plain))
395 (cons (nth 1 (std11-extract-address-components addr))
396 (if decoder (funcall decoder addr) addr)))
401 (setq decoder (mime-find-field-decoder 'Cc 'plain))
402 (cons (nth 1 (std11-extract-address-components addr))
403 (if decoder (funcall decoder addr) addr)))
405 (and wl-reply-subject-prefix
406 (setq subject (concat wl-reply-subject-prefix
407 (wl-draft-strip-subject-re
409 (setq in-reply-to (std11-field-body "Message-Id"))
410 (setq references (nconc
411 (std11-field-bodies '("References" "In-Reply-To"))
413 (setq to (delq nil (mapcar 'car to-alist)))
414 (setq cc (delq nil (mapcar 'car cc-alist)))
415 ;; if subscribed mailing list is contained in cc or to
416 ;; and myself is contained in cc,
417 ;; delete myself from cc.
418 (setq cc (wl-draft-delete-myself-from-cc to cc))
419 (when wl-insert-mail-followup-to
420 (setq mail-followup-to
421 (wl-draft-make-mail-followup-to (append to cc)))
422 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
423 (with-temp-buffer ; to keep raw buffer unibyte.
424 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
425 (setq newsgroups (wl-parse newsgroups
426 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
427 newsgroups (wl-delete-duplicates newsgroups)
432 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
433 (if decoder (funcall decoder grp) grp))
435 (setq to (wl-delete-duplicates to nil t))
436 (setq cc (wl-delete-duplicates
437 (append (wl-delete-duplicates cc nil t)
438 to (copy-sequence to))
440 (and to (setq to (mapconcat
442 (if wl-draft-reply-use-address-with-full-name
443 (or (cdr (assoc addr to-alist)) addr)
446 (and cc (setq cc (mapconcat
448 (if wl-draft-reply-use-address-with-full-name
449 (or (cdr (assoc addr cc-alist)) addr)
452 (and mail-followup-to
453 (setq mail-followup-to
456 (if wl-draft-reply-use-address-with-full-name
457 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
459 mail-followup-to ",\n\t")))
460 (and (null to) (setq to cc cc nil))
461 (setq references (delq nil references)
462 references (mapconcat 'identity references " ")
463 references (wl-draft-parse-msg-id-list-string references)
464 references (wl-delete-duplicates references)
465 references (if references
466 (mapconcat 'identity references "\n\t")))
467 (wl-draft (list (cons 'To to)
469 (cons 'Newsgroups newsgroups)
470 (cons 'Subject subject)
471 (cons 'In-Reply-To in-reply-to)
472 (cons 'References references)
473 (cons 'Mail-Followup-To mail-followup-to))
474 nil nil nil nil parent-folder)
475 (setq wl-draft-parent-number number)
476 (setq wl-draft-reply-buffer buf))
477 (run-hooks 'wl-reply-hook))
479 (defun wl-draft-add-references ()
480 (wl-draft-add-in-reply-to "References"))
482 (defun wl-draft-add-in-reply-to (&optional alt-field)
483 (let* ((mes-id (save-excursion
484 (set-buffer mail-reply-buffer)
485 (std11-field-body "message-id")))
486 (field (or alt-field "In-Reply-To"))
487 (ref (std11-field-body field))
488 (ref-list nil) (st nil))
489 (when (and mes-id ref)
490 (while (string-match "<[^>]+>" ref st)
492 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
495 (member mes-id ref-list))
499 (when (mail-position-on-field field)
501 (while (looking-at "^[ \t]")
503 (setq mes-id (concat "\t" mes-id "\n")))
507 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
508 &optional ignored-fields)
511 (narrow-to-region (point)(point))
513 (with-current-buffer mail-reply-buffer
515 (decode-mime-charset-region (point-min) (point-max)
517 (buffer-substring-no-properties
518 (point-min) (point-max))))
520 (goto-char (point-min))
521 (wl-draft-delete-fields ignored-fields))
522 (goto-char (point-max))
523 (push-mark (point) nil t)
524 (goto-char (point-min)))
526 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
527 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
528 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
529 (run-hooks 'wl-draft-cited-hook)
530 (when (if wl-draft-add-references
531 (wl-draft-add-references)
532 (if wl-draft-add-in-reply-to
533 (wl-draft-add-in-reply-to)))
534 (wl-highlight-headers 'for-draft)) ; highlight when added References:
535 (when wl-highlight-body-too
536 (wl-highlight-body-region beg (point-max)))))
538 (defun wl-message-news-p ()
539 "If exist valid Newsgroups field, return non-nil."
540 (std11-field-body "Newsgroups"))
542 (defun wl-message-field-exists-p (field)
543 "If FIELD exist and FIELD value is not empty, return non-nil."
544 (let ((value (std11-field-body field)))
546 (not (string= value "")))))
548 (defun wl-message-mail-p ()
549 "If exist To, Cc or Bcc field, return non-nil."
550 (or (wl-message-field-exists-p "To")
551 (wl-message-field-exists-p "Resent-to")
552 (wl-message-field-exists-p "Cc")
553 (wl-message-field-exists-p "Bcc")
554 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
555 ;;; This may be needed..
556 ;;; (wl-message-field-exists-p "Fcc")
559 (defun wl-draft-edit-string (string)
560 (let ((cur-buf (current-buffer))
561 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
562 to subject in-reply-to cc references newsgroups mail-followup-to
563 content-type content-transfer-encoding from
568 (setq to (std11-field-body "To"))
571 (decode-mime-charset-string
574 (setq subject (std11-field-body "Subject"))
575 (setq subject (and subject
577 (decode-mime-charset-string
580 (setq from (std11-field-body "From")
583 (decode-mime-charset-string
586 (setq in-reply-to (std11-field-body "In-Reply-To"))
587 (setq cc (std11-field-body "Cc"))
590 (decode-mime-charset-string
593 (setq references (std11-field-body "References"))
594 (setq newsgroups (std11-field-body "Newsgroups"))
595 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
596 (setq content-type (std11-field-body "Content-Type"))
597 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
598 (goto-char (point-min))
599 (or (re-search-forward "\n\n" nil t)
600 (search-forward (concat mail-header-separator "\n") nil t))
606 (nth 1 (std11-extract-address-components from))
607 wl-user-mail-address-list)
611 (cons 'Subject subject)
612 (cons 'Newsgroups newsgroups)
613 (cons 'Mail-Followup-To mail-followup-to)
614 (cons 'In-Reply-To in-reply-to)
615 (cons 'References references))
616 content-type content-transfer-encoding
617 (buffer-substring (point) (point-max))
619 (and to (mail-position-on-field "To"))
620 (delete-other-windows)
621 (kill-buffer tmp-buf)))
622 (run-hooks 'wl-draft-reedit-hook))
624 (defun wl-draft-insert-current-message (dummy)
626 (let (original-buffer
628 mail-citation-hook mail-yank-hooks
629 wl-draft-add-references wl-draft-add-in-reply-to
630 wl-draft-cite-function)
631 (with-current-buffer wl-draft-buffer-cur-summary-buffer
632 (with-current-buffer wl-message-buffer
633 (setq original-buffer (wl-message-get-original-buffer))
635 (with-current-buffer original-buffer
637 (error "No current message"))))
638 (setq mail-reply-buffer original-buffer)
639 (wl-draft-yank-from-mail-reply-buffer
641 wl-ignored-forwarded-headers)))
643 (defun wl-draft-insert-get-message (dummy)
644 (let ((fld (completing-read
646 (if (memq 'read-folder wl-use-folder-petname)
647 (wl-folder-get-entity-with-petname)
648 wl-folder-entity-hashtb)
649 nil nil wl-default-spec
650 'wl-read-folder-hist))
651 (number (call-interactively
652 (function (lambda (num)
653 (interactive "nNumber: ")
655 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
656 mail-citation-hook mail-yank-hooks
657 wl-draft-cite-function)
660 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
663 (elmo-make-fetch-strategy 'entire)
664 nil mail-reply-buffer)
665 (wl-draft-yank-from-mail-reply-buffer nil))
666 (kill-buffer mail-reply-buffer))))
669 ;; default body citation func
671 (defun wl-default-draft-cite ()
672 (let ((mail-yank-ignored-headers "[^:]+:")
673 (mail-yank-prefix "> ")
674 (summary-buf wl-current-summary-buffer)
675 (message-buf (get-buffer (wl-current-message-buffer)))
676 from date cite-title num entity)
678 (buffer-live-p summary-buf)
680 (buffer-live-p message-buf))
682 (with-current-buffer summary-buf
683 (let ((elmo-mime-charset wl-summary-buffer-mime-charset))
684 (setq num (save-excursion
685 (set-buffer message-buf)
686 wl-message-buffer-cur-number))
687 (setq entity (elmo-msgdb-overview-get-entity
688 num (wl-summary-buffer-msgdb)))
689 (setq date (elmo-msgdb-overview-entity-get-date entity))
690 (setq from (elmo-msgdb-overview-entity-get-from entity))))
691 (setq cite-title (format "At %s,\n%s wrote:"
692 (or date "some time ago")
693 (if wl-default-draft-cite-decorate-author
694 (funcall wl-summary-from-function
698 (insert cite-title "\n"))
699 (mail-indent-citation)))
701 (defvar wl-draft-buffer nil "Draft buffer to yank content")
702 (defun wl-draft-yank-to-draft-buffer (buffer)
703 "Yank BUFFER content to `wl-draft-buffer'."
704 (set-buffer wl-draft-buffer)
705 (let ((mail-reply-buffer buffer))
706 (wl-draft-yank-from-mail-reply-buffer nil)
707 (kill-buffer buffer)))
709 (defun wl-draft-yank-original (&optional arg)
710 "Yank original message."
713 (let (buf mail-reply-buffer)
716 (setq buf (current-buffer)))
717 (setq mail-reply-buffer buf)
718 (wl-draft-yank-from-mail-reply-buffer nil))
719 (wl-draft-yank-current-message-entity)))
721 (defun wl-draft-hide (editing-buffer)
722 "Hide the editing draft buffer if possible."
723 (when (and editing-buffer
724 (buffer-live-p editing-buffer))
725 (set-buffer editing-buffer)
726 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
727 fld-buf sum-win fld-win)
728 (if (and wl-draft-use-frame
729 (> (length (visible-frame-list)) 1))
735 ;; stay folder window if required
736 (when wl-stay-folder-window
737 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
738 (if (setq fld-win (get-buffer-window fld-buf))
739 (select-window fld-win)
740 (if wl-draft-resume-folder-window ;; resume folder window
741 (switch-to-buffer fld-buf)))))
742 (if (buffer-live-p sum-buf)
743 (if (setq sum-win (get-buffer-window sum-buf t))
744 ;; if Summary is on the frame, select it.
745 (select-window sum-win)
746 ;; if summary is not on the frame, switch to it.
747 (if (and wl-stay-folder-window
748 (or wl-draft-resume-folder-window fld-win))
749 (wl-folder-select-buffer sum-buf)
750 (switch-to-buffer sum-buf))))))))
752 (defun wl-draft-delete (editing-buffer)
753 "kill the editing draft buffer and delete the file corresponds to it."
756 (set-buffer editing-buffer)
757 (if wl-draft-buffer-file-name
759 (if (file-exists-p wl-draft-buffer-file-name)
760 (delete-file wl-draft-buffer-file-name))
761 (let ((msg (and wl-draft-buffer-file-name
762 (string-match "[0-9]+$" wl-draft-buffer-file-name)
764 (match-string 0 wl-draft-buffer-file-name)))))
765 (wl-draft-config-info-operation msg 'delete))))
766 (set-buffer-modified-p nil) ; force kill
767 (kill-buffer editing-buffer))))
769 (defun wl-draft-kill (&optional force-kill)
770 "Kill current draft buffer and quit editing."
773 (when (and (or (eq major-mode 'wl-draft-mode)
774 (eq major-mode 'mail-mode))
776 (y-or-n-p "Kill Current Draft? ")))
777 (let ((cur-buf (current-buffer)))
778 (when wl-draft-parent-number
779 (let ((number wl-draft-parent-number))
780 (with-current-buffer wl-draft-buffer-cur-summary-buffer
781 (wl-summary-jump-to-msg number)
782 (elmo-folder-unmark-answered wl-summary-buffer-elmo-folder
784 (wl-summary-update-mark number))))
785 (wl-draft-hide cur-buf)
786 (wl-draft-delete cur-buf)))
789 (defun wl-draft-fcc ()
790 "Add a new Fcc field, with file name completion."
792 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
793 (mail-position-on-field "to"))
796 ;; Imported from message.el.
797 (defun wl-draft-elide-region (b e)
798 "Elide the text in the region.
799 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
803 (insert wl-draft-elide-ellipsis))
805 ;; Imported from message.el.
806 (defun wl-draft-beginning-of-line (&optional n)
807 "Move point to beginning of header value or to beginning of line."
809 (let ((zrs 'zmacs-region-stays))
810 (when (and (interactive-p) (boundp zrs))
812 (if (wl-draft-point-in-header-p)
813 (let* ((here (point))
814 (bol (progn (beginning-of-line n) (point)))
815 (eol (line-end-position))
816 (eoh (and (looking-at "[^ \t]")
817 (re-search-forward ": *" eol t))))
818 (if (and eoh (or (> here eoh) (= here bol)))
821 (beginning-of-line n)))
823 (defun wl-draft-point-in-header-p ()
824 "Return t if point is in the header."
827 (goto-char (point-min))
828 (not (re-search-forward
829 (concat "^" (regexp-quote mail-header-separator) "\n")
832 ;; function for wl-sent-message-via
834 (defmacro wl-draft-sent-message-p (type)
835 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
837 (defmacro wl-draft-set-sent-message (type result &optional server-port)
838 (` (let ((element (assq (, type) wl-sent-message-via)))
840 (unless (eq (nth 1 element) (, result))
841 (setcdr element (list (, result) (, server-port)))
842 (setq wl-sent-message-modified t))
843 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
844 (setq wl-sent-message-modified t)))))
846 (defun wl-draft-sent-message-results ()
847 (let ((results wl-sent-message-via)
848 unplugged-via sent-via)
850 (cond ((eq (nth 1 (car results)) 'unplugged)
851 (push (caar results) unplugged-via))
852 ((eq (nth 1 (car results)) 'sent)
853 (push (caar results) sent-via)))
854 (setq results (cdr results)))
855 (list unplugged-via sent-via)))
857 (defun wl-draft-write-sendlog (status proto server to id)
858 "Write send log file, if `wl-draft-sendlog' is non-nil."
859 (when wl-draft-sendlog
861 (let* ((filename (expand-file-name wl-draft-sendlog-filename
862 elmo-msgdb-directory))
863 (filesize (nth 7 (file-attributes filename)))
864 (server (if server (concat " server=" server) ""))
866 ((memq proto '(fcc queue))
867 (format " folder=\"%s\"" to))
869 (format " ng=<%s>" to))
874 (mapcar '(lambda(x) (format "<%s>" x)) to)
877 (id (if id (concat " id=" id) ""))
878 (time (format-time-string "%Y/%m/%d %T")))
879 (insert (format "%s proto=%s stat=%s%s%s%s\n"
880 time proto status server to id))
881 (if (and wl-draft-sendlog-max-size filesize
882 (> filesize wl-draft-sendlog-max-size))
883 (rename-file filename (concat filename ".old") t))
884 (if (file-writable-p filename)
885 (write-region-as-binary (point-min) (point-max)
887 (message "%s is not writable." filename))))))
889 (defun wl-draft-get-header-delimiter (&optional delete)
890 ;; If DELETE is non-nil, replace the header delimiter with a blank line
892 (goto-char (point-min))
893 (when (re-search-forward
894 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
898 (setq delimline (point-marker)))
901 (defun wl-draft-send-mail-with-qmail ()
902 "Pass the prepared message buffer to qmail-inject.
903 Refer to the documentation for the variable `send-mail-function'
904 to find out how to use this."
905 (if (and wl-draft-qmail-send-plugged
906 (not (elmo-plugged-p)))
907 (wl-draft-set-sent-message 'mail 'unplugged)
909 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
910 (let ((id (std11-field-body "Message-ID"))
911 (to (std11-field-body "To")))
915 'call-process-region 1 (point-max) wl-qmail-inject-program
917 wl-qmail-inject-args))
918 ;; qmail-inject doesn't say anything on it's stdout/stderr,
919 ;; we have to look at the retval instead
921 (wl-draft-set-sent-message 'mail 'sent)
922 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
923 (1 (error "`qmail-inject' reported permanent failure"))
924 (111 (error "`qmail-inject' reported transient failure"))
925 ;; should never happen
926 (t (error "`qmail-inject' reported unknown failure"))))))
928 (defun wl-draft-parse-msg-id-list-string (string)
929 "Get msg-id list from STRING."
931 (dolist (parsed-id (std11-parse-msg-ids-string string))
932 (when (eq (car parsed-id) 'msg-id)
933 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
935 (nreverse msg-id-list)))
937 (defun wl-draft-eword-encode-address-list (string &optional column)
938 "Encode header field STRING as list of address, and return the result.
939 Cause an error when STRING contains invalid address.
940 Optional argument COLUMN is start-position of the field."
941 (car (eword-encode-rword-list
942 (or column eword-encode-default-start-column)
943 (eword-encode-addresses-to-rword-list
944 (wl-draft-std11-parse-addresses (std11-lexical-analyze string))))))
946 (defun wl-draft-std11-parse-addresses (lal)
947 (let ((ret (std11-parse-address lal)))
948 (when (and (not (and (eq (length lal) 1)
949 (eq (car (car lal)) 'spaces)))
951 (error "Error while parsing address"))
953 (let ((dest (list (car ret))))
955 (while (and (setq ret (std11-parse-ascii-token lal))
956 (string-equal (cdr (assq 'specials (car ret))) ",")
957 (setq ret (std11-parse-address (cdr ret)))
959 (setq dest (cons (car ret) dest))
960 (setq lal (cdr ret)))
961 (while (eq 'spaces (car (car lal)))
962 (setq lal (cdr lal)))
963 (if lal (error "Error while parsing address"))
966 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
967 "Get mailbox list of FIELD from current buffer.
968 The buffer is expected to be narrowed to just the headers of the message.
969 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
970 from current buffer."
972 (let ((case-fold-search t)
973 (inhibit-read-only t)
975 mailbox-list beg seq has-group-list)
976 (goto-char (point-min))
977 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
980 (re-search-forward "^[^ \t]" nil 'move)
982 (skip-chars-backward "\n")
983 (setq seq (std11-lexical-analyze
984 (buffer-substring-no-properties beg (point))))
985 (setq addresses (wl-draft-std11-parse-addresses seq))
987 (cond ((eq (car (car addresses)) 'group)
988 (setq has-group-list t)
992 'std11-address-string
993 (nth 2 (car addresses))))))
994 ((eq (car (car addresses)) 'mailbox)
995 (setq address (nth 1 (car addresses)))
999 (std11-addr-to-string
1000 (if (eq (car address) 'phrase-route-addr)
1002 (cdr address))))))))
1003 (setq addresses (cdr addresses)))
1004 (when (and remove-group-list has-group-list)
1005 (delete-region beg (point))
1006 (insert (wl-address-string-without-group-list-contents seq))))
1009 (defun wl-draft-deduce-address-list (buffer header-start header-end)
1010 "Get address list suitable for smtp RCPT TO:<address>.
1011 Group list content is removed if `wl-draft-remove-group-list-contents' is
1013 (let ((fields (if (and wl-draft-doing-mime-bcc
1014 wl-draft-disable-bcc-for-mime-bcc)
1016 '("to" "cc" "bcc")))
1017 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
1018 (case-fold-search t)
1022 (narrow-to-region header-start header-end)
1023 (goto-char (point-min))
1025 (if (re-search-forward "^resent-to[\t ]*:" nil t)
1026 (setq fields resent-fields)))
1030 (wl-draft-parse-mailbox-list
1032 wl-draft-remove-group-list-contents)))
1033 (setq fields (cdr fields)))
1039 (defun wl-draft-send-mail-with-smtp ()
1040 "Send the prepared message buffer with SMTP."
1042 (let* ((errbuf (if mail-interactive
1043 (generate-new-buffer " smtp errors")
1045 (case-fold-search t)
1046 (default-case-fold-search t)
1047 (sender (or wl-envelope-from
1048 (wl-address-header-extract-address wl-from)))
1049 (delimline (save-excursion
1050 (goto-char (point-min))
1052 (concat "^" (regexp-quote mail-header-separator)
1056 (or wl-smtp-posting-server smtp-server "localhost"))
1057 (smtp-service (or wl-smtp-posting-port smtp-service))
1058 (smtp-local-domain (or smtp-local-domain wl-local-domain))
1059 (id (std11-field-body "message-id"))
1061 (if (not (elmo-plugged-p smtp-server smtp-service))
1062 (wl-draft-set-sent-message 'mail 'unplugged
1063 (cons smtp-server smtp-service))
1066 ;; Instead of `smtp-deduce-address-list'.
1067 (setq recipients (wl-draft-deduce-address-list
1068 (current-buffer) (point-min) delimline))
1069 (unless recipients (error "No recipients"))
1070 ;; Insert an extra newline if we need it to work around
1071 ;; Sun's bug that swallows newlines.
1072 (goto-char (1+ delimline))
1073 (if (eval mail-mailer-swallows-blank-line)
1075 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1076 (if mail-interactive
1080 (wl-draft-delete-field "bcc" delimline)
1081 (wl-draft-delete-field "resent-bcc" delimline)
1082 (let (process-connection-type)
1085 (wl-smtp-extension-bind
1087 (smtp-send-buffer sender recipients (current-buffer))
1089 (wl-draft-write-sendlog 'failed 'smtp smtp-server
1091 (if (and (eq (car err) 'smtp-response-error)
1092 (/= (nth 1 err) 334))
1094 (wl-smtp-password-key
1096 (car smtp-sasl-mechanisms)
1098 (signal (car err) (cdr err)))))
1099 (wl-draft-set-sent-message 'mail 'sent)
1100 (wl-draft-write-sendlog
1101 'ok 'smtp smtp-server recipients id)))))
1102 (if (bufferp errbuf)
1103 (kill-buffer errbuf))))))
1105 (defun wl-draft-send-mail-with-pop-before-smtp ()
1106 "Send the prepared message buffer with POP-before-SMTP."
1107 (require 'elmo-pop3)
1111 :user (or wl-pop-before-smtp-user
1112 elmo-pop3-default-user)
1113 :server (or wl-pop-before-smtp-server
1114 elmo-pop3-default-server)
1115 :port (or wl-pop-before-smtp-port
1116 elmo-pop3-default-port)
1117 :auth (or wl-pop-before-smtp-authenticate-type
1118 elmo-pop3-default-authenticate-type)
1119 :stream-type (or wl-pop-before-smtp-stream-type
1120 elmo-pop3-default-stream-type))))
1121 (condition-case error
1123 (elmo-pop3-get-session session)
1124 (when session (elmo-network-close-session session)))
1126 (elmo-network-close-session session)
1127 (unless (string= (nth 1 error) "Unplugged")
1128 (signal (car error)(cdr error))))))
1129 (wl-draft-send-mail-with-smtp))
1131 (defun wl-draft-insert-required-fields (&optional force-msgid)
1132 "Insert Message-ID, Date, and From field.
1133 If FORCE-MSGID, insert message-id regardless of `wl-insert-message-id'."
1134 ;; Insert Message-Id field...
1135 (goto-char (point-min))
1136 (when (and (or force-msgid
1137 wl-insert-message-id)
1138 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1139 (insert (concat "Message-ID: "
1140 (funcall wl-message-id-function)
1142 ;; Insert date field.
1143 (goto-char (point-min))
1144 (or (re-search-forward "^Date[ \t]*:" nil t)
1145 (wl-draft-insert-date-field))
1146 ;; Insert from field.
1147 (goto-char (point-min))
1148 (or (re-search-forward "^From[ \t]*:" nil t)
1149 (wl-draft-insert-from-field)))
1151 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1152 "Send the message in the current buffer."
1154 (std11-narrow-to-header mail-header-separator)
1155 (wl-draft-insert-required-fields)
1156 ;; Delete null fields.
1157 (goto-char (point-min))
1158 (while (re-search-forward "^[^ \t\n:]+:[ \t]*\n" nil t)
1160 ;; ignore any blank lines in the header
1161 (while (re-search-forward "\n\n\n*" nil t)
1162 (replace-match "\n")))
1163 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1164 (wl-draft-dispatch-message)
1165 (when kill-when-done
1166 ;; hide editing-buffer.
1167 (wl-draft-hide editing-buffer)
1168 ;; delete editing-buffer and its file.
1169 (wl-draft-delete editing-buffer)))
1171 (defun wl-draft-dispatch-message (&optional mes-string)
1172 "Send the message in the current buffer. Not modified the header fields."
1173 (let (delimline mime-bcc)
1174 (if (and wl-draft-verbose-send mes-string)
1175 (message mes-string))
1177 (setq delimline (wl-draft-get-header-delimiter t))
1178 (unless wl-draft-fcc-list
1179 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1181 (setq wl-sent-message-modified nil)
1184 (if (and (wl-message-mail-p)
1185 (not (wl-draft-sent-message-p 'mail)))
1186 (if (or (not (or wl-draft-force-queuing
1187 wl-draft-force-queuing-mail))
1188 (memq 'mail wl-sent-message-queued))
1190 (setq mime-bcc (wl-draft-mime-bcc-field))
1191 (funcall wl-draft-send-mail-function)
1192 (when (not (zerop (length mime-bcc)))
1193 (wl-draft-do-mime-bcc mime-bcc)))
1194 (push 'mail wl-sent-message-queued)
1195 (wl-draft-set-sent-message 'mail 'unplugged)))
1196 (if (and (wl-message-news-p)
1197 (not (wl-draft-sent-message-p 'news))
1198 (not (wl-message-field-exists-p "Resent-to")))
1199 (if (or (not (or wl-draft-force-queuing
1200 wl-draft-force-queuing-news))
1201 (memq 'news wl-sent-message-queued))
1202 (funcall wl-draft-send-news-function)
1203 (push 'news wl-sent-message-queued)
1204 (wl-draft-set-sent-message 'news 'unplugged))))
1205 (let* ((status (wl-draft-sent-message-results))
1206 (unplugged-via (car status))
1207 (sent-via (nth 1 status)))
1208 ;; If one sent, process fcc folder.
1209 (if (and sent-via wl-draft-fcc-list)
1211 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1213 (setq wl-draft-fcc-list nil)))
1214 (if wl-draft-use-cache
1215 (let ((id (std11-field-body "Message-ID"))
1216 (elmo-enable-disconnected-operation t))
1217 (elmo-file-cache-save (elmo-file-cache-get-path id)
1219 ;; If one unplugged, append queue.
1220 (when (and unplugged-via
1221 wl-sent-message-modified)
1222 (if wl-draft-enable-queuing
1224 (wl-draft-queue-append wl-sent-message-via)
1225 (setq wl-sent-message-modified 'requeue))
1226 (error "Unplugged")))
1227 (when wl-draft-verbose-send
1228 (if (and unplugged-via sent-via);; combined message
1230 (setq wl-draft-verbose-msg
1231 (format "Sending%s and Queuing%s..."
1232 sent-via unplugged-via))
1233 (message (concat wl-draft-verbose-msg "done")))
1235 (message (concat mes-string
1236 (if sent-via "done" "failed")))))))))
1237 (not wl-sent-message-modified)) ;; return value
1239 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1240 "Force send current buffer as raw message."
1243 (let (wl-interactive-send
1244 ;;; wl-draft-verbose-send
1245 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1246 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1249 (wl-draft-send kill-when-done mes-string))))
1251 (defun wl-draft-clone-local-variables ()
1252 (let ((locals (buffer-local-variables))
1255 (when (and (consp (car locals))
1257 (string-match wl-draft-clone-local-variable-regexp
1258 (symbol-name (car (car locals)))))
1259 (wl-append result (list (car (car locals)))))
1260 (setq locals (cdr locals)))
1263 (defun wl-draft-send (&optional kill-when-done mes-string)
1264 "Send current draft message.
1265 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1267 ;; Don't call this explicitly.
1268 ;; Added to 'wl-draft-send-hook (by teranisi)
1269 ;; (wl-draft-config-exec)
1270 (run-hooks 'wl-draft-send-hook)
1271 (when (or (not wl-interactive-send)
1272 (y-or-n-p "Do you really want to send current draft? "))
1273 (let ((send-mail-function 'wl-draft-raw-send)
1274 (editing-buffer (current-buffer))
1275 (sending-buffer (wl-draft-generate-clone-buffer
1276 " *wl-draft-sending-buffer*"
1277 (append wl-draft-config-variables
1278 (wl-draft-clone-local-variables))))
1279 (wl-draft-verbose-msg nil)
1282 (save-excursion (set-buffer sending-buffer)
1283 (if (and (not (wl-message-mail-p))
1284 (not (wl-message-news-p)))
1285 (error "No recipient is specified"))
1286 (expand-abbrev) ; for mail-abbrevs
1287 (let ((mime-header-encode-method-alist
1289 '((wl-draft-eword-encode-address-list
1290 . (To Cc Bcc Resent-To Resent-Cc Bcc Resent-Bcc)))
1291 (if (boundp 'mime-header-encode-method-alist)
1292 (symbol-value 'mime-header-encode-method-alist)))))
1293 (run-hooks 'mail-send-hook) ; translate buffer
1296 (if wl-draft-verbose-send
1297 (message (or mes-string "Sending...")))
1298 (funcall wl-draft-send-function editing-buffer kill-when-done)
1299 ;; Now perform actions on successful sending.
1300 (while mail-send-actions
1302 (apply (car (car mail-send-actions))
1303 (cdr (car mail-send-actions)))
1305 (setq mail-send-actions (cdr mail-send-actions)))
1306 (if wl-draft-verbose-send
1307 (message (concat (or wl-draft-verbose-msg
1308 mes-string "Sending...")
1310 ;; kill sending buffer, anyway.
1311 (and (buffer-live-p sending-buffer)
1312 (kill-buffer sending-buffer))))))
1314 (defun wl-draft-mime-bcc-field ()
1315 "Return the MIME-Bcc field body. The field is deleted."
1316 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1317 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1319 (defun wl-draft-do-mime-bcc (field-body)
1320 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1321 (let ((orig-from (mime-decode-field-body (std11-field-body "from")
1323 (orig-subj (mime-decode-field-body (or (std11-field-body "subject")
1326 (recipients (wl-parse-addresses field-body))
1327 (draft-buffer (current-buffer))
1329 (save-window-excursion
1330 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1331 (not (zerop (length field-body))))
1332 (let ((wl-draft-doing-mime-bcc t))
1333 (dolist (recipient recipients)
1334 (wl-draft-create-buffer)
1335 (wl-draft-create-contents
1336 (append `((From . ,orig-from)
1338 (Subject . ,(concat "A blind carbon copy ("
1341 (wl-draft-default-headers)))
1342 (wl-draft-insert-mail-header-separator)
1343 (wl-draft-prepare-edit)
1344 (goto-char (point-max))
1345 (insert (or wl-draft-mime-bcc-body
1346 "This is a blind carbon copy.")
1348 (mime-edit-insert-tag "message" "rfc822")
1349 (insert-buffer draft-buffer)
1350 (let (wl-interactive-send)
1351 (wl-draft-send 'kill-when-done))))))))
1353 ;; Derived from `message-save-drafts' in T-gnus.
1354 (defun wl-draft-save ()
1355 "Save current draft."
1357 (if (buffer-modified-p)
1359 (message "Saving %s..." wl-draft-buffer-file-name)
1360 (let ((msg (buffer-substring-no-properties (point-min) (point-max))))
1361 (with-temp-file wl-draft-buffer-file-name
1363 ;; If no header separator, insert it.
1365 (goto-char (point-min))
1366 (unless (re-search-forward
1367 (concat "^" (regexp-quote mail-header-separator) "$")
1369 (goto-char (point-min))
1370 (if (re-search-forward "\n\n" nil t)
1371 (replace-match (concat "\n" mail-header-separator "\n"))
1372 (goto-char (point-max))
1373 (insert (if (eq (char-before) ?\n) "" "\n")
1374 mail-header-separator "\n"))))
1375 (let ((mime-header-encode-method-alist
1376 '((eword-encode-unstructured-field-body))))
1377 (mime-edit-translate-buffer))
1378 (wl-draft-get-header-delimiter t)))
1379 (set-buffer-modified-p nil)
1380 (wl-draft-config-info-operation
1381 (and (string-match "[0-9]+$" wl-draft-buffer-file-name)
1383 (match-string 0 wl-draft-buffer-file-name)))
1385 (message "Saving %s...done" wl-draft-buffer-file-name))
1386 (message "(No changes need to be saved)")))
1388 (defun wl-draft-mimic-kill-buffer ()
1389 "Kill the current (draft) buffer with query."
1391 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1394 (if (or (not bufname)
1395 (string-equal bufname "")
1396 (string-equal bufname (buffer-name)))
1397 (let ((bufname (current-buffer)))
1398 (when (or (not (buffer-modified-p))
1400 (format "Buffer %s modified; kill anyway? " bufname)))
1401 (set-buffer-modified-p nil)
1402 (wl-draft-hide bufname)
1403 (kill-buffer bufname)))
1404 (kill-buffer bufname))))
1406 (defun wl-draft-save-and-exit ()
1407 "Save current draft and exit current draft mode."
1410 (let ((editing-buffer (current-buffer)))
1411 (wl-draft-hide editing-buffer)
1412 (kill-buffer editing-buffer)))
1414 (defun wl-draft-send-and-exit ()
1415 "Send current draft message and kill it."
1419 (defun wl-draft-send-from-toolbar ()
1421 (let ((wl-interactive-send t))
1422 (wl-draft-send-and-exit)))
1424 (defun wl-draft-delete-field (field &optional delimline replace)
1425 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1427 (defun wl-draft-delete-fields (field &optional delimline replace)
1430 (goto-char (point-min))
1431 (if (search-forward "\n\n" nil t)
1432 (setq delimline (point))
1433 (setq delimline (point-max))))
1434 (narrow-to-region (point-min) delimline)
1435 (goto-char (point-min))
1436 (let ((regexp (concat "^" field ":"))
1437 (case-fold-search t))
1439 (if (looking-at regexp)
1445 (if (re-search-forward "^[^ \t]" nil t)
1446 (goto-char (match-beginning 0))
1449 (insert (concat field ": " replace "\n"))))
1451 (if (re-search-forward "^[^ \t]" nil t)
1452 (goto-char (match-beginning 0))
1455 (defun wl-draft-get-fcc-list (header-end)
1456 (if (and wl-draft-doing-mime-bcc
1457 wl-draft-disable-fcc-for-mime-bcc)
1459 (wl-draft-delete-field "fcc")
1462 (case-fold-search t))
1463 (or (markerp header-end) (error "HEADER-END must be a marker"))
1465 (goto-char (point-min))
1466 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1471 (buffer-substring-no-properties
1475 (skip-chars-backward " \t")
1478 (dolist (folder fcc-list)
1479 (wl-folder-confirm-existence
1480 (wl-folder-get-elmo-folder (eword-decode-string folder)))))
1481 (delete-region (match-beginning 0)
1482 (progn (forward-line 1) (point)))))
1485 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1486 (let ((send-mail-buffer (current-buffer))
1487 (tembuf (generate-new-buffer " fcc output"))
1488 (case-fold-search t)
1490 (or (markerp header-end) (error "HEADER-END must be a marker"))
1493 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1496 ;; insert just the headers to avoid moving the gap more than
1497 ;; necessary (the message body could be arbitrarily huge.)
1498 (insert-buffer-substring send-mail-buffer 1 header-end)
1499 (wl-draft-insert-required-fields t)
1500 (goto-char (point-max))
1501 (insert-buffer-substring send-mail-buffer header-end)
1502 (let ((id (std11-field-body "Message-ID"))
1503 (elmo-enable-disconnected-operation t)
1506 (unless (or cache-saved
1507 (elmo-folder-plugged-p
1508 (wl-folder-get-elmo-folder (car fcc-list))))
1509 (elmo-file-cache-save id nil) ;; for disconnected operation
1510 (setq cache-saved t))
1511 (if (elmo-folder-append-buffer
1512 (wl-folder-get-elmo-folder
1513 (eword-decode-string (car fcc-list)))
1514 (not wl-fcc-force-as-read))
1515 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1516 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1517 (setq fcc-list (cdr fcc-list)))))
1518 (kill-buffer tembuf)))
1520 (defun wl-draft-on-field-p ()
1523 (goto-char (point-min))
1524 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1531 (if (or (looking-at ".*,[ \t]?$")
1532 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1534 (let ((pos (point)))
1537 (if (looking-at "^[ \t]")
1539 (if (re-search-forward ":" pos t) nil t)))))))
1543 (defun wl-draft (&optional header-alist
1544 content-type content-transfer-encoding
1547 "Write and send mail/news message with Wanderlust."
1554 (wl-plugged-init t))
1556 (wl-init)) ; returns immediately if already initialized.
1559 (let (buf-name header-alist-internal)
1561 (wl-draft-create-buffer
1563 (eq this-command 'wl-draft)
1564 (eq this-command 'wl-summary-write)
1565 (eq this-command 'wl-summary-write-current-folder)
1566 (eq this-command 'wl-folder-write-current-folder))
1569 (unless (cdr (assq 'From header-alist))
1571 (append (list (cons 'From wl-from)) header-alist)))
1572 (unless (cdr (assq 'To header-alist))
1577 (if (assq 'To header-alist)
1578 (setcdr (assq 'To header-alist) to)
1580 (append header-alist
1581 (list (cons 'To to))))))))
1582 (unless (cdr (assq 'Subject header-alist))
1583 (if (assq 'Subject header-alist)
1584 (setcdr (assq 'Subject header-alist) "")
1586 (append header-alist (list (cons 'Subject ""))))))
1587 (setq header-alist (append header-alist
1588 (wl-draft-default-headers)
1589 wl-draft-additional-header-alist
1590 (if body (list "" (cons 'Body body)))))
1591 (wl-draft-create-contents header-alist)
1593 (wl-draft-decode-body
1594 content-type content-transfer-encoding))
1595 (wl-draft-insert-mail-header-separator)
1596 (wl-draft-prepare-edit)
1598 (run-hooks 'wl-mail-setup-hook))
1599 (goto-char (point-min))
1600 (wl-user-agent-compose-internal) ;; user-agent
1603 (string= (cdr (assq 'To header-alist)) ""))
1604 (mail-position-on-field "To"))
1606 (goto-char (point-max))))
1609 (defun wl-draft-create-buffer (&optional full parent-folder)
1610 (let* ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1611 (parent-folder (or parent-folder (wl-summary-buffer-folder-name)))
1612 (summary-buf (wl-summary-get-buffer parent-folder))
1613 buf-name file-name num change-major-mode-hook)
1614 (if (not (elmo-folder-message-file-p draft-folder))
1615 (error "%s folder cannot be used for draft folder" wl-draft-folder))
1616 (setq num (elmo-max-of-list
1617 (or (elmo-folder-list-messages draft-folder) '(0))))
1618 (setq num (+ 1 num))
1619 ;; To get unused buffer name.
1620 (while (get-buffer (concat wl-draft-folder "/" (int-to-string num)))
1621 (setq num (+ 1 num)))
1622 (setq buf-name (find-file-noselect
1624 (elmo-message-file-name
1625 (wl-folder-get-elmo-folder wl-draft-folder)
1627 (if wl-draft-use-frame
1628 (switch-to-buffer-other-frame buf-name)
1629 (switch-to-buffer buf-name))
1630 (set-buffer buf-name)
1631 (if (not (string-match (regexp-quote wl-draft-folder)
1633 (rename-buffer (concat wl-draft-folder "/" (int-to-string num))))
1634 (if (or (eq wl-draft-reply-buffer-style 'full)
1636 (delete-other-windows))
1639 (make-local-variable 'truncate-partial-width-windows)
1640 (setq truncate-partial-width-windows nil)
1641 (setq truncate-lines wl-draft-truncate-lines)
1642 (setq wl-sent-message-via nil)
1643 (setq wl-sent-message-queued nil)
1644 (setq wl-draft-buffer-file-name file-name)
1645 (setq wl-draft-config-exec-flag t)
1646 (setq wl-draft-parent-folder (or parent-folder ""))
1647 (or (eq this-command 'wl-folder-write-current-folder)
1648 (setq wl-draft-buffer-cur-summary-buffer summary-buf))
1651 (defun wl-draft-create-contents (header-alist)
1652 "header-alist' sample
1653 '(function ;; funcall
1654 string ;; insert string
1655 (symbol . string) ;; insert symbol-value: string
1656 (symbol . function) ;; (funcall) and if it returns string,
1657 ;; insert symbol-value: string
1658 (symbol . nil) ;; do nothing
1662 (unless (eq major-mode 'wl-draft-mode)
1663 (error "wl-draft-create-header must be use in wl-draft-mode."))
1664 (let ((halist header-alist)
1669 ((functionp (car halist)) (funcall (car halist)))
1671 ((stringp (car halist)) (insert (car halist) "\n"))
1673 ((consp (car halist))
1674 (setq field (car (car halist)))
1675 (setq value (cdr (car halist)))
1679 ((eq field 'Body) ; body
1681 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1683 (let ((value-return (funcall value)))
1684 (when (stringp value-return)
1685 (insert (symbol-name field) ": " value-return "\n"))))
1694 (setq halist (cdr halist)))))
1696 (defun wl-draft-prepare-edit ()
1697 (unless (eq major-mode 'wl-draft-mode)
1698 (error "wl-draft-create-header must be use in wl-draft-mode."))
1699 (let (change-major-mode-hook)
1700 (wl-draft-editor-mode)
1701 (add-hook 'local-write-file-hooks 'wl-draft-save)
1702 (wl-draft-overload-functions)
1703 (wl-highlight-headers 'for-draft)
1705 (clear-visited-file-modtime)))
1707 (defun wl-draft-decode-header ()
1709 (std11-narrow-to-header)
1710 (wl-draft-decode-message-in-buffer)
1713 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1716 (std11-field-body "content-type")))
1717 (content-transfer-encoding
1718 (or content-transfer-encoding
1719 (std11-field-body "content-transfer-encoding")))
1722 (std11-narrow-to-header)
1723 (wl-draft-delete-field "content-type")
1724 (wl-draft-delete-field "content-transfer-encoding")
1725 (goto-char (point-max))
1726 (setq delimline (point-marker))
1728 (narrow-to-region delimline (point-max))
1729 (goto-char (point-min))
1731 (insert "Content-type: " content-type "\n"))
1732 (when content-transfer-encoding
1733 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1734 (wl-draft-decode-message-in-buffer)
1735 (goto-char (point-min))
1736 (unless (re-search-forward "^$" (point-at-eol) t)
1741 ;;; subroutine for wl-draft-create-contents
1742 ;;; must be used in wl-draft-mode
1743 (defun wl-draft-check-new-line ()
1744 (if (not (= (preceding-char) ?\n))
1747 (defsubst wl-draft-trim-ccs (cc)
1753 (null (and wl-draft-delete-myself-from-bcc-fcc
1755 (mapcar 'wl-address-header-extract-address
1757 (wl-parse-addresses (std11-field-body "To"))
1758 (wl-parse-addresses (std11-field-body "Cc"))))
1759 (mapcar 'downcase wl-subscribed-mailing-list)))))
1763 (defsubst wl-draft-default-headers ()
1765 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1766 (wl-address-header-extract-address
1768 (cons 'User-Agent wl-generate-mailer-string-function)
1769 (cons 'Reply-To mail-default-reply-to)
1770 (cons 'Bcc (function
1773 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1774 (cons 'Fcc (function
1776 (wl-draft-trim-ccs wl-fcc))))
1777 (cons 'Organization wl-organization)
1778 (and wl-auto-insert-x-face
1779 (file-exists-p wl-x-face-file)
1780 'wl-draft-insert-x-face-field-here) ;; allow nil
1781 mail-default-headers
1782 ;; check \n at th end of line for `mail-default-headers'
1783 'wl-draft-check-new-line
1786 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1789 (goto-char delimline)
1790 (goto-char (point-min))
1791 (if (search-forward "\n\n" nil t)
1792 (delete-backward-char 1)
1793 (goto-char (point-max))))
1794 (wl-draft-check-new-line)
1795 (put-text-property (point)
1797 (insert mail-header-separator "\n")
1799 'category 'mail-header-separator)))
1803 (defun wl-draft-elmo-nntp-send ()
1804 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1805 (elmo-nntp-default-user
1806 (or wl-nntp-posting-user elmo-nntp-default-user))
1807 (elmo-nntp-default-server
1808 (or wl-nntp-posting-server elmo-nntp-default-server))
1809 (elmo-nntp-default-port
1810 (or wl-nntp-posting-port elmo-nntp-default-port))
1811 (elmo-nntp-default-stream-type
1812 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1813 (elmo-nntp-default-function wl-nntp-posting-function)
1815 (if (setq condition (cdr (elmo-string-matched-assoc
1816 (std11-field-body "Newsgroups")
1817 wl-nntp-posting-config-alist)))
1818 (if (stringp condition)
1819 (setq elmo-nntp-default-server condition)
1820 (while (car condition)
1821 (set (intern (format "elmo-nntp-default-%s"
1822 (symbol-name (caar condition))))
1824 (setq condition (cdr condition)))))
1825 (unless elmo-nntp-default-function
1826 (error "wl-draft-nntp-send: posting-function is nil."))
1827 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1828 (wl-draft-set-sent-message 'news 'unplugged
1829 (cons elmo-nntp-default-server
1830 elmo-nntp-default-port))
1831 (funcall elmo-nntp-default-function
1832 elmo-nntp-default-server (current-buffer))
1833 (wl-draft-set-sent-message 'news 'sent)
1834 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1835 (std11-field-body "Newsgroups")
1836 (std11-field-body "Message-ID")))))
1838 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1839 "Generate clone of current buffer named NAME."
1840 (let ((editing-buffer (current-buffer)))
1842 (set-buffer (generate-new-buffer name))
1845 (wl-draft-editor-mode)
1846 (insert-buffer editing-buffer)
1848 (while local-variables
1849 (make-local-variable (car local-variables))
1850 (set (car local-variables)
1852 (set-buffer editing-buffer)
1853 (symbol-value (car local-variables))))
1854 (setq local-variables (cdr local-variables)))
1857 (defun wl-draft-reedit (number)
1858 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1860 buffer file-name change-major-mode-hook)
1861 (setq file-name (elmo-message-file-name draft-folder number))
1862 (unless (file-exists-p file-name)
1863 (error "File %s does not exist" file-name))
1864 (if (setq buffer (get-buffer
1865 (concat wl-draft-folder "/"
1866 (number-to-string number))))
1868 (if wl-draft-use-frame
1869 (switch-to-buffer-other-frame buffer)
1870 (switch-to-buffer buffer))
1871 (set-buffer buffer))
1872 (setq buffer (get-buffer-create (number-to-string number)))
1873 (if wl-draft-use-frame
1874 (switch-to-buffer-other-frame buffer)
1875 (switch-to-buffer buffer))
1877 (setq wl-draft-parent-folder "")
1878 (insert-file-contents-as-binary file-name)
1879 (let((mime-edit-again-ignored-field-regexp
1880 "^\\(Content-.*\\|Mime-Version\\):"))
1881 (wl-draft-decode-message-in-buffer))
1882 (wl-draft-insert-mail-header-separator)
1883 (if (not (string-match (regexp-quote wl-draft-folder)
1885 (rename-buffer (concat wl-draft-folder "/" (buffer-name))))
1888 (setq buffer-file-name file-name)
1889 (make-local-variable 'truncate-partial-width-windows)
1890 (setq truncate-partial-width-windows nil)
1891 (setq truncate-lines wl-draft-truncate-lines)
1892 (setq wl-sent-message-via nil)
1893 (setq wl-sent-message-queued nil)
1894 (setq wl-draft-buffer-file-name file-name)
1895 (wl-draft-config-info-operation number 'load)
1896 (goto-char (point-min))
1897 (wl-draft-overload-functions)
1898 (wl-draft-editor-mode)
1899 (add-hook 'local-write-file-hooks 'wl-draft-save)
1900 (wl-highlight-headers 'for-draft)
1901 (run-hooks 'wl-draft-reedit-hook)
1902 (goto-char (point-max))
1905 (defmacro wl-draft-body-goto-top ()
1907 (goto-char (point-min))
1908 (if (re-search-forward mail-header-separator nil t)
1910 (goto-char (point-max))))))
1912 (defmacro wl-draft-body-goto-bottom ()
1913 (` (goto-char (point-max))))
1915 (defmacro wl-draft-config-body-goto-header ()
1917 (goto-char (point-min))
1918 (if (re-search-forward mail-header-separator nil t)
1920 (goto-char (point-max))))))
1922 (defsubst wl-draft-config-sub-eval-insert (content &optional newline)
1923 (let (content-value)
1925 (stringp (setq content-value (eval content))))
1926 (insert content-value)
1927 (if newline (insert "\n")))))
1929 (defun wl-draft-config-sub-body (content)
1930 (wl-draft-body-goto-top)
1931 (delete-region (point) (point-max))
1932 (wl-draft-config-sub-eval-insert content))
1934 (defun wl-draft-config-sub-top (content)
1935 (wl-draft-body-goto-top)
1936 (wl-draft-config-sub-eval-insert content))
1938 (defun wl-draft-config-sub-bottom (content)
1939 (wl-draft-body-goto-bottom)
1940 (wl-draft-config-sub-eval-insert content))
1942 (defun wl-draft-config-sub-header (content)
1943 (wl-draft-config-body-goto-header)
1944 (wl-draft-config-sub-eval-insert content 'newline))
1946 (defun wl-draft-config-sub-header-top (content)
1947 (goto-char (point-min))
1948 (wl-draft-config-sub-eval-insert content 'newline))
1950 (defun wl-draft-config-sub-part-top (content)
1951 (goto-char (mime-edit-content-beginning))
1952 (wl-draft-config-sub-eval-insert content 'newline))
1954 (defun wl-draft-config-sub-part-bottom (content)
1955 (goto-char (mime-edit-content-end))
1956 (wl-draft-config-sub-eval-insert content 'newline))
1958 (defsubst wl-draft-config-sub-file (content)
1959 (let ((coding-system-for-read wl-cs-autoconv)
1960 (file (expand-file-name (eval content))))
1961 (if (file-exists-p file)
1962 (insert-file-contents file)
1963 (error "%s: no exists file" file))))
1965 (defun wl-draft-config-sub-body-file (content)
1966 (wl-draft-body-goto-top)
1967 (delete-region (point) (point-max))
1968 (wl-draft-config-sub-file content))
1970 (defun wl-draft-config-sub-top-file (content)
1971 (wl-draft-body-goto-top)
1972 (wl-draft-config-sub-file content))
1974 (defun wl-draft-config-sub-bottom-file (content)
1975 (wl-draft-body-goto-bottom)
1976 (wl-draft-config-sub-file content))
1978 (defun wl-draft-config-sub-header-file (content)
1979 (wl-draft-config-body-goto-header)
1980 (wl-draft-config-sub-file content))
1982 (defun wl-draft-config-sub-template (content)
1983 (setq wl-draft-config-variables
1984 (wl-template-insert (eval content))))
1986 (defun wl-draft-config-sub-x-face (content)
1987 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
1988 (fboundp 'x-face-insert)) ; x-face.el is installed.
1989 (x-face-insert content)
1990 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
1992 (defsubst wl-draft-config-sub-func (field content)
1994 (if (setq func (assq field wl-draft-config-sub-func-alist))
1995 (let (wl-draft-config-variables)
1996 (funcall (cdr func) content)
1997 ;; for wl-draft-config-sub-template
1998 (cons t wl-draft-config-variables)))))
2000 (defsubst wl-draft-config-exec-sub (clist)
2001 (let (config local-variables)
2003 (setq config (car clist))
2008 (let ((field (car config))
2009 (content (cdr config))
2013 (wl-draft-replace-field field (eval content) t))
2014 ((setq ret-val (wl-draft-config-sub-func field content))
2015 (if (cdr ret-val) ;; for wl-draft-config-sub-template
2016 (wl-append local-variables (cdr ret-val))))
2017 ((boundp field) ;; variable
2018 (make-local-variable field)
2019 (set field (eval content))
2020 (wl-append local-variables (list field)))
2022 (error "%s: not variable" field)))))
2024 (error "%s: not supported type" config)))
2025 (setq clist (cdr clist)))
2028 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
2029 "Change headers in draft preparation time."
2031 (unless wl-draft-reedit
2034 (and (boundp 'wl-draft-prepared-config-alist)
2035 wl-draft-prepared-config-alist) ;; For compatible.
2036 wl-draft-config-alist)))
2038 (wl-draft-config-exec config-alist reply-buf)))))
2040 (defun wl-draft-config-exec (&optional config-alist reply-buf)
2041 "Change headers according to the value of `wl-draft-config-alist'.
2042 Automatically applied in draft sending time."
2044 (let ((case-fold-search t)
2045 (alist (or config-alist wl-draft-config-alist))
2046 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2047 wl-draft-reply-buffer)))
2048 (local-variables wl-draft-config-variables)
2050 (when (and (or (interactive-p)
2051 wl-draft-config-exec-flag)
2056 (setq key (caar alist)
2063 (set-buffer reply-buf)
2065 (std11-narrow-to-header)
2066 (goto-char (point-min))
2067 (re-search-forward (car clist) nil t))))
2068 (wl-draft-config-exec-sub (cdr clist))
2071 (when (save-restriction
2072 (std11-narrow-to-header mail-header-separator)
2073 (goto-char (point-min))
2074 (re-search-forward key nil t))
2075 (wl-append local-variables
2076 (wl-draft-config-exec-sub clist))
2079 (wl-append local-variables
2080 (wl-draft-config-exec-sub clist))
2082 (if (and found wl-draft-config-matchone)
2084 (setq alist (cdr alist))))
2086 (setq wl-draft-config-exec-flag nil))
2087 (run-hooks 'wl-draft-config-exec-hook)
2088 (put-text-property (point-min)(point-max) 'face nil)
2089 (wl-highlight-message (point-min)(point-max) t)
2090 (setq wl-draft-config-variables
2091 (elmo-uniq-list local-variables))))))
2093 (defun wl-draft-replace-field (field content &optional add)
2096 (let ((case-fold-search t)
2097 (inhibit-read-only t) ;; added by teranisi.
2099 (std11-narrow-to-header mail-header-separator)
2100 (goto-char (point-min))
2101 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2106 (re-search-forward "^[^ \t]" nil 'move)
2108 (skip-chars-backward "\n")
2109 (delete-region beg (point))
2110 (insert " " content))
2115 (re-search-forward "^[^ \t]" nil 'move)
2117 (delete-region beg (point)))
2118 (when (and add content)
2120 (goto-char (point-max))
2121 (insert (concat field ": " content "\n"))))))))
2123 (defun wl-draft-config-info-operation (msg operation)
2124 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-folder-get-elmo-folder
2128 (format "%s-%d" wl-draft-config-save-filename msg)
2130 element alist variable)
2132 ((eq operation 'save)
2133 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2134 (while (setq variable (pop variables))
2135 (when (boundp variable)
2137 (list (cons variable (eval variable))))))
2138 (elmo-object-save filename alist)))
2139 ((eq operation 'load)
2140 (setq alist (elmo-object-load filename))
2141 (while (setq element (pop alist))
2142 (set (make-local-variable (car element)) (cdr element))
2143 (wl-append wl-draft-config-variables (list (car element)))))
2144 ((eq operation 'delete)
2145 (if (file-exists-p filename)
2146 (delete-file filename))))))
2148 (defun wl-draft-queue-info-operation (msg operation
2149 &optional add-sent-message-via)
2150 (let* ((msgdb-dir (elmo-folder-msgdb-path
2151 (wl-folder-get-elmo-folder wl-queue-folder)))
2154 (format "%s-%d" wl-draft-queue-save-filename msg)
2156 element alist variable)
2158 ((eq operation 'save)
2159 (let ((variables (elmo-uniq-list
2160 (append wl-draft-queue-save-variables
2161 wl-draft-config-variables
2162 (list 'wl-draft-fcc-list)))))
2163 (if add-sent-message-via
2165 (push 'wl-sent-message-queued variables)
2166 (push 'wl-sent-message-via variables)))
2167 (while (setq variable (pop variables))
2168 (when (boundp variable)
2170 (list (cons variable (eval variable))))))
2171 (elmo-object-save filename alist)))
2172 ((eq operation 'load)
2173 (setq alist (elmo-object-load filename))
2174 (while (setq element (pop alist))
2175 (set (make-local-variable (car element)) (cdr element))))
2176 ((eq operation 'get-sent-via)
2177 (setq alist (elmo-object-load filename))
2178 (cdr (assq 'wl-sent-message-via alist)))
2179 ((eq operation 'delete)
2180 (if (file-exists-p filename)
2181 (delete-file filename))))))
2183 (defun wl-draft-queue-append (wl-sent-message-via)
2184 (if wl-draft-verbose-send
2185 (message "Queuing..."))
2186 (let ((send-buffer (current-buffer))
2187 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2188 (message-id (std11-field-body "Message-ID")))
2189 (if (elmo-folder-append-buffer folder t)
2191 (wl-draft-queue-info-operation
2192 (car (elmo-folder-status folder))
2193 'save wl-sent-message-via)
2194 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2195 (when wl-draft-verbose-send
2196 (setq wl-draft-verbose-msg "Queuing...")
2197 (message "Queuing...done")))
2198 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2199 (error "Queuing failed"))))
2201 (defun wl-draft-queue-flush ()
2202 "Flush draft queue."
2204 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2206 (elmo-folder-open-internal queue-folder)
2207 (elmo-folder-list-messages queue-folder)))
2210 (wl-draft-queue-flushing t)
2211 msgs failure len buffer msgid sent-via)
2212 ;; get plugged send message
2214 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2217 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2218 (or (not (nth 2 (car sent-via)))
2220 (car (nth 2 (car sent-via)))
2221 (cdr (nth 2 (car sent-via))))))
2222 (wl-append msgs (list (car msgs2)))
2224 (setq sent-via (cdr sent-via))))
2225 (setq msgs2 (cdr msgs2)))
2226 (when (> (setq len (length msgs)) 0)
2227 (if (elmo-y-or-n-p (format
2228 "%d message(s) are in the sending queue. Send now? "
2230 (not elmo-dop-flush-confirm) t)
2233 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2236 ;; reset buffer local variables
2237 (kill-all-local-variables)
2241 (setq wl-sent-message-via nil)
2242 (wl-draft-queue-info-operation (car msgs) 'load)
2243 (elmo-message-fetch queue-folder
2245 (elmo-make-fetch-strategy 'entire)
2246 nil (current-buffer))
2248 (setq failure (funcall
2249 wl-draft-queue-flush-send-function
2250 (format "Sending (%d/%d)..." i len)))
2251 ;;; (wl-draft-raw-send nil nil
2252 ;;; (format "Sending (%d/%d)..." i len))
2254 (elmo-display-error err t)
2258 (if (eq wl-sent-message-modified 'requeue)
2260 (elmo-folder-delete-messages
2261 queue-folder (cons (car msgs) nil))
2262 (wl-draft-queue-info-operation (car msgs) 'delete))
2264 (elmo-folder-delete-messages
2265 queue-folder (cons (car msgs) nil))
2266 (wl-draft-queue-info-operation (car msgs) 'delete)
2267 (setq performed (+ 1 performed))))
2268 (setq msgs (cdr msgs)))
2269 (kill-buffer buffer)
2270 (message "%d message(s) are sent." performed)))
2271 (message "%d message(s) are remained to be sent." len))
2272 (elmo-folder-close queue-folder)
2275 (defun wl-jump-to-draft-buffer (&optional arg)
2276 "Jump to the draft if exists."
2279 (wl-jump-to-draft-folder)
2280 (let ((draft-bufs (wl-collect-draft))
2284 (message "No draft buffer exist."))
2287 (sort (mapcar 'buffer-name draft-bufs)
2288 (function (lambda (a b)
2289 (not (string< a b))))))
2290 (if (setq buf (cdr (member (buffer-name)
2292 (setq buf (car buf))
2293 (setq buf (car draft-bufs)))
2294 (switch-to-buffer buf))))))
2296 (defun wl-jump-to-draft-folder ()
2297 (let ((msgs (reverse (elmo-folder-list-messages (wl-folder-get-elmo-folder
2299 (mybuf (buffer-name))
2302 (message "No draft message exist.")
2303 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2304 (setq msg (cadr (memq
2305 (string-to-int (substring mybuf (match-end 0)))
2307 (or msg (setq msg (car msgs)))
2308 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2309 (switch-to-buffer buf)
2310 (wl-draft-reedit msg)))))
2312 (defun wl-draft-highlight-and-recenter (&optional n)
2314 (if wl-highlight-body-too
2315 (let ((beg (point-min))
2317 (put-text-property beg end 'face nil)
2318 (wl-highlight-message beg end t)))
2321 ;;;; user-agent support by Sen Nagata
2323 ;; this appears to be necessarily global...
2324 (defvar wl-user-agent-compose-p nil)
2325 (defvar wl-user-agent-headers-and-body-alist nil)
2327 ;; this should be a generic function for mail-mode -- i wish there was
2328 ;; something like it in sendmail.el
2329 (defun wl-user-agent-insert-header (header-name header-value)
2330 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2331 ;; it seems like overriding existing headers is acceptable -- should
2332 ;; we provide an option?
2334 ;; plan was: unfold header (might be folded), remove existing value, insert
2336 ;; wl doesn't seem to fold header lines yet anyway :-)
2338 (let ((kill-whole-line t)
2340 (mail-position-on-field (capitalize header-name))
2341 (setq end-of-line (point))
2343 (re-search-forward ":" end-of-line)
2344 (insert (concat " " header-value "\n"))
2347 ;; this should be a generic function for mail-mode -- i wish there was
2348 ;; something like it in sendmail.el
2350 ;; ** haven't dealt w/ case where the body is already set **
2351 (defun wl-user-agent-insert-body (body-text)
2352 "Insert a body of text, BODY-TEXT, into a message."
2353 ;; code defensively... :-P
2354 (goto-char (point-min))
2355 (search-forward mail-header-separator)
2360 (defun wl-user-agent-compose (&optional to subject other-headers continue
2361 switch-function yank-action
2363 "Support the `compose-mail' interface for wl.
2364 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2365 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2366 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2368 (unless (featurep 'wl)
2370 ;; protect these -- to and subject get bound at some point, so it looks
2371 ;; to be necessary to protect the values used w/in
2372 (let ((wl-user-agent-headers-and-body-alist other-headers)
2373 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2374 (wl-draft-reply-buffer-style 'split))
2375 (when (eq switch-function 'switch-to-buffer-other-window)
2376 (when (one-window-p t)
2377 (if (window-minibuffer-p) (other-window 1))
2381 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2384 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2387 (setq wl-user-agent-headers-and-body-alist
2388 (cons (cons "to" to)
2389 wl-user-agent-headers-and-body-alist))))
2391 (if (wl-string-match-assoc "subject"
2392 wl-user-agent-headers-and-body-alist
2395 (wl-string-match-assoc "subject"
2396 wl-user-agent-headers-and-body-alist
2399 (setq wl-user-agent-headers-and-body-alist
2400 (cons (cons "subject" subject)
2401 wl-user-agent-headers-and-body-alist))))
2402 ;; i think this is what we want to use...
2405 ;; tell the hook-function to do its stuff
2406 (setq wl-user-agent-compose-p t)
2407 ;; because to get the hooks working, wl-draft has to think it has
2408 ;; been called interactively
2409 (call-interactively 'wl-draft))
2410 (setq wl-user-agent-compose-p nil))))
2412 (defun wl-user-agent-compose-internal ()
2413 "Manipulate headers and/or a body of a draft message."
2414 ;; being called from wl-user-agent-compose?
2415 (if wl-user-agent-compose-p
2418 (let ((headers wl-user-agent-headers-and-body-alist)
2419 (case-fold-search t))
2422 (if (not (string-match "^body$" (car (car headers))))
2423 (wl-user-agent-insert-header
2424 (car (car headers)) (cdr (car headers)))
2426 (setq headers (cdr headers))))
2427 ;; highlight headers (from wl-draft in wl-draft.el)
2428 (wl-highlight-headers 'for-draft)
2430 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2432 (wl-user-agent-insert-body
2433 (cdr (wl-string-match-assoc
2435 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2439 (product-provide (provide 'wl-draft) (require 'wl-version))
2441 ;;; wl-draft.el ends here