*** 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                    (current-buffer)))
1022           nil)))))
1023
1024 (defun gnus-tokenize-header (header &optional separator)
1025   "Split HEADER into a list of header elements.
1026 \",\" is used as the separator."
1027   (let* ((beg 0)
1028          (separator (or separator ","))
1029          (regexp
1030           (format "[ \t]*\\([^%s]+\\)?\\([%s]+\\|\\'\\)" separator separator))
1031          elems)
1032     (while (and (string-match regexp header beg)
1033                 (< beg (length header)))
1034       (when (match-beginning 1)
1035         (push (match-string 1 header) elems))
1036       (setq beg (match-end 0)))
1037     (nreverse elems)))
1038
1039 (defun gnus-article-checksum ()
1040   (let ((sum 0))
1041     (save-excursion
1042       (while (not (eobp))
1043         (setq sum (logxor sum (following-char)))
1044         (forward-char 1)))
1045     sum))
1046
1047 ;; Returns non-nil if this type is not to be checked.
1048 (defun gnus-check-before-posting (type)
1049   (not 
1050    (or (not gnus-check-before-posting)
1051        (if (listp gnus-check-before-posting)
1052            (memq type gnus-check-before-posting)
1053          t))))
1054
1055 (defun gnus-cancel-news ()
1056   "Cancel an article you posted."
1057   (interactive)
1058   (if (or gnus-expert-user
1059           (gnus-yes-or-no-p "Do you really want to cancel this article? "))
1060       (let ((from nil)
1061             (newsgroups nil)
1062             (message-id nil)
1063             (distribution nil))
1064         (or (gnus-news-group-p gnus-newsgroup-name)
1065             (error "This backend does not support canceling"))
1066         (save-excursion
1067           ;; Get header info. from original article.
1068           (save-restriction
1069             (gnus-article-show-all-headers)
1070             (goto-char (point-min))
1071             (search-forward "\n\n" nil 'move)
1072             (narrow-to-region (point-min) (point))
1073             (setq from (mail-fetch-field "from"))
1074             (setq newsgroups (mail-fetch-field "newsgroups"))
1075             (setq message-id (mail-fetch-field "message-id"))
1076             (setq distribution (mail-fetch-field "distribution")))
1077           ;; Verify if the article is absolutely user's by comparing
1078           ;; user id with value of its From: field.
1079           (if (not
1080                (string-equal
1081                 (downcase (gnus-mail-strip-quoted-names from))
1082                 (downcase (gnus-mail-strip-quoted-names
1083                            (gnus-inews-user-name)))))
1084               (progn
1085                 (ding) (gnus-message 3 "This article is not yours.")
1086                 nil)
1087             ;; Make control article.
1088             (set-buffer (get-buffer-create " *Gnus-canceling*"))
1089             (buffer-disable-undo (current-buffer))
1090             (erase-buffer)
1091             (insert "Newsgroups: " newsgroups "\n"
1092                     "From: " (gnus-inews-user-name) "\n"
1093                     "Subject: cancel " message-id "\n"
1094                     "Control: cancel " message-id "\n"
1095                     (if distribution
1096                         (concat "Distribution: " distribution "\n")
1097                       "")
1098                     mail-header-separator "\n"
1099                     "This is a cancel message from " from ".\n")
1100             ;; Send the control article to NNTP server.
1101             (gnus-message 5 "Canceling your article...")
1102             (prog1
1103                 (if (funcall gnus-inews-article-function)
1104                     (gnus-message 5 "Canceling your article...done")
1105                   (progn
1106                     (ding) 
1107                     (gnus-message 1 "Cancel failed; %s" 
1108                                   (gnus-status-message gnus-newsgroup-name))
1109                     nil)
1110                   t)
1111               ;; Kill the article buffer.
1112               (kill-buffer (current-buffer))))))))
1113
1114 (defun gnus-inews-remove-signature ()
1115   "Remove the signature from the text between point and mark.
1116 The text will also be indented the normal way.
1117 This function can be used in `mail-citation-hook', for instance."
1118   (save-excursion
1119     (let ((start (point))
1120           mark)
1121     (if (not (re-search-forward gnus-signature-separator (mark t) t))
1122         ;; No signature here, so we just indent the cited text.
1123         (mail-indent-citation)
1124       ;; Find the last non-empty line.
1125       (forward-line -1)
1126       (while (looking-at "[ \t]*$")
1127         (forward-line -1))
1128       (forward-line 1)
1129       (setq mark (set-marker (make-marker) (point)))
1130       (goto-char start)
1131       (mail-indent-citation)
1132       ;; Enable undoing the deletion.
1133       (undo-boundary)
1134       (delete-region mark (mark t))
1135       (set-marker mark nil)))))
1136
1137 \f
1138
1139 ;; Dummy to avoid byte-compile warning.
1140 (defvar nnspool-rejected-article-hook)
1141
1142 (defun gnus-inews-article (&optional use-group-method)
1143   "Post an article in current buffer using NNTP protocol."
1144   (let ((artbuf (current-buffer))
1145         gcc result)
1146     (widen)
1147     (goto-char (point-max))
1148     ;; Require a newline at the end of the buffer since inews may
1149     ;; append a .signature.
1150     (or (= (preceding-char) ?\n)
1151         (insert ?\n))
1152     ;; Prepare article headers.  All message body such as signature
1153     ;; must be inserted before Lines: field is prepared.
1154     (save-restriction
1155       (gnus-inews-narrow-to-headers)
1156       ;; Fix some headers.
1157       (gnus-inews-cleanup-headers)
1158       ;; Remove some headers.
1159       (gnus-inews-remove-headers)
1160       ;; Insert some headers.
1161       (gnus-inews-insert-headers)
1162       ;; Let the user do all of the above.
1163       (run-hooks 'gnus-inews-article-header-hook)
1164       ;; Copy the Gcc header, if any.
1165       (setq gcc (mail-fetch-field "gcc"))
1166       (widen))
1167     ;; Check whether the article is a good Net Citizen.
1168     (if (not (gnus-inews-check-post))
1169         ;; Aber nein!
1170         'illegal
1171       ;; We fudge a hook for nnspool.
1172       (setq nnspool-rejected-article-hook
1173             `((lambda ()
1174                 (condition-case ()
1175                     (save-excursion
1176                       (set-buffer ,(buffer-name))
1177                       (gnus-associate-buffer-with-draft nil 'silent))
1178                   (error 
1179                    (ding)
1180                    (gnus-message 
1181                     1 "Couldn't enter rejected article into draft group"))))))
1182                                    
1183       ;; Looks ok, so we do the nasty.
1184       (save-excursion
1185         ;; This hook may insert a signature.
1186         (save-excursion
1187           (goto-char (point-min))
1188           (let ((gnus-newsgroup-name (or (mail-fetch-field "newsgroups")
1189                                          gnus-newsgroup-name)))
1190             (run-hooks 'gnus-prepare-article-hook)))
1191         ;; Send off copies using mail, if that is wanted.
1192         (gnus-inews-send-mail-copy)
1193         ;; Remove more headers.
1194         (gnus-inews-remove-headers-after-mail)
1195         ;; Copy the article over to a temp buffer.
1196         (nnheader-set-temp-buffer " *Gnus-posting*")
1197         (insert-buffer-substring artbuf)
1198         ;; Remove the header separator.
1199         (goto-char (point-min))
1200         (re-search-forward
1201          (concat "^" (regexp-quote mail-header-separator) "$"))
1202         (replace-match "" t t)
1203         ;; Remove X- prefixes to headers.
1204         (gnus-inews-dex-headers)
1205         ;; Run final inews hooks.  This hook may do FCC.
1206         ;; The article must be saved before being posted because
1207         ;; `gnus-request-post' modifies the buffer.
1208         (run-hooks 'gnus-inews-article-hook)
1209         ;; Copy the article over to some group, possibly.
1210         (and gcc (gnus-inews-do-gcc gcc))
1211         ;; Post the article.
1212         (setq result (gnus-request-post
1213                       (gnus-post-method gnus-newsgroup-name use-group-method)))
1214         (kill-buffer (current-buffer)))
1215       (run-hooks 'gnus-message-sent-hook)
1216       ;; If the posting was unsuccessful (that it, it was rejected) we
1217       ;; put it into the draft group.
1218       (or result (gnus-associate-buffer-with-draft))
1219       result)))
1220
1221 (defun gnus-inews-cleanup-headers ()
1222   ;; Remove empty lines in the header.
1223   (save-restriction
1224     (gnus-inews-narrow-to-headers)
1225     (while (re-search-forward "^[ \t]*\n" nil t)
1226       (replace-match "" t t)))
1227
1228   ;; Correct newsgroups field: change sequence of spaces to comma and 
1229   ;; eliminate spaces around commas.  Eliminate imbedded line breaks.
1230   (goto-char (point-min))
1231   (when (re-search-forward "^Newsgroups: +" nil t)
1232     (save-restriction
1233       (narrow-to-region
1234        (point)
1235        (if (re-search-forward "^[^ \t]" nil t)
1236            (match-beginning 0)
1237          (forward-line 1)
1238          (point)))
1239       (goto-char (point-min))
1240       (while (re-search-forward "\n[ \t]+" nil t)
1241         (replace-match " " t t))        ;No line breaks (too confusing)
1242       (goto-char (point-min))
1243       (while (re-search-forward "[ \t\n]*,[ \t\n]*\\|[ \t]+" nil t)
1244         (replace-match "," t t))
1245       (goto-char (point-min))
1246       ;; Remove trailing commas.
1247       (when (re-search-forward ",+$" nil t)
1248         (replace-match "" t t))))
1249  
1250   ;; Added by Per Abrahamsen <abraham@iesd.auc.dk>.
1251   ;; Help save the the world!
1252   (unless gnus-expert-user
1253     (let ((newsgroups (mail-fetch-field "newsgroups"))
1254           (followup-to (mail-fetch-field "followup-to"))
1255           to)
1256       (when (and newsgroups (string-match "," newsgroups)
1257                  (not followup-to)
1258                  (not
1259                   (zerop
1260                    (length
1261                     (setq to (completing-read 
1262                               "Followups to: (default all groups) " 
1263                               (mapcar (lambda (g) (list g))
1264                                       (cons "poster" 
1265                                             (gnus-tokenize-header 
1266                                              newsgroups)))))))))
1267         (goto-char (point-min))
1268         (insert "Followup-To: " to "\n"))))
1269
1270   ;; Cleanup Followup-To.
1271   (goto-char (point-min))
1272   (when (search-forward-regexp "^Followup-To: +" nil t)
1273     (save-restriction
1274       (narrow-to-region
1275        (point)
1276        (if (re-search-forward "^[^ \t]" nil 'end)
1277            (match-beginning 0)
1278          (point-max)))
1279       (goto-char (point-min))
1280       ;; No line breaks (too confusing)
1281       (while (re-search-forward "\n[ \t]+" nil t )
1282         (replace-match " " t ))
1283       (goto-char (point-min))
1284       (while (re-search-forward "[ \t\n]*,[ \t\n]*\\|[ \t]+" nil t)
1285         (replace-match "," t 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         (goto-char (point-max)))
1817       (insert mail-header-separator)
1818       ;; Rename all old ("Also-")Resent headers.
1819       (while (re-search-backward "^\\(Also-\\)?Resent-" beg t)
1820         (beginning-of-line)
1821         (insert "Also-"))
1822       ;; Send it.
1823       (mail-send)
1824       (kill-buffer (current-buffer)))))
1825
1826 (defun gnus-summary-post-forward ()
1827   "Forward the current article to a newsgroup."
1828   (interactive)
1829   (gnus-summary-mail-forward t))
1830
1831 (defvar gnus-nastygram-message 
1832   "The following article was inappropriately posted to %s.\n"
1833   "Format string to insert in nastygrams.
1834 The current group name will be inserted at \"%s\".")
1835
1836 (defun gnus-summary-mail-nastygram (n)
1837   "Send a nastygram to the author of the current article."
1838   (interactive "P")
1839   (if (or gnus-expert-user
1840           (gnus-y-or-n-p 
1841            "Really send a nastygram to the author of the current article? "))
1842       (let ((group gnus-newsgroup-name))
1843         (gnus-summary-reply-with-original n)
1844         (set-buffer gnus-mail-buffer)
1845         (insert (format gnus-nastygram-message group))
1846         (gnus-mail-send-and-exit))))
1847
1848 (defun gnus-summary-mail-other-window ()
1849   "Compose mail in other window.
1850 Customize the variable `gnus-mail-other-window-method' to use another
1851 mailer."
1852   (interactive)
1853   (gnus-set-global-variables)
1854   (gnus-new-mail
1855    ;; We might want to prompt here.
1856    (when (and gnus-interactive-post
1857               (not gnus-expert-user))
1858      (read-string "To: ")))
1859   (gnus-configure-windows 'summary-mail 'force))
1860
1861 (defun gnus-new-mail (&optional to)
1862   (let (subject)
1863     (when (and gnus-interactive-post
1864                (not gnus-expert-user))
1865       (setq subject (read-string "Subject: ")))
1866     (pop-to-buffer gnus-mail-buffer)
1867     (erase-buffer)
1868     (gnus-mail-setup 'new to subject)
1869     (gnus-inews-insert-gcc)
1870     (gnus-inews-insert-archive-gcc)
1871     (run-hooks 'gnus-mail-hook)))
1872
1873 (defun gnus-new-empty-mail ()
1874   "Create a new, virtually empty mail mode buffer."
1875   (pop-to-buffer gnus-mail-buffer)
1876   (gnus-mail-setup 'new "" ""))
1877
1878 (defun gnus-mail-reply (&optional yank to-address followup)
1879   (save-excursion
1880     (set-buffer gnus-summary-buffer)
1881     (let ((group (gnus-group-real-name gnus-newsgroup-name))
1882           (cur (cons (current-buffer) (cdr gnus-article-current)))
1883           (winconf (current-window-configuration))
1884           from subject date reply-to message-of to cc
1885           references message-id sender follow-to sendto elt new-cc new-to
1886           mct mctdo gnus-warning)
1887       (set-buffer (get-buffer-create gnus-mail-buffer))
1888       (mail-mode)
1889       (if (and (buffer-modified-p)
1890                (> (buffer-size) 0)
1891                (not (gnus-y-or-n-p 
1892                      "Unsent message being composed; erase it? ")))
1893           ()
1894         (erase-buffer)
1895         (save-excursion
1896           (gnus-copy-article-buffer)
1897           (save-restriction
1898             (set-buffer gnus-article-copy)
1899             (nnheader-narrow-to-headers)
1900             (if (not followup)
1901                 ;; This is a regular reply.
1902                 (if (gnus-functionp gnus-reply-to-function)
1903                     (setq follow-to (funcall gnus-reply-to-function group)))
1904               ;; This is a followup.
1905               (if (gnus-functionp gnus-followup-to-function)
1906                   (save-excursion
1907                     (setq follow-to
1908                           (funcall gnus-followup-to-function group)))))
1909             (setq from (mail-fetch-field "from"))
1910             (setq date (or (mail-fetch-field "date") 
1911                            (mail-header-date gnus-current-headers)))
1912             (setq message-of (gnus-message-of from date))
1913             (setq sender (mail-fetch-field "sender"))
1914             (setq subject (or (mail-fetch-field "subject") "none"))
1915             ;; Remove any (buggy) Re:'s that are present and make a
1916             ;; proper one.
1917             (and (string-match "^[ \t]*[Re][Ee]:[ \t]*" subject)
1918                  (setq subject (substring subject (match-end 0))))
1919             (setq subject (concat "Re: " subject))
1920             (setq to (mail-fetch-field "to"))
1921             (setq cc (mail-fetch-field "cc"))
1922             (setq mct (mail-fetch-field "mail-copies-to"))
1923             (setq reply-to
1924                   (unless (gnus-group-get-parameter group 'broken-reply-to)
1925                     (mail-fetch-field "reply-to")))
1926             (setq references (mail-fetch-field "references"))
1927             (setq message-id (mail-fetch-field "message-id"))
1928             (when (and (setq gnus-warning (mail-fetch-field "gnus-warning"))
1929                        (string-match "<[^>]+>" gnus-warning))
1930               (setq message-id (match-string 0 gnus-warning)))
1931             
1932             (setq mctdo (and mct (not (equal mct "never"))))
1933             (when (and mct (string= (downcase mct) "always"))
1934               (setq mct (or reply-to from)))
1935
1936             (if (not (and followup (not to-address)))
1937                 (setq new-to (or reply-to from)
1938                       new-cc 
1939                       (if (and mctdo
1940                                (not (string= 
1941                                      (gnus-mail-strip-quoted-names mct)
1942                                      (gnus-mail-strip-quoted-names
1943                                       (or to-address 
1944                                           (if (and follow-to 
1945                                                    (not (stringp follow-to)))
1946                                               sendto
1947                                             (or follow-to new-to
1948                                                 sender "")))))))
1949                           mct))
1950               (let (ccalist)
1951                 (save-excursion
1952                   (gnus-set-work-buffer)
1953                   (unless (equal mct "never")
1954                     (insert (or reply-to from "")))
1955                   (insert 
1956                    (if (bolp) "" ", ") (or to "")
1957                    (if (or (not mct) (not mctdo)) ""
1958                      (concat (if (bolp) "" ", ") mct))
1959                    (if cc (concat (if (bolp) "" ", ") cc) ""))
1960                   ;; Remove addresses that match `rmail-dont-reply-to-names'. 
1961                   (insert (prog1 (rmail-dont-reply-to (buffer-string))
1962                             (erase-buffer)))
1963                   (goto-char (point-min))
1964                   (setq ccalist
1965                         (mapcar
1966                          (lambda (addr)
1967                            (cons (gnus-mail-strip-quoted-names addr) addr))
1968                          (nreverse (gnus-mail-parse-comma-list))))
1969                   (let ((s ccalist))
1970                     (while s
1971                       (setq ccalist (delq (assoc (car (pop s)) s) ccalist)))))
1972                 (setq new-to (cdr (pop ccalist)))
1973                 (setq new-cc 
1974                       (mapconcat 
1975                        (lambda (addr) (cdr addr))
1976                        ccalist ", "))))
1977             (widen)))
1978
1979         (setq news-reply-yank-from (or from "(nobody)"))
1980         (setq news-reply-yank-message-id
1981               (or message-id "(unknown Message-ID)"))
1982
1983         ;; Gather the "to" addresses out of the follow-to list and remove
1984         ;; them as we go.
1985         (if (and follow-to (listp follow-to))
1986             (while (setq elt (assoc "To" follow-to))
1987               (setq sendto (concat sendto (and sendto ", ") (cdr elt)))
1988               (setq follow-to (delq elt follow-to))))
1989
1990         (gnus-mail-setup 
1991          (if followup 'followup 'reply)
1992          (or to-address 
1993              (if (and follow-to (not (stringp follow-to))) sendto
1994                (or follow-to new-to sender "")))
1995          subject message-of
1996          (if (zerop (length new-cc)) nil new-cc)
1997          gnus-article-copy)
1998
1999         (make-local-variable 'gnus-article-reply)
2000         (setq gnus-article-reply cur)
2001         (make-local-variable 'gnus-prev-winconf)
2002         (setq gnus-prev-winconf winconf)
2003         (make-local-variable 'gnus-reply-subject)
2004         (setq gnus-reply-subject subject)
2005         (make-local-variable 'gnus-in-reply-to)
2006         (setq gnus-in-reply-to message-of)
2007
2008         (auto-save-mode auto-save-default)
2009         (gnus-inews-insert-gcc)
2010         (gnus-inews-insert-archive-gcc group)
2011
2012         (when (and follow-to (listp follow-to))
2013           (let (beg)
2014             (gnus-inews-narrow-to-headers)
2015             (re-search-forward "^To:" nil t)
2016             (beginning-of-line)
2017             (forward-line 1)
2018             (setq beg (point))
2019             ;; Insert the rest of the Follow-To headers.
2020             (while follow-to
2021               (goto-char (point-min))
2022               (if (not (re-search-forward 
2023                         (concat "^" (caar follow-to) ": *") nil t))
2024                   (progn
2025                     (goto-char beg)
2026                     (insert (caar follow-to) ": " (cdar follow-to) "\n"))
2027                 (unless (eolp)
2028                   (insert ", "))
2029                 (insert (cdar follow-to)))
2030               (setq follow-to (cdr follow-to)))
2031             (widen)))
2032         (nnheader-insert-references references message-id)
2033
2034         ;; Now the headers should be ok, so we do the yanking.
2035         (goto-char (point-min))
2036         (re-search-forward
2037          (concat "^" (regexp-quote mail-header-separator) "$"))
2038         (forward-line 1)
2039         (if (not yank)
2040             (gnus-configure-windows 'reply 'force)
2041           (let ((last (point))
2042                 end)
2043             (if (not (listp yank))
2044                 (progn
2045                   ;; Just a single article being yanked.
2046                   (save-excursion
2047                     (mail-yank-original nil))
2048                   (or mail-yank-hooks mail-citation-hook
2049                       (run-hooks 'news-reply-header-hook)))
2050               (while yank
2051                 (save-window-excursion
2052                   (set-buffer gnus-summary-buffer)
2053                   (gnus-summary-select-article nil nil nil (car yank))
2054                   (gnus-summary-remove-process-mark (car yank)))
2055                 (save-excursion
2056                   (setq end (point))
2057                   (gnus-copy-article-buffer)
2058                   (mail-yank-original nil)
2059                   (save-restriction
2060                     (narrow-to-region (point-min) (point))
2061                     (goto-char (mark t))
2062                     (let ((news-reply-yank-from
2063                            (save-excursion 
2064                              (set-buffer gnus-article-buffer)
2065                              (or (mail-fetch-field "from") "(nobody)")))
2066                           (news-reply-yank-message-id
2067                            (save-excursion 
2068                              (set-buffer gnus-article-buffer)
2069                              (or (mail-fetch-field "message-id")
2070                                  "(unknown Message-ID)"))))
2071                       (or mail-yank-hooks mail-citation-hook
2072                           (run-hooks 'news-reply-header-hook))
2073                       (setq end (point-max)))))
2074                 (goto-char end)
2075                 (setq yank (cdr yank))))
2076             (goto-char last))
2077           (forward-line 2)
2078           (gnus-configure-windows 'reply-yank 'force))
2079         (run-hooks 'gnus-mail-hook)
2080         ;; Mark this buffer as unchanged.
2081         (set-buffer-modified-p nil)))))
2082
2083 (defun gnus-mail-parse-comma-list ()
2084   (let (accumulated
2085         beg)
2086     (skip-chars-forward " ")
2087     (while (not (eobp))
2088       (setq beg (point))
2089       (skip-chars-forward "^,")
2090       (while (zerop
2091               (save-excursion 
2092                 (save-restriction
2093                   (let ((i 0))
2094                     (narrow-to-region beg (point))
2095                     (goto-char beg)
2096                     (logand (progn
2097                               (while (search-forward "\"" nil t)
2098                                 (incf i))
2099                               (if (zerop i) 2 i)) 2)))))
2100         (skip-chars-forward ",")
2101         (skip-chars-forward "^,"))
2102       (skip-chars-backward " ")
2103       (setq accumulated
2104             (cons (buffer-substring beg (point))
2105                   accumulated))
2106       (skip-chars-forward "^,")
2107       (skip-chars-forward ", "))
2108     accumulated))
2109
2110 (defun gnus-new-news (&optional group inhibit-prompt)
2111   "Set up a *Post Gnus* buffer that points to GROUP.
2112 If INHIBIT-PROMPT, never prompt for a Subject."
2113   (let ((winconf (current-window-configuration))
2114         subject)
2115     (when (and gnus-interactive-post
2116                (not inhibit-prompt)
2117                (not gnus-expert-user))
2118       (setq subject (read-string "Subject: ")))
2119     (pop-to-buffer gnus-post-news-buffer)  
2120     (erase-buffer)
2121     (news-reply-mode)
2122     ;; Let posting styles be configured.
2123     (gnus-configure-posting-styles)
2124     (news-setup nil subject nil (and group (gnus-group-real-name group)) nil)
2125     ;; Associate this buffer with the draft group.
2126     (gnus-enter-buffer-into-draft)
2127     (goto-char (point-min))
2128
2129     (unless (re-search-forward 
2130              (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2131       (goto-char (point-max)))
2132     (insert "\n\n")
2133
2134     (gnus-inews-insert-bfcc)
2135     (gnus-inews-insert-gcc)
2136     (gnus-inews-insert-archive-gcc group)
2137     (gnus-inews-insert-signature)
2138     (and gnus-post-prepare-function
2139          (gnus-functionp gnus-post-prepare-function)
2140          (funcall gnus-post-prepare-function group))
2141     (run-hooks 'gnus-post-prepare-hook)
2142     (gnus-inews-set-point)
2143     (make-local-variable 'gnus-prev-winconf)
2144     (setq gnus-prev-winconf winconf)
2145     (gnus-inews-modify-mail-mode-map)
2146     (local-set-key "\C-c\C-c" 'gnus-inews-news)))
2147
2148 (defun gnus-news-followup (&optional yank group)
2149   (save-excursion
2150     (set-buffer gnus-summary-buffer)
2151     (if (not (or (not gnus-novice-user)
2152                  gnus-expert-user
2153                  (gnus-y-or-n-p
2154                   "Are you sure you want to post to all of USENET? ")))
2155         ()
2156       (let ((group (gnus-group-real-name (or group gnus-newsgroup-name)))
2157             (cur (cons (current-buffer) (cdr gnus-article-current)))
2158             (winconf (current-window-configuration))
2159             from subject date message-of
2160             references message-id follow-to sendto elt 
2161             followup-to distribution newsgroups gnus-warning)
2162         (set-buffer (get-buffer-create gnus-post-news-buffer))
2163         (news-reply-mode)
2164         ;; Associate this buffer with the draft group.
2165         (gnus-enter-buffer-into-draft)
2166         (if (and (buffer-modified-p)
2167                  (> (buffer-size) 0)
2168                  (not (gnus-y-or-n-p 
2169                        "Unsent message being composed; erase it? ")))
2170             ()
2171           (erase-buffer)
2172           (save-excursion
2173             (gnus-copy-article-buffer)
2174             (save-restriction
2175               (set-buffer gnus-article-copy)
2176               (nnheader-narrow-to-headers)
2177               (if (gnus-functionp gnus-followup-to-function)
2178                   (save-excursion
2179                     (setq follow-to
2180                           (funcall gnus-followup-to-function group))))
2181               (setq from (mail-fetch-field "from"))
2182               (setq date (or (mail-fetch-field "date") 
2183                              (mail-header-date gnus-current-headers)))
2184               (setq message-of (gnus-message-of from date))
2185               (setq subject (or (mail-fetch-field "subject") "none"))
2186               ;; Remove any (buggy) Re:'s that are present and make a
2187               ;; proper one.
2188               (and (string-match "^[ \t]*[Re][Ee]:[ \t]*" subject)
2189                    (setq subject (substring subject (match-end 0))))
2190               (setq subject (concat "Re: " subject))
2191               (setq references (mail-fetch-field "references"))
2192               (setq message-id (mail-fetch-field "message-id"))
2193               (when (and (setq gnus-warning (mail-fetch-field "gnus-warning"))
2194                          (string-match "<[^>]+>" gnus-warning))
2195                 (setq message-id (match-string 0 gnus-warning)))
2196               (setq followup-to (mail-fetch-field "followup-to"))
2197               (setq newsgroups (mail-fetch-field "newsgroups"))
2198               (setq distribution (mail-fetch-field "distribution"))
2199               ;; Remove bogus distribution.
2200               (and (stringp distribution)
2201                    (string-match "world" distribution)
2202                    (setq distribution nil))
2203               (widen)))
2204
2205           (setq news-reply-yank-from (or from "(nobody)"))
2206           (setq news-reply-yank-message-id
2207                 (or message-id "(unknown Message-ID)"))
2208
2209           ;; Gather the "to" addresses out of the follow-to list and remove
2210           ;; them as we go.
2211           (if (and follow-to (listp follow-to))
2212               (while (setq elt (assoc "Newsgroups" follow-to))
2213                 (setq sendto (concat sendto (and sendto ", ") (cdr elt)))
2214                 (setq follow-to (delq elt follow-to))))
2215
2216           ;; Let posting styles be configured.
2217           (gnus-configure-posting-styles)
2218
2219           (news-setup
2220            nil subject nil 
2221            (or sendto 
2222                (if (equal followup-to "poster")
2223                    (or newsgroups group "")
2224                  (and followup-to
2225                       gnus-use-followup-to
2226                       (or (not (eq gnus-use-followup-to 'ask))
2227                           (equal followup-to newsgroups)
2228                           (gnus-y-or-n-p 
2229                            (format "Use Followup-To %s? " followup-to)))
2230                       followup-to))
2231                newsgroups group "")
2232            gnus-article-copy)
2233
2234           (make-local-variable 'gnus-article-reply)
2235           (setq gnus-article-reply cur)
2236           (make-local-variable 'gnus-prev-winconf)
2237           (setq gnus-prev-winconf winconf)
2238           (make-local-variable 'gnus-reply-subject)
2239           (setq gnus-reply-subject (mail-header-subject gnus-current-headers))
2240           (make-local-variable 'gnus-in-reply-to)
2241           (setq gnus-in-reply-to message-of)
2242           (when (and followup-to newsgroups)
2243             (make-local-variable 'gnus-newsgroup-followup)
2244             (setq gnus-newsgroup-followup
2245                   (cons newsgroups followup-to)))
2246
2247           (gnus-inews-insert-signature)
2248
2249           (and gnus-post-prepare-function
2250                (gnus-functionp gnus-post-prepare-function)
2251                (funcall gnus-post-prepare-function group))
2252           (run-hooks 'gnus-post-prepare-hook)
2253
2254           (auto-save-mode auto-save-default)
2255           (gnus-inews-modify-mail-mode-map)
2256           (local-set-key "\C-c\C-c" 'gnus-inews-news)
2257
2258           (if (and follow-to (listp follow-to))
2259               (progn
2260                 (goto-char (point-min))
2261                 (and (re-search-forward "^Newsgroups:" nil t)
2262                      (forward-line 1))
2263                 (while follow-to
2264                   (insert (caar follow-to) ": " (cdar follow-to) "\n")
2265                   (setq follow-to (cdr follow-to)))))
2266           
2267           ;; If a distribution existed, we use it.
2268           (if distribution
2269               (progn
2270                 (mail-position-on-field "Distribution")
2271                 (insert distribution)))
2272           
2273           (nnheader-insert-references references message-id)
2274
2275           ;; Handle `gnus-auto-mail-to-author'.
2276           ;; Suggested by Daniel Quinlan <quinlan@best.com>.
2277           ;; Revised to respect Reply-To by Ulrik Dickow <dickow@nbi.dk>.
2278           (let ((to (if (if (eq gnus-auto-mail-to-author 'ask)
2279                             (y-or-n-p "Also send mail to author? ")
2280                           gnus-auto-mail-to-author)
2281                         (or (save-excursion
2282                               (set-buffer gnus-article-copy)
2283                               (unless (gnus-group-get-parameter
2284                                        group 'broken-reply-to)
2285                                 (gnus-fetch-field "reply-to")))
2286                             from)))
2287                 (x-mail (save-excursion
2288                           (set-buffer gnus-article-copy)
2289                           (gnus-fetch-field "x-mail-copy-to"))))
2290             ;; Deny sending copy if there's a negative X-Mail-Copy-To
2291             ;; header. 
2292             (if x-mail
2293                 (if (and (string= x-mail "never")
2294                          (not (eq gnus-auto-mail-to-author 'force)))
2295                     (setq to nil)
2296                   (setq to x-mail)))
2297             ;; Insert a To or Cc header.
2298             (if to
2299                 (if (mail-fetch-field "To")
2300                     (progn
2301                       (beginning-of-line)
2302                       (insert "Cc: " to "\n"))
2303                   (mail-position-on-field "To")
2304                   (insert to))))
2305
2306           (gnus-inews-insert-bfcc)
2307           (gnus-inews-insert-gcc)
2308           (gnus-inews-insert-archive-gcc group)
2309
2310           ;; Now the headers should be ok, so we do the yanking.
2311           (goto-char (point-min))
2312           (re-search-forward
2313            (concat "^" (regexp-quote mail-header-separator) "$"))
2314           (forward-line 1)
2315           (if (not yank)
2316               (progn
2317                 (gnus-configure-windows 'followup 'force)
2318                 (insert "\n\n")
2319                 (forward-line -2))
2320             (let ((last (point))
2321                   end)
2322               (if (not (listp yank))
2323                   (progn
2324                     (save-excursion
2325                       (mail-yank-original nil))
2326                     (or mail-yank-hooks mail-citation-hook
2327                         (run-hooks 'news-reply-header-hook)))
2328                 (while yank
2329                   (save-window-excursion
2330                     (set-buffer gnus-summary-buffer)
2331                     (gnus-summary-select-article nil nil nil (car yank))
2332                     (gnus-summary-remove-process-mark (car yank)))
2333                   (save-excursion
2334                     (gnus-copy-article-buffer)
2335                     (mail-yank-original nil)
2336                     (setq end (set-marker (make-marker) (point))))
2337                   (or mail-yank-hooks mail-citation-hook
2338                       (run-hooks 'news-reply-header-hook))
2339                   (goto-char end)
2340                   (set-marker end nil)
2341                   (setq yank (cdr yank))))
2342               (goto-char last))
2343             (gnus-configure-windows 'followup-yank 'force))
2344         
2345           (make-local-variable 'gnus-article-check-size)
2346           (setq gnus-article-check-size
2347                 (cons (buffer-size) (gnus-article-checksum)))
2348           (gnus-inews-set-point))))))
2349
2350 (defun gnus-message-of (from date)
2351   "Take a FROM and a DATE and return an IN-REPLY-TO."
2352   (when from
2353     (let ((stop-pos 
2354            (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
2355       (concat (if stop-pos (substring from 0 stop-pos) from)
2356               "'s message of " 
2357               (if (or (not date) (string= date ""))
2358                   "(unknown date)" date)))))
2359
2360 (defun gnus-mail-yank-original ()
2361   (interactive)
2362   (save-excursion
2363     (mail-yank-original nil))
2364   (or mail-yank-hooks mail-citation-hook
2365       (run-hooks 'news-reply-header-hook)))
2366
2367 (defun gnus-mail-send-and-exit (&optional dont-send)
2368   "Send the current mail and return to Gnus."
2369   (interactive)
2370   (let* ((reply gnus-article-reply)
2371          (winconf gnus-prev-winconf)
2372          (address-group gnus-add-to-address)
2373          (to-address (and address-group
2374                           (mail-fetch-field "to"))))
2375     (setq gnus-add-to-address nil)
2376     (let ((buffer-file-name nil))
2377       (or dont-send (gnus-mail-send)))
2378     (bury-buffer)
2379     (when (gnus-alive-p)
2380       ;; This mail group doesn't have a `to-list', so we add one
2381       ;; here.  Magic!  
2382       (when to-address
2383         (gnus-group-add-parameter address-group (cons 'to-list to-address)))
2384       (when (and (gnus-buffer-exists-p (car reply))
2385                  (cdr reply))
2386         (save-excursion
2387           (set-buffer (car reply))
2388           (gnus-summary-mark-article-as-replied (cdr reply))))
2389       (and winconf (set-window-configuration winconf)))))
2390
2391 (defun gnus-kill-message-buffer ()
2392   "Kill the current buffer after dissociating it from the draft group."
2393   (interactive)
2394   (when (gnus-y-or-n-p "Dissociate and kill the current buffer? ")
2395     (gnus-dissociate-buffer-from-draft)
2396     (let ((winconf gnus-prev-winconf))
2397       (kill-buffer (current-buffer))
2398       (when winconf 
2399         (set-window-configuration winconf)))))
2400
2401 (defun gnus-put-message ()
2402   "Put the current message in some group and return to Gnus."
2403   (interactive)
2404   (let ((reply gnus-article-reply)
2405         (winconf gnus-prev-winconf)
2406         (group gnus-newsgroup-name))
2407     
2408     (or (and group (not (gnus-group-read-only-p group)))
2409         (setq group (read-string "Put in group: " nil
2410                                  (gnus-writable-groups))))
2411     (and (gnus-gethash group gnus-newsrc-hashtb)
2412          (error "No such group: %s" group))
2413
2414     (save-excursion
2415       (save-restriction
2416         (widen)
2417         (gnus-inews-narrow-to-headers)
2418         (let (gnus-deletable-headers)
2419           (if (eq major-mode 'mail-mode)
2420               (gnus-inews-insert-headers gnus-required-mail-headers)
2421             (gnus-inews-insert-headers)))
2422         (goto-char (point-max))
2423         (insert "Gcc: " group "\n")
2424         (widen)))
2425
2426     (gnus-inews-do-gcc)
2427
2428     (if (get-buffer gnus-group-buffer)
2429         (progn
2430           (if (gnus-buffer-exists-p (car-safe reply))
2431               (progn
2432                 (set-buffer (car reply))
2433                 (and (cdr reply)
2434                      (gnus-summary-mark-article-as-replied 
2435                       (cdr reply)))))
2436           (and winconf (set-window-configuration winconf))))))
2437
2438 (defun gnus-forward-make-subject (buffer)
2439   (save-excursion
2440     (set-buffer buffer)
2441     (concat "[" (if (memq 'mail (assoc (symbol-name 
2442                                         (car (gnus-find-method-for-group 
2443                                               gnus-newsgroup-name)))
2444                                        gnus-valid-select-methods))
2445                     (gnus-fetch-field "From")
2446                   gnus-newsgroup-name)
2447             "] " (or (gnus-fetch-field "Subject") ""))))
2448
2449 (defun gnus-forward-insert-buffer (buffer)
2450   (save-excursion
2451     (save-restriction
2452       ;; Put point where we want it before inserting the forwarded
2453       ;; message. 
2454       (if gnus-signature-before-forwarded-message
2455           (goto-char (point-max))
2456         (goto-char (point-min))
2457         (re-search-forward
2458          (concat "^" (regexp-quote mail-header-separator) "$"))
2459         (forward-line 1))
2460       ;; Narrow to the area we are to insert.
2461       (narrow-to-region (point) (point))
2462       ;; Insert the separators and the forwarded buffer.
2463       (insert gnus-forward-start-separator)
2464       (insert-buffer-substring buffer)
2465       (goto-char (point-max))
2466       (insert gnus-forward-end-separator)
2467       (gnus-set-text-properties (point-min) (point-max) nil)
2468       ;; Remove all unwanted headers.
2469       (goto-char (point-min))
2470       (forward-line 1)
2471       (save-restriction
2472         (narrow-to-region (point) (if (search-forward "\n\n" nil t)
2473                                       (1- (point))
2474                                     (point)))
2475         (goto-char (point-min))
2476         (let ((case-fold-search t))
2477           (delete-non-matching-lines gnus-forward-included-headers))))))
2478
2479 (defun gnus-mail-forward (&optional buffer)
2480   "Forward the current message to another user using mail."
2481   (let* ((forward-buffer (or buffer (current-buffer)))
2482          (winconf (current-window-configuration))
2483          (subject (gnus-forward-make-subject forward-buffer)))
2484     (set-buffer (get-buffer-create gnus-mail-buffer))
2485     (if (and (buffer-modified-p)
2486              (> (buffer-size) 0)
2487              (not (gnus-y-or-n-p 
2488                    "Unsent message being composed; erase it? ")))
2489         ()
2490       (erase-buffer)
2491       (gnus-mail-setup 'forward nil subject)
2492       (make-local-variable 'gnus-prev-winconf)
2493       (setq gnus-prev-winconf winconf)
2494       (gnus-forward-insert-buffer forward-buffer)
2495       (goto-char (point-min))
2496       (re-search-forward "^To: ?" nil t)
2497       (gnus-configure-windows 'mail-forward 'force)
2498       ;; You have a chance to arrange the message.
2499       (run-hooks 'gnus-mail-forward-hook)
2500       (run-hooks 'gnus-mail-hook))))
2501
2502 (defun gnus-forward-using-post (&optional buffer)
2503   (save-excursion
2504     (let* ((forward-buffer (or buffer (current-buffer))) 
2505            (subject (gnus-forward-make-subject forward-buffer))
2506            (gnus-newsgroup-name nil))
2507       (gnus-post-news 'post nil nil nil nil subject)
2508       (save-excursion
2509         (gnus-forward-insert-buffer forward-buffer)
2510         ;; You have a chance to arrange the message.
2511         (run-hooks 'gnus-mail-forward-hook)))))
2512
2513 (defun gnus-mail-other-window-using-mail ()
2514   "Compose mail other window using mail."
2515   (let ((winconf (current-window-configuration)))
2516     (mail-other-window nil nil nil nil nil (get-buffer gnus-article-buffer))
2517     (gnus-inews-modify-mail-mode-map)
2518     (make-local-variable 'gnus-prev-winconf)
2519     (setq gnus-prev-winconf winconf)
2520     (run-hooks 'gnus-mail-hook)
2521     (gnus-configure-windows 'summary-mail 'force)))
2522
2523 (defun gnus-article-mail (yank)
2524   "Send a reply to the address near point.
2525 If YANK is non-nil, include the original article."
2526   (interactive "P")
2527   (let ((address 
2528          (buffer-substring
2529           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
2530           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
2531     (and address
2532          (progn
2533            (switch-to-buffer gnus-summary-buffer)
2534            (gnus-mail-reply yank address)))))
2535
2536 (defun gnus-bug ()
2537   "Send a bug report to the Gnus maintainers."
2538   (interactive)
2539   (let ((winconf (current-window-configuration)))
2540     (delete-other-windows)
2541     (switch-to-buffer "*Gnus Help Bug*")
2542     (erase-buffer)
2543     (insert gnus-bug-message)
2544     (goto-char (point-min))
2545     (pop-to-buffer "*Gnus Bug*")
2546     (erase-buffer)
2547     (mail-mode)
2548     (mail-setup gnus-maintainer nil nil nil nil nil)
2549     (auto-save-mode auto-save-default)
2550     (make-local-variable 'gnus-prev-winconf)
2551     (setq gnus-prev-winconf winconf)
2552     (gnus-inews-modify-mail-mode-map)
2553     (local-set-key "\C-c\C-c" 'gnus-bug-mail-send-and-exit)
2554     (goto-char (point-min))
2555     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2556     (forward-line 1)
2557     (insert (gnus-version) "\n")
2558     (insert (emacs-version))
2559     (insert "\n\n\n\n\n")
2560     (gnus-debug)
2561     (goto-char (point-min))
2562     (search-forward "Subject: " nil t)
2563     (message "")))
2564
2565 (defun gnus-bug-mail-send-and-exit ()
2566   "Send the bug message and exit."
2567   (interactive)
2568   (let ((cur (current-buffer)))
2569     (and (get-buffer "*Gnus Help Bug*")
2570          (kill-buffer "*Gnus Help Bug*"))
2571     (gnus-mail-send-and-exit)
2572     (when (buffer-name cur)
2573       (kill-buffer cur))))
2574
2575 (defun gnus-debug ()
2576   "Attemps to go through the Gnus source file and report what variables have been changed.
2577 The source file has to be in the Emacs load path."
2578   (interactive)
2579   (let ((files '("gnus.el" "gnus-msg.el" "gnus-score.el" "nnmail.el"))
2580         file dirs expr olist sym)
2581     (message "Please wait while we snoop your variables...")
2582     (sit-for 0)
2583     (save-excursion
2584       (set-buffer (get-buffer-create " *gnus bug info*"))
2585       (buffer-disable-undo (current-buffer))
2586       (while files
2587         (erase-buffer)
2588         (setq dirs load-path)
2589         (while dirs
2590           (if (or (not (car dirs))
2591                   (not (stringp (car dirs)))
2592                   (not (file-exists-p 
2593                         (setq file (concat (file-name-as-directory 
2594                                             (car dirs)) (car files))))))
2595               (setq dirs (cdr dirs))
2596             (setq dirs nil)
2597             (insert-file-contents file)
2598             (goto-char (point-min))
2599             (if (not (re-search-forward "^;;* *Internal variables" nil t))
2600                 (message "Malformed sources in file %s" file)
2601               (narrow-to-region (point-min) (point))
2602               (goto-char (point-min))
2603               (while (setq expr (condition-case () 
2604                                     (read (current-buffer)) (error nil)))
2605                 (condition-case ()
2606                     (and (eq (car expr) 'defvar)
2607                          (stringp (nth 3 expr))
2608                          (or (not (boundp (nth 1 expr)))
2609                              (not (equal (eval (nth 2 expr))
2610                                          (symbol-value (nth 1 expr)))))
2611                          (setq olist (cons (nth 1 expr) olist)))
2612                   (error nil))))))
2613         (setq files (cdr files)))
2614       (kill-buffer (current-buffer)))
2615     (when (setq olist (nreverse olist))
2616       (insert "------------------ Environment follows ------------------\n\n"))
2617     (while olist
2618       (if (boundp (car olist))
2619           (condition-case ()
2620               (pp `(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                   (current-buffer))
2628             (error
2629              (format "(setq %s 'whatever)\n" (car olist))))
2630         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
2631       (setq olist (cdr olist)))
2632     (insert "\n\n")
2633     ;; Remove any null chars - they seem to cause trouble for some
2634     ;; mailers. (Byte-compiled output from the stuff above.) 
2635     (goto-char (point-min))
2636     (while (re-search-forward "[\000\200]" nil t)
2637       (replace-match "" t t))))
2638
2639 ;;; Treatment of rejected articles.
2640 ;;; Bounced mail.
2641
2642 (defvar mail-unsent-separator)
2643
2644 (defun gnus-summary-resend-bounced-mail (fetch)
2645   "Re-mail the current message.
2646 This only makes sense if the current message is a bounce message than
2647 contains some mail you have written which has been bounced back to
2648 you.
2649 If FETCH, try to fetch the article that this is a reply to, if indeed
2650 this is a reply."
2651   (interactive "P")
2652   (require 'rmail)
2653   (gnus-summary-select-article t)
2654   ;; Create a mail buffer.
2655   (gnus-new-empty-mail)
2656   (erase-buffer)
2657   (insert-buffer-substring gnus-article-buffer)
2658   (goto-char (point-min))
2659   (or (re-search-forward mail-unsent-separator nil t)
2660       (and (search-forward "\n\n" nil t)
2661            (re-search-forward "^Return-Path:.*\n" nil t)))
2662   ;; We remove everything before the bounced mail.
2663   (delete-region 
2664    (point-min)
2665    (if (re-search-forward "[^ \t]*:" nil t)
2666        (match-beginning 0)
2667      (point)))
2668   (let (references)
2669     (save-excursion
2670       (save-restriction
2671         (nnheader-narrow-to-headers)
2672         (nnheader-remove-header gnus-bounced-headers-junk t)
2673         (setq references (mail-fetch-field "references"))
2674         (goto-char (point-max))
2675         (insert mail-header-separator)))
2676     ;; If there are references, we fetch the article we answered to.  
2677     (and fetch 
2678          references
2679          (string-match "\\(<[^]+>\\)[ \t]*$" references)
2680          (gnus-summary-refer-article 
2681           (substring references (match-beginning 1) (match-end 1)))
2682          (progn
2683            (gnus-summary-show-all-headers)
2684            (gnus-configure-windows 'compose-bounce))))
2685   (goto-char (point-min)))
2686
2687 ;;; Sending mail.
2688
2689 (defun gnus-mail-send (&optional no-generated-headers)
2690   "Send the current buffer as mail.
2691 Headers will be generated before sending."
2692   (interactive)
2693   (unless no-generated-headers
2694     (save-excursion
2695       (save-restriction
2696         (widen)
2697         (gnus-inews-narrow-to-headers)
2698         (gnus-inews-insert-headers gnus-required-mail-headers)
2699         (gnus-inews-remove-empty-headers))))
2700   (widen)
2701   (when (not (gnus-inews-reject-message))
2702     ;; Remove the header separator.
2703     (goto-char (point-min))
2704     (and (re-search-forward
2705           (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2706          (replace-match "" t t))
2707     ;; Run final inews hooks.  This hook may do FCC.
2708     (run-hooks 'gnus-inews-article-hook)
2709     (gnus-inews-do-gcc)
2710     (nnheader-narrow-to-headers)
2711     (nnheader-remove-header "^[gf]cc:" t)
2712     (widen)
2713     (goto-char (point-min))
2714     (search-forward "\n\n")
2715     (forward-char -1)
2716     (insert mail-header-separator)
2717     (mail-send)
2718     (run-hooks 'gnus-message-sent-hook)))
2719
2720 (defun gnus-inews-modify-mail-mode-map ()
2721   (use-local-map (copy-keymap (current-local-map)))
2722   (gnus-local-set-keys
2723    "\C-c\C-c" gnus-mail-send-and-exit
2724    "\C-c\M-\C-p" gnus-put-message
2725    "\C-c\C-q" gnus-kill-message-buffer
2726    "\C-c\M-d" gnus-dissociate-buffer-from-draft
2727    "\C-c\C-d" gnus-associate-buffer-with-draft))
2728
2729 (defun gnus-mail-setup (type &optional to subject in-reply-to cc
2730                              replybuffer actions)
2731   ;; Let posting styles be configured.
2732   (gnus-configure-posting-styles)
2733   (funcall
2734    (cond
2735     ((or 
2736       (eq gnus-mail-method 'mh)
2737       (and (or (eq type 'reply) (eq type 'followup))
2738            (eq gnus-mail-reply-method 'gnus-mail-reply-using-mhe))
2739       (and (eq type 'forward)
2740            (eq gnus-mail-forward-method 'gnus-mail-forward-using-mhe))
2741       (and (eq type 'new) 
2742            (eq gnus-mail-other-window-method 
2743                'gnus-mail-other-window-using-mhe)))
2744      'gnus-mh-mail-setup)
2745     ((or 
2746       (eq gnus-mail-method 'vm)
2747       (and (or (eq type 'reply) (eq type 'followup)) 
2748            (eq gnus-mail-reply-method 'gnus-mail-reply-using-vm))
2749       (and (eq type 'forward)
2750            (eq gnus-mail-forward-method 'gnus-mail-forward-using-vm))
2751       (and (eq type 'new) 
2752            (eq gnus-mail-other-window-method 
2753                'gnus-mail-other-window-using-vm)))
2754      'gnus-vm-mail-setup)
2755     ((or
2756       (eq gnus-mail-method 'sendmail)
2757       (and (or (eq type 'reply) (eq type 'followup)) 
2758            (eq gnus-mail-reply-method 'gnus-mail-reply-using-mail))
2759       (and (eq type 'forward)
2760            (eq gnus-mail-forward-method 'gnus-mail-forward-using-mail))
2761       (and (eq type 'new) 
2762            (eq gnus-mail-other-window-method 
2763                'gnus-mail-other-window-using-mail)))
2764      'gnus-sendmail-mail-setup)
2765     (t
2766      (cond ((or (eq type 'reply) (eq type 'followup))
2767             gnus-mail-reply-method)
2768            ((eq type 'forward)
2769             gnus-mail-forward-method)
2770            ((eq type 'new) 
2771             gnus-mail-other-window-method))))
2772    to subject in-reply-to cc replybuffer actions)
2773   ;; Associate this mail buffer with the draft group.
2774   (gnus-enter-buffer-into-draft))
2775
2776 (defun gnus-sendmail-mail-setup (to subject in-reply-to cc replybuffer actions)
2777   (mail-mode)
2778   (mail-setup to subject nil cc replybuffer actions)
2779   (gnus-inews-set-point)
2780   (gnus-inews-modify-mail-mode-map))
2781   
2782 ;;; Gcc handling.
2783
2784 ;; Do Gcc handling, which copied the message over to some group. 
2785 (defun gnus-inews-do-gcc (&optional gcc)
2786   (save-excursion
2787     (save-restriction
2788       (nnheader-narrow-to-headers)
2789       (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
2790             (cur (current-buffer))
2791             groups group method)
2792         (when gcc
2793           (nnheader-remove-header "gcc")
2794           (widen)
2795           (setq groups (gnus-tokenize-header gcc " ,"))
2796           ;; Copy the article over to some group(s).
2797           (while (setq group (pop groups))
2798             (gnus-check-server 
2799              (setq method
2800                    (cond ((and (null (gnus-get-info group))
2801                                (eq (car gnus-message-archive-method)
2802                                    (car 
2803                                     (gnus-server-to-method
2804                                      (gnus-group-method group)))))
2805                           ;; If the group doesn't exist, we assume
2806                           ;; it's an archive group...
2807                           gnus-message-archive-method)
2808                          (t (gnus-group-method group)))))
2809             (unless (gnus-request-group group t method)
2810               (gnus-request-create-group group method))
2811             (gnus-check-server method)
2812             (save-excursion
2813               (nnheader-set-temp-buffer " *acc*")
2814               (insert-buffer-substring cur)
2815               (unless (condition-case ()
2816                           (gnus-request-accept-article group t method)
2817                         (error nil))
2818                 (gnus-message 1 "Couldn't store article in group %s: %s" 
2819                               group (gnus-status-message method))
2820                 (sit-for 2))
2821               (kill-buffer (current-buffer)))))))))
2822
2823 (defun gnus-inews-insert-bfcc ()
2824   "Insert Bcc and Fcc headers."
2825   (save-excursion
2826     ;; Handle author copy using BCC field.
2827     (when (and gnus-mail-self-blind
2828                (not (mail-fetch-field "bcc")))
2829       (mail-position-on-field "Bcc")
2830       (insert (if (stringp gnus-mail-self-blind)
2831                   gnus-mail-self-blind
2832                 (user-login-name))))
2833     ;; Handle author copy using FCC field.
2834     (when gnus-author-copy
2835       (mail-position-on-field "Fcc")
2836       (insert gnus-author-copy))))
2837
2838 (defun gnus-inews-insert-gcc ()
2839   "Insert Gcc headers based on `gnus-outgoing-message-group'."
2840   (save-excursion
2841     (save-restriction
2842       (gnus-inews-narrow-to-headers)
2843       (let* ((group gnus-outgoing-message-group)
2844              (gcc (cond 
2845                    ((gnus-functionp group)
2846                     (funcall group))
2847                    ((or (stringp group) (list group))
2848                     group))))
2849         (when gcc
2850           (insert "Gcc: "
2851                   (if (stringp gcc) gcc
2852                     (mapconcat 'identity gcc " "))
2853                   "\n"))))))
2854
2855 (defun gnus-inews-insert-archive-gcc (&optional group)
2856   "Insert the Gcc to say where the article is to be archived."
2857   (let* ((var gnus-message-archive-group)
2858          (group (or group gnus-newsgroup-name ""))
2859          result
2860          (groups
2861           (cond 
2862            ((null gnus-message-archive-method)
2863             ;; Ignore.
2864             nil)
2865            ((stringp var)
2866             ;; Just a single group.
2867             (list var))
2868            ((null var)
2869             ;; We don't want this.
2870             nil)
2871            ((and (listp var) (stringp (car var)))
2872             ;; A list of groups.
2873             var)
2874            ((gnus-functionp var)
2875             ;; A function.
2876             (funcall var group))
2877            (t
2878             ;; An alist of regexps/functions/forms.
2879             (while (and var
2880                         (not
2881                          (setq result
2882                                (cond 
2883                                 ((stringp (caar var))
2884                                  ;; Regexp.
2885                                  (when (string-match (caar var) group)
2886                                    (cdar var)))
2887                                 ((gnus-functionp (car var))
2888                                  ;; Function.
2889                                  (funcall (car var) group))
2890                                 (t
2891                                  (eval (car var)))))))
2892               (setq var (cdr var)))
2893             result))))
2894     (when groups
2895       (when (stringp groups)
2896         (setq groups (list groups)))
2897       (save-excursion
2898         (save-restriction
2899           (gnus-inews-narrow-to-headers)
2900           (goto-char (point-max))
2901           (insert "Gcc: ")
2902           (while groups
2903             (insert (gnus-group-prefixed-name 
2904                      (pop groups) gnus-message-archive-method))
2905             (insert " "))
2906           (insert "\n"))))))
2907
2908 ;;; Handling rejected (and postponed) news.
2909
2910 (defun gnus-draft-group ()
2911   "Return the name of the draft group."
2912   (gnus-group-prefixed-name 
2913    (file-name-nondirectory (directory-file-name gnus-draft-group-directory))
2914    (list 'nndraft gnus-draft-group-directory)))
2915
2916 (defun gnus-make-draft-group ()
2917   "Make the draft group or die trying."
2918   (let* ((method `(nndraft "private" 
2919                            (nndraft-directory 
2920                             ,gnus-draft-group-directory)))
2921          (group (gnus-draft-group)))
2922     (or (gnus-gethash group gnus-newsrc-hashtb)
2923         (gnus-group-make-group (gnus-group-real-name group) method)
2924         (error "Can't create the draft group"))
2925     (gnus-check-server method)
2926     group))
2927
2928 (defun gnus-associate-buffer-with-draft (&optional generate silent)
2929   "Enter the current buffer into the draft group."
2930   (interactive)
2931   (when (gnus-request-accept-article (gnus-make-draft-group) t)
2932     (unless silent
2933       ;; This function does the proper marking of articles.
2934       (gnus-mail-send-and-exit 'dont-send))
2935     (set-buffer-modified-p nil)))
2936
2937 (defun gnus-enter-buffer-into-draft ()
2938   (when gnus-use-draft
2939     (save-excursion
2940       ;; Make sure the draft group exists.
2941       (gnus-make-draft-group)
2942       ;; Associate the buffer with the draft group.
2943       (let ((article (gnus-request-associate-buffer (gnus-draft-group))))
2944         ;; Arrange for deletion of the draft after successful sending.
2945         (make-local-variable 'gnus-message-sent-hook)
2946         (setq gnus-message-sent-hook
2947               (list
2948                `(lambda ()
2949                   (let ((gnus-verbose-backends nil))
2950                     (gnus-request-expire-articles 
2951                      (quote ,(list article))
2952                      ,(gnus-draft-group) t)))))))))
2953
2954 (defun gnus-dissociate-buffer-from-draft ()
2955   "Disable auto-saving and association to the draft group of the current buffer."
2956   (interactive)
2957   (run-hooks 'gnus-message-sent-hook)
2958   (setq buffer-file-name nil)
2959   (setq buffer-auto-save-file-name nil))
2960
2961 (defun gnus-summary-send-draft ()
2962   "Enter a mail/post buffer to edit and send the draft."
2963   (interactive)
2964   (gnus-set-global-variables)
2965   (unless (equal gnus-newsgroup-name (gnus-draft-group))
2966     (error "This function can only be used in the draft buffer"))
2967   (let (buf)
2968     (if (not (setq buf (gnus-request-restore-buffer 
2969                         (gnus-summary-article-number) gnus-newsgroup-name)))
2970         (error "Couldn't restore the article")
2971       (switch-to-buffer buf)
2972       (gnus-inews-modify-mail-mode-map)
2973       (when (eq major-mode 'news-reply-mode)
2974         (local-set-key "\C-c\C-c" 'gnus-inews-news))
2975       (gnus-enter-buffer-into-draft) 
2976       ;; Insert the separator.
2977       (goto-char (point-min))
2978       (search-forward "\n\n")
2979       (forward-char -1)
2980       (insert mail-header-separator)
2981       ;; Configure windows.
2982       (let ((gnus-draft-buffer (current-buffer)))
2983         (gnus-configure-windows 'draft t)
2984         (goto-char (point))))))
2985   
2986 (defun gnus-configure-posting-styles ()
2987   "Configure posting styles according to `gnus-posting-styles'."
2988   (let ((styles gnus-posting-styles)
2989         (gnus-newsgroup-name (or gnus-newsgroup-name ""))
2990         style match variable attribute value value-value)
2991     ;; Go through all styles and look for matches.
2992     (while styles
2993       (setq style (pop styles)
2994             match (pop style))
2995       (when (cond ((stringp match)
2996                    ;; Regexp string match on the group name.
2997                    (string-match match gnus-newsgroup-name))
2998                   ((or (symbolp match)
2999                        (gnus-functionp match))
3000                    (cond ((gnus-functionp match)
3001                           ;; Function to be called.
3002                           (funcall match))
3003                          ((boundp match)
3004                           ;; Variable to be checked.
3005                           (symbol-value match))))
3006                   ((listp match)
3007                    ;; This is a form to be evaled.
3008                    (eval match)))
3009         ;; We have a match, so we set the variables.
3010         (while style
3011           (setq attribute (pop style)
3012                 value (cdr attribute))
3013           ;; We find the variable that is to be modified.
3014           (if (and (not (stringp (car attribute)))
3015                    (not (setq variable (cdr (assq (car attribute) 
3016                                                   gnus-posting-style-alist)))))
3017               (message "Couldn't find attribute %s" (car attribute))
3018             ;; We set the variable.
3019             (setq value-value
3020                   (cond ((stringp value)
3021                          value)
3022                         ((or (symbolp value)
3023                              (gnus-functionp value))
3024                          (cond ((gnus-functionp value)
3025                                 (funcall value))
3026                                ((boundp value)
3027                                 (symbol-value value))))
3028                         ((listp value)
3029                          (eval value))))
3030             (if variable
3031                 (progn
3032                   ;; This is an ordinary variable.
3033                   (make-local-variable variable)
3034                   (set variable value-value))
3035               ;; This is a header to be added to the headers when
3036               ;; posting. 
3037               (when value-value
3038                 (make-local-variable gnus-required-headers)
3039                 (make-local-variable gnus-required-mail-headers)
3040                 (push (cons (car attribute) value-value) 
3041                       gnus-required-headers)
3042                 (push (cons (car attribute) value-value) 
3043                       gnus-required-mail-headers)))))))))
3044
3045 (defun gnus-inews-set-point ()
3046   "Move point to where the user probably wants to find it."
3047   (gnus-inews-narrow-to-headers)
3048   (cond 
3049    ((re-search-forward "^[^:]+:[ \t]*$" nil t)
3050     (search-backward ":" )
3051     (widen)
3052     (forward-char 2))
3053    (t
3054     (goto-char (point-max))
3055     (widen)
3056     (forward-line 1)
3057     (unless (looking-at "$")
3058       (forward-line 2)))
3059    (sit-for 0)))
3060
3061 (gnus-add-shutdown 'gnus-inews-close 'gnus)
3062
3063 (defun gnus-inews-close ()
3064   (setq gnus-inews-sent-ids nil))
3065   
3066 ;;; Allow redefinition of functions.
3067
3068 (gnus-ems-redefine)
3069
3070 (provide 'gnus-msg)
3071
3072 ;;; gnus-msg.el ends here