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