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