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