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