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-buffer-file-name nil)
57 (defvar wl-draft-field-completion-list nil)
58 (defvar wl-draft-verbose-send t)
59 (defvar wl-draft-verbose-msg nil)
60 (defvar wl-draft-queue-flushing nil)
61 (defvar wl-draft-config-variables nil)
62 (defvar wl-draft-config-exec-flag t)
63 (defvar wl-draft-buffer-cur-summary-buffer nil)
64 (defvar wl-draft-clone-local-variable-regexp "^\\(wl\\|mime\\)")
65 (defvar wl-draft-sendlog-filename "sendlog")
66 (defvar wl-draft-queue-save-filename "qinfo")
67 (defvar wl-draft-config-save-filename "config")
68 (defvar wl-draft-queue-flush-send-function 'wl-draft-dispatch-message)
69 (defvar wl-sent-message-via nil)
70 (defvar wl-sent-message-modified nil)
71 (defvar wl-sent-message-queued nil)
72 (defvar wl-draft-fcc-list nil)
73 (defvar wl-draft-reedit nil)
74 (defvar wl-draft-reply-buffer nil)
75 (defvar wl-draft-forward nil)
76 (defvar wl-draft-doing-mime-bcc nil)
78 (defvar wl-draft-parent-folder nil
79 "Folder name of the summary in which current draft is invoked.
80 This variable is local in each draft buffer.
81 You can refer its value in `wl-draft-config-alist'.
84 \(setq wl-draft-config-alist
85 '(((string-match \".*@domain1$\" wl-draft-parent-folder)
86 (\"From\" . \"user@domain1\"))
87 ((string-match \".*@domain2$\" wl-draft-parent-folder)
88 (\"From\" . \"user@domain2\"))))")
90 (defvar wl-draft-config-sub-func-alist
91 '((body . wl-draft-config-sub-body)
92 (top . wl-draft-config-sub-top)
93 (bottom . wl-draft-config-sub-bottom)
94 (header . wl-draft-config-sub-header)
95 (header-top . wl-draft-config-sub-header-top)
96 (header-bottom . wl-draft-config-sub-header)
97 (part-top . wl-draft-config-sub-part-top)
98 (part-bottom . wl-draft-config-sub-part-bottom)
99 (body-file . wl-draft-config-sub-body-file)
100 (top-file . wl-draft-config-sub-top-file)
101 (bottom-file . wl-draft-config-sub-bottom-file)
102 (header-file . wl-draft-config-sub-header-file)
103 (template . wl-draft-config-sub-template)
104 (x-face . wl-draft-config-sub-x-face)))
106 (make-variable-buffer-local 'wl-draft-buffer-file-name)
107 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
108 (make-variable-buffer-local 'wl-draft-config-variables)
109 (make-variable-buffer-local 'wl-draft-config-exec-flag)
110 (make-variable-buffer-local 'wl-sent-message-via)
111 (make-variable-buffer-local 'wl-sent-message-queued)
112 (make-variable-buffer-local 'wl-draft-fcc-list)
113 (make-variable-buffer-local 'wl-draft-reply-buffer)
114 (make-variable-buffer-local 'wl-draft-parent-folder)
116 (defsubst wl-smtp-password-key (user mechanism server)
117 (format "SMTP:%s/%s@%s"
118 user mechanism server))
120 (defmacro wl-smtp-extension-bind (&rest body)
121 (` (let* ((smtp-sasl-mechanisms
122 (if wl-smtp-authenticate-type
124 (if (listp wl-smtp-authenticate-type)
125 wl-smtp-authenticate-type
126 (list wl-smtp-authenticate-type)))))
127 (smtp-use-sasl (and smtp-sasl-mechanisms t))
128 (smtp-use-starttls (eq wl-smtp-connection-type 'starttls))
129 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
130 (if (and (string= (car smtp-sasl-mechanisms) "DIGEST-MD5")
132 (string-match "^\\([^@]*\\)@\\([^@]*\\)"
133 wl-smtp-posting-user))
134 (setq smtp-sasl-user-name (match-string 1 wl-smtp-posting-user)
135 smtp-sasl-properties (list 'realm
136 (match-string 2 wl-smtp-posting-user)))
137 (setq smtp-sasl-user-name wl-smtp-posting-user
138 smtp-sasl-properties nil))
139 (setq sasl-read-passphrase
143 (wl-smtp-password-key
145 (car smtp-sasl-mechanisms)
149 (defun wl-draft-insert-date-field ()
151 (insert "Date: " (wl-make-date-string) "\n"))
153 (defun wl-draft-insert-from-field ()
155 ;; Put the "From:" field in unless for some odd reason
156 ;; they put one in themselves.
159 (setq from (wl-draft-eword-encode-address-list wl-from))
160 (error (error "Please look at `wl-from' again")))
161 (insert "From: " from "\n")))
163 (defun wl-draft-insert-x-face-field ()
164 "Insert X-Face header."
166 (if (not (file-exists-p wl-x-face-file))
167 (error "File %s does not exist" wl-x-face-file)
168 (goto-char (point-min))
169 (search-forward mail-header-separator nil t)
171 (wl-draft-insert-x-face-field-here)
172 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
174 (defun wl-draft-insert-x-face-field-here ()
175 "Insert X-Face field at point."
176 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
177 (when (string-match "^\\(X-Face:\\)?[ \t\n]*" x-face-string)
178 (setq x-face-string (substring x-face-string (match-end 0))))
179 (insert "X-Face: " x-face-string))
180 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
182 ;; Insert X-Face-Version: field
183 (when (and (fboundp 'x-face-insert-version-header)
184 (boundp 'x-face-add-x-face-version-header)
185 x-face-add-x-face-version-header)
186 (x-face-insert-version-header)))
188 (defun wl-draft-setup ()
189 (let ((field wl-draft-fields)
193 (list (cons (concat (car field) " ")
194 (concat (car field) " ")))))
195 (setq field (cdr field)))
197 (cons (cons (concat wl-draft-mime-bcc-field-name ": ")
198 (concat wl-draft-mime-bcc-field-name ": "))
200 (setq wl-draft-field-completion-list cl)
201 (setq wl-address-complete-header-regexp
203 (append wl-address-complete-header-list
204 (list (concat wl-draft-mime-bcc-field-name ":")))))))
206 (defun wl-draft-make-mail-followup-to (recipients)
207 (if (elmo-list-member
208 (or wl-user-mail-address-list
209 (list (wl-address-header-extract-address wl-from)))
211 (let ((rlist (elmo-list-delete
212 (or wl-user-mail-address-list
213 (list (wl-address-header-extract-address wl-from)))
217 (lambda (item) (string= (downcase elem)
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
234 (lambda (item) (string= (downcase elem)
237 ((elmo-list-member (append to cc) ; subscribed mailing-list
238 (mapcar 'downcase wl-subscribed-mailing-list))
239 (elmo-list-delete myself cc
242 (lambda (item) (string= (downcase elem)
247 (defun wl-draft-forward (original-subject summary-buf)
248 (let (references parent-folder)
249 (with-current-buffer summary-buf
250 (setq parent-folder (wl-summary-buffer-folder-name)))
251 (with-current-buffer (wl-message-get-original-buffer)
252 (setq references (nconc
253 (std11-field-bodies '("References" "In-Reply-To"))
254 (list (std11-field-body "Message-Id"))))
255 (setq references (delq nil references)
256 references (mapconcat 'identity references " ")
257 references (wl-draft-parse-msg-id-list-string references)
258 references (wl-delete-duplicates references)
259 references (when references
260 (mapconcat 'identity references "\n\t"))))
261 (and wl-draft-use-frame
262 (get-buffer-window summary-buf)
263 (select-window (get-buffer-window summary-buf)))
264 (wl-draft (list (cons 'To "")
266 (concat wl-forward-subject-prefix original-subject))
267 (cons 'References references))
268 nil nil nil nil parent-folder))
269 (goto-char (point-max))
270 (wl-draft-insert-message)
271 (mail-position-on-field "To"))
273 (defun wl-draft-strip-subject-re (subject)
274 "Remove \"Re:\" from SUBJECT string. Shamelessly copied from Gnus."
275 (if (string-match wl-subject-prefix-regexp subject)
276 (substring subject (match-end 0))
279 (defun wl-draft-self-reply-p ()
280 "Return t when From address in the current message is user's self one or not."
281 (wl-address-user-mail-address-p (or (elmo-field-body "From") "")))
283 (defun wl-draft-reply (buf with-arg summary-buf)
284 "Reply to BUF buffer message.
285 Reply to author if WITH-ARG is non-nil."
288 to mail-followup-to cc subject in-reply-to references newsgroups
289 to-alist cc-alist decoder parent-folder)
290 (when (buffer-live-p summary-buf)
291 (with-current-buffer summary-buf
292 (setq parent-folder (wl-summary-buffer-folder-name))))
293 (set-buffer (or buf mime-mother-buffer))
294 (setq r-list (if with-arg wl-draft-reply-with-argument-list
295 wl-draft-reply-without-argument-list))
298 (when (let ((condition (car (car r-list))))
299 (cond ((stringp condition)
300 (std11-field-body condition))
305 ((stringp (car condition))
306 (or (std11-field-body (car condition))
308 ((symbolp (car condition))
309 (or (funcall (car condition))
313 (setq condition (cdr condition)))
316 (funcall condition))))
317 (let ((r-to-list (nth 0 (cdr (car r-list))))
318 (r-cc-list (nth 1 (cdr (car r-list))))
319 (r-ng-list (nth 2 (cdr (car r-list)))))
320 (when (and (member "Followup-To" r-ng-list)
321 (string= (std11-field-body "Followup-To") "poster"))
322 (setq r-to-list (cons "From" r-to-list))
323 (setq r-ng-list (delete "Followup-To"
324 (copy-sequence r-ng-list))))
325 (if (and r-to-list (symbolp r-to-list))
326 (setq to (wl-concat-list (funcall r-to-list) ","))
327 (setq to (wl-concat-list (cons to
328 (elmo-multiple-fields-body-list
331 (if (and r-cc-list (symbolp r-cc-list))
332 (setq cc (wl-concat-list (funcall r-cc-list) ","))
333 (setq cc (wl-concat-list (cons cc
334 (elmo-multiple-fields-body-list
337 (if (and r-ng-list (symbolp r-ng-list))
338 (setq newsgroups (wl-concat-list (funcall r-ng-list) ","))
339 (setq newsgroups (wl-concat-list (cons newsgroups
344 (setq r-list (cdr r-list)))
345 (error "No match field: check your `wl-draft-reply-%s-argument-list'"
346 (if with-arg "with" "without")))
347 (setq subject (std11-field-body "Subject"))
348 (setq to (wl-parse-addresses to)
349 cc (wl-parse-addresses cc))
350 (with-temp-buffer ; to keep raw buffer unibyte.
351 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
352 (setq decoder (mime-find-field-decoder 'Subject 'plain))
353 (setq subject (if (and subject decoder)
354 (funcall decoder subject) subject))
358 (setq decoder (mime-find-field-decoder 'To 'plain))
359 (cons (nth 1 (std11-extract-address-components addr))
360 (if decoder (funcall decoder addr) addr)))
365 (setq decoder (mime-find-field-decoder 'Cc 'plain))
366 (cons (nth 1 (std11-extract-address-components addr))
367 (if decoder (funcall decoder addr) addr)))
369 (and wl-reply-subject-prefix
370 (setq subject (concat wl-reply-subject-prefix
371 (wl-draft-strip-subject-re
373 (setq in-reply-to (std11-field-body "Message-Id"))
374 (setq references (nconc
375 (std11-field-bodies '("References" "In-Reply-To"))
377 (setq to (delq nil (mapcar 'car to-alist)))
378 (setq cc (delq nil (mapcar 'car cc-alist)))
379 ;; if subscribed mailing list is contained in cc or to
380 ;; and myself is contained in cc,
381 ;; delete myself from cc.
382 (setq cc (wl-draft-delete-myself-from-cc to cc))
383 (when wl-insert-mail-followup-to
384 (setq mail-followup-to
385 (wl-draft-make-mail-followup-to (append to cc)))
386 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
387 (with-temp-buffer ; to keep raw buffer unibyte.
388 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
389 (setq newsgroups (wl-parse newsgroups
390 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
391 newsgroups (wl-delete-duplicates newsgroups)
396 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
397 (if decoder (funcall decoder grp) grp))
399 (setq to (wl-delete-duplicates to nil t))
400 (setq cc (wl-delete-duplicates
401 (append (wl-delete-duplicates cc nil t)
402 to (copy-sequence to))
404 (and to (setq to (mapconcat
406 (if wl-draft-reply-use-address-with-full-name
407 (or (cdr (assoc addr to-alist)) addr)
410 (and cc (setq cc (mapconcat
412 (if wl-draft-reply-use-address-with-full-name
413 (or (cdr (assoc addr cc-alist)) addr)
416 (and mail-followup-to
417 (setq mail-followup-to
420 (if wl-draft-reply-use-address-with-full-name
421 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
423 mail-followup-to ",\n\t")))
424 (and (null to) (setq to cc cc nil))
425 (setq references (delq nil references)
426 references (mapconcat 'identity references " ")
427 references (wl-draft-parse-msg-id-list-string references)
428 references (wl-delete-duplicates references)
429 references (if references
430 (mapconcat 'identity references "\n\t")))
431 (and wl-draft-use-frame
432 (get-buffer-window summary-buf)
433 (select-window (get-buffer-window summary-buf)))
434 (wl-draft (list (cons 'To to)
436 (cons 'Newsgroups newsgroups)
437 (cons 'Subject subject)
438 (cons 'In-Reply-To in-reply-to)
439 (cons 'References references)
440 (cons 'Mail-Followup-To mail-followup-to))
441 nil nil nil nil parent-folder)
442 (setq wl-draft-reply-buffer buf)
443 (run-hooks 'wl-reply-hook)))
445 (defun wl-draft-reply-position (position)
446 (cond ((eq position 'body)
447 (wl-draft-body-goto-top))
448 ((eq position 'bottom)
449 (wl-draft-body-goto-bottom))
451 (goto-char (point-min)))
452 ((and (stringp position)
453 (std11-field-body position))
454 (progn (mail-position-on-field position)
455 (wl-draft-beginning-of-line)))
457 (while (car position)
458 (wl-draft-reply-position (car position))
459 (setq position (cdr position))))))
461 (defun wl-draft-add-references ()
462 (wl-draft-add-in-reply-to "References"))
464 (defun wl-draft-add-in-reply-to (&optional alt-field)
465 (let* ((mes-id (save-excursion
466 (set-buffer mail-reply-buffer)
467 (std11-field-body "message-id")))
468 (field (or alt-field "In-Reply-To"))
469 (ref (std11-field-body field))
470 (ref-list nil) (st nil))
471 (when (and mes-id ref)
472 (while (string-match "<[^>]+>" ref st)
474 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
477 (member mes-id ref-list))
481 (when (mail-position-on-field field)
483 (while (looking-at "^[ \t]")
485 (setq mes-id (concat "\t" mes-id "\n")))
489 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
490 &optional ignored-fields)
493 (narrow-to-region (point)(point))
495 (with-current-buffer mail-reply-buffer
497 (decode-mime-charset-region (point-min) (point-max)
499 (buffer-substring-no-properties
500 (point-min) (point-max))))
502 (goto-char (point-min))
503 (wl-draft-delete-fields ignored-fields))
504 (goto-char (point-max))
505 (push-mark (point) nil t)
506 (goto-char (point-min)))
508 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
509 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
510 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
511 (run-hooks 'wl-draft-cited-hook)
512 (when (if wl-draft-add-references
513 (wl-draft-add-references)
514 (if wl-draft-add-in-reply-to
515 (wl-draft-add-in-reply-to)))
516 (wl-highlight-headers 'for-draft)) ; highlight when added References:
517 (when wl-highlight-body-too
518 (wl-highlight-body-region beg (point-max)))))
520 (defun wl-message-news-p ()
521 "If exist valid Newsgroups field, return non-nil."
522 (std11-field-body "Newsgroups"))
524 (defun wl-message-field-exists-p (field)
525 "If FIELD exist and FIELD value is not empty, return non-nil."
526 (let ((value (std11-field-body field)))
528 (not (string= value "")))))
530 (defun wl-message-mail-p ()
531 "If exist To, Cc or Bcc field, return non-nil."
532 (or (wl-message-field-exists-p "To")
533 (wl-message-field-exists-p "Resent-to")
534 (wl-message-field-exists-p "Cc")
535 (wl-message-field-exists-p "Bcc")
536 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
537 ;;; This may be needed..
538 ;;; (wl-message-field-exists-p "Fcc")
541 (defun wl-draft-edit-string (string)
542 (let ((cur-buf (current-buffer))
543 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
544 to subject in-reply-to cc references newsgroups mail-followup-to
545 content-type content-transfer-encoding from
550 (setq to (std11-field-body "To"))
553 (decode-mime-charset-string
556 (setq subject (std11-field-body "Subject"))
557 (setq subject (and subject
559 (decode-mime-charset-string
562 (setq from (std11-field-body "From")
565 (decode-mime-charset-string
568 (setq in-reply-to (std11-field-body "In-Reply-To"))
569 (setq cc (std11-field-body "Cc"))
572 (decode-mime-charset-string
575 (setq references (std11-field-body "References"))
576 (setq newsgroups (std11-field-body "Newsgroups"))
577 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
578 (setq content-type (std11-field-body "Content-Type"))
579 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
580 (goto-char (point-min))
581 (or (re-search-forward "\n\n" nil t)
582 (search-forward (concat mail-header-separator "\n") nil t))
588 (nth 1 (std11-extract-address-components from))
589 wl-user-mail-address-list)
593 (cons 'Subject subject)
594 (cons 'Newsgroups newsgroups)
595 (cons 'Mail-Followup-To mail-followup-to)
596 (cons 'In-Reply-To in-reply-to)
597 (cons 'References references))
598 content-type content-transfer-encoding
599 (buffer-substring (point) (point-max))
601 (kill-buffer tmp-buf))
602 ;; Set cursor point to the top.
603 (goto-char (point-min))
604 (search-forward (concat mail-header-separator "\n") nil t)
605 (run-hooks 'wl-draft-reedit-hook)
606 (and to (mail-position-on-field "To"))))
608 (defun wl-draft-insert-current-message (dummy)
610 (let (original-buffer
612 mail-citation-hook mail-yank-hooks
613 wl-draft-add-references wl-draft-add-in-reply-to
614 wl-draft-cite-function)
615 (with-current-buffer wl-draft-buffer-cur-summary-buffer
616 (with-current-buffer wl-message-buffer
617 (setq original-buffer (wl-message-get-original-buffer))
619 (with-current-buffer original-buffer
621 (error "No current message"))))
622 (setq mail-reply-buffer original-buffer)
623 (wl-draft-yank-from-mail-reply-buffer
625 wl-ignored-forwarded-headers)))
627 (defun wl-draft-insert-get-message (dummy)
628 (let ((fld (completing-read
630 (if (memq 'read-folder wl-use-folder-petname)
631 (wl-folder-get-entity-with-petname)
632 wl-folder-entity-hashtb)
633 nil nil wl-default-spec
634 'wl-read-folder-hist))
635 (number (call-interactively
636 (function (lambda (num)
637 (interactive "nNumber: ")
639 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
640 mail-citation-hook mail-yank-hooks
641 wl-draft-cite-function)
644 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
647 (elmo-make-fetch-strategy 'entire)
648 nil mail-reply-buffer)
649 (wl-draft-yank-from-mail-reply-buffer nil))
650 (kill-buffer mail-reply-buffer))))
653 ;; default body citation func
655 (defun wl-default-draft-cite ()
656 (let ((mail-yank-ignored-headers "[^:]+:")
657 (mail-yank-prefix "> ")
658 (summary-buf wl-current-summary-buffer)
659 (message-buf (get-buffer (wl-current-message-buffer)))
660 from date cite-title num entity)
662 (buffer-live-p summary-buf)
664 (buffer-live-p message-buf))
666 (with-current-buffer summary-buf
667 (let ((elmo-mime-charset wl-summary-buffer-mime-charset))
668 (setq num (save-excursion
669 (set-buffer message-buf)
670 wl-message-buffer-cur-number))
671 (setq entity (elmo-msgdb-overview-get-entity
672 num (wl-summary-buffer-msgdb)))
673 (setq date (elmo-msgdb-overview-entity-get-date entity))
674 (setq from (elmo-msgdb-overview-entity-get-from entity))))
675 (setq cite-title (format "At %s,\n%s wrote:"
676 (or date "some time ago")
677 (if wl-default-draft-cite-decorate-author
678 (funcall wl-summary-from-function
682 (insert cite-title "\n"))
683 (mail-indent-citation)))
685 (defvar wl-draft-buffer nil "Draft buffer to yank content.")
686 (defun wl-draft-yank-to-draft-buffer (buffer)
687 "Yank BUFFER content to `wl-draft-buffer'."
688 (set-buffer wl-draft-buffer)
689 (let ((mail-reply-buffer buffer))
690 (wl-draft-yank-from-mail-reply-buffer nil)
691 (kill-buffer buffer)))
693 (defun wl-draft-yank-original (&optional arg)
694 "Yank original message."
697 (let (buf mail-reply-buffer)
700 (setq buf (current-buffer)))
701 (setq mail-reply-buffer buf)
702 (wl-draft-yank-from-mail-reply-buffer nil))
703 (wl-draft-yank-current-message-entity)))
705 (defun wl-draft-hide (editing-buffer)
706 "Hide the editing draft buffer if possible."
707 (when (and editing-buffer
708 (buffer-live-p editing-buffer))
709 (set-buffer editing-buffer)
710 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
711 fld-buf sum-win fld-win)
712 (if (and wl-draft-use-frame
713 (> (length (visible-frame-list)) 1))
719 ;; stay folder window if required
720 (when wl-stay-folder-window
721 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
722 (if (setq fld-win (get-buffer-window fld-buf))
723 (select-window fld-win)
724 (if wl-draft-resume-folder-window ;; resume folder window
725 (switch-to-buffer fld-buf)))))
726 (if (buffer-live-p sum-buf)
727 (if (setq sum-win (get-buffer-window sum-buf t))
728 ;; if Summary is on the frame, select it.
729 (select-window sum-win)
730 ;; if summary is not on the frame, switch to it.
731 (if (and wl-stay-folder-window
732 (or wl-draft-resume-folder-window fld-win))
733 (wl-folder-select-buffer sum-buf)
734 (switch-to-buffer sum-buf))))))))
736 (defun wl-draft-delete (editing-buffer)
737 "Kill the editing draft buffer and delete the file corresponds to it."
740 (set-buffer editing-buffer)
741 (if wl-draft-buffer-file-name
743 (if (file-exists-p wl-draft-buffer-file-name)
744 (delete-file wl-draft-buffer-file-name))
745 (let ((msg (and wl-draft-buffer-file-name
746 (string-match "[0-9]+$" wl-draft-buffer-file-name)
748 (match-string 0 wl-draft-buffer-file-name)))))
749 (wl-draft-config-info-operation msg 'delete))))
750 (set-buffer-modified-p nil) ; force kill
751 (kill-buffer editing-buffer))))
753 (defun wl-draft-kill (&optional force-kill)
754 "Kill current draft buffer and quit editing."
757 (when (and (or (eq major-mode 'wl-draft-mode)
758 (eq major-mode 'mail-mode))
760 (y-or-n-p "Kill Current Draft? ")))
761 (let ((cur-buf (current-buffer)))
762 (wl-draft-hide cur-buf)
763 (wl-draft-delete cur-buf)))
766 (defun wl-draft-fcc ()
767 "Add a new Fcc field, with file name completion."
769 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
770 (mail-position-on-field "to"))
773 ;; Imported from message.el.
774 (defun wl-draft-elide-region (b e)
775 "Elide the text in the region.
776 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
780 (insert wl-draft-elide-ellipsis))
782 ;; Imported from message.el.
783 (defun wl-draft-beginning-of-line (&optional n)
784 "Move point to beginning of header value or to beginning of line."
786 (let ((zrs 'zmacs-region-stays))
787 (when (and (interactive-p) (boundp zrs))
789 (if (wl-draft-point-in-header-p)
790 (let* ((here (point))
791 (bol (progn (beginning-of-line n) (point)))
792 (eol (line-end-position))
793 (eoh (and (looking-at "[^ \t]")
794 (re-search-forward ": *" eol t))))
795 (if (and eoh (or (> here eoh) (= here bol)))
798 (beginning-of-line n)))
800 (defun wl-draft-point-in-header-p ()
801 "Return t if point is in the header."
804 (goto-char (point-min))
805 (not (re-search-forward
806 (concat "^" (regexp-quote mail-header-separator) "\n")
809 ;; function for wl-sent-message-via
811 (defmacro wl-draft-sent-message-p (type)
812 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
814 (defmacro wl-draft-set-sent-message (type result &optional server-port)
815 (` (let ((element (assq (, type) wl-sent-message-via)))
817 (unless (eq (nth 1 element) (, result))
818 (setcdr element (list (, result) (, server-port)))
819 (setq wl-sent-message-modified t))
820 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
821 (setq wl-sent-message-modified t)))))
823 (defun wl-draft-sent-message-results ()
824 (let ((results wl-sent-message-via)
825 unplugged-via sent-via)
827 (cond ((eq (nth 1 (car results)) 'unplugged)
828 (push (caar results) unplugged-via))
829 ((eq (nth 1 (car results)) 'sent)
830 (push (caar results) sent-via)))
831 (setq results (cdr results)))
832 (list unplugged-via sent-via)))
834 (defun wl-draft-write-sendlog (status proto server to id)
835 "Write send log file, if `wl-draft-sendlog' is non-nil."
836 (when wl-draft-sendlog
838 (let* ((filename (expand-file-name wl-draft-sendlog-filename
839 elmo-msgdb-directory))
840 (filesize (nth 7 (file-attributes filename)))
841 (server (if server (concat " server=" server) ""))
843 ((memq proto '(fcc queue))
844 (format " folder=\"%s\"" to))
846 (format " ng=<%s>" to))
851 (mapcar '(lambda(x) (format "<%s>" x)) to)
854 (id (if id (concat " id=" id) ""))
855 (time (format-time-string "%Y/%m/%d %T")))
856 (insert (format "%s proto=%s stat=%s%s%s%s\n"
857 time proto status server to id))
858 (if (and wl-draft-sendlog-max-size filesize
859 (> filesize wl-draft-sendlog-max-size))
860 (rename-file filename (concat filename ".old") t))
861 (if (file-writable-p filename)
862 (write-region-as-binary (point-min) (point-max)
864 (message "%s is not writable." filename))))))
866 (defun wl-draft-get-header-delimiter (&optional delete)
867 ;; If DELETE is non-nil, replace the header delimiter with a blank line
869 (goto-char (point-min))
870 (when (re-search-forward
871 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
875 (setq delimline (point-marker)))
878 (defun wl-draft-send-mail-with-qmail ()
879 "Pass the prepared message buffer to qmail-inject.
880 Refer to the documentation for the variable `send-mail-function'
881 to find out how to use this."
882 (if (and wl-draft-qmail-send-plugged
883 (not (elmo-plugged-p)))
884 (wl-draft-set-sent-message 'mail 'unplugged)
886 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
887 (let ((id (std11-field-body "Message-ID"))
888 (to (std11-field-body "To")))
892 'call-process-region 1 (point-max) wl-qmail-inject-program
894 wl-qmail-inject-args))
895 ;; qmail-inject doesn't say anything on it's stdout/stderr,
896 ;; we have to look at the retval instead
898 (wl-draft-set-sent-message 'mail 'sent)
899 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
900 (1 (error "`qmail-inject' reported permanent failure"))
901 (111 (error "`qmail-inject' reported transient failure"))
902 ;; should never happen
903 (t (error "`qmail-inject' reported unknown failure"))))))
905 (defun wl-draft-parse-msg-id-list-string (string)
906 "Get msg-id list from STRING."
908 (dolist (parsed-id (std11-parse-msg-ids-string string))
909 (when (eq (car parsed-id) 'msg-id)
910 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
912 (nreverse msg-id-list)))
914 (defun wl-draft-eword-encode-address-list (string &optional column)
915 "Encode header field STRING as list of address, and return the result.
916 Cause an error when STRING contains invalid address.
917 Optional argument COLUMN is start-position of the field."
918 (car (eword-encode-rword-list
919 (or column eword-encode-default-start-column)
920 (eword-encode-addresses-to-rword-list
921 (wl-draft-std11-parse-addresses (std11-lexical-analyze string))))))
923 (defun wl-draft-std11-parse-addresses (lal)
924 (let ((ret (std11-parse-address lal)))
925 (when (and (not (and (eq (length lal) 1)
926 (eq (car (car lal)) 'spaces)))
928 (error "Error while parsing address"))
930 (let ((dest (list (car ret))))
932 (while (and (setq ret (std11-parse-ascii-token lal))
933 (string-equal (cdr (assq 'specials (car ret))) ",")
934 (setq ret (std11-parse-address (cdr ret)))
936 (setq dest (cons (car ret) dest))
937 (setq lal (cdr ret)))
938 (while (eq 'spaces (car (car lal)))
939 (setq lal (cdr lal)))
940 (if lal (error "Error while parsing address"))
943 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
944 "Get mailbox list of FIELD from current buffer.
945 The buffer is expected to be narrowed to just the headers of the message.
946 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
947 from current buffer."
949 (let ((case-fold-search t)
950 (inhibit-read-only t)
952 mailbox-list beg seq has-group-list)
953 (goto-char (point-min))
954 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
957 (re-search-forward "^[^ \t]" nil 'move)
959 (skip-chars-backward "\n")
960 (setq seq (std11-lexical-analyze
961 (buffer-substring-no-properties beg (point))))
962 (setq addresses (wl-draft-std11-parse-addresses seq))
964 (cond ((eq (car (car addresses)) 'group)
965 (setq has-group-list t)
969 'std11-address-string
970 (nth 2 (car addresses))))))
971 ((eq (car (car addresses)) 'mailbox)
972 (setq address (nth 1 (car addresses)))
976 (std11-addr-to-string
977 (if (eq (car address) 'phrase-route-addr)
980 (setq addresses (cdr addresses)))
981 (when (and remove-group-list has-group-list)
982 (delete-region beg (point))
983 (insert (wl-address-string-without-group-list-contents seq))))
986 (defun wl-draft-deduce-address-list (buffer header-start header-end)
987 "Get address list suitable for smtp RCPT TO:<address>.
988 Group list content is removed if `wl-draft-remove-group-list-contents' is
990 (let ((fields (if (and wl-draft-doing-mime-bcc
991 wl-draft-disable-bcc-for-mime-bcc)
994 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
999 (narrow-to-region header-start header-end)
1000 (goto-char (point-min))
1002 (if (re-search-forward "^resent-to[\t ]*:" nil t)
1003 (setq fields resent-fields)))
1007 (wl-draft-parse-mailbox-list
1009 wl-draft-remove-group-list-contents)))
1010 (setq fields (cdr fields)))
1016 (defun wl-draft-send-mail-with-smtp ()
1017 "Send the prepared message buffer with SMTP."
1019 (let* ((errbuf (if mail-interactive
1020 (generate-new-buffer " smtp errors")
1022 (case-fold-search t)
1023 (default-case-fold-search t)
1024 (sender (or wl-envelope-from
1025 (wl-address-header-extract-address wl-from)))
1026 (delimline (save-excursion
1027 (goto-char (point-min))
1029 (concat "^" (regexp-quote mail-header-separator)
1033 (or wl-smtp-posting-server smtp-server "localhost"))
1034 (smtp-service (or wl-smtp-posting-port smtp-service))
1035 (smtp-local-domain (or smtp-local-domain wl-local-domain))
1036 (id (std11-field-body "message-id"))
1038 (if (not (elmo-plugged-p smtp-server smtp-service))
1039 (wl-draft-set-sent-message 'mail 'unplugged
1040 (cons smtp-server smtp-service))
1043 ;; Instead of `smtp-deduce-address-list'.
1044 (setq recipients (wl-draft-deduce-address-list
1045 (current-buffer) (point-min) delimline))
1046 (unless recipients (error "No recipients"))
1047 ;; Insert an extra newline if we need it to work around
1048 ;; Sun's bug that swallows newlines.
1049 (goto-char (1+ delimline))
1050 (if (eval mail-mailer-swallows-blank-line)
1052 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1053 (if mail-interactive
1057 (wl-draft-delete-field "bcc" delimline)
1058 (wl-draft-delete-field "resent-bcc" delimline)
1059 (let (process-connection-type)
1062 (wl-smtp-extension-bind
1064 (smtp-send-buffer sender recipients (current-buffer))
1066 (wl-draft-write-sendlog 'failed 'smtp smtp-server
1068 (if (and (eq (car err) 'smtp-response-error)
1069 (= (nth 1 err) 535))
1071 (wl-smtp-password-key
1073 (car smtp-sasl-mechanisms)
1075 (signal (car err) (cdr err)))
1077 (wl-draft-write-sendlog 'uncertain 'smtp smtp-server
1079 (signal (car err) (cdr err)))))
1080 (wl-draft-set-sent-message 'mail 'sent)
1081 (wl-draft-write-sendlog
1082 'ok 'smtp smtp-server recipients id)))))
1083 (if (bufferp errbuf)
1084 (kill-buffer errbuf))))))
1086 (defun wl-draft-send-mail-with-pop-before-smtp ()
1087 "Send the prepared message buffer with POP-before-SMTP."
1088 (require 'elmo-pop3)
1092 :user (or wl-pop-before-smtp-user
1093 elmo-pop3-default-user)
1094 :server (or wl-pop-before-smtp-server
1095 elmo-pop3-default-server)
1096 :port (or wl-pop-before-smtp-port
1097 elmo-pop3-default-port)
1098 :auth (or wl-pop-before-smtp-authenticate-type
1099 elmo-pop3-default-authenticate-type)
1100 :stream-type (or wl-pop-before-smtp-stream-type
1101 elmo-pop3-default-stream-type))))
1102 (condition-case error
1104 (elmo-pop3-get-session session)
1105 (when session (elmo-network-close-session session)))
1107 (elmo-network-close-session session)
1108 (unless (string= (nth 1 error) "Unplugged")
1109 (signal (car error)(cdr error))))))
1110 (wl-draft-send-mail-with-smtp))
1112 (defun wl-draft-insert-required-fields (&optional force-msgid)
1113 "Insert Message-ID, Date, and From field.
1114 If FORCE-MSGID, insert message-id regardless of `wl-insert-message-id'."
1115 ;; Insert Message-Id field...
1116 (goto-char (point-min))
1117 (when (and (or force-msgid
1118 wl-insert-message-id)
1119 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1120 (insert (concat "Message-ID: "
1121 (funcall wl-message-id-function)
1123 ;; Insert date field.
1124 (goto-char (point-min))
1125 (or (re-search-forward "^Date[ \t]*:" nil t)
1126 (wl-draft-insert-date-field))
1127 ;; Insert from field.
1128 (goto-char (point-min))
1129 (or (re-search-forward "^From[ \t]*:" nil t)
1130 (wl-draft-insert-from-field)))
1132 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1133 "Send the message in the current buffer."
1135 (narrow-to-region (goto-char (point-min))
1136 (if (re-search-forward
1138 "^" (regexp-quote mail-header-separator) "$")
1142 ;; ignore any blank lines in the header
1143 (while (progn (goto-char (point-min))
1144 (re-search-forward "\n[ \t]*\n\n*" nil t))
1145 (replace-match "\n"))
1146 (goto-char (point-min))
1147 (while (re-search-forward
1148 "^[^ \t\n:]+:[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n"
1150 (when (string= "" (match-string 1))
1151 (replace-match ""))))
1152 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1153 (wl-draft-dispatch-message)
1154 (when kill-when-done
1155 ;; hide editing-buffer.
1156 (wl-draft-hide editing-buffer)
1157 ;; delete editing-buffer and its file.
1158 (wl-draft-delete editing-buffer)))
1160 (defun wl-draft-dispatch-message (&optional mes-string)
1161 "Send the message in the current buffer. Not modified the header fields."
1162 (let (delimline mime-bcc)
1163 (if (and wl-draft-verbose-send mes-string)
1164 (message "%s" mes-string))
1166 (setq delimline (wl-draft-get-header-delimiter t))
1167 (unless wl-draft-fcc-list
1168 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1170 (setq wl-sent-message-modified nil)
1173 (if (and (wl-message-mail-p)
1174 (not (wl-draft-sent-message-p 'mail)))
1175 (if (or (not (or wl-draft-force-queuing
1176 wl-draft-force-queuing-mail))
1177 (memq 'mail wl-sent-message-queued))
1179 (setq mime-bcc (wl-draft-mime-bcc-field))
1180 (funcall wl-draft-send-mail-function)
1181 (when (not (zerop (length mime-bcc)))
1182 (wl-draft-do-mime-bcc mime-bcc)))
1183 (push 'mail wl-sent-message-queued)
1184 (wl-draft-set-sent-message 'mail 'unplugged)))
1185 (if (and (wl-message-news-p)
1186 (not (wl-draft-sent-message-p 'news))
1187 (not (wl-message-field-exists-p "Resent-to")))
1188 (if (or (not (or wl-draft-force-queuing
1189 wl-draft-force-queuing-news))
1190 (memq 'news wl-sent-message-queued))
1191 (funcall wl-draft-send-news-function)
1192 (push 'news wl-sent-message-queued)
1193 (wl-draft-set-sent-message 'news 'unplugged))))
1194 (let* ((status (wl-draft-sent-message-results))
1195 (unplugged-via (car status))
1196 (sent-via (nth 1 status)))
1197 ;; If one sent, process fcc folder.
1198 (if (and sent-via wl-draft-fcc-list)
1200 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1202 (setq wl-draft-fcc-list nil)))
1203 (if wl-draft-use-cache
1204 (let ((id (std11-field-body "Message-ID"))
1205 (elmo-enable-disconnected-operation t))
1206 (elmo-file-cache-save (elmo-file-cache-get-path id)
1208 ;; If one unplugged, append queue.
1209 (when (and unplugged-via
1210 wl-sent-message-modified)
1211 (if wl-draft-enable-queuing
1213 (wl-draft-queue-append wl-sent-message-via)
1214 (setq wl-sent-message-modified 'requeue))
1215 (error "Unplugged")))
1216 (when wl-draft-verbose-send
1217 (if (and unplugged-via sent-via);; combined message
1219 (setq wl-draft-verbose-msg
1220 (format "Sending%s and Queuing%s..."
1221 sent-via unplugged-via))
1222 (message "%sdone" wl-draft-verbose-msg))
1226 (if sent-via "done" "failed"))))))))
1227 (not wl-sent-message-modified)) ;; return value
1229 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1230 "Force send current buffer as raw message."
1233 (let (wl-interactive-send
1234 ;;; wl-draft-verbose-send
1235 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1236 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1239 (wl-draft-send kill-when-done mes-string))))
1241 (defun wl-draft-clone-local-variables ()
1242 (let ((locals (buffer-local-variables))
1245 (when (and (consp (car locals))
1247 (string-match wl-draft-clone-local-variable-regexp
1248 (symbol-name (car (car locals)))))
1249 (wl-append result (list (car (car locals)))))
1250 (setq locals (cdr locals)))
1253 (defcustom wl-draft-send-confirm-with-preview t
1254 "Non-nil to invoke preview through confirmation of sending.
1255 This variable is valid when `wl-interactive-send' has non-nil value."
1259 (defun wl-draft-send-confirm ()
1262 (condition-case quit
1264 (when wl-draft-send-confirm-with-preview
1265 (wl-draft-preview-message))
1267 (goto-char (point-min)) ; to show recipients in header
1270 (message "Send current draft? <y/n/j(up)/k(down)> ")
1271 (setq answer (let ((cursor-in-echo-area t)) (read-char)))
1290 (throw 'done nil)))))))
1292 (when wl-draft-send-confirm-with-preview
1293 (mime-preview-quit)))))
1295 (defun wl-draft-send (&optional kill-when-done mes-string)
1296 "Send current draft message.
1297 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1299 ;; Don't call this explicitly.
1300 ;; Added to 'wl-draft-send-hook (by teranisi)
1301 ;; (wl-draft-config-exec)
1302 (run-hooks 'wl-draft-send-hook)
1303 (when (or (not wl-interactive-send)
1304 (wl-draft-send-confirm))
1305 (let ((send-mail-function 'wl-draft-raw-send)
1306 (editing-buffer (current-buffer))
1307 (sending-buffer (wl-draft-generate-clone-buffer
1308 " *wl-draft-sending-buffer*"
1309 (append wl-draft-config-variables
1310 (wl-draft-clone-local-variables))))
1311 (wl-draft-verbose-msg nil)
1315 (set-buffer sending-buffer)
1316 (if (and (not (wl-message-mail-p))
1317 (not (wl-message-news-p)))
1318 (error "No recipient is specified"))
1319 (expand-abbrev) ; for mail-abbrevs
1320 (let ((mime-header-encode-method-alist
1322 '((wl-draft-eword-encode-address-list
1323 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
1324 (if (boundp 'mime-header-encode-method-alist)
1325 (symbol-value 'mime-header-encode-method-alist)))))
1326 (run-hooks 'mail-send-hook) ; translate buffer
1329 (if wl-draft-verbose-send
1330 (message "%s" (or mes-string "Sending...")))
1331 (funcall wl-draft-send-function editing-buffer kill-when-done)
1332 ;; Now perform actions on successful sending.
1333 (while mail-send-actions
1335 (apply (car (car mail-send-actions))
1336 (cdr (car mail-send-actions)))
1338 (setq mail-send-actions (cdr mail-send-actions)))
1339 (if wl-draft-verbose-send
1341 (or wl-draft-verbose-msg
1344 ;; kill sending buffer, anyway.
1345 (and (buffer-live-p sending-buffer)
1346 (kill-buffer sending-buffer))))))
1348 (defun wl-draft-mime-bcc-field ()
1349 "Return the MIME-Bcc field body. The field is deleted."
1350 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1351 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1353 (defun wl-draft-do-mime-bcc (field-body)
1354 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1355 (let ((orig-from (mime-decode-field-body (std11-field-body "from")
1357 (orig-subj (mime-decode-field-body (or (std11-field-body "subject")
1360 (recipients (wl-parse-addresses field-body))
1361 (draft-buffer (current-buffer))
1363 (save-window-excursion
1364 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1365 (not (zerop (length field-body))))
1366 (let ((wl-draft-doing-mime-bcc t))
1367 (dolist (recipient recipients)
1368 (wl-draft-create-buffer)
1369 (wl-draft-create-contents
1370 (append `((From . ,orig-from)
1372 (Subject . ,(concat "A blind carbon copy ("
1375 (wl-draft-default-headers)))
1376 (wl-draft-insert-mail-header-separator)
1377 (wl-draft-prepare-edit)
1378 (goto-char (point-max))
1379 (insert (or wl-draft-mime-bcc-body
1380 "This is a blind carbon copy.")
1382 (mime-edit-insert-tag "message" "rfc822")
1383 (insert-buffer draft-buffer)
1384 (let (wl-interactive-send)
1385 (wl-draft-send 'kill-when-done))))))))
1387 ;; Derived from `message-save-drafts' in T-gnus.
1388 (defun wl-draft-save ()
1389 "Save current draft."
1391 (if (buffer-modified-p)
1393 (message "Saving %s..." wl-draft-buffer-file-name)
1394 (let ((msg (buffer-substring-no-properties (point-min) (point-max))))
1395 (with-temp-file wl-draft-buffer-file-name
1397 ;; If no header separator, insert it.
1399 (goto-char (point-min))
1400 (unless (re-search-forward
1401 (concat "^" (regexp-quote mail-header-separator) "$")
1403 (goto-char (point-min))
1404 (if (re-search-forward "\n\n" nil t)
1405 (replace-match (concat "\n" mail-header-separator "\n"))
1406 (goto-char (point-max))
1407 (insert (if (eq (char-before) ?\n) "" "\n")
1408 mail-header-separator "\n"))))
1409 (let ((mime-header-encode-method-alist
1410 '((eword-encode-unstructured-field-body))))
1411 (mime-edit-translate-buffer))
1412 (wl-draft-get-header-delimiter t)))
1413 (set-buffer-modified-p nil)
1414 (wl-draft-config-info-operation
1415 (and (string-match "[0-9]+$" wl-draft-buffer-file-name)
1417 (match-string 0 wl-draft-buffer-file-name)))
1419 (message "Saving %s...done" wl-draft-buffer-file-name))
1420 (message "(No changes need to be saved)")))
1422 (defun wl-draft-mimic-kill-buffer ()
1423 "Kill the current (draft) buffer with query."
1425 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1428 (if (or (not bufname)
1429 (string-equal bufname "")
1430 (string-equal bufname (buffer-name)))
1431 (let ((bufname (current-buffer)))
1432 (when (or (not (buffer-modified-p))
1434 (format "Buffer %s modified; kill anyway? " bufname)))
1435 (set-buffer-modified-p nil)
1436 (wl-draft-hide bufname)
1437 (kill-buffer bufname)))
1438 (kill-buffer bufname))))
1440 (defun wl-draft-save-and-exit ()
1441 "Save current draft and exit current draft mode."
1444 (let ((editing-buffer (current-buffer)))
1445 (wl-draft-hide editing-buffer)
1446 (kill-buffer editing-buffer)))
1448 (defun wl-draft-send-and-exit ()
1449 "Send current draft message and kill it."
1453 (defun wl-draft-send-from-toolbar ()
1455 (let ((wl-interactive-send t))
1456 (wl-draft-send-and-exit)))
1458 (defun wl-draft-delete-field (field &optional delimline replace)
1459 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1461 (defun wl-draft-delete-fields (field &optional delimline replace)
1464 (goto-char (point-min))
1465 (if (search-forward "\n\n" nil t)
1466 (setq delimline (point))
1467 (setq delimline (point-max))))
1468 (narrow-to-region (point-min) delimline)
1469 (goto-char (point-min))
1470 (let ((regexp (concat "^" field ":"))
1471 (case-fold-search t))
1473 (if (looking-at regexp)
1479 (if (re-search-forward "^[^ \t]" nil t)
1480 (goto-char (match-beginning 0))
1483 (insert (concat field ": " replace "\n"))))
1485 (if (re-search-forward "^[^ \t]" nil t)
1486 (goto-char (match-beginning 0))
1489 (defun wl-draft-get-fcc-list (header-end)
1490 (if (and wl-draft-doing-mime-bcc
1491 wl-draft-disable-fcc-for-mime-bcc)
1493 (wl-draft-delete-field "fcc")
1496 (case-fold-search t))
1497 (or (markerp header-end) (error "HEADER-END must be a marker"))
1499 (goto-char (point-min))
1500 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1505 (buffer-substring-no-properties
1509 (skip-chars-backward " \t")
1512 (dolist (folder fcc-list)
1513 (wl-folder-confirm-existence
1514 (wl-folder-get-elmo-folder (eword-decode-string folder)))))
1515 (delete-region (match-beginning 0)
1516 (progn (forward-line 1) (point)))))
1519 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1520 (let ((send-mail-buffer (current-buffer))
1521 (tembuf (generate-new-buffer " fcc output"))
1522 (case-fold-search t)
1524 (or (markerp header-end) (error "HEADER-END must be a marker"))
1527 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1530 ;; insert just the headers to avoid moving the gap more than
1531 ;; necessary (the message body could be arbitrarily huge.)
1532 (insert-buffer-substring send-mail-buffer 1 header-end)
1533 (wl-draft-insert-required-fields t)
1534 (goto-char (point-max))
1535 (insert-buffer-substring send-mail-buffer header-end)
1536 (let ((id (std11-field-body "Message-ID"))
1537 (elmo-enable-disconnected-operation t)
1540 (unless (or cache-saved
1541 (elmo-folder-plugged-p
1542 (wl-folder-get-elmo-folder (car fcc-list))))
1543 (elmo-file-cache-save id nil) ;; for disconnected operation
1544 (setq cache-saved t))
1545 (if (elmo-folder-append-buffer
1546 (wl-folder-get-elmo-folder
1547 (eword-decode-string (car fcc-list)))
1548 (not wl-fcc-force-as-read))
1549 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1550 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1551 (setq fcc-list (cdr fcc-list)))))
1552 (kill-buffer tembuf)))
1554 (defun wl-draft-on-field-p ()
1557 (goto-char (point-min))
1558 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1565 (if (or (looking-at ".*,[ \t]?$")
1566 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1568 (let ((pos (point)))
1571 (if (looking-at "^[ \t]")
1573 (if (re-search-forward ":" pos t) nil t)))))))
1577 (defun wl-draft (&optional header-alist
1578 content-type content-transfer-encoding
1581 "Write and send mail/news message with Wanderlust."
1588 (wl-plugged-init t))
1590 (wl-init)) ; returns immediately if already initialized.
1593 (let (buf-name header-alist-internal)
1595 (wl-draft-create-buffer parent-folder))
1597 (unless (cdr (assq 'From header-alist))
1599 (append (list (cons 'From wl-from)) header-alist)))
1600 (unless (cdr (assq 'To header-alist))
1605 (if (assq 'To header-alist)
1606 (setcdr (assq 'To header-alist) to)
1608 (append header-alist
1609 (list (cons 'To to))))))))
1610 (unless (cdr (assq 'Subject header-alist))
1611 (if (assq 'Subject header-alist)
1612 (setcdr (assq 'Subject header-alist) "")
1614 (append header-alist (list (cons 'Subject ""))))))
1615 (setq header-alist (append header-alist
1616 (wl-draft-default-headers)
1617 wl-draft-additional-header-alist
1618 (if body (list "" (cons 'Body body)))))
1619 (wl-draft-create-contents header-alist)
1621 (wl-draft-decode-body
1622 content-type content-transfer-encoding))
1623 (wl-draft-insert-mail-header-separator)
1624 (wl-draft-prepare-edit)
1626 (run-hooks 'wl-mail-setup-hook))
1627 (goto-char (point-min))
1628 (wl-user-agent-compose-internal) ;; user-agent
1631 (string= (cdr (assq 'To header-alist)) ""))
1632 (mail-position-on-field "To"))
1634 (goto-char (point-max))))
1637 (defun wl-draft-create-buffer (&optional parent-folder)
1638 (let* ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1639 (parent-folder (or parent-folder (wl-summary-buffer-folder-name)))
1640 (summary-buf (wl-summary-get-buffer parent-folder))
1642 (or (eq this-command 'wl-summary-reply)
1643 (eq this-command 'wl-summary-reply-with-citation)
1644 (eq this-command 'wl-summary-forward)
1645 (eq this-command 'wl-summary-target-mark-forward)
1646 (eq this-command 'wl-summary-target-mark-reply-with-citation)))
1647 buf-name file-name num change-major-mode-hook)
1648 (if (not (elmo-folder-message-file-p draft-folder))
1649 (error "%s folder cannot be used for draft folder" wl-draft-folder))
1650 (setq num (elmo-max-of-list
1651 (or (elmo-folder-list-messages draft-folder) '(0))))
1652 (setq num (+ 1 num))
1653 ;; To get unused buffer name.
1654 (while (get-buffer (concat wl-draft-folder "/" (int-to-string num)))
1655 (setq num (+ 1 num)))
1656 (setq buf-name (find-file-noselect
1658 (elmo-message-file-name
1659 (wl-folder-get-elmo-folder wl-draft-folder)
1661 ;; switch-buffer according to draft buffer style.
1662 (if wl-draft-use-frame
1663 (switch-to-buffer-other-frame buf-name)
1664 (if reply-or-forward
1665 (case wl-draft-reply-buffer-style
1667 (split-window-vertically)
1669 (switch-to-buffer buf-name))
1671 (switch-to-buffer buf-name))
1673 (delete-other-windows)
1674 (switch-to-buffer buf-name))
1676 (if (functionp wl-draft-reply-buffer-style)
1677 (funcall wl-draft-reply-buffer-style buf-name)
1678 (error "Invalid value for wl-draft-reply-buffer-style"))))
1679 (case wl-draft-buffer-style
1681 (when (eq major-mode 'wl-summary-mode)
1682 (wl-summary-toggle-disp-msg 'off))
1683 (split-window-vertically)
1685 (switch-to-buffer buf-name))
1687 (switch-to-buffer buf-name))
1689 (delete-other-windows)
1690 (switch-to-buffer buf-name))
1691 (t (if (functionp wl-draft-buffer-style)
1692 (funcall wl-draft-buffer-style buf-name)
1693 (error "Invalid value for wl-draft-buffer-style"))))))
1694 (set-buffer buf-name)
1695 (if (not (string-match (regexp-quote wl-draft-folder)
1697 (rename-buffer (concat wl-draft-folder "/" (int-to-string num))))
1700 (make-local-variable 'truncate-partial-width-windows)
1701 (setq truncate-partial-width-windows nil)
1702 (setq truncate-lines wl-draft-truncate-lines)
1703 (setq wl-sent-message-via nil)
1704 (setq wl-sent-message-queued nil)
1705 (setq wl-draft-buffer-file-name file-name)
1706 (setq wl-draft-config-exec-flag t)
1707 (setq wl-draft-parent-folder (or parent-folder ""))
1708 (or (eq this-command 'wl-folder-write-current-folder)
1709 (setq wl-draft-buffer-cur-summary-buffer summary-buf))
1712 (defun wl-draft-create-contents (header-alist)
1713 "header-alist' sample
1714 '(function ;; funcall
1715 string ;; insert string
1716 (symbol . string) ;; insert symbol-value: string
1717 (symbol . function) ;; (funcall) and if it returns string,
1718 ;; insert symbol-value: string
1719 (symbol . nil) ;; do nothing
1722 (unless (eq major-mode 'wl-draft-mode)
1723 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1724 (let ((halist header-alist)
1729 ((functionp (car halist)) (funcall (car halist)))
1731 ((stringp (car halist)) (insert (car halist) "\n"))
1733 ((consp (car halist))
1734 (setq field (car (car halist)))
1735 (setq value (cdr (car halist)))
1739 ((eq field 'Body) ; body
1741 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1743 (let ((value-return (funcall value)))
1744 (when (stringp value-return)
1745 (insert (symbol-name field) ": " value-return "\n"))))
1754 (setq halist (cdr halist)))))
1756 (defun wl-draft-prepare-edit ()
1757 (unless (eq major-mode 'wl-draft-mode)
1758 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1759 (let (change-major-mode-hook)
1760 (wl-draft-editor-mode)
1761 (when wl-draft-write-file-function
1762 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
1763 (wl-draft-overload-functions)
1764 (wl-highlight-headers 'for-draft)
1766 (clear-visited-file-modtime)))
1768 (defun wl-draft-decode-header ()
1770 (std11-narrow-to-header)
1771 (wl-draft-decode-message-in-buffer)
1774 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1777 (std11-field-body "content-type")))
1778 (content-transfer-encoding
1779 (or content-transfer-encoding
1780 (std11-field-body "content-transfer-encoding")))
1783 (std11-narrow-to-header)
1784 (wl-draft-delete-field "content-type")
1785 (wl-draft-delete-field "content-transfer-encoding")
1786 (goto-char (point-max))
1787 (setq delimline (point-marker))
1789 (narrow-to-region delimline (point-max))
1790 (goto-char (point-min))
1792 (insert "Content-type: " content-type "\n"))
1793 (when content-transfer-encoding
1794 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1795 (wl-draft-decode-message-in-buffer)
1796 (goto-char (point-min))
1797 (unless (re-search-forward "^$" (point-at-eol) t)
1802 ;;; subroutine for wl-draft-create-contents
1803 ;;; must be used in wl-draft-mode
1804 (defun wl-draft-check-new-line ()
1805 (if (not (= (preceding-char) ?\n))
1808 (defsubst wl-draft-trim-ccs (cc)
1814 (null (and wl-draft-delete-myself-from-bcc-fcc
1816 (mapcar 'wl-address-header-extract-address
1818 (wl-parse-addresses (std11-field-body "To"))
1819 (wl-parse-addresses (std11-field-body "Cc"))))
1820 (mapcar 'downcase wl-subscribed-mailing-list)))))
1824 (defsubst wl-draft-default-headers ()
1826 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1827 (wl-address-header-extract-address
1829 (cons 'User-Agent wl-generate-mailer-string-function)
1830 (cons 'Reply-To mail-default-reply-to)
1831 (cons 'Bcc (function
1834 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1835 (cons 'Fcc (function
1837 (wl-draft-trim-ccs wl-fcc))))
1838 (cons 'Organization wl-organization)
1839 (and wl-auto-insert-x-face
1840 (file-exists-p wl-x-face-file)
1841 'wl-draft-insert-x-face-field-here) ;; allow nil
1842 mail-default-headers
1843 ;; check \n at th end of line for `mail-default-headers'
1844 'wl-draft-check-new-line
1847 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1850 (goto-char delimline)
1851 (goto-char (point-min))
1852 (if (search-forward "\n\n" nil t)
1853 (delete-backward-char 1)
1854 (goto-char (point-max))))
1855 (wl-draft-check-new-line)
1856 (put-text-property (point)
1858 (insert mail-header-separator "\n")
1860 'category 'mail-header-separator)
1865 (defun wl-draft-elmo-nntp-send ()
1866 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1867 (elmo-nntp-default-user
1868 (or wl-nntp-posting-user elmo-nntp-default-user))
1869 (elmo-nntp-default-server
1870 (or wl-nntp-posting-server elmo-nntp-default-server))
1871 (elmo-nntp-default-port
1872 (or wl-nntp-posting-port elmo-nntp-default-port))
1873 (elmo-nntp-default-stream-type
1874 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1875 (elmo-nntp-default-function wl-nntp-posting-function)
1877 (if (setq condition (cdr (elmo-string-matched-assoc
1878 (std11-field-body "Newsgroups")
1879 wl-nntp-posting-config-alist)))
1880 (if (stringp condition)
1881 (setq elmo-nntp-default-server condition)
1882 (while (car condition)
1883 (set (intern (format "elmo-nntp-default-%s"
1884 (symbol-name (caar condition))))
1886 (setq condition (cdr condition)))))
1887 (unless elmo-nntp-default-function
1888 (error "wl-draft-nntp-send: posting-function is nil"))
1889 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1890 (wl-draft-set-sent-message 'news 'unplugged
1891 (cons elmo-nntp-default-server
1892 elmo-nntp-default-port))
1893 (funcall elmo-nntp-default-function
1894 elmo-nntp-default-server (current-buffer))
1895 (wl-draft-set-sent-message 'news 'sent)
1896 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1897 (std11-field-body "Newsgroups")
1898 (std11-field-body "Message-ID")))))
1900 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1901 "Generate clone of current buffer named NAME."
1902 (let ((editing-buffer (current-buffer)))
1904 (set-buffer (generate-new-buffer name))
1907 (wl-draft-editor-mode)
1908 (insert-buffer editing-buffer)
1910 (while local-variables
1911 (make-local-variable (car local-variables))
1912 (set (car local-variables)
1914 (set-buffer editing-buffer)
1915 (symbol-value (car local-variables))))
1916 (setq local-variables (cdr local-variables)))
1919 (defun wl-draft-remove-text-plain-tag ()
1920 "Remove text/plain tag of mime-edit."
1921 (when (string= (mime-make-text-tag "plain")
1922 (buffer-substring-no-properties (point-at-bol)(point-at-eol)))
1923 (delete-region (point-at-bol)(1+ (point-at-eol)))))
1925 (defun wl-draft-reedit (number)
1926 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1928 buffer file-name change-major-mode-hook body-top)
1929 (setq file-name (elmo-message-file-name draft-folder number))
1930 (unless (file-exists-p file-name)
1931 (error "File %s does not exist" file-name))
1932 (if (setq buffer (get-buffer
1933 (concat wl-draft-folder "/"
1934 (number-to-string number))))
1936 (if wl-draft-use-frame
1937 (switch-to-buffer-other-frame buffer)
1938 (switch-to-buffer buffer))
1939 (set-buffer buffer))
1940 (setq buffer (get-buffer-create (number-to-string number)))
1941 ;; switch-buffer according to draft buffer style.
1942 (if wl-draft-use-frame
1943 (switch-to-buffer-other-frame buffer)
1944 (case wl-draft-buffer-style
1946 (split-window-vertically)
1948 (switch-to-buffer buffer))
1950 (switch-to-buffer buffer))
1952 (delete-other-windows)
1953 (switch-to-buffer buffer))
1954 (t (if (functionp wl-draft-buffer-style)
1955 (funcall wl-draft-buffer-style buffer)
1956 (error "Invalid value for wl-draft-buffer-style")))))
1958 (insert-file-contents-as-binary file-name)
1959 (elmo-delete-cr-buffer)
1960 (let((mime-edit-again-ignored-field-regexp
1961 "^\\(Content-.*\\|Mime-Version\\):"))
1962 (wl-draft-decode-message-in-buffer))
1963 (setq body-top (wl-draft-insert-mail-header-separator))
1964 (if (not (string-match (regexp-quote wl-draft-folder)
1966 (rename-buffer (concat wl-draft-folder "/" (buffer-name))))
1969 (setq buffer-file-name file-name)
1970 (make-local-variable 'truncate-partial-width-windows)
1971 (setq truncate-partial-width-windows nil)
1972 (setq truncate-lines wl-draft-truncate-lines)
1973 (setq wl-sent-message-via nil)
1974 (setq wl-sent-message-queued nil)
1975 (setq wl-draft-buffer-file-name file-name)
1976 (wl-draft-config-info-operation number 'load)
1977 (goto-char (point-min))
1978 (wl-draft-overload-functions)
1979 (wl-draft-editor-mode)
1980 (when wl-draft-write-file-function
1981 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
1982 (wl-highlight-headers 'for-draft)
1983 (goto-char body-top)
1984 (run-hooks 'wl-draft-reedit-hook)
1985 (goto-char (point-max))
1988 (defmacro wl-draft-body-goto-top ()
1990 (goto-char (point-min))
1991 (if (re-search-forward mail-header-separator nil t)
1993 (goto-char (point-max))))))
1995 (defmacro wl-draft-body-goto-bottom ()
1996 (` (goto-char (point-max))))
1998 (defmacro wl-draft-config-body-goto-header ()
2000 (goto-char (point-min))
2001 (if (re-search-forward mail-header-separator nil t)
2003 (goto-char (point-max))))))
2005 (defsubst wl-draft-config-sub-eval-insert (content &optional newline)
2006 (let (content-value)
2008 (stringp (setq content-value (eval content))))
2009 (insert content-value)
2010 (if newline (insert "\n")))))
2012 (defun wl-draft-config-sub-body (content)
2013 (wl-draft-body-goto-top)
2014 (delete-region (point) (point-max))
2015 (wl-draft-config-sub-eval-insert content))
2017 (defun wl-draft-config-sub-top (content)
2018 (wl-draft-body-goto-top)
2019 (wl-draft-config-sub-eval-insert content))
2021 (defun wl-draft-config-sub-bottom (content)
2022 (wl-draft-body-goto-bottom)
2023 (wl-draft-config-sub-eval-insert content))
2025 (defun wl-draft-config-sub-header (content)
2026 (wl-draft-config-body-goto-header)
2027 (wl-draft-config-sub-eval-insert content 'newline))
2029 (defun wl-draft-config-sub-header-top (content)
2030 (goto-char (point-min))
2031 (wl-draft-config-sub-eval-insert content 'newline))
2033 (defun wl-draft-config-sub-part-top (content)
2034 (goto-char (mime-edit-content-beginning))
2035 (wl-draft-config-sub-eval-insert content 'newline))
2037 (defun wl-draft-config-sub-part-bottom (content)
2038 (goto-char (mime-edit-content-end))
2039 (wl-draft-config-sub-eval-insert content 'newline))
2041 (defsubst wl-draft-config-sub-file (content)
2042 (let ((coding-system-for-read wl-cs-autoconv)
2043 (file (expand-file-name (eval content))))
2044 (if (file-exists-p file)
2045 (insert-file-contents file)
2046 (error "%s: no exists file" file))))
2048 (defun wl-draft-config-sub-body-file (content)
2049 (wl-draft-body-goto-top)
2050 (delete-region (point) (point-max))
2051 (wl-draft-config-sub-file content))
2053 (defun wl-draft-config-sub-top-file (content)
2054 (wl-draft-body-goto-top)
2055 (wl-draft-config-sub-file content))
2057 (defun wl-draft-config-sub-bottom-file (content)
2058 (wl-draft-body-goto-bottom)
2059 (wl-draft-config-sub-file content))
2061 (defun wl-draft-config-sub-header-file (content)
2062 (wl-draft-config-body-goto-header)
2063 (wl-draft-config-sub-file content))
2065 (defun wl-draft-config-sub-template (content)
2066 (setq wl-draft-config-variables
2067 (wl-template-insert (eval content))))
2069 (defun wl-draft-config-sub-x-face (content)
2070 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
2071 (fboundp 'x-face-insert)) ; x-face.el is installed.
2072 (x-face-insert content)
2073 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
2075 (defsubst wl-draft-config-sub-func (field content)
2077 (if (setq func (assq field wl-draft-config-sub-func-alist))
2078 (let (wl-draft-config-variables)
2079 (funcall (cdr func) content)
2080 ;; for wl-draft-config-sub-template
2081 (cons t wl-draft-config-variables)))))
2083 (defsubst wl-draft-config-exec-sub (clist)
2084 (let (config local-variables)
2086 (setq config (car clist))
2091 (let ((field (car config))
2092 (content (cdr config))
2096 (wl-draft-replace-field field (eval content) t))
2097 ((setq ret-val (wl-draft-config-sub-func field content))
2098 (if (cdr ret-val) ;; for wl-draft-config-sub-template
2099 (wl-append local-variables (cdr ret-val))))
2100 ((boundp field) ;; variable
2101 (make-local-variable field)
2102 (set field (eval content))
2103 (wl-append local-variables (list field)))
2105 (error "%s: not variable" field)))))
2107 (error "%s: not supported type" config)))
2108 (setq clist (cdr clist)))
2111 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
2112 "Change headers in draft preparation time."
2114 (unless wl-draft-reedit
2117 (and (boundp 'wl-draft-prepared-config-alist)
2118 wl-draft-prepared-config-alist) ;; For compatible.
2119 wl-draft-config-alist)))
2121 (wl-draft-config-exec config-alist reply-buf)))))
2123 (defun wl-draft-config-exec (&optional config-alist reply-buf)
2124 "Change headers according to the value of `wl-draft-config-alist'.
2125 Automatically applied in draft sending time."
2127 (let ((case-fold-search t)
2128 (alist (or config-alist wl-draft-config-alist))
2129 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2130 wl-draft-reply-buffer)))
2131 (local-variables wl-draft-config-variables)
2133 (when (and (or (interactive-p)
2134 wl-draft-config-exec-flag)
2139 (setq key (caar alist)
2146 (set-buffer reply-buf)
2148 (std11-narrow-to-header)
2149 (goto-char (point-min))
2150 (re-search-forward (car clist) nil t))))
2151 (wl-draft-config-exec-sub (cdr clist))
2154 (when (save-restriction
2155 (std11-narrow-to-header mail-header-separator)
2156 (goto-char (point-min))
2157 (re-search-forward key nil t))
2158 (wl-append local-variables
2159 (wl-draft-config-exec-sub clist))
2162 (wl-append local-variables
2163 (wl-draft-config-exec-sub clist))
2165 (if (and found wl-draft-config-matchone)
2167 (setq alist (cdr alist))))
2169 (setq wl-draft-config-exec-flag nil))
2170 (run-hooks 'wl-draft-config-exec-hook)
2171 (put-text-property (point-min)(point-max) 'face nil)
2172 (wl-highlight-message (point-min)(point-max) t)
2173 (setq wl-draft-config-variables
2174 (elmo-uniq-list local-variables))))))
2176 (defun wl-draft-replace-field (field content &optional add)
2179 (let ((case-fold-search t)
2180 (inhibit-read-only t) ;; added by teranisi.
2182 (std11-narrow-to-header mail-header-separator)
2183 (goto-char (point-min))
2184 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2189 (re-search-forward "^[^ \t]" nil 'move)
2191 (skip-chars-backward "\n")
2192 (delete-region beg (point))
2193 (insert " " content))
2198 (re-search-forward "^[^ \t]" nil 'move)
2200 (delete-region beg (point)))
2201 (when (and add content)
2203 (goto-char (point-max))
2204 (insert (concat field ": " content "\n"))))))))
2206 (defun wl-draft-config-info-operation (msg operation)
2207 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-folder-get-elmo-folder
2211 (format "%s-%d" wl-draft-config-save-filename msg)
2213 element alist variable)
2215 ((eq operation 'save)
2216 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2217 (while (setq variable (pop variables))
2218 (when (boundp variable)
2220 (list (cons variable (eval variable))))))
2221 (elmo-object-save filename alist)))
2222 ((eq operation 'load)
2223 (setq alist (elmo-object-load filename))
2224 (while (setq element (pop alist))
2225 (set (make-local-variable (car element)) (cdr element))
2226 (wl-append wl-draft-config-variables (list (car element)))))
2227 ((eq operation 'delete)
2228 (if (file-exists-p filename)
2229 (delete-file filename))))))
2231 (defun wl-draft-queue-info-operation (msg operation
2232 &optional add-sent-message-via)
2233 (let* ((msgdb-dir (elmo-folder-msgdb-path
2234 (wl-folder-get-elmo-folder wl-queue-folder)))
2237 (format "%s-%d" wl-draft-queue-save-filename msg)
2239 element alist variable)
2241 ((eq operation 'save)
2242 (let ((variables (elmo-uniq-list
2243 (append wl-draft-queue-save-variables
2244 wl-draft-config-variables
2245 (list 'wl-draft-fcc-list)))))
2246 (if add-sent-message-via
2248 (push 'wl-sent-message-queued variables)
2249 (push 'wl-sent-message-via variables)))
2250 (while (setq variable (pop variables))
2251 (when (boundp variable)
2253 (list (cons variable (eval variable))))))
2254 (elmo-object-save filename alist)))
2255 ((eq operation 'load)
2256 (setq alist (elmo-object-load filename))
2257 (while (setq element (pop alist))
2258 (set (make-local-variable (car element)) (cdr element))))
2259 ((eq operation 'get-sent-via)
2260 (setq alist (elmo-object-load filename))
2261 (cdr (assq 'wl-sent-message-via alist)))
2262 ((eq operation 'delete)
2263 (if (file-exists-p filename)
2264 (delete-file filename))))))
2266 (defun wl-draft-queue-append (wl-sent-message-via)
2267 (if wl-draft-verbose-send
2268 (message "Queuing..."))
2269 (let ((send-buffer (current-buffer))
2270 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2271 (message-id (std11-field-body "Message-ID")))
2272 (if (elmo-folder-append-buffer folder t)
2274 (wl-draft-queue-info-operation
2275 (car (elmo-folder-status folder))
2276 'save wl-sent-message-via)
2277 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2278 (when wl-draft-verbose-send
2279 (setq wl-draft-verbose-msg "Queuing...")
2280 (message "Queuing...done")))
2281 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2282 (error "Queuing failed"))))
2284 (defun wl-draft-queue-flush ()
2285 "Flush draft queue."
2287 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2289 (elmo-folder-open-internal queue-folder)
2290 (elmo-folder-list-messages queue-folder)))
2293 (wl-draft-queue-flushing t)
2294 msgs failure len buffer msgid sent-via)
2295 ;; get plugged send message
2297 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2300 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2301 (or (not (nth 2 (car sent-via)))
2303 (car (nth 2 (car sent-via)))
2304 (cdr (nth 2 (car sent-via))))))
2305 (wl-append msgs (list (car msgs2)))
2307 (setq sent-via (cdr sent-via))))
2308 (setq msgs2 (cdr msgs2)))
2309 (when (> (setq len (length msgs)) 0)
2310 (if (elmo-y-or-n-p (format
2311 "%d message(s) are in the sending queue. Send now? "
2313 (not elmo-dop-flush-confirm) t)
2316 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2319 ;; reset buffer local variables
2320 (kill-all-local-variables)
2324 (setq wl-sent-message-via nil)
2325 (wl-draft-queue-info-operation (car msgs) 'load)
2326 (elmo-message-fetch queue-folder
2328 (elmo-make-fetch-strategy 'entire)
2329 nil (current-buffer))
2331 (setq failure (funcall
2332 wl-draft-queue-flush-send-function
2333 (format "Sending (%d/%d)..." i len)))
2334 ;;; (wl-draft-raw-send nil nil
2335 ;;; (format "Sending (%d/%d)..." i len))
2337 (elmo-display-error err t)
2341 (if (eq wl-sent-message-modified 'requeue)
2343 (elmo-folder-delete-messages
2344 queue-folder (cons (car msgs) nil))
2345 (wl-draft-queue-info-operation (car msgs) 'delete))
2347 (elmo-folder-delete-messages
2348 queue-folder (cons (car msgs) nil))
2349 (wl-draft-queue-info-operation (car msgs) 'delete)
2350 (setq performed (+ 1 performed))))
2351 (setq msgs (cdr msgs)))
2352 (kill-buffer buffer)
2353 (message "%d message(s) are sent." performed)))
2354 (message "%d message(s) are remained to be sent." len))
2355 (elmo-folder-close queue-folder)
2358 (defun wl-jump-to-draft-buffer (&optional arg)
2359 "Jump to the draft if exists."
2362 (wl-jump-to-draft-folder)
2363 (let ((draft-bufs (wl-collect-draft))
2367 (message "No draft buffer exist."))
2370 (sort (mapcar 'buffer-name draft-bufs)
2371 (function (lambda (a b)
2372 (not (string< a b))))))
2373 (if (setq buf (cdr (member (buffer-name)
2375 (setq buf (car buf))
2376 (setq buf (car draft-bufs)))
2377 (switch-to-buffer buf))))))
2379 (defun wl-jump-to-draft-folder ()
2380 (let ((msgs (reverse (elmo-folder-list-messages (wl-folder-get-elmo-folder
2382 (mybuf (buffer-name))
2385 (message "No draft message exist.")
2386 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2387 (setq msg (cadr (memq
2388 (string-to-int (substring mybuf (match-end 0)))
2390 (or msg (setq msg (car msgs)))
2391 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2392 (switch-to-buffer buf)
2393 (wl-draft-reedit msg)))))
2395 (defun wl-draft-highlight-and-recenter (&optional n)
2397 (when wl-highlight-body-too
2398 (let ((modified (buffer-modified-p)))
2401 (put-text-property (point-min) (point-max) 'face nil)
2402 (wl-highlight-message (point-min) (point-max) t))
2403 (set-buffer-modified-p modified))))
2406 ;;;; user-agent support by Sen Nagata
2408 ;; this appears to be necessarily global...
2409 (defvar wl-user-agent-compose-p nil)
2410 (defvar wl-user-agent-headers-and-body-alist nil)
2412 ;; this should be a generic function for mail-mode -- i wish there was
2413 ;; something like it in sendmail.el
2414 (defun wl-user-agent-insert-header (header-name header-value)
2415 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2416 ;; it seems like overriding existing headers is acceptable -- should
2417 ;; we provide an option?
2419 ;; plan was: unfold header (might be folded), remove existing value, insert
2421 ;; wl doesn't seem to fold header lines yet anyway :-)
2423 (let ((kill-whole-line t)
2425 (mail-position-on-field (capitalize header-name))
2426 (setq end-of-line (point))
2428 (re-search-forward ":" end-of-line)
2429 (insert (concat " " header-value "\n"))
2432 ;; this should be a generic function for mail-mode -- i wish there was
2433 ;; something like it in sendmail.el
2435 ;; ** haven't dealt w/ case where the body is already set **
2436 (defun wl-user-agent-insert-body (body-text)
2437 "Insert a body of text, BODY-TEXT, into a message."
2438 ;; code defensively... :-P
2439 (goto-char (point-min))
2440 (search-forward mail-header-separator)
2445 (defun wl-user-agent-compose (&optional to subject other-headers continue
2446 switch-function yank-action
2448 "Support the `compose-mail' interface for wl.
2449 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2450 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2451 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2453 (unless (featurep 'wl)
2456 (setq switch-function 'keep))
2457 ;; protect these -- to and subject get bound at some point, so it looks
2458 ;; to be necessary to protect the values used w/in
2459 (let ((wl-user-agent-headers-and-body-alist other-headers)
2460 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2461 (wl-draft-buffer-style switch-function))
2463 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2466 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2469 (setq wl-user-agent-headers-and-body-alist
2470 (cons (cons "to" to)
2471 wl-user-agent-headers-and-body-alist))))
2473 (if (wl-string-match-assoc "subject"
2474 wl-user-agent-headers-and-body-alist
2477 (wl-string-match-assoc "subject"
2478 wl-user-agent-headers-and-body-alist
2481 (setq wl-user-agent-headers-and-body-alist
2482 (cons (cons "subject" subject)
2483 wl-user-agent-headers-and-body-alist))))
2484 ;; i think this is what we want to use...
2487 ;; tell the hook-function to do its stuff
2488 (setq wl-user-agent-compose-p t)
2489 ;; because to get the hooks working, wl-draft has to think it has
2490 ;; been called interactively
2491 (call-interactively 'wl-draft))
2492 (setq wl-user-agent-compose-p nil))))
2494 (defun wl-user-agent-compose-internal ()
2495 "Manipulate headers and/or a body of a draft message."
2496 ;; being called from wl-user-agent-compose?
2497 (if wl-user-agent-compose-p
2500 (let ((headers wl-user-agent-headers-and-body-alist)
2501 (case-fold-search t))
2504 (if (not (string-match "^body$" (car (car headers))))
2505 (wl-user-agent-insert-header
2506 (car (car headers)) (cdr (car headers)))
2508 (setq headers (cdr headers))))
2509 ;; highlight headers (from wl-draft in wl-draft.el)
2510 (wl-highlight-headers 'for-draft)
2512 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2514 (wl-user-agent-insert-body
2515 (cdr (wl-string-match-assoc
2517 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2521 (product-provide (provide 'wl-draft) (require 'wl-version))
2523 ;;; wl-draft.el ends here