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