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