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