*** empty log message ***
[gnus] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'gnus)
30 (require 'sendmail)
31 (require 'gnus-ems)
32 (eval-when-compile (require 'cl))
33
34 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
35 (defvar gnus-post-method nil
36   "*Preferred method for posting USENET news.
37 If this variable is nil, Gnus will use the current method to decide
38 which method to use when posting.  If it is non-nil, it will override
39 the current method.  This method will not be used in mail groups and
40 the like, only in \"real\" newsgroups.
41
42 The value must be a valid method as discussed in the documentation of
43 `gnus-select-method'.  It can also be a list of methods.  If that is
44 the case, the user will be queried for what select method to use when
45 posting.")
46
47 (defvar gnus-organization-file "/usr/lib/news/organization"
48   "*Local news organization file.")
49
50 (defvar gnus-prepare-article-hook nil
51   "*A hook called after preparing body, but before preparing header headers.")
52
53 (defvar gnus-post-prepare-function nil
54   "*Function that is run after a post buffer has been prepared.
55 It is called with the name of the newsgroup that is posted to. It
56 might be used, for instance, for inserting signatures based on the
57 newsgroup name. (In that case, `gnus-signature-file' and
58 `mail-signature' should both be set to nil).")
59
60 (defvar gnus-post-prepare-hook nil
61   "*Hook that is run after a post buffer has been prepared.
62 If you want to insert the signature, you might put
63 `gnus-inews-insert-signature' in this hook.")
64
65 (defvar gnus-use-followup-to 'ask
66   "*Specifies what to do with Followup-To header.
67 If nil, ignore the header. If it is t, use its value, but ignore
68 \"poster\".  If it is the symbol `ask', query the user whether to
69 ignore the \"poster\" value.  If it is the symbol `use', always use
70 the value.")
71
72 (defvar gnus-followup-to-function nil
73   "*A variable that contains a function that returns a followup address.
74 The function will be called in the buffer of the article that is being
75 followed up. The buffer will be narrowed to the headers of the
76 article. To pick header headers, one might use `mail-fetch-field'.  The
77 function will be called with the name of the current newsgroup as the
78 argument.
79
80 Here's an example `gnus-followup-to-function':
81
82 (setq gnus-followup-to-function
83       (lambda (group)
84         (cond ((string= group \"mail.list\")
85                (or (mail-fetch-field \"sender\") 
86                    (mail-fetch-field \"from\")))
87               (t
88                (or (mail-fetch-field \"reply-to\") 
89                    (mail-fetch-field \"from\"))))))")
90
91 (defvar gnus-reply-to-function nil
92   "*A variable that contains a function that returns a reply address.
93 See the `gnus-followup-to-function' variable for an explanation of how
94 this variable is used.
95
96 This function should return a string that will be used to fill in the
97 header.  This function may also return a list.  In that case, every
98 list element should be a cons where the first car should be a string
99 with the header name, and the cdr should be a string with the header
100 value.")
101
102 (defvar gnus-author-copy (getenv "AUTHORCOPY")
103   "*Save outgoing articles in this file.
104 Initialized from the AUTHORCOPY environment variable.
105
106 If this variable begins with the character \"|\", outgoing articles
107 will be piped to the named program. It is possible to save an article
108 in an MH folder as follows:
109
110 \(setq gnus-author-copy \"|/usr/local/lib/mh/rcvstore +Article\")
111
112 If the first character is not a pipe, articles are saved using the
113 function specified by the `gnus-author-copy-saver' variable.")
114
115 (defvar gnus-mail-self-blind nil
116   "*Non-nil means insert a BCC header in all outgoing articles.
117 This will result in having a copy of the article mailed to yourself.
118 The BCC header is inserted when the post buffer is initialized, so you
119 can remove or alter the BCC header to override the default.")
120
121 (defvar gnus-author-copy-saver (function rmail-output)
122   "*A function called to save outgoing articles.
123 This function will be called with the same of the file to store the
124 article in. The default function is `rmail-output' which saves in Unix
125 mailbox format.")
126
127 (defvar gnus-outgoing-message-group nil
128   "*All outgoing messages will be put in this group.
129 If you want to store all your outgoing mail and articles in the group
130 \"nnml:archive\", you set this variable to that value. This variable
131 can also be a list of group names. 
132
133 If you want to have greater control over what group to put each
134 message in, you can set this variable to a function that checks the
135 current newsgroup name and then returns a suitable group name (or list
136 of names).")
137
138 (defvar gnus-message-archive-group
139   '((if (eq major-mode 'news-reply-mode) "misc-news" "misc-mail"))
140   "*Name of the group in which to save the messages you've written.
141 This can either be a string, a list of strings; or an alist
142 of regexps/functions/forms to be evaluated to return a string (or a list
143 of strings).  The functions are called with the name of the current
144 group (or nil) as a parameter.")
145
146 (defvar gnus-mailing-list-groups nil
147   "*Regexp matching groups that are really mailing lists.
148 This is useful when you're reading a mailing list that has been
149 gatewayed to a newsgroup, and you want to followup to an article in
150 the group.")
151
152 (defvar gnus-draft-group-directory 
153   (expand-file-name
154    (concat (file-name-as-directory gnus-article-save-directory)
155            "drafts"))
156   "*The directory where draft messages will be stored.")
157
158 (defvar gnus-use-draft t
159   "*Whether Gnus should use the draft group.")
160
161 (defvar gnus-posting-styles nil
162   "*Alist of styles to use when posting.")
163
164 (defvar gnus-posting-style-alist
165   '((organization . gnus-organization-file)
166     (signature . gnus-signature-file)
167     (from . gnus-user-from-line)))
168
169 (defvar gnus-user-login-name nil
170   "*The login name of the user.
171 Got from the function `user-login-name' if undefined.")
172
173 (defvar gnus-user-full-name nil
174   "*The full name of the user.
175 Got from the NAME environment variable if undefined.")
176
177 (defvar gnus-user-from-line nil
178   "*Your full, complete e-mail address.  
179 Overrides the other Gnus variables if it is non-nil.
180
181 Here are two example values of this variable:
182
183  \"Lars Magne Ingebrigtsen <larsi@ifi.uio.no>\"
184
185 and
186
187  \"larsi@ifi.uio.no (Lars Magne Ingebrigtsen)\"
188
189 The first version is recommended, but the name has to be quoted if it
190 contains non-alphanumerical characters.")
191
192 (defvar gnus-signature-file "~/.signature"
193   "*Your signature file.
194 If the variable is a string that doesn't correspond to a file, the
195 string itself is inserted.")
196
197 (defvar gnus-signature-function nil
198   "*A function that should return a signature file name.
199 The function will be called with the name of the newsgroup being
200 posted to.
201 If the function returns a string that doesn't correspond to a file, the
202 string itself is inserted.
203 If the function returns nil, the `gnus-signature-file' variable will
204 be used instead.")
205
206 (defvar gnus-forward-start-separator 
207   "------- Start of forwarded message -------\n"
208   "*Delimiter inserted before forwarded messages.")
209
210 (defvar gnus-forward-end-separator
211   "------- End of forwarded message -------\n"
212   "*Delimiter inserted after forwarded messages.")
213
214 (defvar gnus-signature-before-forwarded-message t
215   "*If non-nil, put the signature before any included forwarded message.")
216
217 (defvar gnus-forward-included-headers 
218   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-\\:^Message-ID:\\|^References:"
219   "*Regexp matching headers to be included in forwarded messages.")
220
221 (defvar gnus-required-headers
222   '(From Date Newsgroups Subject Message-ID Organization Lines X-Newsreader)
223   "*Headers to be generated or prompted for when posting an article.
224 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
225 Message-ID.  Organization, Lines, In-Reply-To, Expires, and
226 X-Newsreader are optional.  If you want Gnus not to insert some
227 header, remove it from this list.")
228
229 (defvar gnus-required-mail-headers 
230   '(From Date To Subject (optional . In-Reply-To) Message-ID Organization Lines)
231   "*Headers to be generated or prompted for when mailing a message.
232 RFC822 required that From, Date, To, Subject and Message-ID be
233 included.  Organization, Lines and X-Mailer are optional.")
234
235 (defvar gnus-deletable-headers '(Message-ID Date)
236   "*Headers to be deleted if they already exists and were generated by Gnus previously.")
237
238 (defvar gnus-removable-headers '(NNTP-Posting-Host Xref)
239   "*Headers to be removed unconditionally before posting.")
240
241 (defvar gnus-article-expires 14
242   "*Number of days before your article expires.
243 This variable isn't used unless you have the `Expires' element in
244 `gnus-required-headers'.")
245
246 (defvar gnus-distribution-function nil
247   "*Function that should return the Distribution header for outgoing articles.
248 It will be called from the buffer where the outgoing article
249 is being prepared with the group name as the only parameter.
250 It should return a valid distribution.  
251
252 The function will only be called if you have the `Distribution' header in 
253 `gnus-required-headers'.")
254
255 (defvar gnus-check-before-posting 
256   '(subject-cmsg multiple-headers sendsys message-id from
257                  long-lines control-chars size new-text
258                  redirected-followup signature approved sender empty)
259   "In non-nil, Gnus will attempt to run some checks on outgoing posts.
260 If this variable is t, Gnus will check everything it can.  If it is a
261 list, then those elements in that list will be checked.")
262
263 (defvar gnus-delete-supersedes-headers
264   "^Path:\\|^Date\\|^NNTP-Posting-Host:\\|^Supersedes:\\|^Xref:\\|^Lines:"
265   "*Header lines matching this regexp will be deleted before posting.
266 It's best to delete old Path and Date headers before posting to avoid
267 any confusion.")
268
269 (defvar gnus-auto-mail-to-author nil
270   "*If non-nil, mail the authors of articles a copy of your follow-ups.
271 If this variable is `ask', the user will be prompted for whether to
272 mail a copy.  The string given by `gnus-mail-courtesy-message' will be
273 inserted at the beginning of the mail copy.")
274
275 ;; Added by Ethan Bradford <ethanb@ptolemy.astro.washington.edu>.
276 (defvar gnus-mail-courtesy-message
277   "The following message is a courtesy copy of an article\nthat has been posted as well.\n\n"
278   "*This is inserted at the start of a mailed copy of a posted message.
279 If this variable is nil, no such courtesy message will be added.")
280
281 (defvar gnus-mail-method 'sendmail
282   "*Method to use for composing mail.
283 There are three legal values: `sendmail' (which is the default), `mh', 
284 and `vm'.")
285
286 (defvar gnus-mail-reply-method (function gnus-mail-reply-using-mail)
287   "*Function to compose a reply.
288 Three pre-made functions are `gnus-mail-reply-using-mail' (sendmail);
289 `gnus-mail-reply-using-mhe' (MH-E); and `gnus-mail-reply-using-vm'.")
290
291 (defvar gnus-mail-forward-method (function gnus-mail-forward-using-mail)
292   "*Function to forward the current message to another user.
293 Three pre-made functions are `gnus-mail-forward-using-mail' (sendmail);
294 `gnus-mail-forward-using-mhe' (MH-E); and `gnus-mail-forward-using-vm'.") 
295
296 (defvar gnus-mail-other-window-method 'gnus-mail-other-window-using-mail
297   "*Function to compose mail in the other window.
298 Three pre-made functions are `gnus-mail-other-window-using-mail'
299 (sendmail); `gnus-mail-other-window-using-mhe' (MH-E); and
300 `gnus-mail-other-window-using-vm'.")
301
302 (defvar gnus-inews-article-function 'gnus-inews-article
303   "*Function to post an article.")
304
305 (defvar gnus-bounced-headers-junk "^\\(Received\\):"
306   "*Regexp that matches headers to be removed in resent bounced mail.")
307
308 (defvar gnus-inews-article-hook (list 'gnus-inews-do-fcc)
309   "*A hook called before finally posting an article.
310 The default hook (`gnus-inews-do-fcc') does FCC processing (ie. saves
311 the article to a file).")
312
313 (defvar gnus-inews-article-header-hook nil
314   "*A hook called after inserting the headers in an article to be posted.
315 The hook is called from the *post-news* buffer, narrowed to the
316 headers.")
317
318 (defvar gnus-mail-hook nil
319   "*A hook called as the last thing after setting up a mail buffer.")
320
321 (defvar gnus-message-sent-hook nil
322   "*A hook run after an article has been sent (or attempted sent).")
323
324 ;;; Internal variables.
325
326 (defvar gnus-post-news-buffer "*post-news*")
327 (defvar gnus-mail-buffer "*Mail Gnus*")
328 (defvar gnus-article-copy nil)
329 (defvar gnus-reply-subject nil)
330 (defvar gnus-newsgroup-followup nil)
331 (defvar gnus-add-to-address nil)
332 (defvar gnus-in-reply-to nil)
333 (defvar gnus-last-posting-server nil)
334
335 (eval-and-compile
336   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
337   (autoload 'news-setup "rnewspost")
338   (autoload 'news-reply-mode "rnewspost")
339   (autoload 'rmail-dont-reply-to "mail-utils")
340   (autoload 'rmail-output "rmailout"))
341
342 \f
343 ;;;
344 ;;; Gnus Posting Functions
345 ;;;
346
347 (gnus-define-keys 
348  (gnus-summary-send-map "S" gnus-summary-mode-map)
349  "p" gnus-summary-post-news
350  "f" gnus-summary-followup
351  "F" gnus-summary-followup-with-original
352  "b" gnus-summary-followup-and-reply
353  "B" gnus-summary-followup-and-reply-with-original
354  "c" gnus-summary-cancel-article
355  "s" gnus-summary-supersede-article
356  "r" gnus-summary-reply
357  "R" gnus-summary-reply-with-original
358  "m" gnus-summary-mail-other-window
359  "u" gnus-uu-post-news
360  "om" gnus-summary-mail-forward
361  "op" gnus-summary-post-forward
362  "Om" gnus-uu-digest-mail-forward
363  "Op" gnus-uu-digest-post-forward)
364
365 (gnus-define-keys
366  (gnus-send-bounce-map "D" gnus-summary-send-map)
367  "b" gnus-summary-resend-bounced-mail
368  "c" gnus-summary-send-draft
369  "r" gnus-summary-resend-message)
370
371 ;;; Internal functions.
372
373 (defun gnus-number-base36 (num len)
374   (if (if (< len 0) (<= num 0) (= len 0))
375       ""
376     (concat (gnus-number-base36 (/ num 36) (1- len))
377             (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
378                                   (% num 36))))))
379
380 ;;; Post news commands of Gnus group mode and summary mode
381
382 (defun gnus-group-mail ()
383   "Start composing a mail."
384   (interactive)
385   (gnus-new-mail
386    ;; We might want to prompt here.
387    (when (and gnus-interactive-post
388               (not gnus-expert-user))
389      (read-string "To: "))))
390
391 (defun gnus-group-post-news (&optional arg)
392   "Post an article.
393 The newsgroup under the cursor is used as the group to post to.
394
395 If you wish to get an empty post buffer, use a prefix ARG.  You can
396 also do this by calling this function from the bottom of the Group
397 buffer."
398   (interactive "P")
399   (let ((gnus-newsgroup-name nil)
400         (group (unless arg (gnus-group-group-name)))
401         subject)
402     ;; We might want to prompt here.
403     (when (and gnus-interactive-post
404                (not gnus-expert-user))
405       (setq gnus-newsgroup-name
406             (setq group 
407                   (completing-read "Group: " gnus-active-hashtb nil nil
408                                    (cons (or group "") 0)))))
409     (gnus-post-news 'post group nil gnus-article-buffer)))
410
411 (defun gnus-summary-post-news ()
412   "Post an article."
413   (interactive)
414   (gnus-set-global-variables)
415   (gnus-post-news 'post gnus-newsgroup-name))
416
417 (defun gnus-summary-followup (yank &optional yank-articles)
418   "Compose a followup to an article.
419 If prefix argument YANK is non-nil, original article is yanked automatically."
420   (interactive "P")
421   (gnus-set-global-variables)
422   (if yank-articles (gnus-summary-goto-subject (car yank-articles)))
423   (save-window-excursion
424     (gnus-summary-select-article))
425   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
426         (gnus-newsgroup-name gnus-newsgroup-name))
427     ;; Check Followup-To: poster.
428     (set-buffer gnus-article-buffer)
429     (if (and gnus-use-followup-to
430              (string-equal "poster" (gnus-fetch-field "followup-to"))
431              (or (not (memq gnus-use-followup-to '(t ask)))
432                  (not (gnus-y-or-n-p 
433                        "Do you want to ignore `Followup-To: poster'? "))))
434         ;; Mail to the poster. 
435         (gnus-summary-reply yank)
436       ;; Send a followup.
437       (gnus-post-news nil gnus-newsgroup-name
438                       headers gnus-article-buffer 
439                       (or yank-articles (not (not yank)))))))
440
441 (defun gnus-summary-followup-with-original (n)
442   "Compose a followup to an article and include the original article."
443   (interactive "P")
444   (gnus-summary-followup t (gnus-summary-work-articles n)))
445
446 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
447 (defun gnus-summary-followup-and-reply (yank &optional yank-articles)
448   "Compose a followup and do an auto mail to author."
449   (interactive "P")
450   (gnus-set-global-variables)
451   (let ((gnus-auto-mail-to-author 'force))
452     (gnus-summary-followup yank yank-articles)))
453
454 (defun gnus-summary-followup-and-reply-with-original (n)
455   "Compose a followup, include the original, and do an auto mail to author."
456   (interactive "P")
457   (gnus-summary-followup-and-reply t (gnus-summary-work-articles n)))
458
459 (defun gnus-summary-cancel-article (n)
460   "Cancel an article you posted."
461   (interactive "P")
462   (gnus-set-global-variables)
463   (let ((articles (gnus-summary-work-articles n))
464         article)
465     (while (setq article (pop articles))
466       (when (gnus-summary-select-article t nil nil article)
467         (when (gnus-eval-in-buffer-window 
468                gnus-original-article-buffer (gnus-cancel-news))
469           (gnus-summary-mark-as-read article gnus-canceled-mark))
470         (gnus-article-hide-headers-if-wanted))
471       (gnus-summary-remove-process-mark article))))
472
473 (defun gnus-summary-supersede-article ()
474   "Compose an article that will supersede a previous article.
475 This is done simply by taking the old article and adding a Supersedes
476 header line with the old Message-ID."
477   (interactive)
478   (gnus-set-global-variables)
479   (gnus-summary-select-article t)
480   ;; Check whether the user owns the article that is to be superseded. 
481   (unless (string-equal
482            (downcase (mail-strip-quoted-names 
483                       (mail-header-from gnus-current-headers)))
484            (downcase (mail-strip-quoted-names (gnus-inews-user-name))))
485     (error "This article is not yours."))
486   ;; Get a normal *post-news* buffer.
487   (gnus-new-news gnus-newsgroup-name t)
488   (erase-buffer)
489   (insert-buffer-substring gnus-original-article-buffer)
490   (nnheader-narrow-to-headers)
491   ;; Remove unwanted headers.
492   (when gnus-delete-supersedes-headers
493     (nnheader-remove-header gnus-delete-supersedes-headers t))
494   (goto-char (point-min))
495   (if (not (re-search-forward "^Message-ID: " nil t))
496       (error "No Message-ID in this article")
497     (replace-match "Supersedes: " t t))
498   (goto-char (point-max))
499   (insert mail-header-separator)
500   (widen)
501   (forward-line 1))
502
503 \f
504 ;;;###autoload
505 (defalias 'sendnews 'gnus-post-news)
506
507 ;;;###autoload
508 (defalias 'postnews 'gnus-post-news)
509
510 (defun gnus-copy-article-buffer (&optional article-buffer)
511   ;; make a copy of the article buffer with all text properties removed
512   ;; this copy is in the buffer gnus-article-copy.
513   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
514   ;; this buffer should be passed to all mail/news reply/post routines.
515   (setq gnus-article-copy (get-buffer-create " *gnus article copy*"))
516   (buffer-disable-undo gnus-article-copy)
517   (or (memq gnus-article-copy gnus-buffer-list)
518       (setq gnus-buffer-list (cons gnus-article-copy gnus-buffer-list)))
519   (let ((article-buffer (or article-buffer gnus-article-buffer)))
520     (if (and (get-buffer article-buffer)
521              (buffer-name (get-buffer article-buffer)))
522         (save-excursion
523           (set-buffer article-buffer)
524           (widen)
525           (copy-to-buffer gnus-article-copy (point-min) (point-max))
526           (set-text-properties (point-min) (point-max) 
527                                nil gnus-article-copy)))))
528
529 (defun gnus-post-news (post &optional group header article-buffer yank subject)
530   "Begin editing a new USENET news article to be posted.
531 Type \\[describe-mode] in the buffer to get a list of commands."
532   (interactive (list t))
533   (let* ((group (or group gnus-newsgroup-name))
534          (pgroup group)
535          to-address to-group mailing-list to-list)
536     (when group
537       (setq to-address (gnus-group-get-parameter group 'to-address)
538             to-group (gnus-group-get-parameter group 'to-group)
539             to-list (gnus-group-get-parameter group 'to-list)
540             mailing-list (when gnus-mailing-list-groups
541                            (string-match gnus-mailing-list-groups group))
542             group (gnus-group-real-name group)))
543     (if (or (and to-group
544                  (gnus-news-group-p to-group))
545             (and (gnus-news-group-p 
546                   (or pgroup gnus-newsgroup-name)
547                   (if header (mail-header-number header) gnus-current-article))
548                  (not mailing-list)
549                  (not to-list)
550                  (not to-address)))
551         ;; This is news.
552         (if post
553             (gnus-new-news (or to-group group))
554           (gnus-news-followup yank (or to-group group)))
555       ;; The is mail.
556       (if post
557           (progn
558             (gnus-new-mail (or to-address to-list))
559             ;; Arrange for mail groups that have no `to-address' to
560             ;; get that when the user sends off the mail.
561             (unless to-address
562               (make-local-variable 'gnus-add-to-address)
563               (setq gnus-add-to-address group)))
564         (gnus-mail-reply yank to-address 'followup)))))
565
566 (defun gnus-post-method (group query-method &optional silent)
567   "Return the posting method based on GROUP and query-method.
568 If SILENT, don't prompt the user."
569   (let ((group-method (if (stringp group)
570                           (gnus-find-method-for-group group)
571                         group)))
572     (cond 
573      ;; If the group-method is nil (which shouldn't happen) we use 
574      ;; the default method.
575      ((null group-method)
576       (or gnus-post-method gnus-select-method))
577      ;; We want this group's method.
578      ((and query-method (not (eq query-method 0)))
579       group-method)
580      ;; We query the user for a post method.
581      ((or query-method
582           (and gnus-post-method
583                (listp (car gnus-post-method))))
584       (let* ((methods
585               ;; Collect all methods we know about.
586               (append
587                (when gnus-post-method
588                  (if (listp (car gnus-post-method))
589                      gnus-post-method
590                    (listp gnus-post-method)))
591                gnus-secondary-select-methods
592                (list gnus-select-method)
593                (list group-method)))
594              method-alist post-methods method)
595         ;; Weed out all mail methods.
596         (while methods
597           (setq method (gnus-server-get-method "" (pop methods)))
598           (when (or (gnus-method-option-p method 'post)
599                     (gnus-method-option-p method 'post-mail))
600             (push method post-methods)))
601         ;; Create a name-method alist.
602         (setq method-alist
603               (mapcar 
604                (lambda (m)
605                  (list (concat (cadr m) " (" (symbol-name (car m)) ")") m))
606                post-methods))
607         ;; Query the user.
608         (cadr
609          (assoc
610           (setq gnus-last-posting-server
611                 (if (and silent
612                          gnus-last-posting-server)
613                     ;; Just use the last value.
614                     gnus-last-posting-server
615                   (completing-read
616                    "Posting method: " method-alist nil t
617                    (cons (or gnus-last-posting-server "") 0))))
618           method-alist))))
619      ;; Override normal method.
620      ((and gnus-post-method
621            (or (gnus-method-option-p group-method 'post)
622                (gnus-method-option-p group-method 'post-mail)))
623       gnus-post-method)
624      ;; Perhaps this is a mail group?
625      ((and (not (gnus-member-of-valid 'post group))
626            (not (gnus-method-option-p group-method 'post-mail)))
627       group-method)
628      ;; Use the normal select method.
629      (t gnus-select-method))))
630
631 (defun gnus-news-group-p (group &optional article)
632   "Return non-nil if GROUP (and ARTICLE) come from a news server."
633   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
634       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
635            (eq (gnus-request-type group article) 'news))))
636            
637 (defun gnus-inews-news (&optional use-group-method)
638   "Send a news message.
639
640 If given a non-zero prefix and the group is a foreign group, this
641 function will attempt to use the foreign server to post the article.
642
643 If given an zero prefix, the user will be prompted for a posting
644 method to use."
645   (interactive "P")
646   (or gnus-current-select-method
647       (setq gnus-current-select-method gnus-select-method))
648   (let* ((case-fold-search nil)
649          (reply gnus-article-reply)
650          error post-result)
651     (save-excursion
652       (gnus-start-news-server)          ;Use default server.
653       (widen)
654       (goto-char (point-min))
655       (run-hooks 'news-inews-hook)
656
657       ;; Send to server. 
658       (gnus-message 5 "Posting to USENET...")
659       (setq post-result (funcall gnus-inews-article-function use-group-method))
660       (cond 
661        ((eq post-result 'illegal)
662         (setq error t)
663         (ding))
664        (post-result
665         (gnus-message 5 "Posting to USENET...done")
666         (set-buffer-modified-p nil)
667         ;; We mark the article as replied.
668         (when (gnus-buffer-exists-p (car-safe reply))
669           (save-excursion
670             (set-buffer gnus-summary-buffer)
671             (gnus-summary-mark-article-as-replied (cdr reply)))))
672        (t
673         ;; We cannot signal an error.
674         (setq error t)
675         (ding)
676         (gnus-message 
677          1 "Article rejected: %s" 
678          (gnus-status-message
679           (gnus-post-method gnus-newsgroup-name use-group-method t))))))
680
681     (let ((conf gnus-prev-winconf))
682       (unless error
683         (bury-buffer)
684         ;; Restore last window configuration.
685         (and conf (set-window-configuration conf))))))
686
687 (defun gnus-inews-narrow-to-headers ()
688   (widen)
689   (narrow-to-region
690    (goto-char (point-min))
691    (or (and (re-search-forward 
692              (concat "^" (regexp-quote mail-header-separator) "$") nil t)
693             (match-beginning 0))
694        (point-max)))
695   (goto-char (point-min)))
696
697 (defun gnus-inews-send-mail-copy ()
698   ;; Mail the message if To, Bcc or Cc exists.
699   (let* ((types '("to" "bcc" "cc"))
700          (ty types)
701          (buffer (current-buffer))
702          fcc)
703     (save-restriction
704       (widen)
705       (gnus-inews-narrow-to-headers)
706
707       (while ty
708         (or (mail-fetch-field (car ty) nil t)
709             (setq types (delete (car ty) types)))
710         (setq ty (cdr ty)))
711
712       (if (not types)
713           ;; We do not want to send mail.
714           ()
715         (gnus-message 5 "Sending via mail...")
716         (widen)
717         (save-excursion
718           ;; We copy the article over to a temp buffer since we are
719           ;; going to modify it a little.  
720           (nnheader-set-temp-buffer " *Gnus-mailing*")
721           (insert-buffer-substring buffer)
722           ;; We remove Fcc, because we don't want the mailer to see
723           ;; that header.  
724           (gnus-inews-narrow-to-headers)
725           (nnheader-remove-header "fcc")
726
727           ;; Insert the X-Courtesy-Message header.
728           (and (or (member "to" types)
729                    (member "cc" types))
730                (progn
731                 (goto-char (point-max))
732                 (insert "Posted-To: " 
733                         (mail-fetch-field "newsgroups") "\n")))
734           
735           (widen)
736           
737           (if (and gnus-mail-courtesy-message
738                    (or (member "to" types)
739                        (member "cc" types)))
740               ;; We only want to insert the courtesy mail message if
741               ;; we use To or Cc; Bcc should not have one. Well, if
742               ;; both Bcc and To are present, it will get one
743               ;; anyway.
744               (progn
745                 ;; Insert "courtesy" mail message.
746                 (goto-char (point-min))
747                 (re-search-forward
748                  (concat "^" (regexp-quote mail-header-separator) "$"))
749                 (forward-line 1)
750                 (insert gnus-mail-courtesy-message)))
751
752           (gnus-mail-send t)
753           (kill-buffer (current-buffer))
754           (gnus-message 5 "Sending via mail...done"))))))
755
756 (defun gnus-inews-remove-headers-after-mail ()
757   (save-excursion
758     (save-restriction
759       (let ((case-fold-search t))
760         (gnus-inews-narrow-to-headers)
761         ;; Remove Bcc completely.
762         (nnheader-remove-header "bcc")
763         ;; We transform To and Cc headers to avoid re-mailing if the user
764         ;; accidentally (or purposefully) leans on the `C-c C-c' keys
765         ;; and the news server rejects the posting.
766         (while (re-search-forward "^\\(to\\|[bcf]cc\\|cc\\):" nil t)
767           (beginning-of-line)
768           (insert "X-"))
769         (widen)))))
770
771 (defun gnus-inews-dex-headers ()
772   "Remove \"X-\" prefixes from To and Cc headers."
773   (save-excursion
774     (save-restriction
775       (let ((case-fold-search t))
776         (nnheader-narrow-to-headers)
777         (while (re-search-forward "^X-\\(to\\|[bcf]cc\\|cc\\):" nil t)
778           (beginning-of-line)
779           (delete-char 2))
780         (widen)))))
781
782 (defun gnus-inews-remove-empty-headers ()
783   "Remove empty headers from news and mail.
784 The buffer should be narrowed to the headers before this function is
785 called."
786   (save-excursion
787     (goto-char (point-min))
788     (while (re-search-forward "^[^ \t:]+:\\([ \t]*\n\\)+[^ \t]" nil t)
789       (delete-region (match-beginning 0) (1- (match-end 0)))
790       (beginning-of-line))))
791
792 (defun gnus-inews-check-post ()
793   "Check whether the post looks ok."
794   (or
795    (not gnus-check-before-posting)
796    (and 
797     ;; We narrow to the headers and check them first.
798     (save-excursion
799       (save-restriction
800         (goto-char (point-min))
801         (narrow-to-region 
802          (point) 
803          (progn
804            (re-search-forward 
805             (concat "^" (regexp-quote mail-header-separator) "$"))
806            (match-beginning 0)))
807         (goto-char (point-min))
808         (and 
809          ;; Check for commands in Subject.
810          (or 
811           (gnus-check-before-posting 'subject-cmsg)
812           (save-excursion
813             (if (string-match "^cmsg " (mail-fetch-field "subject"))
814                 (gnus-y-or-n-p
815                  "The control code \"cmsg \" is in the subject. Really post? ")
816               t)))
817          ;; Check for multiple identical headers.
818          (or (gnus-check-before-posting 'multiple-headers)
819              (save-excursion
820                (let (found)
821                  (while (and (not found) (re-search-forward "^[^ \t:]+: "
822                                                             nil t))
823                    (save-excursion
824                      (or (re-search-forward 
825                           (concat "^" (setq found
826                                             (buffer-substring 
827                                              (match-beginning 0) 
828                                              (- (match-end 0) 2))))
829                           nil t)
830                          (setq found nil))))
831                  (if found
832                      (gnus-y-or-n-p 
833                       (format "Multiple %s headers. Really post? " found))
834                    t))))
835          ;; Check for Version and Sendsys.
836          (or (gnus-check-before-posting 'sendsys)
837              (save-excursion
838                (if (re-search-forward "^Sendsys:\\|^Version:" nil t)
839                    (gnus-y-or-n-p
840                     (format "The article contains a %s command. Really post? "
841                             (buffer-substring (match-beginning 0) 
842                                               (1- (match-end 0)))))
843                  t)))
844          ;; Check for Approved.
845          (or (gnus-check-before-posting 'approved)
846              (save-excursion
847                (if (re-search-forward "^Approved:" nil t)
848                    (gnus-y-or-n-p
849                     "The article contains an Approved header. Really post? ")
850                  t)))
851          ;; Check whether a Followup-To has redirected the newsgroup.
852          (or
853           (gnus-check-before-posting 'redirected-followup)
854           (not gnus-newsgroup-followup)
855           (save-excursion
856             (let ((followups (gnus-tokenize-header
857                               (mail-fetch-field "Newsgroups")))
858                   (newsgroups (gnus-tokenize-header
859                                (car gnus-newsgroup-followup)))
860                   shared)
861               (while (and followups
862                           (not (member followups newsgroups)))
863                 (setq followups (cdr followups)))
864               (if followups
865                   t
866                 (gnus-y-or-n-p
867                  "Followup redirected from original newsgroups.  Really post? "
868                  )))))
869          ;; Check the Message-ID header.
870          (or (gnus-check-before-posting 'message-id)
871              (save-excursion
872                (let* ((case-fold-search t)
873                       (message-id (mail-fetch-field "message-id")))
874                  (or (not message-id)
875                      (and (string-match "@" message-id)
876                           (string-match "@[^\\.]*\\." message-id))
877                      (gnus-y-or-n-p
878                       (format 
879                        "The Message-ID looks strange: \"%s\". Really post? "
880                        message-id))))))
881          ;; Check the From header.
882          (or 
883           (gnus-check-before-posting 'from)
884           (save-excursion
885             (let* ((case-fold-search t)
886                    (from (mail-fetch-field "from")))
887               (cond
888                ((not from)
889                 (gnus-y-or-n-p "There is no From line. Really post? "))
890                ((not (string-match "@[^\\.]*\\." from))
891                 (gnus-y-or-n-p
892                  (format 
893                   "The address looks strange: \"%s\". Really post? " from)))
894                ((string-match "(.*).*(.*)" from)
895                 (gnus-y-or-n-p
896                  (format
897                   "The From header looks strange: \"%s\". Really post? " 
898                   from)))
899                ((string-match "<[^>]+> *$" from)
900                 (let ((name (substring from 0 (match-beginning 0))))
901                   (or 
902                    (string-match "^ *\"[^\"]*\" *$" name)
903                    (not (string-match "[][.!()<>@,;:\\]" name))
904                    (gnus-y-or-n-p
905                     (format
906                      "The From header name has bogus characters.  Really post? " 
907                      from)))))
908                (t t)))))
909          )))
910     ;; Check for long lines.
911     (or (gnus-check-before-posting 'long-lines)
912         (save-excursion
913           (goto-char (point-min))
914           (re-search-forward
915            (concat "^" (regexp-quote mail-header-separator) "$"))
916           (while (and
917                   (progn
918                     (end-of-line)
919                     (< (current-column) 80))
920                   (zerop (forward-line 1))))
921           (or (bolp)
922               (eobp)
923               (gnus-y-or-n-p
924                "You have lines longer than 79 characters.  Really post? "))))
925     ;; Check whether the article is empty.
926     (or (gnus-check-before-posting 'empty)
927         (save-excursion
928           (goto-char (point-min))
929           (re-search-forward
930            (concat "^" (regexp-quote mail-header-separator) "$"))
931           (forward-line 1)
932           (or (re-search-forward "[^ \n\t]" nil t)
933               (gnus-y-or-n-p "Empty article.  Really post?"))))
934     ;; Check for control characters.
935     (or (gnus-check-before-posting 'control-chars)
936         (save-excursion
937           (if (re-search-forward "[\000-\007\013\015-\037\200-\237]" nil t)
938               (gnus-y-or-n-p 
939                "The article contains control characters. Really post? ")
940             t)))
941     ;; Check excessive size.
942     (or (gnus-check-before-posting 'size)
943         (if (> (buffer-size) 60000)
944             (gnus-y-or-n-p
945              (format "The article is %d octets long. Really post? "
946                      (buffer-size)))
947           t))
948     ;; Use the (size . checksum) variable to see whether the
949     ;; article is empty or has only quoted text.
950     (or
951      (gnus-check-before-posting 'new-text)
952      (if (and (= (buffer-size) (car gnus-article-check-size))
953               (= (gnus-article-checksum) (cdr gnus-article-check-size)))
954          (gnus-y-or-n-p
955           "It looks like there's no new text in your article. Really post? ")
956        t))
957     ;; Check the length of the signature.
958     (or (gnus-check-before-posting 'signature)
959         (progn
960           (goto-char (point-max))
961           (if (not (re-search-backward gnus-signature-separator nil t))
962               t
963             (if (> (count-lines (point) (point-max)) 5)
964                 (gnus-y-or-n-p
965                  (format
966                   "Your .sig is %d lines; it should be max 4.  Really post? "
967                   (count-lines (point) (point-max))))
968               t)))))))
969
970 (defun gnus-tokenize-header (header &optional separator)
971   "Split HEADER into a list of header elements.
972 \",\" is used as the separator."
973   (let* ((beg 0)
974          (separator (or separator ","))
975          (regexp
976           (format "[ \t]*\\([^%s]+\\)?\\(%s\\|\\'\\)" separator separator))
977          elems)
978     (while (and (string-match regexp header beg)
979                 (< beg (length header)))
980       (when (match-beginning 1)
981         (push (match-string 1 header) elems))
982       (setq beg (match-end 0)))
983     (nreverse elems)))
984
985 (defun gnus-article-checksum ()
986   (let ((sum 0))
987     (save-excursion
988       (while (not (eobp))
989         (setq sum (logxor sum (following-char)))
990         (forward-char 1)))
991     sum))
992
993 ;; Returns non-nil if this type is not to be checked.
994 (defun gnus-check-before-posting (type)
995   (not 
996    (or (not gnus-check-before-posting)
997        (if (listp gnus-check-before-posting)
998            (memq type gnus-check-before-posting)
999          t))))
1000
1001 (defun gnus-cancel-news ()
1002   "Cancel an article you posted."
1003   (interactive)
1004   (if (or gnus-expert-user
1005           (gnus-yes-or-no-p "Do you really want to cancel this article? "))
1006       (let ((from nil)
1007             (newsgroups nil)
1008             (message-id nil)
1009             (distribution nil))
1010         (or (gnus-news-group-p gnus-newsgroup-name)
1011             (error "This backend does not support canceling"))
1012         (save-excursion
1013           ;; Get header info. from original article.
1014           (save-restriction
1015             (gnus-article-show-all-headers)
1016             (goto-char (point-min))
1017             (search-forward "\n\n" nil 'move)
1018             (narrow-to-region (point-min) (point))
1019             (setq from (mail-fetch-field "from"))
1020             (setq newsgroups (mail-fetch-field "newsgroups"))
1021             (setq message-id (mail-fetch-field "message-id"))
1022             (setq distribution (mail-fetch-field "distribution")))
1023           ;; Verify if the article is absolutely user's by comparing
1024           ;; user id with value of its From: field.
1025           (if (not
1026                (string-equal
1027                 (downcase (mail-strip-quoted-names from))
1028                 (downcase (mail-strip-quoted-names (gnus-inews-user-name)))))
1029               (progn
1030                 (ding) (gnus-message 3 "This article is not yours.")
1031                 nil)
1032             ;; Make control article.
1033             (set-buffer (get-buffer-create " *Gnus-canceling*"))
1034             (buffer-disable-undo (current-buffer))
1035             (erase-buffer)
1036             (insert "Newsgroups: " newsgroups "\n"
1037                     "From: " (gnus-inews-user-name) "\n"
1038                     "Subject: cancel " message-id "\n"
1039                     "Control: cancel " message-id "\n"
1040                     (if distribution
1041                         (concat "Distribution: " distribution "\n")
1042                       "")
1043                     mail-header-separator "\n"
1044                     "This is a cancel message from " from ".\n")
1045             ;; Send the control article to NNTP server.
1046             (gnus-message 5 "Canceling your article...")
1047             (prog1
1048                 (if (funcall gnus-inews-article-function)
1049                     (gnus-message 5 "Canceling your article...done")
1050                   (progn
1051                     (ding) 
1052                     (gnus-message 1 "Cancel failed; %s" 
1053                                   (gnus-status-message gnus-newsgroup-name))
1054                     nil)
1055                   t)
1056               ;; Kill the article buffer.
1057               (kill-buffer (current-buffer))))))))
1058
1059 \f
1060 ;;; Lowlevel inews interface.
1061
1062 ;; Dummy to avoid byte-compile warning.
1063 (defvar nnspool-rejected-article-hook)
1064
1065 (defun gnus-inews-article (&optional use-group-method)
1066   "Post an article in current buffer using NNTP protocol."
1067   (let ((artbuf (current-buffer))
1068         gcc result)
1069     (widen)
1070     (goto-char (point-max))
1071     ;; Require a newline at the end of the buffer since inews may
1072     ;; append a .signature.
1073     (or (= (preceding-char) ?\n)
1074         (insert ?\n))
1075     ;; Prepare article headers.  All message body such as signature
1076     ;; must be inserted before Lines: field is prepared.
1077     (save-restriction
1078       (gnus-inews-narrow-to-headers)
1079       ;; Fix some headers.
1080       (gnus-inews-cleanup-headers)
1081       ;; Remove some headers.
1082       (gnus-inews-remove-headers)
1083       ;; Insert some headers.
1084       (gnus-inews-insert-headers)
1085       ;; Let the user do all of the above.
1086       (run-hooks 'gnus-inews-article-header-hook)
1087       ;; Copy the Gcc header, if any.
1088       (setq gcc (mail-fetch-field "gcc"))
1089       (widen))
1090     ;; Check whether the article is a good Net Citizen.
1091     (if (and gnus-article-check-size
1092              (not (gnus-inews-check-post)))
1093         ;; Aber nein!
1094         'illegal
1095       ;; We fudge a hook for nnspool.
1096       (setq nnspool-rejected-article-hook
1097             (`
1098              (list
1099               (lambda ()
1100                 (condition-case ()
1101                     (save-excursion
1102                       (set-buffer (, (buffer-name)))
1103                       (gnus-associate-buffer-with-draft nil 'silent))
1104                   (error 
1105                    (ding)
1106                    (gnus-message 
1107                     1 "Couldn't enter rejected article into draft group")))))))
1108                                    
1109       ;; Looks ok, so we do the nasty.
1110       (save-excursion
1111         ;; This hook may insert a signature.
1112         (save-excursion
1113           (goto-char (point-min))
1114           (let ((gnus-newsgroup-name (or (mail-fetch-field "newsgroups")
1115                                          gnus-newsgroup-name)))
1116             (run-hooks 'gnus-prepare-article-hook)))
1117         ;; Send off copies using mail, if that is wanted.
1118         (gnus-inews-send-mail-copy)
1119         ;; Remove more headers.
1120         (gnus-inews-remove-headers-after-mail)
1121         ;; Copy the article over to a temp buffer.
1122         (nnheader-set-temp-buffer " *Gnus-posting*")
1123         (insert-buffer-substring artbuf)
1124         ;; Remove the header separator.
1125         (goto-char (point-min))
1126         (re-search-forward
1127          (concat "^" (regexp-quote mail-header-separator) "$"))
1128         (replace-match "" t t)
1129         ;; Remove X- prefixes to headers.
1130         (gnus-inews-dex-headers)
1131         ;; Run final inews hooks.  This hook may do FCC.
1132         ;; The article must be saved before being posted because
1133         ;; `gnus-request-post' modifies the buffer.
1134         (run-hooks 'gnus-inews-article-hook)
1135         ;; Copy the article over to some group, possibly.
1136         (and gcc (gnus-inews-do-gcc gcc))
1137         ;; Post the article.
1138         (setq result (gnus-request-post
1139                       (gnus-post-method gnus-newsgroup-name use-group-method)))
1140         (kill-buffer (current-buffer)))
1141       (run-hooks 'gnus-message-sent-hook)
1142       ;; If the posting was unsuccessful (that it, it was rejected) we
1143       ;; put it into the draft group.
1144       (or result (gnus-associate-buffer-with-draft))
1145       result)))
1146
1147 (defun gnus-inews-cleanup-headers ()
1148   ;; Correct newsgroups field: change sequence of spaces to comma and 
1149   ;; eliminate spaces around commas.  Eliminate imbedded line breaks.
1150   (goto-char (point-min))
1151   (if (re-search-forward "^Newsgroups: +" nil t)
1152       (save-restriction
1153         (narrow-to-region
1154          (point)
1155          (if (re-search-forward "^[^ \t]" nil t)
1156              (match-beginning 0)
1157            (forward-line 1)
1158            (point)))
1159         (goto-char (point-min))
1160         (while (re-search-forward "\n[ \t]+" nil t)
1161           (replace-match " " t t))      ;No line breaks (too confusing)
1162         (goto-char (point-min))
1163         (while (re-search-forward "[ \t\n]*,[ \t\n]*\\|[ \t]+" nil t)
1164           (replace-match "," t t))
1165         (goto-char (point-min))
1166         ;; Remove a trailing comma.
1167         (if (re-search-forward ",$" nil t)
1168             (replace-match "" t t))))
1169
1170   ;; Added by Per Abrahamsen <abraham@iesd.auc.dk>.
1171   ;; Help save the the world!
1172   (or 
1173    gnus-expert-user
1174    (let ((newsgroups (mail-fetch-field "newsgroups"))
1175          (followup-to (mail-fetch-field "followup-to"))
1176          groups to)
1177      (if (and newsgroups
1178               (string-match "," newsgroups) (not followup-to))
1179          (progn
1180            (while (string-match "," newsgroups)
1181              (setq groups
1182                    (cons (list (substring newsgroups 0 (match-beginning 0)))
1183                          groups))
1184              (setq newsgroups (substring newsgroups (match-end 0))))
1185            (setq groups (nreverse (cons (list newsgroups) groups)))
1186
1187            (setq to (completing-read 
1188                      "Followups to: (default all groups) " groups))
1189            (if (> (length to) 0)
1190                (progn
1191                  (goto-char (point-min))
1192                  (insert "Followup-To: " to "\n")))))))
1193
1194   ;; Cleanup Followup-To.
1195   (goto-char (point-min))
1196   (if (search-forward-regexp "^Followup-To: +" nil t)
1197       (save-restriction
1198         (narrow-to-region
1199          (point)
1200          (if (re-search-forward "^[^ \t]" nil 'end)
1201              (match-beginning 0)
1202            (point-max)))
1203         (goto-char (point-min))
1204         (replace-regexp "\n[ \t]+" " ") ;No line breaks (too confusing)
1205         (goto-char (point-min))
1206         (replace-regexp "[ \t\n]*,[ \t\n]*\\|[ \t]+" ","))))
1207
1208
1209 (defun gnus-inews-remove-headers ()
1210   (let ((case-fold-search t)
1211         (headers gnus-removable-headers))
1212     ;; Remove toxic headers.
1213     (while headers
1214       (goto-char (point-min))
1215       (and (re-search-forward 
1216             (concat "^" (downcase (format "%s" (car headers))))
1217             nil t)
1218            (delete-region (progn (beginning-of-line) (point))
1219                           (progn (forward-line 1) (point))))
1220       (setq headers (cdr headers)))))
1221
1222 ;;; Since the X-Newsreader/X-Mailer are ``vanity'' headers, they might
1223 ;;; as well include the Emacs version as well.
1224 ;;; The following function works with later GNU Emacs, and XEmacs.
1225 (defun gnus-extended-version ()
1226   "Stringified Gnus version and Emacs version"
1227   (interactive)
1228   (concat
1229    gnus-version
1230    "/"
1231    (cond
1232     ((string-match "^\\([0-9]+\\.[0-9]+\\)\\.[.0-9]+$" emacs-version)
1233      (concat "Emacs " (substring emacs-version
1234                                  (match-beginning 1)
1235                                  (match-end 1))))
1236     ((string-match "\\([A-Z]*[Mm][Aa][Cc][Ss]\\)" emacs-version)
1237      (concat (substring emacs-version
1238                         (match-beginning 1)
1239                         (match-end 1))
1240              (format " %d.%d" emacs-major-version emacs-minor-version)))
1241     (t emacs-version))))
1242
1243 (defun gnus-inews-insert-headers (&optional headers)
1244   "Prepare article headers.
1245 Headers already prepared in the buffer are not modified.
1246 Headers in `gnus-required-headers' will be generated."
1247   (let* ((Date (gnus-inews-date))
1248          (Message-ID (gnus-inews-message-id))
1249          (Organization (gnus-inews-organization))
1250          (From (gnus-inews-user-name))
1251          (Path (gnus-inews-path))
1252          (Subject nil)
1253          (Newsgroups nil)
1254          (In-Reply-To (gnus-inews-in-reply-to))
1255          (To nil)
1256          (Distribution (gnus-inews-distribution))
1257          (Lines (gnus-inews-lines))
1258          (X-Newsreader (gnus-extended-version))
1259          (X-Mailer X-Newsreader)
1260          (Expires (gnus-inews-expires))
1261          (headers (or headers gnus-required-headers))
1262          (case-fold-search t)
1263          header value elem)
1264     ;; First we remove any old generated headers.
1265     (let ((headers gnus-deletable-headers))
1266       (while headers
1267         (goto-char (point-min))
1268         (and (re-search-forward 
1269               (concat "^" (symbol-name (car headers)) ": *") nil t)
1270              (get-text-property (1+ (match-beginning 0)) 'gnus-deletable)
1271              (gnus-delete-line))
1272         (setq headers (cdr headers))))
1273     ;; If there are References, and no "Re: ", then the thread has
1274     ;; changed name. See Son-of-1036.
1275     (if (and (mail-fetch-field "references")
1276              (get-buffer gnus-article-buffer))
1277         (let ((psubject (gnus-simplify-subject-re
1278                          (mail-fetch-field "subject"))))
1279           (or (and psubject gnus-reply-subject 
1280                    (string= (gnus-simplify-subject-re gnus-reply-subject)
1281                             psubject))
1282               (progn
1283                 (string-match "@" Message-ID)
1284                 (setq Message-ID
1285                       (concat (substring Message-ID 0 (match-beginning 0))
1286                               "_-_" 
1287                               (substring Message-ID (match-beginning 0))))))))
1288     ;; Go through all the required headers and see if they are in the
1289     ;; articles already. If they are not, or are empty, they are
1290     ;; inserted automatically - except for Subject, Newsgroups and
1291     ;; Distribution. 
1292     (while headers
1293       (goto-char (point-min))
1294       (setq elem (pop headers))
1295       (if (consp elem)
1296           (setq header (car elem))
1297         (setq header elem))
1298       (when (or (not (re-search-forward 
1299                       (concat "^" (downcase (symbol-name header)) ":") nil t))
1300                 (progn
1301                   ;; The header was found. We insert a space after the
1302                   ;; colon, if there is none.
1303                   (if (/= (following-char) ? ) (insert " "))
1304                   ;; Find out whether the header is empty...
1305                   (looking-at "[ \t]*$")))
1306         ;; So we find out what value we should insert.
1307         (setq value
1308               (cond 
1309                ((and (consp elem) (eq (car elem) 'optional))
1310                 ;; This is an optional header.  If the cdr of this
1311                 ;; is something that is nil, then we do not insert
1312                 ;; this header.
1313                 (setq header (cdr elem))
1314                 (or (and (fboundp (cdr elem)) (funcall (cdr elem)))
1315                     (and (boundp (cdr elem)) (symbol-value (cdr elem)))))
1316                ((consp elem)
1317                 ;; The element is a cons.  Either the cdr is a
1318                 ;; string to be inserted verbatim, or it is a
1319                 ;; function, and we insert the value returned from
1320                 ;; this function.
1321                 (or (and (stringp (cdr elem)) (cdr elem))
1322                     (and (fboundp (cdr elem)) (funcall (cdr elem)))))
1323                ((and (boundp header) (symbol-value header))
1324                 ;; The element is a symbol.  We insert the value
1325                 ;; of this symbol, if any.
1326                 (symbol-value header))
1327                (t
1328                 ;; We couldn't generate a value for this header,
1329                 ;; so we just ask the user.
1330                 (read-from-minibuffer
1331                  (format "Empty header for %s; enter value: " header)))))
1332         ;; Finally insert the header.
1333         (when (and value 
1334                    (not (equal value "")))
1335           (save-excursion
1336             (if (bolp)
1337                 (progn
1338                   ;; This header didn't exist, so we insert it.
1339                   (goto-char (point-max))
1340                   (insert (symbol-name header) ": " value "\n")
1341                   (forward-line -1))
1342               ;; The value of this header was empty, so we clear
1343               ;; totally and insert the new value.
1344               (delete-region (point) (gnus-point-at-eol))
1345               (insert value))
1346             ;; Add the deletable property to the headers that require it.
1347             (and (memq header gnus-deletable-headers)
1348                  (progn (beginning-of-line) (looking-at "[^:]+: "))
1349                  (add-text-properties 
1350                   (point) (match-end 0)
1351                   '(gnus-deletable t face italic) (current-buffer)))))))
1352     ;; Insert new Sender if the From is strange. 
1353     (let ((from (mail-fetch-field "from"))
1354           (sender (mail-fetch-field "sender"))
1355           (secure-sender (gnus-inews-real-user-address)))
1356       (when (and from 
1357                  (not (gnus-check-before-posting 'sender))
1358                  (not (string=
1359                        (downcase (car (cdr (gnus-extract-address-components
1360                                             from))))
1361                        (downcase (gnus-inews-real-user-address))))
1362                  (or (null sender)
1363                      (not 
1364                       (string=
1365                        (downcase (car (cdr (gnus-extract-address-components
1366                                             sender))))
1367                        (downcase secure-sender)))))
1368         (goto-char (point-min))    
1369         ;; Rename any old Sender headers to Original-Sender.
1370         (when (re-search-forward "^Sender:" nil t)
1371           (beginning-of-line)
1372           (insert "Original-")
1373           (beginning-of-line))
1374         (insert "Sender: " secure-sender "\n")))))
1375
1376 (defun gnus-inews-insert-signature ()
1377   "Insert a signature file.
1378 If `gnus-signature-function' is bound and returns a string, this
1379 string is used instead of the variable `gnus-signature-file'.
1380 In either case, if the string is a file name, this file is
1381 inserted. If the string is not a file name, the string itself is
1382 inserted. 
1383
1384 If you never want any signature inserted, set both of these variables to
1385 nil."
1386   (save-excursion
1387     (let ((signature 
1388            (or (and gnus-signature-function
1389                     (funcall gnus-signature-function gnus-newsgroup-name))
1390                gnus-signature-file)))
1391       (if (and signature
1392                (or (file-exists-p signature)
1393                    (string-match " " signature)
1394                    (not (string-match 
1395                          "^/[^/]+/" (expand-file-name signature)))))
1396           (progn
1397             (goto-char (point-max))
1398             (if (and mail-signature (search-backward "\n-- \n" nil t))
1399                 ()
1400               ;; Delete any previous signatures.
1401               (if (search-backward "\n-- \n" nil t)
1402                   (delete-region (point) (point-max)))
1403               (or (eolp) (insert "\n"))
1404               (insert "-- \n")
1405               (if (file-exists-p signature)
1406                   (insert-file-contents signature)
1407                 (insert signature))
1408               (goto-char (point-max))
1409               (or (bolp) (insert "\n"))))))))
1410
1411 ;; Written by "Mr. Per Persson" <pp@solace.mh.se>.
1412 (defun gnus-inews-insert-mime-headers ()
1413   (goto-char (point-min))
1414   (let ((mail-header-separator 
1415          (progn 
1416            (goto-char (point-min))
1417            (if (and (search-forward (concat "\n" mail-header-separator "\n")
1418                                     nil t)
1419                     (not (search-backward "\n\n" nil t)))
1420                mail-header-separator
1421              ""))))
1422     (or (mail-position-on-field "Mime-Version")
1423         (insert "1.0")
1424         (cond ((save-restriction
1425                  (widen)
1426                  (goto-char (point-min))
1427                  (re-search-forward "[\200-\377]" nil t))
1428                (or (mail-position-on-field "Content-Type")
1429                    (insert "text/plain; charset=ISO-8859-1"))
1430                (or (mail-position-on-field "Content-Transfer-Encoding")
1431                    (insert "8bit")))
1432               (t (or (mail-position-on-field "Content-Type")
1433                      (insert "text/plain; charset=US-ASCII"))
1434                  (or (mail-position-on-field "Content-Transfer-Encoding")
1435                      (insert "7bit")))))))
1436
1437 (defun gnus-inews-do-fcc ()
1438   "Process Fcc headers in the current buffer.
1439 Unless the first character of the field is `|', the article is saved
1440 to the specified file using the function specified by the variable
1441 gnus-author-copy-saver.  The default function rmail-output saves in
1442 Unix mailbox format.
1443
1444 If the first character is `|', the contents of the article is sent to
1445 a program specified by the rest of the value."
1446   (let ((case-fold-search t)            ;Should ignore case.
1447         list file)
1448     (save-excursion
1449       (save-restriction
1450         (nnheader-narrow-to-headers)
1451         (while (setq file (mail-fetch-field "fcc"))
1452           (push file list)
1453           (nnheader-remove-header "fcc" nil t))
1454         ;; Process FCC operations.
1455         (widen)
1456         (while list
1457           (setq file (pop list))
1458           (if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
1459               ;; Pipe the article to the program in question.
1460               (call-process-region (point-min) (point-max) shell-file-name
1461                                    nil nil nil "-c" (match-string 1 file))
1462             ;; Save the article.
1463             (setq file (expand-file-name file))
1464             (gnus-make-directory (file-name-directory file))
1465             (if (and gnus-author-copy-saver
1466                      (not (eq gnus-author-copy-saver 'rmail-output)))
1467                 (funcall gnus-author-copy-saver file)
1468               (if (and (file-readable-p file) (mail-file-babyl-p file))
1469                   (gnus-output-to-rmail file)
1470                 (let ((mail-use-rfc822 t))
1471                   (rmail-output file 1 t t))))))))))
1472
1473 (defun gnus-inews-path ()
1474   "Return uucp path."
1475   (let ((login-name (gnus-inews-login-name)))
1476     (cond ((null gnus-use-generic-path)
1477            (concat (nth 1 gnus-select-method) "!" login-name))
1478           ((stringp gnus-use-generic-path)
1479            ;; Support GENERICPATH.  Suggested by vixie@decwrl.dec.com.
1480            (concat gnus-use-generic-path "!" login-name))
1481           (t login-name))))
1482
1483 (defun gnus-inews-user-name ()
1484   "Return user's network address as \"NAME@DOMAIN (FULL-NAME)\"."
1485   (let ((full-name (gnus-inews-full-name))
1486         (address (if (or gnus-user-login-name gnus-use-generic-from
1487                          gnus-local-domain (getenv "DOMAINNAME"))
1488                      (concat (gnus-inews-login-name) "@"
1489                              (gnus-inews-domain-name gnus-use-generic-from))
1490                    user-mail-address))) 
1491     (or gnus-user-from-line
1492         (concat address
1493                 ;; User's full name.
1494                 (cond ((string-equal full-name "&") ;Unix hack.
1495                        (concat " (" (user-login-name) ")"))
1496                       ((string-match "[^ ]+@[^ ]+ +(.*)" address)
1497                        "")
1498                       (t
1499                        (concat " (" full-name ")")))))))
1500
1501 (defun gnus-inews-real-user-address ()
1502   "Return the \"real\" user address.
1503 This function tries to ignore all user modifications, and 
1504 give as trustworthy answer as possible."
1505   (concat (user-login-name) "@" (system-name)))
1506
1507 (defun gnus-inews-login-name ()
1508   "Return login name."
1509   (or gnus-user-login-name (getenv "LOGNAME") (user-login-name)))
1510
1511 (defun gnus-inews-full-name ()
1512   "Return full user name."
1513   (or gnus-user-full-name (getenv "NAME") (user-full-name)))
1514
1515 (defun gnus-inews-domain-name (&optional genericfrom)
1516   "Return user's domain name.
1517 If optional argument GENERICFROM is a string, use it as the domain
1518 name; if it is non-nil, strip off local host name from the domain name.
1519 If the function `system-name' returns full internet name and the
1520 domain is undefined, the domain name is got from it."
1521   (if (or genericfrom gnus-local-domain (getenv "DOMAINNAME"))
1522       (let* ((system-name (system-name))
1523              (domain 
1524               (or (if (stringp genericfrom) genericfrom)
1525                   (getenv "DOMAINNAME")
1526                   (and (boundp 'mail-host-address)
1527                        mail-host-address)
1528                   gnus-local-domain
1529                   ;; Function `system-name' may return full internet name.
1530                   ;; Suggested by Mike DeCorte <mrd@sun.soe.clarkson.edu>.
1531                   (if (string-match "\\.." system-name)
1532                       ;; Some machines return "name.", and that's not
1533                       ;; very nice. 
1534                       (substring system-name (1- (match-end 0))))
1535                   (read-string "Domain name (no host): ")))
1536              (host (or (if (string-match "\\." system-name)
1537                            (substring system-name 0 (match-beginning 0)))
1538                        system-name)))
1539         (if (string-equal "." (substring domain 0 1))
1540             (setq domain (substring domain 1)))
1541         ;; Support GENERICFROM as same as standard Bnews system.
1542         ;; Suggested by ohm@kaba.junet and vixie@decwrl.dec.com.
1543         (cond ((null genericfrom)
1544                (concat host "." domain))
1545               ;;((stringp genericfrom) genericfrom)
1546               (t domain)))
1547     (if (string-match "\\." (system-name))
1548         (system-name)
1549       (if (string-match "@\\([^ ]+\\)\\($\\| \\)" user-mail-address)
1550           (substring user-mail-address 
1551                      (match-beginning 1) (match-end 1))
1552         "bogus-domain"))))
1553
1554 (defun gnus-inews-full-address ()
1555   (let ((domain (gnus-inews-domain-name))
1556         (system (system-name))
1557         (case-fold-search t))
1558     (if (string-match "\\." system) system
1559       (if (string-match (concat "^" (regexp-quote system)) domain) domain
1560         (concat system "." domain)))))
1561
1562 (defun gnus-inews-expires ()
1563   "Return an Expires header based on `gnus-article-expires'."
1564   (let ((current (current-time))
1565         (future (* 1.0 gnus-article-expires 60 60 24)))
1566     ;; Add the future to current.
1567     (setcar current (+ (car current) (round (/ future (expt 2 16)))))
1568     (setcar (cdr current) (+ (nth 1 current) (% (round future) (expt 2 16))))
1569     ;; Return the date in the future in UT.
1570     (timezone-make-date-arpa-standard 
1571      (current-time-string current) (current-time-zone) '(0 "UT"))))
1572
1573 (defun gnus-inews-distribution ()
1574   "Return the current Distribution header, if any."
1575   (when (and gnus-distribution-function
1576              (fboundp gnus-distribution-function))
1577     (funcall gnus-distribution-function (or gnus-newsgroup-name ""))))
1578
1579 (defun gnus-inews-message-id ()
1580   "Generate unique Message-ID for user."
1581   ;; Message-ID should not contain a slash and should be terminated by
1582   ;; a number.  I don't know the reason why it is so.
1583   (concat "<" (gnus-inews-unique-id) "@" (gnus-inews-full-address) ">"))
1584
1585 (defvar gnus-unique-id-char nil)
1586
1587 ;; If you ever change this function, make sure the new version
1588 ;; cannot generate IDs that the old version could.
1589 ;; You might for example insert a "." somewhere (not next to another dot
1590 ;; or string boundary), or modify the newsreader name to "Ding".
1591 (defun gnus-inews-unique-id ()
1592   ;; Don't use microseconds from (current-time), they may be unsupported.
1593   ;; Instead we use this randomly inited counter.
1594   (setq gnus-unique-id-char
1595         (% (1+ (or gnus-unique-id-char (logand (random t) (1- (lsh 1 20)))))
1596            ;; (current-time) returns 16-bit ints,
1597            ;; and 2^16*25 just fits into 4 digits i base 36.
1598            (* 25 25)))
1599   (let ((tm (if (fboundp 'current-time)
1600                 (current-time) '(12191 46742 287898))))
1601     (concat
1602      (if (memq system-type '(ms-dos emx vax-vms))
1603          (let ((user (downcase (gnus-inews-login-name))))
1604            (while (string-match "[^a-z0-9_]" user)
1605              (aset user (match-beginning 0) ?_))
1606            user)
1607        (gnus-number-base36 (user-uid) -1))
1608      (gnus-number-base36 (+ (car   tm) (lsh (% gnus-unique-id-char 25) 16)) 4)
1609      (gnus-number-base36 (+ (nth 1 tm) (lsh (/ gnus-unique-id-char 25) 16)) 4)
1610      ;; Append the newsreader name, because while the generated
1611      ;; ID is unique to this newsreader, other newsreaders might
1612      ;; otherwise generate the same ID via another algorithm.
1613      ".fsf")))
1614
1615
1616 (defun gnus-inews-date ()
1617   "Current time string."
1618   (let ((now (current-time)))
1619     (timezone-make-date-arpa-standard 
1620      (current-time-string now) (current-time-zone now))))
1621
1622 (defun gnus-inews-organization ()
1623   "Return user's organization.
1624 The ORGANIZATION environment variable is used if defined.
1625 If not, the variable `gnus-local-organization' is used instead.
1626 If it is a function, the function will be called with the current
1627 newsgroup name as the argument.
1628 If this is a file name, the contents of this file will be used as the
1629 organization."
1630   (let* ((organization 
1631           (or (getenv "ORGANIZATION")
1632               (if gnus-local-organization
1633                   (if (gnus-functionp gnus-local-organization)
1634                       (funcall gnus-local-organization gnus-newsgroup-name)
1635                     gnus-local-organization))
1636               gnus-organization-file
1637               "~/.organization")))
1638     (and (stringp organization)
1639          (> (length organization) 0)
1640          (or (file-exists-p organization)
1641              (string-match " " organization)
1642              (not (string-match "^/usr/lib/\\|^~/" organization)))
1643          (save-excursion
1644            (gnus-set-work-buffer)
1645            (if (file-exists-p organization)
1646                (insert-file-contents organization)
1647              (insert organization))
1648            (goto-char (point-min))
1649            (while (re-search-forward " *\n *" nil t)
1650              (replace-match " " t t))
1651            (buffer-substring (point-min) (point-max))))))
1652
1653 (defun gnus-inews-lines ()
1654   "Count the number of lines and return numeric string."
1655   (save-excursion
1656     (save-restriction
1657       (widen)
1658       (goto-char (point-min))
1659       (re-search-forward 
1660        (concat "^" (regexp-quote mail-header-separator) "$"))
1661       (forward-line 1)
1662       (int-to-string (count-lines (point) (point-max))))))
1663
1664 (defun gnus-inews-in-reply-to ()
1665   "Return the In-Reply-To header for this message."
1666   gnus-in-reply-to)
1667
1668 \f
1669 ;;;
1670 ;;; Gnus Mail Functions 
1671 ;;;
1672
1673 ;;; Mail reply commands of Gnus summary mode
1674
1675 (defun gnus-summary-reply (yank &optional yank-articles)
1676   "Reply mail to news author.
1677 If prefix argument YANK is non-nil, original article is yanked automatically.
1678 Customize the variable gnus-mail-reply-method to use another mailer."
1679   (interactive "P")
1680   ;; Bug fix by jbw@bigbird.bu.edu (Joe Wells)
1681   ;; Stripping headers should be specified with mail-yank-ignored-headers.
1682   (gnus-set-global-variables)
1683   (if yank-articles (gnus-summary-goto-subject (car yank-articles)))
1684   (gnus-summary-select-article)
1685   (bury-buffer gnus-article-buffer)
1686   (gnus-mail-reply (or yank-articles (not (not yank)))))
1687
1688 (defun gnus-summary-reply-with-original (n)
1689   "Reply mail to news author with original article.
1690 Customize the variable gnus-mail-reply-method to use another mailer."
1691   (interactive "P")
1692   (gnus-summary-reply t (gnus-summary-work-articles n)))
1693
1694 (defun gnus-summary-mail-forward (post)
1695   "Forward the current message to another user.
1696 Customize the variable gnus-mail-forward-method to use another mailer."
1697   (interactive "P")
1698   (gnus-set-global-variables)
1699   (gnus-summary-select-article)
1700   (gnus-copy-article-buffer)
1701   (if post
1702       (gnus-forward-using-post gnus-original-article-buffer)
1703     (gnus-mail-forward gnus-original-article-buffer)))
1704
1705 (defun gnus-summary-resend-message (address)
1706   "Resend the current article to ADDRESS."
1707   (interactive "sResend message to: ")
1708   (gnus-summary-select-article)
1709   (save-excursion
1710     (let (resent beg)
1711       ;; We first set up a normal mail buffer.
1712       (nnheader-set-temp-buffer " *Gnus resend*")
1713       ;; This code from sendmail.el
1714       (insert "To: ")
1715       (let ((fill-prefix "\t")
1716             (address-start (point)))
1717         (insert address "\n")
1718         (fill-region-as-paragraph address-start (point-max)))
1719       (insert mail-header-separator "\n")
1720       ;; Insert our usual headers.
1721       (gnus-inews-narrow-to-headers)
1722       (gnus-inews-insert-headers '(From Date To))
1723       (goto-char (point-min))
1724       ;; Rename them all to "Resent-*".
1725       (while (re-search-forward "^[A-Za-z]" nil t)
1726         (forward-char -1)
1727         (insert "Resent-"))
1728       (widen)
1729       (forward-line)
1730       (delete-region (point) (point-max))
1731       (setq beg (point))
1732       ;; Insert the message to be resent.
1733       (insert-buffer-substring gnus-original-article-buffer)
1734       (goto-char (point-min))
1735       (search-forward "\n\n")
1736       (forward-char -1)
1737       (insert mail-header-separator)
1738       ;; Rename all old ("Also-")Resent headers.
1739       (while (re-search-backward "^\\(Also-\\)?Resent-" beg t)
1740         (beginning-of-line)
1741         (insert "Also-"))
1742       ;; Send it.
1743       (mail-send)
1744       (kill-buffer (current-buffer)))))
1745
1746 (defun gnus-summary-post-forward ()
1747   "Forward the current article to a newsgroup."
1748   (interactive)
1749   (gnus-summary-mail-forward t))
1750
1751 (defvar gnus-nastygram-message 
1752   "The following article was inappropriately posted to %s.\n"
1753   "Format string to insert in nastygrams.
1754 The current group name will be inserted at \"%s\".")
1755
1756 (defun gnus-summary-mail-nastygram (n)
1757   "Send a nastygram to the author of the current article."
1758   (interactive "P")
1759   (if (or gnus-expert-user
1760           (gnus-y-or-n-p 
1761            "Really send a nastygram to the author of the current article? "))
1762       (let ((group gnus-newsgroup-name))
1763         (gnus-summary-reply-with-original n)
1764         (set-buffer gnus-mail-buffer)
1765         (insert (format gnus-nastygram-message group))
1766         (gnus-mail-send-and-exit))))
1767
1768 (defun gnus-summary-mail-other-window ()
1769   "Compose mail in other window.
1770 Customize the variable `gnus-mail-other-window-method' to use another
1771 mailer."
1772   (interactive)
1773   (gnus-set-global-variables)
1774   (gnus-new-mail
1775    ;; We might want to prompt here.
1776    (when (and gnus-interactive-post
1777               (not gnus-expert-user))
1778      (read-string "To: ")))
1779   (gnus-configure-windows 'summary-mail 'force))
1780
1781 (defun gnus-new-mail (&optional to)
1782   (let (subject)
1783     (when (and gnus-interactive-post
1784                (not gnus-expert-user))
1785       (setq subject (read-string "Subject: ")))
1786     (pop-to-buffer gnus-mail-buffer)
1787     (erase-buffer)
1788     (gnus-mail-setup 'new to subject)
1789     (gnus-inews-insert-gcc)
1790     (gnus-inews-insert-archive-gcc)
1791     (run-hooks 'gnus-mail-hook)))
1792
1793 (defun gnus-mail-reply (&optional yank to-address followup)
1794   (save-excursion
1795     (set-buffer gnus-summary-buffer)
1796     (let ((group (gnus-group-real-name gnus-newsgroup-name))
1797           (cur (cons (current-buffer) (cdr gnus-article-current)))
1798           (winconf (current-window-configuration))
1799           from subject date reply-to message-of to cc
1800           references message-id sender follow-to sendto elt new-cc new-to
1801           mct mctdo)
1802       (set-buffer (get-buffer-create gnus-mail-buffer))
1803       (mail-mode)
1804       (if (and (buffer-modified-p)
1805                (> (buffer-size) 0)
1806                (not (gnus-y-or-n-p 
1807                      "Unsent message being composed; erase it? ")))
1808           ()
1809         (erase-buffer)
1810         (save-excursion
1811           (gnus-copy-article-buffer)
1812           (save-restriction
1813             (set-buffer gnus-article-copy)
1814             (nnheader-narrow-to-headers)
1815             (if (not followup)
1816                 ;; This is a regular reply.
1817                 (if (gnus-functionp gnus-reply-to-function)
1818                     (setq follow-to (funcall gnus-reply-to-function group)))
1819               ;; This is a followup.
1820               (if (gnus-functionp gnus-followup-to-function)
1821                   (save-excursion
1822                     (setq follow-to
1823                           (funcall gnus-followup-to-function group)))))
1824             (setq from (mail-fetch-field "from"))
1825             (setq date (or (mail-fetch-field "date") 
1826                            (mail-header-date gnus-current-headers)))
1827             (setq message-of (gnus-message-of from date))
1828             (setq sender (mail-fetch-field "sender"))
1829             (setq subject (or (mail-fetch-field "subject") "none"))
1830             ;; Remove any (buggy) Re:'s that are present and make a
1831             ;; proper one.
1832             (and (string-match "^[ \t]*[Re][Ee]:[ \t]*" subject)
1833                  (setq subject (substring subject (match-end 0))))
1834             (setq subject (concat "Re: " subject))
1835             (setq to (mail-fetch-field "to"))
1836             (setq cc (mail-fetch-field "cc"))
1837             (setq mct (mail-fetch-field "mail-copies-to"))
1838             (setq reply-to
1839                   (unless (gnus-group-get-parameter group 'broken-reply-to)
1840                     (mail-fetch-field "reply-to")))
1841             (setq references (mail-fetch-field "references"))
1842             (setq message-id (mail-fetch-field "message-id"))
1843             
1844             (setq mctdo (not (equal mct "never")))
1845
1846             (if (not (and followup (not to-address)))
1847                 (setq new-to (or reply-to from))
1848               (let (ccalist)
1849                 (save-excursion
1850                   (gnus-set-work-buffer)
1851                   (unless (equal mct "never")
1852                     (insert (or reply-to from "")))
1853                   (insert 
1854                    (if (bolp) "" ", ") (or to "")
1855                    (if (or (not mct) (not mctdo)) ""
1856                      (concat (if (bolp) "" ", ") mct))
1857                    (if cc (concat (if (bolp) "" ", ") cc) ""))
1858                   ;; Remove addresses that match `rmail-dont-reply-to-names'. 
1859                   (insert (prog1 (rmail-dont-reply-to (buffer-string))
1860                             (erase-buffer)))
1861                   (goto-char (point-min))
1862                   (setq ccalist
1863                         (mapcar
1864                          (lambda (addr)
1865                            (cons (mail-strip-quoted-names addr) addr))
1866                          (nreverse (gnus-mail-parse-comma-list))))
1867                   (let ((s ccalist))
1868                     (while s
1869                       (setq ccalist (delq (assoc (car (pop s)) s) ccalist)))))
1870                 (setq new-to (cdr (pop ccalist)))
1871                 (setq new-cc 
1872                       (mapconcat 
1873                        (lambda (addr) (cdr addr))
1874                        ccalist ", "))))
1875             (widen)))
1876
1877         (setq news-reply-yank-from (or from "(nobody)"))
1878         (setq news-reply-yank-message-id
1879               (or message-id "(unknown Message-ID)"))
1880
1881         ;; Gather the "to" addresses out of the follow-to list and remove
1882         ;; them as we go.
1883         (if (and follow-to (listp follow-to))
1884             (while (setq elt (assoc "To" follow-to))
1885               (setq sendto (concat sendto (and sendto ", ") (cdr elt)))
1886               (setq follow-to (delq elt follow-to))))
1887
1888         (gnus-mail-setup 
1889          (if followup 'followup 'reply)
1890          (or to-address 
1891              (if (and follow-to (not (stringp follow-to))) sendto
1892                (or follow-to new-to sender "")))
1893          subject message-of
1894          (if (zerop (length new-cc)) nil new-cc)
1895          gnus-article-copy)
1896
1897         (make-local-variable 'gnus-article-reply)
1898         (setq gnus-article-reply cur)
1899         (make-local-variable 'gnus-prev-winconf)
1900         (setq gnus-prev-winconf winconf)
1901         (make-local-variable 'gnus-reply-subject)
1902         (setq gnus-reply-subject subject)
1903         (make-local-variable 'gnus-in-reply-to)
1904         (setq gnus-in-reply-to message-of)
1905
1906         (auto-save-mode auto-save-default)
1907         (gnus-inews-insert-gcc)
1908         (gnus-inews-insert-archive-gcc)
1909
1910         (when (and follow-to (listp follow-to))
1911           (let (beg)
1912             (gnus-inews-narrow-to-headers)
1913             (re-search-forward "^To:" nil t)
1914             (beginning-of-line)
1915             (forward-line 1)
1916             (setq beg (point))
1917             ;; Insert the rest of the Follow-To headers.
1918             (while follow-to
1919               (goto-char (point-min))
1920               (if (not (re-search-forward 
1921                         (concat "^" (caar follow-to) ": *") nil t))
1922                   (progn
1923                     (goto-char beg)
1924                     (insert (caar follow-to) ": " (cdar follow-to) "\n"))
1925                 (unless (eolp)
1926                   (insert ", "))
1927                 (insert (cdar follow-to)))
1928               (setq follow-to (cdr follow-to)))
1929             (widen)))
1930         (nnheader-insert-references references message-id)
1931
1932         ;; Now the headers should be ok, so we do the yanking.
1933         (goto-char (point-min))
1934         (re-search-forward
1935          (concat "^" (regexp-quote mail-header-separator) "$"))
1936         (forward-line 1)
1937         (if (not yank)
1938             (gnus-configure-windows 'reply 'force)
1939           (let ((last (point))
1940                 end)
1941             (if (not (listp yank))
1942                 (progn
1943                   ;; Just a single article being yanked.
1944                   (save-excursion
1945                     (mail-yank-original nil))
1946                   (or mail-yank-hooks mail-citation-hook
1947                       (run-hooks 'news-reply-header-hook)))
1948               (while yank
1949                 (save-window-excursion
1950                   (set-buffer gnus-summary-buffer)
1951                   (gnus-summary-select-article nil nil nil (car yank))
1952                   (gnus-summary-remove-process-mark (car yank)))
1953                 (save-excursion
1954                   (setq end (point))
1955                   (gnus-copy-article-buffer)
1956                   (mail-yank-original nil)
1957                   (save-restriction
1958                     (narrow-to-region (point-min) (point))
1959                     (goto-char (mark t))
1960                     (let ((news-reply-yank-from
1961                            (save-excursion 
1962                              (set-buffer gnus-article-buffer)
1963                              (or (mail-fetch-field "from") "(nobody)")))
1964                           (news-reply-yank-message-id
1965                            (save-excursion 
1966                              (set-buffer gnus-article-buffer)
1967                              (or (mail-fetch-field "message-id")
1968                                  "(unknown Message-ID)"))))
1969                       (or mail-yank-hooks mail-citation-hook
1970                           (run-hooks 'news-reply-header-hook))
1971                       (setq end (point-max)))))
1972                 (goto-char end)
1973                 (setq yank (cdr yank))))
1974             (goto-char last))
1975           (forward-line 2)
1976           (gnus-configure-windows 'reply-yank 'force))
1977         (run-hooks 'gnus-mail-hook)
1978         ;; Mark this buffer as unchanged.
1979         (set-buffer-modified-p nil)))))
1980
1981 (defun gnus-mail-parse-comma-list ()
1982   (let (accumulated
1983         beg)
1984     (skip-chars-forward " ")
1985     (while (not (eobp))
1986       (setq beg (point))
1987       (skip-chars-forward "^,")
1988       (while (zerop
1989               (save-excursion 
1990                 (save-restriction
1991                   (let ((i 0))
1992                     (narrow-to-region beg (point))
1993                     (goto-char beg)
1994                     (logand (progn
1995                               (while (search-forward "\"" nil t)
1996                                 (incf i))
1997                               (if (zerop i) 2 i)) 2)))))
1998         (skip-chars-forward ",")
1999         (skip-chars-forward "^,"))
2000       (skip-chars-backward " ")
2001       (setq accumulated
2002             (cons (buffer-substring beg (point))
2003                   accumulated))
2004       (skip-chars-forward "^,")
2005       (skip-chars-forward ", "))
2006     accumulated))
2007
2008 (defun gnus-new-news (&optional group inhibit-prompt)
2009   "Set up a *post-news* buffer that points to GROUP.
2010 If INHIBIT-PROMPT, never prompt for a Subject."
2011   (let ((winconf (current-window-configuration))
2012         subject)
2013     (when (and gnus-interactive-post
2014                (not inhibit-prompt)
2015                (not gnus-expert-user))
2016       (setq subject (read-string "Subject: ")))
2017     (pop-to-buffer gnus-post-news-buffer)  
2018     (erase-buffer)
2019     (news-reply-mode)
2020     ;; Let posting styles be configured.
2021     (gnus-configure-posting-styles)
2022     (news-setup nil subject nil (and group (gnus-group-real-name group)) nil)
2023     ;; Associate this buffer with the draft group.
2024     (gnus-enter-buffer-into-draft)
2025     (goto-char (point-min))
2026
2027     (unless (re-search-forward 
2028              (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2029       (goto-char (point-max)))
2030     (insert "\n\n")
2031
2032     (gnus-inews-insert-bfcc)
2033     (gnus-inews-insert-gcc)
2034     (gnus-inews-insert-archive-gcc)
2035     (gnus-inews-insert-signature)
2036     (and gnus-post-prepare-function
2037          (gnus-functionp gnus-post-prepare-function)
2038          (funcall gnus-post-prepare-function group))
2039     (run-hooks 'gnus-post-prepare-hook)
2040     (gnus-inews-set-point)
2041     (make-local-variable 'gnus-prev-winconf)
2042     (setq gnus-prev-winconf winconf)
2043     (gnus-inews-modify-mail-mode-map)
2044     (local-set-key "\C-c\C-c" 'gnus-inews-news)))
2045
2046 (defun gnus-news-followup (&optional yank group)
2047   (save-excursion
2048     (set-buffer gnus-summary-buffer)
2049     (if (not (or (not gnus-novice-user)
2050                  gnus-expert-user
2051                  (gnus-y-or-n-p
2052                   "Are you sure you want to post to all of USENET? ")))
2053         ()
2054       (let ((group (gnus-group-real-name (or group gnus-newsgroup-name)))
2055             (cur (cons (current-buffer) (cdr gnus-article-current)))
2056             (winconf (current-window-configuration))
2057             from subject date reply-to message-of
2058             references message-id sender follow-to sendto elt 
2059             followup-to distribution newsgroups)
2060         (set-buffer (get-buffer-create gnus-post-news-buffer))
2061         (news-reply-mode)
2062         ;; Associate this buffer with the draft group.
2063         (gnus-enter-buffer-into-draft)
2064         (if (and (buffer-modified-p)
2065                  (> (buffer-size) 0)
2066                  (not (gnus-y-or-n-p 
2067                        "Unsent message being composed; erase it? ")))
2068             ()
2069           (erase-buffer)
2070           (save-excursion
2071             (gnus-copy-article-buffer)
2072             (save-restriction
2073               (set-buffer gnus-article-copy)
2074               (nnheader-narrow-to-headers)
2075               (if (gnus-functionp gnus-followup-to-function)
2076                   (save-excursion
2077                     (setq follow-to
2078                           (funcall gnus-followup-to-function group))))
2079               (setq from (mail-fetch-field "from"))
2080               (setq date (or (mail-fetch-field "date") 
2081                              (mail-header-date gnus-current-headers)))
2082               (setq message-of (gnus-message-of from date))
2083               (setq subject (or (mail-fetch-field "subject") "none"))
2084               ;; Remove any (buggy) Re:'s that are present and make a
2085               ;; proper one.
2086               (and (string-match "^[ \t]*[Re][Ee]:[ \t]*" subject)
2087                    (setq subject (substring subject (match-end 0))))
2088               (setq subject (concat "Re: " subject))
2089               (setq references (mail-fetch-field "references"))
2090               (setq message-id (mail-fetch-field "message-id"))
2091               (setq followup-to (mail-fetch-field "followup-to"))
2092               (setq newsgroups (mail-fetch-field "newsgroups"))
2093               (setq distribution (mail-fetch-field "distribution"))
2094               ;; Remove bogus distribution.
2095               (and (stringp distribution)
2096                    (string-match "world" distribution)
2097                    (setq distribution nil))
2098               (widen)))
2099
2100           (setq news-reply-yank-from (or from "(nobody)"))
2101           (setq news-reply-yank-message-id
2102                 (or message-id "(unknown Message-ID)"))
2103
2104           ;; Gather the "to" addresses out of the follow-to list and remove
2105           ;; them as we go.
2106           (if (and follow-to (listp follow-to))
2107               (while (setq elt (assoc "Newsgroups" follow-to))
2108                 (setq sendto (concat sendto (and sendto ", ") (cdr elt)))
2109                 (setq follow-to (delq elt follow-to))))
2110
2111           ;; Let posting styles be configured.
2112           (gnus-configure-posting-styles)
2113
2114           (news-setup nil subject nil 
2115                       (or sendto 
2116                           (and followup-to
2117                                gnus-use-followup-to
2118                                (or (not (eq gnus-use-followup-to 'ask))
2119                                    (gnus-y-or-n-p 
2120                                     (format
2121                                      "Use Followup-To %s? " followup-to)))
2122                                followup-to)
2123                           newsgroups group "")
2124                       gnus-article-copy)
2125
2126           (make-local-variable 'gnus-article-reply)
2127           (setq gnus-article-reply cur)
2128           (make-local-variable 'gnus-prev-winconf)
2129           (setq gnus-prev-winconf winconf)
2130           (make-local-variable 'gnus-reply-subject)
2131           (setq gnus-reply-subject (mail-header-subject gnus-current-headers))
2132           (make-local-variable 'gnus-in-reply-to)
2133           (setq gnus-in-reply-to message-of)
2134           (when (and followup-to newsgroups)
2135             (make-local-variable 'gnus-newsgroup-followup)
2136             (setq gnus-newsgroup-followup
2137                   (cons newsgroups followup-to)))
2138
2139           (gnus-inews-insert-signature)
2140
2141           (and gnus-post-prepare-function
2142                (gnus-functionp gnus-post-prepare-function)
2143                (funcall gnus-post-prepare-function group))
2144           (run-hooks 'gnus-post-prepare-hook)
2145
2146           (auto-save-mode auto-save-default)
2147           (gnus-inews-modify-mail-mode-map)
2148           (local-set-key "\C-c\C-c" 'gnus-inews-news)
2149
2150           (if (and follow-to (listp follow-to))
2151               (progn
2152                 (goto-char (point-min))
2153                 (and (re-search-forward "^Newsgroups:" nil t)
2154                      (forward-line 1))
2155                 (while follow-to
2156                   (insert (car (car follow-to)) ": " 
2157                           (cdr (car follow-to)) "\n")
2158                   (setq follow-to (cdr follow-to)))))
2159           
2160           ;; If a distribution existed, we use it.
2161           (if distribution
2162               (progn
2163                 (mail-position-on-field "Distribution")
2164                 (insert distribution)))
2165           
2166           (nnheader-insert-references references message-id)
2167
2168           ;; Handle `gnus-auto-mail-to-author'.
2169           ;; Suggested by Daniel Quinlan <quinlan@best.com>.
2170           ;; Revised to respect Reply-To by Ulrik Dickow <dickow@nbi.dk>.
2171           (let ((to (if (if (eq gnus-auto-mail-to-author 'ask)
2172                             (y-or-n-p "Also send mail to author? ")
2173                           gnus-auto-mail-to-author)
2174                         (or (save-excursion
2175                               (set-buffer gnus-article-copy)
2176                               (unless (gnus-group-get-parameter
2177                                        group 'broken-reply-to)
2178                                 (gnus-fetch-field "reply-to")))
2179                             from)))
2180                 (x-mail (save-excursion
2181                           (set-buffer gnus-article-copy)
2182                           (gnus-fetch-field "x-mail-copy-to"))))
2183             ;; Deny sending copy if there's a negative X-Mail-Copy-To
2184             ;; header. 
2185             (if x-mail
2186                 (if (and (string= x-mail "never")
2187                          (not (eq gnus-auto-mail-to-author 'force)))
2188                     (setq to nil)
2189                   (setq to x-mail)))
2190             ;; Insert a To or Cc header.
2191             (if to
2192                 (if (mail-fetch-field "To")
2193                     (progn
2194                       (beginning-of-line)
2195                       (insert "Cc: " to "\n"))
2196                   (mail-position-on-field "To")
2197                   (insert to))))
2198
2199           (gnus-inews-insert-bfcc)
2200           (gnus-inews-insert-gcc)
2201           (gnus-inews-insert-archive-gcc)
2202
2203           ;; Now the headers should be ok, so we do the yanking.
2204           (goto-char (point-min))
2205           (re-search-forward
2206            (concat "^" (regexp-quote mail-header-separator) "$"))
2207           (forward-line 1)
2208           (if (not yank)
2209               (progn
2210                 (gnus-configure-windows 'followup 'force)
2211                 (insert "\n\n")
2212                 (forward-line -2))
2213             (let ((last (point))
2214                   end)
2215               (if (not (listp yank))
2216                   (progn
2217                     (save-excursion
2218                       (mail-yank-original nil))
2219                     (or mail-yank-hooks mail-citation-hook
2220                         (run-hooks 'news-reply-header-hook)))
2221                 (while yank
2222                   (save-window-excursion
2223                     (set-buffer gnus-summary-buffer)
2224                     (gnus-summary-select-article nil nil nil (car yank))
2225                     (gnus-summary-remove-process-mark (car yank)))
2226                   (save-excursion
2227                     (gnus-copy-article-buffer)
2228                     (mail-yank-original nil)
2229                     (setq end (point)))
2230                   (or mail-yank-hooks mail-citation-hook
2231                       (run-hooks 'news-reply-header-hook))
2232                   (goto-char end)
2233                   (setq yank (cdr yank))))
2234               (goto-char last))
2235             (gnus-configure-windows 'followup-yank 'force))
2236         
2237           (make-local-variable 'gnus-article-check-size)
2238           (setq gnus-article-check-size
2239                 (cons (buffer-size) (gnus-article-checksum)))
2240           (gnus-inews-set-point))))))
2241
2242 (defun gnus-message-of (from date)
2243   "Take a FROM and a DATE and return an IN-REPLY-TO."
2244   (when from
2245     (let ((stop-pos 
2246            (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
2247       (concat (if stop-pos (substring from 0 stop-pos) from)
2248               "'s message of " 
2249               (if (or (not date) (string= date ""))
2250                   "(unknown date)" date)))))
2251
2252 (defun gnus-mail-yank-original ()
2253   (interactive)
2254   (save-excursion
2255     (mail-yank-original nil))
2256   (or mail-yank-hooks mail-citation-hook
2257       (run-hooks 'news-reply-header-hook)))
2258
2259 (defun gnus-mail-send-and-exit (&optional dont-send)
2260   "Send the current mail and return to Gnus."
2261   (interactive)
2262   (let* ((reply gnus-article-reply)
2263          (winconf gnus-prev-winconf)
2264          (address-group gnus-add-to-address)
2265          (to-address (and address-group
2266                           (mail-fetch-field "to"))))
2267     (setq gnus-add-to-address nil)
2268     (let ((buffer-file-name nil))
2269       (or dont-send (gnus-mail-send)))
2270     (bury-buffer)
2271     ;; This mail group doesn't have a `to-list', so we add one
2272     ;; here.  Magic!  
2273     (and to-address
2274          (gnus-group-add-parameter 
2275           address-group (cons 'to-list to-address)))
2276     (if (get-buffer gnus-group-buffer)
2277         (progn
2278           (if (gnus-buffer-exists-p (car-safe reply))
2279               (progn
2280                 (set-buffer (car reply))
2281                 (and (cdr reply)
2282                      (gnus-summary-mark-article-as-replied 
2283                       (cdr reply)))))
2284           (and winconf (set-window-configuration winconf))))))
2285
2286 (defun gnus-kill-message-buffer ()
2287   "Kill the current buffer after dissociating it from the draft group."
2288   (interactive)
2289   (when (gnus-y-or-n-p "Dissociate and kill the current buffer? ")
2290     (gnus-dissociate-buffer-from-draft)
2291     (kill-buffer (current-buffer))))
2292
2293 (defun gnus-put-message ()
2294   "Put the current message in some group and return to Gnus."
2295   (interactive)
2296   (let ((reply gnus-article-reply)
2297         (winconf gnus-prev-winconf)
2298         (group gnus-newsgroup-name)
2299         buf)
2300     
2301     (or (and group (not (gnus-group-read-only-p group)))
2302         (setq group (read-string "Put in group: " nil
2303                                  (gnus-writable-groups))))
2304     (and (gnus-gethash group gnus-newsrc-hashtb)
2305          (error "No such group: %s" group))
2306
2307     (save-excursion
2308       (save-restriction
2309         (widen)
2310         (gnus-inews-narrow-to-headers)
2311         (let (gnus-deletable-headers)
2312           (if (eq major-mode 'mail-mode)
2313               (gnus-inews-insert-headers gnus-required-mail-headers)
2314             (gnus-inews-insert-headers)))
2315         (goto-char (point-max))
2316         (insert "Gcc: " group "\n")
2317         (widen)))
2318
2319     (gnus-inews-do-gcc)
2320
2321     (if (get-buffer gnus-group-buffer)
2322         (progn
2323           (if (gnus-buffer-exists-p (car-safe reply))
2324               (progn
2325                 (set-buffer (car reply))
2326                 (and (cdr reply)
2327                      (gnus-summary-mark-article-as-replied 
2328                       (cdr reply)))))
2329           (and winconf (set-window-configuration winconf))))))
2330
2331 (defun gnus-forward-make-subject (buffer)
2332   (save-excursion
2333     (set-buffer buffer)
2334     (concat "[" (if (memq 'mail (assoc (symbol-name 
2335                                         (car (gnus-find-method-for-group 
2336                                               gnus-newsgroup-name)))
2337                                        gnus-valid-select-methods))
2338                     (gnus-fetch-field "From")
2339                   gnus-newsgroup-name)
2340             "] " (or (gnus-fetch-field "Subject") ""))))
2341
2342 (defun gnus-forward-insert-buffer (buffer)
2343   (save-excursion
2344     (save-restriction
2345       ;; Put point where we want it before inserting the forwarded
2346       ;; message. 
2347       (if gnus-signature-before-forwarded-message
2348           (goto-char (point-max))
2349         (goto-char (point-min))
2350         (re-search-forward
2351          (concat "^" (regexp-quote mail-header-separator) "$"))
2352         (forward-line 1))
2353       ;; Narrow to the area we are to insert.
2354       (narrow-to-region (point) (point))
2355       ;; Insert the separators and the forwarded buffer.
2356       (insert gnus-forward-start-separator)
2357       (insert-buffer-substring buffer)
2358       (goto-char (point-max))
2359       (insert gnus-forward-end-separator)
2360       ;; Remove all unwanted headers.
2361       (goto-char (point-min))
2362       (forward-line 1)
2363       (save-restriction
2364         (narrow-to-region (point) (if (search-forward "\n\n" nil t)
2365                                       (1- (point))
2366                                     (point)))
2367         (goto-char (point-min))
2368         (delete-non-matching-lines gnus-forward-included-headers))
2369       ;; Delete any invisible text.
2370       (goto-char (point-min))
2371       (let (beg)
2372         (while (setq beg (next-single-property-change (point) 'invisible))
2373           (goto-char beg)
2374           (delete-region beg (or (next-single-property-change 
2375                                   (point) 'invisible)
2376                                  (point-max))))))))
2377
2378 (defun gnus-mail-forward (&optional buffer)
2379   "Forward the current message to another user using mail."
2380   (let* ((forward-buffer (or buffer (current-buffer)))
2381          (winconf (current-window-configuration))
2382          (subject (gnus-forward-make-subject forward-buffer)))
2383     (set-buffer (get-buffer-create gnus-mail-buffer))
2384     (if (and (buffer-modified-p)
2385              (> (buffer-size) 0)
2386              (not (gnus-y-or-n-p 
2387                    "Unsent message being composed; erase it? ")))
2388         ()
2389       (erase-buffer)
2390       (gnus-mail-setup 'forward nil subject)
2391       (make-local-variable 'gnus-prev-winconf)
2392       (setq gnus-prev-winconf winconf)
2393       (gnus-forward-insert-buffer forward-buffer)
2394       (goto-char (point-min))
2395       (re-search-forward "^To: ?" nil t)
2396       (gnus-configure-windows 'mail-forward 'force)
2397       ;; You have a chance to arrange the message.
2398       (run-hooks 'gnus-mail-forward-hook)
2399       (run-hooks 'gnus-mail-hook))))
2400
2401 (defun gnus-forward-using-post (&optional buffer)
2402   (save-excursion
2403     (let* ((forward-buffer (or buffer (current-buffer))) 
2404            (subject (gnus-forward-make-subject forward-buffer))
2405            (gnus-newsgroup-name nil))
2406       (gnus-post-news 'post nil nil nil nil subject)
2407       (save-excursion
2408         (gnus-forward-insert-buffer forward-buffer)
2409         ;; You have a chance to arrange the message.
2410         (run-hooks 'gnus-mail-forward-hook)))))
2411
2412 (defun gnus-mail-other-window-using-mail ()
2413   "Compose mail other window using mail."
2414   (let ((winconf (current-window-configuration)))
2415     (mail-other-window nil nil nil nil nil (get-buffer gnus-article-buffer))
2416     (gnus-inews-modify-mail-mode-map)
2417     (make-local-variable 'gnus-prev-winconf)
2418     (setq gnus-prev-winconf winconf)
2419     (run-hooks 'gnus-mail-hook)
2420     (gnus-configure-windows 'summary-mail 'force)))
2421
2422 (defun gnus-article-mail (yank)
2423   "Send a reply to the address near point.
2424 If YANK is non-nil, include the original article."
2425   (interactive "P")
2426   (let ((address 
2427          (buffer-substring
2428           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
2429           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
2430     (and address
2431          (progn
2432            (switch-to-buffer gnus-summary-buffer)
2433            (gnus-mail-reply yank address)))))
2434
2435 (defun gnus-bug ()
2436   "Send a bug report to the Gnus maintainers."
2437   (interactive)
2438   (let ((winconf (current-window-configuration)))
2439     (delete-other-windows)
2440     (switch-to-buffer "*Gnus Help Bug*")
2441     (erase-buffer)
2442     (insert gnus-bug-message)
2443     (goto-char (point-min))
2444     (pop-to-buffer "*Gnus Bug*")
2445     (erase-buffer)
2446     (mail-mode)
2447     (mail-setup gnus-maintainer nil nil nil nil nil)
2448     (auto-save-mode auto-save-default)
2449     (make-local-variable 'gnus-prev-winconf)
2450     (setq gnus-prev-winconf winconf)
2451     (gnus-inews-modify-mail-mode-map)
2452     (local-set-key "\C-c\C-c" 'gnus-bug-mail-send-and-exit)
2453     (goto-char (point-min))
2454     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2455     (forward-line 1)
2456     (insert (gnus-version) "\n")
2457     (insert (emacs-version))
2458     (insert "\n\n\n\n\n")
2459     (gnus-debug)
2460     (goto-char (point-min))
2461     (search-forward "Subject: " nil t)
2462     (message "")))
2463
2464 (defun gnus-bug-mail-send-and-exit ()
2465   "Send the bug message and exit."
2466   (interactive)
2467   (let ((cur (current-buffer)))
2468     (and (get-buffer "*Gnus Help Bug*")
2469          (kill-buffer "*Gnus Help Bug*"))
2470     (gnus-mail-send-and-exit)
2471     (when (buffer-name cur)
2472       (kill-buffer cur))))
2473
2474 (defun gnus-debug ()
2475   "Attemps to go through the Gnus source file and report what variables have been changed.
2476 The source file has to be in the Emacs load path."
2477   (interactive)
2478   (let ((files '("gnus.el" "gnus-msg.el" "gnus-score.el" "nnmail.el"))
2479         file dirs expr olist sym)
2480     (message "Please wait while we snoop your variables...")
2481     (sit-for 0)
2482     (save-excursion
2483       (set-buffer (get-buffer-create " *gnus bug info*"))
2484       (buffer-disable-undo (current-buffer))
2485       (while files
2486         (erase-buffer)
2487         (setq dirs load-path)
2488         (while dirs
2489           (if (or (not (car dirs))
2490                   (not (stringp (car dirs)))
2491                   (not (file-exists-p 
2492                         (setq file (concat (file-name-as-directory 
2493                                             (car dirs)) (car files))))))
2494               (setq dirs (cdr dirs))
2495             (setq dirs nil)
2496             (insert-file-contents file)
2497             (goto-char (point-min))
2498             (if (not (re-search-forward "^;;* *Internal variables" nil t))
2499                 (message "Malformed sources in file %s" file)
2500               (narrow-to-region (point-min) (point))
2501               (goto-char (point-min))
2502               (while (setq expr (condition-case () 
2503                                     (read (current-buffer)) (error nil)))
2504                 (condition-case ()
2505                     (and (eq (car expr) 'defvar)
2506                          (stringp (nth 3 expr))
2507                          (or (not (boundp (nth 1 expr)))
2508                              (not (equal (eval (nth 2 expr))
2509                                          (symbol-value (nth 1 expr)))))
2510                          (setq olist (cons (nth 1 expr) olist)))
2511                   (error nil))))))
2512         (setq files (cdr files)))
2513       (kill-buffer (current-buffer)))
2514     (when (setq olist (nreverse olist))
2515       (insert "------------------ Environment follows ------------------\n\n"))
2516     (while olist
2517       (if (boundp (car olist))
2518           (insert "(setq " (symbol-name (car olist)) 
2519                   (if (or (consp (setq sym (symbol-value (car olist))))
2520                           (and (symbolp sym)
2521                                (not (or (eq sym nil)
2522                                         (eq sym t)))))
2523                       " '" " ")
2524                   (prin1-to-string (symbol-value (car olist))) ")\n")
2525         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
2526       (setq olist (cdr olist)))
2527     (insert "\n\n")
2528     ;; Remove any null chars - they seem to cause trouble for some
2529     ;; mailers. (Byte-compiled output from the stuff above.) 
2530     (goto-char (point-min))
2531     (while (re-search-forward "[\000\200]" nil t)
2532       (replace-match "" t t))))
2533
2534
2535 ;;; Treatment of rejected articles.
2536
2537
2538 ;;; Bounced mail.
2539
2540 (defun gnus-summary-resend-bounced-mail (fetch)
2541   "Re-mail the current message.
2542 This only makes sense if the current message is a bounce message than
2543 contains some mail you have written which has been bounced back to
2544 you.
2545 If FETCH, try to fetch the article that this is a reply to, if indeed
2546 this is a reply."
2547   (interactive "P")
2548   (gnus-summary-select-article t)
2549   ;; Create a mail buffer.
2550   (gnus-new-mail)
2551   (erase-buffer)
2552   (insert-buffer-substring gnus-article-buffer)
2553   (goto-char (point-min))
2554   (or (re-search-forward "--- *Original message.*\n" nil t)
2555       (and (search-forward "\n\n" nil t)
2556            (re-search-forward "^Return-Path:.*\n" nil t)))
2557   ;; We remove everything before the bounced mail.
2558   (delete-region 
2559    (point-min)
2560    (if (re-search-forward "[^ \t]*:" nil t)
2561        (match-beginning 0)
2562      (point)))
2563   (let (references)
2564     (save-excursion
2565       (save-restriction
2566         (nnheader-narrow-to-headers)
2567         (nnheader-remove-header gnus-bounced-headers-junk t)
2568         (setq references (mail-fetch-field "references"))
2569         (goto-char (point-max))
2570         (insert mail-header-separator)))
2571     ;; If there are references, we fetch the article we answered to.  
2572     (and fetch 
2573          references
2574          (string-match "\\(<[^]+>\\)[ \t]*$" references)
2575          (gnus-summary-refer-article 
2576           (substring references (match-beginning 1) (match-end 1)))
2577          (progn
2578            (gnus-summary-show-all-headers)
2579            (gnus-configure-windows 'compose-bounce))))
2580   (goto-char (point-min)))
2581
2582 ;;; Sending mail.
2583
2584 (defun gnus-mail-send (&optional no-generated-headers)
2585   "Send the current buffer as mail.
2586 Headers will be generated before sending."
2587   (interactive)
2588   (unless no-generated-headers
2589     (save-excursion
2590       (save-restriction
2591         (widen)
2592         (gnus-inews-narrow-to-headers)
2593         (gnus-inews-insert-headers gnus-required-mail-headers)
2594         (gnus-inews-remove-empty-headers))))
2595   (widen)
2596   ;; Remove the header separator.
2597   (goto-char (point-min))
2598   (and (re-search-forward
2599         (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2600        (replace-match "" t t))
2601   ;; Run final inews hooks.  This hook may do FCC.
2602   (run-hooks 'gnus-inews-article-hook)
2603   (gnus-inews-do-gcc)
2604   (nnheader-narrow-to-headers)
2605   (nnheader-remove-header "^[gf]cc:" t)
2606   (widen)
2607   (goto-char (point-min))
2608   (search-forward "\n\n")
2609   (forward-char -1)
2610   (insert mail-header-separator)
2611   (mail-send)
2612   (run-hooks 'gnus-message-sent-hook))
2613
2614 (defun gnus-inews-modify-mail-mode-map ()
2615   (use-local-map (copy-keymap (current-local-map)))
2616   (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
2617   (local-set-key "\C-c\C-p" 'gnus-put-message)
2618   (local-set-key "\C-c\C-k" 'gnus-kill-message-buffer)
2619   (local-set-key "\C-c\M-d" 'gnus-dissociate-buffer-from-draft)
2620   (local-set-key "\C-c\C-d" 'gnus-associate-buffer-with-draft))
2621
2622 (defun gnus-mail-setup (type &optional to subject in-reply-to cc
2623                              replybuffer actions)
2624   ;; Let posting styles be configured.
2625   (gnus-configure-posting-styles)
2626   (funcall
2627    (cond
2628     ((or 
2629       (eq gnus-mail-method 'mh)
2630       (and (or (eq type 'reply) (eq type 'followup))
2631            (eq gnus-mail-reply-method 'gnus-mail-reply-using-mhe))
2632       (and (eq type 'forward)
2633            (eq gnus-mail-forward-method 'gnus-mail-forward-using-mhe))
2634       (and (eq type 'new) 
2635            (eq gnus-mail-other-window-method 
2636                'gnus-mail-other-window-using-mhe)))
2637      'gnus-mh-mail-setup)
2638     ((or 
2639       (eq gnus-mail-method 'vm)
2640       (and (or (eq type 'reply) (eq type 'followup)) 
2641            (eq gnus-mail-reply-method 'gnus-mail-reply-using-vm))
2642       (and (eq type 'forward)
2643            (eq gnus-mail-forward-method 'gnus-mail-forward-using-vm))
2644       (and (eq type 'new) 
2645            (eq gnus-mail-other-window-method 
2646                'gnus-mail-other-window-using-vm)))
2647      'gnus-vm-mail-setup)
2648     ((or
2649       (eq gnus-mail-method 'sendmail)
2650       (and (or (eq type 'reply) (eq type 'followup)) 
2651            (eq gnus-mail-reply-method 'gnus-mail-reply-using-mail))
2652       (and (eq type 'forward)
2653            (eq gnus-mail-forward-method 'gnus-mail-forward-using-mail))
2654       (and (eq type 'new) 
2655            (eq gnus-mail-other-window-method 
2656                'gnus-mail-other-window-using-mail)))
2657      'gnus-sendmail-mail-setup)
2658     (t
2659      (cond ((or (eq type 'reply) (eq type 'followup))
2660             gnus-mail-reply-method)
2661            ((eq type 'forward)
2662             gnus-mail-forward-method)
2663            ((eq type 'new) 
2664             gnus-mail-other-window-method))))
2665    to subject in-reply-to cc replybuffer actions)
2666   ;; Associate this mail buffer with the draft group.
2667   (gnus-enter-buffer-into-draft))
2668
2669 (defun gnus-sendmail-mail-setup (to subject in-reply-to cc replybuffer actions)
2670   (mail-mode)
2671   (mail-setup to subject nil cc replybuffer actions)
2672   (gnus-inews-set-point)
2673   (gnus-inews-modify-mail-mode-map))
2674   
2675 ;;; Gcc handling.
2676
2677 ;; Do Gcc handling, which copied the message over to some group. 
2678 (defun gnus-inews-do-gcc (&optional gcc)
2679   (save-excursion
2680     (save-restriction
2681       (nnheader-narrow-to-headers)
2682       (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
2683             (cur (current-buffer))
2684             end groups group method)
2685         (when gcc
2686           (nnheader-remove-header "gcc")
2687           (widen)
2688           (setq groups (gnus-tokenize-header gcc " ,"))
2689           ;; Copy the article over to some group(s).
2690           (while (setq group (pop groups))
2691             (gnus-check-server 
2692              (setq method
2693                    (cond ((and (null (gnus-get-info group))
2694                                (eq (car gnus-message-archive-method)
2695                                    (car (gnus-group-method-name group))))
2696                           ;; If the group doesn't exist, we assume
2697                           ;; it's an archive group...
2698                           gnus-message-archive-method)
2699                          (t (gnus-find-method-for-group group)))))
2700             (unless (gnus-request-group group t method)
2701               (gnus-request-create-group group method))
2702             (gnus-check-server method)
2703             (save-excursion
2704               (nnheader-set-temp-buffer " *acc*")
2705               (insert-buffer-substring cur)
2706               (unless (condition-case ()
2707                           (gnus-request-accept-article group t method)
2708                         (error nil))
2709                 (gnus-message 1 "Couldn't store article in group %s: %s" 
2710                               group (gnus-status-message method))
2711                 (sit-for 2))
2712               (kill-buffer (current-buffer)))))))))
2713
2714 (defun gnus-inews-insert-bfcc ()
2715   "Insert Bcc and Fcc headers."
2716   (save-excursion
2717     ;; Handle author copy using BCC field.
2718     (when (and gnus-mail-self-blind
2719                (not (mail-fetch-field "bcc")))
2720       (mail-position-on-field "Bcc")
2721       (insert (if (stringp gnus-mail-self-blind)
2722                   gnus-mail-self-blind
2723                 (user-login-name))))
2724     ;; Handle author copy using FCC field.
2725     (when gnus-author-copy
2726       (mail-position-on-field "Fcc")
2727       (insert gnus-author-copy))))
2728
2729 (defun gnus-inews-insert-gcc ()
2730   "Insert Gcc headers based on `gnus-outgoing-message-group'."
2731   (save-excursion
2732     (save-restriction
2733       (gnus-inews-narrow-to-headers)
2734       (let* ((group gnus-outgoing-message-group)
2735              (gcc (cond 
2736                    ((gnus-functionp group)
2737                     (funcall group))
2738                    ((or (stringp group) (list group))
2739                     group))))
2740         (when gcc
2741           (insert "Gcc: "
2742                   (if (stringp gcc) gcc
2743                     (mapconcat 'identity gcc " "))
2744                   "\n"))))))
2745
2746 (defun gnus-inews-insert-archive-gcc ()
2747   "Insert the Gcc to say where the article is to be archived."
2748   (let* ((var gnus-message-archive-group)
2749          result
2750          (groups
2751           (cond 
2752            ((stringp var)
2753             ;; Just a single group.
2754             (list var))
2755            ((null var)
2756             ;; We don't want this.
2757             nil)
2758            ((and (listp var) (stringp (car var)))
2759             ;; A list of groups.
2760             var)
2761            ((gnus-functionp var)
2762             ;; A function.
2763             (funcall var gnus-newsgroup-name))
2764            (t
2765             ;; An alist of regexps/functions/forms.
2766             (while (and var
2767                         (not
2768                          (setq result
2769                                (cond 
2770                                 ((stringp (caar var))
2771                                  ;; Regexp.
2772                                  (when (string-match (caar var)
2773                                                      gnus-newsgroup-name)
2774                                    (cdar var)))
2775                                 ((gnus-functionp (car var))
2776                                  ;; Function.
2777                                  (funcall (car var) gnus-newsgroup-name))
2778                                 (t
2779                                  (eval (car var)))))))
2780               (setq var (cdr var)))
2781             result))))
2782     (when groups
2783       (when (stringp groups)
2784         (setq groups (list groups)))
2785       (save-excursion
2786         (save-restriction
2787           (gnus-inews-narrow-to-headers)
2788           (goto-char (point-max))
2789           (insert "Gcc: ")
2790           (while groups
2791             (insert (gnus-group-prefixed-name 
2792                      (pop groups) gnus-message-archive-method))
2793             (insert " "))
2794           (insert "\n"))))))
2795
2796 ;;; Handling rejected (and postponed) news.
2797
2798 (defun gnus-draft-group ()
2799   "Return the name of the draft group."
2800   (gnus-group-prefixed-name 
2801    (file-name-nondirectory gnus-draft-group-directory)
2802    (list 'nndraft gnus-draft-group-directory)))
2803
2804 (defun gnus-make-draft-group ()
2805   "Make the draft group or die trying."
2806   (let* ((method (` (nndraft "private" 
2807                              (nndraft-directory 
2808                               (, gnus-draft-group-directory)))))
2809          (group (gnus-draft-group)))
2810     (or (gnus-gethash group gnus-newsrc-hashtb)
2811         (gnus-group-make-group (gnus-group-real-name group) method)
2812         (error "Can't create the draft group"))
2813     (gnus-check-server method)
2814     group))
2815
2816 (defun gnus-associate-buffer-with-draft (&optional generate silent)
2817   "Enter the current buffer into the draft group."
2818   (interactive)
2819   (when (gnus-request-accept-article (gnus-make-draft-group) t)
2820     (unless silent
2821       ;; This function does the proper marking of articles.
2822       (gnus-mail-send-and-exit 'dont-send))
2823     (set-buffer-modified-p nil)))
2824
2825 (defun gnus-enter-buffer-into-draft ()
2826   (when gnus-use-draft
2827     (save-excursion
2828       ;; Make sure the draft group exists.
2829       (gnus-make-draft-group)
2830       ;; Associate the buffer with the draft group.
2831       (let ((article (gnus-request-associate-buffer (gnus-draft-group))))
2832         ;; Arrange for deletion of the draft after successful sending.
2833         (make-local-variable 'gnus-message-sent-hook)
2834         (setq gnus-message-sent-hook
2835               (list
2836                `(lambda ()
2837                   (let ((gnus-verbose-backends nil))
2838                     (gnus-request-expire-articles 
2839                      (quote ,(list article))
2840                      ,(gnus-draft-group) t)))))))))
2841
2842 (defun gnus-dissociate-buffer-from-draft ()
2843   "Disable auto-saving and association to the draft group of the current buffer."
2844   (interactive)
2845   (run-hooks 'gnus-message-sent-hook)
2846   (setq buffer-file-name nil)
2847   (setq buffer-auto-save-file-name nil))
2848
2849 (defun gnus-summary-send-draft ()
2850   "Enter a mail/post buffer to edit and send the draft."
2851   (interactive)
2852   (gnus-set-global-variables)
2853   (unless (equal gnus-newsgroup-name (gnus-draft-group))
2854     (error "This function can only be used in the draft buffer"))
2855   (let (buf point)
2856     (if (not (setq buf (gnus-request-restore-buffer 
2857                         (gnus-summary-article-number) gnus-newsgroup-name)))
2858         (error "Couldn't restore the article")
2859       (setq point (point))
2860       (switch-to-buffer buf)
2861       (gnus-inews-modify-mail-mode-map)
2862       (when (eq major-mode 'news-reply-mode)
2863         (local-set-key "\C-c\C-c" 'gnus-inews-news))
2864       (gnus-enter-buffer-into-draft) 
2865       ;; Insert the separator.
2866       (goto-char (point-min))
2867       (search-forward "\n\n")
2868       (forward-char -1)
2869       (insert mail-header-separator)
2870       ;; Configure windows.
2871       (let ((gnus-draft-buffer (current-buffer)))
2872         (gnus-configure-windows 'draft)
2873         (goto-char (point))))))
2874   
2875 (defun gnus-configure-posting-styles ()
2876   "Configure posting styles according to `gnus-posting-styles'."
2877   (let ((styles gnus-posting-styles)
2878         (gnus-newsgroup-name (or gnus-newsgroup-name ""))
2879         style match variable attribute value value-value)
2880     ;; Go through all styles and look for matches.
2881     (while styles
2882       (setq style (pop styles)
2883             match (pop style))
2884       (when (cond ((stringp match)
2885                    ;; Regexp string match on the group name.
2886                    (string-match match gnus-newsgroup-name))
2887                   ((or (symbolp match)
2888                        (gnus-functionp match))
2889                    (cond ((gnus-functionp match)
2890                           ;; Function to be called.
2891                           (funcall match))
2892                          ((boundp match)
2893                           ;; Variable to be checked.
2894                           (symbol-value match))))
2895                   ((listp match)
2896                    ;; This is a form to be evaled.
2897                    (eval match)))
2898         ;; We have a match, so we set the variables.
2899         (while style
2900           (setq attribute (pop style)
2901                 value (cdr attribute))
2902           ;; We find the variable that is to be modified.
2903           (if (and (not (stringp (car attribute)))
2904                    (not (setq variable (cdr (assq (car attribute) 
2905                                                   gnus-posting-style-alist)))))
2906               (message "Couldn't find attribute %s" (car attribute))
2907             ;; We set the variable.
2908             (setq value-value
2909                   (cond ((stringp value)
2910                          value)
2911                         ((or (symbolp value)
2912                              (gnus-functionp value))
2913                          (cond ((gnus-functionp value)
2914                                 (funcall value))
2915                                ((boundp value)
2916                                 (symbol-value value))))
2917                         ((listp value)
2918                          (eval value))))
2919             (if variable
2920                 (progn
2921                   ;; This is an ordinary variable.
2922                   (make-local-variable variable)
2923                   (set variable value-value))
2924               ;; This is a header to be added to the headers when
2925               ;; posting. 
2926               (when value-value
2927                 (make-local-variable gnus-required-headers)
2928                 (make-local-variable gnus-required-mail-headers)
2929                 (push (cons (car attribute) value-value) 
2930                       gnus-required-headers)
2931                 (push (cons (car attribute) value-value) 
2932                       gnus-required-mail-headers)))))))))
2933
2934 (defun gnus-inews-set-point ()
2935   "Move point to where the user probably wants to find it."
2936   (gnus-inews-narrow-to-headers)
2937   (cond 
2938    ((re-search-forward "^[^:]+:[ \t]*$" nil t)
2939     (search-backward ":" )
2940     (widen)
2941     (forward-char 2))
2942    (t
2943     (goto-char (point-max))
2944     (widen)
2945     (forward-line 1)
2946     (unless (looking-at "$")
2947       (forward-line 2)))))
2948   
2949 ;;; Allow redefinition of functions.
2950
2951 (gnus-ems-redefine)
2952
2953 (provide 'gnus-msg)
2954
2955 ;;; gnus-msg.el ends here