6ec17511571b736806cc003a2e902b38f3d09c35
[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-post-prepare-function nil
37   "*Function that is run after a post buffer has been prepared.
38 It is called with the name of the newsgroup that is posted to. It
39 might be used, for instance, for inserting signatures based on the
40 newsgroup name. (In that case, `gnus-signature-file' and
41 `mail-signature' should both be set to nil).")
42
43 (defvar gnus-post-prepare-hook nil
44   "*Hook that is run after a post buffer has been prepared.
45 If you want to insert the signature, you might put
46 `gnus-inews-insert-signature' in this hook.")
47
48 (defvar gnus-use-followup-to 'use
49   "*Specifies what to do with Followup-To header.
50 If nil, ignore the header. If it is t, use its value, but ignore 
51 `poster'. If it is neither nil nor t, which is the default, always use
52 the value.") 
53
54 (defvar gnus-followup-to-function nil
55   "*A variable that contains a function that returns a followup address.
56 The function will be called in the buffer of the article that is being
57 followed up. The buffer will be narrowed to the headers of the
58 article. To pick header headers, one might use `mail-fetch-field'.  The
59 function will be called with the name of the current newsgroup as the
60 argument.
61
62 Here's an example `gnus-followup-to-function':
63
64 (setq gnus-followup-to-function
65       (lambda (group)
66         (cond ((string= group \"mail.list\")
67                (or (mail-fetch-field \"sender\") 
68                    (mail-fetch-field \"from\")))
69               (t
70                (or (mail-fetch-field \"reply-to\") 
71                    (mail-fetch-field \"from\"))))))")
72
73 (defvar gnus-reply-to-function nil
74   "*A variable that contains a function that returns a reply address.
75 See the `gnus-followup-to-function' variable for an explanation of how
76 this variable is used.
77
78 This function should return a string that will be used to fill in the
79 header.  This function may also return a list.  In that case, every
80 list element should be a cons where the first car should be a string
81 with the header name, and the cdr should be a string with the header
82 value.")
83
84 (defvar gnus-author-copy (getenv "AUTHORCOPY")
85   "*Save outgoing articles in this file.
86 Initialized from the AUTHORCOPY environment variable.
87
88 If this variable begins with the character \"|\", outgoing articles
89 will be piped to the named program. It is possible to save an article
90 in an MH folder as follows:
91
92 \(setq gnus-author-copy \"|/usr/local/lib/mh/rcvstore +Article\")
93
94 If the first character is not a pipe, articles are saved using the
95 function specified by the `gnus-author-copy-saver' variable.")
96
97 (defvar gnus-mail-self-blind nil
98   "*Non-nil means insert a BCC header in all outgoing articles.
99 This will result in having a copy of the article mailed to yourself.
100 The BCC header is inserted when the post buffer is initialized, so you
101 can remove or alter the BCC header to override the default.")
102
103 (defvar gnus-author-copy-saver (function rmail-output)
104   "*A function called to save outgoing articles.
105 This function will be called with the same of the file to store the
106 article in. The default function is `rmail-output' which saves in Unix
107 mailbox format.")
108
109 (defvar gnus-user-login-name nil
110   "*The login name of the user.
111 Got from the function `user-login-name' if undefined.")
112
113 (defvar gnus-user-full-name nil
114   "*The full name of the user.
115 Got from the NAME environment variable if undefined.")
116
117 (defvar gnus-user-from-line nil
118   "*Your full, complete e-mail address.  
119 Overrides the other Gnus variables if it is non-nil.
120
121 Here are two example values of this variable:
122
123  \"Lars Magne Ingebrigtsen <larsi@ifi.uio.no>\"
124
125 and
126
127  \"larsi@ifi.uio.no (Lars Magne Ingebrigtsen)\"
128
129 The first version is recommended, but the name has to be quoted if it
130 contains non-alphanumerical characters.")
131
132 (defvar gnus-signature-file "~/.signature"
133   "*Your signature file.
134 If the variable is a string that doesn't correspond to a file, the
135 string itself is inserted.")
136
137 (defvar gnus-signature-function nil
138   "*A function that should return a signature file name.
139 The function will be called with the name of the newsgroup being
140 posted to.
141 If the function returns a string that doesn't correspond to a file, the
142 string itself is inserted.
143 If the function returns nil, the `gnus-signature-file' variable will
144 be used instead.")
145
146 (defvar gnus-required-headers
147   '(From Date Newsgroups Subject Message-ID Organization Lines X-Newsreader)
148   "*Headers to be generated or prompted for when posting an article.
149 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
150 Message-ID.  Organization, Lines and X-Newsreader are optional.  If
151 you want Gnus not to insert some header, remove it from this list.")
152
153 (defvar gnus-deletable-headers '(Message-ID)
154   "*Headers to be deleted if they already exists.")
155
156 (defvar gnus-check-before-posting 
157   '(subject-cmsg multiple-headers sendsys message-id from
158                  long-lines control-chars size new-text
159                  signature)
160   "In non-nil, Gnus will attempt to run some checks on outgoing posts.
161 If this variable is t, Gnus will check everything it can.  If it is a
162 list, then those elements in that list will be checked.")
163
164 (defvar gnus-delete-supersedes-headers "^Path:\\|^Date\\|^NNTP-Posting-Host:"
165   "*Header lines matching this regexp will be deleted before posting.
166 It's best to delete old Path and Date headers before psoting to avoid
167 any confusion.")
168
169 (defvar gnus-auto-mail-to-author nil
170   "*If non-nil, mail the authors of articles a copy of your follow-ups.
171 If this variable is `ask', the user will be prompted for whether to
172 mail a copy.  The string given by `gnus-mail-courtesy-message' will be
173 inserted at the beginning of the mail copy.
174
175 Mail is sent using the function specified by the
176 `gnus-mail-send-method' variable.")
177
178 ;; Added by Ethan Bradford <ethanb@ptolemy.astro.washington.edu>.
179 (defvar gnus-mail-courtesy-message
180   "The following message is a courtesy copy of an article\nthat has been posted as well.\n\n"
181   "*This is inserted at the start of a mailed copy of a posted message.
182 If this variable is nil, no such courtesy message will be added.")
183
184 (defvar gnus-mail-reply-method (function gnus-mail-reply-using-mail)
185   "*Function to compose a reply.
186 Three pre-made functions are `gnus-mail-reply-using-mail' (sendmail);
187 `gnus-mail-reply-using-mhe' (MH-E); and `gnus-mail-reply-using-vm'.")
188
189 (defvar gnus-mail-forward-method (function gnus-mail-forward-using-mail)
190   "*Function to forward the current message to another user.
191 Three pre-made functions are `gnus-mail-forward-using-mail' (sendmail);
192 `gnus-mail-forward-using-mhe' (MH-E); and `gnus-mail-forward-using-vm'.") 
193
194 (defvar gnus-mail-other-window-method 'gnus-mail-other-window-using-mail
195   "*Function to compose mail in the other window.
196 Three pre-made functions are `gnus-mail-other-window-using-mail'
197 (sendmail); `gnus-mail-other-window-using-mhe' (MH-E); and
198 `gnus-mail-other-window-using-vm'.")
199
200 (defvar gnus-mail-send-method send-mail-function
201   "*Function to mail a message which is also being posted as an article.
202 The message must have To or Cc header.  The default is copied from
203 the variable `send-mail-function'.")
204
205 (defvar gnus-inews-article-function 'gnus-inews-article
206   "*Function to post an article.")
207
208 (defvar gnus-inews-article-hook (list 'gnus-inews-do-fcc)
209   "*A hook called before finally posting an article.
210 The default hook (`gnus-inews-do-fcc') does FCC processing (ie. saves
211 the article to a file).")
212
213 (defvar gnus-inews-article-header-hook nil
214   "*A hook called after inserting the headers in an article to be posted.
215 The hook is called from the *post-news* buffer, narrowed to the
216 headers.")
217
218 (defvar gnus-mail-hook nil
219   "*A hook called as the last thing after setting up a mail buffer.")
220
221 ;;; Internal variables.
222
223 (defvar gnus-post-news-buffer "*post-news*")
224 (defvar gnus-mail-buffer "*mail*")
225 (defvar gnus-summary-send-map nil)
226 (defvar gnus-article-copy nil)
227 (defvar gnus-reply-subject nil)
228
229 \f
230 ;;;
231 ;;; Gnus Posting Functions
232 ;;;
233
234 (define-prefix-command 'gnus-summary-send-map)
235 (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
236 (define-key gnus-summary-send-map "p" 'gnus-summary-post-news)
237 (define-key gnus-summary-send-map "f" 'gnus-summary-followup)
238 (define-key gnus-summary-send-map "F" 'gnus-summary-followup-with-original)
239 (define-key gnus-summary-send-map "b" 'gnus-summary-followup-and-reply)
240 (define-key gnus-summary-send-map "B" 'gnus-summary-followup-and-reply-with-original)
241 (define-key gnus-summary-send-map "c" 'gnus-summary-cancel-article)
242 (define-key gnus-summary-send-map "s" 'gnus-summary-supersede-article)
243 (define-key gnus-summary-send-map "r" 'gnus-summary-reply)
244 (define-key gnus-summary-send-map "R" 'gnus-summary-reply-with-original)
245 (define-key gnus-summary-send-map "m" 'gnus-summary-mail-other-window)
246 (define-key gnus-summary-send-map "u" 'gnus-uu-post-news)
247 (define-key gnus-summary-send-map "om" 'gnus-summary-mail-forward)
248 (define-key gnus-summary-send-map "op" 'gnus-summary-post-forward)
249 (define-key gnus-summary-send-map "Om" 'gnus-uu-digest-mail-forward)
250 (define-key gnus-summary-send-map "Op" 'gnus-uu-digest-post-forward)
251
252 ;;; Internal functions.
253
254 (defun gnus-number-base36 (num len)
255   (if (if (< len 0) (<= num 0) (= len 0))
256       ""
257     (concat (gnus-number-base36 (/ num 36) (1- len))
258             (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
259                                   (% num 36))))))
260
261 ;;; Post news commands of Gnus group mode and summary mode
262
263 (defun gnus-group-mail ()
264   "Start composing a mail."
265   (interactive)
266   (funcall gnus-mail-other-window-method)
267   (gnus-configure-windows 'group-mail)
268   (run-hooks 'gnus-mail-hook))
269
270 (defun gnus-group-post-news ()
271   "Post an article."
272   (interactive)
273   (let ((gnus-newsgroup-name nil))
274     (gnus-post-news 'post nil nil gnus-article-buffer)))
275
276 (defun gnus-summary-post-news ()
277   "Post an article."
278   (interactive)
279   (gnus-set-global-variables)
280   (gnus-post-news 'post gnus-newsgroup-name))
281
282 (defun gnus-summary-followup (yank &optional yank-articles)
283   "Compose a followup to an article.
284 If prefix argument YANK is non-nil, original article is yanked automatically."
285   (interactive "P")
286   (gnus-set-global-variables)
287   (if yank-articles (gnus-summary-goto-subject (car yank-articles)))
288   (save-window-excursion
289     (gnus-summary-select-article))
290   (let ((headers (gnus-get-header-by-number (gnus-summary-article-number)))
291         (gnus-newsgroup-name gnus-newsgroup-name))
292     ;; Check Followup-To: poster.
293     (set-buffer gnus-article-buffer)
294     (if (and gnus-use-followup-to
295              (string-equal "poster" (gnus-fetch-field "followup-to"))
296              (or (not (eq gnus-use-followup-to t))
297                  (not (gnus-y-or-n-p 
298                        "Do you want to ignore `Followup-To: poster'? "))))
299         ;; Mail to the poster. 
300         (gnus-summary-reply yank)
301       (gnus-post-news nil gnus-newsgroup-name
302                       headers gnus-article-buffer 
303                       (or yank-articles (not (not yank)))))))
304
305 (defun gnus-summary-followup-with-original (n)
306   "Compose a followup to an article and include the original article."
307   (interactive "P")
308   (gnus-summary-followup t (gnus-summary-work-articles n)))
309
310 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
311 (defun gnus-summary-followup-and-reply (yank &optional yank-articles)
312   "Compose a followup and do an auto mail to author."
313   (interactive "P")
314   (gnus-set-global-variables)
315   (let ((gnus-auto-mail-to-author t))
316     (gnus-summary-followup yank yank-articles)))
317
318 (defun gnus-summary-followup-and-reply-with-original (n)
319   "Compose a followup, include the original, and do an auto mail to author."
320   (interactive "P")
321   (gnus-summary-followup-and-reply t (gnus-summary-work-articles n)))
322
323 (defun gnus-summary-cancel-article (n)
324   "Cancel an article you posted."
325   (interactive "P")
326   (gnus-set-global-variables)
327   (let ((articles (gnus-summary-work-articles n)))
328     (while articles
329       (gnus-summary-select-article t nil nil (car articles))
330       (and (gnus-eval-in-buffer-window gnus-article-buffer (gnus-cancel-news))
331            (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
332       (gnus-summary-remove-process-mark (car articles))
333       (gnus-article-hide-headers-if-wanted)
334       (setq articles (cdr articles)))))
335
336 (defun gnus-summary-supersede-article ()
337   "Compose an article that will supersede a previous article.
338 This is done simply by taking the old article and adding a Supersedes
339 header line with the old Message-ID."
340   (interactive)
341   (gnus-set-global-variables)
342   (gnus-summary-select-article t)
343   (if (not
344        (string-equal
345         (downcase (mail-strip-quoted-names 
346                    (header-from gnus-current-headers)))
347         (downcase (mail-strip-quoted-names (gnus-inews-user-name)))))
348       (error "This article is not yours."))
349   (save-excursion
350     (set-buffer gnus-article-buffer)
351     (let ((buffer-read-only nil))
352       (goto-char (point-min))
353       (search-forward "\n\n" nil t)
354       (if (not (re-search-backward "^Message-ID: " nil t))
355           (error "No Message-ID in this article"))))
356   (if (gnus-post-news 'post gnus-newsgroup-name)
357       (progn
358         (erase-buffer)
359         (insert-buffer gnus-article-buffer)
360         (goto-char (point-min))
361         (search-forward "\n\n" nil t)
362         (if (not (re-search-backward "^Message-ID: " nil t))
363             (error "No Message-ID in this article")
364           (replace-match "Supersedes: " t t))
365         (search-forward "\n\n")
366         (forward-line -1)
367         (insert mail-header-separator)
368
369         (forward-line -1)
370         (narrow-to-region (point-min) (point))
371         (goto-char (point-min))
372         (and gnus-delete-supersedes-headers
373              (delete-matching-lines gnus-delete-supersedes-headers))
374         (widen))))
375
376 \f
377 ;;;###autoload
378 (defalias 'sendnews 'gnus-post-news)
379
380 ;;;###autoload
381 (defalias 'postnews 'gnus-post-news)
382
383 (defun gnus-copy-article-buffer (&optional article-buffer)
384   ;; make a copy of the article buffer with all text properties removed
385   ;; this copy is in the buffer gnus-article-copy.
386   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
387   ;; this buffer should be passed to all mail/news reply/post routines.
388   (setq gnus-article-copy (get-buffer-create " *gnus article copy*"))
389   (buffer-disable-undo gnus-article-copy)
390   (or (memq gnus-article-copy gnus-buffer-list)
391       (setq gnus-buffer-list (cons gnus-article-copy gnus-buffer-list)))
392   (let ((article-buffer (or article-buffer gnus-article-buffer)))
393     (if (and (get-buffer article-buffer)
394              (buffer-name (get-buffer article-buffer)))
395         (save-excursion
396           (set-buffer article-buffer)
397           (widen)
398           (copy-to-buffer gnus-article-copy (point-min) (point-max))
399           (set-text-properties (point-min) (point-max) 
400                                nil gnus-article-copy)))))
401
402 (defun gnus-post-news (post &optional group header article-buffer yank subject)
403   "Begin editing a new USENET news article to be posted.
404 Type \\[describe-mode] in the buffer to get a list of commands."
405   (interactive (list t))
406   (gnus-copy-article-buffer article-buffer)
407   (if (or (not gnus-novice-user)
408           gnus-expert-user
409           (not (eq 'post 
410                    (nth 1 (assoc 
411                            (format "%s" (car (gnus-find-method-for-group 
412                                               gnus-newsgroup-name)))
413                            gnus-valid-select-methods))))
414           (and group
415                (assq 'to-address 
416                      (nth 5 (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))))
417           (gnus-y-or-n-p "Are you sure you want to post to all of USENET? "))
418       (let ((sumart (if (not post)
419                         (save-excursion
420                           (set-buffer gnus-summary-buffer)
421                           (cons (current-buffer) gnus-current-article))))
422             (from (and header (header-from header)))
423             (winconf (current-window-configuration))
424             real-group)
425         (and gnus-interactive-post
426              (not gnus-expert-user)
427              post (not group)
428              (progn
429                (setq group 
430                      (completing-read "Group: " gnus-active-hashtb))
431                (or subject
432                    (setq subject (read-string "Subject: ")))))
433         (setq mail-reply-buffer gnus-article-copy)
434
435         (let ((newsgroup-name (or group gnus-newsgroup-name "")))
436           (setq real-group (and group (gnus-group-real-name group)))
437           (setq gnus-post-news-buffer 
438                 (gnus-request-post-buffer 
439                  post real-group subject header gnus-article-copy
440                  (nth 2 (and group (gnus-gethash group gnus-newsrc-hashtb)))
441                  (or (cdr (assq 'to-group
442                                 (nth 5 (nth 2 (gnus-gethash 
443                                                newsgroup-name
444                                                gnus-newsrc-hashtb)))))
445                      (if (and (boundp 'gnus-followup-to-function)
446                               gnus-followup-to-function
447                               gnus-article-copy)
448                          (save-excursion
449                            (set-buffer gnus-article-copy)
450                            (funcall gnus-followup-to-function group))))
451                  gnus-use-followup-to))
452           (if post
453               (gnus-configure-windows 'post)
454             (if yank
455                 (gnus-configure-windows 'followup-yank)
456               (gnus-configure-windows 'followup)))
457           (gnus-overload-functions)
458           (make-local-variable 'gnus-article-reply)
459           (make-local-variable 'gnus-article-check-size)
460           (make-local-variable 'gnus-reply-subject)
461           (setq gnus-reply-subject (and header (header-subject header)))
462           (setq gnus-article-reply sumart)
463           ;; Handle `gnus-auto-mail-to-author'.
464           ;; Suggested by Daniel Quinlan <quinlan@best.com>.
465           (let ((to (and (not post)
466                          (if (eq gnus-auto-mail-to-author 'ask)
467                              (and (y-or-n-p "Also send mail to author? ") from)
468                            (and gnus-auto-mail-to-author from)))))
469             (if to
470                 (progn
471                   (if (mail-fetch-field "To")
472                       (progn
473                         (beginning-of-line)
474                         (insert "Cc: " to "\n"))
475                     (mail-position-on-field "To")
476                     (insert to)))))
477           ;; Handle author copy using BCC field.
478           (if (and gnus-mail-self-blind
479                    (not (mail-fetch-field "bcc")))
480               (progn
481                 (mail-position-on-field "Bcc")
482                 (insert (if (stringp gnus-mail-self-blind)
483                             gnus-mail-self-blind
484                           (user-login-name)))))
485           ;; Handle author copy using FCC field.
486           (if gnus-author-copy
487               (progn
488                 (mail-position-on-field "Fcc")
489                 (insert gnus-author-copy)))
490           (goto-char (point-min))
491           (if post 
492               (cond ((not group)
493                      (re-search-forward "^Newsgroup:" nil t)
494                      (end-of-line))
495                     ((not subject)
496                      (re-search-forward "^Subject:" nil t)
497                      (end-of-line))
498                     (t
499                      (re-search-forward 
500                       (concat "^" (regexp-quote mail-header-separator) "$"))
501                      (forward-line 1)))
502             (re-search-forward 
503              (concat "^" (regexp-quote mail-header-separator) "$"))
504             (forward-line 1)
505             (if (not yank)
506                 ()
507               (save-excursion 
508                 (if (not (listp yank))
509                     (news-reply-yank-original nil)
510                   (while yank
511                     (save-window-excursion
512                       (set-buffer gnus-summary-buffer)
513                       (gnus-summary-select-article nil nil nil (car yank))
514                       (gnus-summary-remove-process-mark (car yank)))
515                     (let ((mail-reply-buffer gnus-article-copy))
516                       (news-reply-yank-original nil))
517                     (setq yank (cdr yank)))))))
518           (if gnus-post-prepare-function
519               (funcall gnus-post-prepare-function group))
520           (run-hooks 'gnus-post-prepare-hook)
521           (make-local-variable 'gnus-prev-winconf)
522           (setq gnus-prev-winconf winconf))))
523   (setq gnus-article-check-size (cons (buffer-size) (gnus-article-checksum)))
524   (message "")
525   t)
526
527 (defun gnus-inews-news (&optional use-group-method)
528   "Send a news message.
529 If given a prefix, and the group is a foreign group, this function
530 will attempt to use the foreign server to post the article."
531   (interactive "P")
532   (let* ((case-fold-search nil)
533          (server-running (gnus-server-opened gnus-select-method))
534          (reply gnus-article-reply)
535          error)
536     (save-excursion
537       ;; Connect to default NNTP server if necessary.
538       ;; Suggested by yuki@flab.fujitsu.junet.
539       (gnus-start-news-server)          ;Use default server.
540       ;; NNTP server must be opened before current buffer is modified.
541       (widen)
542       (goto-char (point-min))
543       (run-hooks 'news-inews-hook)
544       (save-restriction
545         (narrow-to-region
546          (point-min)
547          (progn
548            (goto-char (point-min))
549            (re-search-forward 
550             (concat "^" (regexp-quote mail-header-separator) "$"))
551            (match-beginning 0)))
552
553         ;; Correct newsgroups field: change sequence of spaces to comma and 
554         ;; eliminate spaces around commas.  Eliminate imbedded line breaks.
555         (goto-char (point-min))
556         (if (search-forward-regexp "^Newsgroups: +" nil t)
557             (save-restriction
558               (narrow-to-region
559                (point)
560                (if (re-search-forward "^[^ \t]" nil 'end)
561                    (match-beginning 0)
562                  (point-max)))
563               (goto-char (point-min))
564               (replace-regexp "\n[ \t]+" " ") ;No line breaks (too confusing)
565               (goto-char (point-min))
566               (replace-regexp "[ \t\n]*,[ \t\n]*\\|[ \t]+" ",")))
567
568         ;; Added by Per Abrahamsen <abraham@iesd.auc.dk>.
569         ;; Help save the the world!
570         (or 
571          gnus-expert-user
572          (let ((newsgroups (mail-fetch-field "newsgroups"))
573                (followup-to (mail-fetch-field "followup-to"))
574                groups to)
575            (if (and (string-match "," newsgroups) (not followup-to))
576                (progn
577                  (while (string-match "," newsgroups)
578                    (setq groups
579                          (cons (list (substring newsgroups
580                                                 0 (match-beginning 0)))
581                                groups))
582                    (setq newsgroups (substring newsgroups (match-end 0))))
583                  (setq groups (nreverse (cons (list newsgroups) groups)))
584
585                  (setq to
586                        (completing-read "Followups to: (default all groups) "
587                                         groups))
588                  (if (> (length to) 0)
589                      (progn
590                        (goto-char (point-min))
591                        (insert "Followup-To: " to "\n")))))))
592
593         ;; Cleanup Followup-To.
594         (goto-char (point-min))
595         (if (search-forward-regexp "^Followup-To: +" nil t)
596             (save-restriction
597               (narrow-to-region
598                (point)
599                (if (re-search-forward "^[^ \t]" nil 'end)
600                    (match-beginning 0)
601                  (point-max)))
602               (goto-char (point-min))
603               (replace-regexp "\n[ \t]+" " ") ;No line breaks (too confusing)
604               (goto-char (point-min))
605               (replace-regexp "[ \t\n]*,[ \t\n]*\\|[ \t]+" ",")))
606
607         ;; Mail the message too if To:, Bcc:. or Cc: exists.
608         (let* ((types '("to" "bcc" "cc"))
609                (ty types)
610                fcc-line)
611           (while ty
612             (or (mail-fetch-field (car ty) nil t)
613                 (setq types (delete (car ty) types)))
614             (setq ty (cdr ty)))
615
616           (if (not types)
617               ;; We do not want to send mail.
618               ()
619             (if (not gnus-mail-send-method)
620                 (progn
621                   (ding)
622                   (gnus-message 
623                    1 "No mailer defined.  To: and/or Cc: fields ignored.")
624                   (sit-for 1))
625               (save-excursion
626                 ;; We want to remove Fcc, because we want to handle
627                 ;; that one ourselves...  
628                   
629                 (goto-char (point-min))
630                 (if (re-search-forward "^Fcc: " nil t)
631                     (progn
632                       (setq fcc-line
633                             (buffer-substring
634                              (progn (beginning-of-line) (point))
635                              (progn (forward-line 1) (point))))
636                       (forward-line -1)
637                       (gnus-delete-line)))
638
639                 ;; We generate a Message-ID so that the mail and the
640                 ;; news copy of the message both get the same ID.
641                 (or (mail-fetch-field "message-id")
642                     (progn
643                       (goto-char (point-max))
644                       (insert "Message-ID: " (gnus-inews-message-id) "\n")))
645
646                 (save-restriction
647                   (widen)
648                   (gnus-message 5 "Sending via mail...")
649
650                   (if (and gnus-mail-courtesy-message
651                            (or (member "to" types)
652                                (member "cc" types)))
653                       ;; We only want to insert the courtesy mail
654                       ;; message if we use to or cc; bcc should not
655                       ;; have one. Well, if both bcc and to are
656                       ;; present, it will get one anyway.
657                       (progn
658                         ;; Insert "courtesy" mail message.
659                         (goto-char (point-min))
660                         (re-search-forward
661                          (concat "^" (regexp-quote
662                                       mail-header-separator) "$"))
663                         (forward-line 1)
664                         (insert gnus-mail-courtesy-message)
665                         (funcall gnus-mail-send-method)
666                         (goto-char (point-min))
667                         (search-forward gnus-mail-courtesy-message)
668                         (replace-match "" t t))
669                     (funcall gnus-mail-send-method))
670
671                   (gnus-message 5 "Sending via mail...done")
672                       
673                   (goto-char (point-min))
674                   (narrow-to-region
675                    (point) 
676                    (re-search-forward 
677                     (concat "^" (regexp-quote mail-header-separator) "$")))
678                   (goto-char (point-min))
679                   (delete-matching-lines "^BCC:"))
680                 (if fcc-line
681                     (progn
682                       (goto-char (point-max))
683                       (insert fcc-line))))))))
684
685       ;; Send to server. 
686       (gnus-message 5 "Posting to USENET...")
687       (if (funcall gnus-inews-article-function use-group-method)
688           (progn
689             (gnus-message 5 "Posting to USENET...done")
690             (if (gnus-buffer-exists-p (car-safe reply))
691                 (progn
692                   (save-excursion
693                     (set-buffer gnus-summary-buffer)
694                     (gnus-summary-mark-article-as-replied 
695                      (cdr reply))))))
696         ;; We cannot signal an error.
697         (setq error t)
698         (ding) (gnus-message 1 "Article rejected: %s" 
699                              (gnus-status-message gnus-select-method)))
700       (set-buffer-modified-p nil))
701     ;; If NNTP server is opened by gnus-inews-news, close it by myself.
702     (or server-running
703         (gnus-close-server (gnus-find-method-for-group gnus-newsgroup-name)))
704     (let ((conf gnus-prev-winconf))
705       (if (not error)
706           (progn
707             (bury-buffer)
708             ;; Restore last window configuration.
709             (and conf (set-window-configuration conf)))))))
710
711 (defun gnus-inews-check-post ()
712   "Check whether the post looks ok."
713   (or
714    (not gnus-check-before-posting)
715    (and 
716     ;; We narrow to the headers and check them first.
717     (save-excursion
718       (save-restriction
719         (goto-char (point-min))
720         (narrow-to-region 
721          (point) 
722          (re-search-forward 
723           (concat "^" (regexp-quote mail-header-separator) "$")))
724         (goto-char (point-min))
725         (and 
726          ;; Check for commands in Subject.
727          (or (gnus-check-before-posting 'subject-cmsg)
728              (save-excursion
729                (if (string-match "^cmsg " (mail-fetch-field "subject"))
730                    (gnus-y-or-n-p
731                     "The control code \"cmsg \" is in the subject. Really post? ")
732                  t)))
733          ;; Check for multiple identical headers.
734          (or (gnus-check-before-posting 'multiple-headers)
735              (save-excursion
736                (let (found)
737                  (while (and (not found) (re-search-forward "^[^ \t:]+: " nil t))
738                    (save-excursion
739                      (or (re-search-forward 
740                           (concat "^" (setq found
741                                             (buffer-substring 
742                                              (match-beginning 0) 
743                                              (- (match-end 0) 2))))
744                           nil t)
745                          (setq found nil))))
746                  (if found
747                      (gnus-y-or-n-p 
748                       (format "Multiple %s headers. Really post? " found))
749                    t))))
750          ;; Check for version and sendsys.
751          (or (gnus-check-before-posting 'sendsys)
752              (save-excursion
753                (if (re-search-forward "^Sendsys:\\|^Version:" nil t)
754                    (gnus-yes-or-no-p
755                     (format "The article contains a %s command. Really post? "
756                             (buffer-substring (match-beginning 0) 
757                                               (1- (match-end 0)))))
758                  t)))
759          ;; Check the Message-Id header.
760          (or (gnus-check-before-posting 'message-id)
761              (save-excursion
762                (let* ((case-fold-search t)
763                       (message-id (mail-fetch-field "message-id")))
764                  (or (not message-id)
765                      (and (string-match "@" message-id)
766                           (string-match "@[^\\.]*\\." message-id))
767                      (gnus-yes-or-no-p
768                       (format 
769                        "The Message-ID looks strange: \"%s\". Really post? "
770                        message-id))))))
771          ;; Check the From header.
772          (or (gnus-check-before-posting 'from)
773              (save-excursion
774                (let* ((case-fold-search t)
775                       (from (mail-fetch-field "from")))
776                  (cond
777                   ((not from)
778                    (gnus-yes-or-no-p "There is no From line. Really post? "))
779                   ((not (string-match "@[^\\.]*\\." from))
780                    (gnus-yes-or-no-p
781                     (format 
782                      "The address looks strange: \"%s\". Really post? " from)))
783                   ((string-match "(.*).*(.*)")
784                    (gnus-yes-or-no-p
785                     (format
786                      "The From header looks strange: \"%s\". Really post? " 
787                      from)))))))
788          )))
789     ;; Check for long lines.
790     (or (gnus-check-before-posting 'long-lines)
791         (save-excursion
792           (goto-char (point-min))
793           (re-search-forward
794            (concat "^" (regexp-quote mail-header-separator) "$"))
795           (while (and
796                   (progn
797                     (end-of-line)
798                     (< (current-column) 80))
799                   (zerop (forward-line 1))))
800           (or (bolp)
801               (eobp)
802               (gnus-yes-or-no-p
803                (format
804                 "You have lines longer than 79 characters.  Really post? ")))))
805     ;; Check for control characters.
806     (or (gnus-check-before-posting 'control-chars)
807         (save-excursion
808           (if (re-search-forward "[\000-\007\013\015-\037\200-\237]" nil t)
809               (gnus-y-or-n-p 
810                "The article contains control characters. Really post? ")
811             t)))
812     ;; Check excessive size.
813     (or (gnus-check-before-posting 'size)
814         (if (> (buffer-size) 60000)
815             (gnus-y-or-n-p
816              (format "The article is %d octets long. Really post? "
817                      (buffer-size)))
818           t))
819     ;; Use the (size . checksum) variable to see whether the
820     ;; article is empty or has only quoted text.
821     (or (gnus-check-before-posting 'new-text)
822         (if (and (= (buffer-size) (car gnus-article-check-size))
823                  (= (gnus-article-checksum) (cdr gnus-article-check-size)))
824             (gnus-yes-or-no-p
825              "It looks like there's no new text in your article. Really post? ")
826           t))
827     ;; Check the length of the signature.
828     (or (gnus-check-before-posting 'signature)
829         (progn
830           (goto-char (point-max))
831           (if (not (re-search-backward gnus-signature-separator nil t))
832               t
833             (if (> (count-lines (point) (point-max)) 4)
834                 (gnus-y-or-n-p
835                  (format
836                   "Your .sig is %d lines; it should be max 4.  Really post? "))
837               t)))))))
838
839 (defun gnus-article-checksum ()
840   (let ((sum 0))
841     (save-excursion
842       (while (not (eobp))
843         (setq sum (logxor sum (following-char)))
844         (forward-char 1)))
845     sum))
846
847 ;; Returns non-nil if this type is not to be checked.
848 (defun gnus-check-before-posting (type)
849   (or (not gnus-check-before-posting)
850       (if (listp gnus-check-before-posting)
851           (memq type gnus-check-before-posting)
852         t)))
853
854 (defun gnus-cancel-news ()
855   "Cancel an article you posted."
856   (interactive)
857   (if (or gnus-expert-user
858           (gnus-yes-or-no-p "Do you really want to cancel this article? "))
859       (let ((from nil)
860             (newsgroups nil)
861             (message-id nil)
862             (distribution nil))
863         (or (gnus-member-of-valid 'post gnus-newsgroup-name)
864             (error "This backend does not support canceling"))
865         (save-excursion
866           ;; Get header info. from original article.
867           (save-restriction
868             (gnus-article-show-all-headers)
869             (goto-char (point-min))
870             (search-forward "\n\n" nil 'move)
871             (narrow-to-region (point-min) (point))
872             (setq from (mail-fetch-field "from"))
873             (setq newsgroups (mail-fetch-field "newsgroups"))
874             (setq message-id (mail-fetch-field "message-id"))
875             (setq distribution (mail-fetch-field "distribution")))
876           ;; Verify if the article is absolutely user's by comparing
877           ;; user id with value of its From: field.
878           (if (not
879                (string-equal
880                 (downcase (mail-strip-quoted-names from))
881                 (downcase (mail-strip-quoted-names (gnus-inews-user-name)))))
882               (progn
883                 (ding) (gnus-message 3 "This article is not yours.")
884                 nil)
885             ;; Make control article.
886             (set-buffer (get-buffer-create " *Gnus-canceling*"))
887             (buffer-disable-undo (current-buffer))
888             (erase-buffer)
889             (insert "Newsgroups: " newsgroups "\n"
890                     "Subject: cancel " message-id "\n"
891                     "Control: cancel " message-id "\n"
892                     (if distribution
893                         (concat "Distribution: " distribution "\n")
894                       "")
895                     mail-header-separator "\n"
896                     "This is a cancel message from " from ".\n")
897             ;; Send the control article to NNTP server.
898             (gnus-message 5 "Canceling your article...")
899             (prog1
900                 (if (funcall gnus-inews-article-function)
901                     (gnus-message 5 "Canceling your article...done")
902                   (progn
903                     (ding) 
904                     (gnus-message 1 "Cancel failed; %s" 
905                                   (gnus-status-message gnus-newsgroup-name))
906                     nil)
907                   t)
908               ;; Kill the article buffer.
909               (kill-buffer (current-buffer))))))))
910
911 \f
912 ;;; Lowlevel inews interface
913
914 (defun gnus-inews-article (&optional use-group-method)
915   "Post an article in current buffer using NNTP protocol."
916   (let ((artbuf (current-buffer))
917         (tmpbuf (get-buffer-create " *Gnus-posting*")))
918     (widen)
919     (goto-char (point-max))
920     ;; require a newline at the end for inews to append .signature to
921     (or (= (preceding-char) ?\n)
922         (insert ?\n))
923     ;; Prepare article headers.  All message body such as signature
924     ;; must be inserted before Lines: field is prepared.
925     (save-restriction
926       (goto-char (point-min))
927       (narrow-to-region 
928        (point-min) 
929        (save-excursion
930          (re-search-forward 
931           (concat "^" (regexp-quote mail-header-separator) "$"))
932          (match-beginning 0)))
933       (gnus-inews-remove-headers)
934       (gnus-inews-insert-headers)
935       (run-hooks gnus-inews-article-header-hook)
936       (widen))
937     ;; Check whether the article is a good Net Citizen.
938     (if (and gnus-article-check-size
939              (not (gnus-inews-check-post)))
940         ;; Aber nein!
941         ()
942       ;; Looks ok, so we do the nasty.
943       (save-excursion
944         (set-buffer tmpbuf)
945         (buffer-disable-undo (current-buffer))
946         (erase-buffer)
947         (insert-buffer-substring artbuf)
948         ;; Remove the header separator.
949         (goto-char (point-min))
950         (re-search-forward
951          (concat "^" (regexp-quote mail-header-separator) "$"))
952         (replace-match "" t t)
953         ;; This hook may insert a signature.
954         (run-hooks 'gnus-prepare-article-hook)
955         ;; Run final inews hooks.  This hook may do FCC.
956         ;; The article must be saved before being posted because
957         ;; `gnus-request-post' modifies the buffer.
958         (run-hooks 'gnus-inews-article-hook)
959         ;; Post an article to NNTP server.
960         ;; Return NIL if post failed.
961         (prog1
962             (gnus-request-post 
963              (if use-group-method
964                  (gnus-find-method-for-group gnus-newsgroup-name)
965                gnus-select-method) use-group-method)
966           (kill-buffer (current-buffer)))))))
967
968 (defun gnus-inews-remove-headers ()
969   (let ((case-fold-search t))
970     ;; Remove NNTP-posting-host.
971     (goto-char (point-min))
972     (and (re-search-forward "^nntp-posting-host:" nil t)
973          (delete-region (progn (beginning-of-line) (point))
974                         (progn (forward-line 1) (point))))
975     ;; Remove Bcc.
976     (goto-char (point-min))
977     (and (re-search-forward "^bcc:" nil t)
978          (delete-region (progn (beginning-of-line) (point))
979                         (progn (forward-line 1) (point))))))
980   
981 (defun gnus-inews-insert-headers ()
982   "Prepare article headers.
983 Headers already prepared in the buffer are not modified.
984 Headers in `gnus-required-headers' will be generated."
985   (let ((Date (gnus-inews-date))
986         (Message-ID (gnus-inews-message-id))
987         (Organization (gnus-inews-organization))
988         (From (gnus-inews-user-name))
989         (Path (gnus-inews-path))
990         (Subject nil)
991         (Newsgroups nil)
992         (Distribution nil)
993         (Lines (gnus-inews-lines))
994         (X-Newsreader gnus-version)
995         (headers gnus-required-headers)
996         (case-fold-search t)
997         header value elem)
998     ;; First we remove any old generated headers.
999     (let ((headers gnus-deletable-headers))
1000       (while headers
1001         (goto-char (point-min))
1002         (and (re-search-forward 
1003               (concat "^" (symbol-name (car headers)) ": *") nil t)
1004              (get-text-property (1+ (match-beginning 0)) 'gnus-deletable)
1005              (gnus-delete-line))
1006         (setq headers (cdr headers))))
1007     ;; Insert new Sender if the From is strange. 
1008     (let ((from (mail-fetch-field "from")))
1009       (if (and from (not (string= (downcase from) (downcase From))))
1010           (progn
1011             (goto-char (point-min))    
1012             (and (re-search-forward "^Sender:" nil t)
1013                  (delete-region (progn (beginning-of-line) (point))
1014                                 (progn (forward-line 1) (point))))
1015             (insert "Sender: " From "\n"))))
1016     ;; If there are References, and no "Re: ", then the thread has
1017     ;; changed name. See Son-of-1036.
1018     (if (and (mail-fetch-field "references")
1019              (get-buffer gnus-article-buffer))
1020         (let ((psubject (gnus-simplify-subject-re
1021                          (mail-fetch-field "subject"))))
1022           (or (and psubject gnus-reply-subject 
1023                    (string= (gnus-simplify-subject-re gnus-reply-subject)
1024                             psubject))
1025               (progn
1026                 (string-match "@" Message-ID)
1027                 (setq Message-ID
1028                       (concat (substring Message-ID 0 (match-beginning 0))
1029                               "_-_" 
1030                               (substring Message-ID (match-beginning 0))))))))
1031     ;; Go through all the required headers and see if they are in the
1032     ;; articles already. If they are not, or are empty, they are
1033     ;; inserted automatically - except for Subject, Newsgroups and
1034     ;; Distribution. 
1035     (while headers
1036       (goto-char (point-min))
1037       (setq elem (car headers))
1038       (if (consp elem)
1039           (setq header (car elem))
1040         (setq header elem))
1041       (if (or (not (re-search-forward 
1042                     (concat "^" (downcase (symbol-name header)) ":") nil t))
1043               (progn
1044                 (if (= (following-char) ? ) (forward-char 1) (insert " "))
1045                 (looking-at "[ \t]*$")))
1046           (progn
1047             (setq value 
1048                   (or (if (consp elem)
1049                           ;; The element is a cons.  Either the cdr is
1050                           ;; a string to be inserted verbatim, or it
1051                           ;; is a function, and we insert the value
1052                           ;; returned from this function.
1053                           (or (and (stringp (cdr elem)) (cdr elem))
1054                               (and (fboundp (cdr elem)) (funcall (cdr elem))))
1055                         ;; The element is a symbol.  We insert the
1056                         ;; value of this symbol, if any.
1057                         (and (boundp header) (symbol-value header)))
1058                       ;; We couldn't generate a value for this header,
1059                       ;; so we just ask the user.
1060                       (read-from-minibuffer
1061                        (format "Empty header for %s; enter value: " header))))
1062             ;; Finally insert the header.
1063             (save-excursion
1064               (if (bolp)
1065                   (progn
1066                     (goto-char (point-max))
1067                     (insert (symbol-name header) ": " value "\n")
1068                     (forward-line -1))
1069                 (replace-match value t t))
1070               ;; Add the deletable property to the headers that require it.
1071               (and (memq header gnus-deletable-headers)
1072                    (progn (beginning-of-line) (looking-at "[^:]+: "))
1073                    (add-text-properties 
1074                     (point) (match-end 0)
1075                     '(gnus-deletable t face italic) (current-buffer))))))
1076       (setq headers (cdr headers)))))
1077
1078 (defun gnus-inews-insert-signature ()
1079   "Insert a signature file.
1080 If `gnus-signature-function' is bound and returns a string, this
1081 string is used instead of the variable `gnus-signature-file'.
1082 In either case, if the string is a file name, this file is
1083 inserted. If the string is not a file name, the string itself is
1084 inserted. 
1085 If you never want any signature inserted, set both those variables to
1086 nil."
1087   (save-excursion
1088     (let ((signature 
1089            (or (and gnus-signature-function
1090                     (funcall gnus-signature-function gnus-newsgroup-name))
1091                gnus-signature-file)))
1092       (if (and signature
1093                (or (file-exists-p signature)
1094                    (string-match " " signature)
1095                    (not (string-match 
1096                          "^/[^/]+/" (expand-file-name signature)))))
1097           (progn
1098             (goto-char (point-max))
1099             (if (and mail-signature (search-backward "\n-- \n" nil t))
1100                 ()
1101               ;; Delete any previous signatures.
1102               (if (search-backward "\n-- \n" nil t)
1103                   (delete-region (1+ (point)) (point-max)))
1104               (insert "\n-- \n")
1105               (if (file-exists-p signature)
1106                   (insert-file-contents signature)
1107                 (insert signature))
1108               (goto-char (point-max))
1109               (or (bolp) (insert "\n"))))))))
1110
1111 ;; Written by "Mr. Per Persson" <pp@solace.mh.se>.
1112 (defun gnus-inews-insert-mime-headers ()
1113   (let ((mail-header-separator ""))
1114     (or (mail-position-on-field "Mime-Version")
1115         (insert "1.0")
1116         (cond ((save-excursion
1117                  (beginning-of-buffer)
1118                  (re-search-forward "[\200-\377]" nil t))
1119                (or (mail-position-on-field "Content-Type")
1120                    (insert "text/plain; charset=ISO-8859-1"))
1121                (or (mail-position-on-field "Content-Transfer-Encoding")
1122                    (insert "8bit")))
1123               (t (or (mail-position-on-field "Content-Type")
1124                      (insert "text/plain; charset=US-ASCII"))
1125                  (or (mail-position-on-field "Content-Transfer-Encoding")
1126                      (insert "7bit")))))))
1127
1128 (defun gnus-inews-do-fcc ()
1129   "Process FCC: fields in current article buffer.
1130 Unless the first character of the field is `|', the article is saved
1131 to the specified file using the function specified by the variable
1132 gnus-author-copy-saver.  The default function rmail-output saves in
1133 Unix mailbox format.
1134 If the first character is `|', the contents of the article is send to
1135 a program specified by the rest of the value."
1136   (let ((fcc-list nil)
1137         (fcc-file nil)
1138         (case-fold-search t))           ;Should ignore case.
1139     (save-excursion
1140       (save-restriction
1141         (goto-char (point-min))
1142         (search-forward "\n\n")
1143         (narrow-to-region (point-min) (point))
1144         (goto-char (point-min))
1145         (while (re-search-forward "^FCC:[ \t]*" nil t)
1146           (setq fcc-list
1147                 (cons (buffer-substring
1148                        (point)
1149                        (progn
1150                          (end-of-line)
1151                          (skip-chars-backward " \t")
1152                          (point)))
1153                       fcc-list))
1154           (delete-region (match-beginning 0)
1155                          (progn (forward-line 1) (point))))
1156         ;; Process FCC operations.
1157         (widen)
1158         (while fcc-list
1159           (setq fcc-file (car fcc-list))
1160           (setq fcc-list (cdr fcc-list))
1161           (cond ((string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" fcc-file)
1162                  (let ((program (substring fcc-file
1163                                            (match-beginning 1) (match-end 1))))
1164                    ;; Suggested by yuki@flab.fujitsu.junet.
1165                    ;; Send article to named program.
1166                    (call-process-region (point-min) (point-max) shell-file-name
1167                                         nil nil nil "-c" program)))
1168                 (t
1169                  ;; Suggested by hyoko@flab.fujitsu.junet.
1170                  ;; Save article in Unix mail format by default.
1171                  (gnus-make-directory (file-name-directory fcc-file))
1172                  (if (and gnus-author-copy-saver
1173                           (not (eq gnus-author-copy-saver 'rmail-output)))
1174                      (funcall gnus-author-copy-saver fcc-file)
1175                    (if (and (file-readable-p fcc-file) (rmail-file-p fcc-file))
1176                        (gnus-output-to-rmail fcc-file)
1177                      (rmail-output fcc-file 1 t t))))))))))
1178
1179 (defun gnus-inews-path ()
1180   "Return uucp path."
1181   (let ((login-name (gnus-inews-login-name)))
1182     (cond ((null gnus-use-generic-path)
1183            (concat (nth 1 gnus-select-method) "!" login-name))
1184           ((stringp gnus-use-generic-path)
1185            ;; Support GENERICPATH.  Suggested by vixie@decwrl.dec.com.
1186            (concat gnus-use-generic-path "!" login-name))
1187           (t login-name))))
1188
1189 (defun gnus-inews-user-name ()
1190   "Return user's network address as \"NAME@DOMAIN (FULL-NAME)\"."
1191   (let ((full-name (gnus-inews-full-name)))
1192     (or gnus-user-from-line
1193         (concat (if (or gnus-user-login-name gnus-use-generic-from
1194                         gnus-local-domain (getenv "DOMAINNAME"))
1195                     (concat (gnus-inews-login-name) "@"
1196                             (gnus-inews-domain-name gnus-use-generic-from))
1197                   user-mail-address)
1198                 ;; User's full name.
1199                 (cond ((string-equal full-name "") "")
1200                       ((string-equal full-name "&") ;Unix hack.
1201                        (concat " (" (user-login-name) ")"))
1202                       (t
1203                        (concat " (" full-name ")")))))))
1204
1205 (defun gnus-inews-login-name ()
1206   "Return login name."
1207   (or gnus-user-login-name (getenv "LOGNAME") (user-login-name)))
1208
1209 (defun gnus-inews-full-name ()
1210   "Return full user name."
1211   (or gnus-user-full-name (getenv "NAME") (user-full-name)))
1212
1213 (defun gnus-inews-domain-name (&optional genericfrom)
1214   "Return user's domain name.
1215 If optional argument GENERICFROM is a string, use it as the domain
1216 name; if it is non-nil, strip off local host name from the domain name.
1217 If the function `system-name' returns full internet name and the
1218 domain is undefined, the domain name is got from it."
1219   (if (or genericfrom gnus-local-domain (getenv "DOMAINNAME"))
1220       (let* ((system-name (system-name))
1221              (domain 
1222               (or (if (stringp genericfrom) genericfrom)
1223                   (getenv "DOMAINNAME")
1224                   gnus-local-domain
1225                   ;; Function `system-name' may return full internet name.
1226                   ;; Suggested by Mike DeCorte <mrd@sun.soe.clarkson.edu>.
1227                   (if (string-match "\\." system-name)
1228                       (substring system-name (match-end 0)))
1229                   (read-string "Domain name (no host): ")))
1230              (host (or (if (string-match "\\." system-name)
1231                            (substring system-name 0 (match-beginning 0)))
1232                        system-name)))
1233         (if (string-equal "." (substring domain 0 1))
1234             (setq domain (substring domain 1)))
1235         ;; Support GENERICFROM as same as standard Bnews system.
1236         ;; Suggested by ohm@kaba.junet and vixie@decwrl.dec.com.
1237         (cond ((null genericfrom)
1238                (concat host "." domain))
1239               ;;((stringp genericfrom) genericfrom)
1240               (t domain)))
1241     (if (string-match "\\." (system-name))
1242         (system-name)
1243       (substring user-mail-address 
1244                  (1+ (string-match "@" user-mail-address))))))
1245
1246 (defun gnus-inews-full-address ()
1247   (let ((domain (gnus-inews-domain-name))
1248         (system (system-name))
1249         (case-fold-search t))
1250     (if (string-match "\\." system) system
1251       (if (string-match (concat "^" (regexp-quote system)) domain) domain
1252         (concat system "." domain)))))
1253
1254 (defun gnus-inews-message-id ()
1255   "Generate unique Message-ID for user."
1256   ;; Message-ID should not contain a slash and should be terminated by
1257   ;; a number.  I don't know the reason why it is so.
1258   (concat "<" (gnus-inews-unique-id) "@" (gnus-inews-full-address) ">"))
1259
1260 (defvar gnus-unique-id-char nil)
1261
1262 ;; If you ever change this function, make sure the new version
1263 ;; cannot generate IDs that the old version could.
1264 ;; You might for example insert a "." somewhere (not next to another dot
1265 ;; or string boundary), or modify the newsreader name to "Ding".
1266 (defun gnus-inews-unique-id ()
1267   ;; Dont use microseconds from (current-time), they may be unsupported.
1268   ;; Instead we use this randomly inited counter.
1269   (setq gnus-unique-id-char
1270         (% (1+ (or gnus-unique-id-char (logand (random t) (1- (lsh 1 20)))))
1271            ;; (current-time) returns 16-bit ints,
1272            ;; and 2^16*25 just fits into 4 digits i base 36.
1273            (* 25 25)))
1274   (let ((tm (if (fboundp 'current-time)
1275                 (current-time) '(12191 46742 287898))))
1276     (concat
1277      (if (memq system-type '(ms-dos emx vax-vms))
1278          (let ((user (downcase (gnus-inews-login-name))))
1279            (while (string-match "[^a-z0-9_]" user)
1280              (aset user (match-beginning 0) ?_))
1281            user)
1282        (gnus-number-base36 (user-uid) -1))
1283      (gnus-number-base36 (+ (car   tm) (lsh (% gnus-unique-id-char 25) 16)) 4)
1284      (gnus-number-base36 (+ (nth 1 tm) (lsh (/ gnus-unique-id-char 25) 16)) 4)
1285      ;; Append the newsreader name, because while the generated
1286      ;; ID is unique to this newsreader, other newsreaders might
1287      ;; otherwise generate the same ID via another algorithm.
1288      ".fsf")))
1289
1290
1291 (defun gnus-inews-date ()
1292   "Current time string."
1293   (timezone-make-date-arpa-standard 
1294    (current-time-string) (current-time-zone)))
1295
1296 (defun gnus-inews-organization ()
1297   "Return user's organization.
1298 The ORGANIZATION environment variable is used if defined.
1299 If not, the variable `gnus-local-organization' is used instead.
1300 If it is a function, the function will be called with the current
1301 newsgroup name as the argument.
1302 If this is a file name, the contents of this file will be used as the
1303 organization."
1304   (let* ((organization 
1305           (or (getenv "ORGANIZATION")
1306               (if gnus-local-organization
1307                   (if (and (symbolp gnus-local-organization)
1308                            (fboundp gnus-local-organization))
1309                       (funcall gnus-local-organization gnus-newsgroup-name)
1310                     gnus-local-organization))
1311               gnus-organization-file
1312               "~/.organization")))
1313     (and (stringp organization)
1314          (> (length organization) 0)
1315          (or (file-exists-p organization)
1316              (string-match " " organization)
1317              (not (string-match "^/usr/lib/" organization)))
1318          (save-excursion
1319            (gnus-set-work-buffer)
1320            (if (file-exists-p organization)
1321                (insert-file-contents organization)
1322              (insert organization))
1323            (goto-char (point-min))
1324            (while (re-search-forward " *\n *" nil t)
1325              (replace-match " " t t))
1326            (buffer-substring (point-min) (point-max))))))
1327
1328 (defun gnus-inews-lines ()
1329   "Count the number of lines and return numeric string."
1330   (save-excursion
1331     (save-restriction
1332       (widen)
1333       (goto-char (point-min))
1334       (re-search-forward 
1335        (concat "^" (regexp-quote mail-header-separator) "$"))
1336       (forward-line 1)
1337       (int-to-string (count-lines (point) (point-max))))))
1338
1339 \f
1340 ;;;
1341 ;;; Gnus Mail Functions 
1342 ;;;
1343
1344 ;;; Mail reply commands of Gnus summary mode
1345
1346 (defun gnus-summary-reply (yank &optional yank-articles)
1347   "Reply mail to news author.
1348 If prefix argument YANK is non-nil, original article is yanked automatically.
1349 Customize the variable gnus-mail-reply-method to use another mailer."
1350   (interactive "P")
1351   ;; Bug fix by jbw@bigbird.bu.edu (Joe Wells)
1352   ;; Stripping headers should be specified with mail-yank-ignored-headers.
1353   (gnus-set-global-variables)
1354   (if yank-articles (gnus-summary-goto-subject (car yank-articles)))
1355   (gnus-summary-select-article)
1356   (let ((gnus-newsgroup-name gnus-newsgroup-name))
1357     (bury-buffer gnus-article-buffer)
1358     (funcall gnus-mail-reply-method (or yank-articles (not (not yank))))))
1359
1360 (defun gnus-summary-reply-with-original (n)
1361   "Reply mail to news author with original article.
1362 Customize the variable gnus-mail-reply-method to use another mailer."
1363   (interactive "P")
1364   (gnus-summary-reply t (gnus-summary-work-articles n)))
1365
1366 (defun gnus-summary-mail-forward (post)
1367   "Forward the current message to another user.
1368 Customize the variable gnus-mail-forward-method to use another mailer."
1369   (interactive "P")
1370   (gnus-set-global-variables)
1371   (gnus-summary-select-article)
1372   (gnus-copy-article-buffer)
1373   (let ((gnus-newsgroup-name gnus-newsgroup-name))
1374     (if post
1375         (gnus-forward-using-post gnus-article-copy)
1376       (funcall gnus-mail-forward-method gnus-article-copy))))
1377
1378 (defun gnus-summary-post-forward ()
1379   "Forward the current article to a newsgroup."
1380   (interactive)
1381   (gnus-summary-mail-forward t))
1382
1383 (defvar gnus-nastygram-message 
1384   "The following article was inappropriately posted to %s.\n"
1385   "Format string to insert in nastygrams.
1386 The current group name will be inserted at \"%s\".")
1387
1388 (defun gnus-summary-mail-nastygram (n)
1389   "Send a nastygram to the author of the current article."
1390   (interactive "P")
1391   (if (or gnus-expert-user
1392           (gnus-y-or-n-p 
1393            "Really send a nastygram to the author of the current article? "))
1394       (let ((group gnus-newsgroup-name))
1395         (gnus-summary-reply-with-original n)
1396         (set-buffer gnus-mail-buffer)
1397         (insert (format gnus-nastygram-message group))
1398         (gnus-mail-send-and-exit))))
1399
1400 (defun gnus-summary-mail-other-window ()
1401   "Compose mail in other window.
1402 Customize the variable `gnus-mail-other-window-method' to use another
1403 mailer."
1404   (interactive)
1405   (gnus-set-global-variables)
1406   (let ((gnus-mail-buffer nntp-server-buffer))
1407     (gnus-configure-windows 'summary-mail))
1408   (let ((gnus-newsgroup-name gnus-newsgroup-name))
1409     (funcall gnus-mail-other-window-method)))
1410
1411 (defun gnus-mail-reply-using-mail (&optional yank to-address)
1412   (save-excursion
1413     (set-buffer gnus-summary-buffer)
1414     (let ((group (gnus-group-real-name gnus-newsgroup-name))
1415           (cur (cons (current-buffer) (cdr gnus-article-current)))
1416           (winconf (current-window-configuration))
1417           from subject date reply-to message-of
1418           references message-id sender follow-to sendto elt)
1419       (set-buffer (get-buffer-create gnus-mail-buffer))
1420       (mail-mode)
1421       (make-local-variable 'gnus-article-reply)
1422       (setq gnus-article-reply cur)
1423       (make-local-variable 'gnus-prev-winconf)
1424       (setq gnus-prev-winconf winconf)
1425       (if (and (buffer-modified-p)
1426                (> (buffer-size) 0)
1427                (not (gnus-y-or-n-p 
1428                      "Unsent article being composed; erase it? ")))
1429           ()
1430         (erase-buffer)
1431         (save-excursion
1432           (gnus-copy-article-buffer)
1433           (save-restriction
1434             (set-buffer gnus-article-copy)
1435             (gnus-narrow-to-headers)
1436             (if (and (boundp 'gnus-reply-to-function)
1437                      gnus-reply-to-function)
1438                 (setq follow-to (funcall gnus-reply-to-function group)))
1439             (setq from (mail-fetch-field "from"))
1440             (setq date (or (mail-fetch-field "date") 
1441                            (header-date gnus-current-headers)))
1442             (and from
1443                  (let ((stop-pos 
1444                         (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
1445                    (setq message-of
1446                          (concat (if stop-pos (substring from 0 stop-pos) from)
1447                                  "'s message of " date))))
1448             (setq sender (mail-fetch-field "sender"))
1449             (setq subject (or (mail-fetch-field "subject")
1450                               "Re: none"))
1451             (or (string-match "^[Rr][Ee]:" subject)
1452                 (setq subject (concat "Re: " subject)))
1453             (setq reply-to (mail-fetch-field "reply-to"))
1454             (setq references (mail-fetch-field "references"))
1455             (setq message-id (mail-fetch-field "message-id"))
1456             (widen))
1457           (setq news-reply-yank-from (or from "(nobody)")))
1458         (setq news-reply-yank-message-id
1459               (or message-id "(unknown Message-ID)"))
1460
1461         ;; Gather the "to" addresses out of the follow-to list and remove
1462         ;; them as we go.
1463         (if (and follow-to (listp follow-to))
1464             (while (setq elt (assoc "To" follow-to))
1465               (setq sendto (concat sendto (and sendto ", ") (cdr elt)))
1466               (setq follow-to (delq elt follow-to))))
1467
1468         (mail-setup (or to-address 
1469                         (if (and follow-to (not (stringp follow-to))) sendto
1470                           (or follow-to reply-to from sender "")))
1471                     subject message-of nil gnus-article-copy nil)
1472
1473         (use-local-map (copy-keymap mail-mode-map))
1474         (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
1475
1476         (if (and follow-to (listp follow-to))
1477             (progn
1478               (goto-char (point-min))
1479               (re-search-forward "^To:" nil t)
1480               (beginning-of-line)
1481               (forward-line 1)
1482               (while follow-to
1483                 (insert (car (car follow-to)) ": " (cdr (car follow-to)) "\n")
1484                 (setq follow-to (cdr follow-to)))))
1485         ;; Fold long references line to follow RFC1036.
1486         (mail-position-on-field "References")
1487         (let ((begin (- (point) (length "References: ")))
1488               (fill-column 78)
1489               (fill-prefix "\t"))
1490           (if references (insert references))
1491           (if (and references message-id) (insert " "))
1492           (if message-id (insert message-id))
1493           ;; The region must end with a newline to fill the region
1494           ;; without inserting extra newline.
1495           (fill-region-as-paragraph begin (1+ (point))))
1496         (goto-char (point-min))
1497         (re-search-forward
1498          (concat "^" (regexp-quote mail-header-separator) "$"))
1499         (forward-line 1)
1500         (if (not yank)
1501             (gnus-configure-windows 'reply)
1502           (let ((last (point))
1503                 end)
1504             (if (not (listp yank))
1505                 (progn
1506                   (save-excursion
1507                     (mail-yank-original nil))
1508                   (or mail-yank-hooks mail-citation-hook
1509                       (run-hooks 'news-reply-header-hook)))
1510               (while yank
1511                 (save-window-excursion
1512                   (set-buffer gnus-summary-buffer)
1513                   (gnus-summary-select-article nil nil nil (car yank))
1514                   (gnus-summary-remove-process-mark (car yank)))
1515                 (save-excursion
1516                   (gnus-copy-article-buffer)
1517                   (mail-yank-original nil)
1518                   (setq end (point)))
1519                 (or mail-yank-hooks mail-citation-hook
1520                     (run-hooks 'news-reply-header-hook))
1521                 (goto-char end)
1522                 (setq yank (cdr yank))))
1523             (goto-char last))
1524           (gnus-configure-windows 'reply-yank))
1525         (run-hooks 'gnus-mail-hook)))))
1526
1527 (defun gnus-mail-yank-original ()
1528   (interactive)
1529   (save-excursion
1530    (mail-yank-original nil))
1531   (or mail-yank-hooks mail-citation-hook
1532       (run-hooks 'news-reply-header-hook)))
1533
1534 (defun gnus-mail-send-and-exit ()
1535   (interactive)
1536   (let ((reply gnus-article-reply)
1537         (winconf gnus-prev-winconf))
1538     (mail-send-and-exit nil)
1539     (if (get-buffer gnus-group-buffer)
1540         (progn
1541           (if (gnus-buffer-exists-p (car-safe reply))
1542               (progn
1543                 (set-buffer (car reply))
1544                 (and (cdr reply)
1545                      (gnus-summary-mark-article-as-replied 
1546                       (cdr reply)))))
1547           (and winconf (set-window-configuration winconf))))))
1548
1549 (defun gnus-forward-make-subject (buffer)
1550   (save-excursion
1551     (set-buffer buffer)
1552     (concat "[" (if (memq 'mail (assoc (symbol-name 
1553                                         (car (gnus-find-method-for-group 
1554                                               gnus-newsgroup-name)))
1555                                        gnus-valid-select-methods))
1556                     (gnus-fetch-field "From")
1557                 gnus-newsgroup-name)
1558             "] " (or (gnus-fetch-field "Subject") ""))))
1559
1560 (defun gnus-forward-insert-buffer (buffer)
1561   (let ((beg (goto-char (point-max))))
1562     (insert "------- Start of forwarded message -------\n")
1563     (insert-buffer buffer)
1564     (goto-char (point-max))
1565     (insert "------- End of forwarded message -------\n")
1566     ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>. 
1567     (goto-char beg)
1568     (while (setq beg (next-single-property-change (point) 'invisible))
1569       (goto-char beg)
1570       (delete-region beg (or (next-single-property-change 
1571                               (point) 'invisible)
1572                              (point-max))))))
1573
1574 (defun gnus-mail-forward-using-mail (&optional buffer)
1575   "Forward the current message to another user using mail."
1576   ;; This is almost a carbon copy of rmail-forward in rmail.el.
1577   (let* ((forward-buffer (or buffer (current-buffer)))
1578          (winconf (current-window-configuration))
1579          (subject (gnus-forward-make-subject forward-buffer)))
1580     (set-buffer forward-buffer)
1581     (mail nil nil subject)
1582     (use-local-map (copy-keymap (current-local-map)))
1583     (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
1584     (make-local-variable 'gnus-prev-winconf)
1585     (setq gnus-prev-winconf winconf)
1586     (gnus-forward-insert-buffer forward-buffer)
1587     (goto-char (point-min))
1588     (re-search-forward "^To: " nil t)
1589     (gnus-configure-windows 'mail-forward)
1590     ;; You have a chance to arrange the message.
1591     (run-hooks 'gnus-mail-forward-hook)
1592     (run-hooks 'gnus-mail-hook)))
1593
1594 (defun gnus-forward-using-post (&optional buffer)
1595   (let* ((forward-buffer (or buffer (current-buffer))) 
1596          (subject (gnus-forward-make-subject forward-buffer)))
1597     (gnus-post-news 'post nil nil nil nil subject)
1598     (save-excursion
1599       (gnus-forward-insert-buffer forward-buffer)
1600       ;; You have a chance to arrange the message.
1601       (run-hooks 'gnus-mail-forward-hook))))
1602
1603 (defun gnus-mail-other-window-using-mail ()
1604   "Compose mail other window using mail."
1605   (let ((winconf (current-window-configuration)))
1606     (mail-other-window nil nil nil nil nil (get-buffer gnus-article-buffer))
1607     (use-local-map (copy-keymap (current-local-map)))
1608     (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
1609     (make-local-variable 'gnus-prev-winconf)
1610     (setq gnus-prev-winconf winconf)
1611     (run-hooks 'gnus-mail-hook)))
1612
1613 (defun gnus-article-mail (yank)
1614   "Send a reply to the address near point.
1615 If YANK is non-nil, include the original article."
1616   (interactive "P")
1617   (let ((address 
1618          (buffer-substring
1619           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
1620           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
1621     (and address
1622          (progn
1623            (switch-to-buffer gnus-summary-buffer)
1624            (funcall gnus-mail-reply-method yank address)))))
1625
1626 (defun gnus-article-mail-with-original ()
1627   "Send a reply to the address near point and include the original article."
1628   (interactive)
1629   (gnus-article-mail 'yank))
1630
1631 (defun gnus-bug ()
1632   "Send a bug report to the Gnus maintainers."
1633   (interactive)
1634   (let ((winconf (current-window-configuration)))
1635     (delete-other-windows)
1636     (switch-to-buffer "*Gnus Help Bug*")
1637     (erase-buffer)
1638     (insert gnus-bug-message)
1639     (goto-char (point-min))
1640     (pop-to-buffer "*Gnus Bug*")
1641     (erase-buffer)
1642     (mail-mode)
1643     (mail-setup gnus-maintainer nil nil nil nil nil)
1644     (make-local-variable 'gnus-prev-winconf)
1645     (setq gnus-prev-winconf winconf)
1646     (use-local-map (copy-keymap mail-mode-map))
1647     (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
1648     (goto-char (point-min))
1649     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1650     (forward-line 1)
1651     (insert (format "%s\n%s\n\n\n\n\n" (gnus-version) (emacs-version)))
1652     (let ((b (point)))
1653       (gnus-debug)
1654       (goto-char (- b 3)))
1655     (message "")))
1656
1657 (defun gnus-debug ()
1658   "Attemps to go through the Gnus source file and report what variables have been changed.
1659 The source file has to be in the Emacs load path."
1660   (interactive)
1661   (let ((files '("gnus.el" "gnus-msg.el" "gnus-score.el"))
1662         file dirs expr olist)
1663     (message "Please wait while we snoop your variables...")
1664     (sit-for 0)
1665     (save-excursion
1666       (set-buffer (get-buffer-create " *gnus bug info*"))
1667       (buffer-disable-undo (current-buffer))
1668       (while files
1669         (erase-buffer)
1670         (setq dirs load-path)
1671         (while dirs
1672           (if (or (not (car dirs))
1673                   (not (stringp (car dirs)))
1674                   (not (file-exists-p 
1675                         (setq file (concat (file-name-as-directory 
1676                                             (car dirs)) (car files))))))
1677               (setq dirs (cdr dirs))
1678             (setq dirs nil)
1679             (insert-file-contents file)
1680             (goto-char (point-min))
1681             (or (re-search-forward "^;;* *Internal variables" nil t)
1682                 (error "Malformed sources in file %s" file))
1683             (narrow-to-region (point-min) (point))
1684             (goto-char (point-min))
1685             (while (setq expr (condition-case () 
1686                                   (read (current-buffer)) (error nil)))
1687               (and (eq (car expr) 'defvar)
1688                    (stringp (nth 3 expr))
1689                    (or (not (boundp (nth 1 expr)))
1690                        (not (equal (eval (nth 2 expr))
1691                                    (symbol-value (nth 1 expr)))))
1692                    (setq olist (cons (nth 1 expr) olist))))))
1693         (setq files (cdr files)))
1694       (kill-buffer (current-buffer)))
1695     (insert "------------------- Environment follows -------------------\n\n")
1696     (while olist
1697       (if (boundp (car olist))
1698           (insert "(setq " (symbol-name (car olist)) " '" 
1699                   (prin1-to-string (symbol-value (car olist))) ")\n")
1700         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1701       (setq olist (cdr olist)))
1702     (insert "\n\n")))
1703
1704 (gnus-ems-redefine)
1705
1706 (provide 'gnus-msg)
1707
1708 ;;; gnus-msg.el ends here