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