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