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