1 ;;; nndiary.el --- A diary back end for Gnus
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Didier Verna <didier@xemacs.org>
7 ;; Maintainer: Didier Verna <didier@xemacs.org>
8 ;; Created: Fri Jul 16 18:55:42 1999
9 ;; Keywords: calendar mail news
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
31 ;; Contents management by FCM version 0.1.
36 ;; nndiary is a mail back end designed to handle mails as diary event
37 ;; reminders. It is now fully documented in the Gnus manual.
43 ;; * Respooling doesn't work because contrary to the request-scan function,
44 ;; Gnus won't allow me to override the split methods when calling the
45 ;; respooling back end functions.
46 ;; * There's a bug in the time zone mechanism with variable TZ locations.
47 ;; * We could allow a keyword like `ask' in X-Diary-* headers, that would mean
48 ;; "ask for value upon reception of the message".
49 ;; * We could add an optional header X-Diary-Reminders to specify a special
50 ;; reminders value for this message. Suggested by Jody Klymak.
51 ;; * We should check messages validity in other circumstances than just
52 ;; moving an article from somewhere else (request-accept). For instance,
53 ;; when editing / saving and so on.
59 ;; * nnoo. NNDiary is very similar to nnml. This makes the idea of using nnoo
60 ;; (to derive nndiary from nnml) natural. However, my experience with nnoo
61 ;; is that for reasonably complex back ends like this one, noo is a burden
62 ;; rather than an help. It's tricky to use, not everything can be inherited,
63 ;; what can be inherited and when is not very clear, and you've got to be
64 ;; very careful because a little mistake can fuck up your other back ends,
65 ;; especially because their variables will be use instead of your real ones.
66 ;; Finally, I found it easier to just clone the needed parts of nnml, and
67 ;; tracking nnml updates is not a big deal.
69 ;; IMHO, nnoo is actually badly designed. A much simpler, and yet more
70 ;; powerful one would be to make *real* functions and variables for a new
71 ;; back end based on another. Lisp is a reflexive language so that's a very
72 ;; easy thing to do: inspect the function's form, replace occurences of
73 ;; <nnfrom> (even in strings) with <nnto>, and you're done.
75 ;; * nndiary-get-new-mail, nndiary-mail-source and nndiary-split-methods:
76 ;; NNDiary has some experimental parts, in the sense Gnus normally uses only
77 ;; one mail back ends for mail retreival and splitting. This back end is
78 ;; also an attempt to make it behave differently. For Gnus developpers: as
79 ;; you can see if you snarf into the code, that was not a very difficult
80 ;; thing to do. Something should be done about the respooling breakage
89 (eval-when-compile (require 'cl
))
94 ;; Compatibility Functions =================================================
97 (if (fboundp 'signal-error
)
98 (defun nndiary-error (&rest args
)
99 (apply #'signal-error
'nndiary args
))
100 (defun nndiary-error (&rest args
)
101 (apply #'error args
))))
104 ;; Back End behavior customization ===========================================
106 (defgroup nndiary nil
107 "The Gnus Diary back end."
111 (defcustom nndiary-mail-sources
112 `((file :path
,(expand-file-name "~/.nndiary")))
113 "*NNDiary specific mail sources.
114 This variable is used by nndiary in place of the standard `mail-sources'
115 variable when `nndiary-get-new-mail' is set to non-nil. These sources
116 must contain diary messages ONLY."
121 (defcustom nndiary-split-methods
'(("diary" ""))
122 "*NNDiary specific split methods.
123 This variable is used by nndiary in place of the standard
124 `nnmail-split-methods' variable when `nndiary-get-new-mail' is set to
128 :type
'(choice (repeat :tag
"Alist" (group (string :tag
"Name") regexp
))
129 (function-item nnmail-split-fancy
)
130 (function :tag
"Other")))
133 (defcustom nndiary-reminders
'((0 . day
))
134 "*Different times when you want to be reminded of your appointments.
135 Diary articles will appear again, as if they'd been just received.
137 Entries look like (3 . day) which means something like \"Please
138 Hortense, would you be so kind as to remind me of my appointments 3 days
139 before the date, thank you very much. Anda, hmmm... by the way, are you
140 doing anything special tonight ?\".
142 The units of measure are 'minute 'hour 'day 'week 'month and 'year (no,
143 not 'century, sorry).
145 NOTE: the units of measure actually express dates, not durations: if you
146 use 'week, messages will pop up on Sundays at 00:00 (or Mondays if
147 `nndiary-week-starts-on-monday' is non-nil) and *not* 7 days before the
148 appointment, if you use 'month, messages will pop up on the first day of
149 each months, at 00:00 and so on.
151 If you really want to specify a duration (like 24 hours exactly), you can
152 use the equivalent in minutes (the smallest unit). A fuzz of 60 seconds
153 maximum in the reminder is not that painful, I think. Although this
154 scheme might appear somewhat weird at a first glance, it is very powerful.
155 In order to make this clear, here are some examples:
157 - '(0 . day): this is the default value of `nndiary-reminders'. It means
158 pop up the appointments of the day each morning at 00:00.
160 - '(1 . day): this means pop up the appointments the day before, at 00:00.
162 - '(6 . hour): for an appointment at 18:30, this would pop up the
163 appointment message at 12:00.
165 - '(360 . minute): for an appointment at 18:30 and 15 seconds, this would
166 pop up the appointment message at 12:30."
168 :type
'(repeat (cons :format
"%v\n"
169 (integer :format
"%v")
170 (choice :format
"%[%v(s)%] before...\n"
172 (const :format
"%v" minute
)
173 (const :format
"%v" hour
)
174 (const :format
"%v" day
)
175 (const :format
"%v" week
)
176 (const :format
"%v" month
)
177 (const :format
"%v" year
)))))
179 (defcustom nndiary-week-starts-on-monday nil
180 "*Whether a week starts on monday (otherwise, sunday)."
185 (defcustom nndiary-request-create-group-hooks nil
186 "*Hooks to run after `nndiary-request-create-group' is executed.
187 The hooks will be called with the full group name as argument."
191 (defcustom nndiary-request-update-info-hooks nil
192 "*Hooks to run after `nndiary-request-update-info-group' is executed.
193 The hooks will be called with the full group name as argument."
197 (defcustom nndiary-request-accept-article-hooks nil
198 "*Hooks to run before accepting an article.
199 Executed near the beginning of `nndiary-request-accept-article'.
200 The hooks will be called with the article in the current buffer."
204 (defcustom nndiary-check-directory-twice t
205 "*If t, check directories twice to avoid NFS failures."
210 ;; Back End declaration ======================================================
212 ;; Well, most of this is nnml clonage.
214 (nnoo-declare nndiary
)
216 (defvoo nndiary-directory
(nnheader-concat gnus-directory
"diary/")
217 "Spool directory for the nndiary back end.")
219 (defvoo nndiary-active-file
220 (expand-file-name "active" nndiary-directory
)
221 "Active file for the nndiary back end.")
223 (defvoo nndiary-newsgroups-file
224 (expand-file-name "newsgroups" nndiary-directory
)
225 "Newsgroups description file for the nndiary back end.")
227 (defvoo nndiary-get-new-mail nil
228 "Whether nndiary gets new mail and split it.
229 Contrary to traditional mail back ends, this variable can be set to t
230 even if your primary mail back end also retreives mail. In such a case,
231 NDiary uses its own mail-sources and split-methods.")
233 (defvoo nndiary-nov-is-evil nil
234 "If non-nil, Gnus will never use nov databases for nndiary groups.
235 Using nov databases will speed up header fetching considerably.
236 This variable shouldn't be flipped much. If you have, for some reason,
237 set this to t, and want to set it to nil again, you should always run
238 the `nndiary-generate-nov-databases' command. The function will go
239 through all nnml directories and generate nov databases for them
240 all. This may very well take some time.")
242 (defvoo nndiary-prepare-save-mail-hook nil
243 "*Hook run narrowed to an article before saving.")
245 (defvoo nndiary-inhibit-expiry nil
246 "If non-nil, inhibit expiry.")
250 (defconst nndiary-version
"0.2-b14"
251 "Current Diary back end version.")
253 (defun nndiary-version ()
254 "Current Diary back end version."
256 (message "NNDiary version %s" nndiary-version
))
258 (defvoo nndiary-nov-file-name
".overview")
260 (defvoo nndiary-current-directory nil
)
261 (defvoo nndiary-current-group nil
)
262 (defvoo nndiary-status-string
"" )
263 (defvoo nndiary-nov-buffer-alist nil
)
264 (defvoo nndiary-group-alist nil
)
265 (defvoo nndiary-active-timestamp nil
)
266 (defvoo nndiary-article-file-alist nil
)
268 (defvoo nndiary-generate-active-function
'nndiary-generate-active-info
)
269 (defvoo nndiary-nov-buffer-file-name nil
)
270 (defvoo nndiary-file-coding-system nnmail-file-coding-system
)
272 (defconst nndiary-headers
279 ("Time-Zone" (("Y" -
43200)
356 ;; List of NNDiary headers that specify the time spec. Each header name is
357 ;; followed by either two integers (specifying a range of possible values
358 ;; for this header) or one list (specifying all the possible values for this
359 ;; header). In the latter case, the list does NOT include the unspecifyed
361 ;; For time zone values, we have symbolic time zone names associated with
362 ;; the (relative) number of seconds ahead GMT.
365 (defsubst nndiary-schedule
()
370 (setq head
(nth 0 elt
))
371 (nndiary-parse-schedule (nth 0 elt
) (nth 1 elt
) (nth 2 elt
)))
374 (nnheader-report 'nndiary
"X-Diary-%s header parse error: %s."
379 ;;; Interface functions =====================================================
381 (nnoo-define-basics nndiary
)
383 (deffoo nndiary-retrieve-headers
(sequence &optional group server fetch-old
)
384 (when (nndiary-possibly-change-directory group server
)
386 (set-buffer nntp-server-buffer
)
389 (number (length sequence
))
391 (file-name-coding-system nnmail-pathname-coding-system
)
393 (nndiary-check-directory-twice
394 (and nndiary-check-directory-twice
395 ;; To speed up, disable it in some case.
396 (or (not (numberp nnmail-large-newsgroup
))
397 (<= number nnmail-large-newsgroup
)))))
398 (if (stringp (car sequence
))
400 (if (nndiary-retrieve-headers-with-nov sequence fetch-old
)
403 (setq article
(car sequence
))
404 (setq file
(nndiary-article-to-file article
))
407 (not (file-directory-p file
)))
408 (insert (format "221 %d Article retrieved.\n" article
))
410 (nnheader-insert-head file
)
412 (if (search-forward "\n\n" nil t
)
414 (goto-char (point-max))
417 (delete-region (point) (point-max)))
418 (setq sequence
(cdr sequence
))
419 (setq count
(1+ count
))
420 (and (numberp nnmail-large-newsgroup
)
421 (> number nnmail-large-newsgroup
)
423 (nnheader-message 6 "nndiary: Receiving headers... %d%%"
424 (/ (* count
100) number
))))
426 (and (numberp nnmail-large-newsgroup
)
427 (> number nnmail-large-newsgroup
)
428 (nnheader-message 6 "nndiary: Receiving headers...done"))
430 (nnheader-fold-continuation-lines)
433 (deffoo nndiary-open-server
(server &optional defs
)
434 (nnoo-change-server 'nndiary server defs
)
435 (when (not (file-exists-p nndiary-directory
))
436 (ignore-errors (make-directory nndiary-directory t
)))
438 ((not (file-exists-p nndiary-directory
))
439 (nndiary-close-server)
440 (nnheader-report 'nndiary
"Couldn't create directory: %s"
442 ((not (file-directory-p (file-truename nndiary-directory
)))
443 (nndiary-close-server)
444 (nnheader-report 'nndiary
"Not a directory: %s" nndiary-directory
))
446 (nnheader-report 'nndiary
"Opened server %s using directory %s"
447 server nndiary-directory
)
450 (deffoo nndiary-request-regenerate
(server)
451 (nndiary-possibly-change-directory nil server
)
452 (nndiary-generate-nov-databases server
)
455 (deffoo nndiary-request-article
(id &optional group server buffer
)
456 (nndiary-possibly-change-directory group server
)
457 (let* ((nntp-server-buffer (or buffer nntp-server-buffer
))
458 (file-name-coding-system nnmail-pathname-coding-system
)
459 path gpath group-num
)
461 (when (and (setq group-num
(nndiary-find-group-number id
))
463 (assq (cdr group-num
)
464 (nnheader-article-to-file-alist
466 (nnmail-group-pathname
468 nndiary-directory
))))))
469 (setq path
(concat gpath
(int-to-string (cdr group-num
)))))
470 (setq path
(nndiary-article-to-file id
)))
473 (nnheader-report 'nndiary
"No such article: %s" id
))
474 ((not (file-exists-p path
))
475 (nnheader-report 'nndiary
"No such file: %s" path
))
476 ((file-directory-p path
)
477 (nnheader-report 'nndiary
"File is a directory: %s" path
))
478 ((not (save-excursion (let ((nnmail-file-coding-system
479 nndiary-file-coding-system
))
480 (nnmail-find-file path
))))
481 (nnheader-report 'nndiary
"Couldn't read file: %s" path
))
483 (nnheader-report 'nndiary
"Article %s retrieved" id
)
484 ;; We return the article number.
485 (cons (if group-num
(car group-num
) group
)
486 (string-to-number (file-name-nondirectory path
)))))))
488 (deffoo nndiary-request-group
(group &optional server dont-check
)
489 (let ((file-name-coding-system nnmail-pathname-coding-system
))
491 ((not (nndiary-possibly-change-directory group server
))
492 (nnheader-report 'nndiary
"Invalid group (no such directory)"))
493 ((not (file-exists-p nndiary-current-directory
))
494 (nnheader-report 'nndiary
"Directory %s does not exist"
495 nndiary-current-directory
))
496 ((not (file-directory-p nndiary-current-directory
))
497 (nnheader-report 'nndiary
"%s is not a directory"
498 nndiary-current-directory
))
500 (nnheader-report 'nndiary
"Group %s selected" group
)
503 (nnheader-re-read-dir nndiary-current-directory
)
504 (nnmail-activate 'nndiary
)
505 (let ((active (nth 1 (assoc group nndiary-group-alist
))))
507 (nnheader-report 'nndiary
"No such group: %s" group
)
508 (nnheader-report 'nndiary
"Selected group %s" group
)
509 (nnheader-insert "211 %d %d %d %s\n"
510 (max (1+ (- (cdr active
) (car active
))) 0)
511 (car active
) (cdr active
) group
)))))))
513 (deffoo nndiary-request-scan
(&optional group server
)
514 ;; Use our own mail sources and split methods while Gnus doesn't let us have
515 ;; multiple back ends for retrieving mail.
516 (let ((mail-sources nndiary-mail-sources
)
517 (nnmail-split-methods nndiary-split-methods
))
518 (setq nndiary-article-file-alist nil
)
519 (nndiary-possibly-change-directory group server
)
520 (nnmail-get-new-mail 'nndiary
'nndiary-save-nov nndiary-directory group
)))
522 (deffoo nndiary-close-group
(group &optional server
)
523 (setq nndiary-article-file-alist nil
)
526 (deffoo nndiary-request-create-group
(group &optional server args
)
527 (nndiary-possibly-change-directory nil server
)
528 (nnmail-activate 'nndiary
)
530 ((assoc group nndiary-group-alist
)
532 ((and (file-exists-p (nnmail-group-pathname group nndiary-directory
))
533 (not (file-directory-p (nnmail-group-pathname
534 group nndiary-directory
))))
535 (nnheader-report 'nndiary
"%s is a file"
536 (nnmail-group-pathname group nndiary-directory
)))
539 (push (list group
(setq active
(cons 1 0)))
541 (nndiary-possibly-create-directory group
)
542 (nndiary-possibly-change-directory group server
)
543 (let ((articles (nnheader-directory-articles nndiary-current-directory
)))
545 (setcar active
(apply 'min articles
))
546 (setcdr active
(apply 'max articles
))))
547 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
548 (run-hook-with-args 'nndiary-request-create-group-hooks
549 (gnus-group-prefixed-name group
550 (list "nndiary" server
)))
554 (deffoo nndiary-request-list
(&optional server
)
556 (let ((nnmail-file-coding-system nnmail-active-file-coding-system
)
557 (file-name-coding-system nnmail-pathname-coding-system
))
558 (nnmail-find-file nndiary-active-file
))
559 (setq nndiary-group-alist
(nnmail-get-active))
562 (deffoo nndiary-request-newgroups
(date &optional server
)
563 (nndiary-request-list server
))
565 (deffoo nndiary-request-list-newsgroups
(&optional server
)
567 (nnmail-find-file nndiary-newsgroups-file
)))
569 (deffoo nndiary-request-expire-articles
(articles group
&optional server force
)
570 (nndiary-possibly-change-directory group server
)
571 (let ((active-articles
572 (nnheader-directory-articles nndiary-current-directory
))
574 (nnmail-activate 'nndiary
)
575 ;; Articles not listed in active-articles are already gone,
576 ;; so don't try to expire them.
577 (setq articles
(gnus-intersection articles active-articles
))
579 (setq article
(nndiary-article-to-file (setq number
(pop articles
))))
580 (if (and (nndiary-deletable-article-p group number
)
581 ;; Don't use nnmail-expired-article-p. Our notion of expiration
582 ;; is a bit peculiar ...
583 (or force
(nndiary-expired-article-p article
)))
585 ;; Allow a special target group.
586 (unless (eq nnmail-expiry-target
'delete
)
588 (nndiary-request-article number group server
(current-buffer))
589 (let ((nndiary-current-directory nil
))
590 (nnmail-expiry-target-group nnmail-expiry-target group
)))
591 (nndiary-possibly-change-directory group server
))
592 (nnheader-message 5 "Deleting article %s in %s" number group
)
594 (funcall nnmail-delete-file-function article
)
595 (file-error (push number rest
)))
596 (setq active-articles
(delq number active-articles
))
597 (nndiary-nov-delete-article group number
))
599 (let ((active (nth 1 (assoc group nndiary-group-alist
))))
601 (setcar active
(or (and active-articles
602 (apply 'min active-articles
))
604 (nnmail-save-active nndiary-group-alist nndiary-active-file
))
606 (nconc rest articles
)))
608 (deffoo nndiary-request-move-article
609 (article group server accept-form
&optional last move-is-internal
)
610 (let ((buf (get-buffer-create " *nndiary move*"))
612 (nndiary-possibly-change-directory group server
)
613 (nndiary-update-file-alist)
615 (nndiary-deletable-article-p group article
)
616 (nndiary-request-article article group server
)
617 (let (nndiary-current-directory
618 nndiary-current-group
619 nndiary-article-file-alist
)
622 (insert-buffer-substring nntp-server-buffer
)
623 (setq result
(eval accept-form
))
624 (kill-buffer (current-buffer))
627 (nndiary-possibly-change-directory group server
)
629 (funcall nnmail-delete-file-function
630 (nndiary-article-to-file article
))
632 (nndiary-nov-delete-article group article
)
635 (nnmail-save-active nndiary-group-alist nndiary-active-file
))))
638 (deffoo nndiary-request-accept-article
(group &optional server last
)
639 (nndiary-possibly-change-directory group server
)
640 (nnmail-check-syntax)
641 (run-hooks 'nndiary-request-accept-article-hooks
)
642 (when (nndiary-schedule)
644 (when nnmail-cache-accepted-message-ids
645 (nnmail-cache-insert (nnmail-fetch-field "message-id")
647 (nnmail-fetch-field "subject")))
650 (nnmail-activate 'nndiary
)
652 (car (nndiary-save-mail
653 (list (cons group
(nndiary-active-number group
))))))
655 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
656 (and last
(nndiary-save-nov))))
658 (nnmail-activate 'nndiary
)
659 (if (and (not (setq result
660 (nnmail-article-group 'nndiary-active-number
)))
661 (yes-or-no-p "Moved to `junk' group; delete article? "))
663 (setq result
(car (nndiary-save-mail result
))))
665 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
666 (when nnmail-cache-accepted-message-ids
667 (nnmail-cache-close))
668 (nndiary-save-nov))))
672 (deffoo nndiary-request-post
(&optional server
)
673 (nnmail-do-request-post 'nndiary-request-accept-article server
))
675 (deffoo nndiary-request-replace-article
(article group buffer
)
676 (nndiary-possibly-change-directory group
)
679 (nndiary-possibly-create-directory group
)
680 (let ((chars (nnmail-insert-lines))
681 (art (concat (int-to-string article
) "\t"))
685 (point-min) (point-max)
686 (or (nndiary-article-to-file article
)
687 (expand-file-name (int-to-string article
)
688 nndiary-current-directory
))
689 nil
(if (nnheader-be-verbose 5) nil
'nomesg
))
691 (setq headers
(nndiary-parse-head chars article
))
692 ;; Replace the NOV line in the NOV file.
694 (set-buffer (nndiary-open-nov group
))
695 (goto-char (point-min))
696 (if (or (looking-at art
)
697 (search-forward (concat "\n" art
) nil t
))
698 ;; Delete the old NOV line.
699 (delete-region (progn (beginning-of-line) (point))
700 (progn (forward-line 1) (point)))
701 ;; The line isn't here, so we have to find out where
702 ;; we should insert it. (This situation should never
703 ;; occur, but one likes to make sure...)
704 (while (and (looking-at "[0-9]+\t")
707 (match-beginning 0) (match-end 0)))
709 (zerop (forward-line 1)))))
711 (nnheader-insert-nov headers
)
715 (deffoo nndiary-request-delete-group
(group &optional force server
)
716 (nndiary-possibly-change-directory group server
)
718 ;; Delete all articles in GROUP.
721 nndiary-current-directory t
722 (concat nnheader-numerical-short-files
723 "\\|" (regexp-quote nndiary-nov-file-name
) "$")))
726 (setq article
(pop articles
))
727 (when (file-writable-p article
)
728 (nnheader-message 5 "Deleting article %s in %s..." article group
)
729 (funcall nnmail-delete-file-function article
))))
730 ;; Try to delete the directory itself.
731 (ignore-errors (delete-directory nndiary-current-directory
)))
732 ;; Remove the group from all structures.
733 (setq nndiary-group-alist
734 (delq (assoc group nndiary-group-alist
) nndiary-group-alist
)
735 nndiary-current-group nil
736 nndiary-current-directory nil
)
737 ;; Save the active file.
738 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
741 (deffoo nndiary-request-rename-group
(group new-name
&optional server
)
742 (nndiary-possibly-change-directory group server
)
743 (let ((new-dir (nnmail-group-pathname new-name nndiary-directory
))
744 (old-dir (nnmail-group-pathname group nndiary-directory
)))
746 (make-directory new-dir t
)
748 ;; We move the articles file by file instead of renaming
749 ;; the directory -- there may be subgroups in this group.
750 ;; One might be more clever, I guess.
751 (let ((files (nnheader-article-to-file-alist old-dir
)))
754 (concat old-dir
(cdar files
))
755 (concat new-dir
(cdar files
)))
757 ;; Move .overview file.
758 (let ((overview (concat old-dir nndiary-nov-file-name
)))
759 (when (file-exists-p overview
)
760 (rename-file overview
(concat new-dir nndiary-nov-file-name
))))
761 (when (<= (length (directory-files old-dir
)) 2)
762 (ignore-errors (delete-directory old-dir
)))
763 ;; That went ok, so we change the internal structures.
764 (let ((entry (assoc group nndiary-group-alist
)))
766 (setcar entry new-name
))
767 (setq nndiary-current-directory nil
768 nndiary-current-group nil
)
769 ;; Save the new group alist.
770 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
773 (deffoo nndiary-set-status
(article name value
&optional group server
)
774 (nndiary-possibly-change-directory group server
)
775 (let ((file (nndiary-article-to-file article
)))
777 ((not (file-exists-p file
))
778 (nnheader-report 'nndiary
"File %s does not exist" file
))
781 (nnheader-insert-file-contents file
)
782 (nnmail-replace-status name value
))
786 ;;; Interface optional functions ============================================
788 (deffoo nndiary-request-update-info
(group info
&optional server
)
789 (nndiary-possibly-change-directory group
)
790 (let ((timestamp (gnus-group-parameter-value (gnus-info-params info
)
793 (nnheader-report 'nndiary
"Group %s doesn't have a timestamp" group
)
795 ;; Figure out which articles should be re-new'ed
796 (let ((articles (nndiary-flatten (gnus-info-read info
) 0))
797 article file unread buf
)
799 (setq buf
(nnheader-set-temp-buffer " *nndiary update*"))
800 (while (setq article
(pop articles
))
801 (setq file
(concat nndiary-current-directory
802 (int-to-string article
)))
803 (and (file-exists-p file
)
804 (nndiary-renew-article-p file timestamp
)
805 (push article unread
)))
806 ;;(message "unread: %s" unread)
809 (setq unread
(sort unread
'<))
811 (gnus-info-set-read info
(gnus-update-read-articles
812 (gnus-info-group info
) unread t
)))
814 (run-hook-with-args 'nndiary-request-update-info-hooks
815 (gnus-info-group info
))
820 ;;; Internal functions ======================================================
822 (defun nndiary-article-to-file (article)
823 (nndiary-update-file-alist)
825 (if (setq file
(cdr (assq article nndiary-article-file-alist
)))
826 (expand-file-name file nndiary-current-directory
)
827 ;; Just to make sure nothing went wrong when reading over NFS --
829 (if nndiary-check-directory-twice
831 (setq file
(expand-file-name (number-to-string article
)
832 nndiary-current-directory
)))
833 (nndiary-update-file-alist t
)
836 (defun nndiary-deletable-article-p (group article
)
837 "Say whether ARTICLE in GROUP can be deleted."
839 (when (setq path
(nndiary-article-to-file article
))
840 (when (file-writable-p path
)
841 (or (not nnmail-keep-last-article
)
842 (not (eq (cdr (nth 1 (assoc group nndiary-group-alist
)))
845 ;; Find an article number in the current group given the Message-ID.
846 (defun nndiary-find-group-number (id)
848 (set-buffer (get-buffer-create " *nndiary id*"))
849 (let ((alist nndiary-group-alist
)
851 ;; We want to look through all .overview files, but we want to
852 ;; start with the one in the current directory. It seems most
853 ;; likely that the article we are looking for is in that group.
854 (if (setq number
(nndiary-find-id nndiary-current-group id
))
855 (cons nndiary-current-group number
)
856 ;; It wasn't there, so we look through the other groups as well.
857 (while (and (not number
)
859 (or (string= (caar alist
) nndiary-current-group
)
860 (setq number
(nndiary-find-id (caar alist
) id
)))
862 (setq alist
(cdr alist
))))
864 (cons (caar alist
) number
))))))
866 (defun nndiary-find-id (group id
)
868 (let ((nov (expand-file-name nndiary-nov-file-name
869 (nnmail-group-pathname group
872 (when (file-exists-p nov
)
873 (nnheader-insert-file-contents nov
)
874 (while (and (not found
)
875 (search-forward id nil t
)) ; We find the ID.
876 ;; And the id is in the fourth field.
877 (if (not (and (search-backward "\t" nil t
4)
878 (not (search-backward"\t" (point-at-bol) t
))))
882 ;; We return the article number.
884 (ignore-errors (read (current-buffer))))))
887 (defun nndiary-retrieve-headers-with-nov (articles &optional fetch-old
)
888 (if (or gnus-nov-is-evil nndiary-nov-is-evil
)
890 (let ((nov (expand-file-name nndiary-nov-file-name
891 nndiary-current-directory
)))
892 (when (file-exists-p nov
)
894 (set-buffer nntp-server-buffer
)
896 (nnheader-insert-file-contents nov
)
898 (not (numberp fetch-old
)))
899 t
; Don't remove anything.
900 (nnheader-nov-delete-outside-range
901 (if fetch-old
(max 1 (- (car articles
) fetch-old
))
903 (car (last articles
)))
906 (defun nndiary-possibly-change-directory (group &optional server
)
908 (not (nndiary-server-opened server
)))
909 (nndiary-open-server server
))
912 (let ((pathname (nnmail-group-pathname group nndiary-directory
))
913 (file-name-coding-system nnmail-pathname-coding-system
))
914 (when (not (equal pathname nndiary-current-directory
))
915 (setq nndiary-current-directory pathname
916 nndiary-current-group group
917 nndiary-article-file-alist nil
))
918 (file-exists-p nndiary-current-directory
))))
920 (defun nndiary-possibly-create-directory (group)
921 (let ((dir (nnmail-group-pathname group nndiary-directory
)))
922 (unless (file-exists-p dir
)
923 (make-directory (directory-file-name dir
) t
)
924 (nnheader-message 5 "Creating mail directory %s" dir
))))
926 (defun nndiary-save-mail (group-art)
927 "Called narrowed to an article."
929 (setq chars
(nnmail-insert-lines))
930 (nnmail-insert-xref group-art
)
931 (run-hooks 'nnmail-prepare-save-mail-hook
)
932 (run-hooks 'nndiary-prepare-save-mail-hook
)
933 (goto-char (point-min))
934 (while (looking-at "From ")
935 (replace-match "X-From-Line: ")
937 ;; We save the article in all the groups it belongs in.
941 (nndiary-possibly-create-directory (caar ga
))
942 (let ((file (concat (nnmail-group-pathname
943 (caar ga
) nndiary-directory
)
944 (int-to-string (cdar ga
)))))
946 ;; It was already saved, so we just make a hard link.
947 (funcall nnmail-crosspost-link-function first file t
)
949 (nnmail-write-region (point-min) (point-max) file nil
950 (if (nnheader-be-verbose 5) nil
'nomesg
))
953 ;; Generate a nov line for this article. We generate the nov
954 ;; line after saving, because nov generation destroys the
956 (setq headers
(nndiary-parse-head chars
))
957 ;; Output the nov line to all nov databases that should have it.
958 (let ((ga group-art
))
960 (nndiary-add-nov (caar ga
) (cdar ga
) headers
)
964 (defun nndiary-active-number (group)
965 "Compute the next article number in GROUP."
966 (let ((active (cadr (assoc group nndiary-group-alist
))))
967 ;; The group wasn't known to nndiary, so we just create an active
970 ;; Perhaps the active file was corrupt? See whether
971 ;; there are any articles in this group.
972 (nndiary-possibly-create-directory group
)
973 (nndiary-possibly-change-directory group
)
974 (unless nndiary-article-file-alist
975 (setq nndiary-article-file-alist
977 (nnheader-article-to-file-alist nndiary-current-directory
)
978 'car-less-than-car
)))
980 (if nndiary-article-file-alist
981 (cons (caar nndiary-article-file-alist
)
982 (caar (last nndiary-article-file-alist
)))
984 (push (list group active
) nndiary-group-alist
))
985 (setcdr active
(1+ (cdr active
)))
986 (while (file-exists-p
987 (expand-file-name (int-to-string (cdr active
))
988 (nnmail-group-pathname group nndiary-directory
)))
989 (setcdr active
(1+ (cdr active
))))
992 (defun nndiary-add-nov (group article headers
)
993 "Add a nov line for the GROUP base."
995 (set-buffer (nndiary-open-nov group
))
996 (goto-char (point-max))
997 (mail-header-set-number headers article
)
998 (nnheader-insert-nov headers
)))
1000 (defsubst nndiary-header-value
()
1001 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
1003 (defun nndiary-parse-head (chars &optional number
)
1004 "Parse the head of the current buffer."
1007 (unless (zerop (buffer-size))
1009 (goto-char (point-min))
1010 (if (search-forward "\n\n" nil t
) (1- (point)) (point-max))))
1011 (let ((headers (nnheader-parse-naked-head)))
1012 (mail-header-set-chars headers chars
)
1013 (mail-header-set-number headers number
)
1016 (defun nndiary-open-nov (group)
1017 (or (cdr (assoc group nndiary-nov-buffer-alist
))
1018 (let ((buffer (get-buffer-create (format " *nndiary overview %s*"
1022 (set (make-local-variable 'nndiary-nov-buffer-file-name
)
1024 nndiary-nov-file-name
1025 (nnmail-group-pathname group nndiary-directory
)))
1027 (when (file-exists-p nndiary-nov-buffer-file-name
)
1028 (nnheader-insert-file-contents nndiary-nov-buffer-file-name
)))
1029 (push (cons group buffer
) nndiary-nov-buffer-alist
)
1032 (defun nndiary-save-nov ()
1034 (while nndiary-nov-buffer-alist
1035 (when (buffer-name (cdar nndiary-nov-buffer-alist
))
1036 (set-buffer (cdar nndiary-nov-buffer-alist
))
1037 (when (buffer-modified-p)
1038 (nnmail-write-region 1 (point-max) nndiary-nov-buffer-file-name
1040 (set-buffer-modified-p nil
)
1041 (kill-buffer (current-buffer)))
1042 (setq nndiary-nov-buffer-alist
(cdr nndiary-nov-buffer-alist
)))))
1045 (defun nndiary-generate-nov-databases (&optional server
)
1046 "Generate NOV databases in all nndiary directories."
1047 (interactive (list (or (nnoo-current-server 'nndiary
) "")))
1048 ;; Read the active file to make sure we don't re-use articles
1049 ;; numbers in empty groups.
1050 (nnmail-activate 'nndiary
)
1051 (unless (nndiary-server-opened server
)
1052 (nndiary-open-server server
))
1053 (setq nndiary-directory
(expand-file-name nndiary-directory
))
1054 ;; Recurse down the directories.
1055 (nndiary-generate-nov-databases-1 nndiary-directory nil t
)
1056 ;; Save the active file.
1057 (nnmail-save-active nndiary-group-alist nndiary-active-file
))
1059 (defun nndiary-generate-nov-databases-1 (dir &optional seen no-active
)
1060 "Regenerate the NOV database in DIR."
1061 (interactive "DRegenerate NOV in: ")
1062 (setq dir
(file-name-as-directory dir
))
1063 ;; Only scan this sub-tree if we haven't been here yet.
1064 (unless (member (file-truename dir
) seen
)
1065 (push (file-truename dir
) seen
)
1066 ;; We descend recursively
1067 (let ((dirs (directory-files dir t nil t
))
1069 (while (setq dir
(pop dirs
))
1070 (when (and (not (string-match "^\\." (file-name-nondirectory dir
)))
1071 (file-directory-p dir
))
1072 (nndiary-generate-nov-databases-1 dir seen
))))
1073 ;; Do this directory.
1074 (let ((files (sort (nnheader-article-to-file-alist dir
)
1075 'car-less-than-car
)))
1077 (let* ((group (nnheader-file-to-group
1078 (directory-file-name dir
) nndiary-directory
))
1079 (info (cadr (assoc group nndiary-group-alist
))))
1081 (setcar info
(1+ (cdr info
)))))
1082 (funcall nndiary-generate-active-function dir
)
1083 ;; Generate the nov file.
1084 (nndiary-generate-nov-file dir files
)
1086 (nnmail-save-active nndiary-group-alist nndiary-active-file
))))))
1089 (defun nndiary-generate-active-info (dir)
1090 ;; Update the active info for this group.
1091 (let* ((group (nnheader-file-to-group
1092 (directory-file-name dir
) nndiary-directory
))
1093 (entry (assoc group nndiary-group-alist
))
1094 (last (or (caadr entry
) 0)))
1095 (setq nndiary-group-alist
(delq entry nndiary-group-alist
))
1097 (cons (or (caar files
) (1+ last
))
1099 (or (caar (last files
))
1101 nndiary-group-alist
)))
1103 (defun nndiary-generate-nov-file (dir files
)
1104 (let* ((dir (file-name-as-directory dir
))
1105 (nov (concat dir nndiary-nov-file-name
))
1106 (nov-buffer (get-buffer-create " *nov*"))
1109 ;; Init the nov buffer.
1110 (set-buffer nov-buffer
)
1111 (buffer-disable-undo)
1113 (set-buffer nntp-server-buffer
)
1114 ;; Delete the old NOV file.
1115 (when (file-exists-p nov
)
1116 (funcall nnmail-delete-file-function nov
))
1118 (unless (file-directory-p (setq file
(concat dir
(cdar files
))))
1120 (nnheader-insert-file-contents file
)
1122 (goto-char (point-min))
1124 (search-forward "\n\n" nil t
)
1125 (setq chars
(- (point-max) (point)))
1126 (max 1 (1- (point)))))
1127 (unless (zerop (buffer-size))
1128 (goto-char (point-min))
1129 (setq headers
(nndiary-parse-head chars
(caar files
)))
1131 (set-buffer nov-buffer
)
1132 (goto-char (point-max))
1133 (nnheader-insert-nov headers
)))
1135 (setq files
(cdr files
)))
1137 (set-buffer nov-buffer
)
1138 (nnmail-write-region 1 (point-max) nov nil
'nomesg
)
1139 (kill-buffer (current-buffer))))))
1141 (defun nndiary-nov-delete-article (group article
)
1143 (set-buffer (nndiary-open-nov group
))
1144 (when (nnheader-find-nov-line article
)
1145 (delete-region (point) (progn (forward-line 1) (point)))
1147 (let ((active (cadr (assoc group nndiary-group-alist
)))
1151 (setf (car active
) (1+ (cdr active
)))
1152 (when (and (setq num
(ignore-errors (read (current-buffer))))
1154 (setf (car active
) num
)))))))
1157 (defun nndiary-update-file-alist (&optional force
)
1158 (when (or (not nndiary-article-file-alist
)
1160 (setq nndiary-article-file-alist
1161 (nnheader-article-to-file-alist nndiary-current-directory
))))
1164 (defun nndiary-string-to-number (str min
&optional max
)
1165 ;; Like `string-to-number' but barf if STR is not exactly an integer, and not
1166 ;; within the specified bounds.
1167 ;; Signals are caught by `nndiary-schedule'.
1168 (if (not (string-match "^[ \t]*[0-9]+[ \t]*$" str
))
1169 (nndiary-error "not an integer value")
1171 (let ((val (string-to-number str
)))
1172 (and (or (< val min
)
1173 (and max
(> val max
)))
1174 (nndiary-error "value out of range"))
1177 (defun nndiary-parse-schedule-value (str min-or-values max
)
1178 ;; Parse the schedule string STR, or signal an error.
1179 ;; Signals are caught by `nndary-schedule'.
1180 (if (string-match "[ \t]*\\*[ \t]*" str
)
1184 (if (listp min-or-values
)
1185 ;; min-or-values is values
1186 ;; #### NOTE: this is actually only a hack for time zones.
1187 (let ((val (and (string-match "[ \t]*\\([^ \t]+\\)[ \t]*" str
)
1188 (match-string 1 str
))))
1189 (if (and val
(setq val
(assoc val min-or-values
)))
1191 (nndiary-error "invalid syntax")))
1192 ;; min-or-values is min
1195 (let ((res (split-string val
"-")))
1198 (nndiary-string-to-number (car res
) min-or-values max
))
1200 ;; don't know if crontab accepts this, but ensure
1201 ;; that BEG is <= END
1202 (let ((beg (nndiary-string-to-number (car res
) min-or-values max
))
1203 (end (nndiary-string-to-number (cadr res
) min-or-values max
)))
1211 (nndiary-error "invalid syntax")))
1213 (split-string str
",")))
1216 ;; ### FIXME: remove this function if it's used only once.
1217 (defun nndiary-parse-schedule (head min-or-values max
)
1218 ;; Parse the cron-like value of header X-Diary-HEAD in current buffer.
1219 ;; - Returns nil if `*'
1220 ;; - Otherwise returns a list of integers and/or ranges (BEG . END)
1221 ;; The exception is the Timze-Zone value which is always of the form (STR).
1222 ;; Signals are caught by `nndary-schedule'.
1223 (let ((header (format "^X-Diary-%s: \\(.*\\)$" head
)))
1224 (goto-char (point-min))
1225 (if (not (re-search-forward header nil t
))
1226 (nndiary-error "header missing")
1228 (nndiary-parse-schedule-value (match-string 1) min-or-values max
))
1231 (defun nndiary-max (spec)
1232 ;; Returns the max of specification SPEC, or nil for permanent schedules.
1237 (while (setq elt
(pop elts
))
1239 (and (> elt max
) (setq max elt
))
1240 (and (> (cdr elt
) max
) (setq max
(cdr elt
)))))
1243 (defun nndiary-flatten (spec min
&optional max
)
1244 ;; flatten the spec by expanding ranges to all possible values.
1247 ;; this happens when I flatten something else than one of my
1248 ;; schedules (a list of read articles for instance).
1257 (while (setq elt
(pop elts
))
1262 (while (<= n
(cdr elt
))
1264 (setq n
(1+ n
))))))))
1267 (defun nndiary-unflatten (spec)
1268 ;; opposite of flatten: build ranges if possible
1269 (setq spec
(sort spec
'<))
1271 (while (setq min
(pop spec
))
1273 (while (and (car spec
) (= (car spec
) (1+ max
)))
1277 (setq res
(append res
(list min
)))
1278 (setq res
(append res
(list (cons min max
))))))
1281 (defun nndiary-compute-reminders (date)
1282 ;; Returns a list of times corresponding to the reminders of date DATE.
1283 ;; See the comment in `nndiary-reminders' about rounding.
1284 (let* ((reminders nndiary-reminders
)
1285 (date-elts (decode-time date
))
1286 ;; ### NOTE: out-of-range values are accepted by encode-time. This
1287 ;; makes our life easier.
1288 (monday (- (nth 3 date-elts
)
1289 (if nndiary-week-starts-on-monday
1290 (if (zerop (nth 6 date-elts
))
1292 (- (nth 6 date-elts
) 1))
1293 (nth 6 date-elts
))))
1295 ;; remove the DOW and DST entries
1296 (setcdr (nthcdr 5 date-elts
) (nthcdr 8 date-elts
))
1297 (while (setq reminder
(pop reminders
))
1299 (cond ((eq (cdr reminder
) 'minute
)
1301 (apply 'encode-time
0 (nthcdr 1 date-elts
))
1302 (seconds-to-time (* (car reminder
) 60.0))))
1303 ((eq (cdr reminder
) 'hour
)
1305 (apply 'encode-time
0 0 (nthcdr 2 date-elts
))
1306 (seconds-to-time (* (car reminder
) 3600.0))))
1307 ((eq (cdr reminder
) 'day
)
1309 (apply 'encode-time
0 0 0 (nthcdr 3 date-elts
))
1310 (seconds-to-time (* (car reminder
) 86400.0))))
1311 ((eq (cdr reminder
) 'week
)
1313 (apply 'encode-time
0 0 0 monday
(nthcdr 4 date-elts
))
1314 (seconds-to-time (* (car reminder
) 604800.0))))
1315 ((eq (cdr reminder
) 'month
)
1317 (apply 'encode-time
0 0 0 1 (nthcdr 4 date-elts
))
1318 (seconds-to-time (* (car reminder
) 18748800.0))))
1319 ((eq (cdr reminder
) 'year
)
1321 (apply 'encode-time
0 0 0 1 1 (nthcdr 5 date-elts
))
1322 (seconds-to-time (* (car reminder
) 400861056.0)))))
1324 (sort res
'time-less-p
)))
1326 (defun nndiary-last-occurence (sched)
1327 ;; Returns the last occurence of schedule SCHED as an Emacs time struct, or
1328 ;; nil for permanent schedule or errors.
1329 (let ((minute (nndiary-max (nth 0 sched
)))
1330 (hour (nndiary-max (nth 1 sched
)))
1331 (year (nndiary-max (nth 4 sched
)))
1332 (time-zone (or (and (nth 6 sched
) (car (nth 6 sched
)))
1333 (current-time-zone))))
1335 (or minute
(setq minute
59))
1336 (or hour
(setq hour
23))
1337 ;; I'll just compute all possible values and test them by decreasing
1338 ;; order until one succeeds. This is probably quide rude, but I got
1339 ;; bored in finding a good algorithm for doing that ;-)
1340 ;; ### FIXME: remove identical entries.
1341 (let ((dom-list (nth 2 sched
))
1342 (month-list (sort (nndiary-flatten (nth 3 sched
) 1 12) '>))
1343 (year-list (sort (nndiary-flatten (nth 4 sched
) 1971) '>))
1344 (dow-list (nth 5 sched
)))
1345 ;; Special case: an asterisk in one of the days specifications means
1346 ;; that only the other should be taken into account. If both are
1347 ;; unspecified, you would get all possible days in both.
1348 (cond ((null dow-list
)
1349 ;; this gets all days if dom-list is nil
1350 (setq dom-list
(nndiary-flatten dom-list
1 31)))
1352 ;; this also gets all days if dow-list is nil
1353 (setq dow-list
(nndiary-flatten dow-list
0 6)))
1355 (setq dom-list
(nndiary-flatten dom-list
1 31))
1356 (setq dow-list
(nndiary-flatten dow-list
0 6))))
1359 (while (setq year
(pop year-list
))
1360 (let ((months month-list
)
1362 (while (setq month
(pop months
))
1363 ;; Now we must merge the Dows with the Doms. To do that, we
1364 ;; have to know which day is the 1st one for this month.
1365 ;; Maybe there's simpler, but decode-time(encode-time) will
1366 ;; give us the answer.
1367 (let ((first (nth 6 (decode-time
1368 (encode-time 0 0 0 1 month year
1370 (max (cond ((= month
2)
1371 (if (date-leap-year-p year
) 29 28))
1373 (if (zerop (% month
2)) 30 31))
1375 (if (zerop (% month
2)) 31 30))))
1379 ;; first, review the doms to see if they are valid.
1380 (while (setq day
(pop doms
))
1383 ;; second add all possible dows
1384 (while (setq day
(pop dows
))
1386 (setq day
(1+ (- day first
)))
1387 (and (< day
0) (setq day
(+ 7 day
)))
1390 (setq day
(+ 7 day
))))
1391 ;; Finally, if we have some days, they are valid
1395 (encode-time 0 minute hour
1396 (car days
) month year time-zone
)))
1398 ;; There's an upper limit, but we didn't find any last occurence.
1399 ;; This means that the schedule is undecidable. This can happen if
1400 ;; you happen to say something like "each Feb 31 until 2038".
1402 (nnheader-report 'nndiary
"Undecidable schedule")
1406 (defun nndiary-next-occurence (sched now
)
1407 ;; Returns the next occurence of schedule SCHED, starting from time NOW.
1408 ;; If there's no next occurence, returns the last one (if any) which is then
1410 (let* ((today (decode-time now
))
1411 (this-minute (nth 1 today
))
1412 (this-hour (nth 2 today
))
1413 (this-day (nth 3 today
))
1414 (this-month (nth 4 today
))
1415 (this-year (nth 5 today
))
1416 (minute-list (sort (nndiary-flatten (nth 0 sched
) 0 59) '<))
1417 (hour-list (sort (nndiary-flatten (nth 1 sched
) 0 23) '<))
1418 (dom-list (nth 2 sched
))
1419 (month-list (sort (nndiary-flatten (nth 3 sched
) 1 12) '<))
1420 (years (if (nth 4 sched
)
1421 (sort (nndiary-flatten (nth 4 sched
) 1971) '<)
1423 (dow-list (nth 5 sched
))
1424 (year (1- this-year
))
1425 (time-zone (or (and (nth 6 sched
) (car (nth 6 sched
)))
1426 (current-time-zone))))
1427 ;; Special case: an asterisk in one of the days specifications means that
1428 ;; only the other should be taken into account. If both are unspecified,
1429 ;; you would get all possible days in both.
1430 (cond ((null dow-list
)
1431 ;; this gets all days if dom-list is nil
1432 (setq dom-list
(nndiary-flatten dom-list
1 31)))
1434 ;; this also gets all days if dow-list is nil
1435 (setq dow-list
(nndiary-flatten dow-list
0 6)))
1437 (setq dom-list
(nndiary-flatten dom-list
1 31))
1438 (setq dow-list
(nndiary-flatten dow-list
0 6))))
1439 ;; Remove past years.
1440 (unless (eq years t
)
1441 (while (and (car years
) (< (car years
) this-year
))
1444 ;; Because we might not be limited in years, we must guard against
1445 ;; infinite loops. Appart from cases like Feb 31, there are probably
1446 ;; other ones, (no monday XXX 2nd etc). I don't know any algorithm to
1447 ;; decide this, so I assume that if we reach 10 years later, the
1448 ;; schedule is undecidable.
1451 (while (if (eq years t
)
1452 (and (setq year
(1+ year
))
1453 (<= year
(+ 10 this-year
)))
1454 (setq year
(pop years
)))
1455 (let ((months month-list
)
1457 ;; Remove past months for this year.
1458 (and (= year this-year
)
1459 (while (and (car months
) (< (car months
) this-month
))
1461 (while (setq month
(pop months
))
1462 ;; Now we must merge the Dows with the Doms. To do that, we
1463 ;; have to know which day is the 1st one for this month.
1464 ;; Maybe there's simpler, but decode-time(encode-time) will
1465 ;; give us the answer.
1466 (let ((first (nth 6 (decode-time
1467 (encode-time 0 0 0 1 month year
1469 (max (cond ((= month
2)
1470 (if (date-leap-year-p year
) 29 28))
1472 (if (zerop (% month
2)) 30 31))
1474 (if (zerop (% month
2)) 31 30))))
1478 ;; first, review the doms to see if they are valid.
1479 (while (setq day
(pop doms
))
1482 ;; second add all possible dows
1483 (while (setq day
(pop dows
))
1485 (setq day
(1+ (- day first
)))
1486 (and (< day
0) (setq day
(+ 7 day
)))
1489 (setq day
(+ 7 day
))))
1490 ;; Aaaaaaall right. Now we have a valid list of DAYS for
1491 ;; this month and this year.
1493 (setq days
(sort days
'<))
1494 ;; Remove past days for this year and this month.
1495 (and (= year this-year
)
1496 (= month this-month
)
1497 (while (and (car days
) (< (car days
) this-day
))
1499 (while (setq day
(pop days
))
1500 (let ((hours hour-list
)
1502 ;; Remove past hours for this year, this month and
1504 (and (= year this-year
)
1505 (= month this-month
)
1507 (while (and (car hours
)
1508 (< (car hours
) this-hour
))
1510 (while (setq hour
(pop hours
))
1511 (let ((minutes minute-list
)
1513 ;; Remove past hours for this year, this month,
1514 ;; this day and this hour.
1515 (and (= year this-year
)
1516 (= month this-month
)
1519 (while (and (car minutes
)
1520 (< (car minutes
) this-minute
))
1522 (while (setq minute
(pop minutes
))
1523 ;; Ouch! Here, we've got a complete valid
1524 ;; schedule. It's a good one if it's in the
1526 (let ((time (encode-time 0 minute hour day
1529 (and (time-less-p now time
)
1530 (throw 'found time
)))
1535 (nndiary-last-occurence sched
))
1537 (nndiary-last-occurence sched
))
1540 (defun nndiary-expired-article-p (file)
1542 (if (nnheader-insert-head file
)
1543 (let ((sched (nndiary-schedule)))
1544 ;; An article has expired if its last schedule (if any) is in the
1545 ;; past. A permanent schedule never expires.
1547 (setq sched
(nndiary-last-occurence sched
))
1548 (time-less-p sched
(current-time))))
1550 (nnheader-report 'nndiary
"Could not read file %s" file
)
1554 (defun nndiary-renew-article-p (file timestamp
)
1556 (if (nnheader-insert-head file
)
1557 (let ((now (current-time))
1558 (sched (nndiary-schedule)))
1559 ;; The article should be re-considered as unread if there's a reminder
1560 ;; between the group timestamp and the current time.
1561 (when (and sched
(setq sched
(nndiary-next-occurence sched now
)))
1562 (let ((reminders ;; add the next occurence itself at the end.
1563 (append (nndiary-compute-reminders sched
) (list sched
))))
1564 (while (and reminders
(time-less-p (car reminders
) timestamp
))
1566 ;; The reminders might be empty if the last date is in the past,
1567 ;; or we've got at least the next occurence itself left. All past
1568 ;; dates are renewed.
1570 (time-less-p (car reminders
) now
)))
1573 (nnheader-report 'nndiary
"Could not read file %s" file
)
1576 ;; The end... ===============================================================
1578 (dolist (header nndiary-headers
)
1579 (setq header
(intern (format "X-Diary-%s" (car header
))))
1580 ;; Required for building NOV databases and some other stuff.
1581 (add-to-list 'gnus-extra-headers header
)
1582 (add-to-list 'nnmail-extra-headers header
))
1584 (unless (assoc "nndiary" gnus-valid-select-methods
)
1585 (gnus-declare-backend "nndiary" 'post-mail
'respool
'address
))
1590 ;; arch-tag: 9c542b95-92e7-4ace-a038-330ab296e203
1591 ;;; nndiary.el ends here