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