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))
39 (require 'eword-encode)
42 (defvar x-face-add-x-face-version-header)
43 (defvar mail-reply-buffer)
44 (defvar mail-from-style)
50 (defalias-maybe 'x-face-insert 'ignore)
51 (defalias-maybe 'x-face-insert-version-header 'ignore)
52 (defalias-maybe 'wl-init 'ignore)
53 (defalias-maybe 'wl-draft-mode 'ignore))
56 (autoload 'wl-addrmgr "wl-addrmgr"))
58 (autoload 'open-ssl-stream "ssl")
60 (defvar wl-draft-buffer-message-number nil)
61 (defvar wl-draft-field-completion-list nil)
62 (defvar wl-draft-verbose-send t)
63 (defvar wl-draft-verbose-msg nil)
64 (defvar wl-draft-queue-flushing nil)
65 (defvar wl-draft-config-variables nil)
66 (defvar wl-draft-config-exec-flag t)
67 (defvar wl-draft-buffer-cur-summary-buffer nil)
68 (defvar wl-draft-clone-local-variable-regexp "^\\(wl\\|mime\\)")
69 (defvar wl-draft-sendlog-filename "sendlog")
70 (defvar wl-draft-queue-save-filename "qinfo")
71 (defvar wl-draft-config-save-filename "config")
72 (defvar wl-draft-queue-flush-send-function 'wl-draft-dispatch-message)
73 (defvar wl-sent-message-via nil)
74 (defvar wl-sent-message-modified nil)
75 (defvar wl-sent-message-queued nil)
76 (defvar wl-draft-fcc-list nil)
77 (defvar wl-draft-reedit nil)
78 (defvar wl-draft-reply-buffer nil)
79 (defvar wl-draft-forward nil)
80 (defvar wl-draft-doing-mime-bcc nil)
82 (defvar wl-draft-parent-folder nil
83 "Folder name of the summary in which current draft is invoked.
84 This variable is local in each draft buffer.
85 You can refer its value in `wl-draft-config-alist'.
88 \(setq wl-draft-config-alist
89 '(((string-match \".*@domain1$\" wl-draft-parent-folder)
90 (\"From\" . \"user@domain1\"))
91 ((string-match \".*@domain2$\" wl-draft-parent-folder)
92 (\"From\" . \"user@domain2\"))))")
94 (defvar wl-draft-parent-number nil)
95 (defvar wl-draft-parent-flag nil)
97 (defconst wl-draft-parent-variables
98 '(wl-draft-parent-folder
99 wl-draft-parent-number
100 wl-draft-parent-flag))
102 (defvar wl-draft-config-sub-func-alist
103 '((body . wl-draft-config-sub-body)
104 (top . wl-draft-config-sub-top)
105 (bottom . wl-draft-config-sub-bottom)
106 (header . wl-draft-config-sub-header)
107 (header-top . wl-draft-config-sub-header-top)
108 (header-bottom . wl-draft-config-sub-header)
109 (part-top . wl-draft-config-sub-part-top)
110 (part-bottom . wl-draft-config-sub-part-bottom)
111 (body-file . wl-draft-config-sub-body-file)
112 (top-file . wl-draft-config-sub-top-file)
113 (bottom-file . wl-draft-config-sub-bottom-file)
114 (header-file . wl-draft-config-sub-header-file)
115 (template . wl-draft-config-sub-template)
116 (x-face . wl-draft-config-sub-x-face)))
118 (make-variable-buffer-local 'wl-draft-buffer-message-number)
119 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
120 (make-variable-buffer-local 'wl-draft-config-variables)
121 (make-variable-buffer-local 'wl-draft-config-exec-flag)
122 (make-variable-buffer-local 'wl-sent-message-via)
123 (make-variable-buffer-local 'wl-sent-message-queued)
124 (make-variable-buffer-local 'wl-draft-fcc-list)
125 (make-variable-buffer-local 'wl-draft-reply-buffer)
126 (make-variable-buffer-local 'wl-draft-parent-folder)
127 (make-variable-buffer-local 'wl-draft-parent-number)
128 (make-variable-buffer-local 'wl-draft-parent-flag)
130 (defvar wl-draft-folder-internal nil
131 "Internal variable for caching `opened' draft folder.")
133 (defsubst wl-smtp-password-key (user mechanism server)
134 (format "SMTP:%s/%s@%s"
135 user mechanism server))
137 (defmacro wl-smtp-extension-bind (&rest body)
138 `(let* ((smtp-sasl-mechanisms
139 (if wl-smtp-authenticate-type
141 (if (listp wl-smtp-authenticate-type)
142 wl-smtp-authenticate-type
143 (list wl-smtp-authenticate-type)))))
144 (smtp-use-sasl (and smtp-sasl-mechanisms t))
145 (smtp-use-starttls (eq wl-smtp-connection-type 'starttls))
146 (smtp-open-connection-function
147 (if (eq wl-smtp-connection-type 'ssl)
149 smtp-open-connection-function))
150 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
151 (setq smtp-sasl-user-name wl-smtp-posting-user
152 smtp-sasl-properties (when wl-smtp-authenticate-realm
154 wl-smtp-authenticate-realm)))
155 (setq sasl-read-passphrase
159 (wl-smtp-password-key
161 (car smtp-sasl-mechanisms)
165 (defun wl-draft-insert-date-field ()
167 (insert "Date: " (wl-make-date-string) "\n"))
169 (defun wl-draft-insert-from-field ()
171 ;; Put the "From:" field in unless for some odd reason
172 ;; they put one in themselves.
175 (setq from (wl-draft-eword-encode-address-list wl-from))
176 (error (error "Please look at `wl-from' again")))
177 (insert "From: " from "\n")))
179 (defun wl-draft-insert-x-face-field ()
180 "Insert X-Face header."
182 (if (not (file-exists-p wl-x-face-file))
183 (error "File %s does not exist" wl-x-face-file)
184 (goto-char (point-min))
185 (search-forward mail-header-separator nil t)
187 (wl-draft-insert-x-face-field-here)
188 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
190 (defun wl-draft-insert-x-face-field-here ()
191 "Insert X-Face field at point."
192 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
193 (when (string-match "^\\(X-Face:\\)?[ \t\n]*" x-face-string)
194 (setq x-face-string (substring x-face-string (match-end 0))))
195 (insert "X-Face: " x-face-string))
196 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
198 ;; Insert X-Face-Version: field
199 (when (and (fboundp 'x-face-insert-version-header)
200 (boundp 'x-face-add-x-face-version-header)
201 x-face-add-x-face-version-header)
202 (x-face-insert-version-header)))
204 (defun wl-draft-setup ()
205 (let ((field wl-draft-fields)
209 (list (cons (concat (car field) " ")
210 (concat (car field) " ")))))
211 (setq field (cdr field)))
213 (cons (cons (concat wl-draft-mime-bcc-field-name ": ")
214 (concat wl-draft-mime-bcc-field-name ": "))
216 (setq wl-draft-field-completion-list cl)
217 (setq wl-address-complete-header-regexp
219 (append wl-address-complete-header-list
220 (list (concat wl-draft-mime-bcc-field-name ":")))))))
222 (defun wl-draft-make-mail-followup-to (recipients)
223 (let ((rlist (wl-address-delete-user-mail-addresses recipients)))
224 (if (elmo-list-member rlist (mapcar 'downcase
225 wl-subscribed-mailing-list))
227 (append rlist (list (wl-address-header-extract-address
230 (defun wl-draft-delete-myself-from-cc (to cc)
231 (cond (wl-draft-always-delete-myself ; always-delete option
232 (wl-address-delete-user-mail-addresses cc))
233 ((elmo-list-member (append to cc) ; subscribed mailing-list
234 (mapcar 'downcase wl-subscribed-mailing-list))
235 (wl-address-delete-user-mail-addresses cc))
238 (defsubst wl-draft-strip-subject-regexp (subject regexp)
239 "Remove REGEXP from SUBJECT string."
240 (if (string-match regexp subject)
241 (substring subject (match-end 0))
244 (defun wl-draft-forward-make-subject (original-subject)
245 "Generate subject string for forwarding."
246 (cond ((functionp wl-forward-subject-prefix)
247 (concat (funcall wl-forward-subject-prefix)
249 ((stringp wl-forward-subject-prefix)
250 (concat wl-forward-subject-prefix
251 (wl-draft-strip-subject-regexp
252 (or original-subject "")
253 wl-subject-forward-prefix-regexp)))
254 (t original-subject)))
256 (defun wl-draft-reply-make-subject (original-subject)
257 "Generate subject string for replying."
258 (cond ((functionp wl-reply-subject-prefix)
259 (concat (funcall wl-reply-subject-prefix)
261 ((stringp wl-reply-subject-prefix)
262 (concat wl-reply-subject-prefix
263 (wl-draft-strip-subject-regexp
264 (or original-subject "")
265 wl-subject-re-prefix-regexp)))
266 (t original-subject)))
268 (defun wl-draft-forward (original-subject summary-buf &optional number)
269 (let (references parent-folder subject)
270 (with-current-buffer summary-buf
271 (setq parent-folder (wl-summary-buffer-folder-name)))
272 (let ((decoder (mime-find-field-decoder 'Subject 'plain)))
273 (setq subject (if (and original-subject decoder)
274 (funcall decoder original-subject) original-subject)))
275 (with-current-buffer (wl-message-get-original-buffer)
276 (setq subject (wl-draft-forward-make-subject subject))
277 (setq references (nconc
278 (std11-field-bodies '("References" "In-Reply-To"))
279 (list (std11-field-body "Message-Id"))))
280 (setq references (delq nil references)
281 references (mapconcat 'identity references " ")
282 references (wl-draft-parse-msg-id-list-string references)
283 references (wl-delete-duplicates references)
284 references (when references
285 (mapconcat 'identity references "\n\t"))))
286 (and wl-draft-use-frame
287 (get-buffer-window summary-buf)
288 (select-window (get-buffer-window summary-buf)))
289 (wl-draft (list (cons 'To "")
290 (cons 'Subject subject)
291 (cons 'References references))
292 nil nil nil nil parent-folder number))
293 (goto-char (point-max))
294 (wl-draft-insert-message)
295 (mail-position-on-field "To")
296 (setq wl-draft-config-variables
297 (append wl-draft-parent-variables
298 wl-draft-config-variables))
299 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save)
300 (run-hooks 'wl-draft-forward-hook))
302 (defun wl-draft-self-reply-p ()
303 "Return t when From address in the current message is user's self one or not."
304 (wl-address-user-mail-address-p (or (elmo-field-body "From") "")))
306 (defun wl-draft-find-reply-headers (rule-symbol)
307 (let ((rule-list (symbol-value rule-symbol))
308 condition-match-p result)
309 (setq condition-match-p
311 (cond ((stringp condition)
312 (std11-field-body condition))
313 ((functionp condition)
316 (and (funcall condition-match-p (car condition))
317 (funcall condition-match-p (cdr condition))))
320 (error "Unkown condition in `%s'" rule-symbol)))))
321 (while (and (null result) rule-list)
322 (let ((rule (car rule-list)))
323 (when (funcall condition-match-p (car rule))
324 (setq result (cdr rule)))
325 (setq rule-list (cdr rule-list))))
328 (defun wl-draft-reply (buf with-arg summary-buf &optional number)
329 "Create draft for replying to the message in buffer BUF.
330 Recipients are prepared along `wl-draft-reply-without-argument-list',
331 or `wl-draft-reply-with-argument-list' if WITH-ARG argument is non-nil."
333 (let ((rule-list (if with-arg
334 'wl-draft-reply-with-argument-list
335 'wl-draft-reply-without-argument-list))
337 to mail-followup-to cc subject in-reply-to references newsgroups
338 to-alist cc-alist decoder parent-folder)
339 (when (buffer-live-p summary-buf)
340 (with-current-buffer summary-buf
341 (setq parent-folder (wl-summary-buffer-folder-name))))
342 (set-buffer (or buf mime-mother-buffer))
344 (or (wl-draft-find-reply-headers rule-list)
345 (error "No match field: check your `%s'" rule-list)))
346 (let ((r-to-list (nth 0 reply-headers))
347 (r-cc-list (nth 1 reply-headers))
348 (r-ng-list (nth 2 reply-headers)))
349 (setq to (wl-concat-list
351 (if (functionp r-to-list)
353 (elmo-multiple-fields-body-list r-to-list))
354 (and (member "Followup-To" r-ng-list)
355 (string= (std11-field-body "Followup-To") "poster")
357 (setq r-ng-list (delete "Followup-To"
358 (copy-sequence r-ng-list)))
359 (elmo-multiple-fields-body-list '("From")))))
361 (setq cc (wl-concat-list
362 (if (functionp r-cc-list)
364 (elmo-multiple-fields-body-list r-cc-list))
366 (setq newsgroups (wl-concat-list
367 (if (functionp r-ng-list)
369 (std11-field-bodies r-ng-list))
371 (setq subject (std11-field-body "Subject"))
372 (setq to (wl-parse-addresses to)
373 cc (wl-parse-addresses cc))
374 (with-temp-buffer ; to keep raw buffer unibyte.
375 (set-buffer-multibyte default-enable-multibyte-characters)
376 (setq decoder (mime-find-field-decoder 'Subject 'plain))
377 (setq subject (if (and subject decoder)
378 (funcall decoder subject) subject))
382 (setq decoder (mime-find-field-decoder 'To 'plain))
383 (cons (nth 1 (std11-extract-address-components addr))
384 (if decoder (funcall decoder addr) addr)))
389 (setq decoder (mime-find-field-decoder 'Cc 'plain))
390 (cons (nth 1 (std11-extract-address-components addr))
391 (if decoder (funcall decoder addr) addr)))
393 (setq subject (wl-draft-reply-make-subject subject))
394 (setq in-reply-to (std11-field-body "Message-Id"))
395 (setq references (nconc
396 (std11-field-bodies '("References" "In-Reply-To"))
398 (setq to (delq nil (mapcar 'car to-alist)))
399 (setq cc (delq nil (mapcar 'car cc-alist)))
400 ;; if subscribed mailing list is contained in cc or to
401 ;; and myself is contained in cc,
402 ;; delete myself from cc.
403 (setq cc (wl-draft-delete-myself-from-cc to cc))
404 (when wl-insert-mail-followup-to
405 (setq mail-followup-to
406 (wl-draft-make-mail-followup-to (append to cc)))
407 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
408 (with-temp-buffer ; to keep raw buffer unibyte.
409 (set-buffer-multibyte default-enable-multibyte-characters)
410 (setq newsgroups (wl-parse newsgroups
411 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
412 newsgroups (wl-delete-duplicates newsgroups)
417 (setq decoder (mime-find-field-decoder 'Newsgroups 'plain))
418 (if decoder (funcall decoder grp) grp))
420 (setq to (wl-delete-duplicates to nil t))
421 (setq cc (wl-delete-duplicates
422 (append (wl-delete-duplicates cc nil t)
423 to (copy-sequence to))
425 (and to (setq to (mapconcat
427 (if wl-draft-reply-use-address-with-full-name
428 (or (cdr (assoc addr to-alist)) addr)
431 (and cc (setq cc (mapconcat
433 (if wl-draft-reply-use-address-with-full-name
434 (or (cdr (assoc addr cc-alist)) addr)
437 (and mail-followup-to
438 (setq mail-followup-to
441 (if wl-draft-reply-use-address-with-full-name
442 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
444 mail-followup-to ",\n\t")))
445 (and (null to) (setq to cc cc nil))
446 (setq references (delq nil references)
447 references (mapconcat 'identity references " ")
448 references (wl-draft-parse-msg-id-list-string references)
449 references (wl-delete-duplicates references)
450 references (if references
451 (mapconcat 'identity references "\n\t")))
452 (and wl-draft-use-frame
453 (get-buffer-window summary-buf)
454 (select-window (get-buffer-window summary-buf)))
455 (wl-draft (list (cons 'To to)
457 (cons 'Newsgroups newsgroups)
458 (cons 'Subject subject)
459 (cons 'In-Reply-To in-reply-to)
460 (cons 'References references)
461 (cons 'Mail-Followup-To mail-followup-to))
462 nil nil nil nil parent-folder number)
463 (setq wl-draft-reply-buffer buf)
464 (setq wl-draft-config-variables
465 (append wl-draft-parent-variables
466 wl-draft-config-variables))
467 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save))
468 (run-hooks 'wl-draft-reply-hook))
470 (defun wl-draft-reply-position (position)
471 (cond ((eq position 'body)
472 (wl-draft-body-goto-top))
473 ((eq position 'bottom)
474 (wl-draft-body-goto-bottom))
476 (goto-char (point-min)))
477 ((and (stringp position)
478 (std11-field-body position))
479 (progn (mail-position-on-field position)
480 (wl-draft-beginning-of-line)))
482 (while (car position)
483 (wl-draft-reply-position (car position))
484 (setq position (cdr position))))))
486 (defun wl-draft-add-references ()
487 (wl-draft-add-in-reply-to "References"))
489 (defun wl-draft-add-in-reply-to (&optional alt-field)
490 (let* ((mes-id (with-current-buffer mail-reply-buffer
491 (std11-field-body "message-id")))
492 (field (or alt-field "In-Reply-To"))
493 (ref (std11-field-body field))
494 (ref-list nil) (st nil))
495 (when (and mes-id ref)
496 (while (string-match "<[^>]+>" ref st)
498 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
501 (member mes-id ref-list))
505 (when (mail-position-on-field field)
507 (while (looking-at "^[ \t]")
509 (setq mes-id (concat "\t" mes-id "\n")))
513 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
514 &optional ignored-fields)
517 (narrow-to-region (point)(point))
520 (with-current-buffer mail-reply-buffer
522 (decode-mime-charset-region (point-min) (point-max)
524 (buffer-substring-no-properties
525 (point-min) (point-max)))))
527 (goto-char (point-min))
528 (wl-draft-delete-fields ignored-fields))
529 (goto-char (point-max))
530 (push-mark (point) nil t)
531 (goto-char (point-min)))
533 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
534 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
535 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
536 (run-hooks 'wl-draft-cited-hook)
537 (when (if wl-draft-add-references
538 (wl-draft-add-references)
539 (if wl-draft-add-in-reply-to
540 (wl-draft-add-in-reply-to)))
541 (wl-highlight-headers 'for-draft)) ; highlight when added References:
542 (when wl-highlight-body-too
543 (wl-highlight-body-region beg (point-max)))))
545 (defun wl-message-news-p ()
546 "If exist valid Newsgroups field, return non-nil."
547 (std11-field-body "Newsgroups"))
549 (defun wl-message-field-exists-p (field)
550 "If FIELD exist and FIELD value is not empty, return non-nil."
551 (let ((value (std11-field-body field)))
553 (not (string= value "")))))
555 (defun wl-message-mail-p ()
556 "If exist To, Cc or Bcc field, return non-nil."
557 (or (wl-message-field-exists-p "To")
558 (wl-message-field-exists-p "Resent-to")
559 (wl-message-field-exists-p "Cc")
560 (wl-message-field-exists-p "Bcc")
561 (wl-message-field-exists-p wl-draft-mime-bcc-field-name)
562 ;;; This may be needed..
563 ;;; (wl-message-field-exists-p "Fcc")
566 (defun wl-draft-edit-string (string)
567 (let ((cur-buf (current-buffer))
568 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
569 to subject in-reply-to cc references newsgroups mail-followup-to
570 content-type content-transfer-encoding from
575 (setq to (std11-field-body "To"))
578 (decode-mime-charset-string
581 (setq subject (std11-field-body "Subject"))
582 (setq subject (and subject
584 (decode-mime-charset-string
587 (setq from (std11-field-body "From")
590 (decode-mime-charset-string
593 (setq in-reply-to (std11-field-body "In-Reply-To"))
594 (setq cc (std11-field-body "Cc"))
597 (decode-mime-charset-string
600 (setq references (std11-field-body "References"))
601 (setq newsgroups (std11-field-body "Newsgroups"))
602 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
603 (setq content-type (std11-field-body "Content-Type"))
604 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
605 (goto-char (point-min))
606 (or (re-search-forward "\n\n" nil t)
607 (search-forward (concat mail-header-separator "\n") nil t))
612 (if (wl-address-user-mail-address-p from) from))
615 (cons 'Subject subject)
616 (cons 'Newsgroups newsgroups)
617 (cons 'Mail-Followup-To mail-followup-to)
618 (cons 'In-Reply-To in-reply-to)
619 (cons 'References references))
620 content-type content-transfer-encoding
621 (buffer-substring (point) (point-max))
623 (kill-buffer tmp-buf))
624 ;; Set cursor point to the top.
625 (goto-char (point-min))
626 (search-forward (concat mail-header-separator "\n") nil t)
627 (run-hooks 'wl-draft-reedit-hook)
628 (and to (mail-position-on-field "To"))))
630 (defun wl-draft-insert-current-message (dummy)
632 (let (original-buffer
634 mail-citation-hook mail-yank-hooks
635 wl-draft-add-references wl-draft-add-in-reply-to
636 wl-draft-cite-function)
637 (if (and wl-draft-buffer-cur-summary-buffer
638 (with-current-buffer wl-draft-buffer-cur-summary-buffer
639 (and wl-message-buffer
640 (with-current-buffer wl-message-buffer
641 (setq original-buffer (wl-message-get-original-buffer))
642 (not (zerop (with-current-buffer original-buffer
645 (setq mail-reply-buffer original-buffer)
646 (wl-draft-yank-from-mail-reply-buffer
648 wl-ignored-forwarded-headers))
649 (when (string= (mime-make-tag "message" "rfc822")
650 (buffer-substring-no-properties (point-at-bol 0)(point-at-eol 0)))
651 (delete-region (point-at-bol 0) (1+ (point-at-eol 0))))
652 (error "No current message"))))
654 (defun wl-draft-insert-get-message (dummy)
655 (let ((fld (completing-read
657 (if (memq 'read-folder wl-use-folder-petname)
658 (wl-folder-get-entity-with-petname)
659 wl-folder-entity-hashtb)
660 nil nil wl-default-spec
661 'wl-read-folder-history))
662 (number (call-interactively
663 (function (lambda (num)
664 (interactive "nNumber: ")
666 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
667 mail-citation-hook mail-yank-hooks
668 wl-draft-cite-function)
671 (with-current-buffer mail-reply-buffer
673 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
676 (elmo-make-fetch-strategy 'entire)))
677 (wl-draft-yank-from-mail-reply-buffer nil))
678 (kill-buffer mail-reply-buffer))))
681 ;; default body citation func
683 (defun wl-default-draft-cite ()
684 (let ((mail-yank-ignored-headers "[^:]+:")
685 (mail-yank-prefix "> ")
686 date from cite-title)
688 (if (< (mark t) (point))
689 (exchange-point-and-mark))
690 (narrow-to-region (point)(point-max))
691 (setq date (std11-field-body "date")
692 from (std11-field-body "from")))
694 (insert (format "At %s,\n%s wrote:\n"
695 (or date "some time ago")
696 (if wl-default-draft-cite-decorate-author
697 (funcall wl-summary-from-function
700 (mail-indent-citation)))
702 (defvar wl-draft-buffer nil "Draft buffer to yank content.")
703 (defun wl-draft-yank-to-draft-buffer (buffer)
704 "Yank BUFFER content to `wl-draft-buffer'."
705 (set-buffer wl-draft-buffer)
706 (let ((mail-reply-buffer buffer))
707 (wl-draft-yank-from-mail-reply-buffer nil)
708 (kill-buffer buffer)))
710 (defun wl-draft-yank-original (&optional arg)
711 "Yank original message."
714 (let ((draft-buffer (current-buffer))
719 (setq mail-reply-buffer (current-buffer))
720 (with-current-buffer draft-buffer
721 (wl-draft-yank-from-mail-reply-buffer nil))))
722 (wl-draft-yank-current-message-entity)))
724 (defun wl-draft-hide (editing-buffer)
725 "Hide the editing draft buffer if possible."
726 (when (and editing-buffer
727 (buffer-live-p editing-buffer)
728 (get-buffer-window editing-buffer))
729 (select-window (get-buffer-window editing-buffer))
730 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
731 fld-buf sum-win fld-win)
732 (if (and wl-draft-use-frame
733 (> (length (visible-frame-list)) 1))
739 ;; stay folder window if required
740 (when wl-stay-folder-window
741 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
742 (if (setq fld-win (get-buffer-window fld-buf))
743 (select-window fld-win)
744 (if wl-draft-resume-folder-window ;; resume folder window
745 (switch-to-buffer fld-buf)))))
746 (if (buffer-live-p sum-buf)
747 (if (setq sum-win (get-buffer-window sum-buf t))
748 ;; if Summary is on the frame, select it.
749 (select-window sum-win)
750 ;; if summary is not on the frame, switch to it.
751 (if (and wl-stay-folder-window
752 (or wl-draft-resume-folder-window fld-win))
753 (wl-folder-select-buffer sum-buf)
754 (switch-to-buffer sum-buf))))))))
756 (defun wl-draft-delete (editing-buffer)
757 "Kill the editing draft buffer and delete the file corresponds to it."
759 (with-current-buffer editing-buffer
760 (when wl-draft-buffer-message-number
761 (elmo-folder-delete-messages (wl-draft-get-folder)
763 wl-draft-buffer-message-number))
764 (wl-draft-config-info-operation wl-draft-buffer-message-number
766 (set-buffer-modified-p nil) ; force kill
767 (kill-buffer editing-buffer))))
769 (defun wl-draft-kill (&optional force-kill)
770 "Kill current draft buffer and quit editing."
773 (when (and (or (eq major-mode 'wl-draft-mode)
774 (eq major-mode 'mail-mode))
776 (yes-or-no-p "Kill Current Draft? ")))
777 (let ((cur-buf (current-buffer)))
778 (run-hooks 'wl-draft-kill-pre-hook)
779 (wl-draft-hide cur-buf)
780 (wl-draft-delete cur-buf)))
783 (defun wl-draft-fcc ()
784 "Add a new Fcc field, with file name completion."
786 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
787 (mail-position-on-field "to"))
790 ;; Imported from message.el.
791 (defun wl-draft-elide-region (b e)
792 "Elide the text in the region.
793 An ellipsis (from `wl-draft-elide-ellipsis') will be inserted where the
797 (insert wl-draft-elide-ellipsis))
799 ;; Imported from message.el.
800 (defun wl-draft-beginning-of-line (&optional n)
801 "Move point to beginning of header value or to beginning of line."
803 (let ((zrs 'zmacs-region-stays))
804 (when (and (interactive-p) (boundp zrs))
806 (if (wl-draft-point-in-header-p)
807 (let* ((here (point))
808 (bol (progn (beginning-of-line n) (point)))
809 (eol (line-end-position))
810 (eoh (and (looking-at "[^ \t]")
811 (re-search-forward ": *" eol t))))
812 (if (and eoh (or (> here eoh) (= here bol)))
815 (beginning-of-line n)))
817 (defun wl-draft-point-in-header-p ()
818 "Return t if point is in the header."
821 (goto-char (point-min))
822 (not (re-search-forward
823 (concat "^" (regexp-quote mail-header-separator) "\n")
826 ;; function for wl-sent-message-via
828 (defmacro wl-draft-sent-message-p (type)
829 `(eq (nth 1 (assq ,type wl-sent-message-via)) 'sent))
831 (defmacro wl-draft-set-sent-message (type result &optional server-port)
832 `(let ((element (assq ,type wl-sent-message-via)))
834 (unless (eq (nth 1 element) ,result)
835 (setcdr element (list ,result ,server-port))
836 (setq wl-sent-message-modified t))
837 (push (list ,type ,result ,server-port) wl-sent-message-via)
838 (setq wl-sent-message-modified t))))
840 (defun wl-draft-sent-message-results ()
841 (let ((results wl-sent-message-via)
842 unplugged-via sent-via)
844 (cond ((eq (nth 1 (car results)) 'unplugged)
845 (push (caar results) unplugged-via))
846 ((eq (nth 1 (car results)) 'sent)
847 (push (caar results) sent-via)))
848 (setq results (cdr results)))
849 (list unplugged-via sent-via)))
851 (defun wl-draft-write-sendlog (status proto server to id)
852 "Write send log file, if `wl-draft-sendlog' is non-nil."
853 (when wl-draft-sendlog
855 (let* ((filename (expand-file-name wl-draft-sendlog-filename
856 elmo-msgdb-directory))
857 (filesize (nth 7 (file-attributes filename)))
858 (server (if server (concat " server=" server) ""))
860 ((memq proto '(fcc queue))
861 (format " folder=\"%s\"" to))
863 (format " ng=<%s>" to))
868 (mapcar '(lambda(x) (format "<%s>" x)) to)
871 (id (if id (concat " id=" id) ""))
872 (time (format-time-string "%Y/%m/%d %T")))
873 (insert (format "%s proto=%s stat=%s%s%s%s\n"
874 time proto status server to id))
875 (if (and wl-draft-sendlog-max-size filesize
876 (> filesize wl-draft-sendlog-max-size))
877 (rename-file filename (concat filename ".old") t))
878 (if (file-writable-p filename)
879 (write-region-as-binary (point-min) (point-max)
881 (message "%s is not writable." filename))))))
883 (defun wl-draft-get-header-delimiter (&optional delete)
884 ;; If DELETE is non-nil, replace the header delimiter with a blank line
886 (goto-char (point-min))
887 (when (re-search-forward
888 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
892 (setq delimline (point-marker)))
895 (defun wl-draft-send-mail-with-qmail ()
896 "Pass the prepared message buffer to qmail-inject.
897 Refer to the documentation for the variable `send-mail-function'
898 to find out how to use this."
899 (if (and wl-draft-qmail-send-plugged
900 (not (elmo-plugged-p)))
901 (wl-draft-set-sent-message 'mail 'unplugged)
903 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
904 (let ((id (std11-field-body "Message-ID"))
905 (to (std11-field-body "To")))
909 'call-process-region 1 (point-max) wl-qmail-inject-program
911 wl-qmail-inject-args))
912 ;; qmail-inject doesn't say anything on it's stdout/stderr,
913 ;; we have to look at the retval instead
915 (wl-draft-set-sent-message 'mail 'sent)
916 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
917 (1 (error "`qmail-inject' reported permanent failure"))
918 (111 (error "`qmail-inject' reported transient failure"))
919 ;; should never happen
920 (t (error "`qmail-inject' reported unknown failure"))))))
922 (defun wl-draft-parse-msg-id-list-string (string)
923 "Get msg-id list from STRING."
925 (dolist (parsed-id (std11-parse-msg-ids-string string))
926 (when (eq (car parsed-id) 'msg-id)
927 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
929 (nreverse msg-id-list)))
931 (defun wl-draft-eword-encode-address-list (string &optional column)
932 "Encode header field STRING as list of address, and return the result.
933 Cause an error when STRING contains invalid address.
934 Optional argument COLUMN is start-position of the field."
935 (car (eword-encode-rword-list
936 (or column eword-encode-default-start-column)
937 (eword-encode-addresses-to-rword-list
938 (wl-draft-std11-parse-addresses (std11-lexical-analyze string))))))
940 (defun wl-draft-std11-parse-addresses (lal)
941 (let ((ret (std11-parse-address lal)))
942 (when (and (not (and (eq (length lal) 1)
943 (eq (car (car lal)) 'spaces)))
945 (error "Error while parsing address"))
947 (let ((dest (list (car ret))))
949 (while (and (setq ret (std11-parse-ascii-token lal))
950 (string-equal (cdr (assq 'specials (car ret))) ",")
951 (setq ret (std11-parse-address (cdr ret)))
953 (setq dest (cons (car ret) dest))
954 (setq lal (cdr ret)))
955 (while (eq 'spaces (car (car lal)))
956 (setq lal (cdr lal)))
957 (if lal (error "Error while parsing address"))
960 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
961 "Get mailbox list of FIELD from current buffer.
962 The buffer is expected to be narrowed to just the headers of the message.
963 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
964 from current buffer."
966 (let ((case-fold-search t)
967 (inhibit-read-only t)
969 mailbox-list beg seq has-group-list)
970 (goto-char (point-min))
971 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
974 (re-search-forward "^[^ \t]" nil 'move)
976 (skip-chars-backward "\n")
977 (setq seq (std11-lexical-analyze
978 (buffer-substring-no-properties beg (point))))
979 (setq addresses (wl-draft-std11-parse-addresses seq))
981 (cond ((eq (car (car addresses)) 'group)
982 (setq has-group-list t)
986 'std11-address-string
987 (nth 2 (car addresses))))))
988 ((eq (car (car addresses)) 'mailbox)
989 (setq address (nth 1 (car addresses)))
993 (std11-addr-to-string
994 (if (eq (car address) 'phrase-route-addr)
997 (setq addresses (cdr addresses)))
998 (when (and remove-group-list has-group-list)
999 (delete-region beg (point))
1000 (insert (wl-address-string-without-group-list-contents seq))))
1003 (defun wl-draft-deduce-address-list (buffer header-start header-end)
1004 "Get address list suitable for smtp RCPT TO:<address>.
1005 Group list content is removed if `wl-draft-remove-group-list-contents' is
1007 (let ((fields (if (and wl-draft-doing-mime-bcc
1008 wl-draft-disable-bcc-for-mime-bcc)
1010 '("to" "cc" "bcc")))
1011 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
1012 (case-fold-search t)
1016 (narrow-to-region header-start header-end)
1017 (goto-char (point-min))
1019 (if (re-search-forward "^resent-to[\t ]*:" nil t)
1020 (setq fields resent-fields)))
1024 (wl-draft-parse-mailbox-list
1026 wl-draft-remove-group-list-contents)))
1027 (setq fields (cdr fields)))
1033 (defun wl-draft-send-mail-with-smtp ()
1034 "Send the prepared message buffer with SMTP."
1036 (let* ((errbuf (if mail-interactive
1037 (generate-new-buffer " smtp errors")
1039 (case-fold-search t)
1040 (default-case-fold-search t)
1041 (sender (or wl-envelope-from
1042 (wl-address-header-extract-address wl-from)))
1043 (delimline (save-excursion
1044 (goto-char (point-min))
1046 (concat "^" (regexp-quote mail-header-separator)
1050 (or wl-smtp-posting-server smtp-server "localhost"))
1051 (smtp-service (or wl-smtp-posting-port smtp-service))
1052 (smtp-local-domain (or smtp-local-domain wl-local-domain))
1053 (id (std11-field-body "message-id"))
1055 (if (not (elmo-plugged-p smtp-server smtp-service))
1056 (wl-draft-set-sent-message 'mail 'unplugged
1057 (cons smtp-server smtp-service))
1060 ;; Instead of `smtp-deduce-address-list'.
1061 (setq recipients (wl-draft-deduce-address-list
1062 (current-buffer) (point-min) delimline))
1063 (unless recipients (error "No recipients"))
1064 ;; Insert an extra newline if we need it to work around
1065 ;; Sun's bug that swallows newlines.
1066 (goto-char (1+ delimline))
1067 (if (eval mail-mailer-swallows-blank-line)
1069 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1070 (if mail-interactive
1074 (wl-draft-delete-field "bcc" delimline)
1075 (wl-draft-delete-field "resent-bcc" delimline)
1076 (let (process-connection-type)
1079 (wl-smtp-extension-bind
1081 (smtp-send-buffer sender recipients (current-buffer))
1083 (wl-draft-write-sendlog 'failed 'smtp smtp-server
1085 (if (and (eq (car err) 'smtp-response-error)
1086 (= (nth 1 err) 535))
1088 (wl-smtp-password-key
1090 (car smtp-sasl-mechanisms)
1092 (signal (car err) (cdr err)))
1094 (wl-draft-write-sendlog 'uncertain 'smtp smtp-server
1096 (signal (car err) (cdr err)))))
1097 (wl-draft-set-sent-message 'mail 'sent)
1098 (wl-draft-write-sendlog
1099 'ok 'smtp smtp-server recipients id)))))
1100 (if (bufferp errbuf)
1101 (kill-buffer errbuf))))))
1103 (defun wl-draft-send-mail-with-pop-before-smtp ()
1104 "Send the prepared message buffer with POP-before-SMTP."
1105 (require 'elmo-pop3)
1109 :user (or wl-pop-before-smtp-user
1110 elmo-pop3-default-user)
1111 :server (or wl-pop-before-smtp-server
1112 elmo-pop3-default-server)
1113 :port (or wl-pop-before-smtp-port
1114 elmo-pop3-default-port)
1115 :auth (or wl-pop-before-smtp-authenticate-type
1116 elmo-pop3-default-authenticate-type)
1117 :stream-type (elmo-get-network-stream-type
1118 (or wl-pop-before-smtp-stream-type
1119 elmo-pop3-default-stream-type))))
1121 (condition-case error
1123 (setq session (elmo-pop3-get-session folder))
1124 (when session (elmo-network-close-session session)))
1126 (unless (string= (nth 1 error) "Unplugged")
1127 (signal (car error) (cdr error))))))
1128 (wl-draft-send-mail-with-smtp))
1130 (defun wl-draft-insert-required-fields (&optional force-msgid)
1131 "Insert Message-ID, Date, and From field.
1132 If FORCE-MSGID, insert message-id regardless of `wl-insert-message-id'."
1133 ;; Insert Message-Id field...
1134 (goto-char (point-min))
1135 (when (and (or force-msgid
1136 wl-insert-message-id)
1137 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
1138 (insert (concat "Message-ID: "
1139 (funcall wl-message-id-function)
1141 ;; Insert date field.
1142 (goto-char (point-min))
1143 (or (re-search-forward "^Date[ \t]*:" nil t)
1144 (wl-draft-insert-date-field))
1145 ;; Insert from field.
1146 (goto-char (point-min))
1147 (or (re-search-forward "^From[ \t]*:" nil t)
1148 (wl-draft-insert-from-field)))
1150 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
1151 "Send the message in the current buffer."
1153 (narrow-to-region (goto-char (point-min))
1154 (if (re-search-forward
1156 "^" (regexp-quote mail-header-separator) "$")
1160 (wl-draft-insert-required-fields)
1161 ;; ignore any blank lines in the header
1162 (while (progn (goto-char (point-min))
1163 (re-search-forward "\n[ \t]*\n\n*" nil t))
1164 (replace-match "\n"))
1165 (goto-char (point-min))
1166 (while (re-search-forward
1167 "^[^ \t\n:]+:[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n"
1169 (when (string= "" (match-string 1))
1170 (replace-match ""))))
1171 ;;; (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1172 (wl-draft-dispatch-message)
1173 (when kill-when-done
1174 ;; hide editing-buffer.
1175 (wl-draft-hide editing-buffer)
1176 ;; delete editing-buffer and its file.
1177 (wl-draft-delete editing-buffer)))
1179 (defun wl-draft-dispatch-message (&optional mes-string)
1180 "Send the message in the current buffer. Not modified the header fields."
1181 (let (delimline mime-bcc)
1182 (if (and wl-draft-verbose-send mes-string)
1183 (message "%s" mes-string))
1185 (setq delimline (wl-draft-get-header-delimiter t))
1186 (unless wl-draft-fcc-list
1187 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1189 (setq wl-sent-message-modified nil)
1192 (if (and (wl-message-mail-p)
1193 (not (wl-draft-sent-message-p 'mail)))
1194 (if (or (not (or wl-draft-force-queuing
1195 wl-draft-force-queuing-mail))
1196 (memq 'mail wl-sent-message-queued))
1198 (setq mime-bcc (wl-draft-mime-bcc-field))
1199 (funcall wl-draft-send-mail-function)
1200 (when (not (zerop (length mime-bcc)))
1201 (wl-draft-do-mime-bcc mime-bcc)))
1202 (push 'mail wl-sent-message-queued)
1203 (wl-draft-set-sent-message 'mail 'unplugged)))
1204 (if (and (wl-message-news-p)
1205 (not (wl-draft-sent-message-p 'news))
1206 (not (wl-message-field-exists-p "Resent-to")))
1207 (if (or (not (or wl-draft-force-queuing
1208 wl-draft-force-queuing-news))
1209 (memq 'news wl-sent-message-queued))
1210 (funcall wl-draft-send-news-function)
1211 (push 'news wl-sent-message-queued)
1212 (wl-draft-set-sent-message 'news 'unplugged))))
1213 (let* ((status (wl-draft-sent-message-results))
1214 (unplugged-via (car status))
1215 (sent-via (nth 1 status)))
1216 ;; If one sent, process fcc folder.
1217 (if (and sent-via wl-draft-fcc-list)
1219 (wl-draft-do-fcc (wl-draft-get-header-delimiter)
1221 (setq wl-draft-fcc-list nil)))
1222 (if wl-draft-use-cache
1223 (let ((id (std11-field-body "Message-ID"))
1224 (elmo-enable-disconnected-operation t))
1225 (elmo-file-cache-save (elmo-file-cache-get-path id)
1227 ;; If one unplugged, append queue.
1228 (when (and unplugged-via
1229 wl-sent-message-modified)
1230 (if wl-draft-enable-queuing
1232 (wl-draft-queue-append wl-sent-message-via)
1233 (setq wl-sent-message-modified 'requeue))
1234 (error "Unplugged")))
1235 (when wl-draft-verbose-send
1236 (if (and unplugged-via sent-via);; combined message
1238 (setq wl-draft-verbose-msg
1239 (format "Sending%s and Queuing%s..."
1240 sent-via unplugged-via))
1241 (message "%sdone" wl-draft-verbose-msg))
1245 (if sent-via "done" "failed"))))))))
1246 (not wl-sent-message-modified)) ;; return value
1248 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1249 "Force send current buffer as raw message."
1252 (let (wl-interactive-send
1253 ;;; wl-draft-verbose-send
1254 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1255 (wl-news-send-pre-hook (and force-pre-hook wl-news-send-pre-hook))
1258 (wl-draft-send kill-when-done mes-string))))
1260 (defun wl-draft-clone-local-variables ()
1261 (let ((locals (buffer-local-variables))
1264 (when (and (consp (car locals))
1266 (string-match wl-draft-clone-local-variable-regexp
1267 (symbol-name (car (car locals)))))
1268 (wl-append result (list (car (car locals)))))
1269 (setq locals (cdr locals)))
1272 (defcustom wl-draft-send-confirm-with-preview t
1273 "*Non-nil to invoke preview through confirmation of sending.
1274 This variable is valid when `wl-interactive-send' has non-nil value."
1278 (defun wl-draft-send-confirm ()
1282 (when wl-draft-send-confirm-with-preview
1283 (let (wl-draft-send-hook)
1284 (wl-draft-preview-message)))
1286 (goto-char (point-min)) ; to show recipients in header
1288 (if (functionp wl-draft-send-confirm-type)
1289 wl-draft-send-confirm-type
1291 (wl-y-or-n-p-with-scroll
1293 (eq wl-draft-send-confirm-type 'scroll-by-SPC/BS))))
1294 "Send current draft? ")))
1296 (when (and wl-draft-send-confirm-with-preview
1297 (eq major-mode 'mime-view-mode))
1298 (wl-mime-quit-preview))))
1300 (defun wl-draft-send (&optional kill-when-done mes-string)
1301 "Send current draft message.
1302 If KILL-WHEN-DONE is non-nil, current draft buffer is killed"
1304 ;; Don't call this explicitly.
1305 ;; Added to 'wl-draft-send-hook (by teranisi)
1306 ;; (wl-draft-config-exec)
1307 (run-hooks 'wl-draft-send-hook)
1308 (when (or (not wl-interactive-send)
1309 (wl-draft-send-confirm))
1310 (let ((send-mail-function 'wl-draft-raw-send)
1311 (editing-buffer (current-buffer))
1312 (sending-buffer (wl-draft-generate-clone-buffer
1313 " *wl-draft-sending-buffer*"
1314 (append wl-draft-config-variables
1315 (wl-draft-clone-local-variables))))
1316 (parent-flag wl-draft-parent-flag)
1317 (parent-number wl-draft-parent-number)
1318 (parent-folder wl-draft-parent-folder)
1319 (wl-draft-verbose-msg nil)
1323 (set-buffer sending-buffer)
1324 (if (and (not (wl-message-mail-p))
1325 (not (wl-message-news-p)))
1326 (error "No recipient is specified"))
1327 (expand-abbrev) ; for mail-abbrevs
1328 (let ((mime-header-encode-method-alist
1330 '((wl-draft-eword-encode-address-list
1331 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
1332 (if (boundp 'mime-header-encode-method-alist)
1333 (symbol-value 'mime-header-encode-method-alist)))))
1334 (run-hooks 'mail-send-hook) ; translate buffer
1337 (if wl-draft-verbose-send
1338 (message "%s" (or mes-string "Sending...")))
1339 ;; Set flag before send-function because
1340 ;; there's no need to change current mailbox at this time.
1341 ;; If flag is set after send-function, the current mailbox
1342 ;; might changed by Fcc.
1343 ;; It causes a huge loss in the IMAP folder.
1344 (when (and parent-flag parent-number
1345 (not (eq (length parent-folder) 0)))
1347 (wl-folder-set-persistent-mark
1348 parent-folder parent-number parent-flag)
1350 (message "Set mark (%s) failed" (symbol-name parent-flag)))))
1351 (funcall wl-draft-send-function editing-buffer kill-when-done)
1352 ;; Now perform actions on successful sending.
1353 (while mail-send-actions
1355 (apply (car (car mail-send-actions))
1356 (cdr (car mail-send-actions)))
1358 (setq mail-send-actions (cdr mail-send-actions)))
1359 (if wl-draft-verbose-send
1361 (or wl-draft-verbose-msg
1364 ;; kill sending buffer, anyway.
1365 (and (buffer-live-p sending-buffer)
1366 (kill-buffer sending-buffer))))))
1368 (defun wl-draft-mime-bcc-field ()
1369 "Return the MIME-Bcc field body. The field is deleted."
1370 (prog1 (std11-field-body wl-draft-mime-bcc-field-name)
1371 (wl-draft-delete-field wl-draft-mime-bcc-field-name)))
1373 (defun wl-draft-do-mime-bcc (field-body)
1374 "Send MIME-Bcc (Encapsulated blind carbon copy)."
1375 (let ((orig-from (mime-decode-field-body (std11-field-body "from")
1377 (orig-subj (mime-decode-field-body (or (std11-field-body "subject")
1380 (recipients (wl-parse-addresses field-body))
1381 (draft-buffer (current-buffer))
1383 (save-window-excursion
1384 (when (and (not wl-draft-doing-mime-bcc) ; To avoid infinite loop.
1385 (not (zerop (length field-body))))
1386 (let ((wl-draft-doing-mime-bcc t))
1387 (dolist (recipient recipients)
1388 (wl-draft-create-buffer)
1389 (wl-draft-create-contents
1390 (append `((From . ,orig-from)
1392 (Subject . ,(concat "A blind carbon copy ("
1395 (wl-draft-default-headers)))
1396 (wl-draft-insert-mail-header-separator)
1397 (wl-draft-prepare-edit)
1398 (goto-char (point-max))
1399 (insert (or wl-draft-mime-bcc-body
1400 "This is a blind carbon copy.")
1402 (mime-edit-insert-tag "message" "rfc822")
1403 (insert-buffer-substring draft-buffer)
1404 (let (wl-interactive-send)
1405 (wl-draft-send 'kill-when-done))))))))
1407 (defun wl-draft-save ()
1408 "Save current draft."
1410 (if (buffer-modified-p)
1412 (message "Saving...")
1413 (let ((msg (buffer-substring-no-properties (point-min) (point-max)))
1415 (when wl-draft-buffer-message-number
1416 (elmo-folder-delete-messages (wl-draft-get-folder)
1417 (list wl-draft-buffer-message-number))
1418 (wl-draft-config-info-operation wl-draft-buffer-message-number
1420 (elmo-folder-check (wl-draft-get-folder))
1421 ;; If no header separator, insert it.
1424 (goto-char (point-min))
1425 (unless (re-search-forward
1426 (concat "^" (regexp-quote mail-header-separator) "$")
1428 (goto-char (point-min))
1429 (if (re-search-forward "\n\n" nil t)
1430 (replace-match (concat "\n" mail-header-separator "\n"))
1431 (goto-char (point-max))
1432 (insert (if (eq (char-before) ?\n) "" "\n")
1433 mail-header-separator "\n")))
1434 (let ((mime-header-encode-method-alist
1436 '((eword-encode-unstructured-field-body
1437 . (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
1438 (if (boundp 'mime-header-encode-method-alist)
1439 (symbol-value 'mime-header-encode-method-alist)))))
1440 (mime-edit-translate-buffer))
1441 (wl-draft-get-header-delimiter t)
1443 (elmo-folder-next-message-number (wl-draft-get-folder)))
1444 (elmo-folder-append-buffer (wl-draft-get-folder)))
1445 (elmo-folder-check (wl-draft-get-folder))
1446 (elmo-folder-commit (wl-draft-get-folder))
1447 (setq wl-draft-buffer-message-number next-number)
1448 (rename-buffer (format "%s/%d" wl-draft-folder next-number) t)
1449 (setq buffer-file-name (buffer-name))
1450 (set-buffer-modified-p nil)
1451 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save)
1452 (message "Saving...done")))
1453 (message "(No changes need to be saved)")))
1455 (defun wl-draft-mimic-kill-buffer ()
1456 "Kill the current (draft) buffer with query."
1458 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1461 (if (or (not bufname)
1462 (string-equal bufname "")
1463 (string-equal bufname (buffer-name)))
1464 (let ((bufname (current-buffer)))
1465 (when (or (not (buffer-modified-p))
1467 (format "Buffer %s modified; kill anyway? " bufname)))
1468 (set-buffer-modified-p nil)
1469 (wl-draft-hide bufname)
1470 (kill-buffer bufname)))
1471 (kill-buffer bufname))))
1473 (defun wl-draft-save-and-exit ()
1474 "Save current draft and exit current draft mode."
1477 (let ((editing-buffer (current-buffer)))
1478 (wl-draft-hide editing-buffer)
1479 (kill-buffer editing-buffer)))
1481 (defun wl-draft-send-and-exit ()
1482 "Send current draft message and kill it."
1486 (defun wl-draft-send-from-toolbar ()
1488 (let ((wl-interactive-send t))
1489 (wl-draft-send-and-exit)))
1491 (defun wl-draft-delete-field (field &optional delimline replace)
1492 (wl-draft-delete-fields (regexp-quote field) delimline replace))
1494 (defun wl-draft-delete-fields (field &optional delimline replace)
1497 (goto-char (point-min))
1498 (if (search-forward "\n\n" nil t)
1499 (setq delimline (point))
1500 (setq delimline (point-max))))
1501 (narrow-to-region (point-min) delimline)
1502 (goto-char (point-min))
1503 (let ((regexp (concat "^" field ":"))
1504 (case-fold-search t))
1506 (if (looking-at regexp)
1512 (if (re-search-forward "^[^ \t]" nil t)
1513 (goto-char (match-beginning 0))
1516 (insert (concat field ": " replace "\n"))))
1518 (if (re-search-forward "^[^ \t]" nil t)
1519 (goto-char (match-beginning 0))
1522 (defun wl-draft-get-fcc-list (header-end)
1523 (if (and wl-draft-doing-mime-bcc
1524 wl-draft-disable-fcc-for-mime-bcc)
1526 (wl-draft-delete-field "fcc")
1529 (case-fold-search t))
1530 (or (markerp header-end) (error "HEADER-END must be a marker"))
1532 (goto-char (point-min))
1533 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1538 (buffer-substring-no-properties
1542 (skip-chars-backward " \t")
1545 (dolist (folder fcc-list)
1546 (wl-folder-confirm-existence
1547 (wl-folder-get-elmo-folder (eword-decode-string folder)))))
1548 (delete-region (match-beginning 0)
1549 (progn (forward-line 1) (point)))))
1552 (defcustom wl-draft-fcc-append-read-folder-history t
1553 "Non-nil to append fcc'ed folder to `wl-read-folder-history'."
1557 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1558 (let ((send-mail-buffer (current-buffer))
1559 (tembuf (generate-new-buffer " fcc output"))
1560 (case-fold-search t)
1562 (or (markerp header-end) (error "HEADER-END must be a marker"))
1565 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1568 ;; insert just the headers to avoid moving the gap more than
1569 ;; necessary (the message body could be arbitrarily huge.)
1570 (insert-buffer-substring send-mail-buffer 1 header-end)
1571 (wl-draft-insert-required-fields t)
1572 (goto-char (point-max))
1573 (insert-buffer-substring send-mail-buffer header-end)
1574 (let ((id (std11-field-body "Message-ID"))
1575 (elmo-enable-disconnected-operation t))
1577 (if (elmo-folder-append-buffer
1578 (wl-folder-get-elmo-folder
1579 (eword-decode-string (car fcc-list)))
1580 (and wl-fcc-force-as-read '(read)))
1581 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1582 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1583 (if (and wl-draft-fcc-append-read-folder-history
1584 (boundp 'wl-read-folder-history))
1585 (or (equal (car fcc-list) (car wl-read-folder-history))
1586 (setq wl-read-folder-history
1587 (append (list (car fcc-list)) wl-read-folder-history))))
1588 (setq fcc-list (cdr fcc-list)))))
1589 (kill-buffer tembuf)))
1591 (defun wl-draft-on-field-p ()
1594 (goto-char (point-min))
1595 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1602 (if (or (looking-at ".*,[ \t]?$")
1603 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1605 (let ((pos (point)))
1608 (if (looking-at "^[ \t]")
1610 (if (re-search-forward ":" pos t) nil t)))))))
1614 (defun wl-draft (&optional header-alist
1615 content-type content-transfer-encoding
1619 "Write and send mail/news message with Wanderlust."
1626 (wl-plugged-init t))
1628 (wl-init)) ; returns immediately if already initialized.
1630 (wl-start-save-drafts)
1631 (let (buffer header-alist-internal)
1632 (setq buffer (wl-draft-create-buffer parent-folder parent-number))
1633 (unless (cdr (assq 'From header-alist))
1635 (append (list (cons 'From wl-from)) header-alist)))
1636 (unless (cdr (assq 'To header-alist))
1641 (if (assq 'To header-alist)
1642 (setcdr (assq 'To header-alist) to)
1644 (append header-alist
1645 (list (cons 'To to))))))))
1646 (unless (cdr (assq 'Subject header-alist))
1647 (if (assq 'Subject header-alist)
1648 (setcdr (assq 'Subject header-alist) "")
1650 (append header-alist (list (cons 'Subject ""))))))
1651 (setq header-alist (append header-alist
1652 (wl-draft-default-headers)
1653 wl-draft-additional-header-alist
1654 (if body (list "" (cons 'Body body)))))
1655 (wl-draft-create-contents header-alist)
1657 (wl-draft-decode-body
1658 content-type content-transfer-encoding))
1659 (wl-draft-insert-mail-header-separator)
1660 (wl-draft-prepare-edit)
1662 (run-hooks 'wl-mail-setup-hook))
1663 (goto-char (point-min))
1664 (setq buffer-undo-list nil)
1665 (wl-user-agent-compose-internal) ;; user-agent
1668 (string= (cdr (assq 'To header-alist)) ""))
1669 (mail-position-on-field "To"))
1671 (goto-char (point-max))))
1674 (defun wl-draft-create-buffer (&optional parent-folder parent-number)
1675 (let* ((draft-folder (wl-draft-get-folder))
1677 (or (eq this-command 'wl-summary-reply)
1678 (eq this-command 'wl-summary-reply-with-citation)
1679 (eq this-command 'wl-summary-forward)
1680 (eq this-command 'wl-summary-target-mark-forward)
1681 (eq this-command 'wl-summary-target-mark-reply-with-citation)))
1682 (buffer (generate-new-buffer "*draft*"))) ; Just for initial name.
1684 ;; switch-buffer according to draft buffer style.
1685 (if wl-draft-use-frame
1686 (switch-to-buffer-other-frame buffer)
1687 (if reply-or-forward
1688 (case wl-draft-reply-buffer-style
1690 (split-window-vertically)
1692 (switch-to-buffer buffer))
1694 (switch-to-buffer buffer))
1696 (delete-other-windows)
1697 (switch-to-buffer buffer))
1699 (if (functionp wl-draft-reply-buffer-style)
1700 (funcall wl-draft-reply-buffer-style buffer)
1701 (error "Invalid value for wl-draft-reply-buffer-style"))))
1702 (case wl-draft-buffer-style
1704 (when (eq major-mode 'wl-summary-mode)
1705 (wl-summary-toggle-disp-msg 'off))
1706 (split-window-vertically)
1708 (switch-to-buffer buffer))
1710 (switch-to-buffer buffer))
1712 (delete-other-windows)
1713 (switch-to-buffer buffer))
1714 (t (if (functionp wl-draft-buffer-style)
1715 (funcall wl-draft-buffer-style buffer)
1716 (error "Invalid value for wl-draft-buffer-style"))))))
1718 (let (change-major-mode-hook)
1720 (set-buffer-multibyte t) ; draft buffer is always multibyte.
1721 (make-local-variable 'truncate-partial-width-windows)
1722 (setq truncate-partial-width-windows nil)
1723 (setq truncate-lines wl-draft-truncate-lines)
1724 (setq wl-sent-message-via nil)
1725 (setq wl-sent-message-queued nil)
1726 (setq wl-draft-config-exec-flag t)
1727 (setq wl-draft-parent-folder (or parent-folder ""))
1728 (setq wl-draft-parent-number parent-number)
1729 (or (eq this-command 'wl-folder-write-current-folder)
1730 (setq wl-draft-buffer-cur-summary-buffer
1731 (wl-summary-get-buffer parent-folder)))
1734 (defun wl-draft-create-contents (header-alist)
1735 "header-alist' sample
1736 '(function ;; funcall
1737 string ;; insert string
1738 (symbol . string) ;; insert symbol-value: string
1739 (symbol . function) ;; (funcall) and if it returns string,
1740 ;; insert symbol-value: string
1741 (symbol . nil) ;; do nothing
1744 (unless (eq major-mode 'wl-draft-mode)
1745 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1746 (let ((halist header-alist)
1751 ((functionp (car halist)) (funcall (car halist)))
1753 ((stringp (car halist)) (insert (car halist) "\n"))
1755 ((consp (car halist))
1756 (setq field (car (car halist)))
1757 (setq value (cdr (car halist)))
1761 ((eq field 'Body) ; body
1763 ((stringp value) (insert (symbol-name field) ": " value "\n"))
1765 (let ((value-return (funcall value)))
1766 (when (stringp value-return)
1767 (insert (symbol-name field) ": " value-return "\n"))))
1776 (setq halist (cdr halist)))))
1778 (defun wl-draft-prepare-edit ()
1779 (unless (eq major-mode 'wl-draft-mode)
1780 (error "`wl-draft-create-header' must be use in wl-draft-mode"))
1781 (let (change-major-mode-hook)
1782 (wl-draft-editor-mode)
1783 (static-when (boundp 'auto-save-file-name-transforms)
1784 (make-local-variable 'auto-save-file-name-transforms)
1785 (setq auto-save-file-name-transforms
1786 (cons (list (concat (regexp-quote wl-draft-folder)
1788 (concat (expand-file-name
1790 (elmo-folder-msgdb-path
1791 (wl-draft-get-folder)))
1793 auto-save-file-name-transforms)))
1794 (when wl-draft-write-file-function
1795 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
1796 (wl-draft-overload-functions)
1797 (wl-highlight-headers 'for-draft)
1799 (clear-visited-file-modtime)))
1801 (defun wl-draft-decode-header ()
1803 (std11-narrow-to-header)
1804 (wl-draft-decode-message-in-buffer)
1807 (defun wl-draft-decode-body (&optional content-type content-transfer-encoding)
1810 (std11-field-body "content-type")))
1811 (content-transfer-encoding
1812 (or content-transfer-encoding
1813 (std11-field-body "content-transfer-encoding")))
1816 (std11-narrow-to-header)
1817 (wl-draft-delete-field "content-type")
1818 (wl-draft-delete-field "content-transfer-encoding")
1819 (goto-char (point-max))
1820 (setq delimline (point-marker))
1822 (narrow-to-region delimline (point-max))
1823 (goto-char (point-min))
1825 (insert "Content-type: " content-type "\n"))
1826 (when content-transfer-encoding
1827 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1828 (wl-draft-decode-message-in-buffer)
1829 (goto-char (point-min))
1830 (unless (re-search-forward "^$" (point-at-eol) t)
1835 ;;; subroutine for wl-draft-create-contents
1836 ;;; must be used in wl-draft-mode
1837 (defun wl-draft-check-new-line ()
1838 (if (not (= (preceding-char) ?\n))
1841 (defsubst wl-draft-trim-ccs (cc)
1847 (null (and wl-draft-delete-myself-from-bcc-fcc
1849 (mapcar 'wl-address-header-extract-address
1851 (wl-parse-addresses (std11-field-body "To"))
1852 (wl-parse-addresses (std11-field-body "Cc"))))
1853 (mapcar 'downcase wl-subscribed-mailing-list)))))
1857 (defsubst wl-draft-default-headers ()
1859 (cons 'Mail-Reply-To (and wl-insert-mail-reply-to
1860 (wl-address-header-extract-address
1862 (cons 'User-Agent wl-generate-mailer-string-function)
1863 (cons 'Reply-To mail-default-reply-to)
1864 (cons 'Bcc (function
1867 (or wl-bcc (and mail-self-blind (user-login-name)))))))
1868 (cons 'Fcc (function
1870 (wl-draft-trim-ccs wl-fcc))))
1871 (cons 'Organization wl-organization)
1872 (and wl-auto-insert-x-face
1873 (file-exists-p wl-x-face-file)
1874 'wl-draft-insert-x-face-field-here) ;; allow nil
1875 mail-default-headers
1876 ;; check \n at th end of line for `mail-default-headers'
1877 'wl-draft-check-new-line
1880 (defun wl-draft-insert-mail-header-separator (&optional delimline)
1883 (goto-char delimline)
1884 (goto-char (point-min))
1885 (if (search-forward "\n\n" nil t)
1886 (delete-backward-char 1)
1887 (goto-char (point-max))))
1888 (wl-draft-check-new-line)
1889 (put-text-property (point)
1891 (insert mail-header-separator "\n")
1893 'category 'mail-header-separator)
1898 (defun wl-draft-elmo-nntp-send ()
1899 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1900 (elmo-nntp-default-user
1901 (or wl-nntp-posting-user elmo-nntp-default-user))
1902 (elmo-nntp-default-server
1903 (or wl-nntp-posting-server elmo-nntp-default-server))
1904 (elmo-nntp-default-port
1905 (or wl-nntp-posting-port elmo-nntp-default-port))
1906 (elmo-nntp-default-stream-type
1907 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type))
1908 (elmo-nntp-default-function wl-nntp-posting-function)
1910 (if (setq condition (cdr (elmo-string-matched-assoc
1911 (std11-field-body "Newsgroups")
1912 wl-nntp-posting-config-alist)))
1913 (if (stringp condition)
1914 (setq elmo-nntp-default-server condition)
1915 (while (car condition)
1916 (set (intern (format "elmo-nntp-default-%s"
1917 (symbol-name (caar condition))))
1919 (setq condition (cdr condition)))))
1920 (unless elmo-nntp-default-function
1921 (error "wl-draft-nntp-send: posting-function is nil"))
1922 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1923 (wl-draft-set-sent-message 'news 'unplugged
1924 (cons elmo-nntp-default-server
1925 elmo-nntp-default-port))
1926 (funcall elmo-nntp-default-function
1927 elmo-nntp-default-server (current-buffer))
1928 (wl-draft-set-sent-message 'news 'sent)
1929 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1930 (std11-field-body "Newsgroups")
1931 (std11-field-body "Message-ID")))))
1933 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1934 "Generate clone of current buffer named NAME."
1935 (let ((editing-buffer (current-buffer)))
1937 (set-buffer (generate-new-buffer name))
1940 (wl-draft-editor-mode)
1941 (insert-buffer-substring editing-buffer)
1943 (while local-variables
1944 (make-local-variable (car local-variables))
1945 (set (car local-variables)
1947 (set-buffer editing-buffer)
1948 (symbol-value (car local-variables))))
1949 (setq local-variables (cdr local-variables)))
1952 (defun wl-draft-remove-text-plain-tag ()
1953 "Remove text/plain tag of mime-edit."
1954 (when (string= (mime-make-text-tag "plain")
1955 (buffer-substring-no-properties (point-at-bol)(point-at-eol)))
1956 (delete-region (point-at-bol)(1+ (point-at-eol)))))
1958 (defun wl-draft-reedit (number)
1959 (let ((draft-folder (wl-draft-get-folder))
1962 buffer change-major-mode-hook body-top)
1963 (setq buffer (get-buffer-create (format "%s/%d" wl-draft-folder
1965 (if wl-draft-use-frame
1966 (switch-to-buffer-other-frame buffer)
1967 (switch-to-buffer buffer))
1969 (elmo-message-fetch draft-folder number (elmo-make-fetch-strategy 'entire))
1970 (elmo-delete-cr-buffer)
1971 (let ((mime-edit-again-ignored-field-regexp
1972 "^\\(Content-.*\\|Mime-Version\\):"))
1973 (wl-draft-decode-message-in-buffer))
1974 (setq body-top (wl-draft-insert-mail-header-separator))
1977 (make-local-variable 'truncate-partial-width-windows)
1978 (setq truncate-partial-width-windows nil)
1979 (setq truncate-lines wl-draft-truncate-lines)
1980 (setq wl-sent-message-via nil)
1981 (setq wl-sent-message-queued nil)
1982 (wl-draft-config-info-operation number 'load)
1983 (goto-char (point-min))
1984 (wl-draft-overload-functions)
1985 (wl-draft-editor-mode)
1986 (static-when (boundp 'auto-save-file-name-transforms)
1987 (make-local-variable 'auto-save-file-name-transforms)
1988 (setq auto-save-file-name-transforms
1989 (cons (list (concat (regexp-quote wl-draft-folder)
1991 (concat (expand-file-name
1993 (elmo-folder-msgdb-path
1994 (wl-draft-get-folder)))
1996 auto-save-file-name-transforms)))
1997 (setq buffer-file-name (buffer-name)
1998 wl-draft-buffer-message-number number)
1999 (unless wl-draft-parent-folder
2000 (setq wl-draft-parent-folder ""))
2001 (when wl-draft-write-file-function
2002 (add-hook 'local-write-file-hooks wl-draft-write-file-function))
2003 (wl-highlight-headers 'for-draft)
2004 (goto-char body-top)
2005 (run-hooks 'wl-draft-reedit-hook)
2006 (goto-char (point-max))
2009 (defmacro wl-draft-body-goto-top ()
2011 (goto-char (point-min))
2012 (if (re-search-forward mail-header-separator nil t)
2014 (goto-char (point-max)))))
2016 (defmacro wl-draft-body-goto-bottom ()
2017 `(goto-char (point-max)))
2019 (defmacro wl-draft-config-body-goto-header ()
2021 (goto-char (point-min))
2022 (if (re-search-forward mail-header-separator nil t)
2024 (goto-char (point-max)))))
2026 (defsubst wl-draft-config-sub-eval-insert (content &optional newline)
2027 (let (content-value)
2029 (stringp (setq content-value (eval content))))
2030 (insert content-value)
2031 (if newline (insert "\n")))))
2033 (defun wl-draft-config-sub-body (content)
2034 (wl-draft-body-goto-top)
2035 (delete-region (point) (point-max))
2036 (wl-draft-config-sub-eval-insert content))
2038 (defun wl-draft-config-sub-top (content)
2039 (wl-draft-body-goto-top)
2040 (wl-draft-config-sub-eval-insert content))
2042 (defun wl-draft-config-sub-bottom (content)
2043 (wl-draft-body-goto-bottom)
2044 (wl-draft-config-sub-eval-insert content))
2046 (defun wl-draft-config-sub-header (content)
2047 (wl-draft-config-body-goto-header)
2048 (wl-draft-config-sub-eval-insert content 'newline))
2050 (defun wl-draft-config-sub-header-top (content)
2051 (goto-char (point-min))
2052 (wl-draft-config-sub-eval-insert content 'newline))
2054 (defun wl-draft-config-sub-part-top (content)
2055 (goto-char (mime-edit-content-beginning))
2056 (wl-draft-config-sub-eval-insert content 'newline))
2058 (defun wl-draft-config-sub-part-bottom (content)
2059 (goto-char (mime-edit-content-end))
2060 (wl-draft-config-sub-eval-insert content 'newline))
2062 (defsubst wl-draft-config-sub-file (content)
2063 (let ((coding-system-for-read wl-cs-autoconv)
2064 (file (expand-file-name (eval content))))
2065 (if (file-exists-p file)
2066 (insert-file-contents file)
2067 (error "%s: no exists file" file))))
2069 (defun wl-draft-config-sub-body-file (content)
2070 (wl-draft-body-goto-top)
2071 (delete-region (point) (point-max))
2072 (wl-draft-config-sub-file content))
2074 (defun wl-draft-config-sub-top-file (content)
2075 (wl-draft-body-goto-top)
2076 (wl-draft-config-sub-file content))
2078 (defun wl-draft-config-sub-bottom-file (content)
2079 (wl-draft-body-goto-bottom)
2080 (wl-draft-config-sub-file content))
2082 (defun wl-draft-config-sub-header-file (content)
2083 (wl-draft-config-body-goto-header)
2084 (wl-draft-config-sub-file content))
2086 (defun wl-draft-config-sub-template (content)
2087 (setq wl-draft-config-variables
2088 (wl-template-insert (eval content))))
2090 (defun wl-draft-config-sub-x-face (content)
2091 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
2092 (fboundp 'x-face-insert)) ; x-face.el is installed.
2093 (x-face-insert content)
2094 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
2096 (defsubst wl-draft-config-sub-func (field content)
2098 (if (setq func (assq field wl-draft-config-sub-func-alist))
2099 (let (wl-draft-config-variables)
2100 (funcall (cdr func) content)
2101 ;; for wl-draft-config-sub-template
2102 (cons t wl-draft-config-variables)))))
2104 (defsubst wl-draft-config-exec-sub (clist)
2105 (let (config local-variables)
2107 (setq config (car clist))
2112 (let ((field (car config))
2113 (content (cdr config))
2117 (wl-draft-replace-field field (eval content) t))
2118 ((setq ret-val (wl-draft-config-sub-func field content))
2119 (if (cdr ret-val) ;; for wl-draft-config-sub-template
2120 (wl-append local-variables (cdr ret-val))))
2121 ((boundp field) ;; variable
2122 (make-local-variable field)
2123 (set field (eval content))
2124 (wl-append local-variables (list field)))
2126 (error "%s: not variable" field)))))
2128 (error "%s: not supported type" config)))
2129 (setq clist (cdr clist)))
2132 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
2133 "Change headers in draft preparation time."
2135 (unless wl-draft-reedit
2138 (and (boundp 'wl-draft-prepared-config-alist)
2139 wl-draft-prepared-config-alist) ;; For compatible.
2140 wl-draft-config-alist)))
2142 (wl-draft-config-exec config-alist reply-buf)))))
2144 (defun wl-draft-config-exec (&optional config-alist reply-buf)
2145 "Change headers according to the value of `wl-draft-config-alist'.
2146 Automatically applied in draft sending time."
2148 (let ((case-fold-search t)
2149 (alist (or config-alist wl-draft-config-alist))
2150 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
2151 wl-draft-reply-buffer)))
2152 (local-variables wl-draft-config-variables)
2154 (when (and (or (interactive-p)
2155 wl-draft-config-exec-flag)
2160 (setq key (caar alist)
2167 (set-buffer reply-buf)
2169 (std11-narrow-to-header)
2170 (goto-char (point-min))
2171 (re-search-forward (car clist) nil t))))
2172 (wl-draft-config-exec-sub (cdr clist))
2175 (when (save-restriction
2176 (std11-narrow-to-header mail-header-separator)
2177 (goto-char (point-min))
2178 (re-search-forward key nil t))
2179 (wl-append local-variables
2180 (wl-draft-config-exec-sub clist))
2183 (wl-append local-variables
2184 (wl-draft-config-exec-sub clist))
2186 (if (and found wl-draft-config-matchone)
2188 (setq alist (cdr alist)))))
2190 (setq wl-draft-config-exec-flag nil))
2191 (run-hooks 'wl-draft-config-exec-hook)
2192 (put-text-property (point-min)(point-max) 'face nil)
2193 (wl-highlight-message (point-min)(point-max) t)
2194 (setq wl-draft-config-variables
2195 (elmo-uniq-list local-variables)))))
2197 (defun wl-draft-replace-field (field content &optional add)
2200 (let ((case-fold-search t)
2201 (inhibit-read-only t) ;; added by teranisi.
2203 (std11-narrow-to-header mail-header-separator)
2204 (goto-char (point-min))
2205 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
2210 (re-search-forward "^[^ \t]" nil 'move)
2212 (skip-chars-backward "\n")
2213 (delete-region beg (point))
2214 (insert " " content))
2219 (re-search-forward "^[^ \t]" nil 'move)
2221 (delete-region beg (point)))
2222 (when (and add content)
2224 (goto-char (point-max))
2225 (insert (concat field ": " content "\n"))))))))
2227 (defsubst wl-draft-config-info-filename (number msgdb-dir)
2229 (format "%s-%d" wl-draft-config-save-filename number)
2232 (defun wl-draft-config-info-operation (msg operation)
2233 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-draft-get-folder)))
2234 (filename (wl-draft-config-info-filename msg msgdb-dir))
2235 element alist variable)
2237 ((eq operation 'save)
2238 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
2239 (while (setq variable (pop variables))
2240 (when (boundp variable)
2242 (list (cons variable (eval variable))))))
2243 (elmo-object-save filename alist)))
2244 ((eq operation 'load)
2245 (setq alist (elmo-object-load filename))
2246 (while (setq element (pop alist))
2247 (set (make-local-variable (car element)) (cdr element))
2248 (wl-append wl-draft-config-variables (list (car element)))))
2249 ((eq operation 'delete)
2250 (if (file-exists-p filename)
2251 (delete-file filename))))))
2253 (defun wl-draft-queue-info-operation (msg operation
2254 &optional add-sent-message-via)
2255 (let* ((msgdb-dir (elmo-folder-msgdb-path
2256 (wl-folder-get-elmo-folder wl-queue-folder)))
2259 (format "%s-%d" wl-draft-queue-save-filename msg)
2261 element alist variable)
2263 ((eq operation 'save)
2264 (let ((variables (elmo-uniq-list
2265 (append wl-draft-queue-save-variables
2266 wl-draft-config-variables
2267 (list 'wl-draft-fcc-list)))))
2268 (if add-sent-message-via
2270 (push 'wl-sent-message-queued variables)
2271 (push 'wl-sent-message-via variables)))
2272 (while (setq variable (pop variables))
2273 (when (boundp variable)
2275 (list (cons variable (eval variable))))))
2276 (elmo-object-save filename alist)))
2277 ((eq operation 'load)
2278 (setq alist (elmo-object-load filename))
2279 (while (setq element (pop alist))
2280 (set (make-local-variable (car element)) (cdr element))))
2281 ((eq operation 'get-sent-via)
2282 (setq alist (elmo-object-load filename))
2283 (cdr (assq 'wl-sent-message-via alist)))
2284 ((eq operation 'delete)
2285 (if (file-exists-p filename)
2286 (delete-file filename))))))
2288 (defun wl-draft-queue-append (wl-sent-message-via)
2289 (if wl-draft-verbose-send
2290 (message "Queuing..."))
2291 (let ((send-buffer (current-buffer))
2292 (folder (wl-folder-get-elmo-folder wl-queue-folder))
2293 (message-id (std11-field-body "Message-ID")))
2294 (if (elmo-folder-append-buffer folder)
2296 (wl-draft-queue-info-operation
2297 (car (elmo-folder-status folder))
2298 'save wl-sent-message-via)
2299 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
2300 (when wl-draft-verbose-send
2301 (setq wl-draft-verbose-msg "Queuing...")
2302 (message "Queuing...done")))
2303 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
2304 (error "Queuing failed"))))
2306 (defun wl-draft-queue-flush ()
2307 "Flush draft queue."
2309 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
2311 (elmo-folder-open-internal queue-folder)
2312 (elmo-folder-list-messages queue-folder)))
2315 (wl-draft-queue-flushing t)
2316 msgs failure len buffer msgid sent-via)
2317 ;; get plugged send message
2319 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
2322 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
2323 (or (not (nth 2 (car sent-via)))
2325 (car (nth 2 (car sent-via)))
2326 (cdr (nth 2 (car sent-via))))))
2327 (wl-append msgs (list (car msgs2)))
2329 (setq sent-via (cdr sent-via))))
2330 (setq msgs2 (cdr msgs2)))
2331 (when (> (setq len (length msgs)) 0)
2332 (if (elmo-y-or-n-p (format
2333 "%d message(s) are in the sending queue. Send now? "
2335 (not elmo-dop-flush-confirm) t)
2338 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
2341 ;; reset buffer local variables
2342 (kill-all-local-variables)
2346 (setq wl-sent-message-via nil)
2347 (wl-draft-queue-info-operation (car msgs) 'load)
2348 (elmo-message-fetch queue-folder
2350 (elmo-make-fetch-strategy 'entire))
2352 (setq failure (funcall
2353 wl-draft-queue-flush-send-function
2354 (format "Sending (%d/%d)..." i len)))
2355 ;;; (wl-draft-raw-send nil nil
2356 ;;; (format "Sending (%d/%d)..." i len))
2358 (elmo-display-error err t)
2362 (if (eq wl-sent-message-modified 'requeue)
2364 (elmo-folder-delete-messages
2365 queue-folder (cons (car msgs) nil))
2366 (wl-draft-queue-info-operation (car msgs) 'delete))
2368 (elmo-folder-delete-messages
2369 queue-folder (cons (car msgs) nil))
2370 (wl-draft-queue-info-operation (car msgs) 'delete)
2371 (setq performed (+ 1 performed))))
2372 (setq msgs (cdr msgs)))
2373 (kill-buffer buffer)
2374 (message "%d message(s) are sent." performed)))
2375 (message "%d message(s) are remained to be sent." len))
2376 (elmo-folder-close queue-folder)
2379 (defun wl-jump-to-draft-buffer (&optional arg)
2380 "Jump to the draft if exists."
2383 (wl-jump-to-draft-folder)
2384 (let ((draft-bufs (wl-collect-draft))
2388 (message "No draft buffer exist."))
2391 (sort (mapcar 'buffer-name draft-bufs)
2392 (function (lambda (a b)
2393 (not (string< a b))))))
2394 (if (setq buf (cdr (member (buffer-name)
2396 (setq buf (car buf))
2397 (setq buf (car draft-bufs)))
2398 (switch-to-buffer buf))))))
2400 (defun wl-jump-to-draft-folder ()
2401 (let ((msgs (reverse (elmo-folder-list-messages (wl-draft-get-folder))))
2402 (mybuf (buffer-name))
2405 (message "No draft message exist.")
2406 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
2407 (setq msg (cadr (memq
2408 (string-to-number (substring mybuf (match-end 0)))
2410 (or msg (setq msg (car msgs)))
2411 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
2412 (switch-to-buffer buf)
2413 (wl-draft-reedit msg)))))
2415 (defun wl-draft-highlight-and-recenter (&optional n)
2417 (when wl-highlight-body-too
2418 (let ((modified (buffer-modified-p)))
2421 (put-text-property (point-min) (point-max) 'face nil)
2422 (wl-highlight-message (point-min) (point-max) t))
2423 (set-buffer-modified-p modified))))
2424 (static-when (featurep 'xemacs)
2425 ;; Cope with one of many XEmacs bugs that `recenter' takes
2426 ;; a long time if there are a lot of invisible text lines.
2430 ;; insert element from history
2431 (defvar wl-draft-current-history-position nil)
2432 (defvar wl-draft-history-backup-word "")
2434 (defun wl-draft-previous-history-element (n)
2436 (let (bol history beg end prev new)
2437 (when (and (not (wl-draft-on-field-p))
2440 (goto-char (point-min))
2441 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
2445 (while (and (looking-at "^[ \t]")
2446 (not (= (point) (point-min))))
2449 ((looking-at wl-folder-complete-header-regexp)
2450 (and (boundp 'wl-read-folder-history)
2451 (setq history wl-read-folder-history)))
2452 ;; ((looking-at wl-address-complete-header-regexp)
2453 ;; (setq history .....))
2457 (setq bol (save-excursion (beginning-of-line) (point)))
2458 (cond ((and (or (eq last-command 'wl-draft-previous-history-element)
2459 (eq last-command 'wl-draft-next-history-element))
2460 wl-draft-current-history-position)
2462 (or (search-backward-regexp ",[ \t]*\\(.*\\)" bol t)
2463 (search-backward-regexp "^[ \t]\\(.*\\)" bol t)
2464 (search-backward-regexp "^[^ \t]*: \\(.*\\)" bol t))
2465 (setq prev (match-string 1))
2466 (goto-char (match-beginning 1))
2469 (>= (+ n wl-draft-current-history-position) 0))
2471 (<= (+ n wl-draft-current-history-position)
2474 (setq wl-draft-current-history-position
2475 (+ n wl-draft-current-history-position))
2477 (nth wl-draft-current-history-position
2478 (append (list wl-draft-history-backup-word)
2480 (delete-region beg end)
2484 (message "End of history; no next item"))
2486 (message "Beginning of history; no preceding item")))))
2489 (or (search-backward-regexp ",[ \t]*\\(.*\\)" bol t)
2490 (search-backward-regexp "^[ \t]\\(.*\\)" bol t)
2491 (search-backward-regexp "^[^ \t]*: \\(.*\\)" bol t)))
2493 (setq wl-draft-current-history-position 1)
2494 (setq wl-draft-history-backup-word (match-string 1))
2495 (delete-region (match-beginning 1) (match-end 1))
2496 (insert (car history)))
2498 (setq wl-draft-current-history-position nil))))))
2500 (defun wl-draft-next-history-element (n)
2502 (wl-draft-previous-history-element (- n)))
2504 ;;;; user-agent support by Sen Nagata
2506 ;; this appears to be necessarily global...
2507 (defvar wl-user-agent-compose-p nil)
2508 (defvar wl-user-agent-headers-and-body-alist nil)
2510 ;; this should be a generic function for mail-mode -- i wish there was
2511 ;; something like it in sendmail.el
2512 (defun wl-user-agent-insert-header (header-name header-value)
2513 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
2514 ;; it seems like overriding existing headers is acceptable -- should
2515 ;; we provide an option?
2517 ;; plan was: unfold header (might be folded), remove existing value, insert
2519 ;; wl doesn't seem to fold header lines yet anyway :-)
2521 (let ((kill-whole-line t)
2523 (mail-position-on-field (capitalize header-name))
2524 (setq end-of-line (point))
2526 (re-search-forward ":" end-of-line)
2527 (insert (concat " " header-value "\n"))
2530 ;; this should be a generic function for mail-mode -- i wish there was
2531 ;; something like it in sendmail.el
2533 ;; ** haven't dealt w/ case where the body is already set **
2534 (defun wl-user-agent-insert-body (body-text)
2535 "Insert a body of text, BODY-TEXT, into a message."
2536 ;; code defensively... :-P
2537 (goto-char (point-min))
2538 (search-forward mail-header-separator)
2543 (defun wl-user-agent-compose (&optional to subject other-headers continue
2544 switch-function yank-action
2546 "Support the `compose-mail' interface for wl.
2547 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
2548 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
2549 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
2551 (unless (featurep 'wl)
2554 (setq switch-function 'keep))
2555 ;; protect these -- to and subject get bound at some point, so it looks
2556 ;; to be necessary to protect the values used w/in
2557 (let ((wl-user-agent-headers-and-body-alist other-headers)
2558 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
2559 (wl-draft-buffer-style switch-function))
2561 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2564 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
2567 (setq wl-user-agent-headers-and-body-alist
2568 (cons (cons "to" to)
2569 wl-user-agent-headers-and-body-alist))))
2571 (if (wl-string-match-assoc "subject"
2572 wl-user-agent-headers-and-body-alist
2575 (wl-string-match-assoc "subject"
2576 wl-user-agent-headers-and-body-alist
2579 (setq wl-user-agent-headers-and-body-alist
2580 (cons (cons "subject" subject)
2581 wl-user-agent-headers-and-body-alist))))
2582 ;; i think this is what we want to use...
2585 ;; tell the hook-function to do its stuff
2586 (setq wl-user-agent-compose-p t)
2587 ;; because to get the hooks working, wl-draft has to think it has
2588 ;; been called interactively
2589 (call-interactively 'wl-draft))
2590 (setq wl-user-agent-compose-p nil))))
2592 (defun wl-user-agent-compose-internal ()
2593 "Manipulate headers and/or a body of a draft message."
2594 ;; being called from wl-user-agent-compose?
2595 (if wl-user-agent-compose-p
2598 (let ((headers wl-user-agent-headers-and-body-alist)
2599 (case-fold-search t))
2602 (if (not (string-match "^body$" (car (car headers))))
2603 (wl-user-agent-insert-header
2604 (car (car headers)) (cdr (car headers)))
2606 (setq headers (cdr headers))))
2607 ;; highlight headers (from wl-draft in wl-draft.el)
2608 (wl-highlight-headers 'for-draft)
2610 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2612 (wl-user-agent-insert-body
2613 (cdr (wl-string-match-assoc
2615 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2618 (defun wl-draft-setup-parent-flag (flag)
2619 "Setup a FLAG for parent message."
2620 (when (and (> (length wl-draft-parent-folder) 0)
2621 wl-draft-parent-number)
2622 (setq wl-draft-parent-flag flag)
2623 (wl-draft-config-info-operation wl-draft-buffer-message-number 'save)))
2625 (defun wl-draft-buffer-change-number (old-number new-number)
2626 (when (eq wl-draft-buffer-message-number old-number)
2627 (setq wl-draft-buffer-message-number new-number)
2628 (rename-buffer (format "%s/%d" wl-draft-folder new-number) t)
2629 (setq buffer-file-name (buffer-name))
2630 (set-buffer-modified-p nil)))
2632 (defun wl-draft-rename-saved-config (old-number new-number)
2633 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-draft-get-folder)))
2634 (old-name (wl-draft-config-info-filename old-number msgdb-dir))
2635 (new-name (wl-draft-config-info-filename new-number msgdb-dir)))
2636 (when (file-exists-p old-name)
2637 (rename-file old-name new-name 'ok-if-already-exists))))
2640 (product-provide (provide 'wl-draft) (require 'wl-version))
2642 ;;; wl-draft.el ends here