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