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)
77 (defvar wl-draft-config-sub-func-alist
78 '((body . wl-draft-config-sub-body)
79 (top . wl-draft-config-sub-top)
80 (bottom . wl-draft-config-sub-bottom)
81 (header . wl-draft-config-sub-header)
82 (body-file . wl-draft-config-sub-body-file)
83 (top-file . wl-draft-config-sub-top-file)
84 (bottom-file . wl-draft-config-sub-bottom-file)
85 (header-file . wl-draft-config-sub-header-file)
86 (template . wl-draft-config-sub-template)
87 (x-face . wl-draft-config-sub-x-face)))
89 (make-variable-buffer-local 'wl-draft-buffer-file-name)
90 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
91 (make-variable-buffer-local 'wl-draft-config-variables)
92 (make-variable-buffer-local 'wl-draft-config-exec-flag)
93 (make-variable-buffer-local 'wl-sent-message-via)
94 (make-variable-buffer-local 'wl-draft-fcc-list)
95 (make-variable-buffer-local 'wl-draft-reply-buffer)
97 (defmacro wl-smtp-extension-bind (&rest body)
98 (` (let* ((smtp-sasl-mechanisms
99 (if wl-smtp-authenticate-type
101 (if (listp wl-smtp-authenticate-type)
102 wl-smtp-authenticate-type
103 (list wl-smtp-authenticate-type)))))
104 (smtp-use-sasl (and smtp-sasl-mechanisms t))
105 (smtp-use-starttls (eq wl-smtp-connection-type 'starttls))
106 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
107 (if (and (string= (car smtp-sasl-mechanisms) "DIGEST-MD5")
109 (string-match "^\\([^@]*\\)@\\([^@]*\\)"
110 wl-smtp-posting-user))
111 (setq smtp-sasl-user-name (match-string 1 wl-smtp-posting-user)
112 smtp-sasl-properties (list 'realm
113 (match-string 2 wl-smtp-posting-user)))
114 (setq smtp-sasl-user-name wl-smtp-posting-user
115 smtp-sasl-properties nil))
116 (setq sasl-read-passphrase
125 (defun wl-draft-insert-date-field ()
127 (insert "Date: " (wl-make-date-string) "\n"))
129 (defun wl-draft-insert-from-field ()
131 ;; Put the "From:" field in unless for some odd reason
132 ;; they put one in themselves.
133 (let* ((login (or user-mail-address (user-login-name)))
134 (fullname (user-full-name)))
135 (cond ((eq mail-from-style 'angles)
136 (insert "From: " fullname)
137 (let ((fullname-start (+ (point-min) (length "From: ")))
138 (fullname-end (point-marker)))
139 (goto-char fullname-start)
140 ;; Look for a character that cannot appear unquoted
141 ;; according to RFC 822.
142 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
145 ;; Quote fullname, escaping specials.
146 (goto-char fullname-start)
148 (while (re-search-forward "[\"\\]"
150 (replace-match "\\\\\\&" t))
152 (insert " <" login ">\n"))
153 ((eq mail-from-style 'parens)
154 (insert "From: " login " (")
155 (let ((fullname-start (point)))
157 (let ((fullname-end (point-marker)))
158 (goto-char fullname-start)
159 ;; RFC 822 says \ and nonmatching parentheses
160 ;; must be escaped in comments.
161 ;; Escape every instance of ()\ ...
162 (while (re-search-forward "[()\\]" fullname-end 1)
163 (replace-match "\\\\\\&" t))
164 ;; ... then undo escaping of matching parentheses,
165 ;; including matching nested parentheses.
166 (goto-char fullname-start)
167 (while (re-search-forward
168 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
170 (replace-match "\\1(\\3)" t)
171 (goto-char fullname-start))))
173 ((not mail-from-style)
174 (insert "From: " login "\n")))))
176 (defun wl-draft-insert-x-face-field ()
177 "Insert X-Face header."
179 (if (not (file-exists-p wl-x-face-file))
180 (error "File %s does not exist" wl-x-face-file)
181 (beginning-of-buffer)
182 (search-forward mail-header-separator nil t)
184 (wl-draft-insert-x-face-field-here)
185 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
187 (defun wl-draft-insert-x-face-field-here ()
188 "Insert X-Face field at point."
189 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
190 (when (string-match "^[ \t]*" x-face-string)
191 (setq x-face-string (substring x-face-string (match-end 0))))
192 (insert "X-Face: " x-face-string))
193 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
195 ;; Insert X-Face-Version: field
196 (when (and (fboundp 'x-face-insert-version-header)
197 (boundp 'x-face-add-x-face-version-header)
198 x-face-add-x-face-version-header)
199 (x-face-insert-version-header)))
201 (defun wl-draft-setup ()
202 (let ((field wl-draft-fields)
205 (setq ret-val (append ret-val
206 (list (cons (concat (car field) " ")
207 (concat (car field) " ")))))
208 (setq field (cdr field)))
209 (setq wl-draft-field-completion-list ret-val)))
211 (defun wl-draft-make-mail-followup-to (recipients)
212 (if (elmo-list-member
213 (or wl-user-mail-address-list
214 (list (wl-address-header-extract-address wl-from)))
216 (let ((rlist (elmo-list-delete
217 (or wl-user-mail-address-list
218 (list (wl-address-header-extract-address wl-from)))
219 (copy-sequence recipients))))
220 (if (elmo-list-member rlist (mapcar 'downcase
221 wl-subscribed-mailing-list))
223 (append rlist (list (wl-address-header-extract-address
227 (defun wl-draft-delete-myself-from-cc (to cc)
228 (let ((myself (or wl-user-mail-address-list
229 (list (wl-address-header-extract-address wl-from)))))
230 (cond (wl-draft-always-delete-myself ; always-delete option
231 (elmo-list-delete myself cc))
232 ((elmo-list-member (append to cc) ; subscribed mailing-list
233 (mapcar 'downcase wl-subscribed-mailing-list))
234 (elmo-list-delete myself cc))
237 (defun wl-draft-forward (original-subject summary-buf)
239 (with-current-buffer (wl-message-get-original-buffer)
240 (setq references (nconc
241 (std11-field-bodies '("References" "In-Reply-To"))
242 (list (std11-field-body "Message-Id"))))
243 (setq references (delq nil references)
244 references (mapconcat 'identity references " ")
245 references (wl-draft-parse-msg-id-list-string references)
246 references (wl-delete-duplicates references)
247 references (when references
248 (mapconcat 'identity references "\n\t"))))
249 (wl-draft "" (concat "Forward: " original-subject)
250 nil nil references nil nil nil nil nil nil summary-buf))
251 (goto-char (point-max))
252 (wl-draft-insert-message)
253 (mail-position-on-field "To"))
255 (defun wl-draft-strip-subject-re (subject)
256 "Remove \"Re:\" from subject lines. Shamelessly copied from Gnus."
257 (if (string-match wl-subject-prefix-regexp subject)
258 (substring subject (match-end 0))
261 (defun wl-draft-reply-list-symbol (with-arg)
262 "Return symbol `wl-draft-reply-*-argument-list' match condition.
263 Check WITH-ARG and From: field."
264 (if (wl-address-user-mail-address-p (or (elmo-field-body "From") ""))
266 'wl-draft-reply-myself-with-argument-list
267 'wl-draft-reply-myself-without-argument-list)
269 'wl-draft-reply-with-argument-list
270 'wl-draft-reply-without-argument-list)))
272 (defun wl-draft-reply (buf with-arg summary-buf)
273 "Reply to BUF buffer message.
274 Reply to author if WITH-ARG is non-nil."
277 to mail-followup-to cc subject in-reply-to references newsgroups
278 from to-alist cc-alist decoder)
280 (setq r-list (symbol-value (wl-draft-reply-list-symbol with-arg)))
283 (when (let ((condition (car (car r-list))))
284 (cond ((stringp condition)
285 (std11-field-body condition))
289 (if (not (std11-field-body (car condition)))
291 (setq condition (cdr condition)))
294 (funcall condition))))
295 (let ((r-to-list (nth 0 (cdr (car r-list))))
296 (r-cc-list (nth 1 (cdr (car r-list))))
297 (r-ng-list (nth 2 (cdr (car r-list)))))
298 (when (and (member "Followup-To" r-ng-list)
299 (string= (std11-field-body "Followup-To") "poster"))
300 (setq r-to-list (cons "From" r-to-list))
301 (setq r-ng-list (delete "Followup-To" (copy-sequence r-ng-list))))
302 (setq to (wl-concat-list (cons to
303 (elmo-multiple-fields-body-list
306 (setq cc (wl-concat-list (cons cc
307 (elmo-multiple-fields-body-list
310 (setq newsgroups (wl-concat-list (cons newsgroups
315 (setq r-list (cdr r-list)))
316 (error "No match field: check your `%s'"
317 (symbol-name (wl-draft-reply-list-symbol with-arg))))
318 (setq subject (std11-field-body "Subject"))
319 (setq to (wl-parse-addresses to)
320 cc (wl-parse-addresses cc))
321 (with-temp-buffer ; to keep raw buffer unibyte.
322 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
323 (setq decoder (mime-find-field-decoder 'Subject 'plain))
324 (setq subject (if (and subject decoder)
325 (funcall decoder subject) subject))
329 (setq decoder (mime-find-field-decoder 'To 'plain))
330 (cons (nth 1 (std11-extract-address-components addr))
331 (if decoder (funcall decoder addr) addr)))
336 (setq decoder (mime-find-field-decoder 'Cc 'plain))
337 (cons (nth 1 (std11-extract-address-components addr))
338 (if decoder (funcall decoder addr) addr)))
340 (and wl-reply-subject-prefix
341 (setq subject (concat wl-reply-subject-prefix
342 (wl-draft-strip-subject-re
344 (setq in-reply-to (std11-field-body "Message-Id"))
345 (setq references (nconc
346 (std11-field-bodies '("References" "In-Reply-To"))
348 (setq to (delq nil (mapcar 'car to-alist)))
349 (setq cc (delq nil (mapcar 'car cc-alist)))
350 ;; if subscribed mailing list is contained in cc or to
351 ;; and myself is contained in cc,
352 ;; delete myself from cc.
353 (setq cc (wl-draft-delete-myself-from-cc to cc))
354 (when wl-insert-mail-followup-to
355 (setq mail-followup-to
356 (wl-draft-make-mail-followup-to (append to cc)))
357 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
358 (setq newsgroups (wl-parse newsgroups
359 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
360 newsgroups (wl-delete-duplicates newsgroups)
361 newsgroups (if newsgroups (mapconcat 'identity newsgroups ",")))
362 (setq to (wl-delete-duplicates to nil t))
363 (setq cc (wl-delete-duplicates
364 (append (wl-delete-duplicates cc nil t)
365 to (copy-sequence to))
367 (and to (setq to (mapconcat
369 (if wl-draft-reply-use-address-with-full-name
370 (or (cdr (assoc addr to-alist)) addr)
373 (and cc (setq cc (mapconcat
375 (if wl-draft-reply-use-address-with-full-name
376 (or (cdr (assoc addr cc-alist)) addr)
379 (and mail-followup-to
380 (setq mail-followup-to
383 (if wl-draft-reply-use-address-with-full-name
384 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
386 mail-followup-to ",\n\t")))
387 (and (null to) (setq to cc cc nil))
388 (setq references (delq nil references)
389 references (mapconcat 'identity references " ")
390 references (wl-draft-parse-msg-id-list-string references)
391 references (wl-delete-duplicates references)
392 references (if references
393 (mapconcat 'identity references "\n\t")))
395 to subject in-reply-to cc references newsgroups mail-followup-to
396 nil nil nil nil summary-buf)
397 (setq wl-draft-reply-buffer buf))
398 (run-hooks 'wl-reply-hook))
400 (defun wl-draft-add-references ()
401 (let* ((mes-id (save-excursion
402 (set-buffer mail-reply-buffer)
403 (std11-field-body "message-id")))
404 (ref (std11-field-body "References"))
405 (ref-list nil) (st nil))
406 (when (and mes-id ref)
407 (while (string-match "<[^>]+>" ref st)
409 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
412 (member mes-id ref-list))
416 (when (mail-position-on-field "References")
418 (while (looking-at "^[ \t]")
420 (setq mes-id (concat "\t" mes-id "\n")))
424 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
425 &optional ignored-fields)
428 (narrow-to-region (point)(point))
430 (with-current-buffer mail-reply-buffer
432 (decode-mime-charset-region (point-min) (point-max)
434 (buffer-substring-no-properties
435 (point-min) (point-max))))
437 (goto-char (point-min))
438 (wl-draft-delete-fields ignored-fields))
439 (goto-char (point-max))
440 (push-mark (point) nil t)
441 (goto-char (point-min)))
443 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
444 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
445 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
446 (run-hooks 'wl-draft-cited-hook)
447 (when (and wl-draft-add-references
448 (wl-draft-add-references))
449 (wl-highlight-headers 'for-draft)) ; highlight when added References:
450 (when wl-highlight-body-too
451 (wl-highlight-body-region beg (point-max)))))
453 (defun wl-draft-confirm ()
454 "Confirm send message."
456 (y-or-n-p (format "Send current draft as %s? "
457 (cond ((and (wl-message-mail-p) (wl-message-news-p))
459 ((wl-message-mail-p) "Mail")
460 ((wl-message-news-p) "News")))))
462 (defun wl-message-news-p ()
463 "If exist valid Newsgroups field, return non-nil."
464 (std11-field-body "Newsgroups"))
466 (defun wl-message-field-exists-p (field)
467 "If FIELD exist and FIELD value is not empty, return non-nil."
468 (let ((value (std11-field-body field)))
470 (not (string= value "")))))
472 (defun wl-message-mail-p ()
473 "If exist To, Cc or Bcc field, return non-nil."
474 (or (wl-message-field-exists-p "To")
475 (wl-message-field-exists-p "Resent-to")
476 (wl-message-field-exists-p "Cc")
477 (wl-message-field-exists-p "Bcc")
478 ;;; This may be needed..
479 ;;; (wl-message-field-exists-p "Fcc")
482 (defun wl-draft-open-file (&optional file)
483 "Open FILE for edit."
485 ;;;(interactive "*fFile to edit: ")
486 (wl-draft-edit-string (elmo-get-file-string
488 (read-file-name "File to edit: "
489 (or wl-tmp-dir "~/"))))))
491 (defun wl-draft-edit-string (string)
492 (let ((cur-buf (current-buffer))
493 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
494 to subject in-reply-to cc references newsgroups mail-followup-to
495 content-type content-transfer-encoding from
496 body-beg buffer-read-only)
500 (setq to (std11-field-body "To"))
503 (decode-mime-charset-string
506 (setq subject (std11-field-body "Subject"))
507 (setq subject (and subject
509 (decode-mime-charset-string
512 (setq from (std11-field-body "From")
515 (decode-mime-charset-string
518 (setq in-reply-to (std11-field-body "In-Reply-To"))
519 (setq cc (std11-field-body "Cc"))
522 (decode-mime-charset-string
525 (setq references (std11-field-body "References"))
526 (setq newsgroups (std11-field-body "Newsgroups"))
527 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
528 (setq content-type (std11-field-body "Content-Type"))
529 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
530 (goto-char (point-min))
531 (or (re-search-forward "\n\n" nil t)
532 (search-forward (concat mail-header-separator "\n") nil t))
535 (wl-draft to subject in-reply-to cc references newsgroups
537 content-type content-transfer-encoding
538 (buffer-substring (point) (point-max))
540 (if (member (nth 1 (std11-extract-address-components from))
541 wl-user-mail-address-list)
543 (and to (mail-position-on-field "To"))
544 (delete-other-windows)
545 (kill-buffer tmp-buf)))
546 (setq buffer-read-only nil) ;;??
547 (run-hooks 'wl-draft-reedit-hook))
549 (defun wl-draft-insert-current-message (dummy)
551 (let (original-buffer
553 mail-citation-hook mail-yank-hooks
554 wl-draft-add-references wl-draft-cite-function)
555 (with-current-buffer wl-draft-buffer-cur-summary-buffer
556 (with-current-buffer wl-message-buffer
557 (setq original-buffer (wl-message-get-original-buffer))
559 (with-current-buffer original-buffer
561 (error "No current message"))))
562 (setq mail-reply-buffer original-buffer)
563 (wl-draft-yank-from-mail-reply-buffer
565 wl-ignored-forwarded-headers)))
567 (defun wl-draft-insert-get-message (dummy)
568 (let ((fld (completing-read
570 (if (memq 'read-folder wl-use-folder-petname)
571 (wl-folder-get-entity-with-petname)
572 wl-folder-entity-hashtb)
573 nil nil wl-default-spec
574 'wl-read-folder-hist))
575 (number (call-interactively
576 (function (lambda (num)
577 (interactive "nNumber: ")
579 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
580 mail-citation-hook mail-yank-hooks
581 wl-draft-cite-function)
584 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
587 (elmo-make-fetch-strategy 'entire)
588 nil mail-reply-buffer)
589 (wl-draft-yank-from-mail-reply-buffer nil))
590 (kill-buffer mail-reply-buffer))))
593 ;; default body citation func
595 (defun wl-default-draft-cite ()
596 (let ((mail-yank-ignored-headers "[^:]+:")
597 (mail-yank-prefix "> ")
598 (summary-buf wl-current-summary-buffer)
599 (message-buf (get-buffer (wl-current-message-buffer)))
600 from date cite-title num entity)
602 (buffer-live-p summary-buf)
604 (buffer-live-p message-buf))
606 (with-current-buffer summary-buf
607 (setq num (save-excursion
608 (set-buffer message-buf)
609 wl-message-buffer-cur-number))
610 (setq entity (elmo-msgdb-overview-get-entity
611 num (wl-summary-buffer-msgdb)))
612 (setq date (elmo-msgdb-overview-entity-get-date entity))
613 (setq from (elmo-msgdb-overview-entity-get-from entity)))
614 (setq cite-title (format "At %s,\n%s wrote:"
615 (or date "some time ago")
616 (wl-summary-from-func-internal
619 (insert cite-title "\n"))
620 (mail-indent-citation)))
622 (defvar wl-draft-buffer nil "Draft buffer to yank content")
623 (defun wl-draft-yank-to-draft-buffer (buffer)
624 "Yank BUFFER content to `wl-draft-buffer'."
625 (set-buffer wl-draft-buffer)
626 (let ((mail-reply-buffer buffer))
627 (wl-draft-yank-from-mail-reply-buffer nil)
628 (kill-buffer buffer)))
630 (defun wl-draft-yank-original (&optional arg)
631 "Yank original message."
634 (let (buf mail-reply-buffer)
637 (setq buf (current-buffer)))
638 (setq mail-reply-buffer buf)
639 (wl-draft-yank-from-mail-reply-buffer nil))
640 (wl-draft-yank-current-message-entity)))
642 (defun wl-draft-hide (editing-buffer)
643 "Hide the editing draft buffer if possible."
645 (when (and editing-buffer
646 (buffer-live-p editing-buffer))
647 (set-buffer editing-buffer)
648 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
649 fld-buf sum-win fld-win)
650 (if (and wl-draft-use-frame
651 (> (length (visible-frame-list)) 1))
657 ;; stay folder window if required
658 (when wl-stay-folder-window
659 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
660 (if (setq fld-win (get-buffer-window fld-buf))
661 (select-window fld-win)
662 (if wl-draft-resume-folder-window ;; resume folder window
663 (switch-to-buffer fld-buf)))))
664 (if (buffer-live-p sum-buf)
665 (if (setq sum-win (get-buffer-window sum-buf t))
666 ;; if Summary is on the frame, select it.
667 (select-window sum-win)
668 ;; if summary is not on the frame, switch to it.
669 (if (and wl-stay-folder-window
670 (or wl-draft-resume-folder-window fld-win))
671 (wl-folder-select-buffer sum-buf)
672 (switch-to-buffer sum-buf)))))))
674 (defun wl-draft-delete (editing-buffer)
675 "kill the editing draft buffer and delete the file corresponds to it."
678 (set-buffer editing-buffer)
679 (if wl-draft-buffer-file-name
681 (if (file-exists-p wl-draft-buffer-file-name)
682 (delete-file wl-draft-buffer-file-name))
683 (let ((msg (and wl-draft-buffer-file-name
684 (string-match "[0-9]+$" wl-draft-buffer-file-name)
686 (match-string 0 wl-draft-buffer-file-name)))))
687 (wl-draft-config-info-operation msg 'delete))))
688 (set-buffer-modified-p nil) ; force kill
689 (kill-buffer editing-buffer))))
691 (defun wl-draft-kill (&optional force-kill)
692 "Kill current draft buffer and quit editing."
695 (when (and (or (eq major-mode 'wl-draft-mode)
696 (eq major-mode 'mail-mode))
698 (y-or-n-p "Kill Current Draft? ")))
699 (let ((cur-buf (current-buffer)))
700 (wl-draft-hide cur-buf)
701 (wl-draft-delete cur-buf)))
704 (defun wl-draft-fcc ()
705 "Add a new Fcc field, with file name completion."
707 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
708 (mail-position-on-field "to"))
711 ;; function for wl-sent-message-via
713 (defmacro wl-draft-sent-message-p (type)
714 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
716 (defmacro wl-draft-set-sent-message (type result &optional server-port)
717 (` (let ((element (assq (, type) wl-sent-message-via)))
719 (unless (eq (nth 1 element) (, result))
720 (setcdr element (list (, result) (, server-port)))
721 (setq wl-sent-message-modified t))
722 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
723 (setq wl-sent-message-modified t)))))
725 (defun wl-draft-sent-message-results ()
726 (let ((results wl-sent-message-via)
727 unplugged-via sent-via)
729 (cond ((eq (nth 1 (car results)) 'unplugged)
730 (push (caar results) unplugged-via))
731 ((eq (nth 1 (car results)) 'sent)
732 (push (caar results) sent-via)))
733 (setq results (cdr results)))
734 (list unplugged-via sent-via)))
736 (defun wl-draft-write-sendlog (status proto server to id)
737 "Write send log file, if `wl-draft-sendlog' is non-nil."
738 (when wl-draft-sendlog
740 (let* ((tmp-buf (get-buffer-create " *wl-draft-sendlog*"))
741 (filename (expand-file-name wl-draft-sendlog-filename
743 (filesize (nth 7 (file-attributes filename)))
744 (server (if server (concat " server=" server) ""))
746 ((memq proto '(fcc queue))
747 (format " folder=\"%s\"" to))
749 (format " ng=<%s>" to))
754 (mapcar '(lambda(x) (format "<%s>" x)) to)
757 (id (if id (concat " id=" id) ""))
758 (time (wl-sendlog-time)))
761 (insert (format "%s proto=%s stat=%s%s%s%s\n"
762 time proto status server to id))
763 (if (and wl-draft-sendlog-max-size filesize
764 (> filesize wl-draft-sendlog-max-size))
765 (rename-file filename (concat filename ".old") t))
766 (if (file-writable-p filename)
767 (write-region (point-min) (point-max)
769 (message (format "%s is not writable." filename)))
770 (kill-buffer tmp-buf)))))
772 (defun wl-draft-get-header-delimiter (&optional delete)
773 ;; If DELETE is non-nil, replace the header delimiter with a blank line
775 (goto-char (point-min))
776 (when (re-search-forward
777 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
781 (setq delimline (point-marker)))
784 (defun wl-draft-send-mail-with-qmail ()
785 "Pass the prepared message buffer to qmail-inject.
786 Refer to the documentation for the variable `send-mail-function'
787 to find out how to use this."
788 (if (and wl-draft-qmail-send-plugged
789 (not (elmo-plugged-p)))
790 (wl-draft-set-sent-message 'mail 'unplugged)
792 (let ((id (std11-field-body "Message-ID"))
793 (to (std11-field-body "To")))
797 'call-process-region 1 (point-max) wl-qmail-inject-program
799 wl-qmail-inject-args))
800 ;; qmail-inject doesn't say anything on it's stdout/stderr,
801 ;; we have to look at the retval instead
803 (wl-draft-set-sent-message 'mail 'sent)
804 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
805 (1 (error "`qmail-inject' reported permanent failure"))
806 (111 (error "`qmail-inject' reported transient failure"))
807 ;; should never happen
808 (t (error "`qmail-inject' reported unknown failure"))))))
810 (defun wl-draft-parse-msg-id-list-string (string)
811 "Get msg-id list from STRING."
813 (dolist (parsed-id (std11-parse-msg-ids-string string))
814 (when (eq (car parsed-id) 'msg-id)
815 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
817 (nreverse msg-id-list)))
819 (defun wl-draft-std11-parse-addresses (lal)
820 (let ((ret (std11-parse-address lal)))
822 (let ((dest (list (car ret))))
824 (while (and (setq ret (std11-parse-ascii-token lal))
825 (string-equal (cdr (assq 'specials (car ret))) ",")
826 (setq ret (std11-parse-address (cdr ret)))
828 (setq dest (cons (car ret) dest))
829 (setq lal (cdr ret)))
830 (while (eq 'spaces (car (car lal)))
831 (setq lal (cdr lal)))
832 (if lal (error "Error while parsing address"))
835 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
836 "Get mailbox list of FIELD from current buffer.
837 The buffer is expected to be narrowed to just the headers of the message.
838 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
839 from current buffer."
841 (let ((case-fold-search t)
842 (inhibit-read-only t)
844 mailbox-list beg seq has-group-list)
845 (goto-char (point-min))
846 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
849 (re-search-forward "^[^ \t]" nil 'move)
851 (skip-chars-backward "\n")
852 (setq seq (std11-lexical-analyze
853 (buffer-substring-no-properties beg (point))))
854 (setq addresses (wl-draft-std11-parse-addresses seq))
856 (cond ((eq (car (car addresses)) 'group)
857 (setq has-group-list t)
861 'std11-address-string
862 (nth 2 (car addresses))))))
863 ((eq (car (car addresses)) 'mailbox)
864 (setq address (nth 1 (car addresses)))
868 (std11-addr-to-string
869 (if (eq (car address) 'phrase-route-addr)
872 (setq addresses (cdr addresses)))
873 (when (and remove-group-list has-group-list)
874 (delete-region beg (point))
875 (insert (wl-address-string-without-group-list-contents seq))))
878 (defun wl-draft-deduce-address-list (buffer header-start header-end)
879 "Get address list suitable for smtp RCPT TO:<address>.
880 Group list content is removed if `wl-draft-remove-group-list-contents' is
882 (let ((fields '("to" "cc" "bcc"))
883 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
888 (narrow-to-region header-start header-end)
889 (goto-char (point-min))
891 (if (re-search-forward "^resent-to[\t ]*:" nil t)
892 (setq fields resent-fields)))
896 (wl-draft-parse-mailbox-list
898 wl-draft-remove-group-list-contents)))
899 (setq fields (cdr fields)))
905 (defun wl-draft-send-mail-with-smtp ()
906 "Send the prepared message buffer with SMTP."
908 (let* ((errbuf (if mail-interactive
909 (generate-new-buffer " smtp errors")
912 (default-case-fold-search t)
913 (sender (or wl-envelope-from
914 (wl-address-header-extract-address wl-from)))
915 (delimline (save-excursion
916 (goto-char (point-min))
918 (concat "^" (regexp-quote mail-header-separator)
922 (or wl-smtp-posting-server
923 ;; Compatibility stuff for FLIM 1.12.5 or earlier.
924 ;; They don't accept a function as the value of `smtp-server'.
925 (if (functionp smtp-server)
930 (let (wl-draft-remove-group-list-contents)
931 (wl-draft-deduce-address-list
932 (current-buffer) (point-min) delimline)))
933 (or smtp-server "localhost"))))
934 (smtp-service (or wl-smtp-posting-port smtp-service))
935 (smtp-local-domain (or smtp-local-domain wl-local-domain))
936 (id (std11-field-body "message-id"))
938 (if (not (elmo-plugged-p smtp-server smtp-service))
939 (wl-draft-set-sent-message 'mail 'unplugged
940 (cons smtp-server smtp-service))
943 ;; Instead of `smtp-deduce-address-list'.
944 (setq recipients (wl-draft-deduce-address-list
945 (current-buffer) (point-min) delimline))
946 (unless recipients (error "No recipients"))
947 ;; Insert an extra newline if we need it to work around
948 ;; Sun's bug that swallows newlines.
949 (goto-char (1+ delimline))
950 (if (eval mail-mailer-swallows-blank-line)
952 ;;; (run-hooks 'wl-mail-send-pre-hook)
957 (wl-draft-delete-field "bcc" delimline)
958 (wl-draft-delete-field "resent-bcc" delimline)
959 (let (process-connection-type)
962 (wl-smtp-extension-bind
964 (smtp-send-buffer sender recipients (current-buffer))
966 (wl-draft-write-sendlog 'failed 'smtp smtp-server
968 (signal (car err) (cdr err)))))
969 (wl-draft-set-sent-message 'mail 'sent)
970 (wl-draft-write-sendlog
971 'ok 'smtp smtp-server recipients id)))))
973 (kill-buffer errbuf))))))
975 (defun wl-draft-send-mail-with-pop-before-smtp ()
976 "Send the prepared message buffer with POP-before-SMTP."
979 (let ((session (elmo-pop3-get-session
982 :user (or wl-pop-before-smtp-user
983 elmo-pop3-default-user)
984 :server (or wl-pop-before-smtp-server
985 elmo-pop3-default-server)
986 :port (or wl-pop-before-smtp-port
987 elmo-pop3-default-port)
988 :auth (or wl-pop-before-smtp-authenticate-type
989 elmo-pop3-default-authenticate-type)
990 :stream-type (or wl-pop-before-smtp-stream-type
991 elmo-pop3-default-stream-type)))))
992 (when session (elmo-network-close-session session)))
994 (wl-draft-send-mail-with-smtp))
996 (defun wl-draft-insert-required-fields (&optional force-msgid)
997 "Insert Message-ID, Date, and From field.
998 If FORCE-MSGID, ignore 'wl-insert-message-id'."
999 ;; Insert Message-Id field...
1000 (goto-char (point-min))
1001 (when (and (or force-msgid
1002 wl-insert-message-id)
1003 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1004 (insert (concat "Message-ID: "
1005 (wl-draft-make-message-id-string)
1007 ;; Insert date field.
1008 (goto-char (point-min))
1009 (or (re-search-forward "^Date[ \t]*:" nil t)
1010 (wl-draft-insert-date-field))
1011 ;; Insert from field.
1012 (goto-char (point-min))
1013 (or (re-search-forward "^From[ \t]*:" nil t)
1014 (wl-draft-insert-from-field)))
1016 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1017 "Send the message in the current buffer."
1019 (std11-narrow-to-header mail-header-separator)
1020 (wl-draft-insert-required-fields)
1021 ;; Delete null fields.
1022 (goto-char (point-min))
1023 (while (re-search-forward "^[^ \t\n:]+:[ \t]*\n" nil t)
1025 ;; ignore any blank lines in the header
1026 (while (re-search-forward "\n\n\n*" nil t)
1027 (replace-match "\n")))
1028 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1029 (wl-draft-dispatch-message)
1030 (when kill-when-done
1031 ;; hide editing-buffer.
1032 (wl-draft-hide editing-buffer)
1033 ;; delete editing-buffer and its file.
1034 (wl-draft-delete editing-buffer)))
1036 (defun wl-draft-dispatch-message (&optional mes-string)
1037 "Send the message in the current buffer. Not modified the header fields."
1039 (if (and wl-draft-verbose-send mes-string)
1040 (message mes-string))
1042 (setq delimline (wl-draft-get-header-delimiter t))
1043 (unless wl-draft-fcc-list
1044 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1046 (setq wl-sent-message-modified nil)
1049 (if (and (wl-message-mail-p)
1050 (not (wl-draft-sent-message-p 'mail)))
1051 (funcall wl-draft-send-mail-function))
1052 (if (and (wl-message-news-p)
1053 (not (wl-draft-sent-message-p 'news))
1054 (not (wl-message-field-exists-p "Resent-to")))
1055 (funcall wl-draft-send-news-function)))
1057 (let* ((status (wl-draft-sent-message-results))
1058 (unplugged-via (car status))
1059 (sent-via (nth 1 status)))
1060 ;; If one sent, process fcc folder.
1061 (if (and sent-via wl-draft-fcc-list)
1063 (wl-draft-do-fcc (wl-draft-get-header-delimiter) wl-draft-fcc-list)
1064 (setq wl-draft-fcc-list nil)))
1065 (if wl-draft-use-cache
1066 (let ((id (std11-field-body "Message-ID"))
1067 (elmo-enable-disconnected-operation t))
1068 (elmo-file-cache-save (elmo-file-cache-get-path id)
1070 ;; If one unplugged, append queue.
1071 (when (and unplugged-via
1072 wl-sent-message-modified)
1073 (if wl-draft-enable-queuing
1074 (wl-draft-queue-append wl-sent-message-via)
1075 (error "Unplugged")))
1076 (when wl-draft-verbose-send
1077 (if (and unplugged-via sent-via);; combined message
1079 (setq wl-draft-verbose-msg
1080 (format "Sending%s and Queuing%s..."
1081 sent-via unplugged-via))
1082 (message (concat wl-draft-verbose-msg "done")))
1084 (message (concat mes-string
1085 (if sent-via "done" "failed")))))))))
1086 (not wl-sent-message-modified)) ;; return value
1088 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1089 "Force send current buffer as raw message."
1092 (let (wl-interactive-send
1093 ;;; wl-draft-verbose-send
1094 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1095 ;;; wl-news-send-pre-hook
1098 (wl-draft-send kill-when-done mes-string))))
1100 (defun wl-draft-clone-local-variables ()
1101 (let ((locals (buffer-local-variables))
1104 (when (and (consp (car locals))
1106 (string-match wl-draft-clone-local-variable-regexp
1107 (symbol-name (car (car locals)))))
1108 (wl-append result (list (car (car locals)))))
1109 (setq locals (cdr locals)))
1112 (defun wl-draft-send (&optional kill-when-done mes-string)
1113 "Send current draft message.
1114 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1116 ;; Don't call this explicitly.
1117 ;; Added to 'wl-draft-send-hook (by teranisi)
1118 ;; (wl-draft-config-exec)
1119 (run-hooks 'wl-draft-send-hook)
1120 (when (or (not wl-interactive-send)
1121 (y-or-n-p "Do you really want to send current draft? "))
1122 (let ((send-mail-function 'wl-draft-raw-send)
1123 (editing-buffer (current-buffer))
1124 (sending-buffer (wl-draft-generate-clone-buffer
1125 " *wl-draft-sending-buffer*"
1126 (append wl-draft-config-variables
1127 (wl-draft-clone-local-variables))))
1128 (wl-draft-verbose-msg nil)
1131 (save-excursion (set-buffer sending-buffer)
1132 (if (and (not (wl-message-mail-p))
1133 (not (wl-message-news-p)))
1134 (error "No recipient is specified"))
1135 (expand-abbrev) ; for mail-abbrevs
1136 (run-hooks 'mail-send-hook) ; translate buffer
1137 (if wl-draft-verbose-send
1138 (message (or mes-string "Sending...")))
1139 (funcall wl-draft-send-function editing-buffer kill-when-done)
1140 ;; Now perform actions on successful sending.
1141 (while mail-send-actions
1143 (apply (car (car mail-send-actions))
1144 (cdr (car mail-send-actions)))
1146 (setq mail-send-actions (cdr mail-send-actions)))
1147 ;; (if (or (eq major-mode 'wl-draft-mode)
1148 ;; (eq major-mode 'mail-mode))
1149 ;; (local-set-key "\C-c\C-s" 'wl-draft-send)) ; override
1150 (if wl-draft-verbose-send
1151 (message (concat (or wl-draft-verbose-msg
1152 mes-string "Sending...")
1154 ;; kill sending buffer, anyway.
1155 (and (buffer-live-p sending-buffer)
1156 (kill-buffer sending-buffer))))))
1158 (defun wl-draft-save ()
1159 "Save current draft."
1162 (wl-draft-config-info-operation
1163 (and (string-match "[0-9]+$" wl-draft-buffer-file-name)
1165 (match-string 0 wl-draft-buffer-file-name)))
1168 (defun wl-draft-mimic-kill-buffer ()
1169 "Kill the current (draft) buffer with query."
1171 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1174 (if (or (not bufname)
1175 (string-equal bufname "")
1176 (string-equal bufname (buffer-name)))
1177 (wl-draft-save-and-exit)
1178 (kill-buffer bufname))))
1180 (defun wl-draft-save-and-exit ()
1181 "Save current draft and exit current draft mode."
1184 (let ((editing-buffer (current-buffer)))
1185 (wl-draft-hide editing-buffer)
1186 (kill-buffer editing-buffer)))
1188 (defun wl-draft-send-and-exit ()
1189 "Send current draft message and kill it."
1193 (defun wl-draft-send-from-toolbar ()
1195 (let ((wl-interactive-send t))
1196 (wl-draft-send-and-exit)))
1198 (defun wl-draft-delete-field (field &optional delimline)
1199 (wl-draft-delete-fields (regexp-quote field) delimline))
1201 (defun wl-draft-delete-fields (regexp &optional delimline)
1204 (if (search-forward "\n\n" nil t)
1205 (setq delimline (point))
1206 (setq delimline (point-max))))
1207 (narrow-to-region (point-min) delimline)
1208 (goto-char (point-min))
1209 (let ((regexp (concat "^" regexp ":"))
1210 (case-fold-search t)
1213 (if (looking-at regexp)
1219 (if (re-search-forward "^[^ \t]" nil t)
1220 (goto-char (match-beginning 0))
1223 (if (re-search-forward "^[^ \t]" nil t)
1224 (goto-char (match-beginning 0))
1227 (defun wl-draft-get-fcc-list (header-end)
1229 (case-fold-search t))
1230 (or (markerp header-end) (error "HEADER-END must be a marker"))
1232 (goto-char (point-min))
1233 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1235 (cons (buffer-substring-no-properties
1239 (skip-chars-backward " \t")
1243 (wl-folder-confirm-existence
1244 (wl-folder-get-elmo-folder (eword-decode-string (car fcc-list)))))
1245 (delete-region (match-beginning 0)
1246 (progn (forward-line 1) (point)))))
1249 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1250 (let ((send-mail-buffer (current-buffer))
1251 (tembuf (generate-new-buffer " fcc output"))
1252 (case-fold-search t)
1254 (or (markerp header-end) (error "HEADER-END must be a marker"))
1257 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1260 ;; insert just the headers to avoid moving the gap more than
1261 ;; necessary (the message body could be arbitrarily huge.)
1262 (insert-buffer-substring send-mail-buffer 1 header-end)
1263 (wl-draft-insert-required-fields t)
1264 (goto-char (point-max))
1265 (insert-buffer-substring send-mail-buffer header-end)
1266 (let ((id (std11-field-body "Message-ID"))
1267 (elmo-enable-disconnected-operation t)
1270 (unless (or cache-saved
1271 (elmo-folder-plugged-p
1272 (wl-folder-get-elmo-folder (car fcc-list))))
1273 (elmo-file-cache-save id nil) ;; for disconnected operation
1274 (setq cache-saved t))
1275 (if (elmo-folder-append-buffer
1276 (wl-folder-get-elmo-folder
1277 (eword-decode-string (car fcc-list)))
1278 (not wl-fcc-force-as-read))
1279 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1280 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1281 (setq fcc-list (cdr fcc-list)))))
1282 (kill-buffer tembuf)))
1284 (defun wl-draft-on-field-p ()
1287 (goto-char (point-min))
1288 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1295 (if (or (looking-at ".*,[ \t]?$")
1296 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1298 (let ((pos (point)))
1301 (if (looking-at "^[ \t]")
1303 (if (re-search-forward ":" pos t) nil t)))))))
1305 (defun wl-draft-random-alphabet ()
1306 (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)))
1307 (nth (abs (% (random) 26)) alphabet)))
1310 (defun wl-draft (&optional to subject in-reply-to cc references newsgroups
1312 content-type content-transfer-encoding
1313 body edit-again summary-buf from)
1314 "Write and send mail/news message with Wanderlust."
1316 (unless (featurep 'wl)
1322 (wl-plugged-init t))
1323 (wl-init) ; returns immediately if already initialized.
1325 (setq summary-buf (wl-summary-get-buffer (wl-summary-buffer-folder-name))))
1326 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1327 buf-name file-name num wl-demo change-major-mode-hook)
1328 (if (not (elmo-folder-message-file-p draft-folder))
1329 (error "%s folder cannot be used for draft folder" wl-draft-folder))
1330 (setq num (elmo-max-of-list
1331 (or (elmo-folder-list-messages draft-folder) '(0))))
1332 (setq num (+ 1 num))
1333 ;; To get unused buffer name.
1334 (while (get-buffer (concat wl-draft-folder "/" (int-to-string num)))
1335 (setq num (+ 1 num)))
1336 (setq buf-name (find-file-noselect
1338 (elmo-message-file-name
1339 (wl-folder-get-elmo-folder wl-draft-folder)
1341 (if wl-draft-use-frame
1342 (switch-to-buffer-other-frame buf-name)
1343 (switch-to-buffer buf-name))
1344 (set-buffer buf-name)
1345 (if (not (string-match (regexp-quote wl-draft-folder)
1347 (rename-buffer (concat wl-draft-folder "/" (int-to-string num))))
1348 (if (or (eq wl-draft-reply-buffer-style 'full)
1349 (eq this-command 'wl-draft)
1350 (eq this-command 'wl-summary-write)
1351 (eq this-command 'wl-summary-write-current-folder))
1352 (delete-other-windows))
1355 (make-local-variable 'truncate-partial-width-windows)
1356 (setq truncate-partial-width-windows nil)
1357 (setq truncate-lines wl-draft-truncate-lines)
1358 (setq wl-sent-message-via nil)
1359 (if (stringp (or from wl-from))
1360 (insert "From: " (or from wl-from) "\n"))
1361 (and (or (interactive-p)
1362 (eq this-command 'wl-summary-write)
1364 (insert "To: " (or to "") "\n"))
1365 (and cc (insert "Cc: " (or cc "") "\n"))
1366 (insert "Subject: " (or subject "") "\n")
1367 (and newsgroups (insert "Newsgroups: " newsgroups "\n"))
1368 (and mail-followup-to (insert "Mail-Followup-To: " mail-followup-to "\n"))
1369 (and wl-insert-mail-reply-to
1370 (insert "Mail-Reply-To: "
1371 (wl-address-header-extract-address
1373 (and in-reply-to (insert "In-Reply-To: " in-reply-to "\n"))
1374 (and references (insert "References: " references "\n"))
1375 (insert (funcall wl-generate-mailer-string-function) "\n")
1376 (setq wl-draft-buffer-file-name file-name)
1377 (if mail-default-reply-to
1378 (insert "Reply-To: " mail-default-reply-to "\n"))
1379 (wl-draft-insert-ccs "Bcc: " (or wl-bcc
1380 (and mail-self-blind (user-login-name))))
1381 (wl-draft-insert-ccs "Fcc: " wl-fcc)
1383 (insert "Organization: " wl-organization "\n"))
1384 (and wl-auto-insert-x-face
1385 (file-exists-p wl-x-face-file)
1386 (wl-draft-insert-x-face-field-here))
1387 (if mail-default-headers
1388 (insert mail-default-headers))
1389 (if (not (= (preceding-char) ?\n))
1393 (setq start (point))
1395 (insert "Content-type: " content-type "\n"))
1396 (when content-transfer-encoding
1397 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1398 (if (or content-type content-transfer-encoding)
1400 (and body (insert body))
1402 (narrow-to-region start (point))
1404 (wl-draft-decode-message-in-buffer))
1407 (put-text-property (point)
1409 (insert mail-header-separator "\n")
1411 'category 'mail-header-separator)))
1412 (put-text-property (point)
1414 (insert mail-header-separator "\n")
1416 'category 'mail-header-separator)
1417 (and body (insert body)))
1418 (as-binary-output-file
1419 (write-region (point-min)(point-max) wl-draft-buffer-file-name
1421 (wl-draft-editor-mode)
1422 (wl-draft-overload-functions)
1423 (wl-highlight-headers 'for-draft)
1424 (goto-char (point-min))
1425 (setq wl-draft-config-exec-flag t)
1427 (run-hooks 'wl-mail-setup-hook))
1428 (wl-user-agent-compose-internal) ;; user-agent
1429 (cond ((eq this-command 'wl-summary-write-current-newsgroup)
1430 (mail-position-on-field "Subject"))
1431 ((and (interactive-p) (null to))
1432 (mail-position-on-field "To"))
1434 (goto-char (point-max))))
1435 (setq wl-draft-buffer-cur-summary-buffer (or summary-buf
1437 wl-summary-buffer-name)))
1440 (defsubst wl-draft-insert-ccs (str cc)
1446 (null (and wl-draft-delete-myself-from-bcc-fcc
1448 (mapcar 'wl-address-header-extract-address
1450 (wl-parse-addresses (std11-field-body "To"))
1451 (wl-parse-addresses (std11-field-body "Cc"))))
1452 (mapcar 'downcase wl-subscribed-mailing-list)))))
1453 (insert str field "\n"))))
1455 (defun wl-draft-elmo-nntp-send ()
1456 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1457 (elmo-nntp-default-user
1458 (or wl-nntp-posting-user elmo-nntp-default-user))
1459 (elmo-nntp-default-server
1460 (or wl-nntp-posting-server elmo-nntp-default-server))
1461 (elmo-nntp-default-port
1462 (or wl-nntp-posting-port elmo-nntp-default-port))
1463 (elmo-nntp-default-stream-type
1464 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type)))
1465 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1466 (wl-draft-set-sent-message 'news 'unplugged
1467 (cons elmo-nntp-default-server
1468 elmo-nntp-default-port))
1469 (elmo-nntp-post elmo-nntp-default-server (current-buffer))
1470 (wl-draft-set-sent-message 'news 'sent)
1471 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1472 (std11-field-body "Newsgroups")
1473 (std11-field-body "Message-ID")))))
1475 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1476 "Generate clone of current buffer named NAME."
1477 (let ((editing-buffer (current-buffer)))
1479 (set-buffer (generate-new-buffer name))
1482 (wl-draft-editor-mode)
1483 (insert-buffer editing-buffer)
1485 (while local-variables
1486 (make-local-variable (car local-variables))
1487 (set (car local-variables)
1489 (set-buffer editing-buffer)
1490 (symbol-value (car local-variables))))
1491 (setq local-variables (cdr local-variables)))
1494 (defun wl-draft-reedit (number)
1495 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1497 buf-name file-name change-major-mode-hook)
1498 (setq file-name (elmo-message-file-name draft-folder number))
1499 (unless (file-exists-p file-name)
1500 (error "File %s does not exist" file-name))
1501 (setq buf-name (find-file-noselect file-name))
1502 (if wl-draft-use-frame
1503 (switch-to-buffer-other-frame buf-name)
1504 (switch-to-buffer buf-name))
1505 (set-buffer buf-name)
1506 (if (not (string-match (regexp-quote wl-draft-folder)
1508 (rename-buffer (concat wl-draft-folder "/" (buffer-name))))
1511 (setq wl-sent-message-via nil)
1512 (setq wl-draft-buffer-file-name file-name)
1513 (wl-draft-config-info-operation number 'load)
1514 (goto-char (point-min))
1515 (or (re-search-forward "\n\n" nil t)
1516 (search-forward (concat mail-header-separator "\n") nil t))
1517 (write-region (point-min)(point-max) wl-draft-buffer-file-name
1519 (wl-draft-overload-functions)
1520 (wl-draft-editor-mode)
1521 (wl-highlight-headers 'for-draft)
1522 (run-hooks 'wl-draft-reedit-hook)
1523 (goto-char (point-max))
1527 (defmacro wl-draft-body-goto-top ()
1529 (goto-char (point-min))
1530 (if (re-search-forward mail-header-separator nil t)
1532 (goto-char (point-max))))))
1534 (defmacro wl-draft-body-goto-bottom ()
1535 (` (goto-char (point-max))))
1537 (defmacro wl-draft-config-body-goto-header ()
1539 (goto-char (point-min))
1540 (if (re-search-forward mail-header-separator nil t)
1542 (goto-char (point-max))))))
1544 (defun wl-draft-config-sub-body (content)
1545 (wl-draft-body-goto-top)
1546 (delete-region (point) (point-max))
1547 (if content (insert (eval content))))
1549 (defun wl-draft-config-sub-top (content)
1550 (wl-draft-body-goto-top)
1551 (if content (insert (eval content))))
1553 (defun wl-draft-config-sub-bottom (content)
1554 (wl-draft-body-goto-bottom)
1555 (if content (insert (eval content))))
1557 (defun wl-draft-config-sub-header (content)
1558 (wl-draft-config-body-goto-header)
1559 (if content (insert (concat (eval content) "\n"))))
1561 (defsubst wl-draft-config-sub-file (content)
1562 (let ((coding-system-for-read wl-cs-autoconv)
1563 (file (expand-file-name (eval content))))
1564 (if (file-exists-p file)
1565 (insert-file-contents file)
1566 (error "%s: no exists file" file))))
1568 (defun wl-draft-config-sub-body-file (content)
1569 (wl-draft-body-goto-top)
1570 (delete-region (point) (point-max))
1571 (wl-draft-config-sub-file content))
1573 (defun wl-draft-config-sub-top-file (content)
1574 (wl-draft-body-goto-top)
1575 (wl-draft-config-sub-file content))
1577 (defun wl-draft-config-sub-bottom-file (content)
1578 (wl-draft-body-goto-bottom)
1579 (wl-draft-config-sub-file content))
1581 (defun wl-draft-config-sub-header-file (content)
1582 (wl-draft-config-body-goto-header)
1583 (wl-draft-config-sub-file content))
1585 (defun wl-draft-config-sub-template (content)
1586 (setq wl-draft-config-variables
1587 (wl-template-insert (eval content))))
1589 (defun wl-draft-config-sub-x-face (content)
1590 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
1591 (fboundp 'x-face-insert)) ; x-face.el is installed.
1592 (x-face-insert content)
1593 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
1595 (defsubst wl-draft-config-sub-func (field content)
1597 (if (setq func (assq field wl-draft-config-sub-func-alist))
1598 (let (wl-draft-config-variables)
1599 (funcall (cdr func) content)
1600 ;; for wl-draft-config-sub-template
1601 (cons t wl-draft-config-variables)))))
1603 (defsubst wl-draft-config-exec-sub (clist)
1604 (let (config local-variables)
1606 (setq config (car clist))
1611 (let ((field (car config))
1612 (content (cdr config))
1616 (wl-draft-replace-field field (eval content) t))
1617 ((setq ret-val (wl-draft-config-sub-func field content))
1618 (if (cdr ret-val) ;; for wl-draft-config-sub-template
1619 (wl-append local-variables (cdr ret-val))))
1620 ((boundp field) ;; variable
1621 (make-local-variable field)
1622 (set field (eval content))
1623 (wl-append local-variables (list field)))
1625 (error "%s: not variable" field)))))
1627 (error "%s: not supported type" config)))
1628 (setq clist (cdr clist)))
1631 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
1632 "Change headers in draft preparation time."
1634 (unless wl-draft-reedit
1637 (and (boundp 'wl-draft-prepared-config-alist)
1638 wl-draft-prepared-config-alist) ;; For compatible.
1639 wl-draft-config-alist)))
1641 (wl-draft-config-exec config-alist reply-buf)))))
1643 (defun wl-draft-config-exec (&optional config-alist reply-buf)
1644 "Change headers in draft sending time."
1646 (let ((case-fold-search t)
1647 (alist (or config-alist wl-draft-config-alist))
1648 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
1649 wl-draft-reply-buffer)))
1650 (local-variables wl-draft-config-variables)
1652 (when (and (or (interactive-p)
1653 wl-draft-config-exec-flag)
1658 (setq key (caar alist)
1665 (set-buffer reply-buf)
1667 (std11-narrow-to-header)
1668 (goto-char (point-min))
1669 (re-search-forward (car clist) nil t))))
1670 (wl-draft-config-exec-sub (cdr clist))
1673 (when (save-restriction
1674 (std11-narrow-to-header mail-header-separator)
1675 (goto-char (point-min))
1676 (re-search-forward key nil t))
1677 (wl-append local-variables
1678 (wl-draft-config-exec-sub clist))
1681 (wl-append local-variables
1682 (wl-draft-config-exec-sub clist))
1684 (if (and found wl-draft-config-matchone)
1686 (setq alist (cdr alist))))
1688 (setq wl-draft-config-exec-flag nil))
1689 (run-hooks 'wl-draft-config-exec-hook)
1690 (put-text-property (point-min)(point-max) 'face nil)
1691 (wl-highlight-message (point-min)(point-max) t)
1692 (setq wl-draft-config-variables
1693 (elmo-uniq-list local-variables))))))
1695 (defun wl-draft-replace-field (field content &optional add)
1698 (let ((case-fold-search t)
1699 (inhibit-read-only t) ;; added by teranisi.
1701 (std11-narrow-to-header mail-header-separator)
1702 (goto-char (point-min))
1703 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
1708 (re-search-forward "^[^ \t]" nil 'move)
1710 (skip-chars-backward "\n")
1711 (delete-region beg (point))
1712 (insert " " content))
1717 (re-search-forward "^[^ \t]" nil 'move)
1719 (delete-region beg (point)))
1720 (when (and add content)
1722 (goto-char (point-max))
1723 (insert (concat field ": " content "\n"))))))))
1725 (defun wl-draft-config-info-operation (msg operation)
1726 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-folder-get-elmo-folder
1730 (format "%s-%d" wl-draft-config-save-filename msg)
1732 element alist variable)
1734 ((eq operation 'save)
1735 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
1736 (while (setq variable (pop variables))
1737 (when (boundp variable)
1739 (list (cons variable (eval variable))))))
1740 (elmo-object-save filename alist)))
1741 ((eq operation 'load)
1742 (setq alist (elmo-object-load filename))
1743 (while (setq element (pop alist))
1744 (set (make-local-variable (car element)) (cdr element))
1745 (wl-append wl-draft-config-variables (list (car element)))))
1746 ((eq operation 'delete)
1747 (if (file-exists-p filename)
1748 (delete-file filename))))))
1750 (defun wl-draft-queue-info-operation (msg operation
1751 &optional add-sent-message-via)
1752 (let* ((msgdb-dir (elmo-folder-msgdb-path
1753 (wl-folder-get-elmo-folder wl-queue-folder)))
1756 (format "%s-%d" wl-draft-queue-save-filename msg)
1758 element alist variable)
1760 ((eq operation 'save)
1761 (let ((variables (elmo-uniq-list
1762 (append wl-draft-queue-save-variables
1763 wl-draft-config-variables
1764 (list 'wl-draft-fcc-list)))))
1765 (if add-sent-message-via
1766 (push 'wl-sent-message-via variables))
1767 (while (setq variable (pop variables))
1768 (when (boundp variable)
1770 (list (cons variable (eval variable))))))
1771 (elmo-object-save filename alist)))
1772 ((eq operation 'load)
1773 (setq alist (elmo-object-load filename))
1774 (while (setq element (pop alist))
1775 (set (make-local-variable (car element)) (cdr element))))
1776 ((eq operation 'get-sent-via)
1777 (setq alist (elmo-object-load filename))
1778 (cdr (assq 'wl-sent-message-via alist)))
1779 ((eq operation 'delete)
1780 (if (file-exists-p filename)
1781 (delete-file filename))))))
1783 (defun wl-draft-queue-append (wl-sent-message-via)
1784 (if wl-draft-verbose-send
1785 (message "Queuing..."))
1786 (let ((send-buffer (current-buffer))
1787 (folder (wl-folder-get-elmo-folder wl-queue-folder))
1788 (message-id (std11-field-body "Message-ID")))
1789 (if (elmo-folder-append-buffer folder t)
1791 (wl-draft-queue-info-operation
1792 (car (elmo-folder-status folder))
1793 'save wl-sent-message-via)
1794 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
1795 (when wl-draft-verbose-send
1796 (setq wl-draft-verbose-msg "Queuing...")
1797 (message "Queuing...done")))
1798 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
1799 (error "Queuing failed"))))
1801 (defun wl-draft-queue-flush ()
1802 "Flush draft queue."
1804 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
1806 (elmo-folder-open-internal queue-folder)
1807 (elmo-folder-list-messages queue-folder)))
1810 (wl-draft-queue-flushing t)
1811 msgs failure len buffer msgid sent-via)
1812 ;; get plugged send message
1814 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
1817 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
1819 (car (nth 2 (car sent-via)))
1820 (cdr (nth 2 (car sent-via)))))
1821 (wl-append msgs (list (car msgs2)))
1823 (setq sent-via (cdr sent-via))))
1824 (setq msgs2 (cdr msgs2)))
1825 (when (> (setq len (length msgs)) 0)
1826 (if (elmo-y-or-n-p (format
1827 "%d message(s) are in the sending queue. Send now? "
1829 (not elmo-dop-flush-confirm) t)
1832 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
1835 ;; reset buffer local variables
1836 (kill-all-local-variables)
1840 (setq wl-sent-message-via nil)
1841 (wl-draft-queue-info-operation (car msgs) 'load)
1842 (elmo-message-fetch queue-folder
1844 (elmo-make-fetch-strategy 'entire)
1845 nil (current-buffer))
1847 (setq failure (funcall
1848 wl-draft-queue-flush-send-function
1849 (format "Sending (%d/%d)..." i len)))
1850 ;;; (wl-draft-raw-send nil nil
1851 ;;; (format "Sending (%d/%d)..." i len))
1853 (elmo-display-error err t)
1858 (elmo-folder-delete-messages
1859 queue-folder (cons (car msgs) nil))
1860 (wl-draft-queue-info-operation (car msgs) 'delete)
1861 (setq performed (+ 1 performed)))
1862 (setq msgs (cdr msgs)))
1863 (kill-buffer buffer)
1864 (message "%d message(s) are sent." performed)))
1865 (message "%d message(s) are remained to be sent." len))
1866 (elmo-folder-close queue-folder)
1869 (defun wl-jump-to-draft-buffer (&optional arg)
1870 "Jump to the draft if exists."
1873 (wl-jump-to-draft-folder)
1874 (let ((bufs (buffer-list))
1875 (draft-regexp (concat
1877 (elmo-localdir-folder-directory-internal
1878 (wl-folder-get-elmo-folder wl-draft-folder)))))
1882 (setq buf (buffer-file-name (car bufs)))
1883 (string-match draft-regexp buf))
1884 (setq draft-bufs (cons (buffer-name (car bufs)) draft-bufs)))
1885 (setq bufs (cdr bufs)))
1888 (message "No draft buffer exist."))
1891 (sort draft-bufs (function (lambda (a b) (not (string< a b))))))
1892 (if (setq buf (cdr (member (buffer-name) draft-bufs)))
1893 (setq buf (car buf))
1894 (setq buf (car draft-bufs)))
1895 (switch-to-buffer buf))))))
1897 (defun wl-jump-to-draft-folder ()
1898 (let ((msgs (reverse (elmo-folder-list-messages (wl-folder-get-elmo-folder
1900 (mybuf (buffer-name))
1903 (message "No draft message exist.")
1904 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
1905 (setq msg (cadr (memq
1906 (string-to-int (substring mybuf (match-end 0)))
1908 (or msg (setq msg (car msgs)))
1909 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
1910 (switch-to-buffer buf)
1911 (wl-draft-reedit msg)))))
1913 (defun wl-draft-highlight-and-recenter (&optional n)
1915 (if wl-highlight-body-too
1916 (let ((beg (point-min))
1918 (put-text-property beg end 'face nil)
1919 (wl-highlight-message beg end t)))
1922 ;;;; user-agent support by Sen Nagata
1924 ;; this appears to be necessarily global...
1925 (defvar wl-user-agent-compose-p nil)
1926 (defvar wl-user-agent-headers-and-body-alist nil)
1928 ;; this should be a generic function for mail-mode -- i wish there was
1929 ;; something like it in sendmail.el
1930 (defun wl-user-agent-insert-header (header-name header-value)
1931 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
1932 ;; it seems like overriding existing headers is acceptable -- should
1933 ;; we provide an option?
1935 ;; plan was: unfold header (might be folded), remove existing value, insert
1937 ;; wl doesn't seem to fold header lines yet anyway :-)
1939 (let ((kill-whole-line t)
1941 (mail-position-on-field (capitalize header-name))
1942 (setq end-of-line (point))
1944 (re-search-forward ":" end-of-line)
1945 (insert (concat " " header-value "\n"))
1948 ;; this should be a generic function for mail-mode -- i wish there was
1949 ;; something like it in sendmail.el
1951 ;; ** haven't dealt w/ case where the body is already set **
1952 (defun wl-user-agent-insert-body (body-text)
1953 "Insert a body of text, BODY-TEXT, into a message."
1954 ;; code defensively... :-P
1955 (goto-char (point-min))
1956 (search-forward mail-header-separator)
1961 (defun wl-user-agent-compose (&optional to subject other-headers continue
1962 switch-function yank-action
1964 "Support the `compose-mail' interface for wl.
1965 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
1966 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
1967 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
1969 (unless (featurep 'wl)
1971 ;; protect these -- to and subject get bound at some point, so it looks
1972 ;; to be necessary to protect the values used w/in
1973 (let ((wl-user-agent-headers-and-body-alist other-headers)
1974 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
1975 (wl-draft-reply-buffer-style 'split))
1976 (when (eq switch-function 'switch-to-buffer-other-window)
1977 (when (one-window-p t)
1978 (if (window-minibuffer-p) (other-window 1))
1982 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
1985 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
1988 (setq wl-user-agent-headers-and-body-alist
1989 (cons (cons "to" to)
1990 wl-user-agent-headers-and-body-alist))))
1992 (if (wl-string-match-assoc "subject"
1993 wl-user-agent-headers-and-body-alist
1996 (wl-string-match-assoc "subject"
1997 wl-user-agent-headers-and-body-alist
2000 (setq wl-user-agent-headers-and-body-alist
2001 (cons (cons "subject" subject)
2002 wl-user-agent-headers-and-body-alist))))
2003 ;; i think this is what we want to use...
2006 ;; tell the hook-function to do its stuff
2007 (setq wl-user-agent-compose-p t)
2008 ;; because to get the hooks working, wl-draft has to think it has
2009 ;; been called interactively
2010 (call-interactively 'wl-draft))
2011 (setq wl-user-agent-compose-p nil))))
2013 (defun wl-user-agent-compose-internal ()
2014 "Manipulate headers and/or a body of a draft message."
2015 ;; being called from wl-user-agent-compose?
2016 (if wl-user-agent-compose-p
2019 (let ((headers wl-user-agent-headers-and-body-alist)
2020 (case-fold-search t))
2023 (if (not (string-match "^body$" (car (car headers))))
2024 (wl-user-agent-insert-header
2025 (car (car headers)) (cdr (car headers)))
2027 (setq headers (cdr headers))))
2028 ;; highlight headers (from wl-draft in wl-draft.el)
2029 (wl-highlight-headers 'for-draft)
2031 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2033 (wl-user-agent-insert-body
2034 (cdr (wl-string-match-assoc
2036 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2040 (product-provide (provide 'wl-draft) (require 'wl-version))
2042 ;;; wl-draft.el ends here