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