(nnimap-group-overview-filename): Create directory for
[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 than 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 ;;;###autoload
206 (defun gnus-msg-mail (&rest args)
207   "Start editing a mail message to be sent.
208 Like `message-mail', but with Gnus paraphernalia, particularly the
209 the Gcc: header for archiving purposes."
210   (interactive)
211   (gnus-setup-message 'message
212     (apply 'message-mail args)))
213
214 ;;;###autoload
215 (define-mail-user-agent 'gnus-user-agent
216       'gnus-msg-mail 'message-send-and-exit
217       'message-kill-buffer 'message-send-hook)
218
219 ;;; Internal functions.
220
221 (defvar gnus-article-reply nil)
222 (defmacro gnus-setup-message (config &rest forms)
223   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
224         (buffer (make-symbol "gnus-setup-message-buffer"))
225         (article (make-symbol "gnus-setup-message-article"))
226         (group (make-symbol "gnus-setup-message-group")))
227     `(let ((,winconf (current-window-configuration))
228            (,buffer (buffer-name (current-buffer)))
229            (,article (and gnus-article-reply (gnus-summary-article-number)))
230            (,group gnus-newsgroup-name)
231            (message-header-setup-hook
232             (copy-sequence message-header-setup-hook))
233            (mbl mml-buffer-list)
234            (message-mode-hook (copy-sequence message-mode-hook)))
235        (setq mml-buffer-list nil)
236        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
237        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
238        (add-hook 'message-mode-hook 'gnus-configure-posting-styles)
239        (unwind-protect
240            (progn
241              ,@forms)
242          (gnus-inews-add-send-actions ,winconf ,buffer ,article)
243          (setq gnus-message-buffer (current-buffer))
244          (set (make-local-variable 'gnus-message-group-art)
245               (cons ,group ,article))
246          (set (make-local-variable 'gnus-newsgroup-name) ,group)
247          (gnus-run-hooks 'gnus-message-setup-hook)
248          (if (eq major-mode 'message-mode)
249              ;; Make mml-buffer-list local.
250              ;; Restore global mml-buffer-list value as mbl.
251              ;; What a hack! -- Shenghuo
252              (let ((mml-buffer-list mml-buffer-list))
253                (setq mml-buffer-list mbl)
254                (make-local-variable 'mml-buffer-list)
255                (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))
256            (mml-destroy-buffers)
257            (setq mml-buffer-list mbl)))
258        (gnus-add-buffer)
259        (gnus-configure-windows ,config t)
260        (set-buffer-modified-p nil))))
261
262 (defun gnus-setup-posting-charset (group)
263   (let ((alist gnus-group-posting-charset-alist)
264         (group (or group ""))
265         elem)
266     (when group
267       (catch 'found
268         (while (setq elem (pop alist))
269           (when (or (and (stringp (car elem))
270                          (string-match (car elem) group))
271                     (and (gnus-functionp (car elem))
272                          (funcall (car elem) group))
273                     (and (symbolp (car elem))
274                          (symbol-value (car elem))))
275             (throw 'found (cons (cadr elem) (caddr elem)))))))))
276
277 (defun gnus-inews-add-send-actions (winconf buffer article)
278   (make-local-hook 'message-sent-hook)
279   (add-hook 'message-sent-hook (if gnus-agent 'gnus-agent-possibly-do-gcc
280                                  'gnus-inews-do-gcc) nil t)
281   (when gnus-agent
282     (make-local-hook 'message-header-hook)
283     (add-hook 'message-header-hook 'gnus-agent-possibly-save-gcc nil t))
284   (setq message-post-method
285         `(lambda (arg)
286            (gnus-post-method arg ,gnus-newsgroup-name)))
287   (setq message-newsreader (setq message-mailer (gnus-extended-version)))
288   (message-add-action
289    `(set-window-configuration ,winconf) 'exit 'postpone 'kill)
290   (message-add-action
291    `(when (gnus-buffer-exists-p ,buffer)
292       (save-excursion
293         (set-buffer ,buffer)
294         ,(when article
295            `(gnus-summary-mark-article-as-replied ,article))))
296    'send))
297
298 (put 'gnus-setup-message 'lisp-indent-function 1)
299 (put 'gnus-setup-message 'edebug-form-spec '(form body))
300
301 ;;; Post news commands of Gnus group mode and summary mode
302
303 (defun gnus-group-mail (&optional arg)
304   "Start composing a mail.
305 If ARG, use the group under the point to find a posting style.
306 If ARG is 1, prompt for a group name to find the posting style."
307   (interactive "P")
308   ;; We can't `let' gnus-newsgroup-name here, since that leads
309   ;; to local variables leaking.
310   (let ((group gnus-newsgroup-name)
311         (buffer (current-buffer)))
312     (unwind-protect
313         (progn
314           (setq gnus-newsgroup-name
315                 (if arg
316                     (if (= 1 (prefix-numeric-value arg))
317                         (completing-read "Use posting style of group: "
318                                          gnus-active-hashtb nil
319                                          (gnus-read-active-file-p))
320                       (gnus-group-group-name))
321                   ""))
322           (gnus-setup-message 'message (message-mail)))
323       (save-excursion
324         (set-buffer buffer)
325         (setq gnus-newsgroup-name group)))))
326
327 (defun gnus-group-post-news (&optional arg)
328   "Start composing a news message.
329 If ARG, post to the group under point.
330 If ARG is 1, prompt for a group name."
331   (interactive "P")
332   ;; Bind this variable here to make message mode hooks work ok.
333   (let ((gnus-newsgroup-name
334          (if arg
335              (if (= 1 (prefix-numeric-value arg))
336                  (completing-read "Newsgroup: " gnus-active-hashtb nil
337                                   (gnus-read-active-file-p))
338                (gnus-group-group-name))
339            "")))
340     (gnus-post-news 'post gnus-newsgroup-name)))
341
342 (defun gnus-summary-post-news ()
343   "Start composing a news message."
344   (interactive)
345   (gnus-post-news 'post gnus-newsgroup-name))
346
347 (defun gnus-summary-followup (yank &optional force-news)
348   "Compose a followup to an article.
349 If prefix argument YANK is non-nil, original article is yanked automatically."
350   (interactive
351    (list (and current-prefix-arg
352               (gnus-summary-work-articles 1))))
353   (when yank
354     (gnus-summary-goto-subject (car yank)))
355   (save-window-excursion
356     (gnus-summary-select-article))
357   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
358         (gnus-newsgroup-name gnus-newsgroup-name))
359     ;; Send a followup.
360     (gnus-post-news nil gnus-newsgroup-name
361                     headers gnus-article-buffer
362                     yank nil force-news)))
363
364 (defun gnus-summary-followup-with-original (n &optional force-news)
365   "Compose a followup to an article and include the original article."
366   (interactive "P")
367   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
368
369 (defun gnus-summary-followup-to-mail (&optional arg)
370   "Followup to the current mail message via news."
371   (interactive
372    (list (and current-prefix-arg
373               (gnus-summary-work-articles 1))))
374   (gnus-summary-followup arg t))
375
376 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
377   "Followup to the current mail message via news."
378   (interactive "P")
379   (gnus-summary-followup (gnus-summary-work-articles arg) t))
380
381 (defun gnus-inews-yank-articles (articles)
382   (let (beg article)
383     (message-goto-body)
384     (while (setq article (pop articles))
385       (save-window-excursion
386         (set-buffer gnus-summary-buffer)
387         (gnus-summary-select-article nil nil nil article)
388         (gnus-summary-remove-process-mark article))
389       (gnus-copy-article-buffer)
390       (let ((message-reply-buffer gnus-article-copy)
391             (message-reply-headers gnus-current-headers))
392         (message-yank-original)
393         (setq beg (or beg (mark t))))
394       (when articles
395         (insert "\n")))
396     (push-mark)
397     (goto-char beg)))
398
399 (defun gnus-summary-cancel-article (&optional n symp)
400   "Cancel an article you posted.
401 Uses the process-prefix convention.  If given the symbolic
402 prefix `a', cancel using the standard posting method; if not
403 post using the current select method."
404   (interactive (gnus-interactive "P\ny"))
405   (let ((articles (gnus-summary-work-articles n))
406         (message-post-method
407          `(lambda (arg)
408             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
409         article)
410     (while (setq article (pop articles))
411       (when (gnus-summary-select-article t nil nil article)
412         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
413                 (message-cancel-news))
414           (gnus-summary-mark-as-read article gnus-canceled-mark)
415           (gnus-cache-remove-article 1))
416         (gnus-article-hide-headers-if-wanted))
417       (gnus-summary-remove-process-mark article))))
418
419 (defun gnus-summary-supersede-article ()
420   "Compose an article that will supersede a previous article.
421 This is done simply by taking the old article and adding a Supersedes
422 header line with the old Message-ID."
423   (interactive)
424   (let ((article (gnus-summary-article-number)))
425     (gnus-setup-message 'reply-yank
426       (gnus-summary-select-article t)
427       (set-buffer gnus-original-article-buffer)
428       (message-supersede)
429       (push
430        `((lambda ()
431            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
432              (save-excursion
433                (set-buffer ,gnus-summary-buffer)
434                (gnus-cache-possibly-remove-article ,article nil nil nil t)
435                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
436        message-send-actions))))
437
438 \f
439
440 (defun gnus-copy-article-buffer (&optional article-buffer)
441   ;; make a copy of the article buffer with all text properties removed
442   ;; this copy is in the buffer gnus-article-copy.
443   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
444   ;; this buffer should be passed to all mail/news reply/post routines.
445   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
446   (save-excursion
447     (set-buffer gnus-article-copy)
448     (mm-enable-multibyte))
449   (let ((article-buffer (or article-buffer gnus-article-buffer))
450         end beg)
451     (if (not (and (get-buffer article-buffer)
452                   (gnus-buffer-exists-p article-buffer)))
453         (error "Can't find any article buffer")
454       (save-excursion
455         (set-buffer article-buffer)
456         (save-restriction
457           ;; Copy over the (displayed) article buffer, delete
458           ;; hidden text and remove text properties.
459           (widen)
460           (copy-to-buffer gnus-article-copy (point-min) (point-max))
461           (set-buffer gnus-article-copy)
462           (gnus-article-delete-text-of-type 'annotation)
463           (gnus-remove-text-with-property 'gnus-prev)
464           (gnus-remove-text-with-property 'gnus-next)
465           (insert
466            (prog1
467                (buffer-substring-no-properties (point-min) (point-max))
468              (erase-buffer)))
469           ;; Find the original headers.
470           (set-buffer gnus-original-article-buffer)
471           (goto-char (point-min))
472           (while (looking-at message-unix-mail-delimiter)
473             (forward-line 1))
474           (setq beg (point))
475           (setq end (or (search-forward "\n\n" nil t) (point)))
476           ;; Delete the headers from the displayed articles.
477           (set-buffer gnus-article-copy)
478           (delete-region (goto-char (point-min))
479                          (or (search-forward "\n\n" nil t) (point-max)))
480           ;; Insert the original article headers.
481           (insert-buffer-substring gnus-original-article-buffer beg end)
482           (article-decode-encoded-words)))
483       gnus-article-copy)))
484
485 (defun gnus-post-news (post &optional group header article-buffer yank subject
486                             force-news)
487   (when article-buffer
488     (gnus-copy-article-buffer))
489   (let ((gnus-article-reply article-buffer)
490         (add-to-list gnus-add-to-list))
491     (gnus-setup-message (cond (yank 'reply-yank)
492                               (article-buffer 'reply)
493                               (t 'message))
494       (let* ((group (or group gnus-newsgroup-name))
495              (charset (gnus-group-name-charset nil group))
496              (pgroup group)
497              to-address to-group mailing-list to-list
498              newsgroup-p)
499         (when group
500           (setq to-address (gnus-group-find-parameter group 'to-address)
501                 to-group (gnus-group-find-parameter group 'to-group)
502                 to-list (gnus-group-find-parameter group 'to-list)
503                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
504                 mailing-list (when gnus-mailing-list-groups
505                                (string-match gnus-mailing-list-groups group))
506                 group (gnus-group-name-decode (gnus-group-real-name group)
507                                               charset)))
508         (if (or (and to-group
509                      (gnus-news-group-p to-group))
510                 newsgroup-p
511                 force-news
512                 (and (gnus-news-group-p
513                       (or pgroup gnus-newsgroup-name)
514                       (if header (mail-header-number header)
515                         gnus-current-article))
516                      (not mailing-list)
517                      (not to-list)
518                      (not to-address)))
519             ;; This is news.
520             (if post
521                 (message-news (or to-group group))
522               (set-buffer gnus-article-copy)
523               (gnus-msg-treat-broken-reply-to)
524               (message-followup (if (or newsgroup-p force-news) nil to-group)))
525           ;; The is mail.
526           (if post
527               (progn
528                 (message-mail (or to-address to-list))
529                 ;; Arrange for mail groups that have no `to-address' to
530                 ;; get that when the user sends off the mail.
531                 (when (and (not to-list)
532                            (not to-address)
533                            add-to-list)
534                   (push (list 'gnus-inews-add-to-address pgroup)
535                         message-send-actions)))
536             (set-buffer gnus-article-copy)
537             (gnus-msg-treat-broken-reply-to)
538             (message-wide-reply to-address)))
539         (when yank
540           (gnus-inews-yank-articles yank))))))
541
542 (defun gnus-msg-treat-broken-reply-to ()
543   "Remove the Reply-to header iff broken-reply-to."
544   (when (gnus-group-find-parameter
545          gnus-newsgroup-name 'broken-reply-to)
546     (save-restriction
547       (message-narrow-to-head)
548       (message-remove-header "reply-to"))))
549
550 (defun gnus-post-method (arg group &optional silent)
551   "Return the posting method based on GROUP and ARG.
552 If SILENT, don't prompt the user."
553   (let ((group-method (gnus-find-method-for-group group)))
554     (cond
555      ;; If the group-method is nil (which shouldn't happen) we use
556      ;; the default method.
557      ((null group-method)
558       (or (and (null (eq gnus-post-method 'active)) gnus-post-method)
559           gnus-select-method message-post-method))
560      ;; We want the inverse of the default
561      ((and arg (not (eq arg 0)))
562       (if (eq gnus-post-method 'active)
563           gnus-select-method
564         group-method))
565      ;; We query the user for a post method.
566      ((or arg
567           (and gnus-post-method
568                (not (eq gnus-post-method 'current))
569                (listp (car gnus-post-method))))
570       (let* ((methods
571               ;; Collect all methods we know about.
572               (append
573                (when (and gnus-post-method
574                           (not (eq gnus-post-method 'current)))
575                  (if (listp (car gnus-post-method))
576                      gnus-post-method
577                    (list gnus-post-method)))
578                gnus-secondary-select-methods
579                (mapcar 'cdr gnus-server-alist)
580                (mapcar 'car gnus-opened-servers)
581                (list gnus-select-method)
582                (list group-method)))
583              method-alist post-methods method)
584         ;; Weed out all mail methods.
585         (while methods
586           (setq method (gnus-server-get-method "" (pop methods)))
587           (when (and (or (gnus-method-option-p method 'post)
588                          (gnus-method-option-p method 'post-mail))
589                      (not (member method post-methods)))
590             (push method post-methods)))
591         ;; Create a name-method alist.
592         (setq method-alist
593               (mapcar
594                (lambda (m)
595                  (list (concat (cadr m) " (" (symbol-name (car m)) ")") m))
596                post-methods))
597         ;; Query the user.
598         (cadr
599          (assoc
600           (setq gnus-last-posting-server
601                 (if (and silent
602                          gnus-last-posting-server)
603                     ;; Just use the last value.
604                     gnus-last-posting-server
605                   (completing-read
606                    "Posting method: " method-alist nil t
607                    (cons (or gnus-last-posting-server "") 0))))
608           method-alist))))
609      ;; Override normal method.
610      ((and (eq gnus-post-method 'current)
611            (not (eq (car group-method) 'nndraft))
612            (gnus-get-function group-method 'request-post t)
613            (not arg))
614       group-method)
615      ((and gnus-post-method
616            (not (eq gnus-post-method 'current)))
617       gnus-post-method)
618      ;; Use the normal select method.
619      (t gnus-select-method))))
620
621 \f
622
623 ;; Dummies to avoid byte-compile warning.
624 (defvar nnspool-rejected-article-hook)
625 (defvar xemacs-codename)
626
627 (defun gnus-extended-version ()
628   "Stringified Gnus version and Emacs version."
629   (interactive)
630   (concat
631    "Gnus/" (prin1-to-string (gnus-continuum-version gnus-version) t)
632    " (" gnus-version ")"
633    " "
634    (cond
635     ((string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version)
636      (concat "Emacs/" (match-string 1 emacs-version)))
637     ((string-match "\\([A-Z]*[Mm][Aa][Cc][Ss]\\)[^(]*\\(\\((beta.*)\\|'\\)\\)?"
638                    emacs-version)
639      (concat (match-string 1 emacs-version)
640              (format "/%d.%d" emacs-major-version emacs-minor-version)
641              (if (match-beginning 3)
642                  (match-string 3 emacs-version)
643                "")
644              (if (boundp 'xemacs-codename)
645                  (concat " (" xemacs-codename ")")
646                "")))
647     (t emacs-version))))
648
649 \f
650 ;;;
651 ;;; Gnus Mail Functions
652 ;;;
653
654 ;;; Mail reply commands of Gnus summary mode
655
656 (defun gnus-summary-reply (&optional yank wide)
657   "Start composing a reply mail to the current message.
658 If prefix argument YANK is non-nil, the original article is yanked
659 automatically."
660   (interactive
661    (list (and current-prefix-arg
662               (gnus-summary-work-articles 1))))
663   ;; Stripping headers should be specified with mail-yank-ignored-headers.
664   (when yank
665     (gnus-summary-goto-subject (car yank)))
666   (let ((gnus-article-reply t))
667     (gnus-setup-message (if yank 'reply-yank 'reply)
668       (gnus-summary-select-article)
669       (set-buffer (gnus-copy-article-buffer))
670       (gnus-msg-treat-broken-reply-to)
671       (save-restriction
672         (message-narrow-to-head)
673         (goto-char (point-max)))
674       (mml-quote-region (point) (point-max))
675       (message-reply nil wide)
676       (when yank
677         (gnus-inews-yank-articles yank)))))
678
679 (defun gnus-summary-reply-with-original (n &optional wide)
680   "Start composing a reply mail to the current message.
681 The original article will be yanked."
682   (interactive "P")
683   (gnus-summary-reply (gnus-summary-work-articles n) wide))
684
685 (defun gnus-summary-wide-reply (&optional yank)
686   "Start composing a wide reply mail to the current message.
687 If prefix argument YANK is non-nil, the original article is yanked
688 automatically."
689   (interactive
690    (list (and current-prefix-arg
691               (gnus-summary-work-articles 1))))
692   (gnus-summary-reply yank t))
693
694 (defun gnus-summary-wide-reply-with-original (n)
695   "Start composing a wide reply mail to the current message.
696 The original article will be yanked."
697   (interactive "P")
698   (gnus-summary-reply-with-original n t))
699
700 (defun gnus-summary-mail-forward (&optional arg post)
701   "Forward the current message to another user.  
702 If ARG is nil, see `message-forward-as-mime' and `message-forward-show-mml';
703 if ARG is 1, decode the message and forward directly inline;
704 if ARG is 2, foward message as an rfc822 MIME section;
705 if ARG is 3, decode message and forward as an rfc822 MIME section;
706 if ARG is 4, foward message directly inline;
707 otherwise, use flipped `message-forward-as-mime'.
708 If POST, post instead of mail."
709   (interactive "P")
710   (let ((message-forward-as-mime message-forward-as-mime)
711         (message-forward-show-mml message-forward-show-mml))
712     (cond 
713      ((null arg))
714      ((eq arg 1) (setq message-forward-as-mime nil
715                        message-forward-show-mml t))
716      ((eq arg 2) (setq message-forward-as-mime t
717                        message-forward-show-mml nil))
718      ((eq arg 3) (setq message-forward-as-mime t
719                        message-forward-show-mml t))
720      ((eq arg 4) (setq message-forward-as-mime nil
721                        message-forward-show-mml nil))
722      (t (setq message-forward-as-mime (not message-forward-as-mime))))
723     (gnus-setup-message 'forward
724       (gnus-summary-select-article)
725       (let ((mail-parse-charset gnus-newsgroup-charset)
726             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
727             text)
728         (save-excursion
729           (set-buffer gnus-original-article-buffer)
730           (setq text (buffer-string)))
731         (set-buffer 
732          (gnus-get-buffer-create
733           (generate-new-buffer-name " *Gnus forward*")))
734         (erase-buffer)
735         (unless message-forward-show-mml
736           (mm-disable-multibyte))
737         (insert text)
738         (goto-char (point-min))
739         (when (looking-at "From ")
740           (replace-match "X-From-Line: ") )
741         (when message-forward-show-mml
742           (mime-to-mml))
743         (message-forward post)))))
744
745 (defun gnus-summary-resend-message (address n)
746   "Resend the current article to ADDRESS."
747   (interactive "sResend message(s) to: \nP")
748   (let ((articles (gnus-summary-work-articles n))
749         article)
750     (while (setq article (pop articles))
751       (gnus-summary-select-article nil nil nil article)
752       (save-excursion
753         (set-buffer gnus-original-article-buffer)
754         (message-resend address)))))
755
756 (defun gnus-summary-post-forward (&optional arg)
757   "Forward the current article to a newsgroup.
758 See `gnus-summary-mail-forward' for ARG."
759   (interactive "P")
760   (gnus-summary-mail-forward arg t))
761
762 (defvar gnus-nastygram-message
763   "The following article was inappropriately posted to %s.\n\n"
764   "Format string to insert in nastygrams.
765 The current group name will be inserted at \"%s\".")
766
767 (defun gnus-summary-mail-nastygram (n)
768   "Send a nastygram to the author of the current article."
769   (interactive "P")
770   (when (or gnus-expert-user
771             (gnus-y-or-n-p
772              "Really send a nastygram to the author of the current article? "))
773     (let ((group gnus-newsgroup-name))
774       (gnus-summary-reply-with-original n)
775       (set-buffer gnus-message-buffer)
776       (message-goto-body)
777       (insert (format gnus-nastygram-message group))
778       (message-send-and-exit))))
779
780 (defun gnus-summary-mail-crosspost-complaint (n)
781   "Send a complaint about crossposting to the current article(s)."
782   (interactive "P")
783   (let ((articles (gnus-summary-work-articles n))
784         article)
785     (while (setq article (pop articles))
786       (set-buffer gnus-summary-buffer)
787       (gnus-summary-goto-subject article)
788       (let ((group (gnus-group-real-name gnus-newsgroup-name))
789             newsgroups followup-to)
790         (gnus-summary-select-article)
791         (set-buffer gnus-original-article-buffer)
792         (if (and (<= (length (message-tokenize-header
793                               (setq newsgroups
794                                     (mail-fetch-field "newsgroups"))
795                               ", "))
796                      1)
797                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
798                      (not (member group (message-tokenize-header
799                                          followup-to ", ")))))
800             (if followup-to
801                 (gnus-message 1 "Followup-to restricted")
802               (gnus-message 1 "Not a crossposted article"))
803           (set-buffer gnus-summary-buffer)
804           (gnus-summary-reply-with-original 1)
805           (set-buffer gnus-message-buffer)
806           (message-goto-body)
807           (insert (format gnus-crosspost-complaint newsgroups group))
808           (message-goto-subject)
809           (re-search-forward " *$")
810           (replace-match " (crosspost notification)" t t)
811           (gnus-deactivate-mark)
812           (when (gnus-y-or-n-p "Send this complaint? ")
813             (message-send-and-exit)))))))
814
815 (defun gnus-summary-mail-other-window ()
816   "Compose mail in other window."
817   (interactive)
818   (gnus-setup-message 'message
819     (message-mail)))
820
821 (defun gnus-mail-parse-comma-list ()
822   (let (accumulated
823         beg)
824     (skip-chars-forward " ")
825     (while (not (eobp))
826       (setq beg (point))
827       (skip-chars-forward "^,")
828       (while (zerop
829               (save-excursion
830                 (save-restriction
831                   (let ((i 0))
832                     (narrow-to-region beg (point))
833                     (goto-char beg)
834                     (logand (progn
835                               (while (search-forward "\"" nil t)
836                                 (incf i))
837                               (if (zerop i) 2 i))
838                             2)))))
839         (skip-chars-forward ",")
840         (skip-chars-forward "^,"))
841       (skip-chars-backward " ")
842       (push (buffer-substring beg (point))
843             accumulated)
844       (skip-chars-forward "^,")
845       (skip-chars-forward ", "))
846     accumulated))
847
848 (defun gnus-inews-add-to-address (group)
849   (let ((to-address (mail-fetch-field "to")))
850     (when (and to-address
851                (gnus-alive-p))
852       ;; This mail group doesn't have a `to-list', so we add one
853       ;; here.  Magic!
854       (when (gnus-y-or-n-p
855              (format "Do you want to add this as `to-list': %s " to-address))
856         (gnus-group-add-parameter group (cons 'to-list to-address))))))
857
858 (defun gnus-put-message ()
859   "Put the current message in some group and return to Gnus."
860   (interactive)
861   (let ((reply gnus-article-reply)
862         (winconf gnus-prev-winconf)
863         (group gnus-newsgroup-name))
864
865     (or (and group (not (gnus-group-read-only-p group)))
866         (setq group (read-string "Put in group: " nil
867                                  (gnus-writable-groups))))
868     (when (gnus-gethash group gnus-newsrc-hashtb)
869       (error "No such group: %s" group))
870
871     (save-excursion
872       (save-restriction
873         (widen)
874         (message-narrow-to-headers)
875         (let (gnus-deletable-headers)
876           (if (message-news-p)
877               (message-generate-headers message-required-news-headers)
878             (message-generate-headers message-required-mail-headers)))
879         (goto-char (point-max))
880         (insert "Gcc: " group "\n")
881         (widen)))
882
883     (gnus-inews-do-gcc)
884
885     (when (get-buffer gnus-group-buffer)
886       (when (gnus-buffer-exists-p (car-safe reply))
887         (set-buffer (car reply))
888         (and (cdr reply)
889              (gnus-summary-mark-article-as-replied
890               (cdr reply))))
891       (when winconf
892         (set-window-configuration winconf)))))
893
894 (defun gnus-article-mail (yank)
895   "Send a reply to the address near point.
896 If YANK is non-nil, include the original article."
897   (interactive "P")
898   (let ((address
899          (buffer-substring
900           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
901           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
902     (when address
903       (message-reply address)
904       (when yank
905         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
906
907 (defvar nntp-server-type)
908 (defun gnus-bug ()
909   "Send a bug report to the Gnus maintainers."
910   (interactive)
911   (unless (gnus-alive-p)
912     (error "Gnus has been shut down"))
913   (gnus-setup-message 'bug
914     (delete-other-windows)
915     (when gnus-bug-create-help-buffer
916       (switch-to-buffer "*Gnus Help Bug*")
917       (erase-buffer)
918       (insert gnus-bug-message)
919       (goto-char (point-min)))
920     (message-pop-to-buffer "*Gnus Bug*")
921     (message-setup `((To . ,gnus-maintainer) (Subject . "")))
922     (when gnus-bug-create-help-buffer
923       (push `(gnus-bug-kill-buffer) message-send-actions))
924     (goto-char (point-min))
925     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
926     (forward-line 1)
927     (insert (gnus-version) "\n"
928             (emacs-version) "\n")
929     (when (and (boundp 'nntp-server-type)
930                (stringp nntp-server-type))
931       (insert nntp-server-type))
932     (insert "\n\n\n\n\n")