1;;; rnews.el --- USENET news reader for GNU Emacs
2
3;; Copyright (C) 1985, 1986, 1987, 2001, 2002, 2003, 2004,
4;;   2005, 2006, 2007 Free Software Foundation, Inc.
5
6;; Maintainer: FSF
7;; Keywords: news
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING.  If not, write to the
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
25
26;;; Change Log:
27
28;; Created Sun Mar 10,1985 at 21:35:01 ads and sundar@hernes.ai.mit.edu
29;; Should do the point pdl stuff sometime
30;; finito except pdl.... Sat Mar 16,1985 at 06:43:44
31;; lets keep the summary stuff out until we get it working ..
32;;	sundar@hermes.ai.mit.edu Wed Apr 10,1985 at 16:32:06
33;; hack slash maim. mly@gnu.org Thu 18 Apr, 1985 06:11:14
34;; modified to correct reentrance bug, to not bother with groups that
35;;   received no new traffic since last read completely, to find out
36;;   what traffic a group has available much more quickly when
37;;   possible, to do some completing reads for group names - should
38;;   be much faster...
39;;	KING@KESTREL.arpa, Thu Mar 13 09:03:28 1986
40;; made news-{next,previous}-group skip groups with no new messages; and
41;; added checking for unsubscribed groups to news-add-news-group
42;;	tower@gnu.org Jul 18 1986
43;; bound rmail-output to C-o; and changed header-field commands binding to
44;; agree with the new C-c C-f usage in sendmail
45;; 	tower@gnu.org Sep  3 1986
46;; added news-rotate-buffer-body
47;;	tower@gnu.org Oct 17 1986
48;; made messages more user friendly, cleaned up news-inews
49;; move posting and mail code to new file rnewpost.el
50;;	tower@gnu.org Oct 29 1986
51;; added caesar-region, rename news-caesar-buffer-body, hacked accordingly
52;;	tower@gnu.org Nov 21 1986
53;; added tower@gnu.org 22 Apr 87
54
55;;; Commentary:
56
57;;; Code:
58
59(require 'mail-utils)
60(require 'sendmail)
61
62(defvar caesar-translate-table)
63(defvar minor-modes)
64(defvar news-buffer-save)
65(defvar news-group-name)
66(defvar news-minor-modes)
67
68(autoload 'rmail-output "rmailout"
69  "Append this message to Unix mail file named FILE-NAME."
70  t)
71
72(autoload 'news-reply "rnewspost"
73  "Compose and post a reply to the current article on USENET.
74While composing the reply, use \\[mail-yank-original] to yank the original
75message into it."
76  t)
77
78(autoload 'news-mail-other-window "rnewspost"
79  "Send mail in another window.
80While composing the message, use \\[mail-yank-original] to yank the
81original message into it."
82  t)
83
84(autoload 'news-post-news "rnewspost"
85  "Begin editing a new USENET news article to be posted."
86  t)
87
88(autoload 'news-mail-reply "rnewspost"
89  "Mail a reply to the author of the current article.
90While composing the reply, use \\[mail-yank-original] to yank the original
91message into it."
92  t)
93
94(defvar news-group-hook-alist nil
95  "Alist of (GROUP-REGEXP . HOOK) pairs.
96Just before displaying a message, each HOOK is called
97if its GROUP-REGEXP matches the current newsgroup name.")
98
99(defvar rmail-last-file (expand-file-name "~/mbox.news"))
100
101;Now in paths.el.
102;(defvar news-path "/usr/spool/news/"
103;  "The root directory below which all news files are stored.")
104
105(defvar news-startup-file "$HOME/.newsrc" "Contains ~/.newsrc")
106(defvar news-certification-file "$HOME/.news-dates" "Contains ~/.news-dates")
107
108;; random headers that we decide to ignore.
109(defvar news-ignored-headers
110  "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:"
111  "All random fields within the header of a message.")
112
113(defvar news-mode-map nil)
114(defvar news-read-first-time-p t)
115;; Contains the (dotified) news groups of which you are a member.
116(defvar news-user-group-list nil)
117
118(defvar news-current-news-group nil)
119(defvar news-current-group-begin nil)
120(defvar news-current-group-end  nil)
121(defvar news-current-certifications nil
122   	"An assoc list of a group name and the time at which it is
123known that the group had no new traffic")
124(defvar news-current-certifiable nil
125	"The time when the directory we are now working on was written")
126
127(defvar news-message-filter nil
128  "User specifiable filter function that will be called during
129formatting of the news file")
130
131;(defvar news-mode-group-string "Starting-Up"
132;  "Mode line group name info is held in this variable")
133(defvar news-list-of-files nil
134  "Global variable in which we store the list of files
135associated with the current newsgroup")
136(defvar news-list-of-files-possibly-bogus nil
137  "variable indicating we only are guessing at which files are available.
138Not currently used.")
139
140;; association list in which we store lists of the form
141;; (pointified-group-name (first last old-last))
142(defvar news-group-article-assoc nil)
143
144(defvar news-current-message-number 0 "Displayed Article Number")
145(defvar news-total-current-group 0 "Total no of messages in group")
146
147(defvar news-unsubscribe-groups ())
148(defvar news-point-pdl () "List of visited news messages.")
149(defvar news-no-jumps-p t)
150(defvar news-buffer () "Buffer into which news files are read.")
151
152(defmacro news-push (item ref)
153  (list 'setq ref (list 'cons item ref)))
154
155(defmacro news-cadr (x) (list 'car (list 'cdr x)))
156(defmacro news-cdar (x) (list 'cdr (list 'car x)))
157(defmacro news-caddr (x) (list 'car (list 'cdr (list 'cdr x))))
158(defmacro news-cadar (x) (list 'car (list 'cdr (list 'car x))))
159(defmacro news-caadr (x) (list 'car (list 'car (list 'cdr x))))
160(defmacro news-cdadr (x) (list 'cdr (list 'car (list 'cdr x))))
161
162(defmacro news-wins (pfx index)
163  `(file-exists-p (concat ,pfx "/" (int-to-string ,index))))
164
165(defvar news-max-plausible-gap 2
166	"* In an rnews directory, the maximum possible gap size.
167A gap is a sequence of missing messages between two messages that exist.
168An empty file does not contribute to a gap -- it ends one.")
169
170(defun news-find-first-and-last (prefix base)
171  (and (news-wins prefix base)
172       (cons (news-find-first-or-last prefix base -1)
173	     (news-find-first-or-last prefix base 1))))
174
175(defmacro news-/ (a1 a2)
176;; a form of / that guarantees that (/ -1 2) = 0
177  (if (zerop (/ -1 2))
178      `(/ ,a1 ,a2)
179    `(if (< ,a1 0)
180	 (- (/ (- ,a1) ,a2))
181       (/ ,a1 ,a2))))
182
183(defun news-find-first-or-last (pfx base dirn)
184  ;; first use powers of two to find a plausible ceiling
185  (let ((original-dir dirn))
186    (while (news-wins pfx (+ base dirn))
187      (setq dirn (* dirn 2)))
188    (setq dirn (news-/ dirn 2))
189    ;; Then use a binary search to find the high water mark
190    (let ((offset (news-/ dirn 2)))
191      (while (/= offset 0)
192	(if (news-wins pfx (+ base dirn offset))
193	    (setq dirn (+ dirn offset)))
194	(setq offset (news-/ offset 2))))
195    ;; If this high-water mark is bogus, recurse.
196    (let ((offset (* news-max-plausible-gap original-dir)))
197      (while (and (/= offset 0) (not (news-wins pfx (+ base dirn offset))))
198	(setq offset (- offset original-dir)))
199      (if (= offset 0)
200	  (+ base dirn)
201	(news-find-first-or-last pfx (+ base dirn offset) original-dir)))))
202
203(defun rnews ()
204"Read USENET news for groups for which you are a member and add or
205delete groups.
206You can reply to articles posted and send articles to any group.
207
208Type \\[describe-mode] once reading news to get a list of rnews commands."
209  (interactive)
210  (let ((last-buffer (buffer-name)))
211    (make-local-variable 'rmail-last-file)
212    (switch-to-buffer (setq news-buffer (get-buffer-create "*news*")))
213    (news-mode)
214    (setq news-buffer-save last-buffer)
215    (setq buffer-read-only nil)
216    (erase-buffer)
217    (setq buffer-read-only t)
218    (set-buffer-modified-p t)
219    (sit-for 0)
220    (message "Getting new USENET news...")
221    (news-set-mode-line)
222    (news-get-certifications)
223    (news-get-new-news)))
224
225(defun news-group-certification (group)
226  (cdr-safe (assoc group news-current-certifications)))
227
228
229(defun news-set-current-certifiable ()
230  ;; Record the date that corresponds to the directory you are about to check
231  (let ((file (concat news-path
232		      (string-subst-char ?/ ?. news-current-news-group))))
233    (setq news-current-certifiable
234	  (nth 5 (file-attributes
235		  (or (file-symlink-p file) file))))))
236
237(defun news-get-certifications ()
238  ;; Read the certified-read file from last session
239  (save-excursion
240    (save-window-excursion
241      (setq news-current-certifications
242	    (car-safe
243	     (condition-case var
244		 (let*
245		     ((file (substitute-in-file-name news-certification-file))
246		      (buf (find-file-noselect file)))
247		   (and (file-exists-p file)
248			(progn
249			  (switch-to-buffer buf 'norecord)
250			  (unwind-protect
251			      (read-from-string (buffer-string))
252			    (kill-buffer buf)))))
253	       (error nil)))))))
254
255(defun news-write-certifications ()
256  ;; Write a certification file.
257  ;; This is an assoc list of group names with doubletons that represent
258  ;; mod times of the directory when group is read completely.
259  (save-excursion
260    (save-window-excursion
261      (with-output-to-temp-buffer
262	  "*CeRtIfIcAtIoNs*"
263	  (print news-current-certifications))
264      (let ((buf (get-buffer "*CeRtIfIcAtIoNs*")))
265	(switch-to-buffer buf)
266	(write-file (substitute-in-file-name news-certification-file))
267	(kill-buffer buf)))))
268
269(defun news-set-current-group-certification ()
270  (let ((cgc (assoc news-current-news-group news-current-certifications)))
271    (if cgc (setcdr cgc news-current-certifiable)
272      (news-push (cons news-current-news-group news-current-certifiable)
273		 news-current-certifications))))
274
275(defun news-set-minor-modes ()
276  "Creates a minor mode list that has group name, total articles,
277and attribute for current article."
278  (setq news-minor-modes (list (cons 'foo
279				     (concat news-current-message-number
280					     "/"
281					     news-total-current-group
282					     (news-get-attribute-string)))))
283  ;; Detect Emacs versions 18.16 and up, which display
284  ;; directly from news-minor-modes by using a list for mode-name.
285  (or (boundp 'minor-mode-alist)
286      (setq minor-modes news-minor-modes)))
287
288(defun news-set-message-counters ()
289  "Scan through current news-groups filelist to figure out how many messages
290are there. Set counters for use with minor mode display."
291    (if (null news-list-of-files)
292	(setq news-current-message-number 0)))
293
294(if news-mode-map
295    nil
296  (setq news-mode-map (make-keymap))
297  (suppress-keymap news-mode-map)
298  (define-key news-mode-map "." 'beginning-of-buffer)
299  (define-key news-mode-map " " 'scroll-up)
300  (define-key news-mode-map "\177" 'scroll-down)
301  (define-key news-mode-map "n" 'news-next-message)
302  (define-key news-mode-map "c" 'news-make-link-to-message)
303  (define-key news-mode-map "p" 'news-previous-message)
304  (define-key news-mode-map "j" 'news-goto-message)
305  (define-key news-mode-map "q" 'news-exit)
306  (define-key news-mode-map "e" 'news-exit)
307  (define-key news-mode-map "\ej" 'news-goto-news-group)
308  (define-key news-mode-map "\en" 'news-next-group)
309  (define-key news-mode-map "\ep" 'news-previous-group)
310  (define-key news-mode-map "l" 'news-list-news-groups)
311  (define-key news-mode-map "?" 'describe-mode)
312  (define-key news-mode-map "g" 'news-get-new-news)
313  (define-key news-mode-map "f" 'news-reply)
314  (define-key news-mode-map "m" 'news-mail-other-window)
315  (define-key news-mode-map "a" 'news-post-news)
316  (define-key news-mode-map "r" 'news-mail-reply)
317  (define-key news-mode-map "o" 'news-save-item-in-file)
318  (define-key news-mode-map "\C-o" 'rmail-output)
319  (define-key news-mode-map "t" 'news-show-all-headers)
320  (define-key news-mode-map "x" 'news-force-update)
321  (define-key news-mode-map "A" 'news-add-news-group)
322  (define-key news-mode-map "u" 'news-unsubscribe-current-group)
323  (define-key news-mode-map "U" 'news-unsubscribe-group)
324  (define-key news-mode-map "\C-c\C-r" 'news-caesar-buffer-body))
325
326(defun news-mode ()
327  "News Mode is used by M-x rnews for reading USENET Newsgroups articles.
328New readers can find additional help in newsgroup: news.announce.newusers .
329All normal editing commands are turned off.
330Instead, these commands are available:
331
332.	move point to front of this news article (same as Meta-<).
333Space	scroll to next screen of this news article.
334Delete  scroll down previous page of this news article.
335n	move to next news article, possibly next group.
336p	move to previous news article, possibly previous group.
337j	jump to news article specified by numeric position.
338M-j     jump to news group.
339M-n     goto next news group.
340M-p     goto previous news group.
341l       list all the news groups with current status.
342?       print this help message.
343C-c C-r caesar rotate all letters by 13 places in the article's body (rot13).
344g       get new USENET news.
345f       post a reply article to USENET.
346a       post an original news article.
347A       add a newsgroup.
348o	save the current article in the named file (append if file exists).
349C-o	output this message to a Unix-format mail file (append it).
350c       \"copy\" (actually link) current or prefix-arg msg to file.
351	warning: target directory and message file must be on same device
352		(UNIX magic)
353t       show all the headers this news article originally had.
354q	quit reading news after updating .newsrc file.
355e	exit updating .newsrc file.
356m	mail a news article.  Same as C-x 4 m.
357x       update last message seen to be the current message.
358r	mail a reply to this news article.  Like m but initializes some fields.
359u       unsubscribe from current newsgroup.
360U       unsubscribe from specified newsgroup."
361  (interactive)
362  (kill-all-local-variables)
363  (make-local-variable 'news-read-first-time-p)
364  (setq news-read-first-time-p t)
365  (make-local-variable 'news-current-news-group)
366;  (setq news-current-news-group "??")
367  (make-local-variable 'news-current-group-begin)
368  (setq news-current-group-begin 0)
369  (make-local-variable 'news-current-message-number)
370  (setq news-current-message-number 0)
371  (make-local-variable 'news-total-current-group)
372  (make-local-variable 'news-buffer-save)
373  (make-local-variable 'version-control)
374  (setq version-control 'never)
375  (make-local-variable 'news-point-pdl)
376;  This breaks it.  I don't have time to figure out why. -- RMS
377;  (make-local-variable 'news-group-article-assoc)
378  (setq major-mode 'news-mode)
379  (setq mode-line-process '(news-minor-modes))
380  (setq mode-name "NEWS")
381  (news-set-mode-line)
382  (set-syntax-table text-mode-syntax-table)
383  (use-local-map news-mode-map)
384  (setq local-abbrev-table text-mode-abbrev-table)
385  (run-mode-hooks 'news-mode-hook))
386
387(defun string-subst-char (new old string)
388  (let (index)
389    (setq old (regexp-quote (char-to-string old))
390	  string (substring string 0))
391    (while (setq index (string-match old string))
392      (aset string index new)))
393  string)
394
395;; update read message number
396(defmacro news-update-message-read (ngroup nno)
397  (list 'setcar
398	(list 'news-cdadr
399	      (list 'assoc ngroup 'news-group-article-assoc))
400	nno))
401
402(defun news-parse-range (number-string)
403  "Parse string representing range of numbers of he form <a>-<b>
404to a list (a . b)"
405  (let ((n (string-match "-" number-string)))
406    (if n
407	(cons (string-to-number (substring number-string 0 n))
408	      (string-to-number (substring number-string (1+ n))))
409      (setq n (string-to-number number-string))
410      (cons n n))))
411
412;(defun is-in (elt lis)
413;  (catch 'foo
414;    (while lis
415;      (if (equal (car lis) elt)
416;	  (throw 'foo t)
417;	(setq lis (cdr lis))))))
418
419(defun news-get-new-news ()
420  "Get new USENET news, if there is any for the current user."
421  (interactive)
422  (if (not (null news-user-group-list))
423      (news-update-newsrc-file))
424  (setq news-group-article-assoc ())
425  (setq news-user-group-list ())
426  (message "Looking up %s file..." news-startup-file)
427  (let ((file (substitute-in-file-name news-startup-file))
428	(temp-user-groups ()))
429    (save-excursion
430      (let ((newsrcbuf (find-file-noselect file))
431	    start end endofline tem)
432	(set-buffer newsrcbuf)
433	(goto-char 0)
434	(while (search-forward ": " nil t)
435	  (setq end (point))
436	  (beginning-of-line)
437	  (setq start (point))
438	  (end-of-line)
439	  (setq endofline (point))
440	  (setq tem (buffer-substring start (- end 2)))
441	  (let ((range (news-parse-range
442			(buffer-substring end endofline))))
443	    (if (assoc tem news-group-article-assoc)
444		(message "You are subscribed twice to %s; I ignore second"
445			 tem)
446	      (setq temp-user-groups (cons tem temp-user-groups)
447		    news-group-article-assoc
448		    (cons (list tem (list (car range)
449					  (cdr range)
450					  (cdr range)))
451			  news-group-article-assoc)))))
452	(kill-buffer newsrcbuf)))
453    (setq temp-user-groups (nreverse temp-user-groups))
454    (message "Prefrobnicating...")
455    (switch-to-buffer news-buffer)
456    (setq news-user-group-list temp-user-groups)
457    (while (and temp-user-groups
458		(not (news-read-files-into-buffer
459		      (car temp-user-groups) nil)))
460      (setq temp-user-groups (cdr temp-user-groups)))
461    (if (null temp-user-groups)
462	(message "No news is good news.")
463      (message ""))))
464
465(defun news-list-news-groups ()
466  "Display all the news groups to which you belong."
467  (interactive)
468  (with-output-to-temp-buffer "*Newsgroups*"
469    (save-excursion
470      (set-buffer standard-output)
471      (insert
472	"News Group        Msg No.       News Group        Msg No.\n")
473      (insert
474	"-------------------------       -------------------------\n")
475      (let ((temp news-user-group-list)
476	    (flag nil))
477	(while temp
478	  (let ((item (assoc (car temp) news-group-article-assoc)))
479	    (insert (car item))
480	    (indent-to (if flag 52 20))
481	    (insert (int-to-string (news-cadr (news-cadr item))))
482	    (if flag
483		(insert "\n")
484	      (indent-to 33))
485	    (setq temp (cdr temp) flag (not flag))))))))
486
487;; Mode line hack
488(defun news-set-mode-line ()
489  "Set mode line string to something useful."
490  (setq mode-line-process
491	(concat " "
492		(if (integerp news-current-message-number)
493		    (int-to-string news-current-message-number)
494		 "??")
495		"/"
496		(if (integerp news-current-group-end)
497		    (int-to-string news-current-group-end)
498		  news-current-group-end)))
499  (setq mode-line-buffer-identification
500	(concat "NEWS: "
501		news-current-news-group
502		;; Enough spaces to pad group name to 17 positions.
503		(substring "                 "
504			   0 (max 0 (- 17 (length news-current-news-group))))))
505  (set-buffer-modified-p t)
506  (sit-for 0))
507
508(defun news-goto-news-group (gp)
509  "Takes a string and goes to that news group."
510  (interactive (list (completing-read "NewsGroup: "
511				      news-group-article-assoc)))
512  (message "Jumping to news group %s..." gp)
513  (news-select-news-group gp)
514  (message "Jumping to news group %s... done." gp))
515
516(defun news-select-news-group (gp)
517  (let ((grp (assoc gp news-group-article-assoc)))
518    (if (null grp)
519 	(error "Group %s not subscribed to" gp)
520      (progn
521	(news-update-message-read news-current-news-group
522				  (news-cdar news-point-pdl))
523	(news-read-files-into-buffer  (car grp) nil)
524	(news-set-mode-line)))))
525
526(defun news-goto-message (arg)
527  "Goes to the article ARG in current newsgroup."
528  (interactive "p")
529  (if (null current-prefix-arg)
530      (setq arg (read-no-blanks-input "Go to article: " "")))
531  (news-select-message arg))
532
533(defun news-select-message (arg)
534  (if (stringp arg) (setq arg (string-to-number arg)))
535  (let ((file (concat news-path
536		      (string-subst-char ?/ ?. news-current-news-group)
537		      "/" arg)))
538    (if (= arg
539	   (or (news-cadr (memq (news-cdar news-point-pdl) news-list-of-files))
540	       0))
541	(setcdr (car news-point-pdl) arg))
542    (setq news-current-message-number arg)
543    (if (file-exists-p file)
544  	(let ((buffer-read-only nil))
545  	  (news-read-in-file file)
546  	  (news-set-mode-line))
547      (news-set-mode-line)
548      (error "Article %d nonexistent" arg))))
549
550(defun news-force-update ()
551  "updates the position of last article read in the current news group"
552  (interactive)
553  (setcdr (car news-point-pdl) news-current-message-number)
554  (message "Updated to %d" news-current-message-number))
555
556(defun news-next-message (arg)
557  "Move ARG messages forward within one newsgroup.
558Negative ARG moves backward.
559If ARG is 1 or -1, moves to next or previous newsgroup if at end."
560  (interactive "p")
561  (let ((no (+ arg news-current-message-number)))
562    (if (or (< no news-current-group-begin)
563	    (> no news-current-group-end))
564	(cond ((= arg 1)
565	       (news-set-current-group-certification)
566	       (news-next-group))
567	      ((= arg -1)
568	       (news-previous-group))
569	      (t (error "Article out of range")))
570      (let ((plist (news-get-motion-lists
571		     news-current-message-number
572		     news-list-of-files)))
573	(if (< arg 0)
574	    (news-select-message (nth (1- (- arg)) (car (cdr plist))))
575	  (news-select-message (nth (1- arg) (car plist))))))))
576
577(defun news-previous-message (arg)
578  "Move ARG messages backward in current newsgroup.
579With no arg or arg of 1, move one message
580and move to previous newsgroup if at beginning.
581A negative ARG means move forward."
582  (interactive "p")
583  (news-next-message (- arg)))
584
585(defun news-move-to-group (arg)
586  "Given arg move forward or backward to a new newsgroup."
587  (let ((cg news-current-news-group))
588    (let ((plist (news-get-motion-lists cg news-user-group-list))
589	  ngrp)
590      (if (< arg 0)
591	  (or (setq ngrp (nth (1- (- arg)) (news-cadr plist)))
592	      (error "No previous news groups"))
593	(or (setq ngrp (nth arg (car plist)))
594	    (error "No more news groups")))
595      (news-select-news-group ngrp))))
596
597(defun news-next-group ()
598  "Moves to the next user group."
599  (interactive)
600;  (message "Moving to next group...")
601  (news-move-to-group 0)
602  (while (null news-list-of-files)
603    (news-move-to-group 0)))
604;  (message "Moving to next group... done.")
605
606(defun news-previous-group ()
607  "Moves to the previous user group."
608  (interactive)
609;  (message "Moving to previous group...")
610  (news-move-to-group -1)
611  (while (null news-list-of-files)
612    (news-move-to-group -1)))
613;  (message "Moving to previous group... done.")
614
615(defun news-get-motion-lists (arg listy)
616  "Given a msgnumber/group this will return a list of two lists;
617one for moving forward and one for moving backward."
618  (let ((temp listy)
619	(result ()))
620    (catch 'out
621      (while temp
622	(if (equal (car temp) arg)
623	    (throw 'out (cons (cdr temp) (list result)))
624	  (setq result (nconc (list (car temp)) result))
625	  (setq temp (cdr temp)))))))
626
627;; miscellaneous io routines
628(defun news-read-in-file (filename)
629  (erase-buffer)
630  (let ((start (point)))
631  (insert-file-contents filename)
632  (news-convert-format)
633  ;; Run each hook that applies to the current newsgroup.
634  (let ((hooks news-group-hook-alist))
635    (while hooks
636      (goto-char start)
637      (if (string-match (car (car hooks)) news-group-name)
638	  (funcall (cdr (car hooks))))
639      (setq hooks (cdr hooks))))
640  (goto-char start)
641  (forward-line 1)
642  (if (eobp)
643      (message "(Empty file?)")
644    (goto-char start))))
645
646(defun news-convert-format ()
647  (save-excursion
648    (save-restriction
649      (let* ((start (point))
650	     (end (condition-case ()
651		      (progn (search-forward "\n\n") (point))
652		    (error nil)))
653	     has-from has-date)
654       (cond (end
655	      (narrow-to-region start end)
656	      (goto-char start)
657	      (setq has-from (search-forward "\nFrom:" nil t))
658	      (cond ((and (not has-from) has-date)
659		     (goto-char start)
660		     (search-forward "\nDate:")
661		     (beginning-of-line)
662		     (kill-line) (kill-line)))
663	      (news-delete-headers start)
664	      (goto-char start)))))))
665
666(defun news-show-all-headers ()
667  "Redisplay current news item with all original headers"
668  (interactive)
669  (let (news-ignored-headers
670	(buffer-read-only ()))
671    (erase-buffer)
672    (news-set-mode-line)
673    (news-read-in-file
674     (concat news-path
675	     (string-subst-char ?/ ?. news-current-news-group)
676	     "/" (int-to-string news-current-message-number)))))
677
678(defun news-delete-headers (pos)
679  (goto-char pos)
680  (and (stringp news-ignored-headers)
681       (while (re-search-forward news-ignored-headers nil t)
682	 (beginning-of-line)
683	 (delete-region (point)
684			(progn (re-search-forward "\n[^ \t]")
685			       (forward-char -1)
686			       (point))))))
687
688(defun news-exit ()
689  "Quit news reading session and update the .newsrc file."
690  (interactive)
691  (if (y-or-n-p "Do you really wanna quit reading news ? ")
692      (progn (message "Updating %s..." news-startup-file)
693	     (news-update-newsrc-file)
694	     (news-write-certifications)
695	     (message "Updating %s... done" news-startup-file)
696	     (message "Now do some real work")
697	     (quit-window)
698	     (switch-to-buffer news-buffer-save)
699	     (setq news-user-group-list ()))
700    (message "")))
701
702(defun news-update-newsrc-file ()
703  "Updates the .newsrc file in the users home dir."
704  (let ((newsrcbuf (find-file-noselect
705		     (substitute-in-file-name news-startup-file)))
706	(tem news-user-group-list)
707	group)
708    (save-excursion
709      (if (not (null news-current-news-group))
710	  (news-update-message-read news-current-news-group
711				    (news-cdar news-point-pdl)))
712      (set-buffer newsrcbuf)
713      (while tem
714	(setq group (assoc (car tem) news-group-article-assoc))
715	(if (= (news-cadr (news-cadr group)) (news-caddr (news-cadr group)))
716	    nil
717	  (goto-char 0)
718	  (if (search-forward (concat (car group) ": ") nil t)
719	      (kill-line nil)
720	    (insert (car group) ": \n") (backward-char 1))
721	  (insert (int-to-string (car (news-cadr group))) "-"
722		  (int-to-string (news-cadr (news-cadr group)))))
723	(setq tem (cdr tem)))
724     (while news-unsubscribe-groups
725       (setq group (assoc (car news-unsubscribe-groups)
726			  news-group-article-assoc))
727       (goto-char 0)
728       (if (search-forward (concat (car group) ": ") nil t)
729	   (progn
730	      (backward-char 2)
731	      (kill-line nil)
732	      (insert "! " (int-to-string (car (news-cadr group)))
733		      "-" (int-to-string (news-cadr (news-cadr group))))))
734       (setq news-unsubscribe-groups (cdr news-unsubscribe-groups)))
735     (save-buffer)
736     (kill-buffer (current-buffer)))))
737
738
739(defun news-unsubscribe-group (group)
740  "Removes you from newgroup GROUP."
741  (interactive (list (completing-read  "Unsubscribe from group: "
742				      news-group-article-assoc)))
743  (news-unsubscribe-internal group))
744
745(defun news-unsubscribe-current-group ()
746  "Removes you from the newsgroup you are now reading."
747  (interactive)
748  (if (y-or-n-p "Do you really want to unsubscribe from this group ? ")
749      (news-unsubscribe-internal news-current-news-group)))
750
751(defun news-unsubscribe-internal (group)
752  (let ((tem (assoc group news-group-article-assoc)))
753    (if tem
754	(progn
755	  (setq news-unsubscribe-groups (cons group news-unsubscribe-groups))
756	  (news-update-message-read group (news-cdar news-point-pdl))
757	  (if (equal group news-current-news-group)
758	      (news-next-group))
759	  (message ""))
760      (error "Not subscribed to group: %s" group))))
761
762(defun news-save-item-in-file (file)
763  "Save the current article that is being read by appending to a file."
764  (interactive "FSave item in file: ")
765  (append-to-file (point-min) (point-max) file))
766
767(defun news-get-pruned-list-of-files (gp-list end-file-no)
768  "Given a news group it finds all files in the news group.
769The arg must be in slashified format.
770Using ls was found to be too slow in a previous version."
771  (let
772      ((answer
773	(and
774	 (not (and end-file-no
775		   (equal (news-set-current-certifiable)
776		     (news-group-certification gp-list))
777		   (setq news-list-of-files nil
778			 news-list-of-files-possibly-bogus t)))
779	 (let* ((file-directory (concat news-path
780					(string-subst-char ?/ ?. gp-list)))
781		tem
782		(last-winner
783		 (and end-file-no
784		      (news-wins file-directory end-file-no)
785		      (news-find-first-or-last file-directory end-file-no 1))))
786	   (setq news-list-of-files-possibly-bogus t news-list-of-files nil)
787	   (if last-winner
788	       (progn
789		 (setq news-list-of-files-possibly-bogus t
790		       news-current-group-end last-winner)
791		 (while (> last-winner end-file-no)
792		   (news-push last-winner news-list-of-files)
793		   (setq last-winner (1- last-winner)))
794		 news-list-of-files)
795	     (if (or (not (file-directory-p file-directory))
796		     (not (file-readable-p file-directory)))
797		 nil
798	       (setq news-list-of-files
799		     (condition-case error
800			 (directory-files file-directory)
801		       (file-error
802			(if (string= (nth 2 error) "permission denied")
803			    (message "Newsgroup %s is read-protected"
804				     gp-list)
805			  (signal 'file-error (cdr error)))
806			nil)))
807	       (setq tem news-list-of-files)
808	       (while tem
809		 (if (or (not (string-match "^[0-9]*$" (car tem)))
810			 ;; don't get confused by directories that look like numbers
811			 (file-directory-p
812			  (concat file-directory "/" (car tem)))
813			 (<= (string-to-number (car tem)) end-file-no))
814		     (setq news-list-of-files
815			   (delq (car tem) news-list-of-files)))
816		 (setq tem (cdr tem)))
817	       (if (null news-list-of-files)
818		   (progn (setq news-current-group-end 0)
819			  nil)
820		 (setq news-list-of-files
821		       (mapcar 'string-to-number news-list-of-files))
822		 (setq news-list-of-files (sort news-list-of-files '<))
823		 (setq news-current-group-end
824		       (elt news-list-of-files
825			    (1- (length news-list-of-files))))
826		 news-list-of-files)))))))
827    (or answer (progn (news-set-current-group-certification) nil))))
828
829(defun news-read-files-into-buffer (group reversep)
830  (let* ((files-start-end (news-cadr (assoc group news-group-article-assoc)))
831	 (start-file-no (car files-start-end))
832	 (end-file-no (news-cadr files-start-end))
833	 (buffer-read-only nil))
834    (setq news-current-news-group group)
835    (setq news-current-message-number nil)
836    (setq news-current-group-end nil)
837    (news-set-mode-line)
838    (news-get-pruned-list-of-files group end-file-no)
839    (news-set-mode-line)
840    ;; @@ should be a lot smarter than this if we have to move
841    ;; @@ around correctly.
842    (setq news-point-pdl (list (cons (car files-start-end)
843				     (news-cadr files-start-end))))
844    (if (null news-list-of-files)
845	(progn (erase-buffer)
846	       (setq news-current-group-end end-file-no)
847	       (setq news-current-group-begin end-file-no)
848	       (setq news-current-message-number end-file-no)
849	       (news-set-mode-line)
850;	       (message "No new articles in " group " group.")
851	       nil)
852      (setq news-current-group-begin (car news-list-of-files))
853      (if reversep
854	  (setq news-current-message-number news-current-group-end)
855	(if (> (car news-list-of-files) end-file-no)
856	    (setcdr (car news-point-pdl) (car news-list-of-files)))
857	(setq news-current-message-number news-current-group-begin))
858      (news-set-message-counters)
859      (news-set-mode-line)
860      (news-read-in-file (concat news-path
861				 (string-subst-char ?/ ?. group)
862				 "/"
863				 (int-to-string
864				   news-current-message-number)))
865      (news-set-message-counters)
866      (news-set-mode-line)
867      t)))
868
869(defun news-add-news-group (gp)
870  "Resubscribe to or add a USENET news group named GROUP (a string)."
871; @@ (completing-read ...)
872; @@ could be based on news library file ../active (slightly fascist)
873; @@ or (expensive to compute) all directories under the news spool directory
874  (interactive "sAdd news group: ")
875  (let ((file-dir (concat news-path (string-subst-char ?/ ?. gp))))
876    (save-excursion
877      (if (null (assoc gp news-group-article-assoc))
878	  (let ((newsrcbuf (find-file-noselect
879			    (substitute-in-file-name news-startup-file))))
880	    (if (file-directory-p file-dir)
881		(progn
882		  (switch-to-buffer newsrcbuf)
883		  (goto-char 0)
884		  (if (search-forward (concat gp "! ") nil t)
885		      (progn
886			(message "Re-subscribing to group %s." gp)
887			;;@@ news-unsubscribe-groups isn't being used
888			;;(setq news-unsubscribe-groups
889			;;    (delq gp news-unsubscribe-groups))
890			(backward-char 2)
891			(delete-char 1)
892			(insert ":"))
893		    (progn
894		      (message
895		       "Added %s to your list of newsgroups." gp)
896		      (goto-char (point-max))
897		      (insert gp ": 1-1\n")))
898		  (search-backward gp nil t)
899		  (let (start end endofline tem)
900		    (search-forward ": " nil t)
901		    (setq end (point))
902		    (beginning-of-line)
903		    (setq start (point))
904		    (end-of-line)
905		    (setq endofline (point))
906		    (setq tem (buffer-substring start (- end 2)))
907		    (let ((range (news-parse-range
908				  (buffer-substring end endofline))))
909		      (setq news-group-article-assoc
910			    (cons (list tem (list (car range)
911						  (cdr range)
912						  (cdr range)))
913				  news-group-article-assoc))))
914		  (save-buffer)
915		  (kill-buffer (current-buffer)))
916	      (message "Newsgroup %s doesn't exist." gp)))
917	(message "Already subscribed to group %s." gp)))))
918
919(defun news-make-link-to-message (number newname)
920	"Forges a link to an rnews message numbered number (current if no arg)
921Good for hanging on to a message that might or might not be
922automatically deleted."
923  (interactive "P
924FName to link to message: ")
925  (add-name-to-file
926   (concat news-path
927	   (string-subst-char ?/ ?. news-current-news-group)
928	   "/" (if number
929		   (prefix-numeric-value number)
930		 news-current-message-number))
931   newname))
932
933;;; caesar-region written by phr@gnu.org  Nov 86
934;;; modified by tower@gnu.org Nov 86
935(defun caesar-region (&optional n)
936  "Caesar rotation of region by N, default 13, for decrypting netnews."
937  (interactive (if current-prefix-arg	; Was there a prefix arg?
938		   (list (prefix-numeric-value current-prefix-arg))
939		 (list nil)))
940  (cond ((not (numberp n)) (setq n 13))
941	(t (setq n (mod n 26))))	;canonicalize N
942  (if (not (zerop n))		; no action needed for a rot of 0
943      (progn
944	(if (or (not (boundp 'caesar-translate-table))
945		(/= (aref caesar-translate-table ?a) (+ ?a n)))
946	    (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
947	      (message "Building caesar-translate-table...")
948	      (setq caesar-translate-table (make-vector 256 0))
949	      (while (< i 256)
950		(aset caesar-translate-table i i)
951		(setq i (1+ i)))
952	      (setq lower (concat lower lower) upper (upcase lower) i 0)
953	      (while (< i 26)
954		(aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
955		(aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
956		(setq i (1+ i)))
957	      (message "Building caesar-translate-table... done")))
958	(let ((from (region-beginning))
959	      (to (region-end))
960	      (i 0) str len)
961	  (setq str (buffer-substring from to))
962	  (setq len (length str))
963	  (while (< i len)
964	    (aset str i (aref caesar-translate-table (aref str i)))
965	    (setq i (1+ i)))
966	  (goto-char from)
967	  (kill-region from to)
968	  (insert str)))))
969
970;;; news-caesar-buffer-body written by paul@media-lab.mit.edu  Wed Oct 1, 1986
971;;; hacked further by tower@gnu.org
972(defun news-caesar-buffer-body (&optional rotnum)
973  "Caesar rotates all letters in the current buffer by 13 places.
974Used to encode/decode possibly offensive messages (commonly in net.jokes).
975With prefix arg, specifies the number of places to rotate each letter forward.
976Mail and USENET news headers are not rotated."
977  (interactive (if current-prefix-arg	; Was there a prefix arg?
978		   (list (prefix-numeric-value current-prefix-arg))
979		 (list nil)))
980  (save-excursion
981    (let ((buffer-status buffer-read-only))
982      (setq buffer-read-only nil)
983      ;; setup the region
984      (set-mark (if (equal major-mode 'news-mode)
985		    (progn (goto-char (point-min))
986			   (search-forward "\n\n" nil t))
987		  (mail-text-start)))
988      (goto-char (point-max))
989      (caesar-region rotnum)
990      (setq buffer-read-only buffer-status))))
991
992(provide 'rnews)
993
994;;; arch-tag: c032a20b-cafb-466c-b3fa-5be404a18f8c
995;;; rnews.el ends here
996