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