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