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