nnrss.el 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. ;;; nnrss.el --- interfacing with RSS
  2. ;; Copyright (C) 2001-2015 Free Software Foundation, Inc.
  3. ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
  4. ;; Keywords: RSS
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (require 'gnus)
  20. (require 'nnoo)
  21. (require 'nnmail)
  22. (require 'message)
  23. (require 'mm-util)
  24. (require 'gnus-util)
  25. (require 'time-date)
  26. (require 'rfc2231)
  27. (require 'mm-url)
  28. (require 'rfc2047)
  29. (require 'mml)
  30. (eval-when-compile
  31. (ignore-errors
  32. (require 'xml)))
  33. (eval '(require 'xml))
  34. (nnoo-declare nnrss)
  35. (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
  36. "Where nnrss will save its files.")
  37. (defvoo nnrss-ignore-article-fields '(slash:comments)
  38. "*List of fields that should be ignored when comparing RSS articles.
  39. Some RSS feeds update article fields during their lives, e.g. to
  40. indicate the number of comments or the number of times the
  41. articles have been seen. However, if there is a difference
  42. between the local article and the distant one, the latter is
  43. considered to be new. To avoid this and discard some fields, set
  44. this variable to the list of fields to be ignored.")
  45. ;; (group max rss-url)
  46. (defvoo nnrss-server-data nil)
  47. ;; (num timestamp url subject author date extra)
  48. (defvoo nnrss-group-data nil)
  49. (defvoo nnrss-group-max 0)
  50. (defvoo nnrss-group-min 1)
  51. (defvoo nnrss-group nil)
  52. (defvoo nnrss-group-hashtb (make-hash-table :test 'equal))
  53. (defvoo nnrss-status-string "")
  54. (defconst nnrss-version "nnrss 1.0")
  55. (defvar nnrss-group-alist '()
  56. "List of RSS addresses.")
  57. (defvar nnrss-use-local nil
  58. "If non-nil nnrss will read the feeds from local files in nnrss-directory.")
  59. (defvar nnrss-description-field 'X-Gnus-Description
  60. "Field name used for DESCRIPTION.
  61. To use the description in headers, put this name into `nnmail-extra-headers'.")
  62. (defvar nnrss-url-field 'X-Gnus-Url
  63. "Field name used for URL.
  64. To use the description in headers, put this name into `nnmail-extra-headers'.")
  65. (defvar nnrss-content-function nil
  66. "A function which is called in `nnrss-request-article'.
  67. The arguments are (ENTRY GROUP ARTICLE).
  68. ENTRY is the record of the current headline. GROUP is the group name.
  69. ARTICLE is the article number of the current headline.")
  70. (defvar nnrss-file-coding-system mm-universal-coding-system
  71. "*Coding system used when reading and writing files.
  72. If you run Gnus with various versions of Emacsen, the value of this
  73. variable should be the coding system that all those Emacsen support.
  74. Note that you have to regenerate all the nnrss groups if you change
  75. the value. Moreover, you should be patient even if you are made to
  76. read the same articles twice, that arises for the difference of the
  77. versions of xml.el.")
  78. (defvar nnrss-compatible-encoding-alist
  79. (delq nil (mapcar (lambda (elem)
  80. (if (and (mm-coding-system-p (car elem))
  81. (mm-coding-system-p (cdr elem)))
  82. elem))
  83. mm-charset-override-alist))
  84. "Alist of encodings and those supersets.
  85. The cdr of each element is used to decode data if it is available when
  86. the car is what the data specify as the encoding. Or, the car is used
  87. for decoding when the cdr that the data specify is not available.")
  88. (nnoo-define-basics nnrss)
  89. ;;; Interface functions
  90. (defsubst nnrss-format-string (string)
  91. (gnus-replace-in-string string " *\n *" " "))
  92. (defun nnrss-decode-group-name (group)
  93. (if (and group (mm-coding-system-p 'utf-8))
  94. (setq group (mm-decode-coding-string group 'utf-8))
  95. group))
  96. (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
  97. (setq group (nnrss-decode-group-name group))
  98. (nnrss-possibly-change-group group server)
  99. (let (e)
  100. (with-current-buffer nntp-server-buffer
  101. (erase-buffer)
  102. (dolist (article articles)
  103. (if (setq e (assq article nnrss-group-data))
  104. (insert (number-to-string (car e)) "\t" ;; number
  105. ;; subject
  106. (or (nth 3 e) "")
  107. "\t"
  108. ;; from
  109. (or (nth 4 e) "(nobody)")
  110. "\t"
  111. ;; date
  112. (or (nth 5 e) "")
  113. "\t"
  114. ;; id
  115. (format "<%d@%s.nnrss>" (car e) group)
  116. "\t"
  117. ;; refs
  118. "\t"
  119. ;; chars
  120. "-1" "\t"
  121. ;; lines
  122. "-1" "\t"
  123. ;; Xref
  124. "" "\t"
  125. (if (and (nth 6 e)
  126. (memq nnrss-description-field
  127. nnmail-extra-headers))
  128. (concat (symbol-name nnrss-description-field)
  129. ": "
  130. (nnrss-format-string (nth 6 e))
  131. "\t")
  132. "")
  133. (if (and (nth 2 e)
  134. (memq nnrss-url-field
  135. nnmail-extra-headers))
  136. (concat (symbol-name nnrss-url-field)
  137. ": "
  138. (nnrss-format-string (nth 2 e))
  139. "\t")
  140. "")
  141. "\n")))))
  142. 'nov)
  143. (deffoo nnrss-request-group (group &optional server dont-check info)
  144. (setq group (nnrss-decode-group-name group))
  145. (nnheader-message 6 "nnrss: Requesting %s..." group)
  146. (nnrss-possibly-change-group group server)
  147. (prog1
  148. (if dont-check
  149. t
  150. (nnrss-check-group group server)
  151. (nnheader-report 'nnrss "Opened group %s" group)
  152. (nnheader-insert
  153. "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
  154. (prin1-to-string group)
  155. t))
  156. (nnheader-message 6 "nnrss: Requesting %s...done" group)))
  157. (deffoo nnrss-close-group (group &optional server)
  158. t)
  159. (deffoo nnrss-request-article (article &optional group server buffer)
  160. (setq group (nnrss-decode-group-name group))
  161. (when (stringp article)
  162. (setq article (if (string-match "\\`<\\([0-9]+\\)@" article)
  163. (string-to-number (match-string 1 article))
  164. 0)))
  165. (nnrss-possibly-change-group group server)
  166. (let ((e (assq article nnrss-group-data))
  167. (nntp-server-buffer (or buffer nntp-server-buffer))
  168. post err)
  169. (when e
  170. (with-current-buffer nntp-server-buffer
  171. (erase-buffer)
  172. (if group
  173. (insert "Newsgroups: " group "\n"))
  174. (if (nth 3 e)
  175. (insert "Subject: " (nth 3 e) "\n"))
  176. (if (nth 4 e)
  177. (insert "From: " (nth 4 e) "\n"))
  178. (if (nth 5 e)
  179. (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
  180. (let ((header (buffer-string))
  181. (text (nth 6 e))
  182. (link (nth 2 e))
  183. (enclosure (nth 7 e))
  184. (comments (nth 8 e))
  185. (rfc2047-header-encoding-alist
  186. (if (mm-coding-system-p 'utf-8)
  187. (cons '("Newsgroups" . utf-8)
  188. rfc2047-header-encoding-alist)
  189. rfc2047-header-encoding-alist))
  190. rfc2047-encode-encoded-words body fn)
  191. (when (or text link enclosure comments)
  192. (insert "\n")
  193. (insert "<#multipart type=alternative>\n"
  194. "<#part type=\"text/plain\">\n")
  195. (setq body (point))
  196. (when text
  197. (insert text)
  198. (goto-char body)
  199. (while (re-search-forward "\n+" nil t)
  200. (replace-match " "))
  201. (goto-char body)
  202. ;; See `nnrss-check-group', which inserts "<br /><br />".
  203. (when (search-forward "<br /><br />" nil t)
  204. (if (eobp)
  205. (replace-match "\n")
  206. (replace-match "\n\n")))
  207. (unless (eobp)
  208. (let ((fill-column (default-value 'fill-column))
  209. (window (get-buffer-window nntp-server-buffer)))
  210. (when window
  211. (setq fill-column
  212. (max 1 (/ (* (window-width window) 7) 8))))
  213. (fill-region (point) (point-max))
  214. (goto-char (point-max))
  215. ;; XEmacs version of `fill-region' inserts newline.
  216. (unless (bolp)
  217. (insert "\n"))))
  218. (when (or link enclosure)
  219. (insert "\n")))
  220. (when link
  221. (insert link "\n"))
  222. (when enclosure
  223. (insert (car enclosure) " "
  224. (nth 2 enclosure) " "
  225. (nth 3 enclosure) "\n"))
  226. (when comments
  227. (insert comments "\n"))
  228. (setq body (buffer-substring body (point)))
  229. (insert "<#/part>\n"
  230. "<#part type=\"text/html\">\n"
  231. "<html><head></head><body>\n")
  232. (when text
  233. (insert text "\n"))
  234. (when link
  235. (insert "<p><a href=\"" link "\">link</a></p>\n"))
  236. (when enclosure
  237. (insert "<p><a href=\"" (car enclosure) "\">"
  238. (cadr enclosure) "</a> " (nth 2 enclosure)
  239. " " (nth 3 enclosure) "</p>\n"))
  240. (when comments
  241. (insert "<p><a href=\"" comments "\">comments</a></p>\n"))
  242. (insert "</body></html>\n"
  243. "<#/part>\n"
  244. "<#/multipart>\n"))
  245. (condition-case nil
  246. ;; Allow `mml-to-mime' to generate MIME article without
  247. ;; making inquiry to a user for unknown encoding.
  248. (let ((mml-confirmation-set
  249. (cons 'unknown-encoding mml-confirmation-set)))
  250. (mml-to-mime))
  251. (error
  252. (erase-buffer)
  253. (insert header
  254. "Content-Type: text/plain; charset=gnus-decoded\n"
  255. "Content-Transfer-Encoding: 8bit\n\n"
  256. body)
  257. (nnheader-message
  258. 3 "Warning - there might be invalid characters"))))
  259. (goto-char (point-min))
  260. (search-forward "\n\n")
  261. (forward-line -1)
  262. (insert (format "Message-ID: <%d@%s.nnrss>\n"
  263. (car e)
  264. (let ((rfc2047-encoding-type 'mime)
  265. rfc2047-encode-max-chars)
  266. (rfc2047-encode-string
  267. (gnus-replace-in-string group "[\t\n ]+" "_")))))
  268. (when nnrss-content-function
  269. (funcall nnrss-content-function e group article))))
  270. (cond
  271. (err
  272. (nnheader-report 'nnrss err))
  273. ((not e)
  274. (nnheader-report 'nnrss "no such id: %d" article))
  275. (t
  276. (nnheader-report 'nnrss "article %s retrieved" (car e))
  277. ;; we return the article number.
  278. (cons nnrss-group (car e))))))
  279. (deffoo nnrss-open-server (server &optional defs connectionless)
  280. (nnrss-read-server-data server)
  281. (nnoo-change-server 'nnrss server defs)
  282. t)
  283. (deffoo nnrss-request-expire-articles
  284. (articles group &optional server force)
  285. (setq group (nnrss-decode-group-name group))
  286. (nnrss-possibly-change-group group server)
  287. (let (e days not-expirable changed)
  288. (dolist (art articles)
  289. (if (and (setq e (assq art nnrss-group-data))
  290. (nnmail-expired-article-p
  291. group
  292. (if (listp (setq days (nth 1 e))) days
  293. (days-to-time (- days (time-to-days '(0 0)))))
  294. force))
  295. (setq nnrss-group-data (delq e nnrss-group-data)
  296. changed t)
  297. (push art not-expirable)))
  298. (if changed
  299. (nnrss-save-group-data group server))
  300. not-expirable))
  301. (deffoo nnrss-request-delete-group (group &optional force server)
  302. (setq group (nnrss-decode-group-name group))
  303. (nnrss-possibly-change-group group server)
  304. (let (elem)
  305. ;; There may be two or more entries in `nnrss-group-alist' since
  306. ;; this function didn't delete them formerly.
  307. (while (setq elem (assoc group nnrss-group-alist))
  308. (setq nnrss-group-alist (delq elem nnrss-group-alist))))
  309. (setq nnrss-server-data
  310. (delq (assoc group nnrss-server-data) nnrss-server-data))
  311. (nnrss-save-server-data server)
  312. (ignore-errors
  313. (let ((file-name-coding-system nnmail-pathname-coding-system))
  314. (delete-file (nnrss-make-filename group server))))
  315. t)
  316. (deffoo nnrss-request-list-newsgroups (&optional server)
  317. (nnrss-possibly-change-group nil server)
  318. (with-current-buffer nntp-server-buffer
  319. (erase-buffer)
  320. (dolist (elem nnrss-group-alist)
  321. (if (third elem)
  322. (insert (car elem) "\t" (third elem) "\n"))))
  323. t)
  324. (deffoo nnrss-retrieve-groups (groups &optional server)
  325. (dolist (group groups)
  326. (setq group (nnrss-decode-group-name group))
  327. (nnrss-possibly-change-group group server)
  328. (nnrss-check-group group server))
  329. (with-current-buffer nntp-server-buffer
  330. (erase-buffer)
  331. (dolist (group groups)
  332. (let ((elem (assoc (gnus-group-decoded-name group) nnrss-server-data)))
  333. (insert (format "%S %s 1 y\n" group (or (cadr elem) 0)))))
  334. 'active))
  335. (nnoo-define-skeleton nnrss)
  336. ;;; Internal functions
  337. (eval-when-compile (defun xml-rpc-method-call (&rest args)))
  338. (defun nnrss-get-encoding ()
  339. "Return an encoding attribute specified in the current xml contents.
  340. If `nnrss-compatible-encoding-alist' specifies the compatible encoding,
  341. it is used instead. If the xml contents doesn't specify the encoding,
  342. return `utf-8' which is the default encoding for xml if it is available,
  343. otherwise return nil."
  344. (goto-char (point-min))
  345. (if (re-search-forward
  346. "<\\?[^>]*encoding=\\(?:\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
  347. nil t)
  348. (let ((encoding (intern (downcase (or (match-string 1)
  349. (match-string 2))))))
  350. (or
  351. (mm-coding-system-p (cdr (assq encoding
  352. nnrss-compatible-encoding-alist)))
  353. (mm-coding-system-p encoding)
  354. (mm-coding-system-p (car (rassq encoding
  355. nnrss-compatible-encoding-alist)))))
  356. (mm-coding-system-p 'utf-8)))
  357. (declare-function libxml-parse-html-region "xml.c"
  358. (start end &optional base-url discard-comments))
  359. (defun nnrss-fetch (url &optional local)
  360. "Fetch URL and put it in a the expected Lisp structure."
  361. (mm-with-unibyte-buffer
  362. ;;some versions of url.el need this to close the connection quickly
  363. (let (cs xmlform htmlform)
  364. ;; bit o' work necessary for w3 pre-cvs and post-cvs
  365. (if local
  366. (let ((coding-system-for-read 'binary))
  367. (insert-file-contents url))
  368. ;; FIXME: shouldn't binding `coding-system-for-read' be moved
  369. ;; to `mm-url-insert'?
  370. (let ((coding-system-for-read 'binary))
  371. (condition-case err
  372. (mm-url-insert url)
  373. (error (if (or debug-on-quit debug-on-error)
  374. (signal (car err) (cdr err))
  375. (message "nnrss: Failed to fetch %s" url))))))
  376. (nnheader-remove-cr-followed-by-lf)
  377. ;; Decode text according to the encoding attribute.
  378. (when (setq cs (nnrss-get-encoding))
  379. (insert (prog1
  380. (mm-decode-coding-string (buffer-string) cs)
  381. (erase-buffer)
  382. (mm-enable-multibyte))))
  383. (goto-char (point-min))
  384. (condition-case err1
  385. (setq xmlform (xml-parse-region (point-min) (point-max)))
  386. (error
  387. (condition-case err2
  388. (setq htmlform (libxml-parse-html-region (point-min) (point-max)))
  389. (error
  390. (message "\
  391. nnrss: %s: Not valid XML %s and libxml-parse-html-region doesn't work %s"
  392. url err1 err2)))))
  393. (if htmlform
  394. htmlform
  395. xmlform))))
  396. (defun nnrss-possibly-change-group (&optional group server)
  397. (when (and server
  398. (not (nnrss-server-opened server)))
  399. (nnrss-open-server server))
  400. (when (and group (not (equal group nnrss-group)))
  401. (nnrss-read-group-data group server)
  402. (setq nnrss-group group)))
  403. (autoload 'timezone-parse-date "timezone")
  404. (defun nnrss-normalize-date (date)
  405. "Return a date string of DATE in the RFC822 style.
  406. This function handles the ISO 8601 date format described in
  407. URL `http://www.w3.org/TR/NOTE-datetime', and also the RFC822 style
  408. which RSS 2.0 allows."
  409. (let (case-fold-search vector year month day time zone cts given)
  410. (cond ((null date)) ; do nothing for this case
  411. ;; if the date is just digits (unix time stamp):
  412. ((string-match "^[0-9]+$" date)
  413. (setq given (seconds-to-time (string-to-number date))))
  414. ;; RFC822
  415. ((string-match " [0-9]+ " date)
  416. (setq vector (timezone-parse-date date)
  417. year (string-to-number (aref vector 0)))
  418. (when (>= year 1969)
  419. (setq month (string-to-number (aref vector 1))
  420. day (string-to-number (aref vector 2)))
  421. (unless (>= (length (setq time (aref vector 3))) 3)
  422. (setq time "00:00:00"))
  423. (when (and (setq zone (aref vector 4))
  424. (not (string-match "\\`[A-Z+-]" zone)))
  425. (setq zone nil))))
  426. ;; ISO 8601
  427. ((string-match
  428. (eval-when-compile
  429. (concat
  430. ;; 1. year
  431. "\\(199[0-9]\\|20[0-9][0-9]\\)"
  432. "\\(?:-"
  433. ;; 2. month
  434. "\\([01][0-9]\\)"
  435. "\\(?:-"
  436. ;; 3. day
  437. "\\([0-3][0-9]\\)"
  438. "\\)?\\)?\\(?:T"
  439. ;; 4. hh:mm
  440. "\\([012][0-9]:[0-5][0-9]\\)"
  441. "\\(?:"
  442. ;; 5. :ss
  443. "\\(:[0-5][0-9]\\)"
  444. "\\(?:\\.[0-9]+\\)?\\)?\\)?"
  445. ;; 6+7,8,9. zone
  446. "\\(?:\\(?:\\([+-][012][0-9]\\):\\([0-5][0-9]\\)\\)"
  447. "\\|\\([+-][012][0-9][0-5][0-9]\\)"
  448. "\\|\\(Z\\)\\)?"))
  449. date)
  450. (setq year (string-to-number (match-string 1 date))
  451. month (string-to-number (or (match-string 2 date) "1"))
  452. day (string-to-number (or (match-string 3 date) "1"))
  453. time (if (match-beginning 5)
  454. (substring date (match-beginning 4) (match-end 5))
  455. (concat (or (match-string 4 date) "00:00") ":00"))
  456. zone (cond ((match-beginning 6)
  457. (concat (match-string 6 date)
  458. (match-string 7 date)))
  459. ((match-beginning 9) ;; Z
  460. "+0000")
  461. (t ;; nil if zone is not provided.
  462. (match-string 8 date))))))
  463. (if month
  464. (progn
  465. (setq cts (current-time-string (encode-time 0 0 0 day month year)))
  466. (format "%s, %02d %s %04d %s%s"
  467. (substring cts 0 3) day (substring cts 4 7) year time
  468. (if zone
  469. (concat " " zone)
  470. "")))
  471. (message-make-date given))))
  472. ;;; data functions
  473. (defun nnrss-read-server-data (server)
  474. (setq nnrss-server-data nil)
  475. (let ((file (nnrss-make-filename "nnrss" server))
  476. (file-name-coding-system nnmail-pathname-coding-system))
  477. (when (file-exists-p file)
  478. (load file nil t t))))
  479. (defun nnrss-save-server-data (server)
  480. (gnus-make-directory nnrss-directory)
  481. (let ((coding-system-for-write nnrss-file-coding-system)
  482. (file-name-coding-system nnmail-pathname-coding-system))
  483. (with-temp-file (nnrss-make-filename "nnrss" server)
  484. (insert (format ";; -*- coding: %s; -*-\n"
  485. nnrss-file-coding-system))
  486. (gnus-prin1 `(setq nnrss-group-alist ',nnrss-group-alist))
  487. (insert "\n")
  488. (gnus-prin1 `(setq nnrss-server-data ',nnrss-server-data)))))
  489. (defun nnrss-read-group-data (group server)
  490. (setq nnrss-group-data nil)
  491. (if (hash-table-p nnrss-group-hashtb)
  492. (clrhash nnrss-group-hashtb)
  493. (setq nnrss-group-hashtb (make-hash-table :test 'equal)))
  494. (let ((pair (assoc group nnrss-server-data)))
  495. (setq nnrss-group-max (or (cadr pair) 0))
  496. (setq nnrss-group-min (+ nnrss-group-max 1)))
  497. (let ((file (nnrss-make-filename group server))
  498. (file-name-coding-system nnmail-pathname-coding-system))
  499. (when (file-exists-p file)
  500. (load file nil t t)
  501. (dolist (e nnrss-group-data)
  502. (puthash (nth 9 e) t nnrss-group-hashtb)
  503. (when (and (car e) (> nnrss-group-min (car e)))
  504. (setq nnrss-group-min (car e)))
  505. (when (and (car e) (< nnrss-group-max (car e)))
  506. (setq nnrss-group-max (car e)))))))
  507. (defun nnrss-save-group-data (group server)
  508. (gnus-make-directory nnrss-directory)
  509. (let ((coding-system-for-write nnrss-file-coding-system)
  510. (file-name-coding-system nnmail-pathname-coding-system))
  511. (with-temp-file (nnrss-make-filename group server)
  512. (insert (format ";; -*- coding: %s; -*-\n"
  513. nnrss-file-coding-system))
  514. (gnus-prin1 `(setq nnrss-group-data ',nnrss-group-data)))))
  515. (defun nnrss-make-filename (name server)
  516. (expand-file-name
  517. (nnrss-translate-file-chars
  518. (concat name
  519. (and server
  520. (not (equal server ""))
  521. "-")
  522. server
  523. ".el"))
  524. nnrss-directory))
  525. (gnus-add-shutdown 'nnrss-close 'gnus)
  526. (defun nnrss-close ()
  527. "Clear internal nnrss variables."
  528. (setq nnrss-group-data nil
  529. nnrss-server-data nil
  530. nnrss-group-hashtb nil
  531. nnrss-group-alist nil))
  532. ;;; URL interface
  533. (defun nnrss-no-cache (url)
  534. "")
  535. (defun nnrss-insert (url)
  536. (mm-with-unibyte-current-buffer
  537. (condition-case err
  538. (mm-url-insert url)
  539. (error (if (or debug-on-quit debug-on-error)
  540. (signal (car err) (cdr err))
  541. (message "nnrss: Failed to fetch %s" url))))))
  542. (defun nnrss-decode-entities-string (string)
  543. (if string
  544. (mm-with-multibyte-buffer
  545. (insert string)
  546. (mm-url-decode-entities-nbsp)
  547. (buffer-string))))
  548. (defun nnrss-mime-encode-string (string)
  549. (mm-with-multibyte-buffer
  550. (insert string)
  551. (mm-url-decode-entities-nbsp)
  552. (goto-char (point-min))
  553. (while (re-search-forward "[\t\n ]+" nil t)
  554. (replace-match " "))
  555. (goto-char (point-min))
  556. (skip-chars-forward " ")
  557. (delete-region (point-min) (point))
  558. (goto-char (point-max))
  559. (skip-chars-forward " ")
  560. (delete-region (point) (point-max))
  561. (let ((rfc2047-encoding-type 'mime)
  562. rfc2047-encode-max-chars)
  563. (rfc2047-encode-region (point-min) (point-max)))
  564. (goto-char (point-min))
  565. (while (search-forward "\n" nil t)
  566. (delete-char -1))
  567. (buffer-string)))
  568. ;;; Snarf functions
  569. (defun nnrss-make-hash-index (item)
  570. (gnus-message 9 "nnrss: Making hash index of %s" (gnus-prin1-to-string item))
  571. (setq item (gnus-remove-if
  572. (lambda (field)
  573. (when (listp field)
  574. (memq (car field) nnrss-ignore-article-fields)))
  575. item))
  576. (md5 (gnus-prin1-to-string item)
  577. nil nil
  578. nnrss-file-coding-system))
  579. (defun nnrss-check-group (group server)
  580. (let (file xml subject url extra changed author date feed-subject
  581. enclosure comments rss-ns rdf-ns content-ns dc-ns
  582. hash-index)
  583. (if (and nnrss-use-local
  584. (file-exists-p (setq file (expand-file-name
  585. (nnrss-translate-file-chars
  586. (concat group ".xml"))
  587. nnrss-directory))))
  588. (setq xml (nnrss-fetch file t))
  589. (setq url (or (nth 2 (assoc group nnrss-server-data))
  590. (second (assoc group nnrss-group-alist))))
  591. (unless url
  592. (setq url
  593. (cdr
  594. (assoc 'href
  595. (nnrss-discover-feed
  596. (read-string
  597. (format "URL to search for %s: " group) "http://")))))
  598. (let ((pair (assoc group nnrss-server-data)))
  599. (if pair
  600. (setcdr (cdr pair) (list url))
  601. (push (list group nnrss-group-max url) nnrss-server-data)))
  602. (setq changed t))
  603. (setq xml (nnrss-fetch url)))
  604. (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
  605. rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
  606. rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
  607. content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
  608. (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
  609. (when (and (listp item)
  610. (string= (concat rss-ns "item") (car item))
  611. (progn (setq hash-index (nnrss-make-hash-index item))
  612. (not (gethash hash-index nnrss-group-hashtb))))
  613. (setq subject (nnrss-node-text rss-ns 'title item))
  614. (setq url (nnrss-decode-entities-string
  615. (nnrss-node-text rss-ns 'link (cddr item))))
  616. (setq extra (or (nnrss-node-text content-ns 'encoded item)
  617. (nnrss-node-text rss-ns 'description item)))
  618. (if (setq feed-subject (nnrss-node-text dc-ns 'subject item))
  619. (setq extra (concat feed-subject "<br /><br />" extra)))
  620. (setq author (or (nnrss-node-text rss-ns 'author item)
  621. (nnrss-node-text dc-ns 'creator item)
  622. (nnrss-node-text dc-ns 'contributor item)))
  623. (setq date (nnrss-normalize-date
  624. (or (nnrss-node-text dc-ns 'date item)
  625. (nnrss-node-text rss-ns 'pubDate item))))
  626. (setq comments (nnrss-node-text rss-ns 'comments item))
  627. (when (setq enclosure (cadr (assq (intern (concat rss-ns "enclosure")) item)))
  628. (let ((url (cdr (assq 'url enclosure)))
  629. (len (cdr (assq 'length enclosure)))
  630. (type (cdr (assq 'type enclosure)))
  631. (name))
  632. (setq len
  633. (if (and len (integerp (setq len (string-to-number len))))
  634. ;; actually already in `ls-lisp-format-file-size' but
  635. ;; probably not worth to require it for one function
  636. (do ((size (/ len 1.0) (/ size 1024.0))
  637. (post-fixes (list "" "k" "M" "G" "T" "P" "E")
  638. (cdr post-fixes)))
  639. ((< size 1024)
  640. (format "%.1f%s" size (car post-fixes))))
  641. "0"))
  642. (setq url (or url ""))
  643. (setq name (if (string-match "/\\([^/]*\\)$" url)
  644. (match-string 1 url)
  645. "file"))
  646. (setq type (or type ""))
  647. (setq enclosure (list url name len type))))
  648. (push
  649. (list
  650. (incf nnrss-group-max)
  651. (current-time)
  652. url
  653. (and subject (nnrss-mime-encode-string subject))
  654. (and author (nnrss-mime-encode-string author))
  655. date
  656. (and extra (nnrss-decode-entities-string extra))
  657. enclosure
  658. comments
  659. hash-index)
  660. nnrss-group-data)
  661. (puthash hash-index t nnrss-group-hashtb)
  662. (setq changed t))
  663. (setq extra nil))
  664. (when changed
  665. (nnrss-save-group-data group server)
  666. (let ((pair (assoc group nnrss-server-data)))
  667. (if pair
  668. (setcar (cdr pair) nnrss-group-max)
  669. (push (list group nnrss-group-max) nnrss-server-data)))
  670. (nnrss-save-server-data server))))
  671. (declare-function gnus-group-make-rss-group "gnus-group" (&optional url))
  672. (defun nnrss-opml-import (opml-file)
  673. "OPML subscriptions import.
  674. Read the file and attempt to subscribe to each Feed in the file."
  675. (interactive "fImport file: ")
  676. (mapc
  677. (lambda (node)
  678. (let ((xmlurl (cdr (assq 'xmlUrl (cadr node)))))
  679. (when (and xmlurl
  680. (not (string-match "\\`[\t ]*\\'" xmlurl))
  681. (prog1
  682. (y-or-n-p (format "Subscribe to %s " xmlurl))
  683. (message "")))
  684. (condition-case err
  685. (progn
  686. (gnus-group-make-rss-group xmlurl)
  687. (forward-line 1))
  688. (error
  689. (message
  690. "Failed to subscribe to %s (%s); type any key to continue: "
  691. xmlurl
  692. (error-message-string err))
  693. (let ((echo-keystrokes 0))
  694. (read-char)))))))
  695. (nnrss-find-el 'outline
  696. (mm-with-multibyte-buffer
  697. (insert-file-contents opml-file)
  698. (xml-parse-region (point-min) (point-max))))))
  699. (defun nnrss-opml-export ()
  700. "OPML subscription export.
  701. Export subscriptions to a buffer in OPML Format."
  702. (interactive)
  703. (with-current-buffer (get-buffer-create "*OPML Export*")
  704. (mm-set-buffer-file-coding-system 'utf-8)
  705. (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  706. "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
  707. "<opml version=\"1.1\">\n"
  708. " <head>\n"
  709. " <title>mySubscriptions</title>\n"
  710. " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
  711. "</dateCreated>\n"
  712. " <ownerEmail>" user-mail-address "</ownerEmail>\n"
  713. " <ownerName>" (user-full-name) "</ownerName>\n"
  714. " </head>\n"
  715. " <body>\n")
  716. (dolist (sub nnrss-group-alist)
  717. (insert " <outline text=\"" (car sub)
  718. "\" xmlUrl=\"" (cadr sub) "\"/>\n"))
  719. (insert " </body>\n"
  720. "</opml>\n"))
  721. (pop-to-buffer "*OPML Export*")
  722. (when (fboundp 'sgml-mode)
  723. (sgml-mode)))
  724. (defun nnrss-generate-download-script ()
  725. "Generate a download script in the current buffer.
  726. It is useful when `(setq nnrss-use-local t)'."
  727. (interactive)
  728. (insert "#!/bin/sh\n")
  729. (insert "WGET=wget\n")
  730. (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
  731. (dolist (elem nnrss-server-data)
  732. (let ((url (or (nth 2 elem)
  733. (second (assoc (car elem) nnrss-group-alist)))))
  734. (insert "$WGET -q -O \"$RSSDIR\"/'"
  735. (nnrss-translate-file-chars (concat (car elem) ".xml"))
  736. "' '" url "'\n"))))
  737. (defun nnrss-translate-file-chars (name)
  738. (let ((nnheader-file-name-translation-alist
  739. (append nnheader-file-name-translation-alist '((?' . ?_)))))
  740. (nnheader-translate-file-chars name)))
  741. (defun nnrss-node-text (namespace local-name element)
  742. (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
  743. element))
  744. (text (if (and node (listp node))
  745. (nnrss-node-just-text node)
  746. node))
  747. (cleaned-text (if text
  748. (gnus-replace-in-string
  749. (gnus-replace-in-string
  750. text "^[\000-\037\177]+\\|^ +\\| +$" "")
  751. "\r\n" "\n"))))
  752. (if (string-equal "" cleaned-text)
  753. nil
  754. cleaned-text)))
  755. (defun nnrss-node-just-text (node)
  756. (if (and node (listp node))
  757. (mapconcat 'nnrss-node-just-text (cddr node) " ")
  758. node))
  759. (defun nnrss-find-el (tag data &optional found-list)
  760. "Find the all matching elements in the data.
  761. Careful with this on large documents!"
  762. (when (consp data)
  763. (dolist (bit data)
  764. (when (car-safe bit)
  765. (when (equal tag (car bit))
  766. ;; Old xml.el may return a list of string.
  767. (when (and (consp (caddr bit))
  768. (stringp (caaddr bit)))
  769. (setcar (cddr bit) (caaddr bit)))
  770. (setq found-list
  771. (append found-list
  772. (list bit))))
  773. (if (and (consp (car-safe (caddr bit)))
  774. (not (stringp (caddr bit))))
  775. (setq found-list
  776. (append found-list
  777. (nnrss-find-el
  778. tag (caddr bit))))
  779. (setq found-list
  780. (append found-list
  781. (nnrss-find-el
  782. tag (cddr bit))))))))
  783. found-list)
  784. (defun nnrss-rsslink-p (el)
  785. "Test if the element we are handed is an RSS autodiscovery link."
  786. (and (eq (car-safe el) 'link)
  787. (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
  788. (or (string-equal (cdr (assoc 'type (cadr el)))
  789. "application/rss+xml")
  790. (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
  791. (defun nnrss-get-rsslinks (data)
  792. "Extract the <link> elements that are links to RSS from the parsed data."
  793. (delq nil (mapcar
  794. (lambda (el)
  795. (if (nnrss-rsslink-p el) el))
  796. (nnrss-find-el 'link data))))
  797. (defun nnrss-extract-hrefs (data)
  798. "Recursively extract hrefs from a page's source.
  799. DATA should be the output of `xml-parse-region'."
  800. (mapcar (lambda (ahref)
  801. (cdr (assoc 'href (cadr ahref))))
  802. (nnrss-find-el 'a data)))
  803. (defmacro nnrss-match-macro (base-uri item onsite-list offsite-list)
  804. `(cond ((or (string-match (concat "^" ,base-uri) ,item)
  805. (not (string-match "://" ,item)))
  806. (setq ,onsite-list (append ,onsite-list (list ,item))))
  807. (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
  808. (defun nnrss-order-hrefs (base-uri hrefs)
  809. "Given a list of hrefs, sort them using the following priorities:
  810. 1. links ending in .rss
  811. 2. links ending in .rdf
  812. 3. links ending in .xml
  813. 4. links containing the above
  814. 5. offsite links
  815. BASE-URI is used to determine the location of the links and
  816. whether they are `offsite' or `onsite'."
  817. (let (rss-onsite-end rdf-onsite-end xml-onsite-end
  818. rss-onsite-in rdf-onsite-in xml-onsite-in
  819. rss-offsite-end rdf-offsite-end xml-offsite-end
  820. rss-offsite-in rdf-offsite-in xml-offsite-in)
  821. (dolist (href hrefs)
  822. (cond ((null href))
  823. ((string-match "\\.rss$" href)
  824. (nnrss-match-macro
  825. base-uri href rss-onsite-end rss-offsite-end))
  826. ((string-match "\\.rdf$" href)
  827. (nnrss-match-macro
  828. base-uri href rdf-onsite-end rdf-offsite-end))
  829. ((string-match "\\.xml$" href)
  830. (nnrss-match-macro
  831. base-uri href xml-onsite-end xml-offsite-end))
  832. ((string-match "rss" href)
  833. (nnrss-match-macro
  834. base-uri href rss-onsite-in rss-offsite-in))
  835. ((string-match "rdf" href)
  836. (nnrss-match-macro
  837. base-uri href rdf-onsite-in rdf-offsite-in))
  838. ((string-match "xml" href)
  839. (nnrss-match-macro
  840. base-uri href xml-onsite-in xml-offsite-in))))
  841. (append
  842. rss-onsite-end rdf-onsite-end xml-onsite-end
  843. rss-onsite-in rdf-onsite-in xml-onsite-in
  844. rss-offsite-end rdf-offsite-end xml-offsite-end
  845. rss-offsite-in rdf-offsite-in xml-offsite-in)))
  846. (defun nnrss-discover-feed (url)
  847. "Given a page, find an RSS feed.
  848. Use Mark Pilgrim's `ultra-liberal rss locator'."
  849. (let ((parsed-page (nnrss-fetch url)))
  850. ;; 1. if this url is the rss, use it.
  851. (if (nnrss-rss-p parsed-page)
  852. (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
  853. (nnrss-rss-title-description rss-ns parsed-page url))
  854. ;; 2. look for the <link rel="alternate"
  855. ;; type="application/rss+xml" and use that if it is there.
  856. (let ((links (nnrss-get-rsslinks parsed-page)))
  857. (if links
  858. (let* ((xml (nnrss-fetch
  859. (cdr (assoc 'href (cadar links)))))
  860. (rss-ns (nnrss-get-namespace-prefix
  861. xml "http://purl.org/rss/1.0/")))
  862. (nnrss-rss-title-description
  863. rss-ns xml (cdr (assoc 'href (cadar links)))))
  864. ;; 3. look for links on the site in the following order:
  865. ;; - onsite links ending in .rss, .rdf, or .xml
  866. ;; - onsite links containing any of the above
  867. ;; - offsite links ending in .rss, .rdf, or .xml
  868. ;; - offsite links containing any of the above
  869. (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
  870. (match-string 0 url)))
  871. (hrefs (nnrss-order-hrefs
  872. base-uri (nnrss-extract-hrefs parsed-page)))
  873. (rss-link nil))
  874. (while (and (eq rss-link nil) (not (eq hrefs nil)))
  875. (let ((href-data (nnrss-fetch (car hrefs))))
  876. (if (nnrss-rss-p href-data)
  877. (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
  878. (setq rss-link (nnrss-rss-title-description
  879. rss-ns href-data (car hrefs))))
  880. (setq hrefs (cdr hrefs)))))
  881. (if rss-link
  882. rss-link
  883. ;; 4. check syndic8
  884. (nnrss-find-rss-via-syndic8 url))))))))
  885. (defun nnrss-find-rss-via-syndic8 (url)
  886. "Query syndic8 for the rss feeds it has for URL."
  887. (if (not (locate-library "xml-rpc"))
  888. (progn
  889. (message "XML-RPC is not available... not checking Syndic8.")
  890. nil)
  891. (require 'xml-rpc)
  892. (let ((feedid (xml-rpc-method-call
  893. "http://www.syndic8.com/xmlrpc.php"
  894. 'syndic8.FindSites
  895. url)))
  896. (when feedid
  897. (let* ((feedinfo (xml-rpc-method-call
  898. "http://www.syndic8.com/xmlrpc.php"
  899. 'syndic8.GetFeedInfo
  900. feedid))
  901. (urllist
  902. (delq nil
  903. (mapcar
  904. (lambda (listinfo)
  905. (if (string-equal
  906. (cdr (assoc "status" listinfo))
  907. "Syndicated")
  908. (cons
  909. (cdr (assoc "sitename" listinfo))
  910. (list
  911. (cons 'title
  912. (cdr (assoc
  913. "sitename" listinfo)))
  914. (cons 'href
  915. (cdr (assoc
  916. "dataurl" listinfo)))))))
  917. feedinfo))))
  918. (if (not (> (length urllist) 1))
  919. (cdar urllist)
  920. (let ((completion-ignore-case t)
  921. (selection
  922. (mapcar (lambda (listinfo)
  923. (cons (cdr (assoc "sitename" listinfo))
  924. (string-to-number
  925. (cdr (assoc "feedid" listinfo)))))
  926. feedinfo)))
  927. (cdr (assoc
  928. (gnus-completing-read
  929. "Multiple feeds found. Select one"
  930. selection t) urllist)))))))))
  931. (defun nnrss-rss-p (data)
  932. "Test if DATA is an RSS feed.
  933. Simply ensures that the first element is rss or rdf."
  934. (or (eq (caar data) 'rss)
  935. (eq (caar data) 'rdf:RDF)))
  936. (defun nnrss-rss-title-description (rss-namespace data url)
  937. "Return the title of an RSS feed."
  938. (if (nnrss-rss-p data)
  939. (let ((description (intern (concat rss-namespace "description")))
  940. (title (intern (concat rss-namespace "title")))
  941. (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
  942. data)))
  943. (list
  944. (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
  945. (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
  946. (cons 'href url)))))
  947. (defun nnrss-get-namespace-prefix (el uri)
  948. "Given EL (containing a parsed element) and URI (containing a string
  949. that gives the URI for which you want to retrieve the namespace
  950. prefix), return the prefix."
  951. (let* ((prefix (car (rassoc uri (cadar el))))
  952. (nslist (if prefix
  953. (split-string (symbol-name prefix) ":")))
  954. (ns (cond ((eq (length nslist) 1) ; no prefix given
  955. "")
  956. ((eq (length nslist) 2) ; extract prefix
  957. (cadr nslist)))))
  958. (if (and ns (not (string= ns "")))
  959. (concat ns ":")
  960. ns)))
  961. (provide 'nnrss)
  962. ;;; nnrss.el ends here