*** 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           (secure-sender (gnus-inews-real-user-address)))
1207       (when (and from 
1208                  (not (gnus-check-before-posting 'sender))
1209                  (not (string=
1210                        (downcase (car (cdr (gnus-extract-address-components
1211                                             from))))
1212                        (downcase (gnus-inews-real-user-address))))
1213                  (or (null sender)
1214                      (not 
1215                       (string=
1216                        (downcase (car (cdr (gnus-extract-address-components
1217                                             sender))))
1218                        (downcase secure-sender)))))
1219         (goto-char (point-min))    
1220         ;; Rename any old Sender headers to Original-Sender.
1221         (when (re-search-forward "^Sender:" nil t)
1222           (beginning-of-line)
1223           (insert "Original-")
1224           (beginning-of-line))
1225         (insert "Sender: " secure-sender "\n")))))
1226
1227 (defun gnus-inews-insert-signature ()
1228   "Insert a signature file.
1229 If `gnus-signature-function' is bound and returns a string, this
1230 string is used instead of the variable `gnus-signature-file'.
1231 In either case, if the string is a file name, this file is
1232 inserted. If the string is not a file name, the string itself is
1233 inserted. 
1234
1235 If you never want any signature inserted, set both of these variables to
1236 nil."
1237   (save-excursion
1238     (let ((signature 
1239            (or (and gnus-signature-function
1240                     (funcall gnus-signature-function gnus-newsgroup-name))
1241                gnus-signature-file)))
1242       (if (and signature
1243                (or (file-exists-p signature)
1244                    (string-match " " signature)
1245                    (not (string-match 
1246                          "^/[^/]+/" (expand-file-name signature)))))
1247           (progn
1248             (goto-char (point-max))
1249             (if (and mail-signature (search-backward "\n-- \n" nil t))
1250                 ()
1251               ;; Delete any previous signatures.
1252               (if (search-backward "\n-- \n" nil t)
1253                   (delete-region (point) (point-max)))
1254               (or (eolp) (insert "\n"))
1255               (insert "-- \n")
1256               (if (file-exists-p signature)
1257                   (insert-file-contents signature)
1258                 (insert signature))
1259               (goto-char (point-max))
1260               (or (bolp) (insert "\n"))))))))
1261
1262 ;; Written by "Mr. Per Persson" <pp@solace.mh.se>.
1263 (defun gnus-inews-insert-mime-headers ()
1264   (goto-char (point-min))
1265   (let ((mail-header-separator 
1266          (progn 
1267            (goto-char (point-min))
1268            (if (and (search-forward (concat "\n" mail-header-separator "\n")
1269                                     nil t)
1270                     (not (search-backward "\n\n" nil t)))
1271                mail-header-separator
1272              ""))))
1273     (or (mail-position-on-field "Mime-Version")
1274         (insert "1.0")
1275         (cond ((progn
1276                  (goto-char (point-min))
1277                  (re-search-forward "[\200-\377]" nil t))
1278                (or (mail-position-on-field "Content-Type")
1279                    (insert "text/plain; charset=ISO-8859-1"))
1280                (or (mail-position-on-field "Content-Transfer-Encoding")
1281                    (insert "8bit")))
1282               (t (or (mail-position-on-field "Content-Type")
1283                      (insert "text/plain; charset=US-ASCII"))
1284                  (or (mail-position-on-field "Content-Transfer-Encoding")
1285                      (insert "7bit")))))))
1286
1287 (defun gnus-inews-do-fcc ()
1288   "Process Fcc headers in the current buffer.
1289 Unless the first character of the field is `|', the article is saved
1290 to the specified file using the function specified by the variable
1291 gnus-author-copy-saver.  The default function rmail-output saves in
1292 Unix mailbox format.
1293
1294 If the first character is `|', the contents of the article is sent to
1295 a program specified by the rest of the value."
1296   (let ((case-fold-search t)            ;Should ignore case.
1297         list file)
1298     (save-excursion
1299       (save-restriction
1300         (gnus-narrow-to-headers)
1301         (while (setq file (mail-fetch-field "fcc"))
1302           (push file list)
1303           (nnheader-remove-header "fcc" nil t))
1304         ;; Process FCC operations.
1305         (widen)
1306         (while list
1307           (setq file (pop list))
1308           (if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
1309               ;; Pipe the article to the program in question.
1310               (call-process-region (point-min) (point-max) shell-file-name
1311                                    nil nil nil "-c" (match-string 1 file))
1312             ;; Save the article.
1313             (setq file (expand-file-name file))
1314             (gnus-make-directory (file-name-directory file))
1315             (if (and gnus-author-copy-saver
1316                      (not (eq gnus-author-copy-saver 'rmail-output)))
1317                 (funcall gnus-author-copy-saver file)
1318               (if (and (file-readable-p file) (mail-file-babyl-p file))
1319                   (gnus-output-to-rmail file)
1320                 (let ((mail-use-rfc822 t))
1321                   (rmail-output file 1 t t))))))))))
1322
1323 (defun gnus-inews-path ()
1324   "Return uucp path."
1325   (let ((login-name (gnus-inews-login-name)))
1326     (cond ((null gnus-use-generic-path)
1327            (concat (nth 1 gnus-select-method) "!" login-name))
1328           ((stringp gnus-use-generic-path)
1329            ;; Support GENERICPATH.  Suggested by vixie@decwrl.dec.com.
1330            (concat gnus-use-generic-path "!" login-name))
1331           (t login-name))))
1332
1333 (defun gnus-inews-user-name ()
1334   "Return user's network address as \"NAME@DOMAIN (FULL-NAME)\"."
1335   (let ((full-name (gnus-inews-full-name))
1336         (address (if (or gnus-user-login-name gnus-use-generic-from
1337                          gnus-local-domain (getenv "DOMAINNAME"))
1338                      (concat (gnus-inews-login-name) "@"
1339                              (gnus-inews-domain-name gnus-use-generic-from))
1340                    user-mail-address))) 
1341     (or gnus-user-from-line
1342         (concat address
1343                 ;; User's full name.
1344                 (cond ((string-equal full-name "&") ;Unix hack.
1345                        (concat " (" (user-login-name) ")"))
1346                       ((string-match "[^ ]+@[^ ]+ +(.*)" address)
1347                        "")
1348                       (t
1349                        (concat " (" full-name ")")))))))
1350
1351 (defun gnus-inews-real-user-address ()
1352   "Return the \"real\" user address.
1353 This function tries to ignore all user modifications, and 
1354 give as trustworthy answer as possible."
1355   (concat (user-login-name) "@" (gnus-inews-full-address)))
1356
1357 (defun gnus-inews-login-name ()
1358   "Return login name."
1359   (or gnus-user-login-name (getenv "LOGNAME") (user-login-name)))
1360
1361 (defun gnus-inews-full-name ()
1362   "Return full user name."
1363   (or gnus-user-full-name (getenv "NAME") (user-full-name)))
1364
1365 (defun gnus-inews-domain-name (&optional genericfrom)
1366   "Return user's domain name.
1367 If optional argument GENERICFROM is a string, use it as the domain
1368 name; if it is non-nil, strip off local host name from the domain name.
1369 If the function `system-name' returns full internet name and the
1370 domain is undefined, the domain name is got from it."
1371   (if (or genericfrom gnus-local-domain (getenv "DOMAINNAME"))
1372       (let* ((system-name (system-name))
1373              (domain 
1374               (or (if (stringp genericfrom) genericfrom)
1375                   (getenv "DOMAINNAME")
1376                   gnus-local-domain
1377                   ;; Function `system-name' may return full internet name.
1378                   ;; Suggested by Mike DeCorte <mrd@sun.soe.clarkson.edu>.
1379                   (if (string-match "\\.." system-name)
1380                       ;; Some machines return "name.", and that's not
1381                       ;; very nice. 
1382                       (substring system-name (1- (match-end 0))))
1383                   (read-string "Domain name (no host): ")))
1384              (host (or (if (string-match "\\." system-name)
1385                            (substring system-name 0 (match-beginning 0)))
1386                        system-name)))
1387         (if (string-equal "." (substring domain 0 1))
1388             (setq domain (substring domain 1)))
1389         ;; Support GENERICFROM as same as standard Bnews system.
1390         ;; Suggested by ohm@kaba.junet and vixie@decwrl.dec.com.
1391         (cond ((null genericfrom)
1392                (concat host "." domain))
1393               ;;((stringp genericfrom) genericfrom)
1394               (t domain)))
1395     (if (string-match "\\." (system-name))
1396         (system-name)
1397       (if (string-match "@\\([^ ]+\\)\\($\\| \\)" user-mail-address)
1398           (substring user-mail-address 
1399                      (match-beginning 1) (match-end 1))
1400         "bogus-domain"))))
1401
1402 (defun gnus-inews-full-address ()
1403   (let ((domain (gnus-inews-domain-name))
1404         (system (system-name))
1405         (case-fold-search t))
1406     (if (string-match "\\." system) system
1407       (if (string-match (concat "^" (regexp-quote system)) domain) domain
1408         (concat system "." domain)))))
1409
1410 (defun gnus-inews-expires ()
1411   "Return an Expires header based on `gnus-article-expires'."
1412   (let ((current (current-time))
1413         (future (* 1.0 gnus-article-expires 60 60 24)))
1414     ;; Add the future to current.
1415     (setcar current (+ (car current) (round (/ future (expt 2 16)))))
1416     (setcar (cdr current) (+ (nth 1 current) (% (round future) (expt 2 16))))
1417     ;; Return the date in the future in UT.
1418     (timezone-make-date-arpa-standard 
1419      (current-time-string current) (current-time-zone) '(0 "UT"))))
1420
1421 (defun gnus-inews-distribution ()
1422   "Return the current Distribution header, if any."
1423   (when (and gnus-distribution-function
1424              (fboundp gnus-distribution-function))
1425     (funcall gnus-distribution-function (or gnus-newsgroup-name ""))))
1426
1427 (defun gnus-inews-message-id ()
1428   "Generate unique Message-ID for user."
1429   ;; Message-ID should not contain a slash and should be terminated by
1430   ;; a number.  I don't know the reason why it is so.
1431   (concat "<" (gnus-inews-unique-id) "@" (gnus-inews-full-address) ">"))
1432
1433 (defvar gnus-unique-id-char nil)
1434
1435 ;; If you ever change this function, make sure the new version
1436 ;; cannot generate IDs that the old version could.
1437 ;; You might for example insert a "." somewhere (not next to another dot
1438 ;; or string boundary), or modify the newsreader name to "Ding".
1439 (defun gnus-inews-unique-id ()
1440   ;; Dont use microseconds from (current-time), they may be unsupported.
1441   ;; Instead we use this randomly inited counter.
1442   (setq gnus-unique-id-char
1443         (% (1+ (or gnus-unique-id-char (logand (random t) (1- (lsh 1 20)))))
1444            ;; (current-time) returns 16-bit ints,
1445            ;; and 2^16*25 just fits into 4 digits i base 36.
1446            (* 25 25)))
1447   (let ((tm (if (fboundp 'current-time)
1448                 (current-time) '(12191 46742 287898))))
1449     (concat
1450      (if (memq system-type '(ms-dos emx vax-vms))
1451          (let ((user (downcase (gnus-inews-login-name))))
1452            (while (string-match "[^a-z0-9_]" user)
1453              (aset user (match-beginning 0) ?_))
1454            user)
1455        (gnus-number-base36 (user-uid) -1))
1456      (gnus-number-base36 (+ (car   tm) (lsh (% gnus-unique-id-char 25) 16)) 4)
1457      (gnus-number-base36 (+ (nth 1 tm) (lsh (/ gnus-unique-id-char 25) 16)) 4)
1458      ;; Append the newsreader name, because while the generated
1459      ;; ID is unique to this newsreader, other newsreaders might
1460      ;; otherwise generate the same ID via another algorithm.
1461      ".fsf")))
1462
1463
1464 (defun gnus-inews-date ()
1465   "Current time string."
1466   (timezone-make-date-arpa-standard 
1467    (current-time-string) (current-time-zone)))
1468
1469 (defun gnus-inews-organization ()
1470   "Return user's organization.
1471 The ORGANIZATION environment variable is used if defined.
1472 If not, the variable `gnus-local-organization' is used instead.
1473 If it is a function, the function will be called with the current
1474 newsgroup name as the argument.
1475 If this is a file name, the contents of this file will be used as the
1476 organization."
1477   (let* ((organization 
1478           (or (getenv "ORGANIZATION")
1479               (if gnus-local-organization
1480                   (if (gnus-functionp gnus-local-organization)
1481                       (funcall gnus-local-organization gnus-newsgroup-name)
1482                     gnus-local-organization))
1483               gnus-organization-file
1484               "~/.organization")))
1485     (and (stringp organization)
1486          (> (length organization) 0)
1487          (or (file-exists-p organization)
1488              (string-match " " organization)
1489              (not (string-match "^/usr/lib/\\|^~/" organization)))
1490          (save-excursion
1491            (gnus-set-work-buffer)
1492            (if (file-exists-p organization)
1493                (insert-file-contents organization)
1494              (insert organization))
1495            (goto-char (point-min))
1496            (while (re-search-forward " *\n *" nil t)
1497              (replace-match " " t t))
1498            (buffer-substring (point-min) (point-max))))))
1499
1500 (defun gnus-inews-lines ()
1501   "Count the number of lines and return numeric string."
1502   (save-excursion
1503     (save-restriction
1504       (widen)
1505       (goto-char (point-min))
1506       (re-search-forward 
1507        (concat "^" (regexp-quote mail-header-separator) "$"))
1508       (forward-line 1)
1509       (int-to-string (count-lines (point) (point-max))))))
1510
1511 (defun gnus-inews-in-reply-to ()
1512   "Return the In-Reply-To header for this message."
1513   gnus-in-reply-to)
1514
1515 \f
1516 ;;;
1517 ;;; Gnus Mail Functions 
1518 ;;;
1519
1520 ;;; Mail reply commands of Gnus summary mode
1521
1522 (defun gnus-summary-reply (yank &optional yank-articles)
1523   "Reply mail to news author.
1524 If prefix argument YANK is non-nil, original article is yanked automatically.
1525 Customize the variable gnus-mail-reply-method to use another mailer."
1526   (interactive "P")
1527   ;; Bug fix by jbw@bigbird.bu.edu (Joe Wells)
1528   ;; Stripping headers should be specified with mail-yank-ignored-headers.
1529   (gnus-set-global-variables)
1530   (if yank-articles (gnus-summary-goto-subject (car yank-articles)))
1531   (gnus-summary-select-article)
1532   (bury-buffer gnus-article-buffer)
1533   (gnus-mail-reply (or yank-articles (not (not yank)))))
1534
1535 (defun gnus-summary-reply-with-original (n)
1536   "Reply mail to news author with original article.
1537 Customize the variable gnus-mail-reply-method to use another mailer."
1538   (interactive "P")
1539   (gnus-summary-reply t (gnus-summary-work-articles n)))
1540
1541 (defun gnus-summary-mail-forward (post)
1542   "Forward the current message to another user.
1543 Customize the variable gnus-mail-forward-method to use another mailer."
1544   (interactive "P")
1545   (gnus-set-global-variables)
1546   (gnus-summary-select-article)
1547   (gnus-copy-article-buffer)
1548   (if post
1549       (gnus-forward-using-post gnus-article-copy)
1550     (gnus-mail-forward gnus-article-copy)))
1551
1552 (defun gnus-summary-resend-message (address)
1553   "Resend the current article to ADDRESS."
1554   (interactive "sResend message to: ")
1555   (gnus-summary-select-article)
1556   (save-excursion
1557     (let (resent beg)
1558       ;; We first set up a normal mail buffer.
1559       (nnheader-set-temp-buffer " *Gnus resend*")
1560       ;; This code from sendmail.el
1561       (insert "To: ")
1562       (let ((fill-prefix "\t")
1563             (address-start (point)))
1564         (insert address "\n")
1565         (fill-region-as-paragraph address-start (point-max)))
1566       (insert mail-header-separator "\n")
1567       ;; Insert our usual headers.
1568       (gnus-inews-narrow-to-headers)
1569       (gnus-inews-insert-headers '(From Date To))
1570       (goto-char (point-min))
1571       ;; Rename them all to "Resent-*".
1572       (while (re-search-forward "^[A-Za-z]" nil t)
1573         (forward-char -1)
1574         (insert "Resent-"))
1575       (widen)
1576       (forward-line)
1577       (delete-region (point) (point-max))
1578       (setq beg (point))
1579       ;; Insert the message to be resent.
1580       (insert-buffer-substring gnus-original-article-buffer)
1581       (goto-char (point-min))
1582       (search-forward "\n\n")
1583       (forward-char -1)
1584       (insert mail-header-separator)
1585       ;; Rename all old ("Also-")Resent headers.
1586       (while (re-search-backward "^\\(Also-\\)?Resent-" beg t)
1587         (beginning-of-line)
1588         (insert "Also-"))
1589       ;; Send it.
1590       (mail-send)
1591       (kill-buffer (current-buffer)))))
1592
1593 (defun gnus-summary-post-forward ()
1594   "Forward the current article to a newsgroup."
1595   (interactive)
1596   (gnus-summary-mail-forward t))
1597
1598 (defvar gnus-nastygram-message 
1599   "The following article was inappropriately posted to %s.\n"
1600   "Format string to insert in nastygrams.
1601 The current group name will be inserted at \"%s\".")
1602
1603 (defun gnus-summary-mail-nastygram (n)
1604   "Send a nastygram to the author of the current article."
1605   (interactive "P")
1606   (if (or gnus-expert-user
1607           (gnus-y-or-n-p 
1608            "Really send a nastygram to the author of the current article? "))
1609       (let ((group gnus-newsgroup-name))
1610         (gnus-summary-reply-with-original n)
1611         (set-buffer gnus-mail-buffer)
1612         (insert (format gnus-nastygram-message group))
1613         (gnus-mail-send-and-exit))))
1614
1615 (defun gnus-summary-mail-other-window ()
1616   "Compose mail in other window.
1617 Customize the variable `gnus-mail-other-window-method' to use another
1618 mailer."
1619   (interactive)
1620   (gnus-set-global-variables)
1621   (gnus-new-mail
1622    ;; We might want to prompt here.
1623    (when (and gnus-interactive-post
1624               (not gnus-expert-user))
1625      (read-string "To: ")))
1626   (gnus-configure-windows 'summary-mail 'force))
1627
1628 (defun gnus-new-mail (&optional to)
1629   (let (subject)
1630     (when (and gnus-interactive-post
1631                (not gnus-expert-user))
1632       (setq subject (read-string "Subject: ")))
1633     (pop-to-buffer gnus-mail-buffer)
1634     (erase-buffer)
1635     (gnus-mail-setup 'new to subject)
1636     (gnus-inews-insert-gcc)
1637     (run-hooks 'gnus-mail-hook)))
1638
1639 (defun gnus-mail-reply (&optional yank to-address followup)
1640   (save-excursion
1641     (set-buffer gnus-summary-buffer)
1642     (let ((group (gnus-group-real-name gnus-newsgroup-name))
1643           (cur (cons (current-buffer) (cdr gnus-article-current)))
1644           (winconf (current-window-configuration))
1645           from subject date reply-to message-of to cc
1646           references message-id sender follow-to sendto elt new-cc new-to
1647           mct mctdo)
1648       (set-buffer (get-buffer-create gnus-mail-buffer))
1649       (mail-mode)
1650       (if (and (buffer-modified-p)
1651                (> (buffer-size) 0)
1652                (not (gnus-y-or-n-p 
1653                      "Unsent message being composed; erase it? ")))
1654           ()
1655         (erase-buffer)
1656         (save-excursion
1657           (gnus-copy-article-buffer)
1658           (save-restriction
1659             (set-buffer gnus-article-copy)
1660             (gnus-narrow-to-headers)
1661             (if (not followup)
1662                 ;; This is a regular reply.
1663                 (if (gnus-functionp gnus-reply-to-function)
1664                     (setq follow-to (funcall gnus-reply-to-function group)))
1665               ;; This is a followup.
1666               (if (gnus-functionp gnus-followup-to-function)
1667                   (save-excursion
1668                     (setq follow-to
1669                           (funcall gnus-followup-to-function group)))))
1670             (setq from (mail-fetch-field "from"))
1671             (setq date (or (mail-fetch-field "date") 
1672                            (mail-header-date gnus-current-headers)))
1673             (setq message-of (gnus-message-of from date))
1674             (setq sender (mail-fetch-field "sender"))
1675             (setq subject (or (mail-fetch-field "subject") "none"))
1676             ;; Remove any (buggy) Re:'s that are present and make a
1677             ;; proper one.
1678             (and (string-match "^[ \t]*[Re][Ee]:[ \t]*" subject)
1679                  (setq subject (substring subject (match-end 0))))
1680             (setq subject (concat "Re: " subject))
1681             (setq to (mail-fetch-field "to"))
1682             (setq cc (mail-fetch-field "cc"))
1683             (setq mct (mail-fetch-field "mail-copies-to"))
1684             (setq reply-to (mail-fetch-field "reply-to"))
1685             (setq references (mail-fetch-field "references"))
1686             (setq message-id (mail-fetch-field "message-id"))
1687             
1688             (setq mctdo (not (equal mct "never")))
1689
1690             (if (not (and followup (not to-address)))
1691                 (setq new-to (or reply-to from))
1692               (let (ccalist)
1693                 (save-excursion
1694                   (gnus-set-work-buffer)
1695                   (unless (equal mct "never")
1696                     (insert (or reply-to from "")))
1697                   (insert (if (bolp) "" ", ")
1698                           (or to "")
1699                           (if (or (not mct) (not mctdo)) ""
1700                             (concat (if (bolp) "" ", ") mct))
1701                           (if cc (concat (if (bolp) "" ", ") cc) ""))
1702                   (goto-char (point-min))
1703                   (setq ccalist
1704                         (mapcar
1705                          (lambda (addr)
1706                            (cons (mail-strip-quoted-names addr) addr))
1707                          (nreverse (mail-parse-comma-list))))
1708                   (let ((s ccalist))
1709                     (while s
1710                       (setq ccalist (delq (assoc (car (pop s)) s) ccalist)))))
1711                 (setq new-to (cdr (pop ccalist)))
1712                 (setq new-cc 
1713                       (mapconcat 
1714                        (lambda (addr) (cdr addr))
1715                        ccalist ", "))))
1716             (widen)))
1717
1718         (setq news-reply-yank-from (or from "(nobody)"))
1719         (setq news-reply-yank-message-id
1720               (or message-id "(unknown Message-ID)"))
1721
1722         ;; Gather the "to" addresses out of the follow-to list and remove
1723         ;; them as we go.
1724         (if (and follow-to (listp follow-to))
1725             (while (setq elt (assoc "To" follow-to))
1726               (setq sendto (concat sendto (and sendto ", ") (cdr elt)))
1727               (setq follow-to (delq elt follow-to))))
1728
1729         (gnus-mail-setup 
1730          (if followup 'followup 'reply)
1731          (or to-address 
1732              (if (and follow-to (not (stringp follow-to))) sendto
1733                (or follow-to new-to sender "")))
1734          subject message-of
1735          (if (zerop (length new-cc)) nil new-cc)
1736          gnus-article-copy)
1737
1738         (make-local-variable 'gnus-article-reply)
1739         (setq gnus-article-reply cur)
1740         (make-local-variable 'gnus-prev-winconf)
1741         (setq gnus-prev-winconf winconf)
1742         (make-local-variable 'gnus-reply-subject)
1743         (setq gnus-reply-subject subject)
1744         (make-local-variable 'gnus-in-reply-to)
1745         (setq gnus-in-reply-to message-of)
1746
1747         (auto-save-mode auto-save-default)
1748         (gnus-inews-insert-gcc)
1749
1750         (if (and follow-to (listp follow-to))
1751             (progn
1752               (goto-char (point-min))
1753               (re-search-forward "^To:" nil t)
1754               (beginning-of-line)
1755               (forward-line 1)
1756               (while follow-to
1757                 (insert (car (car follow-to)) ": " (cdr (car follow-to)) "\n")
1758                 (setq follow-to (cdr follow-to)))))
1759         (nnheader-insert-references references message-id)
1760
1761         ;; Now the headers should be ok, so we do the yanking.
1762         (goto-char (point-min))
1763         (re-search-forward
1764          (concat "^" (regexp-quote mail-header-separator) "$"))
1765         (forward-line 1)
1766         (if (not yank)
1767             (gnus-configure-windows 'reply 'force)
1768           (let ((last (point))
1769                 end)
1770             (if (not (listp yank))
1771                 (progn
1772                   ;; Just a single article being yanked.
1773                   (save-excursion
1774                     (mail-yank-original nil))
1775                   (or mail-yank-hooks mail-citation-hook
1776                       (run-hooks 'news-reply-header-hook)))
1777               (while yank
1778                 (save-window-excursion
1779                   (set-buffer gnus-summary-buffer)
1780                   (gnus-summary-select-article nil nil nil (car yank))
1781                   (gnus-summary-remove-process-mark (car yank)))
1782                 (save-excursion
1783                   (setq end (point))
1784                   (gnus-copy-article-buffer)
1785                   (mail-yank-original nil)
1786                   (save-restriction
1787                     (narrow-to-region (point-min) (point))
1788                     (goto-char (mark t))
1789                     (let ((news-reply-yank-from
1790                            (save-excursion 
1791                              (set-buffer gnus-article-buffer)
1792                              (or (mail-fetch-field "from") "(nobody)")))
1793                           (news-reply-yank-message-id
1794                            (save-excursion 
1795                              (set-buffer gnus-article-buffer)
1796                              (or (mail-fetch-field "message-id")
1797                                  "(unknown Message-ID)"))))
1798                       (or mail-yank-hooks mail-citation-hook
1799                           (run-hooks 'news-reply-header-hook))
1800                       (setq end (point-max)))))
1801                 (goto-char end)
1802                 (setq yank (cdr yank))))
1803             (goto-char last))
1804           (forward-line 2)
1805           (gnus-configure-windows 'reply-yank 'force))
1806         (run-hooks 'gnus-mail-hook)))))
1807
1808 (defun gnus-new-news (&optional group inhibit-prompt)
1809   "Set up a *post-news* buffer that points to GROUP.
1810 If INHIBIT-PROMPT, never prompt for a Subject."
1811   (let ((winconf (current-window-configuration))
1812         subject)
1813     (when (and gnus-interactive-post
1814                (not inhibit-prompt)
1815                (not gnus-expert-user))
1816       (setq subject (read-string "Subject: ")))
1817     (pop-to-buffer gnus-post-news-buffer)  
1818     (erase-buffer)
1819     (news-reply-mode)
1820     ;; Let posting styles be configured.
1821     (gnus-configure-posting-styles)
1822     (news-setup nil subject nil (and group (gnus-group-real-name group)) nil)
1823     (goto-char (point-min))
1824
1825     (unless (re-search-forward 
1826              (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1827       (goto-char (point-max)))
1828     (insert "\n\n")
1829
1830     (gnus-inews-insert-bfcc)
1831     (gnus-inews-insert-gcc)
1832     (gnus-inews-insert-signature)
1833     (and gnus-post-prepare-function
1834          (gnus-functionp gnus-post-prepare-function)
1835          (funcall gnus-post-prepare-function group))
1836     (goto-char (point-min))
1837     (if group
1838         (re-search-forward "^Subject: " nil t)
1839       (re-search-forward "^Newsgroups: " nil t))
1840     (run-hooks 'gnus-post-prepare-hook)
1841     (make-local-variable 'gnus-prev-winconf)
1842     (setq gnus-prev-winconf winconf)
1843     (gnus-inews-modify-mail-mode-map)
1844     (local-set-key "\C-c\C-c" 'gnus-inews-news)))
1845
1846 (defun gnus-news-followup (&optional yank group)
1847   (save-excursion
1848     (set-buffer gnus-summary-buffer)
1849     (if (not (or (not gnus-novice-user)
1850                  gnus-expert-user
1851                  (gnus-y-or-n-p
1852                   "Are you sure you want to post to all of USENET? ")))
1853         ()
1854       (let ((group (gnus-group-real-name (or group gnus-newsgroup-name)))
1855             (cur (cons (current-buffer) (cdr gnus-article-current)))
1856             (winconf (current-window-configuration))
1857             from subject date reply-to message-of
1858             references message-id sender follow-to sendto elt 
1859             followup-to distribution newsgroups)
1860         (set-buffer (get-buffer-create gnus-post-news-buffer))
1861         (news-reply-mode)
1862         (if (and (buffer-modified-p)
1863                  (> (buffer-size) 0)
1864                  (not (gnus-y-or-n-p 
1865                        "Unsent message being composed; erase it? ")))
1866             ()
1867           (erase-buffer)
1868           (save-excursion
1869             (gnus-copy-article-buffer)
1870             (save-restriction
1871               (set-buffer gnus-article-copy)
1872               (gnus-narrow-to-headers)
1873               (if (gnus-functionp gnus-followup-to-function)
1874                   (save-excursion
1875                     (setq follow-to
1876                           (funcall gnus-followup-to-function group))))
1877               (setq from (mail-fetch-field "from"))
1878               (setq date (or (mail-fetch-field "date") 
1879                              (mail-header-date gnus-current-headers)))
1880               (setq message-of (gnus-message-of from date))
1881               (setq subject (or (mail-fetch-field "subject") "none"))
1882               ;; Remove any (buggy) Re:'s that are present and make a
1883               ;; proper one.
1884               (and (string-match "^[ \t]*[Re][Ee]:[ \t]*" subject)
1885                    (setq subject (substring subject (match-end 0))))
1886               (setq subject (concat "Re: " subject))
1887               (setq references (mail-fetch-field "references"))
1888               (setq message-id (mail-fetch-field "message-id"))
1889               (setq followup-to (mail-fetch-field "followup-to"))
1890               (setq newsgroups (mail-fetch-field "newsgroups"))
1891               (setq distribution (mail-fetch-field "distribution"))
1892               ;; Remove bogus distribution.
1893               (and (stringp distribution)
1894                    (string-match "world" distribution)
1895                    (setq distribution nil))
1896               (widen)))
1897
1898           (setq news-reply-yank-from (or from "(nobody)"))
1899           (setq news-reply-yank-message-id
1900                 (or message-id "(unknown Message-ID)"))
1901
1902           ;; Gather the "to" addresses out of the follow-to list and remove
1903           ;; them as we go.
1904           (if (and follow-to (listp follow-to))
1905               (while (setq elt (assoc "Newsgroups" follow-to))
1906                 (setq sendto (concat sendto (and sendto ", ") (cdr elt)))
1907                 (setq follow-to (delq elt follow-to))))
1908
1909           ;; Let posting styles be configured.
1910           (gnus-configure-posting-styles)
1911
1912           (news-setup nil subject nil 
1913                       (or sendto 
1914                           (and followup-to
1915                                gnus-use-followup-to
1916                                (or (not (eq gnus-use-followup-to 'ask))
1917                                    (gnus-y-or-n-p 
1918                                     (format
1919                                      "Use Followup-To %s? " followup-to)))
1920                                followup-to)
1921                           newsgroups group "")
1922                       gnus-article-copy)
1923
1924           (make-local-variable 'gnus-article-reply)
1925           (setq gnus-article-reply cur)
1926           (make-local-variable 'gnus-prev-winconf)
1927           (setq gnus-prev-winconf winconf)
1928           (make-local-variable 'gnus-reply-subject)
1929           (setq gnus-reply-subject (mail-header-subject gnus-current-headers))
1930           (make-local-variable 'gnus-in-reply-to)
1931           (setq gnus-in-reply-to message-of)
1932
1933           (gnus-inews-insert-signature)
1934
1935           (and gnus-post-prepare-function
1936                (gnus-functionp gnus-post-prepare-function)
1937                (funcall gnus-post-prepare-function group))
1938           (run-hooks 'gnus-post-prepare-hook)
1939
1940           (auto-save-mode auto-save-default)
1941           (gnus-inews-modify-mail-mode-map)
1942           (local-set-key "\C-c\C-c" 'gnus-inews-news)
1943
1944           (if (and follow-to (listp follow-to))
1945               (progn
1946                 (goto-char (point-min))
1947                 (and (re-search-forward "^Newsgroups:" nil t)
1948                      (forward-line 1))
1949                 (while follow-to
1950                   (insert (car (car follow-to)) ": " 
1951                           (cdr (car follow-to)) "\n")
1952                   (setq follow-to (cdr follow-to)))))
1953           
1954           ;; If a distribution existed, we use it.
1955           (if distribution
1956               (progn
1957                 (mail-position-on-field "Distribution")
1958                 (insert distribution)))
1959           
1960           (nnheader-insert-references references message-id)
1961
1962           ;; Handle `gnus-auto-mail-to-author'.
1963           ;; Suggested by Daniel Quinlan <quinlan@best.com>.
1964           ;; Revised to respect Reply-To by Ulrik Dickow <dickow@nbi.dk>.
1965           (let ((to (if (if (eq gnus-auto-mail-to-author 'ask)
1966                             (y-or-n-p "Also send mail to author? ")
1967                           gnus-auto-mail-to-author)
1968                         (or (save-excursion
1969                               (set-buffer gnus-article-copy)
1970                               (gnus-fetch-field "reply-to"))
1971                             from)))
1972                 (x-mail (save-excursion
1973                           (set-buffer gnus-article-copy)
1974                           (gnus-fetch-field "x-mail-copy-to"))))
1975             ;; Deny sending copy if there's a negative X-Mail-Copy-To
1976             ;; header. 
1977             (if x-mail
1978                 (if (and (string= x-mail "never")
1979                          (not (eq gnus-auto-mail-to-author 'force)))
1980                     (setq to nil)
1981                   (setq to x-mail)))
1982             ;; Insert a To or Cc header.
1983             (if to
1984                 (if (mail-fetch-field "To")
1985                     (progn
1986                       (beginning-of-line)
1987                       (insert "Cc: " to "\n"))
1988                   (mail-position-on-field "To")
1989                   (insert to))))
1990
1991           (gnus-inews-insert-bfcc)
1992           (gnus-inews-insert-gcc)
1993     
1994           ;; Now the headers should be ok, so we do the yanking.
1995           (goto-char (point-min))
1996           (re-search-forward
1997            (concat "^" (regexp-quote mail-header-separator) "$"))
1998           (forward-line 1)
1999           (if (not yank)
2000               (progn
2001                 (gnus-configure-windows 'followup 'force)
2002                 (insert "\n\n")
2003                 (forward-line -2))
2004             (let ((last (point))
2005                   end)
2006               (if (not (listp yank))
2007                   (progn
2008                     (save-excursion
2009                       (mail-yank-original nil))
2010                     (or mail-yank-hooks mail-citation-hook
2011                         (run-hooks 'news-reply-header-hook)))
2012                 (while yank
2013                   (save-window-excursion
2014                     (set-buffer gnus-summary-buffer)
2015                     (gnus-summary-select-article nil nil nil (car yank))
2016                     (gnus-summary-remove-process-mark (car yank)))
2017                   (save-excursion
2018                     (gnus-copy-article-buffer)
2019                     (mail-yank-original nil)
2020                     (setq end (point)))
2021                   (or mail-yank-hooks mail-citation-hook
2022                       (run-hooks 'news-reply-header-hook))
2023                   (goto-char end)
2024                   (setq yank (cdr yank))))
2025               (goto-char last))
2026             (gnus-configure-windows 'followup-yank 'force))
2027         
2028           (make-local-variable 'gnus-article-check-size)
2029           (setq gnus-article-check-size
2030                 (cons (buffer-size) (gnus-article-checksum))))))))
2031
2032 (defun gnus-message-of (from date)
2033   "Take a FROM and a DATE and create an IN-REPLY-TO."
2034   (cond 
2035    ((not from)
2036     nil)
2037    (t
2038     (let ((stop-pos 
2039            (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
2040       (concat (if stop-pos (substring from 0 stop-pos) from)
2041               "'s message of " 
2042               (if (or (not date) (string= date ""))
2043                   "(unknown date)" date))))))
2044
2045 (defun gnus-mail-yank-original ()
2046   (interactive)
2047   (save-excursion
2048     (mail-yank-original nil))
2049   (or mail-yank-hooks mail-citation-hook
2050       (run-hooks 'news-reply-header-hook)))
2051
2052 (defun gnus-mail-send-and-exit (&optional dont-send)
2053   "Send the current mail and return to Gnus."
2054   (interactive)
2055   (let* ((reply gnus-article-reply)
2056          (winconf gnus-prev-winconf)
2057          (address-group gnus-add-to-address)
2058          (to-address (and address-group
2059                           (mail-fetch-field "to"))))
2060     (setq gnus-add-to-address nil)
2061     (or dont-send (gnus-mail-send))
2062     (bury-buffer)
2063     ;; This mail group doesn't have a `to-address', so we add one
2064     ;; here.  Magic!  
2065     (and to-address
2066          (gnus-group-add-parameter 
2067           address-group (cons 'to-address to-address)))
2068     (if (get-buffer gnus-group-buffer)
2069         (progn
2070           (if (gnus-buffer-exists-p (car-safe reply))
2071               (progn
2072                 (set-buffer (car reply))
2073                 (and (cdr reply)
2074                      (gnus-summary-mark-article-as-replied 
2075                       (cdr reply)))))
2076           (and winconf (set-window-configuration winconf))))))
2077
2078 (defun gnus-put-message ()
2079   "Put the current message in some group and return to Gnus."
2080   (interactive)
2081   (let ((reply gnus-article-reply)
2082         (winconf gnus-prev-winconf)
2083         (group gnus-newsgroup-name)
2084         buf)
2085     
2086     (or (and group (not (gnus-group-read-only-p group)))
2087         (setq group (read-string "Put in group: " nil
2088                                  (gnus-writable-groups))))
2089     (and (gnus-gethash group gnus-newsrc-hashtb)
2090          (error "No such group: %s" group))
2091
2092     (save-excursion
2093       (save-restriction
2094         (widen)
2095         (gnus-inews-narrow-to-headers)
2096         (let (gnus-deletable-headers)
2097           (if (eq major-mode 'mail-mode)
2098               (gnus-inews-insert-headers gnus-required-mail-headers)
2099             (gnus-inews-insert-headers)))
2100         (goto-char (point-max))
2101         (insert "Gcc: " group "\n")
2102         (widen)))
2103
2104     (gnus-inews-do-gcc)
2105
2106     (if (get-buffer gnus-group-buffer)
2107         (progn
2108           (if (gnus-buffer-exists-p (car-safe reply))
2109               (progn
2110                 (set-buffer (car reply))
2111                 (and (cdr reply)
2112                      (gnus-summary-mark-article-as-replied 
2113                       (cdr reply)))))
2114           (and winconf (set-window-configuration winconf))))))
2115
2116 (defun gnus-forward-make-subject (buffer)
2117   (save-excursion
2118     (set-buffer buffer)
2119     (concat "[" (if (memq 'mail (assoc (symbol-name 
2120                                         (car (gnus-find-method-for-group 
2121                                               gnus-newsgroup-name)))
2122                                        gnus-valid-select-methods))
2123                     (gnus-fetch-field "From")
2124                   gnus-newsgroup-name)
2125             "] " (or (gnus-fetch-field "Subject") ""))))
2126
2127 (defun gnus-forward-insert-buffer (buffer)
2128   (save-excursion
2129     (save-restriction
2130       (if gnus-signature-before-forwarded-message
2131           (goto-char (point-max))
2132         (goto-char (point-min))
2133         (re-search-forward
2134          (concat "^" (regexp-quote mail-header-separator) "$"))
2135         (forward-line 1))
2136       ;; Narrow to the area we are to insert.
2137       (narrow-to-region (point) (point))
2138       ;; Insert the separators and the forwarded buffer.
2139       (insert gnus-forward-start-separator)
2140       (insert-buffer-substring buffer)
2141       (goto-char (point-max))
2142       (insert gnus-forward-end-separator)
2143       ;; Delete any invisible text.
2144       (goto-char (point-min))
2145       (let (beg)
2146         (while (setq beg (next-single-property-change (point) 'invisible))
2147           (goto-char beg)
2148           (delete-region beg (or (next-single-property-change 
2149                                   (point) 'invisible)
2150                                  (point-max))))))))
2151
2152 (defun gnus-mail-forward (&optional buffer)
2153   "Forward the current message to another user using mail."
2154   (let* ((forward-buffer (or buffer (current-buffer)))
2155          (winconf (current-window-configuration))
2156          (subject (gnus-forward-make-subject forward-buffer)))
2157     (set-buffer (get-buffer-create gnus-mail-buffer))
2158     (if (and (buffer-modified-p)
2159              (> (buffer-size) 0)
2160              (not (gnus-y-or-n-p 
2161                    "Unsent message being composed; erase it? ")))
2162         ()
2163       (erase-buffer)
2164       (gnus-mail-setup 'forward nil subject)
2165       (make-local-variable 'gnus-prev-winconf)
2166       (setq gnus-prev-winconf winconf)
2167       (gnus-forward-insert-buffer forward-buffer)
2168       (goto-char (point-min))
2169       (re-search-forward "^To: ?" nil t)
2170       (gnus-configure-windows 'mail-forward 'force)
2171       ;; You have a chance to arrange the message.
2172       (run-hooks 'gnus-mail-forward-hook)
2173       (run-hooks 'gnus-mail-hook))))
2174
2175 (defun gnus-forward-using-post (&optional buffer)
2176   (save-excursion
2177     (let* ((forward-buffer (or buffer (current-buffer))) 
2178            (subject (gnus-forward-make-subject forward-buffer))
2179            (gnus-newsgroup-name nil))
2180       (gnus-post-news 'post nil nil nil nil subject)
2181       (save-excursion
2182         (gnus-forward-insert-buffer forward-buffer)
2183         ;; You have a chance to arrange the message.
2184         (run-hooks 'gnus-mail-forward-hook)))))
2185
2186 (defun gnus-mail-other-window-using-mail ()
2187   "Compose mail other window using mail."
2188   (let ((winconf (current-window-configuration)))
2189     (mail-other-window nil nil nil nil nil (get-buffer gnus-article-buffer))
2190     (gnus-inews-modify-mail-mode-map)
2191     (make-local-variable 'gnus-prev-winconf)
2192     (setq gnus-prev-winconf winconf)
2193     (run-hooks 'gnus-mail-hook)
2194     (gnus-configure-windows 'summary-mail 'force)))
2195
2196 (defun gnus-article-mail (yank)
2197   "Send a reply to the address near point.
2198 If YANK is non-nil, include the original article."
2199   (interactive "P")
2200   (let ((address 
2201          (buffer-substring
2202           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
2203           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
2204     (and address
2205          (progn
2206            (switch-to-buffer gnus-summary-buffer)
2207            (gnus-mail-reply yank address)))))
2208
2209 (defun gnus-bug ()
2210   "Send a bug report to the Gnus maintainers."
2211   (interactive)
2212   (let ((winconf (current-window-configuration)))
2213     (delete-other-windows)
2214     (switch-to-buffer "*Gnus Help Bug*")
2215     (erase-buffer)
2216     (insert gnus-bug-message)
2217     (goto-char (point-min))
2218     (pop-to-buffer "*Gnus Bug*")
2219     (erase-buffer)
2220     (mail-mode)
2221     (mail-setup gnus-maintainer nil nil nil nil nil)
2222     (auto-save-mode auto-save-default)
2223     (make-local-variable 'gnus-prev-winconf)
2224     (setq gnus-prev-winconf winconf)
2225     (gnus-inews-modify-mail-mode-map)
2226     (local-set-key "\C-c\C-c" 'gnus-bug-mail-send-and-exit)
2227     (goto-char (point-min))
2228     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2229     (forward-line 1)
2230     (insert (format "%s\n%s\n\n\n\n\n" (gnus-version) (emacs-version)))
2231     (gnus-debug)
2232     (goto-char (point-min))
2233     (search-forward "Subject: " nil t)
2234     (message "")))
2235
2236 (defun gnus-bug-mail-send-and-exit ()
2237   "Send the bug message and exit."
2238   (interactive)
2239   (and (get-buffer "*Gnus Help Bug*")
2240        (kill-buffer "*Gnus Help Bug*"))
2241   (gnus-mail-send-and-exit))
2242
2243 (defun gnus-debug ()
2244   "Attemps to go through the Gnus source file and report what variables have been changed.
2245 The source file has to be in the Emacs load path."
2246   (interactive)
2247   (let ((files '("gnus.el" "gnus-msg.el" "gnus-score.el" "nnmail.el"))
2248         file dirs expr olist sym)
2249     (message "Please wait while we snoop your variables...")
2250     (sit-for 0)
2251     (save-excursion
2252       (set-buffer (get-buffer-create " *gnus bug info*"))
2253       (buffer-disable-undo (current-buffer))
2254       (while files
2255         (erase-buffer)
2256         (setq dirs load-path)
2257         (while dirs
2258           (if (or (not (car dirs))
2259                   (not (stringp (car dirs)))
2260                   (not (file-exists-p 
2261                         (setq file (concat (file-name-as-directory 
2262                                             (car dirs)) (car files))))))
2263               (setq dirs (cdr dirs))
2264             (setq dirs nil)
2265             (insert-file-contents file)
2266             (goto-char (point-min))
2267             (or (re-search-forward "^;;* *Internal variables" nil t)
2268                 (error "Malformed sources in file %s" file))
2269             (narrow-to-region (point-min) (point))
2270             (goto-char (point-min))
2271             (while (setq expr (condition-case () 
2272                                   (read (current-buffer)) (error nil)))
2273               (condition-case ()
2274                   (and (eq (car expr) 'defvar)
2275                        (stringp (nth 3 expr))
2276                        (or (not (boundp (nth 1 expr)))
2277                            (not (equal (eval (nth 2 expr))
2278                                        (symbol-value (nth 1 expr)))))
2279                        (setq olist (cons (nth 1 expr) olist)))
2280                 (error nil)))))
2281         (setq files (cdr files)))
2282       (kill-buffer (current-buffer)))
2283     (insert "------------------- Environment follows -------------------\n\n")
2284     (setq olist (nreverse olist))
2285     (while olist
2286       (if (boundp (car olist))
2287           (insert "(setq " (symbol-name (car olist)) 
2288                   (if (or (consp (setq sym (symbol-value (car olist))))
2289                           (and (symbolp sym)
2290                                (not (or (eq sym nil)
2291                                         (eq sym t)))))
2292                       " '" " ")
2293                   (prin1-to-string (symbol-value (car olist))) ")\n")
2294         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
2295       (setq olist (cdr olist)))
2296     (insert "\n\n")
2297     ;; Remove any null chars - they seem to cause trouble for some
2298     ;; mailers. (Byte-compiled output from the stuff above.) 
2299     (goto-char (point-min))
2300     (while (re-search-forward "[\000\200]" nil t)
2301       (replace-match "" t t))))
2302
2303
2304 ;;; Treatment of rejected articles.
2305
2306
2307 ;;; Bounced mail.
2308
2309 (defun gnus-summary-resend-bounced-mail (fetch)
2310   "Re-mail the current message.
2311 This only makes sense if the current message is a bounce message than
2312 contains some mail you have written which has been bounced back to
2313 you.
2314 If FETCH, try to fetch the article that this is a reply to, if indeed
2315 this is a reply."
2316   (interactive "P")
2317   (gnus-summary-select-article t)
2318   ;; Create a mail buffer.
2319   (gnus-new-mail)
2320   (erase-buffer)
2321   (insert-buffer-substring gnus-article-buffer)
2322   (goto-char (point-min))
2323   (search-forward "\n\n")
2324   ;; We remove everything before the bounced mail.
2325   (delete-region 
2326    (point-min)
2327    (if (re-search-forward "[^ \t]*:" nil t)
2328        (match-beginning 0)
2329      (point)))
2330   (let (references)
2331     (save-excursion
2332       (save-restriction
2333         (gnus-narrow-to-headers)
2334         (nnheader-remove-header gnus-bounced-headers-junk t)
2335         (setq references (mail-fetch-field "references"))
2336         (goto-char (point-max))
2337         (insert mail-header-separator)))
2338     ;; If there are references, we fetch the article we answered to.  
2339     (and fetch 
2340          references
2341          (string-match "\\(<[^]+>\\)[ \t]*$" references)
2342          (gnus-summary-refer-article 
2343           (substring references (match-beginning 1) (match-end 1)))
2344          (progn
2345            (gnus-summary-show-all-headers)
2346            (gnus-configure-windows 'compose-bounce))))
2347   (goto-char (point-min)))
2348
2349 ;;; Sending mail.
2350
2351 (defun gnus-mail-send ()
2352   "Send the current buffer as mail.
2353 Headers will be generated before sending."
2354   (interactive)
2355   (save-excursion
2356     (save-restriction
2357       (widen)
2358       (gnus-inews-narrow-to-headers)
2359       (gnus-inews-insert-headers gnus-required-mail-headers)
2360       (gnus-inews-remove-empty-headers)))
2361   (widen)
2362   ;; Remove the header separator.
2363   (goto-char (point-min))
2364   (and (re-search-forward
2365         (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2366        (replace-match "" t t))
2367   ;; Run final inews hooks.  This hook may do FCC.
2368   (run-hooks 'gnus-inews-article-hook)
2369   (gnus-inews-do-gcc)
2370   (gnus-narrow-to-headers)
2371   (nnheader-remove-header "^[gf]cc:" t)
2372   (widen)
2373   (goto-char (point-min))
2374   (search-forward "\n\n")
2375   (forward-char -1)
2376   (insert mail-header-separator)
2377   (mail-send)
2378   (run-hooks 'gnus-message-sent-hook))
2379
2380 (defun gnus-inews-modify-mail-mode-map ()
2381   (use-local-map (copy-keymap (current-local-map)))
2382   (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
2383   (local-set-key "\C-c\C-p" 'gnus-put-message)
2384   (local-set-key "\C-c\C-d" 'gnus-enter-into-draft-group))
2385
2386 (defun gnus-mail-setup (type &optional to subject in-reply-to cc
2387                              replybuffer actions)
2388   ;; Let posting styles be configured.
2389   (gnus-configure-posting-styles)
2390   (funcall
2391    (cond
2392     ((or 
2393       (and (or (eq type 'reply) (eq type 'followup))
2394            (eq gnus-mail-reply-method 'gnus-mail-reply-using-mhe))
2395       (and (eq type 'forward)
2396            (eq gnus-mail-forward-method 'gnus-mail-forward-using-mhe))
2397       (and (eq type 'new) 
2398            (eq gnus-mail-other-window-method 
2399                'gnus-mail-other-window-using-mhe)))
2400      'gnus-mh-mail-setup)
2401     ((or 
2402       (and (or (eq type 'reply) (eq type 'followup)) 
2403            (eq gnus-mail-reply-method 'gnus-mail-reply-using-vm))
2404       (and (eq type 'forward)
2405            (eq gnus-mail-forward-method 'gnus-mail-forward-using-vm))
2406       (and (eq type 'new) 
2407            (eq gnus-mail-other-window-method 
2408                'gnus-mail-other-window-using-vm)))
2409      'gnus-vm-mail-setup)
2410     (t 'gnus-sendmail-mail-setup))
2411    to subject in-reply-to cc replybuffer actions))
2412
2413 (defun gnus-sendmail-mail-setup (to subject in-reply-to cc replybuffer actions)
2414   (mail-mode)
2415   (mail-setup to subject nil cc replybuffer actions)
2416   (goto-char (point-min))
2417   (if (re-search-forward 
2418        (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2419       (forward-line 1)
2420     (goto-char (point-max)))
2421   (gnus-inews-modify-mail-mode-map))
2422   
2423 ;;; Gcc handling.
2424
2425 ;; Do Gcc handling, which copied the message over to some group. 
2426 (defun gnus-inews-do-gcc (&optional gcc)
2427   (save-excursion
2428     (save-restriction
2429       (gnus-narrow-to-headers)
2430       (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
2431             end)
2432         (if (not gcc)
2433             () ; Nothing to be done.
2434           (nnheader-remove-header "gcc")
2435           ;; Copy the article over to some group(s).
2436           (while (string-match
2437                   "^[ \t]*\\([^ \t]+\\)\\([ \t]+\\|$\\)" gcc)
2438             (setq end (match-end 0))
2439             (condition-case ()
2440                 (gnus-request-accept-article 
2441                  (substring gcc (match-beginning 1) (match-end 1)) t)
2442               (error nil))
2443             (setq gcc (substring gcc end))))))))
2444
2445 (defun gnus-inews-insert-bfcc ()
2446   "Insert Bcc and Fcc headers."
2447   (save-excursion
2448     ;; Handle author copy using BCC field.
2449     (when (and gnus-mail-self-blind
2450                (not (mail-fetch-field "bcc")))
2451       (mail-position-on-field "Bcc")
2452       (insert (if (stringp gnus-mail-self-blind)
2453                   gnus-mail-self-blind
2454                 (user-login-name))))
2455     ;; Handle author copy using FCC field.
2456     (when gnus-author-copy
2457       (mail-position-on-field "Fcc")
2458       (insert gnus-author-copy))))
2459
2460 (defun gnus-inews-insert-gcc ()
2461   (save-excursion
2462     (save-restriction
2463       (gnus-inews-narrow-to-headers)
2464       (let* ((group gnus-outgoing-message-group)
2465              (gcc (cond 
2466                    ((gnus-functionp group)
2467                     (funcall group))
2468                    ((or (stringp group) (list group))
2469                     group))))
2470         (when gcc
2471           (insert "Gcc: "
2472                   (if (stringp group) group
2473                     (mapconcat 'identity group " "))
2474                   "\n"))))))
2475
2476 ;;; Handling rejected (and postponed) news.
2477
2478 (defun gnus-draft-group ()
2479   "Return the name of the draft group."
2480   (gnus-group-prefixed-name 
2481    (file-name-nondirectory gnus-draft-group-directory)
2482    (list 'nndir gnus-draft-group-directory)))
2483
2484 (defun gnus-make-draft-group ()
2485   "Make the draft group or die trying."
2486   (let* ((method (` (nndir "private" 
2487                            (nndir-directory (, gnus-draft-group-directory)))))
2488          (group (gnus-group-prefixed-name 
2489                  (file-name-nondirectory gnus-draft-group-directory)
2490                  method)))
2491     (or (gnus-gethash group gnus-newsrc-hashtb)
2492         (gnus-group-make-group (gnus-group-real-name group) method)
2493         (error "Can't create the draft group"))
2494     group))
2495
2496 (defun gnus-enter-into-draft-group ()
2497   "Enter the current buffer into the draft group."
2498   (interactive)
2499   (gnus-put-in-draft-group t))
2500
2501 (defun gnus-put-in-draft-group (&optional generate silent)
2502   "Does the actual putting."
2503   (let ((group (gnus-make-draft-group))
2504         (type (list major-mode (buffer-name) gnus-newsgroup-name
2505                     (point)))
2506         (mode major-mode)
2507         (buf (current-buffer)))
2508     (widen)
2509     (save-excursion
2510       (nnheader-set-temp-buffer " *enter-draft*")
2511       (insert-buffer-substring buf)
2512       (save-restriction
2513         (widen)
2514         (gnus-inews-narrow-to-headers)
2515         (let (gnus-deletable-headers)
2516           (if (eq mode 'mail-mode)
2517               (gnus-inews-insert-headers gnus-required-mail-headers)
2518             (gnus-inews-insert-headers)))
2519         (widen))
2520
2521       (goto-char (point-min))
2522       ;; We have to store whether we are in a mail group or news group. 
2523       (insert (format "X-Gnus-Draft-Type: %S\n" type))
2524       (and (re-search-forward
2525             (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2526            (replace-match "" t t))
2527       (if (prog1
2528               (gnus-request-accept-article group t)
2529             (kill-buffer (current-buffer)))
2530           (or silent
2531               (gnus-mail-send-and-exit 'dont-send))))
2532     (set-buffer-modified-p nil)))
2533
2534 (defun gnus-summary-send-draft ()
2535   "Enter a mail/post buffer to edit and send the draft."
2536   (interactive)
2537   (gnus-set-global-variables)
2538   (gnus-summary-select-article t)
2539   ;; First we find the draft type.
2540   (let (type)
2541     (save-excursion 
2542       (set-buffer gnus-article-buffer)
2543       (widen)
2544       (gnus-narrow-to-headers)
2545       (setq type (condition-case ()
2546                      (read (mail-fetch-field "x-gnus-draft-type"))
2547                    (error nil)))
2548       (widen))
2549     (or type
2550         (error "Unknown draft type"))
2551     ;; Get to the proper buffer.
2552     (set-buffer (get-buffer-create (nth 1 type)))
2553     ;; It might be modified.
2554     (and (buffer-modified-p)
2555          (or (gnus-yes-or-no-p "Unsent message being composed; discard it? ")
2556              (error "Break")))
2557     (setq buffer-read-only nil)
2558     (buffer-enable-undo (current-buffer))
2559     (erase-buffer)
2560     ;; Set proper mode.
2561     (funcall (car type))
2562     (gnus-inews-modify-mail-mode-map)
2563     (when (eq major-mode 'news-reply-mode)
2564       (local-set-key "\C-c\C-c" 'gnus-inews-news))
2565     ;; Arrange for deletion of the draft after successful sending.
2566     (make-local-variable 'gnus-message-sent-hook)
2567     (setq gnus-message-sent-hook
2568           (list
2569            `(lambda ()
2570               (gnus-request-expire-articles 
2571                (quote ,(list (cdr gnus-article-current)))
2572                ,gnus-newsgroup-name t)
2573                 (and (buffer-name ,gnus-summary-buffer)
2574                      (save-excursion
2575                        (set-buffer ,gnus-summary-buffer)
2576                        (gnus-summary-mark-article 
2577                         ,(cdr gnus-article-current) gnus-canceled-mark))))))
2578     ;; Insert the draft.
2579     (insert-buffer-substring gnus-article-buffer)
2580     ;; Insert the separator.
2581     (goto-char (point-min))
2582     (search-forward "\n\n")
2583     (forward-char -1)
2584     (insert mail-header-separator)
2585     ;; Remove the draft header.
2586     (gnus-inews-narrow-to-headers)
2587     (nnheader-remove-header "x-gnus-draft-type")
2588     (widen)
2589     ;; Configure windows.
2590     (let ((gnus-draft-buffer (current-buffer)))
2591       (gnus-configure-windows 'draft))
2592     ;; Put point where you left it.
2593     (goto-char (nth 3 type))))
2594   
2595 (defun gnus-configure-posting-styles ()
2596   "Configure posting styles according to `gnus-posting-styles'."
2597   (let ((styles gnus-posting-styles)
2598         (gnus-newsgroup-name (or gnus-newsgroup-name ""))
2599         style match variable attribute value value-value)
2600     ;; Go through all styles and look for matches.
2601     (while styles
2602       (setq style (pop styles)
2603             match (pop style))
2604       (when (cond ((stringp match)
2605                    ;; Regexp string match on the group name.
2606                    (string-match match gnus-newsgroup-name))
2607                   ((or (symbolp match)
2608                        (gnus-functionp match))
2609                    (cond ((gnus-functionp match)
2610                           ;; Function to be called.
2611                           (funcall match))
2612                          ((boundp match)
2613                           ;; Variable to be checked.
2614                           (symbol-value match))))
2615                   ((listp match)
2616                    ;; This is a form to be evaled.
2617                    (eval match)))
2618         ;; We have a match, so we set the variables.
2619         (while style
2620           (setq attribute (pop style)
2621                 value (cdr attribute))
2622           ;; We find the variable that is to be modified.
2623           (if (and (not (stringp (car attribute)))
2624                    (not (setq variable (cdr (assq (car attribute) 
2625                                                   gnus-posting-style-alist)))))
2626               (message "Couldn't find attribute %s" (car attribute))
2627             ;; We set the variable.
2628             (setq value-value
2629                   (cond ((stringp value)
2630                          value)
2631                         ((or (symbolp value)
2632                              (gnus-functionp value))
2633                          (cond ((gnus-functionp value)
2634                                 (funcall value))
2635                                ((boundp value)
2636                                 (symbol-value value))))
2637                         ((listp value)
2638                          (eval value))))
2639             (if variable
2640                 (progn
2641                   ;; This is an ordinary variable.
2642                   (make-local-variable variable)
2643                   (set variable value-value))
2644               ;; This is a header to be added to the headers when
2645               ;; posting. 
2646               (when value-value
2647                 (make-local-variable gnus-required-headers)
2648                 (make-local-variable gnus-required-mail-headers)
2649                 (push (cons (car attribute) value-value) 
2650                       gnus-required-headers)
2651                 (push (cons (car attribute) value-value) 
2652                       gnus-required-mail-headers)))))))))
2653
2654 ;;; Allow redefinition of functions.
2655
2656 (gnus-ems-redefine)
2657
2658 (provide 'gnus-msg)
2659
2660 ;;; gnus-msg.el ends here