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