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