newsticker: handle url-retrieve errors.
[emacs.git] / lisp / net / newst-backend.el
blobf0174c588b8af2433565ea4202d43bbcd13c8bc2
1 ;;; newst-backend.el --- Retrieval backend for newsticker.
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
6 ;; Author: Ulf Jasper <ulf.jasper@web.de>
7 ;; Filename: newst-backend.el
8 ;; URL: http://www.nongnu.org/newsticker
9 ;; Keywords: News, RSS, Atom
10 ;; Time-stamp: "7. Juli 2008, 19:20:10 (ulf)"
12 ;; ======================================================================
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; ======================================================================
31 ;;; Commentary:
33 ;; See newsticker.el
35 ;; ======================================================================
36 ;;; Code:
38 (require 'derived)
39 (require 'xml)
41 ;; Silence warnings
42 (defvar w3-mode-map)
43 (defvar w3m-minor-mode-map)
46 (defvar newsticker--retrieval-timer-list nil
47 "List of timers for news retrieval.
48 This is an alist, each element consisting of (feed-name . timer).")
50 (defvar newsticker--download-logos nil
51 "If non-nil download feed logos if available.")
53 (defvar newsticker--sentinel-callback nil
54 "Function called at end of `newsticker--sentinel'.")
56 ;;;###autoload
57 (defun newsticker-running-p ()
58 "Check whether newsticker is running.
59 Return t if newsticker is running, nil otherwise. Newsticker is
60 considered to be running if the newsticker timer list is not empty."
61 (> (length newsticker--retrieval-timer-list) 0))
63 ;; ======================================================================
64 ;;; Customization
65 ;; ======================================================================
66 (defgroup newsticker nil
67 "Aggregator for RSS and Atom feeds."
68 :group 'applications)
70 (defconst newsticker--raw-url-list-defaults
71 '(("CNET News.com"
72 "http://export.cnet.com/export/feeds/news/rss/1,11176,,00.xml")
73 ("Debian Security Advisories"
74 "http://www.debian.org/security/dsa.en.rdf")
75 ("Debian Security Advisories - Long format"
76 "http://www.debian.org/security/dsa-long.en.rdf")
77 ("Emacs Wiki"
78 "http://www.emacswiki.org/cgi-bin/wiki.pl?action=rss"
79 nil
80 3600)
81 ("Freshmeat.net"
82 "http://freshmeat.net/backend/fm.rdf")
83 ("Kuro5hin.org"
84 "http://www.kuro5hin.org/backend.rdf")
85 ("LWN (Linux Weekly News)"
86 "http://lwn.net/headlines/rss")
87 ("NewsForge"
88 "http://newsforge.com/index.rss")
89 ("NY Times: Technology"
90 "http://partners.userland.com/nytRss/technology.xml")
91 ("NY Times"
92 "http://partners.userland.com/nytRss/nytHomepage.xml")
93 ("Quote of the day"
94 "http://www.quotationspage.com/data/qotd.rss"
95 "07:00"
96 86400)
97 ("The Register"
98 "http://www.theregister.co.uk/tonys/slashdot.rdf")
99 ("slashdot"
100 "http://slashdot.org/index.rss"
102 3600) ;/. will ban you if under 3600 seconds!
103 ("Wired News"
104 "http://www.wired.com/news_drop/netcenter/netcenter.rdf")
105 ("Heise News (german)"
106 "http://www.heise.de/newsticker/heise.rdf")
107 ("Tagesschau (german)"
108 "http://www.tagesschau.de/newsticker.rdf"
110 1800)
111 ("Telepolis (german)"
112 "http://www.heise.de/tp/news.rdf"))
113 "Default URL list in raw form.
114 This list is fed into defcustom via `newsticker--splicer'.")
116 (defun newsticker--splicer (item)
117 "Convert ITEM for splicing into `newsticker-url-list-defaults'."
118 (let ((result (list 'list :tag (nth 0 item) (list 'const (nth 0 item))))
119 (element (cdr item)))
120 (while element
121 (setq result (append result (list (list 'const (car element)))))
122 (setq element (cdr element)))
123 result))
125 (defun newsticker--set-customvar-retrieval (symbol value)
126 "Set retrieval related newsticker-variable SYMBOL value to VALUE.
127 Calls all actions which are necessary in order to make the new
128 value effective."
129 (if (or (not (boundp symbol))
130 (equal (symbol-value symbol) value))
131 (set symbol value)
132 ;; something must have changed
133 (let ((need-restart nil)
134 (new-or-changed-feeds nil)
135 (removed-feeds))
136 (cond ((eq symbol 'newsticker-retrieval-interval)
137 (setq need-restart t))
138 ((memq symbol '(newsticker-url-list-defaults newsticker-url-list))
139 (dolist (elt value)
140 (unless (member elt (symbol-value symbol))
141 (setq new-or-changed-feeds (cons elt new-or-changed-feeds))))
142 (dolist (elt (symbol-value symbol))
143 (unless (member elt value)
144 (setq removed-feeds (cons elt removed-feeds))))))
145 (cond (need-restart
146 (set symbol value)
147 (when (newsticker-running-p)
148 (message "Restarting newsticker")
149 (newsticker-stop)
150 (newsticker-start)))
152 (dolist (feed removed-feeds)
153 (message "Stopping feed `%s'" (car feed))
154 (newsticker--stop-feed (car feed)))
155 (dolist (feed new-or-changed-feeds)
156 (message "Starting feed `%s'" (car feed))
157 (newsticker--stop-feed (car feed))
158 (newsticker--start-feed feed))
159 (unless new-or-changed-feeds
160 (when newsticker--sentinel-callback
161 (funcall newsticker--sentinel-callback)))))
162 (set symbol value))))
164 ;; ======================================================================
165 ;; retrieval
166 (defgroup newsticker-retrieval nil
167 "Settings for news retrieval."
168 :group 'newsticker)
170 (defcustom newsticker-url-list-defaults
171 '(("Emacs Wiki"
172 "http://www.emacswiki.org/cgi-bin/wiki.pl?action=rss"
174 3600))
175 "A customizable list of news feeds to select from.
176 These were mostly extracted from the Radio Community Server at
177 http://subhonker6.userland.com/rcsPublic/rssHotlist.
179 You may add other entries in `newsticker-url-list'."
180 :type `(set ,@(mapcar `newsticker--splicer
181 newsticker--raw-url-list-defaults))
182 :set 'newsticker--set-customvar-retrieval
183 :group 'newsticker-retrieval)
185 (defcustom newsticker-url-list nil
186 "The news feeds which you like to watch.
188 This alist will be used in addition to selection made customizing
189 `newsticker-url-list-defaults'.
191 This is an alist. Each element consists of two items: a LABEL and a URL,
192 optionally followed by a START-TIME, INTERVAL specifier and WGET-ARGUMENTS.
194 The LABEL gives the name of the news feed. It can be an arbitrary string.
196 The URL gives the location of the news feed. It must point to a valid
197 RSS or Atom file. The file is retrieved by calling wget, or whatever you
198 specify as `newsticker-wget-name'.
200 URL may also be a function which returns news data. In this case
201 `newsticker-retrieval-method' etc. are ignored for this feed.
203 The START-TIME can be either a string, or nil. If it is a string it
204 specifies a fixed time at which this feed shall be retrieved for the
205 first time. (Examples: \"11:00pm\", \"23:00\".) If it is nil (or
206 unspecified), this feed will be retrieved immediately after calling
207 `newsticker-start'.
209 The INTERVAL specifies the time between retrievals for this feed. If it
210 is nil (or unspecified) the default interval value as set in
211 `newsticker-retrieval-interval' is used.
213 \(newsticker.el calls `run-at-time'. The newsticker-parameters START-TIME
214 and INTERVAL correspond to the `run-at-time'-parameters TIME and REPEAT.)
216 WGET-ARGUMENTS specifies arguments for wget (see `newsticker-wget-name')
217 which apply for this feed only, overriding the value of
218 `newsticker-wget-arguments'."
219 :type '(repeat (list :tag "News feed"
220 (string :tag "Label")
221 (choice :tag "URI"
222 (string :tag "String")
223 (function :tag "Function"))
224 (choice :tag "Start"
225 (const :tag "Default" nil)
226 (string :tag "Fixed Time"))
227 (choice :tag "Interval"
228 (const :tag "Default" nil)
229 (const :tag "Hourly" 3600)
230 (const :tag "Daily" 86400)
231 (const :tag "Weekly" 604800)
232 (integer :tag "Interval"))
233 (choice :tag "Wget Arguments"
234 (const :tag "Default arguments" nil)
235 (repeat :tag "Special arguments" string))))
236 :set 'newsticker--set-customvar-retrieval
237 :group 'newsticker-retrieval)
239 (defcustom newsticker-retrieval-method
240 'intern
241 "Method for retrieving news from the web, either `intern' or `extern'.
242 Default value `intern' uses Emacs' built-in asynchronous download
243 capabilities ('url-retrieve'). If set to `extern' the external
244 program wget is used, see `newsticker-wget-name'."
245 :type '(choice :tag "Method"
246 (const :tag "Intern" intern)
247 (const :tag "Extern" extern))
248 :group 'newsticker-retrieval)
250 (defcustom newsticker-wget-name
251 "wget"
252 "Name of the program which is called to retrieve news from the web.
253 The canonical choice is wget but you may take any other program which is
254 able to return the contents of a news feed file on stdout."
255 :type 'string
256 :group 'newsticker-retrieval)
258 (defcustom newsticker-wget-arguments
259 '("-q" "-O" "-")
260 "Arguments which are passed to wget.
261 There is probably no reason to change the default settings, unless you
262 are living behind a firewall."
263 :type '(repeat (string :tag "Argument"))
264 :group 'newsticker-retrieval)
266 (defcustom newsticker-retrieval-interval
267 3600
268 "Time interval for retrieving new news items (seconds).
269 If this value is not positive (i.e. less than or equal to 0)
270 items are retrieved only once!
271 Please note that some feeds, e.g. Slashdot, will ban you if you
272 make it less than 1800 seconds (30 minutes)!"
273 :type '(choice :tag "Interval"
274 (const :tag "No automatic retrieval" 0)
275 (const :tag "Hourly" 3600)
276 (const :tag "Daily" 86400)
277 (const :tag "Weekly" 604800)
278 (integer :tag "Interval"))
279 :set 'newsticker--set-customvar-retrieval
280 :group 'newsticker-retrieval)
282 (defcustom newsticker-desc-comp-max
284 "Relevant length of headline descriptions.
285 This value gives the maximum number of characters which will be
286 taken into account when newsticker compares two headline
287 descriptions."
288 :type 'integer
289 :group 'newsticker-retrieval)
291 ;; ======================================================================
292 ;; headline processing
293 (defgroup newsticker-headline-processing nil
294 "Settings for the automatic processing of headlines."
295 :group 'newsticker)
297 (defcustom newsticker-automatically-mark-items-as-old
299 "Decides whether to automatically mark items as old.
300 If t a new item is considered as new only after its first retrieval. As
301 soon as it is retrieved a second time, it becomes old. If not t all
302 items stay new until you mark them as old. This is done in the
303 *newsticker* buffer."
304 :type 'boolean
305 :group 'newsticker-headline-processing)
307 (defcustom newsticker-automatically-mark-visited-items-as-old
309 "Decides whether to automatically mark visited items as old.
310 If t an item is marked as old as soon as the associated link is
311 visited, i.e. after pressing RET or mouse2 on the item's
312 headline."
314 :type 'boolean
315 :group 'newsticker-headline-processing)
317 (defcustom newsticker-keep-obsolete-items
319 "Decides whether to keep unread items which have been removed from feed.
320 If t a new item, which has been removed from the feed, is kept in
321 the cache until it is marked as read."
322 :type 'boolean
323 :group 'newsticker-headline-processing)
325 (defcustom newsticker-obsolete-item-max-age
326 (* 60 60 24)
327 "Maximal age of obsolete items, in seconds.
328 Obsolete items which are older than this value will be silently
329 deleted at the next retrieval."
330 :type 'integer
331 :group 'newsticker-headline-processing)
333 (defcustom newsticker-auto-mark-filter-list
335 "A list of filters for automatically marking headlines.
337 This is an alist of the form (FEED-NAME PATTERN-LIST). I.e. each
338 element consists of a FEED-NAME a PATTERN-LIST. Each element of
339 the pattern-list has the form (AGE TITLE-OR-DESCRIPTION REGEXP).
340 AGE must be one of the symbols 'old or 'immortal.
341 TITLE-OR-DESCRIPTION must be on of the symbols 'title,
342 'description, or 'all. REGEXP is a regular expression, i.e. a
343 string.
345 This filter is checked after a new headline has been retrieved.
346 If FEED-NAME matches the name of the corresponding news feed, the
347 pattern-list is checked: The new headline will be marked as AGE
348 if REGEXP matches the headline's TITLE-OR-DESCRIPTION.
350 If, for example, `newsticker-auto-mark-filter-list' looks like
351 \((slashdot ('old 'title \"^Forget me!$\") ('immortal 'title \"Read me\")
352 \('immortal 'all \"important\"))))
354 then all articles from slashdot are marked as old if they have
355 the title \"Forget me!\". All articles with a title containing
356 the string \"Read me\" are marked as immortal. All articles which
357 contain the string \"important\" in their title or their
358 description are marked as immortal."
359 :type '(repeat (list :tag "Auto mark filter"
360 (string :tag "Feed name")
361 (repeat
362 (list :tag "Filter element"
363 (choice
364 :tag "Auto-assigned age"
365 (const :tag "Old" old)
366 (const :tag "Immortal" immortal))
367 (choice
368 :tag "Title/Description"
369 (const :tag "Title" title)
370 (const :tag "Description" description)
371 (const :tag "All" all))
372 (string :tag "Regexp")))))
373 :group 'newsticker-headline-processing)
375 ;; ======================================================================
376 ;; hooks
377 (defgroup newsticker-hooks nil
378 "Settings for newsticker hooks."
379 :group 'newsticker)
381 (defcustom newsticker-start-hook
383 "Hook run when starting newsticker.
384 This hook is run at the very end of `newsticker-start'."
385 :options '(newsticker-start-ticker)
386 :type 'hook
387 :group 'newsticker-hooks)
389 (defcustom newsticker-stop-hook
391 "Hook run when stopping newsticker.
392 This hook is run at the very end of `newsticker-stop'."
393 :options nil
394 :type 'hook
395 :group 'newsticker-hooks)
397 (defcustom newsticker-new-item-functions
399 "List of functions run after a new headline has been retrieved.
400 Each function is called with the following three arguments:
401 FEED the name of the corresponding news feed,
402 TITLE the title of the headline,
403 DESC the decoded description of the headline.
405 See `newsticker-download-images', and
406 `newsticker-download-enclosures' for sample functions.
408 Please note that these functions are called only once for a
409 headline after it has been retrieved for the first time."
410 :type 'hook
411 :options '(newsticker-download-images
412 newsticker-download-enclosures)
413 :group 'newsticker-hooks)
415 ;; ======================================================================
416 ;; miscellaneous
417 (defgroup newsticker-miscellaneous nil
418 "Miscellaneous newsticker settings."
419 :group 'newsticker)
421 (defcustom newsticker-cache-filename
422 "~/.newsticker-cache"
423 "Name of the newsticker cache file."
424 :type 'string
425 :group 'newsticker-miscellaneous)
427 (defcustom newsticker-imagecache-dirname
428 "~/.newsticker-images"
429 "Name of the directory where newsticker stores cached images."
430 :type 'string
431 :group 'newsticker-miscellaneous)
433 ;; debugging
434 (defcustom newsticker-debug
436 "Enables some features needed for debugging newsticker.el.
438 If set to t newsticker.el will print lots of debugging messages, and the
439 buffers *newsticker-wget-<feed>* will not be closed."
440 :type 'boolean
441 :group 'newsticker-miscellaneous)
443 ;; ======================================================================
444 ;;; Compatibility section, XEmacs, Emacs
445 ;; ======================================================================
446 (unless (fboundp 'time-add)
447 (require 'time-date);;FIXME
448 (defun time-add (t1 t2)
449 (seconds-to-time (+ (time-to-seconds t1) (time-to-seconds t2)))))
451 (unless (fboundp 'match-string-no-properties)
452 (defalias 'match-string-no-properties 'match-string))
454 (when (featurep 'xemacs)
455 (unless (fboundp 'replace-regexp-in-string)
456 (defun replace-regexp-in-string (re rp st)
457 (save-match-data ;; apparently XEmacs needs save-match-data
458 (replace-in-string st re rp)))))
460 ;; copied from subr.el
461 (unless (fboundp 'add-to-invisibility-spec)
462 (defun add-to-invisibility-spec (arg)
463 "Add elements to `buffer-invisibility-spec'.
464 See documentation for `buffer-invisibility-spec' for the kind of elements
465 that can be added."
466 (if (eq buffer-invisibility-spec t)
467 (setq buffer-invisibility-spec (list t)))
468 (setq buffer-invisibility-spec
469 (cons arg buffer-invisibility-spec))))
471 ;; copied from subr.el
472 (unless (fboundp 'remove-from-invisibility-spec)
473 (defun remove-from-invisibility-spec (arg)
474 "Remove elements from `buffer-invisibility-spec'."
475 (if (consp buffer-invisibility-spec)
476 (setq buffer-invisibility-spec
477 (delete arg buffer-invisibility-spec)))))
479 ;; ======================================================================
480 ;;; Internal variables
481 ;; ======================================================================
482 (defvar newsticker--item-list nil
483 "List of newsticker items.")
484 (defvar newsticker--item-position 0
485 "Actual position in list of newsticker items.")
486 (defvar newsticker--prev-message "There was no previous message yet!"
487 "Last message that the newsticker displayed.")
488 (defvar newsticker--scrollable-text ""
489 "The text which is scrolled smoothly in the echo area.")
490 (defvar newsticker--buffer-uptodate-p nil
491 "Tells whether the newsticker buffer is up to date.")
492 (defvar newsticker--latest-update-time (current-time)
493 "The time at which the latest news arrived.")
494 (defvar newsticker--process-ids nil
495 "List of PIDs of active newsticker processes.")
497 (defvar newsticker--cache nil "Cached newsticker data.
498 This is a list of the form
500 ((label1
501 (title description link time age index preformatted-contents
502 preformatted-title extra-elements)
503 ...)
504 (label2
505 (title description link time age index preformatted-contents
506 preformatted-title extra-elements)
507 ...)
508 ...)
510 where LABEL is a symbol. TITLE, DESCRIPTION, and LINK are
511 strings. TIME is a time value as returned by `current-time'.
512 AGE is a symbol: 'new, 'old, 'immortal, and 'obsolete denote
513 ordinary news items, whereas 'feed denotes an item which is not a
514 headline but describes the feed itself. INDEX denotes the
515 original position of the item -- used for restoring the original
516 order. PREFORMATTED-CONTENTS and PREFORMATTED-TITLE hold the
517 formatted contents of the item's description and title. This
518 speeds things up if HTML rendering is used, which is rather
519 slow. EXTRA-ELEMENTS is an alist containing additional elements.")
521 (defvar newsticker--auto-narrow-to-feed nil
522 "Automatically narrow to current news feed.
523 If non-nil only the items of the current news feed are visible.")
525 (defvar newsticker--auto-narrow-to-item nil
526 "Automatically narrow to current news item.
527 If non-nil only the current headline is visible.")
529 (defconst newsticker--error-headline
530 "[COULD NOT DOWNLOAD HEADLINES!]"
531 "Title of error headline which will be inserted if news retrieval fails.")
533 ;; ======================================================================
534 ;;; Shortcuts
535 ;; ======================================================================
536 (defsubst newsticker--title (item)
537 "Return title of ITEM."
538 (nth 0 item))
539 (defsubst newsticker--desc (item)
540 "Return description of ITEM."
541 (nth 1 item))
542 (defsubst newsticker--link (item)
543 "Return link of ITEM."
544 (nth 2 item))
545 (defsubst newsticker--time (item)
546 "Return time of ITEM."
547 (nth 3 item))
548 (defsubst newsticker--age (item)
549 "Return age of ITEM."
550 (nth 4 item))
551 (defsubst newsticker--pos (item)
552 "Return position/index of ITEM."
553 (nth 5 item))
554 (defsubst newsticker--preformatted-contents (item)
555 "Return pre-formatted text of ITEM."
556 (nth 6 item))
557 (defsubst newsticker--preformatted-title (item)
558 "Return pre-formatted title of ITEM."
559 (nth 7 item))
560 (defsubst newsticker--extra (item)
561 "Return extra attributes of ITEM."
562 (nth 8 item))
563 (defsubst newsticker--guid-to-string (guid)
564 "Return string representation of GUID."
565 (if (stringp guid)
566 guid
567 (car (xml-node-children guid))))
568 (defsubst newsticker--guid (item)
569 "Return guid of ITEM."
570 (newsticker--guid-to-string (assoc 'guid (newsticker--extra item))))
571 (defsubst newsticker--enclosure (item)
572 "Return enclosure element of ITEM in the form \(...FIXME...\) or nil."
573 (let ((enclosure (assoc 'enclosure (newsticker--extra item))))
574 (if enclosure
575 (xml-node-attributes enclosure))))
576 (defun newsticker--real-feed-name (feed)
577 "Return real name of FEED."
578 (catch 'name
579 (mapc (lambda (item)
580 (if (eq (newsticker--age item) 'feed)
581 (throw 'name (newsticker--title item))))
582 (cdr (newsticker--cache-get-feed feed)))
583 (symbol-name feed)))
586 ;; ======================================================================
587 ;;; User fun
588 ;; ======================================================================
590 (defun newsticker--start-feed (feed &optional do-not-complain-if-running)
591 "Start retrieval timer for FEED.
592 If timer is running already a warning message is printed unless
593 DO-NOT-COMPLAIN-IF-RUNNING is not nil. Add the started
594 name/timer pair to `newsticker--retrieval-timer-list'."
595 (let* ((feed-name (car feed))
596 (start-time (nth 2 feed))
597 (interval (or (nth 3 feed)
598 newsticker-retrieval-interval))
599 (timer (assoc (car feed)
600 newsticker--retrieval-timer-list)))
601 (if timer
602 (or do-not-complain-if-running
603 (message "Timer for %s is running already!"
604 feed-name))
605 (newsticker--debug-msg "Starting timer for %s: %s, %d"
606 feed-name start-time interval)
607 ;; do not repeat retrieval if interval not positive
608 (if (<= interval 0)
609 (setq interval nil))
610 ;; Suddenly XEmacs doesn't like start-time 0
611 (if (or (not start-time)
612 (and (numberp start-time) (= start-time 0)))
613 (setq start-time 1))
614 ;; (message "start-time %s" start-time)
615 (setq timer (run-at-time start-time interval
616 'newsticker-get-news feed-name))
617 (if interval
618 (add-to-list 'newsticker--retrieval-timer-list
619 (cons feed-name timer))))))
621 ;;;###autoload
622 (defun newsticker-start (&optional do-not-complain-if-running)
623 "Start the newsticker.
624 Start the timers for display and retrieval. If the newsticker, i.e. the
625 timers, are running already a warning message is printed unless
626 DO-NOT-COMPLAIN-IF-RUNNING is not nil.
627 Run `newsticker-start-hook' if newsticker was not running already."
628 (interactive)
629 (let ((running (newsticker-running-p)))
630 ;; read old cache if it exists and newsticker is not running
631 (unless running
632 (let ((coding-system-for-read 'utf-8))
633 (when (file-exists-p newsticker-cache-filename)
634 (with-temp-buffer
635 (insert-file-contents newsticker-cache-filename)
636 (goto-char (point-min))
637 (condition-case nil
638 (setq newsticker--cache (read (current-buffer)))
639 (error
640 (message "Error while reading newsticker cache file!")
641 (setq newsticker--cache nil)))))))
642 ;; start retrieval timers -- one timer for each feed
643 (dolist (feed (append newsticker-url-list-defaults newsticker-url-list))
644 (newsticker--start-feed feed))
645 (unless running
646 (run-hooks 'newsticker-start-hook)
647 (message "Newsticker started!"))))
649 (defun newsticker--stop-feed (feed-name)
650 "Stop retrieval for feed FEED-NAME.
651 Delete the stopped name/timer pair from `newsticker--retrieval-timer-list'."
652 (let ((name-and-timer (assoc feed-name newsticker--retrieval-timer-list)))
653 (when name-and-timer
654 (cancel-timer (cdr name-and-timer))
655 (setq newsticker--retrieval-timer-list
656 (delete name-and-timer newsticker--retrieval-timer-list)))))
658 (defun newsticker-stop ()
659 "Stop the newsticker and the newsticker-ticker.
660 Cancel the timers for display and retrieval. Run `newsticker-stop-hook'
661 if newsticker has been running."
662 (interactive)
663 (newsticker--cache-update t)
664 (when (fboundp 'newsticker-stop-ticker) ; silence compiler warnings
665 (newsticker-stop-ticker))
666 (when (newsticker-running-p)
667 (mapc (lambda (name-and-timer)
668 (newsticker--stop-feed (car name-and-timer)))
669 newsticker--retrieval-timer-list)
670 (setq newsticker--retrieval-timer-list nil)
671 (run-hooks 'newsticker-stop-hook)
672 (message "Newsticker stopped!")))
674 (defun newsticker-get-all-news ()
675 "Launch retrieval of news from all configured newsticker sites.
676 This does NOT start the retrieval timers."
677 (interactive)
678 ;; launch retrieval of news
679 (mapc (lambda (item)
680 (newsticker-get-news (car item)))
681 (append newsticker-url-list-defaults newsticker-url-list)))
683 (defun newsticker-save-item (feed item)
684 "Save FEED ITEM."
685 (interactive)
686 (let ((filename (read-string "Filename: "
687 (concat feed ":_"
688 (replace-regexp-in-string
689 " " "_" (newsticker--title item))
690 ".html"))))
691 (with-temp-buffer
692 (insert (newsticker--desc item))
693 (write-file filename t))))
695 (defun newsticker-add-url (url name)
696 "Add given URL under given NAME to `newsticker-url-list'.
697 If URL is nil it is searched at point."
698 (interactive
699 (list
700 (read-string "URL: "
701 (save-excursion
702 (end-of-line)
703 (and
704 (re-search-backward
705 "http://"
706 (if (> (point) (+ (point-min) 100))
707 (- (point) 100)
708 (point-min))
710 (re-search-forward
711 "http://[-a-zA-Z0-9&/_.]*"
712 (if (< (point) (- (point-max) 200))
713 (+ (point) 200)
714 (point-max))
716 (buffer-substring-no-properties (match-beginning 0)
717 (match-end 0)))))
718 (read-string "Name: ")))
719 (add-to-list 'newsticker-url-list (list name url nil nil nil) t)
720 (customize-variable 'newsticker-url-list))
722 (defun newsticker-customize ()
723 "Open the newsticker customization group."
724 (interactive)
725 (customize-group "newsticker"))
727 ;; ======================================================================
728 ;;; Local stuff
729 ;; ======================================================================
730 (defun newsticker--get-news-by-funcall (feed-name function)
731 "Get news for the site FEED-NAME by calling FUNCTION.
732 See `newsticker-get-news'."
733 (let ((buffername (concat " *newsticker-funcall-" feed-name "*")))
734 (save-excursion
735 (set-buffer (get-buffer-create buffername))
736 (erase-buffer)
737 (insert (string-to-multibyte (funcall function feed-name)))
738 (newsticker--sentinel-work nil t feed-name function
739 (current-buffer)))))
741 (defun newsticker--get-news-by-url (feed-name url)
742 "Get news for the site FEED-NAME from address URL using `url-retrieve'.
743 See `newsticker-get-news'."
744 (let ((coding-system-for-read 'no-conversion))
745 (condition-case error-data
746 (url-retrieve url 'newsticker--get-news-by-url-callback
747 (list feed-name))
748 (error (message "Error retrieving news from %s: %s" feed-name
749 error-data))))
750 (force-mode-line-update))
752 (defun newsticker--get-news-by-url-callback (status feed-name)
753 "Callback function for `newsticker--get-news-by-url'.
754 STATUS is the return status as delivered by `url-retrieve', and
755 FEED-NAME is the name of the feed that the news were retrieved
756 from."
757 (let ((buf (get-buffer-create (concat " *newsticker-url-" feed-name "*")))
758 (result (string-to-multibyte (buffer-string))))
759 (set-buffer buf)
760 (erase-buffer)
761 (insert result)
762 ;; remove MIME header
763 (goto-char (point-min))
764 (search-forward "\n\n")
765 (delete-region (point-min) (point))
766 ;; read the rss/atom contents
767 (newsticker--sentinel-work nil t feed-name "url-retrieve" (current-buffer))
768 (when status
769 (let ((status-type (car status))
770 (status-details (cdr status)))
771 (cond ((eq status-type :redirect)
772 ;; don't care about redirects
774 ((eq status-type :error)
775 (message "%s: Error while retrieving news from %s: %s: \"%s\""
776 (format-time-string "%A, %H:%M" (current-time))
777 feed-name
778 (car status-details) (cdr status-details))))))))
780 (defun newsticker--get-news-by-wget (feed-name url wget-arguments)
781 "Get news for the site FEED-NAME from address URL using wget.
782 WGET-ARGUMENTS is a list of arguments for wget.
783 See `newsticker-get-news'."
784 (let ((buffername (concat " *newsticker-wget-" feed-name "*")))
785 (save-excursion
786 (set-buffer (get-buffer-create buffername))
787 (erase-buffer)
788 ;; throw an error if there is an old wget-process around
789 (if (get-process feed-name)
790 (error "Another wget-process is running for %s" feed-name))
791 ;; start wget
792 (let* ((args (append wget-arguments (list url)))
793 (proc (apply 'start-process feed-name buffername
794 newsticker-wget-name args)))
795 (set-process-coding-system proc 'no-conversion 'no-conversion)
796 (set-process-sentinel proc 'newsticker--sentinel)
797 (setq newsticker--process-ids (cons (process-id proc)
798 newsticker--process-ids))
799 (force-mode-line-update)))))
801 (defun newsticker-get-news (feed-name)
802 "Get news from the site FEED-NAME and load feed logo.
803 FEED-NAME must be a string which occurs as the label (i.e. the first element)
804 in an element of `newsticker-url-list' or `newsticker-url-list-defaults'."
805 (newsticker--debug-msg "%s: Getting news for %s"
806 (format-time-string "%A, %H:%M" (current-time))
807 feed-name)
808 (let* ((item (or (assoc feed-name newsticker-url-list)
809 (assoc feed-name newsticker-url-list-defaults)
810 (error
811 "Cannot get news for %s: Check newsticker-url-list"
812 feed-name)))
813 (url (cadr item))
814 (wget-arguments (or (car (cdr (cdr (cdr (cdr item)))))
815 newsticker-wget-arguments)))
816 (if (functionp url)
817 (newsticker--get-news-by-funcall feed-name url)
818 (if (eq newsticker-retrieval-method 'intern)
819 (newsticker--get-news-by-url feed-name url)
820 (newsticker--get-news-by-wget feed-name url wget-arguments)))))
822 ;; ======================================================================
823 ;; Parsing
824 ;; ======================================================================
826 (defun newsticker--sentinel (process event)
827 "Sentinel for extracting news titles from an RDF buffer.
828 Argument PROCESS is the process which has just changed its state.
829 Argument EVENT tells what has happened to the process."
830 (let ((p-status (process-status process))
831 (exit-status (process-exit-status process))
832 (name (process-name process))
833 (command (process-command process))
834 (buffer (process-buffer process)))
835 (newsticker--sentinel-work event
836 (and (eq p-status 'exit)
837 (= exit-status 0))
838 name command buffer)))
840 (defun newsticker--sentinel-work (event status-ok name command buffer)
841 "Actually do the sentinel work.
842 Argument EVENT tells what has happened to the retrieval process.
843 Argument STATUS-OK is the final status of the retrieval process,
844 non-nil meaning retrieval was successful.
845 Argument NAME is the name of the retrieval process.
846 Argument COMMAND is the command of the retrieval process.
847 Argument BUFFER is the buffer of the retrieval process."
848 (let ((time (current-time))
849 (name-symbol (intern name))
850 (something-was-added nil))
851 ;; catch known errors (zombie processes, rubbish-xml etc.
852 ;; if an error occurs the news feed is not updated!
853 (catch 'oops
854 (unless status-ok
855 (setq newsticker--cache
856 (newsticker--cache-add
857 newsticker--cache
858 name-symbol
859 newsticker--error-headline
860 (format
861 (concat "%s: Newsticker could not retrieve news from %s.\n"
862 "Return status: `%s'\n"
863 "Command was `%s'")
864 (format-time-string "%A, %H:%M" (current-time))
865 name event command)
867 (current-time)
868 'new
869 0 nil))
870 (message "%s: Error while retrieving news from %s"
871 (format-time-string "%A, %H:%M" (current-time))
872 name)
873 (throw 'oops nil))
874 (let* ((coding-system 'utf-8)
875 (node-list
876 (save-current-buffer
877 (set-buffer buffer)
878 ;; a very very dirty workaround to overcome the
879 ;; problems with the newest (20030621) xml.el:
880 ;; remove all unnecessary whitespace
881 (goto-char (point-min))
882 (while (re-search-forward ">[ \t\r\n]+<" nil t)
883 (replace-match "><" nil t))
884 ;; and another brutal workaround (20031105)! For some
885 ;; reason the xml parser does not like the colon in the
886 ;; doctype name "rdf:RDF"
887 (goto-char (point-min))
888 (if (re-search-forward "<!DOCTYPE[ \t\n]+rdf:RDF" nil t)
889 (replace-match "<!DOCTYPE rdfColonRDF" nil t))
890 ;; finally.... ~##^°!!!!!
891 (goto-char (point-min))
892 (while (search-forward "\r\n" nil t)
893 (replace-match "\n" nil t))
894 ;; still more brutal workarounds (20040309)! The xml
895 ;; parser does not like doctype rss
896 (goto-char (point-min))
897 (if (re-search-forward "<!DOCTYPE[ \t\n]+rss[ \t\n]*>" nil t)
898 (replace-match "" nil t))
899 ;; And another one (20050618)! (Fixed in GNU Emacs 22.0.50.18)
900 ;; Remove comments to avoid this xml-parsing bug:
901 ;; "XML files can have only one toplevel tag"
902 (goto-char (point-min))
903 (while (search-forward "<!--" nil t)
904 (let ((start (match-beginning 0)))
905 (unless (search-forward "-->" nil t)
906 (error "Can't find end of comment"))
907 (delete-region start (point))))
908 ;; And another one (20050702)! If description is HTML
909 ;; encoded and starts with a `<', wrap the whole
910 ;; description in a CDATA expression. This happened for
911 ;; http://www.thefreedictionary.com/_/WoD/rss.aspx?type=quote
912 (goto-char (point-min))
913 (while (re-search-forward
914 "<description>\\(<img.*?\\)</description>" nil t)
915 (replace-match
916 "<description><![CDATA[ \\1 ]]></description>"))
917 ;; And another one (20051123)! XML parser does not
918 ;; like this: <yweather:location city="Frankfurt/Main"
919 ;; region="" country="GM" />
920 ;; try to "fix" empty attributes
921 ;; This happened for
922 ;; http://xml.weather.yahoo.com/forecastrss?p=GMXX0040&u=f
923 (goto-char (point-min))
924 (while (re-search-forward "\\(<[^>]*\\)=\"\"" nil t)
925 (replace-match "\\1=\" \""))
927 (set-buffer-modified-p nil)
928 ;; check coding system
929 (goto-char (point-min))
930 (if (re-search-forward "encoding=\"\\([^\"]+\\)\""
931 nil t)
932 (setq coding-system (intern (downcase (match-string 1))))
933 (setq coding-system
934 (condition-case nil
935 (check-coding-system coding-system)
936 (coding-system-error
937 (message
938 "newsticker.el: ignoring coding system %s for %s"
939 coding-system name)
940 nil))))
941 ;; Decode if possible
942 (when coding-system
943 (decode-coding-region (point-min) (point-max)
944 coding-system))
945 (condition-case errordata
946 ;; The xml parser might fail
947 ;; or the xml might be bugged
948 (xml-parse-region (point-min) (point-max))
949 (error (message "Could not parse %s: %s"
950 (buffer-name) (cadr errordata))
951 (throw 'oops nil)))))
952 (topnode (car node-list))
953 (channelnode (car (xml-get-children topnode 'channel)))
954 (imageurl nil))
955 ;; mark all items as obsolete
956 (newsticker--cache-replace-age newsticker--cache
957 name-symbol
958 'new 'obsolete-new)
959 (newsticker--cache-replace-age newsticker--cache
960 name-symbol
961 'old 'obsolete-old)
962 (newsticker--cache-replace-age newsticker--cache
963 name-symbol
964 'feed 'obsolete-old)
966 ;; check Atom/RSS version and call corresponding parser
967 (condition-case error-data
968 (if (cond
969 ;; RSS 0.91
970 ((and (eq 'rss (xml-node-name topnode))
971 (string= "0.91" (xml-get-attribute topnode 'version)))
972 (setq imageurl (newsticker--get-logo-url-rss-0.91 topnode))
973 (newsticker--parse-rss-0.91 name time topnode))
974 ;; RSS 0.92
975 ((and (eq 'rss (xml-node-name topnode))
976 (string= "0.92" (xml-get-attribute topnode 'version)))
977 (setq imageurl (newsticker--get-logo-url-rss-0.92 topnode))
978 (newsticker--parse-rss-0.92 name time topnode))
979 ;; RSS 1.0
980 ((eq 'rdf:RDF (xml-node-name topnode))
981 (setq imageurl (newsticker--get-logo-url-rss-1.0 topnode))
982 (newsticker--parse-rss-1.0 name time topnode))
983 ;; RSS 2.0
984 ((and (eq 'rss (xml-node-name topnode))
985 (string= "2.0" (xml-get-attribute topnode 'version)))
986 (setq imageurl (newsticker--get-logo-url-rss-2.0 topnode))
987 (newsticker--parse-rss-2.0 name time topnode))
988 ;; Atom 0.3
989 ((and (eq 'feed (xml-node-name topnode))
990 (string= "http://purl.org/atom/ns#"
991 (xml-get-attribute topnode 'xmlns)))
992 (setq imageurl (newsticker--get-logo-url-atom-0.3 topnode))
993 (newsticker--parse-atom-0.3 name time topnode))
994 ;; Atom 1.0
995 ((and (eq 'feed (xml-node-name topnode))
996 (string= "http://www.w3.org/2005/Atom"
997 (xml-get-attribute topnode 'xmlns)))
998 (setq imageurl (newsticker--get-logo-url-atom-1.0 topnode))
999 (newsticker--parse-atom-1.0 name time topnode))
1000 ;; unknown feed type
1002 (newsticker--debug-msg "Feed type unknown: %s: %s"
1003 (xml-node-name topnode) name)
1004 nil))
1005 (setq something-was-added t))
1006 (error (message "sentinelerror in %s: %s" name error-data)))
1008 ;; Remove those old items from cache which have been removed from
1009 ;; the feed
1010 (newsticker--cache-replace-age newsticker--cache
1011 name-symbol 'obsolete-old 'deleteme)
1012 (newsticker--cache-remove newsticker--cache name-symbol
1013 'deleteme)
1014 ;; Remove those new items from cache which have been removed from
1015 ;; the feed. Or keep them as `obsolete'
1016 (if (not newsticker-keep-obsolete-items)
1017 (newsticker--cache-remove newsticker--cache
1018 name-symbol 'obsolete-new)
1019 (setq newsticker--cache
1020 (newsticker--cache-mark-expired
1021 newsticker--cache name-symbol 'obsolete 'obsolete-expired
1022 newsticker-obsolete-item-max-age))
1023 (newsticker--cache-remove newsticker--cache
1024 name-symbol 'obsolete-expired)
1025 (newsticker--cache-replace-age newsticker--cache
1026 name-symbol 'obsolete-new
1027 'obsolete))
1028 (newsticker--update-process-ids)
1029 ;; setup scrollable text
1030 (when (= 0 (length newsticker--process-ids))
1031 (when (fboundp 'newsticker--ticker-text-setup) ;silence
1032 ;compiler
1033 ;warnings
1034 (newsticker--ticker-text-setup)))
1035 (setq newsticker--latest-update-time (current-time))
1036 (when something-was-added
1037 ;; FIXME: should we care about removed items as well?
1038 (newsticker--cache-update)
1039 (when (fboundp 'newsticker--buffer-set-uptodate) ;silence
1040 ;compiler
1041 ;warnings
1042 (newsticker--buffer-set-uptodate nil)))
1043 ;; kill the process buffer if wanted
1044 (unless newsticker-debug
1045 (kill-buffer buffer))
1046 ;; launch retrieval of image
1047 (when (and imageurl newsticker--download-logos)
1048 (newsticker--image-get name imageurl)))))
1049 (when newsticker--sentinel-callback
1050 (funcall newsticker--sentinel-callback)))
1052 (defun newsticker--get-logo-url-atom-1.0 (node)
1053 "Return logo URL from atom 1.0 data in NODE."
1054 (car (xml-node-children
1055 (car (xml-get-children node 'logo)))))
1057 (defun newsticker--get-logo-url-atom-0.3 (node)
1058 "Return logo URL from atom 0.3 data in NODE."
1059 (car (xml-node-children
1060 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1062 (defun newsticker--get-logo-url-rss-2.0 (node)
1063 "Return logo URL from RSS 2.0 data in NODE."
1064 (car (xml-node-children
1065 (car (xml-get-children
1066 (car (xml-get-children
1067 (car (xml-get-children node 'channel)) 'image)) 'url)))))
1069 (defun newsticker--get-logo-url-rss-1.0 (node)
1070 "Return logo URL from RSS 1.0 data in NODE."
1071 (car (xml-node-children
1072 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1074 (defun newsticker--get-logo-url-rss-0.92 (node)
1075 "Return logo URL from RSS 0.92 data in NODE."
1076 (car (xml-node-children
1077 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1079 (defun newsticker--get-logo-url-rss-0.91 (node)
1080 "Return logo URL from RSS 0.91 data in NODE."
1081 (car (xml-node-children
1082 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1084 (defun newsticker--parse-atom-0.3 (name time topnode)
1085 "Parse Atom 0.3 data.
1086 Return value as well as arguments NAME, TIME, and TOPNODE are the
1087 same as in `newsticker--parse-atom-1.0'."
1088 (newsticker--debug-msg "Parsing Atom 0.3 feed %s" name)
1089 (let (new-feed new-item)
1090 (setq new-feed (newsticker--parse-generic-feed
1091 name time
1092 ;; title
1093 (car (xml-node-children
1094 (car (xml-get-children topnode 'title))))
1095 ;; desc
1096 (car (xml-node-children
1097 (car (xml-get-children topnode 'content))))
1098 ;; link
1099 (xml-get-attribute
1100 (car (xml-get-children topnode 'link)) 'href)
1101 ;; extra-elements
1102 (xml-node-children topnode)))
1103 (setq new-item (newsticker--parse-generic-items
1104 name time (xml-get-children topnode 'entry)
1105 ;; title-fn
1106 (lambda (node)
1107 (car (xml-node-children
1108 (car (xml-get-children node 'title)))))
1109 ;; desc-fn
1110 (lambda (node)
1111 (or (car (xml-node-children
1112 (car (xml-get-children node 'content))))
1113 (car (xml-node-children
1114 (car (xml-get-children node 'summary))))))
1115 ;; link-fn
1116 (lambda (node)
1117 (xml-get-attribute
1118 (car (xml-get-children node 'link)) 'href))
1119 ;; time-fn
1120 (lambda (node)
1121 (newsticker--decode-rfc822-date
1122 (car (xml-node-children
1123 (car (xml-get-children node 'modified))))))
1124 ;; guid-fn
1125 (lambda (node)
1126 (newsticker--guid-to-string
1127 (assoc 'guid (xml-node-children node))))
1128 ;; extra-fn
1129 (lambda (node)
1130 (xml-node-children node))))
1131 (or new-item new-feed)))
1133 (defun newsticker--parse-atom-1.0 (name time topnode)
1134 "Parse Atom 1.0 data.
1135 Argument NAME gives the name of a news feed. TIME gives the
1136 system time at which the data have been retrieved. TOPNODE
1137 contains the feed data as returned by the xml parser.
1139 For the Atom 1.0 specification see
1140 http://www.atompub.org/2005/08/17/draft-ietf-atompub-format-11.html"
1141 (newsticker--debug-msg "Parsing Atom 1.0 feed %s" name)
1142 (let (new-feed new-item)
1143 (setq new-feed (newsticker--parse-generic-feed
1144 name time
1145 ;; title
1146 (car (xml-node-children
1147 (car (xml-get-children topnode 'title))))
1148 ;; desc
1149 (car (xml-node-children
1150 (car (xml-get-children topnode 'subtitle))))
1151 ;; link
1152 (lambda (node)
1153 (xml-get-attribute
1154 (car (xml-get-children node 'link)) 'href))
1155 ;; extra-elements
1156 (xml-node-children topnode)))
1157 (setq new-item (newsticker--parse-generic-items
1158 name time (xml-get-children topnode 'entry)
1159 ;; title-fn
1160 (lambda (node)
1161 (car (xml-node-children
1162 (car (xml-get-children node 'title)))))
1163 ;; desc-fn
1164 (lambda (node)
1165 (or (car (xml-node-children
1166 (car (xml-get-children node 'content))))
1167 (car (xml-node-children
1168 (car (xml-get-children node 'summary))))))
1169 ;; link-fn
1170 (lambda (node)
1171 (xml-get-attribute
1172 (car (xml-get-children node 'link)) 'href))
1173 ;; time-fn
1174 (lambda (node)
1175 (newsticker--decode-iso8601-date
1176 (or (car (xml-node-children
1177 (car (xml-get-children node 'updated))))
1178 (car (xml-node-children
1179 (car (xml-get-children node 'published)))))))
1180 ;; guid-fn
1181 (lambda (node)
1182 (car (xml-node-children
1183 (car (xml-get-children node 'id)))))
1184 ;; extra-fn
1185 (lambda (node)
1186 (xml-node-children node))))
1187 (or new-item new-feed)))
1189 (defun newsticker--parse-rss-0.91 (name time topnode)
1190 "Parse RSS 0.91 data.
1191 Return value as well as arguments NAME, TIME, and TOPNODE are the
1192 same as in `newsticker--parse-atom-1.0'.
1194 For the RSS 0.91 specification see http://backend.userland.com/rss091 or
1195 http://my.netscape.com/publish/formats/rss-spec-0.91.html."
1196 (newsticker--debug-msg "Parsing RSS 0.91 feed %s" name)
1197 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1198 (pub-date (newsticker--decode-rfc822-date
1199 (car (xml-node-children
1200 (car (xml-get-children channelnode 'pubDate))))))
1201 is-new-feed has-new-items)
1202 (setq is-new-feed (newsticker--parse-generic-feed
1203 name time
1204 ;; title
1205 (car (xml-node-children
1206 (car (xml-get-children channelnode 'title))))
1207 ;; desc
1208 (car (xml-node-children
1209 (car (xml-get-children channelnode
1210 'description))))
1211 ;; link
1212 (car (xml-node-children
1213 (car (xml-get-children channelnode 'link))))
1214 ;; extra-elements
1215 (xml-node-children channelnode)))
1216 (setq has-new-items (newsticker--parse-generic-items
1217 name time (xml-get-children channelnode 'item)
1218 ;; title-fn
1219 (lambda (node)
1220 (car (xml-node-children
1221 (car (xml-get-children node 'title)))))
1222 ;; desc-fn
1223 (lambda (node)
1224 (car (xml-node-children
1225 (car (xml-get-children node 'description)))))
1226 ;; link-fn
1227 (lambda (node)
1228 (car (xml-node-children
1229 (car (xml-get-children node 'link)))))
1230 ;; time-fn
1231 (lambda (node)
1232 (newsticker--decode-rfc822-date
1233 (car (xml-node-children
1234 (car (xml-get-children node 'pubDate))))))
1235 ;; guid-fn
1236 (lambda (node)
1237 nil)
1238 ;; extra-fn
1239 (lambda (node)
1240 (xml-node-children node))))
1241 (or has-new-items is-new-feed)))
1243 (defun newsticker--parse-rss-0.92 (name time topnode)
1244 "Parse RSS 0.92 data.
1245 Return value as well as arguments NAME, TIME, and TOPNODE are the
1246 same as in `newsticker--parse-atom-1.0'.
1248 For the RSS 0.92 specification see http://backend.userland.com/rss092."
1249 (newsticker--debug-msg "Parsing RSS 0.92 feed %s" name)
1250 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1251 (pub-date (newsticker--decode-rfc822-date
1252 (car (xml-node-children
1253 (car (xml-get-children channelnode 'pubDate))))))
1254 is-new-feed has-new-items)
1255 (setq is-new-feed (newsticker--parse-generic-feed
1256 name time
1257 ;; title
1258 (car (xml-node-children
1259 (car (xml-get-children channelnode 'title))))
1260 ;; desc
1261 (car (xml-node-children
1262 (car (xml-get-children channelnode
1263 'description))))
1264 ;; link
1265 (car (xml-node-children
1266 (car (xml-get-children channelnode 'link))))
1267 ;; extra-elements
1268 (xml-node-children channelnode)))
1269 (setq has-new-items (newsticker--parse-generic-items
1270 name time (xml-get-children channelnode 'item)
1271 ;; title-fn
1272 (lambda (node)
1273 (car (xml-node-children
1274 (car (xml-get-children node 'title)))))
1275 ;; desc-fn
1276 (lambda (node)
1277 (car (xml-node-children
1278 (car (xml-get-children node 'description)))))
1279 ;; link-fn
1280 (lambda (node)
1281 (car (xml-node-children
1282 (car (xml-get-children node 'link)))))
1283 ;; time-fn
1284 (lambda (node)
1285 (newsticker--decode-rfc822-date
1286 (car (xml-node-children
1287 (car (xml-get-children node 'pubDate))))))
1288 ;; guid-fn
1289 (lambda (node)
1290 nil)
1291 ;; extra-fn
1292 (lambda (node)
1293 (xml-node-children node))))
1294 (or has-new-items is-new-feed)))
1296 (defun newsticker--parse-rss-1.0 (name time topnode)
1297 "Parse RSS 1.0 data.
1298 Return value as well as arguments NAME, TIME, and TOPNODE are the
1299 same as in `newsticker--parse-atom-1.0'.
1301 For the RSS 1.0 specification see http://web.resource.org/rss/1.0/spec."
1302 (newsticker--debug-msg "Parsing RSS 1.0 feed %s" name)
1303 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1304 is-new-feed has-new-items)
1305 (setq is-new-feed (newsticker--parse-generic-feed
1306 name time
1307 ;; title
1308 (car (xml-node-children
1309 (car (xml-get-children channelnode 'title))))
1310 ;; desc
1311 (car (xml-node-children
1312 (car (xml-get-children channelnode
1313 'description))))
1314 ;; link
1315 (car (xml-node-children
1316 (car (xml-get-children channelnode 'link))))
1317 ;; extra-elements
1318 (xml-node-children channelnode)))
1319 (setq has-new-items (newsticker--parse-generic-items
1320 name time (xml-get-children topnode 'item)
1321 ;; title-fn
1322 (lambda (node)
1323 (car (xml-node-children
1324 (car (xml-get-children node 'title)))))
1325 ;; desc-fn
1326 (lambda (node)
1327 (car (xml-node-children
1328 (car (xml-get-children node
1329 'description)))))
1330 ;; link-fn
1331 (lambda (node)
1332 (car (xml-node-children
1333 (car (xml-get-children node 'link)))))
1334 ;; time-fn
1335 (lambda (node)
1336 (newsticker--decode-iso8601-date
1337 (car (xml-node-children
1338 (car (xml-get-children node 'dc:date))))))
1339 ;; guid-fn
1340 (lambda (node)
1341 nil)
1342 ;; extra-fn
1343 (lambda (node)
1344 (xml-node-children node))))
1345 (or has-new-items is-new-feed)))
1347 (defun newsticker--parse-rss-2.0 (name time topnode)
1348 "Parse RSS 2.0 data.
1349 Return value as well as arguments NAME, TIME, and TOPNODE are the
1350 same as in `newsticker--parse-atom-1.0'.
1352 For the RSS 2.0 specification see http://blogs.law.harvard.edu/tech/rss."
1353 (newsticker--debug-msg "Parsing RSS 2.0 feed %s" name)
1354 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1355 is-new-feed has-new-items)
1356 (setq is-new-feed (newsticker--parse-generic-feed
1357 name time
1358 ;; title
1359 (car (xml-node-children
1360 (car (xml-get-children channelnode 'title))))
1361 ;; desc
1362 (car (xml-node-children
1363 (car (xml-get-children channelnode
1364 'description))))
1365 ;; link
1366 (car (xml-node-children
1367 (car (xml-get-children channelnode 'link))))
1368 ;; extra-elements
1369 (xml-node-children channelnode)))
1370 (setq has-new-items (newsticker--parse-generic-items
1371 name time (xml-get-children channelnode 'item)
1372 ;; title-fn
1373 (lambda (node)
1374 (car (xml-node-children
1375 (car (xml-get-children node 'title)))))
1376 ;; desc-fn
1377 (lambda (node)
1378 (or (car (xml-node-children
1379 (car (xml-get-children node
1380 'content:encoded))))
1381 (car (xml-node-children
1382 (car (xml-get-children node
1383 'description))))))
1384 ;; link-fn
1385 (lambda (node)
1386 (car (xml-node-children
1387 (car (xml-get-children node 'link)))))
1388 ;; time-fn
1389 (lambda (node)
1390 (newsticker--decode-rfc822-date
1391 (car (xml-node-children
1392 (car (xml-get-children node 'pubDate))))))
1393 ;; guid-fn
1394 (lambda (node)
1395 (newsticker--guid-to-string
1396 (assoc 'guid (xml-node-children node))))
1397 ;; extra-fn
1398 (lambda (node)
1399 (xml-node-children node))))
1400 (or has-new-items is-new-feed)))
1402 (defun newsticker--parse-generic-feed (name time title desc link
1403 extra-elements)
1404 "Parse generic news feed data.
1405 Argument NAME gives the name of a news feed. TIME gives the
1406 system time at which the data have been retrieved.
1408 The arguments TITLE, DESC, LINK, and EXTRA-ELEMENTS give the feed's title,
1409 description, link, and extra elements resp."
1410 (let ((title (or title "[untitled]"))
1411 (link (or link ""))
1412 (old-item nil)
1413 (position 0)
1414 (something-was-added nil))
1415 ;; decode numeric entities
1416 (setq title (newsticker--decode-numeric-entities title))
1417 (setq desc (newsticker--decode-numeric-entities desc))
1418 (setq link (newsticker--decode-numeric-entities link))
1419 ;; remove whitespace from title, desc, and link
1420 (setq title (newsticker--remove-whitespace title))
1421 (setq desc (newsticker--remove-whitespace desc))
1422 (setq link (newsticker--remove-whitespace link))
1424 ;; handle the feed itself
1425 (unless (newsticker--cache-contains newsticker--cache
1426 (intern name) title
1427 desc link 'feed)
1428 (setq something-was-added t))
1429 (setq newsticker--cache
1430 (newsticker--cache-add newsticker--cache (intern name)
1431 title desc link time 'feed position
1432 extra-elements time 'feed))
1433 something-was-added))
1435 (defun newsticker--parse-generic-items (name time itemlist
1436 title-fn desc-fn
1437 link-fn time-fn
1438 guid-fn extra-fn)
1439 "Parse generic news feed data.
1440 Argument NAME gives the name of a news feed. TIME gives the
1441 system time at which the data have been retrieved. ITEMLIST
1442 contains the news items returned by the xml parser.
1444 The arguments TITLE-FN, DESC-FN, LINK-FN, TIME-FN, GUID-FN, and
1445 EXTRA-FN give functions for extracting title, description, link,
1446 time, guid, and extra-elements resp. They are called with one
1447 argument, which is one of the items in ITEMLIST."
1448 (let (title desc link
1449 (old-item nil)
1450 (position 0)
1451 (something-was-added nil))
1452 ;; gather all items for this feed
1453 (mapc (lambda (node)
1454 (setq position (1+ position))
1455 (setq title (or (funcall title-fn node) "[untitled]"))
1456 (setq desc (funcall desc-fn node))
1457 (setq link (or (funcall link-fn node) ""))
1458 (setq time (or (funcall time-fn node) time))
1459 ;; It happened that the title or description
1460 ;; contained evil HTML code that confused the
1461 ;; xml parser. Therefore:
1462 (unless (stringp title)
1463 (setq title (prin1-to-string title)))
1464 (unless (or (stringp desc) (not desc))
1465 (setq desc (prin1-to-string desc)))
1466 ;; ignore items with empty title AND empty desc
1467 (when (or (> (length title) 0)
1468 (> (length desc) 0))
1469 ;; decode numeric entities
1470 (setq title (newsticker--decode-numeric-entities title))
1471 (when desc
1472 (setq desc (newsticker--decode-numeric-entities desc)))
1473 (setq link (newsticker--decode-numeric-entities link))
1474 ;; remove whitespace from title, desc, and link
1475 (setq title (newsticker--remove-whitespace title))
1476 (setq desc (newsticker--remove-whitespace desc))
1477 (setq link (newsticker--remove-whitespace link))
1478 ;; add data to cache
1479 ;; do we have this item already?
1480 (let* ((guid (funcall guid-fn node)))
1481 ;;(message "guid=%s" guid)
1482 (setq old-item
1483 (newsticker--cache-contains newsticker--cache
1484 (intern name) title
1485 desc link nil guid)))
1486 ;; add this item, or mark it as old, or do nothing
1487 (let ((age1 'new)
1488 (age2 'old)
1489 (item-new-p nil))
1490 (if old-item
1491 (let ((prev-age (newsticker--age old-item)))
1492 (unless newsticker-automatically-mark-items-as-old
1493 ;; Some feeds deliver items multiply, the
1494 ;; first time we find an 'obsolete-old one the
1495 ;; cache, the following times we find an 'old
1496 ;; one
1497 (if (memq prev-age '(obsolete-old old))
1498 (setq age2 'old)
1499 (setq age2 'new)))
1500 (if (eq prev-age 'immortal)
1501 (setq age2 'immortal))
1502 (setq time (newsticker--time old-item)))
1503 ;; item was not there
1504 (setq item-new-p t)
1505 (setq something-was-added t))
1506 (setq newsticker--cache
1507 (newsticker--cache-add
1508 newsticker--cache (intern name) title desc link
1509 time age1 position (funcall extra-fn node)
1510 time age2))
1511 (when item-new-p
1512 (let ((item (newsticker--cache-contains
1513 newsticker--cache (intern name) title
1514 desc link nil)))
1515 (if newsticker-auto-mark-filter-list
1516 (newsticker--run-auto-mark-filter name item))
1517 (run-hook-with-args
1518 'newsticker-new-item-functions name item))))))
1519 itemlist)
1520 something-was-added))
1522 ;; ======================================================================
1523 ;;; Misc
1524 ;; ======================================================================
1525 (defun newsticker--decode-numeric-entities (string)
1526 "Decode SGML numeric entities by their respective utf characters.
1527 This function replaces numeric entities in the input STRING and
1528 returns the modified string. For example \"&#42;\" gets replaced
1529 by \"*\"."
1530 (if (and string (stringp string))
1531 (let ((start 0))
1532 (while (string-match "&#\\([0-9]+\\);" string start)
1533 (condition-case nil
1534 (setq string (replace-match
1535 (string (read (substring string
1536 (match-beginning 1)
1537 (match-end 1))))
1538 nil nil string))
1539 (error nil))
1540 (setq start (1+ (match-beginning 0))))
1541 string)
1542 nil))
1544 (defun newsticker--remove-whitespace (string)
1545 "Remove leading and trailing whitespace from STRING."
1546 ;; we must have ...+ but not ...* in the regexps otherwise xemacs loops
1547 ;; endlessly...
1548 (when (and string (stringp string))
1549 (replace-regexp-in-string
1550 "[ \t\r\n]+$" ""
1551 (replace-regexp-in-string "^[ \t\r\n]+" "" string))))
1553 (defun newsticker--do-forget-preformatted (item)
1554 "Forget pre-formatted data for ITEM.
1555 Remove the pre-formatted from `newsticker--cache'."
1556 (if (nthcdr 7 item)
1557 (setcar (nthcdr 7 item) nil))
1558 (if (nthcdr 6 item)
1559 (setcar (nthcdr 6 item) nil)))
1561 (defun newsticker--forget-preformatted ()
1562 "Forget all cached pre-formatted data.
1563 Remove the pre-formatted from `newsticker--cache'."
1564 (mapc (lambda (feed)
1565 (mapc 'newsticker--do-forget-preformatted
1566 (cdr feed)))
1567 newsticker--cache)
1568 (when (fboundp 'newsticker--buffer-set-uptodate)
1569 (newsticker--buffer-set-uptodate nil)))
1571 (defun newsticker--debug-msg (string &rest args)
1572 "Print newsticker debug messages.
1573 This function calls `message' with arguments STRING and ARGS, if
1574 `newsticker-debug' is non-nil."
1575 (and newsticker-debug
1576 ;;(not (active-minibuffer-window))
1577 ;;(not (current-message))
1578 (apply 'message string args)))
1580 (defun newsticker--decode-iso8601-date (iso8601-string)
1581 "Return ISO8601-STRING in format like `decode-time'.
1582 Converts from ISO-8601 to Emacs representation.
1583 Examples:
1584 2004-09-17T05:09:49.001+00:00
1585 2004-09-17T05:09:49+00:00
1586 2004-09-17T05:09+00:00
1587 2004-09-17T05:09:49
1588 2004-09-17T05:09
1589 2004-09-17
1590 2004-09
1591 2004"
1592 (if iso8601-string
1593 (when (string-match
1594 (concat
1595 "^ *\\([0-9]\\{4\\}\\)" ;year
1596 "\\(-\\([0-9]\\{2\\}\\)" ;month
1597 "\\(-\\([0-9]\\{2\\}\\)" ;day
1598 "\\(T"
1599 "\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)" ;hour:minute
1600 "\\(:\\([0-9]\\{2\\}\\)\\(\\.[0-9]+\\)?\\)?" ;second
1601 ;timezone
1602 "\\(\\([-+Z]\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)?"
1603 "\\)?\\)?\\)? *$")
1604 iso8601-string)
1605 (let ((year (read (match-string 1 iso8601-string)))
1606 (month (read (or (match-string 3 iso8601-string)
1607 "1")))
1608 (day (read (or (match-string 5 iso8601-string)
1609 "1")))
1610 (hour (read (or (match-string 7 iso8601-string)
1611 "0")))
1612 (minute (read (or (match-string 8 iso8601-string)
1613 "0")))
1614 (second (read (or (match-string 10 iso8601-string)
1615 "0")))
1616 (sign (match-string 13 iso8601-string))
1617 (offset-hour (read (or (match-string 15 iso8601-string)
1618 "0")))
1619 (offset-minute (read (or (match-string 16 iso8601-string)
1620 "0"))))
1621 (cond ((string= sign "+")
1622 (setq hour (- hour offset-hour))
1623 (setq minute (- minute offset-minute)))
1624 ((string= sign "-")
1625 (setq hour (+ hour offset-hour))
1626 (setq minute (+ minute offset-minute))))
1627 ;; if UTC subtract current-time-zone offset
1628 ;;(setq second (+ (car (current-time-zone)) second)))
1630 (condition-case nil
1631 (encode-time second minute hour day month year t)
1632 (error
1633 (message "Cannot decode \"%s\"" iso8601-string)
1634 nil))))
1635 nil))
1637 (defun newsticker--decode-rfc822-date (rfc822-string)
1638 "Return RFC822-STRING in format like `decode-time'.
1639 Converts from RFC822 to Emacs representation.
1640 Examples:
1641 Sat, 07 September 2002 00:00:01 +0100
1642 Sat, 07 September 2002 00:00:01 MET
1643 Sat, 07 Sep 2002 00:00:01 GMT
1644 07 Sep 2002 00:00:01 GMT
1645 07 Sep 2002"
1646 (if (and rfc822-string (stringp rfc822-string))
1647 (when (string-match
1648 (concat
1649 "\\s-*"
1650 ;; week day
1651 "\\(\\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\)\\s-*,?\\)?\\s-*"
1652 ;; day
1653 "\\([0-9]\\{1,2\\}\\)\\s-+"
1654 ;; month
1655 "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|"
1656 "Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\).*?\\s-+"
1657 ;; year
1658 "\\([0-9]\\{2,4\\}\\)"
1659 ;; time may be missing
1660 "\\(\\s-+"
1661 ;; hour
1662 "\\([0-9]\\{2\\}\\)"
1663 ;; minute
1664 ":\\([0-9]\\{2\\}\\)"
1665 ;; second
1666 "\\(:\\([0-9]\\{2\\}\\)\\)?"
1667 ;; zone -- fixme
1668 "\\(\\s-+\\("
1669 "UT\\|GMT\\|EST\\|EDT\\|CST\\|CDT\\|MST\\|MDT\\|PST\\|PDT"
1670 "\\|\\([-+]\\)\\([0-9]\\{2\\}\\)\\([0-9]\\{2\\}\\)"
1671 "\\)\\)?"
1672 "\\)?")
1673 rfc822-string)
1674 (let ((day (read (match-string 3 rfc822-string)))
1675 (month-name (match-string 4 rfc822-string))
1676 (month 0)
1677 (year (read (match-string 5 rfc822-string)))
1678 (hour (read (or (match-string 7 rfc822-string) "0")))
1679 (minute (read (or (match-string 8 rfc822-string) "0")))
1680 (second (read (or (match-string 10 rfc822-string) "0")))
1681 (zone (match-string 12 rfc822-string))
1682 (sign (match-string 13 rfc822-string))
1683 (offset-hour (read (or (match-string 14 rfc822-string)
1684 "0")))
1685 (offset-minute (read (or (match-string 15 rfc822-string)
1686 "0")))
1687 ;;FIXME
1689 (when zone
1690 (cond ((string= sign "+")
1691 (setq hour (- hour offset-hour))
1692 (setq minute (- minute offset-minute)))
1693 ((string= sign "-")
1694 (setq hour (+ hour offset-hour))
1695 (setq minute (+ minute offset-minute)))))
1696 (condition-case error-data
1697 (let ((i 1))
1698 (mapc (lambda (m)
1699 (if (string= month-name m)
1700 (setq month i))
1701 (setq i (1+ i)))
1702 '("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
1703 "Sep" "Oct" "Nov" "Dec"))
1704 (encode-time second minute hour day month year t))
1705 (error
1706 (message "Cannot decode \"%s\": %s %s" rfc822-string
1707 (car error-data) (cdr error-data))
1708 nil))))
1709 nil))
1711 (defun newsticker--lists-intersect-p (list1 list2)
1712 "Return t if LIST1 and LIST2 share elements."
1713 (let ((result nil))
1714 (mapc (lambda (elt)
1715 (if (memq elt list2)
1716 (setq result t)))
1717 list1)
1718 result))
1720 (defun newsticker--update-process-ids ()
1721 "Update list of ids of active newsticker processes.
1722 Checks list of active processes against list of newsticker processes."
1723 (let ((active-procs (process-list))
1724 (new-list nil))
1725 (mapc (lambda (proc)
1726 (let ((id (process-id proc)))
1727 (if (memq id newsticker--process-ids)
1728 (setq new-list (cons id new-list)))))
1729 active-procs)
1730 (setq newsticker--process-ids new-list))
1731 (force-mode-line-update))
1733 ;; ======================================================================
1734 ;;; Images
1735 ;; ======================================================================
1736 (defun newsticker--image-get (feed-name url)
1737 "Get image of the news site FEED-NAME from URL.
1738 If the image has been downloaded in the last 24h do nothing."
1739 (let ((image-name (concat newsticker-imagecache-dirname "/"
1740 feed-name)))
1741 (if (and (file-exists-p image-name)
1742 (time-less-p (current-time)
1743 (time-add (nth 5 (file-attributes image-name))
1744 (seconds-to-time 86400))))
1745 (newsticker--debug-msg "%s: Getting image for %s skipped"
1746 (format-time-string "%A, %H:%M" (current-time))
1747 feed-name)
1748 ;; download
1749 (newsticker--debug-msg "%s: Getting image for %s"
1750 (format-time-string "%A, %H:%M" (current-time))
1751 feed-name)
1752 (let* ((buffername (concat " *newsticker-wget-image-" feed-name "*"))
1753 (item (or (assoc feed-name newsticker-url-list)
1754 (assoc feed-name newsticker-url-list-defaults)
1755 (error
1756 "Cannot get news for %s: Check newsticker-url-list"
1757 feed-name)))
1758 (wget-arguments (or (car (cdr (cdr (cdr (cdr item)))))
1759 newsticker-wget-arguments)))
1760 (save-excursion
1761 (set-buffer (get-buffer-create buffername))
1762 (erase-buffer)
1763 ;; throw an error if there is an old wget-process around
1764 (if (get-process feed-name)
1765 (error "Another wget-process is running for image %s"
1766 feed-name))
1767 ;; start wget
1768 (let* ((args (append wget-arguments (list url)))
1769 (proc (apply 'start-process feed-name buffername
1770 newsticker-wget-name args)))
1771 (set-process-coding-system proc 'no-conversion 'no-conversion)
1772 (set-process-sentinel proc 'newsticker--image-sentinel)))))))
1774 (defun newsticker--image-sentinel (process event)
1775 "Sentinel for image-retrieving PROCESS caused by EVENT."
1776 (let* ((p-status (process-status process))
1777 (exit-status (process-exit-status process))
1778 (feed-name (process-name process)))
1779 ;; catch known errors (zombie processes, rubbish-xml, etc.)
1780 ;; if an error occurs the news feed is not updated!
1781 (catch 'oops
1782 (unless (and (eq p-status 'exit)
1783 (= exit-status 0))
1784 (message "%s: Error while retrieving image from %s"
1785 (format-time-string "%A, %H:%M" (current-time))
1786 feed-name)
1787 (throw 'oops nil))
1788 (let (image-name)
1789 (save-excursion
1790 (set-buffer (process-buffer process))
1791 (setq image-name (concat newsticker-imagecache-dirname "/"
1792 feed-name))
1793 (set-buffer-file-coding-system 'no-conversion)
1794 ;; make sure the cache dir exists
1795 (unless (file-directory-p newsticker-imagecache-dirname)
1796 (make-directory newsticker-imagecache-dirname))
1797 ;; write and close buffer
1798 (let ((require-final-newline nil)
1799 (backup-inhibited t)
1800 (coding-system-for-write 'no-conversion))
1801 (write-region nil nil image-name nil 'quiet))
1802 (set-buffer-modified-p nil)
1803 (kill-buffer (current-buffer)))))))
1807 (defun newsticker--insert-image (img string)
1808 "Insert IMG with STRING at point."
1809 (insert-image img string))
1811 ;; ======================================================================
1812 ;;; HTML rendering
1813 ;; ======================================================================
1814 (defun newsticker-htmlr-render (pos1 pos2) ;
1815 "Replacement for `htmlr-render'.
1816 Renders the HTML code in the region POS1 to POS2 using htmlr."
1817 (let ((str (buffer-substring-no-properties pos1 pos2)))
1818 (delete-region pos1 pos2)
1819 (insert
1820 (with-temp-buffer
1821 (insert str)
1822 (goto-char (point-min))
1823 ;; begin original htmlr-render
1824 (when (fboundp 'htmlr-reset) (htmlr-reset))
1825 ;; something omitted here...
1826 (when (fboundp 'htmlr-step)
1827 (while (< (point) (point-max))
1828 (htmlr-step)))
1829 ;; end original htmlr-render
1830 (newsticker--remove-whitespace (buffer-string))))))
1832 ;; ======================================================================
1833 ;;; Manipulation of cached data
1834 ;; ======================================================================
1835 (defun newsticker--cache-set-preformatted-contents (item contents)
1836 "Set preformatted contents of ITEM to CONTENTS."
1837 (if (nthcdr 6 item)
1838 (setcar (nthcdr 6 item) contents)
1839 (setcdr (nthcdr 5 item) (list contents))))
1841 (defun newsticker--cache-set-preformatted-title (item title)
1842 "Set preformatted title of ITEM to TITLE."
1843 (if (nthcdr 7 item)
1844 (setcar (nthcdr 7 item) title)
1845 (setcdr (nthcdr 6 item) title)))
1847 (defun newsticker--cache-replace-age (data feed old-age new-age)
1848 "Mark all items in DATA in FEED which carry age OLD-AGE with NEW-AGE.
1849 If FEED is 'any it applies to all feeds. If OLD-AGE is 'any,
1850 all marks are replaced by NEW-AGE. Removes all pre-formatted contents."
1851 (mapc (lambda (a-feed)
1852 (when (or (eq feed 'any)
1853 (eq (car a-feed) feed))
1854 (let ((items (cdr a-feed)))
1855 (mapc (lambda (item)
1856 (when (or (eq old-age 'any)
1857 (eq (newsticker--age item) old-age))
1858 (setcar (nthcdr 4 item) new-age)
1859 (newsticker--do-forget-preformatted item)))
1860 items))))
1861 data)
1862 data)
1864 (defun newsticker--cache-mark-expired (data feed old-age new-age time)
1865 "Mark all expired entries.
1866 This function sets the age entries in DATA in the feed FEED. If
1867 an item's age is OLD-AGE it is set to NEW-AGE if the item is
1868 older than TIME."
1869 (mapc
1870 (lambda (a-feed)
1871 (when (or (eq feed 'any)
1872 (eq (car a-feed) feed))
1873 (let ((items (cdr a-feed)))
1874 (mapc
1875 (lambda (item)
1876 (when (eq (newsticker--age item) old-age)
1877 (let ((exp-time (time-add (newsticker--time item)
1878 (seconds-to-time time))))
1879 (when (time-less-p exp-time (current-time))
1880 (newsticker--debug-msg
1881 "Item `%s' from %s has expired on %s"
1882 (newsticker--title item)
1883 (format-time-string "%Y-%02m-%d, %H:%M"
1884 (newsticker--time item))
1885 (format-time-string "%Y-%02m-%d, %H:%M" exp-time))
1886 (setcar (nthcdr 4 item) new-age)))))
1887 items))))
1888 data)
1889 data)
1891 (defun newsticker--cache-contains (data feed title desc link age
1892 &optional guid)
1893 "Check DATA whether FEED contains an item with the given properties.
1894 This function returns the contained item or nil if it is not
1895 contained.
1896 The properties which are checked are TITLE, DESC, LINK, AGE, and
1897 GUID. In general all properties must match in order to return a
1898 certain item, except for the following cases.
1900 If AGE equals 'feed the TITLE, DESCription and LINK do not
1901 matter. If DESC is nil it is ignored as well. If
1902 `newsticker-desc-comp-max' is non-nil, only the first
1903 `newsticker-desc-comp-max' characters of DESC are taken into
1904 account.
1906 If GUID is non-nil it is sufficient to match this value, and the
1907 other properties are ignored."
1908 ;;(newsticker--debug-msg "Looking for %s guid=%s" title guid)
1909 (condition-case nil
1910 (catch 'found
1911 (when (and desc newsticker-desc-comp-max
1912 (> (length desc) newsticker-desc-comp-max))
1913 (setq desc (substring desc 0 newsticker-desc-comp-max)))
1914 (mapc
1915 (lambda (this-feed)
1916 (when (eq (car this-feed) feed)
1917 (mapc (lambda (anitem)
1918 (when (cond (guid
1919 ;; global unique id can match
1920 (string= guid (newsticker--guid anitem)))
1921 (t;;FIXME?
1923 ;; or title, desc, etc.
1924 (and
1925 ;;(or (not (eq age 'feed))
1926 ;; (eq (newsticker--age anitem) 'feed))
1927 (string= (newsticker--title anitem)
1928 title)
1929 (or (not link)
1930 (string= (newsticker--link anitem)
1931 link))
1932 (or (not desc)
1933 (if (and desc newsticker-desc-comp-max
1934 (> (length (newsticker--desc
1935 anitem))
1936 newsticker-desc-comp-max))
1937 (string= (substring
1938 (newsticker--desc anitem)
1940 newsticker-desc-comp-max)
1941 desc)
1942 (string= (newsticker--desc anitem)
1943 desc)))))))
1944 ;;(newsticker--debug-msg "Found %s guid=%s"
1945 ;; (newsticker--title anitem)
1946 ;; (newsticker--guid anitem))
1947 (throw 'found anitem)))
1948 (cdr this-feed))))
1949 data)
1950 ;;(newsticker--debug-msg "Found nothing")
1951 nil)
1952 (error nil)))
1954 (defun newsticker--cache-add (data feed-name-symbol title desc link time age
1955 position extra-elements
1956 &optional updated-time updated-age
1957 preformatted-contents
1958 preformatted-title)
1959 "Add another item to cache data.
1960 Add to DATA in the FEED-NAME-SYMBOL an item with TITLE, DESC,
1961 LINK, TIME, AGE, POSITION, and EXTRA-ELEMENTS. If this item is
1962 contained already, its time is set to UPDATED-TIME, its mark is
1963 set to UPDATED-AGE, and its pre-formatted contents is set to
1964 PREFORMATTED-CONTENTS and PREFORMATTED-TITLE. Returns the age
1965 which the item got."
1966 (let* ((guid (newsticker--guid-to-string (assoc 'guid extra-elements)))
1967 (item (newsticker--cache-contains data feed-name-symbol title desc link
1968 age guid)))
1969 ;;(message "guid=%s" guid)
1970 (if item
1971 ;; does exist already -- change age, update time and position
1972 (progn
1973 ;;(newsticker--debug-msg "Updating item %s %s %s %s %s -> %s %s
1974 ;; (guid %s -> %s)"
1975 ;; feed-name-symbol title link time age
1976 ;; updated-time updated-age
1977 ;; guid (newsticker--guid item))
1978 (if (nthcdr 5 item)
1979 (setcar (nthcdr 5 item) position)
1980 (setcdr (nthcdr 4 item) (list position)))
1981 (setcar (nthcdr 4 item) updated-age)
1982 (if updated-time
1983 (setcar (nthcdr 3 item) updated-time))
1984 ;; replace cached pre-formatted contents
1985 (newsticker--cache-set-preformatted-contents
1986 item preformatted-contents)
1987 (newsticker--cache-set-preformatted-title
1988 item preformatted-title))
1989 ;; did not exist or age equals 'feed-name-symbol
1990 (setq item (list title desc link time age position preformatted-contents
1991 preformatted-title extra-elements))
1992 ;;(newsticker--debug-msg "Adding item %s" item)
1993 (catch 'found
1994 (mapc (lambda (this-feed)
1995 (when (eq (car this-feed) feed-name-symbol)
1996 (setcdr this-feed (nconc (cdr this-feed) (list item)))
1997 (throw 'found this-feed)))
1998 data)
1999 ;; the feed is not contained
2000 (add-to-list 'data (list feed-name-symbol item) t))))
2001 data)
2003 (defun newsticker--cache-remove (data feed-symbol age)
2004 "Remove all entries from DATA in the feed FEED-SYMBOL with AGE.
2005 FEED-SYMBOL may be 'any. Entries from old feeds, which are no longer in
2006 `newsticker-url-list' or `newsticker-url-list-defaults', are removed as
2007 well."
2008 (let* ((pos data)
2009 (feed (car pos))
2010 (last-pos nil))
2011 (while feed
2012 (if (or (assoc (symbol-name (car feed)) newsticker-url-list)
2013 (assoc (symbol-name (car feed)) newsticker-url-list-defaults))
2014 ;; feed is still valid=active
2015 ;; (message "Keeping feed %s" (car feed))
2016 (if (or (eq feed-symbol 'any)
2017 (eq feed-symbol (car feed)))
2018 (let* ((item-pos (cdr feed))
2019 (item (car item-pos))
2020 (prev-pos nil))
2021 (while item
2022 ;;(message "%s" (car item))
2023 (if (eq age (newsticker--age item))
2024 ;; remove this item
2025 (progn
2026 ;;(message "Removing item %s" (car item))
2027 (if prev-pos
2028 (setcdr prev-pos (cdr item-pos))
2029 (setcdr feed (cdr item-pos))))
2030 ;;(message "Keeping item %s" (car item))
2031 (setq prev-pos item-pos))
2032 (setq item-pos (cdr item-pos))
2033 (setq item (car item-pos)))))
2034 ;; feed is not active anymore
2035 ;; (message "Removing feed %s" (car feed))
2036 (if last-pos
2037 (setcdr last-pos (cdr pos))
2038 (setq data (cdr pos))))
2039 (setq last-pos pos)
2040 (setq pos (cdr pos))
2041 (setq feed (car pos)))))
2043 ;; ======================================================================
2044 ;;; Sorting
2045 ;; ======================================================================
2046 (defun newsticker--cache-item-compare-by-time (item1 item2)
2047 "Compare two news items ITEM1 and ITEM2 by comparing their time values."
2048 (catch 'result
2049 (let ((age1 (newsticker--age item1))
2050 (age2 (newsticker--age item2)))
2051 (if (not (eq age1 age2))
2052 (cond ((eq age1 'obsolete)
2053 (throw 'result nil))
2054 ((eq age2 'obsolete)
2055 (throw 'result t)))))
2056 (let* ((time1 (newsticker--time item1))
2057 (time2 (newsticker--time item2)))
2058 (cond ((< (nth 0 time1) (nth 0 time2))
2059 nil)
2060 ((> (nth 0 time1) (nth 0 time2))
2062 ((< (nth 1 time1) (nth 1 time2))
2063 nil)
2064 ((> (nth 1 time1) (nth 1 time2))
2066 ((< (or (nth 2 time1) 0) (or (nth 2 time2) 0))
2067 nil)
2068 ((> (or (nth 2 time1) 0) (or (nth 2 time2) 0))
2071 nil)))))
2073 (defun newsticker--cache-item-compare-by-title (item1 item2)
2074 "Compare ITEM1 and ITEM2 by comparing their titles."
2075 (catch 'result
2076 (let ((age1 (newsticker--age item1))
2077 (age2 (newsticker--age item2)))
2078 (if (not (eq age1 age2))
2079 (cond ((eq age1 'obsolete)
2080 (throw 'result nil))
2081 ((eq age2 'obsolete)
2082 (throw 'result t)))))
2083 (string< (newsticker--title item1) (newsticker--title item2))))
2085 (defun newsticker--cache-item-compare-by-position (item1 item2)
2086 "Compare ITEM1 and ITEM2 by comparing their original positions."
2087 (catch 'result
2088 (let ((age1 (newsticker--age item1))
2089 (age2 (newsticker--age item2)))
2090 (if (not (eq age1 age2))
2091 (cond ((eq age1 'obsolete)
2092 (throw 'result nil))
2093 ((eq age2 'obsolete)
2094 (throw 'result t)))))
2095 (< (or (newsticker--pos item1) 0) (or (newsticker--pos item2) 0))))
2099 (defun newsticker--cache-save ()
2100 "Update and save newsticker cache file."
2101 (interactive)
2102 (newsticker--cache-update t))
2104 (defun newsticker--cache-update (&optional save)
2105 "Update newsticker cache file.
2106 If optional argument SAVE is not nil the cache file is saved to disk."
2107 (save-excursion
2108 (let ((coding-system-for-write 'utf-8))
2109 (with-temp-buffer
2110 (setq buffer-undo-list t)
2111 (erase-buffer)
2112 (insert ";; -*- coding: utf-8 -*-\n")
2113 (insert (prin1-to-string newsticker--cache))
2114 (when save
2115 (set-visited-file-name newsticker-cache-filename)
2116 (save-buffer))))))
2118 (defun newsticker--cache-get-feed (feed)
2119 "Return the cached data for the feed FEED.
2120 FEED is a symbol!"
2121 (assoc feed newsticker--cache))
2123 ;; ======================================================================
2124 ;;; Statistics
2125 ;; ======================================================================
2126 (defun newsticker--stat-num-items (feed &rest ages)
2127 "Return number of items in the given FEED which have one of the given AGES.
2128 If AGES is nil, the total number of items is returned."
2129 (let ((items (cdr (newsticker--cache-get-feed feed)))
2130 (num 0))
2131 (while items
2132 (if ages
2133 (if (memq (newsticker--age (car items)) ages)
2134 (setq num (1+ num)))
2135 (if (memq (newsticker--age (car items)) '(new old immortal obsolete))
2136 (setq num (1+ num))))
2137 (setq items (cdr items)))
2138 num))
2140 (defun newsticker--stat-num-items-total (&optional age)
2141 "Return total number of items in all feeds which have the given AGE.
2142 If AGE is nil, the total number of items is returned."
2143 (apply '+
2144 (mapcar (lambda (feed)
2145 (if age
2146 (newsticker--stat-num-items (intern (car feed)) age)
2147 (newsticker--stat-num-items (intern (car feed)))))
2148 (append newsticker-url-list-defaults newsticker-url-list))))
2150 ;; ======================================================================
2151 ;;; OPML
2152 ;; ======================================================================
2153 (defun newsticker-opml-export ()
2154 "OPML subscription export.
2155 Export subscriptions to a buffer in OPML Format."
2156 (interactive)
2157 (with-current-buffer (get-buffer-create "*OPML Export*")
2158 (set-buffer-file-coding-system 'utf-8)
2159 (insert (concat
2160 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
2161 "<!-- OPML generated by Emacs newsticker.el -->\n"
2162 "<opml version=\"1.0\">\n"
2163 " <head>\n"
2164 " <title>mySubscriptions</title>\n"
2165 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
2166 "</dateCreated>\n"
2167 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
2168 " <ownerName>" (user-full-name) "</ownerName>\n"
2169 " </head>\n"
2170 " <body>\n"))
2171 (mapc (lambda (sub)
2172 (insert " <outline text=\"")
2173 (insert (newsticker--title sub))
2174 (insert "\" xmlUrl=\"")
2175 (insert (cadr sub))
2176 (insert "\"/>\n"))
2177 (append newsticker-url-list newsticker-url-list-defaults))
2178 (insert " </body>\n</opml>\n"))
2179 (pop-to-buffer "*OPML Export*")
2180 (when (fboundp 'sgml-mode)
2181 (sgml-mode)))
2183 (defun newsticker--opml-import-outlines (outlines)
2184 "Recursively import OUTLINES from OPML data.
2185 Note that nested outlines are currently flattened -- i.e. grouping is
2186 removed."
2187 (mapc (lambda (outline)
2188 (let ((name (xml-get-attribute outline 'text))
2189 (url (xml-get-attribute outline 'xmlUrl))
2190 (children (xml-get-children outline 'outline)))
2191 (unless (string= "" url)
2192 (add-to-list 'newsticker-url-list
2193 (list name url nil nil nil) t))
2194 (if children
2195 (newsticker--opml-import-outlines children))))
2196 outlines))
2198 (defun newsticker-opml-import (filename)
2199 "Import OPML data from FILENAME."
2200 (interactive "fOPML file: ")
2201 (set-buffer (find-file-noselect filename))
2202 (goto-char (point-min))
2203 (let* ((node-list (xml-parse-region (point-min) (point-max)))
2204 (body (car (xml-get-children (car node-list) 'body)))
2205 (outlines (xml-get-children body 'outline)))
2206 (newsticker--opml-import-outlines outlines))
2207 (customize-variable 'newsticker-url-list))
2209 ;; ======================================================================
2210 ;;; Auto marking
2211 ;; ======================================================================
2212 (defun newsticker--run-auto-mark-filter (feed item)
2213 "Automatically mark an item as old or immortal.
2214 This function checks the variable `newsticker-auto-mark-filter-list'
2215 for an entry that matches FEED and ITEM."
2216 (let ((case-fold-search t))
2217 (mapc (lambda (filter)
2218 (let ((filter-feed (car filter))
2219 (pattern-list (cadr filter)))
2220 (when (string-match filter-feed feed)
2221 (newsticker--do-run-auto-mark-filter item pattern-list))))
2222 newsticker-auto-mark-filter-list)))
2224 (defun newsticker--do-run-auto-mark-filter (item list)
2225 "Actually compare ITEM against the pattern-LIST.
2226 LIST must be an element of `newsticker-auto-mark-filter-list'."
2227 (mapc (lambda (pattern)
2228 (let ((age (nth 0 pattern))
2229 (place (nth 1 pattern))
2230 (regexp (nth 2 pattern))
2231 (title (newsticker--title item))
2232 (desc (newsticker--desc item)))
2233 (when (or (eq place 'title) (eq place 'all))
2234 (when (and title (string-match regexp title))
2235 (newsticker--debug-msg "Auto-marking as %s: `%s'"
2236 age (newsticker--title item))
2237 (setcar (nthcdr 4 item) age)))
2238 (when (or (eq place 'description) (eq place 'all))
2239 (when (and desc (string-match regexp desc))
2240 (newsticker--debug-msg "Auto-marking as %s: `%s'"
2241 age (newsticker--title item))
2242 (setcar (nthcdr 4 item) age)))))
2243 list))
2246 ;; ======================================================================
2247 ;;; Hook samples
2248 ;; ======================================================================
2249 (defun newsticker-new-item-functions-sample (feed item)
2250 "Demonstrate the use of the `newsticker-new-item-functions' hook.
2251 This function just prints out the values of the FEED and title of the ITEM."
2252 (message (concat "newsticker-new-item-functions-sample: feed=`%s', "
2253 "title=`%s'")
2254 feed (newsticker--title item)))
2256 (defun newsticker-download-images (feed item)
2257 "Download the first image.
2258 If FEED equals \"imagefeed\" download the first image URL found
2259 in the description=contents of ITEM to the directory
2260 \"~/tmp/newsticker/FEED/TITLE\" where TITLE is the title of the item."
2261 (when (string= feed "imagefeed")
2262 (let ((title (newsticker--title item))
2263 (desc (newsticker--desc item)))
2264 (when (string-match "<img src=\"\\(http://[^ \"]+\\)\"" desc)
2265 (let ((url (substring desc (match-beginning 1) (match-end 1)))
2266 (temp-dir (concat "~/tmp/newsticker/" feed "/" title))
2267 (org-dir default-directory))
2268 (unless (file-directory-p temp-dir)
2269 (make-directory temp-dir t))
2270 (cd temp-dir)
2271 (message "Getting image %s" url)
2272 (apply 'start-process "wget-image"
2273 " *newsticker-wget-download-images*"
2274 newsticker-wget-name
2275 (list url))
2276 (cd org-dir))))))
2278 (defun newsticker-download-enclosures (feed item)
2279 "In all FEEDs download the enclosed object of the news ITEM.
2280 The object is saved to the directory \"~/tmp/newsticker/FEED/TITLE\", which
2281 is created if it does not exist. TITLE is the title of the news
2282 item. Argument FEED is ignored.
2283 This function is suited for adding it to `newsticker-new-item-functions'."
2284 (let ((title (newsticker--title item))
2285 (enclosure (newsticker--enclosure item)))
2286 (when enclosure
2287 (let ((url (cdr (assoc 'url enclosure)))
2288 (temp-dir (concat "~/tmp/newsticker/" feed "/" title))
2289 (org-dir default-directory))
2290 (unless (file-directory-p temp-dir)
2291 (make-directory temp-dir t))
2292 (cd temp-dir)
2293 (message "Getting enclosure %s" url)
2294 (apply 'start-process "wget-enclosure"
2295 " *newsticker-wget-download-enclosures*"
2296 newsticker-wget-name
2297 (list url))
2298 (cd org-dir)))))
2300 ;; ======================================================================
2301 ;;; Retrieve samples
2302 ;; ======================================================================
2303 (defun newsticker-retrieve-random-message (feed-name)
2304 "Return an artificial RSS string under the name FEED-NAME."
2305 (concat "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?><rss version=\"0.91\">"
2306 "<channel>"
2307 "<title>newsticker-retrieve-random-message</title>"
2308 "<description>Sample retrieval function</description>"
2309 "<pubDate>FIXME Sat, 07 Sep 2005 00:00:01 GMT</pubDate>"
2310 "<item><title>" (format "Your lucky number is %d" (random 10000))
2311 "</title><description>" (format "Or maybe it is %d" (random 10000))
2312 "</description></item></channel></rss>"))
2314 (provide 'newsticker-backend)
2316 ;; arch-tag: 0e37b658-56e9-49ab-90f9-f2df57e1a659
2317 ;;; newsticker-backend.el ends here