1 ;;; nndiary.el --- A diary backend for Gnus
3 ;; Copyright (C) 1999-2001 Didier Verna.
5 ;; PRCS: $Id: nndiary.el 1.22 Tue, 04 Sep 2001 11:32:13 +0200 didier $
7 ;; Author: Didier Verna <didier@xemacs.org>
8 ;; Maintainer: Didier Verna <didier@xemacs.org>
9 ;; Created: Fri Jul 16 18:55:42 1999
10 ;; Last Revision: Wed Aug 8 17:36:21 2001
11 ;; Keywords: calendar mail news
13 ;; This file is part of NNDiary.
15 ;; NNDiary is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2 of the License, or
18 ;; (at your option) any later version.
20 ;; NNDiary is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with this program; if not, write to the Free Software
27 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 ;; Contents management by FCM version 0.1.
37 ;; This package implements NNDiary, a diary backend for Gnus. NNDiary is a
38 ;; mail backend, pretty similar to nnml in its functionnning (it has all the
39 ;; features of nnml, actually), but in which messages are treated as event
42 ;; Here is a typical scenario:
43 ;; - You've got a date with Andy Mc Dowell or Bruce Willis (select according
44 ;; to your sexual preference) in one month. You don't want to forget it.
45 ;; - Send a (special) diary message to yourself (see below).
46 ;; - Forget all about it and keep on getting and reading new mail, as usual.
47 ;; - From time to time, as you type `g' in the group buffer and as the date
48 ;; is getting closer, the message will pop up again, just like if it were
50 ;; - Read your "new" messages, this one included, and start dreaming of the
51 ;; night you're gonna have.
52 ;; - Once the date is over (you actually fell asleep just after dinner), the
53 ;; message will be automatically deleted if it is marked as expirable.
55 ;; Some more notes on the diary backend:
56 ;; - NNDiary is a *real* mail backend. You *really* send real diary
57 ;; messsages. This means for instance that you can give appointements to
58 ;; anybody (provided they use Gnus and NNDiary) by sending the diary message
60 ;; - However, since NNDiary also has a 'request-post method, you can also
61 ;; `C-u a' instead of `C-u m' on a diary group and the message won't actually
62 ;; be sent; just stored in the group.
63 ;; - The events you want to remember need not be punctual. You can set up
64 ;; reminders for regular dates (like once each week, each monday at 13:30
65 ;; and so on). Diary messages of this kind will never be deleted (unless
66 ;; you do it explicitely). But that, you guessed.
72 ;; 1/ Diary messages contain several `X-Diary-*' special headers. You *must*
73 ;; arrange that these messages be split in a private folder *before* Gnus
74 ;; treat them. You need this because Gnus is not able yet to manage
75 ;; multiple backends for mail retrieval. Getting them from a separate
76 ;; source will compensate this misfeature to some extent, as we will see.
77 ;; As an example, here's my procmailrc entry to store diary files in
78 ;; ~/.nndiary (the default nndiary mail source file):
84 ;; 2/ Install nndiary somewhere Emacs / Gnus can find it. Normally, you
85 ;; *don't* have to '(require 'nndiary) anywhere. Gnus will do so when
86 ;; appropriate as long as nndiary is somewhere in the load path.
87 ;; 3/ Now, customize nndiary: type `M-x customize-group', and then `nndiary'
88 ;; at the prompt (note that if you have not restarted Emacs yet, you'll
89 ;; have to the load the library by hand before being able to customize it).
90 ;; In particular, you should customize the following options:
91 ;; - `nndiary-mail-sources', which overrides the normal `mail-sources'
92 ;; value for diary messages retrieving. It defaults to
93 ;; '(file :path "~/.nndiary").
94 ;; - `nndiary-split-methods', which overrides the normal
95 ;; `nnmail-split-methods' value for diary messages splitting. You can
96 ;; have all the diary groups you want (for example, I have a birthdays
97 ;; group, and stuff like that).
98 ;; - `nndiary-reminders', the list of times when you want to be reminded
99 ;; of your appointements (e.g. 3 weeks before, then 2 days before, then
100 ;; 1 hour before and that's it).
101 ;; 4/ You *must* use the group timestamp feature of Gnus. This adds a
102 ;; timestamp to each groups' parameters (please refer to the Gnus
103 ;; documentation ("Group Timestamp" info node) to see how it's done.
104 ;; 5/ Once you have done this, you may add a permanent nndiary virtual server
105 ;; (something like '(nndiary "")) to your `gnus-secondary-select-methods'.
106 ;; Yes, this server will be able to retrieve mails and split them when you
107 ;; type `g' in the group buffer, just as if it were your only mail backend.
108 ;; This is the benefit of using a private folder.
109 ;; 6/ Hopefully, almost everything (see the TODO section below) will work as
110 ;; expected when you restart Gnus: in the group buffer, `g' and `M-g' will
111 ;; also get your new diary mails, `F' will find your new diary groups etc.
114 ;; How to send diary messages:
115 ;; ==========================
117 ;; There are 7 special headers in diary messages. These headers are of the
118 ;; form `X-Diary-<something>', the <something> being one of `Minute', `Hour',
119 ;; `Dom', `Month', `Year', `Time-Zone' and `Dow'. `Dom' means "Day of Month",
120 ;; and `dow' means "Day of Week". These headers actually behave like crontab
121 ;; specifications and define the event date(s).
123 ;; For all headers but the `Time-Zone' one, a header value is either a
124 ;; star (meaning all possible values), or a list of fields (separated by a
125 ;; comma). A field is either an integer, or a range. A range is two integers
126 ;; separated by a dash. Possible integer values are 0-59 for `Minute', 0-23
127 ;; for `Hour', 1-31 for `Dom', `1-12' for Month, above 1971 for `Year' and 0-6
128 ;; for `Dow' (0 = sunday). As a special case, a star in either `Dom' or `Dow'
129 ;; doesn't mean "all possible values", but "use only the other field". Note
130 ;; that if both are star'ed, the use of either one gives the same result :-),
132 ;; The `Time-Zone' header is special in that it can have only one value (you
134 ;; A star doesn't mean "all possible values" (because it has no sense), but
135 ;; "the current local time zone".
137 ;; As an example, here's how you would say "Each Monday and each 1st of month,
138 ;; at 12:00, 20:00, 21:00, 22:00, 23:00 and 24:00, from 1999 to 2010" (I let
139 ;; you find what to do then):
142 ;; X-Diary-Hour: 12, 20-24
145 ;; X-Diary-Year: 1999-2010
147 ;; X-Diary-Time-Zone: *
150 ;; Sending a diary message is not different from sending any other kind of
151 ;; mail, except that such messages are identified by the presence of these
159 ;; * Respooling doesn't work because contrary to the request-scan function,
160 ;; Gnus won't allow me to override the split methods when calling the
161 ;; respooling backend functions.
162 ;; * The time zone mechanism is subject to change.
163 ;; * We could allow a keyword like `ask' in X-Diary-* headers, that would mean
164 ;; "ask for value upon reception of the message". Suggested by Jody Klymak.
165 ;; * We could add an optional header X-Diary-Reminders to specify a special
166 ;; reminders value for this message. Suggested by Jody Klymak.
167 ;; * Modify the request-accept-article function to make it prompt for diary
168 ;; headers if they're missing.
174 ;; NNDiary is very similar to nnml. This makes the idea of using nnoo (to
175 ;; derive nndiary from nnml) natural. However, my experience with nnoo is
176 ;; that for reasonably complex backends like this one, noo is a burden
177 ;; rather than an help. It's tricky to use, not everything can be
178 ;; inherited, what can be inherited and when is not very clear, and you've
179 ;; got to be very careful because a little mistake can fuck up your your
180 ;; other backends, especially because their variables will be use instead of
181 ;; your real ones. Finally, I found it easier to just clone the needed
182 ;; parts of nnml, and tracking nnml updates is not a big deal.
184 ;; IMHO, nnoo is actually badly designed. A much simpler, and yet more
185 ;; powerful one would be to make *real* functions and variables for a new
186 ;; backend based on another. Lisp is a reflexive language so that's a very
187 ;; easy thing to do: inspect the function's form, replace occurences of
188 ;; <nnfrom> (even in strings) with <nnto>, and you're done.
190 ;; * nndiary-get-new-mail, nndiary-mail-source and nndiary-split-methods:
191 ;; NNDiary has some experimental parts, in the sense Gnus normally uses only
192 ;; one mail backends for mail retreival and splitting. This backend is also
193 ;; an attempt to make it behave differently. For Gnus developpers: as you
194 ;; can see if you snarf into the code, that was not a very difficult thing
195 ;; to do. Something should be done about the respooling breakage though.
203 (eval-when-compile (require 'cl))
205 (require 'gnus-start)
208 ;; Compatibility Functions =================================================
210 (if (fboundp 'signal-error)
211 (defun nndiary-error (&rest args)
212 (apply #'signal-error 'nndiary args))
213 (defun nndiary-error (&rest args)
214 (apply #'error args)))
217 ;; Backend behavior customization ===========================================
219 (defgroup nndiary nil
220 "The Gnus Diary backend."
223 (defcustom nndiary-mail-sources
224 `((file :path ,(expand-file-name "~/.nndiary")))
225 "*NNDiary specific mail sources.
226 This variable is used by nndiary in place of the standard `mail-sources'
227 variable. These sources must contain diary messages ONLY."
232 (defcustom nndiary-split-methods '(("diary" ""))
233 "*NNDiary specific split methods.
234 This variable is used by nndiary in place of the standard
235 `nnmail-split-methods' variable."
238 :type '(choice (repeat :tag "Alist" (group (string :tag "Name") regexp))
239 (function-item nnmail-split-fancy)
240 (function :tag "Other")))
243 (defcustom nndiary-reminders '((0 . day))
244 "*Different times when you want to be reminded of your appointements.
245 Diary articles will appear again, as if they'd been just received.
247 Entries look like (3 . day) which means something like \"Please
248 Hortense, would you be so kind as to remind me of my appointments 3 days
249 before the date, thank you very much. Anda, hmmm... by the way, are you
250 doing anything special tonight ?\".
252 The units of measure are 'minute 'hour 'day 'week 'month and 'year (no,
253 not 'century, sorry).
255 NOTE: the units of measure actually express dates, not durations: if you
256 use 'week, messages will pop up on Sundays at 00:00 (or Mondays if
257 `nndiary-week-starts-on-monday' is non nil) and *not* 7 days before the
258 appointement, if you use 'month, messages will pop up on the first day of
259 each months, at 00:00 and so on.
261 If you really want to specify a duration (like 24 hours exactly), you can
262 use the equivalent in minutes (the smallest unit). A fuzz of 60 seconds
263 maximum in the reminder is not that painful, I think. Although this
264 scheme might appear somewhat weird at a first glance, it is very powerful.
265 In order to make this clear, here are some examples:
267 - '(0 . day): this is the default value of `nndiary-reminders'. It means
268 pop up the appointements of the day each morning at 00:00.
270 - '(1 . day): this means pop up the appointements the day before, at 00:00.
272 - '(6 . hour): for an appointement at 18:30, this would pop up the
273 appointement message at 12:00.
275 - '(360 . minute): for an appointement at 18:30 and 15 seconds, this would
276 pop up the appointement message at 12:30."
278 :type '(repeat (cons :format "%v\n"
279 (integer :format "%v")
280 (choice :format "%[%v(s)%] before...\n"
282 (const :format "%v" minute)
283 (const :format "%v" hour)
284 (const :format "%v" day)
285 (const :format "%v" week)
286 (const :format "%v" month)
287 (const :format "%v" year)))))
289 (defcustom nndiary-week-starts-on-monday nil
290 "*Whether a week starts on monday (otherwise, sunday)."
295 (defcustom nndiary-request-create-group-hooks nil
296 "*Hooks to run after `nndiary-request-create-group' is executed.
297 The hooks will be called with the full group name as argument."
301 (defcustom nndiary-request-update-info-hooks nil
302 "*Hooks to run after `nndiary-request-update-info-group' is executed.
303 The hooks will be called with the full group name as argument."
307 (defcustom nndiary-check-directory-twice t
308 "*If t, check directories twice to avoid NFS failures."
313 ;; Backend declaration ======================================================
315 ;; Well, most of this is nnml clonage.
317 (nnoo-declare nndiary)
319 (defvoo nndiary-directory (nnheader-concat gnus-directory "diary/")
320 "Spool directory for the nndiary backend.")
322 (defvoo nndiary-active-file
323 (expand-file-name "active" nndiary-directory)
324 "Active file for the nndiary backend.")
326 (defvoo nndiary-newsgroups-file
327 (expand-file-name "newsgroups" nndiary-directory)
328 "Newsgroups description file for the nndiary backend.")
330 (defvoo nndiary-get-new-mail t
331 "Whether nndiary gets new mail and split it.
332 Contrary to traditional mail backends, this variable should always be
333 non-nil because nndiary uses its own mail-sources and split-methods.")
335 (defvoo nndiary-nov-is-evil nil
336 "If non-nil, Gnus will never use nov databases for nndiary groups.
337 Using nov databases will speed up header fetching considerably.
338 This variable shouldn't be flipped much. If you have, for some reason,
339 set this to t, and want to set it to nil again, you should always run
340 the `nndiary-generate-nov-databases' command. The function will go
341 through all nnml directories and generate nov databases for them
342 all. This may very well take some time.")
344 (defvoo nndiary-prepare-save-mail-hook nil
345 "*Hook run narrowed to an article before saving.")
347 (defvoo nndiary-inhibit-expiry nil
348 "If non-nil, inhibit expiry.")
352 ;; $Format: "(defconst nndiary-prcs-major-version \"$ProjectMajorVersion$\")"$
353 (defconst nndiary-prcs-major-version "branch-0-2")
354 ;; $Format: "(defconst nndiary-prcs-minor-version \"$ProjectMinorVersion$\")"$
355 (defconst nndiary-prcs-minor-version "1")
356 (defconst nndiary-version
357 (let ((level nndiary-prcs-minor-version)
359 (string-match "\\(branch\\|version\\)-\\([0-9]+\\)-\\([0-9]+\\)"
360 nndiary-prcs-major-version)
361 (setq major (match-string 2 nndiary-prcs-major-version)
362 minor (match-string 3 nndiary-prcs-major-version)
363 status (match-string 1 nndiary-prcs-major-version))
364 (cond ((string= status "version")
365 (setq level (int-to-string (1- (string-to-int level))))
367 (concat major "." minor)
368 (concat major "." minor "." level)))
369 ((string= status "branch")
370 (concat major "." minor "-b" level))))
371 "Current Diary backend version.")
373 (defun nndiary-version ()
374 "Current Diary backend version."
376 (message "NNDiary version %s" nndiary-version))
379 (defvoo nndiary-nov-file-name ".overview")
381 (defvoo nndiary-current-directory nil)
382 (defvoo nndiary-current-group nil)
383 (defvoo nndiary-status-string "" )
384 (defvoo nndiary-nov-buffer-alist nil)
385 (defvoo nndiary-group-alist nil)
386 (defvoo nndiary-active-timestamp nil)
387 (defvoo nndiary-article-file-alist nil)
389 (defvoo nndiary-generate-active-function 'nndiary-generate-active-info)
390 (defvoo nndiary-nov-buffer-file-name nil)
391 (defvoo nndiary-file-coding-system nnmail-file-coding-system)
393 (defconst nndiary-headers
400 ("Time-Zone" (("Y" -43200)
477 ;; List of NNDiary headers that specify the time spec. Each header name is
478 ;; followed by either two integers (specifying a range of possible values
479 ;; for this header) or one list (specifying all the possible values for this
480 ;; header). In the latter case, the list does NOT include the unspecifyed
482 ;; For time zone values, we have symbolic time zone names associated with
483 ;; the (relative) number of seconds ahead GMT.
487 ;;; Interface functions =====================================================
489 (nnoo-define-basics nndiary)
491 (deffoo nndiary-retrieve-headers (sequence &optional group server fetch-old)
492 (when (nndiary-possibly-change-directory group server)
494 (set-buffer nntp-server-buffer)
497 (number (length sequence))
499 (file-name-coding-system nnmail-pathname-coding-system)
501 (nndiary-check-directory-twice
502 (and nndiary-check-directory-twice
503 ;; To speed up, disable it in some case.
504 (or (not (numberp nnmail-large-newsgroup))
505 (<= number nnmail-large-newsgroup)))))
506 (if (stringp (car sequence))
508 (if (nndiary-retrieve-headers-with-nov sequence fetch-old)
511 (setq article (car sequence))
512 (setq file (nndiary-article-to-file article))
515 (not (file-directory-p file)))
516 (insert (format "221 %d Article retrieved.\n" article))
518 (nnheader-insert-head file)
520 (if (search-forward "\n\n" nil t)
522 (goto-char (point-max))
525 (delete-region (point) (point-max)))
526 (setq sequence (cdr sequence))
527 (setq count (1+ count))
528 (and (numberp nnmail-large-newsgroup)
529 (> number nnmail-large-newsgroup)
531 (nnheader-message 6 "nndiary: Receiving headers... %d%%"
532 (/ (* count 100) number))))
534 (and (numberp nnmail-large-newsgroup)
535 (> number nnmail-large-newsgroup)
536 (nnheader-message 6 "nndiary: Receiving headers...done"))
538 (nnheader-fold-continuation-lines)
541 (deffoo nndiary-open-server (server &optional defs)
542 (nnoo-change-server 'nndiary server defs)
543 (when (not (file-exists-p nndiary-directory))
544 (ignore-errors (make-directory nndiary-directory t)))
546 ((not (file-exists-p nndiary-directory))
547 (nndiary-close-server)
548 (nnheader-report 'nndiary "Couldn't create directory: %s"
550 ((not (file-directory-p (file-truename nndiary-directory)))
551 (nndiary-close-server)
552 (nnheader-report 'nndiary "Not a directory: %s" nndiary-directory))
554 (nnheader-report 'nndiary "Opened server %s using directory %s"
555 server nndiary-directory)
558 (deffoo nndiary-request-regenerate (server)
559 (nndiary-possibly-change-directory nil server)
560 (nndiary-generate-nov-databases server)
563 (deffoo nndiary-request-article (id &optional group server buffer)
564 (nndiary-possibly-change-directory group server)
565 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
566 (file-name-coding-system nnmail-pathname-coding-system)
567 path gpath group-num)
569 (when (and (setq group-num (nndiary-find-group-number id))
571 (assq (cdr group-num)
572 (nnheader-article-to-file-alist
574 (nnmail-group-pathname
576 nndiary-directory))))))
577 (setq path (concat gpath (int-to-string (cdr group-num)))))
578 (setq path (nndiary-article-to-file id)))
581 (nnheader-report 'nndiary "No such article: %s" id))
582 ((not (file-exists-p path))
583 (nnheader-report 'nndiary "No such file: %s" path))
584 ((file-directory-p path)
585 (nnheader-report 'nndiary "File is a directory: %s" path))
586 ((not (save-excursion (let ((nnmail-file-coding-system
587 nndiary-file-coding-system))
588 (nnmail-find-file path))))
589 (nnheader-report 'nndiary "Couldn't read file: %s" path))
591 (nnheader-report 'nndiary "Article %s retrieved" id)
592 ;; We return the article number.
593 (cons (if group-num (car group-num) group)
594 (string-to-int (file-name-nondirectory path)))))))
596 (deffoo nndiary-request-group (group &optional server dont-check)
597 (let ((file-name-coding-system nnmail-pathname-coding-system))
599 ((not (nndiary-possibly-change-directory group server))
600 (nnheader-report 'nndiary "Invalid group (no such directory)"))
601 ((not (file-exists-p nndiary-current-directory))
602 (nnheader-report 'nndiary "Directory %s does not exist"
603 nndiary-current-directory))
604 ((not (file-directory-p nndiary-current-directory))
605 (nnheader-report 'nndiary "%s is not a directory"
606 nndiary-current-directory))
608 (nnheader-report 'nndiary "Group %s selected" group)
611 (nnheader-re-read-dir nndiary-current-directory)
612 (nnmail-activate 'nndiary)
613 (let ((active (nth 1 (assoc group nndiary-group-alist))))
615 (nnheader-report 'nndiary "No such group: %s" group)
616 (nnheader-report 'nndiary "Selected group %s" group)
617 (nnheader-insert "211 %d %d %d %s\n"
618 (max (1+ (- (cdr active) (car active))) 0)
619 (car active) (cdr active) group)))))))
621 (deffoo nndiary-request-scan (&optional group server)
622 ;; Use our own mail sources and split methods while Gnus doesn't let us have
623 ;; multiple backends for retrieving mail.
624 (let ((mail-sources nndiary-mail-sources)
625 (nnmail-split-methods nndiary-split-methods))
626 (setq nndiary-article-file-alist nil)
627 (nndiary-possibly-change-directory group server)
628 (nnmail-get-new-mail 'nndiary 'nndiary-save-nov nndiary-directory group)))
630 (deffoo nndiary-close-group (group &optional server)
631 (setq nndiary-article-file-alist nil)
634 (deffoo nndiary-request-create-group (group &optional server args)
635 (nndiary-possibly-change-directory nil server)
636 (nnmail-activate 'nndiary)
638 ((assoc group nndiary-group-alist)
640 ((and (file-exists-p (nnmail-group-pathname group nndiary-directory))
641 (not (file-directory-p (nnmail-group-pathname
642 group nndiary-directory))))
643 (nnheader-report 'nndiary "%s is a file"
644 (nnmail-group-pathname group nndiary-directory)))
647 (push (list group (setq active (cons 1 0)))
649 (nndiary-possibly-create-directory group)
650 (nndiary-possibly-change-directory group server)
651 (let ((articles (nnheader-directory-articles nndiary-current-directory)))
653 (setcar active (apply 'min articles))
654 (setcdr active (apply 'max articles))))
655 (nnmail-save-active nndiary-group-alist nndiary-active-file)
656 (run-hook-with-args 'nndiary-request-create-group-hooks
657 (gnus-group-prefixed-name group
658 (list "nndiary" server)))
662 (deffoo nndiary-request-list (&optional server)
664 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
665 (file-name-coding-system nnmail-pathname-coding-system))
666 (nnmail-find-file nndiary-active-file))
667 (setq nndiary-group-alist (nnmail-get-active))
670 (deffoo nndiary-request-newgroups (date &optional server)
671 (nndiary-request-list server))
673 (deffoo nndiary-request-list-newsgroups (&optional server)
675 (nnmail-find-file nndiary-newsgroups-file)))
677 (deffoo nndiary-request-expire-articles (articles group &optional server force)
678 (nndiary-possibly-change-directory group server)
679 (let ((active-articles
680 (nnheader-directory-articles nndiary-current-directory))
682 (nnmail-activate 'nndiary)
683 ;; Articles not listed in active-articles are already gone,
684 ;; so don't try to expire them.
685 (setq articles (gnus-intersection articles active-articles))
687 (setq article (nndiary-article-to-file (setq number (pop articles))))
688 (if (and (nndiary-deletable-article-p group number)
689 ;; Don't use nnmail-expired-article-p. Our notion of expiration
690 ;; is a bit peculiar ...
691 (or force (nndiary-expired-article-p article)))
693 ;; Allow a special target group.
694 (unless (eq nnmail-expiry-target 'delete)
696 (nndiary-request-article number group server (current-buffer))
697 (let ((nndiary-current-directory nil))
698 (nnmail-expiry-target-group nnmail-expiry-target group))))
699 (nnheader-message 5 "Deleting article %s in %s" number group)
701 (funcall nnmail-delete-file-function article)
702 (file-error (push number rest)))
703 (setq active-articles (delq number active-articles))
704 (nndiary-nov-delete-article group number))
706 (let ((active (nth 1 (assoc group nndiary-group-alist))))
708 (setcar active (or (and active-articles
709 (apply 'min active-articles))
711 (nnmail-save-active nndiary-group-alist nndiary-active-file))
713 (nconc rest articles)))
715 (deffoo nndiary-request-move-article
716 (article group server accept-form &optional last)
717 (let ((buf (get-buffer-create " *nndiary move*"))
719 (nndiary-possibly-change-directory group server)
720 (nndiary-update-file-alist)
722 (nndiary-deletable-article-p group article)
723 (nndiary-request-article article group server)
724 (let (nndiary-current-directory
725 nndiary-current-group
726 nndiary-article-file-alist)
729 (insert-buffer-substring nntp-server-buffer)
730 (setq result (eval accept-form))
731 (kill-buffer (current-buffer))
734 (nndiary-possibly-change-directory group server)
736 (funcall nnmail-delete-file-function
737 (nndiary-article-to-file article))
739 (nndiary-nov-delete-article group article)
742 (nnmail-save-active nndiary-group-alist nndiary-active-file))))
745 (deffoo nndiary-request-accept-article (group &optional server last)
746 (nndiary-possibly-change-directory group server)
747 (nnmail-check-syntax)
749 (when nnmail-cache-accepted-message-ids
750 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
753 (nnmail-activate 'nndiary)
755 (car (nndiary-save-mail
756 (list (cons group (nndiary-active-number group))))))
758 (nnmail-save-active nndiary-group-alist nndiary-active-file)
759 (and last (nndiary-save-nov))))
761 (nnmail-activate 'nndiary)
762 (if (and (not (setq result
763 (nnmail-article-group 'nndiary-active-number)))
764 (yes-or-no-p "Moved to `junk' group; delete article? "))
766 (setq result (car (nndiary-save-mail result))))
768 (nnmail-save-active nndiary-group-alist nndiary-active-file)
769 (when nnmail-cache-accepted-message-ids
770 (nnmail-cache-close))
771 (nndiary-save-nov))))
774 (deffoo nndiary-request-post (&optional server)
775 (nnmail-do-request-post 'nndiary-request-accept-article server))
777 (deffoo nndiary-request-replace-article (article group buffer)
778 (nndiary-possibly-change-directory group)
781 (nndiary-possibly-create-directory group)
782 (let ((chars (nnmail-insert-lines))
783 (art (concat (int-to-string article) "\t"))
787 (point-min) (point-max)
788 (or (nndiary-article-to-file article)
789 (expand-file-name (int-to-string article)
790 nndiary-current-directory))
791 nil (if (nnheader-be-verbose 5) nil 'nomesg))
793 (setq headers (nndiary-parse-head chars article))
794 ;; Replace the NOV line in the NOV file.
796 (set-buffer (nndiary-open-nov group))
797 (goto-char (point-min))
798 (if (or (looking-at art)
799 (search-forward (concat "\n" art) nil t))
800 ;; Delete the old NOV line.
801 (delete-region (progn (beginning-of-line) (point))
802 (progn (forward-line 1) (point)))
803 ;; The line isn't here, so we have to find out where
804 ;; we should insert it. (This situation should never
805 ;; occur, but one likes to make sure...)
806 (while (and (looking-at "[0-9]+\t")
809 (match-beginning 0) (match-end 0)))
811 (zerop (forward-line 1)))))
813 (nnheader-insert-nov headers)
817 (deffoo nndiary-request-delete-group (group &optional force server)
818 (nndiary-possibly-change-directory group server)
820 ;; Delete all articles in GROUP.
823 nndiary-current-directory t
824 (concat nnheader-numerical-short-files
825 "\\|" (regexp-quote nndiary-nov-file-name) "$")))
828 (setq article (pop articles))
829 (when (file-writable-p article)
830 (nnheader-message 5 "Deleting article %s in %s..." article group)
831 (funcall nnmail-delete-file-function article))))
832 ;; Try to delete the directory itself.
833 (ignore-errors (delete-directory nndiary-current-directory)))
834 ;; Remove the group from all structures.
835 (setq nndiary-group-alist
836 (delq (assoc group nndiary-group-alist) nndiary-group-alist)
837 nndiary-current-group nil
838 nndiary-current-directory nil)
839 ;; Save the active file.
840 (nnmail-save-active nndiary-group-alist nndiary-active-file)
843 (deffoo nndiary-request-rename-group (group new-name &optional server)
844 (nndiary-possibly-change-directory group server)
845 (let ((new-dir (nnmail-group-pathname new-name nndiary-directory))
846 (old-dir (nnmail-group-pathname group nndiary-directory)))
848 (make-directory new-dir t)
850 ;; We move the articles file by file instead of renaming
851 ;; the directory -- there may be subgroups in this group.
852 ;; One might be more clever, I guess.
853 (let ((files (nnheader-article-to-file-alist old-dir)))
856 (concat old-dir (cdar files))
857 (concat new-dir (cdar files)))
859 ;; Move .overview file.
860 (let ((overview (concat old-dir nndiary-nov-file-name)))
861 (when (file-exists-p overview)
862 (rename-file overview (concat new-dir nndiary-nov-file-name))))
863 (when (<= (length (directory-files old-dir)) 2)
864 (ignore-errors (delete-directory old-dir)))
865 ;; That went ok, so we change the internal structures.
866 (let ((entry (assoc group nndiary-group-alist)))
868 (setcar entry new-name))
869 (setq nndiary-current-directory nil
870 nndiary-current-group nil)
871 ;; Save the new group alist.
872 (nnmail-save-active nndiary-group-alist nndiary-active-file)
875 (deffoo nndiary-set-status (article name value &optional group server)
876 (nndiary-possibly-change-directory group server)
877 (let ((file (nndiary-article-to-file article)))
879 ((not (file-exists-p file))
880 (nnheader-report 'nndiary "File %s does not exist" file))
883 (nnheader-insert-file-contents file)
884 (nnmail-replace-status name value))
888 ;;; Interface optional functions ============================================
890 (deffoo nndiary-request-update-info (group info &optional server)
891 (nndiary-possibly-change-directory group)
892 (let ((timestamp (gnus-group-parameter-value (gnus-info-params info)
895 (nnheader-report 'nndiary "Group %s doesn't have a timestamp" group)
897 ;; Figure out which articles should be re-new'ed
898 (let ((articles (nndiary-flatten (gnus-info-read info) 0))
899 article file unread buf)
901 (setq buf (nnheader-set-temp-buffer " *nndiary update*"))
902 (while (setq article (pop articles))
903 (setq file (concat nndiary-current-directory
904 (int-to-string article)))
905 (and (file-exists-p file)
906 (nndiary-renew-article-p file timestamp)
907 (push article unread)))
908 ;;(message "unread: %s" unread)
911 (setq unread (sort unread '<))
913 (gnus-info-set-read info (gnus-update-read-articles
914 (gnus-info-group info) unread t)))
916 (run-hook-with-args 'nndiary-request-update-info-hooks
917 (gnus-info-group info))
922 ;;; Internal functions ======================================================
924 (defun nndiary-article-to-file (article)
925 (nndiary-update-file-alist)
927 (if (setq file (cdr (assq article nndiary-article-file-alist)))
928 (expand-file-name file nndiary-current-directory)
929 ;; Just to make sure nothing went wrong when reading over NFS --
931 (if nndiary-check-directory-twice
933 (setq file (expand-file-name (number-to-string article)
934 nndiary-current-directory)))
935 (nndiary-update-file-alist t)
938 (defun nndiary-deletable-article-p (group article)
939 "Say whether ARTICLE in GROUP can be deleted."
941 (when (setq path (nndiary-article-to-file article))
942 (when (file-writable-p path)
943 (or (not nnmail-keep-last-article)
944 (not (eq (cdr (nth 1 (assoc group nndiary-group-alist)))
947 ;; Find an article number in the current group given the Message-ID.
948 (defun nndiary-find-group-number (id)
950 (set-buffer (get-buffer-create " *nndiary id*"))
951 (let ((alist nndiary-group-alist)
953 ;; We want to look through all .overview files, but we want to
954 ;; start with the one in the current directory. It seems most
955 ;; likely that the article we are looking for is in that group.
956 (if (setq number (nndiary-find-id nndiary-current-group id))
957 (cons nndiary-current-group number)
958 ;; It wasn't there, so we look through the other groups as well.
959 (while (and (not number)
961 (or (string= (caar alist) nndiary-current-group)
962 (setq number (nndiary-find-id (caar alist) id)))
964 (setq alist (cdr alist))))
966 (cons (caar alist) number))))))
968 (defun nndiary-find-id (group id)
970 (let ((nov (expand-file-name nndiary-nov-file-name
971 (nnmail-group-pathname group
974 (when (file-exists-p nov)
975 (nnheader-insert-file-contents nov)
976 (while (and (not found)
977 (search-forward id nil t)) ; We find the ID.
978 ;; And the id is in the fourth field.
979 (if (not (and (search-backward "\t" nil t 4)
980 (not (search-backward"\t" (gnus-point-at-bol) t))))
984 ;; We return the article number.
986 (ignore-errors (read (current-buffer))))))
989 (defun nndiary-retrieve-headers-with-nov (articles &optional fetch-old)
990 (if (or gnus-nov-is-evil nndiary-nov-is-evil)
992 (let ((nov (expand-file-name nndiary-nov-file-name
993 nndiary-current-directory)))
994 (when (file-exists-p nov)
996 (set-buffer nntp-server-buffer)
998 (nnheader-insert-file-contents nov)
1000 (not (numberp fetch-old)))
1001 t ; Don't remove anything.
1002 (nnheader-nov-delete-outside-range
1003 (if fetch-old (max 1 (- (car articles) fetch-old))
1005 (car (last articles)))
1008 (defun nndiary-possibly-change-directory (group &optional server)
1010 (not (nndiary-server-opened server)))
1011 (nndiary-open-server server))
1014 (let ((pathname (nnmail-group-pathname group nndiary-directory))
1015 (file-name-coding-system nnmail-pathname-coding-system))
1016 (when (not (equal pathname nndiary-current-directory))
1017 (setq nndiary-current-directory pathname
1018 nndiary-current-group group
1019 nndiary-article-file-alist nil))
1020 (file-exists-p nndiary-current-directory))))
1022 (defun nndiary-possibly-create-directory (group)
1023 (let ((dir (nnmail-group-pathname group nndiary-directory)))
1024 (unless (file-exists-p dir)
1025 (make-directory (directory-file-name dir) t)
1026 (nnheader-message 5 "Creating mail directory %s" dir))))
1028 (defun nndiary-save-mail (group-art)
1029 "Called narrowed to an article."
1030 (let (chars headers)
1031 (setq chars (nnmail-insert-lines))
1032 (nnmail-insert-xref group-art)
1033 (run-hooks 'nnmail-prepare-save-mail-hook)
1034 (run-hooks 'nndiary-prepare-save-mail-hook)
1035 (goto-char (point-min))
1036 (while (looking-at "From ")
1037 (replace-match "X-From-Line: ")
1039 ;; We save the article in all the groups it belongs in.
1040 (let ((ga group-art)
1043 (nndiary-possibly-create-directory (caar ga))
1044 (let ((file (concat (nnmail-group-pathname
1045 (caar ga) nndiary-directory)
1046 (int-to-string (cdar ga)))))
1048 ;; It was already saved, so we just make a hard link.
1049 (funcall nnmail-crosspost-link-function first file t)
1050 ;; Save the article.
1051 (nnmail-write-region (point-min) (point-max) file nil
1052 (if (nnheader-be-verbose 5) nil 'nomesg))
1054 (setq ga (cdr ga))))
1055 ;; Generate a nov line for this article. We generate the nov
1056 ;; line after saving, because nov generation destroys the
1058 (setq headers (nndiary-parse-head chars))
1059 ;; Output the nov line to all nov databases that should have it.
1060 (let ((ga group-art))
1062 (nndiary-add-nov (caar ga) (cdar ga) headers)
1063 (setq ga (cdr ga))))
1066 (defun nndiary-active-number (group)
1067 "Compute the next article number in GROUP."
1068 (let ((active (cadr (assoc group nndiary-group-alist))))
1069 ;; The group wasn't known to nndiary, so we just create an active
1072 ;; Perhaps the active file was corrupt? See whether
1073 ;; there are any articles in this group.
1074 (nndiary-possibly-create-directory group)
1075 (nndiary-possibly-change-directory group)
1076 (unless nndiary-article-file-alist
1077 (setq nndiary-article-file-alist
1079 (nnheader-article-to-file-alist nndiary-current-directory)
1080 'car-less-than-car)))
1082 (if nndiary-article-file-alist
1083 (cons (caar nndiary-article-file-alist)
1084 (caar (last nndiary-article-file-alist)))
1086 (push (list group active) nndiary-group-alist))
1087 (setcdr active (1+ (cdr active)))
1088 (while (file-exists-p
1089 (expand-file-name (int-to-string (cdr active))
1090 (nnmail-group-pathname group nndiary-directory)))
1091 (setcdr active (1+ (cdr active))))
1094 (defun nndiary-add-nov (group article headers)
1095 "Add a nov line for the GROUP base."
1097 (set-buffer (nndiary-open-nov group))
1098 (goto-char (point-max))
1099 (mail-header-set-number headers article)
1100 (nnheader-insert-nov headers)))
1102 (defsubst nndiary-header-value ()
1103 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
1105 (defun nndiary-parse-head (chars &optional number)
1106 "Parse the head of the current buffer."
1109 (unless (zerop (buffer-size))
1111 (goto-char (point-min))
1112 (if (search-forward "\n\n" nil t) (1- (point)) (point-max))))
1113 ;; Fold continuation lines.
1114 (goto-char (point-min))
1115 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1116 (replace-match " " t t))
1117 ;; Remove any tabs; they are too confusing.
1118 (subst-char-in-region (point-min) (point-max) ?\t ? )
1119 (let ((headers (nnheader-parse-head t)))
1120 (mail-header-set-chars headers chars)
1121 (mail-header-set-number headers number)
1124 (defun nndiary-open-nov (group)
1125 (or (cdr (assoc group nndiary-nov-buffer-alist))
1126 (let ((buffer (get-buffer-create (format " *nndiary overview %s*"
1130 (set (make-local-variable 'nndiary-nov-buffer-file-name)
1132 nndiary-nov-file-name
1133 (nnmail-group-pathname group nndiary-directory)))
1135 (when (file-exists-p nndiary-nov-buffer-file-name)
1136 (nnheader-insert-file-contents nndiary-nov-buffer-file-name)))
1137 (push (cons group buffer) nndiary-nov-buffer-alist)
1140 (defun nndiary-save-nov ()
1142 (while nndiary-nov-buffer-alist
1143 (when (buffer-name (cdar nndiary-nov-buffer-alist))
1144 (set-buffer (cdar nndiary-nov-buffer-alist))
1145 (when (buffer-modified-p)
1146 (nnmail-write-region 1 (point-max) nndiary-nov-buffer-file-name
1148 (set-buffer-modified-p nil)
1149 (kill-buffer (current-buffer)))
1150 (setq nndiary-nov-buffer-alist (cdr nndiary-nov-buffer-alist)))))
1153 (defun nndiary-generate-nov-databases (&optional server)
1154 "Generate NOV databases in all nndiary directories."
1155 (interactive (list (or (nnoo-current-server 'nndiary) "")))
1156 ;; Read the active file to make sure we don't re-use articles
1157 ;; numbers in empty groups.
1158 (nnmail-activate 'nndiary)
1159 (unless (nndiary-server-opened server)
1160 (nndiary-open-server server))
1161 (setq nndiary-directory (expand-file-name nndiary-directory))
1162 ;; Recurse down the directories.
1163 (nndiary-generate-nov-databases-1 nndiary-directory nil t)
1164 ;; Save the active file.
1165 (nnmail-save-active nndiary-group-alist nndiary-active-file))
1167 (defun nndiary-generate-nov-databases-1 (dir &optional seen no-active)
1168 "Regenerate the NOV database in DIR."
1169 (interactive "DRegenerate NOV in: ")
1170 (setq dir (file-name-as-directory dir))
1171 ;; Only scan this sub-tree if we haven't been here yet.
1172 (unless (member (file-truename dir) seen)
1173 (push (file-truename dir) seen)
1174 ;; We descend recursively
1175 (let ((dirs (directory-files dir t nil t))
1177 (while (setq dir (pop dirs))
1178 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
1179 (file-directory-p dir))
1180 (nndiary-generate-nov-databases-1 dir seen))))
1181 ;; Do this directory.
1182 (let ((files (sort (nnheader-article-to-file-alist dir)
1183 'car-less-than-car)))
1185 (let* ((group (nnheader-file-to-group
1186 (directory-file-name dir) nndiary-directory))
1187 (info (cadr (assoc group nndiary-group-alist))))
1189 (setcar info (1+ (cdr info)))))
1190 (funcall nndiary-generate-active-function dir)
1191 ;; Generate the nov file.
1192 (nndiary-generate-nov-file dir files)
1194 (nnmail-save-active nndiary-group-alist nndiary-active-file))))))
1196 (eval-when-compile (defvar files))
1197 (defun nndiary-generate-active-info (dir)
1198 ;; Update the active info for this group.
1199 (let* ((group (nnheader-file-to-group
1200 (directory-file-name dir) nndiary-directory))
1201 (entry (assoc group nndiary-group-alist))
1202 (last (or (caadr entry) 0)))
1203 (setq nndiary-group-alist (delq entry nndiary-group-alist))
1205 (cons (or (caar files) (1+ last))
1207 (or (let ((f files))
1208 (while (cdr f) (setq f (cdr f)))
1211 nndiary-group-alist)))
1213 (defun nndiary-generate-nov-file (dir files)
1214 (let* ((dir (file-name-as-directory dir))
1215 (nov (concat dir nndiary-nov-file-name))
1216 (nov-buffer (get-buffer-create " *nov*"))
1219 ;; Init the nov buffer.
1220 (set-buffer nov-buffer)
1221 (buffer-disable-undo)
1223 (set-buffer nntp-server-buffer)
1224 ;; Delete the old NOV file.
1225 (when (file-exists-p nov)
1226 (funcall nnmail-delete-file-function nov))
1228 (unless (file-directory-p (setq file (concat dir (cdar files))))
1230 (nnheader-insert-file-contents file)
1232 (goto-char (point-min))
1234 (search-forward "\n\n" nil t)
1235 (setq chars (- (point-max) (point)))
1236 (max 1 (1- (point)))))
1237 (unless (zerop (buffer-size))
1238 (goto-char (point-min))
1239 (setq headers (nndiary-parse-head chars (caar files)))
1241 (set-buffer nov-buffer)
1242 (goto-char (point-max))
1243 (nnheader-insert-nov headers)))
1245 (setq files (cdr files)))
1247 (set-buffer nov-buffer)
1248 (nnmail-write-region 1 (point-max) nov nil 'nomesg)
1249 (kill-buffer (current-buffer))))))
1251 (defun nndiary-nov-delete-article (group article)
1253 (set-buffer (nndiary-open-nov group))
1254 (when (nnheader-find-nov-line article)
1255 (delete-region (point) (progn (forward-line 1) (point)))
1257 (let ((active (cadr (assoc group nndiary-group-alist)))
1261 (setf (car active) (1+ (cdr active)))
1262 (when (and (setq num (ignore-errors (read (current-buffer))))
1264 (setf (car active) num)))))))
1267 (defun nndiary-update-file-alist (&optional force)
1268 (when (or (not nndiary-article-file-alist)
1270 (setq nndiary-article-file-alist
1271 (nnheader-article-to-file-alist nndiary-current-directory))))
1274 (defun nndiary-string-to-int (str min &optional max)
1275 ;; Like `string-to-int' but barf if STR is not exactly an integer, and not
1276 ;; within the specified bounds.
1277 ;; Signals are caught by `nndiary-schedule'.
1278 (if (not (string-match "^[ \t]*[0-9]+[ \t]*$" str))
1279 (nndiary-error "not an integer value")
1281 (let ((val (string-to-int str)))
1282 (and (or (< val min)
1283 (and max (> val max)))
1284 (nndiary-error "value out of range"))
1287 (defun nndiary-parse-schedule-value (str min-or-values max)
1288 ;; Parse the schedule string STR.
1289 ;; Signals are caught by `nndary-schedule'.
1290 (if (string-match "[ \t]*\\*[ \t]*" str)
1294 (if (listp min-or-values)
1295 ;; min-or-values is values
1296 ;; #### NOTE: this is actually only a hack for time zones.
1297 (let ((val (and (string-match "[ \t]*\\([^ \t]+\\)[ \t]*" str)
1298 (match-string 1 str))))
1299 (if (and val (setq val (assoc val min-or-values)))
1301 (nndiary-error "invalid syntax")))
1302 ;; min-or-values is min
1305 (let ((res (split-string val "-")))
1308 (nndiary-string-to-int (car res) min-or-values max))
1310 ;; don't know if crontab accepts this, but ensure
1311 ;; that BEG is <= END
1312 (let ((beg (nndiary-string-to-int (car res) min-or-values max))
1313 (end (nndiary-string-to-int (cadr res) min-or-values max)))
1321 (nndiary-error "invalid syntax")))
1323 (split-string str ",")))
1326 ;; ### FIXME: remove this function if it's used only once.
1327 (defun nndiary-parse-schedule (head min-or-values max)
1328 ;; Parse the cron-like value of header X-Diary-HEAD in current buffer.
1329 ;; - Returns nil if `*'
1330 ;; - Otherwise returns a list of integers and/or ranges (BEG . END)
1331 ;; The exception is the Timze-Zone value which is always of the form (STR).
1332 ;; Signals are caught by `nndary-schedule'.
1333 (let ((header (format "^X-Diary-%s: \\(.*\\)$" head)))
1334 (goto-char (point-min))
1335 (if (not (re-search-forward header nil t))
1336 (nndiary-error "header missing")
1338 (nndiary-parse-schedule-value (match-string 1) min-or-values max))
1341 (defsubst nndiary-schedule ()
1345 (nndiary-parse-schedule (nth 0 elt) (nth 1 elt) (nth 2 elt))
1347 (nnheader-report 'nndiary "X-Diary-%s header parse error: %s."
1348 (car elt) (cdr arg))
1352 (defun nndiary-max (spec)
1353 ;; Returns the max of specification SPEC, or nil for permanent schedules.
1358 (while (setq elt (pop elts))
1360 (and (> elt max) (setq max elt))
1361 (and (> (cdr elt) max) (setq max (cdr elt)))))
1364 (defun nndiary-flatten (spec min &optional max)
1365 ;; flatten the spec by expanding ranges to all possible values.
1368 ;; this happens when I flatten something else than one of my
1369 ;; schedules (a list of read articles for instance).
1378 (while (setq elt (pop elts))
1383 (while (<= n (cdr elt))
1385 (setq n (1+ n))))))))
1388 (defun nndiary-unflatten (spec)
1389 ;; opposite of flatten: build ranges if possible
1390 (setq spec (sort spec '<))
1392 (while (setq min (pop spec))
1394 (while (and (car spec) (= (car spec) (1+ max)))
1398 (setq res (append res (list min)))
1399 (setq res (append res (list (cons min max))))))
1402 (defun nndiary-compute-reminders (date)
1403 ;; Returns a list of times corresponding to the reminders of date DATE.
1404 ;; See the comment in `nndiary-reminders' about rounding.
1405 (let* ((reminders nndiary-reminders)
1406 (date-elts (decode-time date))
1407 ;; ### NOTE: out-of-range values are accepted by encode-time. This
1408 ;; makes our life easier.
1409 (monday (- (nth 3 date-elts)
1410 (if nndiary-week-starts-on-monday
1411 (if (zerop (nth 6 date-elts))
1413 (- (nth 6 date-elts) 1))
1414 (nth 6 date-elts))))
1416 ;; remove the DOW and DST entries
1417 (setf (nthcdr 6 date-elts) (nthcdr 8 date-elts))
1418 (while (setq reminder (pop reminders))
1420 (cond ((eq (cdr reminder) 'minute)
1422 (apply 'encode-time 0 (nthcdr 1 date-elts))
1423 (seconds-to-time (* (car reminder) 60.0))))
1424 ((eq (cdr reminder) 'hour)
1426 (apply 'encode-time 0 0 (nthcdr 2 date-elts))
1427 (seconds-to-time (* (car reminder) 3600.0))))
1428 ((eq (cdr reminder) 'day)
1430 (apply 'encode-time 0 0 0 (nthcdr 3 date-elts))
1431 (seconds-to-time (* (car reminder) 86400.0))))
1432 ((eq (cdr reminder) 'week)
1434 (apply 'encode-time 0 0 0 monday (nthcdr 4 date-elts))
1435 (seconds-to-time (* (car reminder) 604800.0))))
1436 ((eq (cdr reminder) 'month)
1438 (apply 'encode-time 0 0 0 1 (nthcdr 4 date-elts))
1439 (seconds-to-time (* (car reminder) 18748800.0))))
1440 ((eq (cdr reminder) 'year)
1442 (apply 'encode-time 0 0 0 1 1 (nthcdr 5 date-elts))
1443 (seconds-to-time (* (car reminder) 400861056.0)))))
1445 (sort res 'time-less-p)))
1447 (defun nndiary-last-occurence (sched)
1448 ;; Returns the last occurence of schedule SCHED as an Emacs time struct, or
1449 ;; nil for permanent schedule or errors.
1450 (let ((minute (nndiary-max (nth 0 sched)))
1451 (hour (nndiary-max (nth 1 sched)))
1452 (year (nndiary-max (nth 4 sched)))
1453 (time-zone (or (and (nth 6 sched) (car (nth 6 sched)))
1454 (current-time-zone))))
1456 (or minute (setq minute 59))
1457 (or hour (setq hour 23))
1458 ;; I'll just compute all possible values and test them by decreasing
1459 ;; order until one succeeds. This is probably quide rude, but I got
1460 ;; bored in finding a good algorithm for doing that ;-)
1461 ;; ### FIXME: remove identical entries.
1462 (let ((dom-list (nth 2 sched))
1463 (month-list (sort (nndiary-flatten (nth 3 sched) 1 12) '>))
1464 (year-list (sort (nndiary-flatten (nth 4 sched) 1971) '>))
1465 (dow-list (nth 5 sched)))
1466 ;; Special case: an asterisk in one of the days specifications means
1467 ;; that only the other should be taken into account. If both are
1468 ;; unspecified, you would get all possible days in both.
1469 (cond ((null dow-list)
1470 ;; this gets all days if dom-list is nil
1471 (setq dom-list (nndiary-flatten dom-list 1 31)))
1473 ;; this also gets all days if dow-list is nil
1474 (setq dow-list (nndiary-flatten dow-list 0 6)))
1476 (setq dom-list (nndiary-flatten dom-list 1 31))
1477 (setq dow-list (nndiary-flatten dow-list 0 6))))
1480 (while (setq year (pop year-list))
1481 (let ((months month-list)
1483 (while (setq month (pop months))
1484 ;; Now we must merge the Dows with the Doms. To do that, we
1485 ;; have to know which day is the 1st one for this month.
1486 ;; Maybe there's simpler, but decode-time(encode-time) will
1487 ;; give us the answer.
1488 (let ((first (nth 6 (decode-time
1489 (encode-time 0 0 0 1 month year
1491 (max (cond ((= month 2)
1492 (if (date-leap-year-p year) 29 28))
1494 (if (zerop (% month 2)) 30 31))
1496 (if (zerop (% month 2)) 31 30))))
1500 ;; first, review the doms to see if they are valid.
1501 (while (setq day (pop doms))
1504 ;; second add all possible dows
1505 (while (setq day (pop dows))
1507 (setq day (1+ (- day first)))
1508 (and (< day 0) (setq day (+ 7 day)))
1511 (setq day (+ 7 day))))
1512 ;; Finally, if we have some days, they are valid
1516 (encode-time 0 minute hour
1517 (car days) month year time-zone)))
1519 ;; There's an upper limit, but we didn't find any last occurence.
1520 ;; This means that the schedule is undecidable. This can happen if
1521 ;; you happen to say something like "each Feb 31 until 2038".
1523 (nnheader-report 'nndiary "Undecidable schedule")
1527 (defun nndiary-next-occurence (sched now)
1528 ;; Returns the next occurence of schedule SCHED, starting from time NOW.
1529 ;; If there's no next occurence, returns the last one (if any) which is then
1531 (let* ((today (decode-time now))
1532 (this-minute (nth 1 today))
1533 (this-hour (nth 2 today))
1534 (this-day (nth 3 today))
1535 (this-month (nth 4 today))
1536 (this-year (nth 5 today))
1537 (minute-list (sort (nndiary-flatten (nth 0 sched) 0 59) '<))
1538 (hour-list (sort (nndiary-flatten (nth 1 sched) 0 23) '<))
1539 (dom-list (nth 2 sched))
1540 (month-list (sort (nndiary-flatten (nth 3 sched) 1 12) '<))
1541 (years (if (nth 4 sched)
1542 (sort (nndiary-flatten (nth 4 sched) 1971) '<)
1544 (dow-list (nth 5 sched))
1545 (year (1- this-year))
1546 (time-zone (or (and (nth 6 sched) (car (nth 6 sched)))
1547 (current-time-zone))))
1548 ;; Special case: an asterisk in one of the days specifications means that
1549 ;; only the other should be taken into account. If both are unspecified,
1550 ;; you would get all possible days in both.
1551 (cond ((null dow-list)
1552 ;; this gets all days if dom-list is nil
1553 (setq dom-list (nndiary-flatten dom-list 1 31)))
1555 ;; this also gets all days if dow-list is nil
1556 (setq dow-list (nndiary-flatten dow-list 0 6)))
1558 (setq dom-list (nndiary-flatten dom-list 1 31))
1559 (setq dow-list (nndiary-flatten dow-list 0 6))))
1560 ;; Remove past years.
1561 (unless (eq years t)
1562 (while (and (car years) (< (car years) this-year))
1565 ;; Because we might not be limited in years, we must guard against
1566 ;; infinite loops. Appart from cases like Feb 31, there are probably
1567 ;; other ones, (no monday XXX 2nd etc). I don't know any algorithm to
1568 ;; decide this, so I assume that if we reach 10 years later, the
1569 ;; schedule is undecidable.
1572 (while (if (eq years t)
1573 (and (setq year (1+ year))
1574 (<= year (+ 10 this-year)))
1575 (setq year (pop years)))
1576 (let ((months month-list)
1578 ;; Remove past months for this year.
1579 (and (= year this-year)
1580 (while (and (car months) (< (car months) this-month))
1582 (while (setq month (pop months))
1583 ;; Now we must merge the Dows with the Doms. To do that, we
1584 ;; have to know which day is the 1st one for this month.
1585 ;; Maybe there's simpler, but decode-time(encode-time) will
1586 ;; give us the answer.
1587 (let ((first (nth 6 (decode-time
1588 (encode-time 0 0 0 1 month year
1590 (max (cond ((= month 2)
1591 (if (date-leap-year-p year) 29 28))
1593 (if (zerop (% month 2)) 30 31))
1595 (if (zerop (% month 2)) 31 30))))
1599 ;; first, review the doms to see if they are valid.
1600 (while (setq day (pop doms))
1603 ;; second add all possible dows
1604 (while (setq day (pop dows))
1606 (setq day (1+ (- day first)))
1607 (and (< day 0) (setq day (+ 7 day)))
1610 (setq day (+ 7 day))))
1611 ;; Aaaaaaall right. Now we have a valid list of DAYS for
1612 ;; this month and this year.
1614 (setq days (sort days '<))
1615 ;; Remove past days for this year and this month.
1616 (and (= year this-year)
1617 (= month this-month)
1618 (while (and (car days) (< (car days) this-day))
1620 (while (setq day (pop days))
1621 (let ((hours hour-list)
1623 ;; Remove past hours for this year, this month and
1625 (and (= year this-year)
1626 (= month this-month)
1628 (while (and (car hours)
1629 (< (car hours) this-hour))
1631 (while (setq hour (pop hours))
1632 (let ((minutes minute-list)
1634 ;; Remove past hours for this year, this month,
1635 ;; this day and this hour.
1636 (and (= year this-year)
1637 (= month this-month)
1640 (while (and (car minutes)
1641 (< (car minutes) this-minute))
1643 (while (setq minute (pop minutes))
1644 ;; Ouch! Here, we've got a complete valid
1645 ;; schedule. It's a good one if it's in the
1647 (let ((time (encode-time 0 minute hour day
1650 (and (time-less-p now time)
1651 (throw 'found time)))
1656 (nndiary-last-occurence sched))
1658 (nndiary-last-occurence sched))
1661 (defun nndiary-expired-article-p (file)
1663 (if (nnheader-insert-head file)
1664 (let ((sched (nndiary-schedule)))
1665 ;; An article has expired if its last schedule (if any) is in the
1666 ;; past. A permanent schedule never expires.
1668 (setq sched (nndiary-last-occurence sched))
1669 (time-less-p sched (current-time))))
1671 (nnheader-report 'nndiary "Could not read file %s" file)
1675 (defun nndiary-renew-article-p (file timestamp)
1677 (if (nnheader-insert-head file)
1678 (let ((now (current-time))
1679 (sched (nndiary-schedule)))
1680 ;; The article should be re-considered as unread if there's a reminder
1681 ;; between the group timestamp and the current time.
1682 (when (and sched (setq sched (nndiary-next-occurence sched now)))
1683 (let ((reminders ;; add the next occurence itself at the end.
1684 (append (nndiary-compute-reminders sched) (list sched))))
1685 (while (and reminders (time-less-p (car reminders) timestamp))
1687 ;; The reminders might be empty if the last date is in the past,
1688 ;; or we've got at least the next occurence itself left. All past
1689 ;; dates are renewed.
1691 (time-less-p (car reminders) now)))
1694 (nnheader-report 'nndiary "Could not read file %s" file)
1697 ;; The end... ===============================================================
1701 (let ((header (intern (format "X-Diary-%s" (car elt)))))
1702 ;; Required for building NOV databases and some other stuff
1703 (add-to-list 'gnus-extra-headers header)
1704 (add-to-list 'nnmail-extra-headers header)))
1707 (unless (assoc "nndiary" gnus-valid-select-methods)
1708 (gnus-declare-backend "nndiary" 'post-mail 'respool 'address))
1713 ;;; nndiary.el ends here