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