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