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