1 ;;; nndiary.el --- A diary backend for Gnus
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 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 2 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 this program; if not, write to the Free Software
25 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 ;; MA 02110-1301, USA.
31 ;; Contents management by FCM version 0.1.
36 ;; This package implements NNDiary, a diary backend for Gnus. NNDiary is a
37 ;; mail backend, pretty similar to nnml in its functionnning (it has all the
38 ;; features of nnml, actually), but in which messages are treated as event
41 ;; Here is a typical scenario:
42 ;; - You've got a date with Andy Mc Dowell or Bruce Willis (select according
43 ;; to your sexual preference) in one month. You don't want to forget it.
44 ;; - Send a (special) diary message to yourself (see below).
45 ;; - Forget all about it and keep on getting and reading new mail, as usual.
46 ;; - From time to time, as you type `g' in the group buffer and as the date
47 ;; is getting closer, the message will pop up again, just like if it were
49 ;; - Read your "new" messages, this one included, and start dreaming of the
50 ;; night you're gonna have.
51 ;; - Once the date is over (you actually fell asleep just after dinner), the
52 ;; message will be automatically deleted if it is marked as expirable.
54 ;; Some more notes on the diary backend:
55 ;; - NNDiary is a *real* mail backend. You *really* send real diary
56 ;; messsages. This means for instance that you can give appointements to
57 ;; anybody (provided they use Gnus and NNDiary) by sending the diary message
59 ;; - However, since NNDiary also has a 'request-post method, you can also
60 ;; `C-u a' instead of `C-u m' on a diary group and the message won't actually
61 ;; be sent; just stored in the group.
62 ;; - The events you want to remember need not be punctual. You can set up
63 ;; reminders for regular dates (like once each week, each monday at 13:30
64 ;; and so on). Diary messages of this kind will never be deleted (unless
65 ;; you do it explicitely). But that, you guessed.
71 ;; 1/ NNDiary has two modes of operation: traditional (the default) and
73 ;; a/ In traditional mode, NNDiary does not get new mail by itself. You
74 ;; have to move mails from your primary mail backend to nndiary
76 ;; b/ In autonomous mode, NNDiary retrieves its own mail and handles it
77 ;; independantly of your primary mail backend. To use NNDiary in
78 ;; autonomous mode, you have several things to do:
79 ;; i/ Put (setq nndiary-get-new-mail t) in your gnusrc file.
80 ;; ii/ Diary messages contain several `X-Diary-*' special headers.
81 ;; You *must* arrange that these messages be split in a private
82 ;; folder *before* Gnus treat them. You need this because Gnus
83 ;; is not able yet to manage multiple backends for mail
84 ;; retrieval. Getting them from a separate source will
85 ;; compensate this misfeature to some extent, as we will see.
86 ;; As an example, here's my procmailrc entry to store diary files
87 ;; in ~/.nndiary (the default nndiary mail source file):
92 ;; iii/ Customize the variables `nndiary-mail-sources' and
93 ;; `nndiary-split-methods'. These are replacements for the usual
94 ;; mail sources and split methods which, and will be used in
95 ;; autonomous mode. `nndiary-mail-sources' defaults to
96 ;; '(file :path "~/.nndiary").
97 ;; 2/ Install nndiary somewhere Emacs / Gnus can find it. Normally, you
98 ;; *don't* have to '(require 'nndiary) anywhere. Gnus will do so when
99 ;; appropriate as long as nndiary is somewhere in the load path.
100 ;; 3/ Now, customize the rest of nndiary. In particular, you should
101 ;; customize `nndiary-reminders', the list of times when you want to be
102 ;; reminded of your appointements (e.g. 3 weeks before, then 2 days
103 ;; before, then 1 hour before and that's it).
104 ;; 4/ You *must* use the group timestamp feature of Gnus. This adds a
105 ;; timestamp to each groups' parameters (please refer to the Gnus
106 ;; documentation ("Group Timestamp" info node) to see how it's done.
107 ;; 5/ Once you have done this, you may add a permanent nndiary virtual server
108 ;; (something like '(nndiary "")) to your `gnus-secondary-select-methods'.
109 ;; Yes, this server will be able to retrieve mails and split them when you
110 ;; type `g' in the group buffer, just as if it were your only mail backend.
111 ;; This is the benefit of using a private folder.
112 ;; 6/ Hopefully, almost everything (see the TODO section below) will work as
113 ;; expected when you restart Gnus: in the group buffer, `g' and `M-g' will
114 ;; also get your new diary mails, `F' will find your new diary groups etc.
117 ;; How to send diary messages:
118 ;; ==========================
120 ;; There are 7 special headers in diary messages. These headers are of the
121 ;; form `X-Diary-<something>', the <something> being one of `Minute', `Hour',
122 ;; `Dom', `Month', `Year', `Time-Zone' and `Dow'. `Dom' means "Day of Month",
123 ;; and `dow' means "Day of Week". These headers actually behave like crontab
124 ;; specifications and define the event date(s).
126 ;; For all headers but the `Time-Zone' one, a header value is either a
127 ;; star (meaning all possible values), or a list of fields (separated by a
128 ;; comma). A field is either an integer, or a range. A range is two integers
129 ;; separated by a dash. Possible integer values are 0-59 for `Minute', 0-23
130 ;; for `Hour', 1-31 for `Dom', `1-12' for Month, above 1971 for `Year' and 0-6
131 ;; for `Dow' (0 = sunday). As a special case, a star in either `Dom' or `Dow'
132 ;; doesn't mean "all possible values", but "use only the other field". Note
133 ;; that if both are star'ed, the use of either one gives the same result :-),
135 ;; The `Time-Zone' header is special in that it can have only one value (you
137 ;; A star doesn't mean "all possible values" (because it has no sense), but
138 ;; "the current local time zone".
140 ;; As an example, here's how you would say "Each Monday and each 1st of month,
141 ;; at 12:00, 20:00, 21:00, 22:00, 23:00 and 24:00, from 1999 to 2010" (I let
142 ;; you find what to do then):
145 ;; X-Diary-Hour: 12, 20-24
148 ;; X-Diary-Year: 1999-2010
150 ;; X-Diary-Time-Zone: *
153 ;; Sending a diary message is not different from sending any other kind of
154 ;; mail, except that such messages are identified by the presence of these
162 ;; * Respooling doesn't work because contrary to the request-scan function,
163 ;; Gnus won't allow me to override the split methods when calling the
164 ;; respooling backend functions.
165 ;; * There's a bug in the time zone mechanism with variable TZ locations.
166 ;; * We could allow a keyword like `ask' in X-Diary-* headers, that would mean
167 ;; "ask for value upon reception of the message".
168 ;; * We could add an optional header X-Diary-Reminders to specify a special
169 ;; reminders value for this message. Suggested by Jody Klymak.
170 ;; * We should check messages validity in other circumstances than just
171 ;; moving an article from sonwhere else (request-accept). For instance, when
172 ;; editing / saving and so on.
179 ;; NNDiary is very similar to nnml. This makes the idea of using nnoo (to
180 ;; derive nndiary from nnml) natural. However, my experience with nnoo is
181 ;; that for reasonably complex backends like this one, noo is a burden
182 ;; rather than an help. It's tricky to use, not everything can be
183 ;; inherited, what can be inherited and when is not very clear, and you've
184 ;; got to be very careful because a little mistake can fuck up your your
185 ;; other backends, especially because their variables will be use instead of
186 ;; your real ones. Finally, I found it easier to just clone the needed
187 ;; parts of nnml, and tracking nnml updates is not a big deal.
189 ;; IMHO, nnoo is actually badly designed. A much simpler, and yet more
190 ;; powerful one would be to make *real* functions and variables for a new
191 ;; backend based on another. Lisp is a reflexive language so that's a very
192 ;; easy thing to do: inspect the function's form, replace occurences of
193 ;; <nnfrom> (even in strings) with <nnto>, and you're done.
195 ;; * nndiary-get-new-mail, nndiary-mail-source and nndiary-split-methods:
196 ;; NNDiary has some experimental parts, in the sense Gnus normally uses only
197 ;; one mail backends for mail retreival and splitting. This backend is also
198 ;; an attempt to make it behave differently. For Gnus developpers: as you
199 ;; can see if you snarf into the code, that was not a very difficult thing
200 ;; to do. Something should be done about the respooling breakage though.
208 (eval-when-compile (require 'cl
))
210 (require 'gnus-start
)
213 ;; Compatibility Functions =================================================
216 (if (fboundp 'signal-error
)
217 (defun nndiary-error (&rest args
)
218 (apply #'signal-error
'nndiary args
))
219 (defun nndiary-error (&rest args
)
220 (apply #'error args
))))
223 ;; Backend behavior customization ===========================================
225 (defgroup nndiary nil
226 "The Gnus Diary backend."
230 (defcustom nndiary-mail-sources
231 `((file :path
,(expand-file-name "~/.nndiary")))
232 "*NNDiary specific mail sources.
233 This variable is used by nndiary in place of the standard `mail-sources'
234 variable when `nndiary-get-new-mail' is set to non-nil. These sources
235 must contain diary messages ONLY."
240 (defcustom nndiary-split-methods
'(("diary" ""))
241 "*NNDiary specific split methods.
242 This variable is used by nndiary in place of the standard
243 `nnmail-split-methods' variable when `nndiary-get-new-mail' is set to
247 :type
'(choice (repeat :tag
"Alist" (group (string :tag
"Name") regexp
))
248 (function-item nnmail-split-fancy
)
249 (function :tag
"Other")))
252 (defcustom nndiary-reminders
'((0 . day
))
253 "*Different times when you want to be reminded of your appointements.
254 Diary articles will appear again, as if they'd been just received.
256 Entries look like (3 . day) which means something like \"Please
257 Hortense, would you be so kind as to remind me of my appointments 3 days
258 before the date, thank you very much. Anda, hmmm... by the way, are you
259 doing anything special tonight ?\".
261 The units of measure are 'minute 'hour 'day 'week 'month and 'year (no,
262 not 'century, sorry).
264 NOTE: the units of measure actually express dates, not durations: if you
265 use 'week, messages will pop up on Sundays at 00:00 (or Mondays if
266 `nndiary-week-starts-on-monday' is non nil) and *not* 7 days before the
267 appointement, if you use 'month, messages will pop up on the first day of
268 each months, at 00:00 and so on.
270 If you really want to specify a duration (like 24 hours exactly), you can
271 use the equivalent in minutes (the smallest unit). A fuzz of 60 seconds
272 maximum in the reminder is not that painful, I think. Although this
273 scheme might appear somewhat weird at a first glance, it is very powerful.
274 In order to make this clear, here are some examples:
276 - '(0 . day): this is the default value of `nndiary-reminders'. It means
277 pop up the appointements of the day each morning at 00:00.
279 - '(1 . day): this means pop up the appointements the day before, at 00:00.
281 - '(6 . hour): for an appointement at 18:30, this would pop up the
282 appointement message at 12:00.
284 - '(360 . minute): for an appointement at 18:30 and 15 seconds, this would
285 pop up the appointement message at 12:30."
287 :type
'(repeat (cons :format
"%v\n"
288 (integer :format
"%v")
289 (choice :format
"%[%v(s)%] before...\n"
291 (const :format
"%v" minute
)
292 (const :format
"%v" hour
)
293 (const :format
"%v" day
)
294 (const :format
"%v" week
)
295 (const :format
"%v" month
)
296 (const :format
"%v" year
)))))
298 (defcustom nndiary-week-starts-on-monday nil
299 "*Whether a week starts on monday (otherwise, sunday)."
304 (defcustom nndiary-request-create-group-hooks nil
305 "*Hooks to run after `nndiary-request-create-group' is executed.
306 The hooks will be called with the full group name as argument."
310 (defcustom nndiary-request-update-info-hooks nil
311 "*Hooks to run after `nndiary-request-update-info-group' is executed.
312 The hooks will be called with the full group name as argument."
316 (defcustom nndiary-request-accept-article-hooks nil
317 "*Hooks to run before accepting an article.
318 Executed near the beginning of `nndiary-request-accept-article'.
319 The hooks will be called with the article in the current buffer."
323 (defcustom nndiary-check-directory-twice t
324 "*If t, check directories twice to avoid NFS failures."
329 ;; Backend declaration ======================================================
331 ;; Well, most of this is nnml clonage.
333 (nnoo-declare nndiary
)
335 (defvoo nndiary-directory
(nnheader-concat gnus-directory
"diary/")
336 "Spool directory for the nndiary backend.")
338 (defvoo nndiary-active-file
339 (expand-file-name "active" nndiary-directory
)
340 "Active file for the nndiary backend.")
342 (defvoo nndiary-newsgroups-file
343 (expand-file-name "newsgroups" nndiary-directory
)
344 "Newsgroups description file for the nndiary backend.")
346 (defvoo nndiary-get-new-mail nil
347 "Whether nndiary gets new mail and split it.
348 Contrary to traditional mail backends, this variable can be set to t
349 even if your primary mail backend also retreives mail. In such a case,
350 NDiary uses its own mail-sources and split-methods.")
352 (defvoo nndiary-nov-is-evil nil
353 "If non-nil, Gnus will never use nov databases for nndiary groups.
354 Using nov databases will speed up header fetching considerably.
355 This variable shouldn't be flipped much. If you have, for some reason,
356 set this to t, and want to set it to nil again, you should always run
357 the `nndiary-generate-nov-databases' command. The function will go
358 through all nnml directories and generate nov databases for them
359 all. This may very well take some time.")
361 (defvoo nndiary-prepare-save-mail-hook nil
362 "*Hook run narrowed to an article before saving.")
364 (defvoo nndiary-inhibit-expiry nil
365 "If non-nil, inhibit expiry.")
369 (defconst nndiary-version
"0.2-b14"
370 "Current Diary backend version.")
372 (defun nndiary-version ()
373 "Current Diary backend version."
375 (message "NNDiary version %s" nndiary-version
))
377 (defvoo nndiary-nov-file-name
".overview")
379 (defvoo nndiary-current-directory nil
)
380 (defvoo nndiary-current-group nil
)
381 (defvoo nndiary-status-string
"" )
382 (defvoo nndiary-nov-buffer-alist nil
)
383 (defvoo nndiary-group-alist nil
)
384 (defvoo nndiary-active-timestamp nil
)
385 (defvoo nndiary-article-file-alist nil
)
387 (defvoo nndiary-generate-active-function
'nndiary-generate-active-info
)
388 (defvoo nndiary-nov-buffer-file-name nil
)
389 (defvoo nndiary-file-coding-system nnmail-file-coding-system
)
391 (defconst nndiary-headers
398 ("Time-Zone" (("Y" -
43200)
475 ;; List of NNDiary headers that specify the time spec. Each header name is
476 ;; followed by either two integers (specifying a range of possible values
477 ;; for this header) or one list (specifying all the possible values for this
478 ;; header). In the latter case, the list does NOT include the unspecifyed
480 ;; For time zone values, we have symbolic time zone names associated with
481 ;; the (relative) number of seconds ahead GMT.
484 (defsubst nndiary-schedule
()
489 (setq head
(nth 0 elt
))
490 (nndiary-parse-schedule (nth 0 elt
) (nth 1 elt
) (nth 2 elt
)))
493 (nnheader-report 'nndiary
"X-Diary-%s header parse error: %s."
498 ;;; Interface functions =====================================================
500 (nnoo-define-basics nndiary
)
502 (deffoo nndiary-retrieve-headers
(sequence &optional group server fetch-old
)
503 (when (nndiary-possibly-change-directory group server
)
505 (set-buffer nntp-server-buffer
)
508 (number (length sequence
))
510 (file-name-coding-system nnmail-pathname-coding-system
)
512 (nndiary-check-directory-twice
513 (and nndiary-check-directory-twice
514 ;; To speed up, disable it in some case.
515 (or (not (numberp nnmail-large-newsgroup
))
516 (<= number nnmail-large-newsgroup
)))))
517 (if (stringp (car sequence
))
519 (if (nndiary-retrieve-headers-with-nov sequence fetch-old
)
522 (setq article
(car sequence
))
523 (setq file
(nndiary-article-to-file article
))
526 (not (file-directory-p file
)))
527 (insert (format "221 %d Article retrieved.\n" article
))
529 (nnheader-insert-head file
)
531 (if (search-forward "\n\n" nil t
)
533 (goto-char (point-max))
536 (delete-region (point) (point-max)))
537 (setq sequence
(cdr sequence
))
538 (setq count
(1+ count
))
539 (and (numberp nnmail-large-newsgroup
)
540 (> number nnmail-large-newsgroup
)
542 (nnheader-message 6 "nndiary: Receiving headers... %d%%"
543 (/ (* count
100) number
))))
545 (and (numberp nnmail-large-newsgroup
)
546 (> number nnmail-large-newsgroup
)
547 (nnheader-message 6 "nndiary: Receiving headers...done"))
549 (nnheader-fold-continuation-lines)
552 (deffoo nndiary-open-server
(server &optional defs
)
553 (nnoo-change-server 'nndiary server defs
)
554 (when (not (file-exists-p nndiary-directory
))
555 (ignore-errors (make-directory nndiary-directory t
)))
557 ((not (file-exists-p nndiary-directory
))
558 (nndiary-close-server)
559 (nnheader-report 'nndiary
"Couldn't create directory: %s"
561 ((not (file-directory-p (file-truename nndiary-directory
)))
562 (nndiary-close-server)
563 (nnheader-report 'nndiary
"Not a directory: %s" nndiary-directory
))
565 (nnheader-report 'nndiary
"Opened server %s using directory %s"
566 server nndiary-directory
)
569 (deffoo nndiary-request-regenerate
(server)
570 (nndiary-possibly-change-directory nil server
)
571 (nndiary-generate-nov-databases server
)
574 (deffoo nndiary-request-article
(id &optional group server buffer
)
575 (nndiary-possibly-change-directory group server
)
576 (let* ((nntp-server-buffer (or buffer nntp-server-buffer
))
577 (file-name-coding-system nnmail-pathname-coding-system
)
578 path gpath group-num
)
580 (when (and (setq group-num
(nndiary-find-group-number id
))
582 (assq (cdr group-num
)
583 (nnheader-article-to-file-alist
585 (nnmail-group-pathname
587 nndiary-directory
))))))
588 (setq path
(concat gpath
(int-to-string (cdr group-num
)))))
589 (setq path
(nndiary-article-to-file id
)))
592 (nnheader-report 'nndiary
"No such article: %s" id
))
593 ((not (file-exists-p path
))
594 (nnheader-report 'nndiary
"No such file: %s" path
))
595 ((file-directory-p path
)
596 (nnheader-report 'nndiary
"File is a directory: %s" path
))
597 ((not (save-excursion (let ((nnmail-file-coding-system
598 nndiary-file-coding-system
))
599 (nnmail-find-file path
))))
600 (nnheader-report 'nndiary
"Couldn't read file: %s" path
))
602 (nnheader-report 'nndiary
"Article %s retrieved" id
)
603 ;; We return the article number.
604 (cons (if group-num
(car group-num
) group
)
605 (string-to-number (file-name-nondirectory path
)))))))
607 (deffoo nndiary-request-group
(group &optional server dont-check
)
608 (let ((file-name-coding-system nnmail-pathname-coding-system
))
610 ((not (nndiary-possibly-change-directory group server
))
611 (nnheader-report 'nndiary
"Invalid group (no such directory)"))
612 ((not (file-exists-p nndiary-current-directory
))
613 (nnheader-report 'nndiary
"Directory %s does not exist"
614 nndiary-current-directory
))
615 ((not (file-directory-p nndiary-current-directory
))
616 (nnheader-report 'nndiary
"%s is not a directory"
617 nndiary-current-directory
))
619 (nnheader-report 'nndiary
"Group %s selected" group
)
622 (nnheader-re-read-dir nndiary-current-directory
)
623 (nnmail-activate 'nndiary
)
624 (let ((active (nth 1 (assoc group nndiary-group-alist
))))
626 (nnheader-report 'nndiary
"No such group: %s" group
)
627 (nnheader-report 'nndiary
"Selected group %s" group
)
628 (nnheader-insert "211 %d %d %d %s\n"
629 (max (1+ (- (cdr active
) (car active
))) 0)
630 (car active
) (cdr active
) group
)))))))
632 (deffoo nndiary-request-scan
(&optional group server
)
633 ;; Use our own mail sources and split methods while Gnus doesn't let us have
634 ;; multiple backends for retrieving mail.
635 (let ((mail-sources nndiary-mail-sources
)
636 (nnmail-split-methods nndiary-split-methods
))
637 (setq nndiary-article-file-alist nil
)
638 (nndiary-possibly-change-directory group server
)
639 (nnmail-get-new-mail 'nndiary
'nndiary-save-nov nndiary-directory group
)))
641 (deffoo nndiary-close-group
(group &optional server
)
642 (setq nndiary-article-file-alist nil
)
645 (deffoo nndiary-request-create-group
(group &optional server args
)
646 (nndiary-possibly-change-directory nil server
)
647 (nnmail-activate 'nndiary
)
649 ((assoc group nndiary-group-alist
)
651 ((and (file-exists-p (nnmail-group-pathname group nndiary-directory
))
652 (not (file-directory-p (nnmail-group-pathname
653 group nndiary-directory
))))
654 (nnheader-report 'nndiary
"%s is a file"
655 (nnmail-group-pathname group nndiary-directory
)))
658 (push (list group
(setq active
(cons 1 0)))
660 (nndiary-possibly-create-directory group
)
661 (nndiary-possibly-change-directory group server
)
662 (let ((articles (nnheader-directory-articles nndiary-current-directory
)))
664 (setcar active
(apply 'min articles
))
665 (setcdr active
(apply 'max articles
))))
666 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
667 (run-hook-with-args 'nndiary-request-create-group-hooks
668 (gnus-group-prefixed-name group
669 (list "nndiary" server
)))
673 (deffoo nndiary-request-list
(&optional server
)
675 (let ((nnmail-file-coding-system nnmail-active-file-coding-system
)
676 (file-name-coding-system nnmail-pathname-coding-system
))
677 (nnmail-find-file nndiary-active-file
))
678 (setq nndiary-group-alist
(nnmail-get-active))
681 (deffoo nndiary-request-newgroups
(date &optional server
)
682 (nndiary-request-list server
))
684 (deffoo nndiary-request-list-newsgroups
(&optional server
)
686 (nnmail-find-file nndiary-newsgroups-file
)))
688 (deffoo nndiary-request-expire-articles
(articles group
&optional server force
)
689 (nndiary-possibly-change-directory group server
)
690 (let ((active-articles
691 (nnheader-directory-articles nndiary-current-directory
))
693 (nnmail-activate 'nndiary
)
694 ;; Articles not listed in active-articles are already gone,
695 ;; so don't try to expire them.
696 (setq articles
(gnus-intersection articles active-articles
))
698 (setq article
(nndiary-article-to-file (setq number
(pop articles
))))
699 (if (and (nndiary-deletable-article-p group number
)
700 ;; Don't use nnmail-expired-article-p. Our notion of expiration
701 ;; is a bit peculiar ...
702 (or force
(nndiary-expired-article-p article
)))
704 ;; Allow a special target group.
705 (unless (eq nnmail-expiry-target
'delete
)
707 (nndiary-request-article number group server
(current-buffer))
708 (let ((nndiary-current-directory nil
))
709 (nnmail-expiry-target-group nnmail-expiry-target group
)))
710 (nndiary-possibly-change-directory group server
))
711 (nnheader-message 5 "Deleting article %s in %s" number group
)
713 (funcall nnmail-delete-file-function article
)
714 (file-error (push number rest
)))
715 (setq active-articles
(delq number active-articles
))
716 (nndiary-nov-delete-article group number
))
718 (let ((active (nth 1 (assoc group nndiary-group-alist
))))
720 (setcar active
(or (and active-articles
721 (apply 'min active-articles
))
723 (nnmail-save-active nndiary-group-alist nndiary-active-file
))
725 (nconc rest articles
)))
727 (deffoo nndiary-request-move-article
728 (article group server accept-form
&optional last
)
729 (let ((buf (get-buffer-create " *nndiary move*"))
731 (nndiary-possibly-change-directory group server
)
732 (nndiary-update-file-alist)
734 (nndiary-deletable-article-p group article
)
735 (nndiary-request-article article group server
)
736 (let (nndiary-current-directory
737 nndiary-current-group
738 nndiary-article-file-alist
)
741 (insert-buffer-substring nntp-server-buffer
)
742 (setq result
(eval accept-form
))
743 (kill-buffer (current-buffer))
746 (nndiary-possibly-change-directory group server
)
748 (funcall nnmail-delete-file-function
749 (nndiary-article-to-file article
))
751 (nndiary-nov-delete-article group article
)
754 (nnmail-save-active nndiary-group-alist nndiary-active-file
))))
757 (deffoo nndiary-request-accept-article
(group &optional server last
)
758 (nndiary-possibly-change-directory group server
)
759 (nnmail-check-syntax)
760 (run-hooks 'nndiary-request-accept-article-hooks
)
761 (when (nndiary-schedule)
763 (when nnmail-cache-accepted-message-ids
764 (nnmail-cache-insert (nnmail-fetch-field "message-id")
766 (nnmail-fetch-field "subject")))
769 (nnmail-activate 'nndiary
)
771 (car (nndiary-save-mail
772 (list (cons group
(nndiary-active-number group
))))))
774 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
775 (and last
(nndiary-save-nov))))
777 (nnmail-activate 'nndiary
)
778 (if (and (not (setq result
779 (nnmail-article-group 'nndiary-active-number
)))
780 (yes-or-no-p "Moved to `junk' group; delete article? "))
782 (setq result
(car (nndiary-save-mail result
))))
784 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
785 (when nnmail-cache-accepted-message-ids
786 (nnmail-cache-close))
787 (nndiary-save-nov))))
791 (deffoo nndiary-request-post
(&optional server
)
792 (nnmail-do-request-post 'nndiary-request-accept-article server
))
794 (deffoo nndiary-request-replace-article
(article group buffer
)
795 (nndiary-possibly-change-directory group
)
798 (nndiary-possibly-create-directory group
)
799 (let ((chars (nnmail-insert-lines))
800 (art (concat (int-to-string article
) "\t"))
804 (point-min) (point-max)
805 (or (nndiary-article-to-file article
)
806 (expand-file-name (int-to-string article
)
807 nndiary-current-directory
))
808 nil
(if (nnheader-be-verbose 5) nil
'nomesg
))
810 (setq headers
(nndiary-parse-head chars article
))
811 ;; Replace the NOV line in the NOV file.
813 (set-buffer (nndiary-open-nov group
))
814 (goto-char (point-min))
815 (if (or (looking-at art
)
816 (search-forward (concat "\n" art
) nil t
))
817 ;; Delete the old NOV line.
818 (delete-region (progn (beginning-of-line) (point))
819 (progn (forward-line 1) (point)))
820 ;; The line isn't here, so we have to find out where
821 ;; we should insert it. (This situation should never
822 ;; occur, but one likes to make sure...)
823 (while (and (looking-at "[0-9]+\t")
826 (match-beginning 0) (match-end 0)))
828 (zerop (forward-line 1)))))
830 (nnheader-insert-nov headers
)
834 (deffoo nndiary-request-delete-group
(group &optional force server
)
835 (nndiary-possibly-change-directory group server
)
837 ;; Delete all articles in GROUP.
840 nndiary-current-directory t
841 (concat nnheader-numerical-short-files
842 "\\|" (regexp-quote nndiary-nov-file-name
) "$")))
845 (setq article
(pop articles
))
846 (when (file-writable-p article
)
847 (nnheader-message 5 "Deleting article %s in %s..." article group
)
848 (funcall nnmail-delete-file-function article
))))
849 ;; Try to delete the directory itself.
850 (ignore-errors (delete-directory nndiary-current-directory
)))
851 ;; Remove the group from all structures.
852 (setq nndiary-group-alist
853 (delq (assoc group nndiary-group-alist
) nndiary-group-alist
)
854 nndiary-current-group nil
855 nndiary-current-directory nil
)
856 ;; Save the active file.
857 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
860 (deffoo nndiary-request-rename-group
(group new-name
&optional server
)
861 (nndiary-possibly-change-directory group server
)
862 (let ((new-dir (nnmail-group-pathname new-name nndiary-directory
))
863 (old-dir (nnmail-group-pathname group nndiary-directory
)))
865 (make-directory new-dir t
)
867 ;; We move the articles file by file instead of renaming
868 ;; the directory -- there may be subgroups in this group.
869 ;; One might be more clever, I guess.
870 (let ((files (nnheader-article-to-file-alist old-dir
)))
873 (concat old-dir
(cdar files
))
874 (concat new-dir
(cdar files
)))
876 ;; Move .overview file.
877 (let ((overview (concat old-dir nndiary-nov-file-name
)))
878 (when (file-exists-p overview
)
879 (rename-file overview
(concat new-dir nndiary-nov-file-name
))))
880 (when (<= (length (directory-files old-dir
)) 2)
881 (ignore-errors (delete-directory old-dir
)))
882 ;; That went ok, so we change the internal structures.
883 (let ((entry (assoc group nndiary-group-alist
)))
885 (setcar entry new-name
))
886 (setq nndiary-current-directory nil
887 nndiary-current-group nil
)
888 ;; Save the new group alist.
889 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
892 (deffoo nndiary-set-status
(article name value
&optional group server
)
893 (nndiary-possibly-change-directory group server
)
894 (let ((file (nndiary-article-to-file article
)))
896 ((not (file-exists-p file
))
897 (nnheader-report 'nndiary
"File %s does not exist" file
))
900 (nnheader-insert-file-contents file
)
901 (nnmail-replace-status name value
))
905 ;;; Interface optional functions ============================================
907 (deffoo nndiary-request-update-info
(group info
&optional server
)
908 (nndiary-possibly-change-directory group
)
909 (let ((timestamp (gnus-group-parameter-value (gnus-info-params info
)
912 (nnheader-report 'nndiary
"Group %s doesn't have a timestamp" group
)
914 ;; Figure out which articles should be re-new'ed
915 (let ((articles (nndiary-flatten (gnus-info-read info
) 0))
916 article file unread buf
)
918 (setq buf
(nnheader-set-temp-buffer " *nndiary update*"))
919 (while (setq article
(pop articles
))
920 (setq file
(concat nndiary-current-directory
921 (int-to-string article
)))
922 (and (file-exists-p file
)
923 (nndiary-renew-article-p file timestamp
)
924 (push article unread
)))
925 ;;(message "unread: %s" unread)
928 (setq unread
(sort unread
'<))
930 (gnus-info-set-read info
(gnus-update-read-articles
931 (gnus-info-group info
) unread t
)))
933 (run-hook-with-args 'nndiary-request-update-info-hooks
934 (gnus-info-group info
))
939 ;;; Internal functions ======================================================
941 (defun nndiary-article-to-file (article)
942 (nndiary-update-file-alist)
944 (if (setq file
(cdr (assq article nndiary-article-file-alist
)))
945 (expand-file-name file nndiary-current-directory
)
946 ;; Just to make sure nothing went wrong when reading over NFS --
948 (if nndiary-check-directory-twice
950 (setq file
(expand-file-name (number-to-string article
)
951 nndiary-current-directory
)))
952 (nndiary-update-file-alist t
)
955 (defun nndiary-deletable-article-p (group article
)
956 "Say whether ARTICLE in GROUP can be deleted."
958 (when (setq path
(nndiary-article-to-file article
))
959 (when (file-writable-p path
)
960 (or (not nnmail-keep-last-article
)
961 (not (eq (cdr (nth 1 (assoc group nndiary-group-alist
)))
964 ;; Find an article number in the current group given the Message-ID.
965 (defun nndiary-find-group-number (id)
967 (set-buffer (get-buffer-create " *nndiary id*"))
968 (let ((alist nndiary-group-alist
)
970 ;; We want to look through all .overview files, but we want to
971 ;; start with the one in the current directory. It seems most
972 ;; likely that the article we are looking for is in that group.
973 (if (setq number
(nndiary-find-id nndiary-current-group id
))
974 (cons nndiary-current-group number
)
975 ;; It wasn't there, so we look through the other groups as well.
976 (while (and (not number
)
978 (or (string= (caar alist
) nndiary-current-group
)
979 (setq number
(nndiary-find-id (caar alist
) id
)))
981 (setq alist
(cdr alist
))))
983 (cons (caar alist
) number
))))))
985 (defun nndiary-find-id (group id
)
987 (let ((nov (expand-file-name nndiary-nov-file-name
988 (nnmail-group-pathname group
991 (when (file-exists-p nov
)
992 (nnheader-insert-file-contents nov
)
993 (while (and (not found
)
994 (search-forward id nil t
)) ; We find the ID.
995 ;; And the id is in the fourth field.
996 (if (not (and (search-backward "\t" nil t
4)
997 (not (search-backward"\t" (gnus-point-at-bol) t
))))
1001 ;; We return the article number.
1003 (ignore-errors (read (current-buffer))))))
1006 (defun nndiary-retrieve-headers-with-nov (articles &optional fetch-old
)
1007 (if (or gnus-nov-is-evil nndiary-nov-is-evil
)
1009 (let ((nov (expand-file-name nndiary-nov-file-name
1010 nndiary-current-directory
)))
1011 (when (file-exists-p nov
)
1013 (set-buffer nntp-server-buffer
)
1015 (nnheader-insert-file-contents nov
)
1017 (not (numberp fetch-old
)))
1018 t
; Don't remove anything.
1019 (nnheader-nov-delete-outside-range
1020 (if fetch-old
(max 1 (- (car articles
) fetch-old
))
1022 (car (last articles
)))
1025 (defun nndiary-possibly-change-directory (group &optional server
)
1027 (not (nndiary-server-opened server
)))
1028 (nndiary-open-server server
))
1031 (let ((pathname (nnmail-group-pathname group nndiary-directory
))
1032 (file-name-coding-system nnmail-pathname-coding-system
))
1033 (when (not (equal pathname nndiary-current-directory
))
1034 (setq nndiary-current-directory pathname
1035 nndiary-current-group group
1036 nndiary-article-file-alist nil
))
1037 (file-exists-p nndiary-current-directory
))))
1039 (defun nndiary-possibly-create-directory (group)
1040 (let ((dir (nnmail-group-pathname group nndiary-directory
)))
1041 (unless (file-exists-p dir
)
1042 (make-directory (directory-file-name dir
) t
)
1043 (nnheader-message 5 "Creating mail directory %s" dir
))))
1045 (defun nndiary-save-mail (group-art)
1046 "Called narrowed to an article."
1047 (let (chars headers
)
1048 (setq chars
(nnmail-insert-lines))
1049 (nnmail-insert-xref group-art
)
1050 (run-hooks 'nnmail-prepare-save-mail-hook
)
1051 (run-hooks 'nndiary-prepare-save-mail-hook
)
1052 (goto-char (point-min))
1053 (while (looking-at "From ")
1054 (replace-match "X-From-Line: ")
1056 ;; We save the article in all the groups it belongs in.
1057 (let ((ga group-art
)
1060 (nndiary-possibly-create-directory (caar ga
))
1061 (let ((file (concat (nnmail-group-pathname
1062 (caar ga
) nndiary-directory
)
1063 (int-to-string (cdar ga
)))))
1065 ;; It was already saved, so we just make a hard link.
1066 (funcall nnmail-crosspost-link-function first file t
)
1067 ;; Save the article.
1068 (nnmail-write-region (point-min) (point-max) file nil
1069 (if (nnheader-be-verbose 5) nil
'nomesg
))
1071 (setq ga
(cdr ga
))))
1072 ;; Generate a nov line for this article. We generate the nov
1073 ;; line after saving, because nov generation destroys the
1075 (setq headers
(nndiary-parse-head chars
))
1076 ;; Output the nov line to all nov databases that should have it.
1077 (let ((ga group-art
))
1079 (nndiary-add-nov (caar ga
) (cdar ga
) headers
)
1080 (setq ga
(cdr ga
))))
1083 (defun nndiary-active-number (group)
1084 "Compute the next article number in GROUP."
1085 (let ((active (cadr (assoc group nndiary-group-alist
))))
1086 ;; The group wasn't known to nndiary, so we just create an active
1089 ;; Perhaps the active file was corrupt? See whether
1090 ;; there are any articles in this group.
1091 (nndiary-possibly-create-directory group
)
1092 (nndiary-possibly-change-directory group
)
1093 (unless nndiary-article-file-alist
1094 (setq nndiary-article-file-alist
1096 (nnheader-article-to-file-alist nndiary-current-directory
)
1097 'car-less-than-car
)))
1099 (if nndiary-article-file-alist
1100 (cons (caar nndiary-article-file-alist
)
1101 (caar (last nndiary-article-file-alist
)))
1103 (push (list group active
) nndiary-group-alist
))
1104 (setcdr active
(1+ (cdr active
)))
1105 (while (file-exists-p
1106 (expand-file-name (int-to-string (cdr active
))
1107 (nnmail-group-pathname group nndiary-directory
)))
1108 (setcdr active
(1+ (cdr active
))))
1111 (defun nndiary-add-nov (group article headers
)
1112 "Add a nov line for the GROUP base."
1114 (set-buffer (nndiary-open-nov group
))
1115 (goto-char (point-max))
1116 (mail-header-set-number headers article
)
1117 (nnheader-insert-nov headers
)))
1119 (defsubst nndiary-header-value
()
1120 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
1122 (defun nndiary-parse-head (chars &optional number
)
1123 "Parse the head of the current buffer."
1126 (unless (zerop (buffer-size))
1128 (goto-char (point-min))
1129 (if (search-forward "\n\n" nil t
) (1- (point)) (point-max))))
1130 (let ((headers (nnheader-parse-naked-head)))
1131 (mail-header-set-chars headers chars
)
1132 (mail-header-set-number headers number
)
1135 (defun nndiary-open-nov (group)
1136 (or (cdr (assoc group nndiary-nov-buffer-alist
))
1137 (let ((buffer (get-buffer-create (format " *nndiary overview %s*"
1141 (set (make-local-variable 'nndiary-nov-buffer-file-name
)
1143 nndiary-nov-file-name
1144 (nnmail-group-pathname group nndiary-directory
)))
1146 (when (file-exists-p nndiary-nov-buffer-file-name
)
1147 (nnheader-insert-file-contents nndiary-nov-buffer-file-name
)))
1148 (push (cons group buffer
) nndiary-nov-buffer-alist
)
1151 (defun nndiary-save-nov ()
1153 (while nndiary-nov-buffer-alist
1154 (when (buffer-name (cdar nndiary-nov-buffer-alist
))
1155 (set-buffer (cdar nndiary-nov-buffer-alist
))
1156 (when (buffer-modified-p)
1157 (nnmail-write-region 1 (point-max) nndiary-nov-buffer-file-name
1159 (set-buffer-modified-p nil
)
1160 (kill-buffer (current-buffer)))
1161 (setq nndiary-nov-buffer-alist
(cdr nndiary-nov-buffer-alist
)))))
1164 (defun nndiary-generate-nov-databases (&optional server
)
1165 "Generate NOV databases in all nndiary directories."
1166 (interactive (list (or (nnoo-current-server 'nndiary
) "")))
1167 ;; Read the active file to make sure we don't re-use articles
1168 ;; numbers in empty groups.
1169 (nnmail-activate 'nndiary
)
1170 (unless (nndiary-server-opened server
)
1171 (nndiary-open-server server
))
1172 (setq nndiary-directory
(expand-file-name nndiary-directory
))
1173 ;; Recurse down the directories.
1174 (nndiary-generate-nov-databases-1 nndiary-directory nil t
)
1175 ;; Save the active file.
1176 (nnmail-save-active nndiary-group-alist nndiary-active-file
))
1178 (defun nndiary-generate-nov-databases-1 (dir &optional seen no-active
)
1179 "Regenerate the NOV database in DIR."
1180 (interactive "DRegenerate NOV in: ")
1181 (setq dir
(file-name-as-directory dir
))
1182 ;; Only scan this sub-tree if we haven't been here yet.
1183 (unless (member (file-truename dir
) seen
)
1184 (push (file-truename dir
) seen
)
1185 ;; We descend recursively
1186 (let ((dirs (directory-files dir t nil t
))
1188 (while (setq dir
(pop dirs
))
1189 (when (and (not (string-match "^\\." (file-name-nondirectory dir
)))
1190 (file-directory-p dir
))
1191 (nndiary-generate-nov-databases-1 dir seen
))))
1192 ;; Do this directory.
1193 (let ((files (sort (nnheader-article-to-file-alist dir
)
1194 'car-less-than-car
)))
1196 (let* ((group (nnheader-file-to-group
1197 (directory-file-name dir
) nndiary-directory
))
1198 (info (cadr (assoc group nndiary-group-alist
))))
1200 (setcar info
(1+ (cdr info
)))))
1201 (funcall nndiary-generate-active-function dir
)
1202 ;; Generate the nov file.
1203 (nndiary-generate-nov-file dir files
)
1205 (nnmail-save-active nndiary-group-alist nndiary-active-file
))))))
1207 (eval-when-compile (defvar files
))
1208 (defun nndiary-generate-active-info (dir)
1209 ;; Update the active info for this group.
1210 (let* ((group (nnheader-file-to-group
1211 (directory-file-name dir
) nndiary-directory
))
1212 (entry (assoc group nndiary-group-alist
))
1213 (last (or (caadr entry
) 0)))
1214 (setq nndiary-group-alist
(delq entry nndiary-group-alist
))
1216 (cons (or (caar files
) (1+ last
))
1218 (or (let ((f files
))
1219 (while (cdr f
) (setq f
(cdr f
)))
1222 nndiary-group-alist
)))
1224 (defun nndiary-generate-nov-file (dir files
)
1225 (let* ((dir (file-name-as-directory dir
))
1226 (nov (concat dir nndiary-nov-file-name
))
1227 (nov-buffer (get-buffer-create " *nov*"))
1230 ;; Init the nov buffer.
1231 (set-buffer nov-buffer
)
1232 (buffer-disable-undo)
1234 (set-buffer nntp-server-buffer
)
1235 ;; Delete the old NOV file.
1236 (when (file-exists-p nov
)
1237 (funcall nnmail-delete-file-function nov
))
1239 (unless (file-directory-p (setq file
(concat dir
(cdar files
))))
1241 (nnheader-insert-file-contents file
)
1243 (goto-char (point-min))
1245 (search-forward "\n\n" nil t
)
1246 (setq chars
(- (point-max) (point)))
1247 (max 1 (1- (point)))))
1248 (unless (zerop (buffer-size))
1249 (goto-char (point-min))
1250 (setq headers
(nndiary-parse-head chars
(caar files
)))
1252 (set-buffer nov-buffer
)
1253 (goto-char (point-max))
1254 (nnheader-insert-nov headers
)))
1256 (setq files
(cdr files
)))
1258 (set-buffer nov-buffer
)
1259 (nnmail-write-region 1 (point-max) nov nil
'nomesg
)
1260 (kill-buffer (current-buffer))))))
1262 (defun nndiary-nov-delete-article (group article
)
1264 (set-buffer (nndiary-open-nov group
))
1265 (when (nnheader-find-nov-line article
)
1266 (delete-region (point) (progn (forward-line 1) (point)))
1268 (let ((active (cadr (assoc group nndiary-group-alist
)))
1272 (setf (car active
) (1+ (cdr active
)))
1273 (when (and (setq num
(ignore-errors (read (current-buffer))))
1275 (setf (car active
) num
)))))))
1278 (defun nndiary-update-file-alist (&optional force
)
1279 (when (or (not nndiary-article-file-alist
)
1281 (setq nndiary-article-file-alist
1282 (nnheader-article-to-file-alist nndiary-current-directory
))))
1285 (defun nndiary-string-to-number (str min
&optional max
)
1286 ;; Like `string-to-number' but barf if STR is not exactly an integer, and not
1287 ;; within the specified bounds.
1288 ;; Signals are caught by `nndiary-schedule'.
1289 (if (not (string-match "^[ \t]*[0-9]+[ \t]*$" str
))
1290 (nndiary-error "not an integer value")
1292 (let ((val (string-to-number str
)))
1293 (and (or (< val min
)
1294 (and max
(> val max
)))
1295 (nndiary-error "value out of range"))
1298 (defun nndiary-parse-schedule-value (str min-or-values max
)
1299 ;; Parse the schedule string STR, or signal an error.
1300 ;; Signals are caught by `nndary-schedule'.
1301 (if (string-match "[ \t]*\\*[ \t]*" str
)
1305 (if (listp min-or-values
)
1306 ;; min-or-values is values
1307 ;; #### NOTE: this is actually only a hack for time zones.
1308 (let ((val (and (string-match "[ \t]*\\([^ \t]+\\)[ \t]*" str
)
1309 (match-string 1 str
))))
1310 (if (and val
(setq val
(assoc val min-or-values
)))
1312 (nndiary-error "invalid syntax")))
1313 ;; min-or-values is min
1316 (let ((res (split-string val
"-")))
1319 (nndiary-string-to-number (car res
) min-or-values max
))
1321 ;; don't know if crontab accepts this, but ensure
1322 ;; that BEG is <= END
1323 (let ((beg (nndiary-string-to-number (car res
) min-or-values max
))
1324 (end (nndiary-string-to-number (cadr res
) min-or-values max
)))
1332 (nndiary-error "invalid syntax")))
1334 (split-string str
",")))
1337 ;; ### FIXME: remove this function if it's used only once.
1338 (defun nndiary-parse-schedule (head min-or-values max
)
1339 ;; Parse the cron-like value of header X-Diary-HEAD in current buffer.
1340 ;; - Returns nil if `*'
1341 ;; - Otherwise returns a list of integers and/or ranges (BEG . END)
1342 ;; The exception is the Timze-Zone value which is always of the form (STR).
1343 ;; Signals are caught by `nndary-schedule'.
1344 (let ((header (format "^X-Diary-%s: \\(.*\\)$" head
)))
1345 (goto-char (point-min))
1346 (if (not (re-search-forward header nil t
))
1347 (nndiary-error "header missing")
1349 (nndiary-parse-schedule-value (match-string 1) min-or-values max
))
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 (setcdr (nthcdr 5 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 ;;; arch-tag: 9c542b95-92e7-4ace-a038-330ab296e203
1714 ;;; nndiary.el ends here