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