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