1 ;;; wl-draft.el --- Message draft mode for Wanderlust.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright (C) 1998,1999,2000 Masahiro MURATA <muse@ba2.so-net.ne.jp>
6 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
7 ;; Masahiro MURATA <muse@ba2.so-net.ne.jp>
8 ;; Keywords: mail, net news
10 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
35 (require 'wl-template)
37 (condition-case nil (require 'timezone) (error nil))
41 (defvar x-face-add-x-face-version-header)
42 (defvar mail-reply-buffer)
43 (defvar mail-from-style)
47 (defalias-maybe 'x-face-insert 'ignore)
48 (defalias-maybe 'x-face-insert-version-header 'ignore)
49 (defalias-maybe 'wl-init 'ignore)
50 (defalias-maybe 'wl-draft-mode 'ignore))
53 (autoload 'wl-addrmgr "wl-addrmgr"))
55 (defvar wl-draft-buf-name "Draft")
56 (defvar wl-draft-cite-function 'wl-default-draft-cite)
57 (defvar wl-draft-buffer-file-name nil)
58 (defvar wl-draft-field-completion-list nil)
59 (defvar wl-draft-verbose-send t)
60 (defvar wl-draft-verbose-msg nil)
61 (defvar wl-draft-queue-flushing nil)
62 (defvar wl-draft-config-variables nil)
63 (defvar wl-draft-config-exec-flag t)
64 (defvar wl-draft-buffer-cur-summary-buffer nil)
65 (defvar wl-draft-clone-local-variable-regexp "^\\(wl\\|mime\\)")
66 (defvar wl-draft-sendlog-filename "sendlog")
67 (defvar wl-draft-queue-save-filename "qinfo")
68 (defvar wl-draft-config-save-filename "config")
69 (defvar wl-draft-queue-flush-send-function 'wl-draft-dispatch-message)
70 (defvar wl-sent-message-via nil)
71 (defvar wl-sent-message-modified nil)
72 (defvar wl-draft-fcc-list nil)
73 (defvar wl-draft-reedit nil)
74 (defvar wl-draft-reply-buffer nil)
75 (defvar wl-draft-forward nil)
76 (defvar wl-draft-parent-folder nil)
78 (defvar wl-draft-config-sub-func-alist
79 '((body . wl-draft-config-sub-body)
80 (top . wl-draft-config-sub-top)
81 (bottom . wl-draft-config-sub-bottom)
82 (header . wl-draft-config-sub-header)
83 (header-top . wl-draft-config-sub-header-top)
84 (header-bottom . wl-draft-config-sub-header)
85 (part-top . wl-draft-config-sub-part-top)
86 (part-bottom . wl-draft-config-sub-part-bottom)
87 (body-file . wl-draft-config-sub-body-file)
88 (top-file . wl-draft-config-sub-top-file)
89 (bottom-file . wl-draft-config-sub-bottom-file)
90 (header-file . wl-draft-config-sub-header-file)
91 (template . wl-draft-config-sub-template)
92 (x-face . wl-draft-config-sub-x-face)))
94 (make-variable-buffer-local 'wl-draft-buffer-file-name)
95 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
96 (make-variable-buffer-local 'wl-draft-config-variables)
97 (make-variable-buffer-local 'wl-draft-config-exec-flag)
98 (make-variable-buffer-local 'wl-sent-message-via)
99 (make-variable-buffer-local 'wl-draft-fcc-list)
100 (make-variable-buffer-local 'wl-draft-reply-buffer)
101 (make-variable-buffer-local 'wl-draft-parent-folder)
103 (defmacro wl-smtp-extension-bind (&rest body)
104 (` (let* ((smtp-sasl-mechanisms
105 (if wl-smtp-authenticate-type
107 (if (listp wl-smtp-authenticate-type)
108 wl-smtp-authenticate-type
109 (list wl-smtp-authenticate-type)))))
110 (smtp-use-sasl (and smtp-sasl-mechanisms t))
111 (smtp-use-starttls (eq wl-smtp-connection-type 'starttls))
112 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
113 (if (and (string= (car smtp-sasl-mechanisms) "DIGEST-MD5")
115 (string-match "^\\([^@]*\\)@\\([^@]*\\)"
116 wl-smtp-posting-user))
117 (setq smtp-sasl-user-name (match-string 1 wl-smtp-posting-user)
118 smtp-sasl-properties (list 'realm
119 (match-string 2 wl-smtp-posting-user)))
120 (setq smtp-sasl-user-name wl-smtp-posting-user
121 smtp-sasl-properties nil))
122 (setq sasl-read-passphrase
131 (defun wl-draft-insert-date-field ()
133 (insert "Date: " (wl-make-date-string) "\n"))
135 (defun wl-draft-insert-from-field ()
137 ;; Put the "From:" field in unless for some odd reason
138 ;; they put one in themselves.
139 (let* ((login (or user-mail-address (user-login-name)))
140 (fullname (user-full-name)))
141 (cond ((eq mail-from-style 'angles)
142 (insert "From: " fullname)
143 (let ((fullname-start (+ (point-min) (length "From: ")))
144 (fullname-end (point-marker)))
145 (goto-char fullname-start)
146 ;; Look for a character that cannot appear unquoted
147 ;; according to RFC 822.
148 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
151 ;; Quote fullname, escaping specials.
152 (goto-char fullname-start)
154 (while (re-search-forward "[\"\\]"
156 (replace-match "\\\\\\&" t))
158 (insert " <" login ">\n"))
159 ((eq mail-from-style 'parens)
160 (insert "From: " login " (")
161 (let ((fullname-start (point)))
163 (let ((fullname-end (point-marker)))
164 (goto-char fullname-start)
165 ;; RFC 822 says \ and nonmatching parentheses
166 ;; must be escaped in comments.
167 ;; Escape every instance of ()\ ...
168 (while (re-search-forward "[()\\]" fullname-end 1)
169 (replace-match "\\\\\\&" t))
170 ;; ... then undo escaping of matching parentheses,
171 ;; including matching nested parentheses.
172 (goto-char fullname-start)
173 (while (re-search-forward
174 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
176 (replace-match "\\1(\\3)" t)
177 (goto-char fullname-start))))
179 ((not mail-from-style)
180 (insert "From: " login "\n")))))
182 (defun wl-draft-insert-x-face-field ()
183 "Insert X-Face header."
185 (if (not (file-exists-p wl-x-face-file))
186 (error "File %s does not exist" wl-x-face-file)
187 (beginning-of-buffer)
188 (search-forward mail-header-separator nil t)
190 (wl-draft-insert-x-face-field-here)
191 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
193 (defun wl-draft-insert-x-face-field-here ()
194 "Insert X-Face field at point."
195 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
196 (when (string-match "^[ \t]*" x-face-string)
197 (setq x-face-string (substring x-face-string (match-end 0))))
198 (insert "X-Face: " x-face-string))
199 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
201 ;; Insert X-Face-Version: field
202 (when (and (fboundp 'x-face-insert-version-header)
203 (boundp 'x-face-add-x-face-version-header)
204 x-face-add-x-face-version-header)
205 (x-face-insert-version-header)))
207 (defun wl-draft-setup ()
208 (let ((field wl-draft-fields)
211 (setq ret-val (append ret-val
212 (list (cons (concat (car field) " ")
213 (concat (car field) " ")))))
214 (setq field (cdr field)))
215 (setq wl-draft-field-completion-list ret-val)))
217 (defun wl-draft-make-mail-followup-to (recipients)
218 (if (elmo-list-member
219 (or wl-user-mail-address-list
220 (list (wl-address-header-extract-address wl-from)))
222 (let ((rlist (elmo-list-delete
223 (or wl-user-mail-address-list
224 (list (wl-address-header-extract-address wl-from)))
225 (copy-sequence recipients))))
226 (if (elmo-list-member rlist (mapcar 'downcase
227 wl-subscribed-mailing-list))
229 (append rlist (list (wl-address-header-extract-address
233 (defun wl-draft-delete-myself-from-cc (to cc)
234 (let ((myself (or wl-user-mail-address-list
235 (list (wl-address-header-extract-address wl-from)))))
236 (cond (wl-draft-always-delete-myself ; always-delete option
237 (elmo-list-delete myself cc))
238 ((elmo-list-member (append to cc) ; subscribed mailing-list
239 (mapcar 'downcase wl-subscribed-mailing-list))
240 (elmo-list-delete myself cc))
243 (defun wl-draft-forward (original-subject summary-buf)
245 (with-current-buffer (wl-message-get-original-buffer)
246 (setq references (nconc
247 (std11-field-bodies '("References" "In-Reply-To"))
248 (list (std11-field-body "Message-Id"))))
249 (setq references (delq nil references)
250 references (mapconcat 'identity references " ")
251 references (wl-draft-parse-msg-id-list-string references)
252 references (wl-delete-duplicates references)
253 references (when references
254 (mapconcat 'identity references "\n\t"))))
255 (wl-draft "" (concat "Forward: " original-subject)
256 nil nil references nil nil nil nil nil nil summary-buf))
257 (goto-char (point-max))
258 (wl-draft-insert-message)
259 (mail-position-on-field "To"))
261 (defun wl-draft-strip-subject-re (subject)
262 "Remove \"Re:\" from subject lines. Shamelessly copied from Gnus."
263 (if (string-match wl-subject-prefix-regexp subject)
264 (substring subject (match-end 0))
267 (defun wl-draft-reply-list-symbol (with-arg)
268 "Return symbol `wl-draft-reply-*-argument-list' match condition.
269 Check WITH-ARG and From: field."
270 (if (wl-address-user-mail-address-p (or (elmo-field-body "From") ""))
272 'wl-draft-reply-myself-with-argument-list
273 'wl-draft-reply-myself-without-argument-list)
275 'wl-draft-reply-with-argument-list
276 'wl-draft-reply-without-argument-list)))
278 (defun wl-draft-reply (buf with-arg summary-buf)
279 "Reply to BUF buffer message.
280 Reply to author if WITH-ARG is non-nil."
283 to mail-followup-to cc subject in-reply-to references newsgroups
284 from to-alist cc-alist decoder parent-folder)
285 (set-buffer summary-buf)
286 (setq parent-folder (wl-summary-buffer-folder-name))
288 (setq r-list (symbol-value (wl-draft-reply-list-symbol with-arg)))
291 (when (let ((condition (car (car r-list))))
292 (cond ((stringp condition)
293 (std11-field-body condition))
297 (if (not (std11-field-body (car condition)))
299 (setq condition (cdr condition)))
302 (funcall condition))))
303 (let ((r-to-list (nth 0 (cdr (car r-list))))
304 (r-cc-list (nth 1 (cdr (car r-list))))
305 (r-ng-list (nth 2 (cdr (car r-list)))))
306 (when (and (member "Followup-To" r-ng-list)
307 (string= (std11-field-body "Followup-To") "poster"))
308 (setq r-to-list (cons "From" r-to-list))
309 (setq r-ng-list (delete "Followup-To" (copy-sequence r-ng-list))))
310 (setq to (wl-concat-list (cons to
311 (elmo-multiple-fields-body-list
314 (setq cc (wl-concat-list (cons cc
315 (elmo-multiple-fields-body-list
318 (setq newsgroups (wl-concat-list (cons newsgroups
323 (setq r-list (cdr r-list)))
324 (error "No match field: check your `%s'"
325 (symbol-name (wl-draft-reply-list-symbol with-arg))))
326 (setq subject (std11-field-body "Subject"))
327 (setq to (wl-parse-addresses to)
328 cc (wl-parse-addresses cc))
329 (with-temp-buffer ; to keep raw buffer unibyte.
330 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
331 (setq decoder (mime-find-field-decoder 'Subject 'plain))
332 (setq subject (if (and subject decoder)
333 (funcall decoder subject) subject))
337 (setq decoder (mime-find-field-decoder 'To 'plain))
338 (cons (nth 1 (std11-extract-address-components addr))
339 (if decoder (funcall decoder addr) addr)))
344 (setq decoder (mime-find-field-decoder 'Cc 'plain))
345 (cons (nth 1 (std11-extract-address-components addr))
346 (if decoder (funcall decoder addr) addr)))
348 (and wl-reply-subject-prefix
349 (setq subject (concat wl-reply-subject-prefix
350 (wl-draft-strip-subject-re
352 (setq in-reply-to (std11-field-body "Message-Id"))
353 (setq references (nconc
354 (std11-field-bodies '("References" "In-Reply-To"))
356 (setq to (delq nil (mapcar 'car to-alist)))
357 (setq cc (delq nil (mapcar 'car cc-alist)))
358 ;; if subscribed mailing list is contained in cc or to
359 ;; and myself is contained in cc,
360 ;; delete myself from cc.
361 (setq cc (wl-draft-delete-myself-from-cc to cc))
362 (when wl-insert-mail-followup-to
363 (setq mail-followup-to
364 (wl-draft-make-mail-followup-to (append to cc)))
365 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
366 (with-temp-buffer ; to keep raw buffer unibyte.
367 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
368 (setq newsgroups (wl-parse newsgroups
369 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
370 newsgroups (wl-delete-duplicates newsgroups)
375 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
376 (if decoder (funcall decoder grp) grp))
378 (setq to (wl-delete-duplicates to nil t))
379 (setq cc (wl-delete-duplicates
380 (append (wl-delete-duplicates cc nil t)
381 to (copy-sequence to))
383 (and to (setq to (mapconcat
385 (if wl-draft-reply-use-address-with-full-name
386 (or (cdr (assoc addr to-alist)) addr)
389 (and cc (setq cc (mapconcat
391 (if wl-draft-reply-use-address-with-full-name
392 (or (cdr (assoc addr cc-alist)) addr)
395 (and mail-followup-to
396 (setq mail-followup-to
399 (if wl-draft-reply-use-address-with-full-name
400 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
402 mail-followup-to ",\n\t")))
403 (and (null to) (setq to cc cc nil))
404 (setq references (delq nil references)
405 references (mapconcat 'identity references " ")
406 references (wl-draft-parse-msg-id-list-string references)
407 references (wl-delete-duplicates references)
408 references (if references
409 (mapconcat 'identity references "\n\t")))
411 to subject in-reply-to cc references newsgroups mail-followup-to
412 nil nil nil nil summary-buf nil parent-folder)
413 (setq wl-draft-reply-buffer buf))
414 (run-hooks 'wl-reply-hook))
416 (defun wl-draft-add-references ()
417 (wl-draft-add-in-reply-to "References"))
419 (defun wl-draft-add-in-reply-to (&optional alt-field)
420 (let* ((mes-id (save-excursion
421 (set-buffer mail-reply-buffer)
422 (std11-field-body "message-id")))
423 (field (or alt-field "In-Reply-To"))
424 (ref (std11-field-body field))
425 (ref-list nil) (st nil))
426 (when (and mes-id ref)
427 (while (string-match "<[^>]+>" ref st)
429 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
432 (member mes-id ref-list))
436 (when (mail-position-on-field field)
438 (while (looking-at "^[ \t]")
440 (setq mes-id (concat "\t" mes-id "\n")))
444 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
445 &optional ignored-fields)
448 (narrow-to-region (point)(point))
450 (with-current-buffer mail-reply-buffer
452 (decode-mime-charset-region (point-min) (point-max)
454 (buffer-substring-no-properties
455 (point-min) (point-max))))
457 (goto-char (point-min))
458 (wl-draft-delete-fields ignored-fields))
459 (goto-char (point-max))
460 (push-mark (point) nil t)
461 (goto-char (point-min)))
463 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
464 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
465 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
466 (run-hooks 'wl-draft-cited-hook)
467 (when (if wl-draft-add-references
468 (wl-draft-add-references)
469 (if wl-draft-add-in-reply-to
470 (wl-draft-add-in-reply-to)))
471 (wl-highlight-headers 'for-draft)) ; highlight when added References:
472 (when wl-highlight-body-too
473 (wl-highlight-body-region beg (point-max)))))
475 (defun wl-draft-confirm ()
476 "Confirm send message."
478 (y-or-n-p (format "Send current draft as %s? "
479 (cond ((and (wl-message-mail-p) (wl-message-news-p))
481 ((wl-message-mail-p) "Mail")
482 ((wl-message-news-p) "News")))))
484 (defun wl-message-news-p ()
485 "If exist valid Newsgroups field, return non-nil."
486 (std11-field-body "Newsgroups"))
488 (defun wl-message-field-exists-p (field)
489 "If FIELD exist and FIELD value is not empty, return non-nil."
490 (let ((value (std11-field-body field)))
492 (not (string= value "")))))
494 (defun wl-message-mail-p ()
495 "If exist To, Cc or Bcc field, return non-nil."
496 (or (wl-message-field-exists-p "To")
497 (wl-message-field-exists-p "Resent-to")
498 (wl-message-field-exists-p "Cc")
499 (wl-message-field-exists-p "Bcc")
500 ;;; This may be needed..
501 ;;; (wl-message-field-exists-p "Fcc")
504 (defun wl-draft-open-file (&optional file)
505 "Open FILE for edit."
507 ;;;(interactive "*fFile to edit: ")
508 (wl-draft-edit-string (elmo-get-file-string
510 (read-file-name "File to edit: "
511 (or wl-temporary-file-directory
514 (defun wl-draft-edit-string (string)
515 (let ((cur-buf (current-buffer))
516 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
517 to subject in-reply-to cc references newsgroups mail-followup-to
518 content-type content-transfer-encoding from
519 body-beg buffer-read-only)
523 (setq to (std11-field-body "To"))
526 (decode-mime-charset-string
529 (setq subject (std11-field-body "Subject"))
530 (setq subject (and subject
532 (decode-mime-charset-string
535 (setq from (std11-field-body "From")
538 (decode-mime-charset-string
541 (setq in-reply-to (std11-field-body "In-Reply-To"))
542 (setq cc (std11-field-body "Cc"))
545 (decode-mime-charset-string
548 (setq references (std11-field-body "References"))
549 (setq newsgroups (std11-field-body "Newsgroups"))
550 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
551 (setq content-type (std11-field-body "Content-Type"))
552 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
553 (goto-char (point-min))
554 (or (re-search-forward "\n\n" nil t)
555 (search-forward (concat mail-header-separator "\n") nil t))
558 (wl-draft to subject in-reply-to cc references newsgroups
560 content-type content-transfer-encoding
561 (buffer-substring (point) (point-max))
563 (if (member (nth 1 (std11-extract-address-components from))
564 wl-user-mail-address-list)
566 (and to (mail-position-on-field "To"))
567 (delete-other-windows)
568 (kill-buffer tmp-buf)))
569 (setq buffer-read-only nil) ;;??
570 (run-hooks 'wl-draft-reedit-hook))
572 (defun wl-draft-insert-current-message (dummy)
574 (let (original-buffer
576 mail-citation-hook mail-yank-hooks
577 wl-draft-add-references wl-draft-add-in-reply-to
578 wl-draft-cite-function)
579 (with-current-buffer wl-draft-buffer-cur-summary-buffer
580 (with-current-buffer wl-message-buffer
581 (setq original-buffer (wl-message-get-original-buffer))
583 (with-current-buffer original-buffer
585 (error "No current message"))))
586 (setq mail-reply-buffer original-buffer)
587 (wl-draft-yank-from-mail-reply-buffer
589 wl-ignored-forwarded-headers)))
591 (defun wl-draft-insert-get-message (dummy)
592 (let ((fld (completing-read
594 (if (memq 'read-folder wl-use-folder-petname)
595 (wl-folder-get-entity-with-petname)
596 wl-folder-entity-hashtb)
597 nil nil wl-default-spec
598 'wl-read-folder-hist))
599 (number (call-interactively
600 (function (lambda (num)
601 (interactive "nNumber: ")
603 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
604 mail-citation-hook mail-yank-hooks
605 wl-draft-cite-function)
608 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
611 (elmo-make-fetch-strategy 'entire)
612 nil mail-reply-buffer)
613 (wl-draft-yank-from-mail-reply-buffer nil))
614 (kill-buffer mail-reply-buffer))))
617 ;; default body citation func
619 (defun wl-default-draft-cite ()
620 (let ((mail-yank-ignored-headers "[^:]+:")
621 (mail-yank-prefix "> ")
622 (summary-buf wl-current-summary-buffer)
623 (message-buf (get-buffer (wl-current-message-buffer)))
624 from date cite-title num entity)
626 (buffer-live-p summary-buf)
628 (buffer-live-p message-buf))
630 (with-current-buffer summary-buf
631 (setq num (save-excursion
632 (set-buffer message-buf)
633 wl-message-buffer-cur-number))
634 (setq entity (elmo-msgdb-overview-get-entity
635 num (wl-summary-buffer-msgdb)))
636 (setq date (elmo-msgdb-overview-entity-get-date entity))
637 (setq from (elmo-msgdb-overview-entity-get-from entity)))
638 (setq cite-title (format "At %s,\n%s wrote:"
639 (or date "some time ago")
640 (wl-summary-from-func-internal
643 (insert cite-title "\n"))
644 (mail-indent-citation)))
646 (defvar wl-draft-buffer nil "Draft buffer to yank content")
647 (defun wl-draft-yank-to-draft-buffer (buffer)
648 "Yank BUFFER content to `wl-draft-buffer'."
649 (set-buffer wl-draft-buffer)
650 (let ((mail-reply-buffer buffer))
651 (wl-draft-yank-from-mail-reply-buffer nil)
652 (kill-buffer buffer)))
654 (defun wl-draft-yank-original (&optional arg)
655 "Yank original message."
658 (let (buf mail-reply-buffer)
661 (setq buf (current-buffer)))
662 (setq mail-reply-buffer buf)
663 (wl-draft-yank-from-mail-reply-buffer nil))
664 (wl-draft-yank-current-message-entity)))
666 (defun wl-draft-hide (editing-buffer)
667 "Hide the editing draft buffer if possible."
668 (when (and editing-buffer
669 (buffer-live-p editing-buffer))
670 (set-buffer editing-buffer)
671 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
672 fld-buf sum-win fld-win)
673 (if (and wl-draft-use-frame
674 (> (length (visible-frame-list)) 1))
680 ;; stay folder window if required
681 (when wl-stay-folder-window
682 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
683 (if (setq fld-win (get-buffer-window fld-buf))
684 (select-window fld-win)
685 (if wl-draft-resume-folder-window ;; resume folder window
686 (switch-to-buffer fld-buf)))))
687 (if (buffer-live-p sum-buf)
688 (if (setq sum-win (get-buffer-window sum-buf t))
689 ;; if Summary is on the frame, select it.
690 (select-window sum-win)
691 ;; if summary is not on the frame, switch to it.
692 (if (and wl-stay-folder-window
693 (or wl-draft-resume-folder-window fld-win))
694 (wl-folder-select-buffer sum-buf)
695 (switch-to-buffer sum-buf)))))))
697 (defun wl-draft-delete (editing-buffer)
698 "kill the editing draft buffer and delete the file corresponds to it."
701 (set-buffer editing-buffer)
702 (if wl-draft-buffer-file-name
704 (if (file-exists-p wl-draft-buffer-file-name)
705 (delete-file wl-draft-buffer-file-name))
706 (let ((msg (and wl-draft-buffer-file-name
707 (string-match "[0-9]+$" wl-draft-buffer-file-name)
709 (match-string 0 wl-draft-buffer-file-name)))))
710 (wl-draft-config-info-operation msg 'delete))))
711 (set-buffer-modified-p nil) ; force kill
712 (kill-buffer editing-buffer))))
714 (defun wl-draft-kill (&optional force-kill)
715 "Kill current draft buffer and quit editing."
718 (when (and (or (eq major-mode 'wl-draft-mode)
719 (eq major-mode 'mail-mode))
721 (y-or-n-p "Kill Current Draft? ")))
722 (let ((cur-buf (current-buffer)))
723 (wl-draft-hide cur-buf)
724 (wl-draft-delete cur-buf)))
727 (defun wl-draft-fcc ()
728 "Add a new Fcc field, with file name completion."
730 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
731 (mail-position-on-field "to"))
734 ;; Imported from message.el.
735 (defun wl-draft-elide-region (b e)
736 "Elide the text in the region.
737 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
741 (insert wl-draft-elide-ellipsis))
743 ;; function for wl-sent-message-via
745 (defmacro wl-draft-sent-message-p (type)
746 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
748 (defmacro wl-draft-set-sent-message (type result &optional server-port)
749 (` (let ((element (assq (, type) wl-sent-message-via)))
751 (unless (eq (nth 1 element) (, result))
752 (setcdr element (list (, result) (, server-port)))
753 (setq wl-sent-message-modified t))
754 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
755 (setq wl-sent-message-modified t)))))
757 (defun wl-draft-sent-message-results ()
758 (let ((results wl-sent-message-via)
759 unplugged-via sent-via)
761 (cond ((eq (nth 1 (car results)) 'unplugged)
762 (push (caar results) unplugged-via))
763 ((eq (nth 1 (car results)) 'sent)
764 (push (caar results) sent-via)))
765 (setq results (cdr results)))
766 (list unplugged-via sent-via)))
768 (defun wl-draft-write-sendlog (status proto server to id)
769 "Write send log file, if `wl-draft-sendlog' is non-nil."
770 (when wl-draft-sendlog
772 (let* ((filename (expand-file-name wl-draft-sendlog-filename
773 elmo-msgdb-directory))
774 (filesize (nth 7 (file-attributes filename)))
775 (server (if server (concat " server=" server) ""))
777 ((memq proto '(fcc queue))
778 (format " folder=\"%s\"" to))
780 (format " ng=<%s>" to))
785 (mapcar '(lambda(x) (format "<%s>" x)) to)
788 (id (if id (concat " id=" id) ""))
789 (time (wl-sendlog-time)))
790 (insert (format "%s proto=%s stat=%s%s%s%s\n"
791 time proto status server to id))
792 (if (and wl-draft-sendlog-max-size filesize
793 (> filesize wl-draft-sendlog-max-size))
794 (rename-file filename (concat filename ".old") t))
795 (if (file-writable-p filename)
796 (write-region-as-binary (point-min) (point-max)
798 (message (format "%s is not writable." filename)))))))
800 (defun wl-draft-get-header-delimiter (&optional delete)
801 ;; If DELETE is non-nil, replace the header delimiter with a blank line
803 (goto-char (point-min))
804 (when (re-search-forward
805 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
809 (setq delimline (point-marker)))
812 (defun wl-draft-send-mail-with-qmail ()
813 "Pass the prepared message buffer to qmail-inject.
814 Refer to the documentation for the variable `send-mail-function'
815 to find out how to use this."
816 (if (and wl-draft-qmail-send-plugged
817 (not (elmo-plugged-p)))
818 (wl-draft-set-sent-message 'mail 'unplugged)
820 (let ((id (std11-field-body "Message-ID"))
821 (to (std11-field-body "To")))
825 'call-process-region 1 (point-max) wl-qmail-inject-program
827 wl-qmail-inject-args))
828 ;; qmail-inject doesn't say anything on it's stdout/stderr,
829 ;; we have to look at the retval instead
831 (wl-draft-set-sent-message 'mail 'sent)
832 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
833 (1 (error "`qmail-inject' reported permanent failure"))
834 (111 (error "`qmail-inject' reported transient failure"))
835 ;; should never happen
836 (t (error "`qmail-inject' reported unknown failure"))))))
838 (defun wl-draft-parse-msg-id-list-string (string)
839 "Get msg-id list from STRING."
841 (dolist (parsed-id (std11-parse-msg-ids-string string))
842 (when (eq (car parsed-id) 'msg-id)
843 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
845 (nreverse msg-id-list)))
847 (defun wl-draft-std11-parse-addresses (lal)
848 (let ((ret (std11-parse-address lal)))
850 (let ((dest (list (car ret))))
852 (while (and (setq ret (std11-parse-ascii-token lal))
853 (string-equal (cdr (assq 'specials (car ret))) ",")
854 (setq ret (std11-parse-address (cdr ret)))
856 (setq dest (cons (car ret) dest))
857 (setq lal (cdr ret)))
858 (while (eq 'spaces (car (car lal)))
859 (setq lal (cdr lal)))
860 (if lal (error "Error while parsing address"))
863 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
864 "Get mailbox list of FIELD from current buffer.
865 The buffer is expected to be narrowed to just the headers of the message.
866 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
867 from current buffer."
869 (let ((case-fold-search t)
870 (inhibit-read-only t)
872 mailbox-list beg seq has-group-list)
873 (goto-char (point-min))
874 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
877 (re-search-forward "^[^ \t]" nil 'move)
879 (skip-chars-backward "\n")
880 (setq seq (std11-lexical-analyze
881 (buffer-substring-no-properties beg (point))))
882 (setq addresses (wl-draft-std11-parse-addresses seq))
884 (cond ((eq (car (car addresses)) 'group)
885 (setq has-group-list t)
889 'std11-address-string
890 (nth 2 (car addresses))))))
891 ((eq (car (car addresses)) 'mailbox)
892 (setq address (nth 1 (car addresses)))
896 (std11-addr-to-string
897 (if (eq (car address) 'phrase-route-addr)
900 (setq addresses (cdr addresses)))
901 (when (and remove-group-list has-group-list)
902 (delete-region beg (point))
903 (insert (wl-address-string-without-group-list-contents seq))))
906 (defun wl-draft-deduce-address-list (buffer header-start header-end)
907 "Get address list suitable for smtp RCPT TO:<address>.
908 Group list content is removed if `wl-draft-remove-group-list-contents' is
910 (let ((fields '("to" "cc" "bcc"))
911 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
916 (narrow-to-region header-start header-end)
917 (goto-char (point-min))
919 (if (re-search-forward "^resent-to[\t ]*:" nil t)
920 (setq fields resent-fields)))
924 (wl-draft-parse-mailbox-list
926 wl-draft-remove-group-list-contents)))
927 (setq fields (cdr fields)))
933 (defun wl-draft-send-mail-with-smtp ()
934 "Send the prepared message buffer with SMTP."
936 (let* ((errbuf (if mail-interactive
937 (generate-new-buffer " smtp errors")
940 (default-case-fold-search t)
941 (sender (or wl-envelope-from
942 (wl-address-header-extract-address wl-from)))
943 (delimline (save-excursion
944 (goto-char (point-min))
946 (concat "^" (regexp-quote mail-header-separator)
950 (or wl-smtp-posting-server
951 ;; Compatibility stuff for FLIM 1.12.5 or earlier.
952 ;; They don't accept a function as the value of `smtp-server'.
953 (if (functionp smtp-server)
958 (let (wl-draft-remove-group-list-contents)
959 (wl-draft-deduce-address-list
960 (current-buffer) (point-min) delimline)))
961 (or smtp-server "localhost"))))
962 (smtp-service (or wl-smtp-posting-port smtp-service))
963 (smtp-local-domain (or smtp-local-domain wl-local-domain))
964 (id (std11-field-body "message-id"))
966 (if (not (elmo-plugged-p smtp-server smtp-service))
967 (wl-draft-set-sent-message 'mail 'unplugged
968 (cons smtp-server smtp-service))
971 ;; Instead of `smtp-deduce-address-list'.
972 (setq recipients (wl-draft-deduce-address-list
973 (current-buffer) (point-min) delimline))
974 (unless recipients (error "No recipients"))
975 ;; Insert an extra newline if we need it to work around
976 ;; Sun's bug that swallows newlines.
977 (goto-char (1+ delimline))
978 (if (eval mail-mailer-swallows-blank-line)
980 ;;; (run-hooks 'wl-mail-send-pre-hook)
985 (wl-draft-delete-field "bcc" delimline)
986 (wl-draft-delete-field "resent-bcc" delimline)
987 (let (process-connection-type)
990 (wl-smtp-extension-bind
992 (smtp-send-buffer sender recipients (current-buffer))
994 (wl-draft-write-sendlog 'failed 'smtp smtp-server
996 (signal (car err) (cdr err)))))
997 (wl-draft-set-sent-message 'mail 'sent)
998 (wl-draft-write-sendlog
999 'ok 'smtp smtp-server recipients id)))))
1000 (if (bufferp errbuf)
1001 (kill-buffer errbuf))))))
1003 (defun wl-draft-send-mail-with-pop-before-smtp ()
1004 "Send the prepared message buffer with POP-before-SMTP."
1005 (require 'elmo-pop3)
1007 (let ((session (elmo-pop3-get-session
1010 :user (or wl-pop-before-smtp-user
1011 elmo-pop3-default-user)
1012 :server (or wl-pop-before-smtp-server
1013 elmo-pop3-default-server)
1014 :port (or wl-pop-before-smtp-port
1015 elmo-pop3-default-port)
1016 :auth (or wl-pop-before-smtp-authenticate-type
1017 elmo-pop3-default-authenticate-type)
1018 :stream-type (or wl-pop-before-smtp-stream-type
1019 elmo-pop3-default-stream-type)))))
1020 (when session (elmo-network-close-session session)))
1022 (wl-draft-send-mail-with-smtp))
1024 (defun wl-draft-insert-required-fields (&optional force-msgid)
1025 "Insert Message-ID, Date, and From field.
1026 If FORCE-MSGID, ignore 'wl-insert-message-id'."
1027 ;; Insert Message-Id field...
1028 (goto-char (point-min))
1029 (when (and (or force-msgid
1030 wl-insert-message-id)
1031 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1032 (insert (concat "Message-ID: "
1033 (wl-draft-make-message-id-string)
1035 ;; Insert date field.
1036 (goto-char (point-min))
1037 (or (re-search-forward "^Date[ \t]*:" nil t)
1038 (wl-draft-insert-date-field))
1039 ;; Insert from field.
1040 (goto-char (point-min))
1041 (or (re-search-forward "^From[ \t]*:" nil t)
1042 (wl-draft-insert-from-field)))
1044 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1045 "Send the message in the current buffer."
1047 (std11-narrow-to-header mail-header-separator)
1048 (wl-draft-insert-required-fields)
1049 ;; Delete null fields.
1050 (goto-char (point-min))
1051 (while (re-search-forward "^[^ \t\n:]+:[ \t]*\n" nil t)
1053 ;; ignore any blank lines in the header
1054 (while (re-search-forward "\n\n\n*" nil t)
1055 (replace-match "\n")))
1056 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1057 (wl-draft-dispatch-message)
1058 (when kill-when-done
1059 ;; hide editing-buffer.
1060 (wl-draft-hide editing-buffer)
1061 ;; delete editing-buffer and its file.
1062 (wl-draft-delete editing-buffer)))
1064 (defun wl-draft-dispatch-message (&optional mes-string)
1065 "Send the message in the current buffer. Not modified the header fields."
1067 (if (and wl-draft-verbose-send mes-string)
1068 (message mes-string))
1070 (setq delimline (wl-draft-get-header-delimiter t))
1071 (unless wl-draft-fcc-list
1072 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1074 (setq wl-sent-message-modified nil)
1077 (if (and (wl-message-mail-p)
1078 (not (wl-draft-sent-message-p 'mail)))
1079 (funcall wl-draft-send-mail-function))
1080 (if (and (wl-message-news-p)
1081 (not (wl-draft-sent-message-p 'news))
1082 (not (wl-message-field-exists-p "Resent-to")))
1083 (funcall wl-draft-send-news-function)))
1085 (let* ((status (wl-draft-sent-message-results))
1086 (unplugged-via (car status))
1087 (sent-via (nth 1 status)))
1088 ;; If one sent, process fcc folder.
1089 (if (and sent-via wl-draft-fcc-list)
1091 (wl-draft-do-fcc (wl-draft-get-header-delimiter) wl-draft-fcc-list)
1092 (setq wl-draft-fcc-list nil)))
1093 (if wl-draft-use-cache
1094 (let ((id (std11-field-body "Message-ID"))
1095 (elmo-enable-disconnected-operation t))
1096 (elmo-file-cache-save (elmo-file-cache-get-path id)
1098 ;; If one unplugged, append queue.
1099 (when (and unplugged-via
1100 wl-sent-message-modified)
1101 (if wl-draft-enable-queuing
1102 (wl-draft-queue-append wl-sent-message-via)
1103 (error "Unplugged")))
1104 (when wl-draft-verbose-send
1105 (if (and unplugged-via sent-via);; combined message
1107 (setq wl-draft-verbose-msg
1108 (format "Sending%s and Queuing%s..."
1109 sent-via unplugged-via))
1110 (message (concat wl-draft-verbose-msg "done")))
1112 (message (concat mes-string
1113 (if sent-via "done" "failed")))))))))
1114 (not wl-sent-message-modified)) ;; return value
1116 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1117 "Force send current buffer as raw message."
1120 (let (wl-interactive-send
1121 ;;; wl-draft-verbose-send
1122 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1123 ;;; wl-news-send-pre-hook
1126 (wl-draft-send kill-when-done mes-string))))
1128 (defun wl-draft-clone-local-variables ()
1129 (let ((locals (buffer-local-variables))
1132 (when (and (consp (car locals))
1134 (string-match wl-draft-clone-local-variable-regexp
1135 (symbol-name (car (car locals)))))
1136 (wl-append result (list (car (car locals)))))
1137 (setq locals (cdr locals)))
1140 (defun wl-draft-send (&optional kill-when-done mes-string)
1141 "Send current draft message.
1142 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1144 ;; Don't call this explicitly.
1145 ;; Added to 'wl-draft-send-hook (by teranisi)
1146 ;; (wl-draft-config-exec)
1147 (run-hooks 'wl-draft-send-hook)
1148 (when (or (not wl-interactive-send)
1149 (y-or-n-p "Do you really want to send current draft? "))
1150 (let ((send-mail-function 'wl-draft-raw-send)
1151 (editing-buffer (current-buffer))
1152 (sending-buffer (wl-draft-generate-clone-buffer
1153 " *wl-draft-sending-buffer*"
1154 (append wl-draft-config-variables
1155 (wl-draft-clone-local-variables))))
1156 (wl-draft-verbose-msg nil)
1159 (save-excursion (set-buffer sending-buffer)
1160 (if (and (not (wl-message-mail-p))
1161 (not (wl-message-news-p)))
1162 (error "No recipient is specified"))
1163 (expand-abbrev) ; for mail-abbrevs
1164 (run-hooks 'mail-send-hook) ; translate buffer
1165 (if wl-draft-verbose-send
1166 (message (or mes-string "Sending...")))
1167 (funcall wl-draft-send-function editing-buffer kill-when-done)
1168 ;; Now perform actions on successful sending.
1169 (while mail-send-actions
1171 (apply (car (car mail-send-actions))
1172 (cdr (car mail-send-actions)))
1174 (setq mail-send-actions (cdr mail-send-actions)))
1175 ;; (if (or (eq major-mode 'wl-draft-mode)
1176 ;; (eq major-mode 'mail-mode))
1177 ;; (local-set-key "\C-c\C-s" 'wl-draft-send)) ; override
1178 (if wl-draft-verbose-send
1179 (message (concat (or wl-draft-verbose-msg
1180 mes-string "Sending...")
1182 ;; kill sending buffer, anyway.
1183 (and (buffer-live-p sending-buffer)
1184 (kill-buffer sending-buffer))))))
1186 (defun wl-draft-save ()
1187 "Save current draft."
1190 (wl-draft-config-info-operation
1191 (and (string-match "[0-9]+$" wl-draft-buffer-file-name)
1193 (match-string 0 wl-draft-buffer-file-name)))
1196 (defun wl-draft-mimic-kill-buffer ()
1197 "Kill the current (draft) buffer with query."
1199 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1202 (if (or (not bufname)
1203 (string-equal bufname "")
1204 (string-equal bufname (buffer-name)))
1205 (wl-draft-save-and-exit)
1206 (kill-buffer bufname))))
1208 (defun wl-draft-save-and-exit ()
1209 "Save current draft and exit current draft mode."
1212 (let ((editing-buffer (current-buffer)))
1213 (wl-draft-hide editing-buffer)
1214 (kill-buffer editing-buffer)))
1216 (defun wl-draft-send-and-exit ()
1217 "Send current draft message and kill it."
1221 (defun wl-draft-send-from-toolbar ()
1223 (let ((wl-interactive-send t))
1224 (wl-draft-send-and-exit)))
1226 (defun wl-draft-delete-field (field &optional delimline)
1227 (wl-draft-delete-fields (regexp-quote field) delimline))
1229 (defun wl-draft-delete-fields (regexp &optional delimline)
1232 (if (search-forward "\n\n" nil t)
1233 (setq delimline (point))
1234 (setq delimline (point-max))))
1235 (narrow-to-region (point-min) delimline)
1236 (goto-char (point-min))
1237 (let ((regexp (concat "^" regexp ":"))
1238 (case-fold-search t)
1241 (if (looking-at regexp)
1247 (if (re-search-forward "^[^ \t]" nil t)
1248 (goto-char (match-beginning 0))
1251 (if (re-search-forward "^[^ \t]" nil t)
1252 (goto-char (match-beginning 0))
1255 (defun wl-draft-get-fcc-list (header-end)
1257 (case-fold-search t))
1258 (or (markerp header-end) (error "HEADER-END must be a marker"))
1260 (goto-char (point-min))
1261 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1263 (cons (buffer-substring-no-properties
1267 (skip-chars-backward " \t")
1271 (wl-folder-confirm-existence
1272 (wl-folder-get-elmo-folder (eword-decode-string (car fcc-list)))))
1273 (delete-region (match-beginning 0)
1274 (progn (forward-line 1) (point)))))
1277 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1278 (let ((send-mail-buffer (current-buffer))
1279 (tembuf (generate-new-buffer " fcc output"))
1280 (case-fold-search t)
1282 (or (markerp header-end) (error "HEADER-END must be a marker"))
1285 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1288 ;; insert just the headers to avoid moving the gap more than
1289 ;; necessary (the message body could be arbitrarily huge.)
1290 (insert-buffer-substring send-mail-buffer 1 header-end)
1291 (wl-draft-insert-required-fields t)
1292 (goto-char (point-max))
1293 (insert-buffer-substring send-mail-buffer header-end)
1294 (let ((id (std11-field-body "Message-ID"))
1295 (elmo-enable-disconnected-operation t)
1298 (unless (or cache-saved
1299 (elmo-folder-plugged-p
1300 (wl-folder-get-elmo-folder (car fcc-list))))
1301 (elmo-file-cache-save id nil) ;; for disconnected operation
1302 (setq cache-saved t))
1303 (if (elmo-folder-append-buffer
1304 (wl-folder-get-elmo-folder
1305 (eword-decode-string (car fcc-list)))
1306 (not wl-fcc-force-as-read))
1307 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1308 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1309 (setq fcc-list (cdr fcc-list)))))
1310 (kill-buffer tembuf)))
1312 (defun wl-draft-on-field-p ()
1315 (goto-char (point-min))
1316 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1323 (if (or (looking-at ".*,[ \t]?$")
1324 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1326 (let ((pos (point)))
1329 (if (looking-at "^[ \t]")
1331 (if (re-search-forward ":" pos t) nil t)))))))
1333 (defun wl-draft-random-alphabet ()
1334 (let ((alphabet '(?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K ?L ?M ?N ?O ?P ?Q ?R ?S ?T ?U ?V ?W ?X ?Y ?Z)))
1335 (nth (abs (% (random) 26)) alphabet)))
1338 (defun wl-draft (&optional to subject in-reply-to cc references newsgroups
1340 content-type content-transfer-encoding
1341 body edit-again summary-buf from parent-folder)
1342 "Write and send mail/news message with Wanderlust."
1349 (wl-plugged-init t))
1350 (wl-init) ; returns immediately if already initialized.
1352 (setq summary-buf (wl-summary-get-buffer (wl-summary-buffer-folder-name))))
1353 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1354 buf-name file-name num wl-demo change-major-mode-hook)
1355 (if (not (elmo-folder-message-file-p draft-folder))
1356 (error "%s folder cannot be used for draft folder" wl-draft-folder))
1357 (setq num (elmo-max-of-list
1358 (or (elmo-folder-list-messages draft-folder) '(0))))
1359 (setq num (+ 1 num))
1360 ;; To get unused buffer name.
1361 (while (get-buffer (concat wl-draft-folder "/" (int-to-string num)))
1362 (setq num (+ 1 num)))
1363 (setq buf-name (find-file-noselect
1365 (elmo-message-file-name
1366 (wl-folder-get-elmo-folder wl-draft-folder)
1368 (if wl-draft-use-frame
1369 (switch-to-buffer-other-frame buf-name)
1370 (switch-to-buffer buf-name))
1371 (set-buffer buf-name)
1372 (if (not (string-match (regexp-quote wl-draft-folder)
1374 (rename-buffer (concat wl-draft-folder "/" (int-to-string num))))
1375 (if (or (eq wl-draft-reply-buffer-style 'full)
1376 (eq this-command 'wl-draft)
1377 (eq this-command 'wl-summary-write)
1378 (eq this-command 'wl-summary-write-current-folder))
1379 (delete-other-windows))
1382 (make-local-variable 'truncate-partial-width-windows)
1383 (setq truncate-partial-width-windows nil)
1384 (setq truncate-lines wl-draft-truncate-lines)
1385 (setq wl-sent-message-via nil)
1386 (setq wl-draft-parent-folder parent-folder)
1387 (if (stringp (or from wl-from))
1388 (insert "From: " (or from wl-from) "\n"))
1389 (and (or (interactive-p)
1390 (eq this-command 'wl-summary-write)
1392 (insert "To: " (or to "") "\n"))
1393 (and cc (insert "Cc: " (or cc "") "\n"))
1394 (insert "Subject: " (or subject "") "\n")
1395 (and newsgroups (insert "Newsgroups: " newsgroups "\n"))
1396 (and mail-followup-to (insert "Mail-Followup-To: " mail-followup-to "\n"))
1397 (and wl-insert-mail-reply-to
1398 (insert "Mail-Reply-To: "
1399 (wl-address-header-extract-address
1401 (and in-reply-to (insert "In-Reply-To: " in-reply-to "\n"))
1402 (and references (insert "References: " references "\n"))
1403 (insert (funcall wl-generate-mailer-string-function) "\n")
1404 (setq wl-draft-buffer-file-name file-name)
1405 (if mail-default-reply-to
1406 (insert "Reply-To: " mail-default-reply-to "\n"))
1407 (wl-draft-insert-ccs "Bcc: " (or wl-bcc
1408 (and mail-self-blind (user-login-name))))
1409 (wl-draft-insert-ccs "Fcc: " wl-fcc)
1411 (insert "Organization: " wl-organization "\n"))
1412 (and wl-auto-insert-x-face
1413 (file-exists-p wl-x-face-file)
1414 (wl-draft-insert-x-face-field-here))
1415 (if mail-default-headers
1416 (insert mail-default-headers))
1417 (if (not (= (preceding-char) ?\n))
1421 (setq start (point))
1423 (insert "Content-type: " content-type "\n"))
1424 (when content-transfer-encoding
1425 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1426 (if (or content-type content-transfer-encoding)
1428 (and body (insert body))
1430 (narrow-to-region start (point))
1432 (wl-draft-decode-message-in-buffer))
1435 (put-text-property (point)
1437 (insert mail-header-separator "\n")
1439 'category 'mail-header-separator)))
1440 (put-text-property (point)
1442 (insert mail-header-separator "\n")
1444 'category 'mail-header-separator)
1445 (and body (insert body)))
1446 (as-binary-output-file
1447 (write-region (point-min)(point-max) wl-draft-buffer-file-name
1449 (wl-draft-editor-mode)
1450 (wl-draft-overload-functions)
1451 (wl-highlight-headers 'for-draft)
1452 (goto-char (point-min))
1453 (setq wl-draft-config-exec-flag t)
1455 (run-hooks 'wl-mail-setup-hook))
1456 (wl-user-agent-compose-internal) ;; user-agent
1457 (cond ((eq this-command 'wl-summary-write-current-newsgroup)
1458 (mail-position-on-field "Subject"))
1459 ((and (interactive-p) (null to))
1460 (mail-position-on-field "To"))
1462 (goto-char (point-max))))
1463 (setq wl-draft-buffer-cur-summary-buffer (or summary-buf
1465 wl-summary-buffer-name)))
1468 (defsubst wl-draft-insert-ccs (str cc)
1474 (null (and wl-draft-delete-myself-from-bcc-fcc
1476 (mapcar 'wl-address-header-extract-address
1478 (wl-parse-addresses (std11-field-body "To"))
1479 (wl-parse-addresses (std11-field-body "Cc"))))
1480 (mapcar 'downcase wl-subscribed-mailing-list)))))
1481 (insert str field "\n"))))
1483 (defun wl-draft-elmo-nntp-send ()
1484 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1485 (elmo-nntp-default-user
1486 (or wl-nntp-posting-user elmo-nntp-default-user))
1487 (elmo-nntp-default-server
1488 (or wl-nntp-posting-server elmo-nntp-default-server))
1489 (elmo-nntp-default-port
1490 (or wl-nntp-posting-port elmo-nntp-default-port))
1491 (elmo-nntp-default-stream-type
1492 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type)))
1493 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1494 (wl-draft-set-sent-message 'news 'unplugged
1495 (cons elmo-nntp-default-server
1496 elmo-nntp-default-port))
1497 (elmo-nntp-post elmo-nntp-default-server (current-buffer))
1498 (wl-draft-set-sent-message 'news 'sent)
1499 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1500 (std11-field-body "Newsgroups")
1501 (std11-field-body "Message-ID")))))
1503 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1504 "Generate clone of current buffer named NAME."
1505 (let ((editing-buffer (current-buffer)))
1507 (set-buffer (generate-new-buffer name))
1510 (wl-draft-editor-mode)
1511 (insert-buffer editing-buffer)
1513 (while local-variables
1514 (make-local-variable (car local-variables))
1515 (set (car local-variables)
1517 (set-buffer editing-buffer)
1518 (symbol-value (car local-variables))))
1519 (setq local-variables (cdr local-variables)))
1522 (defun wl-draft-reedit (number)
1523 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1525 buf-name file-name change-major-mode-hook)
1526 (setq file-name (elmo-message-file-name draft-folder number))
1527 (unless (file-exists-p file-name)
1528 (error "File %s does not exist" file-name))
1529 (setq buf-name (find-file-noselect file-name))
1530 (if wl-draft-use-frame
1531 (switch-to-buffer-other-frame buf-name)
1532 (switch-to-buffer buf-name))
1533 (set-buffer buf-name)
1534 (if (not (string-match (regexp-quote wl-draft-folder)
1536 (rename-buffer (concat wl-draft-folder "/" (buffer-name))))
1539 (setq wl-sent-message-via nil)
1540 (setq wl-draft-buffer-file-name file-name)
1541 (wl-draft-config-info-operation number 'load)
1542 (goto-char (point-min))
1543 (or (re-search-forward "\n\n" nil t)
1544 (search-forward (concat mail-header-separator "\n") nil t))
1545 (write-region (point-min)(point-max) wl-draft-buffer-file-name
1547 (wl-draft-overload-functions)
1548 (wl-draft-editor-mode)
1549 (wl-highlight-headers 'for-draft)
1550 (run-hooks 'wl-draft-reedit-hook)
1551 (goto-char (point-max))
1555 (defmacro wl-draft-body-goto-top ()
1557 (goto-char (point-min))
1558 (if (re-search-forward mail-header-separator nil t)
1560 (goto-char (point-max))))))
1562 (defmacro wl-draft-body-goto-bottom ()
1563 (` (goto-char (point-max))))
1565 (defmacro wl-draft-config-body-goto-header ()
1567 (goto-char (point-min))
1568 (if (re-search-forward mail-header-separator nil t)
1570 (goto-char (point-max))))))
1572 (defun wl-draft-config-sub-body (content)
1573 (wl-draft-body-goto-top)
1574 (delete-region (point) (point-max))
1575 (if content (insert (eval content))))
1577 (defun wl-draft-config-sub-top (content)
1578 (wl-draft-body-goto-top)
1579 (if content (insert (eval content))))
1581 (defun wl-draft-config-sub-bottom (content)
1582 (wl-draft-body-goto-bottom)
1583 (if content (insert (eval content))))
1585 (defun wl-draft-config-sub-header (content)
1586 (wl-draft-config-body-goto-header)
1587 (if content (insert (concat (eval content) "\n"))))
1589 (defun wl-draft-config-sub-header-top (content)
1590 (goto-char (point-min))
1591 (if content (insert (concat (eval content) "\n"))))
1593 (defun wl-draft-config-sub-part-top (content)
1594 (goto-char (mime-edit-content-beginning))
1595 (if content (insert (concat (eval content) "\n"))))
1597 (defun wl-draft-config-sub-part-bottom (content)
1598 (goto-char (mime-edit-content-end))
1599 (if content (insert (concat (eval content) "\n"))))
1601 (defsubst wl-draft-config-sub-file (content)
1602 (let ((coding-system-for-read wl-cs-autoconv)
1603 (file (expand-file-name (eval content))))
1604 (if (file-exists-p file)
1605 (insert-file-contents file)
1606 (error "%s: no exists file" file))))
1608 (defun wl-draft-config-sub-body-file (content)
1609 (wl-draft-body-goto-top)
1610 (delete-region (point) (point-max))
1611 (wl-draft-config-sub-file content))
1613 (defun wl-draft-config-sub-top-file (content)
1614 (wl-draft-body-goto-top)
1615 (wl-draft-config-sub-file content))
1617 (defun wl-draft-config-sub-bottom-file (content)
1618 (wl-draft-body-goto-bottom)
1619 (wl-draft-config-sub-file content))
1621 (defun wl-draft-config-sub-header-file (content)
1622 (wl-draft-config-body-goto-header)
1623 (wl-draft-config-sub-file content))
1625 (defun wl-draft-config-sub-template (content)
1626 (setq wl-draft-config-variables
1627 (wl-template-insert (eval content))))
1629 (defun wl-draft-config-sub-x-face (content)
1630 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
1631 (fboundp 'x-face-insert)) ; x-face.el is installed.
1632 (x-face-insert content)
1633 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
1635 (defsubst wl-draft-config-sub-func (field content)
1637 (if (setq func (assq field wl-draft-config-sub-func-alist))
1638 (let (wl-draft-config-variables)
1639 (funcall (cdr func) content)
1640 ;; for wl-draft-config-sub-template
1641 (cons t wl-draft-config-variables)))))
1643 (defsubst wl-draft-config-exec-sub (clist)
1644 (let (config local-variables)
1646 (setq config (car clist))
1651 (let ((field (car config))
1652 (content (cdr config))
1656 (wl-draft-replace-field field (eval content) t))
1657 ((setq ret-val (wl-draft-config-sub-func field content))
1658 (if (cdr ret-val) ;; for wl-draft-config-sub-template
1659 (wl-append local-variables (cdr ret-val))))
1660 ((boundp field) ;; variable
1661 (make-local-variable field)
1662 (set field (eval content))
1663 (wl-append local-variables (list field)))
1665 (error "%s: not variable" field)))))
1667 (error "%s: not supported type" config)))
1668 (setq clist (cdr clist)))
1671 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
1672 "Change headers in draft preparation time."
1674 (unless wl-draft-reedit
1677 (and (boundp 'wl-draft-prepared-config-alist)
1678 wl-draft-prepared-config-alist) ;; For compatible.
1679 wl-draft-config-alist)))
1681 (wl-draft-config-exec config-alist reply-buf)))))
1683 (defun wl-draft-config-exec (&optional config-alist reply-buf)
1684 "Change headers in draft sending time."
1686 (let ((case-fold-search t)
1687 (alist (or config-alist wl-draft-config-alist))
1688 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
1689 wl-draft-reply-buffer)))
1690 (local-variables wl-draft-config-variables)
1692 (when (and (or (interactive-p)
1693 wl-draft-config-exec-flag)
1698 (setq key (caar alist)
1705 (set-buffer reply-buf)
1707 (std11-narrow-to-header)
1708 (goto-char (point-min))
1709 (re-search-forward (car clist) nil t))))
1710 (wl-draft-config-exec-sub (cdr clist))
1713 (when (save-restriction
1714 (std11-narrow-to-header mail-header-separator)
1715 (goto-char (point-min))
1716 (re-search-forward key nil t))
1717 (wl-append local-variables
1718 (wl-draft-config-exec-sub clist))
1721 (wl-append local-variables
1722 (wl-draft-config-exec-sub clist))
1724 (if (and found wl-draft-config-matchone)
1726 (setq alist (cdr alist))))
1728 (setq wl-draft-config-exec-flag nil))
1729 (run-hooks 'wl-draft-config-exec-hook)
1730 (put-text-property (point-min)(point-max) 'face nil)
1731 (wl-highlight-message (point-min)(point-max) t)
1732 (setq wl-draft-config-variables
1733 (elmo-uniq-list local-variables))))))
1735 (defun wl-draft-replace-field (field content &optional add)
1738 (let ((case-fold-search t)
1739 (inhibit-read-only t) ;; added by teranisi.
1741 (std11-narrow-to-header mail-header-separator)
1742 (goto-char (point-min))
1743 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
1748 (re-search-forward "^[^ \t]" nil 'move)
1750 (skip-chars-backward "\n")
1751 (delete-region beg (point))
1752 (insert " " content))
1757 (re-search-forward "^[^ \t]" nil 'move)
1759 (delete-region beg (point)))
1760 (when (and add content)
1762 (goto-char (point-max))
1763 (insert (concat field ": " content "\n"))))))))
1765 (defun wl-draft-config-info-operation (msg operation)
1766 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-folder-get-elmo-folder
1770 (format "%s-%d" wl-draft-config-save-filename msg)
1772 element alist variable)
1774 ((eq operation 'save)
1775 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
1776 (while (setq variable (pop variables))
1777 (when (boundp variable)
1779 (list (cons variable (eval variable))))))
1780 (elmo-object-save filename alist)))
1781 ((eq operation 'load)
1782 (setq alist (elmo-object-load filename))
1783 (while (setq element (pop alist))
1784 (set (make-local-variable (car element)) (cdr element))
1785 (wl-append wl-draft-config-variables (list (car element)))))
1786 ((eq operation 'delete)
1787 (if (file-exists-p filename)
1788 (delete-file filename))))))
1790 (defun wl-draft-queue-info-operation (msg operation
1791 &optional add-sent-message-via)
1792 (let* ((msgdb-dir (elmo-folder-msgdb-path
1793 (wl-folder-get-elmo-folder wl-queue-folder)))
1796 (format "%s-%d" wl-draft-queue-save-filename msg)
1798 element alist variable)
1800 ((eq operation 'save)
1801 (let ((variables (elmo-uniq-list
1802 (append wl-draft-queue-save-variables
1803 wl-draft-config-variables
1804 (list 'wl-draft-fcc-list)))))
1805 (if add-sent-message-via
1806 (push 'wl-sent-message-via variables))
1807 (while (setq variable (pop variables))
1808 (when (boundp variable)
1810 (list (cons variable (eval variable))))))
1811 (elmo-object-save filename alist)))
1812 ((eq operation 'load)
1813 (setq alist (elmo-object-load filename))
1814 (while (setq element (pop alist))
1815 (set (make-local-variable (car element)) (cdr element))))
1816 ((eq operation 'get-sent-via)
1817 (setq alist (elmo-object-load filename))
1818 (cdr (assq 'wl-sent-message-via alist)))
1819 ((eq operation 'delete)
1820 (if (file-exists-p filename)
1821 (delete-file filename))))))
1823 (defun wl-draft-queue-append (wl-sent-message-via)
1824 (if wl-draft-verbose-send
1825 (message "Queuing..."))
1826 (let ((send-buffer (current-buffer))
1827 (folder (wl-folder-get-elmo-folder wl-queue-folder))
1828 (message-id (std11-field-body "Message-ID")))
1829 (if (elmo-folder-append-buffer folder t)
1831 (wl-draft-queue-info-operation
1832 (car (elmo-folder-status folder))
1833 'save wl-sent-message-via)
1834 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
1835 (when wl-draft-verbose-send
1836 (setq wl-draft-verbose-msg "Queuing...")
1837 (message "Queuing...done")))
1838 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
1839 (error "Queuing failed"))))
1841 (defun wl-draft-queue-flush ()
1842 "Flush draft queue."
1844 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
1846 (elmo-folder-open-internal queue-folder)
1847 (elmo-folder-list-messages queue-folder)))
1850 (wl-draft-queue-flushing t)
1851 msgs failure len buffer msgid sent-via)
1852 ;; get plugged send message
1854 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
1857 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
1859 (car (nth 2 (car sent-via)))
1860 (cdr (nth 2 (car sent-via)))))
1861 (wl-append msgs (list (car msgs2)))
1863 (setq sent-via (cdr sent-via))))
1864 (setq msgs2 (cdr msgs2)))
1865 (when (> (setq len (length msgs)) 0)
1866 (if (elmo-y-or-n-p (format
1867 "%d message(s) are in the sending queue. Send now? "
1869 (not elmo-dop-flush-confirm) t)
1872 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
1875 ;; reset buffer local variables
1876 (kill-all-local-variables)
1880 (setq wl-sent-message-via nil)
1881 (wl-draft-queue-info-operation (car msgs) 'load)
1882 (elmo-message-fetch queue-folder
1884 (elmo-make-fetch-strategy 'entire)
1885 nil (current-buffer))
1887 (setq failure (funcall
1888 wl-draft-queue-flush-send-function
1889 (format "Sending (%d/%d)..." i len)))
1890 ;;; (wl-draft-raw-send nil nil
1891 ;;; (format "Sending (%d/%d)..." i len))
1893 (elmo-display-error err t)
1898 (elmo-folder-delete-messages
1899 queue-folder (cons (car msgs) nil))
1900 (wl-draft-queue-info-operation (car msgs) 'delete)
1901 (setq performed (+ 1 performed)))
1902 (setq msgs (cdr msgs)))
1903 (kill-buffer buffer)
1904 (message "%d message(s) are sent." performed)))
1905 (message "%d message(s) are remained to be sent." len))
1906 (elmo-folder-close queue-folder)
1909 (defun wl-jump-to-draft-buffer (&optional arg)
1910 "Jump to the draft if exists."
1913 (wl-jump-to-draft-folder)
1914 (let ((bufs (buffer-list))
1915 (draft-regexp (concat
1917 (elmo-localdir-folder-directory-internal
1918 (wl-folder-get-elmo-folder wl-draft-folder)))))
1922 (setq buf (buffer-file-name (car bufs)))
1923 (string-match draft-regexp buf))
1924 (setq draft-bufs (cons (buffer-name (car bufs)) draft-bufs)))
1925 (setq bufs (cdr bufs)))
1928 (message "No draft buffer exist."))
1931 (sort draft-bufs (function (lambda (a b) (not (string< a b))))))
1932 (if (setq buf (cdr (member (buffer-name) draft-bufs)))
1933 (setq buf (car buf))
1934 (setq buf (car draft-bufs)))
1935 (switch-to-buffer buf))))))
1937 (defun wl-jump-to-draft-folder ()
1938 (let ((msgs (reverse (elmo-folder-list-messages (wl-folder-get-elmo-folder
1940 (mybuf (buffer-name))
1943 (message "No draft message exist.")
1944 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
1945 (setq msg (cadr (memq
1946 (string-to-int (substring mybuf (match-end 0)))
1948 (or msg (setq msg (car msgs)))
1949 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
1950 (switch-to-buffer buf)
1951 (wl-draft-reedit msg)))))
1953 (defun wl-draft-highlight-and-recenter (&optional n)
1955 (if wl-highlight-body-too
1956 (let ((beg (point-min))
1958 (put-text-property beg end 'face nil)
1959 (wl-highlight-message beg end t)))
1962 ;;;; user-agent support by Sen Nagata
1964 ;; this appears to be necessarily global...
1965 (defvar wl-user-agent-compose-p nil)
1966 (defvar wl-user-agent-headers-and-body-alist nil)
1968 ;; this should be a generic function for mail-mode -- i wish there was
1969 ;; something like it in sendmail.el
1970 (defun wl-user-agent-insert-header (header-name header-value)
1971 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
1972 ;; it seems like overriding existing headers is acceptable -- should
1973 ;; we provide an option?
1975 ;; plan was: unfold header (might be folded), remove existing value, insert
1977 ;; wl doesn't seem to fold header lines yet anyway :-)
1979 (let ((kill-whole-line t)
1981 (mail-position-on-field (capitalize header-name))
1982 (setq end-of-line (point))
1984 (re-search-forward ":" end-of-line)
1985 (insert (concat " " header-value "\n"))
1988 ;; this should be a generic function for mail-mode -- i wish there was
1989 ;; something like it in sendmail.el
1991 ;; ** haven't dealt w/ case where the body is already set **
1992 (defun wl-user-agent-insert-body (body-text)
1993 "Insert a body of text, BODY-TEXT, into a message."
1994 ;; code defensively... :-P
1995 (goto-char (point-min))
1996 (search-forward mail-header-separator)
2001 (defun wl-user-agent-compose (&optional to subject other-headers continue
2002 switch-function yank-action
2004 "Support the `compose-mail' interface for wl.
2005 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2006 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2007 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2009 (unless (featurep 'wl)
2011 ;; protect these -- to and subject get bound at some point, so it looks
2012 ;; to be necessary to protect the values used w/in
2013 (let ((wl-user-agent-headers-and-body-alist other-headers)
2014 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2015 (wl-draft-reply-buffer-style 'split))
2016 (when (eq switch-function 'switch-to-buffer-other-window)
2017 (when (one-window-p t)
2018 (if (window-minibuffer-p) (other-window 1))
2022 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2025 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2028 (setq wl-user-agent-headers-and-body-alist
2029 (cons (cons "to" to)
2030 wl-user-agent-headers-and-body-alist))))
2032 (if (wl-string-match-assoc "subject"
2033 wl-user-agent-headers-and-body-alist
2036 (wl-string-match-assoc "subject"
2037 wl-user-agent-headers-and-body-alist
2040 (setq wl-user-agent-headers-and-body-alist
2041 (cons (cons "subject" subject)
2042 wl-user-agent-headers-and-body-alist))))
2043 ;; i think this is what we want to use...
2046 ;; tell the hook-function to do its stuff
2047 (setq wl-user-agent-compose-p t)
2048 ;; because to get the hooks working, wl-draft has to think it has
2049 ;; been called interactively
2050 (call-interactively 'wl-draft))
2051 (setq wl-user-agent-compose-p nil))))
2053 (defun wl-user-agent-compose-internal ()
2054 "Manipulate headers and/or a body of a draft message."
2055 ;; being called from wl-user-agent-compose?
2056 (if wl-user-agent-compose-p
2059 (let ((headers wl-user-agent-headers-and-body-alist)
2060 (case-fold-search t))
2063 (if (not (string-match "^body$" (car (car headers))))
2064 (wl-user-agent-insert-header
2065 (car (car headers)) (cdr (car headers)))
2067 (setq headers (cdr headers))))
2068 ;; highlight headers (from wl-draft in wl-draft.el)
2069 (wl-highlight-headers 'for-draft)
2071 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2073 (wl-user-agent-insert-body
2074 (cdr (wl-string-match-assoc
2076 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2080 (product-provide (provide 'wl-draft) (require 'wl-version))
2082 ;;; wl-draft.el ends here