nnrss.el 35 KB

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