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))
52 (defvar wl-draft-buf-name "Draft")
53 (defvar wl-draft-cite-function 'wl-default-draft-cite)
54 (defvar wl-draft-buffer-file-name nil)
55 (defvar wl-draft-field-completion-list nil)
56 (defvar wl-draft-verbose-send t)
57 (defvar wl-draft-verbose-msg nil)
58 (defvar wl-draft-queue-flushing nil)
59 (defvar wl-draft-config-variables nil)
60 (defvar wl-draft-config-exec-flag t)
61 (defvar wl-draft-buffer-cur-summary-buffer nil)
62 (defvar wl-draft-clone-local-variable-regexp "^\\(wl\\|mime\\)")
63 (defvar wl-draft-sendlog-filename "sendlog")
64 (defvar wl-draft-queue-save-filename "qinfo")
65 (defvar wl-draft-config-save-filename "config")
66 (defvar wl-draft-queue-flush-send-function 'wl-draft-dispatch-message)
67 (defvar wl-sent-message-via nil)
68 (defvar wl-sent-message-modified nil)
69 (defvar wl-draft-fcc-list nil)
70 (defvar wl-draft-reedit nil)
71 (defvar wl-draft-reply-buffer nil)
72 (defvar wl-draft-forward nil)
74 (defvar wl-draft-config-sub-func-alist
75 '((body . wl-draft-config-sub-body)
76 (top . wl-draft-config-sub-top)
77 (bottom . wl-draft-config-sub-bottom)
78 (header . wl-draft-config-sub-header)
79 (body-file . wl-draft-config-sub-body-file)
80 (top-file . wl-draft-config-sub-top-file)
81 (bottom-file . wl-draft-config-sub-bottom-file)
82 (header-file . wl-draft-config-sub-header-file)
83 (template . wl-draft-config-sub-template)
84 (x-face . wl-draft-config-sub-x-face)))
86 (make-variable-buffer-local 'wl-draft-buffer-file-name)
87 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
88 (make-variable-buffer-local 'wl-draft-config-variables)
89 (make-variable-buffer-local 'wl-draft-config-exec-flag)
90 (make-variable-buffer-local 'wl-sent-message-via)
91 (make-variable-buffer-local 'wl-draft-fcc-list)
92 (make-variable-buffer-local 'wl-draft-reply-buffer)
94 (defmacro wl-smtp-extension-bind (&rest body)
95 (` (let* ((smtp-sasl-mechanisms
96 (if wl-smtp-authenticate-type
98 (if (listp wl-smtp-authenticate-type)
99 wl-smtp-authenticate-type
100 (list wl-smtp-authenticate-type)))))
101 (smtp-use-sasl (and smtp-sasl-mechanisms t))
102 (smtp-use-starttls wl-smtp-connection-type)
103 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
104 (if (and (string= (car smtp-sasl-mechanisms) "DIGEST-MD5")
106 (string-match "^\\([^@]*\\)@\\([^@]*\\)"
107 wl-smtp-posting-user))
108 (setq smtp-sasl-user-name (match-string 1 wl-smtp-posting-user)
109 smtp-sasl-properties (list 'realm
110 (match-string 2 wl-smtp-posting-user)))
111 (setq smtp-sasl-user-name wl-smtp-posting-user
112 smtp-sasl-properties nil))
113 (setq sasl-read-passphrase
122 (defun wl-draft-insert-date-field ()
124 (insert "Date: " (wl-make-date-string) "\n"))
126 (defun wl-draft-insert-from-field ()
128 ;; Put the "From:" field in unless for some odd reason
129 ;; they put one in themselves.
130 (let* ((login (or user-mail-address (user-login-name)))
131 (fullname (user-full-name)))
132 (cond ((eq mail-from-style 'angles)
133 (insert "From: " fullname)
134 (let ((fullname-start (+ (point-min) (length "From: ")))
135 (fullname-end (point-marker)))
136 (goto-char fullname-start)
137 ;; Look for a character that cannot appear unquoted
138 ;; according to RFC 822.
139 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
142 ;; Quote fullname, escaping specials.
143 (goto-char fullname-start)
145 (while (re-search-forward "[\"\\]"
147 (replace-match "\\\\\\&" t))
149 (insert " <" login ">\n"))
150 ((eq mail-from-style 'parens)
151 (insert "From: " login " (")
152 (let ((fullname-start (point)))
154 (let ((fullname-end (point-marker)))
155 (goto-char fullname-start)
156 ;; RFC 822 says \ and nonmatching parentheses
157 ;; must be escaped in comments.
158 ;; Escape every instance of ()\ ...
159 (while (re-search-forward "[()\\]" fullname-end 1)
160 (replace-match "\\\\\\&" t))
161 ;; ... then undo escaping of matching parentheses,
162 ;; including matching nested parentheses.
163 (goto-char fullname-start)
164 (while (re-search-forward
165 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
167 (replace-match "\\1(\\3)" t)
168 (goto-char fullname-start))))
170 ((not mail-from-style)
171 (insert "From: " login "\n")))))
173 (defun wl-draft-insert-x-face-field ()
174 "Insert X-Face header."
176 (if (not (file-exists-p wl-x-face-file))
177 (error "File %s does not exist" wl-x-face-file)
178 (beginning-of-buffer)
179 (search-forward mail-header-separator nil t)
181 (wl-draft-insert-x-face-field-here)
182 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
184 (defun wl-draft-insert-x-face-field-here ()
185 "Insert X-Face field at point."
186 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
187 (when (string-match "^[ \t]*" x-face-string)
188 (setq x-face-string (substring x-face-string (match-end 0))))
189 (insert "X-Face: " x-face-string))
190 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
192 ;; Insert X-Face-Version: field
193 (when (and (fboundp 'x-face-insert-version-header)
194 (boundp 'x-face-add-x-face-version-header)
195 x-face-add-x-face-version-header)
196 (x-face-insert-version-header)))
198 (defun wl-draft-setup ()
199 (let ((field wl-draft-fields)
202 (setq ret-val (append ret-val
203 (list (cons (concat (car field) " ")
204 (concat (car field) " ")))))
205 (setq field (cdr field)))
206 (setq wl-draft-field-completion-list ret-val)))
208 (defun wl-draft-make-mail-followup-to (recipients)
209 (if (elmo-list-member
210 (or wl-user-mail-address-list
211 (list (wl-address-header-extract-address wl-from)))
213 (let ((rlist (elmo-list-delete
214 (or wl-user-mail-address-list
215 (list (wl-address-header-extract-address wl-from)))
216 (copy-sequence recipients))))
217 (if (elmo-list-member rlist (mapcar 'downcase
218 wl-subscribed-mailing-list))
220 (append rlist (list (wl-address-header-extract-address
224 (defun wl-draft-delete-myself-from-cc (to cc)
225 (let ((myself (or wl-user-mail-address-list
226 (list (wl-address-header-extract-address wl-from)))))
227 (cond (wl-draft-always-delete-myself ; always-delete option
228 (elmo-list-delete myself cc))
229 ((elmo-list-member (append to cc) ; subscribed mailing-list
230 (mapcar 'downcase wl-subscribed-mailing-list))
231 (elmo-list-delete myself cc))
234 (defun wl-draft-forward (original-subject summary-buf)
236 (with-current-buffer (wl-message-get-original-buffer)
237 (setq references (nconc
238 (std11-field-bodies '("References" "In-Reply-To"))
239 (list (std11-field-body "Message-Id"))))
240 (setq references (delq nil references)
241 references (mapconcat 'identity references " ")
242 references (wl-draft-parse-msg-id-list-string references)
243 references (wl-delete-duplicates references)
244 references (when references
245 (mapconcat 'identity references "\n\t"))))
246 (wl-draft "" (concat "Forward: " original-subject)
247 nil nil references nil nil nil nil nil nil summary-buf))
248 (goto-char (point-max))
249 (wl-draft-insert-message)
250 (mail-position-on-field "To"))
252 (defun wl-draft-strip-subject-re (subject)
253 "Remove \"Re:\" from subject lines. Shamelessly copied from Gnus"
254 (if (string-match wl-subject-prefix-regexp subject)
255 (substring subject (match-end 0))
258 (defun wl-draft-reply-list-symbol (with-arg)
259 "Return symbol `wl-draft-reply-*-argument-list' match condition.
260 Check WITH-ARG and From: field."
261 (if (wl-address-user-mail-address-p (or (elmo-field-body "From") ""))
263 'wl-draft-reply-myself-with-argument-list
264 'wl-draft-reply-myself-without-argument-list)
266 'wl-draft-reply-with-argument-list
267 'wl-draft-reply-without-argument-list)))
269 (defun wl-draft-reply (buf with-arg summary-buf)
270 "Reply to BUF buffer message.
271 Reply to author if WITH-ARG is non-nil."
274 to mail-followup-to cc subject in-reply-to references newsgroups
275 from to-alist cc-alist decoder)
277 (setq r-list (symbol-value (wl-draft-reply-list-symbol with-arg)))
280 (when (let ((condition (car (car r-list))))
281 (cond ((stringp condition)
282 (std11-field-body condition))
286 (if (not (std11-field-body (car condition)))
288 (setq condition (cdr condition)))
291 (funcall condition))))
292 (let ((r-to-list (nth 0 (cdr (car r-list))))
293 (r-cc-list (nth 1 (cdr (car r-list))))
294 (r-ng-list (nth 2 (cdr (car r-list)))))
295 (when (and (member "Followup-To" r-ng-list)
296 (string= (std11-field-body "Followup-To") "poster"))
297 (setq r-to-list (cons "From" r-to-list))
298 (setq r-ng-list (delete "Followup-To" (copy-sequence r-ng-list))))
299 (setq to (wl-concat-list (cons to
300 (elmo-multiple-fields-body-list
303 (setq cc (wl-concat-list (cons cc
304 (elmo-multiple-fields-body-list
307 (setq newsgroups (wl-concat-list (cons newsgroups
312 (setq r-list (cdr r-list)))
313 (error "No match field: check your `%s'"
314 (symbol-name (wl-draft-reply-list-symbol with-arg))))
315 (setq subject (std11-field-body "Subject"))
316 (setq to (wl-parse-addresses to)
317 cc (wl-parse-addresses cc))
318 (with-temp-buffer ; to keep raw buffer unibyte.
319 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
320 (setq decoder (mime-find-field-decoder 'Subject 'plain))
321 (setq subject (if (and subject decoder)
322 (funcall decoder subject) subject))
326 (setq decoder (mime-find-field-decoder 'To 'plain))
327 (cons (nth 1 (std11-extract-address-components addr))
328 (if decoder (funcall decoder addr) addr)))
333 (setq decoder (mime-find-field-decoder 'Cc 'plain))
334 (cons (nth 1 (std11-extract-address-components addr))
335 (if decoder (funcall decoder addr) addr)))
337 (and wl-reply-subject-prefix
338 (setq subject (concat wl-reply-subject-prefix
339 (wl-draft-strip-subject-re
341 (setq in-reply-to (std11-field-body "Message-Id"))
342 (setq references (nconc
343 (std11-field-bodies '("References" "In-Reply-To"))
345 (setq to (delq nil (mapcar 'car to-alist)))
346 (setq cc (delq nil (mapcar 'car cc-alist)))
347 ;; if subscribed mailing list is contained in cc or to
348 ;; and myself is contained in cc,
349 ;; delete myself from cc.
350 (setq cc (wl-draft-delete-myself-from-cc to cc))
351 (when wl-insert-mail-followup-to
352 (setq mail-followup-to
353 (wl-draft-make-mail-followup-to (append to cc)))
354 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
355 (setq newsgroups (wl-parse newsgroups
356 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
357 newsgroups (wl-delete-duplicates newsgroups)
358 newsgroups (if newsgroups (mapconcat 'identity newsgroups ",")))
359 (setq to (wl-delete-duplicates to nil t))
360 (setq cc (wl-delete-duplicates
361 (append (wl-delete-duplicates cc nil t)
362 to (copy-sequence to))
364 (and to (setq to (mapconcat
366 (if wl-draft-reply-use-address-with-full-name
367 (or (cdr (assoc addr to-alist)) addr)
370 (and cc (setq cc (mapconcat
372 (if wl-draft-reply-use-address-with-full-name
373 (or (cdr (assoc addr cc-alist)) addr)
376 (and mail-followup-to
377 (setq mail-followup-to
380 (if wl-draft-reply-use-address-with-full-name
381 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
383 mail-followup-to ",\n\t")))
384 (and (null to) (setq to cc cc nil))
385 (setq references (delq nil references)
386 references (mapconcat 'identity references " ")
387 references (wl-draft-parse-msg-id-list-string references)
388 references (wl-delete-duplicates references)
389 references (if references
390 (mapconcat 'identity references "\n\t")))
392 to subject in-reply-to cc references newsgroups mail-followup-to
393 nil nil nil nil summary-buf)
394 (setq wl-draft-reply-buffer buf))
395 (run-hooks 'wl-reply-hook))
397 (defun wl-draft-add-references ()
398 (let* ((mes-id (save-excursion
399 (set-buffer mail-reply-buffer)
400 (std11-field-body "message-id")))
401 (ref (std11-field-body "References"))
402 (ref-list nil) (st nil))
403 (when (and mes-id ref)
404 (while (string-match "<[^>]+>" ref st)
406 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
409 (member mes-id ref-list))
413 (when (mail-position-on-field "References")
415 (while (looking-at "^[ \t]")
417 (setq mes-id (concat "\t" mes-id "\n")))
421 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
422 &optional ignored-fields)
425 (narrow-to-region (point)(point))
428 (set-buffer mail-reply-buffer)
430 (decode-mime-charset-region (point-min) (point-max)
432 (buffer-substring-no-properties
433 (point-min) (point-max))))
435 (goto-char (point-min))
436 (wl-draft-delete-fields ignored-fields))
437 (goto-char (point-max))
439 (goto-char (point-min)))
441 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
442 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
443 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
444 (run-hooks 'wl-draft-cited-hook)
445 (when (and wl-draft-add-references
446 (wl-draft-add-references))
447 (wl-highlight-headers 'for-draft)) ; highlight when added References:
448 (when wl-highlight-body-too
449 (wl-highlight-body-region beg (point-max)))))
451 (defun wl-draft-confirm ()
452 "Confirm send message."
454 (y-or-n-p (format "Send current draft as %s? "
455 (cond ((and (wl-message-mail-p) (wl-message-news-p))
457 ((wl-message-mail-p) "Mail")
458 ((wl-message-news-p) "News")))))
460 (defun wl-message-news-p ()
461 "If exist valid Newsgroups field, return non-nil."
462 (std11-field-body "Newsgroups"))
464 (defun wl-message-field-exists-p (field)
465 "If FIELD exist and FIELD value is not empty, return non-nil."
466 (let ((value (std11-field-body field)))
468 (not (string= value "")))))
470 (defun wl-message-mail-p ()
471 "If exist To, Cc or Bcc field, return non-nil."
472 (or (wl-message-field-exists-p "To")
473 (wl-message-field-exists-p "Cc")
474 (wl-message-field-exists-p "Bcc")
475 ;;; This may be needed..
476 ;;; (wl-message-field-exists-p "Fcc")
479 (defun wl-draft-open-file (&optional file)
480 "Open FILE for edit."
482 ;;;(interactive "*fFile to edit: ")
483 (wl-draft-edit-string (elmo-get-file-string
485 (read-file-name "File to edit: "
486 (or wl-tmp-dir "~/"))))))
488 (defun wl-draft-edit-string (string)
489 (let ((cur-buf (current-buffer))
490 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
491 to subject in-reply-to cc references newsgroups mail-followup-to
492 content-type content-transfer-encoding
493 body-beg buffer-read-only
498 (setq to (std11-field-body "To"))
501 (decode-mime-charset-string
504 (setq subject (std11-field-body "Subject"))
505 (setq subject (and subject
507 (decode-mime-charset-string
510 (setq in-reply-to (std11-field-body "In-Reply-To"))
511 (setq cc (std11-field-body "Cc"))
514 (decode-mime-charset-string
517 (setq references (std11-field-body "References"))
518 (setq newsgroups (std11-field-body "Newsgroups"))
519 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
520 (setq content-type (std11-field-body "Content-Type"))
521 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
522 (goto-char (point-min))
523 (or (re-search-forward "\n\n" nil t)
524 (search-forward (concat mail-header-separator "\n") nil t))
527 (wl-draft to subject in-reply-to cc references newsgroups
529 content-type content-transfer-encoding
530 (buffer-substring (point) (point-max))
533 (and to (mail-position-on-field "To"))
534 (delete-other-windows)
535 (kill-buffer tmp-buf)))
536 (setq buffer-read-only nil) ;;??
537 (run-hooks 'wl-draft-reedit-hook))
539 (defun wl-draft-insert-current-message (dummy)
541 (let (mail-reply-buffer
542 mail-citation-hook mail-yank-hooks
543 wl-draft-add-references wl-draft-cite-function)
544 (with-current-buffer wl-draft-buffer-cur-summary-buffer
545 (with-current-buffer wl-message-buffer
546 (setq mail-reply-buffer (wl-message-get-original-buffer))))
548 (with-current-buffer mail-reply-buffer
550 (error "No current message")
551 (wl-draft-yank-from-mail-reply-buffer
553 wl-ignored-forwarded-headers))))
555 (defun wl-draft-insert-get-message (dummy)
556 (let ((fld (completing-read
558 (if (memq 'read-folder wl-use-folder-petname)
559 (wl-folder-get-entity-with-petname)
560 wl-folder-entity-hashtb)
561 nil nil wl-default-spec
562 'wl-read-folder-hist))
563 (number (call-interactively
564 (function (lambda (num)
565 (interactive "nNumber: ")
567 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
568 mail-citation-hook mail-yank-hooks
569 wl-draft-cite-function)
572 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
575 (elmo-make-fetch-strategy 'entire)
576 nil mail-reply-buffer)
577 (wl-draft-yank-from-mail-reply-buffer nil))
578 (kill-buffer mail-reply-buffer))))
581 ;; default body citation func
583 (defun wl-default-draft-cite ()
584 (let ((mail-yank-ignored-headers "[^:]+:")
585 (mail-yank-prefix "> ")
586 (summary-buf wl-current-summary-buffer)
587 (message-buf (get-buffer (wl-current-message-buffer)))
588 from date cite-title num entity)
589 (setq date (std11-fetch-field "date"))
591 (buffer-live-p summary-buf)
593 (buffer-live-p message-buf))
596 (set-buffer summary-buf)
599 (set-buffer message-buf)
600 wl-message-buffer-cur-number))
601 (setq entity (elmo-msgdb-overview-get-entity
602 num (wl-summary-buffer-msgdb)))
603 (setq from (elmo-msgdb-overview-entity-get-from entity)))
604 (setq cite-title (format "At %s,\n%s wrote:"
605 (or date "some time ago")
606 (wl-summary-from-func-internal
609 (insert cite-title "\n"))
610 (mail-indent-citation)))
612 (defvar wl-draft-buffer nil "Draft buffer to yank content")
613 (defun wl-draft-yank-to-draft-buffer (buffer)
614 "Yank BUFFER content to `wl-draft-buffer'."
615 (set-buffer wl-draft-buffer)
616 (let ((mail-reply-buffer buffer))
617 (wl-draft-yank-from-mail-reply-buffer nil)
618 (kill-buffer buffer)))
620 (defun wl-draft-yank-original (&optional arg)
621 "Yank original message."
624 (let (buf mail-reply-buffer)
627 (setq buf (current-buffer)))
628 (setq mail-reply-buffer buf)
629 (wl-draft-yank-from-mail-reply-buffer nil))
630 (wl-draft-yank-current-message-entity)))
632 (defun wl-draft-hide (editing-buffer)
633 "Hide the editing draft buffer if possible."
635 (when (and editing-buffer
636 (buffer-live-p editing-buffer))
637 (set-buffer editing-buffer)
638 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
639 fld-buf sum-win fld-win)
640 (if (and wl-draft-use-frame
641 (> (length (visible-frame-list)) 1))
647 ;; stay folder window if required
648 (when wl-stay-folder-window
649 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
650 (if (setq fld-win (get-buffer-window fld-buf))
651 (select-window fld-win)
652 (if wl-draft-resume-folder-window ;; resume folder window
653 (switch-to-buffer fld-buf)))))
654 (if (buffer-live-p sum-buf)
655 (if (setq sum-win (get-buffer-window sum-buf t))
656 ;; if Summary is on the frame, select it.
657 (select-window sum-win)
658 ;; if summary is not on the frame, switch to it.
659 (if (and wl-stay-folder-window
660 (or wl-draft-resume-folder-window fld-win))
661 (wl-folder-select-buffer sum-buf)
662 (switch-to-buffer sum-buf)))))))
664 (defun wl-draft-delete (editing-buffer)
665 "kill the editing draft buffer and delete the file corresponds to it."
668 (set-buffer editing-buffer)
669 (if wl-draft-buffer-file-name
671 (if (file-exists-p wl-draft-buffer-file-name)
672 (delete-file wl-draft-buffer-file-name))
673 (let ((msg (and wl-draft-buffer-file-name
674 (string-match "[0-9]+$" wl-draft-buffer-file-name)
676 (match-string 0 wl-draft-buffer-file-name)))))
677 (wl-draft-config-info-operation msg 'delete))))
678 (set-buffer-modified-p nil) ; force kill
679 (kill-buffer editing-buffer))))
681 (defun wl-draft-kill (&optional force-kill)
682 "Kill current draft buffer and quit editing."
685 (when (and (or (eq major-mode 'wl-draft-mode)
686 (eq major-mode 'mail-mode))
688 (y-or-n-p "Kill Current Draft?")))
689 (let ((cur-buf (current-buffer)))
690 (wl-draft-hide cur-buf)
691 (wl-draft-delete cur-buf)))
694 (defun wl-draft-fcc ()
695 "Add a new Fcc field, with file name completion."
697 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
698 (mail-position-on-field "to"))
701 ;; function for wl-sent-message-via
703 (defmacro wl-draft-sent-message-p (type)
704 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
706 (defmacro wl-draft-set-sent-message (type result &optional server-port)
707 (` (let ((element (assq (, type) wl-sent-message-via)))
709 (unless (eq (nth 1 element) (, result))
710 (setcdr element (list (, result) (, server-port)))
711 (setq wl-sent-message-modified t))
712 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
713 (setq wl-sent-message-modified t)))))
715 (defun wl-draft-sent-message-results ()
716 (let ((results wl-sent-message-via)
717 unplugged-via sent-via)
719 (cond ((eq (nth 1 (car results)) 'unplugged)
720 (push (caar results) unplugged-via))
721 ((eq (nth 1 (car results)) 'sent)
722 (push (caar results) sent-via)))
723 (setq results (cdr results)))
724 (list unplugged-via sent-via)))
726 (defun wl-draft-write-sendlog (status proto server to id)
727 "Write send log file, if `wl-draft-sendlog' is non-nil."
728 (when wl-draft-sendlog
730 (let* ((tmp-buf (get-buffer-create " *wl-draft-sendlog*"))
731 (filename (expand-file-name wl-draft-sendlog-filename
733 (filesize (nth 7 (file-attributes filename)))
734 (server (if server (concat " server=" server) ""))
736 ((memq proto '(fcc queue))
737 (format " folder=\"%s\"" to))
739 (format " ng=<%s>" to))
744 (mapcar '(lambda(x) (format "<%s>" x)) to)
747 (id (if id (concat " id=" id) ""))
748 (time (wl-sendlog-time)))
751 (insert (format "%s proto=%s stat=%s%s%s%s\n"
752 time proto status server to id))
753 (if (and wl-draft-sendlog-max-size filesize
754 (> filesize wl-draft-sendlog-max-size))
755 (rename-file filename (concat filename ".old") t))
756 (if (file-writable-p filename)
757 (write-region (point-min) (point-max)
759 (message (format "%s is not writable." filename)))
760 (kill-buffer tmp-buf)))))
762 (defun wl-draft-get-header-delimiter (&optional delete)
763 ;; If DELETE is non-nil, replace the header delimiter with a blank line
765 (goto-char (point-min))
766 (when (re-search-forward
767 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
771 (setq delimline (point-marker)))
774 (defun wl-draft-send-mail-with-qmail ()
775 "Pass the prepared message buffer to qmail-inject.
776 Refer to the documentation for the variable `send-mail-function'
777 to find out how to use this."
778 (if (and wl-draft-qmail-send-plugged
779 (not (elmo-plugged-p)))
780 (wl-draft-set-sent-message 'mail 'unplugged)
782 (let ((id (std11-field-body "Message-ID"))
783 (to (std11-field-body "To")))
787 'call-process-region 1 (point-max) wl-qmail-inject-program
789 wl-qmail-inject-args))
790 ;; qmail-inject doesn't say anything on it's stdout/stderr,
791 ;; we have to look at the retval instead
793 (wl-draft-set-sent-message 'mail 'sent)
794 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
795 (1 (error "qmail-inject reported permanent failure"))
796 (111 (error "qmail-inject reported transient failure"))
797 ;; should never happen
798 (t (error "qmail-inject reported unknown failure"))))))
800 (defun wl-draft-parse-msg-id-list-string (string)
801 "Get msg-id list from STRING."
803 (dolist (parsed-id (std11-parse-msg-ids-string string))
804 (when (eq (car parsed-id) 'msg-id)
805 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
807 (nreverse msg-id-list)))
809 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
810 "Get mailbox list of FIELD from current buffer.
811 The buffer is expected to be narrowed to just the headers of the message.
812 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
813 from current buffer."
815 (let ((case-fold-search t)
816 (inhibit-read-only t)
818 mailbox-list beg seq has-group-list)
819 (goto-char (point-min))
820 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
823 (re-search-forward "^[^ \t]" nil 'move)
825 (skip-chars-backward "\n")
826 (setq seq (std11-lexical-analyze
827 (buffer-substring-no-properties beg (point))))
828 (setq addresses (std11-parse-addresses seq))
830 (cond ((eq (car (car addresses)) 'group)
831 (setq has-group-list t)
835 'std11-address-string
836 (nth 2 (car addresses))))))
837 ((eq (car (car addresses)) 'mailbox)
838 (setq address (nth 1 (car addresses)))
842 (std11-addr-to-string
843 (if (eq (car address) 'phrase-route-addr)
846 (setq addresses (cdr addresses)))
847 (when (and remove-group-list has-group-list)
848 (delete-region beg (point))
849 (insert (wl-address-string-without-group-list-contents seq))))
852 (defun wl-draft-deduce-address-list (buffer header-start header-end)
853 "Get address list suitable for smtp RCPT TO:<address>.
854 Group list content is removed if `wl-draft-remove-group-list-contents' is
856 (let ((fields '("to" "cc" "bcc"))
857 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
862 (narrow-to-region header-start header-end)
863 (goto-char (point-min))
865 (if (re-search-forward "^resent-to[\t ]*:" nil t)
866 (setq fields resent-fields)))
870 (wl-draft-parse-mailbox-list
872 wl-draft-remove-group-list-contents)))
873 (setq fields (cdr fields)))
879 (defun wl-draft-send-mail-with-smtp ()
880 "Send the prepared message buffer with SMTP."
882 (let* ((errbuf (if mail-interactive
883 (generate-new-buffer " smtp errors")
886 (default-case-fold-search t)
887 (sender (or wl-envelope-from
888 (wl-address-header-extract-address wl-from)))
889 (delimline (save-excursion
890 (goto-char (point-min))
892 (concat "^" (regexp-quote mail-header-separator)
896 (or wl-smtp-posting-server
897 ;; Compatibility stuff for FLIM 1.12.5 or earlier.
898 ;; They don't accept a function as the value of `smtp-server'.
899 (if (functionp smtp-server)
904 (let (wl-draft-remove-group-list-contents)
905 (wl-draft-deduce-address-list
906 (current-buffer) (point-min) delimline)))
907 (or smtp-server "localhost"))))
908 (smtp-service (or wl-smtp-posting-port smtp-service))
909 (smtp-local-domain (or smtp-local-domain wl-local-domain))
910 (id (std11-field-body "message-id"))
912 (if (not (elmo-plugged-p smtp-server smtp-service))
913 (wl-draft-set-sent-message 'mail 'unplugged
914 (cons smtp-server smtp-service))
917 ;; Instead of `smtp-deduce-address-list'.
918 (setq recipients (wl-draft-deduce-address-list
919 (current-buffer) (point-min) delimline))
920 (unless recipients (error "No recipients"))
921 ;; Insert an extra newline if we need it to work around
922 ;; Sun's bug that swallows newlines.
923 (goto-char (1+ delimline))
924 (if (eval mail-mailer-swallows-blank-line)
926 ;;; (run-hooks 'wl-mail-send-pre-hook)
931 (wl-draft-delete-field "bcc" delimline)
932 (wl-draft-delete-field "resent-bcc" delimline)
933 (let (process-connection-type)
936 (wl-smtp-extension-bind
938 (smtp-send-buffer sender recipients (current-buffer))
940 (wl-draft-write-sendlog 'failed 'smtp smtp-server
942 (signal (car err) (cdr err)))))
943 (wl-draft-set-sent-message 'mail 'sent)
944 (wl-draft-write-sendlog
945 'ok 'smtp smtp-server recipients id)))))
947 (kill-buffer errbuf))))))
949 (defun wl-draft-send-mail-with-pop-before-smtp ()
950 "Send the prepared message buffer with POP-before-SMTP."
953 (let ((session (elmo-pop3-get-session
955 (or wl-pop-before-smtp-user
956 elmo-pop3-default-user)
957 (or wl-pop-before-smtp-authenticate-type
958 elmo-pop3-default-authenticate-type)
959 (or wl-pop-before-smtp-server
960 elmo-pop3-default-server)
961 (or wl-pop-before-smtp-port
962 elmo-pop3-default-port)
963 (or wl-pop-before-smtp-stream-type
964 elmo-pop3-default-stream-type)))))
965 (when session (elmo-network-close-session session)))
967 (wl-draft-send-mail-with-smtp))
969 (defun wl-draft-insert-required-fields (&optional force-msgid)
970 "Insert Message-ID, Date, and From field.
971 If FORCE-MSGID, ignore 'wl-insert-message-id'."
972 ;; Insert Message-Id field...
973 (goto-char (point-min))
974 (when (and (or force-msgid
975 wl-insert-message-id)
976 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
977 (insert (concat "Message-ID: "
978 (wl-draft-make-message-id-string)
980 ;; Insert date field.
981 (goto-char (point-min))
982 (or (re-search-forward "^Date[ \t]*:" nil t)
983 (wl-draft-insert-date-field))
984 ;; Insert from field.
985 (goto-char (point-min))
986 (or (re-search-forward "^From[ \t]*:" nil t)
987 (wl-draft-insert-from-field)))
989 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
990 "Send the message in the current buffer."
992 (std11-narrow-to-header mail-header-separator)
993 (wl-draft-insert-required-fields)
994 ;; Delete null fields.
995 (goto-char (point-min))
996 (while (re-search-forward "^[^ \t\n:]+:[ \t]*\n" nil t)
998 ;; ignore any blank lines in the header
999 (while (re-search-forward "\n\n\n*" nil t)
1000 (replace-match "\n")))
1001 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1002 (wl-draft-dispatch-message)
1003 (when kill-when-done
1004 ;; hide editing-buffer.
1005 (wl-draft-hide editing-buffer)
1006 ;; delete editing-buffer and its file.
1007 (wl-draft-delete editing-buffer)))
1009 (defun wl-draft-dispatch-message (&optional mes-string)
1010 "Send the message in the current buffer. Not modified the header fields."
1012 (if (and wl-draft-verbose-send mes-string)
1013 (message mes-string))
1015 (setq delimline (wl-draft-get-header-delimiter t))
1016 (unless wl-draft-fcc-list
1017 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1019 (setq wl-sent-message-modified nil)
1022 (if (and (wl-message-mail-p)
1023 (not (wl-draft-sent-message-p 'mail)))
1024 (funcall wl-draft-send-mail-function))
1025 (if (and (wl-message-news-p)
1026 (not (wl-draft-sent-message-p 'news))
1027 (not (wl-message-field-exists-p "Resent-to")))
1028 (funcall wl-draft-send-news-function)))
1030 (let* ((status (wl-draft-sent-message-results))
1031 (unplugged-via (car status))
1032 (sent-via (nth 1 status)))
1033 ;; If one sent, process fcc folder.
1034 (if (and sent-via wl-draft-fcc-list)
1036 (wl-draft-do-fcc (wl-draft-get-header-delimiter) wl-draft-fcc-list)
1037 (setq wl-draft-fcc-list nil)))
1038 (if wl-draft-use-cache
1039 (let ((id (std11-field-body "Message-ID"))
1040 (elmo-enable-disconnected-operation t))
1041 (elmo-file-cache-save id nil)))
1042 ;; If one unplugged, append queue.
1043 (when (and unplugged-via
1044 wl-sent-message-modified)
1045 (if wl-draft-enable-queuing
1046 (wl-draft-queue-append wl-sent-message-via)
1047 (error "Unplugged")))
1048 (when wl-draft-verbose-send
1049 (if (and unplugged-via sent-via);; combined message
1051 (setq wl-draft-verbose-msg
1052 (format "Sending%s and Queuing%s..."
1053 sent-via unplugged-via))
1054 (message (concat wl-draft-verbose-msg "done")))
1056 (message (concat mes-string
1057 (if sent-via "done" "failed")))))))))
1058 (not wl-sent-message-modified)) ;; return value
1060 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1061 "Force send current buffer as raw message."
1064 (let (wl-interactive-send
1065 ;;; wl-draft-verbose-send
1066 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1067 ;;; wl-news-send-pre-hook
1070 (wl-draft-send kill-when-done mes-string))))
1072 (defun wl-draft-clone-local-variables ()
1073 (let ((locals (buffer-local-variables))
1076 (when (and (consp (car locals))
1078 (string-match wl-draft-clone-local-variable-regexp
1079 (symbol-name (car (car locals)))))
1080 (wl-append result (list (car (car locals)))))
1081 (setq locals (cdr locals)))
1084 (defun wl-draft-send (&optional kill-when-done mes-string)
1085 "Send current draft message.
1086 If optional argument is non-nil, current draft buffer is killed"
1088 ;; Don't call this explicitly.
1089 ;; Added to 'wl-draft-send-hook (by teranisi)
1090 ;; (wl-draft-config-exec)
1091 (run-hooks 'wl-draft-send-hook)
1092 (when (or (not wl-interactive-send)
1093 (y-or-n-p "Send current draft. OK?"))
1094 (let ((send-mail-function 'wl-draft-raw-send)
1095 (editing-buffer (current-buffer))
1096 (sending-buffer (wl-draft-generate-clone-buffer
1097 " *wl-draft-sending-buffer*"
1098 (append wl-draft-config-variables
1099 (wl-draft-clone-local-variables))))
1100 (wl-draft-verbose-msg nil)
1103 (save-excursion (set-buffer sending-buffer)
1104 (if (and (not (wl-message-mail-p))
1105 (not (wl-message-news-p)))
1106 (error "No recipient is specified"))
1107 (expand-abbrev) ; for mail-abbrevs
1108 (run-hooks 'mail-send-hook) ; translate buffer
1109 (if wl-draft-verbose-send
1110 (message (or mes-string "Sending...")))
1111 (funcall wl-draft-send-function editing-buffer kill-when-done)
1112 ;; Now perform actions on successful sending.
1113 (while mail-send-actions
1115 (apply (car (car mail-send-actions))
1116 (cdr (car mail-send-actions)))
1118 (setq mail-send-actions (cdr mail-send-actions)))
1119 (if (or (eq major-mode 'wl-draft-mode)
1120 (eq major-mode 'mail-mode))
1121 (local-set-key "\C-c\C-s" 'wl-draft-send)) ; override
1122 (if wl-draft-verbose-send
1123 (message (concat (or wl-draft-verbose-msg
1124 mes-string "Sending...")
1126 ;; kill sending buffer, anyway.
1127 (and (buffer-live-p sending-buffer)
1128 (kill-buffer sending-buffer))))))
1130 (defun wl-draft-save ()
1131 "Save current draft."
1134 (wl-draft-config-info-operation
1135 (and (string-match "[0-9]+$" wl-draft-buffer-file-name)
1137 (match-string 0 wl-draft-buffer-file-name)))
1140 (defun wl-draft-mimic-kill-buffer ()
1141 "Kill the current (draft) buffer with query."
1143 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1146 (if (or (not bufname)
1147 (string-equal bufname "")
1148 (string-equal bufname (buffer-name)))
1149 (wl-draft-save-and-exit)
1150 (kill-buffer bufname))))
1152 (defun wl-draft-save-and-exit ()
1153 "Save current draft and exit current draft mode."
1156 (let ((editing-buffer (current-buffer)))
1157 (wl-draft-hide editing-buffer)
1158 (kill-buffer editing-buffer)))
1160 (defun wl-draft-send-and-exit ()
1161 "Send current draft message and kill it."
1165 (defun wl-draft-send-from-toolbar ()
1167 (let ((wl-interactive-send t))
1168 (wl-draft-send-and-exit)))
1170 (defun wl-draft-delete-field (field &optional delimline)
1171 (wl-draft-delete-fields (regexp-quote field) delimline))
1173 (defun wl-draft-delete-fields (regexp &optional delimline)
1176 (if (search-forward "\n\n" nil t)
1177 (setq delimline (point))
1178 (setq delimline (point-max))))
1179 (narrow-to-region (point-min) delimline)
1180 (goto-char (point-min))
1181 (let ((regexp (concat "^" regexp ":"))
1182 (case-fold-search t)
1185 (if (looking-at regexp)
1191 (if (re-search-forward "^[^ \t]" nil t)
1192 (goto-char (match-beginning 0))
1195 (if (re-search-forward "^[^ \t]" nil t)
1196 (goto-char (match-beginning 0))
1199 (defun wl-draft-get-fcc-list (header-end)
1201 (case-fold-search t))
1202 (or (markerp header-end) (error "header-end must be a marker"))
1204 (goto-char (point-min))
1205 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1207 (cons (buffer-substring-no-properties
1211 (skip-chars-backward " \t")
1215 (wl-folder-confirm-existence
1216 (wl-folder-get-elmo-folder (eword-decode-string (car fcc-list)))))
1217 (delete-region (match-beginning 0)
1218 (progn (forward-line 1) (point)))))
1221 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1222 (let ((send-mail-buffer (current-buffer))
1223 (tembuf (generate-new-buffer " fcc output"))
1224 (case-fold-search t)
1226 (or (markerp header-end) (error "header-end must be a marker"))
1229 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1232 ;; insert just the headers to avoid moving the gap more than
1233 ;; necessary (the message body could be arbitrarily huge.)
1234 (insert-buffer-substring send-mail-buffer 1 header-end)
1235 (wl-draft-insert-required-fields t)
1236 (goto-char (point-max))
1237 (insert-buffer-substring send-mail-buffer header-end)
1238 (let ((id (std11-field-body "Message-ID"))
1239 (elmo-enable-disconnected-operation t)
1242 (unless (or cache-saved
1243 (elmo-folder-plugged-p
1244 (wl-folder-get-elmo-folder (car fcc-list))))
1245 (elmo-file-cache-save id nil) ;; for disconnected operation
1246 (setq cache-saved t))
1247 (if (elmo-folder-append-buffer
1248 (wl-folder-get-elmo-folder
1249 (eword-decode-string (car fcc-list)))
1251 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1252 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1253 (setq fcc-list (cdr fcc-list)))))
1254 (kill-buffer tembuf)))
1256 (defun wl-draft-on-field-p ()
1259 (goto-char (point-min))
1260 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1267 (if (or (looking-at ".*,[ \t]?$")
1268 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1270 (let ((pos (point)))
1273 (if (looking-at "^[ \t]")
1275 (if (re-search-forward ":" pos t) nil t)))))))
1277 (defun wl-draft-random-alphabet ()
1278 (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)))
1279 (nth (abs (% (random) 26)) alphabet)))
1282 (defun wl-draft (&optional to subject in-reply-to cc references newsgroups
1284 content-type content-transfer-encoding
1285 body edit-again summary-buf)
1286 "Write and send mail/news message with Wanderlust."
1288 (unless (featurep 'wl)
1292 (wl-init) ;; returns immediately if already initialized.
1294 (setq summary-buf (wl-summary-get-buffer (wl-summary-buffer-folder-name))))
1295 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1296 buf-name file-name num wl-demo change-major-mode-hook)
1297 (if (not (elmo-folder-message-file-p draft-folder))
1298 (error "%s folder cannot be used for draft folder" wl-draft-folder))
1299 (setq num (elmo-max-of-list
1300 (or (elmo-folder-list-messages draft-folder) '(0))))
1301 (setq num (+ 1 num))
1302 ;; To get unused buffer name.
1303 (while (get-buffer (concat wl-draft-folder "/" (int-to-string num)))
1304 (setq num (+ 1 num)))
1305 (setq buf-name (find-file-noselect
1307 (elmo-message-file-name
1308 (wl-folder-get-elmo-folder wl-draft-folder)
1310 (if wl-draft-use-frame
1311 (switch-to-buffer-other-frame buf-name)
1312 (switch-to-buffer buf-name))
1313 (set-buffer buf-name)
1314 (if (not (string-match (regexp-quote wl-draft-folder)
1316 (rename-buffer (concat wl-draft-folder "/" (int-to-string num))))
1317 (if (or (eq wl-draft-reply-buffer-style 'full)
1318 (eq this-command 'wl-draft)
1319 (eq this-command 'wl-summary-write)
1320 (eq this-command 'wl-summary-write-current-newsgroup))
1321 (delete-other-windows))
1324 (setq wl-sent-message-via nil)
1325 (if (stringp wl-from)
1326 (insert "From: " wl-from "\n"))
1327 (and (or (interactive-p)
1328 (eq this-command 'wl-summary-write)
1330 (insert "To: " (or to "") "\n"))
1331 (and cc (insert "Cc: " (or cc "") "\n"))
1332 (insert "Subject: " (or subject "") "\n")
1333 (and newsgroups (insert "Newsgroups: " newsgroups "\n"))
1334 (and mail-followup-to (insert "Mail-Followup-To: " mail-followup-to "\n"))
1335 (and wl-insert-mail-reply-to
1336 (insert "Mail-Reply-To: "
1337 (wl-address-header-extract-address
1339 (and in-reply-to (insert "In-Reply-To: " in-reply-to "\n"))
1340 (and references (insert "References: " references "\n"))
1341 (insert (funcall wl-generate-mailer-string-function) "\n")
1342 (setq wl-draft-buffer-file-name file-name)
1343 (if mail-default-reply-to
1344 (insert "Reply-To: " mail-default-reply-to "\n"))
1345 (wl-draft-insert-ccs "Bcc: " (or wl-bcc
1346 (and mail-self-blind (user-login-name))))
1347 (wl-draft-insert-ccs "Fcc: " wl-fcc)
1349 (insert "Organization: " wl-organization "\n"))
1350 (and wl-auto-insert-x-face
1351 (file-exists-p wl-x-face-file)
1352 (wl-draft-insert-x-face-field-here))
1353 (if mail-default-headers
1354 (insert mail-default-headers))
1355 (if (not (= (preceding-char) ?\n))
1359 (setq start (point))
1361 (insert "Content-type: " content-type "\n"))
1362 (when content-transfer-encoding
1363 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1364 (if (or content-type content-transfer-encoding)
1366 (and body (insert body))
1368 (narrow-to-region start (point))
1370 (wl-draft-decode-message-in-buffer))
1373 (put-text-property (point)
1375 (insert mail-header-separator "\n")
1377 'category 'mail-header-separator)))
1378 (put-text-property (point)
1380 (insert mail-header-separator "\n")
1382 'category 'mail-header-separator)
1383 (and body (insert body)))
1385 (push-mark (point) t)
1386 (push-mark (point) t t))
1387 (as-binary-output-file
1388 (write-region (point-min)(point-max) wl-draft-buffer-file-name
1390 (wl-draft-editor-mode)
1391 (wl-draft-overload-functions)
1392 (wl-highlight-headers 'for-draft)
1393 (goto-char (point-min))
1394 (setq wl-draft-config-exec-flag t)
1396 (run-hooks 'wl-mail-setup-hook))
1397 (wl-user-agent-compose-internal) ;; user-agent
1398 (cond ((eq this-command 'wl-summary-write-current-newsgroup)
1399 (mail-position-on-field "Subject"))
1400 ((and (interactive-p) (null to))
1401 (mail-position-on-field "To"))
1403 (goto-char (point-max))))
1404 (setq wl-draft-buffer-cur-summary-buffer (or summary-buf
1406 wl-summary-buffer-name)))
1409 (defsubst wl-draft-insert-ccs (str cc)
1415 (null (and wl-draft-delete-myself-from-bcc-fcc
1417 (mapcar 'wl-address-header-extract-address
1419 (wl-parse-addresses (std11-field-body "To"))
1420 (wl-parse-addresses (std11-field-body "Cc"))))
1421 (mapcar 'downcase wl-subscribed-mailing-list)))))
1422 (insert str field "\n"))))
1424 (defun wl-draft-elmo-nntp-send ()
1425 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1426 (elmo-nntp-default-user
1427 (or wl-nntp-posting-user elmo-nntp-default-user))
1428 (elmo-nntp-default-server
1429 (or wl-nntp-posting-server elmo-nntp-default-server))
1430 (elmo-nntp-default-port
1431 (or wl-nntp-posting-port elmo-nntp-default-port))
1432 (elmo-nntp-default-stream-type
1433 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type)))
1434 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1435 (wl-draft-set-sent-message 'news 'unplugged
1436 (cons elmo-nntp-default-server
1437 elmo-nntp-default-port))
1438 (elmo-nntp-post elmo-nntp-default-server (current-buffer))
1439 (wl-draft-set-sent-message 'news 'sent)
1440 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1441 (std11-field-body "Newsgroups")
1442 (std11-field-body "Message-ID")))))
1444 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1445 "generate clone of current buffer named NAME."
1446 (let ((editing-buffer (current-buffer)))
1448 (set-buffer (generate-new-buffer name))
1451 (wl-draft-editor-mode)
1452 (insert-buffer editing-buffer)
1454 (while local-variables
1455 (make-local-variable (car local-variables))
1456 (set (car local-variables)
1458 (set-buffer editing-buffer)
1459 (symbol-value (car local-variables))))
1460 (setq local-variables (cdr local-variables)))
1463 (defun wl-draft-reedit (number)
1464 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1466 buf-name file-name change-major-mode-hook)
1467 (setq file-name (elmo-message-file-name draft-folder number))
1468 (unless (file-exists-p file-name)
1469 (error "File %s does not exist" file-name))
1470 (setq buf-name (find-file-noselect file-name))
1471 (if wl-draft-use-frame
1472 (switch-to-buffer-other-frame buf-name)
1473 (switch-to-buffer buf-name))
1474 (set-buffer buf-name)
1475 (if (not (string-match (regexp-quote wl-draft-folder)
1477 (rename-buffer (concat wl-draft-folder "/" (buffer-name))))
1480 (setq wl-sent-message-via nil)
1481 (setq wl-draft-buffer-file-name file-name)
1482 (wl-draft-config-info-operation number 'load)
1483 (goto-char (point-min))
1484 (or (re-search-forward "\n\n" nil t)
1485 (search-forward (concat mail-header-separator "\n") nil t))
1487 (push-mark (point) t)
1488 (push-mark (point) t t))
1489 (write-region (point-min)(point-max) wl-draft-buffer-file-name
1491 (wl-draft-overload-functions)
1492 (wl-draft-editor-mode)
1493 (wl-highlight-headers 'for-draft)
1494 (run-hooks 'wl-draft-reedit-hook)
1495 (goto-char (point-max))
1499 (defmacro wl-draft-body-goto-top ()
1501 (goto-char (point-min))
1502 (if (re-search-forward mail-header-separator nil t)
1504 (goto-char (point-max))))))
1506 (defmacro wl-draft-body-goto-bottom ()
1507 (` (goto-char (point-max))))
1509 (defmacro wl-draft-config-body-goto-header ()
1511 (goto-char (point-min))
1512 (if (re-search-forward mail-header-separator nil t)
1514 (goto-char (point-max))))))
1516 (defun wl-draft-config-sub-body (content)
1517 (wl-draft-body-goto-top)
1518 (delete-region (point) (point-max))
1519 (if content (insert (eval content))))
1521 (defun wl-draft-config-sub-top (content)
1522 (wl-draft-body-goto-top)
1523 (if content (insert (eval content))))
1525 (defun wl-draft-config-sub-bottom (content)
1526 (wl-draft-body-goto-bottom)
1527 (if content (insert (eval content))))
1529 (defun wl-draft-config-sub-header (content)
1530 (wl-draft-config-body-goto-header)
1531 (if content (insert (concat (eval content) "\n"))))
1533 (defsubst wl-draft-config-sub-file (content)
1534 (let ((coding-system-for-read wl-cs-autoconv)
1535 (file (expand-file-name (eval content))))
1536 (if (file-exists-p file)
1537 (insert-file-contents file)
1538 (error "%s: no exists file" file))))
1540 (defun wl-draft-config-sub-body-file (content)
1541 (wl-draft-body-goto-top)
1542 (delete-region (point) (point-max))
1543 (wl-draft-config-sub-file content))
1545 (defun wl-draft-config-sub-top-file (content)
1546 (wl-draft-body-goto-top)
1547 (wl-draft-config-sub-file content))
1549 (defun wl-draft-config-sub-bottom-file (content)
1550 (wl-draft-body-goto-bottom)
1551 (wl-draft-config-sub-file content))
1553 (defun wl-draft-config-sub-header-file (content)
1554 (wl-draft-config-body-goto-header)
1555 (wl-draft-config-sub-file content))
1557 (defun wl-draft-config-sub-template (content)
1558 (setq wl-draft-config-variables
1559 (wl-template-insert (eval content))))
1561 (defun wl-draft-config-sub-x-face (content)
1562 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
1563 (fboundp 'x-face-insert)) ; x-face.el is installed.
1564 (x-face-insert content)
1565 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
1567 (defsubst wl-draft-config-sub-func (field content)
1569 (if (setq func (assq field wl-draft-config-sub-func-alist))
1570 (let (wl-draft-config-variables)
1571 (funcall (cdr func) content)
1572 ;; for wl-draft-config-sub-template
1573 (cons t wl-draft-config-variables)))))
1575 (defsubst wl-draft-config-exec-sub (clist)
1576 (let (config local-variables)
1578 (setq config (car clist))
1581 (let ((field (car config))
1582 (content (cdr config))
1586 (wl-draft-replace-field field (eval content) t))
1587 ((setq ret-val (wl-draft-config-sub-func field content))
1588 (if (cdr ret-val) ;; for wl-draft-config-sub-template
1589 (wl-append local-variables (cdr ret-val))))
1590 ((boundp field) ;; variable
1591 (make-local-variable field)
1592 (set field (eval content))
1593 (wl-append local-variables (list field)))
1595 (error "%s: not variable" field)))))
1596 ((or (functionp config)
1597 (and (symbolp config)
1601 (error "%s: not supported type" config)))
1602 (setq clist (cdr clist)))
1605 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
1606 "Change headers in draft preparation time."
1608 (unless wl-draft-reedit
1611 (and (boundp 'wl-draft-prepared-config-alist)
1612 wl-draft-prepared-config-alist) ;; For compatible.
1613 wl-draft-config-alist)))
1615 (wl-draft-config-exec config-alist reply-buf)))))
1617 (defun wl-draft-config-exec (&optional config-alist reply-buf)
1618 "Change headers in draft sending time."
1620 (let ((case-fold-search t)
1621 (alist (or config-alist wl-draft-config-alist))
1622 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
1623 wl-draft-reply-buffer)))
1624 (local-variables wl-draft-config-variables)
1626 (when (and (or (interactive-p)
1627 wl-draft-config-exec-flag)
1632 (setq key (caar alist)
1639 (set-buffer reply-buf)
1641 (std11-narrow-to-header)
1642 (goto-char (point-min))
1643 (re-search-forward (car clist) nil t))))
1644 (wl-draft-config-exec-sub (cdr clist))
1647 (when (save-restriction
1648 (std11-narrow-to-header mail-header-separator)
1649 (goto-char (point-min))
1650 (re-search-forward key nil t))
1651 (wl-append local-variables
1652 (wl-draft-config-exec-sub clist))
1655 (wl-append local-variables
1656 (wl-draft-config-exec-sub clist))
1658 (if (and found wl-draft-config-matchone)
1660 (setq alist (cdr alist))))
1662 (setq wl-draft-config-exec-flag nil))
1663 (run-hooks 'wl-draft-config-exec-hook)
1664 (put-text-property (point-min)(point-max) 'face nil)
1665 (wl-highlight-message (point-min)(point-max) t)
1666 (setq wl-draft-config-variables
1667 (elmo-uniq-list local-variables))))))
1669 (defun wl-draft-replace-field (field content &optional add)
1672 (let ((case-fold-search t)
1673 (inhibit-read-only t) ;; added by teranisi.
1675 (std11-narrow-to-header mail-header-separator)
1676 (goto-char (point-min))
1677 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
1682 (re-search-forward "^[^ \t]" nil 'move)
1684 (skip-chars-backward "\n")
1685 (delete-region beg (point))
1686 (insert " " content))
1691 (re-search-forward "^[^ \t]" nil 'move)
1693 (delete-region beg (point)))
1694 (when (and add content)
1696 (goto-char (point-max))
1697 (insert (concat field ": " content "\n"))))))))
1699 (defun wl-draft-config-info-operation (msg operation)
1700 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-folder-get-elmo-folder
1704 (format "%s-%d" wl-draft-config-save-filename msg)
1706 element alist variable)
1708 ((eq operation 'save)
1709 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
1710 (while (setq variable (pop variables))
1711 (when (boundp variable)
1713 (list (cons variable (eval variable))))))
1714 (elmo-object-save filename alist)))
1715 ((eq operation 'load)
1716 (setq alist (elmo-object-load filename))
1717 (while (setq element (pop alist))
1718 (set (make-local-variable (car element)) (cdr element))
1719 (wl-append wl-draft-config-variables (list (car element)))))
1720 ((eq operation 'delete)
1721 (if (file-exists-p filename)
1722 (delete-file filename))))))
1724 (defun wl-draft-queue-info-operation (msg operation
1725 &optional add-sent-message-via)
1726 (let* ((msgdb-dir (elmo-folder-msgdb-path
1727 (wl-folder-get-elmo-folder wl-queue-folder)))
1730 (format "%s-%d" wl-draft-queue-save-filename msg)
1732 element alist variable)
1734 ((eq operation 'save)
1735 (let ((variables (elmo-uniq-list
1736 (append wl-draft-queue-save-variables
1737 wl-draft-config-variables
1738 (list 'wl-draft-fcc-list)))))
1739 (if add-sent-message-via
1740 (push 'wl-sent-message-via variables))
1741 (while (setq variable (pop variables))
1742 (when (boundp variable)
1744 (list (cons variable (eval variable))))))
1745 (elmo-object-save filename alist)))
1746 ((eq operation 'load)
1747 (setq alist (elmo-object-load filename))
1748 (while (setq element (pop alist))
1749 (set (make-local-variable (car element)) (cdr element))))
1750 ((eq operation 'get-sent-via)
1751 (setq alist (elmo-object-load filename))
1752 (cdr (assq 'wl-sent-message-via alist)))
1753 ((eq operation 'delete)
1754 (if (file-exists-p filename)
1755 (delete-file filename))))))
1757 (defun wl-draft-queue-append (wl-sent-message-via)
1758 (if wl-draft-verbose-send
1759 (message "Queuing..."))
1760 (let ((send-buffer (current-buffer))
1761 (folder (wl-folder-get-elmo-folder wl-queue-folder))
1762 (message-id (std11-field-body "Message-ID")))
1763 (if (elmo-folder-append-buffer folder t)
1765 (wl-draft-queue-info-operation
1766 (car (elmo-folder-status folder))
1767 'save wl-sent-message-via)
1768 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
1769 (when wl-draft-verbose-send
1770 (setq wl-draft-verbose-msg "Queuing...")
1771 (message "Queuing...done")))
1772 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
1773 (error "Queuing failed"))))
1775 (defun wl-draft-queue-flush ()
1776 "Flush draft queue."
1778 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
1779 (msgs2 (elmo-folder-list-messages queue-folder))
1782 (wl-draft-queue-flushing t)
1783 msgs failure len buffer msgid sent-via)
1784 ;; get plugged send message
1786 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
1789 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
1791 (car (nth 2 (car sent-via)))
1792 (cdr (nth 2 (car sent-via)))))
1793 (wl-append msgs (list (car msgs2)))
1795 (setq sent-via (cdr sent-via))))
1796 (setq msgs2 (cdr msgs2)))
1797 (when (> (setq len (length msgs)) 0)
1798 (if (elmo-y-or-n-p (format
1799 "%d message(s) are in the sending queue. Send now?"
1801 (not elmo-dop-flush-confirm) t)
1804 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
1807 ;; reset buffer local variables
1808 (kill-all-local-variables)
1812 (setq wl-sent-message-via nil)
1813 (wl-draft-queue-info-operation (car msgs) 'load)
1814 (elmo-message-fetch queue-folder
1816 (elmo-make-fetch-strategy 'entire)
1817 nil (current-buffer))
1819 (setq failure (funcall
1820 wl-draft-queue-flush-send-function
1821 (format "Sending (%d/%d)..." i len)))
1822 ;;; (wl-draft-raw-send nil nil
1823 ;;; (format "Sending (%d/%d)..." i len))
1825 (elmo-display-error err t)
1830 (elmo-folder-delete-messages
1831 queue-folder (cons (car msgs) nil))
1832 (wl-draft-queue-info-operation (car msgs) 'delete)
1833 (setq performed (+ 1 performed)))
1834 (setq msgs (cdr msgs)))
1835 (kill-buffer buffer)
1836 (message "%d message(s) are sent." performed)))
1837 (message "%d message(s) are remained to be sent." len))
1840 (defun wl-jump-to-draft-buffer (&optional arg)
1841 "Jump to the draft if exists."
1844 (wl-jump-to-draft-folder)
1845 (let ((bufs (buffer-list))
1846 (draft-regexp (concat
1848 (elmo-localdir-folder-directory-internal
1849 (wl-folder-get-elmo-folder wl-draft-folder)))))
1853 (setq buf (buffer-file-name (car bufs)))
1854 (string-match draft-regexp buf))
1855 (setq draft-bufs (cons (buffer-name (car bufs)) draft-bufs)))
1856 (setq bufs (cdr bufs)))
1859 (message "No draft buffer exist."))
1862 (sort draft-bufs (function (lambda (a b) (not (string< a b))))))
1863 (if (setq buf (cdr (member (buffer-name) draft-bufs)))
1864 (setq buf (car buf))
1865 (setq buf (car draft-bufs)))
1866 (switch-to-buffer buf))))))
1868 (defun wl-jump-to-draft-folder ()
1869 (let ((msgs (reverse (elmo-folder-list-messages (wl-folder-get-elmo-folder
1871 (mybuf (buffer-name))
1874 (message "No draft message exist.")
1875 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
1876 (setq msg (cadr (memq
1877 (string-to-int (substring mybuf (match-end 0)))
1879 (or msg (setq msg (car msgs)))
1880 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
1881 (switch-to-buffer buf)
1882 (wl-draft-reedit msg)))))
1884 (defun wl-draft-highlight-and-recenter (&optional n)
1886 (if wl-highlight-body-too
1887 (let ((beg (point-min))
1889 (put-text-property beg end 'face nil)
1890 (wl-highlight-message beg end t)))
1893 ;;;; user-agent support by Sen Nagata
1895 ;; this appears to be necessarily global...
1896 (defvar wl-user-agent-compose-p nil)
1897 (defvar wl-user-agent-headers-and-body-alist nil)
1899 ;; this should be a generic function for mail-mode -- i wish there was
1900 ;; something like it in sendmail.el
1901 (defun wl-user-agent-insert-header (header-name header-value)
1902 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
1903 ;; it seems like overriding existing headers is acceptable -- should
1904 ;; we provide an option?
1906 ;; plan was: unfold header (might be folded), remove existing value, insert
1908 ;; wl doesn't seem to fold header lines yet anyway :-)
1910 (let ((kill-whole-line t)
1912 (mail-position-on-field (capitalize header-name))
1913 (setq end-of-line (point))
1915 (re-search-forward ":" end-of-line)
1916 (insert (concat " " header-value "\n"))
1919 ;; this should be a generic function for mail-mode -- i wish there was
1920 ;; something like it in sendmail.el
1922 ;; ** haven't dealt w/ case where the body is already set **
1923 (defun wl-user-agent-insert-body (body-text)
1924 "Insert a body of text, BODY-TEXT, into a message."
1925 ;; code defensively... :-P
1926 (goto-char (point-min))
1927 (search-forward mail-header-separator)
1932 (defun wl-user-agent-compose (&optional to subject other-headers continue
1933 switch-function yank-action
1935 "Support the `compose-mail' interface for wl.
1936 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
1937 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
1938 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
1940 (unless (featurep 'wl)
1942 ;; protect these -- to and subject get bound at some point, so it looks
1943 ;; to be necessary to protect the values used w/in
1944 (let ((wl-user-agent-headers-and-body-alist other-headers)
1945 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
1946 (wl-draft-reply-buffer-style 'split))
1947 (when (eq switch-function 'switch-to-buffer-other-window)
1948 (when (one-window-p t)
1949 (if (window-minibuffer-p) (other-window 1))
1953 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
1956 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
1959 (setq wl-user-agent-headers-and-body-alist
1960 (cons (cons "to" to)
1961 wl-user-agent-headers-and-body-alist))))
1963 (if (wl-string-match-assoc "subject"
1964 wl-user-agent-headers-and-body-alist
1967 (wl-string-match-assoc "subject"
1968 wl-user-agent-headers-and-body-alist
1971 (setq wl-user-agent-headers-and-body-alist
1972 (cons (cons "subject" subject)
1973 wl-user-agent-headers-and-body-alist))))
1974 ;; i think this is what we want to use...
1977 ;; tell the hook-function to do its stuff
1978 (setq wl-user-agent-compose-p t)
1979 ;; because to get the hooks working, wl-draft has to think it has
1980 ;; been called interactively
1981 (call-interactively 'wl-draft))
1982 (setq wl-user-agent-compose-p nil))))
1984 (defun wl-user-agent-compose-internal ()
1985 "Manipulate headers and/or a body of a draft message."
1986 ;; being called from wl-user-agent-compose?
1987 (if wl-user-agent-compose-p
1990 (let ((headers wl-user-agent-headers-and-body-alist)
1991 (case-fold-search t))
1994 (if (not (string-match "^body$" (car (car headers))))
1995 (wl-user-agent-insert-header
1996 (car (car headers)) (cdr (car headers)))
1998 (setq headers (cdr headers))))
1999 ;; highlight headers (from wl-draft in wl-draft.el)
2000 (wl-highlight-headers 'for-draft)
2002 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2004 (wl-user-agent-insert-body
2005 (cdr (wl-string-match-assoc
2007 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2011 (product-provide (provide 'wl-draft) (require 'wl-version))
2013 ;;; wl-draft.el ends here