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