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-parent-folder nil)
77 (defvar wl-draft-doing-mime-bcc nil)
79 (defvar wl-draft-config-sub-func-alist
80 '((body . wl-draft-config-sub-body)
81 (top . wl-draft-config-sub-top)
82 (bottom . wl-draft-config-sub-bottom)
83 (header . wl-draft-config-sub-header)
84 (header-top . wl-draft-config-sub-header-top)
85 (header-bottom . wl-draft-config-sub-header)
86 (part-top . wl-draft-config-sub-part-top)
87 (part-bottom . wl-draft-config-sub-part-bottom)
88 (body-file . wl-draft-config-sub-body-file)
89 (top-file . wl-draft-config-sub-top-file)
90 (bottom-file . wl-draft-config-sub-bottom-file)
91 (header-file . wl-draft-config-sub-header-file)
92 (template . wl-draft-config-sub-template)
93 (x-face . wl-draft-config-sub-x-face)))
95 (make-variable-buffer-local 'wl-draft-buffer-file-name)
96 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
97 (make-variable-buffer-local 'wl-draft-config-variables)
98 (make-variable-buffer-local 'wl-draft-config-exec-flag)
99 (make-variable-buffer-local 'wl-sent-message-via)
100 (make-variable-buffer-local 'wl-sent-message-queued)
101 (make-variable-buffer-local 'wl-draft-fcc-list)
102 (make-variable-buffer-local 'wl-draft-reply-buffer)
103 (make-variable-buffer-local 'wl-draft-parent-folder)
105 (defsubst wl-smtp-password-key (user mechanism server)
106 (format "SMTP:%s/%s@%s"
107 user mechanism server))
109 (defmacro wl-smtp-extension-bind (&rest body)
110 (` (let* ((smtp-sasl-mechanisms
111 (if wl-smtp-authenticate-type
113 (if (listp wl-smtp-authenticate-type)
114 wl-smtp-authenticate-type
115 (list wl-smtp-authenticate-type)))))
116 (smtp-use-sasl (and smtp-sasl-mechanisms t))
117 (smtp-use-starttls (eq wl-smtp-connection-type 'starttls))
118 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
119 (if (and (string= (car smtp-sasl-mechanisms) "DIGEST-MD5")
121 (string-match "^\\([^@]*\\)@\\([^@]*\\)"
122 wl-smtp-posting-user))
123 (setq smtp-sasl-user-name (match-string 1 wl-smtp-posting-user)
124 smtp-sasl-properties (list 'realm
125 (match-string 2 wl-smtp-posting-user)))
126 (setq smtp-sasl-user-name wl-smtp-posting-user
127 smtp-sasl-properties nil))
128 (setq sasl-read-passphrase
132 (wl-smtp-password-key
134 (car smtp-sasl-mechanisms)
138 (defun wl-draft-insert-date-field ()
140 (insert "Date: " (wl-make-date-string) "\n"))
142 (defun wl-draft-insert-from-field ()
144 ;; Put the "From:" field in unless for some odd reason
145 ;; they put one in themselves.
146 (let* ((login (or user-mail-address (user-login-name)))
147 (fullname (user-full-name)))
148 (cond ((eq mail-from-style 'angles)
149 (insert "From: " fullname)
150 (let ((fullname-start (+ (point-min) (length "From: ")))
151 (fullname-end (point-marker)))
152 (goto-char fullname-start)
153 ;; Look for a character that cannot appear unquoted
154 ;; according to RFC 822.
155 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
158 ;; Quote fullname, escaping specials.
159 (goto-char fullname-start)
161 (while (re-search-forward "[\"\\]"
163 (replace-match "\\\\\\&" t))
165 (insert " <" login ">\n"))
166 ((eq mail-from-style 'parens)
167 (insert "From: " login " (")
168 (let ((fullname-start (point)))
170 (let ((fullname-end (point-marker)))
171 (goto-char fullname-start)
172 ;; RFC 822 says \ and nonmatching parentheses
173 ;; must be escaped in comments.
174 ;; Escape every instance of ()\ ...
175 (while (re-search-forward "[()\\]" fullname-end 1)
176 (replace-match "\\\\\\&" t))
177 ;; ... then undo escaping of matching parentheses,
178 ;; including matching nested parentheses.
179 (goto-char fullname-start)
180 (while (re-search-forward
181 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
183 (replace-match "\\1(\\3)" t)
184 (goto-char fullname-start))))
186 ((not mail-from-style)
187 (insert "From: " login "\n")))))
189 (defun wl-draft-insert-x-face-field ()
190 "Insert X-Face header."
192 (if (not (file-exists-p wl-x-face-file))
193 (error "File %s does not exist" wl-x-face-file)
194 (goto-char (point-min))
195 (search-forward mail-header-separator nil t)
197 (wl-draft-insert-x-face-field-here)
198 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
200 (defun wl-draft-insert-x-face-field-here ()
201 "Insert X-Face field at point."
202 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
203 (when (string-match "^\\(X-Face:\\)?[ \t\n]*" x-face-string)
204 (setq x-face-string (substring x-face-string (match-end 0))))
205 (insert "X-Face: " x-face-string))
206 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
208 ;; Insert X-Face-Version: field
209 (when (and (fboundp 'x-face-insert-version-header)
210 (boundp 'x-face-add-x-face-version-header)
211 x-face-add-x-face-version-header)
212 (x-face-insert-version-header)))
214 (defun wl-draft-setup ()
215 (let ((field wl-draft-fields)
219 (list (cons (concat (car field) " ")
220 (concat (car field) " ")))))
221 (setq field (cdr field)))
223 (cons (cons (concat wl-draft-mime-bcc-field-name ": ")
224 (concat wl-draft-mime-bcc-field-name ": "))
226 (setq wl-draft-field-completion-list cl)
227 (setq wl-address-complete-header-regexp
229 (append wl-address-complete-header-list
230 (list (concat wl-draft-mime-bcc-field-name ":")))))))
232 (defun wl-draft-make-mail-followup-to (recipients)
233 (if (elmo-list-member
234 (or wl-user-mail-address-list
235 (list (wl-address-header-extract-address wl-from)))
237 (let ((rlist (elmo-list-delete
238 (or wl-user-mail-address-list
239 (list (wl-address-header-extract-address wl-from)))
243 (lambda (item) (string= (downcase elem)
246 (if (elmo-list-member rlist (mapcar 'downcase
247 wl-subscribed-mailing-list))
249 (append rlist (list (wl-address-header-extract-address
253 (defun wl-draft-delete-myself-from-cc (to cc)
254 (let ((myself (or wl-user-mail-address-list
255 (list (wl-address-header-extract-address wl-from)))))
256 (cond (wl-draft-always-delete-myself ; always-delete option
257 (elmo-list-delete myself cc
260 (lambda (item) (string= (downcase elem)
263 ((elmo-list-member (append to cc) ; subscribed mailing-list
264 (mapcar 'downcase wl-subscribed-mailing-list))
265 (elmo-list-delete myself cc
268 (lambda (item) (string= (downcase elem)
273 (defun wl-draft-forward (original-subject summary-buf)
274 (let (references parent-folder)
275 (with-current-buffer summary-buf
276 (setq parent-folder (wl-summary-buffer-folder-name)))
277 (with-current-buffer (wl-message-get-original-buffer)
278 (setq references (nconc
279 (std11-field-bodies '("References" "In-Reply-To"))
280 (list (std11-field-body "Message-Id"))))
281 (setq references (delq nil references)
282 references (mapconcat 'identity references " ")
283 references (wl-draft-parse-msg-id-list-string references)
284 references (wl-delete-duplicates references)
285 references (when references
286 (mapconcat 'identity references "\n\t"))))
287 (wl-draft (list (cons 'To "")
289 (concat wl-forward-subject-prefix original-subject))
290 (cons 'References references))
291 nil nil nil nil parent-folder))
292 (goto-char (point-max))
293 (wl-draft-insert-message)
294 (mail-position-on-field "To"))
296 (defun wl-draft-strip-subject-re (subject)
297 "Remove \"Re:\" from subject lines. Shamelessly copied from Gnus."
298 (if (string-match wl-subject-prefix-regexp subject)
299 (substring subject (match-end 0))
302 (defun wl-draft-reply-list-symbol (with-arg)
303 "Return symbol `wl-draft-reply-*-argument-list' match condition.
304 Check WITH-ARG and From: field."
305 (if (wl-address-user-mail-address-p (or (elmo-field-body "From") ""))
307 'wl-draft-reply-myself-with-argument-list
308 'wl-draft-reply-myself-without-argument-list)
310 'wl-draft-reply-with-argument-list
311 'wl-draft-reply-without-argument-list)))
313 (defun wl-draft-reply (buf with-arg summary-buf)
314 "Reply to BUF buffer message.
315 Reply to author if WITH-ARG is non-nil."
318 to mail-followup-to cc subject in-reply-to references newsgroups
319 to-alist cc-alist decoder parent-folder)
320 (set-buffer summary-buf)
321 (setq parent-folder (wl-summary-buffer-folder-name))
323 (setq r-list (symbol-value (wl-draft-reply-list-symbol with-arg)))
326 (when (let ((condition (car (car r-list))))
327 (cond ((stringp condition)
328 (std11-field-body condition))
332 (if (not (std11-field-body (car condition)))
334 (setq condition (cdr condition)))
337 (funcall condition))))
338 (let ((r-to-list (nth 0 (cdr (car r-list))))
339 (r-cc-list (nth 1 (cdr (car r-list))))
340 (r-ng-list (nth 2 (cdr (car r-list)))))
341 (when (and (member "Followup-To" r-ng-list)
342 (string= (std11-field-body "Followup-To") "poster"))
343 (setq r-to-list (cons "From" r-to-list))
344 (setq r-ng-list (delete "Followup-To"
345 (copy-sequence r-ng-list))))
346 (if (and r-to-list (symbolp r-to-list))
347 (setq to (wl-concat-list (funcall r-to-list) ","))
348 (setq to (wl-concat-list (cons to
349 (elmo-multiple-fields-body-list
352 (if (and r-cc-list (symbolp r-cc-list))
353 (setq cc (wl-concat-list (funcall r-to-list) ","))
354 (setq cc (wl-concat-list (cons cc
355 (elmo-multiple-fields-body-list
358 (if (and r-ng-list (symbolp r-ng-list))
359 (setq newsgroups (wl-concat-list (funcall r-ng-list) ","))
360 (setq newsgroups (wl-concat-list (cons newsgroups
365 (setq r-list (cdr r-list)))
366 (error "No match field: check your `%s'"
367 (symbol-name (wl-draft-reply-list-symbol with-arg))))
368 (setq subject (std11-field-body "Subject"))
369 (setq to (wl-parse-addresses to)
370 cc (wl-parse-addresses cc))
371 (with-temp-buffer ; to keep raw buffer unibyte.
372 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
373 (setq decoder (mime-find-field-decoder 'Subject 'plain))
374 (setq subject (if (and subject decoder)
375 (funcall decoder subject) subject))
379 (setq decoder (mime-find-field-decoder 'To 'plain))
380 (cons (nth 1 (std11-extract-address-components addr))
381 (if decoder (funcall decoder addr) addr)))
386 (setq decoder (mime-find-field-decoder 'Cc 'plain))
387 (cons (nth 1 (std11-extract-address-components addr))
388 (if decoder (funcall decoder addr) addr)))
390 (and wl-reply-subject-prefix
391 (setq subject (concat wl-reply-subject-prefix
392 (wl-draft-strip-subject-re
394 (setq in-reply-to (std11-field-body "Message-Id"))
395 (setq references (nconc
396 (std11-field-bodies '("References" "In-Reply-To"))
398 (setq to (delq nil (mapcar 'car to-alist)))
399 (setq cc (delq nil (mapcar 'car cc-alist)))
400 ;; if subscribed mailing list is contained in cc or to
401 ;; and myself is contained in cc,
402 ;; delete myself from cc.
403 (setq cc (wl-draft-delete-myself-from-cc to cc))
404 (when wl-insert-mail-followup-to
405 (setq mail-followup-to
406 (wl-draft-make-mail-followup-to (append to cc)))
407 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
408 (with-temp-buffer ; to keep raw buffer unibyte.
409 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
410 (setq newsgroups (wl-parse newsgroups
411 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
412 newsgroups (wl-delete-duplicates newsgroups)
417 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
418 (if decoder (funcall decoder grp) grp))
420 (setq to (wl-delete-duplicates to nil t))
421 (setq cc (wl-delete-duplicates
422 (append (wl-delete-duplicates cc nil t)
423 to (copy-sequence to))
425 (and to (setq to (mapconcat
427 (if wl-draft-reply-use-address-with-full-name
428 (or (cdr (assoc addr to-alist)) addr)
431 (and cc (setq cc (mapconcat
433 (if wl-draft-reply-use-address-with-full-name
434 (or (cdr (assoc addr cc-alist)) addr)
437 (and mail-followup-to
438 (setq mail-followup-to
441 (if wl-draft-reply-use-address-with-full-name
442 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
444 mail-followup-to ",\n\t")))
445 (and (null to) (setq to cc cc nil))
446 (setq references (delq nil references)
447 references (mapconcat 'identity references " ")
448 references (wl-draft-parse-msg-id-list-string references)
449 references (wl-delete-duplicates references)
450 references (if references
451 (mapconcat 'identity references "\n\t")))
452 (wl-draft (list (cons 'To to)
454 (cons 'Newsgroups newsgroups)
455 (cons 'Subject subject)
456 (cons 'In-Reply-To in-reply-to)
457 (cons 'References references)
458 (cons 'Mail-Followup-To mail-followup-to))
459 nil nil nil nil parent-folder)
460 (setq wl-draft-reply-buffer buf))
461 (run-hooks 'wl-reply-hook))
463 (defun wl-draft-add-references ()
464 (wl-draft-add-in-reply-to "References"))
466 (defun wl-draft-add-in-reply-to (&optional alt-field)
467 (let* ((mes-id (save-excursion
468 (set-buffer mail-reply-buffer)
469 (std11-field-body "message-id")))
470 (field (or alt-field "In-Reply-To"))
471 (ref (std11-field-body field))
472 (ref-list nil) (st nil))
473 (when (and mes-id ref)
474 (while (string-match "<[^>]+>" ref st)
476 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
479 (member mes-id ref-list))
483 (when (mail-position-on-field field)
485 (while (looking-at "^[ \t]")
487 (setq mes-id (concat "\t" mes-id "\n")))
491 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
492 &optional ignored-fields)
495 (narrow-to-region (point)(point))
497 (with-current-buffer mail-reply-buffer
499 (decode-mime-charset-region (point-min) (point-max)
501 (buffer-substring-no-properties
502 (point-min) (point-max))))
504 (goto-char (point-min))
505 (wl-draft-delete-fields ignored-fields))
506 (goto-char (point-max))
507 (push-mark (point) nil t)
508 (goto-char (point-min)))
510 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
511 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
512 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
513 (run-hooks 'wl-draft-cited-hook)
514 (when (if wl-draft-add-references
515 (wl-draft-add-references)
516 (if wl-draft-add-in-reply-to
517 (wl-draft-add-in-reply-to)))
518 (wl-highlight-headers 'for-draft)) ; highlight when added References:
519 (when wl-highlight-body-too
520 (wl-highlight-body-region beg (point-max)))))
522 (defun wl-message-news-p ()
523 "If exist valid Newsgroups field, return non-nil."
524 (std11-field-body "Newsgroups"))
526 (defun wl-message-field-exists-p (field)
527 "If FIELD exist and FIELD value is not empty, return non-nil."
528 (let ((value (std11-field-body field)))
530 (not (string= value "")))))
532 (defun wl-message-mail-p ()
533 "If exist To, Cc or Bcc field, return non-nil."
534 (or (wl-message-field-exists-p "To")
535 (wl-message-field-exists-p "Resent-to")
536 (wl-message-field-exists-p "Cc")
537 (wl-message-field-exists-p "Bcc")
538 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
539 ;;; This may be needed..
540 ;;; (wl-message-field-exists-p "Fcc")
543 (defun wl-draft-edit-string (string)
544 (let ((cur-buf (current-buffer))
545 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
546 to subject in-reply-to cc references newsgroups mail-followup-to
547 content-type content-transfer-encoding from
552 (setq to (std11-field-body "To"))
555 (decode-mime-charset-string
558 (setq subject (std11-field-body "Subject"))
559 (setq subject (and subject
561 (decode-mime-charset-string
564 (setq from (std11-field-body "From")
567 (decode-mime-charset-string
570 (setq in-reply-to (std11-field-body "In-Reply-To"))
571 (setq cc (std11-field-body "Cc"))
574 (decode-mime-charset-string
577 (setq references (std11-field-body "References"))
578 (setq newsgroups (std11-field-body "Newsgroups"))
579 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
580 (setq content-type (std11-field-body "Content-Type"))
581 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
582 (goto-char (point-min))
583 (or (re-search-forward "\n\n" nil t)
584 (search-forward (concat mail-header-separator "\n") nil t))
590 (nth 1 (std11-extract-address-components from))
591 wl-user-mail-address-list)
595 (cons 'Subject subject)
596 (cons 'Newsgroups newsgroups)
597 (cons 'Mail-Followup-To mail-followup-to)
598 (cons 'In-Reply-To in-reply-to)
599 (cons 'References references))
600 content-type content-transfer-encoding
601 (buffer-substring (point) (point-max))
603 (and to (mail-position-on-field "To"))
604 (delete-other-windows)
605 (kill-buffer tmp-buf)))
606 (run-hooks 'wl-draft-reedit-hook))
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) 334))
1071 (wl-smtp-password-key
1073 (car smtp-sasl-mechanisms)
1075 (signal (car err) (cdr err)))))
1076 (wl-draft-set-sent-message 'mail 'sent)
1077 (wl-draft-write-sendlog
1078 'ok 'smtp smtp-server recipients id)))))
1079 (if (bufferp errbuf)
1080 (kill-buffer errbuf))))))
1082 (defun wl-draft-send-mail-with-pop-before-smtp ()
1083 "Send the prepared message buffer with POP-before-SMTP."
1084 (require 'elmo-pop3)
1088 :user (or wl-pop-before-smtp-user
1089 elmo-pop3-default-user)
1090 :server (or wl-pop-before-smtp-server
1091 elmo-pop3-default-server)
1092 :port (or wl-pop-before-smtp-port
1093 elmo-pop3-default-port)
1094 :auth (or wl-pop-before-smtp-authenticate-type
1095 elmo-pop3-default-authenticate-type)
1096 :stream-type (or wl-pop-before-smtp-stream-type
1097 elmo-pop3-default-stream-type))))
1098 (condition-case error
1100 (elmo-pop3-get-session session)
1101 (when session (elmo-network-close-session session)))
1103 (elmo-network-close-session session)
1104 (unless (string= (nth 1 error) "Unplugged")
1105 (signal (car error)(cdr error))))))
1106 (wl-draft-send-mail-with-smtp))
1108 (defun wl-draft-insert-required-fields (&optional force-msgid)
1109 "Insert Message-ID, Date, and From field.
1110 If FORCE-MSGID, insert message-id regardless of `wl-insert-message-id'."
1111 ;; Insert Message-Id field...
1112 (goto-char (point-min))
1113 (when (and (or force-msgid
1114 wl-insert-message-id)
1115 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1116 (insert (concat "Message-ID: "
1117 (funcall wl-message-id-function)
1119 ;; Insert date field.
1120 (goto-char (point-min))
1121 (or (re-search-forward "^Date[ \t]*:" nil t)
1122 (wl-draft-insert-date-field))
1123 ;; Insert from field.
1124 (goto-char (point-min))
1125 (or (re-search-forward "^From[ \t]*:" nil t)
1126 (wl-draft-insert-from-field)))
1128 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1129 "Send the message in the current buffer."
1131 (std11-narrow-to-header mail-header-separator)
1132 (wl-draft-insert-required-fields)
1133 ;; Delete null fields.
1134 (goto-char (point-min))
1135 (while (re-search-forward "^[^ \t\n:]+:[ \t]*\n" nil t)
1137 ;; ignore any blank lines in the header
1138 (while (re-search-forward "\n\n\n*" nil t)
1139 (replace-match "\n")))
1140 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1141 (wl-draft-dispatch-message)
1142 (when kill-when-done
1143 ;; hide editing-buffer.
1144 (wl-draft-hide editing-buffer)
1145 ;; delete editing-buffer and its file.
1146 (wl-draft-delete editing-buffer)))
1148 (defun wl-draft-dispatch-message (&optional mes-string)
1149 "Send the message in the current buffer. Not modified the header fields."
1150 (let (delimline mime-bcc)
1151 (if (and wl-draft-verbose-send mes-string)
1152 (message mes-string))
1154 (setq delimline (wl-draft-get-header-delimiter t))
1155 (unless wl-draft-fcc-list
1156 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1158 (setq wl-sent-message-modified nil)
1161 (if (and (wl-message-mail-p)
1162 (not (wl-draft-sent-message-p 'mail)))
1163 (if (or (not (or wl-draft-force-queuing
1164 wl-draft-force-queuing-mail))
1165 (memq 'mail wl-sent-message-queued))
1167 (setq mime-bcc (wl-draft-mime-bcc-field))
1168 (funcall wl-draft-send-mail-function)
1169 (when (not (zerop (length mime-bcc)))
1170 (wl-draft-do-mime-bcc mime-bcc)))
1171 (push 'mail wl-sent-message-queued)
1172 (wl-draft-set-sent-message 'mail 'unplugged)))
1173 (if (and (wl-message-news-p)
1174 (not (wl-draft-sent-message-p 'news))
1175 (not (wl-message-field-exists-p "Resent-to")))
1176 (if (or (not (or wl-draft-force-queuing
1177 wl-draft-force-queuing-news))
1178 (memq 'news wl-sent-message-queued))
1179 (funcall wl-draft-send-news-function)
1180 (push 'news wl-sent-message-queued)
1181 (wl-draft-set-sent-message 'news 'unplugged))))
1182 (let* ((status (wl-draft-sent-message-results))
1183 (unplugged-via (car status))
1184 (sent-via (nth 1 status)))
1185 ;; If one sent, process fcc folder.
1186 (if (and sent-via wl-draft-fcc-list)
1188 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1190 (setq wl-draft-fcc-list nil)))
1191 (if wl-draft-use-cache
1192 (let ((id (std11-field-body "Message-ID"))
1193 (elmo-enable-disconnected-operation t))
1194 (elmo-file-cache-save (elmo-file-cache-get-path id)
1196 ;; If one unplugged, append queue.
1197 (when (and unplugged-via
1198 wl-sent-message-modified)
1199 (if wl-draft-enable-queuing
1201 (wl-draft-queue-append wl-sent-message-via)
1202 (setq wl-sent-message-modified 'requeue))
1203 (error "Unplugged")))
1204 (when wl-draft-verbose-send
1205 (if (and unplugged-via sent-via);; combined message
1207 (setq wl-draft-verbose-msg
1208 (format "Sending%s and Queuing%s..."
1209 sent-via unplugged-via))
1210 (message (concat wl-draft-verbose-msg "done")))
1212 (message (concat mes-string
1213 (if sent-via "done" "failed")))))))))
1214 (not wl-sent-message-modified)) ;; return value
1216 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1217 "Force send current buffer as raw message."
1220 (let (wl-interactive-send
1221 ;;; wl-draft-verbose-send
1222 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1223 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1226 (wl-draft-send kill-when-done mes-string))))
1228 (defun wl-draft-clone-local-variables ()
1229 (let ((locals (buffer-local-variables))
1232 (when (and (consp (car locals))
1234 (string-match wl-draft-clone-local-variable-regexp
1235 (symbol-name (car (car locals)))))
1236 (wl-append result (list (car (car locals)))))
1237 (setq locals (cdr locals)))
1240 (defun wl-draft-send (&optional kill-when-done mes-string)
1241 "Send current draft message.
1242 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1244 ;; Don't call this explicitly.
1245 ;; Added to 'wl-draft-send-hook (by teranisi)
1246 ;; (wl-draft-config-exec)
1247 (run-hooks 'wl-draft-send-hook)
1248 (when (or (not wl-interactive-send)
1249 (y-or-n-p "Do you really want to send current draft? "))
1250 (let ((send-mail-function 'wl-draft-raw-send)
1251 (editing-buffer (current-buffer))
1252 (sending-buffer (wl-draft-generate-clone-buffer
1253 " *wl-draft-sending-buffer*"
1254 (append wl-draft-config-variables
1255 (wl-draft-clone-local-variables))))
1256 (wl-draft-verbose-msg nil)
1259 (save-excursion (set-buffer sending-buffer)
1260 (if (and (not (wl-message-mail-p))
1261 (not (wl-message-news-p)))
1262 (error "No recipient is specified"))
1263 (expand-abbrev) ; for mail-abbrevs
1264 (let ((mime-header-encode-method-alist
1266 '((wl-draft-eword-encode-address-list
1267 . (To Cc Bcc Resent-To Resent-Cc Bcc Resent-Bcc)))
1268 (if (boundp 'mime-header-encode-method-alist)
1269 (symbol-value 'mime-header-encode-method-alist)))))
1270 (run-hooks 'mail-send-hook) ; translate buffer
1273 (if wl-draft-verbose-send
1274 (message (or mes-string "Sending...")))
1275 (funcall wl-draft-send-function editing-buffer kill-when-done)
1276 ;; Now perform actions on successful sending.
1277 (while mail-send-actions
1279 (apply (car (car mail-send-actions))
1280 (cdr (car mail-send-actions)))
1282 (setq mail-send-actions (cdr mail-send-actions)))
1283 (if wl-draft-verbose-send
1284 (message (concat (or wl-draft-verbose-msg
1285 mes-string "Sending...")
1287 ;; kill sending buffer, anyway.
1288 (and (buffer-live-p sending-buffer)
1289 (kill-buffer sending-buffer))))))
1291 (defun wl-draft-mime-bcc-field ()
1292 "Return the MIME-Bcc field body. The field is deleted."
1293 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1294 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1296 (defun wl-draft-do-mime-bcc (field-body)
1297 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1298 (let ((orig-from (mime-decode-field-body (std11-field-body "from")
1300 (orig-subj (mime-decode-field-body (or (std11-field-body "subject")
1303 (recipients (wl-parse-addresses field-body))
1304 (draft-buffer (current-buffer))
1306 (save-window-excursion
1307 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1308 (not (zerop (length field-body))))
1309 (let ((wl-draft-doing-mime-bcc t))
1310 (dolist (recipient recipients)
1311 (wl-draft-create-buffer)
1312 (wl-draft-create-contents
1313 (append `((From . ,orig-from)
1315 (Subject . ,(concat "A blind carbon copy ("
1318 (wl-draft-default-headers)))
1319 (wl-draft-insert-mail-header-separator)
1320 (wl-draft-prepare-edit)
1321 (goto-char (point-max))
1322 (insert (or wl-draft-mime-bcc-body
1323 "This is a blind carbon copy.")
1325 (mime-edit-insert-tag "message" "rfc822")
1326 (insert-buffer draft-buffer)
1327 (let (wl-interactive-send)
1328 (wl-draft-send 'kill-when-done))))))))
1330 ;; Derived from `message-save-drafts' in T-gnus.
1331 (defun wl-draft-save ()
1332 "Save current draft."
1334 (if (buffer-modified-p)
1336 (message "Saving %s..." wl-draft-buffer-file-name)
1337 (let ((msg (buffer-substring-no-properties (point-min) (point-max))))
1338 (with-temp-file wl-draft-buffer-file-name
1340 ;; If no header separator, insert it.
1342 (goto-char (point-min))
1343 (unless (re-search-forward
1344 (concat "^" (regexp-quote mail-header-separator) "$")
1346 (goto-char (point-min))
1347 (if (re-search-forward "\n\n" nil t)
1348 (replace-match (concat "\n" mail-header-separator "\n"))
1349 (goto-char (point-max))
1350 (insert (if (eq (char-before) ?\n) "" "\n")
1351 mail-header-separator "\n"))))
1352 (let ((mime-header-encode-method-alist
1353 '((eword-encode-unstructured-field-body))))
1354 (mime-edit-translate-buffer))
1355 (wl-draft-get-header-delimiter t)))
1356 (set-buffer-modified-p nil)
1357 (wl-draft-config-info-operation
1358 (and (string-match "[0-9]+$" wl-draft-buffer-file-name)
1360 (match-string 0 wl-draft-buffer-file-name)))
1362 (message "Saving %s...done" wl-draft-buffer-file-name))
1363 (message "(No changes need to be saved)")))
1365 (defun wl-draft-mimic-kill-buffer ()
1366 "Kill the current (draft) buffer with query."
1368 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1371 (if (or (not bufname)
1372 (string-equal bufname "")
1373 (string-equal bufname (buffer-name)))
1374 (let ((bufname (current-buffer)))
1375 (when (or (not (buffer-modified-p))
1377 (format "Buffer %s modified; kill anyway? " bufname)))
1378 (set-buffer-modified-p nil)
1379 (wl-draft-hide bufname)
1380 (kill-buffer bufname)))
1381 (kill-buffer bufname))))
1383 (defun wl-draft-save-and-exit ()
1384 "Save current draft and exit current draft mode."
1387 (let ((editing-buffer (current-buffer)))
1388 (wl-draft-hide editing-buffer)
1389 (kill-buffer editing-buffer)))
1391 (defun wl-draft-send-and-exit ()
1392 "Send current draft message and kill it."
1396 (defun wl-draft-send-from-toolbar ()
1398 (let ((wl-interactive-send t))
1399 (wl-draft-send-and-exit)))
1401 (defun wl-draft-delete-field (field &optional delimline replace)
1402 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1404 (defun wl-draft-delete-fields (field &optional delimline replace)
1407 (goto-char (point-min))
1408 (if (search-forward "\n\n" nil t)
1409 (setq delimline (point))
1410 (setq delimline (point-max))))
1411 (narrow-to-region (point-min) delimline)
1412 (goto-char (point-min))
1413 (let ((regexp (concat "^" field ":"))
1414 (case-fold-search t))
1416 (if (looking-at regexp)
1422 (if (re-search-forward "^[^ \t]" nil t)
1423 (goto-char (match-beginning 0))
1426 (insert (concat field ": " replace "\n"))))
1428 (if (re-search-forward "^[^ \t]" nil t)
1429 (goto-char (match-beginning 0))
1432 (defun wl-draft-get-fcc-list (header-end)
1433 (if (and wl-draft-doing-mime-bcc
1434 wl-draft-disable-fcc-for-mime-bcc)
1436 (wl-draft-delete-field "fcc")
1439 (case-fold-search t))
1440 (or (markerp header-end) (error "HEADER-END must be a marker"))
1442 (goto-char (point-min))
1443 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1448 (buffer-substring-no-properties
1452 (skip-chars-backward " \t")
1455 (dolist (folder fcc-list)
1456 (wl-folder-confirm-existence
1457 (wl-folder-get-elmo-folder (eword-decode-string folder)))))
1458 (delete-region (match-beginning 0)
1459 (progn (forward-line 1) (point)))))
1462 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1463 (let ((send-mail-buffer (current-buffer))
1464 (tembuf (generate-new-buffer " fcc output"))
1465 (case-fold-search t)
1467 (or (markerp header-end) (error "HEADER-END must be a marker"))
1470 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1473 ;; insert just the headers to avoid moving the gap more than
1474 ;; necessary (the message body could be arbitrarily huge.)
1475 (insert-buffer-substring send-mail-buffer 1 header-end)
1476 (wl-draft-insert-required-fields t)
1477 (goto-char (point-max))
1478 (insert-buffer-substring send-mail-buffer header-end)
1479 (let ((id (std11-field-body "Message-ID"))
1480 (elmo-enable-disconnected-operation t)
1483 (unless (or cache-saved
1484 (elmo-folder-plugged-p
1485 (wl-folder-get-elmo-folder (car fcc-list))))
1486 (elmo-file-cache-save id nil) ;; for disconnected operation
1487 (setq cache-saved t))
1488 (if (elmo-folder-append-buffer
1489 (wl-folder-get-elmo-folder
1490 (eword-decode-string (car fcc-list)))
1491 (not wl-fcc-force-as-read))
1492 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1493 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1494 (setq fcc-list (cdr fcc-list)))))
1495 (kill-buffer tembuf)))
1497 (defun wl-draft-on-field-p ()
1500 (goto-char (point-min))
1501 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1508 (if (or (looking-at ".*,[ \t]?$")
1509 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1511 (let ((pos (point)))
1514 (if (looking-at "^[ \t]")
1516 (if (re-search-forward ":" pos t) nil t)))))))
1520 (defun wl-draft (&optional header-alist
1521 content-type content-transfer-encoding
1524 "Write and send mail/news message with Wanderlust."
1531 (wl-plugged-init t))
1533 (wl-init)) ; returns immediately if already initialized.
1536 (let (buf-name header-alist-internal)
1538 (wl-draft-create-buffer
1540 (eq this-command 'wl-draft)
1541 (eq this-command 'wl-summary-write)
1542 (eq this-command 'wl-summary-write-current-folder)
1543 (eq this-command 'wl-folder-write-current-folder))
1546 (unless (cdr (assq 'From header-alist))
1548 (append (list (cons 'From wl-from)) header-alist)))
1549 (unless (cdr (assq 'To header-alist))
1554 (if (assq 'To header-alist)
1555 (setcdr (assq 'To header-alist) to)
1557 (append header-alist
1558 (list (cons 'To to))))))))
1559 (unless (cdr (assq 'Subject header-alist))
1560 (if (assq 'Subject header-alist)
1561 (setcdr (assq 'Subject header-alist) "")
1563 (append header-alist (list (cons 'Subject ""))))))
1564 (setq header-alist (append header-alist
1565 (wl-draft-default-headers)
1566 wl-draft-additional-header-alist
1567 (if body (list "" (cons 'Body body)))))
1568 (wl-draft-create-contents header-alist)
1570 (wl-draft-decode-body
1571 content-type content-transfer-encoding))
1572 (wl-draft-insert-mail-header-separator)
1573 (wl-draft-prepare-edit)
1575 (run-hooks 'wl-mail-setup-hook))
1576 (goto-char (point-min))
1577 (wl-user-agent-compose-internal) ;; user-agent
1580 (string= (cdr (assq 'To header-alist)) ""))
1581 (mail-position-on-field "To"))
1583 (goto-char (point-max))))
1586 (defun wl-draft-create-buffer (&optional full parent-folder)
1587 (let* ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1588 (parent-folder (or parent-folder (wl-summary-buffer-folder-name)))
1589 (summary-buf (wl-summary-get-buffer parent-folder))
1590 buf-name file-name num change-major-mode-hook)
1591 (if (not (elmo-folder-message-file-p draft-folder))
1592 (error "%s folder cannot be used for draft folder" wl-draft-folder))
1593 (setq num (elmo-max-of-list
1594 (or (elmo-folder-list-messages draft-folder) '(0))))
1595 (setq num (+ 1 num))
1596 ;; To get unused buffer name.
1597 (while (get-buffer (concat wl-draft-folder "/" (int-to-string num)))
1598 (setq num (+ 1 num)))
1599 (setq buf-name (find-file-noselect
1601 (elmo-message-file-name
1602 (wl-folder-get-elmo-folder wl-draft-folder)
1604 (if wl-draft-use-frame
1605 (switch-to-buffer-other-frame buf-name)
1606 (switch-to-buffer buf-name))
1607 (set-buffer buf-name)
1608 (if (not (string-match (regexp-quote wl-draft-folder)
1610 (rename-buffer (concat wl-draft-folder "/" (int-to-string num))))
1611 (if (or (eq wl-draft-reply-buffer-style 'full)
1613 (delete-other-windows))
1616 (make-local-variable 'truncate-partial-width-windows)
1617 (setq truncate-partial-width-windows nil)
1618 (setq truncate-lines wl-draft-truncate-lines)
1619 (setq wl-sent-message-via nil)
1620 (setq wl-sent-message-queued nil)
1621 (setq wl-draft-buffer-file-name file-name)
1622 (setq wl-draft-config-exec-flag t)
1623 (setq wl-draft-parent-folder (or parent-folder ""))
1624 (or (eq this-command 'wl-folder-write-current-folder)
1625 (setq wl-draft-buffer-cur-summary-buffer summary-buf))
1628 (defun wl-draft-create-contents (header-alist)
1629 "header-alist' sample
1630 '(function ;; funcall
1631 string ;; insert string
1632 (symbol . string) ;; insert symbol-value: string
1633 (symbol . function) ;; (funcall) and if it returns string,
1634 ;; insert symbol-value: string
1635 (symbol . nil) ;; do nothing
1639 (unless (eq major-mode 'wl-draft-mode)
1640 (error "wl-draft-create-header must be use in wl-draft-mode."))
1641 (let ((halist header-alist)
1646 ((functionp (car halist)) (funcall (car halist)))
1648 ((stringp (car halist)) (insert (car halist) "\n"))
1650 ((consp (car halist))
1651 (setq field (car (car halist)))
1652 (setq value (cdr (car halist)))
1656 ((eq field 'Body) ; body
1658 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1660 (let ((value-return (funcall value)))
1661 (when (stringp value-return)
1662 (insert (symbol-name field) ": " value-return "\n"))))
1671 (setq halist (cdr halist)))))
1673 (defun wl-draft-prepare-edit ()
1674 (unless (eq major-mode 'wl-draft-mode)
1675 (error "wl-draft-create-header must be use in wl-draft-mode."))
1676 (let (change-major-mode-hook)
1677 (wl-draft-editor-mode)
1678 (add-hook 'local-write-file-hooks 'wl-draft-save)
1679 (wl-draft-overload-functions)
1680 (wl-highlight-headers 'for-draft)
1682 (clear-visited-file-modtime)))
1684 (defun wl-draft-decode-header ()
1686 (std11-narrow-to-header)
1687 (wl-draft-decode-message-in-buffer)
1690 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1693 (std11-field-body "content-type")))
1694 (content-transfer-encoding
1695 (or content-transfer-encoding
1696 (std11-field-body "content-transfer-encoding")))
1699 (std11-narrow-to-header)
1700 (wl-draft-delete-field "content-type")
1701 (wl-draft-delete-field "content-transfer-encoding")
1702 (goto-char (point-max))
1703 (setq delimline (point-marker))
1705 (narrow-to-region delimline (point-max))
1706 (goto-char (point-min))
1708 (insert "Content-type: " content-type "\n"))
1709 (when content-transfer-encoding
1710 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1711 (wl-draft-decode-message-in-buffer)
1712 (goto-char (point-min))
1713 (unless (re-search-forward "^$" (point-at-eol) t)
1718 ;;; subroutine for wl-draft-create-contents
1719 ;;; must be used in wl-draft-mode
1720 (defun wl-draft-check-new-line ()
1721 (if (not (= (preceding-char) ?\n))
1724 (defsubst wl-draft-trim-ccs (cc)
1730 (null (and wl-draft-delete-myself-from-bcc-fcc
1732 (mapcar 'wl-address-header-extract-address
1734 (wl-parse-addresses (std11-field-body "To"))
1735 (wl-parse-addresses (std11-field-body "Cc"))))
1736 (mapcar 'downcase wl-subscribed-mailing-list)))))
1740 (defsubst wl-draft-default-headers ()
1742 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1743 (wl-address-header-extract-address
1745 (cons 'User-Agent wl-generate-mailer-string-function)
1746 (cons 'Reply-To mail-default-reply-to)
1747 (cons 'Bcc (function
1750 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1751 (cons 'Fcc (function
1753 (wl-draft-trim-ccs wl-fcc))))
1754 (cons 'Organization wl-organization)
1755 (and wl-auto-insert-x-face
1756 (file-exists-p wl-x-face-file)
1757 'wl-draft-insert-x-face-field-here) ;; allow nil
1758 mail-default-headers
1759 ;; check \n at th end of line for `mail-default-headers'
1760 'wl-draft-check-new-line
1763 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1766 (goto-char delimline)
1767 (goto-char (point-min))
1768 (if (search-forward "\n\n" nil t)
1769 (delete-backward-char 1)
1770 (goto-char (point-max))))
1771 (wl-draft-check-new-line)
1772 (put-text-property (point)
1774 (insert mail-header-separator "\n")
1776 'category 'mail-header-separator)))
1780 (defun wl-draft-elmo-nntp-send ()
1781 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1782 (elmo-nntp-default-user
1783 (or wl-nntp-posting-user elmo-nntp-default-user))
1784 (elmo-nntp-default-server
1785 (or wl-nntp-posting-server elmo-nntp-default-server))
1786 (elmo-nntp-default-port
1787 (or wl-nntp-posting-port elmo-nntp-default-port))
1788 (elmo-nntp-default-stream-type
1789 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1790 (elmo-nntp-default-function wl-nntp-posting-function)
1792 (if (setq condition (cdr (elmo-string-matched-assoc
1793 (std11-field-body "Newsgroups")
1794 wl-nntp-posting-config-alist)))
1795 (if (stringp condition)
1796 (setq elmo-nntp-default-server condition)
1797 (while (car condition)
1798 (set (intern (format "elmo-nntp-default-%s"
1799 (symbol-name (caar condition))))
1801 (setq condition (cdr condition)))))
1802 (unless elmo-nntp-default-function
1803 (error "wl-draft-nntp-send: posting-function is nil."))
1804 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1805 (wl-draft-set-sent-message 'news 'unplugged
1806 (cons elmo-nntp-default-server
1807 elmo-nntp-default-port))
1808 (funcall elmo-nntp-default-function
1809 elmo-nntp-default-server (current-buffer))
1810 (wl-draft-set-sent-message 'news 'sent)
1811 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1812 (std11-field-body "Newsgroups")
1813 (std11-field-body "Message-ID")))))
1815 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1816 "Generate clone of current buffer named NAME."
1817 (let ((editing-buffer (current-buffer)))
1819 (set-buffer (generate-new-buffer name))
1822 (wl-draft-editor-mode)
1823 (insert-buffer editing-buffer)
1825 (while local-variables
1826 (make-local-variable (car local-variables))
1827 (set (car local-variables)
1829 (set-buffer editing-buffer)
1830 (symbol-value (car local-variables))))
1831 (setq local-variables (cdr local-variables)))
1834 (defun wl-draft-reedit (number)
1835 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1837 buffer file-name change-major-mode-hook)
1838 (setq file-name (elmo-message-file-name draft-folder number))
1839 (unless (file-exists-p file-name)
1840 (error "File %s does not exist" file-name))
1841 (if (setq buffer (get-buffer
1842 (concat wl-draft-folder "/"
1843 (number-to-string number))))
1845 (if wl-draft-use-frame
1846 (switch-to-buffer-other-frame buffer)
1847 (switch-to-buffer buffer))
1848 (set-buffer buffer))
1849 (setq buffer (get-buffer-create (number-to-string number)))
1850 (if wl-draft-use-frame
1851 (switch-to-buffer-other-frame buffer)
1852 (switch-to-buffer buffer))
1854 (insert-file-contents-as-binary file-name)
1855 (let((mime-edit-again-ignored-field-regexp
1856 "^\\(Content-.*\\|Mime-Version\\):"))
1857 (wl-draft-decode-message-in-buffer))
1858 (wl-draft-insert-mail-header-separator)
1859 (if (not (string-match (regexp-quote wl-draft-folder)
1861 (rename-buffer (concat wl-draft-folder "/" (buffer-name))))
1864 (setq buffer-file-name file-name)
1865 (make-local-variable 'truncate-partial-width-windows)
1866 (setq truncate-partial-width-windows nil)
1867 (setq truncate-lines wl-draft-truncate-lines)
1868 (setq wl-sent-message-via nil)
1869 (setq wl-sent-message-queued nil)
1870 (setq wl-draft-buffer-file-name file-name)
1871 (wl-draft-config-info-operation number 'load)
1872 (goto-char (point-min))
1873 (wl-draft-overload-functions)
1874 (wl-draft-editor-mode)
1875 (add-hook 'local-write-file-hooks 'wl-draft-save)
1876 (wl-highlight-headers 'for-draft)
1877 (run-hooks 'wl-draft-reedit-hook)
1878 (goto-char (point-max))
1881 (defmacro wl-draft-body-goto-top ()
1883 (goto-char (point-min))
1884 (if (re-search-forward mail-header-separator nil t)
1886 (goto-char (point-max))))))
1888 (defmacro wl-draft-body-goto-bottom ()
1889 (` (goto-char (point-max))))
1891 (defmacro wl-draft-config-body-goto-header ()
1893 (goto-char (point-min))
1894 (if (re-search-forward mail-header-separator nil t)
1896 (goto-char (point-max))))))
1898 (defun wl-draft-config-sub-body (content)
1899 (wl-draft-body-goto-top)
1900 (delete-region (point) (point-max))
1901 (if content (insert (eval content))))
1903 (defun wl-draft-config-sub-top (content)
1904 (wl-draft-body-goto-top)
1905 (if content (insert (eval content))))
1907 (defun wl-draft-config-sub-bottom (content)
1908 (wl-draft-body-goto-bottom)
1909 (if content (insert (eval content))))
1911 (defun wl-draft-config-sub-header (content)
1912 (wl-draft-config-body-goto-header)
1913 (if content (insert (concat (eval content) "\n"))))
1915 (defun wl-draft-config-sub-header-top (content)
1916 (goto-char (point-min))
1917 (if content (insert (concat (eval content) "\n"))))
1919 (defun wl-draft-config-sub-part-top (content)
1920 (goto-char (mime-edit-content-beginning))
1921 (if content (insert (concat (eval content) "\n"))))
1923 (defun wl-draft-config-sub-part-bottom (content)
1924 (goto-char (mime-edit-content-end))
1925 (if content (insert (concat (eval content) "\n"))))
1927 (defsubst wl-draft-config-sub-file (content)
1928 (let ((coding-system-for-read wl-cs-autoconv)
1929 (file (expand-file-name (eval content))))
1930 (if (file-exists-p file)
1931 (insert-file-contents file)
1932 (error "%s: no exists file" file))))
1934 (defun wl-draft-config-sub-body-file (content)
1935 (wl-draft-body-goto-top)
1936 (delete-region (point) (point-max))
1937 (wl-draft-config-sub-file content))
1939 (defun wl-draft-config-sub-top-file (content)
1940 (wl-draft-body-goto-top)
1941 (wl-draft-config-sub-file content))
1943 (defun wl-draft-config-sub-bottom-file (content)
1944 (wl-draft-body-goto-bottom)
1945 (wl-draft-config-sub-file content))
1947 (defun wl-draft-config-sub-header-file (content)
1948 (wl-draft-config-body-goto-header)
1949 (wl-draft-config-sub-file content))
1951 (defun wl-draft-config-sub-template (content)
1952 (setq wl-draft-config-variables
1953 (wl-template-insert (eval content))))
1955 (defun wl-draft-config-sub-x-face (content)
1956 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
1957 (fboundp 'x-face-insert)) ; x-face.el is installed.
1958 (x-face-insert content)
1959 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
1961 (defsubst wl-draft-config-sub-func (field content)
1963 (if (setq func (assq field wl-draft-config-sub-func-alist))
1964 (let (wl-draft-config-variables)
1965 (funcall (cdr func) content)
1966 ;; for wl-draft-config-sub-template
1967 (cons t wl-draft-config-variables)))))
1969 (defsubst wl-draft-config-exec-sub (clist)
1970 (let (config local-variables)
1972 (setq config (car clist))
1977 (let ((field (car config))
1978 (content (cdr config))
1982 (wl-draft-replace-field field (eval content) t))
1983 ((setq ret-val (wl-draft-config-sub-func field content))
1984 (if (cdr ret-val) ;; for wl-draft-config-sub-template
1985 (wl-append local-variables (cdr ret-val))))
1986 ((boundp field) ;; variable
1987 (make-local-variable field)
1988 (set field (eval content))
1989 (wl-append local-variables (list field)))
1991 (error "%s: not variable" field)))))
1993 (error "%s: not supported type" config)))
1994 (setq clist (cdr clist)))
1997 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
1998 "Change headers in draft preparation time."
2000 (unless wl-draft-reedit
2003 (and (boundp 'wl-draft-prepared-config-alist)
2004 wl-draft-prepared-config-alist) ;; For compatible.
2005 wl-draft-config-alist)))
2007 (wl-draft-config-exec config-alist reply-buf)))))
2009 (defun wl-draft-config-exec (&optional config-alist reply-buf)
2010 "Change headers according to the value of `wl-draft-config-alist'.
2011 Automatically applied in draft sending time."
2013 (let ((case-fold-search t)
2014 (alist (or config-alist wl-draft-config-alist))
2015 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2016 wl-draft-reply-buffer)))
2017 (local-variables wl-draft-config-variables)
2019 (when (and (or (interactive-p)
2020 wl-draft-config-exec-flag)
2025 (setq key (caar alist)
2032 (set-buffer reply-buf)
2034 (std11-narrow-to-header)
2035 (goto-char (point-min))
2036 (re-search-forward (car clist) nil t))))
2037 (wl-draft-config-exec-sub (cdr clist))
2040 (when (save-restriction
2041 (std11-narrow-to-header mail-header-separator)
2042 (goto-char (point-min))
2043 (re-search-forward key nil t))
2044 (wl-append local-variables
2045 (wl-draft-config-exec-sub clist))
2048 (wl-append local-variables
2049 (wl-draft-config-exec-sub clist))
2051 (if (and found wl-draft-config-matchone)
2053 (setq alist (cdr alist))))
2055 (setq wl-draft-config-exec-flag nil))
2056 (run-hooks 'wl-draft-config-exec-hook)
2057 (put-text-property (point-min)(point-max) 'face nil)
2058 (wl-highlight-message (point-min)(point-max) t)
2059 (setq wl-draft-config-variables
2060 (elmo-uniq-list local-variables))))))
2062 (defun wl-draft-replace-field (field content &optional add)
2065 (let ((case-fold-search t)
2066 (inhibit-read-only t) ;; added by teranisi.
2068 (std11-narrow-to-header mail-header-separator)
2069 (goto-char (point-min))
2070 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2075 (re-search-forward "^[^ \t]" nil 'move)
2077 (skip-chars-backward "\n")
2078 (delete-region beg (point))
2079 (insert " " content))
2084 (re-search-forward "^[^ \t]" nil 'move)
2086 (delete-region beg (point)))
2087 (when (and add content)
2089 (goto-char (point-max))
2090 (insert (concat field ": " content "\n"))))))))
2092 (defun wl-draft-config-info-operation (msg operation)
2093 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-folder-get-elmo-folder
2097 (format "%s-%d" wl-draft-config-save-filename msg)
2099 element alist variable)
2101 ((eq operation 'save)
2102 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2103 (while (setq variable (pop variables))
2104 (when (boundp variable)
2106 (list (cons variable (eval variable))))))
2107 (elmo-object-save filename alist)))
2108 ((eq operation 'load)
2109 (setq alist (elmo-object-load filename))
2110 (while (setq element (pop alist))
2111 (set (make-local-variable (car element)) (cdr element))
2112 (wl-append wl-draft-config-variables (list (car element)))))
2113 ((eq operation 'delete)
2114 (if (file-exists-p filename)
2115 (delete-file filename))))))
2117 (defun wl-draft-queue-info-operation (msg operation
2118 &optional add-sent-message-via)
2119 (let* ((msgdb-dir (elmo-folder-msgdb-path
2120 (wl-folder-get-elmo-folder wl-queue-folder)))
2123 (format "%s-%d" wl-draft-queue-save-filename msg)
2125 element alist variable)
2127 ((eq operation 'save)
2128 (let ((variables (elmo-uniq-list
2129 (append wl-draft-queue-save-variables
2130 wl-draft-config-variables
2131 (list 'wl-draft-fcc-list)))))
2132 (if add-sent-message-via
2134 (push 'wl-sent-message-queued variables)
2135 (push 'wl-sent-message-via variables)))
2136 (while (setq variable (pop variables))
2137 (when (boundp variable)
2139 (list (cons variable (eval variable))))))
2140 (elmo-object-save filename alist)))
2141 ((eq operation 'load)
2142 (setq alist (elmo-object-load filename))
2143 (while (setq element (pop alist))
2144 (set (make-local-variable (car element)) (cdr element))))
2145 ((eq operation 'get-sent-via)
2146 (setq alist (elmo-object-load filename))
2147 (cdr (assq 'wl-sent-message-via alist)))
2148 ((eq operation 'delete)
2149 (if (file-exists-p filename)
2150 (delete-file filename))))))
2152 (defun wl-draft-queue-append (wl-sent-message-via)
2153 (if wl-draft-verbose-send
2154 (message "Queuing..."))
2155 (let ((send-buffer (current-buffer))
2156 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2157 (message-id (std11-field-body "Message-ID")))
2158 (if (elmo-folder-append-buffer folder t)
2160 (wl-draft-queue-info-operation
2161 (car (elmo-folder-status folder))
2162 'save wl-sent-message-via)
2163 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2164 (when wl-draft-verbose-send
2165 (setq wl-draft-verbose-msg "Queuing...")
2166 (message "Queuing...done")))
2167 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2168 (error "Queuing failed"))))
2170 (defun wl-draft-queue-flush ()
2171 "Flush draft queue."
2173 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2175 (elmo-folder-open-internal queue-folder)
2176 (elmo-folder-list-messages queue-folder)))
2179 (wl-draft-queue-flushing t)
2180 msgs failure len buffer msgid sent-via)
2181 ;; get plugged send message
2183 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2186 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2187 (or (not (nth 2 (car sent-via)))
2189 (car (nth 2 (car sent-via)))
2190 (cdr (nth 2 (car sent-via))))))
2191 (wl-append msgs (list (car msgs2)))
2193 (setq sent-via (cdr sent-via))))
2194 (setq msgs2 (cdr msgs2)))
2195 (when (> (setq len (length msgs)) 0)
2196 (if (elmo-y-or-n-p (format
2197 "%d message(s) are in the sending queue. Send now? "
2199 (not elmo-dop-flush-confirm) t)
2202 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2205 ;; reset buffer local variables
2206 (kill-all-local-variables)
2210 (setq wl-sent-message-via nil)
2211 (wl-draft-queue-info-operation (car msgs) 'load)
2212 (elmo-message-fetch queue-folder
2214 (elmo-make-fetch-strategy 'entire)
2215 nil (current-buffer))
2217 (setq failure (funcall
2218 wl-draft-queue-flush-send-function
2219 (format "Sending (%d/%d)..." i len)))
2220 ;;; (wl-draft-raw-send nil nil
2221 ;;; (format "Sending (%d/%d)..." i len))
2223 (elmo-display-error err t)
2227 (if (eq wl-sent-message-modified 'requeue)
2229 (elmo-folder-delete-messages
2230 queue-folder (cons (car msgs) nil))
2231 (wl-draft-queue-info-operation (car msgs) 'delete))
2233 (elmo-folder-delete-messages
2234 queue-folder (cons (car msgs) nil))
2235 (wl-draft-queue-info-operation (car msgs) 'delete)
2236 (setq performed (+ 1 performed))))
2237 (setq msgs (cdr msgs)))
2238 (kill-buffer buffer)
2239 (message "%d message(s) are sent." performed)))
2240 (message "%d message(s) are remained to be sent." len))
2241 (elmo-folder-close queue-folder)
2244 (defun wl-jump-to-draft-buffer (&optional arg)
2245 "Jump to the draft if exists."
2248 (wl-jump-to-draft-folder)
2249 (let ((draft-bufs (wl-collect-draft))
2253 (message "No draft buffer exist."))
2256 (sort (mapcar 'buffer-name draft-bufs)
2257 (function (lambda (a b)
2258 (not (string< a b))))))
2259 (if (setq buf (cdr (member (buffer-name)
2261 (setq buf (car buf))
2262 (setq buf (car draft-bufs)))
2263 (switch-to-buffer buf))))))
2265 (defun wl-jump-to-draft-folder ()
2266 (let ((msgs (reverse (elmo-folder-list-messages (wl-folder-get-elmo-folder
2268 (mybuf (buffer-name))
2271 (message "No draft message exist.")
2272 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2273 (setq msg (cadr (memq
2274 (string-to-int (substring mybuf (match-end 0)))
2276 (or msg (setq msg (car msgs)))
2277 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2278 (switch-to-buffer buf)
2279 (wl-draft-reedit msg)))))
2281 (defun wl-draft-highlight-and-recenter (&optional n)
2283 (if wl-highlight-body-too
2284 (let ((beg (point-min))
2286 (put-text-property beg end 'face nil)
2287 (wl-highlight-message beg end t)))
2290 ;;;; user-agent support by Sen Nagata
2292 ;; this appears to be necessarily global...
2293 (defvar wl-user-agent-compose-p nil)
2294 (defvar wl-user-agent-headers-and-body-alist nil)
2296 ;; this should be a generic function for mail-mode -- i wish there was
2297 ;; something like it in sendmail.el
2298 (defun wl-user-agent-insert-header (header-name header-value)
2299 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2300 ;; it seems like overriding existing headers is acceptable -- should
2301 ;; we provide an option?
2303 ;; plan was: unfold header (might be folded), remove existing value, insert
2305 ;; wl doesn't seem to fold header lines yet anyway :-)
2307 (let ((kill-whole-line t)
2309 (mail-position-on-field (capitalize header-name))
2310 (setq end-of-line (point))
2312 (re-search-forward ":" end-of-line)
2313 (insert (concat " " header-value "\n"))
2316 ;; this should be a generic function for mail-mode -- i wish there was
2317 ;; something like it in sendmail.el
2319 ;; ** haven't dealt w/ case where the body is already set **
2320 (defun wl-user-agent-insert-body (body-text)
2321 "Insert a body of text, BODY-TEXT, into a message."
2322 ;; code defensively... :-P
2323 (goto-char (point-min))
2324 (search-forward mail-header-separator)
2329 (defun wl-user-agent-compose (&optional to subject other-headers continue
2330 switch-function yank-action
2332 "Support the `compose-mail' interface for wl.
2333 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2334 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2335 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2337 (unless (featurep 'wl)
2339 ;; protect these -- to and subject get bound at some point, so it looks
2340 ;; to be necessary to protect the values used w/in
2341 (let ((wl-user-agent-headers-and-body-alist other-headers)
2342 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2343 (wl-draft-reply-buffer-style 'split))
2344 (when (eq switch-function 'switch-to-buffer-other-window)
2345 (when (one-window-p t)
2346 (if (window-minibuffer-p) (other-window 1))
2350 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2353 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2356 (setq wl-user-agent-headers-and-body-alist
2357 (cons (cons "to" to)
2358 wl-user-agent-headers-and-body-alist))))
2360 (if (wl-string-match-assoc "subject"
2361 wl-user-agent-headers-and-body-alist
2364 (wl-string-match-assoc "subject"
2365 wl-user-agent-headers-and-body-alist
2368 (setq wl-user-agent-headers-and-body-alist
2369 (cons (cons "subject" subject)
2370 wl-user-agent-headers-and-body-alist))))
2371 ;; i think this is what we want to use...
2374 ;; tell the hook-function to do its stuff
2375 (setq wl-user-agent-compose-p t)
2376 ;; because to get the hooks working, wl-draft has to think it has
2377 ;; been called interactively
2378 (call-interactively 'wl-draft))
2379 (setq wl-user-agent-compose-p nil))))
2381 (defun wl-user-agent-compose-internal ()
2382 "Manipulate headers and/or a body of a draft message."
2383 ;; being called from wl-user-agent-compose?
2384 (if wl-user-agent-compose-p
2387 (let ((headers wl-user-agent-headers-and-body-alist)
2388 (case-fold-search t))
2391 (if (not (string-match "^body$" (car (car headers))))
2392 (wl-user-agent-insert-header
2393 (car (car headers)) (cdr (car headers)))
2395 (setq headers (cdr headers))))
2396 ;; highlight headers (from wl-draft in wl-draft.el)
2397 (wl-highlight-headers 'for-draft)
2399 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2401 (wl-user-agent-insert-body
2402 (cdr (wl-string-match-assoc
2404 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2408 (product-provide (provide 'wl-draft) (require 'wl-version))
2410 ;;; wl-draft.el ends here