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