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