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