Newsticker: Show feedicons in treeview. Small fix in opml export.
[emacs.git] / lisp / net / newst-backend.el
blob4052116074d1d1d508a9031ccb6a1825e81b5eb1
1 ;;; newst-backend.el --- Retrieval backend for newsticker.
3 ;; Copyright (C) 2003-2014 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 on 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")
749 (delete-region (point-min) (point))
750 ;; read the rss/atom contents
751 (newsticker--sentinel-work nil t feed-name "url-retrieve" (current-buffer))
752 (when status
753 (let ((status-type (car status))
754 (status-details (cdr status)))
755 (cond ((eq status-type :redirect)
756 ;; don't care about redirects
758 ((eq status-type :error)
759 (message "%s: Error while retrieving news from %s: %s: \"%s\""
760 (format-time-string "%A, %H:%M" (current-time))
761 feed-name
762 (car status-details) (cdr status-details))))))))
764 (defun newsticker--get-news-by-wget (feed-name url wget-arguments)
765 "Get news for the site FEED-NAME from address URL using wget.
766 WGET-ARGUMENTS is a list of arguments for wget.
767 See `newsticker-get-news'."
768 (let ((buffername (concat " *newsticker-wget-" feed-name "*")))
769 (with-current-buffer (get-buffer-create buffername)
770 (erase-buffer)
771 ;; throw an error if there is an old wget-process around
772 (if (get-process feed-name)
773 (error "Another wget-process is running for %s" feed-name))
774 ;; start wget
775 (let* ((args (append wget-arguments (list url)))
776 (proc (apply 'start-process feed-name buffername
777 newsticker-wget-name args)))
778 (set-process-coding-system proc 'no-conversion 'no-conversion)
779 (set-process-sentinel proc 'newsticker--sentinel)
780 (process-put proc 'nt-feed-name feed-name)
781 (setq newsticker--process-ids (cons (process-id proc)
782 newsticker--process-ids))
783 (force-mode-line-update)))))
785 (defun newsticker-get-news (feed-name)
786 "Get news from the site FEED-NAME and load feed logo.
787 FEED-NAME must be a string which occurs as the label (i.e. the first element)
788 in an element of `newsticker-url-list' or `newsticker-url-list-defaults'."
789 (newsticker--debug-msg "%s: Getting news for %s"
790 (format-time-string "%A, %H:%M" (current-time))
791 feed-name)
792 (let* ((item (or (assoc feed-name newsticker-url-list)
793 (assoc feed-name newsticker-url-list-defaults)
794 (error
795 "Cannot get news for %s: Check newsticker-url-list"
796 feed-name)))
797 (url (cadr item))
798 (wget-arguments (or (car (cdr (cdr (cdr (cdr item)))))
799 newsticker-wget-arguments)))
800 (if (functionp url)
801 (newsticker--get-news-by-funcall feed-name url)
802 (if (eq newsticker-retrieval-method 'intern)
803 (newsticker--get-news-by-url feed-name url)
804 (newsticker--get-news-by-wget feed-name url wget-arguments)))))
806 ;; ======================================================================
807 ;; Parsing
808 ;; ======================================================================
810 (defun newsticker--sentinel (process event)
811 "Sentinel for extracting news titles from an RDF buffer.
812 Argument PROCESS is the process which has just changed its state.
813 Argument EVENT tells what has happened to the process."
814 (let ((p-status (process-status process))
815 (exit-status (process-exit-status process))
816 (feed-name (process-get process 'nt-feed-name))
817 (command (process-command process))
818 (buffer (process-buffer process)))
819 (newsticker--sentinel-work event
820 (and (eq p-status 'exit)
821 (= exit-status 0))
822 feed-name command buffer)))
824 (defun newsticker--sentinel-work (event status-ok feed-name command buffer)
825 "Actually do the sentinel work.
826 Argument EVENT tells what has happened to the retrieval process.
827 Argument STATUS-OK is the final status of the retrieval process,
828 non-nil meaning retrieval was successful.
829 Argument FEED-NAME is the name of the retrieved feed.
830 Argument COMMAND is the command of the retrieval process.
831 Argument BUFFER is the buffer of the retrieval process."
832 (let ((time (current-time))
833 (name-symbol (intern feed-name))
834 (something-was-added nil))
835 ;; catch known errors (zombie processes, rubbish-xml etc.
836 ;; if an error occurs the news feed is not updated!
837 (catch 'oops
838 (unless status-ok
839 (setq newsticker--cache
840 (newsticker--cache-add
841 newsticker--cache
842 name-symbol
843 newsticker--error-headline
844 (format
845 (concat "%s: Newsticker could not retrieve news from %s.\n"
846 "Return status: `%s'\n"
847 "Command was `%s'")
848 (format-time-string "%A, %H:%M" (current-time))
849 feed-name event command)
851 (current-time)
852 'new
853 0 nil))
854 (message "%s: Error while retrieving news from %s"
855 (format-time-string "%A, %H:%M" (current-time))
856 feed-name)
857 (throw 'oops nil))
858 (let* ((coding-system 'utf-8)
859 (node-list
860 (save-current-buffer
861 (set-buffer buffer)
862 (unless (fboundp 'libxml-parse-xml-region)
863 (newsticker--do-xml-workarounds))
864 ;; check coding system
865 (goto-char (point-min))
866 (if (re-search-forward "encoding=['\"]\\([^\"]+?\\)['\"]"
867 nil t)
868 (setq coding-system (intern (downcase (match-string 1))))
869 (setq coding-system
870 (condition-case nil
871 (check-coding-system coding-system)
872 (coding-system-error
873 (message
874 "newsticker.el: ignoring coding system %s for %s"
875 coding-system feed-name)
876 nil))))
877 ;; Decode if possible
878 (when coding-system
879 (decode-coding-region (point-min) (point-max)
880 coding-system))
881 (condition-case errordata
882 ;; The xml parser might fail
883 ;; or the xml might be bugged
884 (if (fboundp 'libxml-parse-xml-region)
885 (list (libxml-parse-xml-region (point-min) (point-max)))
886 (xml-parse-region (point-min) (point-max)))
887 (error (message "Could not parse %s: %s"
888 (buffer-name) (cadr errordata))
889 (throw 'oops nil)))))
890 (topnode (car node-list))
891 (image-url nil)
892 (icon-url nil))
893 ;; mark all items as obsolete
894 (newsticker--cache-replace-age newsticker--cache
895 name-symbol
896 'new 'obsolete-new)
897 (newsticker--cache-replace-age newsticker--cache
898 name-symbol
899 'old 'obsolete-old)
900 (newsticker--cache-replace-age newsticker--cache
901 name-symbol
902 'feed 'obsolete-old)
904 ;; check Atom/RSS version and call corresponding parser
905 (condition-case error-data
906 (if (cond
907 ;; RSS 0.91
908 ((and (eq 'rss (xml-node-name topnode))
909 (string= "0.91" (xml-get-attribute topnode 'version)))
910 (setq image-url (newsticker--get-logo-url-rss-0.91 topnode))
911 (newsticker--parse-rss-0.91 feed-name time topnode))
912 ;; RSS 0.92
913 ((and (eq 'rss (xml-node-name topnode))
914 (string= "0.92" (xml-get-attribute topnode 'version)))
915 (setq image-url (newsticker--get-logo-url-rss-0.92 topnode))
916 (newsticker--parse-rss-0.92 feed-name time topnode))
917 ;; RSS 1.0
918 ((or (eq 'RDF (xml-node-name topnode))
919 (eq 'rdf:RDF (xml-node-name topnode)))
920 (setq image-url (newsticker--get-logo-url-rss-1.0 topnode))
921 (newsticker--parse-rss-1.0 feed-name time topnode))
922 ;; RSS 2.0
923 ((and (eq 'rss (xml-node-name topnode))
924 (string= "2.0" (xml-get-attribute topnode 'version)))
925 (setq image-url (newsticker--get-logo-url-rss-2.0 topnode))
926 (newsticker--parse-rss-2.0 feed-name time topnode))
927 ;; Atom 0.3
928 ((and (eq 'feed (xml-node-name topnode))
929 (string= "http://purl.org/atom/ns#"
930 (xml-get-attribute topnode 'xmlns)))
931 (setq image-url (newsticker--get-logo-url-atom-0.3 topnode))
932 (newsticker--parse-atom-0.3 feed-name time topnode))
933 ;; Atom 1.0
935 ;; The test for Atom 1.0 does not work when using
936 ;; libxml, as with libxml the namespace attribute is
937 ;; not in the xml tree. For the time being we skip
938 ;; the check and assume that we are dealing with an
939 ;; Atom 1.0 feed.
941 ;; (and (eq 'feed (xml-node-name topnode))
942 ;; (string= "http://www.w3.org/2005/Atom"
943 ;; (xml-get-attribute topnode 'xmlns)))
944 (setq image-url (newsticker--get-logo-url-atom-1.0 topnode))
945 (setq icon-url (newsticker--get-icon-url-atom-1.0 topnode))
946 (newsticker--parse-atom-1.0 feed-name time topnode))
947 ;; unknown feed type
948 ;; (t
949 ;; (newsticker--debug-msg "Feed type unknown: %s: %s"
950 ;; (xml-node-name topnode) feed-name)
951 ;; nil)
953 (setq something-was-added t))
954 (error (message "sentinelerror in %s: %s" feed-name error-data)))
956 ;; Remove those old items from cache which have been removed from
957 ;; the feed
958 (newsticker--cache-replace-age newsticker--cache
959 name-symbol 'obsolete-old 'deleteme)
960 (newsticker--cache-remove newsticker--cache name-symbol
961 'deleteme)
962 ;; Remove those new items from cache which have been removed from
963 ;; the feed. Or keep them as `obsolete'
964 (if (not newsticker-keep-obsolete-items)
965 (newsticker--cache-remove newsticker--cache
966 name-symbol 'obsolete-new)
967 (setq newsticker--cache
968 (newsticker--cache-mark-expired
969 newsticker--cache name-symbol 'obsolete 'obsolete-expired
970 newsticker-obsolete-item-max-age))
971 (newsticker--cache-remove newsticker--cache
972 name-symbol 'obsolete-expired)
973 (newsticker--cache-replace-age newsticker--cache
974 name-symbol 'obsolete-new
975 'obsolete))
976 (newsticker--update-process-ids)
977 ;; setup scrollable text
978 (when (= 0 (length newsticker--process-ids))
979 (when (fboundp 'newsticker--ticker-text-setup) ;silence
980 ;compiler
981 ;warnings
982 (newsticker--ticker-text-setup)))
983 (setq newsticker--latest-update-time (current-time))
984 (when something-was-added
985 ;; FIXME: should we care about removed items as well?
986 (newsticker--cache-save-feed
987 (newsticker--cache-get-feed name-symbol))
988 (when (fboundp 'newsticker--buffer-set-uptodate) ;silence
989 ;compiler
990 ;warnings
991 (newsticker--buffer-set-uptodate nil)))
992 ;; kill the process buffer if wanted
993 (unless newsticker-debug
994 (kill-buffer buffer))
995 ;; launch retrieval of images
996 (when (and (boundp 'newsticker-download-logos)
997 newsticker-download-logos)
998 ;; feed logo
999 (when image-url
1000 (newsticker--image-get feed-name feed-name (newsticker--images-dir)
1001 image-url))
1002 ;; icon / favicon
1003 (setq icon-url
1004 (or icon-url
1005 (let* ((feed-url (newsticker--link (cadr (newsticker--cache-get-feed
1006 (intern feed-name)))))
1007 (uri (url-generic-parse-url feed-url)))
1008 (when (and feed-url uri)
1009 (setf (url-filename uri) nil)
1010 (setf (url-target uri) nil)
1011 (concat (url-recreate-url uri) "favicon.ico")))))
1012 (when icon-url
1013 (newsticker--image-get feed-name
1014 (concat feed-name "."
1015 (file-name-extension icon-url))
1016 (newsticker--icons-dir)
1017 icon-url))))))
1018 (when newsticker--sentinel-callback
1019 (funcall newsticker--sentinel-callback)))
1021 (defun newsticker--do-xml-workarounds ()
1022 "Fix all issues which `xml-parse-region' could be choking on."
1024 ;; a very very dirty workaround to overcome the
1025 ;; problems with the newest (20030621) xml.el:
1026 ;; remove all unnecessary whitespace
1027 (goto-char (point-min))
1028 (while (re-search-forward ">[ \t\r\n]+<" nil t)
1029 (replace-match "><" nil t))
1030 ;; and another brutal workaround (20031105)! For some
1031 ;; reason the xml parser does not like the colon in the
1032 ;; doctype name "rdf:RDF"
1033 (goto-char (point-min))
1034 (if (re-search-forward "<!DOCTYPE[ \t\n]+rdf:RDF" nil t)
1035 (replace-match "<!DOCTYPE rdfColonRDF" nil t))
1036 ;; finally.... ~##^°!!!!!
1037 (goto-char (point-min))
1038 (while (search-forward "\r\n" nil t)
1039 (replace-match "\n" nil t))
1040 ;; still more brutal workarounds (20040309)! The xml
1041 ;; parser does not like doctype rss
1042 (goto-char (point-min))
1043 (if (re-search-forward "<!DOCTYPE[ \t\n]+rss[ \t\n]*>" nil t)
1044 (replace-match "" nil t))
1045 ;; And another one (20050618)! (Fixed in GNU Emacs 22.0.50.18)
1046 ;; Remove comments to avoid this xml-parsing bug:
1047 ;; "XML files can have only one toplevel tag"
1048 (goto-char (point-min))
1049 (while (search-forward "<!--" nil t)
1050 (let ((start (match-beginning 0)))
1051 (unless (search-forward "-->" nil t)
1052 (error "Can't find end of comment"))
1053 (delete-region start (point))))
1054 ;; And another one (20050702)! If description is HTML
1055 ;; encoded and starts with a `<', wrap the whole
1056 ;; description in a CDATA expression. This happened for
1057 ;; http://www.thefreedictionary.com/_/WoD/rss.aspx?type=quote
1058 (goto-char (point-min))
1059 (while (re-search-forward
1060 "<description>\\(<img.*?\\)</description>" nil t)
1061 (replace-match
1062 "<description><![CDATA[ \\1 ]]></description>"))
1063 ;; And another one (20051123)! XML parser does not
1064 ;; like this: <yweather:location city="Frankfurt/Main"
1065 ;; region="" country="GM" />
1066 ;; try to "fix" empty attributes
1067 ;; This happened for
1068 ;; http://xml.weather.yahoo.com/forecastrss?p=GMXX0040&u=f
1069 (goto-char (point-min))
1070 (while (re-search-forward "\\(<[^>]*\\)=\"\"" nil t)
1071 (replace-match "\\1=\" \""))
1073 (set-buffer-modified-p nil))
1076 (defun newsticker--get-logo-url-atom-1.0 (node)
1077 "Return logo URL from atom 1.0 data in NODE."
1078 (car (xml-node-children
1079 (car (xml-get-children node 'logo)))))
1081 (defun newsticker--get-icon-url-atom-1.0 (node)
1082 "Return icon URL from atom 1.0 data in NODE."
1083 (car (xml-node-children
1084 (car (xml-get-children node 'icon)))))
1086 (defun newsticker--get-logo-url-atom-0.3 (node)
1087 "Return logo URL from atom 0.3 data in NODE."
1088 (car (xml-node-children
1089 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1091 (defun newsticker--get-logo-url-rss-2.0 (node)
1092 "Return logo URL from RSS 2.0 data in NODE."
1093 (car (xml-node-children
1094 (car (xml-get-children
1095 (car (xml-get-children
1096 (car (xml-get-children node 'channel)) 'image)) 'url)))))
1098 (defun newsticker--get-logo-url-rss-1.0 (node)
1099 "Return logo URL from RSS 1.0 data in NODE."
1100 (car (xml-node-children
1101 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1103 (defun newsticker--get-logo-url-rss-0.92 (node)
1104 "Return logo URL from RSS 0.92 data in NODE."
1105 (car (xml-node-children
1106 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1108 (defun newsticker--get-logo-url-rss-0.91 (node)
1109 "Return logo URL from RSS 0.91 data in NODE."
1110 (car (xml-node-children
1111 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1113 (defun newsticker--parse-atom-0.3 (name time topnode)
1114 "Parse Atom 0.3 data.
1115 Return value as well as arguments NAME, TIME, and TOPNODE are the
1116 same as in `newsticker--parse-atom-1.0'."
1117 (newsticker--debug-msg "Parsing Atom 0.3 feed %s" name)
1118 (let (new-feed new-item)
1119 (setq new-feed (newsticker--parse-generic-feed
1120 name time
1121 ;; title
1122 (car (xml-node-children
1123 (car (xml-get-children topnode 'title))))
1124 ;; desc
1125 (car (xml-node-children
1126 (car (xml-get-children topnode 'content))))
1127 ;; link
1128 (xml-get-attribute
1129 (car (xml-get-children topnode 'link)) 'href)
1130 ;; extra-elements
1131 (xml-node-children topnode)))
1132 (setq new-item (newsticker--parse-generic-items
1133 name time (xml-get-children topnode 'entry)
1134 ;; title-fn
1135 (lambda (node)
1136 (car (xml-node-children
1137 (car (xml-get-children node 'title)))))
1138 ;; desc-fn
1139 (lambda (node)
1140 (or (car (xml-node-children
1141 (car (xml-get-children node 'content))))
1142 (car (xml-node-children
1143 (car (xml-get-children node 'summary))))))
1144 ;; link-fn
1145 (lambda (node)
1146 (xml-get-attribute
1147 (car (xml-get-children node 'link)) 'href))
1148 ;; time-fn
1149 (lambda (node)
1150 (newsticker--decode-rfc822-date
1151 (car (xml-node-children
1152 (car (xml-get-children node 'modified))))))
1153 ;; guid-fn
1154 (lambda (node)
1155 (newsticker--guid-to-string
1156 (assoc 'guid (xml-node-children node))))
1157 ;; extra-fn
1158 (lambda (node)
1159 (xml-node-children node))))
1160 (or new-item new-feed)))
1162 (defun newsticker--unxml (node)
1163 "Reverse parsing of an xml string.
1164 Restore an xml-string from a an xml NODE that was returned by xml-parse..."
1165 (if (or (not node) (stringp node))
1166 node
1167 (newsticker--unxml-node node)))
1169 (defun newsticker--unxml-node (node)
1170 "Actually restore xml-string of an xml NODE."
1171 (let ((qname (symbol-name (car node)))
1172 (att-list (cadr node))
1173 (children (cddr node)))
1174 (concat "<" qname
1175 (when att-list " ")
1176 (mapconcat 'newsticker--unxml-attribute att-list " ")
1178 (mapconcat 'newsticker--unxml children "") "</" qname ">")))
1180 (defun newsticker--unxml-attribute (attribute)
1181 "Actually restore xml-string of an ATTRIBUTE of an xml node."
1182 (let ((name (symbol-name (car attribute)))
1183 (value (cdr attribute)))
1184 (concat name "=\"" value "\"")))
1186 (defun newsticker--parse-atom-1.0 (name time topnode)
1187 "Parse Atom 1.0 data.
1188 Argument NAME gives the name of a news feed. TIME gives the
1189 system time at which the data have been retrieved. TOPNODE
1190 contains the feed data as returned by the xml parser.
1192 For the Atom 1.0 specification see
1193 URL `http://www.atompub.org/2005/08/17/draft-ietf-atompub-format-11.html'"
1194 (newsticker--debug-msg "Parsing Atom 1.0 feed %s" name)
1195 (let (new-feed new-item)
1196 (setq new-feed (newsticker--parse-generic-feed
1197 name time
1198 ;; title
1199 (car (xml-node-children
1200 (car (xml-get-children topnode 'title))))
1201 ;; desc
1202 (car (xml-node-children
1203 (car (xml-get-children topnode 'subtitle))))
1204 ;; link
1205 (lambda (node)
1206 (xml-get-attribute
1207 (car (xml-get-children node 'link)) 'href))
1208 ;; extra-elements
1209 (xml-node-children topnode)))
1210 (setq new-item (newsticker--parse-generic-items
1211 name time (xml-get-children topnode 'entry)
1212 ;; title-fn
1213 (lambda (node)
1214 (car (xml-node-children
1215 (car (xml-get-children node 'title)))))
1216 ;; desc-fn
1217 (lambda (node)
1218 ;; unxml the content node. Atom allows for
1219 ;; integrating (x)html into the atom structure
1220 ;; but we need the raw html string.
1221 ;; e.g. http://www.heise.de/open/news/news-atom.xml
1222 (or (newsticker--unxml
1223 (car (xml-node-children
1224 (car (xml-get-children node 'content)))))
1225 (car (xml-node-children
1226 (car (xml-get-children node 'summary))))))
1227 ;; link-fn
1228 (lambda (node)
1229 (xml-get-attribute
1230 (car (xml-get-children node 'link)) 'href))
1231 ;; time-fn
1232 (lambda (node)
1233 (newsticker--decode-iso8601-date
1234 (or (car (xml-node-children
1235 (car (xml-get-children node 'updated))))
1236 (car (xml-node-children
1237 (car (xml-get-children node 'published)))))))
1238 ;; guid-fn
1239 (lambda (node)
1240 (car (xml-node-children
1241 (car (xml-get-children node 'id)))))
1242 ;; extra-fn
1243 (lambda (node)
1244 (xml-node-children node))))
1245 (or new-item new-feed)))
1247 (defun newsticker--parse-rss-0.91 (name time topnode)
1248 "Parse RSS 0.91 data.
1249 Return value as well as arguments NAME, TIME, and TOPNODE are the
1250 same as in `newsticker--parse-atom-1.0'.
1252 For the RSS 0.91 specification see URL `http://backend.userland.com/rss091'
1253 or URL `http://my.netscape.com/publish/formats/rss-spec-0.91.html'."
1254 (newsticker--debug-msg "Parsing RSS 0.91 feed %s" name)
1255 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1256 (pub-date (newsticker--decode-rfc822-date
1257 (car (xml-node-children
1258 (car (xml-get-children channelnode 'pubDate))))))
1259 is-new-feed has-new-items)
1260 (setq is-new-feed (newsticker--parse-generic-feed
1261 name time
1262 ;; title
1263 (car (xml-node-children
1264 (car (xml-get-children channelnode 'title))))
1265 ;; desc
1266 (car (xml-node-children
1267 (car (xml-get-children channelnode
1268 'description))))
1269 ;; link
1270 (car (xml-node-children
1271 (car (xml-get-children channelnode 'link))))
1272 ;; extra-elements
1273 (xml-node-children channelnode)))
1274 (setq has-new-items (newsticker--parse-generic-items
1275 name time (xml-get-children channelnode 'item)
1276 ;; title-fn
1277 (lambda (node)
1278 (car (xml-node-children
1279 (car (xml-get-children node 'title)))))
1280 ;; desc-fn
1281 (lambda (node)
1282 (car (xml-node-children
1283 (car (xml-get-children node 'description)))))
1284 ;; link-fn
1285 (lambda (node)
1286 (car (xml-node-children
1287 (car (xml-get-children node 'link)))))
1288 ;; time-fn
1289 (lambda (node)
1290 (newsticker--decode-rfc822-date
1291 (car (xml-node-children
1292 (car (xml-get-children node 'pubDate))))))
1293 ;; guid-fn
1294 (lambda (node)
1295 nil)
1296 ;; extra-fn
1297 (lambda (node)
1298 (xml-node-children node))))
1299 (or has-new-items is-new-feed)))
1301 (defun newsticker--parse-rss-0.92 (name time topnode)
1302 "Parse RSS 0.92 data.
1303 Return value as well as arguments NAME, TIME, and TOPNODE are the
1304 same as in `newsticker--parse-atom-1.0'.
1306 For the RSS 0.92 specification see URL `http://backend.userland.com/rss092'."
1307 (newsticker--debug-msg "Parsing RSS 0.92 feed %s" name)
1308 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1309 (pub-date (newsticker--decode-rfc822-date
1310 (car (xml-node-children
1311 (car (xml-get-children channelnode 'pubDate))))))
1312 is-new-feed has-new-items)
1313 (setq is-new-feed (newsticker--parse-generic-feed
1314 name time
1315 ;; title
1316 (car (xml-node-children
1317 (car (xml-get-children channelnode 'title))))
1318 ;; desc
1319 (car (xml-node-children
1320 (car (xml-get-children channelnode
1321 'description))))
1322 ;; link
1323 (car (xml-node-children
1324 (car (xml-get-children channelnode 'link))))
1325 ;; extra-elements
1326 (xml-node-children channelnode)))
1327 (setq has-new-items (newsticker--parse-generic-items
1328 name time (xml-get-children channelnode 'item)
1329 ;; title-fn
1330 (lambda (node)
1331 (car (xml-node-children
1332 (car (xml-get-children node 'title)))))
1333 ;; desc-fn
1334 (lambda (node)
1335 (car (xml-node-children
1336 (car (xml-get-children node 'description)))))
1337 ;; link-fn
1338 (lambda (node)
1339 (car (xml-node-children
1340 (car (xml-get-children node 'link)))))
1341 ;; time-fn
1342 (lambda (node)
1343 (newsticker--decode-rfc822-date
1344 (car (xml-node-children
1345 (car (xml-get-children node 'pubDate))))))
1346 ;; guid-fn
1347 (lambda (node)
1348 nil)
1349 ;; extra-fn
1350 (lambda (node)
1351 (xml-node-children node))))
1352 (or has-new-items is-new-feed)))
1354 (defun newsticker--parse-rss-1.0 (name time topnode)
1355 "Parse RSS 1.0 data.
1356 Return value as well as arguments NAME, TIME, and TOPNODE are the
1357 same as in `newsticker--parse-atom-1.0'.
1359 For the RSS 1.0 specification see URL `http://web.resource.org/rss/1.0/spec'."
1360 (newsticker--debug-msg "Parsing RSS 1.0 feed %s" name)
1361 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1362 is-new-feed has-new-items)
1363 (setq is-new-feed (newsticker--parse-generic-feed
1364 name time
1365 ;; title
1366 (car (xml-node-children
1367 (car (xml-get-children channelnode 'title))))
1368 ;; desc
1369 (or (car (xml-node-children
1370 (car (xml-get-children channelnode
1371 'encoded))))
1372 (car (xml-node-children
1373 (car (xml-get-children channelnode
1374 'content:encoded))))
1375 (car (xml-node-children
1376 (car (xml-get-children channelnode
1377 'description)))))
1378 ;; link
1379 (car (xml-node-children
1380 (car (xml-get-children channelnode 'link))))
1381 ;; extra-elements
1382 (xml-node-children channelnode)))
1383 (setq has-new-items (newsticker--parse-generic-items
1384 name time (xml-get-children topnode 'item)
1385 ;; title-fn
1386 (lambda (node)
1387 (car (xml-node-children
1388 (car (xml-get-children node 'title)))))
1389 ;; desc-fn
1390 (lambda (node)
1391 (car (xml-node-children
1392 (car (xml-get-children node
1393 'description)))))
1394 ;; link-fn
1395 (lambda (node)
1396 (car (xml-node-children
1397 (car (xml-get-children node 'link)))))
1398 ;; time-fn
1399 (lambda (node)
1400 (newsticker--decode-iso8601-date
1401 (or (car (xml-node-children
1402 (car (xml-get-children node 'dc:date))))
1403 (car (xml-node-children
1404 (car (xml-get-children node 'date)))))))
1405 ;; guid-fn
1406 (lambda (node)
1407 nil)
1408 ;; extra-fn
1409 (lambda (node)
1410 (xml-node-children node))))
1411 (or has-new-items is-new-feed)))
1413 (defun newsticker--parse-rss-2.0 (name time topnode)
1414 "Parse RSS 2.0 data.
1415 Return value as well as arguments NAME, TIME, and TOPNODE are the
1416 same as in `newsticker--parse-atom-1.0'.
1418 For the RSS 2.0 specification see URL `http://blogs.law.harvard.edu/tech/rss'."
1419 (newsticker--debug-msg "Parsing RSS 2.0 feed %s" name)
1420 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1421 is-new-feed has-new-items)
1422 (setq is-new-feed (newsticker--parse-generic-feed
1423 name time
1424 ;; title
1425 (car (xml-node-children
1426 (car (xml-get-children channelnode 'title))))
1427 ;; desc
1428 (or (car (xml-node-children
1429 (car (xml-get-children channelnode
1430 'encoded))))
1431 (car (xml-node-children
1432 (car (xml-get-children channelnode
1433 'content:encoded))))
1434 (car (xml-node-children
1435 (car (xml-get-children channelnode
1436 'description)))))
1437 ;; link
1438 (car (xml-node-children
1439 (car (xml-get-children channelnode 'link))))
1440 ;; extra-elements
1441 (xml-node-children channelnode)))
1442 (setq has-new-items (newsticker--parse-generic-items
1443 name time (xml-get-children channelnode 'item)
1444 ;; title-fn
1445 (lambda (node)
1446 (car (xml-node-children
1447 (car (xml-get-children node 'title)))))
1448 ;; desc-fn
1449 (lambda (node)
1450 (or (car (xml-node-children
1451 (car (xml-get-children node
1452 'encoded))))
1453 (car (xml-node-children
1454 (car (xml-get-children node
1455 'content:encoded))))
1456 (car (xml-node-children
1457 (car (xml-get-children node
1458 'description))))))
1459 ;; link-fn
1460 (lambda (node)
1461 (car (xml-node-children
1462 (car (xml-get-children node 'link)))))
1463 ;; time-fn
1464 (lambda (node)
1465 (newsticker--decode-rfc822-date
1466 (car (xml-node-children
1467 (car (xml-get-children node 'pubDate))))))
1468 ;; guid-fn
1469 (lambda (node)
1470 (newsticker--guid-to-string
1471 (assoc 'guid (xml-node-children node))))
1472 ;; extra-fn
1473 (lambda (node)
1474 (xml-node-children node))))
1475 (or has-new-items is-new-feed)))
1477 (defun newsticker--parse-generic-feed (name time title desc link
1478 extra-elements)
1479 "Parse generic news feed data.
1480 Argument NAME gives the name of a news feed. TIME gives the
1481 system time at which the data have been retrieved.
1483 The arguments TITLE, DESC, LINK, and EXTRA-ELEMENTS give the feed's title,
1484 description, link, and extra elements resp."
1485 (let ((title (or title "[untitled]"))
1486 (link (or link ""))
1487 (old-item nil)
1488 (position 0)
1489 (something-was-added nil))
1490 ;; decode numeric entities
1491 (setq title (xml-substitute-numeric-entities title))
1492 (setq desc (xml-substitute-numeric-entities desc))
1493 (setq link (xml-substitute-numeric-entities link))
1494 ;; remove whitespace from title, desc, and link
1495 (setq title (newsticker--remove-whitespace title))
1496 (setq desc (newsticker--remove-whitespace desc))
1497 (setq link (newsticker--remove-whitespace link))
1499 ;; handle the feed itself
1500 (unless (newsticker--cache-contains newsticker--cache
1501 (intern name) title
1502 desc link 'feed)
1503 (setq something-was-added t))
1504 (setq newsticker--cache
1505 (newsticker--cache-add newsticker--cache (intern name)
1506 title desc link time 'feed position
1507 extra-elements time 'feed))
1508 something-was-added))
1510 (defun newsticker--parse-generic-items (name time itemlist
1511 title-fn desc-fn
1512 link-fn time-fn
1513 guid-fn extra-fn)
1514 "Parse generic news feed data.
1515 Argument NAME gives the name of a news feed. TIME gives the
1516 system time at which the data have been retrieved. ITEMLIST
1517 contains the news items returned by the xml parser.
1519 The arguments TITLE-FN, DESC-FN, LINK-FN, TIME-FN, GUID-FN, and
1520 EXTRA-FN give functions for extracting title, description, link,
1521 time, guid, and extra-elements resp. They are called with one
1522 argument, which is one of the items in ITEMLIST."
1523 (let (title desc link
1524 (old-item nil)
1525 (position 0)
1526 (something-was-added nil))
1527 ;; gather all items for this feed
1528 (mapc (lambda (node)
1529 (setq position (1+ position))
1530 (setq title (or (funcall title-fn node) "[untitled]"))
1531 (setq desc (funcall desc-fn node))
1532 (setq link (or (funcall link-fn node) ""))
1533 (setq time (or (funcall time-fn node) time))
1534 ;; It happened that the title or description
1535 ;; contained evil HTML code that confused the
1536 ;; xml parser. Therefore:
1537 (unless (stringp title)
1538 (setq title (prin1-to-string title)))
1539 (unless (or (stringp desc) (not desc))
1540 (setq desc (prin1-to-string desc)))
1541 ;; ignore items with empty title AND empty desc
1542 (when (or (> (length title) 0)
1543 (> (length desc) 0))
1544 ;; decode numeric entities
1545 (setq title (xml-substitute-numeric-entities title))
1546 (when desc
1547 (setq desc (xml-substitute-numeric-entities desc)))
1548 (setq link (xml-substitute-numeric-entities link))
1549 ;; remove whitespace from title, desc, and link
1550 (setq title (newsticker--remove-whitespace title))
1551 (setq desc (newsticker--remove-whitespace desc))
1552 (setq link (newsticker--remove-whitespace link))
1553 ;; add data to cache
1554 ;; do we have this item already?
1555 (let* ((guid (funcall guid-fn node)))
1556 ;;(message "guid=%s" guid)
1557 (setq old-item
1558 (newsticker--cache-contains newsticker--cache
1559 (intern name) title
1560 desc link nil guid)))
1561 ;; add this item, or mark it as old, or do nothing
1562 (let ((age1 'new)
1563 (age2 'old)
1564 (item-new-p nil))
1565 (if old-item
1566 (let ((prev-age (newsticker--age old-item)))
1567 (unless newsticker-automatically-mark-items-as-old
1568 ;; Some feeds deliver items multiply, the
1569 ;; first time we find an 'obsolete-old one in
1570 ;; the cache, the following times we find an
1571 ;; 'old one
1572 (if (memq prev-age '(obsolete-old old))
1573 (setq age2 'old)
1574 (setq age2 'new)))
1575 (if (eq prev-age 'immortal)
1576 (setq age2 'immortal))
1577 (setq time (newsticker--time old-item)))
1578 ;; item was not there
1579 (setq item-new-p t)
1580 (setq something-was-added t))
1581 (setq newsticker--cache
1582 (newsticker--cache-add
1583 newsticker--cache (intern name) title desc link
1584 time age1 position (funcall extra-fn node)
1585 time age2))
1586 (when item-new-p
1587 (let ((item (newsticker--cache-contains
1588 newsticker--cache (intern name) title
1589 desc link nil)))
1590 (if newsticker-auto-mark-filter-list
1591 (newsticker--run-auto-mark-filter name item))
1592 (run-hook-with-args
1593 'newsticker-new-item-functions name item))))))
1594 itemlist)
1595 something-was-added))
1597 ;; ======================================================================
1598 ;;; Misc
1599 ;; ======================================================================
1601 (defun newsticker--remove-whitespace (string)
1602 "Remove leading and trailing whitespace from STRING."
1603 ;; we must have ...+ but not ...* in the regexps otherwise xemacs loops
1604 ;; endlessly...
1605 (when (and string (stringp string))
1606 (replace-regexp-in-string
1607 "[ \t\r\n]+$" ""
1608 (replace-regexp-in-string "^[ \t\r\n]+" "" string))))
1610 (defun newsticker--do-forget-preformatted (item)
1611 "Forget pre-formatted data for ITEM.
1612 Remove the pre-formatted from `newsticker--cache'."
1613 (if (nthcdr 7 item)
1614 (setcar (nthcdr 7 item) nil))
1615 (if (nthcdr 6 item)
1616 (setcar (nthcdr 6 item) nil)))
1618 (defun newsticker--forget-preformatted ()
1619 "Forget all cached pre-formatted data.
1620 Remove the pre-formatted from `newsticker--cache'."
1621 (mapc (lambda (feed)
1622 (mapc 'newsticker--do-forget-preformatted
1623 (cdr feed)))
1624 newsticker--cache)
1625 (when (fboundp 'newsticker--buffer-set-uptodate)
1626 (newsticker--buffer-set-uptodate nil)))
1628 (defun newsticker--debug-msg (string &rest args)
1629 "Print newsticker debug messages.
1630 This function calls `message' with arguments STRING and ARGS, if
1631 `newsticker-debug' is non-nil."
1632 (and newsticker-debug
1633 ;;(not (active-minibuffer-window))
1634 ;;(not (current-message))
1635 (apply 'message string args)))
1637 (defun newsticker--decode-iso8601-date (iso8601-string)
1638 "Return ISO8601-STRING in format like `decode-time'.
1639 Converts from ISO-8601 to Emacs representation.
1640 Examples:
1641 2004-09-17T05:09:49.001+00:00
1642 2004-09-17T05:09:49+00:00
1643 2004-09-17T05:09+00:00
1644 2004-09-17T05:09:49
1645 2004-09-17T05:09
1646 2004-09-17
1647 2004-09
1648 2004"
1649 (if iso8601-string
1650 (when (string-match
1651 (concat
1652 "^ *\\([0-9]\\{4\\}\\)" ;year
1653 "\\(-\\([0-9]\\{2\\}\\)" ;month
1654 "\\(-\\([0-9]\\{2\\}\\)" ;day
1655 "\\(T"
1656 "\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)" ;hour:minute
1657 "\\(:\\([0-9]\\{2\\}\\)\\(\\.[0-9]+\\)?\\)?" ;second
1658 ;timezone
1659 "\\(\\([-+Z]\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)?"
1660 "\\)?\\)?\\)? *$")
1661 iso8601-string)
1662 (let ((year (read (match-string 1 iso8601-string)))
1663 (month (read (or (match-string 3 iso8601-string)
1664 "1")))
1665 (day (read (or (match-string 5 iso8601-string)
1666 "1")))
1667 (hour (read (or (match-string 7 iso8601-string)
1668 "0")))
1669 (minute (read (or (match-string 8 iso8601-string)
1670 "0")))
1671 (second (read (or (match-string 10 iso8601-string)
1672 "0")))
1673 (sign (match-string 13 iso8601-string))
1674 (offset-hour (read (or (match-string 15 iso8601-string)
1675 "0")))
1676 (offset-minute (read (or (match-string 16 iso8601-string)
1677 "0"))))
1678 (cond ((string= sign "+")
1679 (setq hour (- hour offset-hour))
1680 (setq minute (- minute offset-minute)))
1681 ((string= sign "-")
1682 (setq hour (+ hour offset-hour))
1683 (setq minute (+ minute offset-minute))))
1684 ;; if UTC subtract current-time-zone offset
1685 ;;(setq second (+ (car (current-time-zone)) second)))
1687 (condition-case nil
1688 (encode-time second minute hour day month year t)
1689 (error
1690 (message "Cannot decode \"%s\"" iso8601-string)
1691 nil))))
1692 nil))
1694 (defun newsticker--decode-rfc822-date (rfc822-string)
1695 "Return RFC822-STRING in format like `decode-time'.
1696 Converts from RFC822 to Emacs representation.
1697 Examples:
1698 Sat, 07 September 2002 00:00:01 +0100
1699 Sat, 07 September 2002 00:00:01 MET
1700 Sat, 07 Sep 2002 00:00:01 GMT
1701 07 Sep 2002 00:00:01 GMT
1702 07 Sep 2002"
1703 (if (and rfc822-string (stringp rfc822-string))
1704 (when (string-match
1705 (concat
1706 "\\s-*"
1707 ;; week day
1708 "\\(\\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\)\\s-*,?\\)?\\s-*"
1709 ;; day
1710 "\\([0-9]\\{1,2\\}\\)\\s-+"
1711 ;; month
1712 "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|"
1713 "Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\).*?\\s-+"
1714 ;; year
1715 "\\([0-9]\\{2,4\\}\\)"
1716 ;; time may be missing
1717 "\\(\\s-+"
1718 ;; hour
1719 "\\([0-9]\\{2\\}\\)"
1720 ;; minute
1721 ":\\([0-9]\\{2\\}\\)"
1722 ;; second
1723 "\\(:\\([0-9]\\{2\\}\\)\\)?"
1724 ;; zone -- fixme
1725 "\\(\\s-+\\("
1726 "UT\\|GMT\\|EST\\|EDT\\|CST\\|CDT\\|MST\\|MDT\\|PST\\|PDT"
1727 "\\|\\([-+]\\)\\([0-9]\\{2\\}\\)\\([0-9]\\{2\\}\\)"
1728 "\\)\\)?"
1729 "\\)?")
1730 rfc822-string)
1731 (let ((day (read (match-string 3 rfc822-string)))
1732 (month-name (match-string 4 rfc822-string))
1733 (month 0)
1734 (year (read (match-string 5 rfc822-string)))
1735 (hour (read (or (match-string 7 rfc822-string) "0")))
1736 (minute (read (or (match-string 8 rfc822-string) "0")))
1737 (second (read (or (match-string 10 rfc822-string) "0")))
1738 (zone (match-string 12 rfc822-string))
1739 (sign (match-string 13 rfc822-string))
1740 (offset-hour (read (or (match-string 14 rfc822-string)
1741 "0")))
1742 (offset-minute (read (or (match-string 15 rfc822-string)
1743 "0")))
1744 ;;FIXME
1746 (when zone
1747 (cond ((string= sign "+")
1748 (setq hour (- hour offset-hour))
1749 (setq minute (- minute offset-minute)))
1750 ((string= sign "-")
1751 (setq hour (+ hour offset-hour))
1752 (setq minute (+ minute offset-minute)))))
1753 (condition-case error-data
1754 (let ((i 1))
1755 (mapc (lambda (m)
1756 (if (string= month-name m)
1757 (setq month i))
1758 (setq i (1+ i)))
1759 '("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
1760 "Sep" "Oct" "Nov" "Dec"))
1761 (encode-time second minute hour day month year t))
1762 (error
1763 (message "Cannot decode \"%s\": %s %s" rfc822-string
1764 (car error-data) (cdr error-data))
1765 nil))))
1766 nil))
1768 (defun newsticker--lists-intersect-p (list1 list2)
1769 "Return t if LIST1 and LIST2 share elements."
1770 (let ((result nil))
1771 (mapc (lambda (elt)
1772 (if (memq elt list2)
1773 (setq result t)))
1774 list1)
1775 result))
1777 (defun newsticker--update-process-ids ()
1778 "Update list of ids of active newsticker processes.
1779 Checks list of active processes against list of newsticker processes."
1780 (let ((active-procs (process-list))
1781 (new-list nil))
1782 (mapc (lambda (proc)
1783 (let ((id (process-id proc)))
1784 (if (memq id newsticker--process-ids)
1785 (setq new-list (cons id new-list)))))
1786 active-procs)
1787 (setq newsticker--process-ids new-list))
1788 (force-mode-line-update))
1790 ;; ======================================================================
1791 ;;; Images
1792 ;; ======================================================================
1793 (defun newsticker--images-dir ()
1794 "Return directory where feed images are saved."
1795 (concat newsticker-dir "/images/"))
1797 (defun newsticker--icons-dir ()
1798 "Return directory where feed icons are saved."
1799 (concat newsticker-dir "/icons/"))
1801 (defun newsticker--image-get (feed-name filename directory url)
1802 "Get image for FEED-NAME by returning FILENAME from DIRECTORY.
1803 If the file does no exist or if it is older than 24 hours
1804 download it from URL first."
1805 (let ((image-name (concat directory feed-name)))
1806 (if (and (file-exists-p image-name)
1807 (time-less-p (current-time)
1808 (time-add (nth 5 (file-attributes image-name))
1809 (seconds-to-time 86400))))
1810 (newsticker--debug-msg "%s: Getting image for %s skipped"
1811 (format-time-string "%A, %H:%M" (current-time))
1812 feed-name)
1813 ;; download
1814 (newsticker--debug-msg "%s: Getting image for %s"
1815 (format-time-string "%A, %H:%M" (current-time))
1816 feed-name)
1817 (if (eq newsticker-retrieval-method 'intern)
1818 (newsticker--image-download-by-url feed-name filename directory url)
1819 (newsticker--image-download-by-wget feed-name filename directory url)))))
1821 (defun newsticker--image-download-by-wget (feed-name filename directory url)
1822 "Download image for FEED-NAME using external program.
1823 Save image as FILENAME in DIRECTORY, download it from URL."
1824 (let* ((proc-name (concat feed-name "-" filename))
1825 (buffername (concat " *newsticker-wget-image-" proc-name "*"))
1826 (item (or (assoc feed-name newsticker-url-list)
1827 (assoc feed-name newsticker-url-list-defaults)
1828 (error
1829 "Cannot get image for %s: Check newsticker-url-list"
1830 feed-name)))
1831 (wget-arguments (or (car (cdr (cdr (cdr (cdr item)))))
1832 newsticker-wget-arguments)))
1833 (with-current-buffer (get-buffer-create buffername)
1834 (erase-buffer)
1835 ;; throw an error if there is an old wget-process around
1836 (if (get-process feed-name)
1837 (error "Another wget-process is running for image %s"
1838 feed-name))
1839 ;; start wget
1840 (let* ((args (append wget-arguments (list url)))
1841 (proc (apply 'start-process proc-name buffername
1842 newsticker-wget-name args)))
1843 (set-process-coding-system proc 'no-conversion 'no-conversion)
1844 (set-process-sentinel proc 'newsticker--image-sentinel)
1845 (process-put proc 'nt-directory directory)
1846 (process-put proc 'nt-feed-name feed-name)
1847 (process-put proc 'nt-filename filename)))))
1849 (defun newsticker--image-sentinel (process event)
1850 "Sentinel for image-retrieving PROCESS caused by EVENT."
1851 (let* ((p-status (process-status process))
1852 (exit-status (process-exit-status process))
1853 (feed-name (process-get process 'nt-feed-name))
1854 (directory (process-get process 'nt-directory))
1855 (filename (process-get process 'nt-filename)))
1856 ;; catch known errors (zombie processes, rubbish-xml, etc.)
1857 ;; if an error occurs the news feed is not updated!
1858 (catch 'oops
1859 (unless (and (eq p-status 'exit)
1860 (= exit-status 0))
1861 (message "%s: Error while retrieving image from %s"
1862 (format-time-string "%A, %H:%M" (current-time))
1863 feed-name)
1864 (newsticker--image-remove directory feed-name)
1865 (throw 'oops nil))
1866 (newsticker--image-save (process-buffer process) directory filename))))
1868 (defun newsticker--image-save (buffer directory file-name)
1869 "Save contents of BUFFER in DIRECTORY as FILE-NAME.
1870 Finally kill buffer."
1871 (with-current-buffer buffer
1872 (let ((image-name (concat directory file-name)))
1873 (set-buffer-file-coding-system 'no-conversion)
1874 ;; make sure the cache dir exists
1875 (unless (file-directory-p directory)
1876 (make-directory directory))
1877 ;; write and close buffer
1878 (let ((require-final-newline nil)
1879 (backup-inhibited t)
1880 (coding-system-for-write 'no-conversion))
1881 (write-region nil nil image-name nil 'quiet))
1882 (set-buffer-modified-p nil)
1883 (kill-buffer buffer))))
1885 (defun newsticker--image-remove (directory file-name)
1886 "In DIRECTORY remove FILE-NAME."
1887 (let ((image-name (concat directory file-name)))
1888 (when (file-exists-p file-name)
1889 (delete-file image-name))))
1891 (defun newsticker--image-download-by-url (feed-name filename directory url)
1892 "Download image for FEED-NAME using `url-retrieve'.
1893 Save image as FILENAME in DIRECTORY, download it from URL."
1894 (let ((coding-system-for-read 'no-conversion))
1895 (condition-case error-data
1896 (url-retrieve url 'newsticker--image-download-by-url-callback
1897 (list feed-name directory filename))
1898 (error (message "Error retrieving image from %s: %s" feed-name
1899 error-data))))
1900 (force-mode-line-update))
1902 (defun newsticker--image-download-by-url-callback (status feed-name directory filename)
1903 "Callback function for `newsticker--image-download-by-url'.
1904 STATUS is the return status as delivered by `url-retrieve'.
1905 FEED-NAME is the name of the feed that the news were retrieved
1906 from.
1907 The image is saved in DIRECTORY as FILENAME."
1908 (when status
1909 (let ((status-type (car status))
1910 (status-details (cdr status)))
1911 (cond ((eq status-type :error)
1912 (newsticker--image-remove directory feed-name))
1914 (let ((buf (get-buffer-create (concat " *newsticker-url-image-" feed-name "-" directory "*")))
1915 (result (string-to-multibyte (buffer-string))))
1916 (set-buffer buf)
1917 (erase-buffer)
1918 (insert result)
1919 ;; remove MIME header
1920 (goto-char (point-min))
1921 (search-forward "\n\n")
1922 (delete-region (point-min) (point))
1923 ;; save
1924 (newsticker--image-save buf directory filename)))))))
1926 (defun newsticker--insert-image (img string)
1927 "Insert IMG with STRING at point."
1928 (insert-image img string))
1930 ;; ======================================================================
1931 ;;; HTML rendering
1932 ;; ======================================================================
1933 (defun newsticker-htmlr-render (pos1 pos2) ;
1934 "Replacement for `htmlr-render'.
1935 Renders the HTML code in the region POS1 to POS2 using htmlr."
1936 (let ((str (buffer-substring-no-properties pos1 pos2)))
1937 (delete-region pos1 pos2)
1938 (insert
1939 (with-temp-buffer
1940 (insert str)
1941 (goto-char (point-min))
1942 ;; begin original htmlr-render
1943 (when (fboundp 'htmlr-reset) (htmlr-reset))
1944 ;; something omitted here...
1945 (when (fboundp 'htmlr-step)
1946 (while (< (point) (point-max))
1947 (htmlr-step)))
1948 ;; end original htmlr-render
1949 (newsticker--remove-whitespace (buffer-string))))))
1951 ;; ======================================================================
1952 ;;; Manipulation of cached data
1953 ;; ======================================================================
1954 (defun newsticker--cache-set-preformatted-contents (item contents)
1955 "Set preformatted contents of ITEM to CONTENTS."
1956 (if (nthcdr 6 item)
1957 (setcar (nthcdr 6 item) contents)
1958 (setcdr (nthcdr 5 item) (list contents))))
1960 (defun newsticker--cache-set-preformatted-title (item title)
1961 "Set preformatted title of ITEM to TITLE."
1962 (if (nthcdr 7 item)
1963 (setcar (nthcdr 7 item) title)
1964 (setcdr (nthcdr 6 item) title)))
1966 (defun newsticker--cache-replace-age (data feed old-age new-age)
1967 "Mark all items in DATA in FEED which carry age OLD-AGE with NEW-AGE.
1968 If FEED is 'any it applies to all feeds. If OLD-AGE is 'any,
1969 all marks are replaced by NEW-AGE. Removes all pre-formatted contents."
1970 (mapc (lambda (a-feed)
1971 (when (or (eq feed 'any)
1972 (eq (car a-feed) feed))
1973 (let ((items (cdr a-feed)))
1974 (mapc (lambda (item)
1975 (when (or (eq old-age 'any)
1976 (eq (newsticker--age item) old-age))
1977 (setcar (nthcdr 4 item) new-age)
1978 (newsticker--do-forget-preformatted item)))
1979 items))))
1980 data)
1981 data)
1983 (defun newsticker--cache-mark-expired (data feed old-age new-age time)
1984 "Mark all expired entries.
1985 This function sets the age entries in DATA in the feed FEED. If
1986 an item's age is OLD-AGE it is set to NEW-AGE if the item is
1987 older than TIME."
1988 (mapc
1989 (lambda (a-feed)
1990 (when (or (eq feed 'any)
1991 (eq (car a-feed) feed))
1992 (let ((items (cdr a-feed)))
1993 (mapc
1994 (lambda (item)
1995 (when (eq (newsticker--age item) old-age)
1996 (let ((exp-time (time-add (newsticker--time item)
1997 (seconds-to-time time))))
1998 (when (time-less-p exp-time (current-time))
1999 (newsticker--debug-msg
2000 "Item `%s' from %s has expired on %s"
2001 (newsticker--title item)
2002 (format-time-string "%Y-%02m-%d, %H:%M"
2003 (newsticker--time item))
2004 (format-time-string "%Y-%02m-%d, %H:%M" exp-time))
2005 (setcar (nthcdr 4 item) new-age)))))
2006 items))))
2007 data)
2008 data)
2010 (defun newsticker--cache-contains (data feed title desc link age
2011 &optional guid)
2012 "Check DATA whether FEED contains an item with the given properties.
2013 This function returns the contained item or nil if it is not
2014 contained.
2015 The properties which are checked are TITLE, DESC, LINK, AGE, and
2016 GUID. In general all properties must match in order to return a
2017 certain item, except for the following cases.
2019 If AGE equals 'feed the TITLE, DESCription and LINK do not
2020 matter. If DESC is nil it is ignored as well. If
2021 `newsticker-desc-comp-max' is non-nil, only the first
2022 `newsticker-desc-comp-max' characters of DESC are taken into
2023 account.
2025 If GUID is non-nil it is sufficient to match this value, and the
2026 other properties are ignored."
2027 ;;(newsticker--debug-msg "Looking for %s guid=%s" title guid)
2028 (condition-case nil
2029 (catch 'found
2030 (when (and desc newsticker-desc-comp-max
2031 (> (length desc) newsticker-desc-comp-max))
2032 (setq desc (substring desc 0 newsticker-desc-comp-max)))
2033 (mapc
2034 (lambda (this-feed)
2035 (when (eq (car this-feed) feed)
2036 (mapc (lambda (anitem)
2037 (when (cond (guid
2038 ;; global unique id can match
2039 (string= guid (newsticker--guid anitem)))
2040 (t;;FIXME?
2042 ;; or title, desc, etc.
2043 (and
2044 ;;(or (not (eq age 'feed))
2045 ;; (eq (newsticker--age anitem) 'feed))
2046 (string= (newsticker--title anitem)
2047 title)
2048 (or (not link)
2049 (string= (newsticker--link anitem)
2050 link))
2051 (or (not desc)
2052 (if (and desc newsticker-desc-comp-max
2053 (> (length (newsticker--desc
2054 anitem))
2055 newsticker-desc-comp-max))
2056 (string= (substring
2057 (newsticker--desc anitem)
2059 newsticker-desc-comp-max)
2060 desc)
2061 (string= (newsticker--desc anitem)
2062 desc)))))))
2063 ;;(newsticker--debug-msg "Found %s guid=%s"
2064 ;; (newsticker--title anitem)
2065 ;; (newsticker--guid anitem))
2066 (throw 'found anitem)))
2067 (cdr this-feed))))
2068 data)
2069 ;;(newsticker--debug-msg "Found nothing")
2070 nil)
2071 (error nil)))
2073 (defun newsticker--cache-add (data feed-name-symbol title desc link time age
2074 position extra-elements
2075 &optional updated-time updated-age
2076 preformatted-contents
2077 preformatted-title)
2078 "Add another item to cache data.
2079 Add to DATA in the FEED-NAME-SYMBOL an item with TITLE, DESC,
2080 LINK, TIME, AGE, POSITION, and EXTRA-ELEMENTS. If this item is
2081 contained already, its time is set to UPDATED-TIME, its mark is
2082 set to UPDATED-AGE, and its pre-formatted contents is set to
2083 PREFORMATTED-CONTENTS and PREFORMATTED-TITLE. Returns the age
2084 which the item got."
2085 (let* ((guid (newsticker--guid-to-string (assoc 'guid extra-elements)))
2086 (item (newsticker--cache-contains data feed-name-symbol title desc link
2087 age guid)))
2088 ;;(message "guid=%s" guid)
2089 (if item
2090 ;; does exist already -- change age, update time and position
2091 (progn
2092 ;;(newsticker--debug-msg "Updating item %s %s %s %s %s -> %s %s
2093 ;; (guid %s -> %s)"
2094 ;; feed-name-symbol title link time age
2095 ;; updated-time updated-age
2096 ;; guid (newsticker--guid item))
2097 (if (nthcdr 5 item)
2098 (setcar (nthcdr 5 item) position)
2099 (setcdr (nthcdr 4 item) (list position)))
2100 (setcar (nthcdr 4 item) updated-age)
2101 (if updated-time
2102 (setcar (nthcdr 3 item) updated-time))
2103 ;; replace cached pre-formatted contents
2104 (newsticker--cache-set-preformatted-contents
2105 item preformatted-contents)
2106 (newsticker--cache-set-preformatted-title
2107 item preformatted-title))
2108 ;; did not exist or age equals 'feed-name-symbol
2109 (setq item (list title desc link time age position preformatted-contents
2110 preformatted-title extra-elements))
2111 ;;(newsticker--debug-msg "Adding item %s" item)
2112 (catch 'found
2113 (mapc (lambda (this-feed)
2114 (when (eq (car this-feed) feed-name-symbol)
2115 (setcdr this-feed (nconc (cdr this-feed) (list item)))
2116 (throw 'found this-feed)))
2117 data)
2118 ;; the feed is not contained
2119 (add-to-list 'data (list feed-name-symbol item) t))))
2120 data)
2122 (defun newsticker--cache-remove (data feed-symbol age)
2123 "Remove all entries from DATA in the feed FEED-SYMBOL with AGE.
2124 FEED-SYMBOL may be 'any. Entries from old feeds, which are no longer in
2125 `newsticker-url-list' or `newsticker-url-list-defaults', are removed as
2126 well."
2127 (let* ((pos data)
2128 (feed (car pos))
2129 (last-pos nil))
2130 (while feed
2131 (if (or (assoc (symbol-name (car feed)) newsticker-url-list)
2132 (assoc (symbol-name (car feed)) newsticker-url-list-defaults))
2133 ;; feed is still valid=active
2134 ;; (message "Keeping feed %s" (car feed))
2135 (if (or (eq feed-symbol 'any)
2136 (eq feed-symbol (car feed)))
2137 (let* ((item-pos (cdr feed))
2138 (item (car item-pos))
2139 (prev-pos nil))
2140 (while item
2141 ;;(message "%s" (car item))
2142 (if (eq age (newsticker--age item))
2143 ;; remove this item
2144 (progn
2145 ;;(message "Removing item %s" (car item))
2146 (if prev-pos
2147 (setcdr prev-pos (cdr item-pos))
2148 (setcdr feed (cdr item-pos))))
2149 ;;(message "Keeping item %s" (car item))
2150 (setq prev-pos item-pos))
2151 (setq item-pos (cdr item-pos))
2152 (setq item (car item-pos)))))
2153 ;; feed is not active anymore
2154 ;; (message "Removing feed %s" (car feed))
2155 (if last-pos
2156 (setcdr last-pos (cdr pos))
2157 (setq data (cdr pos))))
2158 (setq last-pos pos)
2159 (setq pos (cdr pos))
2160 (setq feed (car pos)))))
2162 ;; ======================================================================
2163 ;;; Sorting
2164 ;; ======================================================================
2165 (defun newsticker--cache-item-compare-by-time (item1 item2)
2166 "Compare two news items ITEM1 and ITEM2 by comparing their time values."
2167 (catch 'result
2168 (let ((age1 (newsticker--age item1))
2169 (age2 (newsticker--age item2)))
2170 (if (not (eq age1 age2))
2171 (cond ((eq age1 'obsolete)
2172 (throw 'result nil))
2173 ((eq age2 'obsolete)
2174 (throw 'result t)))))
2175 (let* ((time1 (newsticker--time item1))
2176 (time2 (newsticker--time item2)))
2177 (cond ((< (nth 0 time1) (nth 0 time2))
2178 nil)
2179 ((> (nth 0 time1) (nth 0 time2))
2181 ((< (nth 1 time1) (nth 1 time2))
2182 nil)
2183 ((> (nth 1 time1) (nth 1 time2))
2185 ((< (or (nth 2 time1) 0) (or (nth 2 time2) 0))
2186 nil)
2187 ((> (or (nth 2 time1) 0) (or (nth 2 time2) 0))
2190 nil)))))
2192 (defun newsticker--cache-item-compare-by-title (item1 item2)
2193 "Compare ITEM1 and ITEM2 by comparing their titles."
2194 (catch 'result
2195 (let ((age1 (newsticker--age item1))
2196 (age2 (newsticker--age item2)))
2197 (if (not (eq age1 age2))
2198 (cond ((eq age1 'obsolete)
2199 (throw 'result nil))
2200 ((eq age2 'obsolete)
2201 (throw 'result t)))))
2202 (string< (newsticker--title item1) (newsticker--title item2))))
2204 (defun newsticker--cache-item-compare-by-position (item1 item2)
2205 "Compare ITEM1 and ITEM2 by comparing their original positions."
2206 (catch 'result
2207 (let ((age1 (newsticker--age item1))
2208 (age2 (newsticker--age item2)))
2209 (if (not (eq age1 age2))
2210 (cond ((eq age1 'obsolete)
2211 (throw 'result nil))
2212 ((eq age2 'obsolete)
2213 (throw 'result t)))))
2214 (< (or (newsticker--pos item1) 0) (or (newsticker--pos item2) 0))))
2216 (defun newsticker--cache-save-version1 ()
2217 "Update and save newsticker cache file."
2218 (interactive)
2219 (newsticker--cache-update t))
2221 (defun newsticker--cache-update (&optional save)
2222 "Update newsticker cache file.
2223 If optional argument SAVE is not nil the cache file is saved to disk."
2224 (save-excursion
2225 (unless (file-directory-p newsticker-dir)
2226 (make-directory newsticker-dir t))
2227 (let ((coding-system-for-write 'utf-8)
2228 (buf (find-file-noselect newsticker-cache-filename)))
2229 (when buf
2230 (set-buffer buf)
2231 (setq buffer-undo-list t)
2232 (erase-buffer)
2233 (insert ";; -*- coding: utf-8 -*-\n")
2234 (insert (prin1-to-string newsticker--cache))
2235 (when save
2236 (save-buffer))))))
2238 (defun newsticker--cache-get-feed (feed)
2239 "Return the cached data for the feed FEED.
2240 FEED is a symbol!"
2241 (assoc feed newsticker--cache))
2243 (defun newsticker--cache-dir ()
2244 "Return directory for saving cache data."
2245 (concat newsticker-dir "/feeds"))
2247 (defun newsticker--cache-save ()
2248 "Save cache data for all feeds."
2249 (unless (file-directory-p newsticker-dir)
2250 (make-directory newsticker-dir t))
2251 (mapc 'newsticker--cache-save-feed newsticker--cache)
2252 nil)
2254 (defun newsticker--cache-save-feed (feed)
2255 "Save cache data for FEED."
2256 (let ((dir (concat (newsticker--cache-dir) "/" (symbol-name (car feed)))))
2257 (unless (file-directory-p dir)
2258 (make-directory dir t))
2259 (let ((coding-system-for-write 'utf-8))
2260 (with-temp-file (concat dir "/data")
2261 (insert ";; -*- coding: utf-8 -*-\n")
2262 (insert (prin1-to-string (cdr feed)))))))
2264 (defun newsticker--cache-read-version1 ()
2265 "Read version1 cache data."
2266 (let ((coding-system-for-read 'utf-8))
2267 (when (file-exists-p newsticker-cache-filename)
2268 (with-temp-buffer
2269 (insert-file-contents newsticker-cache-filename)
2270 (goto-char (point-min))
2271 (condition-case nil
2272 (setq newsticker--cache (read (current-buffer)))
2273 (error
2274 (message "Error while reading newsticker cache file!")
2275 (setq newsticker--cache nil)))))))
2277 (defun newsticker--cache-read ()
2278 "Read cache data."
2279 (setq newsticker--cache nil)
2280 (if (file-exists-p newsticker-cache-filename)
2281 (progn
2282 (when (y-or-n-p "Old newsticker cache file exists. Read it? ")
2283 (newsticker--cache-read-version1))
2284 (when (y-or-n-p (format "Delete old newsticker cache file? "))
2285 (delete-file newsticker-cache-filename)))
2286 (mapc (lambda (f)
2287 (newsticker--cache-read-feed (car f)))
2288 (append newsticker-url-list-defaults newsticker-url-list))))
2290 (defun newsticker--cache-read-feed (feed-name)
2291 "Read cache data for feed named FEED-NAME."
2292 (let ((file-name (concat (newsticker--cache-dir) "/" feed-name "/data"))
2293 (coding-system-for-read 'utf-8))
2294 (when (file-exists-p file-name)
2295 (with-temp-buffer
2296 (insert-file-contents file-name)
2297 (goto-char (point-min))
2298 (condition-case nil
2299 (add-to-list 'newsticker--cache (cons (intern feed-name)
2300 (read (current-buffer))))
2301 (error
2302 (message "Error while reading newsticker cache file %s!"
2303 file-name)
2304 (setq newsticker--cache nil)))))))
2306 ;; ======================================================================
2307 ;;; Statistics
2308 ;; ======================================================================
2309 (defun newsticker--stat-num-items (feed &rest ages)
2310 "Return number of items in the given FEED which have one of the given AGES.
2311 If AGES is nil, the total number of items is returned."
2312 (let ((items (cdr (newsticker--cache-get-feed feed)))
2313 (num 0))
2314 (while items
2315 (if ages
2316 (if (memq (newsticker--age (car items)) ages)
2317 (setq num (1+ num)))
2318 (if (memq (newsticker--age (car items)) '(new old immortal obsolete))
2319 (setq num (1+ num))))
2320 (setq items (cdr items)))
2321 num))
2323 (defun newsticker--stat-num-items-total (&optional age)
2324 "Return total number of items in all feeds which have the given AGE.
2325 If AGE is nil, the total number of items is returned."
2326 (apply '+
2327 (mapcar (lambda (feed)
2328 (if age
2329 (newsticker--stat-num-items (intern (car feed)) age)
2330 (newsticker--stat-num-items (intern (car feed)))))
2331 (append newsticker-url-list-defaults newsticker-url-list))))
2333 ;; ======================================================================
2334 ;;; OPML
2335 ;; ======================================================================
2336 (defun newsticker-opml-export ()
2337 "OPML subscription export.
2338 Export subscriptions to a buffer in OPML Format."
2339 ;; FIXME: use newsticker-groups
2340 (interactive)
2341 (with-current-buffer (get-buffer-create "*OPML Export*")
2342 (set-buffer-file-coding-system 'utf-8)
2343 (insert (concat
2344 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
2345 "<!-- OPML generated by Emacs newsticker.el -->\n"
2346 "<opml version=\"1.0\">\n"
2347 " <head>\n"
2348 " <title>mySubscriptions</title>\n"
2349 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
2350 "</dateCreated>\n"
2351 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
2352 " <ownerName>" (user-full-name) "</ownerName>\n"
2353 " </head>\n"
2354 " <body>\n"))
2355 (mapc (lambda (sub)
2356 (insert " <outline text=\"")
2357 (insert (newsticker--title sub))
2358 (insert "\" xmlUrl=\"")
2359 (insert (xml-escape-string (let ((url (cadr sub)))
2360 (if (stringp url) url (prin1-to-string url)))))
2361 (insert "\"/>\n"))
2362 (append newsticker-url-list newsticker-url-list-defaults))
2363 (insert " </body>\n</opml>\n"))
2364 (pop-to-buffer "*OPML Export*")
2365 (when (fboundp 'sgml-mode)
2366 (sgml-mode)))
2368 (defun newsticker--opml-import-outlines (outlines)
2369 "Recursively import OUTLINES from OPML data.
2370 Note that nested outlines are currently flattened -- i.e. grouping is
2371 removed."
2372 (mapc (lambda (outline)
2373 (let ((name (xml-get-attribute outline 'text))
2374 (url (xml-get-attribute outline 'xmlUrl))
2375 (children (xml-get-children outline 'outline)))
2376 (unless (string= "" url)
2377 (add-to-list 'newsticker-url-list
2378 (list name url nil nil nil) t))
2379 (if children
2380 (newsticker--opml-import-outlines children))))
2381 outlines))
2383 (defun newsticker-opml-import (filename)
2384 "Import OPML data from FILENAME."
2385 (interactive "fOPML file: ")
2386 (set-buffer (find-file-noselect filename))
2387 (goto-char (point-min))
2388 (let* ((node-list (xml-parse-region (point-min) (point-max)))
2389 (body (car (xml-get-children (car node-list) 'body)))
2390 (outlines (xml-get-children body 'outline)))
2391 (newsticker--opml-import-outlines outlines))
2392 (customize-variable 'newsticker-url-list))
2394 ;; ======================================================================
2395 ;;; Auto marking
2396 ;; ======================================================================
2397 (defun newsticker--run-auto-mark-filter (feed item)
2398 "Automatically mark an item as old or immortal.
2399 This function checks the variable `newsticker-auto-mark-filter-list'
2400 for an entry that matches FEED and ITEM."
2401 (let ((case-fold-search t))
2402 (mapc (lambda (filter)
2403 (let ((filter-feed (car filter))
2404 (pattern-list (cadr filter)))
2405 (when (string-match filter-feed feed)
2406 (newsticker--do-run-auto-mark-filter item pattern-list))))
2407 newsticker-auto-mark-filter-list)))
2409 (defun newsticker--do-run-auto-mark-filter (item list)
2410 "Actually compare ITEM against the pattern-LIST.
2411 LIST must be an element of `newsticker-auto-mark-filter-list'."
2412 (mapc (lambda (pattern)
2413 (let ((place (nth 1 pattern))
2414 (regexp (nth 2 pattern))
2415 (title (newsticker--title item))
2416 (desc (newsticker--desc item)))
2417 (when (or (eq place 'title) (eq place 'all))
2418 (when (and title (string-match regexp title))
2419 (newsticker--process-auto-mark-filter-match item pattern)))
2420 (when (or (eq place 'description) (eq place 'all))
2421 (when (and desc (string-match regexp desc))
2422 (newsticker--process-auto-mark-filter-match item pattern)))))
2423 list))
2425 (defun newsticker--process-auto-mark-filter-match (item pattern)
2426 "Process ITEM that matches an auto-mark-filter PATTERN."
2427 (let ((age (nth 0 pattern))
2428 (place (nth 1 pattern))
2429 (regexp (nth 2 pattern)))
2430 (newsticker--debug-msg "Auto-mark-filter %s matches `%s'"
2431 pattern (newsticker--title item))
2432 (setcar (nthcdr 4 item) age)
2433 (nconc (newsticker--extra item)
2434 (list (list 'newsticker-auto-mark nil
2435 (format "age=%s, title/desc=%s, regexp=%s"
2436 age place regexp))))))
2438 ;; ======================================================================
2439 ;;; Hook samples
2440 ;; ======================================================================
2441 (defun newsticker-new-item-functions-sample (feed item)
2442 "Demonstrate the use of the `newsticker-new-item-functions' hook.
2443 This function just prints out the values of the FEED and title of the ITEM."
2444 (message (concat "newsticker-new-item-functions-sample: feed=`%s', "
2445 "title=`%s'")
2446 feed (newsticker--title item)))
2448 (defun newsticker-download-images (feed item)
2449 "Download the first image.
2450 If FEED equals \"imagefeed\" download the first image URL found
2451 in the description=contents of ITEM to the directory
2452 \"~/tmp/newsticker/FEED/TITLE\" where TITLE is the title of the item."
2453 (when (string= feed "imagefeed")
2454 (let ((title (newsticker--title item))
2455 (desc (newsticker--desc item)))
2456 (when (string-match "<img src=\"\\(http://[^ \"]+\\)\"" desc)
2457 (let ((url (substring desc (match-beginning 1) (match-end 1)))
2458 (temp-dir (concat "~/tmp/newsticker/" feed "/" title))
2459 (org-dir default-directory))
2460 (unless (file-directory-p temp-dir)
2461 (make-directory temp-dir t))
2462 (cd temp-dir)
2463 (message "Getting image %s" url)
2464 (apply 'start-process "wget-image"
2465 " *newsticker-wget-download-images*"
2466 newsticker-wget-name
2467 (list url))
2468 (cd org-dir))))))
2470 (defun newsticker-download-enclosures (feed item)
2471 "In all FEEDs download the enclosed object of the news ITEM.
2472 The object is saved to the directory \"~/tmp/newsticker/FEED/TITLE\", which
2473 is created if it does not exist. TITLE is the title of the news
2474 item. Argument FEED is ignored.
2475 This function is suited for adding it to `newsticker-new-item-functions'."
2476 (let ((title (newsticker--title item))
2477 (enclosure (newsticker--enclosure item)))
2478 (when enclosure
2479 (let ((url (cdr (assoc 'url enclosure)))
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 enclosure %s" url)
2486 (apply 'start-process "wget-enclosure"
2487 " *newsticker-wget-download-enclosures*"
2488 newsticker-wget-name
2489 (list url))
2490 (cd org-dir)))))
2492 ;; ======================================================================
2493 ;;; Retrieve samples
2494 ;; ======================================================================
2495 (defun newsticker-retrieve-random-message (feed-name)
2496 "Return an artificial RSS string under the name FEED-NAME."
2497 (concat "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?><rss version=\"0.91\">"
2498 "<channel>"
2499 "<title>newsticker-retrieve-random-message</title>"
2500 "<description>Sample retrieval function</description>"
2501 "<pubDate>FIXME Sat, 07 Sep 2005 00:00:01 GMT</pubDate>"
2502 "<item><title>" (format "Your lucky number is %d" (random 10000))
2503 "</title><description>" (format "Or maybe it is %d" (random 10000))
2504 "</description></item></channel></rss>"))
2506 (provide 'newst-backend)
2508 ;;; newst-backend.el ends here