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