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