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