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-check-wl-from ()
154 (or wl-from (error "Please set `wl-from' to your mail address"))
156 (wl-draft-eword-encode-address-list wl-from)
157 (error (error "Please look at `wl-from' again"))))
159 (defun wl-draft-insert-from-field ()
161 ;; Put the "From:" field in unless for some odd reason
162 ;; they put one in themselves.
163 (wl-draft-check-wl-from)
164 (insert "From: " wl-from "\n"))
166 (defun wl-draft-insert-x-face-field ()
167 "Insert X-Face header."
169 (if (not (file-exists-p wl-x-face-file))
170 (error "File %s does not exist" wl-x-face-file)
171 (goto-char (point-min))
172 (search-forward mail-header-separator nil t)
174 (wl-draft-insert-x-face-field-here)
175 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
177 (defun wl-draft-insert-x-face-field-here ()
178 "Insert X-Face field at point."
179 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
180 (when (string-match "^\\(X-Face:\\)?[ \t\n]*" x-face-string)
181 (setq x-face-string (substring x-face-string (match-end 0))))
182 (insert "X-Face: " x-face-string))
183 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
185 ;; Insert X-Face-Version: field
186 (when (and (fboundp 'x-face-insert-version-header)
187 (boundp 'x-face-add-x-face-version-header)
188 x-face-add-x-face-version-header)
189 (x-face-insert-version-header)))
191 (defun wl-draft-setup ()
192 (let ((field wl-draft-fields)
196 (list (cons (concat (car field) " ")
197 (concat (car field) " ")))))
198 (setq field (cdr field)))
200 (cons (cons (concat wl-draft-mime-bcc-field-name ": ")
201 (concat wl-draft-mime-bcc-field-name ": "))
203 (setq wl-draft-field-completion-list cl)
204 (setq wl-address-complete-header-regexp
206 (append wl-address-complete-header-list
207 (list (concat wl-draft-mime-bcc-field-name ":")))))))
209 (defun wl-draft-make-mail-followup-to (recipients)
210 (if (elmo-list-member
211 (or wl-user-mail-address-list
212 (list (wl-address-header-extract-address wl-from)))
214 (let ((rlist (elmo-list-delete
215 (or wl-user-mail-address-list
216 (list (wl-address-header-extract-address wl-from)))
220 (lambda (item) (string= (downcase elem)
223 (if (elmo-list-member rlist (mapcar 'downcase
224 wl-subscribed-mailing-list))
226 (append rlist (list (wl-address-header-extract-address
230 (defun wl-draft-delete-myself-from-cc (to cc)
231 (let ((myself (or wl-user-mail-address-list
232 (list (wl-address-header-extract-address wl-from)))))
233 (cond (wl-draft-always-delete-myself ; always-delete option
234 (elmo-list-delete myself cc
237 (lambda (item) (string= (downcase elem)
240 ((elmo-list-member (append to cc) ; subscribed mailing-list
241 (mapcar 'downcase wl-subscribed-mailing-list))
242 (elmo-list-delete myself cc
245 (lambda (item) (string= (downcase elem)
250 (defun wl-draft-forward (original-subject summary-buf)
251 (let (references parent-folder)
252 (with-current-buffer summary-buf
253 (setq parent-folder (wl-summary-buffer-folder-name)))
254 (with-current-buffer (wl-message-get-original-buffer)
255 (setq references (nconc
256 (std11-field-bodies '("References" "In-Reply-To"))
257 (list (std11-field-body "Message-Id"))))
258 (setq references (delq nil references)
259 references (mapconcat 'identity references " ")
260 references (wl-draft-parse-msg-id-list-string references)
261 references (wl-delete-duplicates references)
262 references (when references
263 (mapconcat 'identity references "\n\t"))))
264 (and wl-draft-use-frame
265 (get-buffer-window summary-buf)
266 (select-window (get-buffer-window summary-buf)))
267 (wl-draft (list (cons 'To "")
269 (concat wl-forward-subject-prefix original-subject))
270 (cons 'References references))
271 nil nil nil nil parent-folder))
272 (goto-char (point-max))
273 (wl-draft-insert-message)
274 (mail-position-on-field "To"))
276 (defun wl-draft-strip-subject-re (subject)
277 "Remove \"Re:\" from SUBJECT string. Shamelessly copied from Gnus."
278 (if (string-match wl-subject-prefix-regexp subject)
279 (substring subject (match-end 0))
282 (defun wl-draft-self-reply-p ()
283 "Return t when From address in the current message is user's self one or not."
284 (wl-address-user-mail-address-p (or (elmo-field-body "From") "")))
286 (defun wl-draft-reply (buf with-arg summary-buf)
287 "Reply to BUF buffer message.
288 Reply to author if WITH-ARG is non-nil."
291 to mail-followup-to cc subject in-reply-to references newsgroups
292 to-alist cc-alist decoder parent-folder)
293 (when (buffer-live-p summary-buf)
294 (with-current-buffer summary-buf
295 (setq parent-folder (wl-summary-buffer-folder-name))))
296 (set-buffer (or buf mime-mother-buffer))
297 (setq r-list (if with-arg wl-draft-reply-with-argument-list
298 wl-draft-reply-without-argument-list))
301 (when (let ((condition (car (car r-list))))
302 (cond ((stringp condition)
303 (std11-field-body condition))
308 ((stringp (car condition))
309 (or (std11-field-body (car condition))
311 ((symbolp (car condition))
312 (or (funcall (car condition))
316 (setq condition (cdr condition)))
319 (funcall condition))))
320 (let ((r-to-list (nth 0 (cdr (car r-list))))
321 (r-cc-list (nth 1 (cdr (car r-list))))
322 (r-ng-list (nth 2 (cdr (car r-list)))))
323 (when (and (member "Followup-To" r-ng-list)
324 (string= (std11-field-body "Followup-To") "poster"))
325 (setq r-to-list (cons "From" r-to-list))
326 (setq r-ng-list (delete "Followup-To"
327 (copy-sequence r-ng-list))))
328 (if (and r-to-list (symbolp r-to-list))
329 (setq to (wl-concat-list (funcall r-to-list) ","))
330 (setq to (wl-concat-list (cons to
331 (elmo-multiple-fields-body-list
334 (if (and r-cc-list (symbolp r-cc-list))
335 (setq cc (wl-concat-list (funcall r-to-list) ","))
336 (setq cc (wl-concat-list (cons cc
337 (elmo-multiple-fields-body-list
340 (if (and r-ng-list (symbolp r-ng-list))
341 (setq newsgroups (wl-concat-list (funcall r-ng-list) ","))
342 (setq newsgroups (wl-concat-list (cons newsgroups
347 (setq r-list (cdr r-list)))
348 (error "No match field: check your `wl-draft-reply-%s-argument-list'"
349 (if with-arg "with" "without")))
350 (setq subject (std11-field-body "Subject"))
351 (setq to (wl-parse-addresses to)
352 cc (wl-parse-addresses cc))
353 (with-temp-buffer ; to keep raw buffer unibyte.
354 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
355 (setq decoder (mime-find-field-decoder 'Subject 'plain))
356 (setq subject (if (and subject decoder)
357 (funcall decoder subject) subject))
361 (setq decoder (mime-find-field-decoder 'To 'plain))
362 (cons (nth 1 (std11-extract-address-components addr))
363 (if decoder (funcall decoder addr) addr)))
368 (setq decoder (mime-find-field-decoder 'Cc 'plain))
369 (cons (nth 1 (std11-extract-address-components addr))
370 (if decoder (funcall decoder addr) addr)))
372 (and wl-reply-subject-prefix
373 (setq subject (concat wl-reply-subject-prefix
374 (wl-draft-strip-subject-re
376 (setq in-reply-to (std11-field-body "Message-Id"))
377 (setq references (nconc
378 (std11-field-bodies '("References" "In-Reply-To"))
380 (setq to (delq nil (mapcar 'car to-alist)))
381 (setq cc (delq nil (mapcar 'car cc-alist)))
382 ;; if subscribed mailing list is contained in cc or to
383 ;; and myself is contained in cc,
384 ;; delete myself from cc.
385 (setq cc (wl-draft-delete-myself-from-cc to cc))
386 (when wl-insert-mail-followup-to
387 (setq mail-followup-to
388 (wl-draft-make-mail-followup-to (append to cc)))
389 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
390 (with-temp-buffer ; to keep raw buffer unibyte.
391 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
392 (setq newsgroups (wl-parse newsgroups
393 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
394 newsgroups (wl-delete-duplicates newsgroups)
399 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
400 (if decoder (funcall decoder grp) grp))
402 (setq to (wl-delete-duplicates to nil t))
403 (setq cc (wl-delete-duplicates
404 (append (wl-delete-duplicates cc nil t)
405 to (copy-sequence to))
407 (and to (setq to (mapconcat
409 (if wl-draft-reply-use-address-with-full-name
410 (or (cdr (assoc addr to-alist)) addr)
413 (and cc (setq cc (mapconcat
415 (if wl-draft-reply-use-address-with-full-name
416 (or (cdr (assoc addr cc-alist)) addr)
419 (and mail-followup-to
420 (setq mail-followup-to
423 (if wl-draft-reply-use-address-with-full-name
424 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
426 mail-followup-to ",\n\t")))
427 (and (null to) (setq to cc cc nil))
428 (setq references (delq nil references)
429 references (mapconcat 'identity references " ")
430 references (wl-draft-parse-msg-id-list-string references)
431 references (wl-delete-duplicates references)
432 references (if references
433 (mapconcat 'identity references "\n\t")))
434 (and wl-draft-use-frame
435 (get-buffer-window summary-buf)
436 (select-window (get-buffer-window summary-buf)))
437 (wl-draft (list (cons 'To to)
439 (cons 'Newsgroups newsgroups)
440 (cons 'Subject subject)
441 (cons 'In-Reply-To in-reply-to)
442 (cons 'References references)
443 (cons 'Mail-Followup-To mail-followup-to))
444 nil nil nil nil parent-folder)
445 (setq wl-draft-reply-buffer buf)
446 (run-hooks 'wl-reply-hook)))
448 (defun wl-draft-reply-position (position)
449 (cond ((eq position 'body)
450 (wl-draft-body-goto-top))
451 ((eq position 'bottom)
452 (wl-draft-body-goto-bottom))
454 (goto-char (point-min)))
455 ((and (stringp position)
456 (std11-field-body position))
457 (progn (mail-position-on-field position)
458 (wl-draft-beginning-of-line)))
460 (while (car position)
461 (wl-draft-reply-position (car position))
462 (setq position (cdr position))))))
464 (defun wl-draft-add-references ()
465 (wl-draft-add-in-reply-to "References"))
467 (defun wl-draft-add-in-reply-to (&optional alt-field)
468 (let* ((mes-id (save-excursion
469 (set-buffer mail-reply-buffer)
470 (std11-field-body "message-id")))
471 (field (or alt-field "In-Reply-To"))
472 (ref (std11-field-body field))
473 (ref-list nil) (st nil))
474 (when (and mes-id ref)
475 (while (string-match "<[^>]+>" ref st)
477 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
480 (member mes-id ref-list))
484 (when (mail-position-on-field field)
486 (while (looking-at "^[ \t]")
488 (setq mes-id (concat "\t" mes-id "\n")))
492 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
493 &optional ignored-fields)
496 (narrow-to-region (point)(point))
498 (with-current-buffer mail-reply-buffer
500 (decode-mime-charset-region (point-min) (point-max)
502 (buffer-substring-no-properties
503 (point-min) (point-max))))
505 (goto-char (point-min))
506 (wl-draft-delete-fields ignored-fields))
507 (goto-char (point-max))
508 (push-mark (point) nil t)
509 (goto-char (point-min)))
511 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
512 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
513 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
514 (run-hooks 'wl-draft-cited-hook)
515 (when (if wl-draft-add-references
516 (wl-draft-add-references)
517 (if wl-draft-add-in-reply-to
518 (wl-draft-add-in-reply-to)))
519 (wl-highlight-headers 'for-draft)) ; highlight when added References:
520 (when wl-highlight-body-too
521 (wl-highlight-body-region beg (point-max)))))
523 (defun wl-message-news-p ()
524 "If exist valid Newsgroups field, return non-nil."
525 (std11-field-body "Newsgroups"))
527 (defun wl-message-field-exists-p (field)
528 "If FIELD exist and FIELD value is not empty, return non-nil."
529 (let ((value (std11-field-body field)))
531 (not (string= value "")))))
533 (defun wl-message-mail-p ()
534 "If exist To, Cc or Bcc field, return non-nil."
535 (or (wl-message-field-exists-p "To")
536 (wl-message-field-exists-p "Resent-to")
537 (wl-message-field-exists-p "Cc")
538 (wl-message-field-exists-p "Bcc")
539 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
540 ;;; This may be needed..
541 ;;; (wl-message-field-exists-p "Fcc")
544 (defun wl-draft-edit-string (string)
545 (let ((cur-buf (current-buffer))
546 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
547 to subject in-reply-to cc references newsgroups mail-followup-to
548 content-type content-transfer-encoding from
553 (setq to (std11-field-body "To"))
556 (decode-mime-charset-string
559 (setq subject (std11-field-body "Subject"))
560 (setq subject (and subject
562 (decode-mime-charset-string
565 (setq from (std11-field-body "From")
568 (decode-mime-charset-string
571 (setq in-reply-to (std11-field-body "In-Reply-To"))
572 (setq cc (std11-field-body "Cc"))
575 (decode-mime-charset-string
578 (setq references (std11-field-body "References"))
579 (setq newsgroups (std11-field-body "Newsgroups"))
580 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
581 (setq content-type (std11-field-body "Content-Type"))
582 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
583 (goto-char (point-min))
584 (or (re-search-forward "\n\n" nil t)
585 (search-forward (concat mail-header-separator "\n") nil t))
591 (nth 1 (std11-extract-address-components from))
592 wl-user-mail-address-list)
596 (cons 'Subject subject)
597 (cons 'Newsgroups newsgroups)
598 (cons 'Mail-Followup-To mail-followup-to)
599 (cons 'In-Reply-To in-reply-to)
600 (cons 'References references))
601 content-type content-transfer-encoding
602 (buffer-substring (point) (point-max))
604 (kill-buffer tmp-buf))
605 ;; Set cursor point to the top.
606 (goto-char (point-min))
607 (search-forward (concat mail-header-separator "\n") nil t)
608 (run-hooks 'wl-draft-reedit-hook)
609 (and to (mail-position-on-field "To"))))
611 (defun wl-draft-insert-current-message (dummy)
613 (let (original-buffer
615 mail-citation-hook mail-yank-hooks
616 wl-draft-add-references wl-draft-add-in-reply-to
617 wl-draft-cite-function)
618 (with-current-buffer wl-draft-buffer-cur-summary-buffer
619 (with-current-buffer wl-message-buffer
620 (setq original-buffer (wl-message-get-original-buffer))
622 (with-current-buffer original-buffer
624 (error "No current message"))))
625 (setq mail-reply-buffer original-buffer)
626 (wl-draft-yank-from-mail-reply-buffer
628 wl-ignored-forwarded-headers)))
630 (defun wl-draft-insert-get-message (dummy)
631 (let ((fld (completing-read
633 (if (memq 'read-folder wl-use-folder-petname)
634 (wl-folder-get-entity-with-petname)
635 wl-folder-entity-hashtb)
636 nil nil wl-default-spec
637 'wl-read-folder-hist))
638 (number (call-interactively
639 (function (lambda (num)
640 (interactive "nNumber: ")
642 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
643 mail-citation-hook mail-yank-hooks
644 wl-draft-cite-function)
647 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
650 (elmo-make-fetch-strategy 'entire)
651 nil mail-reply-buffer)
652 (wl-draft-yank-from-mail-reply-buffer nil))
653 (kill-buffer mail-reply-buffer))))
656 ;; default body citation func
658 (defun wl-default-draft-cite ()
659 (let ((mail-yank-ignored-headers "[^:]+:")
660 (mail-yank-prefix "> ")
661 (summary-buf wl-current-summary-buffer)
662 (message-buf (get-buffer (wl-current-message-buffer)))
663 from date cite-title num entity)
665 (buffer-live-p summary-buf)
667 (buffer-live-p message-buf))
669 (with-current-buffer summary-buf
670 (let ((elmo-mime-charset wl-summary-buffer-mime-charset))
671 (setq num (save-excursion
672 (set-buffer message-buf)
673 wl-message-buffer-cur-number))
674 (setq entity (elmo-msgdb-overview-get-entity
675 num (wl-summary-buffer-msgdb)))
676 (setq date (elmo-msgdb-overview-entity-get-date entity))
677 (setq from (elmo-msgdb-overview-entity-get-from entity))))
678 (setq cite-title (format "At %s,\n%s wrote:"
679 (or date "some time ago")
680 (if wl-default-draft-cite-decorate-author
681 (funcall wl-summary-from-function
685 (insert cite-title "\n"))
686 (mail-indent-citation)))
688 (defvar wl-draft-buffer nil "Draft buffer to yank content.")
689 (defun wl-draft-yank-to-draft-buffer (buffer)
690 "Yank BUFFER content to `wl-draft-buffer'."
691 (set-buffer wl-draft-buffer)
692 (let ((mail-reply-buffer buffer))
693 (wl-draft-yank-from-mail-reply-buffer nil)
694 (kill-buffer buffer)))
696 (defun wl-draft-yank-original (&optional arg)
697 "Yank original message."
700 (let (buf mail-reply-buffer)
703 (setq buf (current-buffer)))
704 (setq mail-reply-buffer buf)
705 (wl-draft-yank-from-mail-reply-buffer nil))
706 (wl-draft-yank-current-message-entity)))
708 (defun wl-draft-hide (editing-buffer)
709 "Hide the editing draft buffer if possible."
710 (when (and editing-buffer
711 (buffer-live-p editing-buffer))
712 (set-buffer editing-buffer)
713 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
714 fld-buf sum-win fld-win)
715 (if (and wl-draft-use-frame
716 (> (length (visible-frame-list)) 1))
722 ;; stay folder window if required
723 (when wl-stay-folder-window
724 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
725 (if (setq fld-win (get-buffer-window fld-buf))
726 (select-window fld-win)
727 (if wl-draft-resume-folder-window ;; resume folder window
728 (switch-to-buffer fld-buf)))))
729 (if (buffer-live-p sum-buf)
730 (if (setq sum-win (get-buffer-window sum-buf t))
731 ;; if Summary is on the frame, select it.
732 (select-window sum-win)
733 ;; if summary is not on the frame, switch to it.
734 (if (and wl-stay-folder-window
735 (or wl-draft-resume-folder-window fld-win))
736 (wl-folder-select-buffer sum-buf)
737 (switch-to-buffer sum-buf))))))))
739 (defun wl-draft-delete (editing-buffer)
740 "Kill the editing draft buffer and delete the file corresponds to it."
743 (set-buffer editing-buffer)
744 (if wl-draft-buffer-file-name
746 (if (file-exists-p wl-draft-buffer-file-name)
747 (delete-file wl-draft-buffer-file-name))
748 (let ((msg (and wl-draft-buffer-file-name
749 (string-match "[0-9]+$" wl-draft-buffer-file-name)
751 (match-string 0 wl-draft-buffer-file-name)))))
752 (wl-draft-config-info-operation msg 'delete))))
753 (set-buffer-modified-p nil) ; force kill
754 (kill-buffer editing-buffer))))
756 (defun wl-draft-kill (&optional force-kill)
757 "Kill current draft buffer and quit editing."
760 (when (and (or (eq major-mode 'wl-draft-mode)
761 (eq major-mode 'mail-mode))
763 (y-or-n-p "Kill Current Draft? ")))
764 (let ((cur-buf (current-buffer)))
765 (wl-draft-hide cur-buf)
766 (wl-draft-delete cur-buf)))
769 (defun wl-draft-fcc ()
770 "Add a new Fcc field, with file name completion."
772 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
773 (mail-position-on-field "to"))
776 ;; Imported from message.el.
777 (defun wl-draft-elide-region (b e)
778 "Elide the text in the region.
779 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
783 (insert wl-draft-elide-ellipsis))
785 ;; Imported from message.el.
786 (defun wl-draft-beginning-of-line (&optional n)
787 "Move point to beginning of header value or to beginning of line."
789 (let ((zrs 'zmacs-region-stays))
790 (when (and (interactive-p) (boundp zrs))
792 (if (wl-draft-point-in-header-p)
793 (let* ((here (point))
794 (bol (progn (beginning-of-line n) (point)))
795 (eol (line-end-position))
796 (eoh (and (looking-at "[^ \t]")
797 (re-search-forward ": *" eol t))))
798 (if (and eoh (or (> here eoh) (= here bol)))
801 (beginning-of-line n)))
803 (defun wl-draft-point-in-header-p ()
804 "Return t if point is in the header."
807 (goto-char (point-min))
808 (not (re-search-forward
809 (concat "^" (regexp-quote mail-header-separator) "\n")
812 ;; function for wl-sent-message-via
814 (defmacro wl-draft-sent-message-p (type)
815 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
817 (defmacro wl-draft-set-sent-message (type result &optional server-port)
818 (` (let ((element (assq (, type) wl-sent-message-via)))
820 (unless (eq (nth 1 element) (, result))
821 (setcdr element (list (, result) (, server-port)))
822 (setq wl-sent-message-modified t))
823 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
824 (setq wl-sent-message-modified t)))))
826 (defun wl-draft-sent-message-results ()
827 (let ((results wl-sent-message-via)
828 unplugged-via sent-via)
830 (cond ((eq (nth 1 (car results)) 'unplugged)
831 (push (caar results) unplugged-via))
832 ((eq (nth 1 (car results)) 'sent)
833 (push (caar results) sent-via)))
834 (setq results (cdr results)))
835 (list unplugged-via sent-via)))
837 (defun wl-draft-write-sendlog (status proto server to id)
838 "Write send log file, if `wl-draft-sendlog' is non-nil."
839 (when wl-draft-sendlog
841 (let* ((filename (expand-file-name wl-draft-sendlog-filename
842 elmo-msgdb-directory))
843 (filesize (nth 7 (file-attributes filename)))
844 (server (if server (concat " server=" server) ""))
846 ((memq proto '(fcc queue))
847 (format " folder=\"%s\"" to))
849 (format " ng=<%s>" to))
854 (mapcar '(lambda(x) (format "<%s>" x)) to)
857 (id (if id (concat " id=" id) ""))
858 (time (format-time-string "%Y/%m/%d %T")))
859 (insert (format "%s proto=%s stat=%s%s%s%s\n"
860 time proto status server to id))
861 (if (and wl-draft-sendlog-max-size filesize
862 (> filesize wl-draft-sendlog-max-size))
863 (rename-file filename (concat filename ".old") t))
864 (if (file-writable-p filename)
865 (write-region-as-binary (point-min) (point-max)
867 (message "%s is not writable." filename))))))
869 (defun wl-draft-get-header-delimiter (&optional delete)
870 ;; If DELETE is non-nil, replace the header delimiter with a blank line
872 (goto-char (point-min))
873 (when (re-search-forward
874 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
878 (setq delimline (point-marker)))
881 (defun wl-draft-send-mail-with-qmail ()
882 "Pass the prepared message buffer to qmail-inject.
883 Refer to the documentation for the variable `send-mail-function'
884 to find out how to use this."
885 (if (and wl-draft-qmail-send-plugged
886 (not (elmo-plugged-p)))
887 (wl-draft-set-sent-message 'mail 'unplugged)
889 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
890 (let ((id (std11-field-body "Message-ID"))
891 (to (std11-field-body "To")))
895 'call-process-region 1 (point-max) wl-qmail-inject-program
897 wl-qmail-inject-args))
898 ;; qmail-inject doesn't say anything on it's stdout/stderr,
899 ;; we have to look at the retval instead
901 (wl-draft-set-sent-message 'mail 'sent)
902 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
903 (1 (error "`qmail-inject' reported permanent failure"))
904 (111 (error "`qmail-inject' reported transient failure"))
905 ;; should never happen
906 (t (error "`qmail-inject' reported unknown failure"))))))
908 (defun wl-draft-parse-msg-id-list-string (string)
909 "Get msg-id list from STRING."
911 (dolist (parsed-id (std11-parse-msg-ids-string string))
912 (when (eq (car parsed-id) 'msg-id)
913 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
915 (nreverse msg-id-list)))
917 (defun wl-draft-eword-encode-address-list (string &optional column)
918 "Encode header field STRING as list of address, and return the result.
919 Cause an error when STRING contains invalid address.
920 Optional argument COLUMN is start-position of the field."
921 (car (eword-encode-rword-list
922 (or column eword-encode-default-start-column)
923 (eword-encode-addresses-to-rword-list
924 (wl-draft-std11-parse-addresses (std11-lexical-analyze string))))))
926 (defun wl-draft-std11-parse-addresses (lal)
927 (let ((ret (std11-parse-address lal)))
928 (when (and (not (and (eq (length lal) 1)
929 (eq (car (car lal)) 'spaces)))
931 (error "Error while parsing address"))
933 (let ((dest (list (car ret))))
935 (while (and (setq ret (std11-parse-ascii-token lal))
936 (string-equal (cdr (assq 'specials (car ret))) ",")
937 (setq ret (std11-parse-address (cdr ret)))
939 (setq dest (cons (car ret) dest))
940 (setq lal (cdr ret)))
941 (while (eq 'spaces (car (car lal)))
942 (setq lal (cdr lal)))
943 (if lal (error "Error while parsing address"))
946 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
947 "Get mailbox list of FIELD from current buffer.
948 The buffer is expected to be narrowed to just the headers of the message.
949 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
950 from current buffer."
952 (let ((case-fold-search t)
953 (inhibit-read-only t)
955 mailbox-list beg seq has-group-list)
956 (goto-char (point-min))
957 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
960 (re-search-forward "^[^ \t]" nil 'move)
962 (skip-chars-backward "\n")
963 (setq seq (std11-lexical-analyze
964 (buffer-substring-no-properties beg (point))))
965 (setq addresses (wl-draft-std11-parse-addresses seq))
967 (cond ((eq (car (car addresses)) 'group)
968 (setq has-group-list t)
972 'std11-address-string
973 (nth 2 (car addresses))))))
974 ((eq (car (car addresses)) 'mailbox)
975 (setq address (nth 1 (car addresses)))
979 (std11-addr-to-string
980 (if (eq (car address) 'phrase-route-addr)
983 (setq addresses (cdr addresses)))
984 (when (and remove-group-list has-group-list)
985 (delete-region beg (point))
986 (insert (wl-address-string-without-group-list-contents seq))))
989 (defun wl-draft-deduce-address-list (buffer header-start header-end)
990 "Get address list suitable for smtp RCPT TO:<address>.
991 Group list content is removed if `wl-draft-remove-group-list-contents' is
993 (let ((fields (if (and wl-draft-doing-mime-bcc
994 wl-draft-disable-bcc-for-mime-bcc)
997 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
1002 (narrow-to-region header-start header-end)
1003 (goto-char (point-min))
1005 (if (re-search-forward "^resent-to[\t ]*:" nil t)
1006 (setq fields resent-fields)))
1010 (wl-draft-parse-mailbox-list
1012 wl-draft-remove-group-list-contents)))
1013 (setq fields (cdr fields)))
1019 (defun wl-draft-send-mail-with-smtp ()
1020 "Send the prepared message buffer with SMTP."
1022 (let* ((errbuf (if mail-interactive
1023 (generate-new-buffer " smtp errors")
1025 (case-fold-search t)
1026 (default-case-fold-search t)
1027 (sender (or wl-envelope-from
1028 (wl-address-header-extract-address wl-from)))
1029 (delimline (save-excursion
1030 (goto-char (point-min))
1032 (concat "^" (regexp-quote mail-header-separator)
1036 (or wl-smtp-posting-server smtp-server "localhost"))
1037 (smtp-service (or wl-smtp-posting-port smtp-service))
1038 (smtp-local-domain (or smtp-local-domain wl-local-domain))
1039 (id (std11-field-body "message-id"))
1041 (if (not (elmo-plugged-p smtp-server smtp-service))
1042 (wl-draft-set-sent-message 'mail 'unplugged
1043 (cons smtp-server smtp-service))
1046 ;; Instead of `smtp-deduce-address-list'.
1047 (setq recipients (wl-draft-deduce-address-list
1048 (current-buffer) (point-min) delimline))
1049 (unless recipients (error "No recipients"))
1050 ;; Insert an extra newline if we need it to work around
1051 ;; Sun's bug that swallows newlines.
1052 (goto-char (1+ delimline))
1053 (if (eval mail-mailer-swallows-blank-line)
1055 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1056 (if mail-interactive
1060 (wl-draft-delete-field "bcc" delimline)
1061 (wl-draft-delete-field "resent-bcc" delimline)
1062 (let (process-connection-type)
1065 (wl-smtp-extension-bind
1067 (smtp-send-buffer sender recipients (current-buffer))
1069 (wl-draft-write-sendlog 'failed 'smtp smtp-server
1071 (if (and (eq (car err) 'smtp-response-error)
1072 (= (nth 1 err) 535))
1074 (wl-smtp-password-key
1076 (car smtp-sasl-mechanisms)
1078 (signal (car err) (cdr err)))))
1079 (wl-draft-set-sent-message 'mail 'sent)
1080 (wl-draft-write-sendlog
1081 'ok 'smtp smtp-server recipients id)))))
1082 (if (bufferp errbuf)
1083 (kill-buffer errbuf))))))
1085 (defun wl-draft-send-mail-with-pop-before-smtp ()
1086 "Send the prepared message buffer with POP-before-SMTP."
1087 (require 'elmo-pop3)
1091 :user (or wl-pop-before-smtp-user
1092 elmo-pop3-default-user)
1093 :server (or wl-pop-before-smtp-server
1094 elmo-pop3-default-server)
1095 :port (or wl-pop-before-smtp-port
1096 elmo-pop3-default-port)
1097 :auth (or wl-pop-before-smtp-authenticate-type
1098 elmo-pop3-default-authenticate-type)
1099 :stream-type (or wl-pop-before-smtp-stream-type
1100 elmo-pop3-default-stream-type))))
1101 (condition-case error
1103 (elmo-pop3-get-session session)
1104 (when session (elmo-network-close-session session)))
1106 (elmo-network-close-session session)
1107 (unless (string= (nth 1 error) "Unplugged")
1108 (signal (car error)(cdr error))))))
1109 (wl-draft-send-mail-with-smtp))
1111 (defun wl-draft-insert-required-fields (&optional force-msgid)
1112 "Insert Message-ID, Date, and From field.
1113 If FORCE-MSGID, insert message-id regardless of `wl-insert-message-id'."
1114 ;; Insert Message-Id field...
1115 (goto-char (point-min))
1116 (when (and (or force-msgid
1117 wl-insert-message-id)
1118 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1119 (insert (concat "Message-ID: "
1120 (funcall wl-message-id-function)
1122 ;; Insert date field.
1123 (goto-char (point-min))
1124 (or (re-search-forward "^Date[ \t]*:" nil t)
1125 (wl-draft-insert-date-field))
1126 ;; Insert from field.
1127 (goto-char (point-min))
1128 (or (re-search-forward "^From[ \t]*:" nil t)
1129 (wl-draft-insert-from-field)))
1131 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1132 "Send the message in the current buffer."
1134 (std11-narrow-to-header mail-header-separator)
1135 (wl-draft-insert-required-fields)
1136 ;; Delete null fields.
1137 (goto-char (point-min))
1138 (while (re-search-forward "^[^ \t\n:]+:[ \t]*\n" nil t)
1140 ;; ignore any blank lines in the header
1141 (while (re-search-forward "\n\n\n*" nil t)
1142 (replace-match "\n")))
1143 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1144 (wl-draft-dispatch-message)
1145 (when kill-when-done
1146 ;; hide editing-buffer.
1147 (wl-draft-hide editing-buffer)
1148 ;; delete editing-buffer and its file.
1149 (wl-draft-delete editing-buffer)))
1151 (defun wl-draft-dispatch-message (&optional mes-string)
1152 "Send the message in the current buffer. Not modified the header fields."
1153 (let (delimline mime-bcc)
1154 (if (and wl-draft-verbose-send mes-string)
1155 (message mes-string))
1157 (setq delimline (wl-draft-get-header-delimiter t))
1158 (unless wl-draft-fcc-list
1159 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1161 (setq wl-sent-message-modified nil)
1164 (if (and (wl-message-mail-p)
1165 (not (wl-draft-sent-message-p 'mail)))
1166 (if (or (not (or wl-draft-force-queuing
1167 wl-draft-force-queuing-mail))
1168 (memq 'mail wl-sent-message-queued))
1170 (setq mime-bcc (wl-draft-mime-bcc-field))
1171 (funcall wl-draft-send-mail-function)
1172 (when (not (zerop (length mime-bcc)))
1173 (wl-draft-do-mime-bcc mime-bcc)))
1174 (push 'mail wl-sent-message-queued)
1175 (wl-draft-set-sent-message 'mail 'unplugged)))
1176 (if (and (wl-message-news-p)
1177 (not (wl-draft-sent-message-p 'news))
1178 (not (wl-message-field-exists-p "Resent-to")))
1179 (if (or (not (or wl-draft-force-queuing
1180 wl-draft-force-queuing-news))
1181 (memq 'news wl-sent-message-queued))
1182 (funcall wl-draft-send-news-function)
1183 (push 'news wl-sent-message-queued)
1184 (wl-draft-set-sent-message 'news 'unplugged))))
1185 (let* ((status (wl-draft-sent-message-results))
1186 (unplugged-via (car status))
1187 (sent-via (nth 1 status)))
1188 ;; If one sent, process fcc folder.
1189 (if (and sent-via wl-draft-fcc-list)
1191 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1193 (setq wl-draft-fcc-list nil)))
1194 (if wl-draft-use-cache
1195 (let ((id (std11-field-body "Message-ID"))
1196 (elmo-enable-disconnected-operation t))
1197 (elmo-file-cache-save (elmo-file-cache-get-path id)
1199 ;; If one unplugged, append queue.
1200 (when (and unplugged-via
1201 wl-sent-message-modified)
1202 (if wl-draft-enable-queuing
1204 (wl-draft-queue-append wl-sent-message-via)
1205 (setq wl-sent-message-modified 'requeue))
1206 (error "Unplugged")))
1207 (when wl-draft-verbose-send
1208 (if (and unplugged-via sent-via);; combined message
1210 (setq wl-draft-verbose-msg
1211 (format "Sending%s and Queuing%s..."
1212 sent-via unplugged-via))
1213 (message (concat wl-draft-verbose-msg "done")))
1215 (message (concat mes-string
1216 (if sent-via "done" "failed")))))))))
1217 (not wl-sent-message-modified)) ;; return value
1219 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1220 "Force send current buffer as raw message."
1223 (let (wl-interactive-send
1224 ;;; wl-draft-verbose-send
1225 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1226 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1229 (wl-draft-send kill-when-done mes-string))))
1231 (defun wl-draft-clone-local-variables ()
1232 (let ((locals (buffer-local-variables))
1235 (when (and (consp (car locals))
1237 (string-match wl-draft-clone-local-variable-regexp
1238 (symbol-name (car (car locals)))))
1239 (wl-append result (list (car (car locals)))))
1240 (setq locals (cdr locals)))
1243 (defun wl-draft-send (&optional kill-when-done mes-string)
1244 "Send current draft message.
1245 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1247 ;; Don't call this explicitly.
1248 ;; Added to 'wl-draft-send-hook (by teranisi)
1249 ;; (wl-draft-config-exec)
1250 (run-hooks 'wl-draft-send-hook)
1251 (when (or (not wl-interactive-send)
1253 (wl-draft-preview-message)
1254 (goto-char (point-min))
1257 (y-or-n-p "Do you really want to send current draft? "))
1260 (signal 'quit nil)))
1263 (let ((send-mail-function 'wl-draft-raw-send)
1264 (editing-buffer (current-buffer))
1265 (sending-buffer (wl-draft-generate-clone-buffer
1266 " *wl-draft-sending-buffer*"
1267 (append wl-draft-config-variables
1268 (wl-draft-clone-local-variables))))
1269 (wl-draft-verbose-msg nil)
1272 (save-excursion (set-buffer sending-buffer)
1273 (if (and (not (wl-message-mail-p))
1274 (not (wl-message-news-p)))
1275 (error "No recipient is specified"))
1276 (expand-abbrev) ; for mail-abbrevs
1277 (let ((mime-header-encode-method-alist
1279 '((wl-draft-eword-encode-address-list
1280 . (To Cc Bcc Resent-To Resent-Cc Bcc Resent-Bcc)))
1281 (if (boundp 'mime-header-encode-method-alist)
1282 (symbol-value 'mime-header-encode-method-alist)))))
1283 (run-hooks 'mail-send-hook) ; translate buffer
1286 (if wl-draft-verbose-send
1287 (message (or mes-string "Sending...")))
1288 (funcall wl-draft-send-function editing-buffer kill-when-done)
1289 ;; Now perform actions on successful sending.
1290 (while mail-send-actions
1292 (apply (car (car mail-send-actions))
1293 (cdr (car mail-send-actions)))
1295 (setq mail-send-actions (cdr mail-send-actions)))
1296 (if wl-draft-verbose-send
1297 (message (concat (or wl-draft-verbose-msg
1298 mes-string "Sending...")
1300 ;; kill sending buffer, anyway.
1301 (and (buffer-live-p sending-buffer)
1302 (kill-buffer sending-buffer))))))
1304 (defun wl-draft-mime-bcc-field ()
1305 "Return the MIME-Bcc field body. The field is deleted."
1306 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1307 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1309 (defun wl-draft-do-mime-bcc (field-body)
1310 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1311 (let ((orig-from (mime-decode-field-body (std11-field-body "from")
1313 (orig-subj (mime-decode-field-body (or (std11-field-body "subject")
1316 (recipients (wl-parse-addresses field-body))
1317 (draft-buffer (current-buffer))
1319 (save-window-excursion
1320 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1321 (not (zerop (length field-body))))
1322 (let ((wl-draft-doing-mime-bcc t))
1323 (dolist (recipient recipients)
1324 (wl-draft-create-buffer)
1325 (wl-draft-create-contents
1326 (append `((From . ,orig-from)
1328 (Subject . ,(concat "A blind carbon copy ("
1331 (wl-draft-default-headers)))
1332 (wl-draft-insert-mail-header-separator)
1333 (wl-draft-prepare-edit)
1334 (goto-char (point-max))
1335 (insert (or wl-draft-mime-bcc-body
1336 "This is a blind carbon copy.")
1338 (mime-edit-insert-tag "message" "rfc822")
1339 (insert-buffer draft-buffer)
1340 (let (wl-interactive-send)
1341 (wl-draft-send 'kill-when-done))))))))
1343 ;; Derived from `message-save-drafts' in T-gnus.
1344 (defun wl-draft-save ()
1345 "Save current draft."
1347 (if (buffer-modified-p)
1349 (message "Saving %s..." wl-draft-buffer-file-name)
1350 (let ((msg (buffer-substring-no-properties (point-min) (point-max))))
1351 (with-temp-file wl-draft-buffer-file-name
1353 ;; If no header separator, insert it.
1355 (goto-char (point-min))
1356 (unless (re-search-forward
1357 (concat "^" (regexp-quote mail-header-separator) "$")
1359 (goto-char (point-min))
1360 (if (re-search-forward "\n\n" nil t)
1361 (replace-match (concat "\n" mail-header-separator "\n"))
1362 (goto-char (point-max))
1363 (insert (if (eq (char-before) ?\n) "" "\n")
1364 mail-header-separator "\n"))))
1365 (let ((mime-header-encode-method-alist
1366 '((eword-encode-unstructured-field-body))))
1367 (mime-edit-translate-buffer))
1368 (wl-draft-get-header-delimiter t)))
1369 (set-buffer-modified-p nil)
1370 (wl-draft-config-info-operation
1371 (and (string-match "[0-9]+$" wl-draft-buffer-file-name)
1373 (match-string 0 wl-draft-buffer-file-name)))
1375 (message "Saving %s...done" wl-draft-buffer-file-name))
1376 (message "(No changes need to be saved)")))
1378 (defun wl-draft-mimic-kill-buffer ()
1379 "Kill the current (draft) buffer with query."
1381 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1384 (if (or (not bufname)
1385 (string-equal bufname "")
1386 (string-equal bufname (buffer-name)))
1387 (let ((bufname (current-buffer)))
1388 (when (or (not (buffer-modified-p))
1390 (format "Buffer %s modified; kill anyway? " bufname)))
1391 (set-buffer-modified-p nil)
1392 (wl-draft-hide bufname)
1393 (kill-buffer bufname)))
1394 (kill-buffer bufname))))
1396 (defun wl-draft-save-and-exit ()
1397 "Save current draft and exit current draft mode."
1400 (let ((editing-buffer (current-buffer)))
1401 (wl-draft-hide editing-buffer)
1402 (kill-buffer editing-buffer)))
1404 (defun wl-draft-send-and-exit ()
1405 "Send current draft message and kill it."
1409 (defun wl-draft-send-from-toolbar ()
1411 (let ((wl-interactive-send t))
1412 (wl-draft-send-and-exit)))
1414 (defun wl-draft-delete-field (field &optional delimline replace)
1415 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1417 (defun wl-draft-delete-fields (field &optional delimline replace)
1420 (goto-char (point-min))
1421 (if (search-forward "\n\n" nil t)
1422 (setq delimline (point))
1423 (setq delimline (point-max))))
1424 (narrow-to-region (point-min) delimline)
1425 (goto-char (point-min))
1426 (let ((regexp (concat "^" field ":"))
1427 (case-fold-search t))
1429 (if (looking-at regexp)
1435 (if (re-search-forward "^[^ \t]" nil t)
1436 (goto-char (match-beginning 0))
1439 (insert (concat field ": " replace "\n"))))
1441 (if (re-search-forward "^[^ \t]" nil t)
1442 (goto-char (match-beginning 0))
1445 (defun wl-draft-get-fcc-list (header-end)
1446 (if (and wl-draft-doing-mime-bcc
1447 wl-draft-disable-fcc-for-mime-bcc)
1449 (wl-draft-delete-field "fcc")
1452 (case-fold-search t))
1453 (or (markerp header-end) (error "HEADER-END must be a marker"))
1455 (goto-char (point-min))
1456 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1461 (buffer-substring-no-properties
1465 (skip-chars-backward " \t")
1468 (dolist (folder fcc-list)
1469 (wl-folder-confirm-existence
1470 (wl-folder-get-elmo-folder (eword-decode-string folder)))))
1471 (delete-region (match-beginning 0)
1472 (progn (forward-line 1) (point)))))
1475 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1476 (let ((send-mail-buffer (current-buffer))
1477 (tembuf (generate-new-buffer " fcc output"))
1478 (case-fold-search t)
1480 (or (markerp header-end) (error "HEADER-END must be a marker"))
1483 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1486 ;; insert just the headers to avoid moving the gap more than
1487 ;; necessary (the message body could be arbitrarily huge.)
1488 (insert-buffer-substring send-mail-buffer 1 header-end)
1489 (wl-draft-insert-required-fields t)
1490 (goto-char (point-max))
1491 (insert-buffer-substring send-mail-buffer header-end)
1492 (let ((id (std11-field-body "Message-ID"))
1493 (elmo-enable-disconnected-operation t)
1496 (unless (or cache-saved
1497 (elmo-folder-plugged-p
1498 (wl-folder-get-elmo-folder (car fcc-list))))
1499 (elmo-file-cache-save id nil) ;; for disconnected operation
1500 (setq cache-saved t))
1501 (if (elmo-folder-append-buffer
1502 (wl-folder-get-elmo-folder
1503 (eword-decode-string (car fcc-list)))
1504 (not wl-fcc-force-as-read))
1505 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1506 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1507 (setq fcc-list (cdr fcc-list)))))
1508 (kill-buffer tembuf)))
1510 (defun wl-draft-on-field-p ()
1513 (goto-char (point-min))
1514 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1521 (if (or (looking-at ".*,[ \t]?$")
1522 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1524 (let ((pos (point)))
1527 (if (looking-at "^[ \t]")
1529 (if (re-search-forward ":" pos t) nil t)))))))
1533 (defun wl-draft (&optional header-alist
1534 content-type content-transfer-encoding
1537 "Write and send mail/news message with Wanderlust."
1544 (wl-plugged-init t))
1546 (wl-init)) ; returns immediately if already initialized.
1549 (let (buf-name header-alist-internal)
1551 (wl-draft-create-buffer parent-folder))
1553 (unless (cdr (assq 'From header-alist))
1555 (append (list (cons 'From wl-from)) header-alist)))
1556 (unless (cdr (assq 'To header-alist))
1561 (if (assq 'To header-alist)
1562 (setcdr (assq 'To header-alist) to)
1564 (append header-alist
1565 (list (cons 'To to))))))))
1566 (unless (cdr (assq 'Subject header-alist))
1567 (if (assq 'Subject header-alist)
1568 (setcdr (assq 'Subject header-alist) "")
1570 (append header-alist (list (cons 'Subject ""))))))
1571 (setq header-alist (append header-alist
1572 (wl-draft-default-headers)
1573 wl-draft-additional-header-alist
1574 (if body (list "" (cons 'Body body)))))
1575 (wl-draft-create-contents header-alist)
1577 (wl-draft-decode-body
1578 content-type content-transfer-encoding))
1579 (wl-draft-insert-mail-header-separator)
1580 (wl-draft-prepare-edit)
1582 (run-hooks 'wl-mail-setup-hook))
1583 (goto-char (point-min))
1584 (wl-user-agent-compose-internal) ;; user-agent
1587 (string= (cdr (assq 'To header-alist)) ""))
1588 (mail-position-on-field "To"))
1590 (goto-char (point-max))))
1593 (defun wl-draft-create-buffer (&optional parent-folder)
1594 (let* ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1595 (parent-folder (or parent-folder (wl-summary-buffer-folder-name)))
1596 (summary-buf (wl-summary-get-buffer parent-folder))
1598 (or (eq this-command 'wl-summary-reply)
1599 (eq this-command 'wl-summary-forward)
1600 (eq this-command 'wl-summary-target-mark-forward)
1601 (eq this-command 'wl-summary-target-mark-reply-with-citation)))
1602 buf-name file-name num change-major-mode-hook)
1603 (if (not (elmo-folder-message-file-p draft-folder))
1604 (error "%s folder cannot be used for draft folder" wl-draft-folder))
1605 (setq num (elmo-max-of-list
1606 (or (elmo-folder-list-messages draft-folder) '(0))))
1607 (setq num (+ 1 num))
1608 ;; To get unused buffer name.
1609 (while (get-buffer (concat wl-draft-folder "/" (int-to-string num)))
1610 (setq num (+ 1 num)))
1611 (setq buf-name (find-file-noselect
1613 (elmo-message-file-name
1614 (wl-folder-get-elmo-folder wl-draft-folder)
1616 ;; switch-buffer according to draft buffer style.
1617 (if wl-draft-use-frame
1618 (switch-to-buffer-other-frame buf-name)
1619 (if reply-or-forward
1620 (case wl-draft-reply-buffer-style
1622 (split-window-vertically)
1624 (switch-to-buffer buf-name))
1626 (switch-to-buffer buf-name))
1628 (delete-other-windows)
1629 (switch-to-buffer buf-name))
1631 (if (functionp wl-draft-reply-buffer-style)
1632 (funcall wl-draft-reply-buffer-style buf-name)
1633 (error "Invalid value for wl-draft-reply-buffer-style"))))
1634 (case wl-draft-buffer-style
1636 (when (eq major-mode 'wl-summary-mode)
1637 (wl-summary-toggle-disp-msg 'off))
1638 (split-window-vertically)
1640 (switch-to-buffer buf-name))
1642 (switch-to-buffer buf-name))
1644 (delete-other-windows)
1645 (switch-to-buffer buf-name))
1646 (t (if (functionp wl-draft-buffer-style)
1647 (funcall wl-draft-buffer-style buf-name)
1648 (error "Invalid value for wl-draft-buffer-style"))))))
1649 (set-buffer buf-name)
1650 (if (not (string-match (regexp-quote wl-draft-folder)
1652 (rename-buffer (concat wl-draft-folder "/" (int-to-string num))))
1655 (make-local-variable 'truncate-partial-width-windows)
1656 (setq truncate-partial-width-windows nil)
1657 (setq truncate-lines wl-draft-truncate-lines)
1658 (setq wl-sent-message-via nil)
1659 (setq wl-sent-message-queued nil)
1660 (setq wl-draft-buffer-file-name file-name)
1661 (setq wl-draft-config-exec-flag t)
1662 (setq wl-draft-parent-folder (or parent-folder ""))
1663 (or (eq this-command 'wl-folder-write-current-folder)
1664 (setq wl-draft-buffer-cur-summary-buffer summary-buf))
1667 (defun wl-draft-create-contents (header-alist)
1668 "header-alist' sample
1669 '(function ;; funcall
1670 string ;; insert string
1671 (symbol . string) ;; insert symbol-value: string
1672 (symbol . function) ;; (funcall) and if it returns string,
1673 ;; insert symbol-value: string
1674 (symbol . nil) ;; do nothing
1677 (unless (eq major-mode 'wl-draft-mode)
1678 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1679 (let ((halist header-alist)
1684 ((functionp (car halist)) (funcall (car halist)))
1686 ((stringp (car halist)) (insert (car halist) "\n"))
1688 ((consp (car halist))
1689 (setq field (car (car halist)))
1690 (setq value (cdr (car halist)))
1694 ((eq field 'Body) ; body
1696 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1698 (let ((value-return (funcall value)))
1699 (when (stringp value-return)
1700 (insert (symbol-name field) ": " value-return "\n"))))
1709 (setq halist (cdr halist)))))
1711 (defun wl-draft-prepare-edit ()
1712 (unless (eq major-mode 'wl-draft-mode)
1713 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1714 (let (change-major-mode-hook)
1715 (wl-draft-editor-mode)
1716 (when wl-draft-write-file-function
1717 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
1718 (wl-draft-overload-functions)
1719 (wl-highlight-headers 'for-draft)
1721 (clear-visited-file-modtime)))
1723 (defun wl-draft-decode-header ()
1725 (std11-narrow-to-header)
1726 (wl-draft-decode-message-in-buffer)
1729 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1732 (std11-field-body "content-type")))
1733 (content-transfer-encoding
1734 (or content-transfer-encoding
1735 (std11-field-body "content-transfer-encoding")))
1738 (std11-narrow-to-header)
1739 (wl-draft-delete-field "content-type")
1740 (wl-draft-delete-field "content-transfer-encoding")
1741 (goto-char (point-max))
1742 (setq delimline (point-marker))
1744 (narrow-to-region delimline (point-max))
1745 (goto-char (point-min))
1747 (insert "Content-type: " content-type "\n"))
1748 (when content-transfer-encoding
1749 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1750 (wl-draft-decode-message-in-buffer)
1751 (goto-char (point-min))
1752 (unless (re-search-forward "^$" (point-at-eol) t)
1757 ;;; subroutine for wl-draft-create-contents
1758 ;;; must be used in wl-draft-mode
1759 (defun wl-draft-check-new-line ()
1760 (if (not (= (preceding-char) ?\n))
1763 (defsubst wl-draft-trim-ccs (cc)
1769 (null (and wl-draft-delete-myself-from-bcc-fcc
1771 (mapcar 'wl-address-header-extract-address
1773 (wl-parse-addresses (std11-field-body "To"))
1774 (wl-parse-addresses (std11-field-body "Cc"))))
1775 (mapcar 'downcase wl-subscribed-mailing-list)))))
1779 (defsubst wl-draft-default-headers ()
1781 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1782 (wl-address-header-extract-address
1784 (cons 'User-Agent wl-generate-mailer-string-function)
1785 (cons 'Reply-To mail-default-reply-to)
1786 (cons 'Bcc (function
1789 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1790 (cons 'Fcc (function
1792 (wl-draft-trim-ccs wl-fcc))))
1793 (cons 'Organization wl-organization)
1794 (and wl-auto-insert-x-face
1795 (file-exists-p wl-x-face-file)
1796 'wl-draft-insert-x-face-field-here) ;; allow nil
1797 mail-default-headers
1798 ;; check \n at th end of line for `mail-default-headers'
1799 'wl-draft-check-new-line
1802 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1805 (goto-char delimline)
1806 (goto-char (point-min))
1807 (if (search-forward "\n\n" nil t)
1808 (delete-backward-char 1)
1809 (goto-char (point-max))))
1810 (wl-draft-check-new-line)
1811 (put-text-property (point)
1813 (insert mail-header-separator "\n")
1815 'category 'mail-header-separator)
1820 (defun wl-draft-elmo-nntp-send ()
1821 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1822 (elmo-nntp-default-user
1823 (or wl-nntp-posting-user elmo-nntp-default-user))
1824 (elmo-nntp-default-server
1825 (or wl-nntp-posting-server elmo-nntp-default-server))
1826 (elmo-nntp-default-port
1827 (or wl-nntp-posting-port elmo-nntp-default-port))
1828 (elmo-nntp-default-stream-type
1829 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1830 (elmo-nntp-default-function wl-nntp-posting-function)
1832 (if (setq condition (cdr (elmo-string-matched-assoc
1833 (std11-field-body "Newsgroups")
1834 wl-nntp-posting-config-alist)))
1835 (if (stringp condition)
1836 (setq elmo-nntp-default-server condition)
1837 (while (car condition)
1838 (set (intern (format "elmo-nntp-default-%s"
1839 (symbol-name (caar condition))))
1841 (setq condition (cdr condition)))))
1842 (unless elmo-nntp-default-function
1843 (error "wl-draft-nntp-send: posting-function is nil"))
1844 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1845 (wl-draft-set-sent-message 'news 'unplugged
1846 (cons elmo-nntp-default-server
1847 elmo-nntp-default-port))
1848 (funcall elmo-nntp-default-function
1849 elmo-nntp-default-server (current-buffer))
1850 (wl-draft-set-sent-message 'news 'sent)
1851 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1852 (std11-field-body "Newsgroups")
1853 (std11-field-body "Message-ID")))))
1855 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1856 "Generate clone of current buffer named NAME."
1857 (let ((editing-buffer (current-buffer)))
1859 (set-buffer (generate-new-buffer name))
1862 (wl-draft-editor-mode)
1863 (insert-buffer editing-buffer)
1865 (while local-variables
1866 (make-local-variable (car local-variables))
1867 (set (car local-variables)
1869 (set-buffer editing-buffer)
1870 (symbol-value (car local-variables))))
1871 (setq local-variables (cdr local-variables)))
1874 (defun wl-draft-remove-text-plain-tag ()
1875 "Remove text/plain tag of mime-edit."
1876 (when (string= (mime-create-tag "text" "plain")
1877 (buffer-substring-no-properties (point-at-bol)(point-at-eol)))
1878 (delete-region (point-at-bol)(1+ (point-at-eol)))))
1880 (defun wl-draft-reedit (number)
1881 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1883 buffer file-name change-major-mode-hook body-top)
1884 (setq file-name (elmo-message-file-name draft-folder number))
1885 (unless (file-exists-p file-name)
1886 (error "File %s does not exist" file-name))
1887 (if (setq buffer (get-buffer
1888 (concat wl-draft-folder "/"
1889 (number-to-string number))))
1891 (if wl-draft-use-frame
1892 (switch-to-buffer-other-frame buffer)
1893 (switch-to-buffer buffer))
1894 (set-buffer buffer))
1895 (setq buffer (get-buffer-create (number-to-string number)))
1896 ;; switch-buffer according to draft buffer style.
1897 (if wl-draft-use-frame
1898 (switch-to-buffer-other-frame buffer)
1899 (case wl-draft-buffer-style
1901 (split-window-vertically)
1903 (switch-to-buffer buffer))
1905 (switch-to-buffer buffer))
1907 (delete-other-windows)
1908 (switch-to-buffer buffer))
1909 (t (if (functionp wl-draft-buffer-style)
1910 (funcall wl-draft-buffer-style buffer)
1911 (error "Invalid value for wl-draft-buffer-style")))))
1913 (insert-file-contents-as-binary file-name)
1914 (elmo-delete-cr-buffer)
1915 (let((mime-edit-again-ignored-field-regexp
1916 "^\\(Content-.*\\|Mime-Version\\):"))
1917 (wl-draft-decode-message-in-buffer))
1918 (setq body-top (wl-draft-insert-mail-header-separator))
1919 (if (not (string-match (regexp-quote wl-draft-folder)
1921 (rename-buffer (concat wl-draft-folder "/" (buffer-name))))
1924 (setq buffer-file-name file-name)
1925 (make-local-variable 'truncate-partial-width-windows)
1926 (setq truncate-partial-width-windows nil)
1927 (setq truncate-lines wl-draft-truncate-lines)
1928 (setq wl-sent-message-via nil)
1929 (setq wl-sent-message-queued nil)
1930 (setq wl-draft-buffer-file-name file-name)
1931 (wl-draft-config-info-operation number 'load)
1932 (goto-char (point-min))
1933 (wl-draft-overload-functions)
1934 (wl-draft-editor-mode)
1935 (when wl-draft-write-file-function
1936 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
1937 (wl-highlight-headers 'for-draft)
1938 (goto-char body-top)
1939 (run-hooks 'wl-draft-reedit-hook)
1940 (goto-char (point-max))
1943 (defmacro wl-draft-body-goto-top ()
1945 (goto-char (point-min))
1946 (if (re-search-forward mail-header-separator nil t)
1948 (goto-char (point-max))))))
1950 (defmacro wl-draft-body-goto-bottom ()
1951 (` (goto-char (point-max))))
1953 (defmacro wl-draft-config-body-goto-header ()
1955 (goto-char (point-min))
1956 (if (re-search-forward mail-header-separator nil t)
1958 (goto-char (point-max))))))
1960 (defsubst wl-draft-config-sub-eval-insert (content &optional newline)
1961 (let (content-value)
1963 (stringp (setq content-value (eval content))))
1964 (insert content-value)
1965 (if newline (insert "\n")))))
1967 (defun wl-draft-config-sub-body (content)
1968 (wl-draft-body-goto-top)
1969 (delete-region (point) (point-max))
1970 (wl-draft-config-sub-eval-insert content))
1972 (defun wl-draft-config-sub-top (content)
1973 (wl-draft-body-goto-top)
1974 (wl-draft-config-sub-eval-insert content))
1976 (defun wl-draft-config-sub-bottom (content)
1977 (wl-draft-body-goto-bottom)
1978 (wl-draft-config-sub-eval-insert content))
1980 (defun wl-draft-config-sub-header (content)
1981 (wl-draft-config-body-goto-header)
1982 (wl-draft-config-sub-eval-insert content 'newline))
1984 (defun wl-draft-config-sub-header-top (content)
1985 (goto-char (point-min))
1986 (wl-draft-config-sub-eval-insert content 'newline))
1988 (defun wl-draft-config-sub-part-top (content)
1989 (goto-char (mime-edit-content-beginning))
1990 (wl-draft-config-sub-eval-insert content 'newline))
1992 (defun wl-draft-config-sub-part-bottom (content)
1993 (goto-char (mime-edit-content-end))
1994 (wl-draft-config-sub-eval-insert content 'newline))
1996 (defsubst wl-draft-config-sub-file (content)
1997 (let ((coding-system-for-read wl-cs-autoconv)
1998 (file (expand-file-name (eval content))))
1999 (if (file-exists-p file)
2000 (insert-file-contents file)
2001 (error "%s: no exists file" file))))
2003 (defun wl-draft-config-sub-body-file (content)
2004 (wl-draft-body-goto-top)
2005 (delete-region (point) (point-max))
2006 (wl-draft-config-sub-file content))
2008 (defun wl-draft-config-sub-top-file (content)
2009 (wl-draft-body-goto-top)
2010 (wl-draft-config-sub-file content))
2012 (defun wl-draft-config-sub-bottom-file (content)
2013 (wl-draft-body-goto-bottom)
2014 (wl-draft-config-sub-file content))
2016 (defun wl-draft-config-sub-header-file (content)
2017 (wl-draft-config-body-goto-header)
2018 (wl-draft-config-sub-file content))
2020 (defun wl-draft-config-sub-template (content)
2021 (setq wl-draft-config-variables
2022 (wl-template-insert (eval content))))
2024 (defun wl-draft-config-sub-x-face (content)
2025 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
2026 (fboundp 'x-face-insert)) ; x-face.el is installed.
2027 (x-face-insert content)
2028 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
2030 (defsubst wl-draft-config-sub-func (field content)
2032 (if (setq func (assq field wl-draft-config-sub-func-alist))
2033 (let (wl-draft-config-variables)
2034 (funcall (cdr func) content)
2035 ;; for wl-draft-config-sub-template
2036 (cons t wl-draft-config-variables)))))
2038 (defsubst wl-draft-config-exec-sub (clist)
2039 (let (config local-variables)
2041 (setq config (car clist))
2046 (let ((field (car config))
2047 (content (cdr config))
2051 (wl-draft-replace-field field (eval content) t))
2052 ((setq ret-val (wl-draft-config-sub-func field content))
2053 (if (cdr ret-val) ;; for wl-draft-config-sub-template
2054 (wl-append local-variables (cdr ret-val))))
2055 ((boundp field) ;; variable
2056 (make-local-variable field)
2057 (set field (eval content))
2058 (wl-append local-variables (list field)))
2060 (error "%s: not variable" field)))))
2062 (error "%s: not supported type" config)))
2063 (setq clist (cdr clist)))
2066 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
2067 "Change headers in draft preparation time."
2069 (unless wl-draft-reedit
2072 (and (boundp 'wl-draft-prepared-config-alist)
2073 wl-draft-prepared-config-alist) ;; For compatible.
2074 wl-draft-config-alist)))
2076 (wl-draft-config-exec config-alist reply-buf)))))
2078 (defun wl-draft-config-exec (&optional config-alist reply-buf)
2079 "Change headers according to the value of `wl-draft-config-alist'.
2080 Automatically applied in draft sending time."
2082 (let ((case-fold-search t)
2083 (alist (or config-alist wl-draft-config-alist))
2084 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2085 wl-draft-reply-buffer)))
2086 (local-variables wl-draft-config-variables)
2088 (when (and (or (interactive-p)
2089 wl-draft-config-exec-flag)
2094 (setq key (caar alist)
2101 (set-buffer reply-buf)
2103 (std11-narrow-to-header)
2104 (goto-char (point-min))
2105 (re-search-forward (car clist) nil t))))
2106 (wl-draft-config-exec-sub (cdr clist))
2109 (when (save-restriction
2110 (std11-narrow-to-header mail-header-separator)
2111 (goto-char (point-min))
2112 (re-search-forward key nil t))
2113 (wl-append local-variables
2114 (wl-draft-config-exec-sub clist))
2117 (wl-append local-variables
2118 (wl-draft-config-exec-sub clist))
2120 (if (and found wl-draft-config-matchone)
2122 (setq alist (cdr alist))))
2124 (setq wl-draft-config-exec-flag nil))
2125 (run-hooks 'wl-draft-config-exec-hook)
2126 (put-text-property (point-min)(point-max) 'face nil)
2127 (wl-highlight-message (point-min)(point-max) t)
2128 (setq wl-draft-config-variables
2129 (elmo-uniq-list local-variables))))))
2131 (defun wl-draft-replace-field (field content &optional add)
2134 (let ((case-fold-search t)
2135 (inhibit-read-only t) ;; added by teranisi.
2137 (std11-narrow-to-header mail-header-separator)
2138 (goto-char (point-min))
2139 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2144 (re-search-forward "^[^ \t]" nil 'move)
2146 (skip-chars-backward "\n")
2147 (delete-region beg (point))
2148 (insert " " content))
2153 (re-search-forward "^[^ \t]" nil 'move)
2155 (delete-region beg (point)))
2156 (when (and add content)
2158 (goto-char (point-max))
2159 (insert (concat field ": " content "\n"))))))))
2161 (defun wl-draft-config-info-operation (msg operation)
2162 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-folder-get-elmo-folder
2166 (format "%s-%d" wl-draft-config-save-filename msg)
2168 element alist variable)
2170 ((eq operation 'save)
2171 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2172 (while (setq variable (pop variables))
2173 (when (boundp variable)
2175 (list (cons variable (eval variable))))))
2176 (elmo-object-save filename alist)))
2177 ((eq operation 'load)
2178 (setq alist (elmo-object-load filename))
2179 (while (setq element (pop alist))
2180 (set (make-local-variable (car element)) (cdr element))
2181 (wl-append wl-draft-config-variables (list (car element)))))
2182 ((eq operation 'delete)
2183 (if (file-exists-p filename)
2184 (delete-file filename))))))
2186 (defun wl-draft-queue-info-operation (msg operation
2187 &optional add-sent-message-via)
2188 (let* ((msgdb-dir (elmo-folder-msgdb-path
2189 (wl-folder-get-elmo-folder wl-queue-folder)))
2192 (format "%s-%d" wl-draft-queue-save-filename msg)
2194 element alist variable)
2196 ((eq operation 'save)
2197 (let ((variables (elmo-uniq-list
2198 (append wl-draft-queue-save-variables
2199 wl-draft-config-variables
2200 (list 'wl-draft-fcc-list)))))
2201 (if add-sent-message-via
2203 (push 'wl-sent-message-queued variables)
2204 (push 'wl-sent-message-via variables)))
2205 (while (setq variable (pop variables))
2206 (when (boundp variable)
2208 (list (cons variable (eval variable))))))
2209 (elmo-object-save filename alist)))
2210 ((eq operation 'load)
2211 (setq alist (elmo-object-load filename))
2212 (while (setq element (pop alist))
2213 (set (make-local-variable (car element)) (cdr element))))
2214 ((eq operation 'get-sent-via)
2215 (setq alist (elmo-object-load filename))
2216 (cdr (assq 'wl-sent-message-via alist)))
2217 ((eq operation 'delete)
2218 (if (file-exists-p filename)
2219 (delete-file filename))))))
2221 (defun wl-draft-queue-append (wl-sent-message-via)
2222 (if wl-draft-verbose-send
2223 (message "Queuing..."))
2224 (let ((send-buffer (current-buffer))
2225 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2226 (message-id (std11-field-body "Message-ID")))
2227 (if (elmo-folder-append-buffer folder t)
2229 (wl-draft-queue-info-operation
2230 (car (elmo-folder-status folder))
2231 'save wl-sent-message-via)
2232 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2233 (when wl-draft-verbose-send
2234 (setq wl-draft-verbose-msg "Queuing...")
2235 (message "Queuing...done")))
2236 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2237 (error "Queuing failed"))))
2239 (defun wl-draft-queue-flush ()
2240 "Flush draft queue."
2242 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2244 (elmo-folder-open-internal queue-folder)
2245 (elmo-folder-list-messages queue-folder)))
2248 (wl-draft-queue-flushing t)
2249 msgs failure len buffer msgid sent-via)
2250 ;; get plugged send message
2252 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2255 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2256 (or (not (nth 2 (car sent-via)))
2258 (car (nth 2 (car sent-via)))
2259 (cdr (nth 2 (car sent-via))))))
2260 (wl-append msgs (list (car msgs2)))
2262 (setq sent-via (cdr sent-via))))
2263 (setq msgs2 (cdr msgs2)))
2264 (when (> (setq len (length msgs)) 0)
2265 (if (elmo-y-or-n-p (format
2266 "%d message(s) are in the sending queue. Send now? "
2268 (not elmo-dop-flush-confirm) t)
2271 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2274 ;; reset buffer local variables
2275 (kill-all-local-variables)
2279 (setq wl-sent-message-via nil)
2280 (wl-draft-queue-info-operation (car msgs) 'load)
2281 (elmo-message-fetch queue-folder
2283 (elmo-make-fetch-strategy 'entire)
2284 nil (current-buffer))
2286 (setq failure (funcall
2287 wl-draft-queue-flush-send-function
2288 (format "Sending (%d/%d)..." i len)))
2289 ;;; (wl-draft-raw-send nil nil
2290 ;;; (format "Sending (%d/%d)..." i len))
2292 (elmo-display-error err t)
2296 (if (eq wl-sent-message-modified 'requeue)
2298 (elmo-folder-delete-messages
2299 queue-folder (cons (car msgs) nil))
2300 (wl-draft-queue-info-operation (car msgs) 'delete))
2302 (elmo-folder-delete-messages
2303 queue-folder (cons (car msgs) nil))
2304 (wl-draft-queue-info-operation (car msgs) 'delete)
2305 (setq performed (+ 1 performed))))
2306 (setq msgs (cdr msgs)))
2307 (kill-buffer buffer)
2308 (message "%d message(s) are sent." performed)))
2309 (message "%d message(s) are remained to be sent." len))
2310 (elmo-folder-close queue-folder)
2313 (defun wl-jump-to-draft-buffer (&optional arg)
2314 "Jump to the draft if exists."
2317 (wl-jump-to-draft-folder)
2318 (let ((draft-bufs (wl-collect-draft))
2322 (message "No draft buffer exist."))
2325 (sort (mapcar 'buffer-name draft-bufs)
2326 (function (lambda (a b)
2327 (not (string< a b))))))
2328 (if (setq buf (cdr (member (buffer-name)
2330 (setq buf (car buf))
2331 (setq buf (car draft-bufs)))
2332 (switch-to-buffer buf))))))
2334 (defun wl-jump-to-draft-folder ()
2335 (let ((msgs (reverse (elmo-folder-list-messages (wl-folder-get-elmo-folder
2337 (mybuf (buffer-name))
2340 (message "No draft message exist.")
2341 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2342 (setq msg (cadr (memq
2343 (string-to-int (substring mybuf (match-end 0)))
2345 (or msg (setq msg (car msgs)))
2346 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2347 (switch-to-buffer buf)
2348 (wl-draft-reedit msg)))))
2350 (defun wl-draft-highlight-and-recenter (&optional n)
2352 (when wl-highlight-body-too
2353 (let ((modified (buffer-modified-p)))
2356 (put-text-property (point-min) (point-max) 'face nil)
2357 (wl-highlight-message (point-min) (point-max) t))
2358 (set-buffer-modified-p modified))))
2361 ;;;; user-agent support by Sen Nagata
2363 ;; this appears to be necessarily global...
2364 (defvar wl-user-agent-compose-p nil)
2365 (defvar wl-user-agent-headers-and-body-alist nil)
2367 ;; this should be a generic function for mail-mode -- i wish there was
2368 ;; something like it in sendmail.el
2369 (defun wl-user-agent-insert-header (header-name header-value)
2370 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2371 ;; it seems like overriding existing headers is acceptable -- should
2372 ;; we provide an option?
2374 ;; plan was: unfold header (might be folded), remove existing value, insert
2376 ;; wl doesn't seem to fold header lines yet anyway :-)
2378 (let ((kill-whole-line t)
2380 (mail-position-on-field (capitalize header-name))
2381 (setq end-of-line (point))
2383 (re-search-forward ":" end-of-line)
2384 (insert (concat " " header-value "\n"))
2387 ;; this should be a generic function for mail-mode -- i wish there was
2388 ;; something like it in sendmail.el
2390 ;; ** haven't dealt w/ case where the body is already set **
2391 (defun wl-user-agent-insert-body (body-text)
2392 "Insert a body of text, BODY-TEXT, into a message."
2393 ;; code defensively... :-P
2394 (goto-char (point-min))
2395 (search-forward mail-header-separator)
2400 (defun wl-user-agent-compose (&optional to subject other-headers continue
2401 switch-function yank-action
2403 "Support the `compose-mail' interface for wl.
2404 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2405 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2406 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2408 (unless (featurep 'wl)
2411 (setq switch-function 'keep))
2412 ;; protect these -- to and subject get bound at some point, so it looks
2413 ;; to be necessary to protect the values used w/in
2414 (let ((wl-user-agent-headers-and-body-alist other-headers)
2415 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2416 (wl-draft-buffer-style switch-function))
2418 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2421 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2424 (setq wl-user-agent-headers-and-body-alist
2425 (cons (cons "to" to)
2426 wl-user-agent-headers-and-body-alist))))
2428 (if (wl-string-match-assoc "subject"
2429 wl-user-agent-headers-and-body-alist
2432 (wl-string-match-assoc "subject"
2433 wl-user-agent-headers-and-body-alist
2436 (setq wl-user-agent-headers-and-body-alist
2437 (cons (cons "subject" subject)
2438 wl-user-agent-headers-and-body-alist))))
2439 ;; i think this is what we want to use...
2442 ;; tell the hook-function to do its stuff
2443 (setq wl-user-agent-compose-p t)
2444 ;; because to get the hooks working, wl-draft has to think it has
2445 ;; been called interactively
2446 (call-interactively 'wl-draft))
2447 (setq wl-user-agent-compose-p nil))))
2449 (defun wl-user-agent-compose-internal ()
2450 "Manipulate headers and/or a body of a draft message."
2451 ;; being called from wl-user-agent-compose?
2452 (if wl-user-agent-compose-p
2455 (let ((headers wl-user-agent-headers-and-body-alist)
2456 (case-fold-search t))
2459 (if (not (string-match "^body$" (car (car headers))))
2460 (wl-user-agent-insert-header
2461 (car (car headers)) (cdr (car headers)))
2463 (setq headers (cdr headers))))
2464 ;; highlight headers (from wl-draft in wl-draft.el)
2465 (wl-highlight-headers 'for-draft)
2467 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2469 (wl-user-agent-insert-body
2470 (cdr (wl-string-match-assoc
2472 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2476 (product-provide (provide 'wl-draft) (require 'wl-version))
2478 ;;; wl-draft.el ends here