rnewspost.el 16.8 KB
Newer Older
Eric S. Raymond's avatar
Eric S. Raymond committed
1 2
;;; rnewspost.el --- USENET news poster/mailer for GNU Emacs

Karl Heuer's avatar
Karl Heuer committed
3
;; Copyright (C) 1985, 1986, 1987, 1995 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

Eric S. Raymond's avatar
Eric S. Raymond committed
5
;; Maintainer: FSF
Eric S. Raymond's avatar
Eric S. Raymond committed
6
;; Keywords: mail, news
Eric S. Raymond's avatar
Eric S. Raymond committed
7

Jim Blandy's avatar
Jim Blandy committed
8 9 10 11
;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
Eric S. Raymond's avatar
Eric S. Raymond committed
12
;; the Free Software Foundation; either version 2, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
13 14 15 16 17 18 19 20
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
21 22 23
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Jim Blandy's avatar
Jim Blandy committed
24

Erik Naggum's avatar
Erik Naggum committed
25
;;; Change Log:
Eric S. Raymond's avatar
Eric S. Raymond committed
26

Jim Blandy's avatar
Jim Blandy committed
27
;; moved posting and mail code from rnews.el
Karl Heuer's avatar
Karl Heuer committed
28
;;	tower@gnu.org Wed Oct 29 1986
Jim Blandy's avatar
Jim Blandy committed
29 30 31 32
;; brought posting code almost up to the revision of RFC 850 for News 2.11
;; - couldn't see handling the special meaning of the Keyword: poster
;; - not worth the code space to support the old A news Title: (which
;;   Subject: replaced) and Article-I.D.: (which Message-ID: replaced)
Karl Heuer's avatar
Karl Heuer committed
33
;;	tower@gnu.org Nov 86
Jim Blandy's avatar
Jim Blandy committed
34
;; changed C-c C-r key-binding due to rename of news-caesar-buffer-body
Karl Heuer's avatar
Karl Heuer committed
35 36
;;	tower@gnu.org 21 Nov 86
;; added (require 'rnews)	tower@gnu.org 22 Apr 87
Jim Blandy's avatar
Jim Blandy committed
37
;; restricted call of news-show-all-headers in news-post-news & news-reply
Karl Heuer's avatar
Karl Heuer committed
38 39 40
;;	tower@gnu.org 28 Apr 87
;; commented out Posting-Front-End to save USENET bytes tower@gnu.org Jul 31 87
;; commented out -n and -t args in news-inews     tower@gnu.org 15 Oct 87
Jim Blandy's avatar
Jim Blandy committed
41 42 43 44 45 46 47 48 49

;Now in paths.el.
;(defvar news-inews-program "inews"
;  "Function to post news.")

;; Replying and posting news items are done by these functions.
;; imported from rmail and modified to work with rnews ...
;; Mon Mar 25,1985 at 03:07:04 ads@mit-hermes.
;; this is done so that rnews can operate independently from rmail.el and
Karl Heuer's avatar
Karl Heuer committed
50
;; sendmail and doesn't have to autoload these functions.
Jim Blandy's avatar
Jim Blandy committed
51 52 53 54
;;
;;; >> Nuked by Mly to autoload those functions again, as the duplication of
;;; >>  code was making maintenance too difficult.

Eric S. Raymond's avatar
Eric S. Raymond committed
55 56 57 58 59
;;; Code:

(require 'sendmail)
(require 'rnews)

Jim Blandy's avatar
Jim Blandy committed
60 61 62 63 64 65 66 67 68 69 70
(defvar news-reply-mode-map () "Mode map used by news-reply.")

(or news-reply-mode-map
    (progn
      (setq news-reply-mode-map (make-keymap))
      (define-key news-reply-mode-map "\C-c\C-f\C-d" 'news-reply-distribution)
      (define-key news-reply-mode-map "\C-c\C-f\C-k" 'news-reply-keywords)
      (define-key news-reply-mode-map "\C-c\C-f\C-n" 'news-reply-newsgroups)
      (define-key news-reply-mode-map "\C-c\C-f\C-f" 'news-reply-followup-to)
      (define-key news-reply-mode-map "\C-c\C-f\C-s" 'mail-subject)
      (define-key news-reply-mode-map "\C-c\C-f\C-a" 'news-reply-summary)
71
      (define-key news-reply-mode-map "\C-c\C-t" 'mail-text)
Jim Blandy's avatar
Jim Blandy committed
72 73 74 75 76
      (define-key news-reply-mode-map "\C-c\C-r" 'news-caesar-buffer-body)
      (define-key news-reply-mode-map "\C-c\C-w" 'news-reply-signature)
      (define-key news-reply-mode-map "\C-c\C-y" 'news-reply-yank-original)
      (define-key news-reply-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
      (define-key news-reply-mode-map "\C-c\C-c" 'news-inews)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
      (define-key news-reply-mode-map "\C-c\C-s" 'news-inews)
      (define-key news-reply-mode-map [menu-bar] (make-sparse-keymap))
      (define-key news-reply-mode-map [menu-bar fields]
	(cons "Fields" (make-sparse-keymap "Fields")))
      (define-key news-reply-mode-map [menu-bar fields news-reply-distribution]
	'("Distribution" . news-reply-distribution))
      (define-key news-reply-mode-map [menu-bar fields news-reply-keywords]
	'("Keywords" . news-reply-keywords))
      (define-key news-reply-mode-map [menu-bar fields news-reply-newsgroups]
	'("Newsgroups" . news-reply-newsgroups))
      (define-key news-reply-mode-map [menu-bar fields news-reply-followup-to]
	'("Followup-to" . news-reply-followup-to))
      (define-key news-reply-mode-map [menu-bar fields mail-subject]
	'("Subject" . mail-subject))
      (define-key news-reply-mode-map [menu-bar fields news-reply-summary]
	'("Summary" . news-reply-summary))
      (define-key news-reply-mode-map [menu-bar fields mail-text]
	'("Text" . mail-text))
      (define-key news-reply-mode-map [menu-bar news]
	(cons "News" (make-sparse-keymap "News")))
      (define-key news-reply-mode-map [menu-bar news news-caesar-buffer-body]
	'("Rot13" . news-caesar-buffer-body))
      (define-key news-reply-mode-map [menu-bar news news-reply-yank-original]
	'("Yank Original" . news-reply-yank-original))
      (define-key news-reply-mode-map [menu-bar news mail-fill-yanked-message]
	'("Fill Yanked Messages" . mail-fill-yanked-message))
      (define-key news-reply-mode-map [menu-bar news news-inews]
	'("Send" . news-inews))))
Jim Blandy's avatar
Jim Blandy committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

(defun news-reply-mode ()
  "Major mode for editing news to be posted on USENET.
First-time posters are asked to please read the articles in newsgroup:
                                                     news.announce.newusers .
Like Text Mode but with these additional commands:

C-c C-s  news-inews (post the message)    C-c C-c  news-inews
C-c C-f	 move to a header field (and create it if there isn't):
	 C-c C-f C-n  move to Newsgroups:	C-c C-f C-s  move to Subj:
	 C-c C-f C-f  move to Followup-To:      C-c C-f C-k  move to Keywords:
	 C-c C-f C-d  move to Distribution:	C-c C-f C-a  move to Summary:
C-c C-y  news-reply-yank-original (insert current message, in NEWS).
C-c C-q  mail-fill-yanked-message (fill what was yanked).
C-c C-r  caesar rotate all letters by 13 places in the article's body (rot13)."
  (interactive)
  (kill-all-local-variables)
  (make-local-variable 'mail-reply-buffer)
  (setq mail-reply-buffer nil)
  (set-syntax-table text-mode-syntax-table)
  (use-local-map news-reply-mode-map)
  (setq local-abbrev-table text-mode-abbrev-table)
  (setq major-mode 'news-reply-mode)
128
  (setq mode-name "News Reply")
Jim Blandy's avatar
Jim Blandy committed
129 130 131 132
  (make-local-variable 'paragraph-separate)
  (make-local-variable 'paragraph-start)
  (run-hooks 'text-mode-hook 'news-reply-mode-hook))

133 134
(defvar news-reply-yank-from ""
  "Save `From:' field for `news-reply-yank-original'.")
Jim Blandy's avatar
Jim Blandy committed
135

136 137
(defvar news-reply-yank-message-id ""
  "Save `Message-Id:' field for `news-reply-yank-original'.")
Jim Blandy's avatar
Jim Blandy committed
138 139

(defun news-reply-yank-original (arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
140
  "Insert the message being replied to, if any (in Mail mode).
Jim Blandy's avatar
Jim Blandy committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
Puts point before the text and mark after.
Indents each nonblank line ARG spaces (default 3).
Just \\[universal-argument] as argument means don't indent
and don't delete any header fields."
  (interactive "P")
  (mail-yank-original arg)
  (exchange-point-and-mark)
  (run-hooks 'news-reply-header-hook))

(defvar news-reply-header-hook
  '(lambda ()
	 (insert "In article " news-reply-yank-message-id
			 " " news-reply-yank-from " writes:\n\n"))
  "Hook for inserting a header at the top of a yanked message.")

(defun news-reply-newsgroups ()
Richard M. Stallman's avatar
Richard M. Stallman committed
157 158 159 160
  "Move point to end of `Newsgroups:' field.
RFC 850 constrains the `Newsgroups:' field to be a comma-separated list
of valid newsgroup names at your site.  For example,
   Newsgroups: news.misc,comp.misc,rec.misc"
Jim Blandy's avatar
Jim Blandy committed
161 162 163 164 165 166
  (interactive)
  (expand-abbrev)
  (goto-char (point-min))
  (mail-position-on-field "Newsgroups"))

(defun news-reply-followup-to ()
Richard M. Stallman's avatar
Richard M. Stallman committed
167
  "Move point to end of `Followup-To:' field.  Create the field if none.
Jim Blandy's avatar
Jim Blandy committed
168
One usually requests followups to only one newsgroup.
Richard M. Stallman's avatar
Richard M. Stallman committed
169 170 171 172 173
RFC 850 constrains the `Followup-To:' field to be a comma-separated list
of valid newsgroups names at your site, and it must be a subset of the
`Newsgroups:' field.  For example:
   Newsgroups: news.misc,comp.misc,rec.misc,misc.misc,soc.misc
   Followup-To: news.misc,comp.misc,rec.misc"
Jim Blandy's avatar
Jim Blandy committed
174 175 176 177 178 179 180 181 182 183
  (interactive)
  (expand-abbrev)
  (or (mail-position-on-field "Followup-To" t)
      (progn (mail-position-on-field "newsgroups")
	     (insert "\nFollowup-To: ")))
	 ;; @@ could do a completing read based on the Newsgroups: field to
	 ;; @@ fill in the Followup-To: field
)

(defun news-reply-distribution ()
Richard M. Stallman's avatar
Richard M. Stallman committed
184
  "Move point to end of `Distribution:' optional field.
Jim Blandy's avatar
Jim Blandy committed
185 186 187 188 189 190 191 192 193 194
Create the field if none.  Without this field the posting goes to all of
USENET.  The field is used to restrict the posting to parts of USENET."
  (interactive)
  (expand-abbrev)
  (mail-position-on-field "Distribution")
  ;; @@could do a completing read based on the news library file:
  ;; @@    ../distributions  to fill in the field.
  )

(defun news-reply-keywords ()
Richard M. Stallman's avatar
Richard M. Stallman committed
195
  "Move point to end of `Keywords:' optional field.  Create the field if none.
Jim Blandy's avatar
Jim Blandy committed
196 197 198 199 200 201 202
Used as an aid to the news reader, it can contain a few, well selected keywords
identifying the message."
  (interactive)
  (expand-abbrev)
  (mail-position-on-field "Keywords"))

(defun news-reply-summary ()
Richard M. Stallman's avatar
Richard M. Stallman committed
203
  "Move point to end of `Summary:' optional field.  Create the field if none.
Jim Blandy's avatar
Jim Blandy committed
204 205 206 207 208 209 210
Used as an aid to the news reader, it can contain a succinct
summary (abstract) of the message."
  (interactive)
  (expand-abbrev)
  (mail-position-on-field "Summary"))

(defun news-reply-signature ()
211
  "The inews program appends `~/.signature' automatically."
Jim Blandy's avatar
Jim Blandy committed
212
  (interactive)
213
  (message "Posting news will append your signature automatically."))
Jim Blandy's avatar
Jim Blandy committed
214 215

(defun news-setup (to subject in-reply-to newsgroups replybuffer)
Richard M. Stallman's avatar
Richard M. Stallman committed
216
  "Set up the news reply or posting buffer with the proper headers and mode."
Jim Blandy's avatar
Jim Blandy committed
217
  (setq mail-reply-buffer replybuffer)
218 219 220
  (let ((mail-setup-hook nil)
	;; Avoid inserting a signature.
       	(mail-signature))
Jim Blandy's avatar
Jim Blandy committed
221 222 223 224 225 226 227
    (if (null to)
	;; this hack is needed so that inews wont be confused by 
	;; the fcc: and bcc: fields
	(let ((mail-self-blind nil)
	      (mail-archive-file-name nil))
	  (mail-setup to subject in-reply-to nil replybuffer nil)
	  (beginning-of-line)
Richard M. Stallman's avatar
Richard M. Stallman committed
228
	  (delete-region (point) (progn (forward-line 1) (point)))
Jim Blandy's avatar
Jim Blandy committed
229 230 231 232 233 234 235 236 237 238 239 240
	  (goto-char (point-max)))
      (mail-setup to subject in-reply-to nil replybuffer nil))
    ;;;(mail-position-on-field "Posting-Front-End")
    ;;;(insert (emacs-version))
    (goto-char (point-max))
    (if (let ((case-fold-search t))
	  (re-search-backward "^Subject:" (point-min) t))
	(progn (beginning-of-line)
	       (insert "Newsgroups: " (or newsgroups "") "\n")
	       (if (not newsgroups)
		   (backward-char 1)
		 (goto-char (point-max)))))
241 242 243 244 245 246 247 248 249
    (let (actual-header-separator)
      (rfc822-goto-eoh)
      (setq actual-header-separator (buffer-substring
				     (point)
				     (save-excursion (end-of-line) (point))))
      (setq paragraph-start
	    (concat "^" actual-header-separator "$\\|" paragraph-start))
      (setq paragraph-separate
	    (concat "^" actual-header-separator "$\\|" paragraph-separate)))
Jim Blandy's avatar
Jim Blandy committed
250 251 252 253 254 255 256 257 258
    (run-hooks 'news-setup-hook)))
   
(defun news-inews ()
  "Send a news message using inews."
  (interactive)
  (let* (newsgroups subject
		    (case-fold-search nil))
    (save-excursion
      (save-restriction
259
	(narrow-to-region (point-min) (mail-header-end))
Jim Blandy's avatar
Jim Blandy committed
260 261 262 263 264
	(setq newsgroups (mail-fetch-field "newsgroups")
	      subject (mail-fetch-field "subject")))
      (widen)
      (goto-char (point-min))
      (run-hooks 'news-inews-hook)
265
      (mail-sendmail-undelimit-header)
Jim Blandy's avatar
Jim Blandy committed
266 267 268 269 270
      (goto-char (point-max))
      ;; require a newline at the end for inews to append .signature to
      (or (= (preceding-char) ?\n)
	  (insert ?\n))
      (message "Posting to USENET...")
271 272 273 274 275 276 277 278 279 280
      (unwind-protect
	  (if (not (eq 0 
		       (call-process-region (point-min) (point-max) 
					    news-inews-program nil 0 nil
					    "-h"))) ; take all header lines!
					;@@ setting of subject and newsgroups still needed?
					;"-t" subject
					;"-n" newsgroups
	      (error "Posting to USENET failed")
	    (message "Posting to USENET... done"))
281
	(mail-sendmail-delimit-header)
282 283
	(set-buffer-modified-p nil)))
    (bury-buffer)))
Jim Blandy's avatar
Jim Blandy committed
284 285 286 287 288 289 290

;@@ shares some code with news-reply and news-post-news
(defun news-mail-reply ()
  "Mail a reply to the author of the current article.
While composing the reply, use \\[news-reply-yank-original] to yank the
original message into it."
  (interactive)
291
  (let (from cc subject date to reply-to message-id
Jim Blandy's avatar
Jim Blandy committed
292 293 294 295 296 297 298 299
	     (buffer (current-buffer)))
    (save-restriction
      (narrow-to-region (point-min) (progn (goto-line (point-min))
					   (search-forward "\n\n")
					   (- (point) 1)))
      (setq from (mail-fetch-field "from")
	    subject (mail-fetch-field "subject")
	    reply-to (mail-fetch-field "reply-to")
300 301
	    date (mail-fetch-field "date")
	    message-id (mail-fetch-field "message-id")))
302 303 304 305 306 307 308
    (setq to from)
    (pop-to-buffer "*mail*")
    (mail nil
	  (if reply-to reply-to to)
	  subject
	  (let ((stop-pos (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
	    (concat (if stop-pos (substring from 0 stop-pos) from)
309 310 311 312
		    "'s message "
		    (if message-id
			(concat message-id " of ")
		      "of ")
313 314 315
		    date))
	  nil
	 buffer)))
Jim Blandy's avatar
Jim Blandy committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370

;@@ the guts of news-reply and news-post-news should be combined. -tower
(defun news-reply ()
  "Compose and post a reply (aka a followup) to the current article on USENET.
While composing the followup, use \\[news-reply-yank-original] to yank the
original message into it."
  (interactive)
  (if (y-or-n-p "Are you sure you want to followup to all of USENET? ")
      (let (from cc subject date to followup-to newsgroups message-of
		 references distribution message-id
		 (buffer (current-buffer)))
	(save-restriction
	  (and (not (= 0 (buffer-size))) ;@@real problem is non-existence of
					;@@	of article file
	       (equal major-mode 'news-mode) ;@@ if rmail-mode,
					;@@	should show full headers
	       (progn
		 (news-show-all-headers) ;@@ should save/restore header state,
					;@@	but rnews.el lacks support
		 (narrow-to-region (point-min) (progn (goto-char (point-min))
						      (search-forward "\n\n")
						      (- (point) 1)))))
	  (setq from (mail-fetch-field "from")
		news-reply-yank-from from
		;; @@ not handling old Title: field
		subject (mail-fetch-field "subject")
		date (mail-fetch-field "date")
		followup-to (mail-fetch-field "followup-to")
		newsgroups (or followup-to
			       (mail-fetch-field "newsgroups"))
		references (mail-fetch-field "references")
		;; @@ not handling old Article-I.D.: field
		distribution (mail-fetch-field "distribution")
		message-id (mail-fetch-field "message-id")
		news-reply-yank-message-id message-id)
	  (pop-to-buffer "*post-news*")
	  (news-reply-mode)
	  (if (and (buffer-modified-p)
		   (not
		    (y-or-n-p "Unsent article being composed; erase it? ")))
	      ()
	    (progn
	      (erase-buffer)
	      (and subject
		   (progn (if (string-match "\\`Re: " subject)
			      (while (string-match "\\`Re: " subject)
				(setq subject (substring subject 4))))
			  (setq subject (concat "Re: " subject))))
	      (and from
		   (progn
		     (let ((stop-pos
			    (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
		       (setq message-of
			     (concat
			      (if stop-pos (substring from 0 stop-pos) from)
371 372 373 374
			      "'s message "
			      (if message-id
				  (concat message-id " of ")
				"of ")
Jim Blandy's avatar
Jim Blandy committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
			      date)))))
	      (news-setup
	       nil
	       subject
	       message-of
	       newsgroups
	       buffer)
	      (if followup-to
		  (progn (news-reply-followup-to)
			 (insert followup-to)))
	      (if distribution
		  (progn
		    (mail-position-on-field "Distribution")
		    (insert distribution)))
	      (mail-position-on-field "References")
	      (if references
		  (insert references))
	      (if (and references message-id)
		  (insert " "))
	      (if message-id
		  (insert message-id))
	      (goto-char (point-max))))))
    (message "")))

;@@ the guts of news-reply and news-post-news should be combined. -tower
Richard M. Stallman's avatar
Richard M. Stallman committed
400
;;;###autoload
401
(defun news-post-news (&optional noquery)
Jim Blandy's avatar
Jim Blandy committed
402
  "Begin editing a new USENET news article to be posted.
403 404
Type \\[describe-mode] once editing the article to get a list of commands.
If NOQUERY is non-nil, we do not query before doing the work."
Jim Blandy's avatar
Jim Blandy committed
405
  (interactive)
406 407
  (if (or noquery
	  (y-or-n-p "Are you sure you want to post to all of USENET? "))
Jim Blandy's avatar
Jim Blandy committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
      (let ((buffer (current-buffer)))
	(save-restriction
	  (and (not (= 0 (buffer-size))) ;@@real problem is non-existence of
					;@@	of article file
	       (equal major-mode 'news-mode) ;@@ if rmail-mode,
					;@@	should show full headers
	       (progn
		 (news-show-all-headers) ;@@ should save/restore header state,
					;@@	but rnews.el lacks support
		 (narrow-to-region (point-min) (progn (goto-char (point-min))
						      (search-forward "\n\n")
						      (- (point) 1)))))
	  (setq news-reply-yank-from (mail-fetch-field "from")
		;; @@ not handling old Article-I.D.: field
		news-reply-yank-message-id (mail-fetch-field "message-id")))
	(pop-to-buffer "*post-news*")
	(news-reply-mode)
	(if (and (buffer-modified-p)
		 (not (y-or-n-p "Unsent article being composed; erase it? ")))
	    ()				;@@ not saving point from last time
	  (progn (erase-buffer)
		 (news-setup () () () () buffer))))
  (message "")))

(defun news-mail-other-window ()
  "Send mail in another window.
While composing the message, use \\[news-reply-yank-original] to yank the
original message into it."
  (interactive)
  (mail-other-window nil nil nil nil nil (current-buffer)))
Eric S. Raymond's avatar
Eric S. Raymond committed
438

Richard M. Stallman's avatar
Richard M. Stallman committed
439 440
(provide 'rnewspost)

Eric S. Raymond's avatar
Eric S. Raymond committed
441
;;; rnewspost.el ends here