* gnus-msg.el (gnus-configure-posting-styles): Just remove the header if nil.
[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     (save-excursion
873       (set-buffer (gnus-get-buffer-create " *gnus environment info*"))
874       (gnus-debug))
875     (insert "<#part type=application/x-emacs-lisp buffer=\" *gnus environment info*\" disposition=inline description=\"User settings\"><#/part>")
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-summary-yank-message (buffer n)
885   "Yank the current article into a composed message."
886   (interactive
887    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
888          current-prefix-arg))
889   (gnus-summary-iterate n
890     (let ((gnus-display-mime-function nil)
891           (gnus-inhibit-treatment t))
892       (gnus-summary-select-article))
893     (save-excursion
894       (set-buffer buffer)
895       (message-yank-buffer gnus-article-buffer))))
896
897 (defun gnus-debug ()
898   "Attempts to go through the Gnus source file and report what variables have been changed.
899 The source file has to be in the Emacs load path."
900   (interactive)
901   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
902                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
903                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
904                  "nnmail.el" "message.el"))
905         (point (point))
906         file expr olist sym)
907     (gnus-message 4 "Please wait while we snoop your variables...")
908     (sit-for 0)
909     ;; Go through all the files looking for non-default values for variables.
910     (save-excursion
911       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
912       (while files
913         (erase-buffer)
914         (when (and (setq file (locate-library (pop files)))
915                    (file-exists-p file))
916           (insert-file-contents file)
917           (goto-char (point-min))
918           (if (not (re-search-forward "^;;* *Internal variables" nil t))
919               (gnus-message 4 "Malformed sources in file %s" file)
920             (narrow-to-region (point-min) (point))
921             (goto-char (point-min))
922             (while (setq expr (ignore-errors (read (current-buffer))))
923               (ignore-errors
924                 (and (or (eq (car expr) 'defvar)
925                          (eq (car expr) 'defcustom))
926                      (stringp (nth 3 expr))
927                      (or (not (boundp (nth 1 expr)))
928                          (not (equal (eval (nth 2 expr))
929                                      (symbol-value (nth 1 expr)))))
930                      (push (nth 1 expr) olist)))))))
931       (kill-buffer (current-buffer)))
932     (when (setq olist (nreverse olist))
933       (insert "------------------ Environment follows ------------------\n\n"))
934     (while olist
935       (if (boundp (car olist))
936           (condition-case ()
937               (pp `(setq ,(car olist)
938                          ,(if (or (consp (setq sym (symbol-value (car olist))))
939                                   (and (symbolp sym)
940                                        (not (or (eq sym nil)
941                                                 (eq sym t)))))
942                               (list 'quote (symbol-value (car olist)))
943                             (symbol-value (car olist))))
944                   (current-buffer))
945             (error
946              (format "(setq %s 'whatever)\n" (car olist))))
947         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
948       (setq olist (cdr olist)))
949     (insert "\n\n")
950     ;; Remove any control chars - they seem to cause trouble for some
951     ;; mailers.  (Byte-compiled output from the stuff above.)
952     (goto-char point)
953     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
954       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
955                      t t))))
956
957 ;;; Treatment of rejected articles.
958 ;;; Bounced mail.
959
960 (defun gnus-summary-resend-bounced-mail (&optional fetch)
961   "Re-mail the current message.
962 This only makes sense if the current message is a bounce message than
963 contains some mail you have written which has been bounced back to
964 you.
965 If FETCH, try to fetch the article that this is a reply to, if indeed
966 this is a reply."
967   (interactive "P")
968   (gnus-summary-select-article t)
969   (set-buffer gnus-original-article-buffer)
970   (gnus-setup-message 'compose-bounce
971     (let* ((references (mail-fetch-field "references"))
972            (parent (and references (gnus-parent-id references))))
973       (message-bounce)
974       ;; If there are references, we fetch the article we answered to.
975       (and fetch parent
976            (gnus-summary-refer-article parent)
977            (gnus-summary-show-all-headers)))))
978
979 ;;; Gcc handling.
980
981 ;; Do Gcc handling, which copied the message over to some group.
982 (defun gnus-inews-do-gcc (&optional gcc)
983   (interactive)
984   (when (gnus-alive-p)
985     (save-excursion
986       (save-restriction
987         (message-narrow-to-headers)
988         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
989               (cur (current-buffer))
990               groups group method)
991           (when gcc
992             (message-remove-header "gcc")
993             (widen)
994             (setq groups (message-tokenize-header gcc " ,"))
995             ;; Copy the article over to some group(s).
996             (while (setq group (pop groups))
997               (gnus-check-server
998                (setq method
999                      (cond ((and (null (gnus-get-info group))
1000                                  (eq (car gnus-message-archive-method)
1001                                      (car
1002                                       (gnus-server-to-method
1003                                        (gnus-group-method group)))))
1004                             ;; If the group doesn't exist, we assume
1005                             ;; it's an archive group...
1006                             gnus-message-archive-method)
1007                            ;; Use the method.
1008                            ((gnus-info-method (gnus-get-info group))
1009                             (gnus-info-method (gnus-get-info group)))
1010                            ;; Find the method.
1011                            (t (gnus-group-method group)))))
1012               (gnus-check-server method)
1013               (unless (gnus-request-group group t method)
1014                 (gnus-request-create-group group method))
1015               (save-excursion
1016                 (nnheader-set-temp-buffer " *acc*")
1017                 (insert-buffer-substring cur)
1018                 (message-encode-message-body)
1019                 (save-restriction
1020                   (message-narrow-to-headers)
1021                   (mail-encode-encoded-word-buffer))
1022                 (goto-char (point-min))
1023                 (when (re-search-forward
1024                        (concat "^" (regexp-quote mail-header-separator) "$")
1025                        nil t)
1026                   (replace-match "" t t ))
1027                 (unless (gnus-request-accept-article group method t t)
1028                   (gnus-message 1 "Couldn't store article in group %s: %s"
1029                                 group (gnus-status-message method))
1030                   (sit-for 2))
1031                 (kill-buffer (current-buffer))))))))))
1032
1033 (defun gnus-inews-insert-gcc ()
1034   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1035   (save-excursion
1036     (save-restriction
1037       (message-narrow-to-headers)
1038       (let* ((group gnus-outgoing-message-group)
1039              (gcc (cond
1040                    ((gnus-functionp group)
1041                     (funcall group))
1042                    ((or (stringp group) (list group))
1043                     group))))
1044         (when gcc
1045           (insert "Gcc: "
1046                   (if (stringp gcc) gcc
1047                     (mapconcat 'identity gcc " "))
1048                   "\n"))))))
1049
1050 (defun gnus-inews-insert-archive-gcc (&optional group)
1051   "Insert the Gcc to say where the article is to be archived."
1052   (let* ((var gnus-message-archive-group)
1053          (group (or group gnus-newsgroup-name ""))
1054          (gcc-self-val
1055           (and gnus-newsgroup-name
1056                (not (equal gnus-newsgroup-name ""))
1057                (gnus-group-find-parameter
1058                 gnus-newsgroup-name 'gcc-self)))
1059          result
1060          (groups
1061           (cond
1062            ((null gnus-message-archive-method)
1063             ;; Ignore.
1064             nil)
1065            ((stringp var)
1066             ;; Just a single group.
1067             (list var))
1068            ((null var)
1069             ;; We don't want this.
1070             nil)
1071            ((and (listp var) (stringp (car var)))
1072             ;; A list of groups.
1073             var)
1074            ((gnus-functionp var)
1075             ;; A function.
1076             (funcall var group))
1077            (t
1078             ;; An alist of regexps/functions/forms.
1079             (while (and var
1080                         (not
1081                          (setq result
1082                                (cond
1083                                 ((stringp (caar var))
1084                                  ;; Regexp.
1085                                  (when (string-match (caar var) group)
1086                                    (cdar var)))
1087                                 ((gnus-functionp (car var))
1088                                  ;; Function.
1089                                  (funcall (car var) group))
1090                                 (t
1091                                  (eval (car var)))))))
1092               (setq var (cdr var)))
1093             result)))
1094          name)
1095     (when (or groups gcc-self-val)
1096       (when (stringp groups)
1097         (setq groups (list groups)))
1098       (save-excursion
1099         (save-restriction
1100           (message-narrow-to-headers)
1101           (goto-char (point-max))
1102           (insert "Gcc: ")
1103           (if gcc-self-val
1104               ;; Use the `gcc-self' param value instead.
1105               (progn
1106                 (insert
1107                  (if (stringp gcc-self-val)
1108                      gcc-self-val
1109                    group))
1110                 (if (not (eq gcc-self-val 'none))
1111                     (insert "\n")
1112                   (progn
1113                     (beginning-of-line)
1114                     (kill-line))))
1115             ;; Use the list of groups.
1116             (while (setq name (pop groups))
1117               (insert (if (string-match ":" name)
1118                           name
1119                         (gnus-group-prefixed-name
1120                          name gnus-message-archive-method)))
1121               (when groups
1122                 (insert " ")))
1123             (insert "\n")))))))
1124
1125 ;;; Posting styles.
1126
1127 (defun gnus-configure-posting-styles ()
1128   "Configure posting styles according to `gnus-posting-styles'."
1129   (unless gnus-inhibit-posting-styles
1130     (let ((group (or gnus-newsgroup-name ""))
1131           (styles gnus-posting-styles)
1132           style match variable attribute value v results
1133           filep name address element)
1134       ;; If the group has a posting-style parameter, add it at the end with a
1135       ;; regexp matching everything, to be sure it takes precedence over all
1136       ;; the others.
1137       (when gnus-newsgroup-name
1138         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1139           (when tmp-style
1140             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1141       ;; Go through all styles and look for matches.
1142       (dolist (style styles)
1143         (setq match (pop style))
1144         (goto-char (point-min))
1145         (when (cond
1146                ((stringp match)
1147                 ;; Regexp string match on the group name.
1148                 (string-match match group))
1149                ((eq match 'header)
1150                 (let ((header (message-fetch-field (pop style))))
1151                   (and header
1152                        (string-match (pop style) header))))
1153                ((or (symbolp match)
1154                     (gnus-functionp match))
1155                 (cond
1156                  ((gnus-functionp match)
1157                   ;; Function to be called.
1158                   (funcall match))
1159                  ((boundp match)
1160                   ;; Variable to be checked.
1161                   (symbol-value match))))
1162                ((listp match)
1163                 ;; This is a form to be evaled.
1164                 (eval match)))
1165           ;; We have a match, so we set the variables.
1166           (dolist (attribute style)
1167             (setq element (pop attribute)
1168                   variable nil
1169                   filep nil)
1170             (setq value
1171                   (cond
1172                    ((eq (car attribute) :file)
1173                     (setq filep t)
1174                     (cadr attribute))
1175                    ((eq (car attribute) :value)
1176                     (cadr attribute))
1177                    (t
1178                     (car attribute))))
1179             ;; We get the value.
1180             (setq v
1181                   (cond
1182                    ((stringp value)
1183                     value)
1184                    ((or (symbolp value)
1185                         (gnus-functionp value))
1186                     (cond ((gnus-functionp value)
1187                            (funcall value))
1188                           ((boundp value)
1189                            (symbol-value value))))
1190                    ((listp value)
1191                     (eval value))))
1192             ;; Translate obsolescent value.
1193             (when (eq element 'signature-file)
1194               (setq element 'signature
1195                     filep t))
1196             ;; Get the contents of file elems.
1197             (when (and filep v)
1198               (setq v (with-temp-buffer
1199                         (insert-file-contents v)
1200                         (buffer-string))))
1201             (setq results (delq (assoc element results) results))
1202             (push (cons element v) results))))
1203       ;; Now we have all the styles, so we insert them.
1204       (setq name (assq 'name results)
1205             address (assq 'address results))
1206       (setq results (delq name (delq address results)))
1207       (make-local-variable 'message-setup-hook)
1208       (dolist (result results)
1209         (add-hook 'message-setup-hook
1210                   (cond
1211                    ((eq 'eval (car result))
1212                     'ignore)
1213                    ((eq 'body (car result))
1214                     `(lambda ()
1215                        (save-excursion
1216                          (message-goto-body)
1217                          (insert ,(cdr result)))))
1218                    ((eq 'signature (car result))
1219                     (set (make-local-variable 'message-signature) nil)
1220                     (set (make-local-variable 'message-signature-file) nil)
1221                     (if (not (cdr result))
1222                         'ignore
1223                       `(lambda ()
1224                          (save-excursion
1225                            (let ((message-signature ,(cdr result)))
1226                              (when message-signature
1227                                (message-insert-signature)))))))
1228                    (t
1229                     (let ((header
1230                            (if (symbolp (car result))
1231                                (capitalize (symbol-name (car result)))
1232                              (car result))))
1233                       `(lambda ()
1234                          (save-excursion
1235                            (message-remove-header ,header)
1236                            (let ((value ,(cdr result)))
1237                              (when value
1238                                (message-goto-eoh)
1239                                (insert ,header ": " value "\n"))))))))))
1240       (when (or name address)
1241         (add-hook 'message-setup-hook
1242                   `(lambda ()
1243                      (set (make-local-variable 'user-mail-address)
1244                           ,(or (cdr address) user-mail-address))
1245                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1246                            (user-mail-address
1247                             ,(or (cdr address) user-mail-address)))
1248                        (save-excursion
1249                          (message-remove-header "From")
1250                          (message-goto-eoh)
1251                          (insert "From: " (message-make-from) "\n")))))))))
1252
1253 ;;; Allow redefinition of functions.
1254
1255 (gnus-ems-redefine)
1256
1257 (provide 'gnus-msg)
1258
1259 ;;; gnus-msg.el ends here