(compose-mail): Give a better error message for `mail-user-agent'
[emacs.git] / lisp / net / newst-backend.el
blob520a9e19b42b7d4f4bff1af9eda05f8ba1d0541f
1 ;;; newst-backend.el --- Retrieval backend for newsticker -*- lexical-binding:t -*-
3 ;; Copyright (C) 2003-2018 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 <https://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 ;; Hard-coding URLs like this is a recipe for propagating obsolete info.
68 (defconst newsticker--raw-url-list-defaults
70 ;; 2017/12: no response.
71 ;;; ("CNET News.com"
72 ;;; "http://export.cnet.com/export/feeds/news/rss/1,11176,,00.xml")
73 ("Debian Security Advisories"
74 "http://www.debian.org/security/dsa.en.rdf")
75 ("Debian Security Advisories - Long format"
76 "http://www.debian.org/security/dsa-long.en.rdf")
77 ("Emacs Wiki"
78 "https://www.emacswiki.org/emacs?action=rss"
79 nil
80 3600)
81 ("LWN (Linux Weekly News)"
82 "https://lwn.net/headlines/rss")
83 ;; Not updated since 2010.
84 ;;; ("NY Times: Technology"
85 ;;; "http://www.nytimes.com/services/xml/rss/userland/Technology.xml")
86 ;;; ("NY Times"
87 ;;; "http://www.nytimes.com/services/xml/rss/userland/HomePage.xml")
88 ("Quote of the day"
89 "http://feeds.feedburner.com/quotationspage/qotd"
90 "07:00"
91 86400)
92 ("The Register"
93 "https://www.theregister.co.uk/headlines.rss")
94 ("slashdot"
95 "http://rss.slashdot.org/Slashdot/slashdot"
96 nil
97 3600) ;/. will ban you if under 3600 seconds!
98 ("Wired News"
99 "https://www.wired.com/feed/rss")
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 "Default URL list in raw form.
107 This list is fed into defcustom via `newsticker--splicer'.")
109 (defun newsticker--splicer (item)
110 "Convert ITEM for splicing into `newsticker-url-list-defaults'."
111 (let ((result (list 'list :tag (nth 0 item) (list 'const (nth 0 item))))
112 (element (cdr item)))
113 (while element
114 (setq result (append result (list (list 'const (car element)))))
115 (setq element (cdr element)))
116 result))
118 (defun newsticker--set-customvar-retrieval (symbol value)
119 "Set retrieval related newsticker-variable SYMBOL value to VALUE.
120 Calls all actions which are necessary in order to make the new
121 value effective."
122 (if (or (not (boundp symbol))
123 (equal (symbol-value symbol) value))
124 (set symbol value)
125 ;; something must have changed
126 (let ((need-restart nil)
127 (new-or-changed-feeds nil)
128 (removed-feeds))
129 (cond ((eq symbol 'newsticker-retrieval-interval)
130 (setq need-restart t))
131 ((memq symbol '(newsticker-url-list-defaults newsticker-url-list))
132 (dolist (elt value)
133 (unless (member elt (symbol-value symbol))
134 (setq new-or-changed-feeds (cons elt new-or-changed-feeds))))
135 (dolist (elt (symbol-value symbol))
136 (unless (member elt value)
137 (setq removed-feeds (cons elt removed-feeds))))))
138 (cond (need-restart
139 (set symbol value)
140 (when (newsticker-running-p)
141 (message "Restarting newsticker")
142 (newsticker-stop)
143 (newsticker-start)))
145 (dolist (feed removed-feeds)
146 (message "Stopping feed `%s'" (car feed))
147 (newsticker--stop-feed (car feed)))
148 (dolist (feed new-or-changed-feeds)
149 (message "Starting feed `%s'" (car feed))
150 (newsticker--stop-feed (car feed))
151 (newsticker--start-feed feed))
152 (unless new-or-changed-feeds
153 (when newsticker--sentinel-callback
154 (funcall newsticker--sentinel-callback)))))
155 (set symbol value))))
157 ;; ======================================================================
158 ;; retrieval
159 (defgroup newsticker-retrieval nil
160 "Settings for news retrieval."
161 :group 'newsticker)
163 (defcustom newsticker-url-list-defaults
164 '(("Emacs Wiki"
165 "https://www.emacswiki.org/emacs?action=rss"
167 3600))
168 "A customizable list of news feeds to select from.
169 These were mostly extracted from the Radio Community Server at
170 http://subhonker6.userland.com/rcsPublic/rssHotlist.
172 You may add other entries in `newsticker-url-list'."
173 :type `(set ,@(mapcar `newsticker--splicer
174 newsticker--raw-url-list-defaults))
175 :set 'newsticker--set-customvar-retrieval
176 :group 'newsticker-retrieval)
178 (defcustom newsticker-url-list nil
179 "The news feeds which you like to watch.
181 This alist will be used in addition to selection made customizing
182 `newsticker-url-list-defaults'.
184 This is an alist. Each element consists of two items: a LABEL and a URL,
185 optionally followed by a START-TIME, INTERVAL specifier and WGET-ARGUMENTS.
187 The LABEL gives the name of the news feed. It can be an arbitrary string.
189 The URL gives the location of the news feed. It must point to a valid
190 RSS or Atom file. The file is retrieved by calling wget, or whatever you
191 specify as `newsticker-wget-name'.
193 URL may also be a function which returns news data. In this case
194 `newsticker-retrieval-method' etc. are ignored for this feed.
196 The START-TIME can be either a string, or nil. If it is a string it
197 specifies a fixed time at which this feed shall be retrieved for the
198 first time. (Examples: \"11:00pm\", \"23:00\".) If it is nil (or
199 unspecified), this feed will be retrieved immediately after calling
200 `newsticker-start'.
202 The INTERVAL specifies the time between retrievals for this feed. If it
203 is nil (or unspecified) the default interval value as set in
204 `newsticker-retrieval-interval' is used.
206 \(newsticker.el calls `run-at-time'. The newsticker-parameters START-TIME
207 and INTERVAL correspond to the `run-at-time'-parameters TIME and REPEAT.)
209 WGET-ARGUMENTS specifies arguments for wget (see `newsticker-wget-name')
210 which apply for this feed only, overriding the value of
211 `newsticker-wget-arguments'."
212 :type '(repeat (list :tag "News feed"
213 (string :tag "Label")
214 (choice :tag "URI"
215 (string :tag "String")
216 (function :tag "Function"))
217 (choice :tag "Start"
218 (const :tag "Default" nil)
219 (string :tag "Fixed Time"))
220 (choice :tag "Interval"
221 (const :tag "Default" nil)
222 (const :tag "Hourly" 3600)
223 (const :tag "Daily" 86400)
224 (const :tag "Weekly" 604800)
225 (integer :tag "Interval"))
226 (choice :tag "Wget Arguments"
227 (const :tag "Default arguments" nil)
228 (repeat :tag "Special arguments" string))))
229 :set 'newsticker--set-customvar-retrieval
230 :group 'newsticker-retrieval)
232 (defcustom newsticker-retrieval-method
233 'intern
234 "Method for retrieving news from the web, either `intern' or `extern'.
235 Default value `intern' uses Emacs' built-in asynchronous download
236 capabilities (`url-retrieve'). If set to `extern' the external
237 program wget is used, see `newsticker-wget-name'."
238 :type '(choice :tag "Method"
239 (const :tag "Intern" intern)
240 (const :tag "Extern" extern))
241 :group 'newsticker-retrieval)
243 (defcustom newsticker-wget-name
244 "wget"
245 "Name of the program which is called to retrieve news from the web.
246 The canonical choice is wget but you may take any other program which is
247 able to return the contents of a news feed file on stdout."
248 :type 'string
249 :group 'newsticker-retrieval)
251 (defcustom newsticker-wget-arguments
252 '("-q" "-O" "-")
253 "Arguments which are passed to wget.
254 There is probably no reason to change the default settings, unless you
255 are living behind a firewall."
256 :type '(repeat (string :tag "Argument"))
257 :group 'newsticker-retrieval)
259 (defcustom newsticker-retrieval-interval
260 3600
261 "Time interval for retrieving new news items (seconds).
262 If this value is not positive (i.e. less than or equal to 0)
263 items are retrieved only once!
264 Please note that some feeds, e.g. Slashdot, will ban you if you
265 make it less than 1800 seconds (30 minutes)!"
266 :type '(choice :tag "Interval"
267 (const :tag "No automatic retrieval" 0)
268 (const :tag "Hourly" 3600)
269 (const :tag "Daily" 86400)
270 (const :tag "Weekly" 604800)
271 (integer :tag "Interval"))
272 :set 'newsticker--set-customvar-retrieval
273 :group 'newsticker-retrieval)
275 (defcustom newsticker-desc-comp-max
277 "Relevant length of headline descriptions.
278 This value gives the maximum number of characters which will be
279 taken into account when newsticker compares two headline
280 descriptions."
281 :type 'integer
282 :group 'newsticker-retrieval)
284 ;; ======================================================================
285 ;; headline processing
286 (defgroup newsticker-headline-processing nil
287 "Settings for the automatic processing of headlines."
288 :group 'newsticker)
290 (defcustom newsticker-automatically-mark-items-as-old
292 "Decides whether to automatically mark items as old.
293 If t a new item is considered as new only after its first retrieval. As
294 soon as it is retrieved a second time, it becomes old. If not t all
295 items stay new until you mark them as old. This is done in the
296 *newsticker* buffer."
297 :type 'boolean
298 :group 'newsticker-headline-processing)
300 (defcustom newsticker-automatically-mark-visited-items-as-old
302 "Decides whether to automatically mark visited items as old.
303 If t an item is marked as old as soon as the associated link is
304 visited, i.e. after pressing RET or mouse2 on the item's
305 headline."
307 :type 'boolean
308 :group 'newsticker-headline-processing)
310 (defcustom newsticker-keep-obsolete-items
312 "Decides whether to keep unread items which have been removed from feed.
313 If t a new item, which has been removed from the feed, is kept in
314 the cache until it is marked as read."
315 :type 'boolean
316 :group 'newsticker-headline-processing)
318 (defcustom newsticker-obsolete-item-max-age
319 (* 60 60 24)
320 "Maximal age of obsolete items, in seconds.
321 Obsolete items which are older than this value will be silently
322 deleted at the next retrieval."
323 :type 'integer
324 :group 'newsticker-headline-processing)
326 (defcustom newsticker-auto-mark-filter-list
328 "A list of filters for automatically marking headlines.
330 This is an alist of the form (FEED-NAME PATTERN-LIST). I.e. each
331 element consists of a FEED-NAME a PATTERN-LIST. Each element of
332 the pattern-list has the form (AGE TITLE-OR-DESCRIPTION REGEXP).
333 AGE must be one of the symbols `old' or `immortal'.
334 TITLE-OR-DESCRIPTION must be one of the symbols `title',
335 `description', or `all'. REGEXP is a regular expression, i.e., a
336 string.
338 This filter is checked after a new headline has been retrieved.
339 If FEED-NAME matches the name of the corresponding news feed, the
340 pattern-list is checked: The new headline will be marked as AGE
341 if REGEXP matches the headline's TITLE-OR-DESCRIPTION.
343 If, for example, `newsticker-auto-mark-filter-list' looks like
344 ((slashdot (\\='old \\='title \"^Forget me!$\") (\\='immortal \\='title \"Read me\")
345 (\\='immortal \\='all \"important\"))))
347 then all articles from slashdot are marked as old if they have
348 the title \"Forget me!\". All articles with a title containing
349 the string \"Read me\" are marked as immortal. All articles which
350 contain the string \"important\" in their title or their
351 description are marked as immortal."
352 :type '(repeat (list :tag "Auto mark filter"
353 (string :tag "Feed name")
354 (repeat
355 (list :tag "Filter element"
356 (choice
357 :tag "Auto-assigned age"
358 (const :tag "Old" old)
359 (const :tag "Immortal" immortal))
360 (choice
361 :tag "Title/Description"
362 (const :tag "Title" title)
363 (const :tag "Description" description)
364 (const :tag "All" all))
365 (string :tag "Regexp")))))
366 :group 'newsticker-headline-processing)
368 ;; ======================================================================
369 ;; hooks
370 (defgroup newsticker-hooks nil
371 "Settings for newsticker hooks."
372 :group 'newsticker)
374 (defcustom newsticker-start-hook
376 "Hook run when starting newsticker.
377 This hook is run at the very end of `newsticker-start'."
378 :options '(newsticker-start-ticker)
379 :type 'hook
380 :group 'newsticker-hooks)
382 (defcustom newsticker-stop-hook
384 "Hook run when stopping newsticker.
385 This hook is run at the very end of `newsticker-stop'."
386 :options nil
387 :type 'hook
388 :group 'newsticker-hooks)
390 (defcustom newsticker-new-item-functions
392 "List of functions run after a new headline has been retrieved.
393 Each function is called with the following two arguments:
394 FEEDNAME the name of the corresponding news feed,
395 ITEM the decoded headline.
397 See `newsticker-new-item-functions-sample',
398 `newsticker-download-images', and
399 `newsticker-download-enclosures' for sample functions.
401 Please note that these functions are called only once for a
402 headline after it has been retrieved for the first time."
403 :type 'hook
404 :options '(newsticker-download-images
405 newsticker-download-enclosures)
406 :group 'newsticker-hooks)
408 ;; ======================================================================
409 ;; miscellaneous
410 (defgroup newsticker-miscellaneous nil
411 "Miscellaneous newsticker settings."
412 :group 'newsticker)
414 (defcustom newsticker-cache-filename
415 "~/.newsticker-cache"
416 "Name of the newsticker cache file."
417 :type 'string
418 :group 'newsticker-miscellaneous)
419 (make-obsolete-variable 'newsticker-cache-filename 'newsticker-dir "23.1")
421 (defcustom newsticker-dir
422 (locate-user-emacs-file "newsticker/" ".newsticker/")
423 "Directory where newsticker saves data."
424 :type 'directory
425 :group 'newsticker-miscellaneous)
427 ;; debugging
428 (defcustom newsticker-debug
430 "Enables some features needed for debugging newsticker.el.
432 If set to t newsticker.el will print lots of debugging messages, and the
433 buffers *newsticker-wget-<feed>* will not be closed."
434 :type 'boolean
435 :group 'newsticker-miscellaneous)
437 ;; ======================================================================
438 ;;; Compatibility section, XEmacs, Emacs
439 ;; ======================================================================
441 ;; FIXME It is bad practice to define compat functions with such generic names.
443 (unless (fboundp 'match-string-no-properties)
444 (defalias 'match-string-no-properties 'match-string))
446 (when (featurep 'xemacs)
447 (unless (fboundp 'replace-regexp-in-string)
448 (defun replace-regexp-in-string (re rp st)
449 (save-match-data ;; apparently XEmacs needs save-match-data
450 (replace-in-string st re rp)))))
452 ;; copied from subr.el
453 (unless (fboundp 'add-to-invisibility-spec)
454 (defun add-to-invisibility-spec (arg)
455 "Add elements to `buffer-invisibility-spec'.
456 See documentation for `buffer-invisibility-spec' for the kind of elements
457 that can be added."
458 (if (eq buffer-invisibility-spec t)
459 (setq buffer-invisibility-spec (list t)))
460 (setq buffer-invisibility-spec
461 (cons arg buffer-invisibility-spec))))
463 ;; copied from subr.el
464 (unless (fboundp 'remove-from-invisibility-spec)
465 (defun remove-from-invisibility-spec (arg)
466 "Remove elements from `buffer-invisibility-spec'."
467 (if (consp buffer-invisibility-spec)
468 (setq buffer-invisibility-spec
469 (delete arg buffer-invisibility-spec)))))
471 ;; ======================================================================
472 ;;; Internal variables
473 ;; ======================================================================
474 (defvar newsticker--buffer-uptodate-p nil
475 "Tells whether the newsticker buffer is up to date.")
476 (defvar newsticker--latest-update-time (current-time)
477 "The time at which the latest news arrived.")
478 (defvar newsticker--process-ids nil
479 "List of PIDs of active newsticker processes.")
481 (defvar newsticker--cache nil "Cached newsticker data.
482 This is a list of the form
484 ((label1
485 (title description link time age index preformatted-contents
486 preformatted-title extra-elements)
487 ...)
488 (label2
489 (title description link time age index preformatted-contents
490 preformatted-title extra-elements)
491 ...)
492 ...)
494 where LABEL is a symbol. TITLE, DESCRIPTION, and LINK are
495 strings. TIME is a time value as returned by `current-time'.
496 AGE is a symbol: `new', `old', `immortal', and `obsolete' denote
497 ordinary news items, whereas `feed' denotes an item which is not a
498 headline but describes the feed itself. INDEX denotes the
499 original position of the item -- used for restoring the original
500 order. PREFORMATTED-CONTENTS and PREFORMATTED-TITLE hold the
501 formatted contents of the item's description and title. This
502 speeds things up if HTML rendering is used, which is rather
503 slow. EXTRA-ELEMENTS is an alist containing additional elements.")
505 (defvar newsticker--auto-narrow-to-feed nil
506 "Automatically narrow to current news feed.
507 If non-nil only the items of the current news feed are visible.")
509 (defvar newsticker--auto-narrow-to-item nil
510 "Automatically narrow to current news item.
511 If non-nil only the current headline is visible.")
513 (defconst newsticker--error-headline
514 "[COULD NOT DOWNLOAD HEADLINES!]"
515 "Title of error headline which will be inserted if news retrieval fails.")
517 ;; ======================================================================
518 ;;; Shortcuts
519 ;; ======================================================================
520 (defsubst newsticker--title (item)
521 "Return title of ITEM."
522 (nth 0 item))
523 (defsubst newsticker--desc (item)
524 "Return description of ITEM."
525 (nth 1 item))
526 (defsubst newsticker--link (item)
527 "Return link of ITEM."
528 (nth 2 item))
529 (defsubst newsticker--time (item)
530 "Return time of ITEM."
531 (nth 3 item))
532 (defsubst newsticker--age (item)
533 "Return age of ITEM."
534 (nth 4 item))
535 (defsubst newsticker--pos (item)
536 "Return position/index of ITEM."
537 (nth 5 item))
538 (defsubst newsticker--preformatted-contents (item)
539 "Return pre-formatted text of ITEM."
540 (nth 6 item))
541 (defsubst newsticker--preformatted-title (item)
542 "Return pre-formatted title of ITEM."
543 (nth 7 item))
544 (defsubst newsticker--extra (item)
545 "Return extra attributes of ITEM."
546 (nth 8 item))
547 (defsubst newsticker--guid-to-string (guid)
548 "Return string representation of GUID."
549 (if (stringp guid)
550 guid
551 (car (xml-node-children guid))))
552 (defsubst newsticker--guid (item)
553 "Return guid of ITEM."
554 (newsticker--guid-to-string (assoc 'guid (newsticker--extra item))))
555 (defsubst newsticker--enclosure (item)
556 "Return enclosure element of ITEM in the form (...FIXME...) or nil."
557 (let ((enclosure (assoc 'enclosure (newsticker--extra item))))
558 (if enclosure
559 (xml-node-attributes enclosure))))
560 (defun newsticker--real-feed-name (feed)
561 "Return real name of FEED."
562 (catch 'name
563 (mapc (lambda (item)
564 (if (eq (newsticker--age item) 'feed)
565 (throw 'name (newsticker--title item))))
566 (cdr (newsticker--cache-get-feed feed)))
567 (symbol-name feed)))
570 ;; ======================================================================
571 ;;; User fun
572 ;; ======================================================================
574 (defun newsticker--start-feed (feed &optional do-not-complain-if-running)
575 "Start retrieval timer for FEED.
576 If timer is running already a warning message is printed unless
577 DO-NOT-COMPLAIN-IF-RUNNING is not nil. Add the started
578 name/timer pair to `newsticker--retrieval-timer-list'."
579 (let* ((feed-name (car feed))
580 (start-time (nth 2 feed))
581 (interval (or (nth 3 feed)
582 newsticker-retrieval-interval))
583 (timer (assoc (car feed)
584 newsticker--retrieval-timer-list)))
585 (if timer
586 (or do-not-complain-if-running
587 (message "Timer for %s is running already!"
588 feed-name))
589 (newsticker--debug-msg "Starting timer for %s: %s, %d"
590 feed-name start-time interval)
591 ;; do not repeat retrieval if interval not positive
592 (if (<= interval 0)
593 (setq interval nil))
594 ;; Suddenly XEmacs doesn't like start-time 0
595 (if (or (not start-time)
596 (and (numberp start-time) (= start-time 0)))
597 (setq start-time 1))
598 ;; (message "start-time %s" start-time)
599 (setq timer (run-at-time start-time interval
600 'newsticker-get-news feed-name))
601 (if interval
602 (add-to-list 'newsticker--retrieval-timer-list
603 (cons feed-name timer))))))
605 ;;;###autoload
606 (defun newsticker-start (&optional _do-not-complain-if-running)
607 "Start the newsticker.
608 Start the timers for display and retrieval. If the newsticker, i.e. the
609 timers, are running already a warning message is printed unless
610 DO-NOT-COMPLAIN-IF-RUNNING is not nil.
611 Run `newsticker-start-hook' if newsticker was not running already."
612 (interactive)
613 (let ((running (newsticker-running-p)))
614 ;; read old cache if it exists and newsticker is not running
615 (unless running
616 (newsticker--cache-read))
617 ;; start retrieval timers -- one timer for each feed
618 (dolist (feed (append newsticker-url-list-defaults newsticker-url-list))
619 (newsticker--start-feed feed))
620 (unless running
621 (run-hooks 'newsticker-start-hook)
622 (message "Newsticker started!"))))
624 (defun newsticker--stop-feed (feed-name)
625 "Stop retrieval for feed FEED-NAME.
626 Delete the stopped name/timer pair from `newsticker--retrieval-timer-list'."
627 (let ((name-and-timer (assoc feed-name newsticker--retrieval-timer-list)))
628 (when name-and-timer
629 (cancel-timer (cdr name-and-timer))
630 (setq newsticker--retrieval-timer-list
631 (delete name-and-timer newsticker--retrieval-timer-list)))))
633 (defun newsticker-stop ()
634 "Stop the newsticker and the newsticker-ticker.
635 Cancel the timers for display and retrieval. Run `newsticker-stop-hook'
636 if newsticker has been running."
637 (interactive)
638 (newsticker--cache-save)
639 (when (fboundp 'newsticker-stop-ticker) ; silence compiler warnings
640 (newsticker-stop-ticker))
641 (when (newsticker-running-p)
642 (dolist (name-and-timer newsticker--retrieval-timer-list)
643 (newsticker--stop-feed (car name-and-timer)))
644 (setq newsticker--retrieval-timer-list nil)
645 (run-hooks 'newsticker-stop-hook)
646 (message "Newsticker stopped!")))
648 (defun newsticker-get-all-news ()
649 "Launch retrieval of news from all configured newsticker sites.
650 This does NOT start the retrieval timers."
651 (interactive)
652 ;; launch retrieval of news
653 (dolist (item (append newsticker-url-list-defaults newsticker-url-list))
654 (newsticker-get-news (car item))))
656 (defun newsticker-save-item (feed item)
657 "Save FEED ITEM."
658 (interactive)
659 (let ((filename (read-string "Filename: "
660 (concat feed ":_"
661 (replace-regexp-in-string
662 " " "_" (newsticker--title item))
663 ".html"))))
664 (with-temp-buffer
665 (insert (newsticker--desc item))
666 (write-file filename t))))
668 (defun newsticker-add-url (url name)
669 "Add given URL under given NAME to `newsticker-url-list'.
670 If URL is nil it is searched at point."
671 (interactive
672 (list
673 (read-string "URL: "
674 (save-excursion
675 (end-of-line)
676 (and
677 (re-search-backward
678 "http://"
679 (if (> (point) (+ (point-min) 100))
680 (- (point) 100)
681 (point-min))
683 (re-search-forward
684 "http://[-a-zA-Z0-9&/_.]*"
685 (if (< (point) (- (point-max) 200))
686 (+ (point) 200)
687 (point-max))
689 (buffer-substring-no-properties (match-beginning 0)
690 (match-end 0)))))
691 (read-string "Name: ")))
692 (add-to-list 'newsticker-url-list (list name url nil nil nil) t)
693 (customize-variable 'newsticker-url-list))
695 (defun newsticker-customize ()
696 "Open the newsticker customization group."
697 (interactive)
698 (delete-other-windows)
699 (customize-group "newsticker"))
701 ;; ======================================================================
702 ;;; Local stuff
703 ;; ======================================================================
704 (defun newsticker--get-news-by-funcall (feed-name function)
705 "Get news for the site FEED-NAME by calling FUNCTION.
706 See `newsticker-get-news'."
707 (let ((buffername (concat " *newsticker-funcall-" feed-name "*")))
708 (with-current-buffer (get-buffer-create buffername)
709 (erase-buffer)
710 (newsticker--insert-bytes (funcall function feed-name))
711 (newsticker--sentinel-work nil t feed-name function
712 (current-buffer)))))
714 (defun newsticker--get-news-by-url (feed-name url)
715 "Get news for the site FEED-NAME from address URL using `url-retrieve'.
716 See `newsticker-get-news'."
717 (let ((coding-system-for-read 'no-conversion))
718 (condition-case error-data
719 (url-retrieve url 'newsticker--get-news-by-url-callback
720 (list feed-name))
721 (error (message "Error retrieving news from %s: %s" feed-name
722 error-data))))
723 (force-mode-line-update))
725 (defun newsticker--get-news-by-url-callback (status feed-name)
726 "Callback function for `newsticker--get-news-by-url'.
727 STATUS is the return status as delivered by `url-retrieve', and
728 FEED-NAME is the name of the feed that the news were retrieved
729 from."
730 (let ((buf (get-buffer-create (concat " *newsticker-url-" feed-name "*")))
731 (result (buffer-string)))
732 (set-buffer buf)
733 (erase-buffer)
734 (newsticker--insert-bytes result)
735 ;; remove MIME header
736 (goto-char (point-min))
737 (search-forward "\n\n" nil t)
738 (delete-region (point-min) (point))
739 ;; read the rss/atom contents
740 (newsticker--sentinel-work nil
741 (or (not status)
742 (not (eq (car status) :error)))
743 feed-name "url-retrieve"
744 (current-buffer))
745 (when status
746 (let ((status-type (car status))
747 (status-details (cdr status)))
748 (cond ((eq status-type :redirect)
749 ;; don't care about redirects
751 ((eq status-type :error)
752 (message "%s: Error while retrieving news from %s: %s: \"%s\""
753 (format-time-string "%A, %H:%M")
754 feed-name
755 (car status-details) (cdr status-details))))))))
757 (defun newsticker--get-news-by-wget (feed-name url wget-arguments)
758 "Get news for the site FEED-NAME from address URL using wget.
759 WGET-ARGUMENTS is a list of arguments for wget.
760 See `newsticker-get-news'."
761 (let ((buffername (concat " *newsticker-wget-" feed-name "*")))
762 (with-current-buffer (get-buffer-create buffername)
763 (erase-buffer)
764 ;; throw an error if there is an old wget-process around
765 (if (get-process feed-name)
766 (error "Another wget-process is running for %s" feed-name))
767 ;; start wget
768 (let* ((args (append wget-arguments (list url)))
769 (proc (apply 'start-process feed-name buffername
770 newsticker-wget-name args)))
771 (set-process-coding-system proc 'no-conversion 'no-conversion)
772 (set-process-sentinel proc 'newsticker--sentinel)
773 (process-put proc 'nt-feed-name feed-name)
774 (setq newsticker--process-ids (cons (process-id proc)
775 newsticker--process-ids))
776 (force-mode-line-update)))))
778 (defun newsticker-get-news (feed-name)
779 "Get news from the site FEED-NAME and load feed logo.
780 FEED-NAME must be a string which occurs as the label (i.e. the first element)
781 in an element of `newsticker-url-list' or `newsticker-url-list-defaults'."
782 (newsticker--debug-msg "%s: Getting news for %s"
783 (format-time-string "%A, %H:%M")
784 feed-name)
785 (let* ((item (or (assoc feed-name newsticker-url-list)
786 (assoc feed-name newsticker-url-list-defaults)
787 (error
788 "Cannot get news for %s: Check newsticker-url-list"
789 feed-name)))
790 (url (cadr item))
791 (wget-arguments (or (car (cdr (cdr (cdr (cdr item)))))
792 newsticker-wget-arguments)))
793 (if (functionp url)
794 (newsticker--get-news-by-funcall feed-name url)
795 (if (eq newsticker-retrieval-method 'intern)
796 (newsticker--get-news-by-url feed-name url)
797 (newsticker--get-news-by-wget feed-name url wget-arguments)))))
799 ;; ======================================================================
800 ;; Parsing
801 ;; ======================================================================
803 (defun newsticker--sentinel (process event)
804 "Sentinel for extracting news titles from an RDF buffer.
805 Argument PROCESS is the process which has just changed its state.
806 Argument EVENT tells what has happened to the process."
807 (let ((p-status (process-status process))
808 (exit-status (process-exit-status process))
809 (feed-name (process-get process 'nt-feed-name))
810 (command (process-command process))
811 (buffer (process-buffer process)))
812 (newsticker--sentinel-work event
813 (and (eq p-status 'exit)
814 (= exit-status 0))
815 feed-name command buffer)))
817 (defun newsticker--sentinel-work (event status-ok feed-name command buffer)
818 "Actually do the sentinel work.
819 Argument EVENT tells what has happened to the retrieval process.
820 Argument STATUS-OK is the final status of the retrieval process,
821 non-nil meaning retrieval was successful.
822 Argument FEED-NAME is the name of the retrieved feed.
823 Argument COMMAND is the command of the retrieval process.
824 Argument BUFFER is the buffer of the retrieval process."
825 (let ((time (current-time))
826 (name-symbol (intern feed-name))
827 (something-was-added nil)
828 (ct (current-time)))
829 ;; catch known errors (zombie processes, rubbish-xml etc.
830 ;; if an error occurs the news feed is not updated!
831 (catch 'oops
832 (unless status-ok
833 (setq newsticker--cache
834 (newsticker--cache-add
835 newsticker--cache
836 name-symbol
837 newsticker--error-headline
838 (format-message
839 (concat "%s: Newsticker could not retrieve news from %s.\n"
840 "Return status: `%s'\n"
841 "Command was `%s'")
842 (format-time-string "%A, %H:%M")
843 feed-name event command)
846 'new
847 0 '((guid nil "newsticker--download-error"))
848 ct))
849 (message "%s: Error while retrieving news from %s"
850 (format-time-string "%A, %H:%M")
851 feed-name)
852 (throw 'oops nil))
853 (let* ((coding-system 'utf-8)
854 (node-list
855 (save-current-buffer
856 (set-buffer buffer)
857 (unless (fboundp 'libxml-parse-xml-region)
858 (newsticker--do-xml-workarounds))
859 ;; check coding system
860 (goto-char (point-min))
861 (if (re-search-forward "encoding=['\"]\\([^\"]+?\\)['\"]"
862 nil t)
863 (setq coding-system (intern (downcase (match-string 1))))
864 (setq coding-system
865 (condition-case nil
866 (check-coding-system coding-system)
867 (coding-system-error
868 (message
869 "newsticker.el: ignoring coding system %s for %s"
870 coding-system feed-name)
871 nil))))
872 ;; Decode if possible
873 (when coding-system
874 (decode-coding-region (point-min) (point-max)
875 coding-system))
876 (condition-case errordata
877 ;; The xml parser might fail or the xml might be
878 ;; bugged
879 (if (fboundp 'libxml-parse-xml-region)
880 (list (libxml-parse-xml-region (point-min) (point-max)
881 nil t))
882 (xml-parse-region (point-min) (point-max)))
883 (error (message "Could not parse %s: %s"
884 (buffer-name) (cadr errordata))
885 (throw 'oops nil)))))
886 (topnode (car node-list))
887 (image-url nil)
888 (icon-url nil))
889 ;; mark all items as obsolete
890 (newsticker--cache-replace-age newsticker--cache
891 name-symbol
892 'new 'obsolete-new)
893 (newsticker--cache-replace-age newsticker--cache
894 name-symbol
895 'old 'obsolete-old)
896 (newsticker--cache-replace-age newsticker--cache
897 name-symbol
898 'feed 'obsolete-old)
900 ;; check Atom/RSS version and call corresponding parser
901 (condition-case error-data
902 (if (cond
903 ;; RSS 0.91
904 ((and (eq 'rss (xml-node-name topnode))
905 (string= "0.91" (xml-get-attribute topnode 'version)))
906 (setq image-url (newsticker--get-logo-url-rss-0.91 topnode))
907 (newsticker--parse-rss-0.91 feed-name time topnode))
908 ;; RSS 0.92
909 ((and (eq 'rss (xml-node-name topnode))
910 (string= "0.92" (xml-get-attribute topnode 'version)))
911 (setq image-url (newsticker--get-logo-url-rss-0.92 topnode))
912 (newsticker--parse-rss-0.92 feed-name time topnode))
913 ;; RSS 1.0
914 ((or (eq 'RDF (xml-node-name topnode))
915 (eq 'rdf:RDF (xml-node-name topnode)))
916 (setq image-url (newsticker--get-logo-url-rss-1.0 topnode))
917 (newsticker--parse-rss-1.0 feed-name time topnode))
918 ;; RSS 2.0
919 ((and (eq 'rss (xml-node-name topnode))
920 (string= "2.0" (xml-get-attribute topnode 'version)))
921 (setq image-url (newsticker--get-logo-url-rss-2.0 topnode))
922 (newsticker--parse-rss-2.0 feed-name time topnode))
923 ;; Atom 0.3
924 ((and (eq 'feed (xml-node-name topnode))
925 (string= "http://purl.org/atom/ns#"
926 (xml-get-attribute topnode 'xmlns)))
927 (setq image-url (newsticker--get-logo-url-atom-0.3 topnode))
928 (newsticker--parse-atom-0.3 feed-name time topnode))
929 ;; Atom 1.0
931 ;; The test for Atom 1.0 does not work when using
932 ;; libxml, as with libxml the namespace attribute is
933 ;; not in the xml tree. For the time being we skip
934 ;; the check and assume that we are dealing with an
935 ;; Atom 1.0 feed.
937 ;; (and (eq 'feed (xml-node-name topnode))
938 ;; (string= "http://www.w3.org/2005/Atom"
939 ;; (xml-get-attribute topnode 'xmlns)))
940 (setq image-url (newsticker--get-logo-url-atom-1.0 topnode))
941 (setq icon-url (newsticker--get-icon-url-atom-1.0 topnode))
942 (newsticker--parse-atom-1.0 feed-name time topnode))
943 ;; unknown feed type
944 ;; (t
945 ;; (newsticker--debug-msg "Feed type unknown: %s: %s"
946 ;; (xml-node-name topnode) feed-name)
947 ;; nil)
949 (setq something-was-added t))
950 (error (message "sentinelerror in %s: %s" feed-name error-data)))
952 ;; Remove those old items from cache which have been removed from
953 ;; the feed
954 (newsticker--cache-replace-age newsticker--cache
955 name-symbol 'obsolete-old 'deleteme)
956 (newsticker--cache-remove newsticker--cache name-symbol
957 'deleteme)
958 ;; Remove those new items from cache which have been removed from
959 ;; the feed. Or keep them as `obsolete'
960 (if (not newsticker-keep-obsolete-items)
961 (newsticker--cache-remove newsticker--cache
962 name-symbol 'obsolete-new)
963 (setq newsticker--cache
964 (newsticker--cache-mark-expired
965 newsticker--cache name-symbol 'obsolete 'obsolete-expired
966 newsticker-obsolete-item-max-age))
967 (newsticker--cache-remove newsticker--cache
968 name-symbol 'obsolete-expired)
969 (newsticker--cache-replace-age newsticker--cache
970 name-symbol 'obsolete-new
971 'obsolete))
972 (newsticker--update-process-ids)
973 ;; setup scrollable text
974 (when (= 0 (length newsticker--process-ids))
975 (when (fboundp 'newsticker--ticker-text-setup) ;silence
976 ;compiler
977 ;warnings
978 (newsticker--ticker-text-setup)))
979 (setq newsticker--latest-update-time (current-time))
980 (when something-was-added
981 ;; FIXME: should we care about removed items as well?
982 (newsticker--cache-save-feed
983 (newsticker--cache-get-feed name-symbol))
984 (when (fboundp 'newsticker--buffer-set-uptodate) ;silence
985 ;compiler
986 ;warnings
987 (newsticker--buffer-set-uptodate nil)))
988 ;; kill the process buffer if wanted
989 (unless newsticker-debug
990 (kill-buffer buffer))
991 ;; launch retrieval of images
992 (when (and (boundp 'newsticker-download-logos)
993 newsticker-download-logos)
994 ;; feed logo
995 (when image-url
996 (newsticker--image-get feed-name feed-name (newsticker--images-dir)
997 image-url))
998 ;; icon / favicon
999 (setq icon-url
1000 (or icon-url
1001 (let* ((feed-url (newsticker--link (cadr (newsticker--cache-get-feed
1002 (intern feed-name)))))
1003 (uri (url-generic-parse-url feed-url)))
1004 (when (and feed-url uri)
1005 (setf (url-filename uri) nil)
1006 (setf (url-target uri) nil)
1007 (concat (url-recreate-url uri) "favicon.ico")))))
1008 (when icon-url
1009 (newsticker--image-get feed-name
1010 (concat feed-name "."
1011 (file-name-extension icon-url))
1012 (newsticker--icons-dir)
1013 icon-url))))))
1014 (when newsticker--sentinel-callback
1015 (funcall newsticker--sentinel-callback)))
1017 (defun newsticker--do-xml-workarounds ()
1018 "Fix all issues which `xml-parse-region' could be choking on."
1020 ;; a very very dirty workaround to overcome the
1021 ;; problems with the newest (20030621) xml.el:
1022 ;; remove all unnecessary whitespace
1023 (goto-char (point-min))
1024 (while (re-search-forward ">[ \t\r\n]+<" nil t)
1025 (replace-match "><" nil t))
1026 ;; and another brutal workaround (20031105)! For some
1027 ;; reason the xml parser does not like the colon in the
1028 ;; doctype name "rdf:RDF"
1029 (goto-char (point-min))
1030 (if (re-search-forward "<!DOCTYPE[ \t\n]+rdf:RDF" nil t)
1031 (replace-match "<!DOCTYPE rdfColonRDF" nil t))
1032 ;; finally.... ~##^°!!!!!
1033 (goto-char (point-min))
1034 (while (search-forward "\r\n" nil t)
1035 (replace-match "\n" nil t))
1036 ;; still more brutal workarounds (20040309)! The xml
1037 ;; parser does not like doctype rss
1038 (goto-char (point-min))
1039 (if (re-search-forward "<!DOCTYPE[ \t\n]+rss[ \t\n]*>" nil t)
1040 (replace-match "" nil t))
1041 ;; And another one (20050618)! (Fixed in GNU Emacs 22.0.50.18)
1042 ;; Remove comments to avoid this xml-parsing bug:
1043 ;; "XML files can have only one toplevel tag"
1044 (goto-char (point-min))
1045 (while (search-forward "<!--" nil t)
1046 (let ((start (match-beginning 0)))
1047 (unless (search-forward "-->" nil t)
1048 (error "Can't find end of comment"))
1049 (delete-region start (point))))
1050 ;; And another one (20050702)! If description is HTML
1051 ;; encoded and starts with a `<', wrap the whole
1052 ;; description in a CDATA expression. This happened for
1053 ;; http://www.thefreedictionary.com/_/WoD/rss.aspx?type=quote
1054 (goto-char (point-min))
1055 (while (re-search-forward
1056 "<description>\\(<img.*?\\)</description>" nil t)
1057 (replace-match
1058 "<description><![CDATA[ \\1 ]]></description>"))
1059 ;; And another one (20051123)! XML parser does not
1060 ;; like this: <yweather:location city="Frankfurt/Main"
1061 ;; region="" country="GM" />
1062 ;; try to "fix" empty attributes
1063 ;; This happened for
1064 ;; http://xml.weather.yahoo.com/forecastrss?p=GMXX0040&u=f
1065 (goto-char (point-min))
1066 (while (re-search-forward "\\(<[^>]*\\)=\"\"" nil t)
1067 (replace-match "\\1=\" \""))
1069 (set-buffer-modified-p nil))
1072 (defun newsticker--get-logo-url-atom-1.0 (node)
1073 "Return logo URL from atom 1.0 data in NODE."
1074 (car (xml-node-children
1075 (car (xml-get-children node 'logo)))))
1077 (defun newsticker--get-icon-url-atom-1.0 (node)
1078 "Return icon URL from atom 1.0 data in NODE."
1079 (car (xml-node-children
1080 (car (xml-get-children node 'icon)))))
1082 (defun newsticker--get-logo-url-atom-0.3 (node)
1083 "Return logo URL from atom 0.3 data in NODE."
1084 (car (xml-node-children
1085 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1087 (defun newsticker--get-logo-url-rss-2.0 (node)
1088 "Return logo URL from RSS 2.0 data in NODE."
1089 (car (xml-node-children
1090 (car (xml-get-children
1091 (car (xml-get-children
1092 (car (xml-get-children node 'channel)) 'image)) 'url)))))
1094 (defun newsticker--get-logo-url-rss-1.0 (node)
1095 "Return logo URL from RSS 1.0 data in NODE."
1096 (car (xml-node-children
1097 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1099 (defun newsticker--get-logo-url-rss-0.92 (node)
1100 "Return logo URL from RSS 0.92 data in NODE."
1101 (car (xml-node-children
1102 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1104 (defun newsticker--get-logo-url-rss-0.91 (node)
1105 "Return logo URL from RSS 0.91 data in NODE."
1106 (car (xml-node-children
1107 (car (xml-get-children (car (xml-get-children node 'image)) 'url)))))
1109 (defun newsticker--parse-atom-0.3 (name time topnode)
1110 "Parse Atom 0.3 data.
1111 Return value as well as arguments NAME, TIME, and TOPNODE are the
1112 same as in `newsticker--parse-atom-1.0'."
1113 (newsticker--debug-msg "Parsing Atom 0.3 feed %s" name)
1114 (let (new-feed new-item)
1115 (setq new-feed (newsticker--parse-generic-feed
1116 name time
1117 ;; title
1118 (car (xml-node-children
1119 (car (xml-get-children topnode 'title))))
1120 ;; desc
1121 (car (xml-node-children
1122 (car (xml-get-children topnode 'content))))
1123 ;; link
1124 (xml-get-attribute
1125 (car (xml-get-children topnode 'link)) 'href)
1126 ;; extra-elements
1127 (xml-node-children topnode)))
1128 (setq new-item (newsticker--parse-generic-items
1129 name time (xml-get-children topnode 'entry)
1130 ;; title-fn
1131 (lambda (node)
1132 (car (xml-node-children
1133 (car (xml-get-children node 'title)))))
1134 ;; desc-fn
1135 (lambda (node)
1136 (or (car (xml-node-children
1137 (car (xml-get-children node 'content))))
1138 (car (xml-node-children
1139 (car (xml-get-children node 'summary))))))
1140 ;; link-fn
1141 (lambda (node)
1142 (xml-get-attribute
1143 (car (xml-get-children node 'link)) 'href))
1144 ;; time-fn
1145 (lambda (node)
1146 (newsticker--decode-rfc822-date
1147 (car (xml-node-children
1148 (car (xml-get-children node 'modified))))))
1149 ;; guid-fn
1150 (lambda (node)
1151 (newsticker--guid-to-string
1152 (assoc 'guid (xml-node-children node))))
1153 ;; extra-fn
1154 (lambda (node)
1155 (xml-node-children node))))
1156 (or new-item new-feed)))
1158 (defun newsticker--unxml (node)
1159 "Reverse parsing of an xml string.
1160 Restore an xml-string from a an xml NODE that was returned by xml-parse..."
1161 (if (or (not node) (stringp node))
1162 node
1163 (newsticker--unxml-node node)))
1165 (defun newsticker--unxml-node (node)
1166 "Actually restore xml-string of an xml NODE."
1167 (let ((qname (symbol-name (car node)))
1168 (att-list (cadr node))
1169 (children (cddr node)))
1170 (concat "<" qname
1171 (when att-list " ")
1172 (mapconcat 'newsticker--unxml-attribute att-list " ")
1174 (mapconcat 'newsticker--unxml children "") "</" qname ">")))
1176 (defun newsticker--unxml-attribute (attribute)
1177 "Actually restore xml-string of an ATTRIBUTE of an xml node."
1178 (let ((name (symbol-name (car attribute)))
1179 (value (cdr attribute)))
1180 (concat name "=\"" value "\"")))
1182 (defun newsticker--parse-atom-1.0 (name time topnode)
1183 "Parse Atom 1.0 data.
1184 Argument NAME gives the name of a news feed. TIME gives the
1185 system time at which the data have been retrieved. TOPNODE
1186 contains the feed data as returned by the xml parser.
1188 For the Atom 1.0 specification see
1189 URL `http://www.atompub.org/2005/08/17/draft-ietf-atompub-format-11.html'"
1190 (newsticker--debug-msg "Parsing Atom 1.0 feed %s" name)
1191 (let (new-feed new-item)
1192 (setq new-feed (newsticker--parse-generic-feed
1193 name time
1194 ;; title
1195 (car (xml-node-children
1196 (car (xml-get-children topnode 'title))))
1197 ;; desc
1198 (car (xml-node-children
1199 (car (xml-get-children topnode 'subtitle))))
1200 ;; link
1201 (lambda (node)
1202 (xml-get-attribute
1203 (car (xml-get-children node 'link)) 'href))
1204 ;; extra-elements
1205 (xml-node-children topnode)))
1206 (setq new-item (newsticker--parse-generic-items
1207 name time (xml-get-children topnode 'entry)
1208 ;; title-fn
1209 (lambda (node)
1210 (car (xml-node-children
1211 (car (xml-get-children node 'title)))))
1212 ;; desc-fn
1213 (lambda (node)
1214 ;; unxml the content or the summary node. Atom
1215 ;; allows for integrating (x)html into the atom
1216 ;; structure but we need the raw html string.
1217 ;; e.g. http://www.heise.de/open/news/news-atom.xml
1218 ;; http://feeds.feedburner.com/ru_nix_blogs
1219 (or (newsticker--unxml
1220 (car (xml-node-children
1221 (car (xml-get-children node 'content)))))
1222 (newsticker--unxml
1223 (car (xml-node-children
1224 (car (xml-get-children node 'summary)))))
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 is-new-feed has-new-items)
1257 (setq is-new-feed (newsticker--parse-generic-feed
1258 name time
1259 ;; title
1260 (car (xml-node-children
1261 (car (xml-get-children channelnode 'title))))
1262 ;; desc
1263 (car (xml-node-children
1264 (car (xml-get-children channelnode
1265 'description))))
1266 ;; link
1267 (car (xml-node-children
1268 (car (xml-get-children channelnode 'link))))
1269 ;; extra-elements
1270 (xml-node-children channelnode)))
1271 (setq has-new-items (newsticker--parse-generic-items
1272 name time (xml-get-children channelnode 'item)
1273 ;; title-fn
1274 (lambda (node)
1275 (car (xml-node-children
1276 (car (xml-get-children node 'title)))))
1277 ;; desc-fn
1278 (lambda (node)
1279 (car (xml-node-children
1280 (car (xml-get-children node 'description)))))
1281 ;; link-fn
1282 (lambda (node)
1283 (car (xml-node-children
1284 (car (xml-get-children node 'link)))))
1285 ;; time-fn
1286 (lambda (node)
1287 (newsticker--decode-rfc822-date
1288 (car (xml-node-children
1289 (car (xml-get-children node 'pubDate))))))
1290 ;; guid-fn
1291 (lambda (_node)
1292 nil)
1293 ;; extra-fn
1294 (lambda (node)
1295 (xml-node-children node))))
1296 (or has-new-items is-new-feed)))
1298 (defun newsticker--parse-rss-0.92 (name time topnode)
1299 "Parse RSS 0.92 data.
1300 Return value as well as arguments NAME, TIME, and TOPNODE are the
1301 same as in `newsticker--parse-atom-1.0'.
1303 For the RSS 0.92 specification see URL `http://backend.userland.com/rss092'."
1304 (newsticker--debug-msg "Parsing RSS 0.92 feed %s" name)
1305 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1306 is-new-feed has-new-items)
1307 (setq is-new-feed (newsticker--parse-generic-feed
1308 name time
1309 ;; title
1310 (car (xml-node-children
1311 (car (xml-get-children channelnode 'title))))
1312 ;; desc
1313 (car (xml-node-children
1314 (car (xml-get-children channelnode
1315 'description))))
1316 ;; link
1317 (car (xml-node-children
1318 (car (xml-get-children channelnode 'link))))
1319 ;; extra-elements
1320 (xml-node-children channelnode)))
1321 (setq has-new-items (newsticker--parse-generic-items
1322 name time (xml-get-children channelnode 'item)
1323 ;; title-fn
1324 (lambda (node)
1325 (car (xml-node-children
1326 (car (xml-get-children node 'title)))))
1327 ;; desc-fn
1328 (lambda (node)
1329 (car (xml-node-children
1330 (car (xml-get-children node 'description)))))
1331 ;; link-fn
1332 (lambda (node)
1333 (car (xml-node-children
1334 (car (xml-get-children node 'link)))))
1335 ;; time-fn
1336 (lambda (node)
1337 (newsticker--decode-rfc822-date
1338 (car (xml-node-children
1339 (car (xml-get-children node 'pubDate))))))
1340 ;; guid-fn
1341 (lambda (_node)
1342 nil)
1343 ;; extra-fn
1344 (lambda (node)
1345 (xml-node-children node))))
1346 (or has-new-items is-new-feed)))
1348 (defun newsticker--parse-rss-1.0 (name time topnode)
1349 "Parse RSS 1.0 data.
1350 Return value as well as arguments NAME, TIME, and TOPNODE are the
1351 same as in `newsticker--parse-atom-1.0'.
1353 For the RSS 1.0 specification see URL `http://web.resource.org/rss/1.0/spec'."
1354 (newsticker--debug-msg "Parsing RSS 1.0 feed %s" name)
1355 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1356 is-new-feed has-new-items)
1357 (setq is-new-feed (newsticker--parse-generic-feed
1358 name time
1359 ;; title
1360 (car (xml-node-children
1361 (car (xml-get-children channelnode 'title))))
1362 ;; desc
1363 (or (car (xml-node-children
1364 (car (xml-get-children channelnode
1365 'encoded))))
1366 (car (xml-node-children
1367 (car (xml-get-children channelnode
1368 'content:encoded))))
1369 (car (xml-node-children
1370 (car (xml-get-children channelnode
1371 'description)))))
1372 ;; link
1373 (car (xml-node-children
1374 (car (xml-get-children channelnode 'link))))
1375 ;; extra-elements
1376 (xml-node-children channelnode)))
1377 (setq has-new-items (newsticker--parse-generic-items
1378 name time (xml-get-children topnode 'item)
1379 ;; title-fn
1380 (lambda (node)
1381 (car (xml-node-children
1382 (car (xml-get-children node 'title)))))
1383 ;; desc-fn
1384 (lambda (node)
1385 (car (xml-node-children
1386 (car (xml-get-children node
1387 'description)))))
1388 ;; link-fn
1389 (lambda (node)
1390 (car (xml-node-children
1391 (car (xml-get-children node 'link)))))
1392 ;; time-fn
1393 (lambda (node)
1394 (newsticker--decode-iso8601-date
1395 (or (car (xml-node-children
1396 (car (xml-get-children node 'dc:date))))
1397 (car (xml-node-children
1398 (car (xml-get-children node 'date)))))))
1399 ;; guid-fn
1400 (lambda (_node)
1401 nil)
1402 ;; extra-fn
1403 (lambda (node)
1404 (xml-node-children node))))
1405 (or has-new-items is-new-feed)))
1407 (defun newsticker--parse-rss-2.0 (name time topnode)
1408 "Parse RSS 2.0 data.
1409 Return value as well as arguments NAME, TIME, and TOPNODE are the
1410 same as in `newsticker--parse-atom-1.0'.
1412 For the RSS 2.0 specification see URL `http://blogs.law.harvard.edu/tech/rss'."
1413 (newsticker--debug-msg "Parsing RSS 2.0 feed %s" name)
1414 (let* ((channelnode (car (xml-get-children topnode 'channel)))
1415 is-new-feed has-new-items)
1416 (setq is-new-feed (newsticker--parse-generic-feed
1417 name time
1418 ;; title
1419 (car (xml-node-children
1420 (car (xml-get-children channelnode 'title))))
1421 ;; desc
1422 (or (car (xml-node-children
1423 (car (xml-get-children channelnode
1424 'encoded))))
1425 (car (xml-node-children
1426 (car (xml-get-children channelnode
1427 'content:encoded))))
1428 (car (xml-node-children
1429 (car (xml-get-children channelnode
1430 'description)))))
1431 ;; link
1432 (car (xml-node-children
1433 (car (xml-get-children channelnode 'link))))
1434 ;; extra-elements
1435 (xml-node-children channelnode)))
1436 (setq has-new-items (newsticker--parse-generic-items
1437 name time (xml-get-children channelnode 'item)
1438 ;; title-fn
1439 (lambda (node)
1440 (car (xml-node-children
1441 (car (xml-get-children node 'title)))))
1442 ;; desc-fn
1443 (lambda (node)
1444 (or (car (xml-node-children
1445 (car (xml-get-children node
1446 'encoded))))
1447 (car (xml-node-children
1448 (car (xml-get-children node
1449 'content:encoded))))
1450 (car (xml-node-children
1451 (car (xml-get-children node
1452 'description))))))
1453 ;; link-fn
1454 (lambda (node)
1455 (car (xml-node-children
1456 (car (xml-get-children node 'link)))))
1457 ;; time-fn
1458 (lambda (node)
1459 (newsticker--decode-rfc822-date
1460 (car (xml-node-children
1461 (car (xml-get-children node 'pubDate))))))
1462 ;; guid-fn
1463 (lambda (node)
1464 (newsticker--guid-to-string
1465 (assoc 'guid (xml-node-children node))))
1466 ;; extra-fn
1467 (lambda (node)
1468 (xml-node-children node))))
1469 (or has-new-items is-new-feed)))
1471 (defun newsticker--parse-generic-feed (name time title desc link
1472 extra-elements)
1473 "Parse generic news feed data.
1474 Argument NAME gives the name of a news feed. TIME gives the
1475 system time at which the data have been retrieved.
1477 The arguments TITLE, DESC, LINK, and EXTRA-ELEMENTS give the feed's title,
1478 description, link, and extra elements resp."
1479 (let ((title (or title "[untitled]"))
1480 (link (or link ""))
1481 (position 0)
1482 (something-was-added nil))
1483 ;; decode numeric entities
1484 (setq title (xml-substitute-numeric-entities title))
1485 (setq desc (xml-substitute-numeric-entities desc))
1486 (setq link (xml-substitute-numeric-entities link))
1487 ;; remove whitespace from title, desc, and link
1488 (setq title (newsticker--remove-whitespace title))
1489 (setq desc (newsticker--remove-whitespace desc))
1490 (setq link (newsticker--remove-whitespace link))
1492 ;; handle the feed itself
1493 (unless (newsticker--cache-contains newsticker--cache
1494 (intern name) title
1495 desc link 'feed)
1496 (setq something-was-added t))
1497 (setq newsticker--cache
1498 (newsticker--cache-add newsticker--cache (intern name)
1499 title desc link time 'feed position
1500 extra-elements time 'feed))
1501 something-was-added))
1503 (defun newsticker--parse-generic-items (name time itemlist
1504 title-fn desc-fn
1505 link-fn time-fn
1506 guid-fn extra-fn)
1507 "Parse generic news feed data.
1508 Argument NAME gives the name of a news feed. TIME gives the
1509 system time at which the data have been retrieved. ITEMLIST
1510 contains the news items returned by the xml parser.
1512 The arguments TITLE-FN, DESC-FN, LINK-FN, TIME-FN, GUID-FN, and
1513 EXTRA-FN give functions for extracting title, description, link,
1514 time, guid, and extra-elements resp. They are called with one
1515 argument, which is one of the items in ITEMLIST."
1516 (let ((position 0)
1517 (something-was-added nil))
1518 ;; gather all items for this feed
1519 (dolist (node itemlist)
1520 (setq position (1+ position))
1521 (let ((title (or (funcall title-fn node) "[untitled]"))
1522 (desc (funcall desc-fn node))
1523 (link (or (funcall link-fn node) "")))
1524 (setq time (or (funcall time-fn node) time))
1525 ;; It happened that the title or description
1526 ;; contained evil HTML code that confused the
1527 ;; xml parser. Therefore:
1528 (unless (stringp title)
1529 (setq title (prin1-to-string title)))
1530 (unless (or (stringp desc) (not desc))
1531 (setq desc (prin1-to-string desc)))
1532 ;; ignore items with empty title AND empty desc
1533 (when (or (> (length title) 0)
1534 (> (length desc) 0))
1535 ;; decode numeric entities
1536 (setq title (xml-substitute-numeric-entities title))
1537 (when desc
1538 (setq desc (xml-substitute-numeric-entities desc)))
1539 (setq link (xml-substitute-numeric-entities link))
1540 ;; remove whitespace from title, desc, and link
1541 (setq title (newsticker--remove-whitespace title))
1542 (setq desc (newsticker--remove-whitespace desc))
1543 (setq link (newsticker--remove-whitespace link))
1544 ;; add data to cache
1545 ;; do we have this item already?
1546 (let ((old-item
1547 (let* ((guid (funcall guid-fn node)))
1548 ;;(message "guid=%s" guid)
1549 (newsticker--cache-contains newsticker--cache
1550 (intern name) title
1551 desc link nil guid)))
1552 (age1 'new)
1553 (age2 'old)
1554 (item-new-p nil))
1555 ;; Add this item, or mark it as old, or do nothing
1556 (if old-item
1557 (let ((prev-age (newsticker--age old-item)))
1558 (unless newsticker-automatically-mark-items-as-old
1559 ;; Some feeds deliver items multiply, the
1560 ;; first time we find an 'obsolete-old one in
1561 ;; the cache, the following times we find an
1562 ;; 'old one
1563 (if (memq prev-age '(obsolete-old old))
1564 (setq age2 'old)
1565 (setq age2 'new)))
1566 (if (eq prev-age 'immortal)
1567 (setq age2 'immortal))
1568 (setq time (newsticker--time old-item)))
1569 ;; item was not there
1570 (setq item-new-p t)
1571 (setq something-was-added t))
1572 (let ((extra-elements-with-guid (funcall extra-fn node)))
1573 (unless (assoc 'guid extra-elements-with-guid)
1574 (setq extra-elements-with-guid
1575 (cons `(guid nil ,(funcall guid-fn node))
1576 extra-elements-with-guid)))
1577 (setq newsticker--cache
1578 (newsticker--cache-add
1579 newsticker--cache (intern name) title desc link
1580 time age1 position extra-elements-with-guid
1581 time age2)))
1582 (when item-new-p
1583 (let ((item (newsticker--cache-contains
1584 newsticker--cache (intern name) title
1585 desc link nil)))
1586 (if newsticker-auto-mark-filter-list
1587 (newsticker--run-auto-mark-filter name item))
1588 (run-hook-with-args
1589 'newsticker-new-item-functions name item)))))))
1590 something-was-added))
1592 ;; ======================================================================
1593 ;;; Misc
1594 ;; ======================================================================
1596 (defun newsticker--insert-bytes (bytes)
1597 (insert (decode-coding-string bytes 'binary)))
1599 (defun newsticker--remove-whitespace (string)
1600 "Remove leading and trailing whitespace from STRING."
1601 ;; we must have ...+ but not ...* in the regexps otherwise xemacs loops
1602 ;; endlessly...
1603 (when (and string (stringp string))
1604 (replace-regexp-in-string
1605 "[ \t\r\n]+$" ""
1606 (replace-regexp-in-string "^[ \t\r\n]+" "" string))))
1608 (defun newsticker--do-forget-preformatted (item)
1609 "Forget pre-formatted data for ITEM.
1610 Remove the pre-formatted from `newsticker--cache'."
1611 (if (nthcdr 7 item)
1612 (setcar (nthcdr 7 item) nil))
1613 (if (nthcdr 6 item)
1614 (setcar (nthcdr 6 item) nil)))
1616 (defun newsticker--forget-preformatted ()
1617 "Forget all cached pre-formatted data.
1618 Remove the pre-formatted from `newsticker--cache'."
1619 (mapc (lambda (feed)
1620 (mapc 'newsticker--do-forget-preformatted
1621 (cdr feed)))
1622 newsticker--cache)
1623 (when (fboundp 'newsticker--buffer-set-uptodate)
1624 (newsticker--buffer-set-uptodate nil)))
1626 (defun newsticker--debug-msg (string &rest args)
1627 "Print newsticker debug messages.
1628 This function calls `message' with arguments STRING and ARGS, if
1629 `newsticker-debug' is non-nil."
1630 (and newsticker-debug
1631 ;;(not (active-minibuffer-window))
1632 ;;(not (current-message))
1633 (apply 'message string args)))
1635 (defun newsticker--decode-iso8601-date (iso8601-string)
1636 "Return ISO8601-STRING in format like `decode-time'.
1637 Converts from ISO-8601 to Emacs representation.
1638 Examples:
1639 2004-09-17T05:09:49.001+00:00
1640 2004-09-17T05:09:49+00:00
1641 2004-09-17T05:09+00:00
1642 2004-09-17T05:09:49
1643 2004-09-17T05:09
1644 2004-09-17
1645 2004-09
1646 2004"
1647 (if iso8601-string
1648 (when (string-match
1649 (concat
1650 "^ *\\([0-9]\\{4\\}\\)" ;year
1651 "\\(-\\([0-9]\\{2\\}\\)" ;month
1652 "\\(-\\([0-9]\\{2\\}\\)" ;day
1653 "\\(T"
1654 "\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)" ;hour:minute
1655 "\\(:\\([0-9]\\{2\\}\\)\\(\\.[0-9]+\\)?\\)?" ;second
1656 ;timezone
1657 "\\(\\([-+Z]\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)?"
1658 "\\)?\\)?\\)? *$")
1659 iso8601-string)
1660 (let ((year (read (match-string 1 iso8601-string)))
1661 (month (read (or (match-string 3 iso8601-string)
1662 "1")))
1663 (day (read (or (match-string 5 iso8601-string)
1664 "1")))
1665 (hour (read (or (match-string 7 iso8601-string)
1666 "0")))
1667 (minute (read (or (match-string 8 iso8601-string)
1668 "0")))
1669 (second (read (or (match-string 10 iso8601-string)
1670 "0")))
1671 (sign (match-string 13 iso8601-string))
1672 (offset-hour (read (or (match-string 15 iso8601-string)
1673 "0")))
1674 (offset-minute (read (or (match-string 16 iso8601-string)
1675 "0"))))
1676 (cond ((string= sign "+")
1677 (setq hour (- hour offset-hour))
1678 (setq minute (- minute offset-minute)))
1679 ((string= sign "-")
1680 (setq hour (+ hour offset-hour))
1681 (setq minute (+ minute offset-minute))))
1682 ;; if UTC subtract current-time-zone offset
1683 ;;(setq second (+ (car (current-time-zone)) second)))
1685 (condition-case nil
1686 (encode-time second minute hour day month year t)
1687 (error
1688 (message "Cannot decode \"%s\"" iso8601-string)
1689 nil))))
1690 nil))
1692 (defun newsticker--decode-rfc822-date (rfc822-string)
1693 "Return RFC822-STRING in format like `decode-time'.
1694 Converts from RFC822 to Emacs representation.
1695 Examples:
1696 Sat, 07 September 2002 00:00:01 +0100
1697 Sat, 07 September 2002 00:00:01 MET
1698 Sat, 07 Sep 2002 00:00:01 GMT
1699 07 Sep 2002 00:00:01 GMT
1700 07 Sep 2002"
1701 (if (and rfc822-string (stringp rfc822-string))
1702 (when (string-match
1703 (concat
1704 "\\s-*"
1705 ;; week day
1706 "\\(\\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\)\\s-*,?\\)?\\s-*"
1707 ;; day
1708 "\\([0-9]\\{1,2\\}\\)\\s-+"
1709 ;; month
1710 "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|"
1711 "Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\).*?\\s-+"
1712 ;; year
1713 "\\([0-9]\\{2,4\\}\\)"
1714 ;; time may be missing
1715 "\\(\\s-+"
1716 ;; hour
1717 "\\([0-9]\\{2\\}\\)"
1718 ;; minute
1719 ":\\([0-9]\\{2\\}\\)"
1720 ;; second
1721 "\\(:\\([0-9]\\{2\\}\\)\\)?"
1722 ;; zone -- fixme
1723 "\\(\\s-+\\("
1724 "UT\\|GMT\\|EST\\|EDT\\|CST\\|CDT\\|MST\\|MDT\\|PST\\|PDT"
1725 "\\|\\([-+]\\)\\([0-9]\\{2\\}\\)\\([0-9]\\{2\\}\\)"
1726 "\\)\\)?"
1727 "\\)?")
1728 rfc822-string)
1729 (let ((day (read (match-string 3 rfc822-string)))
1730 (month-name (match-string 4 rfc822-string))
1731 (month 0)
1732 (year (read (match-string 5 rfc822-string)))
1733 (hour (read (or (match-string 7 rfc822-string) "0")))
1734 (minute (read (or (match-string 8 rfc822-string) "0")))
1735 (second (read (or (match-string 10 rfc822-string) "0")))
1736 (zone (match-string 12 rfc822-string))
1737 (sign (match-string 13 rfc822-string))
1738 (offset-hour (read (or (match-string 14 rfc822-string)
1739 "0")))
1740 (offset-minute (read (or (match-string 15 rfc822-string)
1741 "0")))
1742 ;;FIXME
1744 (when zone
1745 (cond ((string= sign "+")
1746 (setq hour (- hour offset-hour))
1747 (setq minute (- minute offset-minute)))
1748 ((string= sign "-")
1749 (setq hour (+ hour offset-hour))
1750 (setq minute (+ minute offset-minute)))))
1751 (condition-case error-data
1752 (let ((i 1))
1753 (dolist (m '("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
1754 "Sep" "Oct" "Nov" "Dec"))
1755 (if (string= month-name m)
1756 (setq month i))
1757 (setq i (1+ i)))
1758 (encode-time second minute hour day month year t))
1759 (error
1760 (message "Cannot decode \"%s\": %s %s" rfc822-string
1761 (car error-data) (cdr error-data))
1762 nil))))
1763 nil))
1765 (defun newsticker--lists-intersect-p (list1 list2)
1766 "Return t if LIST1 and LIST2 share elements."
1767 (let ((result nil))
1768 (dolist (elt list1)
1769 (if (memq elt list2)
1770 (setq result t)))
1771 result))
1773 (defun newsticker--update-process-ids ()
1774 "Update list of ids of active newsticker processes.
1775 Checks list of active processes against list of newsticker processes."
1776 (let ((new-list nil))
1777 (dolist (proc (process-list))
1778 (let ((id (process-id proc)))
1779 (if (memq id newsticker--process-ids)
1780 (setq new-list (cons id new-list)))))
1781 (setq newsticker--process-ids new-list))
1782 (force-mode-line-update))
1784 ;; ======================================================================
1785 ;;; Images
1786 ;; ======================================================================
1787 (defun newsticker--images-dir ()
1788 "Return directory where feed images are saved."
1789 (concat newsticker-dir "/images/"))
1791 (defun newsticker--icons-dir ()
1792 "Return directory where feed icons are saved."
1793 (concat newsticker-dir "/icons/"))
1795 (defun newsticker--image-get (feed-name filename directory url)
1796 "Get image for FEED-NAME by returning FILENAME from DIRECTORY.
1797 If the file does no exist or if it is older than 24 hours
1798 download it from URL first."
1799 (let ((image-name (concat directory feed-name)))
1800 (if (and (file-exists-p image-name)
1801 (time-less-p nil
1802 (time-add (nth 5 (file-attributes image-name))
1803 (seconds-to-time 86400))))
1804 (newsticker--debug-msg "%s: Getting image for %s skipped"
1805 (format-time-string "%A, %H:%M")
1806 feed-name)
1807 ;; download
1808 (newsticker--debug-msg "%s: Getting image for %s"
1809 (format-time-string "%A, %H:%M")
1810 feed-name)
1811 (if (eq newsticker-retrieval-method 'intern)
1812 (newsticker--image-download-by-url feed-name filename directory url)
1813 (newsticker--image-download-by-wget feed-name filename directory url)))))
1815 (defun newsticker--image-download-by-wget (feed-name filename directory url)
1816 "Download image for FEED-NAME using external program.
1817 Save image as FILENAME in DIRECTORY, download it from URL."
1818 (let* ((proc-name (concat feed-name "-" filename))
1819 (buffername (concat " *newsticker-wget-image-" proc-name "*"))
1820 (item (or (assoc feed-name newsticker-url-list)
1821 (assoc feed-name newsticker-url-list-defaults)
1822 (error
1823 "Cannot get image for %s: Check newsticker-url-list"
1824 feed-name)))
1825 (wget-arguments (or (car (cdr (cdr (cdr (cdr item)))))
1826 newsticker-wget-arguments)))
1827 (with-current-buffer (get-buffer-create buffername)
1828 (erase-buffer)
1829 ;; throw an error if there is an old wget-process around
1830 (if (get-process feed-name)
1831 (error "Another wget-process is running for image %s"
1832 feed-name))
1833 ;; start wget
1834 (let* ((args (append wget-arguments (list url)))
1835 (proc (apply 'start-process proc-name buffername
1836 newsticker-wget-name args)))
1837 (set-process-coding-system proc 'no-conversion 'no-conversion)
1838 (set-process-sentinel proc 'newsticker--image-sentinel)
1839 (process-put proc 'nt-directory directory)
1840 (process-put proc 'nt-feed-name feed-name)
1841 (process-put proc 'nt-filename filename)))))
1843 (defun newsticker--image-sentinel (process _event)
1844 "Sentinel for image-retrieving PROCESS caused by EVENT."
1845 (let* ((p-status (process-status process))
1846 (exit-status (process-exit-status process))
1847 (feed-name (process-get process 'nt-feed-name))
1848 (directory (process-get process 'nt-directory))
1849 (filename (process-get process 'nt-filename)))
1850 ;; catch known errors (zombie processes, rubbish-xml, etc.)
1851 ;; if an error occurs the news feed is not updated!
1852 (catch 'oops
1853 (unless (and (eq p-status 'exit)
1854 (= exit-status 0))
1855 (message "%s: Error while retrieving image from %s"
1856 (format-time-string "%A, %H:%M")
1857 feed-name)
1858 (newsticker--image-remove directory feed-name)
1859 (throw 'oops nil))
1860 (newsticker--image-save (process-buffer process) directory filename))))
1862 (defun newsticker--image-save (buffer directory file-name)
1863 "Save contents of BUFFER in DIRECTORY as FILE-NAME.
1864 Finally kill buffer."
1865 (with-current-buffer buffer
1866 (let ((image-name (concat directory file-name)))
1867 (set-buffer-file-coding-system 'no-conversion)
1868 ;; make sure the cache dir exists
1869 (unless (file-directory-p directory)
1870 (make-directory directory))
1871 ;; write and close buffer
1872 (let ((require-final-newline nil)
1873 (backup-inhibited t)
1874 (coding-system-for-write 'no-conversion))
1875 (write-region nil nil image-name nil 'quiet))
1876 (set-buffer-modified-p nil)
1877 (kill-buffer buffer))))
1879 (defun newsticker--image-remove (directory file-name)
1880 "In DIRECTORY remove FILE-NAME."
1881 (let ((image-name (concat directory file-name)))
1882 (when (file-exists-p file-name)
1883 (delete-file image-name))))
1885 (defun newsticker--image-download-by-url (feed-name filename directory url)
1886 "Download image for FEED-NAME using `url-retrieve'.
1887 Save image as FILENAME in DIRECTORY, download it from URL."
1888 (let ((coding-system-for-read 'no-conversion))
1889 (condition-case error-data
1890 (url-retrieve url 'newsticker--image-download-by-url-callback
1891 (list feed-name directory filename))
1892 (error (message "Error retrieving image from %s: %s" feed-name
1893 error-data))))
1894 (force-mode-line-update))
1896 (defun newsticker--image-download-by-url-callback (status feed-name directory filename)
1897 "Callback function for `newsticker--image-download-by-url'.
1898 STATUS is the return status as delivered by `url-retrieve'.
1899 FEED-NAME is the name of the feed that the news were retrieved
1900 from.
1901 The image is saved in DIRECTORY as FILENAME."
1902 (let ((do-save
1903 (or (not status)
1904 ;; (let ((status-type (car status)))
1905 ;; (cond ((eq status-type :redirect)
1906 ;; ;; don't care about redirects
1907 ;; t)
1908 ;; ((eq status-type :error)
1909 ;; ;; silently ignore errors
1910 ;; nil)))
1911 (eq (car status) :redirect))))
1912 (when do-save
1913 (let ((buf (get-buffer-create (concat " *newsticker-url-image-" feed-name "-"
1914 directory "*")))
1915 (result (buffer-string)))
1916 (set-buffer buf)
1917 (erase-buffer)
1918 (newsticker--insert-bytes 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 nil)
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 (let ((this-feed (assq feed-name-symbol data)))
2113 (if this-feed
2114 (setcdr this-feed (nconc (cdr this-feed) (list item)))
2115 ;; The feed is not contained.
2116 (setq data (append data (list (list feed-name-symbol item)))))))
2117 data))
2119 (defun newsticker--cache-remove (data feed-symbol age)
2120 "Remove all entries from DATA in the feed FEED-SYMBOL with AGE.
2121 FEED-SYMBOL may be `any'. Entries from old feeds, which are no longer in
2122 `newsticker-url-list' or `newsticker-url-list-defaults', are removed as
2123 well."
2124 (let* ((pos data)
2125 (feed (car pos))
2126 (last-pos nil))
2127 (while feed
2128 (if (or (assoc (symbol-name (car feed)) newsticker-url-list)
2129 (assoc (symbol-name (car feed)) newsticker-url-list-defaults))
2130 ;; feed is still valid=active
2131 ;; (message "Keeping feed %s" (car feed))
2132 (if (or (eq feed-symbol 'any)
2133 (eq feed-symbol (car feed)))
2134 (let* ((item-pos (cdr feed))
2135 (item (car item-pos))
2136 (prev-pos nil))
2137 (while item
2138 ;;(message "%s" (car item))
2139 (if (eq age (newsticker--age item))
2140 ;; remove this item
2141 (progn
2142 ;;(message "Removing item %s" (car item))
2143 (if prev-pos
2144 (setcdr prev-pos (cdr item-pos))
2145 (setcdr feed (cdr item-pos))))
2146 ;;(message "Keeping item %s" (car item))
2147 (setq prev-pos item-pos))
2148 (setq item-pos (cdr item-pos))
2149 (setq item (car item-pos)))))
2150 ;; feed is not active anymore
2151 ;; (message "Removing feed %s" (car feed))
2152 (if last-pos
2153 (setcdr last-pos (cdr pos))
2154 (setq data (cdr pos))))
2155 (setq last-pos pos)
2156 (setq pos (cdr pos))
2157 (setq feed (car pos)))))
2159 ;; ======================================================================
2160 ;;; Sorting
2161 ;; ======================================================================
2162 (defun newsticker--cache-item-compare-by-time (item1 item2)
2163 "Compare two news items ITEM1 and ITEM2 by comparing their time values."
2164 (catch 'result
2165 (let ((age1 (newsticker--age item1))
2166 (age2 (newsticker--age item2)))
2167 (if (not (eq age1 age2))
2168 (cond ((eq age1 'obsolete)
2169 (throw 'result nil))
2170 ((eq age2 'obsolete)
2171 (throw 'result t)))))
2172 (let* ((time1 (newsticker--time item1))
2173 (time2 (newsticker--time item2)))
2174 (cond ((< (nth 0 time1) (nth 0 time2))
2175 nil)
2176 ((> (nth 0 time1) (nth 0 time2))
2178 ((< (nth 1 time1) (nth 1 time2))
2179 nil)
2180 ((> (nth 1 time1) (nth 1 time2))
2182 ((< (or (nth 2 time1) 0) (or (nth 2 time2) 0))
2183 nil)
2184 ((> (or (nth 2 time1) 0) (or (nth 2 time2) 0))
2187 nil)))))
2189 (defun newsticker--cache-item-compare-by-title (item1 item2)
2190 "Compare ITEM1 and ITEM2 by comparing their titles."
2191 (catch 'result
2192 (let ((age1 (newsticker--age item1))
2193 (age2 (newsticker--age item2)))
2194 (if (not (eq age1 age2))
2195 (cond ((eq age1 'obsolete)
2196 (throw 'result nil))
2197 ((eq age2 'obsolete)
2198 (throw 'result t)))))
2199 (string< (newsticker--title item1) (newsticker--title item2))))
2201 (defun newsticker--cache-item-compare-by-position (item1 item2)
2202 "Compare ITEM1 and ITEM2 by comparing their original positions."
2203 (catch 'result
2204 (let ((age1 (newsticker--age item1))
2205 (age2 (newsticker--age item2)))
2206 (if (not (eq age1 age2))
2207 (cond ((eq age1 'obsolete)
2208 (throw 'result nil))
2209 ((eq age2 'obsolete)
2210 (throw 'result t)))))
2211 (< (or (newsticker--pos item1) 0) (or (newsticker--pos item2) 0))))
2213 (defun newsticker--cache-save-version1 ()
2214 "Update and save newsticker cache file."
2215 (interactive)
2216 (newsticker--cache-update t))
2218 (defun newsticker--cache-update (&optional save)
2219 "Update newsticker cache file.
2220 If optional argument SAVE is not nil the cache file is saved to disk."
2221 (save-excursion
2222 (unless (file-directory-p newsticker-dir)
2223 (make-directory newsticker-dir t))
2224 (let ((coding-system-for-write 'utf-8)
2225 (buf (find-file-noselect newsticker-cache-filename)))
2226 (when buf
2227 (set-buffer buf)
2228 (setq buffer-undo-list t)
2229 (erase-buffer)
2230 (insert ";; -*- coding: utf-8 -*-\n")
2231 (insert (prin1-to-string newsticker--cache))
2232 (when save
2233 (save-buffer))))))
2235 (defun newsticker--cache-get-feed (feed)
2236 "Return the cached data for the feed FEED.
2237 FEED is a symbol!"
2238 (assoc feed newsticker--cache))
2240 (defun newsticker--cache-dir ()
2241 "Return directory for saving cache data."
2242 (concat newsticker-dir "/feeds"))
2244 (defun newsticker--cache-save ()
2245 "Save cache data for all feeds."
2246 (unless (file-directory-p newsticker-dir)
2247 (make-directory newsticker-dir t))
2248 (mapc 'newsticker--cache-save-feed newsticker--cache)
2249 nil)
2251 (defun newsticker--cache-save-feed (feed)
2252 "Save cache data for FEED."
2253 (let ((dir (concat (newsticker--cache-dir) "/" (symbol-name (car feed)))))
2254 (unless (file-directory-p dir)
2255 (make-directory dir t))
2256 (let ((coding-system-for-write 'utf-8))
2257 (with-temp-file (concat dir "/data")
2258 (insert ";; -*- coding: utf-8 -*-\n")
2259 (insert (prin1-to-string (cdr feed)))))))
2261 (defun newsticker--cache-read-version1 ()
2262 "Read version1 cache data."
2263 (let ((coding-system-for-read 'utf-8))
2264 (when (file-exists-p newsticker-cache-filename)
2265 (with-temp-buffer
2266 (insert-file-contents newsticker-cache-filename)
2267 (goto-char (point-min))
2268 (condition-case nil
2269 (setq newsticker--cache (read (current-buffer)))
2270 (error
2271 (message "Error while reading newsticker cache file!")
2272 (setq newsticker--cache nil)))))))
2274 (defun newsticker--cache-read ()
2275 "Read cache data."
2276 (setq newsticker--cache nil)
2277 (if (file-exists-p newsticker-cache-filename)
2278 (progn
2279 (when (y-or-n-p "Old newsticker cache file exists. Read it? ")
2280 (newsticker--cache-read-version1))
2281 (when (y-or-n-p (format "Delete old newsticker cache file? "))
2282 (delete-file newsticker-cache-filename)))
2283 (dolist (f (append newsticker-url-list-defaults newsticker-url-list))
2284 (newsticker--cache-read-feed (car f)))))
2286 (defun newsticker--cache-read-feed (feed-name)
2287 "Read cache data for feed named FEED-NAME."
2288 (let ((file-name (concat (newsticker--cache-dir) "/" feed-name "/data"))
2289 (coding-system-for-read 'utf-8))
2290 (when (file-exists-p file-name)
2291 (with-temp-buffer
2292 (insert-file-contents file-name)
2293 (goto-char (point-min))
2294 (condition-case nil
2295 (add-to-list 'newsticker--cache (cons (intern feed-name)
2296 (read (current-buffer))))
2297 (error
2298 (message "Error while reading newsticker cache file %s!"
2299 file-name)
2300 (setq newsticker--cache nil)))))))
2302 ;; ======================================================================
2303 ;;; Statistics
2304 ;; ======================================================================
2305 (defun newsticker--stat-num-items (feed &rest ages)
2306 "Return number of items in the given FEED which have one of the given AGES.
2307 If AGES is nil, the total number of items is returned."
2308 (let ((items (cdr (newsticker--cache-get-feed feed)))
2309 (num 0))
2310 (while items
2311 (if ages
2312 (if (memq (newsticker--age (car items)) ages)
2313 (setq num (1+ num)))
2314 (if (memq (newsticker--age (car items)) '(new old immortal obsolete))
2315 (setq num (1+ num))))
2316 (setq items (cdr items)))
2317 num))
2319 (defun newsticker--stat-num-items-total (&optional age)
2320 "Return total number of items in all feeds which have the given AGE.
2321 If AGE is nil, the total number of items is returned."
2322 (apply '+
2323 (mapcar (lambda (feed)
2324 (if age
2325 (newsticker--stat-num-items (intern (car feed)) age)
2326 (newsticker--stat-num-items (intern (car feed)))))
2327 (append newsticker-url-list-defaults newsticker-url-list))))
2329 ;; ======================================================================
2330 ;;; OPML
2331 ;; ======================================================================
2332 (defun newsticker-opml-export ()
2333 "OPML subscription export.
2334 Export subscriptions to a buffer in OPML Format."
2335 ;; FIXME: use newsticker-groups
2336 (interactive)
2337 (with-current-buffer (get-buffer-create "*OPML Export*")
2338 (set-buffer-file-coding-system 'utf-8)
2339 (insert (concat
2340 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
2341 "<!-- OPML generated by Emacs newsticker.el -->\n"
2342 "<opml version=\"1.0\">\n"
2343 " <head>\n"
2344 " <title>mySubscriptions</title>\n"
2345 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
2346 "</dateCreated>\n"
2347 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
2348 " <ownerName>" (user-full-name) "</ownerName>\n"
2349 " </head>\n"
2350 " <body>\n"))
2351 (dolist (sub (append newsticker-url-list newsticker-url-list-defaults))
2352 (insert " <outline text=\"")
2353 (insert (newsticker--title sub))
2354 (insert "\" xmlUrl=\"")
2355 (insert (xml-escape-string (let ((url (cadr sub)))
2356 (if (stringp url) url (prin1-to-string url)))))
2357 (insert "\"/>\n"))
2358 (insert " </body>\n</opml>\n"))
2359 (pop-to-buffer "*OPML Export*")
2360 (when (fboundp 'sgml-mode)
2361 (sgml-mode)))
2363 (defun newsticker--opml-import-outlines (outlines)
2364 "Recursively import OUTLINES from OPML data.
2365 Note that nested outlines are currently flattened -- i.e. grouping is
2366 removed."
2367 (mapc (lambda (outline)
2368 (let ((name (xml-get-attribute outline 'text))
2369 (url (xml-get-attribute outline 'xmlUrl))
2370 (children (xml-get-children outline 'outline)))
2371 (unless (string= "" url)
2372 (add-to-list 'newsticker-url-list
2373 (list name url nil nil nil) t))
2374 (if children
2375 (newsticker--opml-import-outlines children))))
2376 outlines))
2378 (defun newsticker-opml-import (filename)
2379 "Import OPML data from FILENAME."
2380 (interactive "fOPML file: ")
2381 (set-buffer (find-file-noselect filename))
2382 (goto-char (point-min))
2383 (let* ((node-list (xml-parse-region (point-min) (point-max)))
2384 (body (car (xml-get-children (car node-list) 'body)))
2385 (outlines (xml-get-children body 'outline)))
2386 (newsticker--opml-import-outlines outlines))
2387 (customize-variable 'newsticker-url-list))
2389 ;; ======================================================================
2390 ;;; Auto marking
2391 ;; ======================================================================
2392 (defun newsticker--run-auto-mark-filter (feed item)
2393 "Automatically mark an item as old or immortal.
2394 This function checks the variable `newsticker-auto-mark-filter-list'
2395 for an entry that matches FEED and ITEM."
2396 (let ((case-fold-search t))
2397 (dolist (filter newsticker-auto-mark-filter-list)
2398 (let ((filter-feed (car filter))
2399 (pattern-list (cadr filter)))
2400 (when (string-match filter-feed feed)
2401 (newsticker--do-run-auto-mark-filter item pattern-list))))))
2403 (defun newsticker--do-run-auto-mark-filter (item list)
2404 "Actually compare ITEM against the pattern-LIST.
2405 LIST must be an element of `newsticker-auto-mark-filter-list'."
2406 (dolist (pattern list)
2407 (let ((place (nth 1 pattern))
2408 (regexp (nth 2 pattern))
2409 (title (newsticker--title item))
2410 (desc (newsticker--desc item)))
2411 (when (or (eq place 'title) (eq place 'all))
2412 (when (and title (string-match regexp title))
2413 (newsticker--process-auto-mark-filter-match item pattern)))
2414 (when (or (eq place 'description) (eq place 'all))
2415 (when (and desc (string-match regexp desc))
2416 (newsticker--process-auto-mark-filter-match item pattern))))))
2418 (defun newsticker--process-auto-mark-filter-match (item pattern)
2419 "Process ITEM that matches an auto-mark-filter PATTERN."
2420 (let ((age (nth 0 pattern))
2421 (place (nth 1 pattern))
2422 (regexp (nth 2 pattern)))
2423 (newsticker--debug-msg "Auto-mark-filter %s matches `%s'"
2424 pattern (newsticker--title item))
2425 (setcar (nthcdr 4 item) age)
2426 (nconc (newsticker--extra item)
2427 (list (list 'newsticker-auto-mark nil
2428 (format "age=%s, title/desc=%s, regexp=%s"
2429 age place regexp))))))
2431 ;; ======================================================================
2432 ;;; Hook samples
2433 ;; ======================================================================
2434 (defun newsticker-new-item-functions-sample (feedname item)
2435 "Demonstrate the use of the `newsticker-new-item-functions' hook.
2436 This function just prints out the values of the FEEDNAME and title of the ITEM."
2437 (message (concat "newsticker-new-item-functions-sample: feed=`%s', "
2438 "title=`%s'")
2439 feedname (newsticker--title item)))
2441 (defun newsticker-download-images (feedname item)
2442 "Download the first image.
2443 If FEEDNAME equals \"imagefeed\" download the first image URL
2444 found in the description=contents of ITEM to the directory
2445 \"~/tmp/newsticker/FEEDNAME/TITLE\" where TITLE is the title of
2446 the item."
2447 (when (string= feedname "imagefeed")
2448 (let ((title (newsticker--title item))
2449 (desc (newsticker--desc item)))
2450 (when (string-match "<img src=\"\\(http://[^ \"]+\\)\"" desc)
2451 (let ((url (substring desc (match-beginning 1) (match-end 1)))
2452 (temp-dir (concat "~/tmp/newsticker/" feedname "/" title))
2453 (org-dir default-directory))
2454 (unless (file-directory-p temp-dir)
2455 (make-directory temp-dir t))
2456 (cd temp-dir)
2457 (message "Getting image %s" url)
2458 (apply 'start-process "wget-image"
2459 " *newsticker-wget-download-images*"
2460 newsticker-wget-name
2461 (list url))
2462 (cd org-dir))))))
2464 (defun newsticker-download-enclosures (feedname item)
2465 "In all feeds download the enclosed object of the news ITEM.
2466 The object is saved to the directory \"~/tmp/newsticker/FEEDNAME/TITLE\", which
2467 is created if it does not exist. TITLE is the title of the news
2468 item. Argument FEEDNAME is ignored.
2469 This function is suited for adding it to `newsticker-new-item-functions'."
2470 (let ((title (newsticker--title item))
2471 (enclosure (newsticker--enclosure item)))
2472 (when enclosure
2473 (let ((url (cdr (assoc 'url enclosure)))
2474 (temp-dir (concat "~/tmp/newsticker/" feedname "/" title))
2475 (org-dir default-directory))
2476 (unless (file-directory-p temp-dir)
2477 (make-directory temp-dir t))
2478 (cd temp-dir)
2479 (message "Getting enclosure %s" url)
2480 (apply 'start-process "wget-enclosure"
2481 " *newsticker-wget-download-enclosures*"
2482 newsticker-wget-name
2483 (list url))
2484 (cd org-dir)))))
2486 ;; ======================================================================
2487 ;;; Retrieve samples
2488 ;; ======================================================================
2489 (defun newsticker-retrieve-random-message (_feed-name)
2490 "Return an artificial RSS string under the name FEED-NAME."
2491 (concat "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?><rss version=\"0.91\">"
2492 "<channel>"
2493 "<title>newsticker-retrieve-random-message</title>"
2494 "<description>Sample retrieval function</description>"
2495 "<pubDate>FIXME Sat, 07 Sep 2005 00:00:01 GMT</pubDate>"
2496 "<item><title>" (format "Your lucky number is %d" (random 10000))
2497 "</title><description>" (format "Or maybe it is %d" (random 10000))
2498 "</description></item></channel></rss>"))
2500 (provide 'newst-backend)
2502 ;;; newst-backend.el ends here