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