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