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 (autoload 'open-ssl-stream "ssl")
58 (defvar wl-draft-buffer-message-number nil)
59 (defvar wl-draft-field-completion-list nil)
60 (defvar wl-draft-verbose-send t)
61 (defvar wl-draft-verbose-msg nil)
62 (defvar wl-draft-queue-flushing nil)
63 (defvar wl-draft-config-variables nil)
64 (defvar wl-draft-config-exec-flag t)
65 (defvar wl-draft-buffer-cur-summary-buffer nil)
66 (defvar wl-draft-clone-local-variable-regexp "^\\(wl\\|mime\\)")
67 (defvar wl-draft-sendlog-filename "sendlog")
68 (defvar wl-draft-queue-save-filename "qinfo")
69 (defvar wl-draft-config-save-filename "config")
70 (defvar wl-draft-queue-flush-send-function 'wl-draft-dispatch-message)
71 (defvar wl-sent-message-via nil)
72 (defvar wl-sent-message-modified nil)
73 (defvar wl-sent-message-queued nil)
74 (defvar wl-draft-fcc-list nil)
75 (defvar wl-draft-reedit nil)
76 (defvar wl-draft-reply-buffer nil)
77 (defvar wl-draft-forward nil)
78 (defvar wl-draft-doing-mime-bcc nil)
80 (defvar wl-draft-parent-folder nil
81 "Folder name of the summary in which current draft is invoked.
82 This variable is local in each draft buffer.
83 You can refer its value in `wl-draft-config-alist'.
86 \(setq wl-draft-config-alist
87 '(((string-match \".*@domain1$\" wl-draft-parent-folder)
88 (\"From\" . \"user@domain1\"))
89 ((string-match \".*@domain2$\" wl-draft-parent-folder)
90 (\"From\" . \"user@domain2\"))))")
92 (defvar wl-draft-parent-number nil)
94 (defconst wl-draft-reply-saved-variables
95 '(wl-draft-parent-folder
96 wl-draft-parent-number))
98 (defvar wl-draft-config-sub-func-alist
99 '((body . wl-draft-config-sub-body)
100 (top . wl-draft-config-sub-top)
101 (bottom . wl-draft-config-sub-bottom)
102 (header . wl-draft-config-sub-header)
103 (header-top . wl-draft-config-sub-header-top)
104 (header-bottom . wl-draft-config-sub-header)
105 (part-top . wl-draft-config-sub-part-top)
106 (part-bottom . wl-draft-config-sub-part-bottom)
107 (body-file . wl-draft-config-sub-body-file)
108 (top-file . wl-draft-config-sub-top-file)
109 (bottom-file . wl-draft-config-sub-bottom-file)
110 (header-file . wl-draft-config-sub-header-file)
111 (template . wl-draft-config-sub-template)
112 (x-face . wl-draft-config-sub-x-face)))
114 (make-variable-buffer-local 'wl-draft-buffer-message-number)
115 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
116 (make-variable-buffer-local 'wl-draft-config-variables)
117 (make-variable-buffer-local 'wl-draft-config-exec-flag)
118 (make-variable-buffer-local 'wl-sent-message-via)
119 (make-variable-buffer-local 'wl-sent-message-queued)
120 (make-variable-buffer-local 'wl-draft-fcc-list)
121 (make-variable-buffer-local 'wl-draft-reply-buffer)
122 (make-variable-buffer-local 'wl-draft-parent-folder)
123 (make-variable-buffer-local 'wl-draft-parent-number)
125 (defvar wl-draft-folder-internal nil
126 "Internal variable for caching `opened' draft folder.")
128 (defsubst wl-smtp-password-key (user mechanism server)
129 (format "SMTP:%s/%s@%s"
130 user mechanism server))
132 (defmacro wl-smtp-extension-bind (&rest body)
133 (` (let* ((smtp-sasl-mechanisms
134 (if wl-smtp-authenticate-type
136 (if (listp wl-smtp-authenticate-type)
137 wl-smtp-authenticate-type
138 (list wl-smtp-authenticate-type)))))
139 (smtp-use-sasl (and smtp-sasl-mechanisms t))
140 (smtp-use-starttls (eq wl-smtp-connection-type 'starttls))
141 (smtp-open-connection-function
142 (if (eq wl-smtp-connection-type 'ssl)
144 smtp-open-connection-function))
146 (if (eq wl-smtp-connection-type 'ssl)
149 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
150 (if (and (string= (car smtp-sasl-mechanisms) "DIGEST-MD5")
152 (string-match "^\\([^@]*\\)@\\([^@]*\\)"
153 wl-smtp-posting-user))
154 (setq smtp-sasl-user-name (match-string 1 wl-smtp-posting-user)
155 smtp-sasl-properties (list 'realm
156 (match-string 2 wl-smtp-posting-user)))
157 (setq smtp-sasl-user-name wl-smtp-posting-user
158 smtp-sasl-properties nil))
159 (setq sasl-read-passphrase
163 (wl-smtp-password-key
165 (car smtp-sasl-mechanisms)
169 (defun wl-draft-insert-date-field ()
171 (insert "Date: " (wl-make-date-string) "\n"))
173 (defun wl-draft-insert-from-field ()
175 ;; Put the "From:" field in unless for some odd reason
176 ;; they put one in themselves.
179 (setq from (wl-draft-eword-encode-address-list wl-from))
180 (error (error "Please look at `wl-from' again")))
181 (insert "From: " from "\n")))
183 (defun wl-draft-insert-x-face-field ()
184 "Insert X-Face header."
186 (if (not (file-exists-p wl-x-face-file))
187 (error "File %s does not exist" wl-x-face-file)
188 (goto-char (point-min))
189 (search-forward mail-header-separator nil t)
191 (wl-draft-insert-x-face-field-here)
192 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
194 (defun wl-draft-insert-x-face-field-here ()
195 "Insert X-Face field at point."
196 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
197 (when (string-match "^\\(X-Face:\\)?[ \t\n]*" x-face-string)
198 (setq x-face-string (substring x-face-string (match-end 0))))
199 (insert "X-Face: " x-face-string))
200 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
202 ;; Insert X-Face-Version: field
203 (when (and (fboundp 'x-face-insert-version-header)
204 (boundp 'x-face-add-x-face-version-header)
205 x-face-add-x-face-version-header)
206 (x-face-insert-version-header)))
208 (defun wl-draft-setup ()
209 (let ((field wl-draft-fields)
213 (list (cons (concat (car field) " ")
214 (concat (car field) " ")))))
215 (setq field (cdr field)))
217 (cons (cons (concat wl-draft-mime-bcc-field-name ": ")
218 (concat wl-draft-mime-bcc-field-name ": "))
220 (setq wl-draft-field-completion-list cl)
221 (setq wl-address-complete-header-regexp
223 (append wl-address-complete-header-list
224 (list (concat wl-draft-mime-bcc-field-name ":")))))))
226 (defun wl-draft-make-mail-followup-to (recipients)
227 (let ((rlist (wl-address-delete-user-mail-addresses recipients)))
228 (if (elmo-list-member rlist (mapcar 'downcase
229 wl-subscribed-mailing-list))
231 (append rlist (list (wl-address-header-extract-address
234 (defun wl-draft-delete-myself-from-cc (to cc)
235 (cond (wl-draft-always-delete-myself ; always-delete option
236 (wl-address-delete-user-mail-addresses cc))
237 ((elmo-list-member (append to cc) ; subscribed mailing-list
238 (mapcar 'downcase wl-subscribed-mailing-list))
239 (wl-address-delete-user-mail-addresses cc))
242 (defsubst wl-draft-strip-subject-regexp (subject regexp)
243 "Remove REGEXP from SUBJECT string."
244 (if (string-match regexp subject)
245 (substring subject (match-end 0))
248 (defun wl-draft-forward-make-subject (original-subject)
249 "Generate subject string for forwarding."
250 (cond ((functionp wl-forward-subject-prefix)
251 (concat (funcall wl-forward-subject-prefix)
253 ((stringp wl-forward-subject-prefix)
254 (concat wl-forward-subject-prefix
255 (wl-draft-strip-subject-regexp
256 (or original-subject "")
257 wl-subject-forward-prefix-regexp)))
258 (t original-subject)))
260 (defun wl-draft-reply-make-subject (original-subject)
261 "Generate subject string for replying."
262 (cond ((functionp wl-reply-subject-prefix)
263 (concat (funcall wl-reply-subject-prefix)
265 ((stringp wl-reply-subject-prefix)
266 (concat wl-reply-subject-prefix
267 (wl-draft-strip-subject-regexp
268 (or original-subject "")
269 wl-subject-re-prefix-regexp)))
270 (t original-subject)))
272 (defun wl-draft-forward (original-subject summary-buf)
273 (let (references parent-folder subject)
274 (with-current-buffer summary-buf
275 (setq parent-folder (wl-summary-buffer-folder-name)))
276 (setq subject (wl-draft-forward-make-subject original-subject))
277 (with-current-buffer (wl-message-get-original-buffer)
278 (setq references (nconc
279 (std11-field-bodies '("References" "In-Reply-To"))
280 (list (std11-field-body "Message-Id"))))
281 (setq references (delq nil references)
282 references (mapconcat 'identity references " ")
283 references (wl-draft-parse-msg-id-list-string references)
284 references (wl-delete-duplicates references)
285 references (when references
286 (mapconcat 'identity references "\n\t"))))
287 (and wl-draft-use-frame
288 (get-buffer-window summary-buf)
289 (select-window (get-buffer-window summary-buf)))
290 (wl-draft (list (cons 'To "")
291 (cons 'Subject subject)
292 (cons 'References references))
293 nil nil nil nil parent-folder))
294 (goto-char (point-max))
295 (wl-draft-insert-message)
296 (mail-position-on-field "To"))
298 (defun wl-draft-self-reply-p ()
299 "Return t when From address in the current message is user's self one or not."
300 (wl-address-user-mail-address-p (or (elmo-field-body "From") "")))
302 (defun wl-draft-reply (buf with-arg summary-buf &optional number)
303 "Reply to BUF buffer message.
304 Reply to author if WITH-ARG is non-nil."
307 to mail-followup-to cc subject in-reply-to references newsgroups
308 to-alist cc-alist decoder parent-folder)
309 (when (buffer-live-p summary-buf)
310 (with-current-buffer summary-buf
311 (setq parent-folder (wl-summary-buffer-folder-name))))
312 (set-buffer (or buf mime-mother-buffer))
313 (setq r-list (if with-arg wl-draft-reply-with-argument-list
314 wl-draft-reply-without-argument-list))
317 (when (let ((condition (car (car r-list))))
318 (cond ((stringp condition)
319 (std11-field-body condition))
324 ((stringp (car condition))
325 (or (std11-field-body (car condition))
327 ((symbolp (car condition))
328 (or (funcall (car condition))
332 (setq condition (cdr condition)))
335 (funcall condition))))
336 (let ((r-to-list (nth 0 (cdr (car r-list))))
337 (r-cc-list (nth 1 (cdr (car r-list))))
338 (r-ng-list (nth 2 (cdr (car r-list)))))
339 (when (and (member "Followup-To" r-ng-list)
340 (string= (std11-field-body "Followup-To") "poster"))
341 (setq r-to-list (cons "From" r-to-list))
342 (setq r-ng-list (delete "Followup-To"
343 (copy-sequence r-ng-list))))
344 (if (and r-to-list (symbolp r-to-list))
345 (setq to (wl-concat-list (funcall r-to-list) ","))
346 (setq to (wl-concat-list (cons to
347 (elmo-multiple-fields-body-list
350 (if (and r-cc-list (symbolp r-cc-list))
351 (setq cc (wl-concat-list (funcall r-cc-list) ","))
352 (setq cc (wl-concat-list (cons cc
353 (elmo-multiple-fields-body-list
356 (if (and r-ng-list (symbolp r-ng-list))
357 (setq newsgroups (wl-concat-list (funcall r-ng-list) ","))
358 (setq newsgroups (wl-concat-list (cons newsgroups
363 (setq r-list (cdr r-list)))
364 (error "No match field: check your `wl-draft-reply-%s-argument-list'"
365 (if with-arg "with" "without")))
366 (setq subject (wl-draft-reply-make-subject (std11-field-body "Subject")))
367 (setq to (wl-parse-addresses to)
368 cc (wl-parse-addresses cc))
369 (with-temp-buffer ; to keep raw buffer unibyte.
370 (set-buffer-multibyte default-enable-multibyte-characters)
371 (setq decoder (mime-find-field-decoder 'Subject 'plain))
372 (setq subject (if (and subject decoder)
373 (funcall decoder subject) subject))
377 (setq decoder (mime-find-field-decoder 'To 'plain))
378 (cons (nth 1 (std11-extract-address-components addr))
379 (if decoder (funcall decoder addr) addr)))
384 (setq decoder (mime-find-field-decoder 'Cc 'plain))
385 (cons (nth 1 (std11-extract-address-components addr))
386 (if decoder (funcall decoder addr) addr)))
388 (setq in-reply-to (std11-field-body "Message-Id"))
389 (setq references (nconc
390 (std11-field-bodies '("References" "In-Reply-To"))
392 (setq to (delq nil (mapcar 'car to-alist)))
393 (setq cc (delq nil (mapcar 'car cc-alist)))
394 ;; if subscribed mailing list is contained in cc or to
395 ;; and myself is contained in cc,
396 ;; delete myself from cc.
397 (setq cc (wl-draft-delete-myself-from-cc to cc))
398 (when wl-insert-mail-followup-to
399 (setq mail-followup-to
400 (wl-draft-make-mail-followup-to (append to cc)))
401 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
402 (with-temp-buffer ; to keep raw buffer unibyte.
403 (set-buffer-multibyte default-enable-multibyte-characters)
404 (setq newsgroups (wl-parse newsgroups
405 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
406 newsgroups (wl-delete-duplicates newsgroups)
411 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
412 (if decoder (funcall decoder grp) grp))
414 (setq to (wl-delete-duplicates to nil t))
415 (setq cc (wl-delete-duplicates
416 (append (wl-delete-duplicates cc nil t)
417 to (copy-sequence to))
419 (and to (setq to (mapconcat
421 (if wl-draft-reply-use-address-with-full-name
422 (or (cdr (assoc addr to-alist)) addr)
425 (and cc (setq cc (mapconcat
427 (if wl-draft-reply-use-address-with-full-name
428 (or (cdr (assoc addr cc-alist)) addr)
431 (and mail-followup-to
432 (setq mail-followup-to
435 (if wl-draft-reply-use-address-with-full-name
436 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
438 mail-followup-to ",\n\t")))
439 (and (null to) (setq to cc cc nil))
440 (setq references (delq nil references)
441 references (mapconcat 'identity references " ")
442 references (wl-draft-parse-msg-id-list-string references)
443 references (wl-delete-duplicates references)
444 references (if references
445 (mapconcat 'identity references "\n\t")))
446 (and wl-draft-use-frame
447 (get-buffer-window summary-buf)
448 (select-window (get-buffer-window summary-buf)))
449 (wl-draft (list (cons 'To to)
451 (cons 'Newsgroups newsgroups)
452 (cons 'Subject subject)
453 (cons 'In-Reply-To in-reply-to)
454 (cons 'References references)
455 (cons 'Mail-Followup-To mail-followup-to))
456 nil nil nil nil parent-folder)
457 (setq wl-draft-parent-number number)
458 (setq wl-draft-reply-buffer buf)
459 (setq wl-draft-config-variables
460 (append wl-draft-reply-saved-variables
461 wl-draft-config-variables)))
462 (run-hooks 'wl-reply-hook))
464 (defun wl-draft-reply-position (position)
465 (cond ((eq position 'body)
466 (wl-draft-body-goto-top))
467 ((eq position 'bottom)
468 (wl-draft-body-goto-bottom))
470 (goto-char (point-min)))
471 ((and (stringp position)
472 (std11-field-body position))
473 (progn (mail-position-on-field position)
474 (wl-draft-beginning-of-line)))
476 (while (car position)
477 (wl-draft-reply-position (car position))
478 (setq position (cdr position))))))
480 (defun wl-draft-add-references ()
481 (wl-draft-add-in-reply-to "References"))
483 (defun wl-draft-add-in-reply-to (&optional alt-field)
484 (let* ((mes-id (save-excursion
485 (set-buffer mail-reply-buffer)
486 (std11-field-body "message-id")))
487 (field (or alt-field "In-Reply-To"))
488 (ref (std11-field-body field))
489 (ref-list nil) (st nil))
490 (when (and mes-id ref)
491 (while (string-match "<[^>]+>" ref st)
493 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
496 (member mes-id ref-list))
500 (when (mail-position-on-field field)
502 (while (looking-at "^[ \t]")
504 (setq mes-id (concat "\t" mes-id "\n")))
508 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
509 &optional ignored-fields)
512 (narrow-to-region (point)(point))
515 (with-current-buffer mail-reply-buffer
517 (decode-mime-charset-region (point-min) (point-max)
519 (buffer-substring-no-properties
520 (point-min) (point-max)))))
522 (goto-char (point-min))
523 (wl-draft-delete-fields ignored-fields))
524 (goto-char (point-max))
525 (push-mark (point) nil t)
526 (goto-char (point-min)))
528 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
529 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
530 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
531 (run-hooks 'wl-draft-cited-hook)
532 (when (if wl-draft-add-references
533 (wl-draft-add-references)
534 (if wl-draft-add-in-reply-to
535 (wl-draft-add-in-reply-to)))
536 (wl-highlight-headers 'for-draft)) ; highlight when added References:
537 (when wl-highlight-body-too
538 (wl-highlight-body-region beg (point-max)))))
540 (defun wl-message-news-p ()
541 "If exist valid Newsgroups field, return non-nil."
542 (std11-field-body "Newsgroups"))
544 (defun wl-message-field-exists-p (field)
545 "If FIELD exist and FIELD value is not empty, return non-nil."
546 (let ((value (std11-field-body field)))
548 (not (string= value "")))))
550 (defun wl-message-mail-p ()
551 "If exist To, Cc or Bcc field, return non-nil."
552 (or (wl-message-field-exists-p "To")
553 (wl-message-field-exists-p "Resent-to")
554 (wl-message-field-exists-p "Cc")
555 (wl-message-field-exists-p "Bcc")
556 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
557 ;;; This may be needed..
558 ;;; (wl-message-field-exists-p "Fcc")
561 (defun wl-draft-edit-string (string)
562 (let ((cur-buf (current-buffer))
563 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
564 to subject in-reply-to cc references newsgroups mail-followup-to
565 content-type content-transfer-encoding from
570 (setq to (std11-field-body "To"))
573 (decode-mime-charset-string
576 (setq subject (std11-field-body "Subject"))
577 (setq subject (and subject
579 (decode-mime-charset-string
582 (setq from (std11-field-body "From")
585 (decode-mime-charset-string
588 (setq in-reply-to (std11-field-body "In-Reply-To"))
589 (setq cc (std11-field-body "Cc"))
592 (decode-mime-charset-string
595 (setq references (std11-field-body "References"))
596 (setq newsgroups (std11-field-body "Newsgroups"))
597 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
598 (setq content-type (std11-field-body "Content-Type"))
599 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
600 (goto-char (point-min))
601 (or (re-search-forward "\n\n" nil t)
602 (search-forward (concat mail-header-separator "\n") nil t))
607 (if (wl-address-user-mail-address-p from) from))
610 (cons 'Subject subject)
611 (cons 'Newsgroups newsgroups)
612 (cons 'Mail-Followup-To mail-followup-to)
613 (cons 'In-Reply-To in-reply-to)
614 (cons 'References references))
615 content-type content-transfer-encoding
616 (buffer-substring (point) (point-max))
618 (kill-buffer tmp-buf))
619 ;; Set cursor point to the top.
620 (goto-char (point-min))
621 (search-forward (concat mail-header-separator "\n") nil t)
622 (run-hooks 'wl-draft-reedit-hook)
623 (and to (mail-position-on-field "To"))))
625 (defun wl-draft-insert-current-message (dummy)
627 (let (original-buffer
629 mail-citation-hook mail-yank-hooks
630 wl-draft-add-references wl-draft-add-in-reply-to
631 wl-draft-cite-function)
632 (if (and wl-draft-buffer-cur-summary-buffer
633 (with-current-buffer wl-draft-buffer-cur-summary-buffer
634 (and wl-message-buffer
635 (with-current-buffer wl-message-buffer
636 (setq original-buffer (wl-message-get-original-buffer))
637 (not (zerop (with-current-buffer original-buffer
640 (setq mail-reply-buffer original-buffer)
641 (wl-draft-yank-from-mail-reply-buffer
643 wl-ignored-forwarded-headers))
644 (when (string= (mime-make-tag "message" "rfc822")
645 (buffer-substring-no-properties (point-at-bol 0)(point-at-eol 0)))
646 (delete-region (point-at-bol 0) (1+ (point-at-eol 0))))
647 (error "No current message"))))
649 (defun wl-draft-insert-get-message (dummy)
650 (let ((fld (completing-read
652 (if (memq 'read-folder wl-use-folder-petname)
653 (wl-folder-get-entity-with-petname)
654 wl-folder-entity-hashtb)
655 nil nil wl-default-spec
656 'wl-read-folder-history))
657 (number (call-interactively
658 (function (lambda (num)
659 (interactive "nNumber: ")
661 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
662 mail-citation-hook mail-yank-hooks
663 wl-draft-cite-function)
666 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
669 (elmo-make-fetch-strategy 'entire)
670 nil mail-reply-buffer)
671 (wl-draft-yank-from-mail-reply-buffer nil))
672 (kill-buffer mail-reply-buffer))))
675 ;; default body citation func
677 (defun wl-default-draft-cite ()
678 (let ((mail-yank-ignored-headers "[^:]+:")
679 (mail-yank-prefix "> ")
680 date from cite-title)
682 (if (< (mark t) (point))
683 (exchange-point-and-mark))
684 (narrow-to-region (point)(point-max))
685 (setq date (std11-field-body "date")
686 from (std11-field-body "from")))
688 (insert (format "At %s,\n%s wrote:\n"
689 (or date "some time ago")
690 (if wl-default-draft-cite-decorate-author
691 (funcall wl-summary-from-function
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)
712 (setq buf (current-buffer)))
713 (setq mail-reply-buffer buf)
714 (wl-draft-yank-from-mail-reply-buffer nil))
715 (wl-draft-yank-current-message-entity)))
717 (defun wl-draft-hide (editing-buffer)
718 "Hide the editing draft buffer if possible."
719 (when (and editing-buffer
720 (buffer-live-p editing-buffer)
721 (get-buffer-window editing-buffer))
722 (select-window (get-buffer-window editing-buffer))
723 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
724 fld-buf sum-win fld-win)
725 (if (and wl-draft-use-frame
726 (> (length (visible-frame-list)) 1))
732 ;; stay folder window if required
733 (when wl-stay-folder-window
734 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
735 (if (setq fld-win (get-buffer-window fld-buf))
736 (select-window fld-win)
737 (if wl-draft-resume-folder-window ;; resume folder window
738 (switch-to-buffer fld-buf)))))
739 (if (buffer-live-p sum-buf)
740 (if (setq sum-win (get-buffer-window sum-buf t))
741 ;; if Summary is on the frame, select it.
742 (select-window sum-win)
743 ;; if summary is not on the frame, switch to it.
744 (if (and wl-stay-folder-window
745 (or wl-draft-resume-folder-window fld-win))
746 (wl-folder-select-buffer sum-buf)
747 (switch-to-buffer sum-buf))))))))
749 (defun wl-draft-delete (editing-buffer)
750 "Kill the editing draft buffer and delete the file corresponds to it."
753 (set-buffer editing-buffer)
754 (when wl-draft-buffer-message-number
755 (elmo-folder-delete-messages (wl-draft-get-folder)
757 wl-draft-buffer-message-number))
758 (wl-draft-config-info-operation wl-draft-buffer-message-number
760 (set-buffer-modified-p nil) ; force kill
761 (kill-buffer editing-buffer))))
763 (defun wl-draft-kill (&optional force-kill)
764 "Kill current draft buffer and quit editing."
767 (when (and (or (eq major-mode 'wl-draft-mode)
768 (eq major-mode 'mail-mode))
770 (yes-or-no-p "Kill Current Draft? ")))
771 (let ((cur-buf (current-buffer)))
772 (when (and wl-draft-parent-number
773 (not (string= wl-draft-parent-folder "")))
774 (let* ((number wl-draft-parent-number)
775 (folder-name wl-draft-parent-folder)
776 (folder (wl-folder-get-elmo-folder folder-name))
778 (if (and (setq buffer (wl-summary-get-buffer folder-name))
779 (with-current-buffer buffer
780 (string= (wl-summary-buffer-folder-name)
782 (with-current-buffer buffer
783 (elmo-folder-unset-flag folder (list number) 'answered)
784 (when (wl-summary-jump-to-msg number)
785 (wl-summary-update-persistent-mark)))
786 (elmo-folder-open folder 'load-msgdb)
787 (elmo-folder-unset-flag folder (list number) 'answered)
788 (elmo-folder-close folder))))
789 (wl-draft-hide cur-buf)
790 (wl-draft-delete cur-buf)))
793 (defun wl-draft-fcc ()
794 "Add a new Fcc field, with file name completion."
796 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
797 (mail-position-on-field "to"))
800 ;; Imported from message.el.
801 (defun wl-draft-elide-region (b e)
802 "Elide the text in the region.
803 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
807 (insert wl-draft-elide-ellipsis))
809 ;; Imported from message.el.
810 (defun wl-draft-beginning-of-line (&optional n)
811 "Move point to beginning of header value or to beginning of line."
813 (let ((zrs 'zmacs-region-stays))
814 (when (and (interactive-p) (boundp zrs))
816 (if (wl-draft-point-in-header-p)
817 (let* ((here (point))
818 (bol (progn (beginning-of-line n) (point)))
819 (eol (line-end-position))
820 (eoh (and (looking-at "[^ \t]")
821 (re-search-forward ": *" eol t))))
822 (if (and eoh (or (> here eoh) (= here bol)))
825 (beginning-of-line n)))
827 (defun wl-draft-point-in-header-p ()
828 "Return t if point is in the header."
831 (goto-char (point-min))
832 (not (re-search-forward
833 (concat "^" (regexp-quote mail-header-separator) "\n")
836 ;; function for wl-sent-message-via
838 (defmacro wl-draft-sent-message-p (type)
839 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
841 (defmacro wl-draft-set-sent-message (type result &optional server-port)
842 (` (let ((element (assq (, type) wl-sent-message-via)))
844 (unless (eq (nth 1 element) (, result))
845 (setcdr element (list (, result) (, server-port)))
846 (setq wl-sent-message-modified t))
847 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
848 (setq wl-sent-message-modified t)))))
850 (defun wl-draft-sent-message-results ()
851 (let ((results wl-sent-message-via)
852 unplugged-via sent-via)
854 (cond ((eq (nth 1 (car results)) 'unplugged)
855 (push (caar results) unplugged-via))
856 ((eq (nth 1 (car results)) 'sent)
857 (push (caar results) sent-via)))
858 (setq results (cdr results)))
859 (list unplugged-via sent-via)))
861 (defun wl-draft-write-sendlog (status proto server to id)
862 "Write send log file, if `wl-draft-sendlog' is non-nil."
863 (when wl-draft-sendlog
865 (let* ((filename (expand-file-name wl-draft-sendlog-filename
866 elmo-msgdb-directory))
867 (filesize (nth 7 (file-attributes filename)))
868 (server (if server (concat " server=" server) ""))
870 ((memq proto '(fcc queue))
871 (format " folder=\"%s\"" to))
873 (format " ng=<%s>" to))
878 (mapcar '(lambda(x) (format "<%s>" x)) to)
881 (id (if id (concat " id=" id) ""))
882 (time (format-time-string "%Y/%m/%d %T")))
883 (insert (format "%s proto=%s stat=%s%s%s%s\n"
884 time proto status server to id))
885 (if (and wl-draft-sendlog-max-size filesize
886 (> filesize wl-draft-sendlog-max-size))
887 (rename-file filename (concat filename ".old") t))
888 (if (file-writable-p filename)
889 (write-region-as-binary (point-min) (point-max)
891 (message "%s is not writable." filename))))))
893 (defun wl-draft-get-header-delimiter (&optional delete)
894 ;; If DELETE is non-nil, replace the header delimiter with a blank line
896 (goto-char (point-min))
897 (when (re-search-forward
898 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
902 (setq delimline (point-marker)))
905 (defun wl-draft-send-mail-with-qmail ()
906 "Pass the prepared message buffer to qmail-inject.
907 Refer to the documentation for the variable `send-mail-function'
908 to find out how to use this."
909 (if (and wl-draft-qmail-send-plugged
910 (not (elmo-plugged-p)))
911 (wl-draft-set-sent-message 'mail 'unplugged)
913 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
914 (let ((id (std11-field-body "Message-ID"))
915 (to (std11-field-body "To")))
919 'call-process-region 1 (point-max) wl-qmail-inject-program
921 wl-qmail-inject-args))
922 ;; qmail-inject doesn't say anything on it's stdout/stderr,
923 ;; we have to look at the retval instead
925 (wl-draft-set-sent-message 'mail 'sent)
926 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
927 (1 (error "`qmail-inject' reported permanent failure"))
928 (111 (error "`qmail-inject' reported transient failure"))
929 ;; should never happen
930 (t (error "`qmail-inject' reported unknown failure"))))))
932 (defun wl-draft-parse-msg-id-list-string (string)
933 "Get msg-id list from STRING."
935 (dolist (parsed-id (std11-parse-msg-ids-string string))
936 (when (eq (car parsed-id) 'msg-id)
937 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
939 (nreverse msg-id-list)))
941 (defun wl-draft-eword-encode-address-list (string &optional column)
942 "Encode header field STRING as list of address, and return the result.
943 Cause an error when STRING contains invalid address.
944 Optional argument COLUMN is start-position of the field."
945 (car (eword-encode-rword-list
946 (or column eword-encode-default-start-column)
947 (eword-encode-addresses-to-rword-list
948 (wl-draft-std11-parse-addresses (std11-lexical-analyze string))))))
950 (defun wl-draft-std11-parse-addresses (lal)
951 (let ((ret (std11-parse-address lal)))
952 (when (and (not (and (eq (length lal) 1)
953 (eq (car (car lal)) 'spaces)))
955 (error "Error while parsing address"))
957 (let ((dest (list (car ret))))
959 (while (and (setq ret (std11-parse-ascii-token lal))
960 (string-equal (cdr (assq 'specials (car ret))) ",")
961 (setq ret (std11-parse-address (cdr ret)))
963 (setq dest (cons (car ret) dest))
964 (setq lal (cdr ret)))
965 (while (eq 'spaces (car (car lal)))
966 (setq lal (cdr lal)))
967 (if lal (error "Error while parsing address"))
970 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
971 "Get mailbox list of FIELD from current buffer.
972 The buffer is expected to be narrowed to just the headers of the message.
973 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
974 from current buffer."
976 (let ((case-fold-search t)
977 (inhibit-read-only t)
979 mailbox-list beg seq has-group-list)
980 (goto-char (point-min))
981 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
984 (re-search-forward "^[^ \t]" nil 'move)
986 (skip-chars-backward "\n")
987 (setq seq (std11-lexical-analyze
988 (buffer-substring-no-properties beg (point))))
989 (setq addresses (wl-draft-std11-parse-addresses seq))
991 (cond ((eq (car (car addresses)) 'group)
992 (setq has-group-list t)
996 'std11-address-string
997 (nth 2 (car addresses))))))
998 ((eq (car (car addresses)) 'mailbox)
999 (setq address (nth 1 (car addresses)))
1003 (std11-addr-to-string
1004 (if (eq (car address) 'phrase-route-addr)
1006 (cdr address))))))))
1007 (setq addresses (cdr addresses)))
1008 (when (and remove-group-list has-group-list)
1009 (delete-region beg (point))
1010 (insert (wl-address-string-without-group-list-contents seq))))
1013 (defun wl-draft-deduce-address-list (buffer header-start header-end)
1014 "Get address list suitable for smtp RCPT TO:<address>.
1015 Group list content is removed if `wl-draft-remove-group-list-contents' is
1017 (let ((fields (if (and wl-draft-doing-mime-bcc
1018 wl-draft-disable-bcc-for-mime-bcc)
1020 '("to" "cc" "bcc")))
1021 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
1022 (case-fold-search t)
1026 (narrow-to-region header-start header-end)
1027 (goto-char (point-min))
1029 (if (re-search-forward "^resent-to[\t ]*:" nil t)
1030 (setq fields resent-fields)))
1034 (wl-draft-parse-mailbox-list
1036 wl-draft-remove-group-list-contents)))
1037 (setq fields (cdr fields)))
1043 (defun wl-draft-send-mail-with-smtp ()
1044 "Send the prepared message buffer with SMTP."
1046 (let* ((errbuf (if mail-interactive
1047 (generate-new-buffer " smtp errors")
1049 (case-fold-search t)
1050 (default-case-fold-search t)
1051 (sender (or wl-envelope-from
1052 (wl-address-header-extract-address wl-from)))
1053 (delimline (save-excursion
1054 (goto-char (point-min))
1056 (concat "^" (regexp-quote mail-header-separator)
1060 (or wl-smtp-posting-server smtp-server "localhost"))
1061 (smtp-service (or wl-smtp-posting-port smtp-service))
1062 (smtp-local-domain (or smtp-local-domain wl-local-domain))
1063 (id (std11-field-body "message-id"))
1065 (if (not (elmo-plugged-p smtp-server smtp-service))
1066 (wl-draft-set-sent-message 'mail 'unplugged
1067 (cons smtp-server smtp-service))
1070 ;; Instead of `smtp-deduce-address-list'.
1071 (setq recipients (wl-draft-deduce-address-list
1072 (current-buffer) (point-min) delimline))
1073 (unless recipients (error "No recipients"))
1074 ;; Insert an extra newline if we need it to work around
1075 ;; Sun's bug that swallows newlines.
1076 (goto-char (1+ delimline))
1077 (if (eval mail-mailer-swallows-blank-line)
1079 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1080 (if mail-interactive
1084 (wl-draft-delete-field "bcc" delimline)
1085 (wl-draft-delete-field "resent-bcc" delimline)
1086 (let (process-connection-type)
1089 (wl-smtp-extension-bind
1091 (smtp-send-buffer sender recipients (current-buffer))
1093 (wl-draft-write-sendlog 'failed 'smtp smtp-server
1095 (if (and (eq (car err) 'smtp-response-error)
1096 (= (nth 1 err) 535))
1098 (wl-smtp-password-key
1100 (car smtp-sasl-mechanisms)
1102 (signal (car err) (cdr err)))
1104 (wl-draft-write-sendlog 'uncertain 'smtp smtp-server
1106 (signal (car err) (cdr err)))))
1107 (wl-draft-set-sent-message 'mail 'sent)
1108 (wl-draft-write-sendlog
1109 'ok 'smtp smtp-server recipients id)))))
1110 (if (bufferp errbuf)
1111 (kill-buffer errbuf))))))
1113 (defun wl-draft-send-mail-with-pop-before-smtp ()
1114 "Send the prepared message buffer with POP-before-SMTP."
1115 (require 'elmo-pop3)
1119 :user (or wl-pop-before-smtp-user
1120 elmo-pop3-default-user)
1121 :server (or wl-pop-before-smtp-server
1122 elmo-pop3-default-server)
1123 :port (or wl-pop-before-smtp-port
1124 elmo-pop3-default-port)
1125 :auth (or wl-pop-before-smtp-authenticate-type
1126 elmo-pop3-default-authenticate-type)
1127 :stream-type (elmo-get-network-stream-type
1128 (or wl-pop-before-smtp-stream-type
1129 elmo-pop3-default-stream-type))))
1131 (condition-case error
1133 (setq session (elmo-pop3-get-session folder))
1134 (when session (elmo-network-close-session session)))
1136 (unless (string= (nth 1 error) "Unplugged")
1137 (signal (car error) (cdr error))))))
1138 (wl-draft-send-mail-with-smtp))
1140 (defun wl-draft-insert-required-fields (&optional force-msgid)
1141 "Insert Message-ID, Date, and From field.
1142 If FORCE-MSGID, insert message-id regardless of `wl-insert-message-id'."
1143 ;; Insert Message-Id field...
1144 (goto-char (point-min))
1145 (when (and (or force-msgid
1146 wl-insert-message-id)
1147 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1148 (insert (concat "Message-ID: "
1149 (funcall wl-message-id-function)
1151 ;; Insert date field.
1152 (goto-char (point-min))
1153 (or (re-search-forward "^Date[ \t]*:" nil t)
1154 (wl-draft-insert-date-field))
1155 ;; Insert from field.
1156 (goto-char (point-min))
1157 (or (re-search-forward "^From[ \t]*:" nil t)
1158 (wl-draft-insert-from-field)))
1160 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1161 "Send the message in the current buffer."
1163 (narrow-to-region (goto-char (point-min))
1164 (if (re-search-forward
1166 "^" (regexp-quote mail-header-separator) "$")
1170 (wl-draft-insert-required-fields)
1171 ;; ignore any blank lines in the header
1172 (while (progn (goto-char (point-min))
1173 (re-search-forward "\n[ \t]*\n\n*" nil t))
1174 (replace-match "\n"))
1175 (goto-char (point-min))
1176 (while (re-search-forward
1177 "^[^ \t\n:]+:[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n"
1179 (when (string= "" (match-string 1))
1180 (replace-match ""))))
1181 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1182 (wl-draft-dispatch-message)
1183 (when kill-when-done
1184 ;; hide editing-buffer.
1185 (wl-draft-hide editing-buffer)
1186 ;; delete editing-buffer and its file.
1187 (wl-draft-delete editing-buffer)))
1189 (defun wl-draft-dispatch-message (&optional mes-string)
1190 "Send the message in the current buffer. Not modified the header fields."
1191 (let (delimline mime-bcc)
1192 (if (and wl-draft-verbose-send mes-string)
1193 (message "%s" mes-string))
1195 (setq delimline (wl-draft-get-header-delimiter t))
1196 (unless wl-draft-fcc-list
1197 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1199 (setq wl-sent-message-modified nil)
1202 (if (and (wl-message-mail-p)
1203 (not (wl-draft-sent-message-p 'mail)))
1204 (if (or (not (or wl-draft-force-queuing
1205 wl-draft-force-queuing-mail))
1206 (memq 'mail wl-sent-message-queued))
1208 (setq mime-bcc (wl-draft-mime-bcc-field))
1209 (funcall wl-draft-send-mail-function)
1210 (when (not (zerop (length mime-bcc)))
1211 (wl-draft-do-mime-bcc mime-bcc)))
1212 (push 'mail wl-sent-message-queued)
1213 (wl-draft-set-sent-message 'mail 'unplugged)))
1214 (if (and (wl-message-news-p)
1215 (not (wl-draft-sent-message-p 'news))
1216 (not (wl-message-field-exists-p "Resent-to")))
1217 (if (or (not (or wl-draft-force-queuing
1218 wl-draft-force-queuing-news))
1219 (memq 'news wl-sent-message-queued))
1220 (funcall wl-draft-send-news-function)
1221 (push 'news wl-sent-message-queued)
1222 (wl-draft-set-sent-message 'news 'unplugged))))
1223 (let* ((status (wl-draft-sent-message-results))
1224 (unplugged-via (car status))
1225 (sent-via (nth 1 status)))
1226 ;; If one sent, process fcc folder.
1227 (if (and sent-via wl-draft-fcc-list)
1229 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1231 (setq wl-draft-fcc-list nil)))
1232 (if wl-draft-use-cache
1233 (let ((id (std11-field-body "Message-ID"))
1234 (elmo-enable-disconnected-operation t))
1235 (elmo-file-cache-save (elmo-file-cache-get-path id)
1237 ;; If one unplugged, append queue.
1238 (when (and unplugged-via
1239 wl-sent-message-modified)
1240 (if wl-draft-enable-queuing
1242 (wl-draft-queue-append wl-sent-message-via)
1243 (setq wl-sent-message-modified 'requeue))
1244 (error "Unplugged")))
1245 (when wl-draft-verbose-send
1246 (if (and unplugged-via sent-via);; combined message
1248 (setq wl-draft-verbose-msg
1249 (format "Sending%s and Queuing%s..."
1250 sent-via unplugged-via))
1251 (message "%sdone" wl-draft-verbose-msg))
1255 (if sent-via "done" "failed"))))))))
1256 (not wl-sent-message-modified)) ;; return value
1258 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1259 "Force send current buffer as raw message."
1262 (let (wl-interactive-send
1263 ;;; wl-draft-verbose-send
1264 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1265 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1268 (wl-draft-send kill-when-done mes-string))))
1270 (defun wl-draft-clone-local-variables ()
1271 (let ((locals (buffer-local-variables))
1274 (when (and (consp (car locals))
1276 (string-match wl-draft-clone-local-variable-regexp
1277 (symbol-name (car (car locals)))))
1278 (wl-append result (list (car (car locals)))))
1279 (setq locals (cdr locals)))
1282 (defcustom wl-draft-send-confirm-with-preview t
1283 "Non-nil to invoke preview through confirmation of sending.
1284 This variable is valid when `wl-interactive-send' has non-nil value."
1288 (defun wl-draft-send-confirm ()
1291 (condition-case quit
1293 (when wl-draft-send-confirm-with-preview
1294 (wl-draft-preview-message))
1296 (goto-char (point-min)) ; to show recipients in header
1300 (message "Send current draft? <y/n/j(down)/k(up)> ")
1301 (setq answer (let ((cursor-in-echo-area t)) (read-char)))
1320 (throw 'done nil)))))))
1322 (when wl-draft-send-confirm-with-preview
1323 (mime-preview-quit)))))
1325 (defun wl-draft-send (&optional kill-when-done mes-string)
1326 "Send current draft message.
1327 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1329 ;; Don't call this explicitly.
1330 ;; Added to 'wl-draft-send-hook (by teranisi)
1331 ;; (wl-draft-config-exec)
1332 (run-hooks 'wl-draft-send-hook)
1333 (when (or (not wl-interactive-send)
1334 (wl-draft-send-confirm))
1335 (let ((send-mail-function 'wl-draft-raw-send)
1336 (editing-buffer (current-buffer))
1337 (sending-buffer (wl-draft-generate-clone-buffer
1338 " *wl-draft-sending-buffer*"
1339 (append wl-draft-config-variables
1340 (wl-draft-clone-local-variables))))
1341 (wl-draft-verbose-msg nil)
1345 (set-buffer sending-buffer)
1346 (if (and (not (wl-message-mail-p))
1347 (not (wl-message-news-p)))
1348 (error "No recipient is specified"))
1349 (expand-abbrev) ; for mail-abbrevs
1350 (let ((mime-header-encode-method-alist
1352 '((wl-draft-eword-encode-address-list
1353 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
1354 (if (boundp 'mime-header-encode-method-alist)
1355 (symbol-value 'mime-header-encode-method-alist)))))
1356 (run-hooks 'mail-send-hook) ; translate buffer
1359 (if wl-draft-verbose-send
1360 (message "%s" (or mes-string "Sending...")))
1361 (funcall wl-draft-send-function editing-buffer kill-when-done)
1362 ;; Now perform actions on successful sending.
1363 (while mail-send-actions
1365 (apply (car (car mail-send-actions))
1366 (cdr (car mail-send-actions)))
1368 (setq mail-send-actions (cdr mail-send-actions)))
1369 (if wl-draft-verbose-send
1371 (or wl-draft-verbose-msg
1374 ;; kill sending buffer, anyway.
1375 (and (buffer-live-p sending-buffer)
1376 (kill-buffer sending-buffer))))))
1378 (defun wl-draft-mime-bcc-field ()
1379 "Return the MIME-Bcc field body. The field is deleted."
1380 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1381 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1383 (defun wl-draft-do-mime-bcc (field-body)
1384 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1385 (let ((orig-from (mime-decode-field-body (std11-field-body "from")
1387 (orig-subj (mime-decode-field-body (or (std11-field-body "subject")
1390 (recipients (wl-parse-addresses field-body))
1391 (draft-buffer (current-buffer))
1393 (save-window-excursion
1394 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1395 (not (zerop (length field-body))))
1396 (let ((wl-draft-doing-mime-bcc t))
1397 (dolist (recipient recipients)
1398 (wl-draft-create-buffer)
1399 (wl-draft-create-contents
1400 (append `((From . ,orig-from)
1402 (Subject . ,(concat "A blind carbon copy ("
1405 (wl-draft-default-headers)))
1406 (wl-draft-insert-mail-header-separator)
1407 (wl-draft-prepare-edit)
1408 (goto-char (point-max))
1409 (insert (or wl-draft-mime-bcc-body
1410 "This is a blind carbon copy.")
1412 (mime-edit-insert-tag "message" "rfc822")
1413 (insert-buffer draft-buffer)
1414 (let (wl-interactive-send)
1415 (wl-draft-send 'kill-when-done))))))))
1417 (defun wl-draft-save ()
1418 "Save current draft."
1420 (if (buffer-modified-p)
1422 (message "Saving...")
1423 (let ((msg (buffer-substring-no-properties (point-min) (point-max)))
1425 (when wl-draft-buffer-message-number
1426 (elmo-folder-delete-messages (wl-draft-get-folder)
1427 (list wl-draft-buffer-message-number))
1428 (wl-draft-config-info-operation wl-draft-buffer-message-number
1430 (elmo-folder-check (wl-draft-get-folder))
1431 ;; If no header separator, insert it.
1434 (goto-char (point-min))
1435 (unless (re-search-forward
1436 (concat "^" (regexp-quote mail-header-separator) "$")
1438 (goto-char (point-min))
1439 (if (re-search-forward "\n\n" nil t)
1440 (replace-match (concat "\n" mail-header-separator "\n"))
1441 (goto-char (point-max))
1442 (insert (if (eq (char-before) ?\n) "" "\n")
1443 mail-header-separator "\n")))
1444 (let ((mime-header-encode-method-alist
1445 '((eword-encode-unstructured-field-body))))
1446 (mime-edit-translate-buffer))
1447 (wl-draft-get-header-delimiter t)
1449 (elmo-folder-next-message-number (wl-draft-get-folder)))
1450 (elmo-folder-append-buffer (wl-draft-get-folder)))
1451 (elmo-folder-check (wl-draft-get-folder))
1452 (elmo-folder-commit (wl-draft-get-folder))
1453 (setq wl-draft-buffer-message-number next-number)
1454 (rename-buffer (format "%s/%d" wl-draft-folder next-number))
1455 (setq buffer-file-name (buffer-name))
1456 (set-buffer-modified-p nil)
1457 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save)
1458 (message "Saving...done")))
1459 (message "(No changes need to be saved)")))
1461 (defun wl-draft-mimic-kill-buffer ()
1462 "Kill the current (draft) buffer with query."
1464 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1467 (if (or (not bufname)
1468 (string-equal bufname "")
1469 (string-equal bufname (buffer-name)))
1470 (let ((bufname (current-buffer)))
1471 (when (or (not (buffer-modified-p))
1473 (format "Buffer %s modified; kill anyway? " bufname)))
1474 (set-buffer-modified-p nil)
1475 (wl-draft-hide bufname)
1476 (kill-buffer bufname)))
1477 (kill-buffer bufname))))
1479 (defun wl-draft-save-and-exit ()
1480 "Save current draft and exit current draft mode."
1483 (let ((editing-buffer (current-buffer)))
1484 (wl-draft-hide editing-buffer)
1485 (kill-buffer editing-buffer)))
1487 (defun wl-draft-send-and-exit ()
1488 "Send current draft message and kill it."
1492 (defun wl-draft-send-from-toolbar ()
1494 (let ((wl-interactive-send t))
1495 (wl-draft-send-and-exit)))
1497 (defun wl-draft-delete-field (field &optional delimline replace)
1498 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1500 (defun wl-draft-delete-fields (field &optional delimline replace)
1503 (goto-char (point-min))
1504 (if (search-forward "\n\n" nil t)
1505 (setq delimline (point))
1506 (setq delimline (point-max))))
1507 (narrow-to-region (point-min) delimline)
1508 (goto-char (point-min))
1509 (let ((regexp (concat "^" field ":"))
1510 (case-fold-search t))
1512 (if (looking-at regexp)
1518 (if (re-search-forward "^[^ \t]" nil t)
1519 (goto-char (match-beginning 0))
1522 (insert (concat field ": " replace "\n"))))
1524 (if (re-search-forward "^[^ \t]" nil t)
1525 (goto-char (match-beginning 0))
1528 (defun wl-draft-get-fcc-list (header-end)
1529 (if (and wl-draft-doing-mime-bcc
1530 wl-draft-disable-fcc-for-mime-bcc)
1532 (wl-draft-delete-field "fcc")
1535 (case-fold-search t))
1536 (or (markerp header-end) (error "HEADER-END must be a marker"))
1538 (goto-char (point-min))
1539 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1544 (buffer-substring-no-properties
1548 (skip-chars-backward " \t")
1551 (dolist (folder fcc-list)
1552 (wl-folder-confirm-existence
1553 (wl-folder-get-elmo-folder (eword-decode-string folder)))))
1554 (delete-region (match-beginning 0)
1555 (progn (forward-line 1) (point)))))
1558 (defcustom wl-draft-fcc-append-read-folder-history t
1559 "Non-nil to append fcc'ed folder to `wl-read-folder-history'."
1563 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1564 (let ((send-mail-buffer (current-buffer))
1565 (tembuf (generate-new-buffer " fcc output"))
1566 (case-fold-search t)
1568 (or (markerp header-end) (error "HEADER-END must be a marker"))
1571 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1574 ;; insert just the headers to avoid moving the gap more than
1575 ;; necessary (the message body could be arbitrarily huge.)
1576 (insert-buffer-substring send-mail-buffer 1 header-end)
1577 (wl-draft-insert-required-fields t)
1578 (goto-char (point-max))
1579 (insert-buffer-substring send-mail-buffer header-end)
1580 (let ((id (std11-field-body "Message-ID"))
1581 (elmo-enable-disconnected-operation t)
1584 (unless (or cache-saved
1585 (elmo-folder-plugged-p
1586 (wl-folder-get-elmo-folder (car fcc-list))))
1587 (elmo-file-cache-save id nil) ;; for disconnected operation
1588 (setq cache-saved t))
1589 (if (elmo-folder-append-buffer
1590 (wl-folder-get-elmo-folder
1591 (eword-decode-string (car fcc-list)))
1592 (and wl-fcc-force-as-read '(read)))
1593 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1594 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1595 (if (and wl-draft-fcc-append-read-folder-history
1596 (boundp 'wl-read-folder-history))
1597 (or (equal (car fcc-list) (car wl-read-folder-history))
1598 (setq wl-read-folder-history
1599 (append (list (car fcc-list)) wl-read-folder-history))))
1600 (setq fcc-list (cdr fcc-list)))))
1601 (kill-buffer tembuf)))
1603 (defun wl-draft-on-field-p ()
1606 (goto-char (point-min))
1607 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1614 (if (or (looking-at ".*,[ \t]?$")
1615 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1617 (let ((pos (point)))
1620 (if (looking-at "^[ \t]")
1622 (if (re-search-forward ":" pos t) nil t)))))))
1626 (defun wl-draft (&optional header-alist
1627 content-type content-transfer-encoding
1630 "Write and send mail/news message with Wanderlust."
1637 (wl-plugged-init t))
1639 (wl-init)) ; returns immediately if already initialized.
1642 (let (buffer header-alist-internal)
1643 (setq buffer (wl-draft-create-buffer parent-folder))
1644 (unless (cdr (assq 'From header-alist))
1646 (append (list (cons 'From wl-from)) header-alist)))
1647 (unless (cdr (assq 'To header-alist))
1652 (if (assq 'To header-alist)
1653 (setcdr (assq 'To header-alist) to)
1655 (append header-alist
1656 (list (cons 'To to))))))))
1657 (unless (cdr (assq 'Subject header-alist))
1658 (if (assq 'Subject header-alist)
1659 (setcdr (assq 'Subject header-alist) "")
1661 (append header-alist (list (cons 'Subject ""))))))
1662 (setq header-alist (append header-alist
1663 (wl-draft-default-headers)
1664 wl-draft-additional-header-alist
1665 (if body (list "" (cons 'Body body)))))
1666 (wl-draft-create-contents header-alist)
1668 (wl-draft-decode-body
1669 content-type content-transfer-encoding))
1670 (wl-draft-insert-mail-header-separator)
1671 (wl-draft-prepare-edit)
1673 (run-hooks 'wl-mail-setup-hook))
1674 (goto-char (point-min))
1675 (setq buffer-undo-list nil)
1676 (wl-user-agent-compose-internal) ;; user-agent
1679 (string= (cdr (assq 'To header-alist)) ""))
1680 (mail-position-on-field "To"))
1682 (goto-char (point-max))))
1685 (defun wl-draft-create-buffer (&optional parent-folder)
1686 (let* ((draft-folder (wl-draft-get-folder))
1687 (parent-folder (or parent-folder (wl-summary-buffer-folder-name)))
1688 (summary-buf (wl-summary-get-buffer parent-folder))
1690 (or (eq this-command 'wl-summary-reply)
1691 (eq this-command 'wl-summary-reply-with-citation)
1692 (eq this-command 'wl-summary-forward)
1693 (eq this-command 'wl-summary-target-mark-forward)
1694 (eq this-command 'wl-summary-target-mark-reply-with-citation)))
1695 (buffer (generate-new-buffer "*draft*")) ; Just for initial name.
1696 change-major-mode-hook)
1698 ;; switch-buffer according to draft buffer style.
1699 (if wl-draft-use-frame
1700 (switch-to-buffer-other-frame buffer)
1701 (if reply-or-forward
1702 (case wl-draft-reply-buffer-style
1704 (split-window-vertically)
1706 (switch-to-buffer buffer))
1708 (switch-to-buffer buffer))
1710 (delete-other-windows)
1711 (switch-to-buffer buffer))
1713 (if (functionp wl-draft-reply-buffer-style)
1714 (funcall wl-draft-reply-buffer-style buffer)
1715 (error "Invalid value for wl-draft-reply-buffer-style"))))
1716 (case wl-draft-buffer-style
1718 (when (eq major-mode 'wl-summary-mode)
1719 (wl-summary-toggle-disp-msg 'off))
1720 (split-window-vertically)
1722 (switch-to-buffer buffer))
1724 (switch-to-buffer buffer))
1726 (delete-other-windows)
1727 (switch-to-buffer buffer))
1728 (t (if (functionp wl-draft-buffer-style)
1729 (funcall wl-draft-buffer-style buffer)
1730 (error "Invalid value for wl-draft-buffer-style"))))))
1733 (set-buffer-multibyte t) ; draft buffer is always multibyte.
1734 (make-local-variable 'truncate-partial-width-windows)
1735 (setq truncate-partial-width-windows nil)
1736 (setq truncate-lines wl-draft-truncate-lines)
1737 (setq wl-sent-message-via nil)
1738 (setq wl-sent-message-queued nil)
1739 (setq wl-draft-config-exec-flag t)
1740 (setq wl-draft-parent-folder (or parent-folder ""))
1741 (or (eq this-command 'wl-folder-write-current-folder)
1742 (setq wl-draft-buffer-cur-summary-buffer summary-buf))
1745 (defun wl-draft-create-contents (header-alist)
1746 "header-alist' sample
1747 '(function ;; funcall
1748 string ;; insert string
1749 (symbol . string) ;; insert symbol-value: string
1750 (symbol . function) ;; (funcall) and if it returns string,
1751 ;; insert symbol-value: string
1752 (symbol . nil) ;; do nothing
1755 (unless (eq major-mode 'wl-draft-mode)
1756 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1757 (let ((halist header-alist)
1762 ((functionp (car halist)) (funcall (car halist)))
1764 ((stringp (car halist)) (insert (car halist) "\n"))
1766 ((consp (car halist))
1767 (setq field (car (car halist)))
1768 (setq value (cdr (car halist)))
1772 ((eq field 'Body) ; body
1774 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1776 (let ((value-return (funcall value)))
1777 (when (stringp value-return)
1778 (insert (symbol-name field) ": " value-return "\n"))))
1787 (setq halist (cdr halist)))))
1789 (defun wl-draft-prepare-edit ()
1790 (unless (eq major-mode 'wl-draft-mode)
1791 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1792 (let (change-major-mode-hook)
1793 (wl-draft-editor-mode)
1794 (static-when (boundp 'auto-save-file-name-transforms)
1795 (make-local-variable 'auto-save-file-name-transforms)
1796 (setq auto-save-file-name-transforms
1797 (cons (list (concat (regexp-quote wl-draft-folder)
1799 (concat (expand-file-name
1801 (elmo-folder-msgdb-path
1802 (wl-draft-get-folder)))
1804 auto-save-file-name-transforms)))
1805 (when wl-draft-write-file-function
1806 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
1807 (wl-draft-overload-functions)
1808 (wl-highlight-headers 'for-draft)
1810 (clear-visited-file-modtime)))
1812 (defun wl-draft-decode-header ()
1814 (std11-narrow-to-header)
1815 (wl-draft-decode-message-in-buffer)
1818 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1821 (std11-field-body "content-type")))
1822 (content-transfer-encoding
1823 (or content-transfer-encoding
1824 (std11-field-body "content-transfer-encoding")))
1827 (std11-narrow-to-header)
1828 (wl-draft-delete-field "content-type")
1829 (wl-draft-delete-field "content-transfer-encoding")
1830 (goto-char (point-max))
1831 (setq delimline (point-marker))
1833 (narrow-to-region delimline (point-max))
1834 (goto-char (point-min))
1836 (insert "Content-type: " content-type "\n"))
1837 (when content-transfer-encoding
1838 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1839 (wl-draft-decode-message-in-buffer)
1840 (goto-char (point-min))
1841 (unless (re-search-forward "^$" (point-at-eol) t)
1846 ;;; subroutine for wl-draft-create-contents
1847 ;;; must be used in wl-draft-mode
1848 (defun wl-draft-check-new-line ()
1849 (if (not (= (preceding-char) ?\n))
1852 (defsubst wl-draft-trim-ccs (cc)
1858 (null (and wl-draft-delete-myself-from-bcc-fcc
1860 (mapcar 'wl-address-header-extract-address
1862 (wl-parse-addresses (std11-field-body "To"))
1863 (wl-parse-addresses (std11-field-body "Cc"))))
1864 (mapcar 'downcase wl-subscribed-mailing-list)))))
1868 (defsubst wl-draft-default-headers ()
1870 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1871 (wl-address-header-extract-address
1873 (cons 'User-Agent wl-generate-mailer-string-function)
1874 (cons 'Reply-To mail-default-reply-to)
1875 (cons 'Bcc (function
1878 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1879 (cons 'Fcc (function
1881 (wl-draft-trim-ccs wl-fcc))))
1882 (cons 'Organization wl-organization)
1883 (and wl-auto-insert-x-face
1884 (file-exists-p wl-x-face-file)
1885 'wl-draft-insert-x-face-field-here) ;; allow nil
1886 mail-default-headers
1887 ;; check \n at th end of line for `mail-default-headers'
1888 'wl-draft-check-new-line
1891 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1894 (goto-char delimline)
1895 (goto-char (point-min))
1896 (if (search-forward "\n\n" nil t)
1897 (delete-backward-char 1)
1898 (goto-char (point-max))))
1899 (wl-draft-check-new-line)
1900 (put-text-property (point)
1902 (insert mail-header-separator "\n")
1904 'category 'mail-header-separator)
1909 (defun wl-draft-elmo-nntp-send ()
1910 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1911 (elmo-nntp-default-user
1912 (or wl-nntp-posting-user elmo-nntp-default-user))
1913 (elmo-nntp-default-server
1914 (or wl-nntp-posting-server elmo-nntp-default-server))
1915 (elmo-nntp-default-port
1916 (or wl-nntp-posting-port elmo-nntp-default-port))
1917 (elmo-nntp-default-stream-type
1918 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1919 (elmo-nntp-default-function wl-nntp-posting-function)
1921 (if (setq condition (cdr (elmo-string-matched-assoc
1922 (std11-field-body "Newsgroups")
1923 wl-nntp-posting-config-alist)))
1924 (if (stringp condition)
1925 (setq elmo-nntp-default-server condition)
1926 (while (car condition)
1927 (set (intern (format "elmo-nntp-default-%s"
1928 (symbol-name (caar condition))))
1930 (setq condition (cdr condition)))))
1931 (unless elmo-nntp-default-function
1932 (error "wl-draft-nntp-send: posting-function is nil"))
1933 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1934 (wl-draft-set-sent-message 'news 'unplugged
1935 (cons elmo-nntp-default-server
1936 elmo-nntp-default-port))
1937 (funcall elmo-nntp-default-function
1938 elmo-nntp-default-server (current-buffer))
1939 (wl-draft-set-sent-message 'news 'sent)
1940 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1941 (std11-field-body "Newsgroups")
1942 (std11-field-body "Message-ID")))))
1944 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1945 "Generate clone of current buffer named NAME."
1946 (let ((editing-buffer (current-buffer)))
1948 (set-buffer (generate-new-buffer name))
1951 (wl-draft-editor-mode)
1952 (insert-buffer editing-buffer)
1954 (while local-variables
1955 (make-local-variable (car local-variables))
1956 (set (car local-variables)
1958 (set-buffer editing-buffer)
1959 (symbol-value (car local-variables))))
1960 (setq local-variables (cdr local-variables)))
1963 (defun wl-draft-remove-text-plain-tag ()
1964 "Remove text/plain tag of mime-edit."
1965 (when (string= (mime-make-text-tag "plain")
1966 (buffer-substring-no-properties (point-at-bol)(point-at-eol)))
1967 (delete-region (point-at-bol)(1+ (point-at-eol)))))
1969 (defun wl-draft-reedit (number)
1970 (let ((draft-folder (wl-draft-get-folder))
1973 buffer change-major-mode-hook body-top)
1974 (setq buffer (get-buffer-create (format "%s/%d" wl-draft-folder
1976 (if wl-draft-use-frame
1977 (switch-to-buffer-other-frame buffer)
1978 (switch-to-buffer buffer))
1980 (elmo-message-fetch draft-folder number (elmo-make-fetch-strategy 'entire)
1981 nil (current-buffer))
1982 (elmo-delete-cr-buffer)
1983 (let ((mime-edit-again-ignored-field-regexp
1984 "^\\(Content-.*\\|Mime-Version\\):"))
1985 (wl-draft-decode-message-in-buffer))
1986 (setq body-top (wl-draft-insert-mail-header-separator))
1989 (make-local-variable 'truncate-partial-width-windows)
1990 (setq truncate-partial-width-windows nil)
1991 (setq truncate-lines wl-draft-truncate-lines)
1992 (setq wl-sent-message-via nil)
1993 (setq wl-sent-message-queued nil)
1994 (wl-draft-config-info-operation number 'load)
1995 (goto-char (point-min))
1996 (wl-draft-overload-functions)
1997 (wl-draft-editor-mode)
1998 (static-when (boundp 'auto-save-file-name-transforms)
1999 (make-local-variable 'auto-save-file-name-transforms)
2000 (setq auto-save-file-name-transforms
2001 (cons (list (concat (regexp-quote wl-draft-folder)
2003 (concat (expand-file-name
2005 (elmo-folder-msgdb-path
2006 (wl-draft-get-folder)))
2008 auto-save-file-name-transforms)))
2009 (setq buffer-file-name (buffer-name)
2010 wl-draft-parent-folder ""
2011 wl-draft-buffer-message-number number)
2012 (when wl-draft-write-file-function
2013 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
2014 (wl-highlight-headers 'for-draft)
2015 (goto-char body-top)
2016 (run-hooks 'wl-draft-reedit-hook)
2017 (goto-char (point-max))
2020 (defmacro wl-draft-body-goto-top ()
2022 (goto-char (point-min))
2023 (if (re-search-forward mail-header-separator nil t)
2025 (goto-char (point-max))))))
2027 (defmacro wl-draft-body-goto-bottom ()
2028 (` (goto-char (point-max))))
2030 (defmacro wl-draft-config-body-goto-header ()
2032 (goto-char (point-min))
2033 (if (re-search-forward mail-header-separator nil t)
2035 (goto-char (point-max))))))
2037 (defsubst wl-draft-config-sub-eval-insert (content &optional newline)
2038 (let (content-value)
2040 (stringp (setq content-value (eval content))))
2041 (insert content-value)
2042 (if newline (insert "\n")))))
2044 (defun wl-draft-config-sub-body (content)
2045 (wl-draft-body-goto-top)
2046 (delete-region (point) (point-max))
2047 (wl-draft-config-sub-eval-insert content))
2049 (defun wl-draft-config-sub-top (content)
2050 (wl-draft-body-goto-top)
2051 (wl-draft-config-sub-eval-insert content))
2053 (defun wl-draft-config-sub-bottom (content)
2054 (wl-draft-body-goto-bottom)
2055 (wl-draft-config-sub-eval-insert content))
2057 (defun wl-draft-config-sub-header (content)
2058 (wl-draft-config-body-goto-header)
2059 (wl-draft-config-sub-eval-insert content 'newline))
2061 (defun wl-draft-config-sub-header-top (content)
2062 (goto-char (point-min))
2063 (wl-draft-config-sub-eval-insert content 'newline))
2065 (defun wl-draft-config-sub-part-top (content)
2066 (goto-char (mime-edit-content-beginning))
2067 (wl-draft-config-sub-eval-insert content 'newline))
2069 (defun wl-draft-config-sub-part-bottom (content)
2070 (goto-char (mime-edit-content-end))
2071 (wl-draft-config-sub-eval-insert content 'newline))
2073 (defsubst wl-draft-config-sub-file (content)
2074 (let ((coding-system-for-read wl-cs-autoconv)
2075 (file (expand-file-name (eval content))))
2076 (if (file-exists-p file)
2077 (insert-file-contents file)
2078 (error "%s: no exists file" file))))
2080 (defun wl-draft-config-sub-body-file (content)
2081 (wl-draft-body-goto-top)
2082 (delete-region (point) (point-max))
2083 (wl-draft-config-sub-file content))
2085 (defun wl-draft-config-sub-top-file (content)
2086 (wl-draft-body-goto-top)
2087 (wl-draft-config-sub-file content))
2089 (defun wl-draft-config-sub-bottom-file (content)
2090 (wl-draft-body-goto-bottom)
2091 (wl-draft-config-sub-file content))
2093 (defun wl-draft-config-sub-header-file (content)
2094 (wl-draft-config-body-goto-header)
2095 (wl-draft-config-sub-file content))
2097 (defun wl-draft-config-sub-template (content)
2098 (setq wl-draft-config-variables
2099 (wl-template-insert (eval content))))
2101 (defun wl-draft-config-sub-x-face (content)
2102 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
2103 (fboundp 'x-face-insert)) ; x-face.el is installed.
2104 (x-face-insert content)
2105 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
2107 (defsubst wl-draft-config-sub-func (field content)
2109 (if (setq func (assq field wl-draft-config-sub-func-alist))
2110 (let (wl-draft-config-variables)
2111 (funcall (cdr func) content)
2112 ;; for wl-draft-config-sub-template
2113 (cons t wl-draft-config-variables)))))
2115 (defsubst wl-draft-config-exec-sub (clist)
2116 (let (config local-variables)
2118 (setq config (car clist))
2123 (let ((field (car config))
2124 (content (cdr config))
2128 (wl-draft-replace-field field (eval content) t))
2129 ((setq ret-val (wl-draft-config-sub-func field content))
2130 (if (cdr ret-val) ;; for wl-draft-config-sub-template
2131 (wl-append local-variables (cdr ret-val))))
2132 ((boundp field) ;; variable
2133 (make-local-variable field)
2134 (set field (eval content))
2135 (wl-append local-variables (list field)))
2137 (error "%s: not variable" field)))))
2139 (error "%s: not supported type" config)))
2140 (setq clist (cdr clist)))
2143 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
2144 "Change headers in draft preparation time."
2146 (unless wl-draft-reedit
2149 (and (boundp 'wl-draft-prepared-config-alist)
2150 wl-draft-prepared-config-alist) ;; For compatible.
2151 wl-draft-config-alist)))
2153 (wl-draft-config-exec config-alist reply-buf)))))
2155 (defun wl-draft-config-exec (&optional config-alist reply-buf)
2156 "Change headers according to the value of `wl-draft-config-alist'.
2157 Automatically applied in draft sending time."
2159 (let ((case-fold-search t)
2160 (alist (or config-alist wl-draft-config-alist))
2161 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2162 wl-draft-reply-buffer)))
2163 (local-variables wl-draft-config-variables)
2165 (when (and (or (interactive-p)
2166 wl-draft-config-exec-flag)
2171 (setq key (caar alist)
2178 (set-buffer reply-buf)
2180 (std11-narrow-to-header)
2181 (goto-char (point-min))
2182 (re-search-forward (car clist) nil t))))
2183 (wl-draft-config-exec-sub (cdr clist))
2186 (when (save-restriction
2187 (std11-narrow-to-header mail-header-separator)
2188 (goto-char (point-min))
2189 (re-search-forward key nil t))
2190 (wl-append local-variables
2191 (wl-draft-config-exec-sub clist))
2194 (wl-append local-variables
2195 (wl-draft-config-exec-sub clist))
2197 (if (and found wl-draft-config-matchone)
2199 (setq alist (cdr alist)))))
2201 (setq wl-draft-config-exec-flag nil))
2202 (run-hooks 'wl-draft-config-exec-hook)
2203 (put-text-property (point-min)(point-max) 'face nil)
2204 (wl-highlight-message (point-min)(point-max) t)
2205 (setq wl-draft-config-variables
2206 (elmo-uniq-list local-variables)))))
2208 (defun wl-draft-replace-field (field content &optional add)
2211 (let ((case-fold-search t)
2212 (inhibit-read-only t) ;; added by teranisi.
2214 (std11-narrow-to-header mail-header-separator)
2215 (goto-char (point-min))
2216 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2221 (re-search-forward "^[^ \t]" nil 'move)
2223 (skip-chars-backward "\n")
2224 (delete-region beg (point))
2225 (insert " " content))
2230 (re-search-forward "^[^ \t]" nil 'move)
2232 (delete-region beg (point)))
2233 (when (and add content)
2235 (goto-char (point-max))
2236 (insert (concat field ": " content "\n"))))))))
2238 (defun wl-draft-config-info-operation (msg operation)
2239 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-draft-get-folder)))
2242 (format "%s-%d" wl-draft-config-save-filename msg)
2244 element alist variable)
2246 ((eq operation 'save)
2247 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2248 (while (setq variable (pop variables))
2249 (when (boundp variable)
2251 (list (cons variable (eval variable))))))
2252 (elmo-object-save filename alist)))
2253 ((eq operation 'load)
2254 (setq alist (elmo-object-load filename))
2255 (while (setq element (pop alist))
2256 (set (make-local-variable (car element)) (cdr element))
2257 (wl-append wl-draft-config-variables (list (car element)))))
2258 ((eq operation 'delete)
2259 (if (file-exists-p filename)
2260 (delete-file filename))))))
2262 (defun wl-draft-queue-info-operation (msg operation
2263 &optional add-sent-message-via)
2264 (let* ((msgdb-dir (elmo-folder-msgdb-path
2265 (wl-folder-get-elmo-folder wl-queue-folder)))
2268 (format "%s-%d" wl-draft-queue-save-filename msg)
2270 element alist variable)
2272 ((eq operation 'save)
2273 (let ((variables (elmo-uniq-list
2274 (append wl-draft-queue-save-variables
2275 wl-draft-config-variables
2276 (list 'wl-draft-fcc-list)))))
2277 (if add-sent-message-via
2279 (push 'wl-sent-message-queued variables)
2280 (push 'wl-sent-message-via variables)))
2281 (while (setq variable (pop variables))
2282 (when (boundp variable)
2284 (list (cons variable (eval variable))))))
2285 (elmo-object-save filename alist)))
2286 ((eq operation 'load)
2287 (setq alist (elmo-object-load filename))
2288 (while (setq element (pop alist))
2289 (set (make-local-variable (car element)) (cdr element))))
2290 ((eq operation 'get-sent-via)
2291 (setq alist (elmo-object-load filename))
2292 (cdr (assq 'wl-sent-message-via alist)))
2293 ((eq operation 'delete)
2294 (if (file-exists-p filename)
2295 (delete-file filename))))))
2297 (defun wl-draft-queue-append (wl-sent-message-via)
2298 (if wl-draft-verbose-send
2299 (message "Queuing..."))
2300 (let ((send-buffer (current-buffer))
2301 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2302 (message-id (std11-field-body "Message-ID")))
2303 (if (elmo-folder-append-buffer folder)
2305 (wl-draft-queue-info-operation
2306 (car (elmo-folder-status folder))
2307 'save wl-sent-message-via)
2308 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2309 (when wl-draft-verbose-send
2310 (setq wl-draft-verbose-msg "Queuing...")
2311 (message "Queuing...done")))
2312 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2313 (error "Queuing failed"))))
2315 (defun wl-draft-queue-flush ()
2316 "Flush draft queue."
2318 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2320 (elmo-folder-open-internal queue-folder)
2321 (elmo-folder-list-messages queue-folder)))
2324 (wl-draft-queue-flushing t)
2325 msgs failure len buffer msgid sent-via)
2326 ;; get plugged send message
2328 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2331 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2332 (or (not (nth 2 (car sent-via)))
2334 (car (nth 2 (car sent-via)))
2335 (cdr (nth 2 (car sent-via))))))
2336 (wl-append msgs (list (car msgs2)))
2338 (setq sent-via (cdr sent-via))))
2339 (setq msgs2 (cdr msgs2)))
2340 (when (> (setq len (length msgs)) 0)
2341 (if (elmo-y-or-n-p (format
2342 "%d message(s) are in the sending queue. Send now? "
2344 (not elmo-dop-flush-confirm) t)
2347 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2350 ;; reset buffer local variables
2351 (kill-all-local-variables)
2355 (setq wl-sent-message-via nil)
2356 (wl-draft-queue-info-operation (car msgs) 'load)
2357 (elmo-message-fetch queue-folder
2359 (elmo-make-fetch-strategy 'entire)
2360 nil (current-buffer))
2362 (setq failure (funcall
2363 wl-draft-queue-flush-send-function
2364 (format "Sending (%d/%d)..." i len)))
2365 ;;; (wl-draft-raw-send nil nil
2366 ;;; (format "Sending (%d/%d)..." i len))
2368 (elmo-display-error err t)
2372 (if (eq wl-sent-message-modified 'requeue)
2374 (elmo-folder-delete-messages
2375 queue-folder (cons (car msgs) nil))
2376 (wl-draft-queue-info-operation (car msgs) 'delete))
2378 (elmo-folder-delete-messages
2379 queue-folder (cons (car msgs) nil))
2380 (wl-draft-queue-info-operation (car msgs) 'delete)
2381 (setq performed (+ 1 performed))))
2382 (setq msgs (cdr msgs)))
2383 (kill-buffer buffer)
2384 (message "%d message(s) are sent." performed)))
2385 (message "%d message(s) are remained to be sent." len))
2386 (elmo-folder-close queue-folder)
2389 (defun wl-jump-to-draft-buffer (&optional arg)
2390 "Jump to the draft if exists."
2393 (wl-jump-to-draft-folder)
2394 (let ((draft-bufs (wl-collect-draft))
2398 (message "No draft buffer exist."))
2401 (sort (mapcar 'buffer-name draft-bufs)
2402 (function (lambda (a b)
2403 (not (string< a b))))))
2404 (if (setq buf (cdr (member (buffer-name)
2406 (setq buf (car buf))
2407 (setq buf (car draft-bufs)))
2408 (switch-to-buffer buf))))))
2410 (defun wl-jump-to-draft-folder ()
2411 (let ((msgs (reverse (elmo-folder-list-messages (wl-draft-get-folder))))
2412 (mybuf (buffer-name))
2415 (message "No draft message exist.")
2416 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2417 (setq msg (cadr (memq
2418 (string-to-int (substring mybuf (match-end 0)))
2420 (or msg (setq msg (car msgs)))
2421 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2422 (switch-to-buffer buf)
2423 (wl-draft-reedit msg)))))
2425 (defun wl-draft-highlight-and-recenter (&optional n)
2427 (when wl-highlight-body-too
2428 (let ((modified (buffer-modified-p)))
2431 (put-text-property (point-min) (point-max) 'face nil)
2432 (wl-highlight-message (point-min) (point-max) t))
2433 (set-buffer-modified-p modified))))
2434 (static-when (featurep 'xemacs)
2435 ;; Cope with one of many XEmacs bugs that `recenter' takes
2436 ;; a long time if there are a lot of invisible text lines.
2440 ;; insert element from history
2441 (defvar wl-draft-current-history-position nil)
2442 (defvar wl-draft-history-backup-word "")
2444 (defun wl-draft-previous-history-element (n)
2446 (let (bol history beg end prev new)
2447 (when (and (not (wl-draft-on-field-p))
2450 (goto-char (point-min))
2451 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
2455 (while (and (looking-at "^[ \t]")
2456 (not (= (point) (point-min))))
2459 ((looking-at wl-folder-complete-header-regexp)
2460 (and (boundp 'wl-read-folder-history)
2461 (setq history wl-read-folder-history)))
2462 ;; ((looking-at wl-address-complete-header-regexp)
2463 ;; (setq history .....))
2467 (setq bol (save-excursion (beginning-of-line) (point)))
2468 (cond ((and (or (eq last-command 'wl-draft-previous-history-element)
2469 (eq last-command 'wl-draft-next-history-element))
2470 wl-draft-current-history-position)
2472 (or (search-backward-regexp ",[ \t]*\\(.*\\)" bol t)
2473 (search-backward-regexp "^[ \t]\\(.*\\)" bol t)
2474 (search-backward-regexp "^[^ \t]*: \\(.*\\)" bol t))
2475 (setq prev (match-string 1))
2476 (goto-char (match-beginning 1))
2479 (>= (+ n wl-draft-current-history-position) 0))
2481 (<= (+ n wl-draft-current-history-position)
2484 (setq wl-draft-current-history-position
2485 (+ n wl-draft-current-history-position))
2487 (nth wl-draft-current-history-position
2488 (append (list wl-draft-history-backup-word)
2490 (delete-region beg end)
2494 (message "End of history; no next item"))
2496 (message "Beginning of history; no preceding item")))))
2499 (or (search-backward-regexp ",[ \t]*\\(.*\\)" bol t)
2500 (search-backward-regexp "^[ \t]\\(.*\\)" bol t)
2501 (search-backward-regexp "^[^ \t]*: \\(.*\\)" bol t)))
2503 (setq wl-draft-current-history-position 1)
2504 (setq wl-draft-history-backup-word (match-string 1))
2505 (delete-region (match-beginning 1) (match-end 1))
2506 (insert (car history)))
2508 (setq wl-draft-current-history-position nil))))))
2510 (defun wl-draft-next-history-element (n)
2512 (wl-draft-previous-history-element (- n)))
2514 ;;;; user-agent support by Sen Nagata
2516 ;; this appears to be necessarily global...
2517 (defvar wl-user-agent-compose-p nil)
2518 (defvar wl-user-agent-headers-and-body-alist nil)
2520 ;; this should be a generic function for mail-mode -- i wish there was
2521 ;; something like it in sendmail.el
2522 (defun wl-user-agent-insert-header (header-name header-value)
2523 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2524 ;; it seems like overriding existing headers is acceptable -- should
2525 ;; we provide an option?
2527 ;; plan was: unfold header (might be folded), remove existing value, insert
2529 ;; wl doesn't seem to fold header lines yet anyway :-)
2531 (let ((kill-whole-line t)
2533 (mail-position-on-field (capitalize header-name))
2534 (setq end-of-line (point))
2536 (re-search-forward ":" end-of-line)
2537 (insert (concat " " header-value "\n"))
2540 ;; this should be a generic function for mail-mode -- i wish there was
2541 ;; something like it in sendmail.el
2543 ;; ** haven't dealt w/ case where the body is already set **
2544 (defun wl-user-agent-insert-body (body-text)
2545 "Insert a body of text, BODY-TEXT, into a message."
2546 ;; code defensively... :-P
2547 (goto-char (point-min))
2548 (search-forward mail-header-separator)
2553 (defun wl-user-agent-compose (&optional to subject other-headers continue
2554 switch-function yank-action
2556 "Support the `compose-mail' interface for wl.
2557 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2558 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2559 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2561 (unless (featurep 'wl)
2564 (setq switch-function 'keep))
2565 ;; protect these -- to and subject get bound at some point, so it looks
2566 ;; to be necessary to protect the values used w/in
2567 (let ((wl-user-agent-headers-and-body-alist other-headers)
2568 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2569 (wl-draft-buffer-style switch-function))
2571 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2574 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2577 (setq wl-user-agent-headers-and-body-alist
2578 (cons (cons "to" to)
2579 wl-user-agent-headers-and-body-alist))))
2581 (if (wl-string-match-assoc "subject"
2582 wl-user-agent-headers-and-body-alist
2585 (wl-string-match-assoc "subject"
2586 wl-user-agent-headers-and-body-alist
2589 (setq wl-user-agent-headers-and-body-alist
2590 (cons (cons "subject" subject)
2591 wl-user-agent-headers-and-body-alist))))
2592 ;; i think this is what we want to use...
2595 ;; tell the hook-function to do its stuff
2596 (setq wl-user-agent-compose-p t)
2597 ;; because to get the hooks working, wl-draft has to think it has
2598 ;; been called interactively
2599 (call-interactively 'wl-draft))
2600 (setq wl-user-agent-compose-p nil))))
2602 (defun wl-user-agent-compose-internal ()
2603 "Manipulate headers and/or a body of a draft message."
2604 ;; being called from wl-user-agent-compose?
2605 (if wl-user-agent-compose-p
2608 (let ((headers wl-user-agent-headers-and-body-alist)
2609 (case-fold-search t))
2612 (if (not (string-match "^body$" (car (car headers))))
2613 (wl-user-agent-insert-header
2614 (car (car headers)) (cdr (car headers)))
2616 (setq headers (cdr headers))))
2617 ;; highlight headers (from wl-draft in wl-draft.el)
2618 (wl-highlight-headers 'for-draft)
2620 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2622 (wl-user-agent-insert-body
2623 (cdr (wl-string-match-assoc
2625 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2629 (product-provide (provide 'wl-draft) (require 'wl-version))
2631 ;;; wl-draft.el ends here