wl checked de tutorial
[emacs.git] / lisp / net / newst-backend.el
blob7bc6131cf1fa701becea74fa78cbfc961411d809
1 ;;; newst-backend.el --- Retrieval backend for newsticker.
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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: "23. Januar 2009, 19:39:22 (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)
426 (make-obsolete 'newsticker-cache-filename 'newsticker-dir)
428 (defcustom newsticker-dir
429 (locate-user-emacs-file "newsticker/" ".newsticker/")
430 "Directory where newsticker saves data."
431 :type 'directory
432 :group 'newsticker-miscellaneous)
434 ;; debugging
435 (defcustom newsticker-debug
437 "Enables some features needed for debugging newsticker.el.
439 If set to t newsticker.el will print lots of debugging messages, and the
440 buffers *newsticker-wget-<feed>* will not be closed."
441 :type 'boolean
442 :group 'newsticker-miscellaneous)
444 ;; ======================================================================
445 ;;; Compatibility section, XEmacs, Emacs
446 ;; ======================================================================
447 (unless (fboundp 'time-add)
448 (require 'time-date);;FIXME
449 (defun time-add (t1 t2)
450 (seconds-to-time (+ (time-to-seconds t1) (time-to-seconds t2)))))
452 (unless (fboundp 'match-string-no-properties)
453 (defalias 'match-string-no-properties 'match-string))
455 (when (featurep 'xemacs)
456 (unless (fboundp 'replace-regexp-in-string)
457 (defun replace-regexp-in-string (re rp st)
458 (save-match-data ;; apparently XEmacs needs save-match-data
459 (replace-in-string st re rp)))))
461 ;; copied from subr.el
462 (unless (fboundp 'add-to-invisibility-spec)
463 (defun add-to-invisibility-spec (arg)
464 "Add elements to `buffer-invisibility-spec'.
465 See documentation for `buffer-invisibility-spec' for the kind of elements
466 that can be added."
467 (if (eq buffer-invisibility-spec t)
468 (setq buffer-invisibility-spec (list t)))
469 (setq buffer-invisibility-spec
470 (cons arg buffer-invisibility-spec))))
472 ;; copied from subr.el
473 (unless (fboundp 'remove-from-invisibility-spec)
474 (defun remove-from-invisibility-spec (arg)
475 "Remove elements from `buffer-invisibility-spec'."
476 (if (consp buffer-invisibility-spec)
477 (setq buffer-invisibility-spec
478 (delete arg buffer-invisibility-spec)))))
480 ;; ======================================================================
481 ;;; Internal variables
482 ;; ======================================================================
483 (defvar newsticker--item-list nil
484 "List of newsticker items.")
485 (defvar newsticker--item-position 0
486 "Actual position in list of newsticker items.")
487 (defvar newsticker--prev-message "There was no previous message yet!"
488 "Last message that the newsticker displayed.")
489 (defvar newsticker--scrollable-text ""
490 "The text which is scrolled smoothly in the echo area.")
491 (defvar newsticker--buffer-uptodate-p nil
492 "Tells whether the newsticker buffer is up to date.")
493 (defvar newsticker--latest-update-time (current-time)
494 "The time at which the latest news arrived.")
495 (defvar newsticker--process-ids nil
496 "List of PIDs of active newsticker processes.")
498 (defvar newsticker--cache nil "Cached newsticker data.
499 This is a list of the form
501 ((label1
502 (title description link time age index preformatted-contents
503 preformatted-title extra-elements)
504 ...)
505 (label2
506 (title description link time age index preformatted-contents
507 preformatted-title extra-elements)
508 ...)
509 ...)
511 where LABEL is a symbol. TITLE, DESCRIPTION, and LINK are
512 strings. TIME is a time value as returned by `current-time'.
513 AGE is a symbol: 'new, 'old, 'immortal, and 'obsolete denote
514 ordinary news items, whereas 'feed denotes an item which is not a
515 headline but describes the feed itself. INDEX denotes the
516 original position of the item -- used for restoring the original
517 order. PREFORMATTED-CONTENTS and PREFORMATTED-TITLE hold the
518 formatted contents of the item's description and title. This
519 speeds things up if HTML rendering is used, which is rather
520 slow. EXTRA-ELEMENTS is an alist containing additional elements.")
522 (defvar newsticker--auto-narrow-to-feed nil
523 "Automatically narrow to current news feed.
524 If non-nil only the items of the current news feed are visible.")
526 (defvar newsticker--auto-narrow-to-item nil
527 "Automatically narrow to current news item.
528 If non-nil only the current headline is visible.")
530 (defconst newsticker--error-headline
531 "[COULD NOT DOWNLOAD HEADLINES!]"
532 "Title of error headline which will be inserted if news retrieval fails.")
534 ;; ======================================================================
535 ;;; Shortcuts
536 ;; ======================================================================
537 (defsubst newsticker--title (item)
538 "Return title of ITEM."
539 (nth 0 item))
540 (defsubst newsticker--desc (item)
541 "Return description of ITEM."
542 (nth 1 item))
543 (defsubst newsticker--link (item)
544 "Return link of ITEM."
545 (nth 2 item))
546 (defsubst newsticker--time (item)
547 "Return time of ITEM."
548 (nth 3 item))
549 (defsubst newsticker--age (item)
550 "Return age of ITEM."
551 (nth 4 item))
552 (defsubst newsticker--pos (item)
553 "Return position/index of ITEM."
554 (nth 5 item))
555 (defsubst newsticker--preformatted-contents (item)
556 "Return pre-formatted text of ITEM."
557 (nth 6 item))
558 (defsubst newsticker--preformatted-title (item)
559 "Return pre-formatted title of ITEM."
560 (nth 7 item))
561 (defsubst newsticker--extra (item)
562 "Return extra attributes of ITEM."
563 (nth 8 item))
564 (defsubst newsticker--guid-to-string (guid)
565 "Return string representation of GUID."
566 (if (stringp guid)
567 guid
568 (car (xml-node-children guid))))
569 (defsubst newsticker--guid (item)
570 "Return guid of ITEM."
571 (newsticker--guid-to-string (assoc 'guid (newsticker--extra item))))
572 (defsubst newsticker--enclosure (item)
573 "Return enclosure element of ITEM in the form \(...FIXME...\) or nil."
574 (let ((enclosure (assoc 'enclosure (newsticker--extra item))))
575 (if enclosure
576 (xml-node-attributes enclosure))))
577 (defun newsticker--real-feed-name (feed)
578 "Return real name of FEED."
579 (catch 'name
580 (mapc (lambda (item)
581 (if (eq (newsticker--age item) 'feed)
582 (throw 'name (newsticker--title item))))
583 (cdr (newsticker--cache-get-feed feed)))
584 (symbol-name feed)))
587 ;; ======================================================================
588 ;;; User fun
589 ;; ======================================================================
591 (defun newsticker--start-feed (feed &optional do-not-complain-if-running)
592 "Start retrieval timer for FEED.
593 If timer is running already a warning message is printed unless
594 DO-NOT-COMPLAIN-IF-RUNNING is not nil. Add the started
595 name/timer pair to `newsticker--retrieval-timer-list'."
596 (let* ((feed-name (car feed))
597 (start-time (nth 2 feed))
598 (interval (or (nth 3 feed)
599 newsticker-retrieval-interval))
600 (timer (assoc (car feed)
601 newsticker--retrieval-timer-list)))
602 (if timer
603 (or do-not-complain-if-running
604 (message "Timer for %s is running already!"
605 feed-name))
606 (newsticker--debug-msg "Starting timer for %s: %s, %d"
607 feed-name start-time interval)
608 ;; do not repeat retrieval if interval not positive
609 (if (<= interval 0)
610 (setq interval nil))
611 ;; Suddenly XEmacs doesn't like start-time 0
612 (if (or (not start-time)
613 (and (numberp start-time) (= start-time 0)))
614 (setq start-time 1))
615 ;; (message "start-time %s" start-time)
616 (setq timer (run-at-time start-time interval
617 'newsticker-get-news feed-name))
618 (if interval
619 (add-to-list 'newsticker--retrieval-timer-list
620 (cons feed-name timer))))))
622 ;;;###autoload
623 (defun newsticker-start (&optional do-not-complain-if-running)
624 "Start the newsticker.
625 Start the timers for display and retrieval. If the newsticker, i.e. the
626 timers, are running already a warning message is printed unless
627 DO-NOT-COMPLAIN-IF-RUNNING is not nil.
628 Run `newsticker-start-hook' if newsticker was not running already."
629 (interactive)
630 (let ((running (newsticker-running-p)))
631 ;; read old cache if it exists and newsticker is not running
632 (unless running
633 (newsticker--cache-read))
634 ;; start retrieval timers -- one timer for each feed
635 (dolist (feed (append newsticker-url-list-defaults newsticker-url-list))
636 (newsticker--start-feed feed))
637 (unless running
638 (run-hooks 'newsticker-start-hook)
639 (message "Newsticker started!"))))
641 (defun newsticker--stop-feed (feed-name)
642 "Stop retrieval for feed FEED-NAME.
643 Delete the stopped name/timer pair from `newsticker--retrieval-timer-list'."
644 (let ((name-and-timer (assoc feed-name newsticker--retrieval-timer-list)))
645 (when name-and-timer
646 (cancel-timer (cdr name-and-timer))
647 (setq newsticker--retrieval-timer-list
648 (delete name-and-timer newsticker--retrieval-timer-list)))))
650 (defun newsticker-stop ()
651 "Stop the newsticker and the newsticker-ticker.
652 Cancel the timers for display and retrieval. Run `newsticker-stop-hook'
653 if newsticker has been running."
654 (interactive)
655 (newsticker--cache-save)
656 (when (fboundp 'newsticker-stop-ticker) ; silence compiler warnings
657 (newsticker-stop-ticker))
658 (when (newsticker-running-p)
659 (mapc (lambda (name-and-timer)
660 (newsticker--stop-feed (car name-and-timer)))
661 newsticker--retrieval-timer-list)
662 (setq newsticker--retrieval-timer-list nil)
663 (run-hooks 'newsticker-stop-hook)
664 (message "Newsticker stopped!")))
666 (defun newsticker-get-all-news ()
667 "Launch retrieval of news from all configured newsticker sites.
668 This does NOT start the retrieval timers."
669 (interactive)
670 ;; launch retrieval of news
671 (mapc (lambda (item)
672 (newsticker-get-news (car item)))
673 (append newsticker-url-list-defaults newsticker-url-list)))
675 (defun newsticker-save-item (feed item)
676 "Save FEED ITEM."
677 (interactive)
678 (let ((filename (read-string "Filename: "
679 (concat feed ":_"
680 (replace-regexp-in-string
681 " " "_" (newsticker--title item))
682 ".html"))))
683 (with-temp-buffer
684 (insert (newsticker--desc item))
685 (write-file filename t))))
687 (defun newsticker-add-url (url name)
688 "Add given URL under given NAME to `newsticker-url-list'.
689 If URL is nil it is searched at point."
690 (interactive
691 (list
692 (read-string "URL: "
693 (save-excursion
694 (end-of-line)
695 (and
696 (re-search-backward
697 "http://"
698 (if (> (point) (+ (point-min) 100))
699 (- (point) 100)
700 (point-min))
702 (re-search-forward
703 "http://[-a-zA-Z0-9&/_.]*"
704 (if (< (point) (- (point-max) 200))
705 (+ (point) 200)
706 (point-max))
708 (buffer-substring-no-properties (match-beginning 0)
709 (match-end 0)))))
710 (read-string "Name: ")))
711 (add-to-list 'newsticker-url-list (list name url nil nil nil) t)
712 (customize-variable 'newsticker-url-list))
714 (defun newsticker-customize ()
715 "Open the newsticker customization group."
716 (interactive)
717 (customize-group "newsticker"))
719 ;; ======================================================================
720 ;;; Local stuff
721 ;; ======================================================================
722 (defun newsticker--get-news-by-funcall (feed-name function)
723 "Get news for the site FEED-NAME by calling FUNCTION.
724 See `newsticker-get-news'."
725 (let ((buffername (concat " *newsticker-funcall-" feed-name "*")))
726 (save-excursion
727 (set-buffer (get-buffer-create buffername))
728 (erase-buffer)
729 (insert (string-to-multibyte (funcall function feed-name)))
730 (newsticker--sentinel-work nil t feed-name function
731 (current-buffer)))))
733 (defun newsticker--get-news-by-url (feed-name url)
734 "Get news for the site FEED-NAME from address URL using `url-retrieve'.
735 See `newsticker-get-news'."
736 (let ((coding-system-for-read 'no-conversion))
737 (condition-case error-data
738 (url-retrieve url 'newsticker--get-news-by-url-callback
739 (list feed-name))
740 (error (message "Error retrieving news from %s: %s" feed-name
741 error-data))))
742 (force-mode-line-update))
744 (defun newsticker--get-news-by-url-callback (status feed-name)
745 "Callback function for `newsticker--get-news-by-url'.
746 STATUS is the return status as delivered by `url-retrieve', and
747 FEED-NAME is the name of the feed that the news were retrieved
748 from."
749 (let ((buf (get-buffer-create (concat " *newsticker-url-" feed-name "*")))
750 (result (string-to-multibyte (buffer-string))))
751 (set-buffer buf)
752 (erase-buffer)
753 (insert result)
754 ;; remove MIME header
755 (goto-char (point-min))
756 (search-forward "\n\n")
757 (delete-region (point-min) (point))
758 ;; read the rss/atom contents
759 (newsticker--sentinel-work nil t feed-name "url-retrieve" (current-buffer))
760 (when status
761 (let ((status-type (car status))
762 (status-details (cdr status)))
763 (cond ((eq status-type :redirect)
764 ;; don't care about redirects
766 ((eq status-type :error)
767 (message "%s: Error while retrieving news from %s: %s: \"%s\""
768 (format-time-string "%A, %H:%M" (current-time))
769 feed-name
770 (car status-details) (cdr status-details))))))))
772 (defun newsticker--get-news-by-wget (feed-name url wget-arguments)
773 "Get news for the site FEED-NAME from address URL using wget.
774 WGET-ARGUMENTS is a list of arguments for wget.
775 See `newsticker-get-news'."
776 (let ((buffername (concat " *newsticker-wget-" feed-name "*")))
777 (save-excursion
778 (set-buffer (get-buffer-create buffername))
779 (erase-buffer)
780 ;; throw an error if there is an old wget-process around
781 (if (get-process feed-name)
782 (error "Another wget-process is running for %s" feed-name))
783 ;; start wget
784 (let* ((args (append wget-arguments (list url)))
785 (proc (apply 'start-process feed-name buffername
786 newsticker-wget-name args)))
787 (set-process-coding-system proc 'no-conversion 'no-conversion)
788 (set-process-sentinel proc 'newsticker--sentinel)
789 (setq newsticker--process-ids (cons (process-id proc)
790 newsticker--process-ids))
791 (force-mode-line-update)))))
793 (defun newsticker-get-news (feed-name)
794 "Get news from the site FEED-NAME and load feed logo.
795 FEED-NAME must be a string which occurs as the label (i.e. the first element)
796 in an element of `newsticker-url-list' or `newsticker-url-list-defaults'."
797 (newsticker--debug-msg "%s: Getting news for %s"
798 (format-time-string "%A, %H:%M" (current-time))
799 feed-name)
800 (let* ((item (or (assoc feed-name newsticker-url-list)
801 (assoc feed-name newsticker-url-list-defaults)
802 (error
803 "Cannot get news for %s: Check newsticker-url-list"
804 feed-name)))
805 (url (cadr item))
806 (wget-arguments (or (car (cdr (cdr (cdr (cdr item)))))
807 newsticker-wget-arguments)))
808 (if (functionp url)
809 (newsticker--get-news-by-funcall feed-name url)
810 (if (eq newsticker-retrieval-method 'intern)
811 (newsticker--get-news-by-url feed-name url)
812 (newsticker--get-news-by-wget feed-name url wget-arguments)))))
814 ;; ======================================================================
815 ;; Parsing
816 ;; ======================================================================
818 (defun newsticker--sentinel (process event)
819 "Sentinel for extracting news titles from an RDF buffer.
820 Argument PROCESS is the process which has just changed its state.
821 Argument EVENT tells what has happened to the process."
822 (let ((p-status (process-status process))
823 (exit-status (process-exit-status process))
824 (name (process-name process))
825 (command (process-command process))
826 (buffer (process-buffer process)))
827 (newsticker--sentinel-work event
828 (and (eq p-status 'exit)
829 (= exit-status 0))
830 name command buffer)))
832 (defun newsticker--sentinel-work (event status-ok name command buffer)
833 "Actually do the sentinel work.
834 Argument EVENT tells what has happened to the retrieval process.
835 Argument STATUS-OK is the final status of the retrieval process,
836 non-nil meaning retrieval was successful.
837 Argument NAME is the name of the retrieval process.
838 Argument COMMAND is the command of the retrieval process.
839 Argument BUFFER is the buffer of the retrieval process."
840 (let ((time (current-time))
841 (name-symbol (intern name))
842 (something-was-added nil))
843 ;; catch known errors (zombie processes, rubbish-xml etc.
844 ;; if an error occurs the news feed is not updated!
845 (catch 'oops
846 (unless status-ok
847 (setq newsticker--cache
848 (newsticker--cache-add
849 newsticker--cache
850 name-symbol
851 newsticker--error-headline
852 (format
853 (concat "%s: Newsticker could not retrieve news from %s.\n"
854 "Return status: `%s'\n"
855 "Command was `%s'")
856 (format-time-string "%A, %H:%M" (current-time))
857 name event command)
859 (current-time)
860 'new
861 0 nil))
862 (message "%s: Error while retrieving news from %s"
863 (format-time-string "%A, %H:%M" (current-time))
864 name)
865 (throw 'oops nil))
866 (let* ((coding-system 'utf-8)
867 (node-list
868 (save-current-buffer
869 (set-buffer buffer)
870 ;; a very very dirty workaround to overcome the
871 ;; problems with the newest (20030621) xml.el:
872 ;; remove all unnecessary whitespace
873 (goto-char (point-min))
874 (while (re-search-forward ">[ \t\r\n]+<" nil t)
875 (replace-match "><" nil t))
876 ;; and another brutal workaround (20031105)! For some
877 ;; reason the xml parser does not like the colon in the
878 ;; doctype name "rdf:RDF"
879 (goto-char (point-min))
880 (if (re-search-forward "<!DOCTYPE[ \t\n]+rdf:RDF" nil t)
881 (replace-match "<!DOCTYPE rdfColonRDF" nil t))
882 ;; finally.... ~##^°!!!!!
883 (goto-char (point-min))
884 (while (search-forward "\r\n" nil t)
885 (replace-match "\n" nil t))
886 ;; still more brutal workarounds (20040309)! The xml
887 ;; parser does not like doctype rss
888 (goto-char (point-min))
889 (if (re-search-forward "<!DOCTYPE[ \t\n]+rss[ \t\n]*>" nil t)
890 (replace-match "" nil t))
891 ;; And another one (20050618)! (Fixed in GNU Emacs 22.0.50.18)
892 ;; Remove comments to avoid this xml-parsing bug:
893 ;; "XML files can have only one toplevel tag"
894 (goto-char (point-min))
895 (while (search-forward "<!--" nil t)
896 (let ((start (match-beginning 0)))
897 (unless (search-forward "-->" nil t)
898 (error "Can't find end of comment"))
899 (delete-region start (point))))
900 ;; And another one (20050702)! If description is HTML
901 ;; encoded and starts with a `<', wrap the whole
902 ;; description in a CDATA expression. This happened for
903 ;; http://www.thefreedictionary.com/_/WoD/rss.aspx?type=quote
904 (goto-char (point-min))
905 (while (re-search-forward
906 "<description>\\(<img.*?\\)</description>" nil t)
907 (replace-match
908 "<description><![CDATA[ \\1 ]]></description>"))
909 ;; And another one (20051123)! XML parser does not
910 ;; like this: <yweather:location city="Frankfurt/Main"
911 ;; region="" country="GM" />
912 ;; try to "fix" empty attributes
913 ;; This happened for
914 ;; http://xml.weather.yahoo.com/forecastrss?p=GMXX0040&u=f
915 (goto-char (point-min))
916 (while (re-search-forward "\\(<[^>]*\\)=\"\"" nil t)
917 (replace-match "\\1=\" \""))
919 (set-buffer-modified-p nil)
920 ;; check coding system
921 (goto-char (point-min))
922 (if (re-search-forward "encoding=\"\\([^\"]+\\)\""
923 nil t)
924 (setq coding-system (intern (downcase (match-string 1))))
925 (setq coding-system
926 (condition-case nil
927 (check-coding-system coding-system)
928 (coding-system-error
929 (message
930 "newsticker.el: ignoring coding system %s for %s"
931 coding-system name)
932 nil))))
933 ;; Decode if possible
934 (when coding-system
935 (decode-coding-region (point-min) (point-max)
936 coding-system))
937 (condition-case errordata
938 ;; The xml parser might fail
939 ;; or the xml might be bugged
940 (xml-parse-region (point-min) (point-max))
941 (error (message "Could not parse %s: %s"
942 (buffer-name) (cadr errordata))
943 (throw 'oops nil)))))
944 (topnode (car node-list))
945 (channelnode (car (xml-get-children topnode 'channel)))
946 (imageurl nil))
947 ;; mark all items as obsolete
948 (newsticker--cache-replace-age newsticker--cache
949 name-symbol
950 'new 'obsolete-new)
951 (newsticker--cache-replace-age newsticker--cache
952 name-symbol
953 'old 'obsolete-old)
954 (newsticker--cache-replace-age newsticker--cache
955 name-symbol
956 'feed 'obsolete-old)
958 ;; check Atom/RSS version and call corresponding parser
959 (condition-case error-data
960 (if (cond
961 ;; RSS 0.91
962 ((and (eq 'rss (xml-node-name topnode))
963 (string= "0.91" (xml-get-attribute topnode 'version)))
964 (setq imageurl (newsticker--get-logo-url-rss-0.91 topnode))
965 (newsticker--parse-rss-0.91 name time topnode))
966 ;; RSS 0.92
967 ((and (eq 'rss (xml-node-name topnode))
968 (string= "0.92" (xml-get-attribute topnode 'version)))
969 (setq imageurl (newsticker--get-logo-url-rss-0.92 topnode))
970 (newsticker--parse-rss-0.92 name time topnode))
971 ;; RSS 1.0
972 ((eq 'rdf:RDF (xml-node-name topnode))
973 (setq imageurl (newsticker--get-logo-url-rss-1.0 topnode))
974 (newsticker--parse-rss-1.0 name time topnode))
975 ;; RSS 2.0
976 ((and (eq 'rss (xml-node-name topnode))
977 (string= "2.0" (xml-get-attribute topnode 'version)))
978 (setq imageurl (newsticker--get-logo-url-rss-2.0 topnode))
979 (newsticker--parse-rss-2.0 name time topnode))
980 ;; Atom 0.3
981 ((and (eq 'feed (xml-node-name topnode))
982 (string= "http://purl.org/atom/ns#"
983 (xml-get-attribute topnode 'xmlns)))
984 (setq imageurl (newsticker--get-logo-url-atom-0.3 topnode))
985 (newsticker--parse-atom-0.3 name time topnode))
986 ;; Atom 1.0
987 ((and (eq 'feed (xml-node-name topnode))
988 (string= "http://www.w3.org/2005/Atom"
989 (xml-get-attribute topnode 'xmlns)))
990 (setq imageurl (newsticker--get-logo-url-atom-1.0 topnode))
991 (newsticker--parse-atom-1.0 name time topnode))
992 ;; unknown feed type
994 (newsticker--debug-msg "Feed type unknown: %s: %s"
995 (xml-node-name topnode) name)
996 nil))
997 (setq something-was-added t))
998 (error (message "sentinelerror in %s: %s" name error-data)))
1000 ;; Remove those old items from cache which have been removed from
1001 ;; the feed
1002 (newsticker--cache-replace-age newsticker--cache
1003 name-symbol 'obsolete-old 'deleteme)
1004 (newsticker--cache-remove newsticker--cache name-symbol
1005 'deleteme)
1006 ;; Remove those new items from cache which have been removed from
1007 ;; the feed. Or keep them as `obsolete'
1008 (if (not newsticker-keep-obsolete-items)
1009 (newsticker--cache-remove newsticker--cache
1010 name-symbol 'obsolete-new)
1011 (setq newsticker--cache
1012 (newsticker--cache-mark-expired
1013 newsticker--cache name-symbol 'obsolete 'obsolete-expired
1014 newsticker-obsolete-item-max-age))
1015 (newsticker--cache-remove newsticker--cache
1016 name-symbol 'obsolete-expired)
1017 (newsticker--cache-replace-age newsticker--cache
1018 name-symbol 'obsolete-new
1019 'obsolete))
1020 (newsticker--update-process-ids)
1021 ;; setup scrollable text
1022 (when (= 0 (length newsticker--process-ids))
1023 (when (fboundp 'newsticker--ticker-text-setup) ;silence
1024 ;compiler
1025 ;warnings
1026 (newsticker--ticker-text-setup)))
1027 (setq newsticker--latest-update-time (current-time))
1028 (when something-was-added
1029 ;; FIXME: should we care about removed items as well?
1030 (newsticker--cache-save-feed
1031 (newsticker--cache-get-feed name-symbol))
1032 (when (fboundp 'newsticker--buffer-set-uptodate) ;silence
1033 ;compiler
1034 ;warnings
1035 (newsticker--buffer-set-uptodate nil)))
1036 ;; kill the process buffer if wanted
1037 (unless newsticker-debug
1038 (kill-buffer buffer))
1039 ;; launch retrieval of image
1040 (when (and imageurl newsticker--download-logos)
1041 (newsticker--image-get name imageurl)))))
1042 (when newsticker--sentinel-callback
1043 (funcall newsticker--sentinel-callback)))
1045 (defun newsticker--get-logo-url-atom-1.0 (node)
1046 "Return logo URL from atom 1.0 data in NODE."
1047 (car (xml-node-children
1048 (car (xml-get-children node 'logo)))))
1050 (defun newsticker--get-logo-url-atom-0.3 (node)
1051 "Return logo URL from atom 0.3 data in NODE."
1052 (car (xml-node-children
1053 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1055 (defun newsticker--get-logo-url-rss-2.0 (node)
1056 "Return logo URL from RSS 2.0 data in NODE."
1057 (car (xml-node-children
1058 (car (xml-get-children
1059 (car (xml-get-children
1060 (car (xml-get-children node 'channel)) 'image)) 'url)))))
1062 (defun newsticker--get-logo-url-rss-1.0 (node)
1063 "Return logo URL from RSS 1.0 data in NODE."
1064 (car (xml-node-children
1065 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1067 (defun newsticker--get-logo-url-rss-0.92 (node)
1068 "Return logo URL from RSS 0.92 data in NODE."
1069 (car (xml-node-children
1070 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1072 (defun newsticker--get-logo-url-rss-0.91 (node)
1073 "Return logo URL from RSS 0.91 data in NODE."
1074 (car (xml-node-children
1075 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1077 (defun newsticker--parse-atom-0.3 (name time topnode)
1078 "Parse Atom 0.3 data.
1079 Return value as well as arguments NAME, TIME, and TOPNODE are the
1080 same as in `newsticker--parse-atom-1.0'."
1081 (newsticker--debug-msg "Parsing Atom 0.3 feed %s" name)
1082 (let (new-feed new-item)
1083 (setq new-feed (newsticker--parse-generic-feed
1084 name time
1085 ;; title
1086 (car (xml-node-children
1087 (car (xml-get-children topnode 'title))))
1088 ;; desc
1089 (car (xml-node-children
1090 (car (xml-get-children topnode 'content))))
1091 ;; link
1092 (xml-get-attribute
1093 (car (xml-get-children topnode 'link)) 'href)
1094 ;; extra-elements
1095 (xml-node-children topnode)))
1096 (setq new-item (newsticker--parse-generic-items
1097 name time (xml-get-children topnode 'entry)
1098 ;; title-fn
1099 (lambda (node)
1100 (car (xml-node-children
1101 (car (xml-get-children node 'title)))))
1102 ;; desc-fn
1103 (lambda (node)
1104 (or (car (xml-node-children
1105 (car (xml-get-children node 'content))))
1106 (car (xml-node-children
1107 (car (xml-get-children node 'summary))))))
1108 ;; link-fn
1109 (lambda (node)
1110 (xml-get-attribute
1111 (car (xml-get-children node 'link)) 'href))
1112 ;; time-fn
1113 (lambda (node)
1114 (newsticker--decode-rfc822-date
1115 (car (xml-node-children
1116 (car (xml-get-children node 'modified))))))
1117 ;; guid-fn
1118 (lambda (node)
1119 (newsticker--guid-to-string
1120 (assoc 'guid (xml-node-children node))))
1121 ;; extra-fn
1122 (lambda (node)
1123 (xml-node-children node))))
1124 (or new-item new-feed)))
1126 (defun newsticker--parse-atom-1.0 (name time topnode)
1127 "Parse Atom 1.0 data.
1128 Argument NAME gives the name of a news feed. TIME gives the
1129 system time at which the data have been retrieved. TOPNODE
1130 contains the feed data as returned by the xml parser.
1132 For the Atom 1.0 specification see
1133 http://www.atompub.org/2005/08/17/draft-ietf-atompub-format-11.html"
1134 (newsticker--debug-msg "Parsing Atom 1.0 feed %s" name)
1135 (let (new-feed new-item)
1136 (setq new-feed (newsticker--parse-generic-feed
1137 name time
1138 ;; title
1139 (car (xml-node-children
1140 (car (xml-get-children topnode 'title))))
1141 ;; desc
1142 (car (xml-node-children
1143 (car (xml-get-children topnode 'subtitle))))
1144 ;; link
1145 (lambda (node)
1146 (xml-get-attribute
1147 (car (xml-get-children node 'link)) 'href))
1148 ;; extra-elements
1149 (xml-node-children topnode)))
1150 (setq new-item (newsticker--parse-generic-items
1151 name time (xml-get-children topnode 'entry)
1152 ;; title-fn
1153 (lambda (node)
1154 (car (xml-node-children
1155 (car (xml-get-children node 'title)))))
1156 ;; desc-fn
1157 (lambda (node)
1158 (or (car (xml-node-children
1159 (car (xml-get-children node 'content))))
1160 (car (xml-node-children
1161 (car (xml-get-children node 'summary))))))
1162 ;; link-fn
1163 (lambda (node)
1164 (xml-get-attribute
1165 (car (xml-get-children node 'link)) 'href))
1166 ;; time-fn
1167 (lambda (node)
1168 (newsticker--decode-iso8601-date
1169 (or (car (xml-node-children
1170 (car (xml-get-children node 'updated))))
1171 (car (xml-node-children
1172 (car (xml-get-children node 'published)))))))
1173 ;; guid-fn
1174 (lambda (node)
1175 (car (xml-node-children
1176 (car (xml-get-children node 'id)))))
1177 ;; extra-fn
1178 (lambda (node)
1179 (xml-node-children node))))
1180 (or new-item new-feed)))
1182 (defun newsticker--parse-rss-0.91 (name time topnode)
1183 "Parse RSS 0.91 data.
1184 Return value as well as arguments NAME, TIME, and TOPNODE are the
1185 same as in `newsticker--parse-atom-1.0'.
1187 For the RSS 0.91 specification see http://backend.userland.com/rss091 or
1188 http://my.netscape.com/publish/formats/rss-spec-0.91.html."
1189 (newsticker--debug-msg "Parsing RSS 0.91 feed %s" name)
1190 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1191 (pub-date (newsticker--decode-rfc822-date
1192 (car (xml-node-children
1193 (car (xml-get-children channelnode 'pubDate))))))
1194 is-new-feed has-new-items)
1195 (setq is-new-feed (newsticker--parse-generic-feed
1196 name time
1197 ;; title
1198 (car (xml-node-children
1199 (car (xml-get-children channelnode 'title))))
1200 ;; desc
1201 (car (xml-node-children
1202 (car (xml-get-children channelnode
1203 'description))))
1204 ;; link
1205 (car (xml-node-children
1206 (car (xml-get-children channelnode 'link))))
1207 ;; extra-elements
1208 (xml-node-children channelnode)))
1209 (setq has-new-items (newsticker--parse-generic-items
1210 name time (xml-get-children channelnode 'item)
1211 ;; title-fn
1212 (lambda (node)
1213 (car (xml-node-children
1214 (car (xml-get-children node 'title)))))
1215 ;; desc-fn
1216 (lambda (node)
1217 (car (xml-node-children
1218 (car (xml-get-children node 'description)))))
1219 ;; link-fn
1220 (lambda (node)
1221 (car (xml-node-children
1222 (car (xml-get-children node 'link)))))
1223 ;; time-fn
1224 (lambda (node)
1225 (newsticker--decode-rfc822-date
1226 (car (xml-node-children
1227 (car (xml-get-children node 'pubDate))))))
1228 ;; guid-fn
1229 (lambda (node)
1230 nil)
1231 ;; extra-fn
1232 (lambda (node)
1233 (xml-node-children node))))
1234 (or has-new-items is-new-feed)))
1236 (defun newsticker--parse-rss-0.92 (name time topnode)
1237 "Parse RSS 0.92 data.
1238 Return value as well as arguments NAME, TIME, and TOPNODE are the
1239 same as in `newsticker--parse-atom-1.0'.
1241 For the RSS 0.92 specification see http://backend.userland.com/rss092."
1242 (newsticker--debug-msg "Parsing RSS 0.92 feed %s" name)
1243 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1244 (pub-date (newsticker--decode-rfc822-date
1245 (car (xml-node-children
1246 (car (xml-get-children channelnode 'pubDate))))))
1247 is-new-feed has-new-items)
1248 (setq is-new-feed (newsticker--parse-generic-feed
1249 name time
1250 ;; title
1251 (car (xml-node-children
1252 (car (xml-get-children channelnode 'title))))
1253 ;; desc
1254 (car (xml-node-children
1255 (car (xml-get-children channelnode
1256 'description))))
1257 ;; link
1258 (car (xml-node-children
1259 (car (xml-get-children channelnode 'link))))
1260 ;; extra-elements
1261 (xml-node-children channelnode)))
1262 (setq has-new-items (newsticker--parse-generic-items
1263 name time (xml-get-children channelnode 'item)
1264 ;; title-fn
1265 (lambda (node)
1266 (car (xml-node-children
1267 (car (xml-get-children node 'title)))))
1268 ;; desc-fn
1269 (lambda (node)
1270 (car (xml-node-children
1271 (car (xml-get-children node 'description)))))
1272 ;; link-fn
1273 (lambda (node)
1274 (car (xml-node-children
1275 (car (xml-get-children node 'link)))))
1276 ;; time-fn
1277 (lambda (node)
1278 (newsticker--decode-rfc822-date
1279 (car (xml-node-children
1280 (car (xml-get-children node 'pubDate))))))
1281 ;; guid-fn
1282 (lambda (node)
1283 nil)
1284 ;; extra-fn
1285 (lambda (node)
1286 (xml-node-children node))))
1287 (or has-new-items is-new-feed)))
1289 (defun newsticker--parse-rss-1.0 (name time topnode)
1290 "Parse RSS 1.0 data.
1291 Return value as well as arguments NAME, TIME, and TOPNODE are the
1292 same as in `newsticker--parse-atom-1.0'.
1294 For the RSS 1.0 specification see http://web.resource.org/rss/1.0/spec."
1295 (newsticker--debug-msg "Parsing RSS 1.0 feed %s" name)
1296 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1297 is-new-feed has-new-items)
1298 (setq is-new-feed (newsticker--parse-generic-feed
1299 name time
1300 ;; title
1301 (car (xml-node-children
1302 (car (xml-get-children channelnode 'title))))
1303 ;; desc
1304 (car (xml-node-children
1305 (car (xml-get-children channelnode
1306 'description))))
1307 ;; link
1308 (car (xml-node-children
1309 (car (xml-get-children channelnode 'link))))
1310 ;; extra-elements
1311 (xml-node-children channelnode)))
1312 (setq has-new-items (newsticker--parse-generic-items
1313 name time (xml-get-children topnode 'item)
1314 ;; title-fn
1315 (lambda (node)
1316 (car (xml-node-children
1317 (car (xml-get-children node 'title)))))
1318 ;; desc-fn
1319 (lambda (node)
1320 (car (xml-node-children
1321 (car (xml-get-children node
1322 'description)))))
1323 ;; link-fn
1324 (lambda (node)
1325 (car (xml-node-children
1326 (car (xml-get-children node 'link)))))
1327 ;; time-fn
1328 (lambda (node)
1329 (newsticker--decode-iso8601-date
1330 (car (xml-node-children
1331 (car (xml-get-children node 'dc:date))))))
1332 ;; guid-fn
1333 (lambda (node)
1334 nil)
1335 ;; extra-fn
1336 (lambda (node)
1337 (xml-node-children node))))
1338 (or has-new-items is-new-feed)))
1340 (defun newsticker--parse-rss-2.0 (name time topnode)
1341 "Parse RSS 2.0 data.
1342 Return value as well as arguments NAME, TIME, and TOPNODE are the
1343 same as in `newsticker--parse-atom-1.0'.
1345 For the RSS 2.0 specification see http://blogs.law.harvard.edu/tech/rss."
1346 (newsticker--debug-msg "Parsing RSS 2.0 feed %s" name)
1347 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1348 is-new-feed has-new-items)
1349 (setq is-new-feed (newsticker--parse-generic-feed
1350 name time
1351 ;; title
1352 (car (xml-node-children
1353 (car (xml-get-children channelnode 'title))))
1354 ;; desc
1355 (car (xml-node-children
1356 (car (xml-get-children channelnode
1357 'description))))
1358 ;; link
1359 (car (xml-node-children
1360 (car (xml-get-children channelnode 'link))))
1361 ;; extra-elements
1362 (xml-node-children channelnode)))
1363 (setq has-new-items (newsticker--parse-generic-items
1364 name time (xml-get-children channelnode 'item)
1365 ;; title-fn
1366 (lambda (node)
1367 (car (xml-node-children
1368 (car (xml-get-children node 'title)))))
1369 ;; desc-fn
1370 (lambda (node)
1371 (or (car (xml-node-children
1372 (car (xml-get-children node
1373 'content:encoded))))
1374 (car (xml-node-children
1375 (car (xml-get-children node
1376 'description))))))
1377 ;; link-fn
1378 (lambda (node)
1379 (car (xml-node-children
1380 (car (xml-get-children node 'link)))))
1381 ;; time-fn
1382 (lambda (node)
1383 (newsticker--decode-rfc822-date
1384 (car (xml-node-children
1385 (car (xml-get-children node 'pubDate))))))
1386 ;; guid-fn
1387 (lambda (node)
1388 (newsticker--guid-to-string
1389 (assoc 'guid (xml-node-children node))))
1390 ;; extra-fn
1391 (lambda (node)
1392 (xml-node-children node))))
1393 (or has-new-items is-new-feed)))
1395 (defun newsticker--parse-generic-feed (name time title desc link
1396 extra-elements)
1397 "Parse generic news feed data.
1398 Argument NAME gives the name of a news feed. TIME gives the
1399 system time at which the data have been retrieved.
1401 The arguments TITLE, DESC, LINK, and EXTRA-ELEMENTS give the feed's title,
1402 description, link, and extra elements resp."
1403 (let ((title (or title "[untitled]"))
1404 (link (or link ""))
1405 (old-item nil)
1406 (position 0)
1407 (something-was-added nil))
1408 ;; decode numeric entities
1409 (setq title (newsticker--decode-numeric-entities title))
1410 (setq desc (newsticker--decode-numeric-entities desc))
1411 (setq link (newsticker--decode-numeric-entities link))
1412 ;; remove whitespace from title, desc, and link
1413 (setq title (newsticker--remove-whitespace title))
1414 (setq desc (newsticker--remove-whitespace desc))
1415 (setq link (newsticker--remove-whitespace link))
1417 ;; handle the feed itself
1418 (unless (newsticker--cache-contains newsticker--cache
1419 (intern name) title
1420 desc link 'feed)
1421 (setq something-was-added t))
1422 (setq newsticker--cache
1423 (newsticker--cache-add newsticker--cache (intern name)
1424 title desc link time 'feed position
1425 extra-elements time 'feed))
1426 something-was-added))
1428 (defun newsticker--parse-generic-items (name time itemlist
1429 title-fn desc-fn
1430 link-fn time-fn
1431 guid-fn extra-fn)
1432 "Parse generic news feed data.
1433 Argument NAME gives the name of a news feed. TIME gives the
1434 system time at which the data have been retrieved. ITEMLIST
1435 contains the news items returned by the xml parser.
1437 The arguments TITLE-FN, DESC-FN, LINK-FN, TIME-FN, GUID-FN, and
1438 EXTRA-FN give functions for extracting title, description, link,
1439 time, guid, and extra-elements resp. They are called with one
1440 argument, which is one of the items in ITEMLIST."
1441 (let (title desc link
1442 (old-item nil)
1443 (position 0)
1444 (something-was-added nil))
1445 ;; gather all items for this feed
1446 (mapc (lambda (node)
1447 (setq position (1+ position))
1448 (setq title (or (funcall title-fn node) "[untitled]"))
1449 (setq desc (funcall desc-fn node))
1450 (setq link (or (funcall link-fn node) ""))
1451 (setq time (or (funcall time-fn node) time))
1452 ;; It happened that the title or description
1453 ;; contained evil HTML code that confused the
1454 ;; xml parser. Therefore:
1455 (unless (stringp title)
1456 (setq title (prin1-to-string title)))
1457 (unless (or (stringp desc) (not desc))
1458 (setq desc (prin1-to-string desc)))
1459 ;; ignore items with empty title AND empty desc
1460 (when (or (> (length title) 0)
1461 (> (length desc) 0))
1462 ;; decode numeric entities
1463 (setq title (newsticker--decode-numeric-entities title))
1464 (when desc
1465 (setq desc (newsticker--decode-numeric-entities desc)))
1466 (setq link (newsticker--decode-numeric-entities link))
1467 ;; remove whitespace from title, desc, and link
1468 (setq title (newsticker--remove-whitespace title))
1469 (setq desc (newsticker--remove-whitespace desc))
1470 (setq link (newsticker--remove-whitespace link))
1471 ;; add data to cache
1472 ;; do we have this item already?
1473 (let* ((guid (funcall guid-fn node)))
1474 ;;(message "guid=%s" guid)
1475 (setq old-item
1476 (newsticker--cache-contains newsticker--cache
1477 (intern name) title
1478 desc link nil guid)))
1479 ;; add this item, or mark it as old, or do nothing
1480 (let ((age1 'new)
1481 (age2 'old)
1482 (item-new-p nil))
1483 (if old-item
1484 (let ((prev-age (newsticker--age old-item)))
1485 (unless newsticker-automatically-mark-items-as-old
1486 ;; Some feeds deliver items multiply, the
1487 ;; first time we find an 'obsolete-old one the
1488 ;; cache, the following times we find an 'old
1489 ;; one
1490 (if (memq prev-age '(obsolete-old old))
1491 (setq age2 'old)
1492 (setq age2 'new)))
1493 (if (eq prev-age 'immortal)
1494 (setq age2 'immortal))
1495 (setq time (newsticker--time old-item)))
1496 ;; item was not there
1497 (setq item-new-p t)
1498 (setq something-was-added t))
1499 (setq newsticker--cache
1500 (newsticker--cache-add
1501 newsticker--cache (intern name) title desc link
1502 time age1 position (funcall extra-fn node)
1503 time age2))
1504 (when item-new-p
1505 (let ((item (newsticker--cache-contains
1506 newsticker--cache (intern name) title
1507 desc link nil)))
1508 (if newsticker-auto-mark-filter-list
1509 (newsticker--run-auto-mark-filter name item))
1510 (run-hook-with-args
1511 'newsticker-new-item-functions name item))))))
1512 itemlist)
1513 something-was-added))
1515 ;; ======================================================================
1516 ;;; Misc
1517 ;; ======================================================================
1518 (defun newsticker--decode-numeric-entities (string)
1519 "Decode SGML numeric entities by their respective utf characters.
1520 This function replaces numeric entities in the input STRING and
1521 returns the modified string. For example \"&#42;\" gets replaced
1522 by \"*\"."
1523 (if (and string (stringp string))
1524 (let ((start 0))
1525 (while (string-match "&#\\([0-9]+\\);" string start)
1526 (condition-case nil
1527 (setq string (replace-match
1528 (string (read (substring string
1529 (match-beginning 1)
1530 (match-end 1))))
1531 nil nil string))
1532 (error nil))
1533 (setq start (1+ (match-beginning 0))))
1534 string)
1535 nil))
1537 (defun newsticker--remove-whitespace (string)
1538 "Remove leading and trailing whitespace from STRING."
1539 ;; we must have ...+ but not ...* in the regexps otherwise xemacs loops
1540 ;; endlessly...
1541 (when (and string (stringp string))
1542 (replace-regexp-in-string
1543 "[ \t\r\n]+$" ""
1544 (replace-regexp-in-string "^[ \t\r\n]+" "" string))))
1546 (defun newsticker--do-forget-preformatted (item)
1547 "Forget pre-formatted data for ITEM.
1548 Remove the pre-formatted from `newsticker--cache'."
1549 (if (nthcdr 7 item)
1550 (setcar (nthcdr 7 item) nil))
1551 (if (nthcdr 6 item)
1552 (setcar (nthcdr 6 item) nil)))
1554 (defun newsticker--forget-preformatted ()
1555 "Forget all cached pre-formatted data.
1556 Remove the pre-formatted from `newsticker--cache'."
1557 (mapc (lambda (feed)
1558 (mapc 'newsticker--do-forget-preformatted
1559 (cdr feed)))
1560 newsticker--cache)
1561 (when (fboundp 'newsticker--buffer-set-uptodate)
1562 (newsticker--buffer-set-uptodate nil)))
1564 (defun newsticker--debug-msg (string &rest args)
1565 "Print newsticker debug messages.
1566 This function calls `message' with arguments STRING and ARGS, if
1567 `newsticker-debug' is non-nil."
1568 (and newsticker-debug
1569 ;;(not (active-minibuffer-window))
1570 ;;(not (current-message))
1571 (apply 'message string args)))
1573 (defun newsticker--decode-iso8601-date (iso8601-string)
1574 "Return ISO8601-STRING in format like `decode-time'.
1575 Converts from ISO-8601 to Emacs representation.
1576 Examples:
1577 2004-09-17T05:09:49.001+00:00
1578 2004-09-17T05:09:49+00:00
1579 2004-09-17T05:09+00:00
1580 2004-09-17T05:09:49
1581 2004-09-17T05:09
1582 2004-09-17
1583 2004-09
1584 2004"
1585 (if iso8601-string
1586 (when (string-match
1587 (concat
1588 "^ *\\([0-9]\\{4\\}\\)" ;year
1589 "\\(-\\([0-9]\\{2\\}\\)" ;month
1590 "\\(-\\([0-9]\\{2\\}\\)" ;day
1591 "\\(T"
1592 "\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)" ;hour:minute
1593 "\\(:\\([0-9]\\{2\\}\\)\\(\\.[0-9]+\\)?\\)?" ;second
1594 ;timezone
1595 "\\(\\([-+Z]\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)?"
1596 "\\)?\\)?\\)? *$")
1597 iso8601-string)
1598 (let ((year (read (match-string 1 iso8601-string)))
1599 (month (read (or (match-string 3 iso8601-string)
1600 "1")))
1601 (day (read (or (match-string 5 iso8601-string)
1602 "1")))
1603 (hour (read (or (match-string 7 iso8601-string)
1604 "0")))
1605 (minute (read (or (match-string 8 iso8601-string)
1606 "0")))
1607 (second (read (or (match-string 10 iso8601-string)
1608 "0")))
1609 (sign (match-string 13 iso8601-string))
1610 (offset-hour (read (or (match-string 15 iso8601-string)
1611 "0")))
1612 (offset-minute (read (or (match-string 16 iso8601-string)
1613 "0"))))
1614 (cond ((string= sign "+")
1615 (setq hour (- hour offset-hour))
1616 (setq minute (- minute offset-minute)))
1617 ((string= sign "-")
1618 (setq hour (+ hour offset-hour))
1619 (setq minute (+ minute offset-minute))))
1620 ;; if UTC subtract current-time-zone offset
1621 ;;(setq second (+ (car (current-time-zone)) second)))
1623 (condition-case nil
1624 (encode-time second minute hour day month year t)
1625 (error
1626 (message "Cannot decode \"%s\"" iso8601-string)
1627 nil))))
1628 nil))
1630 (defun newsticker--decode-rfc822-date (rfc822-string)
1631 "Return RFC822-STRING in format like `decode-time'.
1632 Converts from RFC822 to Emacs representation.
1633 Examples:
1634 Sat, 07 September 2002 00:00:01 +0100
1635 Sat, 07 September 2002 00:00:01 MET
1636 Sat, 07 Sep 2002 00:00:01 GMT
1637 07 Sep 2002 00:00:01 GMT
1638 07 Sep 2002"
1639 (if (and rfc822-string (stringp rfc822-string))
1640 (when (string-match
1641 (concat
1642 "\\s-*"
1643 ;; week day
1644 "\\(\\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\)\\s-*,?\\)?\\s-*"
1645 ;; day
1646 "\\([0-9]\\{1,2\\}\\)\\s-+"
1647 ;; month
1648 "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|"
1649 "Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\).*?\\s-+"
1650 ;; year
1651 "\\([0-9]\\{2,4\\}\\)"
1652 ;; time may be missing
1653 "\\(\\s-+"
1654 ;; hour
1655 "\\([0-9]\\{2\\}\\)"
1656 ;; minute
1657 ":\\([0-9]\\{2\\}\\)"
1658 ;; second
1659 "\\(:\\([0-9]\\{2\\}\\)\\)?"
1660 ;; zone -- fixme
1661 "\\(\\s-+\\("
1662 "UT\\|GMT\\|EST\\|EDT\\|CST\\|CDT\\|MST\\|MDT\\|PST\\|PDT"
1663 "\\|\\([-+]\\)\\([0-9]\\{2\\}\\)\\([0-9]\\{2\\}\\)"
1664 "\\)\\)?"
1665 "\\)?")
1666 rfc822-string)
1667 (let ((day (read (match-string 3 rfc822-string)))
1668 (month-name (match-string 4 rfc822-string))
1669 (month 0)
1670 (year (read (match-string 5 rfc822-string)))
1671 (hour (read (or (match-string 7 rfc822-string) "0")))
1672 (minute (read (or (match-string 8 rfc822-string) "0")))
1673 (second (read (or (match-string 10 rfc822-string) "0")))
1674 (zone (match-string 12 rfc822-string))
1675 (sign (match-string 13 rfc822-string))
1676 (offset-hour (read (or (match-string 14 rfc822-string)
1677 "0")))
1678 (offset-minute (read (or (match-string 15 rfc822-string)
1679 "0")))
1680 ;;FIXME
1682 (when zone
1683 (cond ((string= sign "+")
1684 (setq hour (- hour offset-hour))
1685 (setq minute (- minute offset-minute)))
1686 ((string= sign "-")
1687 (setq hour (+ hour offset-hour))
1688 (setq minute (+ minute offset-minute)))))
1689 (condition-case error-data
1690 (let ((i 1))
1691 (mapc (lambda (m)
1692 (if (string= month-name m)
1693 (setq month i))
1694 (setq i (1+ i)))
1695 '("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
1696 "Sep" "Oct" "Nov" "Dec"))
1697 (encode-time second minute hour day month year t))
1698 (error
1699 (message "Cannot decode \"%s\": %s %s" rfc822-string
1700 (car error-data) (cdr error-data))
1701 nil))))
1702 nil))
1704 (defun newsticker--lists-intersect-p (list1 list2)
1705 "Return t if LIST1 and LIST2 share elements."
1706 (let ((result nil))
1707 (mapc (lambda (elt)
1708 (if (memq elt list2)
1709 (setq result t)))
1710 list1)
1711 result))
1713 (defun newsticker--update-process-ids ()
1714 "Update list of ids of active newsticker processes.
1715 Checks list of active processes against list of newsticker processes."
1716 (let ((active-procs (process-list))
1717 (new-list nil))
1718 (mapc (lambda (proc)
1719 (let ((id (process-id proc)))
1720 (if (memq id newsticker--process-ids)
1721 (setq new-list (cons id new-list)))))
1722 active-procs)
1723 (setq newsticker--process-ids new-list))
1724 (force-mode-line-update))
1726 ;; ======================================================================
1727 ;;; Images
1728 ;; ======================================================================
1729 (defun newsticker--images-dir ()
1730 "Return directory where feed images are saved."
1731 (concat newsticker-dir "/images"))
1733 (defun newsticker--image-get (feed-name url)
1734 "Get image of the news site FEED-NAME from URL.
1735 If the image has been downloaded in the last 24h do nothing."
1736 (let ((image-name (concat (newsticker--images-dir) feed-name)))
1737 (if (and (file-exists-p image-name)
1738 (time-less-p (current-time)
1739 (time-add (nth 5 (file-attributes image-name))
1740 (seconds-to-time 86400))))
1741 (newsticker--debug-msg "%s: Getting image for %s skipped"
1742 (format-time-string "%A, %H:%M" (current-time))
1743 feed-name)
1744 ;; download
1745 (newsticker--debug-msg "%s: Getting image for %s"
1746 (format-time-string "%A, %H:%M" (current-time))
1747 feed-name)
1748 (let* ((buffername (concat " *newsticker-wget-image-" feed-name "*"))
1749 (item (or (assoc feed-name newsticker-url-list)
1750 (assoc feed-name newsticker-url-list-defaults)
1751 (error
1752 "Cannot get news for %s: Check newsticker-url-list"
1753 feed-name)))
1754 (wget-arguments (or (car (cdr (cdr (cdr (cdr item)))))
1755 newsticker-wget-arguments)))
1756 (save-excursion
1757 (set-buffer (get-buffer-create buffername))
1758 (erase-buffer)
1759 ;; throw an error if there is an old wget-process around
1760 (if (get-process feed-name)
1761 (error "Another wget-process is running for image %s"
1762 feed-name))
1763 ;; start wget
1764 (let* ((args (append wget-arguments (list url)))
1765 (proc (apply 'start-process feed-name buffername
1766 newsticker-wget-name args)))
1767 (set-process-coding-system proc 'no-conversion 'no-conversion)
1768 (set-process-sentinel proc 'newsticker--image-sentinel)))))))
1770 (defun newsticker--image-sentinel (process event)
1771 "Sentinel for image-retrieving PROCESS caused by EVENT."
1772 (let* ((p-status (process-status process))
1773 (exit-status (process-exit-status process))
1774 (feed-name (process-name process)))
1775 ;; catch known errors (zombie processes, rubbish-xml, etc.)
1776 ;; if an error occurs the news feed is not updated!
1777 (catch 'oops
1778 (unless (and (eq p-status 'exit)
1779 (= exit-status 0))
1780 (message "%s: Error while retrieving image from %s"
1781 (format-time-string "%A, %H:%M" (current-time))
1782 feed-name)
1783 (throw 'oops nil))
1784 (let (image-name)
1785 (save-excursion
1786 (set-buffer (process-buffer process))
1787 (setq image-name (concat (newsticker--images-dir) feed-name))
1788 (set-buffer-file-coding-system 'no-conversion)
1789 ;; make sure the cache dir exists
1790 (unless (file-directory-p (newsticker--images-dir))
1791 (make-directory (newsticker--images-dir)))
1792 ;; write and close buffer
1793 (let ((require-final-newline nil)
1794 (backup-inhibited t)
1795 (coding-system-for-write 'no-conversion))
1796 (write-region nil nil image-name nil 'quiet))
1797 (set-buffer-modified-p nil)
1798 (kill-buffer (current-buffer)))))))
1800 (defun newsticker--insert-image (img string)
1801 "Insert IMG with STRING at point."
1802 (insert-image img string))
1804 ;; ======================================================================
1805 ;;; HTML rendering
1806 ;; ======================================================================
1807 (defun newsticker-htmlr-render (pos1 pos2) ;
1808 "Replacement for `htmlr-render'.
1809 Renders the HTML code in the region POS1 to POS2 using htmlr."
1810 (let ((str (buffer-substring-no-properties pos1 pos2)))
1811 (delete-region pos1 pos2)
1812 (insert
1813 (with-temp-buffer
1814 (insert str)
1815 (goto-char (point-min))
1816 ;; begin original htmlr-render
1817 (when (fboundp 'htmlr-reset) (htmlr-reset))
1818 ;; something omitted here...
1819 (when (fboundp 'htmlr-step)
1820 (while (< (point) (point-max))
1821 (htmlr-step)))
1822 ;; end original htmlr-render
1823 (newsticker--remove-whitespace (buffer-string))))))
1825 ;; ======================================================================
1826 ;;; Manipulation of cached data
1827 ;; ======================================================================
1828 (defun newsticker--cache-set-preformatted-contents (item contents)
1829 "Set preformatted contents of ITEM to CONTENTS."
1830 (if (nthcdr 6 item)
1831 (setcar (nthcdr 6 item) contents)
1832 (setcdr (nthcdr 5 item) (list contents))))
1834 (defun newsticker--cache-set-preformatted-title (item title)
1835 "Set preformatted title of ITEM to TITLE."
1836 (if (nthcdr 7 item)
1837 (setcar (nthcdr 7 item) title)
1838 (setcdr (nthcdr 6 item) title)))
1840 (defun newsticker--cache-replace-age (data feed old-age new-age)
1841 "Mark all items in DATA in FEED which carry age OLD-AGE with NEW-AGE.
1842 If FEED is 'any it applies to all feeds. If OLD-AGE is 'any,
1843 all marks are replaced by NEW-AGE. Removes all pre-formatted contents."
1844 (mapc (lambda (a-feed)
1845 (when (or (eq feed 'any)
1846 (eq (car a-feed) feed))
1847 (let ((items (cdr a-feed)))
1848 (mapc (lambda (item)
1849 (when (or (eq old-age 'any)
1850 (eq (newsticker--age item) old-age))
1851 (setcar (nthcdr 4 item) new-age)
1852 (newsticker--do-forget-preformatted item)))
1853 items))))
1854 data)
1855 data)
1857 (defun newsticker--cache-mark-expired (data feed old-age new-age time)
1858 "Mark all expired entries.
1859 This function sets the age entries in DATA in the feed FEED. If
1860 an item's age is OLD-AGE it is set to NEW-AGE if the item is
1861 older than TIME."
1862 (mapc
1863 (lambda (a-feed)
1864 (when (or (eq feed 'any)
1865 (eq (car a-feed) feed))
1866 (let ((items (cdr a-feed)))
1867 (mapc
1868 (lambda (item)
1869 (when (eq (newsticker--age item) old-age)
1870 (let ((exp-time (time-add (newsticker--time item)
1871 (seconds-to-time time))))
1872 (when (time-less-p exp-time (current-time))
1873 (newsticker--debug-msg
1874 "Item `%s' from %s has expired on %s"
1875 (newsticker--title item)
1876 (format-time-string "%Y-%02m-%d, %H:%M"
1877 (newsticker--time item))
1878 (format-time-string "%Y-%02m-%d, %H:%M" exp-time))
1879 (setcar (nthcdr 4 item) new-age)))))
1880 items))))
1881 data)
1882 data)
1884 (defun newsticker--cache-contains (data feed title desc link age
1885 &optional guid)
1886 "Check DATA whether FEED contains an item with the given properties.
1887 This function returns the contained item or nil if it is not
1888 contained.
1889 The properties which are checked are TITLE, DESC, LINK, AGE, and
1890 GUID. In general all properties must match in order to return a
1891 certain item, except for the following cases.
1893 If AGE equals 'feed the TITLE, DESCription and LINK do not
1894 matter. If DESC is nil it is ignored as well. If
1895 `newsticker-desc-comp-max' is non-nil, only the first
1896 `newsticker-desc-comp-max' characters of DESC are taken into
1897 account.
1899 If GUID is non-nil it is sufficient to match this value, and the
1900 other properties are ignored."
1901 ;;(newsticker--debug-msg "Looking for %s guid=%s" title guid)
1902 (condition-case nil
1903 (catch 'found
1904 (when (and desc newsticker-desc-comp-max
1905 (> (length desc) newsticker-desc-comp-max))
1906 (setq desc (substring desc 0 newsticker-desc-comp-max)))
1907 (mapc
1908 (lambda (this-feed)
1909 (when (eq (car this-feed) feed)
1910 (mapc (lambda (anitem)
1911 (when (cond (guid
1912 ;; global unique id can match
1913 (string= guid (newsticker--guid anitem)))
1914 (t;;FIXME?
1916 ;; or title, desc, etc.
1917 (and
1918 ;;(or (not (eq age 'feed))
1919 ;; (eq (newsticker--age anitem) 'feed))
1920 (string= (newsticker--title anitem)
1921 title)
1922 (or (not link)
1923 (string= (newsticker--link anitem)
1924 link))
1925 (or (not desc)
1926 (if (and desc newsticker-desc-comp-max
1927 (> (length (newsticker--desc
1928 anitem))
1929 newsticker-desc-comp-max))
1930 (string= (substring
1931 (newsticker--desc anitem)
1933 newsticker-desc-comp-max)
1934 desc)
1935 (string= (newsticker--desc anitem)
1936 desc)))))))
1937 ;;(newsticker--debug-msg "Found %s guid=%s"
1938 ;; (newsticker--title anitem)
1939 ;; (newsticker--guid anitem))
1940 (throw 'found anitem)))
1941 (cdr this-feed))))
1942 data)
1943 ;;(newsticker--debug-msg "Found nothing")
1944 nil)
1945 (error nil)))
1947 (defun newsticker--cache-add (data feed-name-symbol title desc link time age
1948 position extra-elements
1949 &optional updated-time updated-age
1950 preformatted-contents
1951 preformatted-title)
1952 "Add another item to cache data.
1953 Add to DATA in the FEED-NAME-SYMBOL an item with TITLE, DESC,
1954 LINK, TIME, AGE, POSITION, and EXTRA-ELEMENTS. If this item is
1955 contained already, its time is set to UPDATED-TIME, its mark is
1956 set to UPDATED-AGE, and its pre-formatted contents is set to
1957 PREFORMATTED-CONTENTS and PREFORMATTED-TITLE. Returns the age
1958 which the item got."
1959 (let* ((guid (newsticker--guid-to-string (assoc 'guid extra-elements)))
1960 (item (newsticker--cache-contains data feed-name-symbol title desc link
1961 age guid)))
1962 ;;(message "guid=%s" guid)
1963 (if item
1964 ;; does exist already -- change age, update time and position
1965 (progn
1966 ;;(newsticker--debug-msg "Updating item %s %s %s %s %s -> %s %s
1967 ;; (guid %s -> %s)"
1968 ;; feed-name-symbol title link time age
1969 ;; updated-time updated-age
1970 ;; guid (newsticker--guid item))
1971 (if (nthcdr 5 item)
1972 (setcar (nthcdr 5 item) position)
1973 (setcdr (nthcdr 4 item) (list position)))
1974 (setcar (nthcdr 4 item) updated-age)
1975 (if updated-time
1976 (setcar (nthcdr 3 item) updated-time))
1977 ;; replace cached pre-formatted contents
1978 (newsticker--cache-set-preformatted-contents
1979 item preformatted-contents)
1980 (newsticker--cache-set-preformatted-title
1981 item preformatted-title))
1982 ;; did not exist or age equals 'feed-name-symbol
1983 (setq item (list title desc link time age position preformatted-contents
1984 preformatted-title extra-elements))
1985 ;;(newsticker--debug-msg "Adding item %s" item)
1986 (catch 'found
1987 (mapc (lambda (this-feed)
1988 (when (eq (car this-feed) feed-name-symbol)
1989 (setcdr this-feed (nconc (cdr this-feed) (list item)))
1990 (throw 'found this-feed)))
1991 data)
1992 ;; the feed is not contained
1993 (add-to-list 'data (list feed-name-symbol item) t))))
1994 data)
1996 (defun newsticker--cache-remove (data feed-symbol age)
1997 "Remove all entries from DATA in the feed FEED-SYMBOL with AGE.
1998 FEED-SYMBOL may be 'any. Entries from old feeds, which are no longer in
1999 `newsticker-url-list' or `newsticker-url-list-defaults', are removed as
2000 well."
2001 (let* ((pos data)
2002 (feed (car pos))
2003 (last-pos nil))
2004 (while feed
2005 (if (or (assoc (symbol-name (car feed)) newsticker-url-list)
2006 (assoc (symbol-name (car feed)) newsticker-url-list-defaults))
2007 ;; feed is still valid=active
2008 ;; (message "Keeping feed %s" (car feed))
2009 (if (or (eq feed-symbol 'any)
2010 (eq feed-symbol (car feed)))
2011 (let* ((item-pos (cdr feed))
2012 (item (car item-pos))
2013 (prev-pos nil))
2014 (while item
2015 ;;(message "%s" (car item))
2016 (if (eq age (newsticker--age item))
2017 ;; remove this item
2018 (progn
2019 ;;(message "Removing item %s" (car item))
2020 (if prev-pos
2021 (setcdr prev-pos (cdr item-pos))
2022 (setcdr feed (cdr item-pos))))
2023 ;;(message "Keeping item %s" (car item))
2024 (setq prev-pos item-pos))
2025 (setq item-pos (cdr item-pos))
2026 (setq item (car item-pos)))))
2027 ;; feed is not active anymore
2028 ;; (message "Removing feed %s" (car feed))
2029 (if last-pos
2030 (setcdr last-pos (cdr pos))
2031 (setq data (cdr pos))))
2032 (setq last-pos pos)
2033 (setq pos (cdr pos))
2034 (setq feed (car pos)))))
2036 ;; ======================================================================
2037 ;;; Sorting
2038 ;; ======================================================================
2039 (defun newsticker--cache-item-compare-by-time (item1 item2)
2040 "Compare two news items ITEM1 and ITEM2 by comparing their time values."
2041 (catch 'result
2042 (let ((age1 (newsticker--age item1))
2043 (age2 (newsticker--age item2)))
2044 (if (not (eq age1 age2))
2045 (cond ((eq age1 'obsolete)
2046 (throw 'result nil))
2047 ((eq age2 'obsolete)
2048 (throw 'result t)))))
2049 (let* ((time1 (newsticker--time item1))
2050 (time2 (newsticker--time item2)))
2051 (cond ((< (nth 0 time1) (nth 0 time2))
2052 nil)
2053 ((> (nth 0 time1) (nth 0 time2))
2055 ((< (nth 1 time1) (nth 1 time2))
2056 nil)
2057 ((> (nth 1 time1) (nth 1 time2))
2059 ((< (or (nth 2 time1) 0) (or (nth 2 time2) 0))
2060 nil)
2061 ((> (or (nth 2 time1) 0) (or (nth 2 time2) 0))
2064 nil)))))
2066 (defun newsticker--cache-item-compare-by-title (item1 item2)
2067 "Compare ITEM1 and ITEM2 by comparing their titles."
2068 (catch 'result
2069 (let ((age1 (newsticker--age item1))
2070 (age2 (newsticker--age item2)))
2071 (if (not (eq age1 age2))
2072 (cond ((eq age1 'obsolete)
2073 (throw 'result nil))
2074 ((eq age2 'obsolete)
2075 (throw 'result t)))))
2076 (string< (newsticker--title item1) (newsticker--title item2))))
2078 (defun newsticker--cache-item-compare-by-position (item1 item2)
2079 "Compare ITEM1 and ITEM2 by comparing their original positions."
2080 (catch 'result
2081 (let ((age1 (newsticker--age item1))
2082 (age2 (newsticker--age item2)))
2083 (if (not (eq age1 age2))
2084 (cond ((eq age1 'obsolete)
2085 (throw 'result nil))
2086 ((eq age2 'obsolete)
2087 (throw 'result t)))))
2088 (< (or (newsticker--pos item1) 0) (or (newsticker--pos item2) 0))))
2090 (defun newsticker--cache-save-version1 ()
2091 "Update and save newsticker cache file."
2092 (interactive)
2093 (newsticker--cache-update t))
2095 (defun newsticker--cache-update (&optional save)
2096 "Update newsticker cache file.
2097 If optional argument SAVE is not nil the cache file is saved to disk."
2098 (save-excursion
2099 (unless (file-directory-p newsticker-dir)
2100 (make-directory newsticker-dir t))
2101 (let ((coding-system-for-write 'utf-8)
2102 (buf (find-file-noselect newsticker-cache-filename)))
2103 (when buf
2104 (set-buffer buf)
2105 (setq buffer-undo-list t)
2106 (erase-buffer)
2107 (insert ";; -*- coding: utf-8 -*-\n")
2108 (insert (prin1-to-string newsticker--cache))
2109 (when save
2110 (save-buffer))))))
2112 (defun newsticker--cache-get-feed (feed)
2113 "Return the cached data for the feed FEED.
2114 FEED is a symbol!"
2115 (assoc feed newsticker--cache))
2117 (defun newsticker--cache-dir ()
2118 "Return directory for saving cache data."
2119 (concat newsticker-dir "/feeds"))
2121 (defun newsticker--cache-save ()
2122 "Save cache data for all feeds."
2123 (unless (file-directory-p newsticker-dir)
2124 (make-directory newsticker-dir t))
2125 (mapc 'newsticker--cache-save-feed newsticker--cache)
2126 nil)
2128 (defun newsticker--cache-save-feed (feed)
2129 "Save cache data for FEED."
2130 (let ((dir (concat (newsticker--cache-dir) "/" (symbol-name (car feed)))))
2131 (unless (file-directory-p dir)
2132 (make-directory dir t))
2133 (let ((coding-system-for-write 'utf-8))
2134 (with-temp-file (concat dir "/data")
2135 (insert ";; -*- coding: utf-8 -*-\n")
2136 (insert (prin1-to-string (cdr feed)))))))
2138 (defun newsticker--cache-read-version1 ()
2139 "Read version1 cache data."
2140 (let ((coding-system-for-read 'utf-8))
2141 (when (file-exists-p newsticker-cache-filename)
2142 (with-temp-buffer
2143 (insert-file-contents newsticker-cache-filename)
2144 (goto-char (point-min))
2145 (condition-case nil
2146 (setq newsticker--cache (read (current-buffer)))
2147 (error
2148 (message "Error while reading newsticker cache file!")
2149 (setq newsticker--cache nil)))))))
2151 (defun newsticker--cache-read ()
2152 "Read cache data."
2153 (setq newsticker--cache nil)
2154 (if (file-exists-p newsticker-cache-filename)
2155 (progn
2156 (when (y-or-n-p "Old newsticker cache file exists. Read it? ")
2157 (newsticker--cache-read-version1))
2158 (when (y-or-n-p (format "Delete old newsticker cache file? "))
2159 (delete-file newsticker-cache-filename)))
2160 (mapc (lambda (f)
2161 (newsticker--cache-read-feed (car f)))
2162 (append newsticker-url-list-defaults newsticker-url-list))))
2164 (defun newsticker--cache-read-feed (feed-name)
2165 "Read cache data for feed named FEED-NAME."
2166 (let ((file-name (concat (newsticker--cache-dir) "/" feed-name "/data"))
2167 (coding-system-for-read 'utf-8))
2168 (when (file-exists-p file-name)
2169 (with-temp-buffer
2170 (insert-file-contents file-name)
2171 (goto-char (point-min))
2172 (condition-case nil
2173 (add-to-list 'newsticker--cache (cons (intern feed-name)
2174 (read (current-buffer))))
2175 (error
2176 (message "Error while reading newsticker cache file %s!"
2177 file-name))
2178 (setq newsticker--cache nil))))))
2180 ;; ======================================================================
2181 ;;; Statistics
2182 ;; ======================================================================
2183 (defun newsticker--stat-num-items (feed &rest ages)
2184 "Return number of items in the given FEED which have one of the given AGES.
2185 If AGES is nil, the total number of items is returned."
2186 (let ((items (cdr (newsticker--cache-get-feed feed)))
2187 (num 0))
2188 (while items
2189 (if ages
2190 (if (memq (newsticker--age (car items)) ages)
2191 (setq num (1+ num)))
2192 (if (memq (newsticker--age (car items)) '(new old immortal obsolete))
2193 (setq num (1+ num))))
2194 (setq items (cdr items)))
2195 num))
2197 (defun newsticker--stat-num-items-total (&optional age)
2198 "Return total number of items in all feeds which have the given AGE.
2199 If AGE is nil, the total number of items is returned."
2200 (apply '+
2201 (mapcar (lambda (feed)
2202 (if age
2203 (newsticker--stat-num-items (intern (car feed)) age)
2204 (newsticker--stat-num-items (intern (car feed)))))
2205 (append newsticker-url-list-defaults newsticker-url-list))))
2207 ;; ======================================================================
2208 ;;; OPML
2209 ;; ======================================================================
2210 (defun newsticker-opml-export ()
2211 "OPML subscription export.
2212 Export subscriptions to a buffer in OPML Format."
2213 (interactive)
2214 (with-current-buffer (get-buffer-create "*OPML Export*")
2215 (set-buffer-file-coding-system 'utf-8)
2216 (insert (concat
2217 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
2218 "<!-- OPML generated by Emacs newsticker.el -->\n"
2219 "<opml version=\"1.0\">\n"
2220 " <head>\n"
2221 " <title>mySubscriptions</title>\n"
2222 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
2223 "</dateCreated>\n"
2224 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
2225 " <ownerName>" (user-full-name) "</ownerName>\n"
2226 " </head>\n"
2227 " <body>\n"))
2228 (mapc (lambda (sub)
2229 (insert " <outline text=\"")
2230 (insert (newsticker--title sub))
2231 (insert "\" xmlUrl=\"")
2232 (insert (cadr sub))
2233 (insert "\"/>\n"))
2234 (append newsticker-url-list newsticker-url-list-defaults))
2235 (insert " </body>\n</opml>\n"))
2236 (pop-to-buffer "*OPML Export*")
2237 (when (fboundp 'sgml-mode)
2238 (sgml-mode)))
2240 (defun newsticker--opml-import-outlines (outlines)
2241 "Recursively import OUTLINES from OPML data.
2242 Note that nested outlines are currently flattened -- i.e. grouping is
2243 removed."
2244 (mapc (lambda (outline)
2245 (let ((name (xml-get-attribute outline 'text))
2246 (url (xml-get-attribute outline 'xmlUrl))
2247 (children (xml-get-children outline 'outline)))
2248 (unless (string= "" url)
2249 (add-to-list 'newsticker-url-list
2250 (list name url nil nil nil) t))
2251 (if children
2252 (newsticker--opml-import-outlines children))))
2253 outlines))
2255 (defun newsticker-opml-import (filename)
2256 "Import OPML data from FILENAME."
2257 (interactive "fOPML file: ")
2258 (set-buffer (find-file-noselect filename))
2259 (goto-char (point-min))
2260 (let* ((node-list (xml-parse-region (point-min) (point-max)))
2261 (body (car (xml-get-children (car node-list) 'body)))
2262 (outlines (xml-get-children body 'outline)))
2263 (newsticker--opml-import-outlines outlines))
2264 (customize-variable 'newsticker-url-list))
2266 ;; ======================================================================
2267 ;;; Auto marking
2268 ;; ======================================================================
2269 (defun newsticker--run-auto-mark-filter (feed item)
2270 "Automatically mark an item as old or immortal.
2271 This function checks the variable `newsticker-auto-mark-filter-list'
2272 for an entry that matches FEED and ITEM."
2273 (let ((case-fold-search t))
2274 (mapc (lambda (filter)
2275 (let ((filter-feed (car filter))
2276 (pattern-list (cadr filter)))
2277 (when (string-match filter-feed feed)
2278 (newsticker--do-run-auto-mark-filter item pattern-list))))
2279 newsticker-auto-mark-filter-list)))
2281 (defun newsticker--do-run-auto-mark-filter (item list)
2282 "Actually compare ITEM against the pattern-LIST.
2283 LIST must be an element of `newsticker-auto-mark-filter-list'."
2284 (mapc (lambda (pattern)
2285 (let ((age (nth 0 pattern))
2286 (place (nth 1 pattern))
2287 (regexp (nth 2 pattern))
2288 (title (newsticker--title item))
2289 (desc (newsticker--desc item)))
2290 (when (or (eq place 'title) (eq place 'all))
2291 (when (and title (string-match regexp title))
2292 (newsticker--debug-msg "Auto-marking as %s: `%s'"
2293 age (newsticker--title item))
2294 (setcar (nthcdr 4 item) age)))
2295 (when (or (eq place 'description) (eq place 'all))
2296 (when (and desc (string-match regexp desc))
2297 (newsticker--debug-msg "Auto-marking as %s: `%s'"
2298 age (newsticker--title item))
2299 (setcar (nthcdr 4 item) age)))))
2300 list))
2303 ;; ======================================================================
2304 ;;; Hook samples
2305 ;; ======================================================================
2306 (defun newsticker-new-item-functions-sample (feed item)
2307 "Demonstrate the use of the `newsticker-new-item-functions' hook.
2308 This function just prints out the values of the FEED and title of the ITEM."
2309 (message (concat "newsticker-new-item-functions-sample: feed=`%s', "
2310 "title=`%s'")
2311 feed (newsticker--title item)))
2313 (defun newsticker-download-images (feed item)
2314 "Download the first image.
2315 If FEED equals \"imagefeed\" download the first image URL found
2316 in the description=contents of ITEM to the directory
2317 \"~/tmp/newsticker/FEED/TITLE\" where TITLE is the title of the item."
2318 (when (string= feed "imagefeed")
2319 (let ((title (newsticker--title item))
2320 (desc (newsticker--desc item)))
2321 (when (string-match "<img src=\"\\(http://[^ \"]+\\)\"" desc)
2322 (let ((url (substring desc (match-beginning 1) (match-end 1)))
2323 (temp-dir (concat "~/tmp/newsticker/" feed "/" title))
2324 (org-dir default-directory))
2325 (unless (file-directory-p temp-dir)
2326 (make-directory temp-dir t))
2327 (cd temp-dir)
2328 (message "Getting image %s" url)
2329 (apply 'start-process "wget-image"
2330 " *newsticker-wget-download-images*"
2331 newsticker-wget-name
2332 (list url))
2333 (cd org-dir))))))
2335 (defun newsticker-download-enclosures (feed item)
2336 "In all FEEDs download the enclosed object of the news ITEM.
2337 The object is saved to the directory \"~/tmp/newsticker/FEED/TITLE\", which
2338 is created if it does not exist. TITLE is the title of the news
2339 item. Argument FEED is ignored.
2340 This function is suited for adding it to `newsticker-new-item-functions'."
2341 (let ((title (newsticker--title item))
2342 (enclosure (newsticker--enclosure item)))
2343 (when enclosure
2344 (let ((url (cdr (assoc 'url enclosure)))
2345 (temp-dir (concat "~/tmp/newsticker/" feed "/" title))
2346 (org-dir default-directory))
2347 (unless (file-directory-p temp-dir)
2348 (make-directory temp-dir t))
2349 (cd temp-dir)
2350 (message "Getting enclosure %s" url)
2351 (apply 'start-process "wget-enclosure"
2352 " *newsticker-wget-download-enclosures*"
2353 newsticker-wget-name
2354 (list url))
2355 (cd org-dir)))))
2357 ;; ======================================================================
2358 ;;; Retrieve samples
2359 ;; ======================================================================
2360 (defun newsticker-retrieve-random-message (feed-name)
2361 "Return an artificial RSS string under the name FEED-NAME."
2362 (concat "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?><rss version=\"0.91\">"
2363 "<channel>"
2364 "<title>newsticker-retrieve-random-message</title>"
2365 "<description>Sample retrieval function</description>"
2366 "<pubDate>FIXME Sat, 07 Sep 2005 00:00:01 GMT</pubDate>"
2367 "<item><title>" (format "Your lucky number is %d" (random 10000))
2368 "</title><description>" (format "Or maybe it is %d" (random 10000))
2369 "</description></item></channel></rss>"))
2371 (provide 'newsticker-backend)
2373 ;; arch-tag: 0e37b658-56e9-49ab-90f9-f2df57e1a659
2374 ;;; newst-backend.el ends here