Sync up with gnus-5.6.38 and Enable to get APOP server using pop3-fma.el and gnus...
[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 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-ems)
34 (require 'message)
35 (require 'gnus-art)
36
37 (defcustom gnus-post-method nil
38   "*Preferred method for posting USENET news.
39
40 If this variable is `current', Gnus will use the \"current\" select
41 method when posting.  If it is nil (which is the default), Gnus will
42 use the native posting method of the server.
43
44 This method will not be used in mail groups and the like, only in
45 \"real\" newsgroups.
46
47 If not nil nor `native', the value must be a valid method as discussed
48 in the documentation of `gnus-select-method'. It can also be a list of
49 methods. If that is the case, the user will be queried for what select
50 method to use when posting."
51   :group 'gnus-group-foreign
52   :type `(choice (const nil)
53                  (const current)
54                  (const native)
55                  (sexp :tag "Methods" ,gnus-select-method)))
56
57 (defvar gnus-outgoing-message-group nil
58   "*All outgoing messages will be put in this group.
59 If you want to store all your outgoing mail and articles in the group
60 \"nnml:archive\", you set this variable to that value.  This variable
61 can also be a list of group names.
62
63 If you want to have greater control over what group to put each
64 message in, you can set this variable to a function that checks the
65 current newsgroup name and then returns a suitable group name (or list
66 of names).")
67
68 (defvar gnus-mailing-list-groups nil
69   "*Regexp matching groups that are really mailing lists.
70 This is useful when you're reading a mailing list that has been
71 gatewayed to a newsgroup, and you want to followup to an article in
72 the group.")
73
74 (defvar gnus-add-to-list nil
75   "*If non-nil, add a `to-list' parameter automatically.")
76
77 (defvar gnus-crosspost-complaint
78   "Hi,
79
80 You posted the article below with the following Newsgroups header:
81
82 Newsgroups: %s
83
84 The %s group, at least, was an inappropriate recipient
85 of this message.  Please trim your Newsgroups header to exclude this
86 group before posting in the future.
87
88 Thank you.
89
90 "
91   "Format string to be inserted when complaining about crossposts.
92 The first %s will be replaced by the Newsgroups header;
93 the second with the current group name.")
94
95 (defvar gnus-message-setup-hook nil
96   "Hook run after setting up a message buffer.")
97
98 (defvar gnus-bug-create-help-buffer t
99   "*Should we create the *Gnus Help Bug* buffer?")
100
101 (defvar gnus-posting-styles nil
102   "*Alist of styles to use when posting.")
103
104 (defvar gnus-posting-style-alist
105   '((organization . message-user-organization)
106     (signature . message-signature)
107     (signature-file . message-signature-file)
108     (address . user-mail-address)
109     (name . user-full-name))
110   "*Mapping from style parameters to variables.")
111
112 ;;; Internal variables.
113
114 (defvar gnus-message-buffer "*Mail Gnus*")
115 (defvar gnus-article-copy nil)
116 (defvar gnus-last-posting-server nil)
117 (defvar gnus-message-group-art nil)
118
119 (defconst gnus-bug-message
120   "Sending a bug report to the Gnus Towers.
121 ========================================
122
123 The buffer below is a mail buffer.  When you press `C-c C-c', it will
124 be sent to the Gnus Bug Exterminators.
125
126 At the bottom of the buffer you'll see lots of variable settings.
127 Please do not delete those.  They will tell the Bug People what your
128 environment is, so that it will be easier to locate the bugs.
129
130 If you have found a bug that makes Emacs go \"beep\", set
131 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
132 and include the backtrace in your bug report.
133
134 Please describe the bug in annoying, painstaking detail.
135
136 Thank you for your help in stamping out bugs.
137 ")
138
139 (eval-and-compile
140   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
141   (autoload 'news-setup "rnewspost")
142   (autoload 'news-reply-mode "rnewspost")
143   (autoload 'rmail-dont-reply-to "mail-utils")
144   (autoload 'rmail-output "rmailout"))
145
146 \f
147 ;;;
148 ;;; Gnus Posting Functions
149 ;;;
150
151 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
152   "p" gnus-summary-post-news
153   "f" gnus-summary-followup
154   "F" gnus-summary-followup-with-original
155   "c" gnus-summary-cancel-article
156   "s" gnus-summary-supersede-article
157   "r" gnus-summary-reply
158   "R" gnus-summary-reply-with-original
159   "w" gnus-summary-wide-reply
160   "W" gnus-summary-wide-reply-with-original
161   "n" gnus-summary-followup-to-mail
162   "N" gnus-summary-followup-to-mail-with-original
163   "m" gnus-summary-mail-other-window
164   "u" gnus-uu-post-news
165   "\M-c" gnus-summary-mail-crosspost-complaint
166   "om" gnus-summary-mail-forward
167   "op" gnus-summary-post-forward
168   "Om" gnus-uu-digest-mail-forward
169   "Op" gnus-uu-digest-post-forward)
170
171 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
172   "b" gnus-summary-resend-bounced-mail
173   ;; "c" gnus-summary-send-draft
174   "r" gnus-summary-resend-message)
175
176 ;;; Internal functions.
177
178 (defvar gnus-article-reply nil)
179 (defmacro gnus-setup-message (config &rest forms)
180   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
181         (buffer (make-symbol "gnus-setup-message-buffer"))
182         (article (make-symbol "gnus-setup-message-article"))
183         (group (make-symbol "gnus-setup-message-group")))
184     `(let ((,winconf (current-window-configuration))
185            (,buffer (buffer-name (current-buffer)))
186            (,article (and gnus-article-reply (gnus-summary-article-number)))
187            (,group gnus-newsgroup-name)
188            (message-header-setup-hook
189             (copy-sequence message-header-setup-hook)))
190        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
191        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
192        (add-hook 'message-mode-hook 'gnus-configure-posting-styles)
193        (unwind-protect
194            (progn
195              ,@forms)
196          (gnus-inews-add-send-actions ,winconf ,buffer ,article)
197          (setq gnus-message-buffer (current-buffer))
198          (set (make-local-variable 'gnus-message-group-art)
199               (cons ,group ,article))
200          (make-local-variable 'gnus-newsgroup-name)
201          (gnus-run-hooks 'gnus-message-setup-hook))
202        (gnus-add-buffer)
203        (gnus-configure-windows ,config t)
204        (set-buffer-modified-p nil))))
205
206 (defun gnus-inews-add-send-actions (winconf buffer article)
207   (make-local-hook 'message-sent-hook)
208   (add-hook 'message-sent-hook 'gnus-inews-do-gcc nil t)
209   (setq message-post-method
210         `(lambda (arg)
211            (gnus-post-method arg ,gnus-newsgroup-name)))
212   (setq message-user-agent (gnus-extended-version))
213   (message-add-action
214    `(set-window-configuration ,winconf) 'exit 'postpone 'kill)
215   (message-add-action
216    `(when (gnus-buffer-exists-p ,buffer)
217       (save-excursion
218         (set-buffer ,buffer)
219         ,(when article
220            `(gnus-summary-mark-article-as-replied ,article))))
221    'send))
222
223 (put 'gnus-setup-message 'lisp-indent-function 1)
224 (put 'gnus-setup-message 'edebug-form-spec '(form body))
225
226 ;;; Post news commands of Gnus group mode and summary mode
227
228 (defun gnus-group-mail ()
229   "Start composing a mail."
230   (interactive)
231   (gnus-setup-message 'message
232     (message-mail)))
233
234 (defun gnus-group-post-news (&optional arg)
235   "Start composing a news message.
236 If ARG, post to the group under point.
237 If ARG is 1, prompt for a group name."
238   (interactive "P")
239   ;; Bind this variable here to make message mode hooks work ok.
240   (let ((gnus-newsgroup-name
241          (if arg
242              (if (= 1 (prefix-numeric-value arg))
243                  (completing-read "Newsgroup: " gnus-active-hashtb nil
244                                   (gnus-read-active-file-p))
245                (gnus-group-group-name))
246            "")))
247     (gnus-post-news 'post gnus-newsgroup-name)))
248
249 (defun gnus-summary-post-news ()
250   "Start composing a news message."
251   (interactive)
252   (gnus-post-news 'post gnus-newsgroup-name))
253
254 (defun gnus-summary-followup (yank &optional force-news)
255   "Compose a followup to an article.
256 If prefix argument YANK is non-nil, original article is yanked automatically."
257   (interactive
258    (list (and current-prefix-arg
259               (gnus-summary-work-articles 1))))
260   (when yank
261     (gnus-summary-goto-subject (car yank)))
262   (save-window-excursion
263     (gnus-summary-select-article))
264   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
265         (gnus-newsgroup-name gnus-newsgroup-name))
266     ;; Send a followup.
267     (gnus-post-news nil gnus-newsgroup-name
268                     headers gnus-article-buffer
269                     yank nil force-news)))
270
271 (defun gnus-summary-followup-with-original (n &optional force-news)
272   "Compose a followup to an article and include the original article."
273   (interactive "P")
274   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
275
276 (defun gnus-summary-followup-to-mail (&optional arg)
277   "Followup to the current mail message via news."
278   (interactive
279    (list (and current-prefix-arg
280               (gnus-summary-work-articles 1))))
281   (gnus-summary-followup arg t))
282
283 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
284   "Followup to the current mail message via news."
285   (interactive "P")
286   (gnus-summary-followup (gnus-summary-work-articles arg) t))
287
288 (defun gnus-inews-yank-articles (articles)
289   (let (beg article)
290     (message-goto-body)
291     (while (setq article (pop articles))
292       (save-window-excursion
293         (set-buffer gnus-summary-buffer)
294         (gnus-summary-select-article nil nil nil article)
295         (gnus-summary-remove-process-mark article))
296       (gnus-copy-article-buffer)
297       (let ((message-reply-buffer gnus-article-copy)
298             (message-reply-headers gnus-current-headers))
299         (message-yank-original)
300         (setq beg (or beg (mark t))))
301       (when articles
302         (insert "\n")))
303     (push-mark)
304     (goto-char beg)))
305
306 (defun gnus-summary-cancel-article (&optional n symp)
307   "Cancel an article you posted.
308 Uses the process-prefix convention.  If given the symbolic
309 prefix `a', cancel using the standard posting method; if not
310 post using the current select method."
311   (interactive (gnus-interactive "P\ny"))
312   (let ((articles (gnus-summary-work-articles n))
313         (message-post-method
314          `(lambda (arg)
315             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
316         article)
317     (while (setq article (pop articles))
318       (when (gnus-summary-select-article t nil nil article)
319         (when (gnus-eval-in-buffer-window gnus-article-buffer
320                 (save-excursion
321                   (set-buffer gnus-original-article-buffer)
322                   (message-cancel-news)))
323           (gnus-summary-mark-as-read article gnus-canceled-mark)
324           (gnus-cache-remove-article 1))
325         (gnus-article-hide-headers-if-wanted))
326       (gnus-summary-remove-process-mark article))))
327
328 (defun gnus-summary-supersede-article ()
329   "Compose an article that will supersede a previous article.
330 This is done simply by taking the old article and adding a Supersedes
331 header line with the old Message-ID."
332   (interactive)
333   (let ((article (gnus-summary-article-number)))
334     (gnus-setup-message 'reply-yank
335       (gnus-summary-select-article t)
336       (set-buffer gnus-original-article-buffer)
337       (message-supersede)
338       (push
339        `((lambda ()
340            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
341              (save-excursion
342                (set-buffer ,gnus-summary-buffer)
343                (gnus-cache-possibly-remove-article ,article nil nil nil t)
344                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
345        message-send-actions))))
346
347 \f
348
349 (defun gnus-copy-article-buffer (&optional article-buffer)
350   ;; make a copy of the article buffer with all text properties removed
351   ;; this copy is in the buffer gnus-article-copy.
352   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
353   ;; this buffer should be passed to all mail/news reply/post routines.
354   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
355   (buffer-disable-undo gnus-article-copy)
356   (let ((article-buffer (or article-buffer gnus-article-buffer))
357         end beg contents)
358     (if (not (and (get-buffer article-buffer)
359                   (gnus-buffer-exists-p article-buffer)))
360         (error "Can't find any article buffer")
361       (save-excursion
362         (set-buffer article-buffer)
363         (save-restriction
364           ;; Copy over the (displayed) article buffer, delete
365           ;; hidden text and remove text properties.
366           (widen)
367           (copy-to-buffer gnus-article-copy (point-min) (point-max))
368           (set-buffer gnus-article-copy)
369           (gnus-article-delete-text-of-type 'annotation)
370           (gnus-remove-text-with-property 'gnus-prev)
371           (gnus-remove-text-with-property 'gnus-next)
372           (insert
373            (prog1
374                (format "%s" (buffer-string))
375              (erase-buffer)))
376           ;; Find the original headers.
377           (set-buffer gnus-original-article-buffer)
378           (goto-char (point-min))
379           (while (looking-at message-unix-mail-delimiter)
380             (forward-line 1))
381           (setq beg (point))
382           (setq end (or (search-forward "\n\n" nil t) (point)))
383           ;; Delete the headers from the displayed articles.
384           (set-buffer gnus-article-copy)
385           (delete-region (goto-char (point-min))
386                          (or (search-forward "\n\n" nil t) (point)))
387           ;; Insert the original article headers.
388           (insert-buffer-substring gnus-original-article-buffer beg end)
389           (gnus-article-decode-rfc1522)))
390       gnus-article-copy)))
391
392 (defun gnus-post-news (post &optional group header article-buffer yank subject
393                             force-news)
394   (when article-buffer
395     (gnus-copy-article-buffer))
396   (let ((gnus-article-reply article-buffer)
397         (add-to-list gnus-add-to-list))
398     (gnus-setup-message (cond (yank 'reply-yank)
399                               (article-buffer 'reply)
400                               (t 'message))
401       (let* ((group (or group gnus-newsgroup-name))
402              (pgroup group)
403              to-address to-group mailing-list to-list
404              newsgroup-p)
405         (when group
406           (setq to-address (gnus-group-find-parameter group 'to-address)
407                 to-group (gnus-group-find-parameter group 'to-group)
408                 to-list (gnus-group-find-parameter group 'to-list)
409                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
410                 mailing-list (when gnus-mailing-list-groups
411                                (string-match gnus-mailing-list-groups group))
412                 group (gnus-group-real-name group)))
413         (if (or (and to-group
414                      (gnus-news-group-p to-group))
415                 newsgroup-p
416                 force-news
417                 (and (gnus-news-group-p
418                       (or pgroup gnus-newsgroup-name)
419                       (if header (mail-header-number header)
420                         gnus-current-article))
421                      (not mailing-list)
422                      (not to-list)
423                      (not to-address)))
424             ;; This is news.
425             (if post
426                 (message-news (or to-group group))
427               (set-buffer gnus-article-copy)
428               (gnus-msg-treat-broken-reply-to)
429               (message-followup (if (or newsgroup-p force-news) nil to-group)))
430           ;; The is mail.
431           (if post
432               (progn
433                 (message-mail (or to-address to-list))
434                 ;; Arrange for mail groups that have no `to-address' to
435                 ;; get that when the user sends off the mail.
436                 (when (and (not to-list)
437                            (not to-address)
438                            add-to-list)
439                   (push (list 'gnus-inews-add-to-address pgroup)
440                         message-send-actions)))
441             (set-buffer gnus-article-copy)
442             (gnus-msg-treat-broken-reply-to)
443             (message-wide-reply to-address)))
444         (when yank
445           (gnus-inews-yank-articles yank))))))
446
447 (defun gnus-msg-treat-broken-reply-to ()
448   "Remove the Reply-to header iff broken-reply-to."
449   (when (gnus-group-find-parameter
450          gnus-newsgroup-name 'broken-reply-to)
451     (save-restriction
452       (message-narrow-to-head)
453       (message-remove-header "reply-to"))))
454
455 (defun gnus-post-method (arg group &optional silent)
456   "Return the posting method based on GROUP and ARG.
457 If SILENT, don't prompt the user."
458   (let ((group-method (gnus-find-method-for-group group)))
459     (cond
460      ;; If the group-method is nil (which shouldn't happen) we use
461      ;; the default method.
462      ((null group-method)
463       (or (and (null (eq gnus-post-method 'active)) gnus-post-method)
464                gnus-select-method message-post-method))
465      ;; We want the inverse of the default
466      ((and arg (not (eq arg 0)))
467       (if (eq gnus-post-method 'active)
468           gnus-select-method
469         group-method))
470      ;; We query the user for a post method.
471      ((or arg
472           (and gnus-post-method
473                (not (eq gnus-post-method 'current))
474                (listp (car gnus-post-method))))
475       (let* ((methods
476               ;; Collect all methods we know about.
477               (append
478                (when (and gnus-post-method
479                           (not (eq gnus-post-method 'current)))
480                  (if (listp (car gnus-post-method))
481                      gnus-post-method
482                    (list gnus-post-method)))
483                gnus-secondary-select-methods
484                (mapcar 'cdr gnus-server-alist)
485                (list gnus-select-method)
486                (list group-method)))
487              method-alist post-methods method)
488         ;; Weed out all mail methods.
489         (while methods
490           (setq method (gnus-server-get-method "" (pop methods)))
491           (when (or (gnus-method-option-p method 'post)
492                     (gnus-method-option-p method 'post-mail))
493             (push method post-methods)))
494         ;; Create a name-method alist.
495         (setq method-alist
496               (mapcar
497                (lambda (m)
498                  (list (concat (cadr m) " (" (symbol-name (car m)) ")") m))
499                post-methods))
500         ;; Query the user.
501         (cadr
502          (assoc
503           (setq gnus-last-posting-server
504                 (if (and silent
505                          gnus-last-posting-server)
506                     ;; Just use the last value.
507                     gnus-last-posting-server
508                   (completing-read
509                    "Posting method: " method-alist nil t
510                    (cons (or gnus-last-posting-server "") 0))))
511           method-alist))))
512      ;; Override normal method.
513      ((and (eq gnus-post-method 'current)
514            (not (eq (car group-method) 'nndraft))
515            (not arg))
516       group-method) 
517      ((and gnus-post-method
518            (not (eq gnus-post-method 'current)))
519       gnus-post-method)
520      ;; Use the normal select method.
521      (t gnus-select-method))))
522
523 \f
524
525 ;; Dummy to avoid byte-compile warning.
526 (defvar nnspool-rejected-article-hook)
527 (defvar mule-version)
528 (defvar xemacs-betaname)
529 (defvar xemacs-codename)
530
531 (defun gnus-extended-version ()
532   "Stringified Gnus version and Emacs version."
533   (interactive) ; ???
534   (concat
535    ;; Semi-gnus/VERSION
536    gnus-product-name "/" gnus-version-number
537    ;; EMACS/VERSION
538    (if (featurep 'xemacs)
539        ;; XEmacs
540        (concat
541         (format " XEmacs/%d.%d%s" emacs-major-version emacs-minor-version
542                 (if (and (boundp 'xemacs-betaname) xemacs-betaname)
543                     (if (string-match "\\`(\\(.*\\))\\'" xemacs-betaname)
544                         (match-string 1 xemacs-betaname)
545                       "")               ; unknown format
546                   ""))                  ; not beta
547         (if (boundp 'xemacs-codename)
548             (concat " (" xemacs-codename ")")
549           "")                           ; no codename
550         )
551      ;; not XEmacs
552      (concat
553       (format " Emacs/%d.%d" emacs-major-version emacs-minor-version)
554       (if (and (boundp 'enable-multibyte-characters)
555                enable-multibyte-characters)
556           ;; Should return " (multibyte)" ?
557           ""
558         " (unibyte)")
559       ))
560    ;; MULE[/VERSION]
561    (if (featurep 'mule)
562        (if (and (boundp 'mule-version) mule-version)
563            (concat " MULE/" mule-version)
564          " MULE")                       ; no mule-version
565      "")                                ; not Mule
566    ;; Meadow/VERSION
567    (if (featurep 'meadow)
568        (let ((version (Meadow-version)))
569          (if (string-match "\\`Meadow.\\([^ ]*\\)\\( (.*)\\)\\'" version)
570              (concat " Meadow/"
571                      (match-string 1 version)
572                      (match-string 2 version)
573                      )
574            "Meadow"))                   ; unknown format
575      "")                                ; not Meadow
576    ))
577
578 \f
579 ;;;
580 ;;; Gnus Mail Functions
581 ;;;
582
583 ;;; Mail reply commands of Gnus summary mode
584
585 (defun gnus-summary-reply (&optional yank wide)
586   "Start composing a reply mail to the current message.
587 If prefix argument YANK is non-nil, the original article is yanked
588 automatically."
589   (interactive
590    (list (and current-prefix-arg
591               (gnus-summary-work-articles 1))))
592   ;; Stripping headers should be specified with mail-yank-ignored-headers.
593   (when yank
594     (gnus-summary-goto-subject (car yank)))
595   (let ((gnus-article-reply t))
596     (gnus-setup-message (if yank 'reply-yank 'reply)
597       (gnus-summary-select-article)
598       (set-buffer (gnus-copy-article-buffer))
599       (gnus-msg-treat-broken-reply-to)
600       (message-reply nil wide)
601       (when yank
602         (gnus-inews-yank-articles yank)))))
603
604 (defun gnus-summary-reply-with-original (n &optional wide)
605   "Start composing a reply mail to the current message.
606 The original article will be yanked."
607   (interactive "P")
608   (gnus-summary-reply (gnus-summary-work-articles n) wide))
609
610 (defun gnus-summary-wide-reply (&optional yank)
611   "Start composing a wide reply mail to the current message.
612 If prefix argument YANK is non-nil, the original article is yanked
613 automatically."
614   (interactive
615    (list (and current-prefix-arg
616               (gnus-summary-work-articles 1))))
617   (gnus-summary-reply yank t))
618
619 (defun gnus-summary-wide-reply-with-original (n)
620   "Start composing a wide reply mail to the current message.
621 The original article will be yanked."
622   (interactive "P")
623   (gnus-summary-reply-with-original n t))
624
625 (defun gnus-summary-mail-forward (&optional full-headers post)
626   "Forward the current message to another user.
627 If FULL-HEADERS (the prefix), include full headers when forwarding."
628   (interactive "P")
629   (gnus-setup-message 'forward
630     (gnus-summary-select-article)
631     (let ((charset default-mime-charset))
632       (set-buffer gnus-original-article-buffer)
633       (make-local-variable 'default-mime-charset)
634       (setq default-mime-charset charset)
635       )
636     (let ((message-included-forward-headers
637            (if full-headers "" message-included-forward-headers)))
638       (message-forward post))))
639
640 (defun gnus-summary-resend-message (address n)
641   "Resend the current article to ADDRESS."
642   (interactive "sResend message(s) to: \nP")
643   (let ((articles (gnus-summary-work-articles n))
644         article)
645     (while (setq article (pop articles))
646       (gnus-summary-select-article nil nil nil article)
647       (save-excursion
648         (set-buffer gnus-original-article-buffer)
649         (message-resend address)))))
650
651 (defun gnus-summary-post-forward (&optional full-headers)
652   "Forward the current article to a newsgroup.
653 If FULL-HEADERS (the prefix), include full headers when forwarding."
654   (interactive "P")
655   (gnus-summary-mail-forward full-headers t))
656
657 (defvar gnus-nastygram-message
658   "The following article was inappropriately posted to %s.\n\n"
659   "Format string to insert in nastygrams.
660 The current group name will be inserted at \"%s\".")
661
662 (defun gnus-summary-mail-nastygram (n)
663   "Send a nastygram to the author of the current article."
664   (interactive "P")
665   (when (or gnus-expert-user
666             (gnus-y-or-n-p
667              "Really send a nastygram to the author of the current article? "))
668     (let ((group gnus-newsgroup-name))
669       (gnus-summary-reply-with-original n)
670       (set-buffer gnus-message-buffer)
671       (message-goto-body)
672       (insert (format gnus-nastygram-message group))
673       (message-send-and-exit))))
674
675 (defun gnus-summary-mail-crosspost-complaint (n)
676   "Send a complaint about crossposting to the current article(s)."
677   (interactive "P")
678   (let ((articles (gnus-summary-work-articles n))
679         article)
680     (while (setq article (pop articles))
681       (set-buffer gnus-summary-buffer)
682       (gnus-summary-goto-subject article)
683       (let ((group (gnus-group-real-name gnus-newsgroup-name))
684             newsgroups followup-to)
685         (gnus-summary-select-article)
686         (set-buffer gnus-original-article-buffer)
687         (if (and (<= (length (message-tokenize-header
688                               (setq newsgroups (mail-fetch-field "newsgroups"))
689                               ", "))
690                      1)
691                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
692                      (not (member group (message-tokenize-header
693                                          followup-to ", ")))))
694             (if followup-to
695                 (gnus-message 1 "Followup-to restricted")
696               (gnus-message 1 "Not a crossposted article"))
697           (set-buffer gnus-summary-buffer)
698           (gnus-summary-reply-with-original 1)
699           (set-buffer gnus-message-buffer)
700           (message-goto-body)
701           (insert (format gnus-crosspost-complaint newsgroups group))
702           (message-goto-subject)
703           (re-search-forward " *$")
704           (replace-match " (crosspost notification)" t t)
705           (gnus-deactivate-mark)
706           (when (gnus-y-or-n-p "Send this complaint? ")
707             (message-send-and-exit)))))))
708
709 (defun gnus-summary-mail-other-window ()
710   "Compose mail in other window."
711   (interactive)
712   (gnus-setup-message 'message
713     (message-mail)))
714
715 (defun gnus-mail-parse-comma-list ()
716   (let (accumulated
717         beg)
718     (skip-chars-forward " ")
719     (while (not (eobp))
720       (setq beg (point))
721       (skip-chars-forward "^,")
722       (while (zerop
723               (save-excursion
724                 (save-restriction
725                   (let ((i 0))
726                     (narrow-to-region beg (point))
727                     (goto-char beg)
728                     (logand (progn
729                               (while (search-forward "\"" nil t)
730                                 (incf i))
731                               (if (zerop i) 2 i))
732                             2)))))
733         (skip-chars-forward ",")
734         (skip-chars-forward "^,"))
735       (skip-chars-backward " ")
736       (push (buffer-substring beg (point))
737             accumulated)
738       (skip-chars-forward "^,")
739       (skip-chars-forward ", "))
740     accumulated))
741
742 (defun gnus-inews-add-to-address (group)
743   (let ((to-address (mail-fetch-field "to")))
744     (when (and to-address
745                (gnus-alive-p))
746       ;; This mail group doesn't have a `to-list', so we add one
747       ;; here.  Magic!
748       (when (gnus-y-or-n-p
749              (format "Do you want to add this as `to-list': %s " to-address))
750         (gnus-group-add-parameter group (cons 'to-list to-address))))))
751
752 (defun gnus-put-message ()
753   "Put the current message in some group and return to Gnus."
754   (interactive)
755   (let ((reply gnus-article-reply)
756         (winconf gnus-prev-winconf)
757         (group gnus-newsgroup-name))
758
759     (or (and group (not (gnus-group-read-only-p group)))
760         (setq group (read-string "Put in group: " nil
761                                  (gnus-writable-groups))))
762     (when (gnus-gethash group gnus-newsrc-hashtb)
763       (error "No such group: %s" group))
764
765     (save-excursion
766       (save-restriction
767         (widen)
768         (message-narrow-to-headers)
769         (let (gnus-deletable-headers)
770           (if (message-news-p)
771               (message-generate-headers message-required-news-headers)
772             (message-generate-headers message-required-mail-headers)))
773         (goto-char (point-max))
774         (insert "Gcc: " group "\n")
775         (widen)))
776
777     (gnus-inews-do-gcc)
778
779     (when (get-buffer gnus-group-buffer)
780       (when (gnus-buffer-exists-p (car-safe reply))
781         (set-buffer (car reply))
782         (and (cdr reply)
783              (gnus-summary-mark-article-as-replied
784               (cdr reply))))
785       (when winconf
786         (set-window-configuration winconf)))))
787
788 (defun gnus-article-mail (yank)
789   "Send a reply to the address near point.
790 If YANK is non-nil, include the original article."
791   (interactive "P")
792   (let ((address
793          (buffer-substring
794           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
795           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
796     (when address
797       (message-reply address)
798       (when yank
799         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
800
801 (defvar nntp-server-type)
802 (defun gnus-bug ()
803   "Send a bug report to the Gnus maintainers."
804   (interactive)
805   (unless (gnus-alive-p)
806     (error "Gnus has been shut down"))
807   (gnus-setup-message 'bug
808     (delete-other-windows)
809     (when gnus-bug-create-help-buffer
810       (switch-to-buffer "*Gnus Help Bug*")
811       (erase-buffer)
812       (insert gnus-bug-message)
813       (goto-char (point-min)))
814     (message-pop-to-buffer "*Gnus Bug*")
815     (message-setup `((To . ,gnus-maintainer) (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 (defun gnus-configure-posting-styles ()
1064   "Configure posting styles according to `gnus-posting-styles'."
1065   (let ((styles gnus-posting-styles)
1066         (gnus-newsgroup-name (or gnus-newsgroup-name ""))
1067         style match variable attribute value value-value)
1068     ;; Go through all styles and look for matches.
1069     (while styles
1070       (setq style (pop styles)
1071             match (pop style))
1072       (when (cond ((stringp match)
1073                    ;; Regexp string match on the group name.
1074                    (string-match match gnus-newsgroup-name))
1075                   ((or (symbolp match)
1076                        (gnus-functionp match))
1077                    (cond ((gnus-functionp match)
1078                           ;; Function to be called.
1079                           (funcall match))
1080                          ((boundp match)
1081                           ;; Variable to be checked.
1082                           (symbol-value match))))
1083                   ((listp match)
1084                    ;; This is a form to be evaled.
1085                    (eval match)))
1086         ;; We have a match, so we set the variables.
1087         (while style
1088           (setq attribute (pop style)
1089                 value (cadr attribute)
1090                 variable nil)
1091           ;; We find the variable that is to be modified.
1092           (if (and (not (stringp (car attribute)))
1093                    (not (eq 'body (car attribute)))
1094                    (not (setq variable (cdr (assq (car attribute) 
1095                                                   gnus-posting-style-alist)))))
1096               (message "Couldn't find attribute %s" (car attribute))
1097             ;; We get the value.
1098             (setq value-value
1099                   (cond ((stringp value)
1100                          value)
1101                         ((or (symbolp value)
1102                              (gnus-functionp value))
1103                          (cond ((gnus-functionp value)
1104                                 (funcall value))
1105                                ((boundp value)
1106                                 (symbol-value value))))
1107                         ((listp value)
1108                          (eval value))))
1109             (if variable
1110                 ;; This is an ordinary variable.
1111                 (set (make-local-variable variable) value-value)
1112               ;; This is either a body or a header to be inserted in the
1113               ;; message.
1114               (when value-value
1115                 (let ((attr (car attribute)))
1116                   (make-local-variable 'message-setup-hook)
1117                   (if (eq 'body attr)
1118                       (add-hook 'message-setup-hook
1119                                 `(lambda ()
1120                                    (save-excursion
1121                                      (message-goto-body)
1122                                      (insert ,value-value))))
1123                     (add-hook
1124                      'message-setup-hook
1125                      `(lambda ()
1126                         (save-excursion
1127                           (message-goto-eoh)
1128                           (insert ,(if (stringp attr) attr (symbol-name attr))
1129                                   ": " ,value-value "\n"))))))))))))))
1130
1131 ;;; Allow redefinition of functions.
1132
1133 (gnus-ems-redefine)
1134
1135 (provide 'gnus-msg)
1136
1137 ;;; gnus-msg.el ends here