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)
48 (defalias-maybe 'x-face-insert 'ignore)
49 (defalias-maybe 'x-face-insert-version-header 'ignore)
50 (defalias-maybe 'wl-init 'ignore)
51 (defalias-maybe 'wl-draft-mode 'ignore))
54 (autoload 'wl-addrmgr "wl-addrmgr"))
56 (defvar wl-draft-buffer-message-number nil)
57 (defvar wl-draft-field-completion-list nil)
58 (defvar wl-draft-verbose-send t)
59 (defvar wl-draft-verbose-msg nil)
60 (defvar wl-draft-queue-flushing nil)
61 (defvar wl-draft-config-variables nil)
62 (defvar wl-draft-config-exec-flag t)
63 (defvar wl-draft-buffer-cur-summary-buffer nil)
64 (defvar wl-draft-clone-local-variable-regexp "^\\(wl\\|mime\\)")
65 (defvar wl-draft-sendlog-filename "sendlog")
66 (defvar wl-draft-queue-save-filename "qinfo")
67 (defvar wl-draft-config-save-filename "config")
68 (defvar wl-draft-queue-flush-send-function 'wl-draft-dispatch-message)
69 (defvar wl-sent-message-via nil)
70 (defvar wl-sent-message-modified nil)
71 (defvar wl-sent-message-queued nil)
72 (defvar wl-draft-fcc-list nil)
73 (defvar wl-draft-reedit nil)
74 (defvar wl-draft-reply-buffer nil)
75 (defvar wl-draft-forward nil)
76 (defvar wl-draft-doing-mime-bcc nil)
78 (defvar wl-draft-parent-folder nil
79 "Folder name of the summary in which current draft is invoked.
80 This variable is local in each draft buffer.
81 You can refer its value in `wl-draft-config-alist'.
84 \(setq wl-draft-config-alist
85 '(((string-match \".*@domain1$\" wl-draft-parent-folder)
86 (\"From\" . \"user@domain1\"))
87 ((string-match \".*@domain2$\" wl-draft-parent-folder)
88 (\"From\" . \"user@domain2\"))))")
90 (defvar wl-draft-parent-number nil)
92 (defconst wl-draft-reply-saved-variables
93 '(wl-draft-parent-folder
94 wl-draft-parent-number))
96 (defvar wl-draft-config-sub-func-alist
97 '((body . wl-draft-config-sub-body)
98 (top . wl-draft-config-sub-top)
99 (bottom . wl-draft-config-sub-bottom)
100 (header . wl-draft-config-sub-header)
101 (header-top . wl-draft-config-sub-header-top)
102 (header-bottom . wl-draft-config-sub-header)
103 (part-top . wl-draft-config-sub-part-top)
104 (part-bottom . wl-draft-config-sub-part-bottom)
105 (body-file . wl-draft-config-sub-body-file)
106 (top-file . wl-draft-config-sub-top-file)
107 (bottom-file . wl-draft-config-sub-bottom-file)
108 (header-file . wl-draft-config-sub-header-file)
109 (template . wl-draft-config-sub-template)
110 (x-face . wl-draft-config-sub-x-face)))
112 (make-variable-buffer-local 'wl-draft-buffer-message-number)
113 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
114 (make-variable-buffer-local 'wl-draft-config-variables)
115 (make-variable-buffer-local 'wl-draft-config-exec-flag)
116 (make-variable-buffer-local 'wl-sent-message-via)
117 (make-variable-buffer-local 'wl-sent-message-queued)
118 (make-variable-buffer-local 'wl-draft-fcc-list)
119 (make-variable-buffer-local 'wl-draft-reply-buffer)
120 (make-variable-buffer-local 'wl-draft-parent-folder)
121 (make-variable-buffer-local 'wl-draft-parent-number)
123 (defvar wl-draft-folder-internal nil
124 "Internal variable for caching `opened' draft folder.")
126 (defsubst wl-smtp-password-key (user mechanism server)
127 (format "SMTP:%s/%s@%s"
128 user mechanism server))
130 (defmacro wl-smtp-extension-bind (&rest body)
131 (` (let* ((smtp-sasl-mechanisms
132 (if wl-smtp-authenticate-type
134 (if (listp wl-smtp-authenticate-type)
135 wl-smtp-authenticate-type
136 (list wl-smtp-authenticate-type)))))
137 (smtp-use-sasl (and smtp-sasl-mechanisms t))
138 (smtp-use-starttls (eq wl-smtp-connection-type 'starttls))
139 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
140 (if (and (string= (car smtp-sasl-mechanisms) "DIGEST-MD5")
142 (string-match "^\\([^@]*\\)@\\([^@]*\\)"
143 wl-smtp-posting-user))
144 (setq smtp-sasl-user-name (match-string 1 wl-smtp-posting-user)
145 smtp-sasl-properties (list 'realm
146 (match-string 2 wl-smtp-posting-user)))
147 (setq smtp-sasl-user-name wl-smtp-posting-user
148 smtp-sasl-properties nil))
149 (setq sasl-read-passphrase
153 (wl-smtp-password-key
155 (car smtp-sasl-mechanisms)
159 (defun wl-draft-insert-date-field ()
161 (insert "Date: " (wl-make-date-string) "\n"))
163 (defun wl-draft-insert-from-field ()
165 ;; Put the "From:" field in unless for some odd reason
166 ;; they put one in themselves.
169 (setq from (wl-draft-eword-encode-address-list wl-from))
170 (error (error "Please look at `wl-from' again")))
171 (insert "From: " from "\n")))
173 (defun wl-draft-insert-x-face-field ()
174 "Insert X-Face header."
176 (if (not (file-exists-p wl-x-face-file))
177 (error "File %s does not exist" wl-x-face-file)
178 (goto-char (point-min))
179 (search-forward mail-header-separator nil t)
181 (wl-draft-insert-x-face-field-here)
182 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
184 (defun wl-draft-insert-x-face-field-here ()
185 "Insert X-Face field at point."
186 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
187 (when (string-match "^\\(X-Face:\\)?[ \t\n]*" x-face-string)
188 (setq x-face-string (substring x-face-string (match-end 0))))
189 (insert "X-Face: " x-face-string))
190 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
192 ;; Insert X-Face-Version: field
193 (when (and (fboundp 'x-face-insert-version-header)
194 (boundp 'x-face-add-x-face-version-header)
195 x-face-add-x-face-version-header)
196 (x-face-insert-version-header)))
198 (defun wl-draft-setup ()
199 (let ((field wl-draft-fields)
203 (list (cons (concat (car field) " ")
204 (concat (car field) " ")))))
205 (setq field (cdr field)))
207 (cons (cons (concat wl-draft-mime-bcc-field-name ": ")
208 (concat wl-draft-mime-bcc-field-name ": "))
210 (setq wl-draft-field-completion-list cl)
211 (setq wl-address-complete-header-regexp
213 (append wl-address-complete-header-list
214 (list (concat wl-draft-mime-bcc-field-name ":")))))))
216 (defun wl-draft-make-mail-followup-to (recipients)
217 (if (elmo-list-member
218 (or wl-user-mail-address-list
219 (list (wl-address-header-extract-address wl-from)))
221 (let ((rlist (elmo-list-delete
222 (or wl-user-mail-address-list
223 (list (wl-address-header-extract-address wl-from)))
227 (lambda (item) (string= (downcase elem)
230 (if (elmo-list-member rlist (mapcar 'downcase
231 wl-subscribed-mailing-list))
233 (append rlist (list (wl-address-header-extract-address
237 (defun wl-draft-delete-myself-from-cc (to cc)
238 (let ((myself (or wl-user-mail-address-list
239 (list (wl-address-header-extract-address wl-from)))))
240 (cond (wl-draft-always-delete-myself ; always-delete option
241 (elmo-list-delete myself cc
244 (lambda (item) (string= (downcase elem)
247 ((elmo-list-member (append to cc) ; subscribed mailing-list
248 (mapcar 'downcase wl-subscribed-mailing-list))
249 (elmo-list-delete myself cc
252 (lambda (item) (string= (downcase elem)
257 (defun wl-draft-forward (original-subject summary-buf)
258 (let (references parent-folder)
259 (with-current-buffer summary-buf
260 (setq parent-folder (wl-summary-buffer-folder-name)))
261 (with-current-buffer (wl-message-get-original-buffer)
262 (setq references (nconc
263 (std11-field-bodies '("References" "In-Reply-To"))
264 (list (std11-field-body "Message-Id"))))
265 (setq references (delq nil references)
266 references (mapconcat 'identity references " ")
267 references (wl-draft-parse-msg-id-list-string references)
268 references (wl-delete-duplicates references)
269 references (when references
270 (mapconcat 'identity references "\n\t"))))
271 (and wl-draft-use-frame
272 (get-buffer-window summary-buf)
273 (select-window (get-buffer-window summary-buf)))
274 (wl-draft (list (cons 'To "")
276 (concat wl-forward-subject-prefix original-subject))
277 (cons 'References references))
278 nil nil nil nil parent-folder))
279 (goto-char (point-max))
280 (wl-draft-insert-message)
281 (mail-position-on-field "To"))
283 (defun wl-draft-strip-subject-re (subject)
284 "Remove \"Re:\" from SUBJECT string. Shamelessly copied from Gnus."
285 (if (string-match wl-subject-prefix-regexp subject)
286 (substring subject (match-end 0))
289 (defun wl-draft-self-reply-p ()
290 "Return t when From address in the current message is user's self one or not."
291 (wl-address-user-mail-address-p (or (elmo-field-body "From") "")))
293 (defun wl-draft-reply (buf with-arg summary-buf &optional number)
294 "Reply to BUF buffer message.
295 Reply to author if WITH-ARG is non-nil."
298 to mail-followup-to cc subject in-reply-to references newsgroups
299 to-alist cc-alist decoder parent-folder)
300 (when (buffer-live-p summary-buf)
301 (with-current-buffer summary-buf
302 (setq parent-folder (wl-summary-buffer-folder-name))))
303 (set-buffer (or buf mime-mother-buffer))
304 (setq r-list (if with-arg wl-draft-reply-with-argument-list
305 wl-draft-reply-without-argument-list))
308 (when (let ((condition (car (car r-list))))
309 (cond ((stringp condition)
310 (std11-field-body condition))
315 ((stringp (car condition))
316 (or (std11-field-body (car condition))
318 ((symbolp (car condition))
319 (or (funcall (car condition))
323 (setq condition (cdr condition)))
326 (funcall condition))))
327 (let ((r-to-list (nth 0 (cdr (car r-list))))
328 (r-cc-list (nth 1 (cdr (car r-list))))
329 (r-ng-list (nth 2 (cdr (car r-list)))))
330 (when (and (member "Followup-To" r-ng-list)
331 (string= (std11-field-body "Followup-To") "poster"))
332 (setq r-to-list (cons "From" r-to-list))
333 (setq r-ng-list (delete "Followup-To"
334 (copy-sequence r-ng-list))))
335 (if (and r-to-list (symbolp r-to-list))
336 (setq to (wl-concat-list (funcall r-to-list) ","))
337 (setq to (wl-concat-list (cons to
338 (elmo-multiple-fields-body-list
341 (if (and r-cc-list (symbolp r-cc-list))
342 (setq cc (wl-concat-list (funcall r-cc-list) ","))
343 (setq cc (wl-concat-list (cons cc
344 (elmo-multiple-fields-body-list
347 (if (and r-ng-list (symbolp r-ng-list))
348 (setq newsgroups (wl-concat-list (funcall r-ng-list) ","))
349 (setq newsgroups (wl-concat-list (cons newsgroups
354 (setq r-list (cdr r-list)))
355 (error "No match field: check your `wl-draft-reply-%s-argument-list'"
356 (if with-arg "with" "without")))
357 (setq subject (std11-field-body "Subject"))
358 (setq to (wl-parse-addresses to)
359 cc (wl-parse-addresses cc))
360 (with-temp-buffer ; to keep raw buffer unibyte.
361 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
362 (setq decoder (mime-find-field-decoder 'Subject 'plain))
363 (setq subject (if (and subject decoder)
364 (funcall decoder subject) subject))
368 (setq decoder (mime-find-field-decoder 'To 'plain))
369 (cons (nth 1 (std11-extract-address-components addr))
370 (if decoder (funcall decoder addr) addr)))
375 (setq decoder (mime-find-field-decoder 'Cc 'plain))
376 (cons (nth 1 (std11-extract-address-components addr))
377 (if decoder (funcall decoder addr) addr)))
379 (and wl-reply-subject-prefix
380 (setq subject (concat wl-reply-subject-prefix
381 (wl-draft-strip-subject-re
383 (setq in-reply-to (std11-field-body "Message-Id"))
384 (setq references (nconc
385 (std11-field-bodies '("References" "In-Reply-To"))
387 (setq to (delq nil (mapcar 'car to-alist)))
388 (setq cc (delq nil (mapcar 'car cc-alist)))
389 ;; if subscribed mailing list is contained in cc or to
390 ;; and myself is contained in cc,
391 ;; delete myself from cc.
392 (setq cc (wl-draft-delete-myself-from-cc to cc))
393 (when wl-insert-mail-followup-to
394 (setq mail-followup-to
395 (wl-draft-make-mail-followup-to (append to cc)))
396 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
397 (with-temp-buffer ; to keep raw buffer unibyte.
398 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
399 (setq newsgroups (wl-parse newsgroups
400 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
401 newsgroups (wl-delete-duplicates newsgroups)
406 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
407 (if decoder (funcall decoder grp) grp))
409 (setq to (wl-delete-duplicates to nil t))
410 (setq cc (wl-delete-duplicates
411 (append (wl-delete-duplicates cc nil t)
412 to (copy-sequence to))
414 (and to (setq to (mapconcat
416 (if wl-draft-reply-use-address-with-full-name
417 (or (cdr (assoc addr to-alist)) addr)
420 (and cc (setq cc (mapconcat
422 (if wl-draft-reply-use-address-with-full-name
423 (or (cdr (assoc addr cc-alist)) addr)
426 (and mail-followup-to
427 (setq mail-followup-to
430 (if wl-draft-reply-use-address-with-full-name
431 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
433 mail-followup-to ",\n\t")))
434 (and (null to) (setq to cc cc nil))
435 (setq references (delq nil references)
436 references (mapconcat 'identity references " ")
437 references (wl-draft-parse-msg-id-list-string references)
438 references (wl-delete-duplicates references)
439 references (if references
440 (mapconcat 'identity references "\n\t")))
441 (and wl-draft-use-frame
442 (get-buffer-window summary-buf)
443 (select-window (get-buffer-window summary-buf)))
444 (wl-draft (list (cons 'To to)
446 (cons 'Newsgroups newsgroups)
447 (cons 'Subject subject)
448 (cons 'In-Reply-To in-reply-to)
449 (cons 'References references)
450 (cons 'Mail-Followup-To mail-followup-to))
451 nil nil nil nil parent-folder)
452 (setq wl-draft-parent-number number)
453 (setq wl-draft-reply-buffer buf)
454 (setq wl-draft-config-variables
455 (append wl-draft-reply-saved-variables
456 wl-draft-config-variables)))
457 (run-hooks 'wl-reply-hook))
459 (defun wl-draft-reply-position (position)
460 (cond ((eq position 'body)
461 (wl-draft-body-goto-top))
462 ((eq position 'bottom)
463 (wl-draft-body-goto-bottom))
465 (goto-char (point-min)))
466 ((and (stringp position)
467 (std11-field-body position))
468 (progn (mail-position-on-field position)
469 (wl-draft-beginning-of-line)))
471 (while (car position)
472 (wl-draft-reply-position (car position))
473 (setq position (cdr position))))))
475 (defun wl-draft-add-references ()
476 (wl-draft-add-in-reply-to "References"))
478 (defun wl-draft-add-in-reply-to (&optional alt-field)
479 (let* ((mes-id (save-excursion
480 (set-buffer mail-reply-buffer)
481 (std11-field-body "message-id")))
482 (field (or alt-field "In-Reply-To"))
483 (ref (std11-field-body field))
484 (ref-list nil) (st nil))
485 (when (and mes-id ref)
486 (while (string-match "<[^>]+>" ref st)
488 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
491 (member mes-id ref-list))
495 (when (mail-position-on-field field)
497 (while (looking-at "^[ \t]")
499 (setq mes-id (concat "\t" mes-id "\n")))
503 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
504 &optional ignored-fields)
507 (narrow-to-region (point)(point))
509 (with-current-buffer mail-reply-buffer
511 (decode-mime-charset-region (point-min) (point-max)
513 (buffer-substring-no-properties
514 (point-min) (point-max))))
516 (goto-char (point-min))
517 (wl-draft-delete-fields ignored-fields))
518 (goto-char (point-max))
519 (push-mark (point) nil t)
520 (goto-char (point-min)))
522 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
523 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
524 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
525 (run-hooks 'wl-draft-cited-hook)
526 (when (if wl-draft-add-references
527 (wl-draft-add-references)
528 (if wl-draft-add-in-reply-to
529 (wl-draft-add-in-reply-to)))
530 (wl-highlight-headers 'for-draft)) ; highlight when added References:
531 (when wl-highlight-body-too
532 (wl-highlight-body-region beg (point-max)))))
534 (defun wl-message-news-p ()
535 "If exist valid Newsgroups field, return non-nil."
536 (std11-field-body "Newsgroups"))
538 (defun wl-message-field-exists-p (field)
539 "If FIELD exist and FIELD value is not empty, return non-nil."
540 (let ((value (std11-field-body field)))
542 (not (string= value "")))))
544 (defun wl-message-mail-p ()
545 "If exist To, Cc or Bcc field, return non-nil."
546 (or (wl-message-field-exists-p "To")
547 (wl-message-field-exists-p "Resent-to")
548 (wl-message-field-exists-p "Cc")
549 (wl-message-field-exists-p "Bcc")
550 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
551 ;;; This may be needed..
552 ;;; (wl-message-field-exists-p "Fcc")
555 (defun wl-draft-edit-string (string)
556 (let ((cur-buf (current-buffer))
557 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
558 to subject in-reply-to cc references newsgroups mail-followup-to
559 content-type content-transfer-encoding from
564 (setq to (std11-field-body "To"))
567 (decode-mime-charset-string
570 (setq subject (std11-field-body "Subject"))
571 (setq subject (and subject
573 (decode-mime-charset-string
576 (setq from (std11-field-body "From")
579 (decode-mime-charset-string
582 (setq in-reply-to (std11-field-body "In-Reply-To"))
583 (setq cc (std11-field-body "Cc"))
586 (decode-mime-charset-string
589 (setq references (std11-field-body "References"))
590 (setq newsgroups (std11-field-body "Newsgroups"))
591 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
592 (setq content-type (std11-field-body "Content-Type"))
593 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
594 (goto-char (point-min))
595 (or (re-search-forward "\n\n" nil t)
596 (search-forward (concat mail-header-separator "\n") nil t))
602 (nth 1 (std11-extract-address-components from))
603 wl-user-mail-address-list)
607 (cons 'Subject subject)
608 (cons 'Newsgroups newsgroups)
609 (cons 'Mail-Followup-To mail-followup-to)
610 (cons 'In-Reply-To in-reply-to)
611 (cons 'References references))
612 content-type content-transfer-encoding
613 (buffer-substring (point) (point-max))
615 (kill-buffer tmp-buf))
616 ;; Set cursor point to the top.
617 (goto-char (point-min))
618 (search-forward (concat mail-header-separator "\n") nil t)
619 (run-hooks 'wl-draft-reedit-hook)
620 (and to (mail-position-on-field "To"))))
622 (defun wl-draft-insert-current-message (dummy)
624 (let (original-buffer
626 mail-citation-hook mail-yank-hooks
627 wl-draft-add-references wl-draft-add-in-reply-to
628 wl-draft-cite-function)
629 (with-current-buffer wl-draft-buffer-cur-summary-buffer
630 (with-current-buffer wl-message-buffer
631 (setq original-buffer (wl-message-get-original-buffer))
633 (with-current-buffer original-buffer
635 (error "No current message"))))
636 (setq mail-reply-buffer original-buffer)
637 (wl-draft-yank-from-mail-reply-buffer
639 wl-ignored-forwarded-headers)))
641 (defun wl-draft-insert-get-message (dummy)
642 (let ((fld (completing-read
644 (if (memq 'read-folder wl-use-folder-petname)
645 (wl-folder-get-entity-with-petname)
646 wl-folder-entity-hashtb)
647 nil nil wl-default-spec
648 'wl-read-folder-history))
649 (number (call-interactively
650 (function (lambda (num)
651 (interactive "nNumber: ")
653 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
654 mail-citation-hook mail-yank-hooks
655 wl-draft-cite-function)
658 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
661 (elmo-make-fetch-strategy 'entire)
662 nil mail-reply-buffer)
663 (wl-draft-yank-from-mail-reply-buffer nil))
664 (kill-buffer mail-reply-buffer))))
667 ;; default body citation func
669 (defun wl-default-draft-cite ()
670 (let ((mail-yank-ignored-headers "[^:]+:")
671 (mail-yank-prefix "> ")
672 date from cite-title)
674 (if (< (mark t) (point))
675 (exchange-point-and-mark))
676 (narrow-to-region (point)(point-max))
677 (setq date (std11-field-body "date")
678 from (std11-field-body "from")))
680 (insert (format "At %s,\n%s wrote:\n"
681 (or date "some time ago")
682 (if wl-default-draft-cite-decorate-author
683 (funcall wl-summary-from-function
686 (mail-indent-citation)))
688 (defvar wl-draft-buffer nil "Draft buffer to yank content.")
689 (defun wl-draft-yank-to-draft-buffer (buffer)
690 "Yank BUFFER content to `wl-draft-buffer'."
691 (set-buffer wl-draft-buffer)
692 (let ((mail-reply-buffer buffer))
693 (wl-draft-yank-from-mail-reply-buffer nil)
694 (kill-buffer buffer)))
696 (defun wl-draft-yank-original (&optional arg)
697 "Yank original message."
700 (let (buf mail-reply-buffer)
704 (setq buf (current-buffer)))
705 (setq mail-reply-buffer buf)
706 (wl-draft-yank-from-mail-reply-buffer nil))
707 (wl-draft-yank-current-message-entity)))
709 (defun wl-draft-hide (editing-buffer)
710 "Hide the editing draft buffer if possible."
711 (when (and editing-buffer
712 (buffer-live-p editing-buffer)
713 (get-buffer-window editing-buffer))
714 (select-window (get-buffer-window editing-buffer))
715 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
716 fld-buf sum-win fld-win)
717 (if (and wl-draft-use-frame
718 (> (length (visible-frame-list)) 1))
724 ;; stay folder window if required
725 (when wl-stay-folder-window
726 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
727 (if (setq fld-win (get-buffer-window fld-buf))
728 (select-window fld-win)
729 (if wl-draft-resume-folder-window ;; resume folder window
730 (switch-to-buffer fld-buf)))))
731 (if (buffer-live-p sum-buf)
732 (if (setq sum-win (get-buffer-window sum-buf t))
733 ;; if Summary is on the frame, select it.
734 (select-window sum-win)
735 ;; if summary is not on the frame, switch to it.
736 (if (and wl-stay-folder-window
737 (or wl-draft-resume-folder-window fld-win))
738 (wl-folder-select-buffer sum-buf)
739 (switch-to-buffer sum-buf))))))))
741 (defun wl-draft-delete (editing-buffer)
742 "Kill the editing draft buffer and delete the file corresponds to it."
745 (set-buffer editing-buffer)
746 (when wl-draft-buffer-message-number
747 (elmo-folder-delete-messages (wl-draft-get-folder)
749 wl-draft-buffer-message-number))
750 (wl-draft-config-info-operation wl-draft-buffer-message-number
752 (set-buffer-modified-p nil) ; force kill
753 (kill-buffer editing-buffer))))
755 (defun wl-draft-kill (&optional force-kill)
756 "Kill current draft buffer and quit editing."
759 (when (and (or (eq major-mode 'wl-draft-mode)
760 (eq major-mode 'mail-mode))
762 (yes-or-no-p "Kill Current Draft? ")))
763 (let ((cur-buf (current-buffer)))
764 (when (and wl-draft-parent-number
765 (not (string= wl-draft-parent-folder "")))
766 (let* ((number wl-draft-parent-number)
767 (folder-name wl-draft-parent-folder)
768 (folder (wl-folder-get-elmo-folder folder-name))
770 (if (and (setq buffer (wl-summary-get-buffer folder-name))
771 (with-current-buffer buffer
772 (string= (wl-summary-buffer-folder-name)
774 (with-current-buffer buffer
775 (elmo-folder-unset-flag folder (list number) 'answered)
776 (when (wl-summary-jump-to-msg number)
777 (wl-summary-update-persistent-mark)))
778 (elmo-folder-open folder 'load-msgdb)
779 (elmo-folder-unset-flag folder (list number) 'answered)
780 (elmo-folder-close folder))))
781 (wl-draft-hide cur-buf)
782 (wl-draft-delete cur-buf)))
785 (defun wl-draft-fcc ()
786 "Add a new Fcc field, with file name completion."
788 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
789 (mail-position-on-field "to"))
792 ;; Imported from message.el.
793 (defun wl-draft-elide-region (b e)
794 "Elide the text in the region.
795 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
799 (insert wl-draft-elide-ellipsis))
801 ;; Imported from message.el.
802 (defun wl-draft-beginning-of-line (&optional n)
803 "Move point to beginning of header value or to beginning of line."
805 (let ((zrs 'zmacs-region-stays))
806 (when (and (interactive-p) (boundp zrs))
808 (if (wl-draft-point-in-header-p)
809 (let* ((here (point))
810 (bol (progn (beginning-of-line n) (point)))
811 (eol (line-end-position))
812 (eoh (and (looking-at "[^ \t]")
813 (re-search-forward ": *" eol t))))
814 (if (and eoh (or (> here eoh) (= here bol)))
817 (beginning-of-line n)))
819 (defun wl-draft-point-in-header-p ()
820 "Return t if point is in the header."
823 (goto-char (point-min))
824 (not (re-search-forward
825 (concat "^" (regexp-quote mail-header-separator) "\n")
828 ;; function for wl-sent-message-via
830 (defmacro wl-draft-sent-message-p (type)
831 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
833 (defmacro wl-draft-set-sent-message (type result &optional server-port)
834 (` (let ((element (assq (, type) wl-sent-message-via)))
836 (unless (eq (nth 1 element) (, result))
837 (setcdr element (list (, result) (, server-port)))
838 (setq wl-sent-message-modified t))
839 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
840 (setq wl-sent-message-modified t)))))
842 (defun wl-draft-sent-message-results ()
843 (let ((results wl-sent-message-via)
844 unplugged-via sent-via)
846 (cond ((eq (nth 1 (car results)) 'unplugged)
847 (push (caar results) unplugged-via))
848 ((eq (nth 1 (car results)) 'sent)
849 (push (caar results) sent-via)))
850 (setq results (cdr results)))
851 (list unplugged-via sent-via)))
853 (defun wl-draft-write-sendlog (status proto server to id)
854 "Write send log file, if `wl-draft-sendlog' is non-nil."
855 (when wl-draft-sendlog
857 (let* ((filename (expand-file-name wl-draft-sendlog-filename
858 elmo-msgdb-directory))
859 (filesize (nth 7 (file-attributes filename)))
860 (server (if server (concat " server=" server) ""))
862 ((memq proto '(fcc queue))
863 (format " folder=\"%s\"" to))
865 (format " ng=<%s>" to))
870 (mapcar '(lambda(x) (format "<%s>" x)) to)
873 (id (if id (concat " id=" id) ""))
874 (time (format-time-string "%Y/%m/%d %T")))
875 (insert (format "%s proto=%s stat=%s%s%s%s\n"
876 time proto status server to id))
877 (if (and wl-draft-sendlog-max-size filesize
878 (> filesize wl-draft-sendlog-max-size))
879 (rename-file filename (concat filename ".old") t))
880 (if (file-writable-p filename)
881 (write-region-as-binary (point-min) (point-max)
883 (message "%s is not writable." filename))))))
885 (defun wl-draft-get-header-delimiter (&optional delete)
886 ;; If DELETE is non-nil, replace the header delimiter with a blank line
888 (goto-char (point-min))
889 (when (re-search-forward
890 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
894 (setq delimline (point-marker)))
897 (defun wl-draft-send-mail-with-qmail ()
898 "Pass the prepared message buffer to qmail-inject.
899 Refer to the documentation for the variable `send-mail-function'
900 to find out how to use this."
901 (if (and wl-draft-qmail-send-plugged
902 (not (elmo-plugged-p)))
903 (wl-draft-set-sent-message 'mail 'unplugged)
905 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
906 (let ((id (std11-field-body "Message-ID"))
907 (to (std11-field-body "To")))
911 'call-process-region 1 (point-max) wl-qmail-inject-program
913 wl-qmail-inject-args))
914 ;; qmail-inject doesn't say anything on it's stdout/stderr,
915 ;; we have to look at the retval instead
917 (wl-draft-set-sent-message 'mail 'sent)
918 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
919 (1 (error "`qmail-inject' reported permanent failure"))
920 (111 (error "`qmail-inject' reported transient failure"))
921 ;; should never happen
922 (t (error "`qmail-inject' reported unknown failure"))))))
924 (defun wl-draft-parse-msg-id-list-string (string)
925 "Get msg-id list from STRING."
927 (dolist (parsed-id (std11-parse-msg-ids-string string))
928 (when (eq (car parsed-id) 'msg-id)
929 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
931 (nreverse msg-id-list)))
933 (defun wl-draft-eword-encode-address-list (string &optional column)
934 "Encode header field STRING as list of address, and return the result.
935 Cause an error when STRING contains invalid address.
936 Optional argument COLUMN is start-position of the field."
937 (car (eword-encode-rword-list
938 (or column eword-encode-default-start-column)
939 (eword-encode-addresses-to-rword-list
940 (wl-draft-std11-parse-addresses (std11-lexical-analyze string))))))
942 (defun wl-draft-std11-parse-addresses (lal)
943 (let ((ret (std11-parse-address lal)))
944 (when (and (not (and (eq (length lal) 1)
945 (eq (car (car lal)) 'spaces)))
947 (error "Error while parsing address"))
949 (let ((dest (list (car ret))))
951 (while (and (setq ret (std11-parse-ascii-token lal))
952 (string-equal (cdr (assq 'specials (car ret))) ",")
953 (setq ret (std11-parse-address (cdr ret)))
955 (setq dest (cons (car ret) dest))
956 (setq lal (cdr ret)))
957 (while (eq 'spaces (car (car lal)))
958 (setq lal (cdr lal)))
959 (if lal (error "Error while parsing address"))
962 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
963 "Get mailbox list of FIELD from current buffer.
964 The buffer is expected to be narrowed to just the headers of the message.
965 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
966 from current buffer."
968 (let ((case-fold-search t)
969 (inhibit-read-only t)
971 mailbox-list beg seq has-group-list)
972 (goto-char (point-min))
973 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
976 (re-search-forward "^[^ \t]" nil 'move)
978 (skip-chars-backward "\n")
979 (setq seq (std11-lexical-analyze
980 (buffer-substring-no-properties beg (point))))
981 (setq addresses (wl-draft-std11-parse-addresses seq))
983 (cond ((eq (car (car addresses)) 'group)
984 (setq has-group-list t)
988 'std11-address-string
989 (nth 2 (car addresses))))))
990 ((eq (car (car addresses)) 'mailbox)
991 (setq address (nth 1 (car addresses)))
995 (std11-addr-to-string
996 (if (eq (car address) 'phrase-route-addr)
999 (setq addresses (cdr addresses)))
1000 (when (and remove-group-list has-group-list)
1001 (delete-region beg (point))
1002 (insert (wl-address-string-without-group-list-contents seq))))
1005 (defun wl-draft-deduce-address-list (buffer header-start header-end)
1006 "Get address list suitable for smtp RCPT TO:<address>.
1007 Group list content is removed if `wl-draft-remove-group-list-contents' is
1009 (let ((fields (if (and wl-draft-doing-mime-bcc
1010 wl-draft-disable-bcc-for-mime-bcc)
1012 '("to" "cc" "bcc")))
1013 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
1014 (case-fold-search t)
1018 (narrow-to-region header-start header-end)
1019 (goto-char (point-min))
1021 (if (re-search-forward "^resent-to[\t ]*:" nil t)
1022 (setq fields resent-fields)))
1026 (wl-draft-parse-mailbox-list
1028 wl-draft-remove-group-list-contents)))
1029 (setq fields (cdr fields)))
1035 (defun wl-draft-send-mail-with-smtp ()
1036 "Send the prepared message buffer with SMTP."
1038 (let* ((errbuf (if mail-interactive
1039 (generate-new-buffer " smtp errors")
1041 (case-fold-search t)
1042 (default-case-fold-search t)
1043 (sender (or wl-envelope-from
1044 (wl-address-header-extract-address wl-from)))
1045 (delimline (save-excursion
1046 (goto-char (point-min))
1048 (concat "^" (regexp-quote mail-header-separator)
1052 (or wl-smtp-posting-server smtp-server "localhost"))
1053 (smtp-service (or wl-smtp-posting-port smtp-service))
1054 (smtp-local-domain (or smtp-local-domain wl-local-domain))
1055 (id (std11-field-body "message-id"))
1057 (if (not (elmo-plugged-p smtp-server smtp-service))
1058 (wl-draft-set-sent-message 'mail 'unplugged
1059 (cons smtp-server smtp-service))
1062 ;; Instead of `smtp-deduce-address-list'.
1063 (setq recipients (wl-draft-deduce-address-list
1064 (current-buffer) (point-min) delimline))
1065 (unless recipients (error "No recipients"))
1066 ;; Insert an extra newline if we need it to work around
1067 ;; Sun's bug that swallows newlines.
1068 (goto-char (1+ delimline))
1069 (if (eval mail-mailer-swallows-blank-line)
1071 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1072 (if mail-interactive
1076 (wl-draft-delete-field "bcc" delimline)
1077 (wl-draft-delete-field "resent-bcc" delimline)
1078 (let (process-connection-type)
1081 (wl-smtp-extension-bind
1083 (smtp-send-buffer sender recipients (current-buffer))
1085 (wl-draft-write-sendlog 'failed 'smtp smtp-server
1087 (if (and (eq (car err) 'smtp-response-error)
1088 (= (nth 1 err) 535))
1090 (wl-smtp-password-key
1092 (car smtp-sasl-mechanisms)
1094 (signal (car err) (cdr err)))
1096 (wl-draft-write-sendlog 'uncertain 'smtp smtp-server
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 (narrow-to-region (goto-char (point-min))
1155 (if (re-search-forward
1157 "^" (regexp-quote mail-header-separator) "$")
1161 (wl-draft-insert-required-fields)
1162 ;; ignore any blank lines in the header
1163 (while (progn (goto-char (point-min))
1164 (re-search-forward "\n[ \t]*\n\n*" nil t))
1165 (replace-match "\n"))
1166 (goto-char (point-min))
1167 (while (re-search-forward
1168 "^[^ \t\n:]+:[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n"
1170 (when (string= "" (match-string 1))
1171 (replace-match ""))))
1172 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1173 (wl-draft-dispatch-message)
1174 (when kill-when-done
1175 ;; hide editing-buffer.
1176 (wl-draft-hide editing-buffer)
1177 ;; delete editing-buffer and its file.
1178 (wl-draft-delete editing-buffer)))
1180 (defun wl-draft-dispatch-message (&optional mes-string)
1181 "Send the message in the current buffer. Not modified the header fields."
1182 (let (delimline mime-bcc)
1183 (if (and wl-draft-verbose-send mes-string)
1184 (message "%s" mes-string))
1186 (setq delimline (wl-draft-get-header-delimiter t))
1187 (unless wl-draft-fcc-list
1188 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1190 (setq wl-sent-message-modified nil)
1193 (if (and (wl-message-mail-p)
1194 (not (wl-draft-sent-message-p 'mail)))
1195 (if (or (not (or wl-draft-force-queuing
1196 wl-draft-force-queuing-mail))
1197 (memq 'mail wl-sent-message-queued))
1199 (setq mime-bcc (wl-draft-mime-bcc-field))
1200 (funcall wl-draft-send-mail-function)
1201 (when (not (zerop (length mime-bcc)))
1202 (wl-draft-do-mime-bcc mime-bcc)))
1203 (push 'mail wl-sent-message-queued)
1204 (wl-draft-set-sent-message 'mail 'unplugged)))
1205 (if (and (wl-message-news-p)
1206 (not (wl-draft-sent-message-p 'news))
1207 (not (wl-message-field-exists-p "Resent-to")))
1208 (if (or (not (or wl-draft-force-queuing
1209 wl-draft-force-queuing-news))
1210 (memq 'news wl-sent-message-queued))
1211 (funcall wl-draft-send-news-function)
1212 (push 'news wl-sent-message-queued)
1213 (wl-draft-set-sent-message 'news 'unplugged))))
1214 (let* ((status (wl-draft-sent-message-results))
1215 (unplugged-via (car status))
1216 (sent-via (nth 1 status)))
1217 ;; If one sent, process fcc folder.
1218 (if (and sent-via wl-draft-fcc-list)
1220 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1222 (setq wl-draft-fcc-list nil)))
1223 (if wl-draft-use-cache
1224 (let ((id (std11-field-body "Message-ID"))
1225 (elmo-enable-disconnected-operation t))
1226 (elmo-file-cache-save (elmo-file-cache-get-path id)
1228 ;; If one unplugged, append queue.
1229 (when (and unplugged-via
1230 wl-sent-message-modified)
1231 (if wl-draft-enable-queuing
1233 (wl-draft-queue-append wl-sent-message-via)
1234 (setq wl-sent-message-modified 'requeue))
1235 (error "Unplugged")))
1236 (when wl-draft-verbose-send
1237 (if (and unplugged-via sent-via);; combined message
1239 (setq wl-draft-verbose-msg
1240 (format "Sending%s and Queuing%s..."
1241 sent-via unplugged-via))
1242 (message "%sdone" wl-draft-verbose-msg))
1246 (if sent-via "done" "failed"))))))))
1247 (not wl-sent-message-modified)) ;; return value
1249 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1250 "Force send current buffer as raw message."
1253 (let (wl-interactive-send
1254 ;;; wl-draft-verbose-send
1255 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1256 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1259 (wl-draft-send kill-when-done mes-string))))
1261 (defun wl-draft-clone-local-variables ()
1262 (let ((locals (buffer-local-variables))
1265 (when (and (consp (car locals))
1267 (string-match wl-draft-clone-local-variable-regexp
1268 (symbol-name (car (car locals)))))
1269 (wl-append result (list (car (car locals)))))
1270 (setq locals (cdr locals)))
1273 (defcustom wl-draft-send-confirm-with-preview t
1274 "Non-nil to invoke preview through confirmation of sending.
1275 This variable is valid when `wl-interactive-send' has non-nil value."
1279 (defun wl-draft-send-confirm ()
1282 (condition-case quit
1284 (when wl-draft-send-confirm-with-preview
1285 (wl-draft-preview-message))
1287 (goto-char (point-min)) ; to show recipients in header
1290 (message "Send current draft? <y/n/j(down)/k(up)> ")
1291 (setq answer (let ((cursor-in-echo-area t)) (read-char)))
1310 (throw 'done nil)))))))
1312 (when wl-draft-send-confirm-with-preview
1313 (mime-preview-quit)))))
1315 (defun wl-draft-send (&optional kill-when-done mes-string)
1316 "Send current draft message.
1317 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1319 ;; Don't call this explicitly.
1320 ;; Added to 'wl-draft-send-hook (by teranisi)
1321 ;; (wl-draft-config-exec)
1322 (run-hooks 'wl-draft-send-hook)
1323 (when (or (not wl-interactive-send)
1324 (wl-draft-send-confirm))
1325 (let ((send-mail-function 'wl-draft-raw-send)
1326 (editing-buffer (current-buffer))
1327 (sending-buffer (wl-draft-generate-clone-buffer
1328 " *wl-draft-sending-buffer*"
1329 (append wl-draft-config-variables
1330 (wl-draft-clone-local-variables))))
1331 (wl-draft-verbose-msg nil)
1335 (set-buffer sending-buffer)
1336 (if (and (not (wl-message-mail-p))
1337 (not (wl-message-news-p)))
1338 (error "No recipient is specified"))
1339 (expand-abbrev) ; for mail-abbrevs
1340 (let ((mime-header-encode-method-alist
1342 '((wl-draft-eword-encode-address-list
1343 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
1344 (if (boundp 'mime-header-encode-method-alist)
1345 (symbol-value 'mime-header-encode-method-alist)))))
1346 (run-hooks 'mail-send-hook) ; translate buffer
1349 (if wl-draft-verbose-send
1350 (message "%s" (or mes-string "Sending...")))
1351 (funcall wl-draft-send-function editing-buffer kill-when-done)
1352 ;; Now perform actions on successful sending.
1353 (while mail-send-actions
1355 (apply (car (car mail-send-actions))
1356 (cdr (car mail-send-actions)))
1358 (setq mail-send-actions (cdr mail-send-actions)))
1359 (if wl-draft-verbose-send
1361 (or wl-draft-verbose-msg
1364 ;; kill sending buffer, anyway.
1365 (and (buffer-live-p sending-buffer)
1366 (kill-buffer sending-buffer))))))
1368 (defun wl-draft-mime-bcc-field ()
1369 "Return the MIME-Bcc field body. The field is deleted."
1370 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1371 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1373 (defun wl-draft-do-mime-bcc (field-body)
1374 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1375 (let ((orig-from (mime-decode-field-body (std11-field-body "from")
1377 (orig-subj (mime-decode-field-body (or (std11-field-body "subject")
1380 (recipients (wl-parse-addresses field-body))
1381 (draft-buffer (current-buffer))
1383 (save-window-excursion
1384 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1385 (not (zerop (length field-body))))
1386 (let ((wl-draft-doing-mime-bcc t))
1387 (dolist (recipient recipients)
1388 (wl-draft-create-buffer)
1389 (wl-draft-create-contents
1390 (append `((From . ,orig-from)
1392 (Subject . ,(concat "A blind carbon copy ("
1395 (wl-draft-default-headers)))
1396 (wl-draft-insert-mail-header-separator)
1397 (wl-draft-prepare-edit)
1398 (goto-char (point-max))
1399 (insert (or wl-draft-mime-bcc-body
1400 "This is a blind carbon copy.")
1402 (mime-edit-insert-tag "message" "rfc822")
1403 (insert-buffer draft-buffer)
1404 (let (wl-interactive-send)
1405 (wl-draft-send 'kill-when-done))))))))
1407 (defun wl-draft-save ()
1408 "Save current draft."
1410 (if (buffer-modified-p)
1412 (message "Saving...")
1413 (let ((msg (buffer-substring-no-properties (point-min) (point-max)))
1415 (when wl-draft-buffer-message-number
1416 (elmo-folder-delete-messages (wl-draft-get-folder)
1417 (list wl-draft-buffer-message-number))
1418 (wl-draft-config-info-operation wl-draft-buffer-message-number
1420 (elmo-folder-check (wl-draft-get-folder))
1421 ;; If no header separator, insert it.
1424 (goto-char (point-min))
1425 (unless (re-search-forward
1426 (concat "^" (regexp-quote mail-header-separator) "$")
1428 (goto-char (point-min))
1429 (if (re-search-forward "\n\n" nil t)
1430 (replace-match (concat "\n" mail-header-separator "\n"))
1431 (goto-char (point-max))
1432 (insert (if (eq (char-before) ?\n) "" "\n")
1433 mail-header-separator "\n")))
1434 (let ((mime-header-encode-method-alist
1435 '((eword-encode-unstructured-field-body))))
1436 (mime-edit-translate-buffer))
1437 (wl-draft-get-header-delimiter t)
1439 (elmo-folder-next-message-number (wl-draft-get-folder)))
1440 (elmo-folder-append-buffer (wl-draft-get-folder)))
1441 (elmo-folder-check (wl-draft-get-folder))
1442 (elmo-folder-commit (wl-draft-get-folder))
1443 (setq wl-draft-buffer-message-number next-number)
1444 (rename-buffer (format "%s/%d" wl-draft-folder next-number))
1445 (setq buffer-file-name (buffer-name))
1446 (set-buffer-modified-p nil)
1447 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save)
1448 (message "Saving...done")))
1449 (message "(No changes need to be saved)")))
1451 (defun wl-draft-mimic-kill-buffer ()
1452 "Kill the current (draft) buffer with query."
1454 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1457 (if (or (not bufname)
1458 (string-equal bufname "")
1459 (string-equal bufname (buffer-name)))
1460 (let ((bufname (current-buffer)))
1461 (when (or (not (buffer-modified-p))
1463 (format "Buffer %s modified; kill anyway? " bufname)))
1464 (set-buffer-modified-p nil)
1465 (wl-draft-hide bufname)
1466 (kill-buffer bufname)))
1467 (kill-buffer bufname))))
1469 (defun wl-draft-save-and-exit ()
1470 "Save current draft and exit current draft mode."
1473 (let ((editing-buffer (current-buffer)))
1474 (wl-draft-hide editing-buffer)
1475 (kill-buffer editing-buffer)))
1477 (defun wl-draft-send-and-exit ()
1478 "Send current draft message and kill it."
1482 (defun wl-draft-send-from-toolbar ()
1484 (let ((wl-interactive-send t))
1485 (wl-draft-send-and-exit)))
1487 (defun wl-draft-delete-field (field &optional delimline replace)
1488 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1490 (defun wl-draft-delete-fields (field &optional delimline replace)
1493 (goto-char (point-min))
1494 (if (search-forward "\n\n" nil t)
1495 (setq delimline (point))
1496 (setq delimline (point-max))))
1497 (narrow-to-region (point-min) delimline)
1498 (goto-char (point-min))
1499 (let ((regexp (concat "^" field ":"))
1500 (case-fold-search t))
1502 (if (looking-at regexp)
1508 (if (re-search-forward "^[^ \t]" nil t)
1509 (goto-char (match-beginning 0))
1512 (insert (concat field ": " replace "\n"))))
1514 (if (re-search-forward "^[^ \t]" nil t)
1515 (goto-char (match-beginning 0))
1518 (defun wl-draft-get-fcc-list (header-end)
1519 (if (and wl-draft-doing-mime-bcc
1520 wl-draft-disable-fcc-for-mime-bcc)
1522 (wl-draft-delete-field "fcc")
1525 (case-fold-search t))
1526 (or (markerp header-end) (error "HEADER-END must be a marker"))
1528 (goto-char (point-min))
1529 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1534 (buffer-substring-no-properties
1538 (skip-chars-backward " \t")
1541 (dolist (folder fcc-list)
1542 (wl-folder-confirm-existence
1543 (wl-folder-get-elmo-folder (eword-decode-string folder)))))
1544 (delete-region (match-beginning 0)
1545 (progn (forward-line 1) (point)))))
1548 (defcustom wl-draft-fcc-append-read-folder-history t
1549 "Non-nil to append fcc'ed folder to `wl-read-folder-history'."
1553 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1554 (let ((send-mail-buffer (current-buffer))
1555 (tembuf (generate-new-buffer " fcc output"))
1556 (case-fold-search t)
1558 (or (markerp header-end) (error "HEADER-END must be a marker"))
1561 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1564 ;; insert just the headers to avoid moving the gap more than
1565 ;; necessary (the message body could be arbitrarily huge.)
1566 (insert-buffer-substring send-mail-buffer 1 header-end)
1567 (wl-draft-insert-required-fields t)
1568 (goto-char (point-max))
1569 (insert-buffer-substring send-mail-buffer header-end)
1570 (let ((id (std11-field-body "Message-ID"))
1571 (elmo-enable-disconnected-operation t)
1574 (unless (or cache-saved
1575 (elmo-folder-plugged-p
1576 (wl-folder-get-elmo-folder (car fcc-list))))
1577 (elmo-file-cache-save id nil) ;; for disconnected operation
1578 (setq cache-saved t))
1579 (if (elmo-folder-append-buffer
1580 (wl-folder-get-elmo-folder
1581 (eword-decode-string (car fcc-list)))
1582 (and wl-fcc-force-as-read '(read)))
1583 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1584 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1585 (if (and wl-draft-fcc-append-read-folder-history
1586 (boundp 'wl-read-folder-history))
1587 (or (equal (car fcc-list) (car wl-read-folder-history))
1588 (setq wl-read-folder-history
1589 (append (list (car fcc-list)) wl-read-folder-history))))
1590 (setq fcc-list (cdr fcc-list)))))
1591 (kill-buffer tembuf)))
1593 (defun wl-draft-on-field-p ()
1596 (goto-char (point-min))
1597 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1604 (if (or (looking-at ".*,[ \t]?$")
1605 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1607 (let ((pos (point)))
1610 (if (looking-at "^[ \t]")
1612 (if (re-search-forward ":" pos t) nil t)))))))
1616 (defun wl-draft (&optional header-alist
1617 content-type content-transfer-encoding
1620 "Write and send mail/news message with Wanderlust."
1627 (wl-plugged-init t))
1629 (wl-init)) ; returns immediately if already initialized.
1632 (let (buffer header-alist-internal)
1633 (setq buffer (wl-draft-create-buffer parent-folder))
1634 (unless (cdr (assq 'From header-alist))
1636 (append (list (cons 'From wl-from)) header-alist)))
1637 (unless (cdr (assq 'To header-alist))
1642 (if (assq 'To header-alist)
1643 (setcdr (assq 'To header-alist) to)
1645 (append header-alist
1646 (list (cons 'To to))))))))
1647 (unless (cdr (assq 'Subject header-alist))
1648 (if (assq 'Subject header-alist)
1649 (setcdr (assq 'Subject header-alist) "")
1651 (append header-alist (list (cons 'Subject ""))))))
1652 (setq header-alist (append header-alist
1653 (wl-draft-default-headers)
1654 wl-draft-additional-header-alist
1655 (if body (list "" (cons 'Body body)))))
1656 (wl-draft-create-contents header-alist)
1658 (wl-draft-decode-body
1659 content-type content-transfer-encoding))
1660 (wl-draft-insert-mail-header-separator)
1661 (wl-draft-prepare-edit)
1663 (run-hooks 'wl-mail-setup-hook))
1664 (goto-char (point-min))
1665 (setq buffer-undo-list nil)
1666 (wl-user-agent-compose-internal) ;; user-agent
1669 (string= (cdr (assq 'To header-alist)) ""))
1670 (mail-position-on-field "To"))
1672 (goto-char (point-max))))
1675 (defun wl-draft-create-buffer (&optional parent-folder)
1676 (let* ((draft-folder (wl-draft-get-folder))
1677 (parent-folder (or parent-folder (wl-summary-buffer-folder-name)))
1678 (summary-buf (wl-summary-get-buffer parent-folder))
1680 (or (eq this-command 'wl-summary-reply)
1681 (eq this-command 'wl-summary-reply-with-citation)
1682 (eq this-command 'wl-summary-forward)
1683 (eq this-command 'wl-summary-target-mark-forward)
1684 (eq this-command 'wl-summary-target-mark-reply-with-citation)))
1685 (buffer (generate-new-buffer "*draft*")) ; Just for initial name.
1686 change-major-mode-hook)
1688 ;; switch-buffer according to draft buffer style.
1689 (if wl-draft-use-frame
1690 (switch-to-buffer-other-frame buffer)
1691 (if reply-or-forward
1692 (case wl-draft-reply-buffer-style
1694 (split-window-vertically)
1696 (switch-to-buffer buffer))
1698 (switch-to-buffer buffer))
1700 (delete-other-windows)
1701 (switch-to-buffer buffer))
1703 (if (functionp wl-draft-reply-buffer-style)
1704 (funcall wl-draft-reply-buffer-style buffer)
1705 (error "Invalid value for wl-draft-reply-buffer-style"))))
1706 (case wl-draft-buffer-style
1708 (when (eq major-mode 'wl-summary-mode)
1709 (wl-summary-toggle-disp-msg 'off))
1710 (split-window-vertically)
1712 (switch-to-buffer buffer))
1714 (switch-to-buffer buffer))
1716 (delete-other-windows)
1717 (switch-to-buffer buffer))
1718 (t (if (functionp wl-draft-buffer-style)
1719 (funcall wl-draft-buffer-style buffer)
1720 (error "Invalid value for wl-draft-buffer-style"))))))
1723 (make-local-variable 'truncate-partial-width-windows)
1724 (setq truncate-partial-width-windows nil)
1725 (setq truncate-lines wl-draft-truncate-lines)
1726 (setq wl-sent-message-via nil)
1727 (setq wl-sent-message-queued nil)
1728 (setq wl-draft-config-exec-flag t)
1729 (setq wl-draft-parent-folder (or parent-folder ""))
1730 (or (eq this-command 'wl-folder-write-current-folder)
1731 (setq wl-draft-buffer-cur-summary-buffer summary-buf))
1734 (defun wl-draft-create-contents (header-alist)
1735 "header-alist' sample
1736 '(function ;; funcall
1737 string ;; insert string
1738 (symbol . string) ;; insert symbol-value: string
1739 (symbol . function) ;; (funcall) and if it returns string,
1740 ;; insert symbol-value: string
1741 (symbol . nil) ;; do nothing
1744 (unless (eq major-mode 'wl-draft-mode)
1745 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1746 (let ((halist header-alist)
1751 ((functionp (car halist)) (funcall (car halist)))
1753 ((stringp (car halist)) (insert (car halist) "\n"))
1755 ((consp (car halist))
1756 (setq field (car (car halist)))
1757 (setq value (cdr (car halist)))
1761 ((eq field 'Body) ; body
1763 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1765 (let ((value-return (funcall value)))
1766 (when (stringp value-return)
1767 (insert (symbol-name field) ": " value-return "\n"))))
1776 (setq halist (cdr halist)))))
1778 (defun wl-draft-prepare-edit ()
1779 (unless (eq major-mode 'wl-draft-mode)
1780 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1781 (let (change-major-mode-hook)
1782 (wl-draft-editor-mode)
1783 (static-when (boundp 'auto-save-file-name-transforms)
1784 (make-local-variable 'auto-save-file-name-transforms)
1785 (setq auto-save-file-name-transforms
1786 (cons (list (concat (regexp-quote wl-draft-folder)
1788 (concat (expand-file-name
1790 (elmo-folder-msgdb-path
1791 (wl-draft-get-folder)))
1793 auto-save-file-name-transforms)))
1794 (when wl-draft-write-file-function
1795 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
1796 (wl-draft-overload-functions)
1797 (wl-highlight-headers 'for-draft)
1799 (clear-visited-file-modtime)))
1801 (defun wl-draft-decode-header ()
1803 (std11-narrow-to-header)
1804 (wl-draft-decode-message-in-buffer)
1807 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1810 (std11-field-body "content-type")))
1811 (content-transfer-encoding
1812 (or content-transfer-encoding
1813 (std11-field-body "content-transfer-encoding")))
1816 (std11-narrow-to-header)
1817 (wl-draft-delete-field "content-type")
1818 (wl-draft-delete-field "content-transfer-encoding")
1819 (goto-char (point-max))
1820 (setq delimline (point-marker))
1822 (narrow-to-region delimline (point-max))
1823 (goto-char (point-min))
1825 (insert "Content-type: " content-type "\n"))
1826 (when content-transfer-encoding
1827 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1828 (wl-draft-decode-message-in-buffer)
1829 (goto-char (point-min))
1830 (unless (re-search-forward "^$" (point-at-eol) t)
1835 ;;; subroutine for wl-draft-create-contents
1836 ;;; must be used in wl-draft-mode
1837 (defun wl-draft-check-new-line ()
1838 (if (not (= (preceding-char) ?\n))
1841 (defsubst wl-draft-trim-ccs (cc)
1847 (null (and wl-draft-delete-myself-from-bcc-fcc
1849 (mapcar 'wl-address-header-extract-address
1851 (wl-parse-addresses (std11-field-body "To"))
1852 (wl-parse-addresses (std11-field-body "Cc"))))
1853 (mapcar 'downcase wl-subscribed-mailing-list)))))
1857 (defsubst wl-draft-default-headers ()
1859 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1860 (wl-address-header-extract-address
1862 (cons 'User-Agent wl-generate-mailer-string-function)
1863 (cons 'Reply-To mail-default-reply-to)
1864 (cons 'Bcc (function
1867 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1868 (cons 'Fcc (function
1870 (wl-draft-trim-ccs wl-fcc))))
1871 (cons 'Organization wl-organization)
1872 (and wl-auto-insert-x-face
1873 (file-exists-p wl-x-face-file)
1874 'wl-draft-insert-x-face-field-here) ;; allow nil
1875 mail-default-headers
1876 ;; check \n at th end of line for `mail-default-headers'
1877 'wl-draft-check-new-line
1880 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1883 (goto-char delimline)
1884 (goto-char (point-min))
1885 (if (search-forward "\n\n" nil t)
1886 (delete-backward-char 1)
1887 (goto-char (point-max))))
1888 (wl-draft-check-new-line)
1889 (put-text-property (point)
1891 (insert mail-header-separator "\n")
1893 'category 'mail-header-separator)
1898 (defun wl-draft-elmo-nntp-send ()
1899 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1900 (elmo-nntp-default-user
1901 (or wl-nntp-posting-user elmo-nntp-default-user))
1902 (elmo-nntp-default-server
1903 (or wl-nntp-posting-server elmo-nntp-default-server))
1904 (elmo-nntp-default-port
1905 (or wl-nntp-posting-port elmo-nntp-default-port))
1906 (elmo-nntp-default-stream-type
1907 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1908 (elmo-nntp-default-function wl-nntp-posting-function)
1910 (if (setq condition (cdr (elmo-string-matched-assoc
1911 (std11-field-body "Newsgroups")
1912 wl-nntp-posting-config-alist)))
1913 (if (stringp condition)
1914 (setq elmo-nntp-default-server condition)
1915 (while (car condition)
1916 (set (intern (format "elmo-nntp-default-%s"
1917 (symbol-name (caar condition))))
1919 (setq condition (cdr condition)))))
1920 (unless elmo-nntp-default-function
1921 (error "wl-draft-nntp-send: posting-function is nil"))
1922 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1923 (wl-draft-set-sent-message 'news 'unplugged
1924 (cons elmo-nntp-default-server
1925 elmo-nntp-default-port))
1926 (funcall elmo-nntp-default-function
1927 elmo-nntp-default-server (current-buffer))
1928 (wl-draft-set-sent-message 'news 'sent)
1929 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1930 (std11-field-body "Newsgroups")
1931 (std11-field-body "Message-ID")))))
1933 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1934 "Generate clone of current buffer named NAME."
1935 (let ((editing-buffer (current-buffer)))
1937 (set-buffer (generate-new-buffer name))
1940 (wl-draft-editor-mode)
1941 (insert-buffer editing-buffer)
1943 (while local-variables
1944 (make-local-variable (car local-variables))
1945 (set (car local-variables)
1947 (set-buffer editing-buffer)
1948 (symbol-value (car local-variables))))
1949 (setq local-variables (cdr local-variables)))
1952 (defun wl-draft-remove-text-plain-tag ()
1953 "Remove text/plain tag of mime-edit."
1954 (when (string= (mime-make-text-tag "plain")
1955 (buffer-substring-no-properties (point-at-bol)(point-at-eol)))
1956 (delete-region (point-at-bol)(1+ (point-at-eol)))))
1958 (defun wl-draft-reedit (number)
1959 (let ((draft-folder (wl-draft-get-folder))
1962 buffer change-major-mode-hook body-top)
1963 (setq buffer (get-buffer-create (format "%s/%d" wl-draft-folder
1965 (if wl-draft-use-frame
1966 (switch-to-buffer-other-frame buffer)
1967 (switch-to-buffer buffer))
1969 (elmo-message-fetch draft-folder number (elmo-make-fetch-strategy 'entire)
1970 nil (current-buffer))
1971 (elmo-delete-cr-buffer)
1972 (let ((mime-edit-again-ignored-field-regexp
1973 "^\\(Content-.*\\|Mime-Version\\):"))
1974 (wl-draft-decode-message-in-buffer))
1975 (setq body-top (wl-draft-insert-mail-header-separator))
1978 (make-local-variable 'truncate-partial-width-windows)
1979 (setq truncate-partial-width-windows nil)
1980 (setq truncate-lines wl-draft-truncate-lines)
1981 (setq wl-sent-message-via nil)
1982 (setq wl-sent-message-queued nil)
1983 (wl-draft-config-info-operation number 'load)
1984 (goto-char (point-min))
1985 (wl-draft-overload-functions)
1986 (wl-draft-editor-mode)
1987 (static-when (boundp 'auto-save-file-name-transforms)
1988 (make-local-variable 'auto-save-file-name-transforms)
1989 (setq auto-save-file-name-transforms
1990 (cons (list (concat (regexp-quote wl-draft-folder)
1992 (concat (expand-file-name
1994 (elmo-folder-msgdb-path
1995 (wl-draft-get-folder)))
1997 auto-save-file-name-transforms)))
1998 (setq buffer-file-name (buffer-name)
1999 wl-draft-parent-folder ""
2000 wl-draft-buffer-message-number number)
2001 (when wl-draft-write-file-function
2002 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
2003 (wl-highlight-headers 'for-draft)
2004 (goto-char body-top)
2005 (run-hooks 'wl-draft-reedit-hook)
2006 (goto-char (point-max))
2009 (defmacro wl-draft-body-goto-top ()
2011 (goto-char (point-min))
2012 (if (re-search-forward mail-header-separator nil t)
2014 (goto-char (point-max))))))
2016 (defmacro wl-draft-body-goto-bottom ()
2017 (` (goto-char (point-max))))
2019 (defmacro wl-draft-config-body-goto-header ()
2021 (goto-char (point-min))
2022 (if (re-search-forward mail-header-separator nil t)
2024 (goto-char (point-max))))))
2026 (defsubst wl-draft-config-sub-eval-insert (content &optional newline)
2027 (let (content-value)
2029 (stringp (setq content-value (eval content))))
2030 (insert content-value)
2031 (if newline (insert "\n")))))
2033 (defun wl-draft-config-sub-body (content)
2034 (wl-draft-body-goto-top)
2035 (delete-region (point) (point-max))
2036 (wl-draft-config-sub-eval-insert content))
2038 (defun wl-draft-config-sub-top (content)
2039 (wl-draft-body-goto-top)
2040 (wl-draft-config-sub-eval-insert content))
2042 (defun wl-draft-config-sub-bottom (content)
2043 (wl-draft-body-goto-bottom)
2044 (wl-draft-config-sub-eval-insert content))
2046 (defun wl-draft-config-sub-header (content)
2047 (wl-draft-config-body-goto-header)
2048 (wl-draft-config-sub-eval-insert content 'newline))
2050 (defun wl-draft-config-sub-header-top (content)
2051 (goto-char (point-min))
2052 (wl-draft-config-sub-eval-insert content 'newline))
2054 (defun wl-draft-config-sub-part-top (content)
2055 (goto-char (mime-edit-content-beginning))
2056 (wl-draft-config-sub-eval-insert content 'newline))
2058 (defun wl-draft-config-sub-part-bottom (content)
2059 (goto-char (mime-edit-content-end))
2060 (wl-draft-config-sub-eval-insert content 'newline))
2062 (defsubst wl-draft-config-sub-file (content)
2063 (let ((coding-system-for-read wl-cs-autoconv)
2064 (file (expand-file-name (eval content))))
2065 (if (file-exists-p file)
2066 (insert-file-contents file)
2067 (error "%s: no exists file" file))))
2069 (defun wl-draft-config-sub-body-file (content)
2070 (wl-draft-body-goto-top)
2071 (delete-region (point) (point-max))
2072 (wl-draft-config-sub-file content))
2074 (defun wl-draft-config-sub-top-file (content)
2075 (wl-draft-body-goto-top)
2076 (wl-draft-config-sub-file content))
2078 (defun wl-draft-config-sub-bottom-file (content)
2079 (wl-draft-body-goto-bottom)
2080 (wl-draft-config-sub-file content))
2082 (defun wl-draft-config-sub-header-file (content)
2083 (wl-draft-config-body-goto-header)
2084 (wl-draft-config-sub-file content))
2086 (defun wl-draft-config-sub-template (content)
2087 (setq wl-draft-config-variables
2088 (wl-template-insert (eval content))))
2090 (defun wl-draft-config-sub-x-face (content)
2091 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
2092 (fboundp 'x-face-insert)) ; x-face.el is installed.
2093 (x-face-insert content)
2094 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
2096 (defsubst wl-draft-config-sub-func (field content)
2098 (if (setq func (assq field wl-draft-config-sub-func-alist))
2099 (let (wl-draft-config-variables)
2100 (funcall (cdr func) content)
2101 ;; for wl-draft-config-sub-template
2102 (cons t wl-draft-config-variables)))))
2104 (defsubst wl-draft-config-exec-sub (clist)
2105 (let (config local-variables)
2107 (setq config (car clist))
2112 (let ((field (car config))
2113 (content (cdr config))
2117 (wl-draft-replace-field field (eval content) t))
2118 ((setq ret-val (wl-draft-config-sub-func field content))
2119 (if (cdr ret-val) ;; for wl-draft-config-sub-template
2120 (wl-append local-variables (cdr ret-val))))
2121 ((boundp field) ;; variable
2122 (make-local-variable field)
2123 (set field (eval content))
2124 (wl-append local-variables (list field)))
2126 (error "%s: not variable" field)))))
2128 (error "%s: not supported type" config)))
2129 (setq clist (cdr clist)))
2132 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
2133 "Change headers in draft preparation time."
2135 (unless wl-draft-reedit
2138 (and (boundp 'wl-draft-prepared-config-alist)
2139 wl-draft-prepared-config-alist) ;; For compatible.
2140 wl-draft-config-alist)))
2142 (wl-draft-config-exec config-alist reply-buf)))))
2144 (defun wl-draft-config-exec (&optional config-alist reply-buf)
2145 "Change headers according to the value of `wl-draft-config-alist'.
2146 Automatically applied in draft sending time."
2148 (let ((case-fold-search t)
2149 (alist (or config-alist wl-draft-config-alist))
2150 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2151 wl-draft-reply-buffer)))
2152 (local-variables wl-draft-config-variables)
2154 (when (and (or (interactive-p)
2155 wl-draft-config-exec-flag)
2160 (setq key (caar alist)
2167 (set-buffer reply-buf)
2169 (std11-narrow-to-header)
2170 (goto-char (point-min))
2171 (re-search-forward (car clist) nil t))))
2172 (wl-draft-config-exec-sub (cdr clist))
2175 (when (save-restriction
2176 (std11-narrow-to-header mail-header-separator)
2177 (goto-char (point-min))
2178 (re-search-forward key nil t))
2179 (wl-append local-variables
2180 (wl-draft-config-exec-sub clist))
2183 (wl-append local-variables
2184 (wl-draft-config-exec-sub clist))
2186 (if (and found wl-draft-config-matchone)
2188 (setq alist (cdr alist))))
2190 (setq wl-draft-config-exec-flag nil))
2191 (run-hooks 'wl-draft-config-exec-hook)
2192 (put-text-property (point-min)(point-max) 'face nil)
2193 (wl-highlight-message (point-min)(point-max) t)
2194 (setq wl-draft-config-variables
2195 (elmo-uniq-list local-variables))))))
2197 (defun wl-draft-replace-field (field content &optional add)
2200 (let ((case-fold-search t)
2201 (inhibit-read-only t) ;; added by teranisi.
2203 (std11-narrow-to-header mail-header-separator)
2204 (goto-char (point-min))
2205 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2210 (re-search-forward "^[^ \t]" nil 'move)
2212 (skip-chars-backward "\n")
2213 (delete-region beg (point))
2214 (insert " " content))
2219 (re-search-forward "^[^ \t]" nil 'move)
2221 (delete-region beg (point)))
2222 (when (and add content)
2224 (goto-char (point-max))
2225 (insert (concat field ": " content "\n"))))))))
2227 (defun wl-draft-config-info-operation (msg operation)
2228 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-draft-get-folder)))
2231 (format "%s-%d" wl-draft-config-save-filename msg)
2233 element alist variable)
2235 ((eq operation 'save)
2236 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2237 (while (setq variable (pop variables))
2238 (when (boundp variable)
2240 (list (cons variable (eval variable))))))
2241 (elmo-object-save filename alist)))
2242 ((eq operation 'load)
2243 (setq alist (elmo-object-load filename))
2244 (while (setq element (pop alist))
2245 (set (make-local-variable (car element)) (cdr element))
2246 (wl-append wl-draft-config-variables (list (car element)))))
2247 ((eq operation 'delete)
2248 (if (file-exists-p filename)
2249 (delete-file filename))))))
2251 (defun wl-draft-queue-info-operation (msg operation
2252 &optional add-sent-message-via)
2253 (let* ((msgdb-dir (elmo-folder-msgdb-path
2254 (wl-folder-get-elmo-folder wl-queue-folder)))
2257 (format "%s-%d" wl-draft-queue-save-filename msg)
2259 element alist variable)
2261 ((eq operation 'save)
2262 (let ((variables (elmo-uniq-list
2263 (append wl-draft-queue-save-variables
2264 wl-draft-config-variables
2265 (list 'wl-draft-fcc-list)))))
2266 (if add-sent-message-via
2268 (push 'wl-sent-message-queued variables)
2269 (push 'wl-sent-message-via variables)))
2270 (while (setq variable (pop variables))
2271 (when (boundp variable)
2273 (list (cons variable (eval variable))))))
2274 (elmo-object-save filename alist)))
2275 ((eq operation 'load)
2276 (setq alist (elmo-object-load filename))
2277 (while (setq element (pop alist))
2278 (set (make-local-variable (car element)) (cdr element))))
2279 ((eq operation 'get-sent-via)
2280 (setq alist (elmo-object-load filename))
2281 (cdr (assq 'wl-sent-message-via alist)))
2282 ((eq operation 'delete)
2283 (if (file-exists-p filename)
2284 (delete-file filename))))))
2286 (defun wl-draft-queue-append (wl-sent-message-via)
2287 (if wl-draft-verbose-send
2288 (message "Queuing..."))
2289 (let ((send-buffer (current-buffer))
2290 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2291 (message-id (std11-field-body "Message-ID")))
2292 (if (elmo-folder-append-buffer folder)
2294 (wl-draft-queue-info-operation
2295 (car (elmo-folder-status folder))
2296 'save wl-sent-message-via)
2297 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2298 (when wl-draft-verbose-send
2299 (setq wl-draft-verbose-msg "Queuing...")
2300 (message "Queuing...done")))
2301 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2302 (error "Queuing failed"))))
2304 (defun wl-draft-queue-flush ()
2305 "Flush draft queue."
2307 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2309 (elmo-folder-open-internal queue-folder)
2310 (elmo-folder-list-messages queue-folder)))
2313 (wl-draft-queue-flushing t)
2314 msgs failure len buffer msgid sent-via)
2315 ;; get plugged send message
2317 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2320 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2321 (or (not (nth 2 (car sent-via)))
2323 (car (nth 2 (car sent-via)))
2324 (cdr (nth 2 (car sent-via))))))
2325 (wl-append msgs (list (car msgs2)))
2327 (setq sent-via (cdr sent-via))))
2328 (setq msgs2 (cdr msgs2)))
2329 (when (> (setq len (length msgs)) 0)
2330 (if (elmo-y-or-n-p (format
2331 "%d message(s) are in the sending queue. Send now? "
2333 (not elmo-dop-flush-confirm) t)
2336 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2339 ;; reset buffer local variables
2340 (kill-all-local-variables)
2344 (setq wl-sent-message-via nil)
2345 (wl-draft-queue-info-operation (car msgs) 'load)
2346 (elmo-message-fetch queue-folder
2348 (elmo-make-fetch-strategy 'entire)
2349 nil (current-buffer))
2351 (setq failure (funcall
2352 wl-draft-queue-flush-send-function
2353 (format "Sending (%d/%d)..." i len)))
2354 ;;; (wl-draft-raw-send nil nil
2355 ;;; (format "Sending (%d/%d)..." i len))
2357 (elmo-display-error err t)
2361 (if (eq wl-sent-message-modified 'requeue)
2363 (elmo-folder-delete-messages
2364 queue-folder (cons (car msgs) nil))
2365 (wl-draft-queue-info-operation (car msgs) 'delete))
2367 (elmo-folder-delete-messages
2368 queue-folder (cons (car msgs) nil))
2369 (wl-draft-queue-info-operation (car msgs) 'delete)
2370 (setq performed (+ 1 performed))))
2371 (setq msgs (cdr msgs)))
2372 (kill-buffer buffer)
2373 (message "%d message(s) are sent." performed)))
2374 (message "%d message(s) are remained to be sent." len))
2375 (elmo-folder-close queue-folder)
2378 (defun wl-jump-to-draft-buffer (&optional arg)
2379 "Jump to the draft if exists."
2382 (wl-jump-to-draft-folder)
2383 (let ((draft-bufs (wl-collect-draft))
2387 (message "No draft buffer exist."))
2390 (sort (mapcar 'buffer-name draft-bufs)
2391 (function (lambda (a b)
2392 (not (string< a b))))))
2393 (if (setq buf (cdr (member (buffer-name)
2395 (setq buf (car buf))
2396 (setq buf (car draft-bufs)))
2397 (switch-to-buffer buf))))))
2399 (defun wl-jump-to-draft-folder ()
2400 (let ((msgs (reverse (elmo-folder-list-messages (wl-draft-get-folder))))
2401 (mybuf (buffer-name))
2404 (message "No draft message exist.")
2405 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2406 (setq msg (cadr (memq
2407 (string-to-int (substring mybuf (match-end 0)))
2409 (or msg (setq msg (car msgs)))
2410 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2411 (switch-to-buffer buf)
2412 (wl-draft-reedit msg)))))
2414 (defun wl-draft-highlight-and-recenter (&optional n)
2416 (when wl-highlight-body-too
2417 (let ((modified (buffer-modified-p)))
2420 (put-text-property (point-min) (point-max) 'face nil)
2421 (wl-highlight-message (point-min) (point-max) t))
2422 (set-buffer-modified-p modified))))
2423 (static-when (featurep 'xemacs)
2424 ;; Cope with one of many XEmacs bugs that `recenter' takes
2425 ;; a long time if there are a lot of invisible text lines.
2429 ;; insert element from history
2430 (defvar wl-draft-current-history-position nil)
2431 (defvar wl-draft-history-backup-word "")
2433 (defun wl-draft-previous-history-element (n)
2435 (let (bol history beg end prev new)
2436 (when (and (not (wl-draft-on-field-p))
2439 (goto-char (point-min))
2440 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
2444 (while (and (looking-at "^[ \t]")
2445 (not (= (point) (point-min))))
2448 ((looking-at wl-folder-complete-header-regexp)
2449 (and (boundp 'wl-read-folder-history)
2450 (setq history wl-read-folder-history)))
2451 ;; ((looking-at wl-address-complete-header-regexp)
2452 ;; (setq history .....))
2456 (setq bol (save-excursion (beginning-of-line) (point)))
2457 (cond ((and (or (eq last-command 'wl-draft-previous-history-element)
2458 (eq last-command 'wl-draft-next-history-element))
2459 wl-draft-current-history-position)
2461 (or (search-backward-regexp ",[ \t]*\\(.*\\)" bol t)
2462 (search-backward-regexp "^[ \t]\\(.*\\)" bol t)
2463 (search-backward-regexp "^[^ \t]*: \\(.*\\)" bol t))
2464 (setq prev (match-string 1))
2465 (goto-char (match-beginning 1))
2468 (>= (+ n wl-draft-current-history-position) 0))
2470 (<= (+ n wl-draft-current-history-position)
2473 (setq wl-draft-current-history-position
2474 (+ n wl-draft-current-history-position))
2476 (nth wl-draft-current-history-position
2477 (append (list wl-draft-history-backup-word)
2479 (delete-region beg end)
2483 (message "End of history; no next item"))
2485 (message "Beginning of history; no preceding item")))))
2488 (or (search-backward-regexp ",[ \t]*\\(.*\\)" bol t)
2489 (search-backward-regexp "^[ \t]\\(.*\\)" bol t)
2490 (search-backward-regexp "^[^ \t]*: \\(.*\\)" bol t)))
2492 (setq wl-draft-current-history-position 1)
2493 (setq wl-draft-history-backup-word (match-string 1))
2494 (delete-region (match-beginning 1) (match-end 1))
2495 (insert (car history)))
2497 (setq wl-draft-current-history-position nil))))))
2499 (defun wl-draft-next-history-element (n)
2501 (wl-draft-previous-history-element (- n)))
2503 ;;;; user-agent support by Sen Nagata
2505 ;; this appears to be necessarily global...
2506 (defvar wl-user-agent-compose-p nil)
2507 (defvar wl-user-agent-headers-and-body-alist nil)
2509 ;; this should be a generic function for mail-mode -- i wish there was
2510 ;; something like it in sendmail.el
2511 (defun wl-user-agent-insert-header (header-name header-value)
2512 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2513 ;; it seems like overriding existing headers is acceptable -- should
2514 ;; we provide an option?
2516 ;; plan was: unfold header (might be folded), remove existing value, insert
2518 ;; wl doesn't seem to fold header lines yet anyway :-)
2520 (let ((kill-whole-line t)
2522 (mail-position-on-field (capitalize header-name))
2523 (setq end-of-line (point))
2525 (re-search-forward ":" end-of-line)
2526 (insert (concat " " header-value "\n"))
2529 ;; this should be a generic function for mail-mode -- i wish there was
2530 ;; something like it in sendmail.el
2532 ;; ** haven't dealt w/ case where the body is already set **
2533 (defun wl-user-agent-insert-body (body-text)
2534 "Insert a body of text, BODY-TEXT, into a message."
2535 ;; code defensively... :-P
2536 (goto-char (point-min))
2537 (search-forward mail-header-separator)
2542 (defun wl-user-agent-compose (&optional to subject other-headers continue
2543 switch-function yank-action
2545 "Support the `compose-mail' interface for wl.
2546 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2547 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2548 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2550 (unless (featurep 'wl)
2553 (setq switch-function 'keep))
2554 ;; protect these -- to and subject get bound at some point, so it looks
2555 ;; to be necessary to protect the values used w/in
2556 (let ((wl-user-agent-headers-and-body-alist other-headers)
2557 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2558 (wl-draft-buffer-style switch-function))
2560 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2563 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2566 (setq wl-user-agent-headers-and-body-alist
2567 (cons (cons "to" to)
2568 wl-user-agent-headers-and-body-alist))))
2570 (if (wl-string-match-assoc "subject"
2571 wl-user-agent-headers-and-body-alist
2574 (wl-string-match-assoc "subject"
2575 wl-user-agent-headers-and-body-alist
2578 (setq wl-user-agent-headers-and-body-alist
2579 (cons (cons "subject" subject)
2580 wl-user-agent-headers-and-body-alist))))
2581 ;; i think this is what we want to use...
2584 ;; tell the hook-function to do its stuff
2585 (setq wl-user-agent-compose-p t)
2586 ;; because to get the hooks working, wl-draft has to think it has
2587 ;; been called interactively
2588 (call-interactively 'wl-draft))
2589 (setq wl-user-agent-compose-p nil))))
2591 (defun wl-user-agent-compose-internal ()
2592 "Manipulate headers and/or a body of a draft message."
2593 ;; being called from wl-user-agent-compose?
2594 (if wl-user-agent-compose-p
2597 (let ((headers wl-user-agent-headers-and-body-alist)
2598 (case-fold-search t))
2601 (if (not (string-match "^body$" (car (car headers))))
2602 (wl-user-agent-insert-header
2603 (car (car headers)) (cdr (car headers)))
2605 (setq headers (cdr headers))))
2606 ;; highlight headers (from wl-draft in wl-draft.el)
2607 (wl-highlight-headers 'for-draft)
2609 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2611 (wl-user-agent-insert-body
2612 (cdr (wl-string-match-assoc
2614 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2618 (product-provide (provide 'wl-draft) (require 'wl-version))
2620 ;;; wl-draft.el ends here