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