1 ;;; nndiary.el --- A diary back end for Gnus
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 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 of the License, or
16 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
29 ;; Contents management by FCM version 0.1.
34 ;; nndiary is a mail back end designed to handle mails as diary event
35 ;; reminders. It is now fully documented in the Gnus manual.
41 ;; * Respooling doesn't work because contrary to the request-scan function,
42 ;; Gnus won't allow me to override the split methods when calling the
43 ;; respooling back end functions.
44 ;; * There's a bug in the time zone mechanism with variable TZ locations.
45 ;; * We could allow a keyword like `ask' in X-Diary-* headers, that would mean
46 ;; "ask for value upon reception of the message".
47 ;; * We could add an optional header X-Diary-Reminders to specify a special
48 ;; reminders value for this message. Suggested by Jody Klymak.
49 ;; * We should check messages validity in other circumstances than just
50 ;; moving an article from somewhere else (request-accept). For instance,
51 ;; when editing / saving and so on.
57 ;; * nnoo. NNDiary is very similar to nnml. This makes the idea of using nnoo
58 ;; (to derive nndiary from nnml) natural. However, my experience with nnoo
59 ;; is that for reasonably complex back ends like this one, noo is a burden
60 ;; rather than an help. It's tricky to use, not everything can be inherited,
61 ;; what can be inherited and when is not very clear, and you've got to be
62 ;; very careful because a little mistake can fuck up your other back ends,
63 ;; especially because their variables will be use instead of your real ones.
64 ;; Finally, I found it easier to just clone the needed parts of nnml, and
65 ;; tracking nnml updates is not a big deal.
67 ;; IMHO, nnoo is actually badly designed. A much simpler, and yet more
68 ;; powerful one would be to make *real* functions and variables for a new
69 ;; back end based on another. Lisp is a reflexive language so that's a very
70 ;; easy thing to do: inspect the function's form, replace occurences of
71 ;; <nnfrom> (even in strings) with <nnto>, and you're done.
73 ;; * nndiary-get-new-mail, nndiary-mail-source and nndiary-split-methods:
74 ;; NNDiary has some experimental parts, in the sense Gnus normally uses only
75 ;; one mail back ends for mail retreival and splitting. This back end is
76 ;; also an attempt to make it behave differently. For Gnus developpers: as
77 ;; you can see if you snarf into the code, that was not a very difficult
78 ;; thing to do. Something should be done about the respooling breakage
87 (eval-when-compile (require 'cl
))
92 ;; Compatibility Functions =================================================
95 (if (fboundp 'signal-error
)
96 (defun nndiary-error (&rest args
)
97 (apply #'signal-error
'nndiary args
))
98 (defun nndiary-error (&rest args
)
99 (apply #'error args
))))
102 ;; Back End behavior customization ===========================================
104 (defgroup nndiary nil
105 "The Gnus Diary back end."
109 (defcustom nndiary-mail-sources
110 `((file :path
,(expand-file-name "~/.nndiary")))
111 "*NNDiary specific mail sources.
112 This variable is used by nndiary in place of the standard `mail-sources'
113 variable when `nndiary-get-new-mail' is set to non-nil. These sources
114 must contain diary messages ONLY."
119 (defcustom nndiary-split-methods
'(("diary" ""))
120 "*NNDiary specific split methods.
121 This variable is used by nndiary in place of the standard
122 `nnmail-split-methods' variable when `nndiary-get-new-mail' is set to
126 :type
'(choice (repeat :tag
"Alist" (group (string :tag
"Name") regexp
))
127 (function-item nnmail-split-fancy
)
128 (function :tag
"Other")))
131 (defcustom nndiary-reminders
'((0 . day
))
132 "*Different times when you want to be reminded of your appointments.
133 Diary articles will appear again, as if they'd been just received.
135 Entries look like (3 . day) which means something like \"Please
136 Hortense, would you be so kind as to remind me of my appointments 3 days
137 before the date, thank you very much. Anda, hmmm... by the way, are you
138 doing anything special tonight ?\".
140 The units of measure are 'minute 'hour 'day 'week 'month and 'year (no,
141 not 'century, sorry).
143 NOTE: the units of measure actually express dates, not durations: if you
144 use 'week, messages will pop up on Sundays at 00:00 (or Mondays if
145 `nndiary-week-starts-on-monday' is non-nil) and *not* 7 days before the
146 appointment, if you use 'month, messages will pop up on the first day of
147 each months, at 00:00 and so on.
149 If you really want to specify a duration (like 24 hours exactly), you can
150 use the equivalent in minutes (the smallest unit). A fuzz of 60 seconds
151 maximum in the reminder is not that painful, I think. Although this
152 scheme might appear somewhat weird at a first glance, it is very powerful.
153 In order to make this clear, here are some examples:
155 - '(0 . day): this is the default value of `nndiary-reminders'. It means
156 pop up the appointments of the day each morning at 00:00.
158 - '(1 . day): this means pop up the appointments the day before, at 00:00.
160 - '(6 . hour): for an appointment at 18:30, this would pop up the
161 appointment message at 12:00.
163 - '(360 . minute): for an appointment at 18:30 and 15 seconds, this would
164 pop up the appointment message at 12:30."
166 :type
'(repeat (cons :format
"%v\n"
167 (integer :format
"%v")
168 (choice :format
"%[%v(s)%] before...\n"
170 (const :format
"%v" minute
)
171 (const :format
"%v" hour
)
172 (const :format
"%v" day
)
173 (const :format
"%v" week
)
174 (const :format
"%v" month
)
175 (const :format
"%v" year
)))))
177 (defcustom nndiary-week-starts-on-monday nil
178 "*Whether a week starts on monday (otherwise, sunday)."
183 (defcustom nndiary-request-create-group-hooks nil
184 "*Hooks to run after `nndiary-request-create-group' is executed.
185 The hooks will be called with the full group name as argument."
189 (defcustom nndiary-request-update-info-hooks nil
190 "*Hooks to run after `nndiary-request-update-info-group' is executed.
191 The hooks will be called with the full group name as argument."
195 (defcustom nndiary-request-accept-article-hooks nil
196 "*Hooks to run before accepting an article.
197 Executed near the beginning of `nndiary-request-accept-article'.
198 The hooks will be called with the article in the current buffer."
202 (defcustom nndiary-check-directory-twice t
203 "*If t, check directories twice to avoid NFS failures."
208 ;; Back End declaration ======================================================
210 ;; Well, most of this is nnml clonage.
212 (nnoo-declare nndiary
)
214 (defvoo nndiary-directory
(nnheader-concat gnus-directory
"diary/")
215 "Spool directory for the nndiary back end.")
217 (defvoo nndiary-active-file
218 (expand-file-name "active" nndiary-directory
)
219 "Active file for the nndiary back end.")
221 (defvoo nndiary-newsgroups-file
222 (expand-file-name "newsgroups" nndiary-directory
)
223 "Newsgroups description file for the nndiary back end.")
225 (defvoo nndiary-get-new-mail nil
226 "Whether nndiary gets new mail and split it.
227 Contrary to traditional mail back ends, this variable can be set to t
228 even if your primary mail back end also retreives mail. In such a case,
229 NDiary uses its own mail-sources and split-methods.")
231 (defvoo nndiary-nov-is-evil nil
232 "If non-nil, Gnus will never use nov databases for nndiary groups.
233 Using nov databases will speed up header fetching considerably.
234 This variable shouldn't be flipped much. If you have, for some reason,
235 set this to t, and want to set it to nil again, you should always run
236 the `nndiary-generate-nov-databases' command. The function will go
237 through all nnml directories and generate nov databases for them
238 all. This may very well take some time.")
240 (defvoo nndiary-prepare-save-mail-hook nil
241 "*Hook run narrowed to an article before saving.")
243 (defvoo nndiary-inhibit-expiry nil
244 "If non-nil, inhibit expiry.")
248 (defconst nndiary-version
"0.2-b14"
249 "Current Diary back end version.")
251 (defun nndiary-version ()
252 "Current Diary back end version."
254 (message "NNDiary version %s" nndiary-version
))
256 (defvoo nndiary-nov-file-name
".overview")
258 (defvoo nndiary-current-directory nil
)
259 (defvoo nndiary-current-group nil
)
260 (defvoo nndiary-status-string
"" )
261 (defvoo nndiary-nov-buffer-alist nil
)
262 (defvoo nndiary-group-alist nil
)
263 (defvoo nndiary-active-timestamp nil
)
264 (defvoo nndiary-article-file-alist nil
)
266 (defvoo nndiary-generate-active-function
'nndiary-generate-active-info
)
267 (defvoo nndiary-nov-buffer-file-name nil
)
268 (defvoo nndiary-file-coding-system nnmail-file-coding-system
)
270 (defconst nndiary-headers
277 ("Time-Zone" (("Y" -
43200)
354 ;; List of NNDiary headers that specify the time spec. Each header name is
355 ;; followed by either two integers (specifying a range of possible values
356 ;; for this header) or one list (specifying all the possible values for this
357 ;; header). In the latter case, the list does NOT include the unspecifyed
359 ;; For time zone values, we have symbolic time zone names associated with
360 ;; the (relative) number of seconds ahead GMT.
363 (defsubst nndiary-schedule
()
368 (setq head
(nth 0 elt
))
369 (nndiary-parse-schedule (nth 0 elt
) (nth 1 elt
) (nth 2 elt
)))
372 (nnheader-report 'nndiary
"X-Diary-%s header parse error: %s."
377 ;;; Interface functions =====================================================
379 (nnoo-define-basics nndiary
)
381 (deffoo nndiary-retrieve-headers
(sequence &optional group server fetch-old
)
382 (when (nndiary-possibly-change-directory group server
)
384 (set-buffer nntp-server-buffer
)
387 (number (length sequence
))
389 (file-name-coding-system nnmail-pathname-coding-system
)
391 (nndiary-check-directory-twice
392 (and nndiary-check-directory-twice
393 ;; To speed up, disable it in some case.
394 (or (not (numberp nnmail-large-newsgroup
))
395 (<= number nnmail-large-newsgroup
)))))
396 (if (stringp (car sequence
))
398 (if (nndiary-retrieve-headers-with-nov sequence fetch-old
)
401 (setq article
(car sequence
))
402 (setq file
(nndiary-article-to-file article
))
405 (not (file-directory-p file
)))
406 (insert (format "221 %d Article retrieved.\n" article
))
408 (nnheader-insert-head file
)
410 (if (search-forward "\n\n" nil t
)
412 (goto-char (point-max))
415 (delete-region (point) (point-max)))
416 (setq sequence
(cdr sequence
))
417 (setq count
(1+ count
))
418 (and (numberp nnmail-large-newsgroup
)
419 (> number nnmail-large-newsgroup
)
421 (nnheader-message 6 "nndiary: Receiving headers... %d%%"
422 (/ (* count
100) number
))))
424 (and (numberp nnmail-large-newsgroup
)
425 (> number nnmail-large-newsgroup
)
426 (nnheader-message 6 "nndiary: Receiving headers...done"))
428 (nnheader-fold-continuation-lines)
431 (deffoo nndiary-open-server
(server &optional defs
)
432 (nnoo-change-server 'nndiary server defs
)
433 (when (not (file-exists-p nndiary-directory
))
434 (ignore-errors (make-directory nndiary-directory t
)))
436 ((not (file-exists-p nndiary-directory
))
437 (nndiary-close-server)
438 (nnheader-report 'nndiary
"Couldn't create directory: %s"
440 ((not (file-directory-p (file-truename nndiary-directory
)))
441 (nndiary-close-server)
442 (nnheader-report 'nndiary
"Not a directory: %s" nndiary-directory
))
444 (nnheader-report 'nndiary
"Opened server %s using directory %s"
445 server nndiary-directory
)
448 (deffoo nndiary-request-regenerate
(server)
449 (nndiary-possibly-change-directory nil server
)
450 (nndiary-generate-nov-databases server
)
453 (deffoo nndiary-request-article
(id &optional group server buffer
)
454 (nndiary-possibly-change-directory group server
)
455 (let* ((nntp-server-buffer (or buffer nntp-server-buffer
))
456 (file-name-coding-system nnmail-pathname-coding-system
)
457 path gpath group-num
)
459 (when (and (setq group-num
(nndiary-find-group-number id
))
461 (assq (cdr group-num
)
462 (nnheader-article-to-file-alist
464 (nnmail-group-pathname
466 nndiary-directory
))))))
467 (setq path
(concat gpath
(int-to-string (cdr group-num
)))))
468 (setq path
(nndiary-article-to-file id
)))
471 (nnheader-report 'nndiary
"No such article: %s" id
))
472 ((not (file-exists-p path
))
473 (nnheader-report 'nndiary
"No such file: %s" path
))
474 ((file-directory-p path
)
475 (nnheader-report 'nndiary
"File is a directory: %s" path
))
476 ((not (save-excursion (let ((nnmail-file-coding-system
477 nndiary-file-coding-system
))
478 (nnmail-find-file path
))))
479 (nnheader-report 'nndiary
"Couldn't read file: %s" path
))
481 (nnheader-report 'nndiary
"Article %s retrieved" id
)
482 ;; We return the article number.
483 (cons (if group-num
(car group-num
) group
)
484 (string-to-number (file-name-nondirectory path
)))))))
486 (deffoo nndiary-request-group
(group &optional server dont-check
)
487 (let ((file-name-coding-system nnmail-pathname-coding-system
))
489 ((not (nndiary-possibly-change-directory group server
))
490 (nnheader-report 'nndiary
"Invalid group (no such directory)"))
491 ((not (file-exists-p nndiary-current-directory
))
492 (nnheader-report 'nndiary
"Directory %s does not exist"
493 nndiary-current-directory
))
494 ((not (file-directory-p nndiary-current-directory
))
495 (nnheader-report 'nndiary
"%s is not a directory"
496 nndiary-current-directory
))
498 (nnheader-report 'nndiary
"Group %s selected" group
)
501 (nnheader-re-read-dir nndiary-current-directory
)
502 (nnmail-activate 'nndiary
)
503 (let ((active (nth 1 (assoc group nndiary-group-alist
))))
505 (nnheader-report 'nndiary
"No such group: %s" group
)
506 (nnheader-report 'nndiary
"Selected group %s" group
)
507 (nnheader-insert "211 %d %d %d %s\n"
508 (max (1+ (- (cdr active
) (car active
))) 0)
509 (car active
) (cdr active
) group
)))))))
511 (deffoo nndiary-request-scan
(&optional group server
)
512 ;; Use our own mail sources and split methods while Gnus doesn't let us have
513 ;; multiple back ends for retrieving mail.
514 (let ((mail-sources nndiary-mail-sources
)
515 (nnmail-split-methods nndiary-split-methods
))
516 (setq nndiary-article-file-alist nil
)
517 (nndiary-possibly-change-directory group server
)
518 (nnmail-get-new-mail 'nndiary
'nndiary-save-nov nndiary-directory group
)))
520 (deffoo nndiary-close-group
(group &optional server
)
521 (setq nndiary-article-file-alist nil
)
524 (deffoo nndiary-request-create-group
(group &optional server args
)
525 (nndiary-possibly-change-directory nil server
)
526 (nnmail-activate 'nndiary
)
528 ((assoc group nndiary-group-alist
)
530 ((and (file-exists-p (nnmail-group-pathname group nndiary-directory
))
531 (not (file-directory-p (nnmail-group-pathname
532 group nndiary-directory
))))
533 (nnheader-report 'nndiary
"%s is a file"
534 (nnmail-group-pathname group nndiary-directory
)))
537 (push (list group
(setq active
(cons 1 0)))
539 (nndiary-possibly-create-directory group
)
540 (nndiary-possibly-change-directory group server
)
541 (let ((articles (nnheader-directory-articles nndiary-current-directory
)))
543 (setcar active
(apply 'min articles
))
544 (setcdr active
(apply 'max articles
))))
545 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
546 (run-hook-with-args 'nndiary-request-create-group-hooks
547 (gnus-group-prefixed-name group
548 (list "nndiary" server
)))
552 (deffoo nndiary-request-list
(&optional server
)
554 (let ((nnmail-file-coding-system nnmail-active-file-coding-system
)
555 (file-name-coding-system nnmail-pathname-coding-system
))
556 (nnmail-find-file nndiary-active-file
))
557 (setq nndiary-group-alist
(nnmail-get-active))
560 (deffoo nndiary-request-newgroups
(date &optional server
)
561 (nndiary-request-list server
))
563 (deffoo nndiary-request-list-newsgroups
(&optional server
)
565 (nnmail-find-file nndiary-newsgroups-file
)))
567 (deffoo nndiary-request-expire-articles
(articles group
&optional server force
)
568 (nndiary-possibly-change-directory group server
)
569 (let ((active-articles
570 (nnheader-directory-articles nndiary-current-directory
))
572 (nnmail-activate 'nndiary
)
573 ;; Articles not listed in active-articles are already gone,
574 ;; so don't try to expire them.
575 (setq articles
(gnus-intersection articles active-articles
))
577 (setq article
(nndiary-article-to-file (setq number
(pop articles
))))
578 (if (and (nndiary-deletable-article-p group number
)
579 ;; Don't use nnmail-expired-article-p. Our notion of expiration
580 ;; is a bit peculiar ...
581 (or force
(nndiary-expired-article-p article
)))
583 ;; Allow a special target group.
584 (unless (eq nnmail-expiry-target
'delete
)
586 (nndiary-request-article number group server
(current-buffer))
587 (let ((nndiary-current-directory nil
))
588 (nnmail-expiry-target-group nnmail-expiry-target group
)))
589 (nndiary-possibly-change-directory group server
))
590 (nnheader-message 5 "Deleting article %s in %s" number group
)
592 (funcall nnmail-delete-file-function article
)
593 (file-error (push number rest
)))
594 (setq active-articles
(delq number active-articles
))
595 (nndiary-nov-delete-article group number
))
597 (let ((active (nth 1 (assoc group nndiary-group-alist
))))
599 (setcar active
(or (and active-articles
600 (apply 'min active-articles
))
602 (nnmail-save-active nndiary-group-alist nndiary-active-file
))
604 (nconc rest articles
)))
606 (deffoo nndiary-request-move-article
607 (article group server accept-form
&optional last move-is-internal
)
608 (let ((buf (get-buffer-create " *nndiary move*"))
610 (nndiary-possibly-change-directory group server
)
611 (nndiary-update-file-alist)
613 (nndiary-deletable-article-p group article
)
614 (nndiary-request-article article group server
)
615 (let (nndiary-current-directory
616 nndiary-current-group
617 nndiary-article-file-alist
)
620 (insert-buffer-substring nntp-server-buffer
)
621 (setq result
(eval accept-form
))
622 (kill-buffer (current-buffer))
625 (nndiary-possibly-change-directory group server
)
627 (funcall nnmail-delete-file-function
628 (nndiary-article-to-file article
))
630 (nndiary-nov-delete-article group article
)
633 (nnmail-save-active nndiary-group-alist nndiary-active-file
))))
636 (deffoo nndiary-request-accept-article
(group &optional server last
)
637 (nndiary-possibly-change-directory group server
)
638 (nnmail-check-syntax)
639 (run-hooks 'nndiary-request-accept-article-hooks
)
640 (when (nndiary-schedule)
642 (when nnmail-cache-accepted-message-ids
643 (nnmail-cache-insert (nnmail-fetch-field "message-id")
645 (nnmail-fetch-field "subject")))
648 (nnmail-activate 'nndiary
)
650 (car (nndiary-save-mail
651 (list (cons group
(nndiary-active-number group
))))))
653 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
654 (and last
(nndiary-save-nov))))
656 (nnmail-activate 'nndiary
)
657 (if (and (not (setq result
658 (nnmail-article-group 'nndiary-active-number
)))
659 (yes-or-no-p "Moved to `junk' group; delete article? "))
661 (setq result
(car (nndiary-save-mail result
))))
663 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
664 (when nnmail-cache-accepted-message-ids
665 (nnmail-cache-close))
666 (nndiary-save-nov))))
670 (deffoo nndiary-request-post
(&optional server
)
671 (nnmail-do-request-post 'nndiary-request-accept-article server
))
673 (deffoo nndiary-request-replace-article
(article group buffer
)
674 (nndiary-possibly-change-directory group
)
677 (nndiary-possibly-create-directory group
)
678 (let ((chars (nnmail-insert-lines))
679 (art (concat (int-to-string article
) "\t"))
683 (point-min) (point-max)
684 (or (nndiary-article-to-file article
)
685 (expand-file-name (int-to-string article
)
686 nndiary-current-directory
))
687 nil
(if (nnheader-be-verbose 5) nil
'nomesg
))
689 (setq headers
(nndiary-parse-head chars article
))
690 ;; Replace the NOV line in the NOV file.
692 (set-buffer (nndiary-open-nov group
))
693 (goto-char (point-min))
694 (if (or (looking-at art
)
695 (search-forward (concat "\n" art
) nil t
))
696 ;; Delete the old NOV line.
697 (delete-region (progn (beginning-of-line) (point))
698 (progn (forward-line 1) (point)))
699 ;; The line isn't here, so we have to find out where
700 ;; we should insert it. (This situation should never
701 ;; occur, but one likes to make sure...)
702 (while (and (looking-at "[0-9]+\t")
705 (match-beginning 0) (match-end 0)))
707 (zerop (forward-line 1)))))
709 (nnheader-insert-nov headers
)
713 (deffoo nndiary-request-delete-group
(group &optional force server
)
714 (nndiary-possibly-change-directory group server
)
716 ;; Delete all articles in GROUP.
719 nndiary-current-directory t
720 (concat nnheader-numerical-short-files
721 "\\|" (regexp-quote nndiary-nov-file-name
) "$")))
724 (setq article
(pop articles
))
725 (when (file-writable-p article
)
726 (nnheader-message 5 "Deleting article %s in %s..." article group
)
727 (funcall nnmail-delete-file-function article
))))
728 ;; Try to delete the directory itself.
729 (ignore-errors (delete-directory nndiary-current-directory
)))
730 ;; Remove the group from all structures.
731 (setq nndiary-group-alist
732 (delq (assoc group nndiary-group-alist
) nndiary-group-alist
)
733 nndiary-current-group nil
734 nndiary-current-directory nil
)
735 ;; Save the active file.
736 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
739 (deffoo nndiary-request-rename-group
(group new-name
&optional server
)
740 (nndiary-possibly-change-directory group server
)
741 (let ((new-dir (nnmail-group-pathname new-name nndiary-directory
))
742 (old-dir (nnmail-group-pathname group nndiary-directory
)))
744 (make-directory new-dir t
)
746 ;; We move the articles file by file instead of renaming
747 ;; the directory -- there may be subgroups in this group.
748 ;; One might be more clever, I guess.
749 (let ((files (nnheader-article-to-file-alist old-dir
)))
752 (concat old-dir
(cdar files
))
753 (concat new-dir
(cdar files
)))
755 ;; Move .overview file.
756 (let ((overview (concat old-dir nndiary-nov-file-name
)))
757 (when (file-exists-p overview
)
758 (rename-file overview
(concat new-dir nndiary-nov-file-name
))))
759 (when (<= (length (directory-files old-dir
)) 2)
760 (ignore-errors (delete-directory old-dir
)))
761 ;; That went ok, so we change the internal structures.
762 (let ((entry (assoc group nndiary-group-alist
)))
764 (setcar entry new-name
))
765 (setq nndiary-current-directory nil
766 nndiary-current-group nil
)
767 ;; Save the new group alist.
768 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
771 (deffoo nndiary-set-status
(article name value
&optional group server
)
772 (nndiary-possibly-change-directory group server
)
773 (let ((file (nndiary-article-to-file article
)))
775 ((not (file-exists-p file
))
776 (nnheader-report 'nndiary
"File %s does not exist" file
))
779 (nnheader-insert-file-contents file
)
780 (nnmail-replace-status name value
))
784 ;;; Interface optional functions ============================================
786 (deffoo nndiary-request-update-info
(group info
&optional server
)
787 (nndiary-possibly-change-directory group
)
788 (let ((timestamp (gnus-group-parameter-value (gnus-info-params info
)
791 (nnheader-report 'nndiary
"Group %s doesn't have a timestamp" group
)
793 ;; Figure out which articles should be re-new'ed
794 (let ((articles (nndiary-flatten (gnus-info-read info
) 0))
795 article file unread buf
)
797 (setq buf
(nnheader-set-temp-buffer " *nndiary update*"))
798 (while (setq article
(pop articles
))
799 (setq file
(concat nndiary-current-directory
800 (int-to-string article
)))
801 (and (file-exists-p file
)
802 (nndiary-renew-article-p file timestamp
)
803 (push article unread
)))
804 ;;(message "unread: %s" unread)
807 (setq unread
(sort unread
'<))
809 (gnus-info-set-read info
(gnus-update-read-articles
810 (gnus-info-group info
) unread t
)))
812 (run-hook-with-args 'nndiary-request-update-info-hooks
813 (gnus-info-group info
))
818 ;;; Internal functions ======================================================
820 (defun nndiary-article-to-file (article)
821 (nndiary-update-file-alist)
823 (if (setq file
(cdr (assq article nndiary-article-file-alist
)))
824 (expand-file-name file nndiary-current-directory
)
825 ;; Just to make sure nothing went wrong when reading over NFS --
827 (if nndiary-check-directory-twice
829 (setq file
(expand-file-name (number-to-string article
)
830 nndiary-current-directory
)))
831 (nndiary-update-file-alist t
)
834 (defun nndiary-deletable-article-p (group article
)
835 "Say whether ARTICLE in GROUP can be deleted."
837 (when (setq path
(nndiary-article-to-file article
))
838 (when (file-writable-p path
)
839 (or (not nnmail-keep-last-article
)
840 (not (eq (cdr (nth 1 (assoc group nndiary-group-alist
)))
843 ;; Find an article number in the current group given the Message-ID.
844 (defun nndiary-find-group-number (id)
846 (set-buffer (get-buffer-create " *nndiary id*"))
847 (let ((alist nndiary-group-alist
)
849 ;; We want to look through all .overview files, but we want to
850 ;; start with the one in the current directory. It seems most
851 ;; likely that the article we are looking for is in that group.
852 (if (setq number
(nndiary-find-id nndiary-current-group id
))
853 (cons nndiary-current-group number
)
854 ;; It wasn't there, so we look through the other groups as well.
855 (while (and (not number
)
857 (or (string= (caar alist
) nndiary-current-group
)
858 (setq number
(nndiary-find-id (caar alist
) id
)))
860 (setq alist
(cdr alist
))))
862 (cons (caar alist
) number
))))))
864 (defun nndiary-find-id (group id
)
866 (let ((nov (expand-file-name nndiary-nov-file-name
867 (nnmail-group-pathname group
870 (when (file-exists-p nov
)
871 (nnheader-insert-file-contents nov
)
872 (while (and (not found
)
873 (search-forward id nil t
)) ; We find the ID.
874 ;; And the id is in the fourth field.
875 (if (not (and (search-backward "\t" nil t
4)
876 (not (search-backward"\t" (point-at-bol) t
))))
880 ;; We return the article number.
882 (ignore-errors (read (current-buffer))))))
885 (defun nndiary-retrieve-headers-with-nov (articles &optional fetch-old
)
886 (if (or gnus-nov-is-evil nndiary-nov-is-evil
)
888 (let ((nov (expand-file-name nndiary-nov-file-name
889 nndiary-current-directory
)))
890 (when (file-exists-p nov
)
892 (set-buffer nntp-server-buffer
)
894 (nnheader-insert-file-contents nov
)
896 (not (numberp fetch-old
)))
897 t
; Don't remove anything.
898 (nnheader-nov-delete-outside-range
899 (if fetch-old
(max 1 (- (car articles
) fetch-old
))
901 (car (last articles
)))
904 (defun nndiary-possibly-change-directory (group &optional server
)
906 (not (nndiary-server-opened server
)))
907 (nndiary-open-server server
))
910 (let ((pathname (nnmail-group-pathname group nndiary-directory
))
911 (file-name-coding-system nnmail-pathname-coding-system
))
912 (when (not (equal pathname nndiary-current-directory
))
913 (setq nndiary-current-directory pathname
914 nndiary-current-group group
915 nndiary-article-file-alist nil
))
916 (file-exists-p nndiary-current-directory
))))
918 (defun nndiary-possibly-create-directory (group)
919 (let ((dir (nnmail-group-pathname group nndiary-directory
)))
920 (unless (file-exists-p dir
)
921 (make-directory (directory-file-name dir
) t
)
922 (nnheader-message 5 "Creating mail directory %s" dir
))))
924 (defun nndiary-save-mail (group-art)
925 "Called narrowed to an article."
927 (setq chars
(nnmail-insert-lines))
928 (nnmail-insert-xref group-art
)
929 (run-hooks 'nnmail-prepare-save-mail-hook
)
930 (run-hooks 'nndiary-prepare-save-mail-hook
)
931 (goto-char (point-min))
932 (while (looking-at "From ")
933 (replace-match "X-From-Line: ")
935 ;; We save the article in all the groups it belongs in.
939 (nndiary-possibly-create-directory (caar ga
))
940 (let ((file (concat (nnmail-group-pathname
941 (caar ga
) nndiary-directory
)
942 (int-to-string (cdar ga
)))))
944 ;; It was already saved, so we just make a hard link.
945 (funcall nnmail-crosspost-link-function first file t
)
947 (nnmail-write-region (point-min) (point-max) file nil
948 (if (nnheader-be-verbose 5) nil
'nomesg
))
951 ;; Generate a nov line for this article. We generate the nov
952 ;; line after saving, because nov generation destroys the
954 (setq headers
(nndiary-parse-head chars
))
955 ;; Output the nov line to all nov databases that should have it.
956 (let ((ga group-art
))
958 (nndiary-add-nov (caar ga
) (cdar ga
) headers
)
962 (defun nndiary-active-number (group)
963 "Compute the next article number in GROUP."
964 (let ((active (cadr (assoc group nndiary-group-alist
))))
965 ;; The group wasn't known to nndiary, so we just create an active
968 ;; Perhaps the active file was corrupt? See whether
969 ;; there are any articles in this group.
970 (nndiary-possibly-create-directory group
)
971 (nndiary-possibly-change-directory group
)
972 (unless nndiary-article-file-alist
973 (setq nndiary-article-file-alist
975 (nnheader-article-to-file-alist nndiary-current-directory
)
976 'car-less-than-car
)))
978 (if nndiary-article-file-alist
979 (cons (caar nndiary-article-file-alist
)
980 (caar (last nndiary-article-file-alist
)))
982 (push (list group active
) nndiary-group-alist
))
983 (setcdr active
(1+ (cdr active
)))
984 (while (file-exists-p
985 (expand-file-name (int-to-string (cdr active
))
986 (nnmail-group-pathname group nndiary-directory
)))
987 (setcdr active
(1+ (cdr active
))))
990 (defun nndiary-add-nov (group article headers
)
991 "Add a nov line for the GROUP base."
993 (set-buffer (nndiary-open-nov group
))
994 (goto-char (point-max))
995 (mail-header-set-number headers article
)
996 (nnheader-insert-nov headers
)))
998 (defsubst nndiary-header-value
()
999 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
1001 (defun nndiary-parse-head (chars &optional number
)
1002 "Parse the head of the current buffer."
1005 (unless (zerop (buffer-size))
1007 (goto-char (point-min))
1008 (if (search-forward "\n\n" nil t
) (1- (point)) (point-max))))
1009 (let ((headers (nnheader-parse-naked-head)))
1010 (mail-header-set-chars headers chars
)
1011 (mail-header-set-number headers number
)
1014 (defun nndiary-open-nov (group)
1015 (or (cdr (assoc group nndiary-nov-buffer-alist
))
1016 (let ((buffer (get-buffer-create (format " *nndiary overview %s*"
1020 (set (make-local-variable 'nndiary-nov-buffer-file-name
)
1022 nndiary-nov-file-name
1023 (nnmail-group-pathname group nndiary-directory
)))
1025 (when (file-exists-p nndiary-nov-buffer-file-name
)
1026 (nnheader-insert-file-contents nndiary-nov-buffer-file-name
)))
1027 (push (cons group buffer
) nndiary-nov-buffer-alist
)
1030 (defun nndiary-save-nov ()
1032 (while nndiary-nov-buffer-alist
1033 (when (buffer-name (cdar nndiary-nov-buffer-alist
))
1034 (set-buffer (cdar nndiary-nov-buffer-alist
))
1035 (when (buffer-modified-p)
1036 (nnmail-write-region 1 (point-max) nndiary-nov-buffer-file-name
1038 (set-buffer-modified-p nil
)
1039 (kill-buffer (current-buffer)))
1040 (setq nndiary-nov-buffer-alist
(cdr nndiary-nov-buffer-alist
)))))
1043 (defun nndiary-generate-nov-databases (&optional server
)
1044 "Generate NOV databases in all nndiary directories."
1045 (interactive (list (or (nnoo-current-server 'nndiary
) "")))
1046 ;; Read the active file to make sure we don't re-use articles
1047 ;; numbers in empty groups.
1048 (nnmail-activate 'nndiary
)
1049 (unless (nndiary-server-opened server
)
1050 (nndiary-open-server server
))
1051 (setq nndiary-directory
(expand-file-name nndiary-directory
))
1052 ;; Recurse down the directories.
1053 (nndiary-generate-nov-databases-1 nndiary-directory nil t
)
1054 ;; Save the active file.
1055 (nnmail-save-active nndiary-group-alist nndiary-active-file
))
1057 (defun nndiary-generate-nov-databases-1 (dir &optional seen no-active
)
1058 "Regenerate the NOV database in DIR."
1059 (interactive "DRegenerate NOV in: ")
1060 (setq dir
(file-name-as-directory dir
))
1061 ;; Only scan this sub-tree if we haven't been here yet.
1062 (unless (member (file-truename dir
) seen
)
1063 (push (file-truename dir
) seen
)
1064 ;; We descend recursively
1065 (let ((dirs (directory-files dir t nil t
))
1067 (while (setq dir
(pop dirs
))
1068 (when (and (not (string-match "^\\." (file-name-nondirectory dir
)))
1069 (file-directory-p dir
))
1070 (nndiary-generate-nov-databases-1 dir seen
))))
1071 ;; Do this directory.
1072 (let ((files (sort (nnheader-article-to-file-alist dir
)
1073 'car-less-than-car
)))
1075 (let* ((group (nnheader-file-to-group
1076 (directory-file-name dir
) nndiary-directory
))
1077 (info (cadr (assoc group nndiary-group-alist
))))
1079 (setcar info
(1+ (cdr info
)))))
1080 (funcall nndiary-generate-active-function dir
)
1081 ;; Generate the nov file.
1082 (nndiary-generate-nov-file dir files
)
1084 (nnmail-save-active nndiary-group-alist nndiary-active-file
))))))
1087 (defun nndiary-generate-active-info (dir)
1088 ;; Update the active info for this group.
1089 (let* ((group (nnheader-file-to-group
1090 (directory-file-name dir
) nndiary-directory
))
1091 (entry (assoc group nndiary-group-alist
))
1092 (last (or (caadr entry
) 0)))
1093 (setq nndiary-group-alist
(delq entry nndiary-group-alist
))
1095 (cons (or (caar files
) (1+ last
))
1097 (or (caar (last files
))
1099 nndiary-group-alist
)))
1101 (defun nndiary-generate-nov-file (dir files
)
1102 (let* ((dir (file-name-as-directory dir
))
1103 (nov (concat dir nndiary-nov-file-name
))
1104 (nov-buffer (get-buffer-create " *nov*"))
1107 ;; Init the nov buffer.
1108 (set-buffer nov-buffer
)
1109 (buffer-disable-undo)
1111 (set-buffer nntp-server-buffer
)
1112 ;; Delete the old NOV file.
1113 (when (file-exists-p nov
)
1114 (funcall nnmail-delete-file-function nov
))
1116 (unless (file-directory-p (setq file
(concat dir
(cdar files
))))
1118 (nnheader-insert-file-contents file
)
1120 (goto-char (point-min))
1122 (search-forward "\n\n" nil t
)
1123 (setq chars
(- (point-max) (point)))
1124 (max 1 (1- (point)))))
1125 (unless (zerop (buffer-size))
1126 (goto-char (point-min))
1127 (setq headers
(nndiary-parse-head chars
(caar files
)))
1129 (set-buffer nov-buffer
)
1130 (goto-char (point-max))
1131 (nnheader-insert-nov headers
)))
1133 (setq files
(cdr files
)))
1135 (set-buffer nov-buffer
)
1136 (nnmail-write-region 1 (point-max) nov nil
'nomesg
)
1137 (kill-buffer (current-buffer))))))
1139 (defun nndiary-nov-delete-article (group article
)
1141 (set-buffer (nndiary-open-nov group
))
1142 (when (nnheader-find-nov-line article
)
1143 (delete-region (point) (progn (forward-line 1) (point)))
1145 (let ((active (cadr (assoc group nndiary-group-alist
)))
1149 (setf (car active
) (1+ (cdr active
)))
1150 (when (and (setq num
(ignore-errors (read (current-buffer))))
1152 (setf (car active
) num
)))))))
1155 (defun nndiary-update-file-alist (&optional force
)
1156 (when (or (not nndiary-article-file-alist
)
1158 (setq nndiary-article-file-alist
1159 (nnheader-article-to-file-alist nndiary-current-directory
))))
1162 (defun nndiary-string-to-number (str min
&optional max
)
1163 ;; Like `string-to-number' but barf if STR is not exactly an integer, and not
1164 ;; within the specified bounds.
1165 ;; Signals are caught by `nndiary-schedule'.
1166 (if (not (string-match "^[ \t]*[0-9]+[ \t]*$" str
))
1167 (nndiary-error "not an integer value")
1169 (let ((val (string-to-number str
)))
1170 (and (or (< val min
)
1171 (and max
(> val max
)))
1172 (nndiary-error "value out of range"))
1175 (defun nndiary-parse-schedule-value (str min-or-values max
)
1176 ;; Parse the schedule string STR, or signal an error.
1177 ;; Signals are caught by `nndary-schedule'.
1178 (if (string-match "[ \t]*\\*[ \t]*" str
)
1182 (if (listp min-or-values
)
1183 ;; min-or-values is values
1184 ;; #### NOTE: this is actually only a hack for time zones.
1185 (let ((val (and (string-match "[ \t]*\\([^ \t]+\\)[ \t]*" str
)
1186 (match-string 1 str
))))
1187 (if (and val
(setq val
(assoc val min-or-values
)))
1189 (nndiary-error "invalid syntax")))
1190 ;; min-or-values is min
1193 (let ((res (split-string val
"-")))
1196 (nndiary-string-to-number (car res
) min-or-values max
))
1198 ;; don't know if crontab accepts this, but ensure
1199 ;; that BEG is <= END
1200 (let ((beg (nndiary-string-to-number (car res
) min-or-values max
))
1201 (end (nndiary-string-to-number (cadr res
) min-or-values max
)))
1209 (nndiary-error "invalid syntax")))
1211 (split-string str
",")))
1214 ;; ### FIXME: remove this function if it's used only once.
1215 (defun nndiary-parse-schedule (head min-or-values max
)
1216 ;; Parse the cron-like value of header X-Diary-HEAD in current buffer.
1217 ;; - Returns nil if `*'
1218 ;; - Otherwise returns a list of integers and/or ranges (BEG . END)
1219 ;; The exception is the Timze-Zone value which is always of the form (STR).
1220 ;; Signals are caught by `nndary-schedule'.
1221 (let ((header (format "^X-Diary-%s: \\(.*\\)$" head
)))
1222 (goto-char (point-min))
1223 (if (not (re-search-forward header nil t
))
1224 (nndiary-error "header missing")
1226 (nndiary-parse-schedule-value (match-string 1) min-or-values max
))
1229 (defun nndiary-max (spec)
1230 ;; Returns the max of specification SPEC, or nil for permanent schedules.
1235 (while (setq elt
(pop elts
))
1237 (and (> elt max
) (setq max elt
))
1238 (and (> (cdr elt
) max
) (setq max
(cdr elt
)))))
1241 (defun nndiary-flatten (spec min
&optional max
)
1242 ;; flatten the spec by expanding ranges to all possible values.
1245 ;; this happens when I flatten something else than one of my
1246 ;; schedules (a list of read articles for instance).
1255 (while (setq elt
(pop elts
))
1260 (while (<= n
(cdr elt
))
1262 (setq n
(1+ n
))))))))
1265 (defun nndiary-unflatten (spec)
1266 ;; opposite of flatten: build ranges if possible
1267 (setq spec
(sort spec
'<))
1269 (while (setq min
(pop spec
))
1271 (while (and (car spec
) (= (car spec
) (1+ max
)))
1275 (setq res
(append res
(list min
)))
1276 (setq res
(append res
(list (cons min max
))))))
1279 (defun nndiary-compute-reminders (date)
1280 ;; Returns a list of times corresponding to the reminders of date DATE.
1281 ;; See the comment in `nndiary-reminders' about rounding.
1282 (let* ((reminders nndiary-reminders
)
1283 (date-elts (decode-time date
))
1284 ;; ### NOTE: out-of-range values are accepted by encode-time. This
1285 ;; makes our life easier.
1286 (monday (- (nth 3 date-elts
)
1287 (if nndiary-week-starts-on-monday
1288 (if (zerop (nth 6 date-elts
))
1290 (- (nth 6 date-elts
) 1))
1291 (nth 6 date-elts
))))
1293 ;; remove the DOW and DST entries
1294 (setcdr (nthcdr 5 date-elts
) (nthcdr 8 date-elts
))
1295 (while (setq reminder
(pop reminders
))
1297 (cond ((eq (cdr reminder
) 'minute
)
1299 (apply 'encode-time
0 (nthcdr 1 date-elts
))
1300 (seconds-to-time (* (car reminder
) 60.0))))
1301 ((eq (cdr reminder
) 'hour
)
1303 (apply 'encode-time
0 0 (nthcdr 2 date-elts
))
1304 (seconds-to-time (* (car reminder
) 3600.0))))
1305 ((eq (cdr reminder
) 'day
)
1307 (apply 'encode-time
0 0 0 (nthcdr 3 date-elts
))
1308 (seconds-to-time (* (car reminder
) 86400.0))))
1309 ((eq (cdr reminder
) 'week
)
1311 (apply 'encode-time
0 0 0 monday
(nthcdr 4 date-elts
))
1312 (seconds-to-time (* (car reminder
) 604800.0))))
1313 ((eq (cdr reminder
) 'month
)
1315 (apply 'encode-time
0 0 0 1 (nthcdr 4 date-elts
))
1316 (seconds-to-time (* (car reminder
) 18748800.0))))
1317 ((eq (cdr reminder
) 'year
)
1319 (apply 'encode-time
0 0 0 1 1 (nthcdr 5 date-elts
))
1320 (seconds-to-time (* (car reminder
) 400861056.0)))))
1322 (sort res
'time-less-p
)))
1324 (defun nndiary-last-occurence (sched)
1325 ;; Returns the last occurence of schedule SCHED as an Emacs time struct, or
1326 ;; nil for permanent schedule or errors.
1327 (let ((minute (nndiary-max (nth 0 sched
)))
1328 (hour (nndiary-max (nth 1 sched
)))
1329 (year (nndiary-max (nth 4 sched
)))
1330 (time-zone (or (and (nth 6 sched
) (car (nth 6 sched
)))
1331 (current-time-zone))))
1333 (or minute
(setq minute
59))
1334 (or hour
(setq hour
23))
1335 ;; I'll just compute all possible values and test them by decreasing
1336 ;; order until one succeeds. This is probably quide rude, but I got
1337 ;; bored in finding a good algorithm for doing that ;-)
1338 ;; ### FIXME: remove identical entries.
1339 (let ((dom-list (nth 2 sched
))
1340 (month-list (sort (nndiary-flatten (nth 3 sched
) 1 12) '>))
1341 (year-list (sort (nndiary-flatten (nth 4 sched
) 1971) '>))
1342 (dow-list (nth 5 sched
)))
1343 ;; Special case: an asterisk in one of the days specifications means
1344 ;; that only the other should be taken into account. If both are
1345 ;; unspecified, you would get all possible days in both.
1346 (cond ((null dow-list
)
1347 ;; this gets all days if dom-list is nil
1348 (setq dom-list
(nndiary-flatten dom-list
1 31)))
1350 ;; this also gets all days if dow-list is nil
1351 (setq dow-list
(nndiary-flatten dow-list
0 6)))
1353 (setq dom-list
(nndiary-flatten dom-list
1 31))
1354 (setq dow-list
(nndiary-flatten dow-list
0 6))))
1357 (while (setq year
(pop year-list
))
1358 (let ((months month-list
)
1360 (while (setq month
(pop months
))
1361 ;; Now we must merge the Dows with the Doms. To do that, we
1362 ;; have to know which day is the 1st one for this month.
1363 ;; Maybe there's simpler, but decode-time(encode-time) will
1364 ;; give us the answer.
1365 (let ((first (nth 6 (decode-time
1366 (encode-time 0 0 0 1 month year
1368 (max (cond ((= month
2)
1369 (if (date-leap-year-p year
) 29 28))
1371 (if (zerop (% month
2)) 30 31))
1373 (if (zerop (% month
2)) 31 30))))
1377 ;; first, review the doms to see if they are valid.
1378 (while (setq day
(pop doms
))
1381 ;; second add all possible dows
1382 (while (setq day
(pop dows
))
1384 (setq day
(1+ (- day first
)))
1385 (and (< day
0) (setq day
(+ 7 day
)))
1388 (setq day
(+ 7 day
))))
1389 ;; Finally, if we have some days, they are valid
1393 (encode-time 0 minute hour
1394 (car days
) month year time-zone
)))
1396 ;; There's an upper limit, but we didn't find any last occurence.
1397 ;; This means that the schedule is undecidable. This can happen if
1398 ;; you happen to say something like "each Feb 31 until 2038".
1400 (nnheader-report 'nndiary
"Undecidable schedule")
1404 (defun nndiary-next-occurence (sched now
)
1405 ;; Returns the next occurence of schedule SCHED, starting from time NOW.
1406 ;; If there's no next occurence, returns the last one (if any) which is then
1408 (let* ((today (decode-time now
))
1409 (this-minute (nth 1 today
))
1410 (this-hour (nth 2 today
))
1411 (this-day (nth 3 today
))
1412 (this-month (nth 4 today
))
1413 (this-year (nth 5 today
))
1414 (minute-list (sort (nndiary-flatten (nth 0 sched
) 0 59) '<))
1415 (hour-list (sort (nndiary-flatten (nth 1 sched
) 0 23) '<))
1416 (dom-list (nth 2 sched
))
1417 (month-list (sort (nndiary-flatten (nth 3 sched
) 1 12) '<))
1418 (years (if (nth 4 sched
)
1419 (sort (nndiary-flatten (nth 4 sched
) 1971) '<)
1421 (dow-list (nth 5 sched
))
1422 (year (1- this-year
))
1423 (time-zone (or (and (nth 6 sched
) (car (nth 6 sched
)))
1424 (current-time-zone))))
1425 ;; Special case: an asterisk in one of the days specifications means that
1426 ;; only the other should be taken into account. If both are unspecified,
1427 ;; you would get all possible days in both.
1428 (cond ((null dow-list
)
1429 ;; this gets all days if dom-list is nil
1430 (setq dom-list
(nndiary-flatten dom-list
1 31)))
1432 ;; this also gets all days if dow-list is nil
1433 (setq dow-list
(nndiary-flatten dow-list
0 6)))
1435 (setq dom-list
(nndiary-flatten dom-list
1 31))
1436 (setq dow-list
(nndiary-flatten dow-list
0 6))))
1437 ;; Remove past years.
1438 (unless (eq years t
)
1439 (while (and (car years
) (< (car years
) this-year
))
1442 ;; Because we might not be limited in years, we must guard against
1443 ;; infinite loops. Appart from cases like Feb 31, there are probably
1444 ;; other ones, (no monday XXX 2nd etc). I don't know any algorithm to
1445 ;; decide this, so I assume that if we reach 10 years later, the
1446 ;; schedule is undecidable.
1449 (while (if (eq years t
)
1450 (and (setq year
(1+ year
))
1451 (<= year
(+ 10 this-year
)))
1452 (setq year
(pop years
)))
1453 (let ((months month-list
)
1455 ;; Remove past months for this year.
1456 (and (= year this-year
)
1457 (while (and (car months
) (< (car months
) this-month
))
1459 (while (setq month
(pop months
))
1460 ;; Now we must merge the Dows with the Doms. To do that, we
1461 ;; have to know which day is the 1st one for this month.
1462 ;; Maybe there's simpler, but decode-time(encode-time) will
1463 ;; give us the answer.
1464 (let ((first (nth 6 (decode-time
1465 (encode-time 0 0 0 1 month year
1467 (max (cond ((= month
2)
1468 (if (date-leap-year-p year
) 29 28))
1470 (if (zerop (% month
2)) 30 31))
1472 (if (zerop (% month
2)) 31 30))))
1476 ;; first, review the doms to see if they are valid.
1477 (while (setq day
(pop doms
))
1480 ;; second add all possible dows
1481 (while (setq day
(pop dows
))
1483 (setq day
(1+ (- day first
)))
1484 (and (< day
0) (setq day
(+ 7 day
)))
1487 (setq day
(+ 7 day
))))
1488 ;; Aaaaaaall right. Now we have a valid list of DAYS for
1489 ;; this month and this year.
1491 (setq days
(sort days
'<))
1492 ;; Remove past days for this year and this month.
1493 (and (= year this-year
)
1494 (= month this-month
)
1495 (while (and (car days
) (< (car days
) this-day
))
1497 (while (setq day
(pop days
))
1498 (let ((hours hour-list
)
1500 ;; Remove past hours for this year, this month and
1502 (and (= year this-year
)
1503 (= month this-month
)
1505 (while (and (car hours
)
1506 (< (car hours
) this-hour
))
1508 (while (setq hour
(pop hours
))
1509 (let ((minutes minute-list
)
1511 ;; Remove past hours for this year, this month,
1512 ;; this day and this hour.
1513 (and (= year this-year
)
1514 (= month this-month
)
1517 (while (and (car minutes
)
1518 (< (car minutes
) this-minute
))
1520 (while (setq minute
(pop minutes
))
1521 ;; Ouch! Here, we've got a complete valid
1522 ;; schedule. It's a good one if it's in the
1524 (let ((time (encode-time 0 minute hour day
1527 (and (time-less-p now time
)
1528 (throw 'found time
)))
1533 (nndiary-last-occurence sched
))
1535 (nndiary-last-occurence sched
))
1538 (defun nndiary-expired-article-p (file)
1540 (if (nnheader-insert-head file
)
1541 (let ((sched (nndiary-schedule)))
1542 ;; An article has expired if its last schedule (if any) is in the
1543 ;; past. A permanent schedule never expires.
1545 (setq sched
(nndiary-last-occurence sched
))
1546 (time-less-p sched
(current-time))))
1548 (nnheader-report 'nndiary
"Could not read file %s" file
)
1552 (defun nndiary-renew-article-p (file timestamp
)
1554 (if (nnheader-insert-head file
)
1555 (let ((now (current-time))
1556 (sched (nndiary-schedule)))
1557 ;; The article should be re-considered as unread if there's a reminder
1558 ;; between the group timestamp and the current time.
1559 (when (and sched
(setq sched
(nndiary-next-occurence sched now
)))
1560 (let ((reminders ;; add the next occurence itself at the end.
1561 (append (nndiary-compute-reminders sched
) (list sched
))))
1562 (while (and reminders
(time-less-p (car reminders
) timestamp
))
1564 ;; The reminders might be empty if the last date is in the past,
1565 ;; or we've got at least the next occurence itself left. All past
1566 ;; dates are renewed.
1568 (time-less-p (car reminders
) now
)))
1571 (nnheader-report 'nndiary
"Could not read file %s" file
)
1574 ;; The end... ===============================================================
1576 (dolist (header nndiary-headers
)
1577 (setq header
(intern (format "X-Diary-%s" (car header
))))
1578 ;; Required for building NOV databases and some other stuff.
1579 (add-to-list 'gnus-extra-headers header
)
1580 (add-to-list 'nnmail-extra-headers header
))
1582 (unless (assoc "nndiary" gnus-valid-select-methods
)
1583 (gnus-declare-backend "nndiary" 'post-mail
'respool
'address
))
1588 ;; arch-tag: 9c542b95-92e7-4ace-a038-330ab296e203
1589 ;;; nndiary.el ends here