*** 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-load)
30 (require 'gnus-ems)
31 (require 'message)
32 (require 'gnus-art)
33 (require 'gnus)
34
35 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
36 (defvar gnus-post-method nil
37   "*Preferred method for posting USENET news.
38 If this variable is nil, Gnus will use the current method to decide
39 which method to use when posting.  If it is non-nil, it will override
40 the current method.  This method will not be used in mail groups and
41 the like, only in \"real\" newsgroups.
42
43 The value must be a valid method as discussed in the documentation of
44 `gnus-select-method'.  It can also be a list of methods.  If that is
45 the case, the user will be queried for what select method to use when
46 posting.")
47
48 (defvar gnus-outgoing-message-group nil
49   "*All outgoing messages will be put in this group.
50 If you want to store all your outgoing mail and articles in the group
51 \"nnml:archive\", you set this variable to that value. This variable
52 can also be a list of group names. 
53
54 If you want to have greater control over what group to put each
55 message in, you can set this variable to a function that checks the
56 current newsgroup name and then returns a suitable group name (or list
57 of names).")
58
59 (defvar gnus-mailing-list-groups nil
60   "*Regexp matching groups that are really mailing lists.
61 This is useful when you're reading a mailing list that has been
62 gatewayed to a newsgroup, and you want to followup to an article in
63 the group.")
64
65 (defvar gnus-sent-message-ids-file 
66   (nnheader-concat gnus-directory "Sent-Message-IDs")
67   "File where Gnus saves a cache of sent message ids.")
68
69 (defvar gnus-sent-message-ids-length 1000
70   "The number of sent Message-IDs to save.")
71
72 (defvar gnus-crosspost-complaint
73   "Hi,
74
75 You posted the article below with the following Newsgroups header:
76
77 Newsgroups: %s
78
79 The %s group, at least, was an inappropriate recipient
80 of this message.  Please trim your Newsgroups header to exclude this
81 group before posting in the future.
82
83 Thank you.
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 'lisp-indent-hook 1)
187 (put 'gnus-setup-message 'edebug-form-spec '(form body))
188
189 ;;; Post news commands of Gnus group mode and summary mode
190
191 (defun gnus-group-mail ()
192   "Start composing a mail."
193   (interactive)
194   (gnus-setup-message 'message
195     (message-mail)))
196
197 (defun gnus-group-post-news (&optional arg)
198   "Start composing a news message.
199 If ARG, post to the group under point.
200 If ARG is 1, prompt for a group name."
201   (interactive "P")
202   ;; Bind this variable here to make message mode hooks
203   ;; work ok.
204   (let ((gnus-newsgroup-name
205          (if arg
206              (if (= 1 (prefix-numeric-value arg))
207                  (completing-read "Newsgroup: " gnus-active-hashtb nil
208                                   (gnus-read-active-file-p))
209                (gnus-group-group-name))
210            "")))
211     (gnus-post-news 'post gnus-newsgroup-name)))
212
213 (defun gnus-summary-post-news ()
214   "Start composing a news message."
215   (interactive)
216   (gnus-set-global-variables)
217   (gnus-post-news 'post gnus-newsgroup-name))
218
219 (defun gnus-summary-followup (yank &optional force-news)
220   "Compose a followup to an article.
221 If prefix argument YANK is non-nil, original article is yanked automatically."
222   (interactive 
223    (list (and current-prefix-arg 
224               (gnus-summary-work-articles 1))))
225   (gnus-set-global-variables)
226   (when yank
227     (gnus-summary-goto-subject (car yank)))
228   (save-window-excursion
229     (gnus-summary-select-article))
230   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
231         (gnus-newsgroup-name gnus-newsgroup-name))
232     ;; Send a followup.
233     (gnus-post-news nil gnus-newsgroup-name
234                     headers gnus-article-buffer 
235                     yank nil force-news)))
236
237 (defun gnus-summary-followup-with-original (n &optional force-news)
238   "Compose a followup to an article and include the original article."
239   (interactive "P")
240   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
241
242 (defun gnus-inews-yank-articles (articles)
243   (let (beg article)
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 (insert "\n")))
255     
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       (setq gnus-buffer-list (cons 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       gnus-article-copy)))
335
336 (defun gnus-post-news (post &optional group header article-buffer yank subject
337                             force-news)
338   (when article-buffer
339     (gnus-copy-article-buffer))
340   (let ((gnus-article-reply article-buffer))
341     (gnus-setup-message (cond (yank 'reply-yank)
342                               (article-buffer 'reply)
343                               (t 'message))
344       (let* ((group (or group gnus-newsgroup-name))
345              (pgroup group)
346              to-address to-group mailing-list to-list)
347         (when group
348           (setq to-address (gnus-group-find-parameter group 'to-address)
349                 to-group (gnus-group-find-parameter group 'to-group)
350                 to-list (gnus-group-find-parameter group 'to-list)
351                 mailing-list (when gnus-mailing-list-groups
352                                (string-match gnus-mailing-list-groups group))
353                 group (gnus-group-real-name group)))
354         (if (or (and to-group
355                      (gnus-news-group-p to-group))
356                 force-news
357                 (and (gnus-news-group-p 
358                       (or pgroup gnus-newsgroup-name)
359                       (if header (mail-header-number header)
360                         gnus-current-article))
361                      (not mailing-list)
362                      (not to-list)
363                      (not to-address)))
364             ;; This is news.
365             (if post
366                 (message-news (or to-group group))
367               (set-buffer gnus-article-copy)
368               (message-followup to-group))
369           ;; The is mail.
370           (if post
371               (progn
372                 (message-mail (or to-address to-list))
373                 ;; Arrange for mail groups that have no `to-address' to
374                 ;; get that when the user sends off the mail.
375                 (push (list 'gnus-inews-add-to-address group)
376                       message-send-actions))
377             (set-buffer gnus-article-copy)
378             (message-wide-reply to-address)))
379         (when yank
380           (gnus-inews-yank-articles yank))))))
381
382 (defun gnus-post-method (arg group &optional silent)
383   "Return the posting method based on GROUP and ARG.
384 If SILENT, don't prompt the user."
385   (let ((group-method (gnus-find-method-for-group group)))
386     (cond 
387      ;; If the group-method is nil (which shouldn't happen) we use 
388      ;; the default method.
389      ((null group-method)
390       (or gnus-post-method gnus-select-method message-post-method))
391      ;; We want this group's method.
392      ((and arg (not (eq arg 0)))
393       group-method)
394      ;; We query the user for a post method.
395      ((or arg
396           (and gnus-post-method
397                (listp (car gnus-post-method))))
398       (let* ((methods
399               ;; Collect all methods we know about.
400               (append
401                (when gnus-post-method
402                  (if (listp (car gnus-post-method))
403                      gnus-post-method
404                    (list gnus-post-method)))
405                gnus-secondary-select-methods
406                (list gnus-select-method)
407                (list group-method)))
408              method-alist post-methods method)
409         ;; Weed out all mail methods.
410         (while methods
411           (setq method (gnus-server-get-method "" (pop methods)))
412           (when (or (gnus-method-option-p method 'post)
413                     (gnus-method-option-p method 'post-mail))
414             (push method post-methods)))
415         ;; Create a name-method alist.
416         (setq method-alist
417               (mapcar 
418                (lambda (m)
419                  (list (concat (cadr m) " (" (symbol-name (car m)) ")") m))
420                post-methods))
421         ;; Query the user.
422         (cadr
423          (assoc
424           (setq gnus-last-posting-server
425                 (if (and silent
426                          gnus-last-posting-server)
427                     ;; Just use the last value.
428                     gnus-last-posting-server
429                   (completing-read
430                    "Posting method: " method-alist nil t
431                    (cons (or gnus-last-posting-server "") 0))))
432           method-alist))))
433      ;; Override normal method.
434      ((and gnus-post-method
435            (or (gnus-method-option-p group-method 'post)
436                (gnus-method-option-p group-method 'post-mail)
437                (gnus-group-find-parameter group 'to-group)))
438       gnus-post-method)
439      ;; Perhaps this is a mail group?
440      ((and (not (gnus-member-of-valid 'post group))
441            (not (gnus-method-option-p group-method 'post-mail))
442            (not (gnus-group-find-parameter group 'to-group)))
443       group-method)
444      ;; Use the normal select method.
445      (t gnus-select-method))))
446
447 (defun gnus-inews-narrow-to-headers ()
448   (widen)
449   (narrow-to-region
450    (goto-char (point-min))
451    (or (and (re-search-forward 
452              (concat "^" (regexp-quote mail-header-separator) "$") nil t)
453             (match-beginning 0))
454        (point-max)))
455   (goto-char (point-min)))
456
457 ;;;
458 ;;; Check whether the message has been sent already.
459 ;;;
460
461 (defvar gnus-inews-sent-ids nil)
462
463 (defun gnus-inews-reject-message ()
464   "Check whether this message has already been sent."
465   (when gnus-sent-message-ids-file
466     (let ((message-id (save-restriction (gnus-inews-narrow-to-headers)
467                                         (mail-fetch-field "message-id")))
468           end)
469       (when message-id
470         (unless gnus-inews-sent-ids
471           (condition-case ()
472               (load  t t t)
473             (error nil)))
474         (if (member message-id gnus-inews-sent-ids)
475             ;; Reject this message.
476             (not (gnus-yes-or-no-p 
477                   (format "Message %s already sent.  Send anyway? "
478                           message-id)))
479           (push message-id gnus-inews-sent-ids)
480           ;; Chop off the last Message-IDs.
481           (when (setq end (nthcdr gnus-sent-message-ids-length 
482                                   gnus-inews-sent-ids))
483             (setcdr end nil))
484           (nnheader-temp-write gnus-sent-message-ids-file
485             (gnus-prin1 `(setq gnus-inews-sent-ids ',gnus-inews-sent-ids)))
486           nil)))))
487
488 \f
489
490 ;; Dummy to avoid byte-compile warning.
491 (defvar nnspool-rejected-article-hook)
492
493 ;;; Since the X-Newsreader/X-Mailer are ``vanity'' headers, they might
494 ;;; as well include the Emacs version as well.
495 ;;; The following function works with later GNU Emacs, and XEmacs.
496 (defun gnus-extended-version ()
497   "Stringified Gnus version and Emacs version"
498   (interactive)
499   (concat
500    gnus-version
501    "/"
502    (cond
503     ((string-match "^\\([0-9]+\\.[0-9]+\\)\\.[.0-9]+$" emacs-version)
504      (concat "Emacs " (substring emacs-version
505                                  (match-beginning 1)
506                                  (match-end 1))))
507     ((string-match "\\([A-Z]*[Mm][Aa][Cc][Ss]\\)" emacs-version)
508      (concat (substring emacs-version
509                         (match-beginning 1)
510                         (match-end 1))
511              (format " %d.%d" emacs-major-version emacs-minor-version)))
512     (t emacs-version))))
513
514 ;; Written by "Mr. Per Persson" <pp@gnu.ai.mit.edu>.
515 (defun gnus-inews-insert-mime-headers ()
516   (goto-char (point-min))
517   (let ((mail-header-separator 
518          (progn 
519            (goto-char (point-min))
520            (if (and (search-forward (concat "\n" mail-header-separator "\n")
521                                     nil t)
522                     (not (search-backward "\n\n" nil t)))
523                mail-header-separator
524              ""))))
525     (or (mail-position-on-field "Mime-Version")
526         (insert "1.0")
527         (cond ((save-restriction
528                  (widen)
529                  (goto-char (point-min))
530                  (re-search-forward "[\200-\377]" nil t))
531                (or (mail-position-on-field "Content-Type")
532                    (insert "text/plain; charset=ISO-8859-1"))
533                (or (mail-position-on-field "Content-Transfer-Encoding")
534                    (insert "8bit")))
535               (t (or (mail-position-on-field "Content-Type")
536                      (insert "text/plain; charset=US-ASCII"))
537                  (or (mail-position-on-field "Content-Transfer-Encoding")
538                      (insert "7bit")))))))
539
540 \f
541 ;;;
542 ;;; Gnus Mail Functions 
543 ;;;
544
545 ;;; Mail reply commands of Gnus summary mode
546
547 (defun gnus-summary-reply (&optional yank)
548   "Reply mail to news author.
549 If prefix argument YANK is non-nil, original article is yanked automatically."
550   (interactive 
551    (list (and current-prefix-arg 
552               (gnus-summary-work-articles 1))))
553   ;; Bug fix by jbw@bigbird.bu.edu (Joe Wells)
554   ;; Stripping headers should be specified with mail-yank-ignored-headers.
555   (gnus-set-global-variables)
556   (when yank 
557     (gnus-summary-goto-subject (car yank)))
558   (let ((gnus-article-reply t))
559     (gnus-setup-message (if yank 'reply-yank 'reply)
560       (gnus-summary-select-article)
561       (set-buffer (gnus-copy-article-buffer))
562       (message-reply nil nil (gnus-group-find-parameter
563                               gnus-newsgroup-name 'broken-reply-to))
564       (when yank
565         (gnus-inews-yank-articles yank)))))
566
567 (defun gnus-summary-reply-with-original (n)
568   "Reply mail to news author with original article."
569   (interactive "P")
570   (gnus-summary-reply (gnus-summary-work-articles n)))
571
572 (defun gnus-summary-mail-forward (&optional post)
573   "Forward the current message to another user."
574   (interactive "P")
575   (gnus-set-global-variables)
576   (gnus-setup-message 'forward
577     (gnus-summary-select-article)
578     (set-buffer gnus-original-article-buffer)
579     (message-forward post)))
580
581 (defun gnus-summary-resend-message (address)
582   "Resend the current article to ADDRESS."
583   (interactive "sResend message to: ")
584   (gnus-summary-select-article)
585   (save-excursion
586     (set-buffer gnus-original-article-buffer)
587     (message-resend address)))
588
589 (defun gnus-summary-post-forward ()
590   "Forward the current article to a newsgroup."
591   (interactive)
592   (gnus-summary-mail-forward t))
593
594 (defvar gnus-nastygram-message 
595   "The following article was inappropriately posted to %s.\n"
596   "Format string to insert in nastygrams.
597 The current group name will be inserted at \"%s\".")
598
599 (defun gnus-summary-mail-nastygram (n)
600   "Send a nastygram to the author of the current article."
601   (interactive "P")
602   (if (or gnus-expert-user
603           (gnus-y-or-n-p 
604            "Really send a nastygram to the author of the current article? "))
605       (let ((group gnus-newsgroup-name))
606         (gnus-summary-reply-with-original n)
607         (set-buffer gnus-message-buffer)
608         (insert (format gnus-nastygram-message group))
609         (message-send-and-exit))))
610
611 (defun gnus-summary-mail-crosspost-complaint (n)
612   "Send a complaint about crossposting to the current article(s)."
613   (interactive "P")
614   (let ((articles (gnus-summary-work-articles n))
615         article)
616     (while (setq article (pop articles))
617       (set-buffer gnus-summary-buffer)
618       (gnus-summary-goto-subject article)
619       (let ((group (gnus-group-real-name gnus-newsgroup-name))
620             newsgroups followup-to)
621         (gnus-summary-select-article)
622         (set-buffer gnus-original-article-buffer)
623         (if (and (<= (length (message-tokenize-header
624                               (setq newsgroups (mail-fetch-field "newsgroups"))
625                               ", "))
626                      1)
627                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
628                      (not (member group (message-tokenize-header
629                                          followup-to ", ")))))
630             (if followup-to
631                 (gnus-message 1 "Followup-to restricted")
632               (gnus-message 1 "Not a crossposted article"))
633           (set-buffer gnus-summary-buffer)
634           (gnus-summary-reply-with-original 1)
635           (set-buffer gnus-message-buffer)
636           (insert (format gnus-crosspost-complaint newsgroups group))
637           (when (gnus-y-or-n-p "Send this complaint? ")
638             (message-send-and-exit)))))))
639
640 (defun gnus-summary-mail-other-window ()
641   "Compose mail in other window."
642   (interactive)
643   (gnus-setup-message 'message
644     (message-mail)))
645
646 (defun gnus-mail-parse-comma-list ()
647   (let (accumulated
648         beg)
649     (skip-chars-forward " ")
650     (while (not (eobp))
651       (setq beg (point))
652       (skip-chars-forward "^,")
653       (while (zerop
654               (save-excursion 
655                 (save-restriction
656                   (let ((i 0))
657                     (narrow-to-region beg (point))
658                     (goto-char beg)
659                     (logand (progn
660                               (while (search-forward "\"" nil t)
661                                 (incf i))
662                               (if (zerop i) 2 i)) 2)))))
663         (skip-chars-forward ",")
664         (skip-chars-forward "^,"))
665       (skip-chars-backward " ")
666       (setq accumulated
667             (cons (buffer-substring beg (point))
668                   accumulated))
669       (skip-chars-forward "^,")
670       (skip-chars-forward ", "))
671     accumulated))
672
673 (defun gnus-mail-yank-original ()
674   (interactive)
675   (save-excursion
676     (mail-yank-original nil))
677   (or mail-yank-hooks mail-citation-hook
678       (run-hooks 'news-reply-header-hook)))
679
680 (defun gnus-inews-add-to-address (group)
681   (let ((to-address (mail-fetch-field "to")))
682     (when (and to-address
683                (gnus-alive-p))
684       ;; This mail group doesn't have a `to-list', so we add one
685       ;; here.  Magic!  
686       (gnus-group-add-parameter group (cons 'to-list to-address)))))
687
688 (defun gnus-put-message ()
689   "Put the current message in some group and return to Gnus."
690   (interactive)
691   (let ((reply gnus-article-reply)
692         (winconf gnus-prev-winconf)
693         (group gnus-newsgroup-name))
694     
695     (or (and group (not (gnus-group-read-only-p group)))
696         (setq group (read-string "Put in group: " nil
697                                  (gnus-writable-groups))))
698     (and (gnus-gethash group gnus-newsrc-hashtb)
699          (error "No such group: %s" group))
700
701     (save-excursion
702       (save-restriction
703         (widen)
704         (gnus-inews-narrow-to-headers)
705         (let (gnus-deletable-headers)
706           (if (message-news-p)
707               (message-generate-headers message-required-news-headers)
708             (message-generate-headers message-required-mail-headers)))
709         (goto-char (point-max))
710         (insert "Gcc: " group "\n")
711         (widen)))
712
713     (gnus-inews-do-gcc)
714
715     (if (get-buffer gnus-group-buffer)
716         (progn
717           (if (gnus-buffer-exists-p (car-safe reply))
718               (progn
719                 (set-buffer (car reply))
720                 (and (cdr reply)
721                      (gnus-summary-mark-article-as-replied 
722                       (cdr reply)))))
723           (and winconf (set-window-configuration winconf))))))
724
725 (defun gnus-article-mail (yank)
726   "Send a reply to the address near point.
727 If YANK is non-nil, include the original article."
728   (interactive "P")
729   (let ((address 
730          (buffer-substring
731           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
732           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
733     (when address
734       (message-reply address)
735       (when yank
736         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
737
738 (defun gnus-bug ()
739   "Send a bug report to the Gnus maintainers."
740   (interactive)
741   (unless (gnus-alive-p)
742     (error "Gnus has been shut down"))
743   (gnus-setup-message 'bug
744     (delete-other-windows)
745     (switch-to-buffer "*Gnus Help Bug*")
746     (erase-buffer)
747     (insert gnus-bug-message)
748     (goto-char (point-min))
749     (message-pop-to-buffer "*Gnus Bug*")
750     (message-setup `((To . ,gnus-maintainer) (Subject . "")))
751     (push `(gnus-bug-kill-buffer) message-send-actions)
752     (goto-char (point-min))
753     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
754     (forward-line 1)
755     (insert (gnus-version) "\n")
756     (insert (emacs-version))
757     (insert "\n\n\n\n\n")
758     (gnus-debug)
759     (goto-char (point-min))
760     (search-forward "Subject: " nil t)
761     (message "")))
762
763 (defun gnus-bug-kill-buffer ()
764   (and (get-buffer "*Gnus Help Bug*")
765        (kill-buffer "*Gnus Help Bug*")))
766
767 (defun gnus-debug ()
768   "Attemps to go through the Gnus source file and report what variables have been changed.
769 The source file has to be in the Emacs load path."
770   (interactive)
771   (let ((files '("gnus-load.el" "gnus-sum.el" "gnus-group.el"
772                  "gnus-art.el" "gnus-start.el"
773                  "gnus-msg.el" "gnus-score.el" "gnus-win.el"
774                  "nnmail.el" "message.el"))
775         file expr olist sym)
776     (gnus-message 4 "Please wait while we snoop your variables...")
777     (sit-for 0)
778     ;; Go through all the files looking for non-default values for variables.
779     (save-excursion
780       (set-buffer (get-buffer-create " *gnus bug info*"))
781       (buffer-disable-undo (current-buffer))
782       (while files
783         (erase-buffer)
784         (when (file-exists-p (setq file (locate-library (pop files))))
785           (insert-file-contents file)
786           (goto-char (point-min))
787           (if (not (re-search-forward "^;;* *Internal variables" nil t))
788               (gnus-message 4 "Malformed sources in file %s" file)
789             (narrow-to-region (point-min) (point))
790             (goto-char (point-min))
791             (while (setq expr (condition-case () 
792                                   (read (current-buffer)) (error nil)))
793               (condition-case ()
794                   (and (eq (car expr) 'defvar)
795                        (stringp (nth 3 expr))
796                        (or (not (boundp (nth 1 expr)))
797                            (not (equal (eval (nth 2 expr))
798                                        (symbol-value (nth 1 expr)))))
799                        (push (nth 1 expr) olist))
800                 (error nil))))))
801       (kill-buffer (current-buffer)))
802     (when (setq olist (nreverse olist))
803       (insert "------------------ Environment follows ------------------\n\n"))
804     (while olist
805       (if (boundp (car olist))
806           (condition-case ()
807               (pp `(setq ,(car olist)
808                          ,(if (or (consp (setq sym (symbol-value (car olist))))
809                                   (and (symbolp sym)
810                                        (not (or (eq sym nil)
811                                                 (eq sym t)))))
812                               (list 'quote (symbol-value (car olist)))
813                             (symbol-value (car olist))))
814                   (current-buffer))
815             (error
816              (format "(setq %s 'whatever)\n" (car olist))))
817         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
818       (setq olist (cdr olist)))
819     (insert "\n\n")
820     ;; Remove any null chars - they seem to cause trouble for some
821     ;; mailers. (Byte-compiled output from the stuff above.) 
822     (goto-char (point-min))
823     (while (re-search-forward "[\000\200]" nil t)
824       (replace-match "" t t))))
825
826 ;;; Treatment of rejected articles.
827 ;;; Bounced mail.
828
829 (defun gnus-summary-resend-bounced-mail (&optional fetch)
830   "Re-mail the current message.
831 This only makes sense if the current message is a bounce message than
832 contains some mail you have written which has been bounced back to
833 you.
834 If FETCH, try to fetch the article that this is a reply to, if indeed
835 this is a reply."
836   (interactive "P")
837   (gnus-summary-select-article t)
838   (set-buffer gnus-original-article-buffer)
839   (gnus-setup-message 'compose-bounce
840     (let* ((references (mail-fetch-field "references"))
841            (parent (and references (gnus-parent-id references))))
842       (message-bounce)
843       ;; If there are references, we fetch the article we answered to.  
844       (and fetch parent
845            (gnus-summary-refer-article parent)
846            (gnus-summary-show-all-headers)))))
847
848 ;;; Gcc handling.
849
850 ;; Do Gcc handling, which copied the message over to some group. 
851 (defun gnus-inews-do-gcc (&optional gcc)
852   (when (gnus-alive-p)
853     (save-excursion
854       (save-restriction
855         (message-narrow-to-headers)
856         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
857               (cur (current-buffer))
858               groups group method)
859           (when gcc
860             (message-remove-header "gcc")
861             (widen)
862             (setq groups (message-tokenize-header gcc " ,"))
863             ;; Copy the article over to some group(s).
864             (while (setq group (pop groups))
865               (gnus-check-server 
866                (setq method
867                      (cond ((and (null (gnus-get-info group))
868                                  (eq (car gnus-message-archive-method)
869                                      (car 
870                                       (gnus-server-to-method
871                                        (gnus-group-method group)))))
872                             ;; If the group doesn't exist, we assume
873                             ;; it's an archive group...
874                             gnus-message-archive-method)
875                            ;; Use the method.
876                            ((gnus-info-method (gnus-get-info group))
877                             (gnus-info-method (gnus-get-info group)))
878                            ;; Find the method.
879                            (t (gnus-group-method group)))))
880               (gnus-check-server method)
881               (unless (gnus-request-group group t method)
882                 (gnus-request-create-group group method))
883               (save-excursion
884                 (nnheader-set-temp-buffer " *acc*")
885                 (insert-buffer-substring cur)
886                 (goto-char (point-min))
887                 (when (re-search-forward 
888                        (concat "^" (regexp-quote mail-header-separator) "$")
889                        nil t)
890                   (replace-match "" t t ))
891                 (unless (gnus-request-accept-article group method t)
892                   (gnus-message 1 "Couldn't store article in group %s: %s" 
893                                 group (gnus-status-message method))
894                   (sit-for 2))
895                 (kill-buffer (current-buffer))))))))))
896
897 (defun gnus-inews-insert-gcc ()
898   "Insert Gcc headers based on `gnus-outgoing-message-group'."
899   (save-excursion
900     (save-restriction
901       (gnus-inews-narrow-to-headers)
902       (let* ((group gnus-outgoing-message-group)
903              (gcc (cond 
904                    ((gnus-functionp group)
905                     (funcall group))
906                    ((or (stringp group) (list group))
907                     group))))
908         (when gcc
909           (insert "Gcc: "
910                   (if (stringp gcc) gcc
911                     (mapconcat 'identity gcc " "))
912                   "\n"))))))
913
914 (defun gnus-inews-insert-archive-gcc (&optional group)
915   "Insert the Gcc to say where the article is to be archived."
916   (let* ((var gnus-message-archive-group)
917          (group (or group gnus-newsgroup-name ""))
918          result
919          gcc-self-val
920          (groups
921           (cond 
922            ((null gnus-message-archive-method)
923             ;; Ignore.
924             nil)
925            ((stringp var)
926             ;; Just a single group.
927             (list var))
928            ((null var)
929             ;; We don't want this.
930             nil)
931            ((and (listp var) (stringp (car var)))
932             ;; A list of groups.
933             var)
934            ((gnus-functionp var)
935             ;; A function.
936             (funcall var group))
937            (t
938             ;; An alist of regexps/functions/forms.
939             (while (and var
940                         (not
941                          (setq result
942                                (cond 
943                                 ((stringp (caar var))
944                                  ;; Regexp.
945                                  (when (string-match (caar var) group)
946                                    (cdar var)))
947                                 ((gnus-functionp (car var))
948                                  ;; Function.
949                                  (funcall (car var) group))
950                                 (t
951                                  (eval (car var)))))))
952               (setq var (cdr var)))
953             result)))
954          name)
955     (when groups
956       (when (stringp groups)
957         (setq groups (list groups)))
958       (save-excursion
959         (save-restriction
960           (gnus-inews-narrow-to-headers)
961           (goto-char (point-max))
962           (insert "Gcc: ")
963           (if (and gnus-newsgroup-name
964                    (setq gcc-self-val
965                          (gnus-group-find-parameter
966                           gnus-newsgroup-name 'gcc-self)))
967               (progn 
968                 (insert
969                  (if (stringp gcc-self-val)
970                      gcc-self-val
971                    group))
972                 (if (not (eq gcc-self-val 'none))
973                     (insert "\n")
974                   (progn
975                     (beginning-of-line)
976                     (kill-line))))
977             (while (setq name (pop groups))
978               (insert (if (string-match ":" name)
979                           name
980                         (gnus-group-prefixed-name 
981                          name gnus-message-archive-method)))
982               (if groups (insert " ")))
983             (insert "\n")))))))
984
985 (defun gnus-summary-send-draft ()
986   "Enter a mail/post buffer to edit and send the draft."
987   (interactive)
988   (gnus-set-global-variables)
989   (let (buf)
990     (if (not (setq buf (gnus-request-restore-buffer 
991                         (gnus-summary-article-number) gnus-newsgroup-name)))
992         (error "Couldn't restore the article")
993       (switch-to-buffer buf)
994       (when (eq major-mode 'news-reply-mode)
995         (local-set-key "\C-c\C-c" 'gnus-inews-news))
996       ;; Insert the separator.
997       (goto-char (point-min))
998       (search-forward "\n\n")
999       (forward-char -1)
1000       (insert mail-header-separator)
1001       ;; Configure windows.
1002       (let ((gnus-draft-buffer (current-buffer)))
1003         (gnus-configure-windows 'draft t)
1004         (goto-char (point))))))
1005   
1006 (gnus-add-shutdown 'gnus-inews-close 'gnus)
1007
1008 (defun gnus-inews-close ()
1009   (setq gnus-inews-sent-ids nil))
1010   
1011 ;;; Allow redefinition of functions.
1012
1013 (gnus-ems-redefine)
1014
1015 (provide 'gnus-msg)
1016
1017 ;;; gnus-msg.el ends here