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