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