14
|
1 ;;; url-news.el --- News Uniform Resource Locator retrieval code
|
|
2 ;; Author: wmperry
|
173
|
3 ;; Created: 1997/07/05 22:54:24
|
|
4 ;; Version: 1.8
|
14
|
5 ;; Keywords: comm, data, processes
|
|
6
|
|
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
8 ;;; Copyright (c) 1993-1996 by William M. Perry (wmperry@cs.indiana.edu)
|
16
|
9 ;;; Copyright (c) 1996, 1997 Free Software Foundation, Inc.
|
14
|
10 ;;;
|
|
11 ;;; This file is not part of GNU Emacs, but the same permissions apply.
|
|
12 ;;;
|
|
13 ;;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
14 ;;; it under the terms of the GNU General Public License as published by
|
|
15 ;;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;;; any later version.
|
|
17 ;;;
|
|
18 ;;; GNU Emacs is distributed in the hope that it will be useful,
|
|
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;;; GNU General Public License for more details.
|
|
22 ;;;
|
|
23 ;;; You should have received a copy of the GNU General Public License
|
|
24 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
25 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
26 ;;; Boston, MA 02111-1307, USA.
|
|
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
28 (require 'url-vars)
|
|
29 (require 'url-parse)
|
|
30
|
173
|
31 (defgroup url-news nil
|
|
32 "News related options"
|
|
33 :group 'url)
|
|
34
|
|
35 (defcustom url-news-use-article-mode nil
|
|
36 "*Whether to use Gnus' article mode for displaying news articles."
|
|
37 :type 'boolean
|
|
38 :group 'url-news)
|
|
39
|
14
|
40 (defun url-format-news ()
|
|
41 (url-clear-tmp-buffer)
|
|
42 (insert "HTTP/1.0 200 Retrieval OK\r\n"
|
|
43 (save-excursion
|
|
44 (set-buffer nntp-server-buffer)
|
|
45 (buffer-string)))
|
|
46 (url-parse-mime-headers)
|
|
47 (let* ((from (cdr (assoc "from" url-current-mime-headers)))
|
|
48 (qfrom (if from (url-insert-entities-in-string from) nil))
|
|
49 (subj (cdr (assoc "subject" url-current-mime-headers)))
|
|
50 (qsubj (if subj (url-insert-entities-in-string subj) nil))
|
|
51 (org (cdr (assoc "organization" url-current-mime-headers)))
|
|
52 (qorg (if org (url-insert-entities-in-string org) nil))
|
|
53 (typ (or (cdr (assoc "content-type" url-current-mime-headers))
|
|
54 "text/plain"))
|
173
|
55 (inhibit-read-only t)
|
14
|
56 (qgrps (mapcar 'car
|
|
57 (url-split
|
|
58 (url-insert-entities-in-string
|
|
59 (or (cdr (assoc "newsgroups"
|
|
60 url-current-mime-headers))
|
|
61 ""))
|
|
62 "[ \t\n,]+")))
|
|
63 (qrefs (delete ""
|
|
64 (mapcar
|
|
65 'url-insert-entities-in-string
|
|
66 (mapcar 'car
|
|
67 (url-split
|
|
68 (or (cdr (assoc "references"
|
|
69 url-current-mime-headers))
|
|
70 "")
|
|
71 "[ \t,\n<>]+")))))
|
|
72 (date (cdr (assoc "date" url-current-mime-headers))))
|
|
73 (if (or (not (string-match "text/" typ))
|
|
74 (string-match "text/html" typ))
|
|
75 nil ; Let natural content-type take over
|
173
|
76 (if (and (fboundp 'gnus-article-mode)
|
|
77 url-news-use-article-mode)
|
|
78 (progn
|
|
79 (kill-buffer (current-buffer))
|
|
80 (set-buffer (get-buffer-create "Emacs/W3 News"))
|
|
81 (erase-buffer)
|
|
82 (insert
|
|
83 (save-excursion
|
|
84 (set-buffer nntp-server-buffer)
|
|
85 (save-restriction
|
|
86 (widen)
|
|
87 (buffer-string))))
|
|
88 (gnus-article-mode)
|
|
89 (article-hide-headers 1)
|
|
90 (goto-char (point-min))
|
|
91 (display-buffer (current-buffer)))
|
|
92 (insert "<html>\n"
|
|
93 " <head>\n"
|
|
94 " <title>" qsubj "</title>\n"
|
|
95 " <link rev=\"made\" href=\"mailto:" qfrom "\">\n"
|
|
96 " </head>\n"
|
|
97 " <body>\n"
|
|
98 " <div>\n"
|
|
99 " <h1 align=center>" qsubj "</h1>\n"
|
|
100 " <p role=\"headers\">\n"
|
|
101 " <b>From</b>: " qfrom "<br>\n"
|
|
102 " <b>Newsgroups</b>: "
|
|
103 (mapconcat
|
|
104 (function
|
|
105 (lambda (grp)
|
|
106 (concat "<a href=\"" grp "\">" grp "</a>"))) qgrps ", ")
|
|
107 "<br>\n"
|
|
108 (if org
|
|
109 (concat
|
|
110 " <b>Organization</b>: <i> " qorg "</i> <br>\n")
|
|
111 "")
|
|
112 " <b>Date</b>: <date> " date "</date> <br>\n"
|
|
113 " </p> <hr>\n"
|
|
114 (if (null qrefs)
|
|
115 ""
|
|
116 (concat
|
|
117 " <p>References\n"
|
|
118 " <ol>\n"
|
|
119 (mapconcat
|
|
120 (function
|
|
121 (lambda (ref)
|
|
122 (concat " <li> <a href=\"" ref "\"> "
|
|
123 ref "</a></li>\n")))
|
|
124 qrefs "")
|
|
125 " </ol>\n"
|
|
126 " </p>\n"
|
|
127 " <hr>\n"))
|
|
128 " <ul plain>\n"
|
|
129 " <li><a href=\"newspost:disfunctional\"> "
|
|
130 "Post to this group </a></li>\n"
|
|
131 " <li><a href=\"mailto:" qfrom "\"> Reply to " qfrom
|
|
132 "</a></li>\n"
|
|
133 " </ul>\n"
|
|
134 " <hr>"
|
|
135 " <pre>\n")
|
|
136 (let ((s (buffer-substring (point) (point-max))))
|
|
137 (delete-region (point) (point-max))
|
|
138 (insert (url-insert-entities-in-string s)))
|
|
139 (goto-char (point-max))
|
|
140 (setq url-current-mime-type "text/html"
|
|
141 url-current-mime-viewer (mm-mime-info url-current-mime-type nil 5))
|
|
142 (let ((x (assoc "content-type" url-current-mime-headers)))
|
|
143 (if x
|
|
144 (setcdr x "text/html")
|
|
145 (setq url-current-mime-headers (cons (cons "content-type"
|
|
146 "text/html")
|
|
147 url-current-mime-headers))))
|
|
148 (insert "\n"
|
|
149 " </pre>\n"
|
|
150 " </div>\n"
|
|
151 " </body>\n"
|
|
152 "</html>\n"
|
|
153 "<!-- Automatically generated by URL/" url-version
|
|
154 "-->")))))
|
14
|
155
|
|
156 (defun url-check-gnus-version ()
|
|
157 (require 'nntp)
|
|
158 (condition-case ()
|
|
159 (require 'gnus)
|
|
160 (error (setq gnus-version "GNUS not found")))
|
|
161 (if (or (not (boundp 'gnus-version))
|
|
162 (string-match "v5.[.0-9]+$" gnus-version)
|
|
163 (string-match "Red" gnus-version))
|
|
164 nil
|
|
165 (url-warn 'url (concat
|
|
166 "The version of GNUS found on this system is too old and does\n"
|
|
167 "not support the necessary functionality for the URL package.\n"
|
|
168 "Please upgrade to version 5.x of GNUS. This is bundled by\n"
|
|
169 "default with Emacs 19.30 and XEmacs 19.14 and later.\n\n"
|
|
170 "This version of GNUS is: " gnus-version "\n"))
|
|
171 (fset 'url-news 'url-news-version-too-old))
|
|
172 (fset 'url-check-gnus-version 'ignore))
|
|
173
|
|
174 (defun url-news-version-too-old (article)
|
|
175 (set-buffer (get-buffer-create url-working-buffer))
|
|
176 (setq url-current-mime-headers '(("content-type" . "text/html"))
|
|
177 url-current-mime-type "text/html")
|
|
178 (insert "<html>\n"
|
|
179 " <head>\n"
|
|
180 " <title>News Error</title>\n"
|
|
181 " </head>\n"
|
|
182 " <body>\n"
|
|
183 " <h1>News Error - too old</h1>\n"
|
|
184 " <p>\n"
|
|
185 " The version of GNUS found on this system is too old and does\n"
|
|
186 " not support the necessary functionality for the URL package.\n"
|
|
187 " Please upgrade to version 5.x of GNUS. This is bundled by\n"
|
|
188 " default with Emacs 19.30 and XEmacs 19.14 and later.\n\n"
|
|
189 " This version of GNUS is: " gnus-version "\n"
|
|
190 " </p>\n"
|
|
191 " </body>\n"
|
|
192 "</html>\n"))
|
|
193
|
|
194 (defun url-news-open-host (host port user pass)
|
|
195 (if (fboundp 'nnheader-init-server-buffer)
|
|
196 (nnheader-init-server-buffer))
|
|
197 (nntp-open-server host (list (string-to-int port)))
|
|
198 (if (and user pass)
|
|
199 (progn
|
|
200 (nntp-send-command "^.*\r?\n" "AUTHINFO USER" user)
|
|
201 (nntp-send-command "^.*\r?\n" "AUTHINFO PASS" pass)
|
|
202 (if (not (nntp-server-opened host))
|
|
203 (url-warn 'url (format "NNTP authentication to `%s' as `%s' failed"
|
|
204 host user))))))
|
|
205
|
|
206 (defun url-news-fetch-article-number (newsgroup article)
|
|
207 (nntp-request-group newsgroup)
|
|
208 (nntp-request-article article))
|
|
209
|
|
210 (defun url-news-fetch-message-id (host port message-id)
|
|
211 (if (eq ?> (aref message-id (1- (length message-id))))
|
|
212 nil
|
|
213 (setq message-id (concat "<" message-id ">")))
|
|
214 (if (nntp-request-article message-id)
|
|
215 (url-format-news)
|
|
216 (set-buffer (get-buffer-create url-working-buffer))
|
|
217 (setq url-current-can-be-cached nil)
|
|
218 (insert "<html>\n"
|
|
219 " <head>\n"
|
|
220 " <title>Error</title>\n"
|
|
221 " </head>\n"
|
|
222 " <body>\n"
|
|
223 " <div>\n"
|
|
224 " <h1>Error requesting article...</h1>\n"
|
|
225 " <p>\n"
|
|
226 " The status message returned by the NNTP server was:"
|
|
227 "<br><hr>\n"
|
|
228 " <xmp>\n"
|
|
229 (nntp-status-message)
|
|
230 " </xmp>\n"
|
|
231 " </p>\n"
|
|
232 " <p>\n"
|
|
233 " If you If you feel this is an error, <a href=\""
|
|
234 "mailto:" url-bug-address "\">send me mail</a>\n"
|
|
235 " </p>\n"
|
|
236 " </div>\n"
|
|
237 " </body>\n"
|
|
238 "</html>\n"
|
|
239 "<!-- Automatically generated by URL v" url-version " -->\n"
|
|
240 )))
|
|
241
|
|
242 (defun url-news-fetch-newsgroup (newsgroup host)
|
|
243 (if (string-match "^/+" newsgroup)
|
|
244 (setq newsgroup (substring newsgroup (match-end 0))))
|
|
245 (if (string-match "/+$" newsgroup)
|
|
246 (setq newsgroup (substring newsgroup 0 (match-beginning 0))))
|
|
247
|
|
248 ;; This saves a bogus 'Untitled' buffer by Emacs-W3
|
|
249 (kill-buffer url-working-buffer)
|
|
250
|
|
251 ;; This saves us from checking new news if GNUS is already running
|
|
252 (if (or (not (get-buffer gnus-group-buffer))
|
|
253 (save-excursion
|
|
254 (set-buffer gnus-group-buffer)
|
|
255 (not (eq major-mode 'gnus-group-mode))))
|
|
256 (gnus))
|
|
257 (set-buffer gnus-group-buffer)
|
|
258 (goto-char (point-min))
|
|
259 (gnus-group-read-ephemeral-group newsgroup (list 'nntp host)
|
|
260 nil
|
|
261 (cons (current-buffer) 'browse)))
|
|
262
|
|
263 (defun url-news (article)
|
|
264 ;; Find a news reference
|
|
265 (url-check-gnus-version)
|
|
266 (let* ((urlobj (url-generic-parse-url article))
|
|
267 (host (or (url-host urlobj) url-news-server))
|
|
268 (port (or (url-port urlobj)
|
|
269 (cdr-safe (assoc "news" url-default-ports))))
|
|
270 (article-brackets nil)
|
|
271 (article (url-filename urlobj)))
|
|
272 (url-news-open-host host port (url-user urlobj) (url-password urlobj))
|
|
273 (cond
|
|
274 ((string-match "@" article) ; Its a specific article
|
|
275 (url-news-fetch-message-id host port article))
|
|
276 ((string= article "") ; List all newsgroups
|
|
277 (gnus)
|
|
278 (kill-buffer url-working-buffer))
|
|
279 (t ; Whole newsgroup
|
26
|
280 (url-news-fetch-newsgroup article host)))))
|
14
|
281
|
|
282 (defun url-nntp (url)
|
|
283 ;; Find a news reference
|
|
284 (url-check-gnus-version)
|
|
285 (let* ((urlobj (url-generic-parse-url url))
|
|
286 (host (or (url-host urlobj) url-news-server))
|
|
287 (port (or (url-port urlobj)
|
|
288 (cdr-safe (assoc "nntp" url-default-ports))))
|
|
289 (article-brackets nil)
|
|
290 (article (url-filename urlobj)))
|
|
291 (url-news-open-host host port (url-user urlobj) (url-password urlobj))
|
|
292 (cond
|
|
293 ((string-match "@" article) ; Its a specific article
|
|
294 (url-news-fetch-message-id host port article))
|
|
295 ((string-match "/\\([0-9]+\\)$" article)
|
|
296 (url-news-fetch-article-number (substring article 0
|
|
297 (match-beginning 0))
|
|
298 (match-string 1 article)))
|
|
299
|
|
300 ((string= article "") ; List all newsgroups
|
|
301 (gnus)
|
|
302 (kill-buffer url-working-buffer))
|
|
303 (t ; Whole newsgroup
|
26
|
304 (url-news-fetch-newsgroup article)))))
|
14
|
305
|
|
306 (provide 'url-news)
|