4a79dbfa976d4796a83ea7547d5937072e938a20
[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   (goto-char (point-min))
535   (let ((mail-header-separator
536          (progn
537            (goto-char (point-min))
538            (if (and (search-forward (concat "\n" mail-header-separator "\n")
539                                     nil t)
540                     (not (search-backward "\n\n" nil t)))
541                mail-header-separator
542              ""))))
543     (or (mail-position-on-field "Mime-Version")
544         (insert "1.0")
545         (cond ((save-restriction
546                  (widen)
547                  (goto-char (point-min))
548                  (re-search-forward "[\200-\377]" nil t))
549                (or (mail-position-on-field "Content-Type")
550                    (insert "text/plain; charset=ISO-8859-1"))
551                (or (mail-position-on-field "Content-Transfer-Encoding")
552                    (insert "8bit")))
553               (t (or (mail-position-on-field "Content-Type")
554                      (insert "text/plain; charset=US-ASCII"))
555                  (or (mail-position-on-field "Content-Transfer-Encoding")
556                      (insert "7bit")))))))
557
558 \f
559 ;;;
560 ;;; Gnus Mail Functions
561 ;;;
562
563 ;;; Mail reply commands of Gnus summary mode
564
565 (defun gnus-summary-reply (&optional yank wide)
566   "Start composing a reply mail to the current message.
567 If prefix argument YANK is non-nil, the original article is yanked
568 automatically."
569   (interactive
570    (list (and current-prefix-arg
571               (gnus-summary-work-articles 1))))
572   ;; Stripping headers should be specified with mail-yank-ignored-headers.
573   (when yank
574     (gnus-summary-goto-subject (car yank)))
575   (let ((gnus-article-reply t))
576     (gnus-setup-message (if yank 'reply-yank 'reply)
577       (gnus-summary-select-article)
578       (set-buffer (gnus-copy-article-buffer))
579       (message-reply nil wide (gnus-group-find-parameter
580                                gnus-newsgroup-name 'broken-reply-to))
581       (when yank
582         (gnus-inews-yank-articles yank)))))
583
584 (defun gnus-summary-reply-with-original (n &optional wide)
585   "Start composing a reply mail to the current message.
586 The original article will be yanked."
587   (interactive "P")
588   (gnus-summary-reply (gnus-summary-work-articles n) wide))
589
590 (defun gnus-summary-wide-reply (&optional yank)
591   "Start composing a wide reply mail to the current message.
592 If prefix argument YANK is non-nil, the original article is yanked
593 automatically."
594   (interactive
595    (list (and current-prefix-arg
596               (gnus-summary-work-articles 1))))
597   (gnus-summary-reply yank t))
598
599 (defun gnus-summary-wide-reply-with-original (n)
600   "Start composing a wide reply mail to the current message.
601 The original article will be yanked."
602   (interactive "P")
603   (gnus-summary-reply-with-original n t))
604
605 (defun gnus-summary-mail-forward (&optional full-headers post)
606   "Forward the current message to another user.
607 If FULL-HEADERS (the prefix), include full headers when forwarding."
608   (interactive "P")
609   (gnus-setup-message 'forward
610     (gnus-summary-select-article)
611     (set-buffer gnus-original-article-buffer)
612     (let ((message-included-forward-headers
613            (if full-headers "" message-included-forward-headers)))
614       (message-forward post))))
615
616 (defun gnus-summary-resend-message (address n)
617   "Resend the current article to ADDRESS."
618   (interactive "sResend message(s) to: \nP")
619   (let ((articles (gnus-summary-work-articles n))
620         article)
621     (while (setq article (pop articles))
622       (gnus-summary-select-article nil nil nil article)
623       (save-excursion
624         (set-buffer gnus-original-article-buffer)
625         (message-resend address)))))
626
627 (defun gnus-summary-post-forward (&optional full-headers)
628   "Forward the current article to a newsgroup.
629 If FULL-HEADERS (the prefix), include full headers when forwarding."
630   (interactive "P")
631   (gnus-summary-mail-forward full-headers t))
632
633 (defvar gnus-nastygram-message
634   "The following article was inappropriately posted to %s.\n\n"
635   "Format string to insert in nastygrams.
636 The current group name will be inserted at \"%s\".")
637
638 (defun gnus-summary-mail-nastygram (n)
639   "Send a nastygram to the author of the current article."
640   (interactive "P")
641   (when (or gnus-expert-user
642             (gnus-y-or-n-p
643              "Really send a nastygram to the author of the current article? "))
644     (let ((group gnus-newsgroup-name))
645       (gnus-summary-reply-with-original n)
646       (set-buffer gnus-message-buffer)
647       (message-goto-body)
648       (insert (format gnus-nastygram-message group))
649       (message-send-and-exit))))
650
651 (defun gnus-summary-mail-crosspost-complaint (n)
652   "Send a complaint about crossposting to the current article(s)."
653   (interactive "P")
654   (let ((articles (gnus-summary-work-articles n))
655         article)
656     (while (setq article (pop articles))
657       (set-buffer gnus-summary-buffer)
658       (gnus-summary-goto-subject article)
659       (let ((group (gnus-group-real-name gnus-newsgroup-name))
660             newsgroups followup-to)
661         (gnus-summary-select-article)
662         (set-buffer gnus-original-article-buffer)
663         (if (and (<= (length (message-tokenize-header
664                               (setq newsgroups (mail-fetch-field "newsgroups"))
665                               ", "))
666                      1)
667                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
668                      (not (member group (message-tokenize-header
669                                          followup-to ", ")))))
670             (if followup-to
671                 (gnus-message 1 "Followup-to restricted")
672               (gnus-message 1 "Not a crossposted article"))
673           (set-buffer gnus-summary-buffer)
674           (gnus-summary-reply-with-original 1)
675           (set-buffer gnus-message-buffer)
676           (message-goto-body)
677           (insert (format gnus-crosspost-complaint newsgroups group))
678           (message-goto-subject)
679           (re-search-forward " *$")
680           (replace-match " (crosspost notification)" t t)
681           (gnus-deactivate-mark)
682           (when (gnus-y-or-n-p "Send this complaint? ")
683             (message-send-and-exit)))))))
684
685 (defun gnus-summary-mail-other-window ()
686   "Compose mail in other window."
687   (interactive)
688   (gnus-setup-message 'message
689     (message-mail)))
690
691 (defun gnus-mail-parse-comma-list ()
692   (let (accumulated
693         beg)
694     (skip-chars-forward " ")
695     (while (not (eobp))
696       (setq beg (point))
697       (skip-chars-forward "^,")
698       (while (zerop
699               (save-excursion
700                 (save-restriction
701                   (let ((i 0))
702                     (narrow-to-region beg (point))
703                     (goto-char beg)
704                     (logand (progn
705                               (while (search-forward "\"" nil t)
706                                 (incf i))
707                               (if (zerop i) 2 i))
708                             2)))))
709         (skip-chars-forward ",")
710         (skip-chars-forward "^,"))
711       (skip-chars-backward " ")
712       (push (buffer-substring beg (point))
713             accumulated)
714       (skip-chars-forward "^,")
715       (skip-chars-forward ", "))
716     accumulated))
717
718 (defun gnus-inews-add-to-address (group)
719   (let ((to-address (mail-fetch-field "to")))
720     (when (and to-address
721                (gnus-alive-p))
722       ;; This mail group doesn't have a `to-list', so we add one
723       ;; here.  Magic!
724       (when (gnus-y-or-n-p
725              (format "Do you want to add this as `to-list': %s " to-address))
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         (message-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 (get-buffer-create "*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" "gnus-topic.el"
817                  "nnmail.el" "message.el"))
818         (point (point))
819         file expr olist sym)
820     (gnus-message 4 "Please wait while we snoop your variables...")
821     (sit-for 0)
822     ;; Go through all the files looking for non-default values for variables.
823     (save-excursion
824       (set-buffer (get-buffer-create " *gnus bug info*"))
825       (buffer-disable-undo (current-buffer))
826       (while files
827         (erase-buffer)
828         (when (and (setq file (locate-library (pop files)))
829                    (file-exists-p file))
830           (insert-file-contents file)
831           (goto-char (point-min))
832           (if (not (re-search-forward "^;;* *Internal variables" nil t))
833               (gnus-message 4 "Malformed sources in file %s" file)
834             (narrow-to-region (point-min) (point))
835             (goto-char (point-min))
836             (while (setq expr (ignore-errors (read (current-buffer))))
837               (ignore-errors
838                 (and (or (eq (car expr) 'defvar)
839                          (eq (car expr) 'defcustom))
840                      (stringp (nth 3 expr))
841                      (or (not (boundp (nth 1 expr)))
842                          (not (equal (eval (nth 2 expr))
843                                      (symbol-value (nth 1 expr)))))
844                      (push (nth 1 expr) olist)))))))
845       (kill-buffer (current-buffer)))
846     (when (setq olist (nreverse olist))
847       (insert "------------------ Environment follows ------------------\n\n"))
848     (while olist
849       (if (boundp (car olist))
850           (condition-case ()
851               (pp `(setq ,(car olist)
852                          ,(if (or (consp (setq sym (symbol-value (car olist))))
853                                   (and (symbolp sym)
854                                        (not (or (eq sym nil)
855                                                 (eq sym t)))))
856                               (list 'quote (symbol-value (car olist)))
857                             (symbol-value (car olist))))
858                   (current-buffer))
859             (error
860              (format "(setq %s 'whatever)\n" (car olist))))
861         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
862       (setq olist (cdr olist)))
863     (insert "\n\n")
864     ;; Remove any control chars - they seem to cause trouble for some
865     ;; mailers.  (Byte-compiled output from the stuff above.)
866     (goto-char point)
867     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
868       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
869                      t t))))
870
871 ;;; Treatment of rejected articles.
872 ;;; Bounced mail.
873
874 (defun gnus-summary-resend-bounced-mail (&optional fetch)
875   "Re-mail the current message.
876 This only makes sense if the current message is a bounce message than
877 contains some mail you have written which has been bounced back to
878 you.
879 If FETCH, try to fetch the article that this is a reply to, if indeed
880 this is a reply."
881   (interactive "P")
882   (gnus-summary-select-article t)
883   (set-buffer gnus-original-article-buffer)
884   (gnus-setup-message 'compose-bounce
885     (let* ((references (mail-fetch-field "references"))
886            (parent (and references (gnus-parent-id references))))
887       (message-bounce)
888       ;; If there are references, we fetch the article we answered to.
889       (and fetch parent
890            (gnus-summary-refer-article parent)
891            (gnus-summary-show-all-headers)))))
892
893 ;;; Gcc handling.
894
895 ;; Do Gcc handling, which copied the message over to some group.
896 (defun gnus-inews-do-gcc (&optional gcc)
897   (interactive)
898   (when (gnus-alive-p)
899     (save-excursion
900       (save-restriction
901         (message-narrow-to-headers)
902         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
903               (coding-system-for-write 'raw-text)
904               groups group method)
905           (when gcc
906             (message-remove-header "gcc")
907             (widen)
908             (setq groups (message-tokenize-header gcc " ,"))
909             ;; Copy the article over to some group(s).
910             (while (setq group (pop groups))
911               (gnus-check-server
912                (setq method
913                      (cond ((and (null (gnus-get-info group))
914                                  (eq (car gnus-message-archive-method)
915                                      (car
916                                       (gnus-server-to-method
917                                        (gnus-group-method group)))))
918                             ;; If the group doesn't exist, we assume
919                             ;; it's an archive group...
920                             gnus-message-archive-method)
921                            ;; Use the method.
922                            ((gnus-info-method (gnus-get-info group))
923                             (gnus-info-method (gnus-get-info group)))
924                            ;; Find the method.
925                            (t (gnus-group-method group)))))
926               (gnus-check-server method)
927               (unless (gnus-request-group group t method)
928                 (gnus-request-create-group group method))
929               (save-excursion
930                 (nnheader-set-temp-buffer " *acc*")
931                 (insert-buffer-substring message-encoding-buffer)
932                 (run-hooks 'gnus-before-do-gcc-hook)
933                 (goto-char (point-min))
934                 (when (re-search-forward
935                        (concat "^" (regexp-quote mail-header-separator) "$")
936                        nil t)
937                   (replace-match "" t t ))
938                 (unless (gnus-request-accept-article group method t)
939                   (gnus-message 1 "Couldn't store article in group %s: %s"
940                                 group (gnus-status-message method))
941                   (sit-for 2))
942                 (kill-buffer (current-buffer))))))))))
943
944 (defun gnus-inews-insert-gcc ()
945   "Insert Gcc headers based on `gnus-outgoing-message-group'."
946   (save-excursion
947     (save-restriction
948       (message-narrow-to-headers)
949       (let* ((group gnus-outgoing-message-group)
950              (gcc (cond
951                    ((gnus-functionp group)
952                     (funcall group))
953                    ((or (stringp group) (list group))
954                     group))))
955         (when gcc
956           (insert "Gcc: "
957                   (if (stringp gcc) gcc
958                     (mapconcat 'identity gcc " "))
959                   "\n"))))))
960
961 (defun gnus-inews-insert-archive-gcc (&optional group)
962   "Insert the Gcc to say where the article is to be archived."
963   (let* ((var gnus-message-archive-group)
964          (group (or group gnus-newsgroup-name ""))
965          (gcc-self-val
966           (and gnus-newsgroup-name
967                (gnus-group-find-parameter
968                 gnus-newsgroup-name 'gcc-self)))
969          result 
970          (groups
971           (cond
972            ((null gnus-message-archive-method)
973             ;; Ignore.
974             nil)
975            ((stringp var)
976             ;; Just a single group.
977             (list var))
978            ((null var)
979             ;; We don't want this.
980             nil)
981            ((and (listp var) (stringp (car var)))
982             ;; A list of groups.
983             var)
984            ((gnus-functionp var)
985             ;; A function.
986             (funcall var group))
987            (t
988             ;; An alist of regexps/functions/forms.
989             (while (and var
990                         (not
991                          (setq result
992                                (cond
993                                 ((stringp (caar var))
994                                  ;; Regexp.
995                                  (when (string-match (caar var) group)
996                                    (cdar var)))
997                                 ((gnus-functionp (car var))
998                                  ;; Function.
999                                  (funcall (car var) group))
1000                                 (t
1001                                  (eval (car var)))))))
1002               (setq var (cdr var)))
1003             result)))
1004          name)
1005     (when (or groups gcc-self-val)
1006       (when (stringp groups)
1007         (setq groups (list groups)))
1008       (save-excursion
1009         (save-restriction
1010           (message-narrow-to-headers)
1011           (goto-char (point-max))
1012           (insert "Gcc: ")
1013           (if gcc-self-val
1014               ;; Use the `gcc-self' param value instead.
1015               (progn
1016                 (insert
1017                  (if (stringp gcc-self-val)
1018                      gcc-self-val
1019                    group))
1020                 (if (not (eq gcc-self-val 'none))
1021                     (insert "\n")
1022                   (progn
1023                     (beginning-of-line)
1024                     (kill-line))))
1025             ;; Use the list of groups.
1026             (while (setq name (pop groups))
1027               (insert (if (string-match ":" name)
1028                           name
1029                         (gnus-group-prefixed-name
1030                          name gnus-message-archive-method)))
1031               (when groups
1032                 (insert " ")))
1033             (insert "\n")))))))
1034
1035 (gnus-add-shutdown 'gnus-inews-close 'gnus)
1036
1037 (defun gnus-inews-close ()
1038   (setq gnus-inews-sent-ids nil))
1039
1040 ;;; Allow redefinition of functions.
1041
1042 (gnus-ems-redefine)
1043
1044 (provide 'gnus-msg)
1045
1046 ;;; gnus-msg.el ends here