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