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))
39 (require 'eword-encode)
42 (defvar x-face-add-x-face-version-header)
43 (defvar mail-reply-buffer)
44 (defvar mail-from-style)
50 (defalias-maybe 'x-face-insert 'ignore)
51 (defalias-maybe 'x-face-insert-version-header 'ignore)
52 (defalias-maybe 'wl-init 'ignore)
53 (defalias-maybe 'wl-draft-mode 'ignore))
56 (autoload 'wl-addrmgr "wl-addrmgr"))
58 (autoload 'open-ssl-stream "ssl")
60 (defvar wl-draft-buffer-message-number nil)
61 (defvar wl-draft-field-completion-list nil)
62 (defvar wl-draft-verbose-send t)
63 (defvar wl-draft-verbose-msg nil)
64 (defvar wl-draft-queue-flushing nil)
65 (defvar wl-draft-config-variables nil)
66 (defvar wl-draft-config-exec-flag t)
67 (defvar wl-draft-buffer-cur-summary-buffer nil)
68 (defvar wl-draft-clone-local-variable-regexp "^\\(wl\\|mime\\)")
69 (defvar wl-draft-sendlog-filename "sendlog")
70 (defvar wl-draft-queue-save-filename "qinfo")
71 (defvar wl-draft-config-save-filename "config")
72 (defvar wl-draft-queue-flush-send-function 'wl-draft-dispatch-message)
73 (defvar wl-sent-message-via nil)
74 (defvar wl-sent-message-modified nil)
75 (defvar wl-sent-message-queued nil)
76 (defvar wl-draft-fcc-list nil)
77 (defvar wl-draft-reedit nil)
78 (defvar wl-draft-reply-buffer nil)
79 (defvar wl-draft-forward nil)
80 (defvar wl-draft-doing-mime-bcc nil)
82 (defvar wl-draft-parent-folder nil
83 "Folder name of the summary in which current draft is invoked.
84 This variable is local in each draft buffer.
85 You can refer its value in `wl-draft-config-alist'.
88 \(setq wl-draft-config-alist
89 '(((string-match \".*@domain1$\" wl-draft-parent-folder)
90 (\"From\" . \"user@domain1\"))
91 ((string-match \".*@domain2$\" wl-draft-parent-folder)
92 (\"From\" . \"user@domain2\"))))")
94 (defvar wl-draft-parent-number nil)
95 (defvar wl-draft-parent-flag nil)
97 (defconst wl-draft-parent-variables
98 '(wl-draft-parent-folder
99 wl-draft-parent-number
100 wl-draft-parent-flag))
102 (defvar wl-draft-config-sub-func-alist
103 '((body . wl-draft-config-sub-body)
104 (top . wl-draft-config-sub-top)
105 (bottom . wl-draft-config-sub-bottom)
106 (header . wl-draft-config-sub-header)
107 (header-top . wl-draft-config-sub-header-top)
108 (header-bottom . wl-draft-config-sub-header)
109 (part-top . wl-draft-config-sub-part-top)
110 (part-bottom . wl-draft-config-sub-part-bottom)
111 (body-file . wl-draft-config-sub-body-file)
112 (top-file . wl-draft-config-sub-top-file)
113 (bottom-file . wl-draft-config-sub-bottom-file)
114 (header-file . wl-draft-config-sub-header-file)
115 (template . wl-draft-config-sub-template)
116 (x-face . wl-draft-config-sub-x-face)))
118 (make-variable-buffer-local 'wl-draft-buffer-message-number)
119 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
120 (make-variable-buffer-local 'wl-draft-config-variables)
121 (make-variable-buffer-local 'wl-draft-config-exec-flag)
122 (make-variable-buffer-local 'wl-sent-message-via)
123 (make-variable-buffer-local 'wl-sent-message-queued)
124 (make-variable-buffer-local 'wl-draft-fcc-list)
125 (make-variable-buffer-local 'wl-draft-reply-buffer)
126 (make-variable-buffer-local 'wl-draft-parent-folder)
127 (make-variable-buffer-local 'wl-draft-parent-number)
128 (make-variable-buffer-local 'wl-draft-parent-flag)
130 (defvar wl-draft-folder-internal nil
131 "Internal variable for caching `opened' draft folder.")
133 (defsubst wl-smtp-password-key (user mechanism server)
134 (format "SMTP:%s/%s@%s"
135 user mechanism server))
137 (defmacro wl-smtp-extension-bind (&rest body)
138 (` (let* ((smtp-sasl-mechanisms
139 (if wl-smtp-authenticate-type
141 (if (listp wl-smtp-authenticate-type)
142 wl-smtp-authenticate-type
143 (list wl-smtp-authenticate-type)))))
144 (smtp-use-sasl (and smtp-sasl-mechanisms t))
145 (smtp-use-starttls (eq wl-smtp-connection-type 'starttls))
146 (smtp-open-connection-function
147 (if (eq wl-smtp-connection-type 'ssl)
149 smtp-open-connection-function))
151 (if (eq wl-smtp-connection-type 'ssl)
154 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
155 (setq smtp-sasl-user-name wl-smtp-posting-user
156 smtp-sasl-properties (when wl-smtp-authenticate-realm
158 wl-smtp-authenticate-realm)))
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 &optional number)
273 (let (references parent-folder subject)
274 (with-current-buffer summary-buf
275 (setq parent-folder (wl-summary-buffer-folder-name)))
276 (let ((decoder (mime-find-field-decoder 'Subject 'plain)))
277 (setq subject (if (and original-subject decoder)
278 (funcall decoder original-subject) original-subject)))
279 (with-current-buffer (wl-message-get-original-buffer)
280 (setq subject (wl-draft-forward-make-subject subject))
281 (setq references (nconc
282 (std11-field-bodies '("References" "In-Reply-To"))
283 (list (std11-field-body "Message-Id"))))
284 (setq references (delq nil references)
285 references (mapconcat 'identity references " ")
286 references (wl-draft-parse-msg-id-list-string references)
287 references (wl-delete-duplicates references)
288 references (when references
289 (mapconcat 'identity references "\n\t"))))
290 (and wl-draft-use-frame
291 (get-buffer-window summary-buf)
292 (select-window (get-buffer-window summary-buf)))
293 (wl-draft (list (cons 'To "")
294 (cons 'Subject subject)
295 (cons 'References references))
296 nil nil nil nil parent-folder number))
297 (goto-char (point-max))
298 (wl-draft-insert-message)
299 (mail-position-on-field "To")
300 (setq wl-draft-config-variables
301 (append wl-draft-parent-variables
302 wl-draft-config-variables))
303 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save)
304 (run-hooks 'wl-draft-forward-hook))
306 (defun wl-draft-self-reply-p ()
307 "Return t when From address in the current message is user's self one or not."
308 (wl-address-user-mail-address-p (or (elmo-field-body "From") "")))
310 (defun wl-draft-reply (buf with-arg summary-buf &optional number)
311 "Reply to BUF buffer message.
312 Reply to author if WITH-ARG is non-nil."
315 to mail-followup-to cc subject in-reply-to references newsgroups
316 to-alist cc-alist decoder parent-folder)
317 (when (buffer-live-p summary-buf)
318 (with-current-buffer summary-buf
319 (setq parent-folder (wl-summary-buffer-folder-name))))
320 (set-buffer (or buf mime-mother-buffer))
321 (setq r-list (if with-arg wl-draft-reply-with-argument-list
322 wl-draft-reply-without-argument-list))
325 (when (let ((condition (car (car r-list))))
326 (cond ((stringp condition)
327 (std11-field-body condition))
332 ((stringp (car condition))
333 (or (std11-field-body (car condition))
335 ((symbolp (car condition))
336 (or (funcall (car condition))
340 (setq condition (cdr condition)))
343 (funcall condition))))
344 (let ((r-to-list (nth 0 (cdr (car r-list))))
345 (r-cc-list (nth 1 (cdr (car r-list))))
346 (r-ng-list (nth 2 (cdr (car r-list)))))
347 (when (and (member "Followup-To" r-ng-list)
348 (string= (std11-field-body "Followup-To") "poster"))
349 (setq r-to-list (cons "From" r-to-list))
350 (setq r-ng-list (delete "Followup-To"
351 (copy-sequence r-ng-list))))
352 (if (and r-to-list (symbolp r-to-list))
353 (setq to (wl-concat-list (funcall r-to-list) ","))
354 (setq to (wl-concat-list (cons to
355 (elmo-multiple-fields-body-list
358 (if (and r-cc-list (symbolp r-cc-list))
359 (setq cc (wl-concat-list (funcall r-cc-list) ","))
360 (setq cc (wl-concat-list (cons cc
361 (elmo-multiple-fields-body-list
364 (if (and r-ng-list (symbolp r-ng-list))
365 (setq newsgroups (wl-concat-list (funcall r-ng-list) ","))
366 (setq newsgroups (wl-concat-list (cons newsgroups
371 (setq r-list (cdr r-list)))
372 (error "No match field: check your `wl-draft-reply-%s-argument-list'"
373 (if with-arg "with" "without")))
374 (setq subject (std11-field-body "Subject"))
375 (setq to (wl-parse-addresses to)
376 cc (wl-parse-addresses cc))
377 (with-temp-buffer ; to keep raw buffer unibyte.
378 (set-buffer-multibyte default-enable-multibyte-characters)
379 (setq decoder (mime-find-field-decoder 'Subject 'plain))
380 (setq subject (if (and subject decoder)
381 (funcall decoder subject) subject))
385 (setq decoder (mime-find-field-decoder 'To 'plain))
386 (cons (nth 1 (std11-extract-address-components addr))
387 (if decoder (funcall decoder addr) addr)))
392 (setq decoder (mime-find-field-decoder 'Cc 'plain))
393 (cons (nth 1 (std11-extract-address-components addr))
394 (if decoder (funcall decoder addr) addr)))
396 (setq subject (wl-draft-reply-make-subject subject))
397 (setq in-reply-to (std11-field-body "Message-Id"))
398 (setq references (nconc
399 (std11-field-bodies '("References" "In-Reply-To"))
401 (setq to (delq nil (mapcar 'car to-alist)))
402 (setq cc (delq nil (mapcar 'car cc-alist)))
403 ;; if subscribed mailing list is contained in cc or to
404 ;; and myself is contained in cc,
405 ;; delete myself from cc.
406 (setq cc (wl-draft-delete-myself-from-cc to cc))
407 (when wl-insert-mail-followup-to
408 (setq mail-followup-to
409 (wl-draft-make-mail-followup-to (append to cc)))
410 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
411 (with-temp-buffer ; to keep raw buffer unibyte.
412 (set-buffer-multibyte default-enable-multibyte-characters)
413 (setq newsgroups (wl-parse newsgroups
414 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
415 newsgroups (wl-delete-duplicates newsgroups)
420 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
421 (if decoder (funcall decoder grp) grp))
423 (setq to (wl-delete-duplicates to nil t))
424 (setq cc (wl-delete-duplicates
425 (append (wl-delete-duplicates cc nil t)
426 to (copy-sequence to))
428 (and to (setq to (mapconcat
430 (if wl-draft-reply-use-address-with-full-name
431 (or (cdr (assoc addr to-alist)) addr)
434 (and cc (setq cc (mapconcat
436 (if wl-draft-reply-use-address-with-full-name
437 (or (cdr (assoc addr cc-alist)) addr)
440 (and mail-followup-to
441 (setq mail-followup-to
444 (if wl-draft-reply-use-address-with-full-name
445 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
447 mail-followup-to ",\n\t")))
448 (and (null to) (setq to cc cc nil))
449 (setq references (delq nil references)
450 references (mapconcat 'identity references " ")
451 references (wl-draft-parse-msg-id-list-string references)
452 references (wl-delete-duplicates references)
453 references (if references
454 (mapconcat 'identity references "\n\t")))
455 (and wl-draft-use-frame
456 (get-buffer-window summary-buf)
457 (select-window (get-buffer-window summary-buf)))
458 (wl-draft (list (cons 'To to)
460 (cons 'Newsgroups newsgroups)
461 (cons 'Subject subject)
462 (cons 'In-Reply-To in-reply-to)
463 (cons 'References references)
464 (cons 'Mail-Followup-To mail-followup-to))
465 nil nil nil nil parent-folder number)
466 (setq wl-draft-reply-buffer buf)
467 (setq wl-draft-config-variables
468 (append wl-draft-parent-variables
469 wl-draft-config-variables))
470 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save))
471 (run-hooks 'wl-draft-reply-hook))
473 (defun wl-draft-reply-position (position)
474 (cond ((eq position 'body)
475 (wl-draft-body-goto-top))
476 ((eq position 'bottom)
477 (wl-draft-body-goto-bottom))
479 (goto-char (point-min)))
480 ((and (stringp position)
481 (std11-field-body position))
482 (progn (mail-position-on-field position)
483 (wl-draft-beginning-of-line)))
485 (while (car position)
486 (wl-draft-reply-position (car position))
487 (setq position (cdr position))))))
489 (defun wl-draft-add-references ()
490 (wl-draft-add-in-reply-to "References"))
492 (defun wl-draft-add-in-reply-to (&optional alt-field)
493 (let* ((mes-id (save-excursion
494 (set-buffer mail-reply-buffer)
495 (std11-field-body "message-id")))
496 (field (or alt-field "In-Reply-To"))
497 (ref (std11-field-body field))
498 (ref-list nil) (st nil))
499 (when (and mes-id ref)
500 (while (string-match "<[^>]+>" ref st)
502 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
505 (member mes-id ref-list))
509 (when (mail-position-on-field field)
511 (while (looking-at "^[ \t]")
513 (setq mes-id (concat "\t" mes-id "\n")))
517 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
518 &optional ignored-fields)
521 (narrow-to-region (point)(point))
524 (with-current-buffer mail-reply-buffer
526 (decode-mime-charset-region (point-min) (point-max)
528 (buffer-substring-no-properties
529 (point-min) (point-max)))))
531 (goto-char (point-min))
532 (wl-draft-delete-fields ignored-fields))
533 (goto-char (point-max))
534 (push-mark (point) nil t)
535 (goto-char (point-min)))
537 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
538 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
539 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
540 (run-hooks 'wl-draft-cited-hook)
541 (when (if wl-draft-add-references
542 (wl-draft-add-references)
543 (if wl-draft-add-in-reply-to
544 (wl-draft-add-in-reply-to)))
545 (wl-highlight-headers 'for-draft)) ; highlight when added References:
546 (when wl-highlight-body-too
547 (wl-highlight-body-region beg (point-max)))))
549 (defun wl-message-news-p ()
550 "If exist valid Newsgroups field, return non-nil."
551 (std11-field-body "Newsgroups"))
553 (defun wl-message-field-exists-p (field)
554 "If FIELD exist and FIELD value is not empty, return non-nil."
555 (let ((value (std11-field-body field)))
557 (not (string= value "")))))
559 (defun wl-message-mail-p ()
560 "If exist To, Cc or Bcc field, return non-nil."
561 (or (wl-message-field-exists-p "To")
562 (wl-message-field-exists-p "Resent-to")
563 (wl-message-field-exists-p "Cc")
564 (wl-message-field-exists-p "Bcc")
565 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
566 ;;; This may be needed..
567 ;;; (wl-message-field-exists-p "Fcc")
570 (defun wl-draft-edit-string (string)
571 (let ((cur-buf (current-buffer))
572 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
573 to subject in-reply-to cc references newsgroups mail-followup-to
574 content-type content-transfer-encoding from
579 (setq to (std11-field-body "To"))
582 (decode-mime-charset-string
585 (setq subject (std11-field-body "Subject"))
586 (setq subject (and subject
588 (decode-mime-charset-string
591 (setq from (std11-field-body "From")
594 (decode-mime-charset-string
597 (setq in-reply-to (std11-field-body "In-Reply-To"))
598 (setq cc (std11-field-body "Cc"))
601 (decode-mime-charset-string
604 (setq references (std11-field-body "References"))
605 (setq newsgroups (std11-field-body "Newsgroups"))
606 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
607 (setq content-type (std11-field-body "Content-Type"))
608 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
609 (goto-char (point-min))
610 (or (re-search-forward "\n\n" nil t)
611 (search-forward (concat mail-header-separator "\n") nil t))
616 (if (wl-address-user-mail-address-p from) from))
619 (cons 'Subject subject)
620 (cons 'Newsgroups newsgroups)
621 (cons 'Mail-Followup-To mail-followup-to)
622 (cons 'In-Reply-To in-reply-to)
623 (cons 'References references))
624 content-type content-transfer-encoding
625 (buffer-substring (point) (point-max))
627 (kill-buffer tmp-buf))
628 ;; Set cursor point to the top.
629 (goto-char (point-min))
630 (search-forward (concat mail-header-separator "\n") nil t)
631 (run-hooks 'wl-draft-reedit-hook)
632 (and to (mail-position-on-field "To"))))
634 (defun wl-draft-insert-current-message (dummy)
636 (let (original-buffer
638 mail-citation-hook mail-yank-hooks
639 wl-draft-add-references wl-draft-add-in-reply-to
640 wl-draft-cite-function)
641 (if (and wl-draft-buffer-cur-summary-buffer
642 (with-current-buffer wl-draft-buffer-cur-summary-buffer
643 (and wl-message-buffer
644 (with-current-buffer wl-message-buffer
645 (setq original-buffer (wl-message-get-original-buffer))
646 (not (zerop (with-current-buffer original-buffer
649 (setq mail-reply-buffer original-buffer)
650 (wl-draft-yank-from-mail-reply-buffer
652 wl-ignored-forwarded-headers))
653 (when (string= (mime-make-tag "message" "rfc822")
654 (buffer-substring-no-properties (point-at-bol 0)(point-at-eol 0)))
655 (delete-region (point-at-bol 0) (1+ (point-at-eol 0))))
656 (error "No current message"))))
658 (defun wl-draft-insert-get-message (dummy)
659 (let ((fld (completing-read
661 (if (memq 'read-folder wl-use-folder-petname)
662 (wl-folder-get-entity-with-petname)
663 wl-folder-entity-hashtb)
664 nil nil wl-default-spec
665 'wl-read-folder-history))
666 (number (call-interactively
667 (function (lambda (num)
668 (interactive "nNumber: ")
670 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
671 mail-citation-hook mail-yank-hooks
672 wl-draft-cite-function)
675 (with-current-buffer mail-reply-buffer
677 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
680 (elmo-make-fetch-strategy 'entire)))
681 (wl-draft-yank-from-mail-reply-buffer nil))
682 (kill-buffer mail-reply-buffer))))
685 ;; default body citation func
687 (defun wl-default-draft-cite ()
688 (let ((mail-yank-ignored-headers "[^:]+:")
689 (mail-yank-prefix "> ")
690 date from cite-title)
692 (if (< (mark t) (point))
693 (exchange-point-and-mark))
694 (narrow-to-region (point)(point-max))
695 (setq date (std11-field-body "date")
696 from (std11-field-body "from")))
698 (insert (format "At %s,\n%s wrote:\n"
699 (or date "some time ago")
700 (if wl-default-draft-cite-decorate-author
701 (funcall wl-summary-from-function
704 (mail-indent-citation)))
706 (defvar wl-draft-buffer nil "Draft buffer to yank content.")
707 (defun wl-draft-yank-to-draft-buffer (buffer)
708 "Yank BUFFER content to `wl-draft-buffer'."
709 (set-buffer wl-draft-buffer)
710 (let ((mail-reply-buffer buffer))
711 (wl-draft-yank-from-mail-reply-buffer nil)
712 (kill-buffer buffer)))
714 (defun wl-draft-yank-original (&optional arg)
715 "Yank original message."
718 (let ((draft-buffer (current-buffer))
723 (setq mail-reply-buffer (current-buffer))
724 (with-current-buffer draft-buffer
725 (wl-draft-yank-from-mail-reply-buffer nil))))
726 (wl-draft-yank-current-message-entity)))
728 (defun wl-draft-hide (editing-buffer)
729 "Hide the editing draft buffer if possible."
730 (when (and editing-buffer
731 (buffer-live-p editing-buffer)
732 (get-buffer-window editing-buffer))
733 (select-window (get-buffer-window editing-buffer))
734 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
735 fld-buf sum-win fld-win)
736 (if (and wl-draft-use-frame
737 (> (length (visible-frame-list)) 1))
743 ;; stay folder window if required
744 (when wl-stay-folder-window
745 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
746 (if (setq fld-win (get-buffer-window fld-buf))
747 (select-window fld-win)
748 (if wl-draft-resume-folder-window ;; resume folder window
749 (switch-to-buffer fld-buf)))))
750 (if (buffer-live-p sum-buf)
751 (if (setq sum-win (get-buffer-window sum-buf t))
752 ;; if Summary is on the frame, select it.
753 (select-window sum-win)
754 ;; if summary is not on the frame, switch to it.
755 (if (and wl-stay-folder-window
756 (or wl-draft-resume-folder-window fld-win))
757 (wl-folder-select-buffer sum-buf)
758 (switch-to-buffer sum-buf))))))))
760 (defun wl-draft-delete (editing-buffer)
761 "Kill the editing draft buffer and delete the file corresponds to it."
764 (set-buffer editing-buffer)
765 (when wl-draft-buffer-message-number
766 (elmo-folder-delete-messages (wl-draft-get-folder)
768 wl-draft-buffer-message-number))
769 (wl-draft-config-info-operation wl-draft-buffer-message-number
771 (set-buffer-modified-p nil) ; force kill
772 (kill-buffer editing-buffer))))
774 (defun wl-draft-kill (&optional force-kill)
775 "Kill current draft buffer and quit editing."
778 (when (and (or (eq major-mode 'wl-draft-mode)
779 (eq major-mode 'mail-mode))
781 (yes-or-no-p "Kill Current Draft? ")))
782 (let ((cur-buf (current-buffer)))
783 (run-hooks 'wl-draft-kill-pre-hook)
784 (wl-draft-hide cur-buf)
785 (wl-draft-delete cur-buf)))
788 (defun wl-draft-fcc ()
789 "Add a new Fcc field, with file name completion."
791 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
792 (mail-position-on-field "to"))
795 ;; Imported from message.el.
796 (defun wl-draft-elide-region (b e)
797 "Elide the text in the region.
798 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
802 (insert wl-draft-elide-ellipsis))
804 ;; Imported from message.el.
805 (defun wl-draft-beginning-of-line (&optional n)
806 "Move point to beginning of header value or to beginning of line."
808 (let ((zrs 'zmacs-region-stays))
809 (when (and (interactive-p) (boundp zrs))
811 (if (wl-draft-point-in-header-p)
812 (let* ((here (point))
813 (bol (progn (beginning-of-line n) (point)))
814 (eol (line-end-position))
815 (eoh (and (looking-at "[^ \t]")
816 (re-search-forward ": *" eol t))))
817 (if (and eoh (or (> here eoh) (= here bol)))
820 (beginning-of-line n)))
822 (defun wl-draft-point-in-header-p ()
823 "Return t if point is in the header."
826 (goto-char (point-min))
827 (not (re-search-forward
828 (concat "^" (regexp-quote mail-header-separator) "\n")
831 ;; function for wl-sent-message-via
833 (defmacro wl-draft-sent-message-p (type)
834 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
836 (defmacro wl-draft-set-sent-message (type result &optional server-port)
837 (` (let ((element (assq (, type) wl-sent-message-via)))
839 (unless (eq (nth 1 element) (, result))
840 (setcdr element (list (, result) (, server-port)))
841 (setq wl-sent-message-modified t))
842 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
843 (setq wl-sent-message-modified t)))))
845 (defun wl-draft-sent-message-results ()
846 (let ((results wl-sent-message-via)
847 unplugged-via sent-via)
849 (cond ((eq (nth 1 (car results)) 'unplugged)
850 (push (caar results) unplugged-via))
851 ((eq (nth 1 (car results)) 'sent)
852 (push (caar results) sent-via)))
853 (setq results (cdr results)))
854 (list unplugged-via sent-via)))
856 (defun wl-draft-write-sendlog (status proto server to id)
857 "Write send log file, if `wl-draft-sendlog' is non-nil."
858 (when wl-draft-sendlog
860 (let* ((filename (expand-file-name wl-draft-sendlog-filename
861 elmo-msgdb-directory))
862 (filesize (nth 7 (file-attributes filename)))
863 (server (if server (concat " server=" server) ""))
865 ((memq proto '(fcc queue))
866 (format " folder=\"%s\"" to))
868 (format " ng=<%s>" to))
873 (mapcar '(lambda(x) (format "<%s>" x)) to)
876 (id (if id (concat " id=" id) ""))
877 (time (format-time-string "%Y/%m/%d %T")))
878 (insert (format "%s proto=%s stat=%s%s%s%s\n"
879 time proto status server to id))
880 (if (and wl-draft-sendlog-max-size filesize
881 (> filesize wl-draft-sendlog-max-size))
882 (rename-file filename (concat filename ".old") t))
883 (if (file-writable-p filename)
884 (write-region-as-binary (point-min) (point-max)
886 (message "%s is not writable." filename))))))
888 (defun wl-draft-get-header-delimiter (&optional delete)
889 ;; If DELETE is non-nil, replace the header delimiter with a blank line
891 (goto-char (point-min))
892 (when (re-search-forward
893 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
897 (setq delimline (point-marker)))
900 (defun wl-draft-send-mail-with-qmail ()
901 "Pass the prepared message buffer to qmail-inject.
902 Refer to the documentation for the variable `send-mail-function'
903 to find out how to use this."
904 (if (and wl-draft-qmail-send-plugged
905 (not (elmo-plugged-p)))
906 (wl-draft-set-sent-message 'mail 'unplugged)
908 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
909 (let ((id (std11-field-body "Message-ID"))
910 (to (std11-field-body "To")))
914 'call-process-region 1 (point-max) wl-qmail-inject-program
916 wl-qmail-inject-args))
917 ;; qmail-inject doesn't say anything on it's stdout/stderr,
918 ;; we have to look at the retval instead
920 (wl-draft-set-sent-message 'mail 'sent)
921 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
922 (1 (error "`qmail-inject' reported permanent failure"))
923 (111 (error "`qmail-inject' reported transient failure"))
924 ;; should never happen
925 (t (error "`qmail-inject' reported unknown failure"))))))
927 (defun wl-draft-parse-msg-id-list-string (string)
928 "Get msg-id list from STRING."
930 (dolist (parsed-id (std11-parse-msg-ids-string string))
931 (when (eq (car parsed-id) 'msg-id)
932 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
934 (nreverse msg-id-list)))
936 (defun wl-draft-eword-encode-address-list (string &optional column)
937 "Encode header field STRING as list of address, and return the result.
938 Cause an error when STRING contains invalid address.
939 Optional argument COLUMN is start-position of the field."
940 (car (eword-encode-rword-list
941 (or column eword-encode-default-start-column)
942 (eword-encode-addresses-to-rword-list
943 (wl-draft-std11-parse-addresses (std11-lexical-analyze string))))))
945 (defun wl-draft-std11-parse-addresses (lal)
946 (let ((ret (std11-parse-address lal)))
947 (when (and (not (and (eq (length lal) 1)
948 (eq (car (car lal)) 'spaces)))
950 (error "Error while parsing address"))
952 (let ((dest (list (car ret))))
954 (while (and (setq ret (std11-parse-ascii-token lal))
955 (string-equal (cdr (assq 'specials (car ret))) ",")
956 (setq ret (std11-parse-address (cdr ret)))
958 (setq dest (cons (car ret) dest))
959 (setq lal (cdr ret)))
960 (while (eq 'spaces (car (car lal)))
961 (setq lal (cdr lal)))
962 (if lal (error "Error while parsing address"))
965 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
966 "Get mailbox list of FIELD from current buffer.
967 The buffer is expected to be narrowed to just the headers of the message.
968 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
969 from current buffer."
971 (let ((case-fold-search t)
972 (inhibit-read-only t)
974 mailbox-list beg seq has-group-list)
975 (goto-char (point-min))
976 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
979 (re-search-forward "^[^ \t]" nil 'move)
981 (skip-chars-backward "\n")
982 (setq seq (std11-lexical-analyze
983 (buffer-substring-no-properties beg (point))))
984 (setq addresses (wl-draft-std11-parse-addresses seq))
986 (cond ((eq (car (car addresses)) 'group)
987 (setq has-group-list t)
991 'std11-address-string
992 (nth 2 (car addresses))))))
993 ((eq (car (car addresses)) 'mailbox)
994 (setq address (nth 1 (car addresses)))
998 (std11-addr-to-string
999 (if (eq (car address) 'phrase-route-addr)
1001 (cdr address))))))))
1002 (setq addresses (cdr addresses)))
1003 (when (and remove-group-list has-group-list)
1004 (delete-region beg (point))
1005 (insert (wl-address-string-without-group-list-contents seq))))
1008 (defun wl-draft-deduce-address-list (buffer header-start header-end)
1009 "Get address list suitable for smtp RCPT TO:<address>.
1010 Group list content is removed if `wl-draft-remove-group-list-contents' is
1012 (let ((fields (if (and wl-draft-doing-mime-bcc
1013 wl-draft-disable-bcc-for-mime-bcc)
1015 '("to" "cc" "bcc")))
1016 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
1017 (case-fold-search t)
1021 (narrow-to-region header-start header-end)
1022 (goto-char (point-min))
1024 (if (re-search-forward "^resent-to[\t ]*:" nil t)
1025 (setq fields resent-fields)))
1029 (wl-draft-parse-mailbox-list
1031 wl-draft-remove-group-list-contents)))
1032 (setq fields (cdr fields)))
1038 (defun wl-draft-send-mail-with-smtp ()
1039 "Send the prepared message buffer with SMTP."
1041 (let* ((errbuf (if mail-interactive
1042 (generate-new-buffer " smtp errors")
1044 (case-fold-search t)
1045 (default-case-fold-search t)
1046 (sender (or wl-envelope-from
1047 (wl-address-header-extract-address wl-from)))
1048 (delimline (save-excursion
1049 (goto-char (point-min))
1051 (concat "^" (regexp-quote mail-header-separator)
1055 (or wl-smtp-posting-server smtp-server "localhost"))
1056 (smtp-service (or wl-smtp-posting-port smtp-service))
1057 (smtp-local-domain (or smtp-local-domain wl-local-domain))
1058 (id (std11-field-body "message-id"))
1060 (if (not (elmo-plugged-p smtp-server smtp-service))
1061 (wl-draft-set-sent-message 'mail 'unplugged
1062 (cons smtp-server smtp-service))
1065 ;; Instead of `smtp-deduce-address-list'.
1066 (setq recipients (wl-draft-deduce-address-list
1067 (current-buffer) (point-min) delimline))
1068 (unless recipients (error "No recipients"))
1069 ;; Insert an extra newline if we need it to work around
1070 ;; Sun's bug that swallows newlines.
1071 (goto-char (1+ delimline))
1072 (if (eval mail-mailer-swallows-blank-line)
1074 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1075 (if mail-interactive
1079 (wl-draft-delete-field "bcc" delimline)
1080 (wl-draft-delete-field "resent-bcc" delimline)
1081 (let (process-connection-type)
1084 (wl-smtp-extension-bind
1086 (smtp-send-buffer sender recipients (current-buffer))
1088 (wl-draft-write-sendlog 'failed 'smtp smtp-server
1090 (if (and (eq (car err) 'smtp-response-error)
1091 (= (nth 1 err) 535))
1093 (wl-smtp-password-key
1095 (car smtp-sasl-mechanisms)
1097 (signal (car err) (cdr err)))
1099 (wl-draft-write-sendlog 'uncertain 'smtp smtp-server
1101 (signal (car err) (cdr err)))))
1102 (wl-draft-set-sent-message 'mail 'sent)
1103 (wl-draft-write-sendlog
1104 'ok 'smtp smtp-server recipients id)))))
1105 (if (bufferp errbuf)
1106 (kill-buffer errbuf))))))
1108 (defun wl-draft-send-mail-with-pop-before-smtp ()
1109 "Send the prepared message buffer with POP-before-SMTP."
1110 (require 'elmo-pop3)
1114 :user (or wl-pop-before-smtp-user
1115 elmo-pop3-default-user)
1116 :server (or wl-pop-before-smtp-server
1117 elmo-pop3-default-server)
1118 :port (or wl-pop-before-smtp-port
1119 elmo-pop3-default-port)
1120 :auth (or wl-pop-before-smtp-authenticate-type
1121 elmo-pop3-default-authenticate-type)
1122 :stream-type (elmo-get-network-stream-type
1123 (or wl-pop-before-smtp-stream-type
1124 elmo-pop3-default-stream-type))))
1126 (condition-case error
1128 (setq session (elmo-pop3-get-session folder))
1129 (when session (elmo-network-close-session session)))
1131 (unless (string= (nth 1 error) "Unplugged")
1132 (signal (car error) (cdr error))))))
1133 (wl-draft-send-mail-with-smtp))
1135 (defun wl-draft-insert-required-fields (&optional force-msgid)
1136 "Insert Message-ID, Date, and From field.
1137 If FORCE-MSGID, insert message-id regardless of `wl-insert-message-id'."
1138 ;; Insert Message-Id field...
1139 (goto-char (point-min))
1140 (when (and (or force-msgid
1141 wl-insert-message-id)
1142 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1143 (insert (concat "Message-ID: "
1144 (funcall wl-message-id-function)
1146 ;; Insert date field.
1147 (goto-char (point-min))
1148 (or (re-search-forward "^Date[ \t]*:" nil t)
1149 (wl-draft-insert-date-field))
1150 ;; Insert from field.
1151 (goto-char (point-min))
1152 (or (re-search-forward "^From[ \t]*:" nil t)
1153 (wl-draft-insert-from-field)))
1155 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1156 "Send the message in the current buffer."
1158 (narrow-to-region (goto-char (point-min))
1159 (if (re-search-forward
1161 "^" (regexp-quote mail-header-separator) "$")
1165 (wl-draft-insert-required-fields)
1166 ;; ignore any blank lines in the header
1167 (while (progn (goto-char (point-min))
1168 (re-search-forward "\n[ \t]*\n\n*" nil t))
1169 (replace-match "\n"))
1170 (goto-char (point-min))
1171 (while (re-search-forward
1172 "^[^ \t\n:]+:[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n"
1174 (when (string= "" (match-string 1))
1175 (replace-match ""))))
1176 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1177 (wl-draft-dispatch-message)
1178 (when kill-when-done
1179 ;; hide editing-buffer.
1180 (wl-draft-hide editing-buffer)
1181 ;; delete editing-buffer and its file.
1182 (wl-draft-delete editing-buffer)))
1184 (defun wl-draft-dispatch-message (&optional mes-string)
1185 "Send the message in the current buffer. Not modified the header fields."
1186 (let (delimline mime-bcc)
1187 (if (and wl-draft-verbose-send mes-string)
1188 (message "%s" mes-string))
1190 (setq delimline (wl-draft-get-header-delimiter t))
1191 (unless wl-draft-fcc-list
1192 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1194 (setq wl-sent-message-modified nil)
1197 (if (and (wl-message-mail-p)
1198 (not (wl-draft-sent-message-p 'mail)))
1199 (if (or (not (or wl-draft-force-queuing
1200 wl-draft-force-queuing-mail))
1201 (memq 'mail wl-sent-message-queued))
1203 (setq mime-bcc (wl-draft-mime-bcc-field))
1204 (funcall wl-draft-send-mail-function)
1205 (when (not (zerop (length mime-bcc)))
1206 (wl-draft-do-mime-bcc mime-bcc)))
1207 (push 'mail wl-sent-message-queued)
1208 (wl-draft-set-sent-message 'mail 'unplugged)))
1209 (if (and (wl-message-news-p)
1210 (not (wl-draft-sent-message-p 'news))
1211 (not (wl-message-field-exists-p "Resent-to")))
1212 (if (or (not (or wl-draft-force-queuing
1213 wl-draft-force-queuing-news))
1214 (memq 'news wl-sent-message-queued))
1215 (funcall wl-draft-send-news-function)
1216 (push 'news wl-sent-message-queued)
1217 (wl-draft-set-sent-message 'news 'unplugged))))
1218 (let* ((status (wl-draft-sent-message-results))
1219 (unplugged-via (car status))
1220 (sent-via (nth 1 status)))
1221 ;; If one sent, process fcc folder.
1222 (if (and sent-via wl-draft-fcc-list)
1224 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1226 (setq wl-draft-fcc-list nil)))
1227 (if wl-draft-use-cache
1228 (let ((id (std11-field-body "Message-ID"))
1229 (elmo-enable-disconnected-operation t))
1230 (elmo-file-cache-save (elmo-file-cache-get-path id)
1232 ;; If one unplugged, append queue.
1233 (when (and unplugged-via
1234 wl-sent-message-modified)
1235 (if wl-draft-enable-queuing
1237 (wl-draft-queue-append wl-sent-message-via)
1238 (setq wl-sent-message-modified 'requeue))
1239 (error "Unplugged")))
1240 (when wl-draft-verbose-send
1241 (if (and unplugged-via sent-via);; combined message
1243 (setq wl-draft-verbose-msg
1244 (format "Sending%s and Queuing%s..."
1245 sent-via unplugged-via))
1246 (message "%sdone" wl-draft-verbose-msg))
1250 (if sent-via "done" "failed"))))))))
1251 (not wl-sent-message-modified)) ;; return value
1253 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1254 "Force send current buffer as raw message."
1257 (let (wl-interactive-send
1258 ;;; wl-draft-verbose-send
1259 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1260 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1263 (wl-draft-send kill-when-done mes-string))))
1265 (defun wl-draft-clone-local-variables ()
1266 (let ((locals (buffer-local-variables))
1269 (when (and (consp (car locals))
1271 (string-match wl-draft-clone-local-variable-regexp
1272 (symbol-name (car (car locals)))))
1273 (wl-append result (list (car (car locals)))))
1274 (setq locals (cdr locals)))
1277 (defcustom wl-draft-send-confirm-with-preview t
1278 "*Non-nil to invoke preview through confirmation of sending.
1279 This variable is valid when `wl-interactive-send' has non-nil value."
1283 (defun wl-draft-send-confirm ()
1288 (when wl-draft-send-confirm-with-preview
1289 (wl-draft-preview-message))
1291 (goto-char (point-min)) ; to show recipients in header
1295 (message "Send current draft? <y/n/j(down)/k(up)> ")
1296 (case (let ((cursor-in-echo-area t)) (read-char))
1300 (ignore-errors (scroll-up)))
1302 (ignore-errors (scroll-down)))
1304 (throw 'done nil)))))))
1306 (when (and wl-draft-send-confirm-with-preview
1307 (eq major-mode 'mime-view-mode))
1308 (wl-mime-quit-preview)))))
1310 (defun wl-draft-send (&optional kill-when-done mes-string)
1311 "Send current draft message.
1312 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1314 ;; Don't call this explicitly.
1315 ;; Added to 'wl-draft-send-hook (by teranisi)
1316 ;; (wl-draft-config-exec)
1317 (run-hooks 'wl-draft-send-hook)
1318 (when (or (not wl-interactive-send)
1319 (wl-draft-send-confirm))
1320 (let ((send-mail-function 'wl-draft-raw-send)
1321 (editing-buffer (current-buffer))
1322 (sending-buffer (wl-draft-generate-clone-buffer
1323 " *wl-draft-sending-buffer*"
1324 (append wl-draft-config-variables
1325 (wl-draft-clone-local-variables))))
1326 (parent-flag wl-draft-parent-flag)
1327 (parent-number wl-draft-parent-number)
1328 (parent-folder wl-draft-parent-folder)
1329 (wl-draft-verbose-msg nil)
1333 (set-buffer sending-buffer)
1334 (if (and (not (wl-message-mail-p))
1335 (not (wl-message-news-p)))
1336 (error "No recipient is specified"))
1337 (expand-abbrev) ; for mail-abbrevs
1338 (let ((mime-header-encode-method-alist
1340 '((wl-draft-eword-encode-address-list
1341 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
1342 (if (boundp 'mime-header-encode-method-alist)
1343 (symbol-value 'mime-header-encode-method-alist)))))
1344 (run-hooks 'mail-send-hook) ; translate buffer
1347 (if wl-draft-verbose-send
1348 (message "%s" (or mes-string "Sending...")))
1349 ;; Set flag before send-function because
1350 ;; there's no need to change current mailbox at this time.
1351 ;; If flag is set after send-function, the current mailbox
1352 ;; might changed by Fcc.
1353 ;; It causes a huge loss in the IMAP folder.
1354 (when (and parent-flag parent-number
1355 (not (eq (length parent-folder) 0)))
1356 (wl-folder-set-persistent-mark
1357 parent-folder parent-number parent-flag))
1358 (funcall wl-draft-send-function editing-buffer kill-when-done)
1359 ;; Now perform actions on successful sending.
1360 (while mail-send-actions
1362 (apply (car (car mail-send-actions))
1363 (cdr (car mail-send-actions)))
1365 (setq mail-send-actions (cdr mail-send-actions)))
1366 (if wl-draft-verbose-send
1368 (or wl-draft-verbose-msg
1371 ;; kill sending buffer, anyway.
1372 (and (buffer-live-p sending-buffer)
1373 (kill-buffer sending-buffer))))))
1375 (defun wl-draft-mime-bcc-field ()
1376 "Return the MIME-Bcc field body. The field is deleted."
1377 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1378 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1380 (defun wl-draft-do-mime-bcc (field-body)
1381 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1382 (let ((orig-from (mime-decode-field-body (std11-field-body "from")
1384 (orig-subj (mime-decode-field-body (or (std11-field-body "subject")
1387 (recipients (wl-parse-addresses field-body))
1388 (draft-buffer (current-buffer))
1390 (save-window-excursion
1391 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1392 (not (zerop (length field-body))))
1393 (let ((wl-draft-doing-mime-bcc t))
1394 (dolist (recipient recipients)
1395 (wl-draft-create-buffer)
1396 (wl-draft-create-contents
1397 (append `((From . ,orig-from)
1399 (Subject . ,(concat "A blind carbon copy ("
1402 (wl-draft-default-headers)))
1403 (wl-draft-insert-mail-header-separator)
1404 (wl-draft-prepare-edit)
1405 (goto-char (point-max))
1406 (insert (or wl-draft-mime-bcc-body
1407 "This is a blind carbon copy.")
1409 (mime-edit-insert-tag "message" "rfc822")
1410 (insert-buffer draft-buffer)
1411 (let (wl-interactive-send)
1412 (wl-draft-send 'kill-when-done))))))))
1414 (defun wl-draft-save ()
1415 "Save current draft."
1417 (if (buffer-modified-p)
1419 (message "Saving...")
1420 (let ((msg (buffer-substring-no-properties (point-min) (point-max)))
1422 (when wl-draft-buffer-message-number
1423 (elmo-folder-delete-messages (wl-draft-get-folder)
1424 (list wl-draft-buffer-message-number))
1425 (wl-draft-config-info-operation wl-draft-buffer-message-number
1427 (elmo-folder-check (wl-draft-get-folder))
1428 ;; If no header separator, insert it.
1431 (goto-char (point-min))
1432 (unless (re-search-forward
1433 (concat "^" (regexp-quote mail-header-separator) "$")
1435 (goto-char (point-min))
1436 (if (re-search-forward "\n\n" nil t)
1437 (replace-match (concat "\n" mail-header-separator "\n"))
1438 (goto-char (point-max))
1439 (insert (if (eq (char-before) ?\n) "" "\n")
1440 mail-header-separator "\n")))
1441 (let ((mime-header-encode-method-alist
1443 '((eword-encode-unstructured-field-body
1444 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
1445 (if (boundp 'mime-header-encode-method-alist)
1446 (symbol-value 'mime-header-encode-method-alist)))))
1447 (mime-edit-translate-buffer))
1448 (wl-draft-get-header-delimiter t)
1450 (elmo-folder-next-message-number (wl-draft-get-folder)))
1451 (elmo-folder-append-buffer (wl-draft-get-folder)))
1452 (elmo-folder-check (wl-draft-get-folder))
1453 (elmo-folder-commit (wl-draft-get-folder))
1454 (setq wl-draft-buffer-message-number next-number)
1455 (rename-buffer (format "%s/%d" wl-draft-folder next-number))
1456 (setq buffer-file-name (buffer-name))
1457 (set-buffer-modified-p nil)
1458 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save)
1459 (message "Saving...done")))
1460 (message "(No changes need to be saved)")))
1462 (defun wl-draft-mimic-kill-buffer ()
1463 "Kill the current (draft) buffer with query."
1465 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1468 (if (or (not bufname)
1469 (string-equal bufname "")
1470 (string-equal bufname (buffer-name)))
1471 (let ((bufname (current-buffer)))
1472 (when (or (not (buffer-modified-p))
1474 (format "Buffer %s modified; kill anyway? " bufname)))
1475 (set-buffer-modified-p nil)
1476 (wl-draft-hide bufname)
1477 (kill-buffer bufname)))
1478 (kill-buffer bufname))))
1480 (defun wl-draft-save-and-exit ()
1481 "Save current draft and exit current draft mode."
1484 (let ((editing-buffer (current-buffer)))
1485 (wl-draft-hide editing-buffer)
1486 (kill-buffer editing-buffer)))
1488 (defun wl-draft-send-and-exit ()
1489 "Send current draft message and kill it."
1493 (defun wl-draft-send-from-toolbar ()
1495 (let ((wl-interactive-send t))
1496 (wl-draft-send-and-exit)))
1498 (defun wl-draft-delete-field (field &optional delimline replace)
1499 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1501 (defun wl-draft-delete-fields (field &optional delimline replace)
1504 (goto-char (point-min))
1505 (if (search-forward "\n\n" nil t)
1506 (setq delimline (point))
1507 (setq delimline (point-max))))
1508 (narrow-to-region (point-min) delimline)
1509 (goto-char (point-min))
1510 (let ((regexp (concat "^" field ":"))
1511 (case-fold-search t))
1513 (if (looking-at regexp)
1519 (if (re-search-forward "^[^ \t]" nil t)
1520 (goto-char (match-beginning 0))
1523 (insert (concat field ": " replace "\n"))))
1525 (if (re-search-forward "^[^ \t]" nil t)
1526 (goto-char (match-beginning 0))
1529 (defun wl-draft-get-fcc-list (header-end)
1530 (if (and wl-draft-doing-mime-bcc
1531 wl-draft-disable-fcc-for-mime-bcc)
1533 (wl-draft-delete-field "fcc")
1536 (case-fold-search t))
1537 (or (markerp header-end) (error "HEADER-END must be a marker"))
1539 (goto-char (point-min))
1540 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1545 (buffer-substring-no-properties
1549 (skip-chars-backward " \t")
1552 (dolist (folder fcc-list)
1553 (wl-folder-confirm-existence
1554 (wl-folder-get-elmo-folder (eword-decode-string folder)))))
1555 (delete-region (match-beginning 0)
1556 (progn (forward-line 1) (point)))))
1559 (defcustom wl-draft-fcc-append-read-folder-history t
1560 "Non-nil to append fcc'ed folder to `wl-read-folder-history'."
1564 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1565 (let ((send-mail-buffer (current-buffer))
1566 (tembuf (generate-new-buffer " fcc output"))
1567 (case-fold-search t)
1569 (or (markerp header-end) (error "HEADER-END must be a marker"))
1572 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1575 ;; insert just the headers to avoid moving the gap more than
1576 ;; necessary (the message body could be arbitrarily huge.)
1577 (insert-buffer-substring send-mail-buffer 1 header-end)
1578 (wl-draft-insert-required-fields t)
1579 (goto-char (point-max))
1580 (insert-buffer-substring send-mail-buffer header-end)
1581 (let ((id (std11-field-body "Message-ID"))
1582 (elmo-enable-disconnected-operation t))
1584 (if (elmo-folder-append-buffer
1585 (wl-folder-get-elmo-folder
1586 (eword-decode-string (car fcc-list)))
1587 (and wl-fcc-force-as-read '(read)))
1588 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1589 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1590 (if (and wl-draft-fcc-append-read-folder-history
1591 (boundp 'wl-read-folder-history))
1592 (or (equal (car fcc-list) (car wl-read-folder-history))
1593 (setq wl-read-folder-history
1594 (append (list (car fcc-list)) wl-read-folder-history))))
1595 (setq fcc-list (cdr fcc-list)))))
1596 (kill-buffer tembuf)))
1598 (defun wl-draft-on-field-p ()
1601 (goto-char (point-min))
1602 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1609 (if (or (looking-at ".*,[ \t]?$")
1610 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1612 (let ((pos (point)))
1615 (if (looking-at "^[ \t]")
1617 (if (re-search-forward ":" pos t) nil t)))))))
1621 (defun wl-draft (&optional header-alist
1622 content-type content-transfer-encoding
1626 "Write and send mail/news message with Wanderlust."
1633 (wl-plugged-init t))
1635 (wl-init)) ; returns immediately if already initialized.
1637 (wl-start-save-drafts)
1638 (let (buffer header-alist-internal)
1639 (setq buffer (wl-draft-create-buffer parent-folder parent-number))
1640 (unless (cdr (assq 'From header-alist))
1642 (append (list (cons 'From wl-from)) header-alist)))
1643 (unless (cdr (assq 'To header-alist))
1648 (if (assq 'To header-alist)
1649 (setcdr (assq 'To header-alist) to)
1651 (append header-alist
1652 (list (cons 'To to))))))))
1653 (unless (cdr (assq 'Subject header-alist))
1654 (if (assq 'Subject header-alist)
1655 (setcdr (assq 'Subject header-alist) "")
1657 (append header-alist (list (cons 'Subject ""))))))
1658 (setq header-alist (append header-alist
1659 (wl-draft-default-headers)
1660 wl-draft-additional-header-alist
1661 (if body (list "" (cons 'Body body)))))
1662 (wl-draft-create-contents header-alist)
1664 (wl-draft-decode-body
1665 content-type content-transfer-encoding))
1666 (wl-draft-insert-mail-header-separator)
1667 (wl-draft-prepare-edit)
1669 (run-hooks 'wl-mail-setup-hook))
1670 (goto-char (point-min))
1671 (setq buffer-undo-list nil)
1672 (wl-user-agent-compose-internal) ;; user-agent
1675 (string= (cdr (assq 'To header-alist)) ""))
1676 (mail-position-on-field "To"))
1678 (goto-char (point-max))))
1681 (defun wl-draft-create-buffer (&optional parent-folder parent-number)
1682 (let* ((draft-folder (wl-draft-get-folder))
1683 (parent-folder (or parent-folder (wl-summary-buffer-folder-name)))
1684 (summary-buf (wl-summary-get-buffer parent-folder))
1686 (or (eq this-command 'wl-summary-reply)
1687 (eq this-command 'wl-summary-reply-with-citation)
1688 (eq this-command 'wl-summary-forward)
1689 (eq this-command 'wl-summary-target-mark-forward)
1690 (eq this-command 'wl-summary-target-mark-reply-with-citation)))
1691 (buffer (generate-new-buffer "*draft*"))) ; Just for initial name.
1693 ;; switch-buffer according to draft buffer style.
1694 (if wl-draft-use-frame
1695 (switch-to-buffer-other-frame buffer)
1696 (if reply-or-forward
1697 (case wl-draft-reply-buffer-style
1699 (split-window-vertically)
1701 (switch-to-buffer buffer))
1703 (switch-to-buffer buffer))
1705 (delete-other-windows)
1706 (switch-to-buffer buffer))
1708 (if (functionp wl-draft-reply-buffer-style)
1709 (funcall wl-draft-reply-buffer-style buffer)
1710 (error "Invalid value for wl-draft-reply-buffer-style"))))
1711 (case wl-draft-buffer-style
1713 (when (eq major-mode 'wl-summary-mode)
1714 (wl-summary-toggle-disp-msg 'off))
1715 (split-window-vertically)
1717 (switch-to-buffer buffer))
1719 (switch-to-buffer buffer))
1721 (delete-other-windows)
1722 (switch-to-buffer buffer))
1723 (t (if (functionp wl-draft-buffer-style)
1724 (funcall wl-draft-buffer-style buffer)
1725 (error "Invalid value for wl-draft-buffer-style"))))))
1727 (let (change-major-mode-hook)
1729 (set-buffer-multibyte t) ; draft buffer is always multibyte.
1730 (make-local-variable 'truncate-partial-width-windows)
1731 (setq truncate-partial-width-windows nil)
1732 (setq truncate-lines wl-draft-truncate-lines)
1733 (setq wl-sent-message-via nil)
1734 (setq wl-sent-message-queued nil)
1735 (setq wl-draft-config-exec-flag t)
1736 (setq wl-draft-parent-folder (or parent-folder ""))
1737 (setq wl-draft-parent-number parent-number)
1738 (or (eq this-command 'wl-folder-write-current-folder)
1739 (setq wl-draft-buffer-cur-summary-buffer summary-buf))
1742 (defun wl-draft-create-contents (header-alist)
1743 "header-alist' sample
1744 '(function ;; funcall
1745 string ;; insert string
1746 (symbol . string) ;; insert symbol-value: string
1747 (symbol . function) ;; (funcall) and if it returns string,
1748 ;; insert symbol-value: string
1749 (symbol . nil) ;; do nothing
1752 (unless (eq major-mode 'wl-draft-mode)
1753 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1754 (let ((halist header-alist)
1759 ((functionp (car halist)) (funcall (car halist)))
1761 ((stringp (car halist)) (insert (car halist) "\n"))
1763 ((consp (car halist))
1764 (setq field (car (car halist)))
1765 (setq value (cdr (car halist)))
1769 ((eq field 'Body) ; body
1771 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1773 (let ((value-return (funcall value)))
1774 (when (stringp value-return)
1775 (insert (symbol-name field) ": " value-return "\n"))))
1784 (setq halist (cdr halist)))))
1786 (defun wl-draft-prepare-edit ()
1787 (unless (eq major-mode 'wl-draft-mode)
1788 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1789 (let (change-major-mode-hook)
1790 (wl-draft-editor-mode)
1791 (static-when (boundp 'auto-save-file-name-transforms)
1792 (make-local-variable 'auto-save-file-name-transforms)
1793 (setq auto-save-file-name-transforms
1794 (cons (list (concat (regexp-quote wl-draft-folder)
1796 (concat (expand-file-name
1798 (elmo-folder-msgdb-path
1799 (wl-draft-get-folder)))
1801 auto-save-file-name-transforms)))
1802 (when wl-draft-write-file-function
1803 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
1804 (wl-draft-overload-functions)
1805 (wl-highlight-headers 'for-draft)
1807 (clear-visited-file-modtime)))
1809 (defun wl-draft-decode-header ()
1811 (std11-narrow-to-header)
1812 (wl-draft-decode-message-in-buffer)
1815 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1818 (std11-field-body "content-type")))
1819 (content-transfer-encoding
1820 (or content-transfer-encoding
1821 (std11-field-body "content-transfer-encoding")))
1824 (std11-narrow-to-header)
1825 (wl-draft-delete-field "content-type")
1826 (wl-draft-delete-field "content-transfer-encoding")
1827 (goto-char (point-max))
1828 (setq delimline (point-marker))
1830 (narrow-to-region delimline (point-max))
1831 (goto-char (point-min))
1833 (insert "Content-type: " content-type "\n"))
1834 (when content-transfer-encoding
1835 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1836 (wl-draft-decode-message-in-buffer)
1837 (goto-char (point-min))
1838 (unless (re-search-forward "^$" (point-at-eol) t)
1843 ;;; subroutine for wl-draft-create-contents
1844 ;;; must be used in wl-draft-mode
1845 (defun wl-draft-check-new-line ()
1846 (if (not (= (preceding-char) ?\n))
1849 (defsubst wl-draft-trim-ccs (cc)
1855 (null (and wl-draft-delete-myself-from-bcc-fcc
1857 (mapcar 'wl-address-header-extract-address
1859 (wl-parse-addresses (std11-field-body "To"))
1860 (wl-parse-addresses (std11-field-body "Cc"))))
1861 (mapcar 'downcase wl-subscribed-mailing-list)))))
1865 (defsubst wl-draft-default-headers ()
1867 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1868 (wl-address-header-extract-address
1870 (cons 'User-Agent wl-generate-mailer-string-function)
1871 (cons 'Reply-To mail-default-reply-to)
1872 (cons 'Bcc (function
1875 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1876 (cons 'Fcc (function
1878 (wl-draft-trim-ccs wl-fcc))))
1879 (cons 'Organization wl-organization)
1880 (and wl-auto-insert-x-face
1881 (file-exists-p wl-x-face-file)
1882 'wl-draft-insert-x-face-field-here) ;; allow nil
1883 mail-default-headers
1884 ;; check \n at th end of line for `mail-default-headers'
1885 'wl-draft-check-new-line
1888 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1891 (goto-char delimline)
1892 (goto-char (point-min))
1893 (if (search-forward "\n\n" nil t)
1894 (delete-backward-char 1)
1895 (goto-char (point-max))))
1896 (wl-draft-check-new-line)
1897 (put-text-property (point)
1899 (insert mail-header-separator "\n")
1901 'category 'mail-header-separator)
1906 (defun wl-draft-elmo-nntp-send ()
1907 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1908 (elmo-nntp-default-user
1909 (or wl-nntp-posting-user elmo-nntp-default-user))
1910 (elmo-nntp-default-server
1911 (or wl-nntp-posting-server elmo-nntp-default-server))
1912 (elmo-nntp-default-port
1913 (or wl-nntp-posting-port elmo-nntp-default-port))
1914 (elmo-nntp-default-stream-type
1915 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1916 (elmo-nntp-default-function wl-nntp-posting-function)
1918 (if (setq condition (cdr (elmo-string-matched-assoc
1919 (std11-field-body "Newsgroups")
1920 wl-nntp-posting-config-alist)))
1921 (if (stringp condition)
1922 (setq elmo-nntp-default-server condition)
1923 (while (car condition)
1924 (set (intern (format "elmo-nntp-default-%s"
1925 (symbol-name (caar condition))))
1927 (setq condition (cdr condition)))))
1928 (unless elmo-nntp-default-function
1929 (error "wl-draft-nntp-send: posting-function is nil"))
1930 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1931 (wl-draft-set-sent-message 'news 'unplugged
1932 (cons elmo-nntp-default-server
1933 elmo-nntp-default-port))
1934 (funcall elmo-nntp-default-function
1935 elmo-nntp-default-server (current-buffer))
1936 (wl-draft-set-sent-message 'news 'sent)
1937 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1938 (std11-field-body "Newsgroups")
1939 (std11-field-body "Message-ID")))))
1941 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1942 "Generate clone of current buffer named NAME."
1943 (let ((editing-buffer (current-buffer)))
1945 (set-buffer (generate-new-buffer name))
1948 (wl-draft-editor-mode)
1949 (insert-buffer editing-buffer)
1951 (while local-variables
1952 (make-local-variable (car local-variables))
1953 (set (car local-variables)
1955 (set-buffer editing-buffer)
1956 (symbol-value (car local-variables))))
1957 (setq local-variables (cdr local-variables)))
1960 (defun wl-draft-remove-text-plain-tag ()
1961 "Remove text/plain tag of mime-edit."
1962 (when (string= (mime-make-text-tag "plain")
1963 (buffer-substring-no-properties (point-at-bol)(point-at-eol)))
1964 (delete-region (point-at-bol)(1+ (point-at-eol)))))
1966 (defun wl-draft-reedit (number)
1967 (let ((draft-folder (wl-draft-get-folder))
1970 buffer change-major-mode-hook body-top)
1971 (setq buffer (get-buffer-create (format "%s/%d" wl-draft-folder
1973 (if wl-draft-use-frame
1974 (switch-to-buffer-other-frame buffer)
1975 (switch-to-buffer buffer))
1977 (elmo-message-fetch draft-folder number (elmo-make-fetch-strategy 'entire))
1978 (elmo-delete-cr-buffer)
1979 (let ((mime-edit-again-ignored-field-regexp
1980 "^\\(Content-.*\\|Mime-Version\\):"))
1981 (wl-draft-decode-message-in-buffer))
1982 (setq body-top (wl-draft-insert-mail-header-separator))
1985 (make-local-variable 'truncate-partial-width-windows)
1986 (setq truncate-partial-width-windows nil)
1987 (setq truncate-lines wl-draft-truncate-lines)
1988 (setq wl-sent-message-via nil)
1989 (setq wl-sent-message-queued nil)
1990 (wl-draft-config-info-operation number 'load)
1991 (goto-char (point-min))
1992 (wl-draft-overload-functions)
1993 (wl-draft-editor-mode)
1994 (static-when (boundp 'auto-save-file-name-transforms)
1995 (make-local-variable 'auto-save-file-name-transforms)
1996 (setq auto-save-file-name-transforms
1997 (cons (list (concat (regexp-quote wl-draft-folder)
1999 (concat (expand-file-name
2001 (elmo-folder-msgdb-path
2002 (wl-draft-get-folder)))
2004 auto-save-file-name-transforms)))
2005 (setq buffer-file-name (buffer-name)
2006 wl-draft-parent-folder ""
2007 wl-draft-buffer-message-number number)
2008 (when wl-draft-write-file-function
2009 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
2010 (wl-highlight-headers 'for-draft)
2011 (goto-char body-top)
2012 (run-hooks 'wl-draft-reedit-hook)
2013 (goto-char (point-max))
2016 (defmacro wl-draft-body-goto-top ()
2018 (goto-char (point-min))
2019 (if (re-search-forward mail-header-separator nil t)
2021 (goto-char (point-max))))))
2023 (defmacro wl-draft-body-goto-bottom ()
2024 (` (goto-char (point-max))))
2026 (defmacro wl-draft-config-body-goto-header ()
2028 (goto-char (point-min))
2029 (if (re-search-forward mail-header-separator nil t)
2031 (goto-char (point-max))))))
2033 (defsubst wl-draft-config-sub-eval-insert (content &optional newline)
2034 (let (content-value)
2036 (stringp (setq content-value (eval content))))
2037 (insert content-value)
2038 (if newline (insert "\n")))))
2040 (defun wl-draft-config-sub-body (content)
2041 (wl-draft-body-goto-top)
2042 (delete-region (point) (point-max))
2043 (wl-draft-config-sub-eval-insert content))
2045 (defun wl-draft-config-sub-top (content)
2046 (wl-draft-body-goto-top)
2047 (wl-draft-config-sub-eval-insert content))
2049 (defun wl-draft-config-sub-bottom (content)
2050 (wl-draft-body-goto-bottom)
2051 (wl-draft-config-sub-eval-insert content))
2053 (defun wl-draft-config-sub-header (content)
2054 (wl-draft-config-body-goto-header)
2055 (wl-draft-config-sub-eval-insert content 'newline))
2057 (defun wl-draft-config-sub-header-top (content)
2058 (goto-char (point-min))
2059 (wl-draft-config-sub-eval-insert content 'newline))
2061 (defun wl-draft-config-sub-part-top (content)
2062 (goto-char (mime-edit-content-beginning))
2063 (wl-draft-config-sub-eval-insert content 'newline))
2065 (defun wl-draft-config-sub-part-bottom (content)
2066 (goto-char (mime-edit-content-end))
2067 (wl-draft-config-sub-eval-insert content 'newline))
2069 (defsubst wl-draft-config-sub-file (content)
2070 (let ((coding-system-for-read wl-cs-autoconv)
2071 (file (expand-file-name (eval content))))
2072 (if (file-exists-p file)
2073 (insert-file-contents file)
2074 (error "%s: no exists file" file))))
2076 (defun wl-draft-config-sub-body-file (content)
2077 (wl-draft-body-goto-top)
2078 (delete-region (point) (point-max))
2079 (wl-draft-config-sub-file content))
2081 (defun wl-draft-config-sub-top-file (content)
2082 (wl-draft-body-goto-top)
2083 (wl-draft-config-sub-file content))
2085 (defun wl-draft-config-sub-bottom-file (content)
2086 (wl-draft-body-goto-bottom)
2087 (wl-draft-config-sub-file content))
2089 (defun wl-draft-config-sub-header-file (content)
2090 (wl-draft-config-body-goto-header)
2091 (wl-draft-config-sub-file content))
2093 (defun wl-draft-config-sub-template (content)
2094 (setq wl-draft-config-variables
2095 (wl-template-insert (eval content))))
2097 (defun wl-draft-config-sub-x-face (content)
2098 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
2099 (fboundp 'x-face-insert)) ; x-face.el is installed.
2100 (x-face-insert content)
2101 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
2103 (defsubst wl-draft-config-sub-func (field content)
2105 (if (setq func (assq field wl-draft-config-sub-func-alist))
2106 (let (wl-draft-config-variables)
2107 (funcall (cdr func) content)
2108 ;; for wl-draft-config-sub-template
2109 (cons t wl-draft-config-variables)))))
2111 (defsubst wl-draft-config-exec-sub (clist)
2112 (let (config local-variables)
2114 (setq config (car clist))
2119 (let ((field (car config))
2120 (content (cdr config))
2124 (wl-draft-replace-field field (eval content) t))
2125 ((setq ret-val (wl-draft-config-sub-func field content))
2126 (if (cdr ret-val) ;; for wl-draft-config-sub-template
2127 (wl-append local-variables (cdr ret-val))))
2128 ((boundp field) ;; variable
2129 (make-local-variable field)
2130 (set field (eval content))
2131 (wl-append local-variables (list field)))
2133 (error "%s: not variable" field)))))
2135 (error "%s: not supported type" config)))
2136 (setq clist (cdr clist)))
2139 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
2140 "Change headers in draft preparation time."
2142 (unless wl-draft-reedit
2145 (and (boundp 'wl-draft-prepared-config-alist)
2146 wl-draft-prepared-config-alist) ;; For compatible.
2147 wl-draft-config-alist)))
2149 (wl-draft-config-exec config-alist reply-buf)))))
2151 (defun wl-draft-config-exec (&optional config-alist reply-buf)
2152 "Change headers according to the value of `wl-draft-config-alist'.
2153 Automatically applied in draft sending time."
2155 (let ((case-fold-search t)
2156 (alist (or config-alist wl-draft-config-alist))
2157 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2158 wl-draft-reply-buffer)))
2159 (local-variables wl-draft-config-variables)
2161 (when (and (or (interactive-p)
2162 wl-draft-config-exec-flag)
2167 (setq key (caar alist)
2174 (set-buffer reply-buf)
2176 (std11-narrow-to-header)
2177 (goto-char (point-min))
2178 (re-search-forward (car clist) nil t))))
2179 (wl-draft-config-exec-sub (cdr clist))
2182 (when (save-restriction
2183 (std11-narrow-to-header mail-header-separator)
2184 (goto-char (point-min))
2185 (re-search-forward key nil t))
2186 (wl-append local-variables
2187 (wl-draft-config-exec-sub clist))
2190 (wl-append local-variables
2191 (wl-draft-config-exec-sub clist))
2193 (if (and found wl-draft-config-matchone)
2195 (setq alist (cdr alist)))))
2197 (setq wl-draft-config-exec-flag nil))
2198 (run-hooks 'wl-draft-config-exec-hook)
2199 (put-text-property (point-min)(point-max) 'face nil)
2200 (wl-highlight-message (point-min)(point-max) t)
2201 (setq wl-draft-config-variables
2202 (elmo-uniq-list local-variables)))))
2204 (defun wl-draft-replace-field (field content &optional add)
2207 (let ((case-fold-search t)
2208 (inhibit-read-only t) ;; added by teranisi.
2210 (std11-narrow-to-header mail-header-separator)
2211 (goto-char (point-min))
2212 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2217 (re-search-forward "^[^ \t]" nil 'move)
2219 (skip-chars-backward "\n")
2220 (delete-region beg (point))
2221 (insert " " content))
2226 (re-search-forward "^[^ \t]" nil 'move)
2228 (delete-region beg (point)))
2229 (when (and add content)
2231 (goto-char (point-max))
2232 (insert (concat field ": " content "\n"))))))))
2234 (defsubst wl-draft-config-info-filename (number msgdb-dir)
2236 (format "%s-%d" wl-draft-config-save-filename number)
2239 (defun wl-draft-config-info-operation (msg operation)
2240 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-draft-get-folder)))
2241 (filename (wl-draft-config-info-filename msg msgdb-dir))
2242 element alist variable)
2244 ((eq operation 'save)
2245 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2246 (while (setq variable (pop variables))
2247 (when (boundp variable)
2249 (list (cons variable (eval variable))))))
2250 (elmo-object-save filename alist)))
2251 ((eq operation 'load)
2252 (setq alist (elmo-object-load filename))
2253 (while (setq element (pop alist))
2254 (set (make-local-variable (car element)) (cdr element))
2255 (wl-append wl-draft-config-variables (list (car element)))))
2256 ((eq operation 'delete)
2257 (if (file-exists-p filename)
2258 (delete-file filename))))))
2260 (defun wl-draft-queue-info-operation (msg operation
2261 &optional add-sent-message-via)
2262 (let* ((msgdb-dir (elmo-folder-msgdb-path
2263 (wl-folder-get-elmo-folder wl-queue-folder)))
2266 (format "%s-%d" wl-draft-queue-save-filename msg)
2268 element alist variable)
2270 ((eq operation 'save)
2271 (let ((variables (elmo-uniq-list
2272 (append wl-draft-queue-save-variables
2273 wl-draft-config-variables
2274 (list 'wl-draft-fcc-list)))))
2275 (if add-sent-message-via
2277 (push 'wl-sent-message-queued variables)
2278 (push 'wl-sent-message-via variables)))
2279 (while (setq variable (pop variables))
2280 (when (boundp variable)
2282 (list (cons variable (eval variable))))))
2283 (elmo-object-save filename alist)))
2284 ((eq operation 'load)
2285 (setq alist (elmo-object-load filename))
2286 (while (setq element (pop alist))
2287 (set (make-local-variable (car element)) (cdr element))))
2288 ((eq operation 'get-sent-via)
2289 (setq alist (elmo-object-load filename))
2290 (cdr (assq 'wl-sent-message-via alist)))
2291 ((eq operation 'delete)
2292 (if (file-exists-p filename)
2293 (delete-file filename))))))
2295 (defun wl-draft-queue-append (wl-sent-message-via)
2296 (if wl-draft-verbose-send
2297 (message "Queuing..."))
2298 (let ((send-buffer (current-buffer))
2299 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2300 (message-id (std11-field-body "Message-ID")))
2301 (if (elmo-folder-append-buffer folder)
2303 (wl-draft-queue-info-operation
2304 (car (elmo-folder-status folder))
2305 'save wl-sent-message-via)
2306 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2307 (when wl-draft-verbose-send
2308 (setq wl-draft-verbose-msg "Queuing...")
2309 (message "Queuing...done")))
2310 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2311 (error "Queuing failed"))))
2313 (defun wl-draft-queue-flush ()
2314 "Flush draft queue."
2316 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2318 (elmo-folder-open-internal queue-folder)
2319 (elmo-folder-list-messages queue-folder)))
2322 (wl-draft-queue-flushing t)
2323 msgs failure len buffer msgid sent-via)
2324 ;; get plugged send message
2326 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2329 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2330 (or (not (nth 2 (car sent-via)))
2332 (car (nth 2 (car sent-via)))
2333 (cdr (nth 2 (car sent-via))))))
2334 (wl-append msgs (list (car msgs2)))
2336 (setq sent-via (cdr sent-via))))
2337 (setq msgs2 (cdr msgs2)))
2338 (when (> (setq len (length msgs)) 0)
2339 (if (elmo-y-or-n-p (format
2340 "%d message(s) are in the sending queue. Send now? "
2342 (not elmo-dop-flush-confirm) t)
2345 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2348 ;; reset buffer local variables
2349 (kill-all-local-variables)
2353 (setq wl-sent-message-via nil)
2354 (wl-draft-queue-info-operation (car msgs) 'load)
2355 (elmo-message-fetch queue-folder
2357 (elmo-make-fetch-strategy 'entire))
2359 (setq failure (funcall
2360 wl-draft-queue-flush-send-function
2361 (format "Sending (%d/%d)..." i len)))
2362 ;;; (wl-draft-raw-send nil nil
2363 ;;; (format "Sending (%d/%d)..." i len))
2365 (elmo-display-error err t)
2369 (if (eq wl-sent-message-modified 'requeue)
2371 (elmo-folder-delete-messages
2372 queue-folder (cons (car msgs) nil))
2373 (wl-draft-queue-info-operation (car msgs) 'delete))
2375 (elmo-folder-delete-messages
2376 queue-folder (cons (car msgs) nil))
2377 (wl-draft-queue-info-operation (car msgs) 'delete)
2378 (setq performed (+ 1 performed))))
2379 (setq msgs (cdr msgs)))
2380 (kill-buffer buffer)
2381 (message "%d message(s) are sent." performed)))
2382 (message "%d message(s) are remained to be sent." len))
2383 (elmo-folder-close queue-folder)
2386 (defun wl-jump-to-draft-buffer (&optional arg)
2387 "Jump to the draft if exists."
2390 (wl-jump-to-draft-folder)
2391 (let ((draft-bufs (wl-collect-draft))
2395 (message "No draft buffer exist."))
2398 (sort (mapcar 'buffer-name draft-bufs)
2399 (function (lambda (a b)
2400 (not (string< a b))))))
2401 (if (setq buf (cdr (member (buffer-name)
2403 (setq buf (car buf))
2404 (setq buf (car draft-bufs)))
2405 (switch-to-buffer buf))))))
2407 (defun wl-jump-to-draft-folder ()
2408 (let ((msgs (reverse (elmo-folder-list-messages (wl-draft-get-folder))))
2409 (mybuf (buffer-name))
2412 (message "No draft message exist.")
2413 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2414 (setq msg (cadr (memq
2415 (string-to-int (substring mybuf (match-end 0)))
2417 (or msg (setq msg (car msgs)))
2418 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2419 (switch-to-buffer buf)
2420 (wl-draft-reedit msg)))))
2422 (defun wl-draft-highlight-and-recenter (&optional n)
2424 (when wl-highlight-body-too
2425 (let ((modified (buffer-modified-p)))
2428 (put-text-property (point-min) (point-max) 'face nil)
2429 (wl-highlight-message (point-min) (point-max) t))
2430 (set-buffer-modified-p modified))))
2431 (static-when (featurep 'xemacs)
2432 ;; Cope with one of many XEmacs bugs that `recenter' takes
2433 ;; a long time if there are a lot of invisible text lines.
2437 ;; insert element from history
2438 (defvar wl-draft-current-history-position nil)
2439 (defvar wl-draft-history-backup-word "")
2441 (defun wl-draft-previous-history-element (n)
2443 (let (bol history beg end prev new)
2444 (when (and (not (wl-draft-on-field-p))
2447 (goto-char (point-min))
2448 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
2452 (while (and (looking-at "^[ \t]")
2453 (not (= (point) (point-min))))
2456 ((looking-at wl-folder-complete-header-regexp)
2457 (and (boundp 'wl-read-folder-history)
2458 (setq history wl-read-folder-history)))
2459 ;; ((looking-at wl-address-complete-header-regexp)
2460 ;; (setq history .....))
2464 (setq bol (save-excursion (beginning-of-line) (point)))
2465 (cond ((and (or (eq last-command 'wl-draft-previous-history-element)
2466 (eq last-command 'wl-draft-next-history-element))
2467 wl-draft-current-history-position)
2469 (or (search-backward-regexp ",[ \t]*\\(.*\\)" bol t)
2470 (search-backward-regexp "^[ \t]\\(.*\\)" bol t)
2471 (search-backward-regexp "^[^ \t]*: \\(.*\\)" bol t))
2472 (setq prev (match-string 1))
2473 (goto-char (match-beginning 1))
2476 (>= (+ n wl-draft-current-history-position) 0))
2478 (<= (+ n wl-draft-current-history-position)
2481 (setq wl-draft-current-history-position
2482 (+ n wl-draft-current-history-position))
2484 (nth wl-draft-current-history-position
2485 (append (list wl-draft-history-backup-word)
2487 (delete-region beg end)
2491 (message "End of history; no next item"))
2493 (message "Beginning of history; no preceding item")))))
2496 (or (search-backward-regexp ",[ \t]*\\(.*\\)" bol t)
2497 (search-backward-regexp "^[ \t]\\(.*\\)" bol t)
2498 (search-backward-regexp "^[^ \t]*: \\(.*\\)" bol t)))
2500 (setq wl-draft-current-history-position 1)
2501 (setq wl-draft-history-backup-word (match-string 1))
2502 (delete-region (match-beginning 1) (match-end 1))
2503 (insert (car history)))
2505 (setq wl-draft-current-history-position nil))))))
2507 (defun wl-draft-next-history-element (n)
2509 (wl-draft-previous-history-element (- n)))
2511 ;;;; user-agent support by Sen Nagata
2513 ;; this appears to be necessarily global...
2514 (defvar wl-user-agent-compose-p nil)
2515 (defvar wl-user-agent-headers-and-body-alist nil)
2517 ;; this should be a generic function for mail-mode -- i wish there was
2518 ;; something like it in sendmail.el
2519 (defun wl-user-agent-insert-header (header-name header-value)
2520 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2521 ;; it seems like overriding existing headers is acceptable -- should
2522 ;; we provide an option?
2524 ;; plan was: unfold header (might be folded), remove existing value, insert
2526 ;; wl doesn't seem to fold header lines yet anyway :-)
2528 (let ((kill-whole-line t)
2530 (mail-position-on-field (capitalize header-name))
2531 (setq end-of-line (point))
2533 (re-search-forward ":" end-of-line)
2534 (insert (concat " " header-value "\n"))
2537 ;; this should be a generic function for mail-mode -- i wish there was
2538 ;; something like it in sendmail.el
2540 ;; ** haven't dealt w/ case where the body is already set **
2541 (defun wl-user-agent-insert-body (body-text)
2542 "Insert a body of text, BODY-TEXT, into a message."
2543 ;; code defensively... :-P
2544 (goto-char (point-min))
2545 (search-forward mail-header-separator)
2550 (defun wl-user-agent-compose (&optional to subject other-headers continue
2551 switch-function yank-action
2553 "Support the `compose-mail' interface for wl.
2554 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2555 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2556 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2558 (unless (featurep 'wl)
2561 (setq switch-function 'keep))
2562 ;; protect these -- to and subject get bound at some point, so it looks
2563 ;; to be necessary to protect the values used w/in
2564 (let ((wl-user-agent-headers-and-body-alist other-headers)
2565 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2566 (wl-draft-buffer-style switch-function))
2568 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2571 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2574 (setq wl-user-agent-headers-and-body-alist
2575 (cons (cons "to" to)
2576 wl-user-agent-headers-and-body-alist))))
2578 (if (wl-string-match-assoc "subject"
2579 wl-user-agent-headers-and-body-alist
2582 (wl-string-match-assoc "subject"
2583 wl-user-agent-headers-and-body-alist
2586 (setq wl-user-agent-headers-and-body-alist
2587 (cons (cons "subject" subject)
2588 wl-user-agent-headers-and-body-alist))))
2589 ;; i think this is what we want to use...
2592 ;; tell the hook-function to do its stuff
2593 (setq wl-user-agent-compose-p t)
2594 ;; because to get the hooks working, wl-draft has to think it has
2595 ;; been called interactively
2596 (call-interactively 'wl-draft))
2597 (setq wl-user-agent-compose-p nil))))
2599 (defun wl-user-agent-compose-internal ()
2600 "Manipulate headers and/or a body of a draft message."
2601 ;; being called from wl-user-agent-compose?
2602 (if wl-user-agent-compose-p
2605 (let ((headers wl-user-agent-headers-and-body-alist)
2606 (case-fold-search t))
2609 (if (not (string-match "^body$" (car (car headers))))
2610 (wl-user-agent-insert-header
2611 (car (car headers)) (cdr (car headers)))
2613 (setq headers (cdr headers))))
2614 ;; highlight headers (from wl-draft in wl-draft.el)
2615 (wl-highlight-headers 'for-draft)
2617 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2619 (wl-user-agent-insert-body
2620 (cdr (wl-string-match-assoc
2622 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2625 (defun wl-draft-setup-parent-flag (flag)
2626 "Setup a FLAG for parent message."
2627 (when (and (> (length wl-draft-parent-folder) 0)
2628 wl-draft-parent-number)
2629 (setq wl-draft-parent-flag flag)
2630 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save)))
2632 (defun wl-draft-buffer-change-number (old-number new-number)
2633 (when (eq wl-draft-buffer-message-number old-number)
2634 (setq wl-draft-buffer-message-number new-number)
2635 (rename-buffer (format "%s/%d" wl-draft-folder new-number))
2636 (setq buffer-file-name (buffer-name))
2637 (set-buffer-modified-p nil)))
2639 (defun wl-draft-rename-saved-config (old-number new-number)
2640 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-draft-get-folder)))
2641 (old-name (wl-draft-config-info-filename old-number msgdb-dir))
2642 (new-name (wl-draft-config-info-filename new-number msgdb-dir)))
2643 (when (file-exists-p old-name)
2644 (rename-file old-name new-name 'ok-if-already-exists))))
2647 (product-provide (provide 'wl-draft) (require 'wl-version))
2649 ;;; wl-draft.el ends here