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))
52 (defvar wl-draft-buf-name "Draft")
53 (defvar wl-caesar-region-func nil)
54 (defvar wl-draft-cite-function 'wl-default-draft-cite)
55 (defvar wl-draft-buffer-file-name nil)
56 (defvar wl-draft-field-completion-list nil)
57 (defvar wl-draft-verbose-send t)
58 (defvar wl-draft-verbose-msg nil)
59 (defvar wl-draft-queue-flushing nil)
60 (defvar wl-draft-config-variables nil)
61 (defvar wl-draft-config-exec-flag t)
62 (defvar wl-draft-buffer-cur-summary-buffer nil)
63 (defvar wl-draft-clone-local-variable-regexp "^\\(wl\\|mime\\)")
64 (defvar wl-draft-sendlog-filename "sendlog")
65 (defvar wl-draft-queue-save-filename "qinfo")
66 (defvar wl-draft-config-save-filename "config")
67 (defvar wl-draft-queue-flush-send-function 'wl-draft-dispatch-message)
68 (defvar wl-sent-message-via nil)
69 (defvar wl-sent-message-modified nil)
70 (defvar wl-draft-fcc-list nil)
71 (defvar wl-draft-reedit nil)
72 (defvar wl-draft-reply-buffer nil)
73 (defvar wl-draft-forward nil)
75 (defvar wl-draft-config-sub-func-alist
76 '((body . wl-draft-config-sub-body)
77 (top . wl-draft-config-sub-top)
78 (bottom . wl-draft-config-sub-bottom)
79 (header . wl-draft-config-sub-header)
80 (body-file . wl-draft-config-sub-body-file)
81 (top-file . wl-draft-config-sub-top-file)
82 (bottom-file . wl-draft-config-sub-bottom-file)
83 (header-file . wl-draft-config-sub-header-file)
84 (template . wl-draft-config-sub-template)
85 (x-face . wl-draft-config-sub-x-face)))
87 (make-variable-buffer-local 'wl-draft-buffer-file-name)
88 (make-variable-buffer-local 'wl-draft-buffer-cur-summary-buffer)
89 (make-variable-buffer-local 'wl-draft-config-variables)
90 (make-variable-buffer-local 'wl-draft-config-exec-flag)
91 (make-variable-buffer-local 'wl-sent-message-via)
92 (make-variable-buffer-local 'wl-draft-fcc-list)
93 (make-variable-buffer-local 'wl-draft-reply-buffer)
95 (defmacro wl-smtp-extension-bind (&rest body)
96 (` (let* ((smtp-sasl-mechanisms
97 (if wl-smtp-authenticate-type
99 (if (listp wl-smtp-authenticate-type)
100 wl-smtp-authenticate-type
101 (list wl-smtp-authenticate-type)))))
102 (smtp-use-sasl (and smtp-sasl-mechanisms t))
103 (smtp-use-starttls wl-smtp-connection-type)
104 smtp-sasl-user-name smtp-sasl-properties sasl-read-passphrase)
105 (if (and (string= (car smtp-sasl-mechanisms) "DIGEST-MD5")
107 (string-match "^\\([^@]*\\)@\\([^@]*\\)"
108 wl-smtp-posting-user))
109 (setq smtp-sasl-user-name (match-string 1 wl-smtp-posting-user)
110 smtp-sasl-properties (list 'realm
111 (match-string 2 wl-smtp-posting-user)))
112 (setq smtp-sasl-user-name wl-smtp-posting-user
113 smtp-sasl-properties nil))
114 (setq sasl-read-passphrase
123 (defun wl-draft-insert-date-field ()
125 (insert "Date: " (wl-make-date-string) "\n"))
127 (defun wl-draft-insert-from-field ()
129 ;; Put the "From:" field in unless for some odd reason
130 ;; they put one in themselves.
131 (let* ((login (or user-mail-address (user-login-name)))
132 (fullname (user-full-name)))
133 (cond ((eq mail-from-style 'angles)
134 (insert "From: " fullname)
135 (let ((fullname-start (+ (point-min) (length "From: ")))
136 (fullname-end (point-marker)))
137 (goto-char fullname-start)
138 ;; Look for a character that cannot appear unquoted
139 ;; according to RFC 822.
140 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
143 ;; Quote fullname, escaping specials.
144 (goto-char fullname-start)
146 (while (re-search-forward "[\"\\]"
148 (replace-match "\\\\\\&" t))
150 (insert " <" login ">\n"))
151 ((eq mail-from-style 'parens)
152 (insert "From: " login " (")
153 (let ((fullname-start (point)))
155 (let ((fullname-end (point-marker)))
156 (goto-char fullname-start)
157 ;; RFC 822 says \ and nonmatching parentheses
158 ;; must be escaped in comments.
159 ;; Escape every instance of ()\ ...
160 (while (re-search-forward "[()\\]" fullname-end 1)
161 (replace-match "\\\\\\&" t))
162 ;; ... then undo escaping of matching parentheses,
163 ;; including matching nested parentheses.
164 (goto-char fullname-start)
165 (while (re-search-forward
166 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
168 (replace-match "\\1(\\3)" t)
169 (goto-char fullname-start))))
171 ((not mail-from-style)
172 (insert "From: " login "\n")))))
174 (defun wl-draft-insert-x-face-field ()
175 "Insert X-Face header."
177 (if (not (file-exists-p wl-x-face-file))
178 (error "File %s does not exist" wl-x-face-file)
179 (beginning-of-buffer)
180 (search-forward mail-header-separator nil t)
182 (wl-draft-insert-x-face-field-here)
183 (run-hooks 'wl-draft-insert-x-face-field-hook))) ; highlight it if you want.
185 (defun wl-draft-insert-x-face-field-here ()
186 "Insert X-Face field at point."
187 (let ((x-face-string (elmo-get-file-string wl-x-face-file)))
188 (when (string-match "^[ \t]*" x-face-string)
189 (setq x-face-string (substring x-face-string (match-end 0))))
190 (insert "X-Face: " x-face-string))
191 (when (not (= (preceding-char) ?\n)) ; for chomped (choped) x-face-string
193 ;; Insert X-Face-Version: field
194 (when (and (fboundp 'x-face-insert-version-header)
195 (boundp 'x-face-add-x-face-version-header)
196 x-face-add-x-face-version-header)
197 (x-face-insert-version-header)))
199 (defun wl-draft-setup ()
200 (let ((field wl-draft-fields)
203 (setq ret-val (append ret-val
204 (list (cons (concat (car field) " ")
205 (concat (car field) " ")))))
206 (setq field (cdr field)))
207 (setq wl-draft-field-completion-list ret-val)))
209 (defun wl-draft-make-mail-followup-to (recipients)
210 (if (elmo-list-member
211 (or wl-user-mail-address-list
212 (list (wl-address-header-extract-address wl-from)))
214 (let ((rlist (elmo-list-delete
215 (or wl-user-mail-address-list
216 (list (wl-address-header-extract-address wl-from)))
217 (copy-sequence recipients))))
218 (if (elmo-list-member rlist (mapcar 'downcase
219 wl-subscribed-mailing-list))
221 (append rlist (list (wl-address-header-extract-address
225 (defun wl-draft-delete-myself-from-cc (to cc)
226 (let ((myself (or wl-user-mail-address-list
227 (list (wl-address-header-extract-address wl-from)))))
228 (cond (wl-draft-always-delete-myself ; always-delete option
229 (elmo-list-delete myself cc))
230 ((elmo-list-member (append to cc) ; subscribed mailing-list
231 (mapcar 'downcase wl-subscribed-mailing-list))
232 (elmo-list-delete myself cc))
235 (defun wl-draft-forward (original-subject summary-buf)
237 (with-current-buffer (wl-message-get-original-buffer)
238 (setq references (nconc
239 (std11-field-bodies '("References" "In-Reply-To"))
240 (list (std11-field-body "Message-Id"))))
241 (setq references (delq nil references)
242 references (mapconcat 'identity references " ")
243 references (wl-draft-parse-msg-id-list-string references)
244 references (wl-delete-duplicates references)
245 references (when references
246 (mapconcat 'identity references "\n\t"))))
247 (wl-draft "" (concat "Forward: " original-subject)
248 nil nil references nil nil nil nil nil nil summary-buf))
249 (goto-char (point-max))
250 (wl-draft-insert-message)
251 (mail-position-on-field "To"))
253 (defun wl-draft-strip-subject-re (subject)
254 "Remove \"Re:\" from subject lines. Shamelessly copied from Gnus"
255 (if (string-match wl-subject-prefix-regexp subject)
256 (substring subject (match-end 0))
259 (defun wl-draft-reply-list-symbol (with-arg)
260 "Return symbol `wl-draft-reply-*-argument-list' match condition.
261 Check WITH-ARG and From: field."
262 (if (wl-address-user-mail-address-p (or (elmo-field-body "From") ""))
264 'wl-draft-reply-myself-with-argument-list
265 'wl-draft-reply-myself-without-argument-list)
267 'wl-draft-reply-with-argument-list
268 'wl-draft-reply-without-argument-list)))
270 (defun wl-draft-reply (buf with-arg summary-buf)
271 "Reply to BUF buffer message.
272 Reply to author if WITH-ARG is non-nil."
275 to mail-followup-to cc subject in-reply-to references newsgroups
276 from to-alist cc-alist decoder)
278 (setq r-list (symbol-value (wl-draft-reply-list-symbol with-arg)))
281 (when (let ((condition (car (car r-list))))
282 (cond ((stringp condition)
283 (std11-field-body condition))
287 (if (not (std11-field-body (car condition)))
289 (setq condition (cdr condition)))
292 (funcall condition))))
293 (let ((r-to-list (nth 0 (cdr (car r-list))))
294 (r-cc-list (nth 1 (cdr (car r-list))))
295 (r-ng-list (nth 2 (cdr (car r-list)))))
296 (when (and (member "Followup-To" r-ng-list)
297 (string= (std11-field-body "Followup-To") "poster"))
298 (setq r-to-list (cons "From" r-to-list))
299 (setq r-ng-list (delete "Followup-To" (copy-sequence r-ng-list))))
300 (setq to (wl-concat-list (cons to
301 (elmo-multiple-fields-body-list
304 (setq cc (wl-concat-list (cons cc
305 (elmo-multiple-fields-body-list
308 (setq newsgroups (wl-concat-list (cons newsgroups
313 (setq r-list (cdr r-list)))
314 (error "No match field: check your `%s'"
315 (symbol-name (wl-draft-reply-list-symbol with-arg))))
316 (setq subject (std11-field-body "Subject"))
317 (setq to (wl-parse-addresses to)
318 cc (wl-parse-addresses cc))
319 (with-temp-buffer ; to keep raw buffer unibyte.
320 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
321 (setq decoder (mime-find-field-decoder 'Subject 'plain))
322 (setq subject (if (and subject decoder)
323 (funcall decoder subject) subject))
327 (setq decoder (mime-find-field-decoder 'To 'plain))
328 (cons (nth 1 (std11-extract-address-components addr))
329 (if decoder (funcall decoder addr) addr)))
334 (setq decoder (mime-find-field-decoder 'Cc 'plain))
335 (cons (nth 1 (std11-extract-address-components addr))
336 (if decoder (funcall decoder addr) addr)))
338 (and wl-reply-subject-prefix
339 (setq subject (concat wl-reply-subject-prefix
340 (wl-draft-strip-subject-re
342 (setq in-reply-to (std11-field-body "Message-Id"))
343 (setq references (nconc
344 (std11-field-bodies '("References" "In-Reply-To"))
346 (setq to (delq nil (mapcar 'car to-alist)))
347 (setq cc (delq nil (mapcar 'car cc-alist)))
348 ;; if subscribed mailing list is contained in cc or to
349 ;; and myself is contained in cc,
350 ;; delete myself from cc.
351 (setq cc (wl-draft-delete-myself-from-cc to cc))
352 (when wl-insert-mail-followup-to
353 (setq mail-followup-to
354 (wl-draft-make-mail-followup-to (append to cc)))
355 (setq mail-followup-to (wl-delete-duplicates mail-followup-to nil t)))
356 (setq newsgroups (wl-parse newsgroups
357 "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
358 newsgroups (wl-delete-duplicates newsgroups)
359 newsgroups (if newsgroups (mapconcat 'identity newsgroups ",")))
360 (setq to (wl-delete-duplicates to nil t))
361 (setq cc (wl-delete-duplicates
362 (append (wl-delete-duplicates cc nil t)
363 to (copy-sequence to))
365 (and to (setq to (mapconcat
367 (if wl-draft-reply-use-address-with-full-name
368 (or (cdr (assoc addr to-alist)) addr)
371 (and cc (setq cc (mapconcat
373 (if wl-draft-reply-use-address-with-full-name
374 (or (cdr (assoc addr cc-alist)) addr)
377 (and mail-followup-to
378 (setq mail-followup-to
381 (if wl-draft-reply-use-address-with-full-name
382 (or (cdr (assoc addr (append to-alist cc-alist))) addr)
384 mail-followup-to ",\n\t")))
385 (and (null to) (setq to cc cc nil))
386 (setq references (delq nil references)
387 references (mapconcat 'identity references " ")
388 references (wl-draft-parse-msg-id-list-string references)
389 references (wl-delete-duplicates references)
390 references (if references
391 (mapconcat 'identity references "\n\t")))
393 to subject in-reply-to cc references newsgroups mail-followup-to
394 nil nil nil nil summary-buf)
395 (setq wl-draft-reply-buffer buf))
396 (run-hooks 'wl-reply-hook))
398 (defun wl-draft-add-references ()
399 (let* ((mes-id (save-excursion
400 (set-buffer mail-reply-buffer)
401 (std11-field-body "message-id")))
402 (ref (std11-field-body "References"))
403 (ref-list nil) (st nil))
404 (when (and mes-id ref)
405 (while (string-match "<[^>]+>" ref st)
407 (cons (substring ref (match-beginning 0) (setq st (match-end 0)))
410 (member mes-id ref-list))
414 (when (mail-position-on-field "References")
416 (while (looking-at "^[ \t]")
418 (setq mes-id (concat "\t" mes-id "\n")))
422 (defun wl-draft-yank-from-mail-reply-buffer (decode-it
423 &optional ignored-fields)
426 (narrow-to-region (point)(point))
429 (set-buffer mail-reply-buffer)
431 (decode-mime-charset-region (point-min) (point-max)
433 (buffer-substring-no-properties
434 (point-min) (point-max))))
436 (goto-char (point-min))
437 (wl-draft-delete-fields ignored-fields))
438 (goto-char (point-max))
440 (goto-char (point-min)))
442 (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
443 (mail-yank-hooks (run-hooks 'mail-yank-hooks))
444 (wl-draft-cite-function (funcall wl-draft-cite-function))) ; default cite
445 (run-hooks 'wl-draft-cited-hook)
446 (when (and wl-draft-add-references
447 (wl-draft-add-references))
448 (wl-highlight-headers 'for-draft)) ; highlight when added References:
449 (when wl-highlight-body-too
450 (wl-highlight-body-region beg (point-max)))))
452 (defun wl-draft-confirm ()
453 "Confirm send message."
455 (y-or-n-p (format "Send current draft as %s? "
456 (cond ((and (wl-message-mail-p) (wl-message-news-p))
458 ((wl-message-mail-p) "Mail")
459 ((wl-message-news-p) "News")))))
461 (defun wl-message-news-p ()
462 "If exist valid Newsgroups field, return non-nil."
463 (std11-field-body "Newsgroups"))
465 (defun wl-message-field-exists-p (field)
466 "If FIELD exist and FIELD value is not empty, return non-nil."
467 (let ((value (std11-field-body field)))
469 (not (string= value "")))))
471 (defun wl-message-mail-p ()
472 "If exist To, Cc or Bcc field, return non-nil."
473 (or (wl-message-field-exists-p "To")
474 (wl-message-field-exists-p "Cc")
475 (wl-message-field-exists-p "Bcc")
476 ;;; This may be needed..
477 ;;; (wl-message-field-exists-p "Fcc")
480 (defun wl-draft-open-file (&optional file)
481 "Open FILE for edit."
483 ;;;(interactive "*fFile to edit: ")
484 (wl-draft-edit-string (elmo-get-file-string
486 (read-file-name "File to edit: "
487 (or wl-tmp-dir "~/"))))))
489 (defun wl-draft-edit-string (string)
490 (let ((cur-buf (current-buffer))
491 (tmp-buf (get-buffer-create " *wl-draft-edit-string*"))
492 to subject in-reply-to cc references newsgroups mail-followup-to
493 content-type content-transfer-encoding
494 body-beg buffer-read-only
499 (setq to (std11-field-body "To"))
502 (decode-mime-charset-string
505 (setq subject (std11-field-body "Subject"))
506 (setq subject (and subject
508 (decode-mime-charset-string
511 (setq in-reply-to (std11-field-body "In-Reply-To"))
512 (setq cc (std11-field-body "Cc"))
515 (decode-mime-charset-string
518 (setq references (std11-field-body "References"))
519 (setq newsgroups (std11-field-body "Newsgroups"))
520 (setq mail-followup-to (std11-field-body "Mail-Followup-To"))
521 (setq content-type (std11-field-body "Content-Type"))
522 (setq content-transfer-encoding (std11-field-body "Content-Transfer-Encoding"))
523 (goto-char (point-min))
524 (or (re-search-forward "\n\n" nil t)
525 (search-forward (concat mail-header-separator "\n") nil t))
528 (wl-draft to subject in-reply-to cc references newsgroups
530 content-type content-transfer-encoding
531 (buffer-substring (point) (point-max))
534 (and to (mail-position-on-field "To"))
535 (delete-other-windows)
536 (kill-buffer tmp-buf)))
537 (setq buffer-read-only nil) ;;??
538 (run-hooks 'wl-draft-reedit-hook))
540 (defun wl-draft-insert-current-message (dummy)
542 (let (mail-reply-buffer
543 mail-citation-hook mail-yank-hooks
544 wl-draft-add-references wl-draft-cite-function)
545 (with-current-buffer wl-draft-buffer-cur-summary-buffer
546 (with-current-buffer wl-message-buffer
547 (setq mail-reply-buffer (wl-message-get-original-buffer))))
549 (with-current-buffer mail-reply-buffer
551 (error "No current message")
552 (wl-draft-yank-from-mail-reply-buffer
554 wl-ignored-forwarded-headers))))
556 (defun wl-draft-insert-get-message (dummy)
557 (let ((fld (completing-read
559 (if (memq 'read-folder wl-use-folder-petname)
560 (wl-folder-get-entity-with-petname)
561 wl-folder-entity-hashtb)
562 nil nil wl-default-spec
563 'wl-read-folder-hist))
564 (number (call-interactively
565 (function (lambda (num)
566 (interactive "nNumber: ")
568 (mail-reply-buffer (get-buffer-create "*wl-draft-insert-get-message*"))
569 mail-citation-hook mail-yank-hooks
570 wl-draft-cite-function)
573 (elmo-message-fetch (wl-folder-get-elmo-folder fld)
576 (elmo-make-fetch-strategy 'entire)
577 nil mail-reply-buffer)
578 (wl-draft-yank-from-mail-reply-buffer nil))
579 (kill-buffer mail-reply-buffer))))
582 ;; default body citation func
584 (defun wl-default-draft-cite ()
585 (let ((mail-yank-ignored-headers "[^:]+:")
586 (mail-yank-prefix "> ")
587 (summary-buf wl-current-summary-buffer)
588 (message-buf (get-buffer (wl-current-message-buffer)))
589 from date cite-title num entity)
591 (buffer-live-p summary-buf)
593 (buffer-live-p message-buf))
596 (set-buffer summary-buf)
599 (set-buffer message-buf)
600 wl-message-buffer-cur-number))
601 (setq entity (elmo-msgdb-overview-get-entity
602 num (wl-summary-buffer-msgdb)))
603 (setq from (elmo-msgdb-overview-entity-get-from entity))
604 (setq date (elmo-msgdb-overview-entity-get-date entity)))
605 (setq cite-title (format "At %s,\n%s wrote:"
606 (or date "some time ago")
607 (wl-summary-from-func-internal
610 (insert cite-title "\n"))
611 (mail-indent-citation)))
613 (defvar wl-draft-buffer nil "Draft buffer to yank content")
614 (defun wl-draft-yank-to-draft-buffer (buffer)
615 "Yank BUFFER content to `wl-draft-buffer'."
616 (set-buffer wl-draft-buffer)
617 (let ((mail-reply-buffer buffer))
618 (wl-draft-yank-from-mail-reply-buffer nil)
619 (kill-buffer buffer)))
621 (defun wl-draft-yank-original (&optional arg)
622 "Yank original message."
625 (let (buf mail-reply-buffer)
628 (setq buf (current-buffer)))
629 (setq mail-reply-buffer buf)
630 (wl-draft-yank-from-mail-reply-buffer nil))
631 (wl-draft-yank-current-message-entity)))
633 (defun wl-draft-hide (editing-buffer)
634 "Hide the editing draft buffer if possible."
636 (when (and editing-buffer
637 (buffer-live-p editing-buffer))
638 (set-buffer editing-buffer)
639 (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
640 fld-buf sum-win fld-win)
641 (if (and wl-draft-use-frame
642 (> (length (visible-frame-list)) 1))
648 ;; stay folder window if required
649 (when wl-stay-folder-window
650 (if (setq fld-buf (get-buffer wl-folder-buffer-name))
651 (if (setq fld-win (get-buffer-window fld-buf))
652 (select-window fld-win)
653 (if wl-draft-resume-folder-window ;; resume folder window
654 (switch-to-buffer fld-buf)))))
655 (if (buffer-live-p sum-buf)
656 (if (setq sum-win (get-buffer-window sum-buf t))
657 ;; if Summary is on the frame, select it.
658 (select-window sum-win)
659 ;; if summary is not on the frame, switch to it.
660 (if (and wl-stay-folder-window
661 (or wl-draft-resume-folder-window fld-win))
662 (wl-folder-select-buffer sum-buf)
663 (switch-to-buffer sum-buf)))))))
665 (defun wl-draft-delete (editing-buffer)
666 "kill the editing draft buffer and delete the file corresponds to it."
669 (set-buffer editing-buffer)
670 (if wl-draft-buffer-file-name
672 (if (file-exists-p wl-draft-buffer-file-name)
673 (delete-file wl-draft-buffer-file-name))
674 (let ((msg (and wl-draft-buffer-file-name
675 (string-match "[0-9]+$" wl-draft-buffer-file-name)
677 (elmo-match-string 0 wl-draft-buffer-file-name)))))
678 (wl-draft-config-info-operation msg 'delete))))
679 (set-buffer-modified-p nil) ; force kill
680 (kill-buffer editing-buffer))))
682 (defun wl-draft-kill (&optional force-kill)
683 "Kill current draft buffer and quit editing."
686 (when (and (or (eq major-mode 'wl-draft-mode)
687 (eq major-mode 'mail-mode))
689 (y-or-n-p "Kill Current Draft?")))
690 (let ((cur-buf (current-buffer)))
691 (wl-draft-hide cur-buf)
692 (wl-draft-delete cur-buf)))
695 (defun wl-draft-fcc ()
696 "Add a new Fcc field, with file name completion."
698 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
699 (mail-position-on-field "to"))
702 ;; function for wl-sent-message-via
704 (defmacro wl-draft-sent-message-p (type)
705 (` (eq (nth 1 (assq (, type) wl-sent-message-via)) 'sent)))
707 (defmacro wl-draft-set-sent-message (type result &optional server-port)
708 (` (let ((element (assq (, type) wl-sent-message-via)))
710 (unless (eq (nth 1 element) (, result))
711 (setcdr element (list (, result) (, server-port)))
712 (setq wl-sent-message-modified t))
713 (push (list (, type) (, result) (, server-port)) wl-sent-message-via)
714 (setq wl-sent-message-modified t)))))
716 (defun wl-draft-sent-message-results ()
717 (let ((results wl-sent-message-via)
718 unplugged-via sent-via)
720 (cond ((eq (nth 1 (car results)) 'unplugged)
721 (push (caar results) unplugged-via))
722 ((eq (nth 1 (car results)) 'sent)
723 (push (caar results) sent-via)))
724 (setq results (cdr results)))
725 (list unplugged-via sent-via)))
727 (defun wl-draft-write-sendlog (status proto server to id)
728 "Write send log file, if `wl-draft-sendlog' is non-nil."
729 (when wl-draft-sendlog
731 (let* ((tmp-buf (get-buffer-create " *wl-draft-sendlog*"))
732 (filename (expand-file-name wl-draft-sendlog-filename
734 (filesize (nth 7 (file-attributes filename)))
735 (server (if server (concat " server=" server) ""))
737 ((memq proto '(fcc queue))
738 (format " folder=\"%s\"" to))
740 (format " ng=<%s>" to))
745 (mapcar '(lambda(x) (format "<%s>" x)) to)
748 (id (if id (concat " id=" id) ""))
749 (time (wl-sendlog-time)))
752 (insert (format "%s proto=%s stat=%s%s%s%s\n"
753 time proto status server to id))
754 (if (and wl-draft-sendlog-max-size filesize
755 (> filesize wl-draft-sendlog-max-size))
756 (rename-file filename (concat filename ".old") t))
757 (if (file-writable-p filename)
758 (write-region (point-min) (point-max)
760 (message (format "%s is not writable." filename)))
761 (kill-buffer tmp-buf)))))
763 (defun wl-draft-get-header-delimiter (&optional delete)
764 ;; If DELETE is non-nil, replace the header delimiter with a blank line
766 (goto-char (point-min))
767 (when (re-search-forward
768 (concat "^" (regexp-quote mail-header-separator) "$\\|^$") nil t)
772 (setq delimline (point-marker)))
775 (defun wl-draft-send-mail-with-qmail ()
776 "Pass the prepared message buffer to qmail-inject.
777 Refer to the documentation for the variable `send-mail-function'
778 to find out how to use this."
779 (if (and wl-draft-qmail-send-plugged
780 (not (elmo-plugged-p)))
781 (wl-draft-set-sent-message 'mail 'unplugged)
783 (let ((id (std11-field-body "Message-ID"))
784 (to (std11-field-body "To")))
788 'call-process-region 1 (point-max) wl-qmail-inject-program
790 wl-qmail-inject-args))
791 ;; qmail-inject doesn't say anything on it's stdout/stderr,
792 ;; we have to look at the retval instead
794 (wl-draft-set-sent-message 'mail 'sent)
795 (wl-draft-write-sendlog 'ok 'qmail nil (list to) id)))
796 (1 (error "qmail-inject reported permanent failure"))
797 (111 (error "qmail-inject reported transient failure"))
798 ;; should never happen
799 (t (error "qmail-inject reported unknown failure"))))))
801 (defun wl-draft-parse-msg-id-list-string (string)
802 "Get msg-id list from STRING."
804 (dolist (parsed-id (std11-parse-msg-ids-string string))
805 (when (eq (car parsed-id) 'msg-id)
806 (setq msg-id-list (cons (std11-msg-id-string parsed-id)
808 (nreverse msg-id-list)))
810 (defun wl-draft-parse-mailbox-list (field &optional remove-group-list)
811 "Get mailbox list of FIELD from current buffer.
812 The buffer is expected to be narrowed to just the headers of the message.
813 If optional argument REMOVE-GROUP-LIST is non-nil, remove group list content
814 from current buffer."
816 (let ((case-fold-search t)
817 (inhibit-read-only t)
819 mailbox-list beg seq has-group-list)
820 (goto-char (point-min))
821 (while (re-search-forward (concat "^" (regexp-quote field) "[\t ]*:")
824 (re-search-forward "^[^ \t]" nil 'move)
826 (skip-chars-backward "\n")
827 (setq seq (std11-lexical-analyze
828 (buffer-substring-no-properties beg (point))))
829 (setq addresses (std11-parse-addresses seq))
831 (cond ((eq (car (car addresses)) 'group)
832 (setq has-group-list t)
836 'std11-address-string
837 (nth 2 (car addresses))))))
838 ((eq (car (car addresses)) 'mailbox)
839 (setq address (nth 1 (car addresses)))
843 (std11-addr-to-string
844 (if (eq (car address) 'phrase-route-addr)
847 (setq addresses (cdr addresses)))
848 (when (and remove-group-list has-group-list)
849 (delete-region beg (point))
850 (insert (wl-address-string-without-group-list-contents seq))))
853 (defun wl-draft-deduce-address-list (buffer header-start header-end)
854 "Get address list suitable for smtp RCPT TO:<address>.
855 Group list content is removed if `wl-draft-remove-group-list-contents' is
857 (let ((fields '("to" "cc" "bcc"))
858 (resent-fields '("resent-to" "resent-cc" "resent-bcc"))
863 (narrow-to-region header-start header-end)
864 (goto-char (point-min))
866 (if (re-search-forward "^resent-to[\t ]*:" nil t)
867 (setq fields resent-fields)))
871 (wl-draft-parse-mailbox-list
873 wl-draft-remove-group-list-contents)))
874 (setq fields (cdr fields)))
880 (defun wl-draft-send-mail-with-smtp ()
881 "Send the prepared message buffer with SMTP."
883 (let* ((errbuf (if mail-interactive
884 (generate-new-buffer " smtp errors")
887 (default-case-fold-search t)
888 (sender (or wl-envelope-from
889 (wl-address-header-extract-address wl-from)))
890 (delimline (save-excursion
891 (goto-char (point-min))
893 (concat "^" (regexp-quote mail-header-separator)
897 (or wl-smtp-posting-server
898 ;; Compatibility stuff for FLIM 1.12.5 or earlier.
899 ;; They don't accept a function as the value of `smtp-server'.
900 (if (functionp smtp-server)
905 (let (wl-draft-remove-group-list-contents)
906 (wl-draft-deduce-address-list
907 (current-buffer) (point-min) delimline)))
908 (or smtp-server "localhost"))))
909 (smtp-service (or wl-smtp-posting-port smtp-service))
910 (smtp-local-domain (or smtp-local-domain wl-local-domain))
911 (id (std11-field-body "message-id"))
913 (if (not (elmo-plugged-p smtp-server smtp-service))
914 (wl-draft-set-sent-message 'mail 'unplugged
915 (cons smtp-server smtp-service))
918 ;; Instead of `smtp-deduce-address-list'.
919 (setq recipients (wl-draft-deduce-address-list
920 (current-buffer) (point-min) delimline))
921 (unless recipients (error "No recipients"))
922 ;; Insert an extra newline if we need it to work around
923 ;; Sun's bug that swallows newlines.
924 (goto-char (1+ delimline))
925 (if (eval mail-mailer-swallows-blank-line)
927 ;;; (run-hooks 'wl-mail-send-pre-hook)
932 (wl-draft-delete-field "bcc" delimline)
933 (wl-draft-delete-field "resent-bcc" delimline)
934 (let (process-connection-type)
937 (wl-smtp-extension-bind
939 (smtp-send-buffer sender recipients (current-buffer))
941 (wl-draft-write-sendlog 'failed 'smtp smtp-server
943 (signal (car err) (cdr err)))))
944 (wl-draft-set-sent-message 'mail 'sent)
945 (wl-draft-write-sendlog
946 'ok 'smtp smtp-server recipients id)))))
948 (kill-buffer errbuf))))))
950 (defun wl-draft-send-mail-with-pop-before-smtp ()
951 "Send the prepared message buffer with POP-before-SMTP."
954 (let ((session (elmo-pop3-get-session
956 (or wl-pop-before-smtp-user
957 elmo-pop3-default-user)
958 (or wl-pop-before-smtp-authenticate-type
959 elmo-pop3-default-authenticate-type)
960 (or wl-pop-before-smtp-server
961 elmo-pop3-default-server)
962 (or wl-pop-before-smtp-port
963 elmo-pop3-default-port)
964 (or wl-pop-before-smtp-stream-type
965 elmo-pop3-default-stream-type)))))
966 (when session (elmo-network-close-session session)))
968 (wl-draft-send-mail-with-smtp))
970 (defun wl-draft-insert-required-fields (&optional force-msgid)
971 "Insert Message-ID, Date, and From field.
972 If FORCE-MSGID, ignore 'wl-insert-message-id'."
973 ;; Insert Message-Id field...
974 (goto-char (point-min))
975 (when (and (or force-msgid
976 wl-insert-message-id)
977 (not (re-search-forward "^Message-ID[ \t]*:" nil t)))
978 (insert (concat "Message-ID: "
979 (wl-draft-make-message-id-string)
981 ;; Insert date field.
982 (goto-char (point-min))
983 (or (re-search-forward "^Date[ \t]*:" nil t)
984 (wl-draft-insert-date-field))
985 ;; Insert from field.
986 (goto-char (point-min))
987 (or (re-search-forward "^From[ \t]*:" nil t)
988 (wl-draft-insert-from-field)))
990 (defun wl-draft-normal-send-func (editing-buffer kill-when-done)
991 "Send the message in the current buffer."
993 (std11-narrow-to-header mail-header-separator)
994 (wl-draft-insert-required-fields)
995 ;; Delete null fields.
996 (goto-char (point-min))
997 (while (re-search-forward "^[^ \t\n:]+:[ \t]*\n" nil t)
999 ;; ignore any blank lines in the header
1000 (while (re-search-forward "\n\n\n*" nil t)
1001 (replace-match "\n")))
1002 (run-hooks 'wl-mail-send-pre-hook) ;; X-PGP-Sig, Cancel-Lock
1003 (wl-draft-dispatch-message)
1004 (when kill-when-done
1005 ;; hide editing-buffer.
1006 (wl-draft-hide editing-buffer)
1007 ;; delete editing-buffer and its file.
1008 (wl-draft-delete editing-buffer)))
1010 (defun wl-draft-dispatch-message (&optional mes-string)
1011 "Send the message in the current buffer. Not modified the header fields."
1013 (if (and wl-draft-verbose-send mes-string)
1014 (message mes-string))
1016 (setq delimline (wl-draft-get-header-delimiter t))
1017 (unless wl-draft-fcc-list
1018 (setq wl-draft-fcc-list (wl-draft-get-fcc-list delimline)))
1020 (setq wl-sent-message-modified nil)
1023 (if (and (wl-message-mail-p)
1024 (not (wl-draft-sent-message-p 'mail)))
1025 (funcall wl-draft-send-mail-function))
1026 (if (and (wl-message-news-p)
1027 (not (wl-draft-sent-message-p 'news))
1028 (not (wl-message-field-exists-p "Resent-to")))
1029 (funcall wl-draft-send-news-function)))
1031 (let* ((status (wl-draft-sent-message-results))
1032 (unplugged-via (car status))
1033 (sent-via (nth 1 status)))
1034 ;; If one sent, process fcc folder.
1035 (if (and sent-via wl-draft-fcc-list)
1037 (wl-draft-do-fcc (wl-draft-get-header-delimiter) wl-draft-fcc-list)
1038 (setq wl-draft-fcc-list nil))
1039 (if wl-draft-use-cache
1040 (let ((id (std11-field-body "Message-ID"))
1041 (elmo-enable-disconnected-operation t))
1042 (elmo-file-cache-save id nil))))
1043 ;; If one unplugged, append queue.
1044 (when (and unplugged-via
1045 wl-sent-message-modified)
1046 (if wl-draft-enable-queuing
1047 (wl-draft-queue-append wl-sent-message-via)
1048 (error "Unplugged")))
1049 (when wl-draft-verbose-send
1050 (if (and unplugged-via sent-via);; combined message
1052 (setq wl-draft-verbose-msg
1053 (format "Sending%s and Queuing%s..."
1054 sent-via unplugged-via))
1055 (message (concat wl-draft-verbose-msg "done")))
1057 (message (concat mes-string
1058 (if sent-via "done" "failed")))))))))
1059 (not wl-sent-message-modified)) ;; return value
1061 (defun wl-draft-raw-send (&optional kill-when-done force-pre-hook mes-string)
1062 "Force send current buffer as raw message."
1065 (let (wl-interactive-send
1066 ;;; wl-draft-verbose-send
1067 (wl-mail-send-pre-hook (and force-pre-hook wl-mail-send-pre-hook))
1068 ;;; wl-news-send-pre-hook
1071 (wl-draft-send kill-when-done mes-string))))
1073 (defun wl-draft-clone-local-variables ()
1074 (let ((locals (buffer-local-variables))
1077 (when (and (consp (car locals))
1079 (string-match wl-draft-clone-local-variable-regexp
1080 (symbol-name (car (car locals)))))
1081 (wl-append result (list (car (car locals)))))
1082 (setq locals (cdr locals)))
1085 (defun wl-draft-send (&optional kill-when-done mes-string)
1086 "Send current draft message.
1087 If optional argument is non-nil, current draft buffer is killed"
1089 ;; Don't call this explicitly.
1090 ;; Added to 'wl-draft-send-hook (by teranisi)
1091 ;; (wl-draft-config-exec)
1092 (run-hooks 'wl-draft-send-hook)
1093 (when (or (not wl-interactive-send)
1094 (y-or-n-p "Send current draft. OK?"))
1095 (let ((send-mail-function 'wl-draft-raw-send)
1096 (editing-buffer (current-buffer))
1097 (sending-buffer (wl-draft-generate-clone-buffer
1098 " *wl-draft-sending-buffer*"
1099 (append wl-draft-config-variables
1100 (wl-draft-clone-local-variables))))
1101 (wl-draft-verbose-msg nil)
1104 (save-excursion (set-buffer sending-buffer)
1105 (if (and (not (wl-message-mail-p))
1106 (not (wl-message-news-p)))
1107 (error "No recipient is specified"))
1108 (expand-abbrev) ; for mail-abbrevs
1109 (run-hooks 'mail-send-hook) ; translate buffer
1110 (if wl-draft-verbose-send
1111 (message (or mes-string "Sending...")))
1112 (funcall wl-draft-send-function editing-buffer kill-when-done)
1113 ;; Now perform actions on successful sending.
1114 (while mail-send-actions
1116 (apply (car (car mail-send-actions))
1117 (cdr (car mail-send-actions)))
1119 (setq mail-send-actions (cdr mail-send-actions)))
1120 (if (or (eq major-mode 'wl-draft-mode)
1121 (eq major-mode 'mail-mode))
1122 (local-set-key "\C-c\C-s" 'wl-draft-send)) ; override
1123 (if wl-draft-verbose-send
1124 (message (concat (or wl-draft-verbose-msg
1125 mes-string "Sending...")
1127 ;; kill sending buffer, anyway.
1128 (and (buffer-live-p sending-buffer)
1129 (kill-buffer sending-buffer))))))
1131 (defun wl-draft-save ()
1132 "Save current draft."
1135 (wl-draft-config-info-operation
1136 (and (string-match "[0-9]+$" wl-draft-buffer-file-name)
1138 (elmo-match-string 0 wl-draft-buffer-file-name)))
1141 (defun wl-draft-mimic-kill-buffer ()
1142 "Kill the current (draft) buffer with query."
1144 (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
1147 (if (or (not bufname)
1148 (string-equal bufname "")
1149 (string-equal bufname (buffer-name)))
1150 (wl-draft-save-and-exit)
1151 (kill-buffer bufname))))
1153 (defun wl-draft-save-and-exit ()
1154 "Save current draft and exit current draft mode."
1157 (let ((editing-buffer (current-buffer)))
1158 (wl-draft-hide editing-buffer)
1159 (kill-buffer editing-buffer)))
1161 (defun wl-draft-send-and-exit ()
1162 "Send current draft message and kill it."
1166 (defun wl-draft-send-from-toolbar ()
1168 (let ((wl-interactive-send t))
1169 (wl-draft-send-and-exit)))
1171 (defun wl-draft-delete-field (field &optional delimline)
1172 (wl-draft-delete-fields (regexp-quote field) delimline))
1174 (defun wl-draft-delete-fields (regexp &optional delimline)
1177 (if (search-forward "\n\n" nil t)
1178 (setq delimline (point))
1179 (setq delimline (point-max))))
1180 (narrow-to-region (point-min) delimline)
1181 (goto-char (point-min))
1182 (let ((regexp (concat "^" regexp ":"))
1183 (case-fold-search t)
1186 (if (looking-at regexp)
1192 (if (re-search-forward "^[^ \t]" nil t)
1193 (goto-char (match-beginning 0))
1196 (if (re-search-forward "^[^ \t]" nil t)
1197 (goto-char (match-beginning 0))
1200 (defun wl-draft-get-fcc-list (header-end)
1202 (case-fold-search t))
1203 (or (markerp header-end) (error "header-end must be a marker"))
1205 (goto-char (point-min))
1206 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1208 (cons (buffer-substring-no-properties
1212 (skip-chars-backward " \t")
1216 (wl-folder-confirm-existence
1217 (wl-folder-get-elmo-folder (eword-decode-string (car fcc-list)))))
1218 (delete-region (match-beginning 0)
1219 (progn (forward-line 1) (point)))))
1222 (defun wl-draft-do-fcc (header-end &optional fcc-list)
1223 (let ((send-mail-buffer (current-buffer))
1224 (tembuf (generate-new-buffer " fcc output"))
1225 (case-fold-search t)
1227 (or (markerp header-end) (error "header-end must be a marker"))
1230 (setq fcc-list (wl-draft-get-fcc-list header-end)))
1233 ;; insert just the headers to avoid moving the gap more than
1234 ;; necessary (the message body could be arbitrarily huge.)
1235 (insert-buffer-substring send-mail-buffer 1 header-end)
1236 (wl-draft-insert-required-fields t)
1237 (goto-char (point-max))
1238 (insert-buffer-substring send-mail-buffer header-end)
1239 (let ((id (std11-field-body "Message-ID"))
1240 (elmo-enable-disconnected-operation t)
1243 (unless (or cache-saved
1244 (elmo-folder-plugged-p
1245 (wl-folder-get-elmo-folder (car fcc-list))))
1246 (elmo-file-cache-save id nil) ;; for disconnected operation
1247 (setq cache-saved t))
1248 (if (elmo-folder-append-buffer
1249 (wl-folder-get-elmo-folder
1250 (eword-decode-string (car fcc-list)))
1252 (wl-draft-write-sendlog 'ok 'fcc nil (car fcc-list) id)
1253 (wl-draft-write-sendlog 'failed 'fcc nil (car fcc-list) id))
1254 (setq fcc-list (cdr fcc-list)))))
1255 (kill-buffer tembuf)))
1257 (defun wl-draft-on-field-p ()
1260 (goto-char (point-min))
1261 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
1268 (if (or (looking-at ".*,[ \t]?$")
1269 (looking-at "^[^ \t]+:[ \t]+.*:$")); group list name
1271 (let ((pos (point)))
1274 (if (looking-at "^[ \t]")
1276 (if (re-search-forward ":" pos t) nil t)))))))
1278 (defun wl-draft-random-alphabet ()
1279 (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)))
1280 (nth (abs (% (random) 26)) alphabet)))
1283 (defun wl-draft (&optional to subject in-reply-to cc references newsgroups
1285 content-type content-transfer-encoding
1286 body edit-again summary-buf)
1287 "Write and send mail/news message with Wanderlust."
1289 (unless (featurep 'wl)
1293 (wl-init 'wl-draft) ;; returns immediately if already initialized.
1295 (setq summary-buf (wl-summary-get-buffer (wl-summary-buffer-folder-name))))
1296 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1297 buf-name file-name num wl-demo change-major-mode-hook)
1298 (if (not (elmo-folder-message-file-p draft-folder))
1299 (error "%s folder cannot be used for draft folder" wl-draft-folder))
1300 (setq num (elmo-max-of-list
1301 (or (elmo-folder-list-messages draft-folder) '(0))))
1302 (setq num (+ 1 num))
1303 ;; To get unused buffer name.
1304 (while (get-buffer (concat wl-draft-folder "/" (int-to-string num)))
1305 (setq num (+ 1 num)))
1306 (setq buf-name (find-file-noselect
1308 (elmo-message-file-name
1309 (wl-folder-get-elmo-folder wl-draft-folder)
1311 (if wl-draft-use-frame
1312 (switch-to-buffer-other-frame buf-name)
1313 (switch-to-buffer buf-name))
1314 (set-buffer buf-name)
1315 (if (not (string-match (regexp-quote wl-draft-folder)
1317 (rename-buffer (concat wl-draft-folder "/" (int-to-string num))))
1318 (if (or (eq wl-draft-reply-buffer-style 'full)
1319 (eq this-command 'wl-draft)
1320 (eq this-command 'wl-summary-write)
1321 (eq this-command 'wl-summary-write-current-newsgroup))
1322 (delete-other-windows))
1325 (setq wl-sent-message-via nil)
1326 (if (stringp wl-from)
1327 (insert "From: " wl-from "\n"))
1328 (and (or (interactive-p)
1329 (eq this-command 'wl-summary-write)
1331 (insert "To: " (or to "") "\n"))
1332 (and cc (insert "Cc: " (or cc "") "\n"))
1333 (insert "Subject: " (or subject "") "\n")
1334 (and newsgroups (insert "Newsgroups: " newsgroups "\n"))
1335 (and mail-followup-to (insert "Mail-Followup-To: " mail-followup-to "\n"))
1336 (and wl-insert-mail-reply-to
1337 (insert "Mail-Reply-To: "
1338 (wl-address-header-extract-address
1340 (and in-reply-to (insert "In-Reply-To: " in-reply-to "\n"))
1341 (and references (insert "References: " references "\n"))
1342 (insert (funcall wl-generate-mailer-string-function) "\n")
1343 (setq wl-draft-buffer-file-name file-name)
1344 (if mail-default-reply-to
1345 (insert "Reply-To: " mail-default-reply-to "\n"))
1346 (wl-draft-insert-ccs "Bcc: " (or wl-bcc
1347 (and mail-self-blind (user-login-name))))
1348 (wl-draft-insert-ccs "Fcc: " wl-fcc)
1350 (insert "Organization: " wl-organization "\n"))
1351 (and wl-auto-insert-x-face
1352 (file-exists-p wl-x-face-file)
1353 (wl-draft-insert-x-face-field-here))
1354 (if mail-default-headers
1355 (insert mail-default-headers))
1356 (if (not (= (preceding-char) ?\n))
1360 (setq start (point))
1362 (insert "Content-type: " content-type "\n"))
1363 (when content-transfer-encoding
1364 (insert "Content-Transfer-Encoding: " content-transfer-encoding "\n"))
1365 (if (or content-type content-transfer-encoding)
1367 (and body (insert body))
1369 (narrow-to-region start (point))
1371 (wl-draft-decode-message-in-buffer))
1374 (put-text-property (point)
1376 (insert mail-header-separator "\n")
1378 'category 'mail-header-separator)))
1379 (put-text-property (point)
1381 (insert mail-header-separator "\n")
1383 'category 'mail-header-separator)
1384 (and body (insert body)))
1386 (push-mark (point) t)
1387 (push-mark (point) t t))
1388 (as-binary-output-file
1389 (write-region (point-min)(point-max) wl-draft-buffer-file-name
1391 (wl-draft-editor-mode)
1392 (wl-draft-overload-functions)
1393 (wl-highlight-headers 'for-draft)
1394 (goto-char (point-min))
1395 (setq wl-draft-config-exec-flag t)
1397 (run-hooks 'wl-mail-setup-hook))
1398 (wl-user-agent-compose-internal) ;; user-agent
1399 (cond ((eq this-command 'wl-summary-write-current-newsgroup)
1400 (mail-position-on-field "Subject"))
1401 ((and (interactive-p) (null to))
1402 (mail-position-on-field "To"))
1404 (goto-char (point-max))))
1405 (setq wl-draft-buffer-cur-summary-buffer (or summary-buf
1407 wl-summary-buffer-name)))
1410 (defsubst wl-draft-insert-ccs (str cc)
1416 (null (and wl-draft-delete-myself-from-bcc-fcc
1418 (mapcar 'wl-address-header-extract-address
1420 (wl-parse-addresses (std11-field-body "To"))
1421 (wl-parse-addresses (std11-field-body "Cc"))))
1422 (mapcar 'downcase wl-subscribed-mailing-list)))))
1423 (insert str field "\n"))))
1425 (defun wl-draft-elmo-nntp-send ()
1426 (let ((elmo-nntp-post-pre-hook wl-news-send-pre-hook)
1427 (elmo-nntp-default-user
1428 (or wl-nntp-posting-user elmo-nntp-default-user))
1429 (elmo-nntp-default-server
1430 (or wl-nntp-posting-server elmo-nntp-default-server))
1431 (elmo-nntp-default-port
1432 (or wl-nntp-posting-port elmo-nntp-default-port))
1433 (elmo-nntp-default-stream-type
1434 (or wl-nntp-posting-stream-type elmo-nntp-default-stream-type)))
1435 (if (not (elmo-plugged-p elmo-nntp-default-server elmo-nntp-default-port))
1436 (wl-draft-set-sent-message 'news 'unplugged
1437 (cons elmo-nntp-default-server
1438 elmo-nntp-default-port))
1439 (elmo-nntp-post elmo-nntp-default-server (current-buffer))
1440 (wl-draft-set-sent-message 'news 'sent)
1441 (wl-draft-write-sendlog 'ok 'nntp elmo-nntp-default-server
1442 (std11-field-body "Newsgroups")
1443 (std11-field-body "Message-ID")))))
1445 (defun wl-draft-generate-clone-buffer (name &optional local-variables)
1446 "generate clone of current buffer named NAME."
1447 (let ((editing-buffer (current-buffer)))
1449 (set-buffer (generate-new-buffer name))
1452 (wl-draft-editor-mode)
1453 (insert-buffer editing-buffer)
1455 (while local-variables
1456 (make-local-variable (car local-variables))
1457 (set (car local-variables)
1459 (set-buffer editing-buffer)
1460 (symbol-value (car local-variables))))
1461 (setq local-variables (cdr local-variables)))
1464 (defun wl-draft-reedit (number)
1465 (let ((draft-folder (wl-folder-get-elmo-folder wl-draft-folder))
1467 buf-name file-name change-major-mode-hook)
1468 (setq file-name (elmo-message-file-name draft-folder number))
1469 (unless (file-exists-p file-name)
1470 (error "File %s does not exist" file-name))
1471 (setq buf-name (find-file-noselect file-name))
1472 (if wl-draft-use-frame
1473 (switch-to-buffer-other-frame buf-name)
1474 (switch-to-buffer buf-name))
1475 (set-buffer buf-name)
1476 (if (not (string-match (regexp-quote wl-draft-folder)
1478 (rename-buffer (concat wl-draft-folder "/" (buffer-name))))
1481 (setq wl-sent-message-via nil)
1482 (setq wl-draft-buffer-file-name file-name)
1483 (wl-draft-config-info-operation number 'load)
1484 (goto-char (point-min))
1485 (or (re-search-forward "\n\n" nil t)
1486 (search-forward (concat mail-header-separator "\n") nil t))
1488 (push-mark (point) t)
1489 (push-mark (point) t t))
1490 (write-region (point-min)(point-max) wl-draft-buffer-file-name
1492 (wl-draft-overload-functions)
1493 (wl-draft-editor-mode)
1494 (wl-highlight-headers 'for-draft)
1495 (run-hooks 'wl-draft-reedit-hook)
1496 (goto-char (point-max))
1500 (defmacro wl-draft-body-goto-top ()
1502 (goto-char (point-min))
1503 (if (re-search-forward mail-header-separator nil t)
1505 (goto-char (point-max))))))
1507 (defmacro wl-draft-body-goto-bottom ()
1508 (` (goto-char (point-max))))
1510 (defmacro wl-draft-config-body-goto-header ()
1512 (goto-char (point-min))
1513 (if (re-search-forward mail-header-separator nil t)
1515 (goto-char (point-max))))))
1517 (defun wl-draft-config-sub-body (content)
1518 (wl-draft-body-goto-top)
1519 (delete-region (point) (point-max))
1520 (if content (insert (eval content))))
1522 (defun wl-draft-config-sub-top (content)
1523 (wl-draft-body-goto-top)
1524 (if content (insert (eval content))))
1526 (defun wl-draft-config-sub-bottom (content)
1527 (wl-draft-body-goto-bottom)
1528 (if content (insert (eval content))))
1530 (defun wl-draft-config-sub-header (content)
1531 (wl-draft-config-body-goto-header)
1532 (if content (insert (concat (eval content) "\n"))))
1534 (defsubst wl-draft-config-sub-file (content)
1535 (let ((coding-system-for-read wl-cs-autoconv)
1536 (file (expand-file-name (eval content))))
1537 (if (file-exists-p file)
1538 (insert-file-contents file)
1539 (error "%s: no exists file" file))))
1541 (defun wl-draft-config-sub-body-file (content)
1542 (wl-draft-body-goto-top)
1543 (delete-region (point) (point-max))
1544 (wl-draft-config-sub-file content))
1546 (defun wl-draft-config-sub-top-file (content)
1547 (wl-draft-body-goto-top)
1548 (wl-draft-config-sub-file content))
1550 (defun wl-draft-config-sub-bottom-file (content)
1551 (wl-draft-body-goto-bottom)
1552 (wl-draft-config-sub-file content))
1554 (defun wl-draft-config-sub-header-file (content)
1555 (wl-draft-config-body-goto-header)
1556 (wl-draft-config-sub-file content))
1558 (defun wl-draft-config-sub-template (content)
1559 (setq wl-draft-config-variables
1560 (wl-template-insert (eval content))))
1562 (defun wl-draft-config-sub-x-face (content)
1563 (if (and (string-match "\\.xbm\\(\\.gz\\)?$" content)
1564 (fboundp 'x-face-insert)) ; x-face.el is installed.
1565 (x-face-insert content)
1566 (wl-draft-replace-field "X-Face" (elmo-get-file-string content t) t)))
1568 (defsubst wl-draft-config-sub-func (field content)
1570 (if (setq func (assq field wl-draft-config-sub-func-alist))
1571 (let (wl-draft-config-variables)
1572 (funcall (cdr func) content)
1573 ;; for wl-draft-config-sub-template
1574 (cons t wl-draft-config-variables)))))
1576 (defsubst wl-draft-config-exec-sub (clist)
1577 (let (config local-variables)
1579 (setq config (car clist))
1582 (let ((field (car config))
1583 (content (cdr config))
1587 (wl-draft-replace-field field (eval content) t))
1588 ((setq ret-val (wl-draft-config-sub-func field content))
1589 (if (cdr ret-val) ;; for wl-draft-config-sub-template
1590 (wl-append local-variables (cdr ret-val))))
1591 ((boundp field) ;; variable
1592 (make-local-variable field)
1593 (set field (eval content))
1594 (wl-append local-variables (list field)))
1596 (error "%s: not variable" field)))))
1597 ((or (functionp config)
1598 (and (symbolp config)
1602 (error "%s: not supported type" config)))
1603 (setq clist (cdr clist)))
1606 (defun wl-draft-prepared-config-exec (&optional config-alist reply-buf)
1607 "Change headers in draft preparation time."
1609 (unless wl-draft-reedit
1612 (and (boundp 'wl-draft-prepared-config-alist)
1613 wl-draft-prepared-config-alist) ;; For compatible.
1614 wl-draft-config-alist)))
1616 (wl-draft-config-exec config-alist reply-buf)))))
1618 (defun wl-draft-config-exec (&optional config-alist reply-buf)
1619 "Change headers in draft sending time."
1621 (let ((case-fold-search t)
1622 (alist (or config-alist wl-draft-config-alist))
1623 (reply-buf (or reply-buf (and (buffer-live-p wl-draft-reply-buffer)
1624 wl-draft-reply-buffer)))
1625 (local-variables wl-draft-config-variables)
1627 (when (and (or (interactive-p)
1628 wl-draft-config-exec-flag)
1633 (setq key (caar alist)
1640 (set-buffer reply-buf)
1642 (std11-narrow-to-header)
1643 (goto-char (point-min))
1644 (re-search-forward (car clist) nil t))))
1645 (wl-draft-config-exec-sub (cdr clist))
1648 (when (save-restriction
1649 (std11-narrow-to-header mail-header-separator)
1650 (goto-char (point-min))
1651 (re-search-forward key nil t))
1652 (wl-append local-variables
1653 (wl-draft-config-exec-sub clist))
1656 (wl-append local-variables
1657 (wl-draft-config-exec-sub clist))
1659 (if (and found wl-draft-config-matchone)
1661 (setq alist (cdr alist))))
1663 (setq wl-draft-config-exec-flag nil))
1664 (run-hooks 'wl-draft-config-exec-hook)
1665 (put-text-property (point-min)(point-max) 'face nil)
1666 (wl-highlight-message (point-min)(point-max) t)
1667 (setq wl-draft-config-variables
1668 (elmo-uniq-list local-variables))))))
1670 (defun wl-draft-replace-field (field content &optional add)
1673 (let ((case-fold-search t)
1674 (inhibit-read-only t) ;; added by teranisi.
1676 (std11-narrow-to-header mail-header-separator)
1677 (goto-char (point-min))
1678 (if (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
1683 (re-search-forward "^[^ \t]" nil 'move)
1685 (skip-chars-backward "\n")
1686 (delete-region beg (point))
1687 (insert " " content))
1692 (re-search-forward "^[^ \t]" nil 'move)
1694 (delete-region beg (point)))
1695 (when (and add content)
1697 (goto-char (point-max))
1698 (insert (concat field ": " content "\n"))))))))
1700 (defun wl-draft-config-info-operation (msg operation)
1701 (let* ((msgdb-dir (elmo-folder-msgdb-path (wl-folder-get-elmo-folder
1705 (format "%s-%d" wl-draft-config-save-filename msg)
1707 element alist variable)
1709 ((eq operation 'save)
1710 (let ((variables (elmo-uniq-list wl-draft-config-variables)))
1711 (while (setq variable (pop variables))
1712 (when (boundp variable)
1714 (list (cons variable (eval variable))))))
1715 (elmo-object-save filename alist)))
1716 ((eq operation 'load)
1717 (setq alist (elmo-object-load filename))
1718 (while (setq element (pop alist))
1719 (set (make-local-variable (car element)) (cdr element))
1720 (wl-append wl-draft-config-variables (list (car element)))))
1721 ((eq operation 'delete)
1722 (if (file-exists-p filename)
1723 (delete-file filename))))))
1725 (defun wl-draft-queue-info-operation (msg operation
1726 &optional add-sent-message-via)
1727 (let* ((msgdb-dir (elmo-folder-msgdb-path
1728 (wl-folder-get-elmo-folder wl-queue-folder)))
1731 (format "%s-%d" wl-draft-queue-save-filename msg)
1733 element alist variable)
1735 ((eq operation 'save)
1736 (let ((variables (elmo-uniq-list
1737 (append wl-draft-queue-save-variables
1738 wl-draft-config-variables
1739 (list 'wl-draft-fcc-list)))))
1740 (if add-sent-message-via
1741 (push 'wl-sent-message-via variables))
1742 (while (setq variable (pop variables))
1743 (when (boundp variable)
1745 (list (cons variable (eval variable))))))
1746 (elmo-object-save filename alist)))
1747 ((eq operation 'load)
1748 (setq alist (elmo-object-load filename))
1749 (while (setq element (pop alist))
1750 (set (make-local-variable (car element)) (cdr element))))
1751 ((eq operation 'get-sent-via)
1752 (setq alist (elmo-object-load filename))
1753 (cdr (assq 'wl-sent-message-via alist)))
1754 ((eq operation 'delete)
1755 (if (file-exists-p filename)
1756 (delete-file filename))))))
1758 (defun wl-draft-queue-append (wl-sent-message-via)
1759 (if wl-draft-verbose-send
1760 (message "Queuing..."))
1761 (let ((send-buffer (current-buffer))
1762 (folder (wl-folder-get-elmo-folder wl-queue-folder))
1763 (message-id (std11-field-body "Message-ID")))
1764 (if (elmo-folder-append-buffer folder t)
1766 (wl-draft-queue-info-operation
1767 (car (elmo-folder-status folder))
1768 'save wl-sent-message-via)
1769 (wl-draft-write-sendlog 'ok 'queue nil wl-queue-folder message-id)
1770 (when wl-draft-verbose-send
1771 (setq wl-draft-verbose-msg "Queuing...")
1772 (message "Queuing...done")))
1773 (wl-draft-write-sendlog 'failed 'queue nil wl-queue-folder message-id)
1774 (error "Queuing failed"))))
1776 (defun wl-draft-queue-flush ()
1777 "Flush draft queue."
1779 (let* ((queue-folder (wl-folder-get-elmo-folder wl-queue-folder))
1780 (msgs2 (elmo-folder-list-messages queue-folder))
1783 (wl-draft-queue-flushing t)
1784 msgs failure len buffer msgid sent-via)
1785 ;; get plugged send message
1787 (setq sent-via (wl-draft-queue-info-operation (car msgs2) 'get-sent-via))
1790 (when (and (eq (nth 1 (car sent-via)) 'unplugged)
1792 (car (nth 2 (car sent-via)))
1793 (cdr (nth 2 (car sent-via)))))
1794 (wl-append msgs (list (car msgs2)))
1796 (setq sent-via (cdr sent-via))))
1797 (setq msgs2 (cdr msgs2)))
1798 (when (> (setq len (length msgs)) 0)
1799 (if (elmo-y-or-n-p (format
1800 "%d message(s) are in the sending queue. Send now?"
1802 (not elmo-dop-flush-confirm) t)
1805 (setq buffer (get-buffer-create " *wl-draft-queue-flush*"))
1808 ;; reset buffer local variables
1809 (kill-all-local-variables)
1813 (setq wl-sent-message-via nil)
1814 (wl-draft-queue-info-operation (car msgs) 'load)
1815 (elmo-message-fetch queue-folder
1817 (elmo-make-fetch-strategy 'entire)
1818 nil (current-buffer))
1820 (setq failure (funcall
1821 wl-draft-queue-flush-send-function
1822 (format "Sending (%d/%d)..." i len)))
1823 ;;; (wl-draft-raw-send nil nil
1824 ;;; (format "Sending (%d/%d)..." i len))
1826 (elmo-display-error err t)
1831 (elmo-folder-delete-messages
1832 queue-folder (cons (car msgs) nil))
1833 (wl-draft-queue-info-operation (car msgs) 'delete)
1834 (setq performed (+ 1 performed)))
1835 (setq msgs (cdr msgs)))
1836 (kill-buffer buffer)
1837 (message "%d message(s) are sent." performed)))
1838 (message "%d message(s) are remained to be sent." len))
1841 (defun wl-jump-to-draft-buffer (&optional arg)
1842 "Jump to the draft if exists."
1845 (wl-jump-to-draft-folder)
1846 (let ((bufs (buffer-list))
1847 (draft-regexp (concat
1849 (elmo-localdir-folder-directory-internal
1850 (wl-folder-get-elmo-folder wl-draft-folder)))))
1854 (setq buf (buffer-file-name (car bufs)))
1855 (string-match draft-regexp buf))
1856 (setq draft-bufs (cons (buffer-name (car bufs)) draft-bufs)))
1857 (setq bufs (cdr bufs)))
1860 (message "No draft buffer exist."))
1863 (sort draft-bufs (function (lambda (a b) (not (string< a b))))))
1864 (if (setq buf (cdr (member (buffer-name) draft-bufs)))
1865 (setq buf (car buf))
1866 (setq buf (car draft-bufs)))
1867 (switch-to-buffer buf))))))
1869 (defun wl-jump-to-draft-folder ()
1870 (let ((msgs (reverse (elmo-folder-list-messages (wl-folder-get-elmo-folder
1872 (mybuf (buffer-name))
1875 (message "No draft message exist.")
1876 (if (string-match (concat "^" wl-draft-folder "/") mybuf)
1877 (setq msg (cadr (memq
1878 (string-to-int (substring mybuf (match-end 0)))
1880 (or msg (setq msg (car msgs)))
1881 (if (setq buf (get-buffer (format "%s/%d" wl-draft-folder msg)))
1882 (switch-to-buffer buf)
1883 (wl-draft-reedit msg)))))
1885 (defun wl-draft-highlight-and-recenter (&optional n)
1887 (if wl-highlight-body-too
1888 (let ((beg (point-min))
1890 (put-text-property beg end 'face nil)
1891 (wl-highlight-message beg end t)))
1894 ;;;; user-agent support by Sen Nagata
1896 ;; this appears to be necessarily global...
1897 (defvar wl-user-agent-compose-p nil)
1898 (defvar wl-user-agent-headers-and-body-alist nil)
1900 ;; this should be a generic function for mail-mode -- i wish there was
1901 ;; something like it in sendmail.el
1902 (defun wl-user-agent-insert-header (header-name header-value)
1903 "Insert HEADER-NAME w/ value HEADER-VALUE into a message."
1904 ;; it seems like overriding existing headers is acceptable -- should
1905 ;; we provide an option?
1907 ;; plan was: unfold header (might be folded), remove existing value, insert
1909 ;; wl doesn't seem to fold header lines yet anyway :-)
1911 (let ((kill-whole-line t)
1913 (mail-position-on-field (capitalize header-name))
1914 (setq end-of-line (point))
1916 (re-search-forward ":" end-of-line)
1917 (insert (concat " " header-value "\n"))
1920 ;; this should be a generic function for mail-mode -- i wish there was
1921 ;; something like it in sendmail.el
1923 ;; ** haven't dealt w/ case where the body is already set **
1924 (defun wl-user-agent-insert-body (body-text)
1925 "Insert a body of text, BODY-TEXT, into a message."
1926 ;; code defensively... :-P
1927 (goto-char (point-min))
1928 (search-forward mail-header-separator)
1933 (defun wl-user-agent-compose (&optional to subject other-headers continue
1934 switch-function yank-action
1936 "Support the `compose-mail' interface for wl.
1937 Only support for TO, SUBJECT, and OTHER-HEADERS has been implemented.
1938 Support for CONTINUE, YANK-ACTION, and SEND-ACTIONS has not
1939 been implemented yet. Partial support for SWITCH-FUNCTION now supported."
1941 (unless (featurep 'wl)
1943 ;; protect these -- to and subject get bound at some point, so it looks
1944 ;; to be necessary to protect the values used w/in
1945 (let ((wl-user-agent-headers-and-body-alist other-headers)
1946 (wl-draft-use-frame (eq switch-function 'switch-to-buffer-other-frame))
1947 (wl-draft-reply-buffer-style 'split))
1948 (when (eq switch-function 'switch-to-buffer-other-window)
1949 (when (one-window-p t)
1950 (if (window-minibuffer-p) (other-window 1))
1954 (if (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
1957 (wl-string-match-assoc "to" wl-user-agent-headers-and-body-alist
1960 (setq wl-user-agent-headers-and-body-alist
1961 (cons (cons "to" to)
1962 wl-user-agent-headers-and-body-alist))))
1964 (if (wl-string-match-assoc "subject"
1965 wl-user-agent-headers-and-body-alist
1968 (wl-string-match-assoc "subject"
1969 wl-user-agent-headers-and-body-alist
1972 (setq wl-user-agent-headers-and-body-alist
1973 (cons (cons "subject" subject)
1974 wl-user-agent-headers-and-body-alist))))
1975 ;; i think this is what we want to use...
1978 ;; tell the hook-function to do its stuff
1979 (setq wl-user-agent-compose-p t)
1980 ;; because to get the hooks working, wl-draft has to think it has
1981 ;; been called interactively
1982 (call-interactively 'wl-draft))
1983 (setq wl-user-agent-compose-p nil))))
1985 (defun wl-user-agent-compose-internal ()
1986 "Manipulate headers and/or a body of a draft message."
1987 ;; being called from wl-user-agent-compose?
1988 (if wl-user-agent-compose-p
1991 (let ((headers wl-user-agent-headers-and-body-alist)
1992 (case-fold-search t))
1995 (if (not (string-match "^body$" (car (car headers))))
1996 (wl-user-agent-insert-header
1997 (car (car headers)) (cdr (car headers)))
1999 (setq headers (cdr headers))))
2000 ;; highlight headers (from wl-draft in wl-draft.el)
2001 (wl-highlight-headers 'for-draft)
2003 (if (wl-string-match-assoc "body" wl-user-agent-headers-and-body-alist
2005 (wl-user-agent-insert-body
2006 (cdr (wl-string-match-assoc
2008 wl-user-agent-headers-and-body-alist 'ignore-case)))))
2012 (product-provide (provide 'wl-draft) (require 'wl-version))
2014 ;;; wl-draft.el ends here