*** empty log message ***
[gnus] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus)
29 (require 'sendmail)
30 (require 'gnus-ems)
31 (require 'rmail)
32
33 (defvar gnus-organization-file "/usr/lib/news/organization"
34   "*Local news organization file.")
35
36 (defvar gnus-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-current-headers)
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 (or
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