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