url-news.el 4.0 KB

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