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