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 "Forward: " 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-draft-confirm ()
508 "Confirm send message."
510 (y-or-n-p (format "Send current draft as %s? "
511 (cond ((and (wl-message-mail-p) (wl-message-news-p))
513 ((wl-message-mail-p) "Mail")
514 ((wl-message-news-p) "News")))))
516 (defun wl-message-news-p ()
517 "If exist valid Newsgroups field, return non-nil."
518 (std11-field-body "Newsgroups"))
520 (defun wl-message-field-exists-p (field)
521 "If FIELD exist and FIELD value is not empty, return non-nil."
522 (let ((value (std11-field-body field)))
524 (not (string= value "")))))
526 (defun wl-message-mail-p ()
527 "If exist To, Cc or Bcc field, return non-nil."
528 (or (wl-message-field-exists-p "To")
529 (wl-message-field-exists-p "Resent-to")
530 (wl-message-field-exists-p "Cc")
531 (wl-message-field-exists-p "Bcc")
532 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
533 ;;; This may be needed..
534 ;;; (wl-message-field-exists-p "Fcc")
537 (defun wl-draft-open-file (&optional file)
538 "Open FILE for edit."
540 ;;;(interactive "*fFile to edit: ")
541 (wl-draft-edit-string (elmo-get-file-string
543 (read-file-name "File to edit: "
544 (or wl-temporary-file-directory
547 (defun wl-draft-edit-string (string)
548 (let ((cur-buf (current-buffer))
549 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
550 to subject in-reply-to cc references newsgroups mail-followup-to
551 content-type content-transfer-encoding from
552 body-beg buffer-read-only)
556 (setq to (std11-field-body "To"))
559 (decode-mime-charset-string
562 (setq subject (std11-field-body "Subject"))
563 (setq subject (and subject
565 (decode-mime-charset-string
568 (setq from (std11-field-body "From")
571 (decode-mime-charset-string
574 (setq in-reply-to (std11-field-body "In-Reply-To"))
575 (setq cc (std11-field-body "Cc"))
578 (decode-mime-charset-string
581 (setq references (std11-field-body "References"))
582 (setq newsgroups (std11-field-body "Newsgroups"))
583 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
584 (setq content-type (std11-field-body "Content-Type"))
585 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
586 (goto-char (point-min))
587 (or (re-search-forward "\n\n" nil t)
588 (search-forward (concat mail-header-separator "\n") nil t))
594 (nth 1 (std11-extract-address-components from))
595 wl-user-mail-address-list)
599 (cons 'Subject subject)
600 (cons 'Newsgroups newsgroups)
601 (cons 'Mail-Followup-To mail-followup-to)
602 (cons 'In-Reply-To in-reply-to)
603 (cons 'References references))
604 content-type content-transfer-encoding
605 (buffer-substring (point) (point-max))
607 (and to (mail-position-on-field "To"))
608 (delete-other-windows)
609 (kill-buffer tmp-buf)))
610 (setq buffer-read-only nil) ;;??
611 (run-hooks 'wl-draft-reedit-hook))
613 (defun wl-draft-insert-current-message (dummy)
615 (let (original-buffer
617 mail-citation-hook mail-yank-hooks
618 wl-draft-add-references wl-draft-add-in-reply-to
619 wl-draft-cite-function)
620 (with-current-buffer wl-draft-buffer-cur-summary-buffer
621 (with-current-buffer wl-message-buffer
622 (setq original-buffer (wl-message-get-original-buffer))
624 (with-current-buffer original-buffer
626 (error "No current message"))))
627 (setq mail-reply-buffer original-buffer)
628 (wl-draft-yank-from-mail-reply-buffer
630 wl-ignored-forwarded-headers)))
632 (defun wl-draft-insert-get-message (dummy)
633 (let ((fld (completing-read
635 (if (memq 'read-folder wl-use-folder-petname)
636 (wl-folder-get-entity-with-petname)
637 wl-folder-entity-hashtb)
638 nil nil wl-default-spec
639 'wl-read-folder-hist))
640 (number (call-interactively
641 (function (lambda (num)
642 (interactive "nNumber: ")
644 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
645 mail-citation-hook mail-yank-hooks
646 wl-draft-cite-function)
649 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
652 (elmo-make-fetch-strategy 'entire)
653 nil mail-reply-buffer)
654 (wl-draft-yank-from-mail-reply-buffer nil))
655 (kill-buffer mail-reply-buffer))))
658 ;; default body citation func
660 (defun wl-default-draft-cite ()
661 (let ((mail-yank-ignored-headers "[^:]+:")
662 (mail-yank-prefix "> ")
663 (summary-buf wl-current-summary-buffer)
664 (message-buf (get-buffer (wl-current-message-buffer)))
665 from date cite-title num entity)
667 (buffer-live-p summary-buf)
669 (buffer-live-p message-buf))
671 (with-current-buffer summary-buf
672 (let ((elmo-mime-charset wl-summary-buffer-mime-charset))
673 (setq num (save-excursion
674 (set-buffer message-buf)
675 wl-message-buffer-cur-number))
676 (setq entity (elmo-msgdb-overview-get-entity
677 num (wl-summary-buffer-msgdb)))
678 (setq date (elmo-msgdb-overview-entity-get-date entity))
679 (setq from (elmo-msgdb-overview-entity-get-from entity))))
680 (setq cite-title (format "At %s,\n%s wrote:"
681 (or date "some time ago")
682 (if wl-default-draft-cite-decorate-author
683 (funcall wl-summary-from-function
687 (insert cite-title "\n"))
688 (mail-indent-citation)))
690 (defvar wl-draft-buffer nil "Draft buffer to yank content")
691 (defun wl-draft-yank-to-draft-buffer (buffer)
692 "Yank BUFFER content to `wl-draft-buffer'."
693 (set-buffer wl-draft-buffer)
694 (let ((mail-reply-buffer buffer))
695 (wl-draft-yank-from-mail-reply-buffer nil)
696 (kill-buffer buffer)))
698 (defun wl-draft-yank-original (&optional arg)
699 "Yank original message."
702 (let (buf mail-reply-buffer)
705 (setq buf (current-buffer)))
706 (setq mail-reply-buffer buf)
707 (wl-draft-yank-from-mail-reply-buffer nil))
708 (wl-draft-yank-current-message-entity)))
710 (defun wl-draft-hide (editing-buffer)
711 "Hide the editing draft buffer if possible."
712 (when (and editing-buffer
713 (buffer-live-p editing-buffer))
714 (set-buffer editing-buffer)
715 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
716 fld-buf sum-win fld-win)
717 (if (and wl-draft-use-frame
718 (> (length (visible-frame-list)) 1))
724 ;; stay folder window if required
725 (when wl-stay-folder-window
726 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
727 (if (setq fld-win (get-buffer-window fld-buf))
728 (select-window fld-win)
729 (if wl-draft-resume-folder-window ;; resume folder window
730 (switch-to-buffer fld-buf)))))
731 (if (buffer-live-p sum-buf)
732 (if (setq sum-win (get-buffer-window sum-buf t))
733 ;; if Summary is on the frame, select it.
734 (select-window sum-win)
735 ;; if summary is not on the frame, switch to it.
736 (if (and wl-stay-folder-window
737 (or wl-draft-resume-folder-window fld-win))
738 (wl-folder-select-buffer sum-buf)
739 (switch-to-buffer sum-buf)))))))
741 (defun wl-draft-delete (editing-buffer)
742 "kill the editing draft buffer and delete the file corresponds to it."
745 (set-buffer editing-buffer)
746 (if wl-draft-buffer-file-name
748 (if (file-exists-p wl-draft-buffer-file-name)
749 (delete-file wl-draft-buffer-file-name))
750 (let ((msg (and wl-draft-buffer-file-name
751 (string-match "[0-9]+$" wl-draft-buffer-file-name)
753 (match-string 0 wl-draft-buffer-file-name)))))
754 (wl-draft-config-info-operation msg 'delete))))
755 (set-buffer-modified-p nil) ; force kill
756 (kill-buffer editing-buffer))))
758 (defun wl-draft-kill (&optional force-kill)
759 "Kill current draft buffer and quit editing."
762 (when (and (or (eq major-mode 'wl-draft-mode)
763 (eq major-mode 'mail-mode))
765 (y-or-n-p "Kill Current Draft? ")))
766 (let ((cur-buf (current-buffer)))
767 (wl-draft-hide cur-buf)
768 (wl-draft-delete cur-buf)))
771 (defun wl-draft-fcc ()
772 "Add a new Fcc field, with file name completion."
774 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
775 (mail-position-on-field "to"))
778 ;; Imported from message.el.
779 (defun wl-draft-elide-region (b e)
780 "Elide the text in the region.
781 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
785 (insert wl-draft-elide-ellipsis))
787 ;; Imported from message.el.
788 (defun wl-draft-beginning-of-line (&optional n)
789 "Move point to beginning of header value or to beginning of line."
791 (if (wl-draft-point-in-header-p)
792 (let* ((here (point))
793 (bol (progn (beginning-of-line n) (point)))
794 (eol (line-end-position))
795 (eoh (re-search-forward ": *" eol t)))
796 (if (or (not eoh) (equal here eoh))
799 (beginning-of-line n)))
801 (defun wl-draft-point-in-header-p ()
802 "Return t if point is in the header."
805 (goto-char (point-min))
806 (not (re-search-forward
807 (concat "^" (regexp-quote mail-header-separator) "\n")
810 ;; function for wl-sent-message-via
812 (defmacro wl-draft-sent-message-p (type)
813 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
815 (defmacro wl-draft-set-sent-message (type result &optional server-port)
816 (` (let ((element (assq (, type) wl-sent-message-via)))
818 (unless (eq (nth 1 element) (, result))
819 (setcdr element (list (, result) (, server-port)))
820 (setq wl-sent-message-modified t))
821 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
822 (setq wl-sent-message-modified t)))))
824 (defun wl-draft-sent-message-results ()
825 (let ((results wl-sent-message-via)
826 unplugged-via sent-via)
828 (cond ((eq (nth 1 (car results)) 'unplugged)
829 (push (caar results) unplugged-via))
830 ((eq (nth 1 (car results)) 'sent)
831 (push (caar results) sent-via)))
832 (setq results (cdr results)))
833 (list unplugged-via sent-via)))
835 (defun wl-draft-write-sendlog (status proto server to id)
836 "Write send log file, if `wl-draft-sendlog' is non-nil."
837 (when wl-draft-sendlog
839 (let* ((filename (expand-file-name wl-draft-sendlog-filename
840 elmo-msgdb-directory))
841 (filesize (nth 7 (file-attributes filename)))
842 (server (if server (concat " server=" server) ""))
844 ((memq proto '(fcc queue))
845 (format " folder=\"%s\"" to))
847 (format " ng=<%s>" to))
852 (mapcar '(lambda(x) (format "<%s>" x)) to)
855 (id (if id (concat " id=" id) ""))
856 (time (format-time-string "%Y/%m/%d %T")))
857 (insert (format "%s proto=%s stat=%s%s%s%s\n"
858 time proto status server to id))
859 (if (and wl-draft-sendlog-max-size filesize
860 (> filesize wl-draft-sendlog-max-size))
861 (rename-file filename (concat filename ".old") t))
862 (if (file-writable-p filename)
863 (write-region-as-binary (point-min) (point-max)
865 (message "%s is not writable." filename))))))
867 (defun wl-draft-get-header-delimiter (&optional delete)
868 ;; If DELETE is non-nil, replace the header delimiter with a blank line
870 (goto-char (point-min))
871 (when (re-search-forward
872 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
876 (setq delimline (point-marker)))
879 (defun wl-draft-send-mail-with-qmail ()
880 "Pass the prepared message buffer to qmail-inject.
881 Refer to the documentation for the variable `send-mail-function'
882 to find out how to use this."
883 (if (and wl-draft-qmail-send-plugged
884 (not (elmo-plugged-p)))
885 (wl-draft-set-sent-message 'mail 'unplugged)
887 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
888 (let ((id (std11-field-body "Message-ID"))
889 (to (std11-field-body "To")))
893 'call-process-region 1 (point-max) wl-qmail-inject-program
895 wl-qmail-inject-args))
896 ;; qmail-inject doesn't say anything on it's stdout/stderr,
897 ;; we have to look at the retval instead
899 (wl-draft-set-sent-message 'mail 'sent)
900 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
901 (1 (error "`qmail-inject' reported permanent failure"))
902 (111 (error "`qmail-inject' reported transient failure"))
903 ;; should never happen
904 (t (error "`qmail-inject' reported unknown failure"))))))
906 (defun wl-draft-parse-msg-id-list-string (string)
907 "Get msg-id list from STRING."
909 (dolist (parsed-id (std11-parse-msg-ids-string string))
910 (when (eq (car parsed-id) 'msg-id)
911 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
913 (nreverse msg-id-list)))
915 (defun wl-draft-eword-encode-address-list (string &optional column)
916 "Encode header field STRING as list of address, and return the result.
917 Cause an error when STRING contains invalid address.
918 Optional argument COLUMN is start-position of the field."
919 (car (eword-encode-rword-list
920 (or column eword-encode-default-start-column)
921 (eword-encode-addresses-to-rword-list
922 (wl-draft-std11-parse-addresses (std11-lexical-analyze string))))))
924 (defun wl-draft-std11-parse-addresses (lal)
925 (let ((ret (std11-parse-address lal)))
926 (when (and (not (and (eq (length lal) 1)
927 (eq (car (car lal)) 'spaces)))
929 (error "Error while parsing address"))
931 (let ((dest (list (car ret))))
933 (while (and (setq ret (std11-parse-ascii-token lal))
934 (string-equal (cdr (assq 'specials (car ret))) ",")
935 (setq ret (std11-parse-address (cdr ret)))
937 (setq dest (cons (car ret) dest))
938 (setq lal (cdr ret)))
939 (while (eq 'spaces (car (car lal)))
940 (setq lal (cdr lal)))
941 (if lal (error "Error while parsing address"))
944 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
945 "Get mailbox list of FIELD from current buffer.
946 The buffer is expected to be narrowed to just the headers of the message.
947 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
948 from current buffer."
950 (let ((case-fold-search t)
951 (inhibit-read-only t)
953 mailbox-list beg seq has-group-list)
954 (goto-char (point-min))
955 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
958 (re-search-forward "^[^ \t]" nil 'move)
960 (skip-chars-backward "\n")
961 (setq seq (std11-lexical-analyze
962 (buffer-substring-no-properties beg (point))))
963 (setq addresses (wl-draft-std11-parse-addresses seq))
965 (cond ((eq (car (car addresses)) 'group)
966 (setq has-group-list t)
970 'std11-address-string
971 (nth 2 (car addresses))))))
972 ((eq (car (car addresses)) 'mailbox)
973 (setq address (nth 1 (car addresses)))
977 (std11-addr-to-string
978 (if (eq (car address) 'phrase-route-addr)
981 (setq addresses (cdr addresses)))
982 (when (and remove-group-list has-group-list)
983 (delete-region beg (point))
984 (insert (wl-address-string-without-group-list-contents seq))))
987 (defun wl-draft-deduce-address-list (buffer header-start header-end)
988 "Get address list suitable for smtp RCPT TO:<address>.
989 Group list content is removed if `wl-draft-remove-group-list-contents' is
991 (let ((fields '("to" "cc" "bcc"))
992 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
997 (narrow-to-region header-start header-end)
998 (goto-char (point-min))
1000 (if (re-search-forward "^resent-to[\t ]*:" nil t)
1001 (setq fields resent-fields)))
1005 (wl-draft-parse-mailbox-list
1007 wl-draft-remove-group-list-contents)))
1008 (setq fields (cdr fields)))
1014 (defun wl-draft-send-mail-with-smtp ()
1015 "Send the prepared message buffer with SMTP."
1017 (let* ((errbuf (if mail-interactive
1018 (generate-new-buffer " smtp errors")
1020 (case-fold-search t)
1021 (default-case-fold-search t)
1022 (sender (or wl-envelope-from
1023 (wl-address-header-extract-address wl-from)))
1024 (delimline (save-excursion
1025 (goto-char (point-min))
1027 (concat "^" (regexp-quote mail-header-separator)
1031 (or wl-smtp-posting-server smtp-server "localhost"))
1032 (smtp-service (or wl-smtp-posting-port smtp-service))
1033 (smtp-local-domain (or smtp-local-domain wl-local-domain))
1034 (id (std11-field-body "message-id"))
1036 (if (not (elmo-plugged-p smtp-server smtp-service))
1037 (wl-draft-set-sent-message 'mail 'unplugged
1038 (cons smtp-server smtp-service))
1041 ;; Instead of `smtp-deduce-address-list'.
1042 (setq recipients (wl-draft-deduce-address-list
1043 (current-buffer) (point-min) delimline))
1044 (unless recipients (error "No recipients"))
1045 ;; Insert an extra newline if we need it to work around
1046 ;; Sun's bug that swallows newlines.
1047 (goto-char (1+ delimline))
1048 (if (eval mail-mailer-swallows-blank-line)
1050 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1051 (if mail-interactive
1055 (wl-draft-delete-field "bcc" delimline)
1056 (wl-draft-delete-field "resent-bcc" delimline)
1057 (let (process-connection-type)
1060 (wl-smtp-extension-bind
1062 (smtp-send-buffer sender recipients (current-buffer))
1064 (wl-draft-write-sendlog 'failed 'smtp smtp-server
1066 (if (and (eq (car err) 'smtp-response-error)
1067 (/= (nth 1 err) 334))
1069 (wl-smtp-password-key
1071 (car smtp-sasl-mechanisms)
1073 (signal (car err) (cdr err)))))
1074 (wl-draft-set-sent-message 'mail 'sent)
1075 (wl-draft-write-sendlog
1076 'ok 'smtp smtp-server recipients id)))))
1077 (if (bufferp errbuf)
1078 (kill-buffer errbuf))))))
1080 (defun wl-draft-send-mail-with-pop-before-smtp ()
1081 "Send the prepared message buffer with POP-before-SMTP."
1082 (require 'elmo-pop3)
1086 :user (or wl-pop-before-smtp-user
1087 elmo-pop3-default-user)
1088 :server (or wl-pop-before-smtp-server
1089 elmo-pop3-default-server)
1090 :port (or wl-pop-before-smtp-port
1091 elmo-pop3-default-port)
1092 :auth (or wl-pop-before-smtp-authenticate-type
1093 elmo-pop3-default-authenticate-type)
1094 :stream-type (or wl-pop-before-smtp-stream-type
1095 elmo-pop3-default-stream-type))))
1096 (condition-case error
1098 (elmo-pop3-get-session session)
1099 (when session (elmo-network-close-session session)))
1101 (elmo-network-close-session session)
1102 (unless (string= (nth 1 error) "Unplugged")
1103 (signal (car error)(cdr error))))))
1104 (wl-draft-send-mail-with-smtp))
1106 (defun wl-draft-insert-required-fields (&optional force-msgid)
1107 "Insert Message-ID, Date, and From field.
1108 If FORCE-MSGID, insert message-id regardless of `wl-insert-message-id'."
1109 ;; Insert Message-Id field...
1110 (goto-char (point-min))
1111 (when (and (or force-msgid
1112 wl-insert-message-id)
1113 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1114 (insert (concat "Message-ID: "
1115 (funcall wl-message-id-function)
1117 ;; Insert date field.
1118 (goto-char (point-min))
1119 (or (re-search-forward "^Date[ \t]*:" nil t)
1120 (wl-draft-insert-date-field))
1121 ;; Insert from field.
1122 (goto-char (point-min))
1123 (or (re-search-forward "^From[ \t]*:" nil t)
1124 (wl-draft-insert-from-field)))
1126 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1127 "Send the message in the current buffer."
1129 (std11-narrow-to-header mail-header-separator)
1130 (wl-draft-insert-required-fields)
1131 ;; Delete null fields.
1132 (goto-char (point-min))
1133 (while (re-search-forward "^[^ \t\n:]+:[ \t]*\n" nil t)
1135 ;; ignore any blank lines in the header
1136 (while (re-search-forward "\n\n\n*" nil t)
1137 (replace-match "\n")))
1138 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1139 (wl-draft-dispatch-message)
1140 (when kill-when-done
1141 ;; hide editing-buffer.
1142 (wl-draft-hide editing-buffer)
1143 ;; delete editing-buffer and its file.
1144 (wl-draft-delete editing-buffer)))
1146 (defun wl-draft-dispatch-message (&optional mes-string)
1147 "Send the message in the current buffer. Not modified the header fields."
1148 (let (delimline mime-bcc)
1149 (if (and wl-draft-verbose-send mes-string)
1150 (message mes-string))
1152 (setq delimline (wl-draft-get-header-delimiter t))
1153 (unless wl-draft-fcc-list
1154 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1156 (setq wl-sent-message-modified nil)
1159 (if (and (wl-message-mail-p)
1160 (not (wl-draft-sent-message-p 'mail)))
1161 (if (or (not (or wl-draft-force-queuing
1162 wl-draft-force-queuing-mail))
1163 (memq 'mail wl-sent-message-queued))
1165 (setq mime-bcc (wl-draft-mime-bcc-field))
1166 (funcall wl-draft-send-mail-function)
1167 (when (not (zerop (length mime-bcc)))
1168 (wl-draft-do-mime-bcc mime-bcc)))
1169 (push 'mail wl-sent-message-queued)
1170 (wl-draft-set-sent-message 'mail 'unplugged)))
1171 (if (and (wl-message-news-p)
1172 (not (wl-draft-sent-message-p 'news))
1173 (not (wl-message-field-exists-p "Resent-to")))
1174 (if (or (not (or wl-draft-force-queuing
1175 wl-draft-force-queuing-news))
1176 (memq 'news wl-sent-message-queued))
1177 (funcall wl-draft-send-news-function)
1178 (push 'news wl-sent-message-queued)
1179 (wl-draft-set-sent-message 'news 'unplugged))))
1180 (let* ((status (wl-draft-sent-message-results))
1181 (unplugged-via (car status))
1182 (sent-via (nth 1 status)))
1183 ;; If one sent, process fcc folder.
1184 (if (and sent-via wl-draft-fcc-list)
1186 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1188 (setq wl-draft-fcc-list nil)))
1189 (if wl-draft-use-cache
1190 (let ((id (std11-field-body "Message-ID"))
1191 (elmo-enable-disconnected-operation t))
1192 (elmo-file-cache-save (elmo-file-cache-get-path id)
1194 ;; If one unplugged, append queue.
1195 (when (and unplugged-via
1196 wl-sent-message-modified)
1197 (if wl-draft-enable-queuing
1199 (wl-draft-queue-append wl-sent-message-via)
1200 (setq wl-sent-message-modified 'requeue))
1201 (error "Unplugged")))
1202 (when wl-draft-verbose-send
1203 (if (and unplugged-via sent-via);; combined message
1205 (setq wl-draft-verbose-msg
1206 (format "Sending%s and Queuing%s..."
1207 sent-via unplugged-via))
1208 (message (concat wl-draft-verbose-msg "done")))
1210 (message (concat mes-string
1211 (if sent-via "done" "failed")))))))))
1212 (not wl-sent-message-modified)) ;; return value
1214 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1215 "Force send current buffer as raw message."
1218 (let (wl-interactive-send
1219 ;;; wl-draft-verbose-send
1220 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1221 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1224 (wl-draft-send kill-when-done mes-string))))
1226 (defun wl-draft-clone-local-variables ()
1227 (let ((locals (buffer-local-variables))
1230 (when (and (consp (car locals))
1232 (string-match wl-draft-clone-local-variable-regexp
1233 (symbol-name (car (car locals)))))
1234 (wl-append result (list (car (car locals)))))
1235 (setq locals (cdr locals)))
1238 (defun wl-draft-send (&optional kill-when-done mes-string)
1239 "Send current draft message.
1240 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1242 ;; Don't call this explicitly.
1243 ;; Added to 'wl-draft-send-hook (by teranisi)
1244 ;; (wl-draft-config-exec)
1245 (run-hooks 'wl-draft-send-hook)
1246 (when (or (not wl-interactive-send)
1247 (y-or-n-p "Do you really want to send current draft? "))
1248 (let ((send-mail-function 'wl-draft-raw-send)
1249 (editing-buffer (current-buffer))
1250 (sending-buffer (wl-draft-generate-clone-buffer
1251 " *wl-draft-sending-buffer*"
1252 (append wl-draft-config-variables
1253 (wl-draft-clone-local-variables))))
1254 (wl-draft-verbose-msg nil)
1257 (save-excursion (set-buffer sending-buffer)
1258 (if (and (not (wl-message-mail-p))
1259 (not (wl-message-news-p)))
1260 (error "No recipient is specified"))
1261 (expand-abbrev) ; for mail-abbrevs
1262 (let ((mime-header-encode-method-alist
1264 '((wl-draft-eword-encode-address-list
1265 . (To Cc Bcc Resent-To Resent-Cc Bcc Resent-Bcc)))
1266 (if (boundp 'mime-header-encode-method-alist)
1267 (symbol-value 'mime-header-encode-method-alist)))))
1268 (run-hooks 'mail-send-hook) ; translate buffer
1271 (if wl-draft-verbose-send
1272 (message (or mes-string "Sending...")))
1273 (funcall wl-draft-send-function editing-buffer kill-when-done)
1274 ;; Now perform actions on successful sending.
1275 (while mail-send-actions
1277 (apply (car (car mail-send-actions))
1278 (cdr (car mail-send-actions)))
1280 (setq mail-send-actions (cdr mail-send-actions)))
1281 (if wl-draft-verbose-send
1282 (message (concat (or wl-draft-verbose-msg
1283 mes-string "Sending...")
1285 ;; kill sending buffer, anyway.
1286 (and (buffer-live-p sending-buffer)
1287 (kill-buffer sending-buffer))))))
1289 (defun wl-draft-mime-bcc-field ()
1290 "Return the MIME-Bcc field body. The field is deleted."
1291 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1292 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1294 (defun wl-draft-do-mime-bcc (field-body)
1295 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1296 (let ((orig-subj (std11-field-body "subject"))
1297 (recipients (wl-parse-addresses field-body))
1298 (draft-buffer (current-buffer))
1300 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1301 (not (zerop (length field-body))))
1302 (with-current-buffer (setq buffer (generate-new-buffer
1303 " *temporary buffer for mime bcc*"))
1304 (insert-buffer draft-buffer))
1306 (dolist (recipient recipients)
1308 (let ((wl-draft-doing-mime-bcc t)
1311 wl-draft-add-references
1312 wl-draft-add-in-reply-to
1313 wl-draft-cite-function)
1314 ;; To work wl-draft-create-contents.
1315 (setq major-mode 'wl-draft-mode)
1316 (wl-draft-create-contents
1317 (append `((To . ,recipient)
1319 (Subject . ,(concat "A blind carbon copy ("
1322 (wl-draft-default-headers)))
1323 (wl-draft-insert-required-fields)
1324 (wl-draft-insert-mail-header-separator)
1325 (goto-char (point-max))
1326 (insert (or wl-draft-mime-bcc-body
1327 "This is a blind carbon copy.")
1329 (mime-edit-insert-tag "message" "rfc822")
1330 (let ((mail-reply-buffer buffer))
1331 (wl-draft-yank-from-mail-reply-buffer nil))
1332 (mime-edit-translate-buffer)
1333 (wl-draft-raw-send))))
1334 (kill-buffer buffer)))))
1336 ;; Derived from `message-save-drafts' in T-gnus.
1337 (defun wl-draft-save ()
1338 "Save current draft."
1340 (if (buffer-modified-p)
1342 (message "Saving %s..." wl-draft-buffer-file-name)
1343 (let ((msg (buffer-substring-no-properties (point-min) (point-max))))
1344 (with-temp-file wl-draft-buffer-file-name
1346 ;; If no header separator, insert it.
1348 (goto-char (point-min))
1349 (unless (re-search-forward
1350 (concat "^" (regexp-quote mail-header-separator) "$")
1352 (goto-char (point-min))
1353 (if (re-search-forward "\n\n" nil t)
1354 (replace-match (concat "\n" mail-header-separator "\n"))
1355 (goto-char (point-max))
1356 (insert (if (eq (char-before) ?\n) "" "\n")
1357 mail-header-separator "\n"))))
1358 (let ((mime-header-encode-method-alist
1359 '((eword-encode-unstructured-field-body))))
1360 (mime-edit-translate-buffer))
1361 (wl-draft-get-header-delimiter t)))
1362 (set-buffer-modified-p nil)
1363 (wl-draft-config-info-operation
1364 (and (string-match "[0-9]+$" wl-draft-buffer-file-name)
1366 (match-string 0 wl-draft-buffer-file-name)))
1368 (message "Saving %s...done" wl-draft-buffer-file-name))
1369 (message "(No changes need to be saved)")))
1371 (defun wl-draft-mimic-kill-buffer ()
1372 "Kill the current (draft) buffer with query."
1374 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1377 (if (or (not bufname)
1378 (string-equal bufname "")
1379 (string-equal bufname (buffer-name)))
1380 (let ((bufname (current-buffer)))
1381 (when (or (not (buffer-modified-p))
1383 (format "Buffer %s modified; kill anyway? " bufname)))
1384 (set-buffer-modified-p nil)
1385 (wl-draft-hide bufname)
1386 (kill-buffer bufname)))
1387 (kill-buffer bufname))))
1389 (defun wl-draft-save-and-exit ()
1390 "Save current draft and exit current draft mode."
1393 (let ((editing-buffer (current-buffer)))
1394 (wl-draft-hide editing-buffer)
1395 (kill-buffer editing-buffer)))
1397 (defun wl-draft-send-and-exit ()
1398 "Send current draft message and kill it."
1402 (defun wl-draft-send-from-toolbar ()
1404 (let ((wl-interactive-send t))
1405 (wl-draft-send-and-exit)))
1407 (defun wl-draft-delete-field (field &optional delimline replace)
1408 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1410 (defun wl-draft-delete-fields (field &optional delimline replace)
1413 (goto-char (point-min))
1414 (if (search-forward "\n\n" nil t)
1415 (setq delimline (point))
1416 (setq delimline (point-max))))
1417 (narrow-to-region (point-min) delimline)
1418 (goto-char (point-min))
1419 (let ((regexp (concat "^" field ":"))
1420 (case-fold-search t))
1422 (if (looking-at regexp)
1428 (if (re-search-forward "^[^ \t]" nil t)
1429 (goto-char (match-beginning 0))
1432 (insert (concat field ": " replace "\n"))))
1434 (if (re-search-forward "^[^ \t]" nil t)
1435 (goto-char (match-beginning 0))
1438 (defun wl-draft-get-fcc-list (header-end)
1440 (case-fold-search t))
1441 (or (markerp header-end) (error "HEADER-END must be a marker"))
1443 (goto-char (point-min))
1444 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1446 (cons (buffer-substring-no-properties
1450 (skip-chars-backward " \t")
1454 (wl-folder-confirm-existence
1455 (wl-folder-get-elmo-folder (eword-decode-string (car fcc-list)))))
1456 (delete-region (match-beginning 0)
1457 (progn (forward-line 1) (point)))))
1460 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1461 (let ((send-mail-buffer (current-buffer))
1462 (tembuf (generate-new-buffer " fcc output"))
1463 (case-fold-search t)
1465 (or (markerp header-end) (error "HEADER-END must be a marker"))
1468 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1471 ;; insert just the headers to avoid moving the gap more than
1472 ;; necessary (the message body could be arbitrarily huge.)
1473 (insert-buffer-substring send-mail-buffer 1 header-end)
1474 (wl-draft-insert-required-fields t)
1475 (goto-char (point-max))
1476 (insert-buffer-substring send-mail-buffer header-end)
1477 (let ((id (std11-field-body "Message-ID"))
1478 (elmo-enable-disconnected-operation t)
1481 (unless (or cache-saved
1482 (elmo-folder-plugged-p
1483 (wl-folder-get-elmo-folder (car fcc-list))))
1484 (elmo-file-cache-save id nil) ;; for disconnected operation
1485 (setq cache-saved t))
1486 (if (elmo-folder-append-buffer
1487 (wl-folder-get-elmo-folder
1488 (eword-decode-string (car fcc-list)))
1489 (not wl-fcc-force-as-read))
1490 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1491 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1492 (setq fcc-list (cdr fcc-list)))))
1493 (kill-buffer tembuf)))
1495 (defun wl-draft-on-field-p ()
1498 (goto-char (point-min))
1499 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1506 (if (or (looking-at ".*,[ \t]?$")
1507 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1509 (let ((pos (point)))
1512 (if (looking-at "^[ \t]")
1514 (if (re-search-forward ":" pos t) nil t)))))))
1516 (defun wl-draft-random-alphabet ()
1517 (let ((alphabet '(?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K ?L ?M ?N ?O ?P ?Q ?R ?S ?T ?U ?V ?W ?X ?Y ?Z)))
1518 (nth (abs (% (random) 26)) alphabet)))
1522 (defun wl-draft (&optional header-alist
1523 content-type content-transfer-encoding
1526 "Write and send mail/news message with Wanderlust."
1533 (wl-plugged-init t))
1535 (wl-init)) ; returns immediately if already initialized.
1538 (let (buf-name header-alist-internal)
1540 (wl-draft-create-buffer
1542 (eq this-command 'wl-draft)
1543 (eq this-command 'wl-summary-write)
1544 (eq this-command 'wl-summary-write-current-folder)
1545 (eq this-command 'wl-folder-write-current-folder))
1548 (unless (cdr (assq 'From header-alist))
1550 (append (list (cons 'From wl-from)) header-alist)))
1551 (unless (cdr (assq 'To header-alist))
1556 (if (assq 'To header-alist)
1557 (setcdr (assq 'To header-alist) to)
1559 (append header-alist
1560 (list (cons 'To to))))))))
1561 (unless (cdr (assq 'Subject header-alist))
1562 (if (assq 'Subject header-alist)
1563 (setcdr (assq 'Subject header-alist) "")
1565 (append header-alist (list (cons 'Subject ""))))))
1566 (setq header-alist (append header-alist
1567 (wl-draft-default-headers)
1568 wl-draft-additional-header-alist
1569 (if body (list "" (cons 'Body body)))))
1570 (wl-draft-create-contents header-alist)
1572 (wl-draft-decode-body
1573 content-type content-transfer-encoding))
1574 (wl-draft-insert-mail-header-separator)
1575 (wl-draft-prepare-edit)
1577 (run-hooks 'wl-mail-setup-hook))
1578 (goto-char (point-min))
1579 (wl-user-agent-compose-internal) ;; user-agent
1582 (string= (cdr (assq 'To header-alist)) ""))
1583 (mail-position-on-field "To"))
1585 (goto-char (point-max))))
1588 (defun wl-draft-create-buffer (&optional full parent-folder)
1589 (let* ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1590 (parent-folder (or parent-folder (wl-summary-buffer-folder-name)))
1591 (summary-buf (wl-summary-get-buffer parent-folder))
1592 buf-name file-name num change-major-mode-hook)
1593 (if (not (elmo-folder-message-file-p draft-folder))
1594 (error "%s folder cannot be used for draft folder" wl-draft-folder))
1595 (setq num (elmo-max-of-list
1596 (or (elmo-folder-list-messages draft-folder) '(0))))
1597 (setq num (+ 1 num))
1598 ;; To get unused buffer name.
1599 (while (get-buffer (concat wl-draft-folder "/" (int-to-string num)))
1600 (setq num (+ 1 num)))
1601 (setq buf-name (find-file-noselect
1603 (elmo-message-file-name
1604 (wl-folder-get-elmo-folder wl-draft-folder)
1606 (if wl-draft-use-frame
1607 (switch-to-buffer-other-frame buf-name)
1608 (switch-to-buffer buf-name))
1609 (set-buffer buf-name)
1610 (if (not (string-match (regexp-quote wl-draft-folder)
1612 (rename-buffer (concat wl-draft-folder "/" (int-to-string num))))
1613 (if (or (eq wl-draft-reply-buffer-style 'full)
1615 (delete-other-windows))
1618 (make-local-variable 'truncate-partial-width-windows)
1619 (setq truncate-partial-width-windows nil)
1620 (setq truncate-lines wl-draft-truncate-lines)
1621 (setq wl-sent-message-via nil)
1622 (setq wl-sent-message-queued nil)
1623 (setq wl-draft-buffer-file-name file-name)
1624 (setq wl-draft-config-exec-flag t)
1625 (setq wl-draft-parent-folder parent-folder)
1626 (or (eq this-command 'wl-folder-write-current-folder)
1627 (setq wl-draft-buffer-cur-summary-buffer summary-buf))
1630 (defun wl-draft-create-contents (header-alist)
1631 "header-alist' sample
1632 '(function ;; funcall
1633 string ;; insert string
1634 (symbol . string) ;; insert symbol-value: string
1635 (symbol . function) ;; (funcall) and if it returns string,
1636 ;; insert symbol-value: string
1637 (symbol . nil) ;; do nothing
1641 (unless (eq major-mode 'wl-draft-mode)
1642 (error "wl-draft-create-header must be use in wl-draft-mode."))
1643 (let ((halist header-alist)
1648 ((functionp (car halist)) (funcall (car halist)))
1650 ((stringp (car halist)) (insert (car halist) "\n"))
1652 ((consp (car halist))
1653 (setq field (car (car halist)))
1654 (setq value (cdr (car halist)))
1658 ((eq field 'Body) ; body
1660 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1662 (let ((value-return (funcall value)))
1663 (when (stringp value-return)
1664 (insert (symbol-name field) ": " value-return "\n"))))
1673 (setq halist (cdr halist)))))
1675 (defun wl-draft-prepare-edit ()
1676 (unless (eq major-mode 'wl-draft-mode)
1677 (error "wl-draft-create-header must be use in wl-draft-mode."))
1678 (let (change-major-mode-hook)
1679 (wl-draft-editor-mode)
1680 (add-hook 'local-write-file-hooks 'wl-draft-save)
1681 (wl-draft-overload-functions)
1682 (wl-highlight-headers 'for-draft)
1684 (clear-visited-file-modtime)))
1686 (defun wl-draft-decode-header ()
1688 (std11-narrow-to-header)
1689 (wl-draft-decode-message-in-buffer)
1692 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1695 (std11-field-body "content-type")))
1696 (content-transfer-encoding
1697 (or content-transfer-encoding
1698 (std11-field-body "content-transfer-encoding")))
1701 (std11-narrow-to-header)
1702 (wl-draft-delete-field "content-type")
1703 (wl-draft-delete-field "content-transfer-encoding")
1704 (goto-char (point-max))
1705 (setq delimline (point-marker))
1707 (narrow-to-region delimline (point-max))
1708 (goto-char (point-min))
1710 (insert "Content-type: " content-type "\n"))
1711 (when content-transfer-encoding
1712 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1713 (wl-draft-decode-message-in-buffer)
1714 (goto-char (point-min))
1715 (unless (re-search-forward "^$" (point-at-eol) t)
1720 ;;; subroutine for wl-draft-create-contents
1721 ;;; must be used in wl-draft-mode
1722 (defun wl-draft-check-new-line ()
1723 (if (not (= (preceding-char) ?\n))
1726 (defsubst wl-draft-trim-ccs (cc)
1732 (null (and wl-draft-delete-myself-from-bcc-fcc
1734 (mapcar 'wl-address-header-extract-address
1736 (wl-parse-addresses (std11-field-body "To"))
1737 (wl-parse-addresses (std11-field-body "Cc"))))
1738 (mapcar 'downcase wl-subscribed-mailing-list)))))
1742 (defsubst wl-draft-default-headers ()
1744 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1745 (wl-address-header-extract-address
1747 (cons 'User-Agent wl-generate-mailer-string-function)
1748 (cons 'Reply-To mail-default-reply-to)
1749 (cons 'Bcc (function
1752 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1753 (cons 'Fcc (function
1755 (wl-draft-trim-ccs wl-fcc))))
1756 (cons 'Organization wl-organization)
1757 (and wl-auto-insert-x-face
1758 (file-exists-p wl-x-face-file)
1759 'wl-draft-insert-x-face-field-here) ;; allow nil
1760 mail-default-headers
1761 ;; check \n at th end of line for `mail-default-headers'
1762 'wl-draft-check-new-line
1765 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1768 (goto-char delimline)
1769 (goto-char (point-min))
1770 (if (search-forward "\n\n" nil t)
1771 (delete-backward-char 1)
1772 (goto-char (point-max))))
1773 (wl-draft-check-new-line)
1774 (put-text-property (point)
1776 (insert mail-header-separator "\n")
1778 'category 'mail-header-separator)))
1782 (defun wl-draft-elmo-nntp-send ()
1783 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1784 (elmo-nntp-default-user
1785 (or wl-nntp-posting-user elmo-nntp-default-user))
1786 (elmo-nntp-default-server
1787 (or wl-nntp-posting-server elmo-nntp-default-server))
1788 (elmo-nntp-default-port
1789 (or wl-nntp-posting-port elmo-nntp-default-port))
1790 (elmo-nntp-default-stream-type
1791 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1792 (elmo-nntp-default-function wl-nntp-posting-function)
1794 (if (setq condition (cdr (elmo-string-matched-assoc
1795 (std11-field-body "Newsgroups")
1796 wl-nntp-posting-config-alist)))
1797 (if (stringp condition)
1798 (setq elmo-nntp-default-server condition)
1799 (while (car condition)
1800 (set (intern (format "elmo-nntp-default-%s"
1801 (symbol-name (caar condition))))
1803 (setq condition (cdr condition)))))
1804 (unless elmo-nntp-default-function
1805 (error "wl-draft-nntp-send: posting-function is nil."))
1806 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1807 (wl-draft-set-sent-message 'news 'unplugged
1808 (cons elmo-nntp-default-server
1809 elmo-nntp-default-port))
1810 (funcall elmo-nntp-default-function
1811 elmo-nntp-default-server (current-buffer))
1812 (wl-draft-set-sent-message 'news 'sent)
1813 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1814 (std11-field-body "Newsgroups")
1815 (std11-field-body "Message-ID")))))
1817 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1818 "Generate clone of current buffer named NAME."
1819 (let ((editing-buffer (current-buffer)))
1821 (set-buffer (generate-new-buffer name))
1824 (wl-draft-editor-mode)
1825 (insert-buffer editing-buffer)
1827 (while local-variables
1828 (make-local-variable (car local-variables))
1829 (set (car local-variables)
1831 (set-buffer editing-buffer)
1832 (symbol-value (car local-variables))))
1833 (setq local-variables (cdr local-variables)))
1836 (defun wl-draft-reedit (number)
1837 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1839 buffer file-name change-major-mode-hook)
1840 (setq file-name (elmo-message-file-name draft-folder number))
1841 (unless (file-exists-p file-name)
1842 (error "File %s does not exist" file-name))
1843 (if (setq buffer (get-buffer
1844 (concat wl-draft-folder "/"
1845 (number-to-string number))))
1847 (if wl-draft-use-frame
1848 (switch-to-buffer-other-frame buffer)
1849 (switch-to-buffer buffer))
1850 (set-buffer buffer))
1851 (setq buffer (get-buffer-create (number-to-string number)))
1852 (if wl-draft-use-frame
1853 (switch-to-buffer-other-frame buffer)
1854 (switch-to-buffer buffer))
1856 (insert-file-contents-as-binary file-name)
1857 (let((mime-edit-again-ignored-field-regexp
1858 "^\\(Content-.*\\|Mime-Version\\):"))
1859 (wl-draft-decode-message-in-buffer))
1860 (wl-draft-insert-mail-header-separator)
1861 (if (not (string-match (regexp-quote wl-draft-folder)
1863 (rename-buffer (concat wl-draft-folder "/" (buffer-name))))
1866 (setq buffer-file-name file-name)
1867 (make-local-variable 'truncate-partial-width-windows)
1868 (setq truncate-partial-width-windows nil)
1869 (setq truncate-lines wl-draft-truncate-lines)
1870 (setq wl-sent-message-via nil)
1871 (setq wl-sent-message-queued nil)
1872 (setq wl-draft-buffer-file-name file-name)
1873 (wl-draft-config-info-operation number 'load)
1874 (goto-char (point-min))
1875 (wl-draft-overload-functions)
1876 (wl-draft-editor-mode)
1877 (add-hook 'local-write-file-hooks 'wl-draft-save)
1878 (wl-highlight-headers 'for-draft)
1879 (run-hooks 'wl-draft-reedit-hook)
1880 (goto-char (point-max))
1883 (defmacro wl-draft-body-goto-top ()
1885 (goto-char (point-min))
1886 (if (re-search-forward mail-header-separator nil t)
1888 (goto-char (point-max))))))
1890 (defmacro wl-draft-body-goto-bottom ()
1891 (` (goto-char (point-max))))
1893 (defmacro wl-draft-config-body-goto-header ()
1895 (goto-char (point-min))
1896 (if (re-search-forward mail-header-separator nil t)
1898 (goto-char (point-max))))))
1900 (defun wl-draft-config-sub-body (content)
1901 (wl-draft-body-goto-top)
1902 (delete-region (point) (point-max))
1903 (if content (insert (eval content))))
1905 (defun wl-draft-config-sub-top (content)
1906 (wl-draft-body-goto-top)
1907 (if content (insert (eval content))))
1909 (defun wl-draft-config-sub-bottom (content)
1910 (wl-draft-body-goto-bottom)
1911 (if content (insert (eval content))))
1913 (defun wl-draft-config-sub-header (content)
1914 (wl-draft-config-body-goto-header)
1915 (if content (insert (concat (eval content) "\n"))))
1917 (defun wl-draft-config-sub-header-top (content)
1918 (goto-char (point-min))
1919 (if content (insert (concat (eval content) "\n"))))
1921 (defun wl-draft-config-sub-part-top (content)
1922 (goto-char (mime-edit-content-beginning))
1923 (if content (insert (concat (eval content) "\n"))))
1925 (defun wl-draft-config-sub-part-bottom (content)
1926 (goto-char (mime-edit-content-end))
1927 (if content (insert (concat (eval content) "\n"))))
1929 (defsubst wl-draft-config-sub-file (content)
1930 (let ((coding-system-for-read wl-cs-autoconv)
1931 (file (expand-file-name (eval content))))
1932 (if (file-exists-p file)
1933 (insert-file-contents file)
1934 (error "%s: no exists file" file))))
1936 (defun wl-draft-config-sub-body-file (content)
1937 (wl-draft-body-goto-top)
1938 (delete-region (point) (point-max))
1939 (wl-draft-config-sub-file content))
1941 (defun wl-draft-config-sub-top-file (content)
1942 (wl-draft-body-goto-top)
1943 (wl-draft-config-sub-file content))
1945 (defun wl-draft-config-sub-bottom-file (content)
1946 (wl-draft-body-goto-bottom)
1947 (wl-draft-config-sub-file content))
1949 (defun wl-draft-config-sub-header-file (content)
1950 (wl-draft-config-body-goto-header)
1951 (wl-draft-config-sub-file content))
1953 (defun wl-draft-config-sub-template (content)
1954 (setq wl-draft-config-variables
1955 (wl-template-insert (eval content))))
1957 (defun wl-draft-config-sub-x-face (content)
1958 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
1959 (fboundp 'x-face-insert)) ; x-face.el is installed.
1960 (x-face-insert content)
1961 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
1963 (defsubst wl-draft-config-sub-func (field content)
1965 (if (setq func (assq field wl-draft-config-sub-func-alist))
1966 (let (wl-draft-config-variables)
1967 (funcall (cdr func) content)
1968 ;; for wl-draft-config-sub-template
1969 (cons t wl-draft-config-variables)))))
1971 (defsubst wl-draft-config-exec-sub (clist)
1972 (let (config local-variables)
1974 (setq config (car clist))
1979 (let ((field (car config))
1980 (content (cdr config))
1984 (wl-draft-replace-field field (eval content) t))
1985 ((setq ret-val (wl-draft-config-sub-func field content))
1986 (if (cdr ret-val) ;; for wl-draft-config-sub-template
1987 (wl-append local-variables (cdr ret-val))))
1988 ((boundp field) ;; variable
1989 (make-local-variable field)
1990 (set field (eval content))
1991 (wl-append local-variables (list field)))
1993 (error "%s: not variable" field)))))
1995 (error "%s: not supported type" config)))
1996 (setq clist (cdr clist)))
1999 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
2000 "Change headers in draft preparation time."
2002 (unless wl-draft-reedit
2005 (and (boundp 'wl-draft-prepared-config-alist)
2006 wl-draft-prepared-config-alist) ;; For compatible.
2007 wl-draft-config-alist)))
2009 (wl-draft-config-exec config-alist reply-buf)))))
2011 (defun wl-draft-config-exec (&optional config-alist reply-buf)
2012 "Change headers in draft sending time."
2014 (let ((case-fold-search t)
2015 (alist (or config-alist wl-draft-config-alist))
2016 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2017 wl-draft-reply-buffer)))
2018 (local-variables wl-draft-config-variables)
2020 (when (and (or (interactive-p)
2021 wl-draft-config-exec-flag)
2026 (setq key (caar alist)
2033 (set-buffer reply-buf)
2035 (std11-narrow-to-header)
2036 (goto-char (point-min))
2037 (re-search-forward (car clist) nil t))))
2038 (wl-draft-config-exec-sub (cdr clist))
2041 (when (save-restriction
2042 (std11-narrow-to-header mail-header-separator)
2043 (goto-char (point-min))
2044 (re-search-forward key nil t))
2045 (wl-append local-variables
2046 (wl-draft-config-exec-sub clist))
2049 (wl-append local-variables
2050 (wl-draft-config-exec-sub clist))
2052 (if (and found wl-draft-config-matchone)
2054 (setq alist (cdr alist))))
2056 (setq wl-draft-config-exec-flag nil))
2057 (run-hooks 'wl-draft-config-exec-hook)
2058 (put-text-property (point-min)(point-max) 'face nil)
2059 (wl-highlight-message (point-min)(point-max) t)
2060 (setq wl-draft-config-variables
2061 (elmo-uniq-list local-variables))))))
2063 (defun wl-draft-replace-field (field content &optional add)
2066 (let ((case-fold-search t)
2067 (inhibit-read-only t) ;; added by teranisi.
2069 (std11-narrow-to-header mail-header-separator)
2070 (goto-char (point-min))
2071 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2076 (re-search-forward "^[^ \t]" nil 'move)
2078 (skip-chars-backward "\n")
2079 (delete-region beg (point))
2080 (insert " " content))
2085 (re-search-forward "^[^ \t]" nil 'move)
2087 (delete-region beg (point)))
2088 (when (and add content)
2090 (goto-char (point-max))
2091 (insert (concat field ": " content "\n"))))))))
2093 (defun wl-draft-config-info-operation (msg operation)
2094 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-folder-get-elmo-folder
2098 (format "%s-%d" wl-draft-config-save-filename msg)
2100 element alist variable)
2102 ((eq operation 'save)
2103 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2104 (while (setq variable (pop variables))
2105 (when (boundp variable)
2107 (list (cons variable (eval variable))))))
2108 (elmo-object-save filename alist)))
2109 ((eq operation 'load)
2110 (setq alist (elmo-object-load filename))
2111 (while (setq element (pop alist))
2112 (set (make-local-variable (car element)) (cdr element))
2113 (wl-append wl-draft-config-variables (list (car element)))))
2114 ((eq operation 'delete)
2115 (if (file-exists-p filename)
2116 (delete-file filename))))))
2118 (defun wl-draft-queue-info-operation (msg operation
2119 &optional add-sent-message-via)
2120 (let* ((msgdb-dir (elmo-folder-msgdb-path
2121 (wl-folder-get-elmo-folder wl-queue-folder)))
2124 (format "%s-%d" wl-draft-queue-save-filename msg)
2126 element alist variable)
2128 ((eq operation 'save)
2129 (let ((variables (elmo-uniq-list
2130 (append wl-draft-queue-save-variables
2131 wl-draft-config-variables
2132 (list 'wl-draft-fcc-list)))))
2133 (if add-sent-message-via
2135 (push 'wl-sent-message-queued variables)
2136 (push 'wl-sent-message-via variables)))
2137 (while (setq variable (pop variables))
2138 (when (boundp variable)
2140 (list (cons variable (eval variable))))))
2141 (elmo-object-save filename alist)))
2142 ((eq operation 'load)
2143 (setq alist (elmo-object-load filename))
2144 (while (setq element (pop alist))
2145 (set (make-local-variable (car element)) (cdr element))))
2146 ((eq operation 'get-sent-via)
2147 (setq alist (elmo-object-load filename))
2148 (cdr (assq 'wl-sent-message-via alist)))
2149 ((eq operation 'delete)
2150 (if (file-exists-p filename)
2151 (delete-file filename))))))
2153 (defun wl-draft-queue-append (wl-sent-message-via)
2154 (if wl-draft-verbose-send
2155 (message "Queuing..."))
2156 (let ((send-buffer (current-buffer))
2157 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2158 (message-id (std11-field-body "Message-ID")))
2159 (if (elmo-folder-append-buffer folder t)
2161 (wl-draft-queue-info-operation
2162 (car (elmo-folder-status folder))
2163 'save wl-sent-message-via)
2164 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2165 (when wl-draft-verbose-send
2166 (setq wl-draft-verbose-msg "Queuing...")
2167 (message "Queuing...done")))
2168 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2169 (error "Queuing failed"))))
2171 (defun wl-draft-queue-flush ()
2172 "Flush draft queue."
2174 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2176 (elmo-folder-open-internal queue-folder)
2177 (elmo-folder-list-messages queue-folder)))
2180 (wl-draft-queue-flushing t)
2181 msgs failure len buffer msgid sent-via)
2182 ;; get plugged send message
2184 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2187 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2188 (or (not (nth 2 (car sent-via)))
2190 (car (nth 2 (car sent-via)))
2191 (cdr (nth 2 (car sent-via))))))
2192 (wl-append msgs (list (car msgs2)))
2194 (setq sent-via (cdr sent-via))))
2195 (setq msgs2 (cdr msgs2)))
2196 (when (> (setq len (length msgs)) 0)
2197 (if (elmo-y-or-n-p (format
2198 "%d message(s) are in the sending queue. Send now? "
2200 (not elmo-dop-flush-confirm) t)
2203 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2206 ;; reset buffer local variables
2207 (kill-all-local-variables)
2211 (setq wl-sent-message-via nil)
2212 (wl-draft-queue-info-operation (car msgs) 'load)
2213 (elmo-message-fetch queue-folder
2215 (elmo-make-fetch-strategy 'entire)
2216 nil (current-buffer))
2218 (setq failure (funcall
2219 wl-draft-queue-flush-send-function
2220 (format "Sending (%d/%d)..." i len)))
2221 ;;; (wl-draft-raw-send nil nil
2222 ;;; (format "Sending (%d/%d)..." i len))
2224 (elmo-display-error err t)
2228 (if (eq wl-sent-message-modified 'requeue)
2230 (elmo-folder-delete-messages
2231 queue-folder (cons (car msgs) nil))
2232 (wl-draft-queue-info-operation (car msgs) 'delete))
2234 (elmo-folder-delete-messages
2235 queue-folder (cons (car msgs) nil))
2236 (wl-draft-queue-info-operation (car msgs) 'delete)
2237 (setq performed (+ 1 performed))))
2238 (setq msgs (cdr msgs)))
2239 (kill-buffer buffer)
2240 (message "%d message(s) are sent." performed)))
2241 (message "%d message(s) are remained to be sent." len))
2242 (elmo-folder-close queue-folder)
2245 (defun wl-jump-to-draft-buffer (&optional arg)
2246 "Jump to the draft if exists."
2249 (wl-jump-to-draft-folder)
2250 (let ((draft-bufs (wl-collect-draft))
2254 (message "No draft buffer exist."))
2257 (sort (mapcar 'buffer-name draft-bufs)
2258 (function (lambda (a b)
2259 (not (string< a b))))))
2260 (if (setq buf (cdr (member (buffer-name)
2262 (setq buf (car buf))
2263 (setq buf (car draft-bufs)))
2264 (switch-to-buffer buf))))))
2266 (defun wl-jump-to-draft-folder ()
2267 (let ((msgs (reverse (elmo-folder-list-messages (wl-folder-get-elmo-folder
2269 (mybuf (buffer-name))
2272 (message "No draft message exist.")
2273 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2274 (setq msg (cadr (memq
2275 (string-to-int (substring mybuf (match-end 0)))
2277 (or msg (setq msg (car msgs)))
2278 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2279 (switch-to-buffer buf)
2280 (wl-draft-reedit msg)))))
2282 (defun wl-draft-highlight-and-recenter (&optional n)
2284 (if wl-highlight-body-too
2285 (let ((beg (point-min))
2287 (put-text-property beg end 'face nil)
2288 (wl-highlight-message beg end t)))
2291 ;;;; user-agent support by Sen Nagata
2293 ;; this appears to be necessarily global...
2294 (defvar wl-user-agent-compose-p nil)
2295 (defvar wl-user-agent-headers-and-body-alist nil)
2297 ;; this should be a generic function for mail-mode -- i wish there was
2298 ;; something like it in sendmail.el
2299 (defun wl-user-agent-insert-header (header-name header-value)
2300 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2301 ;; it seems like overriding existing headers is acceptable -- should
2302 ;; we provide an option?
2304 ;; plan was: unfold header (might be folded), remove existing value, insert
2306 ;; wl doesn't seem to fold header lines yet anyway :-)
2308 (let ((kill-whole-line t)
2310 (mail-position-on-field (capitalize header-name))
2311 (setq end-of-line (point))
2313 (re-search-forward ":" end-of-line)
2314 (insert (concat " " header-value "\n"))
2317 ;; this should be a generic function for mail-mode -- i wish there was
2318 ;; something like it in sendmail.el
2320 ;; ** haven't dealt w/ case where the body is already set **
2321 (defun wl-user-agent-insert-body (body-text)
2322 "Insert a body of text, BODY-TEXT, into a message."
2323 ;; code defensively... :-P
2324 (goto-char (point-min))
2325 (search-forward mail-header-separator)
2330 (defun wl-user-agent-compose (&optional to subject other-headers continue
2331 switch-function yank-action
2333 "Support the `compose-mail' interface for wl.
2334 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2335 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2336 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2338 (unless (featurep 'wl)
2340 ;; protect these -- to and subject get bound at some point, so it looks
2341 ;; to be necessary to protect the values used w/in
2342 (let ((wl-user-agent-headers-and-body-alist other-headers)
2343 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2344 (wl-draft-reply-buffer-style 'split))
2345 (when (eq switch-function 'switch-to-buffer-other-window)
2346 (when (one-window-p t)
2347 (if (window-minibuffer-p) (other-window 1))
2351 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2354 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2357 (setq wl-user-agent-headers-and-body-alist
2358 (cons (cons "to" to)
2359 wl-user-agent-headers-and-body-alist))))
2361 (if (wl-string-match-assoc "subject"
2362 wl-user-agent-headers-and-body-alist
2365 (wl-string-match-assoc "subject"
2366 wl-user-agent-headers-and-body-alist
2369 (setq wl-user-agent-headers-and-body-alist
2370 (cons (cons "subject" subject)
2371 wl-user-agent-headers-and-body-alist))))
2372 ;; i think this is what we want to use...
2375 ;; tell the hook-function to do its stuff
2376 (setq wl-user-agent-compose-p t)
2377 ;; because to get the hooks working, wl-draft has to think it has
2378 ;; been called interactively
2379 (call-interactively 'wl-draft))
2380 (setq wl-user-agent-compose-p nil))))
2382 (defun wl-user-agent-compose-internal ()
2383 "Manipulate headers and/or a body of a draft message."
2384 ;; being called from wl-user-agent-compose?
2385 (if wl-user-agent-compose-p
2388 (let ((headers wl-user-agent-headers-and-body-alist)
2389 (case-fold-search t))
2392 (if (not (string-match "^body$" (car (car headers))))
2393 (wl-user-agent-insert-header
2394 (car (car headers)) (cdr (car headers)))
2396 (setq headers (cdr headers))))
2397 ;; highlight headers (from wl-draft in wl-draft.el)
2398 (wl-highlight-headers 'for-draft)
2400 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2402 (wl-user-agent-insert-body
2403 (cdr (wl-string-match-assoc
2405 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2409 (product-provide (provide 'wl-draft) (require 'wl-version))
2411 ;;; wl-draft.el ends here