]> code.delx.au - gnu-emacs/blob - lisp/url/url-news.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / url / url-news.el
1 ;;; url-news.el --- News Uniform Resource Locator retrieval code
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Keywords: comm, data, processes
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Code:
26
27 (require 'url-vars)
28 (require 'url-util)
29 (require 'url-parse)
30 (require 'nntp)
31 (autoload 'url-warn "url")
32 (autoload 'gnus-group-read-ephemeral-group "gnus-group")
33 (eval-when-compile (require 'cl))
34 (defvar nntp-open-tls-stream)
35 (defvar nntp-open-ssl-stream)
36
37 (defgroup url-news nil
38 "News related options."
39 :group 'url)
40
41 (defun url-news-open-host (host port user pass)
42 (if (fboundp 'nnheader-init-server-buffer)
43 (nnheader-init-server-buffer))
44 (nntp-open-server host (list port))
45 (if (and user pass)
46 (progn
47 (nntp-send-command "^.*\r?\n" "AUTHINFO USER" user)
48 (nntp-send-command "^.*\r?\n" "AUTHINFO PASS" pass)
49 (if (not (nntp-server-opened host))
50 (url-warn 'url (format "NNTP authentication to `%s' as `%s' failed"
51 host user))))))
52
53 (defun url-news-fetch-message-id (host message-id)
54 (let ((buf (generate-new-buffer " *url-news*")))
55 (if (eq ?> (aref message-id (1- (length message-id))))
56 nil
57 (setq message-id (concat "<" message-id ">")))
58 (if (cdr-safe (nntp-request-article message-id nil host buf))
59 ;; Successfully retrieved the article
60 nil
61 (with-current-buffer buf
62 (insert "Content-type: text/html\n\n"
63 "<html>\n"
64 " <head>\n"
65 " <title>Error</title>\n"
66 " </head>\n"
67 " <body>\n"
68 " <div>\n"
69 " <h1>Error requesting article...</h1>\n"
70 " <p>\n"
71 " The status message returned by the NNTP server was:"
72 "<br><hr>\n"
73 " <xmp>\n"
74 (nntp-status-message)
75 " </xmp>\n"
76 " </p>\n"
77 " <p>\n"
78 " If you If you feel this is an error, <a href=\""
79 "mailto:" url-bug-address "\">send mail</a>\n"
80 " </p>\n"
81 " </div>\n"
82 " </body>\n"
83 "</html>\n"
84 "<!-- Automatically generated by URL v" url-version " -->\n"
85 )))
86 buf))
87
88 (defun url-news-fetch-newsgroup (newsgroup host)
89 (declare (special gnus-group-buffer))
90 (if (string-match "^/+" newsgroup)
91 (setq newsgroup (substring newsgroup (match-end 0))))
92 (if (string-match "/+$" newsgroup)
93 (setq newsgroup (substring newsgroup 0 (match-beginning 0))))
94
95 ;; This saves us from checking new news if Gnus is already running
96 ;; FIXME - is it relatively safe to use gnus-alive-p here? FIXME
97 (if (or (not (get-buffer gnus-group-buffer))
98 (with-current-buffer gnus-group-buffer
99 (not (eq major-mode 'gnus-group-mode))))
100 (gnus))
101 (set-buffer gnus-group-buffer)
102 (goto-char (point-min))
103 (gnus-group-read-ephemeral-group newsgroup
104 (list 'nntp host
105 'nntp-open-connection-function
106 nntp-open-connection-function)
107 nil
108 (cons (current-buffer) 'browse)))
109
110 ;;;###autoload
111 (defun url-news (url)
112 ;; Find a news reference
113 (let* ((host (or (url-host url) url-news-server))
114 (port (url-port url))
115 (article-brackets nil)
116 (buf nil)
117 (article (url-unhex-string (url-filename url))))
118 (url-news-open-host host port (url-user url) (url-password url))
119 (cond
120 ((string-match "@" article) ; Its a specific article
121 (setq buf (url-news-fetch-message-id host article)))
122 ((string= article "") ; List all newsgroups
123 (gnus))
124 (t ; Whole newsgroup
125 (url-news-fetch-newsgroup article host)))
126 buf))
127
128 ;;;###autoload
129 (defun url-snews (url)
130 (let ((nntp-open-connection-function (if (eq 'tls url-gateway-method)
131 nntp-open-tls-stream
132 nntp-open-ssl-stream)))
133 (url-news url)))
134
135 (provide 'url-news)
136
137 ;; arch-tag: 8975be13-04e8-4d38-bfff-47918e3ad311
138 ;;; url-news.el ends here