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