*** 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 Gnus*")
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           (gnus-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-new-empty-mail ()
1812   "Create a new, virtually empty mail mode buffer."
1813   (pop-to-buffer gnus-mail-buffer)
1814   (gnus-mail-setup 'new "" ""))
1815
1816 (defun gnus-mail-reply (&optional yank to-address followup)
1817   (save-excursion
1818     (set-buffer gnus-summary-buffer)
1819     (let ((group (gnus-group-real-name gnus-newsgroup-name))
1820           (cur (cons (current-buffer) (cdr gnus-article-current)))
1821           (winconf (current-window-configuration))
1822           from subject date reply-to message-of to cc
1823           references message-id sender follow-to sendto elt new-cc new-to
1824           mct mctdo)
1825       (set-buffer (get-buffer-create gnus-mail-buffer))
1826       (mail-mode)
1827       (if (and (buffer-modified-p)
1828                (> (buffer-size) 0)
1829                (not (gnus-y-or-n-p 
1830                      "Unsent message being composed; erase it? ")))
1831           ()
1832         (erase-buffer)
1833         (save-excursion
1834           (gnus-copy-article-buffer)
1835           (save-restriction
1836             (set-buffer gnus-article-copy)
1837             (nnheader-narrow-to-headers)
1838             (if (not followup)
1839                 ;; This is a regular reply.
1840                 (if (gnus-functionp gnus-reply-to-function)
1841                     (setq follow-to (funcall gnus-reply-to-function group)))
1842               ;; This is a followup.
1843               (if (gnus-functionp gnus-followup-to-function)
1844                   (save-excursion
1845                     (setq follow-to
1846                           (funcall gnus-followup-to-function group)))))
1847             (setq from (mail-fetch-field "from"))
1848             (setq date (or (mail-fetch-field "date") 
1849                            (mail-header-date gnus-current-headers)))
1850             (setq message-of (gnus-message-of from date))
1851             (setq sender (mail-fetch-field "sender"))
1852             (setq subject (or (mail-fetch-field "subject") "none"))
1853             ;; Remove any (buggy) Re:'s that are present and make a
1854             ;; proper one.
1855             (and (string-match "^[ \t]*[Re][Ee]:[ \t]*" subject)
1856                  (setq subject (substring subject (match-end 0))))
1857             (setq subject (concat "Re: " subject))
1858             (setq to (mail-fetch-field "to"))
1859             (setq cc (mail-fetch-field "cc"))
1860             (setq mct (mail-fetch-field "mail-copies-to"))
1861             (setq reply-to
1862                   (unless (gnus-group-get-parameter group 'broken-reply-to)
1863                     (mail-fetch-field "reply-to")))
1864             (setq references (mail-fetch-field "references"))
1865             (setq message-id (mail-fetch-field "message-id"))
1866             
1867             (setq mctdo (not (equal mct "never")))
1868
1869             (if (not (and followup (not to-address)))
1870                 (setq new-to (or reply-to from))
1871               (let (ccalist)
1872                 (save-excursion
1873                   (gnus-set-work-buffer)
1874                   (unless (equal mct "never")
1875                     (insert (or reply-to from "")))
1876                   (insert 
1877                    (if (bolp) "" ", ") (or to "")
1878                    (if (or (not mct) (not mctdo)) ""
1879                      (concat (if (bolp) "" ", ") mct))
1880                    (if cc (concat (if (bolp) "" ", ") cc) ""))
1881                   ;; Remove addresses that match `rmail-dont-reply-to-names'. 
1882                   (insert (prog1 (rmail-dont-reply-to (buffer-string))
1883                             (erase-buffer)))
1884                   (goto-char (point-min))
1885                   (setq ccalist
1886                         (mapcar
1887                          (lambda (addr)
1888                            (cons (mail-strip-quoted-names addr) addr))
1889                          (nreverse (gnus-mail-parse-comma-list))))
1890                   (let ((s ccalist))
1891                     (while s
1892                       (setq ccalist (delq (assoc (car (pop s)) s) ccalist)))))
1893                 (setq new-to (cdr (pop ccalist)))
1894                 (setq new-cc 
1895                       (mapconcat 
1896                        (lambda (addr) (cdr addr))
1897                        ccalist ", "))))
1898             (widen)))
1899
1900         (setq news-reply-yank-from (or from "(nobody)"))
1901         (setq news-reply-yank-message-id
1902               (or message-id "(unknown Message-ID)"))
1903
1904         ;; Gather the "to" addresses out of the follow-to list and remove
1905         ;; them as we go.
1906         (if (and follow-to (listp follow-to))
1907             (while (setq elt (assoc "To" follow-to))
1908               (setq sendto (concat sendto (and sendto ", ") (cdr elt)))
1909               (setq follow-to (delq elt follow-to))))
1910
1911         (gnus-mail-setup 
1912          (if followup 'followup 'reply)
1913          (or to-address 
1914              (if (and follow-to (not (stringp follow-to))) sendto
1915                (or follow-to new-to sender "")))
1916          subject message-of
1917          (if (zerop (length new-cc)) nil new-cc)
1918          gnus-article-copy)
1919
1920         (make-local-variable 'gnus-article-reply)
1921         (setq gnus-article-reply cur)
1922         (make-local-variable 'gnus-prev-winconf)
1923         (setq gnus-prev-winconf winconf)
1924         (make-local-variable 'gnus-reply-subject)
1925         (setq gnus-reply-subject subject)
1926         (make-local-variable 'gnus-in-reply-to)
1927         (setq gnus-in-reply-to message-of)
1928
1929         (auto-save-mode auto-save-default)
1930         (gnus-inews-insert-gcc)
1931         (gnus-inews-insert-archive-gcc)
1932
1933         (when (and follow-to (listp follow-to))
1934           (let (beg)
1935             (gnus-inews-narrow-to-headers)
1936             (re-search-forward "^To:" nil t)
1937             (beginning-of-line)
1938             (forward-line 1)
1939             (setq beg (point))
1940             ;; Insert the rest of the Follow-To headers.
1941             (while follow-to
1942               (goto-char (point-min))
1943               (if (not (re-search-forward 
1944                         (concat "^" (caar follow-to) ": *") nil t))
1945                   (progn
1946                     (goto-char beg)
1947                     (insert (caar follow-to) ": " (cdar follow-to) "\n"))
1948                 (unless (eolp)
1949                   (insert ", "))
1950                 (insert (cdar follow-to)))
1951               (setq follow-to (cdr follow-to)))
1952             (widen)))
1953         (nnheader-insert-references references message-id)
1954
1955         ;; Now the headers should be ok, so we do the yanking.
1956         (goto-char (point-min))
1957         (re-search-forward
1958          (concat "^" (regexp-quote mail-header-separator) "$"))
1959         (forward-line 1)
1960         (if (not yank)
1961             (gnus-configure-windows 'reply 'force)
1962           (let ((last (point))
1963                 end)
1964             (if (not (listp yank))
1965                 (progn
1966                   ;; Just a single article being yanked.
1967                   (save-excursion
1968                     (mail-yank-original nil))
1969                   (or mail-yank-hooks mail-citation-hook
1970                       (run-hooks 'news-reply-header-hook)))
1971               (while yank
1972                 (save-window-excursion
1973                   (set-buffer gnus-summary-buffer)
1974                   (gnus-summary-select-article nil nil nil (car yank))
1975                   (gnus-summary-remove-process-mark (car yank)))
1976                 (save-excursion
1977                   (setq end (point))
1978                   (gnus-copy-article-buffer)
1979                   (mail-yank-original nil)
1980                   (save-restriction
1981                     (narrow-to-region (point-min) (point))
1982                     (goto-char (mark t))
1983                     (let ((news-reply-yank-from
1984                            (save-excursion 
1985                              (set-buffer gnus-article-buffer)
1986                              (or (mail-fetch-field "from") "(nobody)")))
1987                           (news-reply-yank-message-id
1988                            (save-excursion 
1989                              (set-buffer gnus-article-buffer)
1990                              (or (mail-fetch-field "message-id")
1991                                  "(unknown Message-ID)"))))
1992                       (or mail-yank-hooks mail-citation-hook
1993                           (run-hooks 'news-reply-header-hook))
1994                       (setq end (point-max)))))
1995                 (goto-char end)
1996                 (setq yank (cdr yank))))
1997             (goto-char last))
1998           (forward-line 2)
1999           (gnus-configure-windows 'reply-yank 'force))
2000         (run-hooks 'gnus-mail-hook)
2001         ;; Mark this buffer as unchanged.
2002         (set-buffer-modified-p nil)))))
2003
2004 (defun gnus-mail-parse-comma-list ()
2005   (let (accumulated
2006         beg)
2007     (skip-chars-forward " ")
2008     (while (not (eobp))
2009       (setq beg (point))
2010       (skip-chars-forward "^,")
2011       (while (zerop
2012               (save-excursion 
2013                 (save-restriction
2014                   (let ((i 0))
2015                     (narrow-to-region beg (point))
2016                     (goto-char beg)
2017                     (logand (progn
2018                               (while (search-forward "\"" nil t)
2019                                 (incf i))
2020                               (if (zerop i) 2 i)) 2)))))
2021         (skip-chars-forward ",")
2022         (skip-chars-forward "^,"))
2023       (skip-chars-backward " ")
2024       (setq accumulated
2025             (cons (buffer-substring beg (point))
2026                   accumulated))
2027       (skip-chars-forward "^,")
2028       (skip-chars-forward ", "))
2029     accumulated))
2030
2031 (defun gnus-new-news (&optional group inhibit-prompt)
2032   "Set up a *Post Gnus* buffer that points to GROUP.
2033 If INHIBIT-PROMPT, never prompt for a Subject."
2034   (let ((winconf (current-window-configuration))
2035         subject)
2036     (when (and gnus-interactive-post
2037                (not inhibit-prompt)
2038                (not gnus-expert-user))
2039       (setq subject (read-string "Subject: ")))
2040     (pop-to-buffer gnus-post-news-buffer)  
2041     (erase-buffer)
2042     (news-reply-mode)
2043     ;; Let posting styles be configured.
2044     (gnus-configure-posting-styles)
2045     (news-setup nil subject nil (and group (gnus-group-real-name group)) nil)
2046     ;; Associate this buffer with the draft group.
2047     (gnus-enter-buffer-into-draft)
2048     (goto-char (point-min))
2049
2050     (unless (re-search-forward 
2051              (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2052       (goto-char (point-max)))
2053     (insert "\n\n")
2054
2055     (gnus-inews-insert-bfcc)
2056     (gnus-inews-insert-gcc)
2057     (gnus-inews-insert-archive-gcc)
2058     (gnus-inews-insert-signature)
2059     (and gnus-post-prepare-function
2060          (gnus-functionp gnus-post-prepare-function)
2061          (funcall gnus-post-prepare-function group))
2062     (run-hooks 'gnus-post-prepare-hook)
2063     (gnus-inews-set-point)
2064     (make-local-variable 'gnus-prev-winconf)
2065     (setq gnus-prev-winconf winconf)
2066     (gnus-inews-modify-mail-mode-map)
2067     (local-set-key "\C-c\C-c" 'gnus-inews-news)))
2068
2069 (defun gnus-news-followup (&optional yank group)
2070   (save-excursion
2071     (set-buffer gnus-summary-buffer)
2072     (if (not (or (not gnus-novice-user)
2073                  gnus-expert-user
2074                  (gnus-y-or-n-p
2075                   "Are you sure you want to post to all of USENET? ")))
2076         ()
2077       (let ((group (gnus-group-real-name (or group gnus-newsgroup-name)))
2078             (cur (cons (current-buffer) (cdr gnus-article-current)))
2079             (winconf (current-window-configuration))
2080             from subject date reply-to message-of
2081             references message-id sender follow-to sendto elt 
2082             followup-to distribution newsgroups)
2083         (set-buffer (get-buffer-create gnus-post-news-buffer))
2084         (news-reply-mode)
2085         ;; Associate this buffer with the draft group.
2086         (gnus-enter-buffer-into-draft)
2087         (if (and (buffer-modified-p)
2088                  (> (buffer-size) 0)
2089                  (not (gnus-y-or-n-p 
2090                        "Unsent message being composed; erase it? ")))
2091             ()
2092           (erase-buffer)
2093           (save-excursion
2094             (gnus-copy-article-buffer)
2095             (save-restriction
2096               (set-buffer gnus-article-copy)
2097               (nnheader-narrow-to-headers)
2098               (if (gnus-functionp gnus-followup-to-function)
2099                   (save-excursion
2100                     (setq follow-to
2101                           (funcall gnus-followup-to-function group))))
2102               (setq from (mail-fetch-field "from"))
2103               (setq date (or (mail-fetch-field "date") 
2104                              (mail-header-date gnus-current-headers)))
2105               (setq message-of (gnus-message-of from date))
2106               (setq subject (or (mail-fetch-field "subject") "none"))
2107               ;; Remove any (buggy) Re:'s that are present and make a
2108               ;; proper one.
2109               (and (string-match "^[ \t]*[Re][Ee]:[ \t]*" subject)
2110                    (setq subject (substring subject (match-end 0))))
2111               (setq subject (concat "Re: " subject))
2112               (setq references (mail-fetch-field "references"))
2113               (setq message-id (mail-fetch-field "message-id"))
2114               (setq followup-to (mail-fetch-field "followup-to"))
2115               (setq newsgroups (mail-fetch-field "newsgroups"))
2116               (setq distribution (mail-fetch-field "distribution"))
2117               ;; Remove bogus distribution.
2118               (and (stringp distribution)
2119                    (string-match "world" distribution)
2120                    (setq distribution nil))
2121               (widen)))
2122
2123           (setq news-reply-yank-from (or from "(nobody)"))
2124           (setq news-reply-yank-message-id
2125                 (or message-id "(unknown Message-ID)"))
2126
2127           ;; Gather the "to" addresses out of the follow-to list and remove
2128           ;; them as we go.
2129           (if (and follow-to (listp follow-to))
2130               (while (setq elt (assoc "Newsgroups" follow-to))
2131                 (setq sendto (concat sendto (and sendto ", ") (cdr elt)))
2132                 (setq follow-to (delq elt follow-to))))
2133
2134           ;; Let posting styles be configured.
2135           (gnus-configure-posting-styles)
2136
2137           (news-setup
2138            nil subject nil 
2139            (or sendto 
2140                (and followup-to
2141                     gnus-use-followup-to
2142                     (or (not (eq gnus-use-followup-to 'ask))
2143                         (equal followup-to newsgroups)
2144                         (gnus-y-or-n-p 
2145                          (format "Use Followup-To %s? " followup-to)))
2146                     followup-to)
2147                newsgroups group "")
2148            gnus-article-copy)
2149
2150           (make-local-variable 'gnus-article-reply)
2151           (setq gnus-article-reply cur)
2152           (make-local-variable 'gnus-prev-winconf)
2153           (setq gnus-prev-winconf winconf)
2154           (make-local-variable 'gnus-reply-subject)
2155           (setq gnus-reply-subject (mail-header-subject gnus-current-headers))
2156           (make-local-variable 'gnus-in-reply-to)
2157           (setq gnus-in-reply-to message-of)
2158           (when (and followup-to newsgroups)
2159             (make-local-variable 'gnus-newsgroup-followup)
2160             (setq gnus-newsgroup-followup
2161                   (cons newsgroups followup-to)))
2162
2163           (gnus-inews-insert-signature)
2164
2165           (and gnus-post-prepare-function
2166                (gnus-functionp gnus-post-prepare-function)
2167                (funcall gnus-post-prepare-function group))
2168           (run-hooks 'gnus-post-prepare-hook)
2169
2170           (auto-save-mode auto-save-default)
2171           (gnus-inews-modify-mail-mode-map)
2172           (local-set-key "\C-c\C-c" 'gnus-inews-news)
2173
2174           (if (and follow-to (listp follow-to))
2175               (progn
2176                 (goto-char (point-min))
2177                 (and (re-search-forward "^Newsgroups:" nil t)
2178                      (forward-line 1))
2179                 (while follow-to
2180                   (insert (car (car follow-to)) ": " 
2181                           (cdr (car follow-to)) "\n")
2182                   (setq follow-to (cdr follow-to)))))
2183           
2184           ;; If a distribution existed, we use it.
2185           (if distribution
2186               (progn
2187                 (mail-position-on-field "Distribution")
2188                 (insert distribution)))
2189           
2190           (nnheader-insert-references references message-id)
2191
2192           ;; Handle `gnus-auto-mail-to-author'.
2193           ;; Suggested by Daniel Quinlan <quinlan@best.com>.
2194           ;; Revised to respect Reply-To by Ulrik Dickow <dickow@nbi.dk>.
2195           (let ((to (if (if (eq gnus-auto-mail-to-author 'ask)
2196                             (y-or-n-p "Also send mail to author? ")
2197                           gnus-auto-mail-to-author)
2198                         (or (save-excursion
2199                               (set-buffer gnus-article-copy)
2200                               (unless (gnus-group-get-parameter
2201                                        group 'broken-reply-to)
2202                                 (gnus-fetch-field "reply-to")))
2203                             from)))
2204                 (x-mail (save-excursion
2205                           (set-buffer gnus-article-copy)
2206                           (gnus-fetch-field "x-mail-copy-to"))))
2207             ;; Deny sending copy if there's a negative X-Mail-Copy-To
2208             ;; header. 
2209             (if x-mail
2210                 (if (and (string= x-mail "never")
2211                          (not (eq gnus-auto-mail-to-author 'force)))
2212                     (setq to nil)
2213                   (setq to x-mail)))
2214             ;; Insert a To or Cc header.
2215             (if to
2216                 (if (mail-fetch-field "To")
2217                     (progn
2218                       (beginning-of-line)
2219                       (insert "Cc: " to "\n"))
2220                   (mail-position-on-field "To")
2221                   (insert to))))
2222
2223           (gnus-inews-insert-bfcc)
2224           (gnus-inews-insert-gcc)
2225           (gnus-inews-insert-archive-gcc)
2226
2227           ;; Now the headers should be ok, so we do the yanking.
2228           (goto-char (point-min))
2229           (re-search-forward
2230            (concat "^" (regexp-quote mail-header-separator) "$"))
2231           (forward-line 1)
2232           (if (not yank)
2233               (progn
2234                 (gnus-configure-windows 'followup 'force)
2235                 (insert "\n\n")
2236                 (forward-line -2))
2237             (let ((last (point))
2238                   end)
2239               (if (not (listp yank))
2240                   (progn
2241                     (save-excursion
2242                       (mail-yank-original nil))
2243                     (or mail-yank-hooks mail-citation-hook
2244                         (run-hooks 'news-reply-header-hook)))
2245                 (while yank
2246                   (save-window-excursion
2247                     (set-buffer gnus-summary-buffer)
2248                     (gnus-summary-select-article nil nil nil (car yank))
2249                     (gnus-summary-remove-process-mark (car yank)))
2250                   (save-excursion
2251                     (gnus-copy-article-buffer)
2252                     (mail-yank-original nil)
2253                     (setq end (point)))
2254                   (or mail-yank-hooks mail-citation-hook
2255                       (run-hooks 'news-reply-header-hook))
2256                   (goto-char end)
2257                   (setq yank (cdr yank))))
2258               (goto-char last))
2259             (gnus-configure-windows 'followup-yank 'force))
2260         
2261           (make-local-variable 'gnus-article-check-size)
2262           (setq gnus-article-check-size
2263                 (cons (buffer-size) (gnus-article-checksum)))
2264           (gnus-inews-set-point))))))
2265
2266 (defun gnus-message-of (from date)
2267   "Take a FROM and a DATE and return an IN-REPLY-TO."
2268   (when from
2269     (let ((stop-pos 
2270            (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
2271       (concat (if stop-pos (substring from 0 stop-pos) from)
2272               "'s message of " 
2273               (if (or (not date) (string= date ""))
2274                   "(unknown date)" date)))))
2275
2276 (defun gnus-mail-yank-original ()
2277   (interactive)
2278   (save-excursion
2279     (mail-yank-original nil))
2280   (or mail-yank-hooks mail-citation-hook
2281       (run-hooks 'news-reply-header-hook)))
2282
2283 (defun gnus-mail-send-and-exit (&optional dont-send)
2284   "Send the current mail and return to Gnus."
2285   (interactive)
2286   (let* ((reply gnus-article-reply)
2287          (winconf gnus-prev-winconf)
2288          (address-group gnus-add-to-address)
2289          (to-address (and address-group
2290                           (mail-fetch-field "to"))))
2291     (setq gnus-add-to-address nil)
2292     (let ((buffer-file-name nil))
2293       (or dont-send (gnus-mail-send)))
2294     (bury-buffer)
2295     ;; This mail group doesn't have a `to-list', so we add one
2296     ;; here.  Magic!  
2297     (and to-address
2298          (gnus-group-add-parameter 
2299           address-group (cons 'to-list to-address)))
2300     (if (get-buffer gnus-group-buffer)
2301         (progn
2302           (if (gnus-buffer-exists-p (car-safe reply))
2303               (progn
2304                 (set-buffer (car reply))
2305                 (and (cdr reply)
2306                      (gnus-summary-mark-article-as-replied 
2307                       (cdr reply)))))
2308           (and winconf (set-window-configuration winconf))))))
2309
2310 (defun gnus-kill-message-buffer ()
2311   "Kill the current buffer after dissociating it from the draft group."
2312   (interactive)
2313   (when (gnus-y-or-n-p "Dissociate and kill the current buffer? ")
2314     (gnus-dissociate-buffer-from-draft)
2315     (kill-buffer (current-buffer))))
2316
2317 (defun gnus-put-message ()
2318   "Put the current message in some group and return to Gnus."
2319   (interactive)
2320   (let ((reply gnus-article-reply)
2321         (winconf gnus-prev-winconf)
2322         (group gnus-newsgroup-name)
2323         buf)
2324     
2325     (or (and group (not (gnus-group-read-only-p group)))
2326         (setq group (read-string "Put in group: " nil
2327                                  (gnus-writable-groups))))
2328     (and (gnus-gethash group gnus-newsrc-hashtb)
2329          (error "No such group: %s" group))
2330
2331     (save-excursion
2332       (save-restriction
2333         (widen)
2334         (gnus-inews-narrow-to-headers)
2335         (let (gnus-deletable-headers)
2336           (if (eq major-mode 'mail-mode)
2337               (gnus-inews-insert-headers gnus-required-mail-headers)
2338             (gnus-inews-insert-headers)))
2339         (goto-char (point-max))
2340         (insert "Gcc: " group "\n")
2341         (widen)))
2342
2343     (gnus-inews-do-gcc)
2344
2345     (if (get-buffer gnus-group-buffer)
2346         (progn
2347           (if (gnus-buffer-exists-p (car-safe reply))
2348               (progn
2349                 (set-buffer (car reply))
2350                 (and (cdr reply)
2351                      (gnus-summary-mark-article-as-replied 
2352                       (cdr reply)))))
2353           (and winconf (set-window-configuration winconf))))))
2354
2355 (defun gnus-forward-make-subject (buffer)
2356   (save-excursion
2357     (set-buffer buffer)
2358     (concat "[" (if (memq 'mail (assoc (symbol-name 
2359                                         (car (gnus-find-method-for-group 
2360                                               gnus-newsgroup-name)))
2361                                        gnus-valid-select-methods))
2362                     (gnus-fetch-field "From")
2363                   gnus-newsgroup-name)
2364             "] " (or (gnus-fetch-field "Subject") ""))))
2365
2366 (defun gnus-forward-insert-buffer (buffer)
2367   (save-excursion
2368     (save-restriction
2369       ;; Put point where we want it before inserting the forwarded
2370       ;; message. 
2371       (if gnus-signature-before-forwarded-message
2372           (goto-char (point-max))
2373         (goto-char (point-min))
2374         (re-search-forward
2375          (concat "^" (regexp-quote mail-header-separator) "$"))
2376         (forward-line 1))
2377       ;; Narrow to the area we are to insert.
2378       (narrow-to-region (point) (point))
2379       ;; Insert the separators and the forwarded buffer.
2380       (insert gnus-forward-start-separator)
2381       (insert-buffer-substring buffer)
2382       (goto-char (point-max))
2383       (insert gnus-forward-end-separator)
2384       (gnus-set-text-properties (point-min) (point-max) nil)
2385       ;; Remove all unwanted headers.
2386       (goto-char (point-min))
2387       (forward-line 1)
2388       (save-restriction
2389         (narrow-to-region (point) (if (search-forward "\n\n" nil t)
2390                                       (1- (point))
2391                                     (point)))
2392         (goto-char (point-min))
2393         (let ((case-fold-search t))
2394           (delete-non-matching-lines gnus-forward-included-headers))))))
2395
2396 (defun gnus-mail-forward (&optional buffer)
2397   "Forward the current message to another user using mail."
2398   (let* ((forward-buffer (or buffer (current-buffer)))
2399          (winconf (current-window-configuration))
2400          (subject (gnus-forward-make-subject forward-buffer)))
2401     (set-buffer (get-buffer-create gnus-mail-buffer))
2402     (if (and (buffer-modified-p)
2403              (> (buffer-size) 0)
2404              (not (gnus-y-or-n-p 
2405                    "Unsent message being composed; erase it? ")))
2406         ()
2407       (erase-buffer)
2408       (gnus-mail-setup 'forward nil subject)
2409       (make-local-variable 'gnus-prev-winconf)
2410       (setq gnus-prev-winconf winconf)
2411       (gnus-forward-insert-buffer forward-buffer)
2412       (goto-char (point-min))
2413       (re-search-forward "^To: ?" nil t)
2414       (gnus-configure-windows 'mail-forward 'force)
2415       ;; You have a chance to arrange the message.
2416       (run-hooks 'gnus-mail-forward-hook)
2417       (run-hooks 'gnus-mail-hook))))
2418
2419 (defun gnus-forward-using-post (&optional buffer)
2420   (save-excursion
2421     (let* ((forward-buffer (or buffer (current-buffer))) 
2422            (subject (gnus-forward-make-subject forward-buffer))
2423            (gnus-newsgroup-name nil))
2424       (gnus-post-news 'post nil nil nil nil subject)
2425       (save-excursion
2426         (gnus-forward-insert-buffer forward-buffer)
2427         ;; You have a chance to arrange the message.
2428         (run-hooks 'gnus-mail-forward-hook)))))
2429
2430 (defun gnus-mail-other-window-using-mail ()
2431   "Compose mail other window using mail."
2432   (let ((winconf (current-window-configuration)))
2433     (mail-other-window nil nil nil nil nil (get-buffer gnus-article-buffer))
2434     (gnus-inews-modify-mail-mode-map)
2435     (make-local-variable 'gnus-prev-winconf)
2436     (setq gnus-prev-winconf winconf)
2437     (run-hooks 'gnus-mail-hook)
2438     (gnus-configure-windows 'summary-mail 'force)))
2439
2440 (defun gnus-article-mail (yank)
2441   "Send a reply to the address near point.
2442 If YANK is non-nil, include the original article."
2443   (interactive "P")
2444   (let ((address 
2445          (buffer-substring
2446           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
2447           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
2448     (and address
2449          (progn
2450            (switch-to-buffer gnus-summary-buffer)
2451            (gnus-mail-reply yank address)))))
2452
2453 (defun gnus-bug ()
2454   "Send a bug report to the Gnus maintainers."
2455   (interactive)
2456   (let ((winconf (current-window-configuration)))
2457     (delete-other-windows)
2458     (switch-to-buffer "*Gnus Help Bug*")
2459     (erase-buffer)
2460     (insert gnus-bug-message)
2461     (goto-char (point-min))
2462     (pop-to-buffer "*Gnus Bug*")
2463     (erase-buffer)
2464     (mail-mode)
2465     (mail-setup gnus-maintainer nil nil nil nil nil)
2466     (auto-save-mode auto-save-default)
2467     (make-local-variable 'gnus-prev-winconf)
2468     (setq gnus-prev-winconf winconf)
2469     (gnus-inews-modify-mail-mode-map)
2470     (local-set-key "\C-c\C-c" 'gnus-bug-mail-send-and-exit)
2471     (goto-char (point-min))
2472     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2473     (forward-line 1)
2474     (insert (gnus-version) "\n")
2475     (insert (emacs-version))
2476     (insert "\n\n\n\n\n")
2477     (gnus-debug)
2478     (goto-char (point-min))
2479     (search-forward "Subject: " nil t)
2480     (message "")))
2481
2482 (defun gnus-bug-mail-send-and-exit ()
2483   "Send the bug message and exit."
2484   (interactive)
2485   (let ((cur (current-buffer)))
2486     (and (get-buffer "*Gnus Help Bug*")
2487          (kill-buffer "*Gnus Help Bug*"))
2488     (gnus-mail-send-and-exit)
2489     (when (buffer-name cur)
2490       (kill-buffer cur))))
2491
2492 (defun gnus-debug ()
2493   "Attemps to go through the Gnus source file and report what variables have been changed.
2494 The source file has to be in the Emacs load path."
2495   (interactive)
2496   (let ((files '("gnus.el" "gnus-msg.el" "gnus-score.el" "nnmail.el"))
2497         file dirs expr olist sym)
2498     (message "Please wait while we snoop your variables...")
2499     (sit-for 0)
2500     (save-excursion
2501       (set-buffer (get-buffer-create " *gnus bug info*"))
2502       (buffer-disable-undo (current-buffer))
2503       (while files
2504         (erase-buffer)
2505         (setq dirs load-path)
2506         (while dirs
2507           (if (or (not (car dirs))
2508                   (not (stringp (car dirs)))
2509                   (not (file-exists-p 
2510                         (setq file (concat (file-name-as-directory 
2511                                             (car dirs)) (car files))))))
2512               (setq dirs (cdr dirs))
2513             (setq dirs nil)
2514             (insert-file-contents file)
2515             (goto-char (point-min))
2516             (if (not (re-search-forward "^;;* *Internal variables" nil t))
2517                 (message "Malformed sources in file %s" file)
2518               (narrow-to-region (point-min) (point))
2519               (goto-char (point-min))
2520               (while (setq expr (condition-case () 
2521                                     (read (current-buffer)) (error nil)))
2522                 (condition-case ()
2523                     (and (eq (car expr) 'defvar)
2524                          (stringp (nth 3 expr))
2525                          (or (not (boundp (nth 1 expr)))
2526                              (not (equal (eval (nth 2 expr))
2527                                          (symbol-value (nth 1 expr)))))
2528                          (setq olist (cons (nth 1 expr) olist)))
2529                   (error nil))))))
2530         (setq files (cdr files)))
2531       (kill-buffer (current-buffer)))
2532     (when (setq olist (nreverse olist))
2533       (insert "------------------ Environment follows ------------------\n\n"))
2534     (while olist
2535       (if (boundp (car olist))
2536           (insert 
2537            (pp-to-string
2538             `(setq ,(symbol-name (car olist))
2539                    ,(if (or (consp (setq sym (symbol-value (car olist))))
2540                             (and (symbolp sym)
2541                                  (not (or (eq sym nil)
2542                                           (eq sym t)))))
2543                         (cons 'quote (symbol-value (car olist)))
2544                       (symbol-value (car olist)))
2545                    "\n")))
2546         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
2547       (setq olist (cdr olist)))
2548     (insert "\n\n")
2549     ;; Remove any null chars - they seem to cause trouble for some
2550     ;; mailers. (Byte-compiled output from the stuff above.) 
2551     (goto-char (point-min))
2552     (while (re-search-forward "[\000\200]" nil t)
2553       (replace-match "" t t))))
2554
2555 ;;; Treatment of rejected articles.
2556 ;;; Bounced mail.
2557
2558 (defun gnus-summary-resend-bounced-mail (fetch)
2559   "Re-mail the current message.
2560 This only makes sense if the current message is a bounce message than
2561 contains some mail you have written which has been bounced back to
2562 you.
2563 If FETCH, try to fetch the article that this is a reply to, if indeed
2564 this is a reply."
2565   (interactive "P")
2566   (gnus-summary-select-article t)
2567   ;; Create a mail buffer.
2568   (gnus-new-empty-mail)
2569   (erase-buffer)
2570   (insert-buffer-substring gnus-article-buffer)
2571   (goto-char (point-min))
2572   (or (re-search-forward "--- *Original message.*\n" nil t)
2573       (and (search-forward "\n\n" nil t)
2574            (re-search-forward "^Return-Path:.*\n" nil t)))
2575   ;; We remove everything before the bounced mail.
2576   (delete-region 
2577    (point-min)
2578    (if (re-search-forward "[^ \t]*:" nil t)
2579        (match-beginning 0)
2580      (point)))
2581   (let (references)
2582     (save-excursion
2583       (save-restriction
2584         (nnheader-narrow-to-headers)
2585         (nnheader-remove-header gnus-bounced-headers-junk t)
2586         (setq references (mail-fetch-field "references"))
2587         (goto-char (point-max))
2588         (insert mail-header-separator)))
2589     ;; If there are references, we fetch the article we answered to.  
2590     (and fetch 
2591          references
2592          (string-match "\\(<[^]+>\\)[ \t]*$" references)
2593          (gnus-summary-refer-article 
2594           (substring references (match-beginning 1) (match-end 1)))
2595          (progn
2596            (gnus-summary-show-all-headers)
2597            (gnus-configure-windows 'compose-bounce))))
2598   (goto-char (point-min)))
2599
2600 ;;; Sending mail.
2601
2602 (defun gnus-mail-send (&optional no-generated-headers)
2603   "Send the current buffer as mail.
2604 Headers will be generated before sending."
2605   (interactive)
2606   (unless no-generated-headers
2607     (save-excursion
2608       (save-restriction
2609         (widen)
2610         (gnus-inews-narrow-to-headers)
2611         (gnus-inews-insert-headers gnus-required-mail-headers)
2612         (gnus-inews-remove-empty-headers))))
2613   (widen)
2614   ;; Remove the header separator.
2615   (goto-char (point-min))
2616   (and (re-search-forward
2617         (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2618        (replace-match "" t t))
2619   ;; Run final inews hooks.  This hook may do FCC.
2620   (run-hooks 'gnus-inews-article-hook)
2621   (gnus-inews-do-gcc)
2622   (nnheader-narrow-to-headers)
2623   (nnheader-remove-header "^[gf]cc:" t)
2624   (widen)
2625   (goto-char (point-min))
2626   (search-forward "\n\n")
2627   (forward-char -1)
2628   (insert mail-header-separator)
2629   (mail-send)
2630   (run-hooks 'gnus-message-sent-hook))
2631
2632 (defun gnus-inews-modify-mail-mode-map ()
2633   (use-local-map (copy-keymap (current-local-map)))
2634   (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
2635   (local-set-key "\C-c\M-\C-p" 'gnus-put-message)
2636   (local-set-key "\C-c\C-k" 'gnus-kill-message-buffer)
2637   (local-set-key "\C-c\M-d" 'gnus-dissociate-buffer-from-draft)
2638   (local-set-key "\C-c\C-d" 'gnus-associate-buffer-with-draft))
2639
2640 (defun gnus-mail-setup (type &optional to subject in-reply-to cc
2641                              replybuffer actions)
2642   ;; Let posting styles be configured.
2643   (gnus-configure-posting-styles)
2644   (funcall
2645    (cond
2646     ((or 
2647       (eq gnus-mail-method 'mh)
2648       (and (or (eq type 'reply) (eq type 'followup))
2649            (eq gnus-mail-reply-method 'gnus-mail-reply-using-mhe))
2650       (and (eq type 'forward)
2651            (eq gnus-mail-forward-method 'gnus-mail-forward-using-mhe))
2652       (and (eq type 'new) 
2653            (eq gnus-mail-other-window-method 
2654                'gnus-mail-other-window-using-mhe)))
2655      'gnus-mh-mail-setup)
2656     ((or 
2657       (eq gnus-mail-method 'vm)
2658       (and (or (eq type 'reply) (eq type 'followup)) 
2659            (eq gnus-mail-reply-method 'gnus-mail-reply-using-vm))
2660       (and (eq type 'forward)
2661            (eq gnus-mail-forward-method 'gnus-mail-forward-using-vm))
2662       (and (eq type 'new) 
2663            (eq gnus-mail-other-window-method 
2664                'gnus-mail-other-window-using-vm)))
2665      'gnus-vm-mail-setup)
2666     ((or
2667       (eq gnus-mail-method 'sendmail)
2668       (and (or (eq type 'reply) (eq type 'followup)) 
2669            (eq gnus-mail-reply-method 'gnus-mail-reply-using-mail))
2670       (and (eq type 'forward)
2671            (eq gnus-mail-forward-method 'gnus-mail-forward-using-mail))
2672       (and (eq type 'new) 
2673            (eq gnus-mail-other-window-method 
2674                'gnus-mail-other-window-using-mail)))
2675      'gnus-sendmail-mail-setup)
2676     (t
2677      (cond ((or (eq type 'reply) (eq type 'followup))
2678             gnus-mail-reply-method)
2679            ((eq type 'forward)
2680             gnus-mail-forward-method)
2681            ((eq type 'new) 
2682             gnus-mail-other-window-method))))
2683    to subject in-reply-to cc replybuffer actions)
2684   ;; Associate this mail buffer with the draft group.
2685   (gnus-enter-buffer-into-draft))
2686
2687 (defun gnus-sendmail-mail-setup (to subject in-reply-to cc replybuffer actions)
2688   (mail-mode)
2689   (mail-setup to subject nil cc replybuffer actions)
2690   (gnus-inews-set-point)
2691   (gnus-inews-modify-mail-mode-map))
2692   
2693 ;;; Gcc handling.
2694
2695 ;; Do Gcc handling, which copied the message over to some group. 
2696 (defun gnus-inews-do-gcc (&optional gcc)
2697   (save-excursion
2698     (save-restriction
2699       (nnheader-narrow-to-headers)
2700       (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
2701             (cur (current-buffer))
2702             end groups group method)
2703         (when gcc
2704           (nnheader-remove-header "gcc")
2705           (widen)
2706           (setq groups (gnus-tokenize-header gcc " ,"))
2707           ;; Copy the article over to some group(s).
2708           (while (setq group (pop groups))
2709             (gnus-check-server 
2710              (setq method
2711                    (cond ((and (null (gnus-get-info group))
2712                                (eq (car gnus-message-archive-method)
2713                                    (car 
2714                                     (gnus-server-to-method
2715                                      (gnus-group-method group)))))
2716                           ;; If the group doesn't exist, we assume
2717                           ;; it's an archive group...
2718                           gnus-message-archive-method)
2719                          (t (gnus-find-method-for-group group)))))
2720             (unless (gnus-request-group group t method)
2721               (gnus-request-create-group group method))
2722             (gnus-check-server method)
2723             (save-excursion
2724               (nnheader-set-temp-buffer " *acc*")
2725               (insert-buffer-substring cur)
2726               (unless (condition-case ()
2727                           (gnus-request-accept-article group t method)
2728                         (error nil))
2729                 (gnus-message 1 "Couldn't store article in group %s: %s" 
2730                               group (gnus-status-message method))
2731                 (sit-for 2))
2732               (kill-buffer (current-buffer)))))))))
2733
2734 (defun gnus-inews-insert-bfcc ()
2735   "Insert Bcc and Fcc headers."
2736   (save-excursion
2737     ;; Handle author copy using BCC field.
2738     (when (and gnus-mail-self-blind
2739                (not (mail-fetch-field "bcc")))
2740       (mail-position-on-field "Bcc")
2741       (insert (if (stringp gnus-mail-self-blind)
2742                   gnus-mail-self-blind
2743                 (user-login-name))))
2744     ;; Handle author copy using FCC field.
2745     (when gnus-author-copy
2746       (mail-position-on-field "Fcc")
2747       (insert gnus-author-copy))))
2748
2749 (defun gnus-inews-insert-gcc ()
2750   "Insert Gcc headers based on `gnus-outgoing-message-group'."
2751   (save-excursion
2752     (save-restriction
2753       (gnus-inews-narrow-to-headers)
2754       (let* ((group gnus-outgoing-message-group)
2755              (gcc (cond 
2756                    ((gnus-functionp group)
2757                     (funcall group))
2758                    ((or (stringp group) (list group))
2759                     group))))
2760         (when gcc
2761           (insert "Gcc: "
2762                   (if (stringp gcc) gcc
2763                     (mapconcat 'identity gcc " "))
2764                   "\n"))))))
2765
2766 (defun gnus-inews-insert-archive-gcc ()
2767   "Insert the Gcc to say where the article is to be archived."
2768   (let* ((var gnus-message-archive-group)
2769          result
2770          (groups
2771           (cond 
2772            ((stringp var)
2773             ;; Just a single group.
2774             (list var))
2775            ((null var)
2776             ;; We don't want this.
2777             nil)
2778            ((and (listp var) (stringp (car var)))
2779             ;; A list of groups.
2780             var)
2781            ((gnus-functionp var)
2782             ;; A function.
2783             (funcall var gnus-newsgroup-name))
2784            (t
2785             ;; An alist of regexps/functions/forms.
2786             (while (and var
2787                         (not
2788                          (setq result
2789                                (cond 
2790                                 ((stringp (caar var))
2791                                  ;; Regexp.
2792                                  (when (string-match (caar var)
2793                                                      gnus-newsgroup-name)
2794                                    (cdar var)))
2795                                 ((gnus-functionp (car var))
2796                                  ;; Function.
2797                                  (funcall (car var) gnus-newsgroup-name))
2798                                 (t
2799                                  (eval (car var)))))))
2800               (setq var (cdr var)))
2801             result))))
2802     (when groups
2803       (when (stringp groups)
2804         (setq groups (list groups)))
2805       (save-excursion
2806         (save-restriction
2807           (gnus-inews-narrow-to-headers)
2808           (goto-char (point-max))
2809           (insert "Gcc: ")
2810           (while groups
2811             (insert (gnus-group-prefixed-name 
2812                      (pop groups) gnus-message-archive-method))
2813             (insert " "))
2814           (insert "\n"))))))
2815
2816 ;;; Handling rejected (and postponed) news.
2817
2818 (defun gnus-draft-group ()
2819   "Return the name of the draft group."
2820   (gnus-group-prefixed-name 
2821    (file-name-nondirectory gnus-draft-group-directory)
2822    (list 'nndraft gnus-draft-group-directory)))
2823
2824 (defun gnus-make-draft-group ()
2825   "Make the draft group or die trying."
2826   (let* ((method `(nndraft "private" 
2827                            (nndraft-directory 
2828                             ,gnus-draft-group-directory)))
2829          (group (gnus-draft-group)))
2830     (or (gnus-gethash group gnus-newsrc-hashtb)
2831         (gnus-group-make-group (gnus-group-real-name group) method)
2832         (error "Can't create the draft group"))
2833     (gnus-check-server method)
2834     group))
2835
2836 (defun gnus-associate-buffer-with-draft (&optional generate silent)
2837   "Enter the current buffer into the draft group."
2838   (interactive)
2839   (when (gnus-request-accept-article (gnus-make-draft-group) t)
2840     (unless silent
2841       ;; This function does the proper marking of articles.
2842       (gnus-mail-send-and-exit 'dont-send))
2843     (set-buffer-modified-p nil)))
2844
2845 (defun gnus-enter-buffer-into-draft ()
2846   (when gnus-use-draft
2847     (save-excursion
2848       ;; Make sure the draft group exists.
2849       (gnus-make-draft-group)
2850       ;; Associate the buffer with the draft group.
2851       (let ((article (gnus-request-associate-buffer (gnus-draft-group))))
2852         ;; Arrange for deletion of the draft after successful sending.
2853         (make-local-variable 'gnus-message-sent-hook)
2854         (setq gnus-message-sent-hook
2855               (list
2856                `(lambda ()
2857                   (let ((gnus-verbose-backends nil))
2858                     (gnus-request-expire-articles 
2859                      (quote ,(list article))
2860                      ,(gnus-draft-group) t)))))))))
2861
2862 (defun gnus-dissociate-buffer-from-draft ()
2863   "Disable auto-saving and association to the draft group of the current buffer."
2864   (interactive)
2865   (run-hooks 'gnus-message-sent-hook)
2866   (setq buffer-file-name nil)
2867   (setq buffer-auto-save-file-name nil))
2868
2869 (defun gnus-summary-send-draft ()
2870   "Enter a mail/post buffer to edit and send the draft."
2871   (interactive)
2872   (gnus-set-global-variables)
2873   (unless (equal gnus-newsgroup-name (gnus-draft-group))
2874     (error "This function can only be used in the draft buffer"))
2875   (let (buf point)
2876     (if (not (setq buf (gnus-request-restore-buffer 
2877                         (gnus-summary-article-number) gnus-newsgroup-name)))
2878         (error "Couldn't restore the article")
2879       (setq point (point))
2880       (switch-to-buffer buf)
2881       (gnus-inews-modify-mail-mode-map)
2882       (when (eq major-mode 'news-reply-mode)
2883         (local-set-key "\C-c\C-c" 'gnus-inews-news))
2884       (gnus-enter-buffer-into-draft) 
2885       ;; Insert the separator.
2886       (goto-char (point-min))
2887       (search-forward "\n\n")
2888       (forward-char -1)
2889       (insert mail-header-separator)
2890       ;; Configure windows.
2891       (let ((gnus-draft-buffer (current-buffer)))
2892         (gnus-configure-windows 'draft)
2893         (goto-char (point))))))
2894   
2895 (defun gnus-configure-posting-styles ()
2896   "Configure posting styles according to `gnus-posting-styles'."
2897   (let ((styles gnus-posting-styles)
2898         (gnus-newsgroup-name (or gnus-newsgroup-name ""))
2899         style match variable attribute value value-value)
2900     ;; Go through all styles and look for matches.
2901     (while styles
2902       (setq style (pop styles)
2903             match (pop style))
2904       (when (cond ((stringp match)
2905                    ;; Regexp string match on the group name.
2906                    (string-match match gnus-newsgroup-name))
2907                   ((or (symbolp match)
2908                        (gnus-functionp match))
2909                    (cond ((gnus-functionp match)
2910                           ;; Function to be called.
2911                           (funcall match))
2912                          ((boundp match)
2913                           ;; Variable to be checked.
2914                           (symbol-value match))))
2915                   ((listp match)
2916                    ;; This is a form to be evaled.
2917                    (eval match)))
2918         ;; We have a match, so we set the variables.
2919         (while style
2920           (setq attribute (pop style)
2921                 value (cdr attribute))
2922           ;; We find the variable that is to be modified.
2923           (if (and (not (stringp (car attribute)))
2924                    (not (setq variable (cdr (assq (car attribute) 
2925                                                   gnus-posting-style-alist)))))
2926               (message "Couldn't find attribute %s" (car attribute))
2927             ;; We set the variable.
2928             (setq value-value
2929                   (cond ((stringp value)
2930                          value)
2931                         ((or (symbolp value)
2932                              (gnus-functionp value))
2933                          (cond ((gnus-functionp value)
2934                                 (funcall value))
2935                                ((boundp value)
2936                                 (symbol-value value))))
2937                         ((listp value)
2938                          (eval value))))
2939             (if variable
2940                 (progn
2941                   ;; This is an ordinary variable.
2942                   (make-local-variable variable)
2943                   (set variable value-value))
2944               ;; This is a header to be added to the headers when
2945               ;; posting. 
2946               (when value-value
2947                 (make-local-variable gnus-required-headers)
2948                 (make-local-variable gnus-required-mail-headers)
2949                 (push (cons (car attribute) value-value) 
2950                       gnus-required-headers)
2951                 (push (cons (car attribute) value-value) 
2952                       gnus-required-mail-headers)))))))))
2953
2954 (defun gnus-inews-set-point ()
2955   "Move point to where the user probably wants to find it."
2956   (gnus-inews-narrow-to-headers)
2957   (cond 
2958    ((re-search-forward "^[^:]+:[ \t]*$" nil t)
2959     (search-backward ":" )
2960     (widen)
2961     (forward-char 2))
2962    (t
2963     (goto-char (point-max))
2964     (widen)
2965     (forward-line 1)
2966     (unless (looking-at "$")
2967       (forward-line 2)))))
2968   
2969 ;;; Allow redefinition of functions.
2970
2971 (gnus-ems-redefine)
2972
2973 (provide 'gnus-msg)
2974
2975 ;;; gnus-msg.el ends here