1 ;;; Post news commands for GNUS newsreader
2 ;; Copyright (C) 1989 Fujitsu Laboratories LTD.
3 ;; Copyright (C) 1989, 1990 Masanobu UMEDA
4 ;; $Header: gnuspost.el,v 1.2 90/03/23 13:25:16 umerin Locked $
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is distributed in the hope that it will be useful,
9 ;; but WITHOUT ANY WARRANTY. No author or distributor
10 ;; accepts responsibility to anyone for the consequences of using it
11 ;; or for whether it serves any particular purpose or works at all,
12 ;; unless he says so in writing. Refer to the GNU Emacs General Public
13 ;; License for full details.
15 ;; Everyone is granted permission to copy, modify and redistribute
16 ;; GNU Emacs, but only under the conditions described in the
17 ;; GNU Emacs General Public License. A copy of this license is
18 ;; supposed to have been given to you along with GNU Emacs so you
19 ;; can know your rights and responsibilities. It should be in a
20 ;; file named COPYING. Among other things, the copyright notice
21 ;; and this notice must be preserved on all copies.
26 (defvar gnus-organization-file
"/usr/lib/news/organization"
27 "*Local news organization file.")
29 (defvar gnus-post-news-buffer
"*post-news*")
30 (defvar gnus-winconf-post-news nil
)
32 (autoload 'news-reply-mode
"rnewspost")
34 ;;; Post news commands of GNUS Group Mode and Subject Mode
36 (defun gnus-Group-post-news ()
39 ;; Save window configuration.
40 (setq gnus-winconf-post-news
(current-window-configuration))
43 (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer
))
44 (not (zerop (buffer-size))))
45 ;; Restore last window configuration.
46 (set-window-configuration gnus-winconf-post-news
)))
47 ;; We don't want to return to Subject buffer nor Article buffer later.
48 (if (get-buffer gnus-Subject-buffer
)
49 (bury-buffer gnus-Subject-buffer
))
50 (if (get-buffer gnus-Article-buffer
)
51 (bury-buffer gnus-Article-buffer
)))
53 (defun gnus-Subject-post-news ()
56 (gnus-Subject-select-article t nil
)
57 ;; Save window configuration.
58 (setq gnus-winconf-post-news
(current-window-configuration))
61 (switch-to-buffer gnus-Article-buffer
)
63 (delete-other-windows)
65 (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer
))
66 (not (zerop (buffer-size))))
67 ;; Restore last window configuration.
68 (set-window-configuration gnus-winconf-post-news
)))
69 ;; We don't want to return to Article buffer later.
70 (bury-buffer gnus-Article-buffer
))
72 (defun gnus-Subject-post-reply (yank)
73 "Post a reply article.
74 If prefix argument YANK is non-nil, original article is yanked automatically."
76 (gnus-Subject-select-article t nil
)
77 ;; Check Followup-To: poster.
78 (set-buffer gnus-Article-buffer
)
79 (if (and gnus-use-followup-to
80 (string-equal "poster" (gnus-fetch-field "followup-to"))
81 (or (not (eq gnus-use-followup-to t
))
82 (not (y-or-n-p "Do you want to ignore `Followup-To: poster'? "))))
83 ;; Mail to the poster. GNUS is now RFC1036 compliant.
84 (gnus-Subject-mail-reply yank
)
85 ;; Save window configuration.
86 (setq gnus-winconf-post-news
(current-window-configuration))
89 (switch-to-buffer gnus-Article-buffer
)
91 (delete-other-windows)
92 (gnus-news-reply yank
))
93 (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer
))
94 (not (zerop (buffer-size))))
95 ;; Restore last window configuration.
96 (set-window-configuration gnus-winconf-post-news
)))
97 ;; We don't want to return to Article buffer later.
98 (bury-buffer gnus-Article-buffer
)))
100 (defun gnus-Subject-post-reply-with-original ()
101 "Post a reply article with original article."
103 (gnus-Subject-post-reply t
))
105 (defun gnus-Subject-cancel-article ()
106 "Cancel an article you posted."
108 (gnus-Subject-select-article t nil
)
109 (gnus-eval-in-buffer-window gnus-Article-buffer
113 ;;; Post a News using NNTP
116 (fset 'sendnews
'gnus-post-news
)
118 (fset 'postnews
'gnus-post-news
)
120 (defun gnus-post-news ()
121 "Begin editing a new USENET news article to be posted.
122 Type \\[describe-mode] once editing the article to get a list of commands."
124 (if (or (not gnus-novice-user
)
125 (y-or-n-p "Are you sure you want to post to all of USENET? "))
126 (let ((artbuf (current-buffer))
127 (newsgroups ;Default newsgroup.
128 (if (eq major-mode
'gnus-Article-mode
) gnus-newsgroup-name
))
132 (and (not (zerop (buffer-size)))
133 ;;(equal major-mode 'news-mode)
134 (equal major-mode
'gnus-Article-mode
)
136 ;;(news-show-all-headers)
137 (gnus-Article-show-all-headers)
138 (narrow-to-region (point-min)
139 (progn (goto-char (point-min))
140 (search-forward "\n\n")
142 (setq news-reply-yank-from
(mail-fetch-field "from"))
143 (setq news-reply-yank-message-id
(mail-fetch-field "message-id")))
144 (pop-to-buffer gnus-post-news-buffer
)
146 (gnus-overload-functions)
147 (if (and (buffer-modified-p)
149 (not (y-or-n-p "Unsent article being composed; erase it? ")))
150 ;; Continue composition.
151 ;; Make news-reply-yank-original work on the current article.
152 (setq mail-reply-buffer artbuf
)
154 (if gnus-interactive-post
155 ;; Newsgroups, subject and distribution are asked for.
156 ;; Suggested by yuki@flab.fujitsu.junet.
158 ;; Subscribed newsgroup names are required for
159 ;; completing read of newsgroup.
160 (or gnus-newsrc-assoc
161 (gnus-read-newsrc-file))
162 ;; Which do you like? (UMERIN)
163 ;; (setq newsgroups (read-string "Newsgroups: " "general"))
164 (or newsgroups
;Use the default newsgroup.
166 (completing-read "Newsgroup: " gnus-newsrc-assoc
168 newsgroups
;Default newsgroup.
170 (setq subject
(read-string "Subject: "))
172 (substring newsgroups
0 (string-match "\\." newsgroups
)))
173 (if (string-equal distribution newsgroups
)
174 ;; Newsgroup may be general or control. In this
175 ;; case, use default distribution.
176 (setq distribution gnus-default-distribution
))
178 (read-string "Distribution: " distribution
))
179 ;; An empty string is ok to ignore gnus-default-distribution.
180 ;;(if (string-equal distribution "")
181 ;; (setq distribution nil))
183 (news-setup () subject
() newsgroups artbuf
)
184 ;; Make sure the article is posted by GNUS.
185 ;;(mail-position-on-field "Posting-Software")
186 ;;(insert "GNUS: NNTP-based News Reader for GNU Emacs")
187 ;; Insert Distribution: field.
188 ;; Suggested by ichikawa@flab.fujitsu.junet.
189 (mail-position-on-field "Distribution")
190 (insert (or distribution gnus-default-distribution
""))
191 ;; Handle author copy using FCC field.
194 (mail-position-on-field "FCC")
195 (insert gnus-author-copy
)))
196 (if gnus-interactive-post
197 ;; All fields are filled in.
198 (goto-char (point-max))
199 ;; Move point to Newsgroup: field.
200 (goto-char (point-min))
205 (defun gnus-news-reply (&optional yank
)
206 "Compose and post a reply (aka a followup) to the current article on USENET.
207 While composing the followup, use \\[news-reply-yank-original] to yank the
208 original message into it."
210 (if (or (not gnus-novice-user
)
211 (y-or-n-p "Are you sure you want to followup to all of USENET? "))
212 (let (from cc subject date to followup-to newsgroups message-of
213 references distribution message-id
214 (artbuf (current-buffer)))
216 (and (not (zerop (buffer-size)))
217 ;;(equal major-mode 'news-mode)
218 (equal major-mode
'gnus-Article-mode
)
220 ;; (news-show-all-headers)
221 (gnus-Article-show-all-headers)
222 (narrow-to-region (point-min)
223 (progn (goto-char (point-min))
224 (search-forward "\n\n")
226 (setq from
(mail-fetch-field "from"))
227 (setq news-reply-yank-from from
)
228 (setq subject
(mail-fetch-field "subject"))
229 (setq date
(mail-fetch-field "date"))
230 (setq followup-to
(mail-fetch-field "followup-to"))
231 ;; Ignore Followup-To: poster.
232 (if (or (null gnus-use-followup-to
) ;Ignore followup-to: field.
233 (string-equal "" followup-to
) ;Bogus header.
234 (string-equal "poster" followup-to
))
235 (setq followup-to nil
))
236 (setq newsgroups
(or followup-to
(mail-fetch-field "newsgroups")))
237 (setq references
(mail-fetch-field "references"))
238 (setq distribution
(mail-fetch-field "distribution"))
239 (setq message-id
(mail-fetch-field "message-id"))
240 (setq news-reply-yank-message-id message-id
))
241 (pop-to-buffer gnus-post-news-buffer
)
243 (gnus-overload-functions)
244 (if (and (buffer-modified-p)
246 (not (y-or-n-p "Unsent article being composed; erase it? ")))
247 ;; Continue composition.
248 ;; Make news-reply-yank-original work on current article.
249 (setq mail-reply-buffer artbuf
)
253 (concat "Re: " (gnus-simplify-subject subject
're-only
))))
257 (string-match " *at \\| *@ \\| *(\\| *<" from
)))
260 (if stop-pos
(substring from
0 stop-pos
) from
)
263 (news-setup nil subject message-of newsgroups artbuf
)
265 (progn (news-reply-followup-to)
266 (insert followup-to
)))
267 ;; Fold long references line to follow RFC1036.
268 (mail-position-on-field "References")
269 (let ((begin (point))
274 (if (and references message-id
)
278 ;; The region must end with a newline to fill the region
279 ;; without inserting extra newline.
280 (fill-region-as-paragraph begin
(1+ (point))))
281 ;; Make sure the article is posted by GNUS.
282 ;;(mail-position-on-field "Posting-Software")
283 ;;(insert "GNUS: NNTP-based News Reader for GNU Emacs")
284 ;; Distribution must be the same as original article.
285 (mail-position-on-field "Distribution")
286 (insert (or distribution
""))
287 ;; Handle author copy using FCC field.
290 (mail-position-on-field "FCC")
291 (insert gnus-author-copy
)))
292 (goto-char (point-max)))
293 ;; Yank original article automatically.
295 (let ((last (point)))
296 (goto-char (point-max))
297 (news-reply-yank-original nil
)
302 (defun gnus-inews-news ()
303 "Send a news message."
305 (let* ((case-fold-search nil
)
306 (server-running (gnus-server-opened)))
308 ;; It is possible to post a news without reading news using
310 ;; Suggested by yuki@flab.fujitsu.junet.
311 (gnus-start-news-server) ;Use default server.
312 ;; NNTP server must be opened before current buffer is modified.
314 (goto-char (point-min))
315 (run-hooks 'news-inews-hook
)
316 (goto-char (point-min))
317 (search-forward (concat "\n" mail-header-separator
"\n"))
318 (replace-match "\n\n")
319 (goto-char (point-max))
320 ;; require a newline at the end for inews to append .signature to
321 (or (= (preceding-char) ?
\n)
323 (message "Posting to USENET...")
324 ;; Post to NNTP server.
325 (if (gnus-inews-article)
326 (message "Posting to USENET... done")
327 ;; We cannot signal an error.
328 (ding) (message "Article rejected: %s" (gnus-status-message)))
329 (goto-char (point-min)) ;restore internal header separator
330 (search-forward "\n\n")
331 (replace-match (concat "\n" mail-header-separator
"\n"))
332 (set-buffer-modified-p nil
))
333 ;; If NNTP server is opened by gnus-inews-news, close it by myself.
336 (and (fboundp 'bury-buffer
) (bury-buffer))
337 ;; Restore last window configuration.
338 (and gnus-winconf-post-news
339 (set-window-configuration gnus-winconf-post-news
))
340 (setq gnus-winconf-post-news nil
)
343 (defun gnus-cancel-news ()
344 "Cancel an article you posted."
346 (if (yes-or-no-p "Do you really want to cancel this article? ")
352 ;; Get header info. from original article.
354 (gnus-Article-show-all-headers)
355 (goto-char (point-min))
356 (search-forward "\n\n")
357 (narrow-to-region (point-min) (point))
358 (setq from
(mail-fetch-field "from"))
359 (setq newsgroups
(mail-fetch-field "newsgroups"))
360 (setq message-id
(mail-fetch-field "message-id"))
361 (setq distribution
(mail-fetch-field "distribution")))
362 ;; Verify if the article is absolutely user's by comparing
363 ;; user id with value of its From: field.
366 (downcase (mail-strip-quoted-names from
))
367 (downcase (mail-strip-quoted-names (gnus-inews-user-name)))))
369 (ding) (message "This article is not yours"))
370 ;; Make control article.
371 (set-buffer (get-buffer-create " *GNUS-posting*"))
372 (buffer-flush-undo (current-buffer))
374 (insert "Newsgroups: " newsgroups
"\n"
375 "Subject: cancel " message-id
"\n"
376 "Control: cancel " message-id
"\n"
377 ;; We should not use the value of
378 ;; `gnus-default-distribution' as default value,
379 ;; because distribution must be as same as original
381 "Distribution: " (or distribution
"") "\n"
383 ;; Prepare article headers.
384 (gnus-inews-insert-headers)
385 (goto-char (point-max))
386 ;; Insert empty line.
388 ;; Send the control article to NNTP server.
389 (message "Canceling your article...")
390 (if (gnus-request-post)
391 (message "Canceling your article... done")
392 (ding) (message "Failed to cancel your article"))
393 (kill-buffer (current-buffer))
398 ;;; Lowlevel inews interface
400 (defun gnus-inews-article ()
401 "NNTP inews interface."
403 (if gnus-signature-file
404 (expand-file-name gnus-signature-file nil
)))
406 (artbuf (current-buffer))
407 (tmpbuf (get-buffer-create " *GNUS-posting*")))
410 (buffer-flush-undo (current-buffer))
412 (insert-buffer-substring artbuf
)
415 (goto-char (point-min))
416 (search-forward "\n\n")
417 (narrow-to-region (point-min) (point))
418 (setq distribution
(mail-fetch-field "distribution")))
422 ;; Change signature file by distribution.
423 ;; Suggested by hyoko@flab.fujitsu.junet.
424 (if (file-exists-p (concat signature
"-" distribution
))
425 (setq signature
(concat signature
"-" distribution
)))
427 (if (file-exists-p signature
)
429 (goto-char (point-max))
431 (insert-file-contents signature
)))
433 ;; Prepare article headers.
435 (goto-char (point-min))
436 (search-forward "\n\n")
437 (narrow-to-region (point-min) (point))
438 (gnus-inews-insert-headers)
439 ;; Save author copy of posted article. The article must be
440 ;; copied before being posted because `gnus-request-post'
441 ;; modifies the buffer.
442 (let ((case-fold-search t
))
443 ;; Find and handle any FCC fields.
444 (goto-char (point-min))
445 (if (re-search-forward "^FCC:" nil t
)
446 (gnus-inews-do-fcc))))
448 ;; Run final inews hooks.
449 (run-hooks 'gnus-Inews-article-hook
)
450 ;; Post an article to NNTP server.
451 ;; Return NIL if post failed.
454 (kill-buffer (current-buffer)))
457 (defun gnus-inews-do-fcc ()
458 "Process FCC: fields."
461 (case-fold-search t
)) ;Should ignore case.
464 (goto-char (point-min))
465 (while (re-search-forward "^FCC:[ \t]*" nil t
)
466 (setq fcc-list
(cons (buffer-substring (point)
469 (skip-chars-backward " \t")
472 (delete-region (match-beginning 0)
473 (progn (forward-line 1) (point))))
474 ;; Process FCC operations.
477 (setq fcc-file
(car fcc-list
))
478 (setq fcc-list
(cdr fcc-list
))
479 (cond ((string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" fcc-file
)
480 (let ((program (substring fcc-file
481 (match-beginning 1) (match-end 1))))
482 ;; Suggested by yuki@flab.fujitsu.junet.
483 ;; Send article to named program.
484 (call-process-region (point-min) (point-max) shell-file-name
485 nil nil nil
"-c" program
)
488 ;; Suggested by hyoko@flab.fujitsu.junet.
489 ;; Save article in Unix mail format by default.
490 (funcall (or gnus-author-copy-saver
'rmail-output
) fcc-file
)
496 (defun gnus-inews-insert-headers ()
497 "Prepare article headers.
498 Path:, From:, Subject: and Distribution: are generated.
499 Message-ID:, Date: and Organization: are optional."
501 (let ((date (gnus-inews-date))
502 (message-id (gnus-inews-message-id))
503 (organization (gnus-inews-organization)))
504 ;; Insert from the top of headers.
505 (goto-char (point-min))
506 (insert "Path: " (gnus-inews-path) "\n")
507 (insert "From: " (gnus-inews-user-name) "\n")
508 ;; If there is no subject, make Subject: field.
509 (or (mail-fetch-field "subject")
510 (insert "Subject: \n"))
511 ;; Insert random headers.
513 (insert "Message-ID: " message-id
"\n"))
515 (insert "Date: " date
"\n"))
517 (let ((begin (point))
520 (insert "Organization: " organization
"\n")
521 (fill-region-as-paragraph begin
(point))))
522 (or (mail-fetch-field "distribution")
523 (insert "Distribution: \n"))
526 (defun gnus-inews-path ()
528 (let ((login-name (gnus-inews-login-name)))
529 (cond ((null gnus-use-generic-path
)
530 (concat gnus-nntp-server
"!" login-name
))
531 ((stringp gnus-use-generic-path
)
532 ;; Support GENERICPATH. Suggested by vixie@decwrl.dec.com.
533 (concat gnus-use-generic-path
"!" login-name
))
537 (defun gnus-inews-user-name ()
538 "Return user's network address as `NAME@DOMAIN (FULL NAME)'."
539 (let ((login-name (gnus-inews-login-name))
540 (full-name (gnus-inews-full-name)))
541 (concat login-name
"@" (gnus-inews-domain-name gnus-use-generic-from
)
543 (cond ((string-equal full-name
"") "")
544 ((string-equal full-name
"&") ;Unix hack.
545 (concat " (" login-name
")"))
547 (concat " (" full-name
")")))
550 (defun gnus-inews-login-name ()
551 "Return user login name.
552 Got from the variable `gnus-user-login-name', the environment variables
553 USER and LOGNAME, and the function `user-login-name'."
554 (or gnus-user-login-name
555 (getenv "USER") (getenv "LOGNAME") (user-login-name)))
557 (defun gnus-inews-full-name ()
558 "Return user full name.
559 Got from the variable `gnus-user-full-name', the environment variable
560 NAME, and the function `user-full-name'."
561 (or gnus-user-full-name
562 (getenv "NAME") (user-full-name)))
564 (defun gnus-inews-domain-name (&optional genericfrom
)
565 "Return user's domain name.
566 If optional argument GENERICFROM is a string, use it as the domain
567 name; if it is non-nil, strip of local host name from the domain name.
568 If the function `system-name' returns full internet name and the
569 domain is undefined, the domain name is got from it."
570 (let ((domain (or (if (stringp genericfrom
) genericfrom
)
571 (getenv "DOMAINNAME")
573 ;; Function `system-name' may return full internet name.
574 ;; Suggested by Mike DeCorte <mrd@sun.soe.clarkson.edu>.
575 (if (string-match "\\." (system-name))
576 (substring (system-name) (match-end 0)))
577 (read-string "Domain name (no host): ")))
578 (host (or (if (string-match "\\." (system-name))
579 (substring (system-name) 0 (match-beginning 0)))
581 (if (string-equal "." (substring domain
0 1))
582 (setq domain
(substring domain
1)))
583 (if (null gnus-your-domain
)
584 (setq gnus-your-domain domain
))
585 ;; Support GENERICFROM as same as standard Bnews system.
586 ;; Suggested by ohm@kaba.junet and vixie@decwrl.dec.com.
587 (cond ((null genericfrom
)
588 (concat host
"." domain
))
589 ;;((stringp genericfrom) genericfrom)
593 (defun gnus-inews-message-id ()
594 "Generate unique Message-ID for user."
595 ;; Message-ID should not contain a slash and should be terminated by
596 ;; a number. I don't know the reason why it is so.
597 (concat "<" (gnus-inews-unique-id) "@" (gnus-inews-domain-name) ">"))
599 (defun gnus-inews-unique-id ()
600 "Generate unique ID from user name and current time."
601 (let ((date (current-time-string))
602 (name (gnus-inews-login-name)))
603 (if (string-match "^[^ ]+ \\([^ ]+\\)[ ]+\\([0-9]+\\) \\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\) [0-9][0-9]\\([0-9][0-9]\\)"
605 (concat (upcase name
) "."
606 (substring date
(match-beginning 6) (match-end 6)) ;Year
607 (substring date
(match-beginning 1) (match-end 1)) ;Month
608 (substring date
(match-beginning 2) (match-end 2)) ;Day
609 (substring date
(match-beginning 3) (match-end 3)) ;Hour
610 (substring date
(match-beginning 4) (match-end 4)) ;Minute
611 (substring date
(match-beginning 5) (match-end 5)) ;Second
613 (error "Cannot understand current-time-string: %s." date
))
616 (defun gnus-inews-date ()
617 "Bnews date format string of today. Time zone is ignored."
618 ;; Insert buggy date (time zone is ignored), but I don't worry about
619 ;; it since inews will rewrite it.
620 (let ((date (current-time-string)))
621 (if (string-match "^[^ ]+ \\([^ ]+\\)[ ]+\\([0-9]+\\) \\([0-9:]+\\) [0-9][0-9]\\([0-9][0-9]\\)"
623 (concat (substring date
(match-beginning 2) (match-end 2)) ;Day
625 (substring date
(match-beginning 1) (match-end 1)) ;Month
627 (substring date
(match-beginning 4) (match-end 4)) ;Year
629 (substring date
(match-beginning 3) (match-end 3))) ;Time
630 (error "Cannot understand current-time-string: %s." date
))
633 (defun gnus-inews-organization ()
634 "Return user's organization.
635 The ORGANIZATION environment variable is used if defined.
636 If not, the variable `gnus-your-organization' is used instead.
637 If the value begins with a slash, it is taken as the name of a file
638 containing the organization."
639 ;; The organization must be got in this order since the ORGANIZATION
640 ;; environment variable is intended for user specific while
641 ;; gnus-your-organization is for machine or organization specific.
642 (let ((organization (or (getenv "ORGANIZATION")
643 gnus-your-organization
644 (expand-file-name "~/.organization" nil
))))
645 (and (stringp organization
)
646 (string-equal (substring organization
0 1) "/")
647 ;; Get it from the user and system file.
648 ;; Suggested by roland@wheaties.ai.mit.edu (Roland McGrath).
649 (let ((dist (mail-fetch-field "distribution")))
651 (cond ((file-exists-p (concat organization
"-" dist
))
652 (concat organization
"-" dist
))
653 ((file-exists-p organization
) organization
)
654 ((file-exists-p gnus-organization-file
)
655 gnus-organization-file
)
658 (cond ((not (stringp organization
)) nil
)
659 ((and (string-equal (substring organization
0 1) "/")
660 (file-exists-p organization
))
661 ;; If the first character is `/', assume it is the name of
662 ;; a file containing the organization.
664 (let ((tmpbuf (get-buffer-create " *GNUS organization*")))
667 (insert-file-contents organization
)
668 (prog1 (buffer-string)
669 (kill-buffer tmpbuf
))