* lisp/gnus.el (gnus-version-number): Update to 6.12.20
[elisp/gnus.git-] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Semi-gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;      Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
8 ;;      Katsumi Yamaoka  <yamaoka@jpl.org>
9 ;;      Kiyokazu SUTO    <suto@merry.xmath.ous.ac.jp>
10 ;; Keywords: mail, news, MIME
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30
31 ;;; Code:
32
33 (eval-when-compile (require 'cl))
34
35 (require 'gnus)
36 (require 'gnus-ems)
37 (require 'message)
38 (require 'gnus-art)
39
40 (defcustom gnus-post-method nil
41   "*Preferred method for posting USENET news.
42
43 If this variable is `current', Gnus will use the \"current\" select
44 method when posting.  If it is nil (which is the default), Gnus will
45 use the native posting method of the server.
46
47 This method will not be used in mail groups and the like, only in
48 \"real\" newsgroups.
49
50 If not nil nor `native', the value must be a valid method as discussed
51 in the documentation of `gnus-select-method'. It can also be a list of
52 methods. If that is the case, the user will be queried for what select
53 method to use when posting."
54   :group 'gnus-group-foreign
55   :type `(choice (const nil)
56                  (const current)
57                  (const native)
58                  (sexp :tag "Methods" ,gnus-select-method)))
59
60 (defvar gnus-outgoing-message-group nil
61   "*All outgoing messages will be put in this group.
62 If you want to store all your outgoing mail and articles in the group
63 \"nnml:archive\", you set this variable to that value.  This variable
64 can also be a list of group names.
65
66 If you want to have greater control over what group to put each
67 message in, you can set this variable to a function that checks the
68 current newsgroup name and then returns a suitable group name (or list
69 of names).")
70
71 (defvar gnus-mailing-list-groups nil
72   "*Regexp matching groups that are really mailing lists.
73 This is useful when you're reading a mailing list that has been
74 gatewayed to a newsgroup, and you want to followup to an article in
75 the group.")
76
77 (defvar gnus-add-to-list nil
78   "*If non-nil, add a `to-list' parameter automatically.")
79
80 (defvar gnus-crosspost-complaint
81   "Hi,
82
83 You posted the article below with the following Newsgroups header:
84
85 Newsgroups: %s
86
87 The %s group, at least, was an inappropriate recipient
88 of this message.  Please trim your Newsgroups header to exclude this
89 group before posting in the future.
90
91 Thank you.
92
93 "
94   "Format string to be inserted when complaining about crossposts.
95 The first %s will be replaced by the Newsgroups header;
96 the second with the current group name.")
97
98 (defvar gnus-message-setup-hook '(gnus-maybe-setup-default-charset)
99   "Hook run after setting up a message buffer.")
100
101 (defvar gnus-bug-create-help-buffer t
102   "*Should we create the *Gnus Help Bug* buffer?")
103
104 (defvar gnus-posting-styles nil
105   "*Alist of styles to use when posting.")
106
107 (defvar gnus-posting-style-alist
108   '((organization . message-user-organization)
109     (signature . message-signature)
110     (signature-file . message-signature-file)
111     (address . user-mail-address)
112     (name . user-full-name))
113   "*Mapping from style parameters to variables.")
114
115 ;;; Internal variables.
116
117 (defvar gnus-inhibit-posting-styles nil
118   "Inhibit the use of posting styles.")
119
120 (defvar gnus-message-buffer "*Mail Gnus*")
121 (defvar gnus-article-copy nil)
122 (defvar gnus-last-posting-server nil)
123 (defvar gnus-message-group-art nil)
124
125 (defconst gnus-bug-message
126   (format "Sending a bug report to the Gnus Towers.
127 ========================================
128
129 This gnus is the %s%s.
130 If you think the bug is a Semi-gnus bug, send a bug report to Semi-gnus
131 Developers. (the addresses below are mailing list addresses)
132
133 ========================================
134
135 The buffer below is a mail buffer.  When you press `C-c C-c', it will
136 be sent to the Gnus Bug Exterminators.
137
138 At the bottom of the buffer you'll see lots of variable settings.
139 Please do not delete those.  They will tell the Bug People what your
140 environment is, so that it will be easier to locate the bugs.
141
142 If you have found a bug that makes Emacs go \"beep\", set
143 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
144 and include the backtrace in your bug report.
145
146 Please describe the bug in annoying, painstaking detail.
147
148 Thank you for your help in stamping out bugs.
149 "
150           gnus-product-name
151           (if (string= gnus-product-name "Semi-gnus")
152               ""
153             ", a modified version of Semi-gnus")))
154
155 (eval-and-compile
156   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
157   (autoload 'news-setup "rnewspost")
158   (autoload 'news-reply-mode "rnewspost")
159   (autoload 'rmail-dont-reply-to "mail-utils")
160   (autoload 'rmail-output "rmailout"))
161
162 \f
163 ;;;
164 ;;; Gnus Posting Functions
165 ;;;
166
167 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
168   "p" gnus-summary-post-news
169   "f" gnus-summary-followup
170   "F" gnus-summary-followup-with-original
171   "c" gnus-summary-cancel-article
172   "s" gnus-summary-supersede-article
173   "r" gnus-summary-reply
174   "R" gnus-summary-reply-with-original
175   "w" gnus-summary-wide-reply
176   "W" gnus-summary-wide-reply-with-original
177   "n" gnus-summary-followup-to-mail
178   "N" gnus-summary-followup-to-mail-with-original
179   "m" gnus-summary-mail-other-window
180   "u" gnus-uu-post-news
181   "\M-c" gnus-summary-mail-crosspost-complaint
182   "om" gnus-summary-mail-forward
183   "op" gnus-summary-post-forward
184   "Om" gnus-summary-mail-digest
185   "Op" gnus-summary-post-digest)
186
187 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
188   "b" gnus-summary-resend-bounced-mail
189   ;; "c" gnus-summary-send-draft
190   "r" gnus-summary-resend-message)
191
192 ;;; Internal functions.
193
194 (defvar gnus-article-reply nil)
195 (defmacro gnus-setup-message (config &rest forms)
196   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
197         (buffer (make-symbol "gnus-setup-message-buffer"))
198         (article (make-symbol "gnus-setup-message-article"))
199         (group (make-symbol "gnus-setup-message-group")))
200     `(let ((,winconf (current-window-configuration))
201            (,buffer (buffer-name (current-buffer)))
202            (,article (and gnus-article-reply (gnus-summary-article-number)))
203            (,group gnus-newsgroup-name)
204            (message-header-setup-hook
205             (copy-sequence message-header-setup-hook))
206            (message-mode-hook (copy-sequence message-mode-hook))
207            (message-startup-parameter-alist
208             '((reply-buffer . gnus-copy-article-buffer)
209               (original-buffer . gnus-original-article-buffer)
210               (user-agent . Gnus))))
211        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
212        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
213        (add-hook 'message-mode-hook 'gnus-configure-posting-styles)
214        (unwind-protect
215            (progn
216              ,@forms)
217          (gnus-inews-add-send-actions ,winconf ,buffer ,article)
218          (setq gnus-message-buffer (current-buffer))
219          (set (make-local-variable 'gnus-message-group-art)
220               (cons ,group ,article))
221          (make-local-variable 'gnus-newsgroup-name)
222          (gnus-maybe-setup-default-charset)
223          (gnus-run-hooks 'gnus-message-setup-hook))
224        (gnus-add-buffer)
225        (gnus-configure-windows ,config t)
226        (set-buffer-modified-p nil))))
227
228 (defun gnus-inews-add-send-actions (winconf buffer article)
229   (make-local-hook 'message-sent-hook)
230   (add-hook 'message-sent-hook 'gnus-inews-do-gcc nil t)
231   (setq message-post-method
232         `(lambda (arg)
233            (gnus-post-method arg ,gnus-newsgroup-name)))
234   (setq message-user-agent (gnus-extended-version))
235   (when (not message-use-multi-frames)
236     (message-add-action
237      `(set-window-configuration ,winconf) 'exit 'postpone 'kill))
238   (message-add-action
239    `(when (gnus-buffer-exists-p ,buffer)
240       (save-excursion
241         (set-buffer ,buffer)
242         ,(when article
243            `(gnus-summary-mark-article-as-replied ,article))))
244    'send))
245
246 (put 'gnus-setup-message 'lisp-indent-function 1)
247 (put 'gnus-setup-message 'edebug-form-spec '(form body))
248
249 ;;; Post news commands of Gnus group mode and summary mode
250
251 (defun gnus-group-mail ()
252   "Start composing a mail."
253   (interactive)
254   (gnus-setup-message 'message
255     (message-mail)))
256
257 (defun gnus-group-post-news (&optional arg)
258   "Start composing a news message.
259 If ARG, post to the group under point.
260 If ARG is 1, prompt for a group name."
261   (interactive "P")
262   ;; Bind this variable here to make message mode hooks work ok.
263   (let ((gnus-newsgroup-name
264          (if arg
265              (if (= 1 (prefix-numeric-value arg))
266                  (completing-read "Newsgroup: " gnus-active-hashtb nil
267                                   (gnus-read-active-file-p))
268                (gnus-group-group-name))
269            "")))
270     (gnus-post-news 'post gnus-newsgroup-name)))
271
272 (defun gnus-summary-post-news ()
273   "Start composing a news message."
274   (interactive)
275   (gnus-post-news 'post gnus-newsgroup-name))
276
277 (defun gnus-summary-followup (yank &optional force-news)
278   "Compose a followup to an article.
279 If prefix argument YANK is non-nil, original article is yanked automatically."
280   (interactive
281    (list (and current-prefix-arg
282               (gnus-summary-work-articles 1))))
283   (when yank
284     (gnus-summary-goto-subject (car yank)))
285   (save-window-excursion
286     (gnus-summary-select-article))
287   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
288         (gnus-newsgroup-name gnus-newsgroup-name))
289     ;; Send a followup.
290     (gnus-post-news nil gnus-newsgroup-name
291                     headers gnus-article-buffer
292                     yank nil force-news)))
293
294 (defun gnus-summary-followup-with-original (n &optional force-news)
295   "Compose a followup to an article and include the original article."
296   (interactive "P")
297   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
298
299 (defun gnus-summary-followup-to-mail (&optional arg)
300   "Followup to the current mail message via news."
301   (interactive
302    (list (and current-prefix-arg
303               (gnus-summary-work-articles 1))))
304   (gnus-summary-followup arg t))
305
306 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
307   "Followup to the current mail message via news."
308   (interactive "P")
309   (gnus-summary-followup (gnus-summary-work-articles arg) t))
310
311 (defun gnus-inews-yank-articles (articles)
312   (let* ((more-than-one (cdr articles))
313          (frame (when (and message-use-multi-frames more-than-one)
314                   (window-frame (get-buffer-window (current-buffer)))))
315          refs beg article)
316     (message-goto-body)
317     (while (setq article (pop articles))
318       (save-window-excursion
319         (set-buffer gnus-summary-buffer)
320         (gnus-summary-select-article nil nil nil article)
321         (gnus-summary-remove-process-mark article))
322       (when frame
323         (select-frame frame))
324
325       ;; Gathering references.
326       (when more-than-one
327         (setq refs (message-list-references
328                     refs
329                     (mail-header-references gnus-current-headers)
330                     (mail-header-message-id gnus-current-headers))))
331
332       (gnus-copy-article-buffer)
333       (let ((message-reply-buffer gnus-article-copy)
334             (message-reply-headers gnus-current-headers))
335         (message-yank-original)
336         (setq beg (or beg (mark t))))
337       (when articles
338         (insert "\n")))
339     (push-mark)
340
341     ;; Replace with the gathered references.
342     (when refs
343       (push-mark beg)
344       (save-restriction
345         (message-narrow-to-headers)
346         (let ((case-fold-search t))
347           (if (re-search-forward "^References:\\([\t ]+.+\n\\)+" nil t)
348               (replace-match "")
349             (goto-char (point-max))))
350         (mail-header-format
351          (list (or (assq 'References message-header-format-alist)
352                    '(References . message-fill-references)))
353          (list (cons 'References
354                      (mapconcat 'identity (nreverse refs) " "))))
355         (backward-delete-char 1))
356       (setq beg (mark t))
357       (pop-mark))
358
359     (goto-char beg)))
360
361 (defun gnus-summary-cancel-article (&optional n symp)
362   "Cancel an article you posted.
363 Uses the process-prefix convention.  If given the symbolic
364 prefix `a', cancel using the standard posting method; if not
365 post using the current select method."
366   (interactive (gnus-interactive "P\ny"))
367   (let ((articles (gnus-summary-work-articles n))
368         (message-post-method
369          `(lambda (arg)
370             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
371         article)
372     (while (setq article (pop articles))
373       (when (gnus-summary-select-article t nil nil article)
374         (when (gnus-eval-in-buffer-window gnus-article-buffer
375                 (save-excursion
376                   (set-buffer gnus-original-article-buffer)
377                   (message-cancel-news)))
378           (gnus-summary-mark-as-read article gnus-canceled-mark)
379           (gnus-cache-remove-article 1))
380         (gnus-article-hide-headers-if-wanted))
381       (gnus-summary-remove-process-mark article))))
382
383 (defun gnus-summary-supersede-article ()
384   "Compose an article that will supersede a previous article.
385 This is done simply by taking the old article and adding a Supersedes
386 header line with the old Message-ID."
387   (interactive)
388   (let ((article (gnus-summary-article-number))
389         (gnus-message-setup-hook '(gnus-maybe-setup-default-charset)))
390     (gnus-setup-message 'reply-yank
391       (gnus-summary-select-article t)
392       (set-buffer gnus-original-article-buffer)
393       (message-supersede)
394       (push
395        `((lambda ()
396            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
397              (save-excursion
398                (set-buffer ,gnus-summary-buffer)
399                (gnus-cache-possibly-remove-article ,article nil nil nil t)
400                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
401        message-send-actions))))
402
403 \f
404
405 (defun gnus-copy-article-buffer (&optional article-buffer)
406   ;; make a copy of the article buffer with all text properties removed
407   ;; this copy is in the buffer gnus-article-copy.
408   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
409   ;; this buffer should be passed to all mail/news reply/post routines.
410   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
411   (buffer-disable-undo gnus-article-copy)
412   (let ((article-buffer (or article-buffer gnus-article-buffer))
413         end beg)
414     (if (not (and (get-buffer article-buffer)
415                   (gnus-buffer-exists-p article-buffer)))
416         (error "Can't find any article buffer")
417       (save-excursion
418         (set-buffer article-buffer)
419         (save-restriction
420           ;; Copy over the (displayed) article buffer, delete
421           ;; hidden text and remove text properties.
422           (widen)
423           (copy-to-buffer gnus-article-copy (point-min) (point-max))
424           (set-buffer gnus-article-copy)
425           (gnus-article-delete-text-of-type 'annotation)
426           (gnus-remove-text-with-property 'gnus-prev)
427           (gnus-remove-text-with-property 'gnus-next)
428           (insert
429            (prog1
430                (format "%s" (buffer-string))
431              (erase-buffer)))
432           ;; Find the original headers.
433           (set-buffer gnus-original-article-buffer)
434           (goto-char (point-min))
435           (while (looking-at message-unix-mail-delimiter)
436             (forward-line 1))
437           (setq beg (point))
438           (setq end (or (search-forward "\n\n" nil t) (point)))
439           ;; Delete the headers from the displayed articles.
440           (set-buffer gnus-article-copy)
441           (delete-region (goto-char (point-min))
442                          (or (search-forward "\n\n" nil t) (point)))
443           ;; Insert the original article headers.
444           (insert-buffer-substring gnus-original-article-buffer beg end)
445           (gnus-article-decode-rfc1522)))
446       gnus-article-copy)))
447
448 (defun gnus-post-news (post &optional group header article-buffer yank subject
449                             force-news)
450   (when article-buffer
451     (gnus-copy-article-buffer))
452   (let ((gnus-article-reply article-buffer)
453         (add-to-list gnus-add-to-list))
454     (gnus-setup-message (cond (yank 'reply-yank)
455                               (article-buffer 'reply)
456                               (t 'message))
457       (let* ((group (or group gnus-newsgroup-name))
458              (pgroup group)
459              to-address to-group mailing-list to-list
460              newsgroup-p)
461         (when group
462           (setq to-address (gnus-group-find-parameter group 'to-address)
463                 to-group (gnus-group-find-parameter group 'to-group)
464                 to-list (gnus-group-find-parameter group 'to-list)
465                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
466                 mailing-list (when gnus-mailing-list-groups
467                                (string-match gnus-mailing-list-groups group))
468                 group (gnus-group-real-name group)))
469         (if (or (and to-group
470                      (gnus-news-group-p to-group))
471                 newsgroup-p
472                 force-news
473                 (and (gnus-news-group-p
474                       (or pgroup gnus-newsgroup-name)
475                       (if header (mail-header-number header)
476                         gnus-current-article))
477                      (not mailing-list)
478                      (not to-list)
479                      (not to-address)))
480             ;; This is news.
481             (if post
482                 (message-news (or to-group group))
483               (set-buffer gnus-article-copy)
484               (gnus-msg-treat-broken-reply-to)
485               (message-followup (if (or newsgroup-p force-news) nil to-group)))
486           ;; The is mail.
487           (if post
488               (progn
489                 (message-mail (or to-address to-list))
490                 ;; Arrange for mail groups that have no `to-address' to
491                 ;; get that when the user sends off the mail.
492                 (when (and (not to-list)
493                            (not to-address)
494                            add-to-list)
495                   (push (list 'gnus-inews-add-to-address pgroup)
496                         message-send-actions)))
497             (set-buffer gnus-article-copy)
498             (gnus-msg-treat-broken-reply-to)
499             (message-wide-reply to-address)))
500         (when yank
501           (gnus-inews-yank-articles yank))))))
502
503 (defun gnus-msg-treat-broken-reply-to ()
504   "Remove the Reply-to header iff broken-reply-to."
505   (when (gnus-group-find-parameter
506          gnus-newsgroup-name 'broken-reply-to)
507     (save-restriction
508       (message-narrow-to-head)
509       (message-remove-header "reply-to"))))
510
511 (defun gnus-post-method (arg group &optional silent)
512   "Return the posting method based on GROUP and ARG.
513 If SILENT, don't prompt the user."
514   (let ((group-method (gnus-find-method-for-group group)))
515     (cond
516      ;; If the group-method is nil (which shouldn't happen) we use
517      ;; the default method.
518      ((null group-method)
519       (or (and (null (eq gnus-post-method 'active)) gnus-post-method)
520                gnus-select-method message-post-method))
521      ;; We want the inverse of the default
522      ((and arg (not (eq arg 0)))
523       (if (eq gnus-post-method 'active)
524           gnus-select-method
525         group-method))
526      ;; We query the user for a post method.
527      ((or arg
528           (and gnus-post-method
529                (not (eq gnus-post-method 'current))
530                (listp (car gnus-post-method))))
531       (let* ((methods
532               ;; Collect all methods we know about.
533               (append
534                (when (and gnus-post-method
535                           (not (eq gnus-post-method 'current)))
536                  (if (listp (car gnus-post-method))
537                      gnus-post-method
538                    (list gnus-post-method)))
539                gnus-secondary-select-methods
540                (mapcar 'cdr gnus-server-alist)
541                (list gnus-select-method)
542                (list group-method)))
543              method-alist post-methods method)
544         ;; Weed out all mail methods.
545         (while methods
546           (setq method (gnus-server-get-method "" (pop methods)))
547           (when (and (or (gnus-method-option-p method 'post)
548                          (gnus-method-option-p method 'post-mail))
549                      (not (member method post-methods)))
550             (push method post-methods)))
551         ;; Create a name-method alist.
552         (setq method-alist
553               (mapcar
554                (lambda (m)
555                  (list (concat (cadr m) " (" (symbol-name (car m)) ")") m))
556                post-methods))
557         ;; Query the user.
558         (cadr
559          (assoc
560           (setq gnus-last-posting-server
561                 (if (and silent
562                          gnus-last-posting-server)
563                     ;; Just use the last value.
564                     gnus-last-posting-server
565                   (completing-read
566                    "Posting method: " method-alist nil t
567                    (cons (or gnus-last-posting-server "") 0))))
568           method-alist))))
569      ;; Override normal method.
570      ((and (eq gnus-post-method 'current)
571            (not (eq (car group-method) 'nndraft))
572            (gnus-get-function group-method 'request-post t)
573            (not arg))
574       group-method)
575      ((and gnus-post-method
576            (not (eq gnus-post-method 'current)))
577       gnus-post-method)
578      ;; Use the normal select method.
579      (t gnus-select-method))))
580
581 \f
582
583 (defun gnus-extended-version ()
584   "Stringified gnus version."
585   (interactive)
586   (concat gnus-product-name "/" gnus-version-number))
587
588 (defun gnus-message-make-user-agent (&optional include-mime-info max-column)
589   "Return user-agent info.
590 INCLUDE-MIME-INFO the optional first argument if it is non-nil and the variable
591   `mime-edit-user-agent-value' exists, the return value will include it.
592 MAX-COLUMN the optional second argument if it is specified, the return value
593   will be folded up in the proper way."
594   (let ((user-agent (if (and include-mime-info
595                              (boundp 'mime-edit-user-agent-value))
596                         (concat (gnus-extended-version)
597                                 " "
598                                 mime-edit-user-agent-value)
599                       (gnus-extended-version))))
600     (if max-column
601         (let (boundary)
602           (unless (natnump max-column) (setq max-column 76))
603           (with-temp-buffer
604             (insert "            " user-agent)
605             (goto-char 13)
606             (while (re-search-forward "[\n\t ]+" nil t)
607               (replace-match " "))
608             (goto-char 13)
609             (while (re-search-forward "[^ ()/]+\\(/[^ ()/]+\\)? ?" nil t)
610               (while (eq ?\( (char-after (point)))
611                 (forward-list)
612                 (skip-chars-forward " "))
613               (skip-chars-backward " ")
614               (if (> (current-column) max-column)
615                   (progn
616                     (if (or (not boundary) (eq ?\n (char-after boundary)))
617                         (progn
618                           (setq boundary (point))
619                           (unless (eobp)
620                             (delete-char 1)
621                             (insert "\n ")))
622                       (goto-char boundary)
623                       (delete-char 1)
624                       (insert "\n ")))
625                 (setq boundary (point))))
626             (buffer-substring 13 (point-max))))
627       user-agent)))
628
629 \f
630 ;;;
631 ;;; Gnus Mail Functions
632 ;;;
633
634 ;;; Mail reply commands of Gnus summary mode
635
636 (defun gnus-summary-reply (&optional yank wide)
637   "Start composing a reply mail to the current message.
638 If prefix argument YANK is non-nil, the original article is yanked
639 automatically."
640   (interactive
641    (list (and current-prefix-arg
642               (gnus-summary-work-articles 1))))
643   ;; Stripping headers should be specified with mail-yank-ignored-headers.
644   (when yank
645     (gnus-summary-goto-subject (car yank)))
646   (let ((gnus-article-reply t))
647     (gnus-setup-message (if yank 'reply-yank 'reply)
648       (gnus-summary-select-article)
649       (set-buffer (gnus-copy-article-buffer))
650       (gnus-msg-treat-broken-reply-to)
651       (message-reply nil wide)
652       (when yank
653         (gnus-inews-yank-articles yank)))))
654
655 (defun gnus-summary-reply-with-original (n &optional wide)
656   "Start composing a reply mail to the current message.
657 The original article will be yanked."
658   (interactive "P")
659   (gnus-summary-reply (gnus-summary-work-articles n) wide))
660
661 (defun gnus-summary-wide-reply (&optional yank)
662   "Start composing a wide reply mail to the current message.
663 If prefix argument YANK is non-nil, the original article is yanked
664 automatically."
665   (interactive
666    (list (and current-prefix-arg
667               (gnus-summary-work-articles 1))))
668   (gnus-summary-reply yank t))
669
670 (defun gnus-summary-wide-reply-with-original (n)
671   "Start composing a wide reply mail to the current message.
672 The original article will be yanked."
673   (interactive "P")
674   (gnus-summary-reply-with-original n t))
675
676 (defun gnus-summary-mail-forward (&optional full-headers post)
677   "Forward the current message to another user.
678 If FULL-HEADERS (the prefix), include full headers when forwarding."
679   (interactive "P")
680   (gnus-setup-message 'forward
681     (gnus-summary-select-article)
682     (let ((charset default-mime-charset))
683       (set-buffer gnus-original-article-buffer)
684       (set (make-local-variable 'default-mime-charset) charset))
685     (let ((message-included-forward-headers
686            (if full-headers "" message-included-forward-headers)))
687       (message-forward post))))
688
689 (defun gnus-summary-post-forward (&optional full-headers)
690   "Forward the current article to a newsgroup.
691 If FULL-HEADERS (the prefix), include full headers when forwarding."
692   (interactive "P")
693   (gnus-summary-mail-forward full-headers t))
694
695 ;;; XXX: generate Subject and ``Topics''?
696 (defun gnus-summary-mail-digest (&optional n post)
697   "Digests and forwards all articles in this series."
698   (interactive "P")
699   (let ((subject "Digested Articles")
700         (articles (gnus-summary-work-articles n))
701         article frame)
702     (gnus-setup-message 'forward
703       (gnus-summary-select-article)
704       (if post (message-news nil subject) (message-mail nil subject))
705       (when (and message-use-multi-frames (cdr articles))
706         (setq frame (window-frame (get-buffer-window (current-buffer)))))
707       (message-goto-body)
708       (while (setq article (pop articles))
709         (save-window-excursion
710           (set-buffer gnus-summary-buffer)
711           (gnus-summary-select-article nil nil nil article)
712           (gnus-summary-remove-process-mark article))
713         (when frame
714           (select-frame frame))
715         (insert (mime-make-tag "message" "rfc822") "\n")
716         (insert-buffer-substring gnus-original-article-buffer))
717       (push-mark)
718       (message-goto-body)
719       (mime-edit-enclose-digest-region (point)(mark t)))))
720
721 (defun gnus-summary-post-digest (&optional n)
722   "Digest and forwards all articles in this series to a newsgroup."
723   (interactive "P")
724   (gnus-summary-mail-digest n t))
725
726 (defun gnus-summary-resend-message (address n)
727   "Resend the current article to ADDRESS."
728   (interactive "sResend message(s) to: \nP")
729   (let ((articles (gnus-summary-work-articles n))
730         article)
731     (while (setq article (pop articles))
732       (gnus-summary-select-article nil nil nil article)
733       (save-excursion
734         (set-buffer gnus-original-article-buffer)
735         (message-resend address)))))
736
737 (defvar gnus-nastygram-message
738   "The following article was inappropriately posted to %s.\n\n"
739   "Format string to insert in nastygrams.
740 The current group name will be inserted at \"%s\".")
741
742 (defun gnus-summary-mail-nastygram (n)
743   "Send a nastygram to the author of the current article."
744   (interactive "P")
745   (when (or gnus-expert-user
746             (gnus-y-or-n-p
747              "Really send a nastygram to the author of the current article? "))
748     (let ((group gnus-newsgroup-name))
749       (gnus-summary-reply-with-original n)
750       (set-buffer gnus-message-buffer)
751       (message-goto-body)
752       (insert (format gnus-nastygram-message group))
753       (message-send-and-exit))))
754
755 (defun gnus-summary-mail-crosspost-complaint (n)
756   "Send a complaint about crossposting to the current article(s)."
757   (interactive "P")
758   (let ((articles (gnus-summary-work-articles n))
759         article)
760     (while (setq article (pop articles))
761       (set-buffer gnus-summary-buffer)
762       (gnus-summary-goto-subject article)
763       (let ((group (gnus-group-real-name gnus-newsgroup-name))
764             newsgroups followup-to)
765         (gnus-summary-select-article)
766         (set-buffer gnus-original-article-buffer)
767         (if (and (<= (length (message-tokenize-header
768                               (setq newsgroups (mail-fetch-field "newsgroups"))
769                               ", "))
770                      1)
771                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
772                      (not (member group (message-tokenize-header
773                                          followup-to ", ")))))
774             (if followup-to
775                 (gnus-message 1 "Followup-to restricted")
776               (gnus-message 1 "Not a crossposted article"))
777           (set-buffer gnus-summary-buffer)
778           (gnus-summary-reply-with-original 1)
779           (set-buffer gnus-message-buffer)
780           (message-goto-body)
781           (insert (format gnus-crosspost-complaint newsgroups group))
782           (message-goto-subject)
783           (re-search-forward " *$")
784           (replace-match " (crosspost notification)" t t)
785           (gnus-deactivate-mark)
786           (when (gnus-y-or-n-p "Send this complaint? ")
787             (message-send-and-exit)))))))
788
789 (defun gnus-summary-mail-other-window ()
790   "Compose mail in other window."
791   (interactive)
792   (gnus-setup-message 'message
793     (message-mail)))
794
795 (defun gnus-mail-parse-comma-list ()
796   (let (accumulated
797         beg)
798     (skip-chars-forward " ")
799     (while (not (eobp))
800       (setq beg (point))
801       (skip-chars-forward "^,")
802       (while (zerop
803               (save-excursion
804                 (save-restriction
805                   (let ((i 0))
806                     (narrow-to-region beg (point))
807                     (goto-char beg)
808                     (logand (progn
809                               (while (search-forward "\"" nil t)
810                                 (incf i))
811                               (if (zerop i) 2 i))
812                             2)))))
813         (skip-chars-forward ",")
814         (skip-chars-forward "^,"))
815       (skip-chars-backward " ")
816       (push (buffer-substring beg (point))
817             accumulated)
818       (skip-chars-forward "^,")
819       (skip-chars-forward ", "))
820     accumulated))
821
822 (defun gnus-inews-add-to-address (group)
823   (let ((to-address (mail-fetch-field "to")))
824     (when (and to-address
825                (gnus-alive-p))
826       ;; This mail group doesn't have a `to-list', so we add one
827       ;; here.  Magic!
828       (when (gnus-y-or-n-p
829              (format "Do you want to add this as `to-list': %s " to-address))
830         (gnus-group-add-parameter group (cons 'to-list to-address))))))
831
832 (defun gnus-put-message ()
833   "Put the current message in some group and return to Gnus."
834   (interactive)
835   (let ((reply gnus-article-reply)
836         (winconf gnus-prev-winconf)
837         (group gnus-newsgroup-name))
838
839     (or (and group (not (gnus-group-read-only-p group)))
840         (setq group (read-string "Put in group: " nil
841                                  (gnus-writable-groups))))
842     (when (gnus-gethash group gnus-newsrc-hashtb)
843       (error "No such group: %s" group))
844
845     (save-excursion
846       (save-restriction
847         (widen)
848         (message-narrow-to-headers)
849         (let (gnus-deletable-headers)
850           (if (message-news-p)
851               (message-generate-headers message-required-news-headers)
852             (message-generate-headers message-required-mail-headers)))
853         (goto-char (point-max))
854         (insert "Gcc: " group "\n")
855         (widen)))
856
857     (gnus-inews-do-gcc)
858
859     (when (get-buffer gnus-group-buffer)
860       (when (gnus-buffer-exists-p (car-safe reply))
861         (set-buffer (car reply))
862         (and (cdr reply)
863              (gnus-summary-mark-article-as-replied
864               (cdr reply))))
865       (when winconf
866         (set-window-configuration winconf)))))
867
868 (defun gnus-article-mail (yank)
869   "Send a reply to the address near point.
870 If YANK is non-nil, include the original article."
871   (interactive "P")
872   (let ((address
873          (buffer-substring
874           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
875           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
876     (when address
877       (message-reply address)
878       (when yank
879         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
880
881 (defvar nntp-server-type)
882 (defun gnus-bug ()
883   "Send a bug report to the Gnus maintainers."
884   (interactive)
885   (unless (gnus-alive-p)
886     (error "Gnus has been shut down"))
887   (gnus-setup-message 'bug
888     (delete-other-windows)
889     (when gnus-bug-create-help-buffer
890       (switch-to-buffer "*Gnus Help Bug*")
891       (erase-buffer)
892       (insert gnus-bug-message)
893       (goto-char (point-min)))
894     (message-pop-to-buffer "*Gnus Bug*")
895     (message-setup
896      `((To . ,semi-gnus-developers) (Subject . "")))
897     (when gnus-bug-create-help-buffer
898       (push `(gnus-bug-kill-buffer) message-send-actions))
899     (goto-char (point-min))
900     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
901     (forward-line 1)
902     (insert (gnus-version) "\n"
903             (emacs-version) "\n")
904     (when (and (boundp 'nntp-server-type)
905                (stringp nntp-server-type))
906       (insert nntp-server-type))
907     (insert "\n\n\n\n\n")
908     (gnus-debug)
909     (goto-char (point-min))
910     (search-forward "Subject: " nil t)
911     (message "")))
912
913 (defun gnus-bug-kill-buffer ()
914   (when (get-buffer "*Gnus Help Bug*")
915     (kill-buffer "*Gnus Help Bug*")))
916
917 (defun gnus-debug ()
918   "Attempts to go through the Gnus source file and report what variables have been changed.
919 The source file has to be in the Emacs load path."
920   (interactive)
921   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
922                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
923                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
924                  "nnmail.el" "message.el"))
925         (point (point))
926         file expr olist sym)
927     (gnus-message 4 "Please wait while we snoop your variables...")
928     (sit-for 0)
929     ;; Go through all the files looking for non-default values for variables.
930     (save-excursion
931       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
932       (buffer-disable-undo (current-buffer))
933       (while files
934         (erase-buffer)
935         (when (and (setq file (locate-library (pop files)))
936                    (file-exists-p file))
937           (insert-file-contents file)
938           (goto-char (point-min))
939           (if (not (re-search-forward "^;;* *Internal variables" nil t))
940               (gnus-message 4 "Malformed sources in file %s" file)
941             (narrow-to-region (point-min) (point))
942             (goto-char (point-min))
943             (while (setq expr (ignore-errors (read (current-buffer))))
944               (ignore-errors
945                 (and (or (eq (car expr) 'defvar)
946                          (eq (car expr) 'defcustom))
947                      (stringp (nth 3 expr))
948                      (or (not (boundp (nth 1 expr)))
949                          (not (equal (eval (nth 2 expr))
950                                      (symbol-value (nth 1 expr)))))
951                      (push (nth 1 expr) olist)))))))
952       (kill-buffer (current-buffer)))
953     (when (setq olist (nreverse olist))
954       (insert "------------------ Environment follows ------------------\n\n"))
955     (while olist
956       (if (boundp (car olist))
957           (condition-case ()
958               (pp `(setq ,(car olist)
959                          ,(if (or (consp (setq sym (symbol-value (car olist))))
960                                   (and (symbolp sym)
961                                        (not (or (eq sym nil)
962                                                 (eq sym t)))))
963                               (list 'quote (symbol-value (car olist)))
964                             (symbol-value (car olist))))
965                   (current-buffer))
966             (error
967              (format "(setq %s 'whatever)\n" (car olist))))
968         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
969       (setq olist (cdr olist)))
970     (insert "\n\n")
971     ;; Remove any control chars - they seem to cause trouble for some
972     ;; mailers.  (Byte-compiled output from the stuff above.)
973     (goto-char point)
974     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
975       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
976                      t t))))
977
978 ;;; Treatment of rejected articles.
979 ;;; Bounced mail.
980
981 (defun gnus-summary-resend-bounced-mail (&optional fetch)
982   "Re-mail the current message.
983 This only makes sense if the current message is a bounce message than
984 contains some mail you have written which has been bounced back to
985 you.
986 If FETCH, try to fetch the article that this is a reply to, if indeed
987 this is a reply."
988   (interactive "P")
989   (gnus-summary-select-article t)
990   (set-buffer gnus-original-article-buffer)
991   (let ((gnus-message-setup-hook '(gnus-maybe-setup-default-charset)))
992     (gnus-setup-message 'compose-bounce
993       (let* ((references (mail-fetch-field "references"))
994              (parent (and references (gnus-parent-id references))))
995         (message-bounce)
996         ;; If there are references, we fetch the article we answered to.
997         (and fetch parent
998              (gnus-summary-refer-article parent)
999              (gnus-summary-show-all-headers))))))
1000
1001 ;;; Gcc handling.
1002
1003 ;; Do Gcc handling, which copied the message over to some group.
1004 (defun gnus-inews-do-gcc (&optional gcc)
1005   (interactive)
1006   (when (gnus-alive-p)
1007     (save-excursion
1008       (save-restriction
1009         (message-narrow-to-headers)
1010         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1011               (coding-system-for-write 'raw-text)
1012               groups group method)
1013           (when gcc
1014             (message-remove-header "gcc")
1015             (widen)
1016             (setq groups (message-tokenize-header gcc " ,"))
1017             ;; Copy the article over to some group(s).
1018             (while (setq group (pop groups))
1019               (gnus-check-server
1020                (setq method
1021                      (cond ((and (null (gnus-get-info group))
1022                                  (eq (car gnus-message-archive-method)
1023                                      (car
1024                                       (gnus-server-to-method
1025                                        (gnus-group-method group)))))
1026                             ;; If the group doesn't exist, we assume
1027                             ;; it's an archive group...
1028                             gnus-message-archive-method)
1029                            ;; Use the method.
1030                            ((gnus-info-method (gnus-get-info group))
1031                             (gnus-info-method (gnus-get-info group)))
1032                            ;; Find the method.
1033                            (t (gnus-group-method group)))))
1034               (gnus-check-server method)
1035               (unless (gnus-request-group group t method)
1036                 (gnus-request-create-group group method))
1037               (save-excursion
1038                 (nnheader-set-temp-buffer " *acc*")
1039                 (insert-buffer-substring message-encoding-buffer)
1040                 (gnus-run-hooks 'gnus-before-do-gcc-hook)
1041                 (goto-char (point-min))
1042                 (when (re-search-forward
1043                        (concat "^" (regexp-quote mail-header-separator) "$")
1044                        nil t)
1045                   (replace-match "" t t ))
1046                 (unless (gnus-request-accept-article group method t)
1047                   (gnus-message 1 "Couldn't store article in group %s: %s"
1048                                 group (gnus-status-message method))
1049                   (sit-for 2))
1050                 (kill-buffer (current-buffer))))))))))
1051
1052 (defun gnus-inews-insert-gcc ()
1053   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1054   (save-excursion
1055     (save-restriction
1056       (message-narrow-to-headers)
1057       (let* ((group gnus-outgoing-message-group)
1058              (gcc (cond
1059                    ((gnus-functionp group)
1060                     (funcall group))
1061                    ((or (stringp group) (list group))
1062                     group))))
1063         (when gcc
1064           (insert "Gcc: "
1065                   (if (stringp gcc) gcc
1066                     (mapconcat 'identity gcc " "))
1067                   "\n"))))))
1068
1069 (defun gnus-inews-insert-archive-gcc (&optional group)
1070   "Insert the Gcc to say where the article is to be archived."
1071   (let* ((var gnus-message-archive-group)
1072          (group (or group gnus-newsgroup-name ""))
1073          (gcc-self-val
1074           (and gnus-newsgroup-name
1075                (gnus-group-find-parameter
1076                 gnus-newsgroup-name 'gcc-self)))
1077          result 
1078          (groups
1079           (cond
1080            ((null gnus-message-archive-method)
1081             ;; Ignore.
1082             nil)
1083            ((stringp var)
1084             ;; Just a single group.
1085             (list var))
1086            ((null var)
1087             ;; We don't want this.
1088             nil)
1089            ((and (listp var) (stringp (car var)))
1090             ;; A list of groups.
1091             var)
1092            ((gnus-functionp var)
1093             ;; A function.
1094             (funcall var group))
1095            (t
1096             ;; An alist of regexps/functions/forms.
1097             (while (and var
1098                         (not
1099                          (setq result
1100                                (cond
1101                                 ((stringp (caar var))
1102                                  ;; Regexp.
1103                                  (when (string-match (caar var) group)
1104                                    (cdar var)))
1105                                 ((gnus-functionp (car var))
1106                                  ;; Function.
1107                                  (funcall (car var) group))
1108                                 (t
1109                                  (eval (car var)))))))
1110               (setq var (cdr var)))
1111             result)))
1112          name)
1113     (when (or groups gcc-self-val)
1114       (when (stringp groups)
1115         (setq groups (list groups)))
1116       (save-excursion
1117         (save-restriction
1118           (message-narrow-to-headers)
1119           (goto-char (point-max))
1120           (insert "Gcc: ")
1121           (if gcc-self-val
1122               ;; Use the `gcc-self' param value instead.
1123               (progn
1124                 (insert
1125                  (if (stringp gcc-self-val)
1126                      gcc-self-val
1127                    group))
1128                 (if (not (eq gcc-self-val 'none))
1129                     (insert "\n")
1130                   (progn
1131                     (beginning-of-line)
1132                     (kill-line))))
1133             ;; Use the list of groups.
1134             (while (setq name (pop groups))
1135               (insert (if (string-match ":" name)
1136                           name
1137                         (gnus-group-prefixed-name
1138                          name gnus-message-archive-method)))
1139               (when groups
1140                 (insert " ")))
1141             (insert "\n")))))))
1142
1143 ;;; Posting styles.
1144
1145 (defvar gnus-message-style-insertions nil)
1146
1147 (defun gnus-configure-posting-styles ()
1148   "Configure posting styles according to `gnus-posting-styles'."
1149   (unless gnus-inhibit-posting-styles
1150     (let ((styles gnus-posting-styles)
1151           (gnus-newsgroup-name (or gnus-newsgroup-name ""))
1152           style match variable attribute value value-value)
1153       (make-local-variable 'gnus-message-style-insertions)
1154       ;; Go through all styles and look for matches.
1155       (while styles
1156         (setq style (pop styles)
1157               match (pop style))
1158         (when (cond ((stringp match)
1159                      ;; Regexp string match on the group name.
1160                      (string-match match gnus-newsgroup-name))
1161                     ((or (symbolp match)
1162                          (gnus-functionp match))
1163                      (cond ((gnus-functionp match)
1164                             ;; Function to be called.
1165                             (funcall match))
1166                            ((boundp match)
1167                             ;; Variable to be checked.
1168                             (symbol-value match))))
1169                     ((listp match)
1170                      ;; This is a form to be evaled.
1171                      (eval match)))
1172           ;; We have a match, so we set the variables.
1173           (while style
1174             (setq attribute (pop style)
1175                   value (cadr attribute)
1176                   variable nil)
1177             ;; We find the variable that is to be modified.
1178             (if (and (not (stringp (car attribute)))
1179                      (not (eq 'body (car attribute)))
1180                      (not (setq variable
1181                                 (cdr (assq (car attribute) 
1182                                            gnus-posting-style-alist)))))
1183                 (message "Couldn't find attribute %s" (car attribute))
1184               ;; We get the value.
1185               (setq value-value
1186                     (cond ((stringp value)
1187                            value)
1188                           ((or (symbolp value)
1189                                (gnus-functionp value))
1190                            (cond ((gnus-functionp value)
1191                                   (funcall value))
1192                                  ((boundp value)
1193                                   (symbol-value value))))
1194                           ((listp value)
1195                            (eval value))))
1196               (if variable
1197                   ;; This is an ordinary variable.
1198                   (set (make-local-variable variable) value-value)
1199                 ;; This is either a body or a header to be inserted in the
1200                 ;; message.
1201                 (when value-value
1202                   (let ((attr (car attribute)))
1203                     (make-local-variable 'message-setup-hook)
1204                     (if (eq 'body attr)
1205                         (add-hook 'message-setup-hook
1206                                   `(lambda ()
1207                                      (save-excursion
1208                                        (message-goto-body)
1209                                        (insert ,value-value))))
1210                       (add-hook 'message-setup-hook
1211                                 'gnus-message-insert-stylings)
1212                       (push (cons (if (stringp attr) attr
1213                                     (symbol-name attr))
1214                                   value-value)
1215                             gnus-message-style-insertions))))))))))))
1216
1217 (defun gnus-message-insert-stylings ()
1218   (let (val)
1219     (save-excursion
1220       (message-goto-eoh)
1221       (while (setq val (pop gnus-message-style-insertions))
1222         (when (cdr val)
1223           (insert (car val) ": " (cdr val) "\n"))
1224         (gnus-pull (car val) gnus-message-style-insertions)))))
1225
1226
1227 ;;; @ for MIME Edit mode
1228 ;;;
1229
1230 (defun gnus-maybe-setup-default-charset ()
1231   (let ((charset
1232          (and (boundp 'gnus-summary-buffer)
1233               (buffer-live-p gnus-summary-buffer)
1234               (save-excursion
1235                 (set-buffer gnus-summary-buffer)
1236                 default-mime-charset))))
1237     (if charset
1238         (progn
1239           (make-local-variable 'default-mime-charset)
1240           (setq default-mime-charset charset)
1241           ))))
1242
1243
1244 ;;; Allow redefinition of functions.
1245
1246 (gnus-ems-redefine)
1247
1248 (provide 'gnus-msg)
1249
1250 ;;; gnus-msg.el ends here