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