Merge qgnus-0.23.
[elisp/gnus.git-] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Semi-gnus
2 ;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;         Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-ems)
34 (require 'message)
35 (require 'gnus-art)
36
37 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
38 (defvar gnus-post-method nil
39   "*Preferred method for posting USENET news.
40 If this variable is nil, Gnus will use the current method to decide
41 which method to use when posting.  If it is non-nil, it will override
42 the current method.  This method will not be used in mail groups and
43 the like, only in \"real\" newsgroups.
44
45 The value must be a valid method as discussed in the documentation of
46 `gnus-select-method'.  It can also be a list of methods.  If that is
47 the case, the user will be queried for what select method to use when
48 posting.")
49
50 (defvar gnus-outgoing-message-group nil
51   "*All outgoing messages will be put in this group.
52 If you want to store all your outgoing mail and articles in the group
53 \"nnml:archive\", you set this variable to that value.  This variable
54 can also be a list of group names.
55
56 If you want to have greater control over what group to put each
57 message in, you can set this variable to a function that checks the
58 current newsgroup name and then returns a suitable group name (or list
59 of names).")
60
61 (defvar gnus-mailing-list-groups nil
62   "*Regexp matching groups that are really mailing lists.
63 This is useful when you're reading a mailing list that has been
64 gatewayed to a newsgroup, and you want to followup to an article in
65 the group.")
66
67 (defvar gnus-add-to-list nil
68   "*If non-nil, add a `to-list' parameter automatically.")
69
70 (defvar gnus-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          (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 ;; Written by "Mr. Per Persson" <pp@gnu.ai.mit.edu>.
496 ;; (defun gnus-inews-insert-mime-headers ()
497 ;;   "Insert MIME headers.  
498 ;; Assumes ISO-Latin-1 is used iff 8-bit characters are present."
499 ;;   (goto-char (point-min))
500 ;;   (let ((mail-header-separator
501 ;;          (progn
502 ;;            (goto-char (point-min))
503 ;;            (if (and (search-forward (concat "\n" mail-header-separator "\n")
504 ;;                                     nil t)
505 ;;                     (not (search-backward "\n\n" nil t)))
506 ;;                mail-header-separator
507 ;;              ""))))
508 ;;     (or (mail-position-on-field "Mime-Version")
509 ;;         (insert "1.0")
510 ;;         (cond ((save-restriction
511 ;;                  (widen)
512 ;;                  (goto-char (point-min))
513 ;;                  (re-search-forward "[^\000-\177]" nil t))
514 ;;                (or (mail-position-on-field "Content-Type")
515 ;;                    (insert "text/plain; charset=ISO-8859-1"))
516 ;;                (or (mail-position-on-field "Content-Transfer-Encoding")
517 ;;                    (insert "8bit")))
518 ;;               (t (or (mail-position-on-field "Content-Type")
519 ;;                      (insert "text/plain; charset=US-ASCII"))
520 ;;                  (or (mail-position-on-field "Content-Transfer-Encoding")
521 ;;                      (insert "7bit")))))))
522
523 ;; (custom-add-option 'message-header-hook 'gnus-inews-insert-mime-headers)
524
525 \f
526 ;;;
527 ;;; Gnus Mail Functions
528 ;;;
529
530 ;;; Mail reply commands of Gnus summary mode
531
532 (defun gnus-summary-reply (&optional yank wide)
533   "Start composing a reply mail to the current message.
534 If prefix argument YANK is non-nil, the original article is yanked
535 automatically."
536   (interactive
537    (list (and current-prefix-arg
538               (gnus-summary-work-articles 1))))
539   ;; Stripping headers should be specified with mail-yank-ignored-headers.
540   (when yank
541     (gnus-summary-goto-subject (car yank)))
542   (let ((gnus-article-reply t))
543     (gnus-setup-message (if yank 'reply-yank 'reply)
544       (gnus-summary-select-article)
545       (set-buffer (gnus-copy-article-buffer))
546       (message-reply nil wide (gnus-group-find-parameter
547                                gnus-newsgroup-name 'broken-reply-to))
548       (when yank
549         (gnus-inews-yank-articles yank)))))
550
551 (defun gnus-summary-reply-with-original (n &optional wide)
552   "Start composing a reply mail to the current message.
553 The original article will be yanked."
554   (interactive "P")
555   (gnus-summary-reply (gnus-summary-work-articles n) wide))
556
557 (defun gnus-summary-wide-reply (&optional yank)
558   "Start composing a wide reply mail to the current message.
559 If prefix argument YANK is non-nil, the original article is yanked
560 automatically."
561   (interactive
562    (list (and current-prefix-arg
563               (gnus-summary-work-articles 1))))
564   (gnus-summary-reply yank t))
565
566 (defun gnus-summary-wide-reply-with-original (n)
567   "Start composing a wide reply mail to the current message.
568 The original article will be yanked."
569   (interactive "P")
570   (gnus-summary-reply-with-original n t))
571
572 (defun gnus-summary-mail-forward (&optional full-headers post)
573   "Forward the current message to another user.
574 If FULL-HEADERS (the prefix), include full headers when forwarding."
575   (interactive "P")
576   (gnus-setup-message 'forward
577     (gnus-summary-select-article)
578     (set-buffer gnus-original-article-buffer)
579     (let ((message-included-forward-headers
580            (if full-headers "" message-included-forward-headers)))
581       (message-forward post))))
582
583 (defun gnus-summary-resend-message (address n)
584   "Resend the current article to ADDRESS."
585   (interactive "sResend message(s) to: \nP")
586   (let ((articles (gnus-summary-work-articles n))
587         article)
588     (while (setq article (pop articles))
589       (gnus-summary-select-article nil nil nil article)
590       (save-excursion
591         (set-buffer gnus-original-article-buffer)
592         (message-resend address)))))
593
594 (defun gnus-summary-post-forward (&optional full-headers)
595   "Forward the current article to a newsgroup.
596 If FULL-HEADERS (the prefix), include full headers when forwarding."
597   (interactive "P")
598   (gnus-summary-mail-forward full-headers t))
599
600 (defvar gnus-nastygram-message
601   "The following article was inappropriately posted to %s.\n\n"
602   "Format string to insert in nastygrams.
603 The current group name will be inserted at \"%s\".")
604
605 (defun gnus-summary-mail-nastygram (n)
606   "Send a nastygram to the author of the current article."
607   (interactive "P")
608   (when (or gnus-expert-user
609             (gnus-y-or-n-p
610              "Really send a nastygram to the author of the current article? "))
611     (let ((group gnus-newsgroup-name))
612       (gnus-summary-reply-with-original n)
613       (set-buffer gnus-message-buffer)
614       (message-goto-body)
615       (insert (format gnus-nastygram-message group))
616       (message-send-and-exit))))
617
618 (defun gnus-summary-mail-crosspost-complaint (n)
619   "Send a complaint about crossposting to the current article(s)."
620   (interactive "P")
621   (let ((articles (gnus-summary-work-articles n))
622         article)
623     (while (setq article (pop articles))
624       (set-buffer gnus-summary-buffer)
625       (gnus-summary-goto-subject article)
626       (let ((group (gnus-group-real-name gnus-newsgroup-name))
627             newsgroups followup-to)
628         (gnus-summary-select-article)
629         (set-buffer gnus-original-article-buffer)
630         (if (and (<= (length (message-tokenize-header
631                               (setq newsgroups (mail-fetch-field "newsgroups"))
632                               ", "))
633                      1)
634                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
635                      (not (member group (message-tokenize-header
636                                          followup-to ", ")))))
637             (if followup-to
638                 (gnus-message 1 "Followup-to restricted")
639               (gnus-message 1 "Not a crossposted article"))
640           (set-buffer gnus-summary-buffer)
641           (gnus-summary-reply-with-original 1)
642           (set-buffer gnus-message-buffer)
643           (message-goto-body)
644           (insert (format gnus-crosspost-complaint newsgroups group))
645           (message-goto-subject)
646           (re-search-forward " *$")
647           (replace-match " (crosspost notification)" t t)
648           (gnus-deactivate-mark)
649           (when (gnus-y-or-n-p "Send this complaint? ")
650             (message-send-and-exit)))))))
651
652 (defun gnus-summary-mail-other-window ()
653   "Compose mail in other window."
654   (interactive)
655   (gnus-setup-message 'message
656     (message-mail)))
657
658 (defun gnus-mail-parse-comma-list ()
659   (let (accumulated
660         beg)
661     (skip-chars-forward " ")
662     (while (not (eobp))
663       (setq beg (point))
664       (skip-chars-forward "^,")
665       (while (zerop
666               (save-excursion
667                 (save-restriction
668                   (let ((i 0))
669                     (narrow-to-region beg (point))
670                     (goto-char beg)
671                     (logand (progn
672                               (while (search-forward "\"" nil t)
673                                 (incf i))
674                               (if (zerop i) 2 i))
675                             2)))))
676         (skip-chars-forward ",")
677         (skip-chars-forward "^,"))
678       (skip-chars-backward " ")
679       (push (buffer-substring beg (point))
680             accumulated)
681       (skip-chars-forward "^,")
682       (skip-chars-forward ", "))
683     accumulated))
684
685 (defun gnus-inews-add-to-address (group)
686   (let ((to-address (mail-fetch-field "to")))
687     (when (and to-address
688                (gnus-alive-p))
689       ;; This mail group doesn't have a `to-list', so we add one
690       ;; here.  Magic!
691       (when (gnus-y-or-n-p
692              (format "Do you want to add this as `to-list': %s " to-address))
693         (gnus-group-add-parameter group (cons 'to-list to-address))))))
694
695 (defun gnus-put-message ()
696   "Put the current message in some group and return to Gnus."
697   (interactive)
698   (let ((reply gnus-article-reply)
699         (winconf gnus-prev-winconf)
700         (group gnus-newsgroup-name))
701
702     (or (and group (not (gnus-group-read-only-p group)))
703         (setq group (read-string "Put in group: " nil
704                                  (gnus-writable-groups))))
705     (when (gnus-gethash group gnus-newsrc-hashtb)
706       (error "No such group: %s" group))
707
708     (save-excursion
709       (save-restriction
710         (widen)
711         (message-narrow-to-headers)
712         (let (gnus-deletable-headers)
713           (if (message-news-p)
714               (message-generate-headers message-required-news-headers)
715             (message-generate-headers message-required-mail-headers)))
716         (goto-char (point-max))
717         (insert "Gcc: " group "\n")
718         (widen)))
719
720     (gnus-inews-do-gcc)
721
722     (when (get-buffer gnus-group-buffer)
723       (when (gnus-buffer-exists-p (car-safe reply))
724         (set-buffer (car reply))
725         (and (cdr reply)
726              (gnus-summary-mark-article-as-replied
727               (cdr reply))))
728       (when winconf
729         (set-window-configuration winconf)))))
730
731 (defun gnus-article-mail (yank)
732   "Send a reply to the address near point.
733 If YANK is non-nil, include the original article."
734   (interactive "P")
735   (let ((address
736          (buffer-substring
737           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
738           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
739     (when address
740       (message-reply address)
741       (when yank
742         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
743
744 (defvar nntp-server-type)
745 (defun gnus-bug ()
746   "Send a bug report to the Gnus maintainers."
747   (interactive)
748   (unless (gnus-alive-p)
749     (error "Gnus has been shut down"))
750   (gnus-setup-message 'bug
751     (delete-other-windows)
752     (switch-to-buffer (get-buffer-create "*Gnus Help Bug*"))
753     (erase-buffer)
754     (insert gnus-bug-message)
755     (goto-char (point-min))
756     (message-pop-to-buffer "*Gnus Bug*")
757     (message-setup `((To . ,gnus-maintainer) (Subject . "")))
758     (push `(gnus-bug-kill-buffer) message-send-actions)
759     (goto-char (point-min))
760     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
761     (forward-line 1)
762     (insert (gnus-version) "\n")
763     (insert (emacs-version) "\n")
764     (when (and (boundp 'nntp-server-type)
765                (stringp nntp-server-type))
766       (insert nntp-server-type))
767     (insert "\n\n\n\n\n")
768     (gnus-debug)
769     (goto-char (point-min))
770     (search-forward "Subject: " nil t)
771     (message "")))
772
773 (defun gnus-bug-kill-buffer ()
774   (when (get-buffer "*Gnus Help Bug*")
775     (kill-buffer "*Gnus Help Bug*")))
776
777 (defun gnus-debug ()
778   "Attempts to go through the Gnus source file and report what variables have been changed.
779 The source file has to be in the Emacs load path."
780   (interactive)
781   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
782                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
783                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
784                  "nnmail.el" "message.el"))
785         (point (point))
786         file expr olist sym)
787     (gnus-message 4 "Please wait while we snoop your variables...")
788     (sit-for 0)
789     ;; Go through all the files looking for non-default values for variables.
790     (save-excursion
791       (set-buffer (get-buffer-create " *gnus bug info*"))
792       (buffer-disable-undo (current-buffer))
793       (while files
794         (erase-buffer)
795         (when (and (setq file (locate-library (pop files)))
796                    (file-exists-p file))
797           (insert-file-contents file)
798           (goto-char (point-min))
799           (if (not (re-search-forward "^;;* *Internal variables" nil t))
800               (gnus-message 4 "Malformed sources in file %s" file)
801             (narrow-to-region (point-min) (point))
802             (goto-char (point-min))
803             (while (setq expr (ignore-errors (read (current-buffer))))
804               (ignore-errors
805                 (and (or (eq (car expr) 'defvar)
806                          (eq (car expr) 'defcustom))
807                      (stringp (nth 3 expr))
808                      (or (not (boundp (nth 1 expr)))
809                          (not (equal (eval (nth 2 expr))
810                                      (symbol-value (nth 1 expr)))))
811                      (push (nth 1 expr) olist)))))))
812       (kill-buffer (current-buffer)))
813     (when (setq olist (nreverse olist))
814       (insert "------------------ Environment follows ------------------\n\n"))
815     (while olist
816       (if (boundp (car olist))
817           (condition-case ()
818               (pp `(setq ,(car olist)
819                          ,(if (or (consp (setq sym (symbol-value (car olist))))
820                                   (and (symbolp sym)
821                                        (not (or (eq sym nil)
822                                                 (eq sym t)))))
823                               (list 'quote (symbol-value (car olist)))
824                             (symbol-value (car olist))))
825                   (current-buffer))
826             (error
827              (format "(setq %s 'whatever)\n" (car olist))))
828         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
829       (setq olist (cdr olist)))
830     (insert "\n\n")
831     ;; Remove any control chars - they seem to cause trouble for some
832     ;; mailers.  (Byte-compiled output from the stuff above.)
833     (goto-char point)
834     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
835       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
836                      t t))))
837
838 ;;; Treatment of rejected articles.
839 ;;; Bounced mail.
840
841 (defun gnus-summary-resend-bounced-mail (&optional fetch)
842   "Re-mail the current message.
843 This only makes sense if the current message is a bounce message than
844 contains some mail you have written which has been bounced back to
845 you.
846 If FETCH, try to fetch the article that this is a reply to, if indeed
847 this is a reply."
848   (interactive "P")
849   (gnus-summary-select-article t)
850   (set-buffer gnus-original-article-buffer)
851   (gnus-setup-message 'compose-bounce
852     (let* ((references (mail-fetch-field "references"))
853            (parent (and references (gnus-parent-id references))))
854       (message-bounce)
855       ;; If there are references, we fetch the article we answered to.
856       (and fetch parent
857            (gnus-summary-refer-article parent)
858            (gnus-summary-show-all-headers)))))
859
860 ;;; Gcc handling.
861
862 ;; Do Gcc handling, which copied the message over to some group.
863 (defun gnus-inews-do-gcc (&optional gcc)
864   (interactive)
865   (when (gnus-alive-p)
866     (save-excursion
867       (save-restriction
868         (message-narrow-to-headers)
869         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
870               (coding-system-for-write 'raw-text)
871               groups group method)
872           (when gcc
873             (message-remove-header "gcc")
874             (widen)
875             (setq groups (message-tokenize-header gcc " ,"))
876             ;; Copy the article over to some group(s).
877             (while (setq group (pop groups))
878               (gnus-check-server
879                (setq method
880                      (cond ((and (null (gnus-get-info group))
881                                  (eq (car gnus-message-archive-method)
882                                      (car
883                                       (gnus-server-to-method
884                                        (gnus-group-method group)))))
885                             ;; If the group doesn't exist, we assume
886                             ;; it's an archive group...
887                             gnus-message-archive-method)
888                            ;; Use the method.
889                            ((gnus-info-method (gnus-get-info group))
890                             (gnus-info-method (gnus-get-info group)))
891                            ;; Find the method.
892                            (t (gnus-group-method group)))))
893               (gnus-check-server method)
894               (unless (gnus-request-group group t method)
895                 (gnus-request-create-group group method))
896               (save-excursion
897                 (nnheader-set-temp-buffer " *acc*")
898                 (insert-buffer-substring message-encoding-buffer)
899                 (run-hooks 'gnus-before-do-gcc-hook)
900                 (goto-char (point-min))
901                 (when (re-search-forward
902                        (concat "^" (regexp-quote mail-header-separator) "$")
903                        nil t)
904                   (replace-match "" t t ))
905                 (unless (gnus-request-accept-article group method t)
906                   (gnus-message 1 "Couldn't store article in group %s: %s"
907                                 group (gnus-status-message method))
908                   (sit-for 2))
909                 (kill-buffer (current-buffer))))))))))
910
911 (defun gnus-inews-insert-gcc ()
912   "Insert Gcc headers based on `gnus-outgoing-message-group'."
913   (save-excursion
914     (save-restriction
915       (message-narrow-to-headers)
916       (let* ((group gnus-outgoing-message-group)
917              (gcc (cond
918                    ((gnus-functionp group)
919                     (funcall group))
920                    ((or (stringp group) (list group))
921                     group))))
922         (when gcc
923           (insert "Gcc: "
924                   (if (stringp gcc) gcc
925                     (mapconcat 'identity gcc " "))
926                   "\n"))))))
927
928 (defun gnus-inews-insert-archive-gcc (&optional group)
929   "Insert the Gcc to say where the article is to be archived."
930   (let* ((var gnus-message-archive-group)
931          (group (or group gnus-newsgroup-name ""))
932          (gcc-self-val
933           (and gnus-newsgroup-name
934                (gnus-group-find-parameter
935                 gnus-newsgroup-name 'gcc-self)))
936          result 
937          (groups
938           (cond
939            ((null gnus-message-archive-method)
940             ;; Ignore.
941             nil)
942            ((stringp var)
943             ;; Just a single group.
944             (list var))
945            ((null var)
946             ;; We don't want this.
947             nil)
948            ((and (listp var) (stringp (car var)))
949             ;; A list of groups.
950             var)
951            ((gnus-functionp var)
952             ;; A function.
953             (funcall var group))
954            (t
955             ;; An alist of regexps/functions/forms.
956             (while (and var
957                         (not
958                          (setq result
959                                (cond
960                                 ((stringp (caar var))
961                                  ;; Regexp.
962                                  (when (string-match (caar var) group)
963                                    (cdar var)))
964                                 ((gnus-functionp (car var))
965                                  ;; Function.
966                                  (funcall (car var) group))
967                                 (t
968                                  (eval (car var)))))))
969               (setq var (cdr var)))
970             result)))
971          name)
972     (when (or groups gcc-self-val)
973       (when (stringp groups)
974         (setq groups (list groups)))
975       (save-excursion
976         (save-restriction
977           (message-narrow-to-headers)
978           (goto-char (point-max))
979           (insert "Gcc: ")
980           (if gcc-self-val
981               ;; Use the `gcc-self' param value instead.
982               (progn
983                 (insert
984                  (if (stringp gcc-self-val)
985                      gcc-self-val
986                    group))
987                 (if (not (eq gcc-self-val 'none))
988                     (insert "\n")
989                   (progn
990                     (beginning-of-line)
991                     (kill-line))))
992             ;; Use the list of groups.
993             (while (setq name (pop groups))
994               (insert (if (string-match ":" name)
995                           name
996                         (gnus-group-prefixed-name
997                          name gnus-message-archive-method)))
998               (when groups
999                 (insert " ")))
1000             (insert "\n")))))))
1001
1002 ;;; Allow redefinition of functions.
1003
1004 (gnus-ems-redefine)
1005
1006 (provide 'gnus-msg)
1007
1008 ;;; gnus-msg.el ends here