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-config-sub-func-alist
91 '((body . wl-draft-config-sub-body)
92 (top . wl-draft-config-sub-top)
93 (bottom . wl-draft-config-sub-bottom)
94 (header . wl-draft-config-sub-header)
95 (header-top . wl-draft-config-sub-header-top)
96 (header-bottom . wl-draft-config-sub-header)
97 (part-top . wl-draft-config-sub-part-top)
98 (part-bottom . wl-draft-config-sub-part-bottom)
99 (body-file . wl-draft-config-sub-body-file)
100 (top-file . wl-draft-config-sub-top-file)
101 (bottom-file . wl-draft-config-sub-bottom-file)
102 (header-file . wl-draft-config-sub-header-file)
103 (template . wl-draft-config-sub-template)
104 (x-face . wl-draft-config-sub-x-face)))
106 (make-variable-buffer-local 'wl-draft-buffer-file-name)
107 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
108 (make-variable-buffer-local 'wl-draft-config-variables)
109 (make-variable-buffer-local 'wl-draft-config-exec-flag)
110 (make-variable-buffer-local 'wl-sent-message-via)
111 (make-variable-buffer-local 'wl-sent-message-queued)
112 (make-variable-buffer-local 'wl-draft-fcc-list)
113 (make-variable-buffer-local 'wl-draft-reply-buffer)
114 (make-variable-buffer-local 'wl-draft-parent-folder)
116 (defsubst wl-smtp-password-key (user mechanism server)
117 (format "SMTP:%s/%s@%s"
118 user mechanism server))
120 (defmacro wl-smtp-extension-bind (&rest body)
121 (` (let* ((smtp-sasl-mechanisms
122 (if wl-smtp-authenticate-type
124 (if (listp wl-smtp-authenticate-type)
125 wl-smtp-authenticate-type
126 (list wl-smtp-authenticate-type)))))
127 (smtp-use-sasl (and smtp-sasl-mechanisms t))
128 (smtp-use-starttls (eq wl-smtp-connection-type 'starttls))
129 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
130 (if (and (string= (car smtp-sasl-mechanisms) "DIGEST-MD5")
132 (string-match "^\\([^@]*\\)@\\([^@]*\\)"
133 wl-smtp-posting-user))
134 (setq smtp-sasl-user-name (match-string 1 wl-smtp-posting-user)
135 smtp-sasl-properties (list 'realm
136 (match-string 2 wl-smtp-posting-user)))
137 (setq smtp-sasl-user-name wl-smtp-posting-user
138 smtp-sasl-properties nil))
139 (setq sasl-read-passphrase
143 (wl-smtp-password-key
145 (car smtp-sasl-mechanisms)
149 (defun wl-draft-insert-date-field ()
151 (insert "Date: " (wl-make-date-string) "\n"))
153 (defun wl-draft-insert-from-field ()
155 ;; Put the "From:" field in unless for some odd reason
156 ;; they put one in themselves.
157 (let* ((login (or user-mail-address (user-login-name)))
158 (fullname (user-full-name)))
159 (cond ((eq mail-from-style 'angles)
160 (insert "From: " fullname)
161 (let ((fullname-start (+ (point-min) (length "From: ")))
162 (fullname-end (point-marker)))
163 (goto-char fullname-start)
164 ;; Look for a character that cannot appear unquoted
165 ;; according to RFC 822.
166 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
169 ;; Quote fullname, escaping specials.
170 (goto-char fullname-start)
172 (while (re-search-forward "[\"\\]"
174 (replace-match "\\\\\\&" t))
176 (insert " <" login ">\n"))
177 ((eq mail-from-style 'parens)
178 (insert "From: " login " (")
179 (let ((fullname-start (point)))
181 (let ((fullname-end (point-marker)))
182 (goto-char fullname-start)
183 ;; RFC 822 says \ and nonmatching parentheses
184 ;; must be escaped in comments.
185 ;; Escape every instance of ()\ ...
186 (while (re-search-forward "[()\\]" fullname-end 1)
187 (replace-match "\\\\\\&" t))
188 ;; ... then undo escaping of matching parentheses,
189 ;; including matching nested parentheses.
190 (goto-char fullname-start)
191 (while (re-search-forward
192 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
194 (replace-match "\\1(\\3)" t)
195 (goto-char fullname-start))))
197 ((not mail-from-style)
198 (insert "From: " login "\n")))))
200 (defun wl-draft-insert-x-face-field ()
201 "Insert X-Face header."
203 (if (not (file-exists-p wl-x-face-file))
204 (error "File %s does not exist" wl-x-face-file)
205 (goto-char (point-min))
206 (search-forward mail-header-separator nil t)
208 (wl-draft-insert-x-face-field-here)
209 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
211 (defun wl-draft-insert-x-face-field-here ()
212 "Insert X-Face field at point."
213 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
214 (when (string-match "^\\(X-Face:\\)?[ \t\n]*" x-face-string)
215 (setq x-face-string (substring x-face-string (match-end 0))))
216 (insert "X-Face: " x-face-string))
217 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
219 ;; Insert X-Face-Version: field
220 (when (and (fboundp 'x-face-insert-version-header)
221 (boundp 'x-face-add-x-face-version-header)
222 x-face-add-x-face-version-header)
223 (x-face-insert-version-header)))
225 (defun wl-draft-setup ()
226 (let ((field wl-draft-fields)
230 (list (cons (concat (car field) " ")
231 (concat (car field) " ")))))
232 (setq field (cdr field)))
234 (cons (cons (concat wl-draft-mime-bcc-field-name ": ")
235 (concat wl-draft-mime-bcc-field-name ": "))
237 (setq wl-draft-field-completion-list cl)
238 (setq wl-address-complete-header-regexp
240 (append wl-address-complete-header-list
241 (list (concat wl-draft-mime-bcc-field-name ":")))))))
243 (defun wl-draft-make-mail-followup-to (recipients)
244 (if (elmo-list-member
245 (or wl-user-mail-address-list
246 (list (wl-address-header-extract-address wl-from)))
248 (let ((rlist (elmo-list-delete
249 (or wl-user-mail-address-list
250 (list (wl-address-header-extract-address wl-from)))
254 (lambda (item) (string= (downcase elem)
257 (if (elmo-list-member rlist (mapcar 'downcase
258 wl-subscribed-mailing-list))
260 (append rlist (list (wl-address-header-extract-address
264 (defun wl-draft-delete-myself-from-cc (to cc)
265 (let ((myself (or wl-user-mail-address-list
266 (list (wl-address-header-extract-address wl-from)))))
267 (cond (wl-draft-always-delete-myself ; always-delete option
268 (elmo-list-delete myself cc
271 (lambda (item) (string= (downcase elem)
274 ((elmo-list-member (append to cc) ; subscribed mailing-list
275 (mapcar 'downcase wl-subscribed-mailing-list))
276 (elmo-list-delete myself cc
279 (lambda (item) (string= (downcase elem)
284 (defun wl-draft-forward (original-subject summary-buf)
285 (let (references parent-folder)
286 (with-current-buffer summary-buf
287 (setq parent-folder (wl-summary-buffer-folder-name)))
288 (with-current-buffer (wl-message-get-original-buffer)
289 (setq references (nconc
290 (std11-field-bodies '("References" "In-Reply-To"))
291 (list (std11-field-body "Message-Id"))))
292 (setq references (delq nil references)
293 references (mapconcat 'identity references " ")
294 references (wl-draft-parse-msg-id-list-string references)
295 references (wl-delete-duplicates references)
296 references (when references
297 (mapconcat 'identity references "\n\t"))))
298 (wl-draft (list (cons 'To "")
300 (concat wl-forward-subject-prefix original-subject))
301 (cons 'References references))
302 nil nil nil nil parent-folder))
303 (goto-char (point-max))
304 (wl-draft-insert-message)
305 (mail-position-on-field "To"))
307 (defun wl-draft-strip-subject-re (subject)
308 "Remove \"Re:\" from subject lines. Shamelessly copied from Gnus."
309 (if (string-match wl-subject-prefix-regexp subject)
310 (substring subject (match-end 0))
313 (defun wl-draft-reply-list-symbol (with-arg)
314 "Return symbol `wl-draft-reply-*-argument-list' match condition.
315 Check WITH-ARG and From: field."
316 (if (wl-address-user-mail-address-p (or (elmo-field-body "From") ""))
318 'wl-draft-reply-myself-with-argument-list
319 'wl-draft-reply-myself-without-argument-list)
321 'wl-draft-reply-with-argument-list
322 'wl-draft-reply-without-argument-list)))
324 (defun wl-draft-reply (buf with-arg summary-buf)
325 "Reply to BUF buffer message.
326 Reply to author if WITH-ARG is non-nil."
329 to mail-followup-to cc subject in-reply-to references newsgroups
330 to-alist cc-alist decoder parent-folder)
331 (set-buffer summary-buf)
332 (setq parent-folder (wl-summary-buffer-folder-name))
334 (setq r-list (symbol-value (wl-draft-reply-list-symbol with-arg)))
337 (when (let ((condition (car (car r-list))))
338 (cond ((stringp condition)
339 (std11-field-body condition))
343 (if (not (std11-field-body (car condition)))
345 (setq condition (cdr condition)))
348 (funcall condition))))
349 (let ((r-to-list (nth 0 (cdr (car r-list))))
350 (r-cc-list (nth 1 (cdr (car r-list))))
351 (r-ng-list (nth 2 (cdr (car r-list)))))
352 (when (and (member "Followup-To" r-ng-list)
353 (string= (std11-field-body "Followup-To") "poster"))
354 (setq r-to-list (cons "From" r-to-list))
355 (setq r-ng-list (delete "Followup-To"
356 (copy-sequence r-ng-list))))
357 (if (and r-to-list (symbolp r-to-list))
358 (setq to (wl-concat-list (funcall r-to-list) ","))
359 (setq to (wl-concat-list (cons to
360 (elmo-multiple-fields-body-list
363 (if (and r-cc-list (symbolp r-cc-list))
364 (setq cc (wl-concat-list (funcall r-to-list) ","))
365 (setq cc (wl-concat-list (cons cc
366 (elmo-multiple-fields-body-list
369 (if (and r-ng-list (symbolp r-ng-list))
370 (setq newsgroups (wl-concat-list (funcall r-ng-list) ","))
371 (setq newsgroups (wl-concat-list (cons newsgroups
376 (setq r-list (cdr r-list)))
377 (error "No match field: check your `%s'"
378 (symbol-name (wl-draft-reply-list-symbol with-arg))))
379 (setq subject (std11-field-body "Subject"))
380 (setq to (wl-parse-addresses to)
381 cc (wl-parse-addresses cc))
382 (with-temp-buffer ; to keep raw buffer unibyte.
383 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
384 (setq decoder (mime-find-field-decoder 'Subject 'plain))
385 (setq subject (if (and subject decoder)
386 (funcall decoder subject) subject))
390 (setq decoder (mime-find-field-decoder 'To 'plain))
391 (cons (nth 1 (std11-extract-address-components addr))
392 (if decoder (funcall decoder addr) addr)))
397 (setq decoder (mime-find-field-decoder 'Cc 'plain))
398 (cons (nth 1 (std11-extract-address-components addr))
399 (if decoder (funcall decoder addr) addr)))
401 (and wl-reply-subject-prefix
402 (setq subject (concat wl-reply-subject-prefix
403 (wl-draft-strip-subject-re
405 (setq in-reply-to (std11-field-body "Message-Id"))
406 (setq references (nconc
407 (std11-field-bodies '("References" "In-Reply-To"))
409 (setq to (delq nil (mapcar 'car to-alist)))
410 (setq cc (delq nil (mapcar 'car cc-alist)))
411 ;; if subscribed mailing list is contained in cc or to
412 ;; and myself is contained in cc,
413 ;; delete myself from cc.
414 (setq cc (wl-draft-delete-myself-from-cc to cc))
415 (when wl-insert-mail-followup-to
416 (setq mail-followup-to
417 (wl-draft-make-mail-followup-to (append to cc)))
418 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
419 (with-temp-buffer ; to keep raw buffer unibyte.
420 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
421 (setq newsgroups (wl-parse newsgroups
422 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
423 newsgroups (wl-delete-duplicates newsgroups)
428 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
429 (if decoder (funcall decoder grp) grp))
431 (setq to (wl-delete-duplicates to nil t))
432 (setq cc (wl-delete-duplicates
433 (append (wl-delete-duplicates cc nil t)
434 to (copy-sequence to))
436 (and to (setq to (mapconcat
438 (if wl-draft-reply-use-address-with-full-name
439 (or (cdr (assoc addr to-alist)) addr)
442 (and cc (setq cc (mapconcat
444 (if wl-draft-reply-use-address-with-full-name
445 (or (cdr (assoc addr cc-alist)) addr)
448 (and mail-followup-to
449 (setq mail-followup-to
452 (if wl-draft-reply-use-address-with-full-name
453 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
455 mail-followup-to ",\n\t")))
456 (and (null to) (setq to cc cc nil))
457 (setq references (delq nil references)
458 references (mapconcat 'identity references " ")
459 references (wl-draft-parse-msg-id-list-string references)
460 references (wl-delete-duplicates references)
461 references (if references
462 (mapconcat 'identity references "\n\t")))
463 (wl-draft (list (cons 'To to)
465 (cons 'Newsgroups newsgroups)
466 (cons 'Subject subject)
467 (cons 'In-Reply-To in-reply-to)
468 (cons 'References references)
469 (cons 'Mail-Followup-To mail-followup-to))
470 nil nil nil nil parent-folder)
471 (setq wl-draft-reply-buffer buf))
472 (run-hooks 'wl-reply-hook))
474 (defun wl-draft-add-references ()
475 (wl-draft-add-in-reply-to "References"))
477 (defun wl-draft-add-in-reply-to (&optional alt-field)
478 (let* ((mes-id (save-excursion
479 (set-buffer mail-reply-buffer)
480 (std11-field-body "message-id")))
481 (field (or alt-field "In-Reply-To"))
482 (ref (std11-field-body field))
483 (ref-list nil) (st nil))
484 (when (and mes-id ref)
485 (while (string-match "<[^>]+>" ref st)
487 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
490 (member mes-id ref-list))
494 (when (mail-position-on-field field)
496 (while (looking-at "^[ \t]")
498 (setq mes-id (concat "\t" mes-id "\n")))
502 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
503 &optional ignored-fields)
506 (narrow-to-region (point)(point))
508 (with-current-buffer mail-reply-buffer
510 (decode-mime-charset-region (point-min) (point-max)
512 (buffer-substring-no-properties
513 (point-min) (point-max))))
515 (goto-char (point-min))
516 (wl-draft-delete-fields ignored-fields))
517 (goto-char (point-max))
518 (push-mark (point) nil t)
519 (goto-char (point-min)))
521 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
522 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
523 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
524 (run-hooks 'wl-draft-cited-hook)
525 (when (if wl-draft-add-references
526 (wl-draft-add-references)
527 (if wl-draft-add-in-reply-to
528 (wl-draft-add-in-reply-to)))
529 (wl-highlight-headers 'for-draft)) ; highlight when added References:
530 (when wl-highlight-body-too
531 (wl-highlight-body-region beg (point-max)))))
533 (defun wl-message-news-p ()
534 "If exist valid Newsgroups field, return non-nil."
535 (std11-field-body "Newsgroups"))
537 (defun wl-message-field-exists-p (field)
538 "If FIELD exist and FIELD value is not empty, return non-nil."
539 (let ((value (std11-field-body field)))
541 (not (string= value "")))))
543 (defun wl-message-mail-p ()
544 "If exist To, Cc or Bcc field, return non-nil."
545 (or (wl-message-field-exists-p "To")
546 (wl-message-field-exists-p "Resent-to")
547 (wl-message-field-exists-p "Cc")
548 (wl-message-field-exists-p "Bcc")
549 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
550 ;;; This may be needed..
551 ;;; (wl-message-field-exists-p "Fcc")
554 (defun wl-draft-edit-string (string)
555 (let ((cur-buf (current-buffer))
556 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
557 to subject in-reply-to cc references newsgroups mail-followup-to
558 content-type content-transfer-encoding from
563 (setq to (std11-field-body "To"))
566 (decode-mime-charset-string
569 (setq subject (std11-field-body "Subject"))
570 (setq subject (and subject
572 (decode-mime-charset-string
575 (setq from (std11-field-body "From")
578 (decode-mime-charset-string
581 (setq in-reply-to (std11-field-body "In-Reply-To"))
582 (setq cc (std11-field-body "Cc"))
585 (decode-mime-charset-string
588 (setq references (std11-field-body "References"))
589 (setq newsgroups (std11-field-body "Newsgroups"))
590 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
591 (setq content-type (std11-field-body "Content-Type"))
592 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
593 (goto-char (point-min))
594 (or (re-search-forward "\n\n" nil t)
595 (search-forward (concat mail-header-separator "\n") nil t))
601 (nth 1 (std11-extract-address-components from))
602 wl-user-mail-address-list)
606 (cons 'Subject subject)
607 (cons 'Newsgroups newsgroups)
608 (cons 'Mail-Followup-To mail-followup-to)
609 (cons 'In-Reply-To in-reply-to)
610 (cons 'References references))
611 content-type content-transfer-encoding
612 (buffer-substring (point) (point-max))
614 (and to (mail-position-on-field "To"))
615 (delete-other-windows)
616 (kill-buffer tmp-buf)))
617 (run-hooks 'wl-draft-reedit-hook))
619 (defun wl-draft-insert-current-message (dummy)
621 (let (original-buffer
623 mail-citation-hook mail-yank-hooks
624 wl-draft-add-references wl-draft-add-in-reply-to
625 wl-draft-cite-function)
626 (with-current-buffer wl-draft-buffer-cur-summary-buffer
627 (with-current-buffer wl-message-buffer
628 (setq original-buffer (wl-message-get-original-buffer))
630 (with-current-buffer original-buffer
632 (error "No current message"))))
633 (setq mail-reply-buffer original-buffer)
634 (wl-draft-yank-from-mail-reply-buffer
636 wl-ignored-forwarded-headers)))
638 (defun wl-draft-insert-get-message (dummy)
639 (let ((fld (completing-read
641 (if (memq 'read-folder wl-use-folder-petname)
642 (wl-folder-get-entity-with-petname)
643 wl-folder-entity-hashtb)
644 nil nil wl-default-spec
645 'wl-read-folder-hist))
646 (number (call-interactively
647 (function (lambda (num)
648 (interactive "nNumber: ")
650 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
651 mail-citation-hook mail-yank-hooks
652 wl-draft-cite-function)
655 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
658 (elmo-make-fetch-strategy 'entire)
659 nil mail-reply-buffer)
660 (wl-draft-yank-from-mail-reply-buffer nil))
661 (kill-buffer mail-reply-buffer))))
664 ;; default body citation func
666 (defun wl-default-draft-cite ()
667 (let ((mail-yank-ignored-headers "[^:]+:")
668 (mail-yank-prefix "> ")
669 (summary-buf wl-current-summary-buffer)
670 (message-buf (get-buffer (wl-current-message-buffer)))
671 from date cite-title num entity)
673 (buffer-live-p summary-buf)
675 (buffer-live-p message-buf))
677 (with-current-buffer summary-buf
678 (let ((elmo-mime-charset wl-summary-buffer-mime-charset))
679 (setq num (save-excursion
680 (set-buffer message-buf)
681 wl-message-buffer-cur-number))
682 (setq entity (elmo-msgdb-overview-get-entity
683 num (wl-summary-buffer-msgdb)))
684 (setq date (elmo-msgdb-overview-entity-get-date entity))
685 (setq from (elmo-msgdb-overview-entity-get-from entity))))
686 (setq cite-title (format "At %s,\n%s wrote:"
687 (or date "some time ago")
688 (if wl-default-draft-cite-decorate-author
689 (funcall wl-summary-from-function
693 (insert cite-title "\n"))
694 (mail-indent-citation)))
696 (defvar wl-draft-buffer nil "Draft buffer to yank content")
697 (defun wl-draft-yank-to-draft-buffer (buffer)
698 "Yank BUFFER content to `wl-draft-buffer'."
699 (set-buffer wl-draft-buffer)
700 (let ((mail-reply-buffer buffer))
701 (wl-draft-yank-from-mail-reply-buffer nil)
702 (kill-buffer buffer)))
704 (defun wl-draft-yank-original (&optional arg)
705 "Yank original message."
708 (let (buf mail-reply-buffer)
711 (setq buf (current-buffer)))
712 (setq mail-reply-buffer buf)
713 (wl-draft-yank-from-mail-reply-buffer nil))
714 (wl-draft-yank-current-message-entity)))
716 (defun wl-draft-hide (editing-buffer)
717 "Hide the editing draft buffer if possible."
718 (when (and editing-buffer
719 (buffer-live-p editing-buffer))
720 (set-buffer editing-buffer)
721 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
722 fld-buf sum-win fld-win)
723 (if (and wl-draft-use-frame
724 (> (length (visible-frame-list)) 1))
730 ;; stay folder window if required
731 (when wl-stay-folder-window
732 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
733 (if (setq fld-win (get-buffer-window fld-buf))
734 (select-window fld-win)
735 (if wl-draft-resume-folder-window ;; resume folder window
736 (switch-to-buffer fld-buf)))))
737 (if (buffer-live-p sum-buf)
738 (if (setq sum-win (get-buffer-window sum-buf t))
739 ;; if Summary is on the frame, select it.
740 (select-window sum-win)
741 ;; if summary is not on the frame, switch to it.
742 (if (and wl-stay-folder-window
743 (or wl-draft-resume-folder-window fld-win))
744 (wl-folder-select-buffer sum-buf)
745 (switch-to-buffer sum-buf))))))))
747 (defun wl-draft-delete (editing-buffer)
748 "kill the editing draft buffer and delete the file corresponds to it."
751 (set-buffer editing-buffer)
752 (if wl-draft-buffer-file-name
754 (if (file-exists-p wl-draft-buffer-file-name)
755 (delete-file wl-draft-buffer-file-name))
756 (let ((msg (and wl-draft-buffer-file-name
757 (string-match "[0-9]+$" wl-draft-buffer-file-name)
759 (match-string 0 wl-draft-buffer-file-name)))))
760 (wl-draft-config-info-operation msg 'delete))))
761 (set-buffer-modified-p nil) ; force kill
762 (kill-buffer editing-buffer))))
764 (defun wl-draft-kill (&optional force-kill)
765 "Kill current draft buffer and quit editing."
768 (when (and (or (eq major-mode 'wl-draft-mode)
769 (eq major-mode 'mail-mode))
771 (y-or-n-p "Kill Current Draft? ")))
772 (let ((cur-buf (current-buffer)))
773 (wl-draft-hide cur-buf)
774 (wl-draft-delete cur-buf)))
777 (defun wl-draft-fcc ()
778 "Add a new Fcc field, with file name completion."
780 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
781 (mail-position-on-field "to"))
784 ;; Imported from message.el.
785 (defun wl-draft-elide-region (b e)
786 "Elide the text in the region.
787 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
791 (insert wl-draft-elide-ellipsis))
793 ;; Imported from message.el.
794 (defun wl-draft-beginning-of-line (&optional n)
795 "Move point to beginning of header value or to beginning of line."
797 (let ((zrs 'zmacs-region-stays))
798 (when (and (interactive-p) (boundp zrs))
800 (if (wl-draft-point-in-header-p)
801 (let* ((here (point))
802 (bol (progn (beginning-of-line n) (point)))
803 (eol (line-end-position))
804 (eoh (and (looking-at "[^ \t]")
805 (re-search-forward ": *" eol t))))
806 (if (and eoh (or (> here eoh) (= here bol)))
809 (beginning-of-line n)))
811 (defun wl-draft-point-in-header-p ()
812 "Return t if point is in the header."
815 (goto-char (point-min))
816 (not (re-search-forward
817 (concat "^" (regexp-quote mail-header-separator) "\n")
820 ;; function for wl-sent-message-via
822 (defmacro wl-draft-sent-message-p (type)
823 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
825 (defmacro wl-draft-set-sent-message (type result &optional server-port)
826 (` (let ((element (assq (, type) wl-sent-message-via)))
828 (unless (eq (nth 1 element) (, result))
829 (setcdr element (list (, result) (, server-port)))
830 (setq wl-sent-message-modified t))
831 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
832 (setq wl-sent-message-modified t)))))
834 (defun wl-draft-sent-message-results ()
835 (let ((results wl-sent-message-via)
836 unplugged-via sent-via)
838 (cond ((eq (nth 1 (car results)) 'unplugged)
839 (push (caar results) unplugged-via))
840 ((eq (nth 1 (car results)) 'sent)
841 (push (caar results) sent-via)))
842 (setq results (cdr results)))
843 (list unplugged-via sent-via)))
845 (defun wl-draft-write-sendlog (status proto server to id)
846 "Write send log file, if `wl-draft-sendlog' is non-nil."
847 (when wl-draft-sendlog
849 (let* ((filename (expand-file-name wl-draft-sendlog-filename
850 elmo-msgdb-directory))
851 (filesize (nth 7 (file-attributes filename)))
852 (server (if server (concat " server=" server) ""))
854 ((memq proto '(fcc queue))
855 (format " folder=\"%s\"" to))
857 (format " ng=<%s>" to))
862 (mapcar '(lambda(x) (format "<%s>" x)) to)
865 (id (if id (concat " id=" id) ""))
866 (time (format-time-string "%Y/%m/%d %T")))
867 (insert (format "%s proto=%s stat=%s%s%s%s\n"
868 time proto status server to id))
869 (if (and wl-draft-sendlog-max-size filesize
870 (> filesize wl-draft-sendlog-max-size))
871 (rename-file filename (concat filename ".old") t))
872 (if (file-writable-p filename)
873 (write-region-as-binary (point-min) (point-max)
875 (message "%s is not writable." filename))))))
877 (defun wl-draft-get-header-delimiter (&optional delete)
878 ;; If DELETE is non-nil, replace the header delimiter with a blank line
880 (goto-char (point-min))
881 (when (re-search-forward
882 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
886 (setq delimline (point-marker)))
889 (defun wl-draft-send-mail-with-qmail ()
890 "Pass the prepared message buffer to qmail-inject.
891 Refer to the documentation for the variable `send-mail-function'
892 to find out how to use this."
893 (if (and wl-draft-qmail-send-plugged
894 (not (elmo-plugged-p)))
895 (wl-draft-set-sent-message 'mail 'unplugged)
897 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
898 (let ((id (std11-field-body "Message-ID"))
899 (to (std11-field-body "To")))
903 'call-process-region 1 (point-max) wl-qmail-inject-program
905 wl-qmail-inject-args))
906 ;; qmail-inject doesn't say anything on it's stdout/stderr,
907 ;; we have to look at the retval instead
909 (wl-draft-set-sent-message 'mail 'sent)
910 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
911 (1 (error "`qmail-inject' reported permanent failure"))
912 (111 (error "`qmail-inject' reported transient failure"))
913 ;; should never happen
914 (t (error "`qmail-inject' reported unknown failure"))))))
916 (defun wl-draft-parse-msg-id-list-string (string)
917 "Get msg-id list from STRING."
919 (dolist (parsed-id (std11-parse-msg-ids-string string))
920 (when (eq (car parsed-id) 'msg-id)
921 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
923 (nreverse msg-id-list)))
925 (defun wl-draft-eword-encode-address-list (string &optional column)
926 "Encode header field STRING as list of address, and return the result.
927 Cause an error when STRING contains invalid address.
928 Optional argument COLUMN is start-position of the field."
929 (car (eword-encode-rword-list
930 (or column eword-encode-default-start-column)
931 (eword-encode-addresses-to-rword-list
932 (wl-draft-std11-parse-addresses (std11-lexical-analyze string))))))
934 (defun wl-draft-std11-parse-addresses (lal)
935 (let ((ret (std11-parse-address lal)))
936 (when (and (not (and (eq (length lal) 1)
937 (eq (car (car lal)) 'spaces)))
939 (error "Error while parsing address"))
941 (let ((dest (list (car ret))))
943 (while (and (setq ret (std11-parse-ascii-token lal))
944 (string-equal (cdr (assq 'specials (car ret))) ",")
945 (setq ret (std11-parse-address (cdr ret)))
947 (setq dest (cons (car ret) dest))
948 (setq lal (cdr ret)))
949 (while (eq 'spaces (car (car lal)))
950 (setq lal (cdr lal)))
951 (if lal (error "Error while parsing address"))
954 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
955 "Get mailbox list of FIELD from current buffer.
956 The buffer is expected to be narrowed to just the headers of the message.
957 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
958 from current buffer."
960 (let ((case-fold-search t)
961 (inhibit-read-only t)
963 mailbox-list beg seq has-group-list)
964 (goto-char (point-min))
965 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
968 (re-search-forward "^[^ \t]" nil 'move)
970 (skip-chars-backward "\n")
971 (setq seq (std11-lexical-analyze
972 (buffer-substring-no-properties beg (point))))
973 (setq addresses (wl-draft-std11-parse-addresses seq))
975 (cond ((eq (car (car addresses)) 'group)
976 (setq has-group-list t)
980 'std11-address-string
981 (nth 2 (car addresses))))))
982 ((eq (car (car addresses)) 'mailbox)
983 (setq address (nth 1 (car addresses)))
987 (std11-addr-to-string
988 (if (eq (car address) 'phrase-route-addr)
991 (setq addresses (cdr addresses)))
992 (when (and remove-group-list has-group-list)
993 (delete-region beg (point))
994 (insert (wl-address-string-without-group-list-contents seq))))
997 (defun wl-draft-deduce-address-list (buffer header-start header-end)
998 "Get address list suitable for smtp RCPT TO:<address>.
999 Group list content is removed if `wl-draft-remove-group-list-contents' is
1001 (let ((fields (if (and wl-draft-doing-mime-bcc
1002 wl-draft-disable-bcc-for-mime-bcc)
1004 '("to" "cc" "bcc")))
1005 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
1006 (case-fold-search t)
1010 (narrow-to-region header-start header-end)
1011 (goto-char (point-min))
1013 (if (re-search-forward "^resent-to[\t ]*:" nil t)
1014 (setq fields resent-fields)))
1018 (wl-draft-parse-mailbox-list
1020 wl-draft-remove-group-list-contents)))
1021 (setq fields (cdr fields)))
1027 (defun wl-draft-send-mail-with-smtp ()
1028 "Send the prepared message buffer with SMTP."
1030 (let* ((errbuf (if mail-interactive
1031 (generate-new-buffer " smtp errors")
1033 (case-fold-search t)
1034 (default-case-fold-search t)
1035 (sender (or wl-envelope-from
1036 (wl-address-header-extract-address wl-from)))
1037 (delimline (save-excursion
1038 (goto-char (point-min))
1040 (concat "^" (regexp-quote mail-header-separator)
1044 (or wl-smtp-posting-server smtp-server "localhost"))
1045 (smtp-service (or wl-smtp-posting-port smtp-service))
1046 (smtp-local-domain (or smtp-local-domain wl-local-domain))
1047 (id (std11-field-body "message-id"))
1049 (if (not (elmo-plugged-p smtp-server smtp-service))
1050 (wl-draft-set-sent-message 'mail 'unplugged
1051 (cons smtp-server smtp-service))
1054 ;; Instead of `smtp-deduce-address-list'.
1055 (setq recipients (wl-draft-deduce-address-list
1056 (current-buffer) (point-min) delimline))
1057 (unless recipients (error "No recipients"))
1058 ;; Insert an extra newline if we need it to work around
1059 ;; Sun's bug that swallows newlines.
1060 (goto-char (1+ delimline))
1061 (if (eval mail-mailer-swallows-blank-line)
1063 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1064 (if mail-interactive
1068 (wl-draft-delete-field "bcc" delimline)
1069 (wl-draft-delete-field "resent-bcc" delimline)
1070 (let (process-connection-type)
1073 (wl-smtp-extension-bind
1075 (smtp-send-buffer sender recipients (current-buffer))
1077 (wl-draft-write-sendlog 'failed 'smtp smtp-server
1079 (if (and (eq (car err) 'smtp-response-error)
1080 (/= (nth 1 err) 334))
1082 (wl-smtp-password-key
1084 (car smtp-sasl-mechanisms)
1086 (signal (car err) (cdr err)))))
1087 (wl-draft-set-sent-message 'mail 'sent)
1088 (wl-draft-write-sendlog
1089 'ok 'smtp smtp-server recipients id)))))
1090 (if (bufferp errbuf)
1091 (kill-buffer errbuf))))))
1093 (defun wl-draft-send-mail-with-pop-before-smtp ()
1094 "Send the prepared message buffer with POP-before-SMTP."
1095 (require 'elmo-pop3)
1099 :user (or wl-pop-before-smtp-user
1100 elmo-pop3-default-user)
1101 :server (or wl-pop-before-smtp-server
1102 elmo-pop3-default-server)
1103 :port (or wl-pop-before-smtp-port
1104 elmo-pop3-default-port)
1105 :auth (or wl-pop-before-smtp-authenticate-type
1106 elmo-pop3-default-authenticate-type)
1107 :stream-type (or wl-pop-before-smtp-stream-type
1108 elmo-pop3-default-stream-type))))
1109 (condition-case error
1111 (elmo-pop3-get-session session)
1112 (when session (elmo-network-close-session session)))
1114 (elmo-network-close-session session)
1115 (unless (string= (nth 1 error) "Unplugged")
1116 (signal (car error)(cdr error))))))
1117 (wl-draft-send-mail-with-smtp))
1119 (defun wl-draft-insert-required-fields (&optional force-msgid)
1120 "Insert Message-ID, Date, and From field.
1121 If FORCE-MSGID, insert message-id regardless of `wl-insert-message-id'."
1122 ;; Insert Message-Id field...
1123 (goto-char (point-min))
1124 (when (and (or force-msgid
1125 wl-insert-message-id)
1126 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1127 (insert (concat "Message-ID: "
1128 (funcall wl-message-id-function)
1130 ;; Insert date field.
1131 (goto-char (point-min))
1132 (or (re-search-forward "^Date[ \t]*:" nil t)
1133 (wl-draft-insert-date-field))
1134 ;; Insert from field.
1135 (goto-char (point-min))
1136 (or (re-search-forward "^From[ \t]*:" nil t)
1137 (wl-draft-insert-from-field)))
1139 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1140 "Send the message in the current buffer."
1142 (std11-narrow-to-header mail-header-separator)
1143 (wl-draft-insert-required-fields)
1144 ;; Delete null fields.
1145 (goto-char (point-min))
1146 (while (re-search-forward "^[^ \t\n:]+:[ \t]*\n" nil t)
1148 ;; ignore any blank lines in the header
1149 (while (re-search-forward "\n\n\n*" nil t)
1150 (replace-match "\n")))
1151 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1152 (wl-draft-dispatch-message)
1153 (when kill-when-done
1154 ;; hide editing-buffer.
1155 (wl-draft-hide editing-buffer)
1156 ;; delete editing-buffer and its file.
1157 (wl-draft-delete editing-buffer)))
1159 (defun wl-draft-dispatch-message (&optional mes-string)
1160 "Send the message in the current buffer. Not modified the header fields."
1161 (let (delimline mime-bcc)
1162 (if (and wl-draft-verbose-send mes-string)
1163 (message mes-string))
1165 (setq delimline (wl-draft-get-header-delimiter t))
1166 (unless wl-draft-fcc-list
1167 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1169 (setq wl-sent-message-modified nil)
1172 (if (and (wl-message-mail-p)
1173 (not (wl-draft-sent-message-p 'mail)))
1174 (if (or (not (or wl-draft-force-queuing
1175 wl-draft-force-queuing-mail))
1176 (memq 'mail wl-sent-message-queued))
1178 (setq mime-bcc (wl-draft-mime-bcc-field))
1179 (funcall wl-draft-send-mail-function)
1180 (when (not (zerop (length mime-bcc)))
1181 (wl-draft-do-mime-bcc mime-bcc)))
1182 (push 'mail wl-sent-message-queued)
1183 (wl-draft-set-sent-message 'mail 'unplugged)))
1184 (if (and (wl-message-news-p)
1185 (not (wl-draft-sent-message-p 'news))
1186 (not (wl-message-field-exists-p "Resent-to")))
1187 (if (or (not (or wl-draft-force-queuing
1188 wl-draft-force-queuing-news))
1189 (memq 'news wl-sent-message-queued))
1190 (funcall wl-draft-send-news-function)
1191 (push 'news wl-sent-message-queued)
1192 (wl-draft-set-sent-message 'news 'unplugged))))
1193 (let* ((status (wl-draft-sent-message-results))
1194 (unplugged-via (car status))
1195 (sent-via (nth 1 status)))
1196 ;; If one sent, process fcc folder.
1197 (if (and sent-via wl-draft-fcc-list)
1199 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1201 (setq wl-draft-fcc-list nil)))
1202 (if wl-draft-use-cache
1203 (let ((id (std11-field-body "Message-ID"))
1204 (elmo-enable-disconnected-operation t))
1205 (elmo-file-cache-save (elmo-file-cache-get-path id)
1207 ;; If one unplugged, append queue.
1208 (when (and unplugged-via
1209 wl-sent-message-modified)
1210 (if wl-draft-enable-queuing
1212 (wl-draft-queue-append wl-sent-message-via)
1213 (setq wl-sent-message-modified 'requeue))
1214 (error "Unplugged")))
1215 (when wl-draft-verbose-send
1216 (if (and unplugged-via sent-via);; combined message
1218 (setq wl-draft-verbose-msg
1219 (format "Sending%s and Queuing%s..."
1220 sent-via unplugged-via))
1221 (message (concat wl-draft-verbose-msg "done")))
1223 (message (concat mes-string
1224 (if sent-via "done" "failed")))))))))
1225 (not wl-sent-message-modified)) ;; return value
1227 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1228 "Force send current buffer as raw message."
1231 (let (wl-interactive-send
1232 ;;; wl-draft-verbose-send
1233 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1234 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1237 (wl-draft-send kill-when-done mes-string))))
1239 (defun wl-draft-clone-local-variables ()
1240 (let ((locals (buffer-local-variables))
1243 (when (and (consp (car locals))
1245 (string-match wl-draft-clone-local-variable-regexp
1246 (symbol-name (car (car locals)))))
1247 (wl-append result (list (car (car locals)))))
1248 (setq locals (cdr locals)))
1251 (defun wl-draft-send (&optional kill-when-done mes-string)
1252 "Send current draft message.
1253 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1255 ;; Don't call this explicitly.
1256 ;; Added to 'wl-draft-send-hook (by teranisi)
1257 ;; (wl-draft-config-exec)
1258 (run-hooks 'wl-draft-send-hook)
1259 (when (or (not wl-interactive-send)
1260 (y-or-n-p "Do you really want to send current draft? "))
1261 (let ((send-mail-function 'wl-draft-raw-send)
1262 (editing-buffer (current-buffer))
1263 (sending-buffer (wl-draft-generate-clone-buffer
1264 " *wl-draft-sending-buffer*"
1265 (append wl-draft-config-variables
1266 (wl-draft-clone-local-variables))))
1267 (wl-draft-verbose-msg nil)
1270 (save-excursion (set-buffer sending-buffer)
1271 (if (and (not (wl-message-mail-p))
1272 (not (wl-message-news-p)))
1273 (error "No recipient is specified"))
1274 (expand-abbrev) ; for mail-abbrevs
1275 (let ((mime-header-encode-method-alist
1277 '((wl-draft-eword-encode-address-list
1278 . (To Cc Bcc Resent-To Resent-Cc Bcc Resent-Bcc)))
1279 (if (boundp 'mime-header-encode-method-alist)
1280 (symbol-value 'mime-header-encode-method-alist)))))
1281 (run-hooks 'mail-send-hook) ; translate buffer
1284 (if wl-draft-verbose-send
1285 (message (or mes-string "Sending...")))
1286 (funcall wl-draft-send-function editing-buffer kill-when-done)
1287 ;; Now perform actions on successful sending.
1288 (while mail-send-actions
1290 (apply (car (car mail-send-actions))
1291 (cdr (car mail-send-actions)))
1293 (setq mail-send-actions (cdr mail-send-actions)))
1294 (if wl-draft-verbose-send
1295 (message (concat (or wl-draft-verbose-msg
1296 mes-string "Sending...")
1298 ;; kill sending buffer, anyway.
1299 (and (buffer-live-p sending-buffer)
1300 (kill-buffer sending-buffer))))))
1302 (defun wl-draft-mime-bcc-field ()
1303 "Return the MIME-Bcc field body. The field is deleted."
1304 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1305 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1307 (defun wl-draft-do-mime-bcc (field-body)
1308 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1309 (let ((orig-from (mime-decode-field-body (std11-field-body "from")
1311 (orig-subj (mime-decode-field-body (or (std11-field-body "subject")
1314 (recipients (wl-parse-addresses field-body))
1315 (draft-buffer (current-buffer))
1317 (save-window-excursion
1318 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1319 (not (zerop (length field-body))))
1320 (let ((wl-draft-doing-mime-bcc t))
1321 (dolist (recipient recipients)
1322 (wl-draft-create-buffer)
1323 (wl-draft-create-contents
1324 (append `((From . ,orig-from)
1326 (Subject . ,(concat "A blind carbon copy ("
1329 (wl-draft-default-headers)))
1330 (wl-draft-insert-mail-header-separator)
1331 (wl-draft-prepare-edit)
1332 (goto-char (point-max))
1333 (insert (or wl-draft-mime-bcc-body
1334 "This is a blind carbon copy.")
1336 (mime-edit-insert-tag "message" "rfc822")
1337 (insert-buffer draft-buffer)
1338 (let (wl-interactive-send)
1339 (wl-draft-send 'kill-when-done))))))))
1341 ;; Derived from `message-save-drafts' in T-gnus.
1342 (defun wl-draft-save ()
1343 "Save current draft."
1345 (if (buffer-modified-p)
1347 (message "Saving %s..." wl-draft-buffer-file-name)
1348 (let ((msg (buffer-substring-no-properties (point-min) (point-max))))
1349 (with-temp-file wl-draft-buffer-file-name
1351 ;; If no header separator, insert it.
1353 (goto-char (point-min))
1354 (unless (re-search-forward
1355 (concat "^" (regexp-quote mail-header-separator) "$")
1357 (goto-char (point-min))
1358 (if (re-search-forward "\n\n" nil t)
1359 (replace-match (concat "\n" mail-header-separator "\n"))
1360 (goto-char (point-max))
1361 (insert (if (eq (char-before) ?\n) "" "\n")
1362 mail-header-separator "\n"))))
1363 (let ((mime-header-encode-method-alist
1364 '((eword-encode-unstructured-field-body))))
1365 (mime-edit-translate-buffer))
1366 (wl-draft-get-header-delimiter t)))
1367 (set-buffer-modified-p nil)
1368 (wl-draft-config-info-operation
1369 (and (string-match "[0-9]+$" wl-draft-buffer-file-name)
1371 (match-string 0 wl-draft-buffer-file-name)))
1373 (message "Saving %s...done" wl-draft-buffer-file-name))
1374 (message "(No changes need to be saved)")))
1376 (defun wl-draft-mimic-kill-buffer ()
1377 "Kill the current (draft) buffer with query."
1379 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1382 (if (or (not bufname)
1383 (string-equal bufname "")
1384 (string-equal bufname (buffer-name)))
1385 (let ((bufname (current-buffer)))
1386 (when (or (not (buffer-modified-p))
1388 (format "Buffer %s modified; kill anyway? " bufname)))
1389 (set-buffer-modified-p nil)
1390 (wl-draft-hide bufname)
1391 (kill-buffer bufname)))
1392 (kill-buffer bufname))))
1394 (defun wl-draft-save-and-exit ()
1395 "Save current draft and exit current draft mode."
1398 (let ((editing-buffer (current-buffer)))
1399 (wl-draft-hide editing-buffer)
1400 (kill-buffer editing-buffer)))
1402 (defun wl-draft-send-and-exit ()
1403 "Send current draft message and kill it."
1407 (defun wl-draft-send-from-toolbar ()
1409 (let ((wl-interactive-send t))
1410 (wl-draft-send-and-exit)))
1412 (defun wl-draft-delete-field (field &optional delimline replace)
1413 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1415 (defun wl-draft-delete-fields (field &optional delimline replace)
1418 (goto-char (point-min))
1419 (if (search-forward "\n\n" nil t)
1420 (setq delimline (point))
1421 (setq delimline (point-max))))
1422 (narrow-to-region (point-min) delimline)
1423 (goto-char (point-min))
1424 (let ((regexp (concat "^" field ":"))
1425 (case-fold-search t))
1427 (if (looking-at regexp)
1433 (if (re-search-forward "^[^ \t]" nil t)
1434 (goto-char (match-beginning 0))
1437 (insert (concat field ": " replace "\n"))))
1439 (if (re-search-forward "^[^ \t]" nil t)
1440 (goto-char (match-beginning 0))
1443 (defun wl-draft-get-fcc-list (header-end)
1444 (if (and wl-draft-doing-mime-bcc
1445 wl-draft-disable-fcc-for-mime-bcc)
1447 (wl-draft-delete-field "fcc")
1450 (case-fold-search t))
1451 (or (markerp header-end) (error "HEADER-END must be a marker"))
1453 (goto-char (point-min))
1454 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1459 (buffer-substring-no-properties
1463 (skip-chars-backward " \t")
1466 (dolist (folder fcc-list)
1467 (wl-folder-confirm-existence
1468 (wl-folder-get-elmo-folder (eword-decode-string folder)))))
1469 (delete-region (match-beginning 0)
1470 (progn (forward-line 1) (point)))))
1473 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1474 (let ((send-mail-buffer (current-buffer))
1475 (tembuf (generate-new-buffer " fcc output"))
1476 (case-fold-search t)
1478 (or (markerp header-end) (error "HEADER-END must be a marker"))
1481 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1484 ;; insert just the headers to avoid moving the gap more than
1485 ;; necessary (the message body could be arbitrarily huge.)
1486 (insert-buffer-substring send-mail-buffer 1 header-end)
1487 (wl-draft-insert-required-fields t)
1488 (goto-char (point-max))
1489 (insert-buffer-substring send-mail-buffer header-end)
1490 (let ((id (std11-field-body "Message-ID"))
1491 (elmo-enable-disconnected-operation t)
1494 (unless (or cache-saved
1495 (elmo-folder-plugged-p
1496 (wl-folder-get-elmo-folder (car fcc-list))))
1497 (elmo-file-cache-save id nil) ;; for disconnected operation
1498 (setq cache-saved t))
1499 (if (elmo-folder-append-buffer
1500 (wl-folder-get-elmo-folder
1501 (eword-decode-string (car fcc-list)))
1502 (not wl-fcc-force-as-read))
1503 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1504 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1505 (setq fcc-list (cdr fcc-list)))))
1506 (kill-buffer tembuf)))
1508 (defun wl-draft-on-field-p ()
1511 (goto-char (point-min))
1512 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1519 (if (or (looking-at ".*,[ \t]?$")
1520 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1522 (let ((pos (point)))
1525 (if (looking-at "^[ \t]")
1527 (if (re-search-forward ":" pos t) nil t)))))))
1531 (defun wl-draft (&optional header-alist
1532 content-type content-transfer-encoding
1535 "Write and send mail/news message with Wanderlust."
1542 (wl-plugged-init t))
1544 (wl-init)) ; returns immediately if already initialized.
1547 (let (buf-name header-alist-internal)
1549 (wl-draft-create-buffer
1551 (eq this-command 'wl-draft)
1552 (eq this-command 'wl-summary-write)
1553 (eq this-command 'wl-summary-write-current-folder)
1554 (eq this-command 'wl-folder-write-current-folder))
1557 (unless (cdr (assq 'From header-alist))
1559 (append (list (cons 'From wl-from)) header-alist)))
1560 (unless (cdr (assq 'To header-alist))
1565 (if (assq 'To header-alist)
1566 (setcdr (assq 'To header-alist) to)
1568 (append header-alist
1569 (list (cons 'To to))))))))
1570 (unless (cdr (assq 'Subject header-alist))
1571 (if (assq 'Subject header-alist)
1572 (setcdr (assq 'Subject header-alist) "")
1574 (append header-alist (list (cons 'Subject ""))))))
1575 (setq header-alist (append header-alist
1576 (wl-draft-default-headers)
1577 wl-draft-additional-header-alist
1578 (if body (list "" (cons 'Body body)))))
1579 (wl-draft-create-contents header-alist)
1581 (wl-draft-decode-body
1582 content-type content-transfer-encoding))
1583 (wl-draft-insert-mail-header-separator)
1584 (wl-draft-prepare-edit)
1586 (run-hooks 'wl-mail-setup-hook))
1587 (goto-char (point-min))
1588 (wl-user-agent-compose-internal) ;; user-agent
1591 (string= (cdr (assq 'To header-alist)) ""))
1592 (mail-position-on-field "To"))
1594 (goto-char (point-max))))
1597 (defun wl-draft-create-buffer (&optional full parent-folder)
1598 (let* ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1599 (parent-folder (or parent-folder (wl-summary-buffer-folder-name)))
1600 (summary-buf (wl-summary-get-buffer parent-folder))
1601 buf-name file-name num change-major-mode-hook)
1602 (if (not (elmo-folder-message-file-p draft-folder))
1603 (error "%s folder cannot be used for draft folder" wl-draft-folder))
1604 (setq num (elmo-max-of-list
1605 (or (elmo-folder-list-messages draft-folder) '(0))))
1606 (setq num (+ 1 num))
1607 ;; To get unused buffer name.
1608 (while (get-buffer (concat wl-draft-folder "/" (int-to-string num)))
1609 (setq num (+ 1 num)))
1610 (setq buf-name (find-file-noselect
1612 (elmo-message-file-name
1613 (wl-folder-get-elmo-folder wl-draft-folder)
1615 (if wl-draft-use-frame
1616 (switch-to-buffer-other-frame buf-name)
1617 (switch-to-buffer buf-name))
1618 (set-buffer buf-name)
1619 (if (not (string-match (regexp-quote wl-draft-folder)
1621 (rename-buffer (concat wl-draft-folder "/" (int-to-string num))))
1622 (if (or (eq wl-draft-reply-buffer-style 'full)
1624 (delete-other-windows))
1627 (make-local-variable 'truncate-partial-width-windows)
1628 (setq truncate-partial-width-windows nil)
1629 (setq truncate-lines wl-draft-truncate-lines)
1630 (setq wl-sent-message-via nil)
1631 (setq wl-sent-message-queued nil)
1632 (setq wl-draft-buffer-file-name file-name)
1633 (setq wl-draft-config-exec-flag t)
1634 (setq wl-draft-parent-folder (or parent-folder ""))
1635 (or (eq this-command 'wl-folder-write-current-folder)
1636 (setq wl-draft-buffer-cur-summary-buffer summary-buf))
1639 (defun wl-draft-create-contents (header-alist)
1640 "header-alist' sample
1641 '(function ;; funcall
1642 string ;; insert string
1643 (symbol . string) ;; insert symbol-value: string
1644 (symbol . function) ;; (funcall) and if it returns string,
1645 ;; insert symbol-value: string
1646 (symbol . nil) ;; do nothing
1650 (unless (eq major-mode 'wl-draft-mode)
1651 (error "wl-draft-create-header must be use in wl-draft-mode."))
1652 (let ((halist header-alist)
1657 ((functionp (car halist)) (funcall (car halist)))
1659 ((stringp (car halist)) (insert (car halist) "\n"))
1661 ((consp (car halist))
1662 (setq field (car (car halist)))
1663 (setq value (cdr (car halist)))
1667 ((eq field 'Body) ; body
1669 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1671 (let ((value-return (funcall value)))
1672 (when (stringp value-return)
1673 (insert (symbol-name field) ": " value-return "\n"))))
1682 (setq halist (cdr halist)))))
1684 (defun wl-draft-prepare-edit ()
1685 (unless (eq major-mode 'wl-draft-mode)
1686 (error "wl-draft-create-header must be use in wl-draft-mode."))
1687 (let (change-major-mode-hook)
1688 (wl-draft-editor-mode)
1689 (add-hook 'local-write-file-hooks 'wl-draft-save)
1690 (wl-draft-overload-functions)
1691 (wl-highlight-headers 'for-draft)
1693 (clear-visited-file-modtime)))
1695 (defun wl-draft-decode-header ()
1697 (std11-narrow-to-header)
1698 (wl-draft-decode-message-in-buffer)
1701 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1704 (std11-field-body "content-type")))
1705 (content-transfer-encoding
1706 (or content-transfer-encoding
1707 (std11-field-body "content-transfer-encoding")))
1710 (std11-narrow-to-header)
1711 (wl-draft-delete-field "content-type")
1712 (wl-draft-delete-field "content-transfer-encoding")
1713 (goto-char (point-max))
1714 (setq delimline (point-marker))
1716 (narrow-to-region delimline (point-max))
1717 (goto-char (point-min))
1719 (insert "Content-type: " content-type "\n"))
1720 (when content-transfer-encoding
1721 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1722 (wl-draft-decode-message-in-buffer)
1723 (goto-char (point-min))
1724 (unless (re-search-forward "^$" (point-at-eol) t)
1729 ;;; subroutine for wl-draft-create-contents
1730 ;;; must be used in wl-draft-mode
1731 (defun wl-draft-check-new-line ()
1732 (if (not (= (preceding-char) ?\n))
1735 (defsubst wl-draft-trim-ccs (cc)
1741 (null (and wl-draft-delete-myself-from-bcc-fcc
1743 (mapcar 'wl-address-header-extract-address
1745 (wl-parse-addresses (std11-field-body "To"))
1746 (wl-parse-addresses (std11-field-body "Cc"))))
1747 (mapcar 'downcase wl-subscribed-mailing-list)))))
1751 (defsubst wl-draft-default-headers ()
1753 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1754 (wl-address-header-extract-address
1756 (cons 'User-Agent wl-generate-mailer-string-function)
1757 (cons 'Reply-To mail-default-reply-to)
1758 (cons 'Bcc (function
1761 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1762 (cons 'Fcc (function
1764 (wl-draft-trim-ccs wl-fcc))))
1765 (cons 'Organization wl-organization)
1766 (and wl-auto-insert-x-face
1767 (file-exists-p wl-x-face-file)
1768 'wl-draft-insert-x-face-field-here) ;; allow nil
1769 mail-default-headers
1770 ;; check \n at th end of line for `mail-default-headers'
1771 'wl-draft-check-new-line
1774 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1777 (goto-char delimline)
1778 (goto-char (point-min))
1779 (if (search-forward "\n\n" nil t)
1780 (delete-backward-char 1)
1781 (goto-char (point-max))))
1782 (wl-draft-check-new-line)
1783 (put-text-property (point)
1785 (insert mail-header-separator "\n")
1787 'category 'mail-header-separator)))
1791 (defun wl-draft-elmo-nntp-send ()
1792 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1793 (elmo-nntp-default-user
1794 (or wl-nntp-posting-user elmo-nntp-default-user))
1795 (elmo-nntp-default-server
1796 (or wl-nntp-posting-server elmo-nntp-default-server))
1797 (elmo-nntp-default-port
1798 (or wl-nntp-posting-port elmo-nntp-default-port))
1799 (elmo-nntp-default-stream-type
1800 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1801 (elmo-nntp-default-function wl-nntp-posting-function)
1803 (if (setq condition (cdr (elmo-string-matched-assoc
1804 (std11-field-body "Newsgroups")
1805 wl-nntp-posting-config-alist)))
1806 (if (stringp condition)
1807 (setq elmo-nntp-default-server condition)
1808 (while (car condition)
1809 (set (intern (format "elmo-nntp-default-%s"
1810 (symbol-name (caar condition))))
1812 (setq condition (cdr condition)))))
1813 (unless elmo-nntp-default-function
1814 (error "wl-draft-nntp-send: posting-function is nil."))
1815 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1816 (wl-draft-set-sent-message 'news 'unplugged
1817 (cons elmo-nntp-default-server
1818 elmo-nntp-default-port))
1819 (funcall elmo-nntp-default-function
1820 elmo-nntp-default-server (current-buffer))
1821 (wl-draft-set-sent-message 'news 'sent)
1822 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1823 (std11-field-body "Newsgroups")
1824 (std11-field-body "Message-ID")))))
1826 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1827 "Generate clone of current buffer named NAME."
1828 (let ((editing-buffer (current-buffer)))
1830 (set-buffer (generate-new-buffer name))
1833 (wl-draft-editor-mode)
1834 (insert-buffer editing-buffer)
1836 (while local-variables
1837 (make-local-variable (car local-variables))
1838 (set (car local-variables)
1840 (set-buffer editing-buffer)
1841 (symbol-value (car local-variables))))
1842 (setq local-variables (cdr local-variables)))
1845 (defun wl-draft-reedit (number)
1846 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1848 buffer file-name change-major-mode-hook)
1849 (setq file-name (elmo-message-file-name draft-folder number))
1850 (unless (file-exists-p file-name)
1851 (error "File %s does not exist" file-name))
1852 (if (setq buffer (get-buffer
1853 (concat wl-draft-folder "/"
1854 (number-to-string number))))
1856 (if wl-draft-use-frame
1857 (switch-to-buffer-other-frame buffer)
1858 (switch-to-buffer buffer))
1859 (set-buffer buffer))
1860 (setq buffer (get-buffer-create (number-to-string number)))
1861 (if wl-draft-use-frame
1862 (switch-to-buffer-other-frame buffer)
1863 (switch-to-buffer buffer))
1865 (insert-file-contents-as-binary file-name)
1866 (let((mime-edit-again-ignored-field-regexp
1867 "^\\(Content-.*\\|Mime-Version\\):"))
1868 (wl-draft-decode-message-in-buffer))
1869 (wl-draft-insert-mail-header-separator)
1870 (if (not (string-match (regexp-quote wl-draft-folder)
1872 (rename-buffer (concat wl-draft-folder "/" (buffer-name))))
1875 (setq buffer-file-name file-name)
1876 (make-local-variable 'truncate-partial-width-windows)
1877 (setq truncate-partial-width-windows nil)
1878 (setq truncate-lines wl-draft-truncate-lines)
1879 (setq wl-sent-message-via nil)
1880 (setq wl-sent-message-queued nil)
1881 (setq wl-draft-buffer-file-name file-name)
1882 (wl-draft-config-info-operation number 'load)
1883 (goto-char (point-min))
1884 (wl-draft-overload-functions)
1885 (wl-draft-editor-mode)
1886 (add-hook 'local-write-file-hooks 'wl-draft-save)
1887 (wl-highlight-headers 'for-draft)
1888 (run-hooks 'wl-draft-reedit-hook)
1889 (goto-char (point-max))
1892 (defmacro wl-draft-body-goto-top ()
1894 (goto-char (point-min))
1895 (if (re-search-forward mail-header-separator nil t)
1897 (goto-char (point-max))))))
1899 (defmacro wl-draft-body-goto-bottom ()
1900 (` (goto-char (point-max))))
1902 (defmacro wl-draft-config-body-goto-header ()
1904 (goto-char (point-min))
1905 (if (re-search-forward mail-header-separator nil t)
1907 (goto-char (point-max))))))
1909 (defsubst wl-draft-config-sub-eval-insert (content &optional newline)
1910 (let (content-value)
1912 (stringp (setq content-value (eval content))))
1913 (insert content-value)
1914 (if newline (insert "\n")))))
1916 (defun wl-draft-config-sub-body (content)
1917 (wl-draft-body-goto-top)
1918 (delete-region (point) (point-max))
1919 (wl-draft-config-sub-eval-insert content))
1921 (defun wl-draft-config-sub-top (content)
1922 (wl-draft-body-goto-top)
1923 (wl-draft-config-sub-eval-insert content))
1925 (defun wl-draft-config-sub-bottom (content)
1926 (wl-draft-body-goto-bottom)
1927 (wl-draft-config-sub-eval-insert content))
1929 (defun wl-draft-config-sub-header (content)
1930 (wl-draft-config-body-goto-header)
1931 (wl-draft-config-sub-eval-insert content 'newline))
1933 (defun wl-draft-config-sub-header-top (content)
1934 (goto-char (point-min))
1935 (wl-draft-config-sub-eval-insert content 'newline))
1937 (defun wl-draft-config-sub-part-top (content)
1938 (goto-char (mime-edit-content-beginning))
1939 (wl-draft-config-sub-eval-insert content 'newline))
1941 (defun wl-draft-config-sub-part-bottom (content)
1942 (goto-char (mime-edit-content-end))
1943 (wl-draft-config-sub-eval-insert content 'newline))
1945 (defsubst wl-draft-config-sub-file (content)
1946 (let ((coding-system-for-read wl-cs-autoconv)
1947 (file (expand-file-name (eval content))))
1948 (if (file-exists-p file)
1949 (insert-file-contents file)
1950 (error "%s: no exists file" file))))
1952 (defun wl-draft-config-sub-body-file (content)
1953 (wl-draft-body-goto-top)
1954 (delete-region (point) (point-max))
1955 (wl-draft-config-sub-file content))
1957 (defun wl-draft-config-sub-top-file (content)
1958 (wl-draft-body-goto-top)
1959 (wl-draft-config-sub-file content))
1961 (defun wl-draft-config-sub-bottom-file (content)
1962 (wl-draft-body-goto-bottom)
1963 (wl-draft-config-sub-file content))
1965 (defun wl-draft-config-sub-header-file (content)
1966 (wl-draft-config-body-goto-header)
1967 (wl-draft-config-sub-file content))
1969 (defun wl-draft-config-sub-template (content)
1970 (setq wl-draft-config-variables
1971 (wl-template-insert (eval content))))
1973 (defun wl-draft-config-sub-x-face (content)
1974 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
1975 (fboundp 'x-face-insert)) ; x-face.el is installed.
1976 (x-face-insert content)
1977 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
1979 (defsubst wl-draft-config-sub-func (field content)
1981 (if (setq func (assq field wl-draft-config-sub-func-alist))
1982 (let (wl-draft-config-variables)
1983 (funcall (cdr func) content)
1984 ;; for wl-draft-config-sub-template
1985 (cons t wl-draft-config-variables)))))
1987 (defsubst wl-draft-config-exec-sub (clist)
1988 (let (config local-variables)
1990 (setq config (car clist))
1995 (let ((field (car config))
1996 (content (cdr config))
2000 (wl-draft-replace-field field (eval content) t))
2001 ((setq ret-val (wl-draft-config-sub-func field content))
2002 (if (cdr ret-val) ;; for wl-draft-config-sub-template
2003 (wl-append local-variables (cdr ret-val))))
2004 ((boundp field) ;; variable
2005 (make-local-variable field)
2006 (set field (eval content))
2007 (wl-append local-variables (list field)))
2009 (error "%s: not variable" field)))))
2011 (error "%s: not supported type" config)))
2012 (setq clist (cdr clist)))
2015 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
2016 "Change headers in draft preparation time."
2018 (unless wl-draft-reedit
2021 (and (boundp 'wl-draft-prepared-config-alist)
2022 wl-draft-prepared-config-alist) ;; For compatible.
2023 wl-draft-config-alist)))
2025 (wl-draft-config-exec config-alist reply-buf)))))
2027 (defun wl-draft-config-exec (&optional config-alist reply-buf)
2028 "Change headers according to the value of `wl-draft-config-alist'.
2029 Automatically applied in draft sending time."
2031 (let ((case-fold-search t)
2032 (alist (or config-alist wl-draft-config-alist))
2033 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2034 wl-draft-reply-buffer)))
2035 (local-variables wl-draft-config-variables)
2037 (when (and (or (interactive-p)
2038 wl-draft-config-exec-flag)
2043 (setq key (caar alist)
2050 (set-buffer reply-buf)
2052 (std11-narrow-to-header)
2053 (goto-char (point-min))
2054 (re-search-forward (car clist) nil t))))
2055 (wl-draft-config-exec-sub (cdr clist))
2058 (when (save-restriction
2059 (std11-narrow-to-header mail-header-separator)
2060 (goto-char (point-min))
2061 (re-search-forward key nil t))
2062 (wl-append local-variables
2063 (wl-draft-config-exec-sub clist))
2066 (wl-append local-variables
2067 (wl-draft-config-exec-sub clist))
2069 (if (and found wl-draft-config-matchone)
2071 (setq alist (cdr alist))))
2073 (setq wl-draft-config-exec-flag nil))
2074 (run-hooks 'wl-draft-config-exec-hook)
2075 (put-text-property (point-min)(point-max) 'face nil)
2076 (wl-highlight-message (point-min)(point-max) t)
2077 (setq wl-draft-config-variables
2078 (elmo-uniq-list local-variables))))))
2080 (defun wl-draft-replace-field (field content &optional add)
2083 (let ((case-fold-search t)
2084 (inhibit-read-only t) ;; added by teranisi.
2086 (std11-narrow-to-header mail-header-separator)
2087 (goto-char (point-min))
2088 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2093 (re-search-forward "^[^ \t]" nil 'move)
2095 (skip-chars-backward "\n")
2096 (delete-region beg (point))
2097 (insert " " content))
2102 (re-search-forward "^[^ \t]" nil 'move)
2104 (delete-region beg (point)))
2105 (when (and add content)
2107 (goto-char (point-max))
2108 (insert (concat field ": " content "\n"))))))))
2110 (defun wl-draft-config-info-operation (msg operation)
2111 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-folder-get-elmo-folder
2115 (format "%s-%d" wl-draft-config-save-filename msg)
2117 element alist variable)
2119 ((eq operation 'save)
2120 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2121 (while (setq variable (pop variables))
2122 (when (boundp variable)
2124 (list (cons variable (eval variable))))))
2125 (elmo-object-save filename alist)))
2126 ((eq operation 'load)
2127 (setq alist (elmo-object-load filename))
2128 (while (setq element (pop alist))
2129 (set (make-local-variable (car element)) (cdr element))
2130 (wl-append wl-draft-config-variables (list (car element)))))
2131 ((eq operation 'delete)
2132 (if (file-exists-p filename)
2133 (delete-file filename))))))
2135 (defun wl-draft-queue-info-operation (msg operation
2136 &optional add-sent-message-via)
2137 (let* ((msgdb-dir (elmo-folder-msgdb-path
2138 (wl-folder-get-elmo-folder wl-queue-folder)))
2141 (format "%s-%d" wl-draft-queue-save-filename msg)
2143 element alist variable)
2145 ((eq operation 'save)
2146 (let ((variables (elmo-uniq-list
2147 (append wl-draft-queue-save-variables
2148 wl-draft-config-variables
2149 (list 'wl-draft-fcc-list)))))
2150 (if add-sent-message-via
2152 (push 'wl-sent-message-queued variables)
2153 (push 'wl-sent-message-via variables)))
2154 (while (setq variable (pop variables))
2155 (when (boundp variable)
2157 (list (cons variable (eval variable))))))
2158 (elmo-object-save filename alist)))
2159 ((eq operation 'load)
2160 (setq alist (elmo-object-load filename))
2161 (while (setq element (pop alist))
2162 (set (make-local-variable (car element)) (cdr element))))
2163 ((eq operation 'get-sent-via)
2164 (setq alist (elmo-object-load filename))
2165 (cdr (assq 'wl-sent-message-via alist)))
2166 ((eq operation 'delete)
2167 (if (file-exists-p filename)
2168 (delete-file filename))))))
2170 (defun wl-draft-queue-append (wl-sent-message-via)
2171 (if wl-draft-verbose-send
2172 (message "Queuing..."))
2173 (let ((send-buffer (current-buffer))
2174 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2175 (message-id (std11-field-body "Message-ID")))
2176 (if (elmo-folder-append-buffer folder t)
2178 (wl-draft-queue-info-operation
2179 (car (elmo-folder-status folder))
2180 'save wl-sent-message-via)
2181 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2182 (when wl-draft-verbose-send
2183 (setq wl-draft-verbose-msg "Queuing...")
2184 (message "Queuing...done")))
2185 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2186 (error "Queuing failed"))))
2188 (defun wl-draft-queue-flush ()
2189 "Flush draft queue."
2191 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2193 (elmo-folder-open-internal queue-folder)
2194 (elmo-folder-list-messages queue-folder)))
2197 (wl-draft-queue-flushing t)
2198 msgs failure len buffer msgid sent-via)
2199 ;; get plugged send message
2201 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2204 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2205 (or (not (nth 2 (car sent-via)))
2207 (car (nth 2 (car sent-via)))
2208 (cdr (nth 2 (car sent-via))))))
2209 (wl-append msgs (list (car msgs2)))
2211 (setq sent-via (cdr sent-via))))
2212 (setq msgs2 (cdr msgs2)))
2213 (when (> (setq len (length msgs)) 0)
2214 (if (elmo-y-or-n-p (format
2215 "%d message(s) are in the sending queue. Send now? "
2217 (not elmo-dop-flush-confirm) t)
2220 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2223 ;; reset buffer local variables
2224 (kill-all-local-variables)
2228 (setq wl-sent-message-via nil)
2229 (wl-draft-queue-info-operation (car msgs) 'load)
2230 (elmo-message-fetch queue-folder
2232 (elmo-make-fetch-strategy 'entire)
2233 nil (current-buffer))
2235 (setq failure (funcall
2236 wl-draft-queue-flush-send-function
2237 (format "Sending (%d/%d)..." i len)))
2238 ;;; (wl-draft-raw-send nil nil
2239 ;;; (format "Sending (%d/%d)..." i len))
2241 (elmo-display-error err t)
2245 (if (eq wl-sent-message-modified 'requeue)
2247 (elmo-folder-delete-messages
2248 queue-folder (cons (car msgs) nil))
2249 (wl-draft-queue-info-operation (car msgs) 'delete))
2251 (elmo-folder-delete-messages
2252 queue-folder (cons (car msgs) nil))
2253 (wl-draft-queue-info-operation (car msgs) 'delete)
2254 (setq performed (+ 1 performed))))
2255 (setq msgs (cdr msgs)))
2256 (kill-buffer buffer)
2257 (message "%d message(s) are sent." performed)))
2258 (message "%d message(s) are remained to be sent." len))
2259 (elmo-folder-close queue-folder)
2262 (defun wl-jump-to-draft-buffer (&optional arg)
2263 "Jump to the draft if exists."
2266 (wl-jump-to-draft-folder)
2267 (let ((draft-bufs (wl-collect-draft))
2271 (message "No draft buffer exist."))
2274 (sort (mapcar 'buffer-name draft-bufs)
2275 (function (lambda (a b)
2276 (not (string< a b))))))
2277 (if (setq buf (cdr (member (buffer-name)
2279 (setq buf (car buf))
2280 (setq buf (car draft-bufs)))
2281 (switch-to-buffer buf))))))
2283 (defun wl-jump-to-draft-folder ()
2284 (let ((msgs (reverse (elmo-folder-list-messages (wl-folder-get-elmo-folder
2286 (mybuf (buffer-name))
2289 (message "No draft message exist.")
2290 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2291 (setq msg (cadr (memq
2292 (string-to-int (substring mybuf (match-end 0)))
2294 (or msg (setq msg (car msgs)))
2295 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2296 (switch-to-buffer buf)
2297 (wl-draft-reedit msg)))))
2299 (defun wl-draft-highlight-and-recenter (&optional n)
2301 (if wl-highlight-body-too
2302 (let ((beg (point-min))
2304 (put-text-property beg end 'face nil)
2305 (wl-highlight-message beg end t)))
2308 ;;;; user-agent support by Sen Nagata
2310 ;; this appears to be necessarily global...
2311 (defvar wl-user-agent-compose-p nil)
2312 (defvar wl-user-agent-headers-and-body-alist nil)
2314 ;; this should be a generic function for mail-mode -- i wish there was
2315 ;; something like it in sendmail.el
2316 (defun wl-user-agent-insert-header (header-name header-value)
2317 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2318 ;; it seems like overriding existing headers is acceptable -- should
2319 ;; we provide an option?
2321 ;; plan was: unfold header (might be folded), remove existing value, insert
2323 ;; wl doesn't seem to fold header lines yet anyway :-)
2325 (let ((kill-whole-line t)
2327 (mail-position-on-field (capitalize header-name))
2328 (setq end-of-line (point))
2330 (re-search-forward ":" end-of-line)
2331 (insert (concat " " header-value "\n"))
2334 ;; this should be a generic function for mail-mode -- i wish there was
2335 ;; something like it in sendmail.el
2337 ;; ** haven't dealt w/ case where the body is already set **
2338 (defun wl-user-agent-insert-body (body-text)
2339 "Insert a body of text, BODY-TEXT, into a message."
2340 ;; code defensively... :-P
2341 (goto-char (point-min))
2342 (search-forward mail-header-separator)
2347 (defun wl-user-agent-compose (&optional to subject other-headers continue
2348 switch-function yank-action
2350 "Support the `compose-mail' interface for wl.
2351 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2352 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2353 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2355 (unless (featurep 'wl)
2357 ;; protect these -- to and subject get bound at some point, so it looks
2358 ;; to be necessary to protect the values used w/in
2359 (let ((wl-user-agent-headers-and-body-alist other-headers)
2360 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2361 (wl-draft-reply-buffer-style 'split))
2362 (when (eq switch-function 'switch-to-buffer-other-window)
2363 (when (one-window-p t)
2364 (if (window-minibuffer-p) (other-window 1))
2368 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2371 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2374 (setq wl-user-agent-headers-and-body-alist
2375 (cons (cons "to" to)
2376 wl-user-agent-headers-and-body-alist))))
2378 (if (wl-string-match-assoc "subject"
2379 wl-user-agent-headers-and-body-alist
2382 (wl-string-match-assoc "subject"
2383 wl-user-agent-headers-and-body-alist
2386 (setq wl-user-agent-headers-and-body-alist
2387 (cons (cons "subject" subject)
2388 wl-user-agent-headers-and-body-alist))))
2389 ;; i think this is what we want to use...
2392 ;; tell the hook-function to do its stuff
2393 (setq wl-user-agent-compose-p t)
2394 ;; because to get the hooks working, wl-draft has to think it has
2395 ;; been called interactively
2396 (call-interactively 'wl-draft))
2397 (setq wl-user-agent-compose-p nil))))
2399 (defun wl-user-agent-compose-internal ()
2400 "Manipulate headers and/or a body of a draft message."
2401 ;; being called from wl-user-agent-compose?
2402 (if wl-user-agent-compose-p
2405 (let ((headers wl-user-agent-headers-and-body-alist)
2406 (case-fold-search t))
2409 (if (not (string-match "^body$" (car (car headers))))
2410 (wl-user-agent-insert-header
2411 (car (car headers)) (cdr (car headers)))
2413 (setq headers (cdr headers))))
2414 ;; highlight headers (from wl-draft in wl-draft.el)
2415 (wl-highlight-headers 'for-draft)
2417 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2419 (wl-user-agent-insert-body
2420 (cdr (wl-string-match-assoc
2422 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2426 (product-provide (provide 'wl-draft) (require 'wl-version))
2428 ;;; wl-draft.el ends here