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