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