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