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 (beginning-of-buffer)
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)))
240 (copy-sequence recipients))))
241 (if (elmo-list-member rlist (mapcar 'downcase
242 wl-subscribed-mailing-list))
244 (append rlist (list (wl-address-header-extract-address
248 (defun wl-draft-delete-myself-from-cc (to cc)
249 (let ((myself (or wl-user-mail-address-list
250 (list (wl-address-header-extract-address wl-from)))))
251 (cond (wl-draft-always-delete-myself ; always-delete option
252 (elmo-list-delete myself cc))
253 ((elmo-list-member (append to cc) ; subscribed mailing-list
254 (mapcar 'downcase wl-subscribed-mailing-list))
255 (elmo-list-delete myself cc))
258 (defun wl-draft-forward (original-subject summary-buf)
259 (let (references parent-folder)
260 (with-current-buffer summary-buf
261 (setq parent-folder (wl-summary-buffer-folder-name)))
262 (with-current-buffer (wl-message-get-original-buffer)
263 (setq references (nconc
264 (std11-field-bodies '("References" "In-Reply-To"))
265 (list (std11-field-body "Message-Id"))))
266 (setq references (delq nil references)
267 references (mapconcat 'identity references " ")
268 references (wl-draft-parse-msg-id-list-string references)
269 references (wl-delete-duplicates references)
270 references (when references
271 (mapconcat 'identity references "\n\t"))))
272 (wl-draft (list (cons 'To "")
274 (concat wl-forward-subject-prefix original-subject))
275 (cons 'References references))
276 nil nil nil nil parent-folder))
277 (goto-char (point-max))
278 (wl-draft-insert-message)
279 (mail-position-on-field "To"))
281 (defun wl-draft-strip-subject-re (subject)
282 "Remove \"Re:\" from subject lines. Shamelessly copied from Gnus."
283 (if (string-match wl-subject-prefix-regexp subject)
284 (substring subject (match-end 0))
287 (defun wl-draft-reply-list-symbol (with-arg)
288 "Return symbol `wl-draft-reply-*-argument-list' match condition.
289 Check WITH-ARG and From: field."
290 (if (wl-address-user-mail-address-p (or (elmo-field-body "From") ""))
292 'wl-draft-reply-myself-with-argument-list
293 'wl-draft-reply-myself-without-argument-list)
295 'wl-draft-reply-with-argument-list
296 'wl-draft-reply-without-argument-list)))
298 (defun wl-draft-reply (buf with-arg summary-buf)
299 "Reply to BUF buffer message.
300 Reply to author if WITH-ARG is non-nil."
303 to mail-followup-to cc subject in-reply-to references newsgroups
304 to-alist cc-alist decoder parent-folder)
305 (set-buffer summary-buf)
306 (setq parent-folder (wl-summary-buffer-folder-name))
308 (setq r-list (symbol-value (wl-draft-reply-list-symbol with-arg)))
311 (when (let ((condition (car (car r-list))))
312 (cond ((stringp condition)
313 (std11-field-body condition))
317 (if (not (std11-field-body (car condition)))
319 (setq condition (cdr condition)))
322 (funcall condition))))
323 (let ((r-to-list (nth 0 (cdr (car r-list))))
324 (r-cc-list (nth 1 (cdr (car r-list))))
325 (r-ng-list (nth 2 (cdr (car r-list)))))
326 (when (and (member "Followup-To" r-ng-list)
327 (string= (std11-field-body "Followup-To") "poster"))
328 (setq r-to-list (cons "From" r-to-list))
329 (setq r-ng-list (delete "Followup-To"
330 (copy-sequence r-ng-list))))
331 (if (and r-to-list (symbolp r-to-list))
332 (setq to (wl-concat-list (funcall r-to-list) ","))
333 (setq to (wl-concat-list (cons to
334 (elmo-multiple-fields-body-list
337 (if (and r-cc-list (symbolp r-cc-list))
338 (setq cc (wl-concat-list (funcall r-to-list) ","))
339 (setq cc (wl-concat-list (cons cc
340 (elmo-multiple-fields-body-list
343 (if (and r-ng-list (symbolp r-ng-list))
344 (setq newsgroups (wl-concat-list (funcall r-ng-list) ","))
345 (setq newsgroups (wl-concat-list (cons newsgroups
350 (setq r-list (cdr r-list)))
351 (error "No match field: check your `%s'"
352 (symbol-name (wl-draft-reply-list-symbol with-arg))))
353 (setq subject (std11-field-body "Subject"))
354 (setq to (wl-parse-addresses to)
355 cc (wl-parse-addresses cc))
356 (with-temp-buffer ; to keep raw buffer unibyte.
357 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
358 (setq decoder (mime-find-field-decoder 'Subject 'plain))
359 (setq subject (if (and subject decoder)
360 (funcall decoder subject) subject))
364 (setq decoder (mime-find-field-decoder 'To 'plain))
365 (cons (nth 1 (std11-extract-address-components addr))
366 (if decoder (funcall decoder addr) addr)))
371 (setq decoder (mime-find-field-decoder 'Cc 'plain))
372 (cons (nth 1 (std11-extract-address-components addr))
373 (if decoder (funcall decoder addr) addr)))
375 (and wl-reply-subject-prefix
376 (setq subject (concat wl-reply-subject-prefix
377 (wl-draft-strip-subject-re
379 (setq in-reply-to (std11-field-body "Message-Id"))
380 (setq references (nconc
381 (std11-field-bodies '("References" "In-Reply-To"))
383 (setq to (delq nil (mapcar 'car to-alist)))
384 (setq cc (delq nil (mapcar 'car cc-alist)))
385 ;; if subscribed mailing list is contained in cc or to
386 ;; and myself is contained in cc,
387 ;; delete myself from cc.
388 (setq cc (wl-draft-delete-myself-from-cc to cc))
389 (when wl-insert-mail-followup-to
390 (setq mail-followup-to
391 (wl-draft-make-mail-followup-to (append to cc)))
392 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
393 (with-temp-buffer ; to keep raw buffer unibyte.
394 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
395 (setq newsgroups (wl-parse newsgroups
396 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
397 newsgroups (wl-delete-duplicates newsgroups)
402 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
403 (if decoder (funcall decoder grp) grp))
405 (setq to (wl-delete-duplicates to nil t))
406 (setq cc (wl-delete-duplicates
407 (append (wl-delete-duplicates cc nil t)
408 to (copy-sequence to))
410 (and to (setq to (mapconcat
412 (if wl-draft-reply-use-address-with-full-name
413 (or (cdr (assoc addr to-alist)) addr)
416 (and cc (setq cc (mapconcat
418 (if wl-draft-reply-use-address-with-full-name
419 (or (cdr (assoc addr cc-alist)) addr)
422 (and mail-followup-to
423 (setq mail-followup-to
426 (if wl-draft-reply-use-address-with-full-name
427 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
429 mail-followup-to ",\n\t")))
430 (and (null to) (setq to cc cc nil))
431 (setq references (delq nil references)
432 references (mapconcat 'identity references " ")
433 references (wl-draft-parse-msg-id-list-string references)
434 references (wl-delete-duplicates references)
435 references (if references
436 (mapconcat 'identity references "\n\t")))
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-add-references ()
449 (wl-draft-add-in-reply-to "References"))
451 (defun wl-draft-add-in-reply-to (&optional alt-field)
452 (let* ((mes-id (save-excursion
453 (set-buffer mail-reply-buffer)
454 (std11-field-body "message-id")))
455 (field (or alt-field "In-Reply-To"))
456 (ref (std11-field-body field))
457 (ref-list nil) (st nil))
458 (when (and mes-id ref)
459 (while (string-match "<[^>]+>" ref st)
461 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
464 (member mes-id ref-list))
468 (when (mail-position-on-field field)
470 (while (looking-at "^[ \t]")
472 (setq mes-id (concat "\t" mes-id "\n")))
476 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
477 &optional ignored-fields)
480 (narrow-to-region (point)(point))
482 (with-current-buffer mail-reply-buffer
484 (decode-mime-charset-region (point-min) (point-max)
486 (buffer-substring-no-properties
487 (point-min) (point-max))))
489 (goto-char (point-min))
490 (wl-draft-delete-fields ignored-fields))
491 (goto-char (point-max))
492 (push-mark (point) nil t)
493 (goto-char (point-min)))
495 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
496 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
497 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
498 (run-hooks 'wl-draft-cited-hook)
499 (when (if wl-draft-add-references
500 (wl-draft-add-references)
501 (if wl-draft-add-in-reply-to
502 (wl-draft-add-in-reply-to)))
503 (wl-highlight-headers 'for-draft)) ; highlight when added References:
504 (when wl-highlight-body-too
505 (wl-highlight-body-region beg (point-max)))))
507 (defun wl-message-news-p ()
508 "If exist valid Newsgroups field, return non-nil."
509 (std11-field-body "Newsgroups"))
511 (defun wl-message-field-exists-p (field)
512 "If FIELD exist and FIELD value is not empty, return non-nil."
513 (let ((value (std11-field-body field)))
515 (not (string= value "")))))
517 (defun wl-message-mail-p ()
518 "If exist To, Cc or Bcc field, return non-nil."
519 (or (wl-message-field-exists-p "To")
520 (wl-message-field-exists-p "Resent-to")
521 (wl-message-field-exists-p "Cc")
522 (wl-message-field-exists-p "Bcc")
523 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
524 ;;; This may be needed..
525 ;;; (wl-message-field-exists-p "Fcc")
528 (defun wl-draft-edit-string (string)
529 (let ((cur-buf (current-buffer))
530 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
531 to subject in-reply-to cc references newsgroups mail-followup-to
532 content-type content-transfer-encoding from
537 (setq to (std11-field-body "To"))
540 (decode-mime-charset-string
543 (setq subject (std11-field-body "Subject"))
544 (setq subject (and subject
546 (decode-mime-charset-string
549 (setq from (std11-field-body "From")
552 (decode-mime-charset-string
555 (setq in-reply-to (std11-field-body "In-Reply-To"))
556 (setq cc (std11-field-body "Cc"))
559 (decode-mime-charset-string
562 (setq references (std11-field-body "References"))
563 (setq newsgroups (std11-field-body "Newsgroups"))
564 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
565 (setq content-type (std11-field-body "Content-Type"))
566 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
567 (goto-char (point-min))
568 (or (re-search-forward "\n\n" nil t)
569 (search-forward (concat mail-header-separator "\n") nil t))
575 (nth 1 (std11-extract-address-components from))
576 wl-user-mail-address-list)
580 (cons 'Subject subject)
581 (cons 'Newsgroups newsgroups)
582 (cons 'Mail-Followup-To mail-followup-to)
583 (cons 'In-Reply-To in-reply-to)
584 (cons 'References references))
585 content-type content-transfer-encoding
586 (buffer-substring (point) (point-max))
588 (and to (mail-position-on-field "To"))
589 (delete-other-windows)
590 (kill-buffer tmp-buf)))
591 (run-hooks 'wl-draft-reedit-hook))
593 (defun wl-draft-insert-current-message (dummy)
595 (let (original-buffer
597 mail-citation-hook mail-yank-hooks
598 wl-draft-add-references wl-draft-add-in-reply-to
599 wl-draft-cite-function)
600 (with-current-buffer wl-draft-buffer-cur-summary-buffer
601 (with-current-buffer wl-message-buffer
602 (setq original-buffer (wl-message-get-original-buffer))
604 (with-current-buffer original-buffer
606 (error "No current message"))))
607 (setq mail-reply-buffer original-buffer)
608 (wl-draft-yank-from-mail-reply-buffer
610 wl-ignored-forwarded-headers)))
612 (defun wl-draft-insert-get-message (dummy)
613 (let ((fld (completing-read
615 (if (memq 'read-folder wl-use-folder-petname)
616 (wl-folder-get-entity-with-petname)
617 wl-folder-entity-hashtb)
618 nil nil wl-default-spec
619 'wl-read-folder-hist))
620 (number (call-interactively
621 (function (lambda (num)
622 (interactive "nNumber: ")
624 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
625 mail-citation-hook mail-yank-hooks
626 wl-draft-cite-function)
629 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
632 (elmo-make-fetch-strategy 'entire)
633 nil mail-reply-buffer)
634 (wl-draft-yank-from-mail-reply-buffer nil))
635 (kill-buffer mail-reply-buffer))))
638 ;; default body citation func
640 (defun wl-default-draft-cite ()
641 (let ((mail-yank-ignored-headers "[^:]+:")
642 (mail-yank-prefix "> ")
643 (summary-buf wl-current-summary-buffer)
644 (message-buf (get-buffer (wl-current-message-buffer)))
645 from date cite-title num entity)
647 (buffer-live-p summary-buf)
649 (buffer-live-p message-buf))
651 (with-current-buffer summary-buf
652 (let ((elmo-mime-charset wl-summary-buffer-mime-charset))
653 (setq num (save-excursion
654 (set-buffer message-buf)
655 wl-message-buffer-cur-number))
656 (setq entity (elmo-msgdb-overview-get-entity
657 num (wl-summary-buffer-msgdb)))
658 (setq date (elmo-msgdb-overview-entity-get-date entity))
659 (setq from (elmo-msgdb-overview-entity-get-from entity))))
660 (setq cite-title (format "At %s,\n%s wrote:"
661 (or date "some time ago")
662 (if wl-default-draft-cite-decorate-author
663 (funcall wl-summary-from-function
667 (insert cite-title "\n"))
668 (mail-indent-citation)))
670 (defvar wl-draft-buffer nil "Draft buffer to yank content")
671 (defun wl-draft-yank-to-draft-buffer (buffer)
672 "Yank BUFFER content to `wl-draft-buffer'."
673 (set-buffer wl-draft-buffer)
674 (let ((mail-reply-buffer buffer))
675 (wl-draft-yank-from-mail-reply-buffer nil)
676 (kill-buffer buffer)))
678 (defun wl-draft-yank-original (&optional arg)
679 "Yank original message."
682 (let (buf mail-reply-buffer)
685 (setq buf (current-buffer)))
686 (setq mail-reply-buffer buf)
687 (wl-draft-yank-from-mail-reply-buffer nil))
688 (wl-draft-yank-current-message-entity)))
690 (defun wl-draft-hide (editing-buffer)
691 "Hide the editing draft buffer if possible."
692 (when (and editing-buffer
693 (buffer-live-p editing-buffer))
694 (set-buffer editing-buffer)
695 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
696 fld-buf sum-win fld-win)
697 (if (and wl-draft-use-frame
698 (> (length (visible-frame-list)) 1))
704 ;; stay folder window if required
705 (when wl-stay-folder-window
706 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
707 (if (setq fld-win (get-buffer-window fld-buf))
708 (select-window fld-win)
709 (if wl-draft-resume-folder-window ;; resume folder window
710 (switch-to-buffer fld-buf)))))
711 (if (buffer-live-p sum-buf)
712 (if (setq sum-win (get-buffer-window sum-buf t))
713 ;; if Summary is on the frame, select it.
714 (select-window sum-win)
715 ;; if summary is not on the frame, switch to it.
716 (if (and wl-stay-folder-window
717 (or wl-draft-resume-folder-window fld-win))
718 (wl-folder-select-buffer sum-buf)
719 (switch-to-buffer sum-buf)))))))
721 (defun wl-draft-delete (editing-buffer)
722 "kill the editing draft buffer and delete the file corresponds to it."
725 (set-buffer editing-buffer)
726 (if wl-draft-buffer-file-name
728 (if (file-exists-p wl-draft-buffer-file-name)
729 (delete-file wl-draft-buffer-file-name))
730 (let ((msg (and wl-draft-buffer-file-name
731 (string-match "[0-9]+$" wl-draft-buffer-file-name)
733 (match-string 0 wl-draft-buffer-file-name)))))
734 (wl-draft-config-info-operation msg 'delete))))
735 (set-buffer-modified-p nil) ; force kill
736 (kill-buffer editing-buffer))))
738 (defun wl-draft-kill (&optional force-kill)
739 "Kill current draft buffer and quit editing."
742 (when (and (or (eq major-mode 'wl-draft-mode)
743 (eq major-mode 'mail-mode))
745 (y-or-n-p "Kill Current Draft? ")))
746 (let ((cur-buf (current-buffer)))
747 (wl-draft-hide cur-buf)
748 (wl-draft-delete cur-buf)))
751 (defun wl-draft-fcc ()
752 "Add a new Fcc field, with file name completion."
754 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
755 (mail-position-on-field "to"))
758 ;; Imported from message.el.
759 (defun wl-draft-elide-region (b e)
760 "Elide the text in the region.
761 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
765 (insert wl-draft-elide-ellipsis))
767 ;; Imported from message.el.
768 (defun wl-draft-beginning-of-line (&optional n)
769 "Move point to beginning of header value or to beginning of line."
771 (let ((zrs 'zmacs-region-stays))
772 (when (and (interactive-p) (boundp zrs))
774 (if (wl-draft-point-in-header-p)
775 (let* ((here (point))
776 (bol (progn (beginning-of-line n) (point)))
777 (eol (line-end-position))
778 (eoh (and (looking-at "[^ \t]")
779 (re-search-forward ": *" eol t))))
780 (if (and eoh (or (> here eoh) (= here bol)))
783 (beginning-of-line n)))
785 (defun wl-draft-point-in-header-p ()
786 "Return t if point is in the header."
789 (goto-char (point-min))
790 (not (re-search-forward
791 (concat "^" (regexp-quote mail-header-separator) "\n")
794 ;; function for wl-sent-message-via
796 (defmacro wl-draft-sent-message-p (type)
797 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
799 (defmacro wl-draft-set-sent-message (type result &optional server-port)
800 (` (let ((element (assq (, type) wl-sent-message-via)))
802 (unless (eq (nth 1 element) (, result))
803 (setcdr element (list (, result) (, server-port)))
804 (setq wl-sent-message-modified t))
805 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
806 (setq wl-sent-message-modified t)))))
808 (defun wl-draft-sent-message-results ()
809 (let ((results wl-sent-message-via)
810 unplugged-via sent-via)
812 (cond ((eq (nth 1 (car results)) 'unplugged)
813 (push (caar results) unplugged-via))
814 ((eq (nth 1 (car results)) 'sent)
815 (push (caar results) sent-via)))
816 (setq results (cdr results)))
817 (list unplugged-via sent-via)))
819 (defun wl-draft-write-sendlog (status proto server to id)
820 "Write send log file, if `wl-draft-sendlog' is non-nil."
821 (when wl-draft-sendlog
823 (let* ((filename (expand-file-name wl-draft-sendlog-filename
824 elmo-msgdb-directory))
825 (filesize (nth 7 (file-attributes filename)))
826 (server (if server (concat " server=" server) ""))
828 ((memq proto '(fcc queue))
829 (format " folder=\"%s\"" to))
831 (format " ng=<%s>" to))
836 (mapcar '(lambda(x) (format "<%s>" x)) to)
839 (id (if id (concat " id=" id) ""))
840 (time (format-time-string "%Y/%m/%d %T")))
841 (insert (format "%s proto=%s stat=%s%s%s%s\n"
842 time proto status server to id))
843 (if (and wl-draft-sendlog-max-size filesize
844 (> filesize wl-draft-sendlog-max-size))
845 (rename-file filename (concat filename ".old") t))
846 (if (file-writable-p filename)
847 (write-region-as-binary (point-min) (point-max)
849 (message "%s is not writable." filename))))))
851 (defun wl-draft-get-header-delimiter (&optional delete)
852 ;; If DELETE is non-nil, replace the header delimiter with a blank line
854 (goto-char (point-min))
855 (when (re-search-forward
856 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
860 (setq delimline (point-marker)))
863 (defun wl-draft-send-mail-with-qmail ()
864 "Pass the prepared message buffer to qmail-inject.
865 Refer to the documentation for the variable `send-mail-function'
866 to find out how to use this."
867 (if (and wl-draft-qmail-send-plugged
868 (not (elmo-plugged-p)))
869 (wl-draft-set-sent-message 'mail 'unplugged)
871 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
872 (let ((id (std11-field-body "Message-ID"))
873 (to (std11-field-body "To")))
877 'call-process-region 1 (point-max) wl-qmail-inject-program
879 wl-qmail-inject-args))
880 ;; qmail-inject doesn't say anything on it's stdout/stderr,
881 ;; we have to look at the retval instead
883 (wl-draft-set-sent-message 'mail 'sent)
884 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
885 (1 (error "`qmail-inject' reported permanent failure"))
886 (111 (error "`qmail-inject' reported transient failure"))
887 ;; should never happen
888 (t (error "`qmail-inject' reported unknown failure"))))))
890 (defun wl-draft-parse-msg-id-list-string (string)
891 "Get msg-id list from STRING."
893 (dolist (parsed-id (std11-parse-msg-ids-string string))
894 (when (eq (car parsed-id) 'msg-id)
895 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
897 (nreverse msg-id-list)))
899 (defun wl-draft-eword-encode-address-list (string &optional column)
900 "Encode header field STRING as list of address, and return the result.
901 Cause an error when STRING contains invalid address.
902 Optional argument COLUMN is start-position of the field."
903 (car (eword-encode-rword-list
904 (or column eword-encode-default-start-column)
905 (eword-encode-addresses-to-rword-list
906 (wl-draft-std11-parse-addresses (std11-lexical-analyze string))))))
908 (defun wl-draft-std11-parse-addresses (lal)
909 (let ((ret (std11-parse-address lal)))
910 (when (and (not (and (eq (length lal) 1)
911 (eq (car (car lal)) 'spaces)))
913 (error "Error while parsing address"))
915 (let ((dest (list (car ret))))
917 (while (and (setq ret (std11-parse-ascii-token lal))
918 (string-equal (cdr (assq 'specials (car ret))) ",")
919 (setq ret (std11-parse-address (cdr ret)))
921 (setq dest (cons (car ret) dest))
922 (setq lal (cdr ret)))
923 (while (eq 'spaces (car (car lal)))
924 (setq lal (cdr lal)))
925 (if lal (error "Error while parsing address"))
928 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
929 "Get mailbox list of FIELD from current buffer.
930 The buffer is expected to be narrowed to just the headers of the message.
931 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
932 from current buffer."
934 (let ((case-fold-search t)
935 (inhibit-read-only t)
937 mailbox-list beg seq has-group-list)
938 (goto-char (point-min))
939 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
942 (re-search-forward "^[^ \t]" nil 'move)
944 (skip-chars-backward "\n")
945 (setq seq (std11-lexical-analyze
946 (buffer-substring-no-properties beg (point))))
947 (setq addresses (wl-draft-std11-parse-addresses seq))
949 (cond ((eq (car (car addresses)) 'group)
950 (setq has-group-list t)
954 'std11-address-string
955 (nth 2 (car addresses))))))
956 ((eq (car (car addresses)) 'mailbox)
957 (setq address (nth 1 (car addresses)))
961 (std11-addr-to-string
962 (if (eq (car address) 'phrase-route-addr)
965 (setq addresses (cdr addresses)))
966 (when (and remove-group-list has-group-list)
967 (delete-region beg (point))
968 (insert (wl-address-string-without-group-list-contents seq))))
971 (defun wl-draft-deduce-address-list (buffer header-start header-end)
972 "Get address list suitable for smtp RCPT TO:<address>.
973 Group list content is removed if `wl-draft-remove-group-list-contents' is
975 (let ((fields (if (and wl-draft-doing-mime-bcc
976 wl-draft-disable-bcc-for-mime-bcc)
979 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
984 (narrow-to-region header-start header-end)
985 (goto-char (point-min))
987 (if (re-search-forward "^resent-to[\t ]*:" nil t)
988 (setq fields resent-fields)))
992 (wl-draft-parse-mailbox-list
994 wl-draft-remove-group-list-contents)))
995 (setq fields (cdr fields)))
1001 (defun wl-draft-send-mail-with-smtp ()
1002 "Send the prepared message buffer with SMTP."
1004 (let* ((errbuf (if mail-interactive
1005 (generate-new-buffer " smtp errors")
1007 (case-fold-search t)
1008 (default-case-fold-search t)
1009 (sender (or wl-envelope-from
1010 (wl-address-header-extract-address wl-from)))
1011 (delimline (save-excursion
1012 (goto-char (point-min))
1014 (concat "^" (regexp-quote mail-header-separator)
1018 (or wl-smtp-posting-server smtp-server "localhost"))
1019 (smtp-service (or wl-smtp-posting-port smtp-service))
1020 (smtp-local-domain (or smtp-local-domain wl-local-domain))
1021 (id (std11-field-body "message-id"))
1023 (if (not (elmo-plugged-p smtp-server smtp-service))
1024 (wl-draft-set-sent-message 'mail 'unplugged
1025 (cons smtp-server smtp-service))
1028 ;; Instead of `smtp-deduce-address-list'.
1029 (setq recipients (wl-draft-deduce-address-list
1030 (current-buffer) (point-min) delimline))
1031 (unless recipients (error "No recipients"))
1032 ;; Insert an extra newline if we need it to work around
1033 ;; Sun's bug that swallows newlines.
1034 (goto-char (1+ delimline))
1035 (if (eval mail-mailer-swallows-blank-line)
1037 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1038 (if mail-interactive
1042 (wl-draft-delete-field "bcc" delimline)
1043 (wl-draft-delete-field "resent-bcc" delimline)
1044 (let (process-connection-type)
1047 (wl-smtp-extension-bind
1049 (smtp-send-buffer sender recipients (current-buffer))
1051 (wl-draft-write-sendlog 'failed 'smtp smtp-server
1053 (if (and (eq (car err) 'smtp-response-error)
1054 (/= (nth 1 err) 334))
1056 (wl-smtp-password-key
1058 (car smtp-sasl-mechanisms)
1060 (signal (car err) (cdr err)))))
1061 (wl-draft-set-sent-message 'mail 'sent)
1062 (wl-draft-write-sendlog
1063 'ok 'smtp smtp-server recipients id)))))
1064 (if (bufferp errbuf)
1065 (kill-buffer errbuf))))))
1067 (defun wl-draft-send-mail-with-pop-before-smtp ()
1068 "Send the prepared message buffer with POP-before-SMTP."
1069 (require 'elmo-pop3)
1073 :user (or wl-pop-before-smtp-user
1074 elmo-pop3-default-user)
1075 :server (or wl-pop-before-smtp-server
1076 elmo-pop3-default-server)
1077 :port (or wl-pop-before-smtp-port
1078 elmo-pop3-default-port)
1079 :auth (or wl-pop-before-smtp-authenticate-type
1080 elmo-pop3-default-authenticate-type)
1081 :stream-type (or wl-pop-before-smtp-stream-type
1082 elmo-pop3-default-stream-type))))
1083 (condition-case error
1085 (elmo-pop3-get-session session)
1086 (when session (elmo-network-close-session session)))
1088 (elmo-network-close-session session)
1089 (unless (string= (nth 1 error) "Unplugged")
1090 (signal (car error)(cdr error))))))
1091 (wl-draft-send-mail-with-smtp))
1093 (defun wl-draft-insert-required-fields (&optional force-msgid)
1094 "Insert Message-ID, Date, and From field.
1095 If FORCE-MSGID, insert message-id regardless of `wl-insert-message-id'."
1096 ;; Insert Message-Id field...
1097 (goto-char (point-min))
1098 (when (and (or force-msgid
1099 wl-insert-message-id)
1100 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1101 (insert (concat "Message-ID: "
1102 (funcall wl-message-id-function)
1104 ;; Insert date field.
1105 (goto-char (point-min))
1106 (or (re-search-forward "^Date[ \t]*:" nil t)
1107 (wl-draft-insert-date-field))
1108 ;; Insert from field.
1109 (goto-char (point-min))
1110 (or (re-search-forward "^From[ \t]*:" nil t)
1111 (wl-draft-insert-from-field)))
1113 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1114 "Send the message in the current buffer."
1116 (std11-narrow-to-header mail-header-separator)
1117 (wl-draft-insert-required-fields)
1118 ;; Delete null fields.
1119 (goto-char (point-min))
1120 (while (re-search-forward "^[^ \t\n:]+:[ \t]*\n" nil t)
1122 ;; ignore any blank lines in the header
1123 (while (re-search-forward "\n\n\n*" nil t)
1124 (replace-match "\n")))
1125 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1126 (wl-draft-dispatch-message)
1127 (when kill-when-done
1128 ;; hide editing-buffer.
1129 (wl-draft-hide editing-buffer)
1130 ;; delete editing-buffer and its file.
1131 (wl-draft-delete editing-buffer)))
1133 (defun wl-draft-dispatch-message (&optional mes-string)
1134 "Send the message in the current buffer. Not modified the header fields."
1135 (let (delimline mime-bcc)
1136 (if (and wl-draft-verbose-send mes-string)
1137 (message mes-string))
1139 (setq delimline (wl-draft-get-header-delimiter t))
1140 (unless wl-draft-fcc-list
1141 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1143 (setq wl-sent-message-modified nil)
1146 (if (and (wl-message-mail-p)
1147 (not (wl-draft-sent-message-p 'mail)))
1148 (if (or (not (or wl-draft-force-queuing
1149 wl-draft-force-queuing-mail))
1150 (memq 'mail wl-sent-message-queued))
1152 (setq mime-bcc (wl-draft-mime-bcc-field))
1153 (funcall wl-draft-send-mail-function)
1154 (when (not (zerop (length mime-bcc)))
1155 (wl-draft-do-mime-bcc mime-bcc)))
1156 (push 'mail wl-sent-message-queued)
1157 (wl-draft-set-sent-message 'mail 'unplugged)))
1158 (if (and (wl-message-news-p)
1159 (not (wl-draft-sent-message-p 'news))
1160 (not (wl-message-field-exists-p "Resent-to")))
1161 (if (or (not (or wl-draft-force-queuing
1162 wl-draft-force-queuing-news))
1163 (memq 'news wl-sent-message-queued))
1164 (funcall wl-draft-send-news-function)
1165 (push 'news wl-sent-message-queued)
1166 (wl-draft-set-sent-message 'news 'unplugged))))
1167 (let* ((status (wl-draft-sent-message-results))
1168 (unplugged-via (car status))
1169 (sent-via (nth 1 status)))
1170 ;; If one sent, process fcc folder.
1171 (if (and sent-via wl-draft-fcc-list)
1173 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1175 (setq wl-draft-fcc-list nil)))
1176 (if wl-draft-use-cache
1177 (let ((id (std11-field-body "Message-ID"))
1178 (elmo-enable-disconnected-operation t))
1179 (elmo-file-cache-save (elmo-file-cache-get-path id)
1181 ;; If one unplugged, append queue.
1182 (when (and unplugged-via
1183 wl-sent-message-modified)
1184 (if wl-draft-enable-queuing
1186 (wl-draft-queue-append wl-sent-message-via)
1187 (setq wl-sent-message-modified 'requeue))
1188 (error "Unplugged")))
1189 (when wl-draft-verbose-send
1190 (if (and unplugged-via sent-via);; combined message
1192 (setq wl-draft-verbose-msg
1193 (format "Sending%s and Queuing%s..."
1194 sent-via unplugged-via))
1195 (message (concat wl-draft-verbose-msg "done")))
1197 (message (concat mes-string
1198 (if sent-via "done" "failed")))))))))
1199 (not wl-sent-message-modified)) ;; return value
1201 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1202 "Force send current buffer as raw message."
1205 (let (wl-interactive-send
1206 ;;; wl-draft-verbose-send
1207 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1208 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1211 (wl-draft-send kill-when-done mes-string))))
1213 (defun wl-draft-clone-local-variables ()
1214 (let ((locals (buffer-local-variables))
1217 (when (and (consp (car locals))
1219 (string-match wl-draft-clone-local-variable-regexp
1220 (symbol-name (car (car locals)))))
1221 (wl-append result (list (car (car locals)))))
1222 (setq locals (cdr locals)))
1225 (defun wl-draft-send (&optional kill-when-done mes-string)
1226 "Send current draft message.
1227 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1229 ;; Don't call this explicitly.
1230 ;; Added to 'wl-draft-send-hook (by teranisi)
1231 ;; (wl-draft-config-exec)
1232 (run-hooks 'wl-draft-send-hook)
1233 (when (or (not wl-interactive-send)
1234 (y-or-n-p "Do you really want to send current draft? "))
1235 (let ((send-mail-function 'wl-draft-raw-send)
1236 (editing-buffer (current-buffer))
1237 (sending-buffer (wl-draft-generate-clone-buffer
1238 " *wl-draft-sending-buffer*"
1239 (append wl-draft-config-variables
1240 (wl-draft-clone-local-variables))))
1241 (wl-draft-verbose-msg nil)
1244 (save-excursion (set-buffer sending-buffer)
1245 (if (and (not (wl-message-mail-p))
1246 (not (wl-message-news-p)))
1247 (error "No recipient is specified"))
1248 (expand-abbrev) ; for mail-abbrevs
1249 (let ((mime-header-encode-method-alist
1251 '((wl-draft-eword-encode-address-list
1252 . (To Cc Bcc Resent-To Resent-Cc Bcc Resent-Bcc)))
1253 (if (boundp 'mime-header-encode-method-alist)
1254 (symbol-value 'mime-header-encode-method-alist)))))
1255 (run-hooks 'mail-send-hook) ; translate buffer
1258 (if wl-draft-verbose-send
1259 (message (or mes-string "Sending...")))
1260 (funcall wl-draft-send-function editing-buffer kill-when-done)
1261 ;; Now perform actions on successful sending.
1262 (while mail-send-actions
1264 (apply (car (car mail-send-actions))
1265 (cdr (car mail-send-actions)))
1267 (setq mail-send-actions (cdr mail-send-actions)))
1268 (if wl-draft-verbose-send
1269 (message (concat (or wl-draft-verbose-msg
1270 mes-string "Sending...")
1272 ;; kill sending buffer, anyway.
1273 (and (buffer-live-p sending-buffer)
1274 (kill-buffer sending-buffer))))))
1276 (defun wl-draft-mime-bcc-field ()
1277 "Return the MIME-Bcc field body. The field is deleted."
1278 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1279 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1281 (defun wl-draft-do-mime-bcc (field-body)
1282 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1283 (let ((orig-from (mime-decode-field-body (std11-field-body "from")
1285 (orig-subj (mime-decode-field-body (or (std11-field-body "subject")
1288 (recipients (wl-parse-addresses field-body))
1289 (draft-buffer (current-buffer))
1291 (save-window-excursion
1292 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1293 (not (zerop (length field-body))))
1294 (let ((wl-draft-doing-mime-bcc t))
1295 (dolist (recipient recipients)
1296 (wl-draft-create-buffer)
1297 (wl-draft-create-contents
1298 (append `((From . ,orig-from)
1300 (Subject . ,(concat "A blind carbon copy ("
1303 (wl-draft-default-headers)))
1304 (wl-draft-insert-mail-header-separator)
1305 (wl-draft-prepare-edit)
1306 (goto-char (point-max))
1307 (insert (or wl-draft-mime-bcc-body
1308 "This is a blind carbon copy.")
1310 (mime-edit-insert-tag "message" "rfc822")
1311 (insert-buffer draft-buffer)
1312 (let (wl-interactive-send)
1313 (wl-draft-send 'kill-when-done))))))))
1315 ;; Derived from `message-save-drafts' in T-gnus.
1316 (defun wl-draft-save ()
1317 "Save current draft."
1319 (if (buffer-modified-p)
1321 (message "Saving %s..." wl-draft-buffer-file-name)
1322 (let ((msg (buffer-substring-no-properties (point-min) (point-max))))
1323 (with-temp-file wl-draft-buffer-file-name
1325 ;; If no header separator, insert it.
1327 (goto-char (point-min))
1328 (unless (re-search-forward
1329 (concat "^" (regexp-quote mail-header-separator) "$")
1331 (goto-char (point-min))
1332 (if (re-search-forward "\n\n" nil t)
1333 (replace-match (concat "\n" mail-header-separator "\n"))
1334 (goto-char (point-max))
1335 (insert (if (eq (char-before) ?\n) "" "\n")
1336 mail-header-separator "\n"))))
1337 (let ((mime-header-encode-method-alist
1338 '((eword-encode-unstructured-field-body))))
1339 (mime-edit-translate-buffer))
1340 (wl-draft-get-header-delimiter t)))
1341 (set-buffer-modified-p nil)
1342 (wl-draft-config-info-operation
1343 (and (string-match "[0-9]+$" wl-draft-buffer-file-name)
1345 (match-string 0 wl-draft-buffer-file-name)))
1347 (message "Saving %s...done" wl-draft-buffer-file-name))
1348 (message "(No changes need to be saved)")))
1350 (defun wl-draft-mimic-kill-buffer ()
1351 "Kill the current (draft) buffer with query."
1353 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1356 (if (or (not bufname)
1357 (string-equal bufname "")
1358 (string-equal bufname (buffer-name)))
1359 (let ((bufname (current-buffer)))
1360 (when (or (not (buffer-modified-p))
1362 (format "Buffer %s modified; kill anyway? " bufname)))
1363 (set-buffer-modified-p nil)
1364 (wl-draft-hide bufname)
1365 (kill-buffer bufname)))
1366 (kill-buffer bufname))))
1368 (defun wl-draft-save-and-exit ()
1369 "Save current draft and exit current draft mode."
1372 (let ((editing-buffer (current-buffer)))
1373 (wl-draft-hide editing-buffer)
1374 (kill-buffer editing-buffer)))
1376 (defun wl-draft-send-and-exit ()
1377 "Send current draft message and kill it."
1381 (defun wl-draft-send-from-toolbar ()
1383 (let ((wl-interactive-send t))
1384 (wl-draft-send-and-exit)))
1386 (defun wl-draft-delete-field (field &optional delimline replace)
1387 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1389 (defun wl-draft-delete-fields (field &optional delimline replace)
1392 (goto-char (point-min))
1393 (if (search-forward "\n\n" nil t)
1394 (setq delimline (point))
1395 (setq delimline (point-max))))
1396 (narrow-to-region (point-min) delimline)
1397 (goto-char (point-min))
1398 (let ((regexp (concat "^" field ":"))
1399 (case-fold-search t))
1401 (if (looking-at regexp)
1407 (if (re-search-forward "^[^ \t]" nil t)
1408 (goto-char (match-beginning 0))
1411 (insert (concat field ": " replace "\n"))))
1413 (if (re-search-forward "^[^ \t]" nil t)
1414 (goto-char (match-beginning 0))
1417 (defun wl-draft-get-fcc-list (header-end)
1418 (if (and wl-draft-doing-mime-bcc
1419 wl-draft-disable-fcc-for-mime-bcc)
1421 (wl-draft-delete-field "fcc")
1424 (case-fold-search t))
1425 (or (markerp header-end) (error "HEADER-END must be a marker"))
1427 (goto-char (point-min))
1428 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1433 (buffer-substring-no-properties
1437 (skip-chars-backward " \t")
1440 (dolist (folder fcc-list)
1441 (wl-folder-confirm-existence
1442 (wl-folder-get-elmo-folder (eword-decode-string folder)))))
1443 (delete-region (match-beginning 0)
1444 (progn (forward-line 1) (point)))))
1447 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1448 (let ((send-mail-buffer (current-buffer))
1449 (tembuf (generate-new-buffer " fcc output"))
1450 (case-fold-search t)
1452 (or (markerp header-end) (error "HEADER-END must be a marker"))
1455 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1458 ;; insert just the headers to avoid moving the gap more than
1459 ;; necessary (the message body could be arbitrarily huge.)
1460 (insert-buffer-substring send-mail-buffer 1 header-end)
1461 (wl-draft-insert-required-fields t)
1462 (goto-char (point-max))
1463 (insert-buffer-substring send-mail-buffer header-end)
1464 (let ((id (std11-field-body "Message-ID"))
1465 (elmo-enable-disconnected-operation t)
1468 (unless (or cache-saved
1469 (elmo-folder-plugged-p
1470 (wl-folder-get-elmo-folder (car fcc-list))))
1471 (elmo-file-cache-save id nil) ;; for disconnected operation
1472 (setq cache-saved t))
1473 (if (elmo-folder-append-buffer
1474 (wl-folder-get-elmo-folder
1475 (eword-decode-string (car fcc-list)))
1476 (not wl-fcc-force-as-read))
1477 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1478 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1479 (setq fcc-list (cdr fcc-list)))))
1480 (kill-buffer tembuf)))
1482 (defun wl-draft-on-field-p ()
1485 (goto-char (point-min))
1486 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1493 (if (or (looking-at ".*,[ \t]?$")
1494 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1496 (let ((pos (point)))
1499 (if (looking-at "^[ \t]")
1501 (if (re-search-forward ":" pos t) nil t)))))))
1505 (defun wl-draft (&optional header-alist
1506 content-type content-transfer-encoding
1509 "Write and send mail/news message with Wanderlust."
1516 (wl-plugged-init t))
1518 (wl-init)) ; returns immediately if already initialized.
1521 (let (buf-name header-alist-internal)
1523 (wl-draft-create-buffer
1525 (eq this-command 'wl-draft)
1526 (eq this-command 'wl-summary-write)
1527 (eq this-command 'wl-summary-write-current-folder)
1528 (eq this-command 'wl-folder-write-current-folder))
1531 (unless (cdr (assq 'From header-alist))
1533 (append (list (cons 'From wl-from)) header-alist)))
1534 (unless (cdr (assq 'To header-alist))
1539 (if (assq 'To header-alist)
1540 (setcdr (assq 'To header-alist) to)
1542 (append header-alist
1543 (list (cons 'To to))))))))
1544 (unless (cdr (assq 'Subject header-alist))
1545 (if (assq 'Subject header-alist)
1546 (setcdr (assq 'Subject header-alist) "")
1548 (append header-alist (list (cons 'Subject ""))))))
1549 (setq header-alist (append header-alist
1550 (wl-draft-default-headers)
1551 wl-draft-additional-header-alist
1552 (if body (list "" (cons 'Body body)))))
1553 (wl-draft-create-contents header-alist)
1555 (wl-draft-decode-body
1556 content-type content-transfer-encoding))
1557 (wl-draft-insert-mail-header-separator)
1558 (wl-draft-prepare-edit)
1560 (run-hooks 'wl-mail-setup-hook))
1561 (goto-char (point-min))
1562 (wl-user-agent-compose-internal) ;; user-agent
1565 (string= (cdr (assq 'To header-alist)) ""))
1566 (mail-position-on-field "To"))
1568 (goto-char (point-max))))
1571 (defun wl-draft-create-buffer (&optional full parent-folder)
1572 (let* ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1573 (parent-folder (or parent-folder (wl-summary-buffer-folder-name)))
1574 (summary-buf (wl-summary-get-buffer parent-folder))
1575 buf-name file-name num change-major-mode-hook)
1576 (if (not (elmo-folder-message-file-p draft-folder))
1577 (error "%s folder cannot be used for draft folder" wl-draft-folder))
1578 (setq num (elmo-max-of-list
1579 (or (elmo-folder-list-messages draft-folder) '(0))))
1580 (setq num (+ 1 num))
1581 ;; To get unused buffer name.
1582 (while (get-buffer (concat wl-draft-folder "/" (int-to-string num)))
1583 (setq num (+ 1 num)))
1584 (setq buf-name (find-file-noselect
1586 (elmo-message-file-name
1587 (wl-folder-get-elmo-folder wl-draft-folder)
1589 (if wl-draft-use-frame
1590 (switch-to-buffer-other-frame buf-name)
1591 (switch-to-buffer buf-name))
1592 (set-buffer buf-name)
1593 (if (not (string-match (regexp-quote wl-draft-folder)
1595 (rename-buffer (concat wl-draft-folder "/" (int-to-string num))))
1596 (if (or (eq wl-draft-reply-buffer-style 'full)
1598 (delete-other-windows))
1601 (make-local-variable 'truncate-partial-width-windows)
1602 (setq truncate-partial-width-windows nil)
1603 (setq truncate-lines wl-draft-truncate-lines)
1604 (setq wl-sent-message-via nil)
1605 (setq wl-sent-message-queued nil)
1606 (setq wl-draft-buffer-file-name file-name)
1607 (setq wl-draft-config-exec-flag t)
1608 (setq wl-draft-parent-folder parent-folder)
1609 (or (eq this-command 'wl-folder-write-current-folder)
1610 (setq wl-draft-buffer-cur-summary-buffer summary-buf))
1613 (defun wl-draft-create-contents (header-alist)
1614 "header-alist' sample
1615 '(function ;; funcall
1616 string ;; insert string
1617 (symbol . string) ;; insert symbol-value: string
1618 (symbol . function) ;; (funcall) and if it returns string,
1619 ;; insert symbol-value: string
1620 (symbol . nil) ;; do nothing
1624 (unless (eq major-mode 'wl-draft-mode)
1625 (error "wl-draft-create-header must be use in wl-draft-mode."))
1626 (let ((halist header-alist)
1631 ((functionp (car halist)) (funcall (car halist)))
1633 ((stringp (car halist)) (insert (car halist) "\n"))
1635 ((consp (car halist))
1636 (setq field (car (car halist)))
1637 (setq value (cdr (car halist)))
1641 ((eq field 'Body) ; body
1643 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1645 (let ((value-return (funcall value)))
1646 (when (stringp value-return)
1647 (insert (symbol-name field) ": " value-return "\n"))))
1656 (setq halist (cdr halist)))))
1658 (defun wl-draft-prepare-edit ()
1659 (unless (eq major-mode 'wl-draft-mode)
1660 (error "wl-draft-create-header must be use in wl-draft-mode."))
1661 (let (change-major-mode-hook)
1662 (wl-draft-editor-mode)
1663 (add-hook 'local-write-file-hooks 'wl-draft-save)
1664 (wl-draft-overload-functions)
1665 (wl-highlight-headers 'for-draft)
1667 (clear-visited-file-modtime)))
1669 (defun wl-draft-decode-header ()
1671 (std11-narrow-to-header)
1672 (wl-draft-decode-message-in-buffer)
1675 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1678 (std11-field-body "content-type")))
1679 (content-transfer-encoding
1680 (or content-transfer-encoding
1681 (std11-field-body "content-transfer-encoding")))
1684 (std11-narrow-to-header)
1685 (wl-draft-delete-field "content-type")
1686 (wl-draft-delete-field "content-transfer-encoding")
1687 (goto-char (point-max))
1688 (setq delimline (point-marker))
1690 (narrow-to-region delimline (point-max))
1691 (goto-char (point-min))
1693 (insert "Content-type: " content-type "\n"))
1694 (when content-transfer-encoding
1695 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1696 (wl-draft-decode-message-in-buffer)
1697 (goto-char (point-min))
1698 (unless (re-search-forward "^$" (point-at-eol) t)
1703 ;;; subroutine for wl-draft-create-contents
1704 ;;; must be used in wl-draft-mode
1705 (defun wl-draft-check-new-line ()
1706 (if (not (= (preceding-char) ?\n))
1709 (defsubst wl-draft-trim-ccs (cc)
1715 (null (and wl-draft-delete-myself-from-bcc-fcc
1717 (mapcar 'wl-address-header-extract-address
1719 (wl-parse-addresses (std11-field-body "To"))
1720 (wl-parse-addresses (std11-field-body "Cc"))))
1721 (mapcar 'downcase wl-subscribed-mailing-list)))))
1725 (defsubst wl-draft-default-headers ()
1727 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1728 (wl-address-header-extract-address
1730 (cons 'User-Agent wl-generate-mailer-string-function)
1731 (cons 'Reply-To mail-default-reply-to)
1732 (cons 'Bcc (function
1735 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1736 (cons 'Fcc (function
1738 (wl-draft-trim-ccs wl-fcc))))
1739 (cons 'Organization wl-organization)
1740 (and wl-auto-insert-x-face
1741 (file-exists-p wl-x-face-file)
1742 'wl-draft-insert-x-face-field-here) ;; allow nil
1743 mail-default-headers
1744 ;; check \n at th end of line for `mail-default-headers'
1745 'wl-draft-check-new-line
1748 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1751 (goto-char delimline)
1752 (goto-char (point-min))
1753 (if (search-forward "\n\n" nil t)
1754 (delete-backward-char 1)
1755 (goto-char (point-max))))
1756 (wl-draft-check-new-line)
1757 (put-text-property (point)
1759 (insert mail-header-separator "\n")
1761 'category 'mail-header-separator)))
1765 (defun wl-draft-elmo-nntp-send ()
1766 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1767 (elmo-nntp-default-user
1768 (or wl-nntp-posting-user elmo-nntp-default-user))
1769 (elmo-nntp-default-server
1770 (or wl-nntp-posting-server elmo-nntp-default-server))
1771 (elmo-nntp-default-port
1772 (or wl-nntp-posting-port elmo-nntp-default-port))
1773 (elmo-nntp-default-stream-type
1774 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1775 (elmo-nntp-default-function wl-nntp-posting-function)
1777 (if (setq condition (cdr (elmo-string-matched-assoc
1778 (std11-field-body "Newsgroups")
1779 wl-nntp-posting-config-alist)))
1780 (if (stringp condition)
1781 (setq elmo-nntp-default-server condition)
1782 (while (car condition)
1783 (set (intern (format "elmo-nntp-default-%s"
1784 (symbol-name (caar condition))))
1786 (setq condition (cdr condition)))))
1787 (unless elmo-nntp-default-function
1788 (error "wl-draft-nntp-send: posting-function is nil."))
1789 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1790 (wl-draft-set-sent-message 'news 'unplugged
1791 (cons elmo-nntp-default-server
1792 elmo-nntp-default-port))
1793 (funcall elmo-nntp-default-function
1794 elmo-nntp-default-server (current-buffer))
1795 (wl-draft-set-sent-message 'news 'sent)
1796 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1797 (std11-field-body "Newsgroups")
1798 (std11-field-body "Message-ID")))))
1800 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1801 "Generate clone of current buffer named NAME."
1802 (let ((editing-buffer (current-buffer)))
1804 (set-buffer (generate-new-buffer name))
1807 (wl-draft-editor-mode)
1808 (insert-buffer editing-buffer)
1810 (while local-variables
1811 (make-local-variable (car local-variables))
1812 (set (car local-variables)
1814 (set-buffer editing-buffer)
1815 (symbol-value (car local-variables))))
1816 (setq local-variables (cdr local-variables)))
1819 (defun wl-draft-reedit (number)
1820 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1822 buffer file-name change-major-mode-hook)
1823 (setq file-name (elmo-message-file-name draft-folder number))
1824 (unless (file-exists-p file-name)
1825 (error "File %s does not exist" file-name))
1826 (if (setq buffer (get-buffer
1827 (concat wl-draft-folder "/"
1828 (number-to-string number))))
1830 (if wl-draft-use-frame
1831 (switch-to-buffer-other-frame buffer)
1832 (switch-to-buffer buffer))
1833 (set-buffer buffer))
1834 (setq buffer (get-buffer-create (number-to-string number)))
1835 (if wl-draft-use-frame
1836 (switch-to-buffer-other-frame buffer)
1837 (switch-to-buffer buffer))
1839 (insert-file-contents-as-binary file-name)
1840 (let((mime-edit-again-ignored-field-regexp
1841 "^\\(Content-.*\\|Mime-Version\\):"))
1842 (wl-draft-decode-message-in-buffer))
1843 (wl-draft-insert-mail-header-separator)
1844 (if (not (string-match (regexp-quote wl-draft-folder)
1846 (rename-buffer (concat wl-draft-folder "/" (buffer-name))))
1849 (setq buffer-file-name file-name)
1850 (make-local-variable 'truncate-partial-width-windows)
1851 (setq truncate-partial-width-windows nil)
1852 (setq truncate-lines wl-draft-truncate-lines)
1853 (setq wl-sent-message-via nil)
1854 (setq wl-sent-message-queued nil)
1855 (setq wl-draft-buffer-file-name file-name)
1856 (wl-draft-config-info-operation number 'load)
1857 (goto-char (point-min))
1858 (wl-draft-overload-functions)
1859 (wl-draft-editor-mode)
1860 (add-hook 'local-write-file-hooks 'wl-draft-save)
1861 (wl-highlight-headers 'for-draft)
1862 (run-hooks 'wl-draft-reedit-hook)
1863 (goto-char (point-max))
1866 (defmacro wl-draft-body-goto-top ()
1868 (goto-char (point-min))
1869 (if (re-search-forward mail-header-separator nil t)
1871 (goto-char (point-max))))))
1873 (defmacro wl-draft-body-goto-bottom ()
1874 (` (goto-char (point-max))))
1876 (defmacro wl-draft-config-body-goto-header ()
1878 (goto-char (point-min))
1879 (if (re-search-forward mail-header-separator nil t)
1881 (goto-char (point-max))))))
1883 (defun wl-draft-config-sub-body (content)
1884 (wl-draft-body-goto-top)
1885 (delete-region (point) (point-max))
1886 (if content (insert (eval content))))
1888 (defun wl-draft-config-sub-top (content)
1889 (wl-draft-body-goto-top)
1890 (if content (insert (eval content))))
1892 (defun wl-draft-config-sub-bottom (content)
1893 (wl-draft-body-goto-bottom)
1894 (if content (insert (eval content))))
1896 (defun wl-draft-config-sub-header (content)
1897 (wl-draft-config-body-goto-header)
1898 (if content (insert (concat (eval content) "\n"))))
1900 (defun wl-draft-config-sub-header-top (content)
1901 (goto-char (point-min))
1902 (if content (insert (concat (eval content) "\n"))))
1904 (defun wl-draft-config-sub-part-top (content)
1905 (goto-char (mime-edit-content-beginning))
1906 (if content (insert (concat (eval content) "\n"))))
1908 (defun wl-draft-config-sub-part-bottom (content)
1909 (goto-char (mime-edit-content-end))
1910 (if content (insert (concat (eval content) "\n"))))
1912 (defsubst wl-draft-config-sub-file (content)
1913 (let ((coding-system-for-read wl-cs-autoconv)
1914 (file (expand-file-name (eval content))))
1915 (if (file-exists-p file)
1916 (insert-file-contents file)
1917 (error "%s: no exists file" file))))
1919 (defun wl-draft-config-sub-body-file (content)
1920 (wl-draft-body-goto-top)
1921 (delete-region (point) (point-max))
1922 (wl-draft-config-sub-file content))
1924 (defun wl-draft-config-sub-top-file (content)
1925 (wl-draft-body-goto-top)
1926 (wl-draft-config-sub-file content))
1928 (defun wl-draft-config-sub-bottom-file (content)
1929 (wl-draft-body-goto-bottom)
1930 (wl-draft-config-sub-file content))
1932 (defun wl-draft-config-sub-header-file (content)
1933 (wl-draft-config-body-goto-header)
1934 (wl-draft-config-sub-file content))
1936 (defun wl-draft-config-sub-template (content)
1937 (setq wl-draft-config-variables
1938 (wl-template-insert (eval content))))
1940 (defun wl-draft-config-sub-x-face (content)
1941 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
1942 (fboundp 'x-face-insert)) ; x-face.el is installed.
1943 (x-face-insert content)
1944 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
1946 (defsubst wl-draft-config-sub-func (field content)
1948 (if (setq func (assq field wl-draft-config-sub-func-alist))
1949 (let (wl-draft-config-variables)
1950 (funcall (cdr func) content)
1951 ;; for wl-draft-config-sub-template
1952 (cons t wl-draft-config-variables)))))
1954 (defsubst wl-draft-config-exec-sub (clist)
1955 (let (config local-variables)
1957 (setq config (car clist))
1962 (let ((field (car config))
1963 (content (cdr config))
1967 (wl-draft-replace-field field (eval content) t))
1968 ((setq ret-val (wl-draft-config-sub-func field content))
1969 (if (cdr ret-val) ;; for wl-draft-config-sub-template
1970 (wl-append local-variables (cdr ret-val))))
1971 ((boundp field) ;; variable
1972 (make-local-variable field)
1973 (set field (eval content))
1974 (wl-append local-variables (list field)))
1976 (error "%s: not variable" field)))))
1978 (error "%s: not supported type" config)))
1979 (setq clist (cdr clist)))
1982 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
1983 "Change headers in draft preparation time."
1985 (unless wl-draft-reedit
1988 (and (boundp 'wl-draft-prepared-config-alist)
1989 wl-draft-prepared-config-alist) ;; For compatible.
1990 wl-draft-config-alist)))
1992 (wl-draft-config-exec config-alist reply-buf)))))
1994 (defun wl-draft-config-exec (&optional config-alist reply-buf)
1995 "Change headers according to the value of `wl-draft-config-alist'.
1996 Automatically applied in draft sending time."
1998 (let ((case-fold-search t)
1999 (alist (or config-alist wl-draft-config-alist))
2000 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2001 wl-draft-reply-buffer)))
2002 (local-variables wl-draft-config-variables)
2004 (when (and (or (interactive-p)
2005 wl-draft-config-exec-flag)
2010 (setq key (caar alist)
2017 (set-buffer reply-buf)
2019 (std11-narrow-to-header)
2020 (goto-char (point-min))
2021 (re-search-forward (car clist) nil t))))
2022 (wl-draft-config-exec-sub (cdr clist))
2025 (when (save-restriction
2026 (std11-narrow-to-header mail-header-separator)
2027 (goto-char (point-min))
2028 (re-search-forward key nil t))
2029 (wl-append local-variables
2030 (wl-draft-config-exec-sub clist))
2033 (wl-append local-variables
2034 (wl-draft-config-exec-sub clist))
2036 (if (and found wl-draft-config-matchone)
2038 (setq alist (cdr alist))))
2040 (setq wl-draft-config-exec-flag nil))
2041 (run-hooks 'wl-draft-config-exec-hook)
2042 (put-text-property (point-min)(point-max) 'face nil)
2043 (wl-highlight-message (point-min)(point-max) t)
2044 (setq wl-draft-config-variables
2045 (elmo-uniq-list local-variables))))))
2047 (defun wl-draft-replace-field (field content &optional add)
2050 (let ((case-fold-search t)
2051 (inhibit-read-only t) ;; added by teranisi.
2053 (std11-narrow-to-header mail-header-separator)
2054 (goto-char (point-min))
2055 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2060 (re-search-forward "^[^ \t]" nil 'move)
2062 (skip-chars-backward "\n")
2063 (delete-region beg (point))
2064 (insert " " content))
2069 (re-search-forward "^[^ \t]" nil 'move)
2071 (delete-region beg (point)))
2072 (when (and add content)
2074 (goto-char (point-max))
2075 (insert (concat field ": " content "\n"))))))))
2077 (defun wl-draft-config-info-operation (msg operation)
2078 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-folder-get-elmo-folder
2082 (format "%s-%d" wl-draft-config-save-filename msg)
2084 element alist variable)
2086 ((eq operation 'save)
2087 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2088 (while (setq variable (pop variables))
2089 (when (boundp variable)
2091 (list (cons variable (eval variable))))))
2092 (elmo-object-save filename alist)))
2093 ((eq operation 'load)
2094 (setq alist (elmo-object-load filename))
2095 (while (setq element (pop alist))
2096 (set (make-local-variable (car element)) (cdr element))
2097 (wl-append wl-draft-config-variables (list (car element)))))
2098 ((eq operation 'delete)
2099 (if (file-exists-p filename)
2100 (delete-file filename))))))
2102 (defun wl-draft-queue-info-operation (msg operation
2103 &optional add-sent-message-via)
2104 (let* ((msgdb-dir (elmo-folder-msgdb-path
2105 (wl-folder-get-elmo-folder wl-queue-folder)))
2108 (format "%s-%d" wl-draft-queue-save-filename msg)
2110 element alist variable)
2112 ((eq operation 'save)
2113 (let ((variables (elmo-uniq-list
2114 (append wl-draft-queue-save-variables
2115 wl-draft-config-variables
2116 (list 'wl-draft-fcc-list)))))
2117 (if add-sent-message-via
2119 (push 'wl-sent-message-queued variables)
2120 (push 'wl-sent-message-via variables)))
2121 (while (setq variable (pop variables))
2122 (when (boundp variable)
2124 (list (cons variable (eval variable))))))
2125 (elmo-object-save filename alist)))
2126 ((eq operation 'load)
2127 (setq alist (elmo-object-load filename))
2128 (while (setq element (pop alist))
2129 (set (make-local-variable (car element)) (cdr element))))
2130 ((eq operation 'get-sent-via)
2131 (setq alist (elmo-object-load filename))
2132 (cdr (assq 'wl-sent-message-via alist)))
2133 ((eq operation 'delete)
2134 (if (file-exists-p filename)
2135 (delete-file filename))))))
2137 (defun wl-draft-queue-append (wl-sent-message-via)
2138 (if wl-draft-verbose-send
2139 (message "Queuing..."))
2140 (let ((send-buffer (current-buffer))
2141 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2142 (message-id (std11-field-body "Message-ID")))
2143 (if (elmo-folder-append-buffer folder t)
2145 (wl-draft-queue-info-operation
2146 (car (elmo-folder-status folder))
2147 'save wl-sent-message-via)
2148 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2149 (when wl-draft-verbose-send
2150 (setq wl-draft-verbose-msg "Queuing...")
2151 (message "Queuing...done")))
2152 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2153 (error "Queuing failed"))))
2155 (defun wl-draft-queue-flush ()
2156 "Flush draft queue."
2158 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2160 (elmo-folder-open-internal queue-folder)
2161 (elmo-folder-list-messages queue-folder)))
2164 (wl-draft-queue-flushing t)
2165 msgs failure len buffer msgid sent-via)
2166 ;; get plugged send message
2168 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2171 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2172 (or (not (nth 2 (car sent-via)))
2174 (car (nth 2 (car sent-via)))
2175 (cdr (nth 2 (car sent-via))))))
2176 (wl-append msgs (list (car msgs2)))
2178 (setq sent-via (cdr sent-via))))
2179 (setq msgs2 (cdr msgs2)))
2180 (when (> (setq len (length msgs)) 0)
2181 (if (elmo-y-or-n-p (format
2182 "%d message(s) are in the sending queue. Send now? "
2184 (not elmo-dop-flush-confirm) t)
2187 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2190 ;; reset buffer local variables
2191 (kill-all-local-variables)
2195 (setq wl-sent-message-via nil)
2196 (wl-draft-queue-info-operation (car msgs) 'load)
2197 (elmo-message-fetch queue-folder
2199 (elmo-make-fetch-strategy 'entire)
2200 nil (current-buffer))
2202 (setq failure (funcall
2203 wl-draft-queue-flush-send-function
2204 (format "Sending (%d/%d)..." i len)))
2205 ;;; (wl-draft-raw-send nil nil
2206 ;;; (format "Sending (%d/%d)..." i len))
2208 (elmo-display-error err t)
2212 (if (eq wl-sent-message-modified 'requeue)
2214 (elmo-folder-delete-messages
2215 queue-folder (cons (car msgs) nil))
2216 (wl-draft-queue-info-operation (car msgs) 'delete))
2218 (elmo-folder-delete-messages
2219 queue-folder (cons (car msgs) nil))
2220 (wl-draft-queue-info-operation (car msgs) 'delete)
2221 (setq performed (+ 1 performed))))
2222 (setq msgs (cdr msgs)))
2223 (kill-buffer buffer)
2224 (message "%d message(s) are sent." performed)))
2225 (message "%d message(s) are remained to be sent." len))
2226 (elmo-folder-close queue-folder)
2229 (defun wl-jump-to-draft-buffer (&optional arg)
2230 "Jump to the draft if exists."
2233 (wl-jump-to-draft-folder)
2234 (let ((draft-bufs (wl-collect-draft))
2238 (message "No draft buffer exist."))
2241 (sort (mapcar 'buffer-name draft-bufs)
2242 (function (lambda (a b)
2243 (not (string< a b))))))
2244 (if (setq buf (cdr (member (buffer-name)
2246 (setq buf (car buf))
2247 (setq buf (car draft-bufs)))
2248 (switch-to-buffer buf))))))
2250 (defun wl-jump-to-draft-folder ()
2251 (let ((msgs (reverse (elmo-folder-list-messages (wl-folder-get-elmo-folder
2253 (mybuf (buffer-name))
2256 (message "No draft message exist.")
2257 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2258 (setq msg (cadr (memq
2259 (string-to-int (substring mybuf (match-end 0)))
2261 (or msg (setq msg (car msgs)))
2262 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2263 (switch-to-buffer buf)
2264 (wl-draft-reedit msg)))))
2266 (defun wl-draft-highlight-and-recenter (&optional n)
2268 (if wl-highlight-body-too
2269 (let ((beg (point-min))
2271 (put-text-property beg end 'face nil)
2272 (wl-highlight-message beg end t)))
2275 ;;;; user-agent support by Sen Nagata
2277 ;; this appears to be necessarily global...
2278 (defvar wl-user-agent-compose-p nil)
2279 (defvar wl-user-agent-headers-and-body-alist nil)
2281 ;; this should be a generic function for mail-mode -- i wish there was
2282 ;; something like it in sendmail.el
2283 (defun wl-user-agent-insert-header (header-name header-value)
2284 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2285 ;; it seems like overriding existing headers is acceptable -- should
2286 ;; we provide an option?
2288 ;; plan was: unfold header (might be folded), remove existing value, insert
2290 ;; wl doesn't seem to fold header lines yet anyway :-)
2292 (let ((kill-whole-line t)
2294 (mail-position-on-field (capitalize header-name))
2295 (setq end-of-line (point))
2297 (re-search-forward ":" end-of-line)
2298 (insert (concat " " header-value "\n"))
2301 ;; this should be a generic function for mail-mode -- i wish there was
2302 ;; something like it in sendmail.el
2304 ;; ** haven't dealt w/ case where the body is already set **
2305 (defun wl-user-agent-insert-body (body-text)
2306 "Insert a body of text, BODY-TEXT, into a message."
2307 ;; code defensively... :-P
2308 (goto-char (point-min))
2309 (search-forward mail-header-separator)
2314 (defun wl-user-agent-compose (&optional to subject other-headers continue
2315 switch-function yank-action
2317 "Support the `compose-mail' interface for wl.
2318 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2319 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2320 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2322 (unless (featurep 'wl)
2324 ;; protect these -- to and subject get bound at some point, so it looks
2325 ;; to be necessary to protect the values used w/in
2326 (let ((wl-user-agent-headers-and-body-alist other-headers)
2327 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2328 (wl-draft-reply-buffer-style 'split))
2329 (when (eq switch-function 'switch-to-buffer-other-window)
2330 (when (one-window-p t)
2331 (if (window-minibuffer-p) (other-window 1))
2335 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2338 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2341 (setq wl-user-agent-headers-and-body-alist
2342 (cons (cons "to" to)
2343 wl-user-agent-headers-and-body-alist))))
2345 (if (wl-string-match-assoc "subject"
2346 wl-user-agent-headers-and-body-alist
2349 (wl-string-match-assoc "subject"
2350 wl-user-agent-headers-and-body-alist
2353 (setq wl-user-agent-headers-and-body-alist
2354 (cons (cons "subject" subject)
2355 wl-user-agent-headers-and-body-alist))))
2356 ;; i think this is what we want to use...
2359 ;; tell the hook-function to do its stuff
2360 (setq wl-user-agent-compose-p t)
2361 ;; because to get the hooks working, wl-draft has to think it has
2362 ;; been called interactively
2363 (call-interactively 'wl-draft))
2364 (setq wl-user-agent-compose-p nil))))
2366 (defun wl-user-agent-compose-internal ()
2367 "Manipulate headers and/or a body of a draft message."
2368 ;; being called from wl-user-agent-compose?
2369 (if wl-user-agent-compose-p
2372 (let ((headers wl-user-agent-headers-and-body-alist)
2373 (case-fold-search t))
2376 (if (not (string-match "^body$" (car (car headers))))
2377 (wl-user-agent-insert-header
2378 (car (car headers)) (cdr (car headers)))
2380 (setq headers (cdr headers))))
2381 ;; highlight headers (from wl-draft in wl-draft.el)
2382 (wl-highlight-headers 'for-draft)
2384 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2386 (wl-user-agent-insert-body
2387 (cdr (wl-string-match-assoc
2389 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2393 (product-provide (provide 'wl-draft) (require 'wl-version))
2395 ;;; wl-draft.el ends here