4f85c0fbae042bbe55d9f3243e57ffb4bbed3412
[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        (gnus-configure-windows ,config t))))
166     
167 (defun gnus-inews-add-send-actions (winconf buffer article)
168   (gnus-make-local-hook 'message-sent-hook)
169   (gnus-add-hook 'message-sent-hook 'gnus-inews-do-gcc nil t)
170   (setq message-post-method
171         `(lambda (arg)
172            (gnus-post-method arg ,gnus-newsgroup-name)))
173   (setq message-newsreader (setq message-mailer (gnus-extended-version)))
174   (message-add-action
175    `(set-window-configuration ,winconf) 'exit 'postpone 'kill)
176   (message-add-action
177    `(when (buffer-name ,buffer)
178       (save-excursion
179         (set-buffer ,buffer)
180         ,(when article
181            `(gnus-summary-mark-article-as-replied ,article))))
182    'send))
183
184 (put 'gnus-setup-message 'lisp-indent-function 1)
185 (put 'gnus-setup-message 'lisp-indent-hook 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     (while (setq article (pop articles))
244       (save-window-excursion
245         (set-buffer gnus-summary-buffer)
246         (gnus-summary-select-article nil nil nil article)
247         (gnus-summary-remove-process-mark article))
248       (gnus-copy-article-buffer)
249       (let ((message-reply-buffer gnus-article-copy)
250             (message-reply-headers gnus-current-headers))
251         (message-yank-original)
252         (setq beg (or beg (mark t))))
253       (when articles (insert "\n")))
254     
255     (push-mark)
256     (goto-char beg)))
257
258 (defun gnus-summary-cancel-article (n)
259   "Cancel an article you posted."
260   (interactive "P")
261   (gnus-set-global-variables)
262   (let ((articles (gnus-summary-work-articles n))
263         (message-post-method
264          `(lambda (arg)
265             (gnus-post-method nil ,gnus-newsgroup-name)))
266         article)
267     (while (setq article (pop articles))
268       (when (gnus-summary-select-article t nil nil article)
269         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
270                 (message-cancel-news))
271           (gnus-summary-mark-as-read article gnus-canceled-mark)
272           (gnus-cache-remove-article 1))
273         (gnus-article-hide-headers-if-wanted))
274       (gnus-summary-remove-process-mark article))))
275
276 (defun gnus-summary-supersede-article ()
277   "Compose an article that will supersede a previous article.
278 This is done simply by taking the old article and adding a Supersedes
279 header line with the old Message-ID."
280   (interactive)
281   (gnus-set-global-variables)
282   (let ((article (gnus-summary-article-number)))
283     (gnus-setup-message 'reply-yank
284       (gnus-summary-select-article t)
285       (set-buffer gnus-original-article-buffer)
286       (message-supersede)
287       (push
288        `((lambda ()
289            (gnus-cache-possibly-remove-article ,article nil nil nil t)))
290        message-send-actions))))
291
292 \f
293
294 (defun gnus-copy-article-buffer (&optional article-buffer)
295   ;; make a copy of the article buffer with all text properties removed
296   ;; this copy is in the buffer gnus-article-copy.
297   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
298   ;; this buffer should be passed to all mail/news reply/post routines.
299   (setq gnus-article-copy (get-buffer-create " *gnus article copy*"))
300   (buffer-disable-undo gnus-article-copy)
301   (or (memq gnus-article-copy gnus-buffer-list)
302       (setq gnus-buffer-list (cons gnus-article-copy gnus-buffer-list)))
303   (let ((article-buffer (or article-buffer gnus-article-buffer))
304         end beg contents)
305     (when (and (get-buffer article-buffer)
306                (buffer-name (get-buffer article-buffer)))
307       (save-excursion
308         (set-buffer article-buffer)
309         (save-restriction
310           (widen)
311           (setq contents (format "%s" (buffer-string)))
312           (set-buffer gnus-original-article-buffer)
313           (goto-char (point-min))
314           (while (looking-at message-unix-mail-delimiter)
315             (forward-line 1))
316           (setq beg (point))
317           (setq end (or (search-forward "\n\n" nil t) (point)))
318           (set-buffer gnus-article-copy)
319           (erase-buffer)
320           (insert contents)
321           (delete-region (goto-char (point-min))
322                          (or (search-forward "\n\n" nil t) (point)))
323           (insert-buffer-substring gnus-original-article-buffer beg end)))
324       gnus-article-copy)))
325
326 (defun gnus-post-news (post &optional group header article-buffer yank subject
327                             force-news)
328   (when article-buffer
329     (gnus-copy-article-buffer))
330   (let ((gnus-article-reply article-buffer))
331     (gnus-setup-message (cond (yank 'reply-yank)
332                               (article-buffer 'reply)
333                               (t 'message))
334       (let* ((group (or group gnus-newsgroup-name))
335              (pgroup group)
336              to-address to-group mailing-list to-list)
337         (when group
338           (setq to-address (gnus-group-find-parameter group 'to-address)
339                 to-group (gnus-group-find-parameter group 'to-group)
340                 to-list (gnus-group-find-parameter group 'to-list)
341                 mailing-list (when gnus-mailing-list-groups
342                                (string-match gnus-mailing-list-groups group))
343                 group (gnus-group-real-name group)))
344         (if (or (and to-group
345                      (gnus-news-group-p to-group))
346                 force-news
347                 (and (gnus-news-group-p 
348                       (or pgroup gnus-newsgroup-name)
349                       (if header (mail-header-number header)
350                         gnus-current-article))
351                      (not mailing-list)
352                      (not to-list)
353                      (not to-address)))
354             ;; This is news.
355             (if post
356                 (message-news (or to-group group))
357               (set-buffer gnus-article-copy)
358               (message-followup))
359           ;; The is mail.
360           (if post
361               (progn
362                 (message-mail (or to-address to-list))
363                 ;; Arrange for mail groups that have no `to-address' to
364                 ;; get that when the user sends off the mail.
365                 (push (list 'gnus-inews-add-to-address group)
366                       message-send-actions))
367             (set-buffer gnus-article-copy)
368             (message-wide-reply to-address)))
369         (when yank
370           (gnus-inews-yank-articles yank))))))
371
372 (defun gnus-post-method (arg group &optional silent)
373   "Return the posting method based on GROUP and ARG.
374 If SILENT, don't prompt the user."
375   (let ((group-method (gnus-find-method-for-group group)))
376     (cond 
377      ;; If the group-method is nil (which shouldn't happen) we use 
378      ;; the default method.
379      ((null group-method)
380       (or gnus-post-method gnus-select-method message-post-method))
381      ;; We want this group's method.
382      ((and arg (not (eq arg 0)))
383       group-method)
384      ;; We query the user for a post method.
385      ((or arg
386           (and gnus-post-method
387                (listp (car gnus-post-method))))
388       (let* ((methods
389               ;; Collect all methods we know about.
390               (append
391                (when gnus-post-method
392                  (if (listp (car gnus-post-method))
393                      gnus-post-method
394                    (list gnus-post-method)))
395                gnus-secondary-select-methods
396                (list gnus-select-method)
397                (list group-method)))
398              method-alist post-methods method)
399         ;; Weed out all mail methods.
400         (while methods
401           (setq method (gnus-server-get-method "" (pop methods)))
402           (when (or (gnus-method-option-p method 'post)
403                     (gnus-method-option-p method 'post-mail))
404             (push method post-methods)))
405         ;; Create a name-method alist.
406         (setq method-alist
407               (mapcar 
408                (lambda (m)
409                  (list (concat (cadr m) " (" (symbol-name (car m)) ")") m))
410                post-methods))
411         ;; Query the user.
412         (cadr
413          (assoc
414           (setq gnus-last-posting-server
415                 (if (and silent
416                          gnus-last-posting-server)
417                     ;; Just use the last value.
418                     gnus-last-posting-server
419                   (completing-read
420                    "Posting method: " method-alist nil t
421                    (cons (or gnus-last-posting-server "") 0))))
422           method-alist))))
423      ;; Override normal method.
424      ((and gnus-post-method
425            (or (gnus-method-option-p group-method 'post)
426                (gnus-method-option-p group-method 'post-mail)))
427       gnus-post-method)
428      ;; Perhaps this is a mail group?
429      ((and (not (gnus-member-of-valid 'post group))
430            (not (gnus-method-option-p group-method 'post-mail)))
431       group-method)
432      ;; Use the normal select method.
433      (t gnus-select-method))))
434
435 (defun gnus-inews-narrow-to-headers ()
436   (widen)
437   (narrow-to-region
438    (goto-char (point-min))
439    (or (and (re-search-forward 
440              (concat "^" (regexp-quote mail-header-separator) "$") nil t)
441             (match-beginning 0))
442        (point-max)))
443   (goto-char (point-min)))
444
445 ;;;
446 ;;; Check whether the message has been sent already.
447 ;;;
448
449 (defvar gnus-inews-sent-ids nil)
450
451 (defun gnus-inews-reject-message ()
452   "Check whether this message has already been sent."
453   (when gnus-sent-message-ids-file
454     (let ((message-id (save-restriction (gnus-inews-narrow-to-headers)
455                                         (mail-fetch-field "message-id")))
456           end)
457       (when message-id
458         (unless gnus-inews-sent-ids
459           (condition-case ()
460               (load  t t t)
461             (error nil)))
462         (if (member message-id gnus-inews-sent-ids)
463             ;; Reject this message.
464             (not (gnus-yes-or-no-p 
465                   (format "Message %s already sent.  Send anyway? "
466                           message-id)))
467           (push message-id gnus-inews-sent-ids)
468           ;; Chop off the last Message-IDs.
469           (when (setq end (nthcdr gnus-sent-message-ids-length 
470                                   gnus-inews-sent-ids))
471             (setcdr end nil))
472           (nnheader-temp-write gnus-sent-message-ids-file
473             (prin1 `(setq gnus-inews-sent-ids ',gnus-inews-sent-ids)
474                    (current-buffer)))
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