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