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