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