Add menu for M-x man
[emacs.git] / lisp / gnus / nndiary.el
blobc92952e7a0ca9b4477046cde36a642ed281b692b
1 ;;; nndiary.el --- A diary back end for Gnus
3 ;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
5 ;; Author: Didier Verna <didier@xemacs.org>
6 ;; Maintainer: Didier Verna <didier@xemacs.org>
7 ;; Created: Fri Jul 16 18:55:42 1999
8 ;; Keywords: calendar mail news
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; Contents management by FCM version 0.1.
30 ;; Description:
31 ;; ===========
33 ;; nndiary is a mail back end designed to handle mails as diary event
34 ;; reminders. It is now fully documented in the Gnus manual.
37 ;; Bugs / Todo:
38 ;; ===========
40 ;; * Respooling doesn't work because contrary to the request-scan function,
41 ;; Gnus won't allow me to override the split methods when calling the
42 ;; respooling back end functions.
43 ;; * There's a bug in the time zone mechanism with variable TZ locations.
44 ;; * We could allow a keyword like `ask' in X-Diary-* headers, that would mean
45 ;; "ask for value upon reception of the message".
46 ;; * We could add an optional header X-Diary-Reminders to specify a special
47 ;; reminders value for this message. Suggested by Jody Klymak.
48 ;; * We should check messages validity in other circumstances than just
49 ;; moving an article from somewhere else (request-accept). For instance,
50 ;; when editing / saving and so on.
53 ;; Remarks:
54 ;; =======
56 ;; * nnoo. NNDiary is very similar to nnml. This makes the idea of using nnoo
57 ;; (to derive nndiary from nnml) natural. However, my experience with nnoo
58 ;; is that for reasonably complex back ends like this one, nnoo is a burden
59 ;; rather than an help. It's tricky to use, not everything can be inherited,
60 ;; what can be inherited and when is not very clear, and you've got to be
61 ;; very careful because a little mistake can fuck up your other back ends,
62 ;; especially because their variables will be use instead of your real ones.
63 ;; Finally, I found it easier to just clone the needed parts of nnml, and
64 ;; tracking nnml updates is not a big deal.
66 ;; IMHO, nnoo is actually badly designed. A much simpler, and yet more
67 ;; powerful one would be to make *real* functions and variables for a new
68 ;; back end based on another. Lisp is a reflexive language so that's a very
69 ;; easy thing to do: inspect the function's form, replace occurrences of
70 ;; <nnfrom> (even in strings) with <nnto>, and you're done.
72 ;; * nndiary-get-new-mail, nndiary-mail-source and nndiary-split-methods:
73 ;; NNDiary has some experimental parts, in the sense Gnus normally uses only
74 ;; one mail back ends for mail retrieval and splitting. This back end is
75 ;; also an attempt to make it behave differently. For Gnus developers: as
76 ;; you can see if you snarf into the code, that was not a very difficult
77 ;; thing to do. Something should be done about the respooling breakage
78 ;; though.
81 ;;; Code:
83 (require 'nnoo)
84 (require 'nnheader)
85 (require 'nnmail)
86 (eval-when-compile (require 'cl))
88 (require 'gnus-start)
89 (require 'gnus-sum)
91 ;; Compatibility Functions =================================================
93 (eval-and-compile
94 (if (fboundp 'signal-error)
95 (defun nndiary-error (&rest args)
96 (apply #'signal-error 'nndiary args))
97 (defun nndiary-error (&rest args)
98 (apply #'error args))))
101 ;; Back End behavior customization ===========================================
103 (defgroup nndiary nil
104 "The Gnus Diary back end."
105 :version "22.1"
106 :group 'gnus-diary)
108 (defcustom nndiary-mail-sources
109 `((file :path ,(expand-file-name "~/.nndiary")))
110 "*NNDiary specific mail sources.
111 This variable is used by nndiary in place of the standard `mail-sources'
112 variable when `nndiary-get-new-mail' is set to non-nil. These sources
113 must contain diary messages ONLY."
114 :group 'nndiary
115 :group 'mail-source
116 :type 'sexp)
118 (defcustom nndiary-split-methods '(("diary" ""))
119 "*NNDiary specific split methods.
120 This variable is used by nndiary in place of the standard
121 `nnmail-split-methods' variable when `nndiary-get-new-mail' is set to
122 non-nil."
123 :group 'nndiary
124 :group 'nnmail-split
125 :type '(choice (repeat :tag "Alist" (group (string :tag "Name") regexp))
126 (function-item nnmail-split-fancy)
127 (function :tag "Other")))
130 (defcustom nndiary-reminders '((0 . day))
131 "*Different times when you want to be reminded of your appointments.
132 Diary articles will appear again, as if they'd been just received.
134 Entries look like (3 . day) which means something like \"Please
135 Hortense, would you be so kind as to remind me of my appointments 3 days
136 before the date, thank you very much. Anda, hmmm... by the way, are you
137 doing anything special tonight ?\".
139 The units of measure are 'minute 'hour 'day 'week 'month and 'year (no,
140 not 'century, sorry).
142 NOTE: the units of measure actually express dates, not durations: if you
143 use 'week, messages will pop up on Sundays at 00:00 (or Mondays if
144 `nndiary-week-starts-on-monday' is non-nil) and *not* 7 days before the
145 appointment, if you use 'month, messages will pop up on the first day of
146 each months, at 00:00 and so on.
148 If you really want to specify a duration (like 24 hours exactly), you can
149 use the equivalent in minutes (the smallest unit). A fuzz of 60 seconds
150 maximum in the reminder is not that painful, I think. Although this
151 scheme might appear somewhat weird at a first glance, it is very powerful.
152 In order to make this clear, here are some examples:
154 - '(0 . day): this is the default value of `nndiary-reminders'. It means
155 pop up the appointments of the day each morning at 00:00.
157 - '(1 . day): this means pop up the appointments the day before, at 00:00.
159 - '(6 . hour): for an appointment at 18:30, this would pop up the
160 appointment message at 12:00.
162 - '(360 . minute): for an appointment at 18:30 and 15 seconds, this would
163 pop up the appointment message at 12:30."
164 :group 'nndiary
165 :type '(repeat (cons :format "%v\n"
166 (integer :format "%v")
167 (choice :format "%[%v(s)%] before...\n"
168 :value day
169 (const :format "%v" minute)
170 (const :format "%v" hour)
171 (const :format "%v" day)
172 (const :format "%v" week)
173 (const :format "%v" month)
174 (const :format "%v" year)))))
176 (defcustom nndiary-week-starts-on-monday nil
177 "*Whether a week starts on monday (otherwise, sunday)."
178 :type 'boolean
179 :group 'nndiary)
182 (define-obsolete-variable-alias 'nndiary-request-create-group-hooks
183 'nndiary-request-create-group-functions "24.3")
184 (defcustom nndiary-request-create-group-functions nil
185 "*Hook run after `nndiary-request-create-group' is executed.
186 The hook functions will be called with the full group name as argument."
187 :group 'nndiary
188 :type 'hook)
190 (define-obsolete-variable-alias 'nndiary-request-update-info-hooks
191 'nndiary-request-update-info-functions "24.3")
192 (defcustom nndiary-request-update-info-functions nil
193 "*Hook run after `nndiary-request-update-info-group' is executed.
194 The hook functions will be called with the full group name as argument."
195 :group 'nndiary
196 :type 'hook)
198 (define-obsolete-variable-alias 'nndiary-request-accept-article-hooks
199 'nndiary-request-accept-article-functions "24.3")
200 (defcustom nndiary-request-accept-article-functions nil
201 "*Hook run before accepting an article.
202 Executed near the beginning of `nndiary-request-accept-article'.
203 The hook functions will be called with the article in the current buffer."
204 :group 'nndiary
205 :type 'hook)
207 (defcustom nndiary-check-directory-twice t
208 "*If t, check directories twice to avoid NFS failures."
209 :group 'nndiary
210 :type 'boolean)
213 ;; Back End declaration ======================================================
215 ;; Well, most of this is nnml clonage.
217 (nnoo-declare nndiary)
219 (defvoo nndiary-directory (nnheader-concat gnus-directory "diary/")
220 "Spool directory for the nndiary back end.")
222 (defvoo nndiary-active-file
223 (expand-file-name "active" nndiary-directory)
224 "Active file for the nndiary back end.")
226 (defvoo nndiary-newsgroups-file
227 (expand-file-name "newsgroups" nndiary-directory)
228 "Newsgroups description file for the nndiary back end.")
230 (defvoo nndiary-get-new-mail nil
231 "Whether nndiary gets new mail and split it.
232 Contrary to traditional mail back ends, this variable can be set to t
233 even if your primary mail back end also retrieves mail. In such a case,
234 NDiary uses its own mail-sources and split-methods.")
236 (defvoo nndiary-nov-is-evil nil
237 "If non-nil, Gnus will never use nov databases for nndiary groups.
238 Using nov databases will speed up header fetching considerably.
239 This variable shouldn't be flipped much. If you have, for some reason,
240 set this to t, and want to set it to nil again, you should always run
241 the `nndiary-generate-nov-databases' command. The function will go
242 through all nnml directories and generate nov databases for them
243 all. This may very well take some time.")
245 (defvoo nndiary-prepare-save-mail-hook nil
246 "*Hook run narrowed to an article before saving.")
248 (defvoo nndiary-inhibit-expiry nil
249 "If non-nil, inhibit expiry.")
253 (defconst nndiary-version "0.2-b14"
254 "Current Diary back end version.")
256 (defun nndiary-version ()
257 "Current Diary back end version."
258 (interactive)
259 (message "NNDiary version %s" nndiary-version))
261 (defvoo nndiary-nov-file-name ".overview")
263 (defvoo nndiary-current-directory nil)
264 (defvoo nndiary-current-group nil)
265 (defvoo nndiary-status-string "" )
266 (defvoo nndiary-nov-buffer-alist nil)
267 (defvoo nndiary-group-alist nil)
268 (defvoo nndiary-active-timestamp nil)
269 (defvoo nndiary-article-file-alist nil)
271 (defvoo nndiary-generate-active-function 'nndiary-generate-active-info)
272 (defvoo nndiary-nov-buffer-file-name nil)
273 (defvoo nndiary-file-coding-system nnmail-file-coding-system)
275 (defconst nndiary-headers
276 '(("Minute" 0 59)
277 ("Hour" 0 23)
278 ("Dom" 1 31)
279 ("Month" 1 12)
280 ("Year" 1971)
281 ("Dow" 0 6)
282 ("Time-Zone" (("Y" -43200)
284 ("X" -39600)
286 ("W" -36000)
288 ("V" -32400)
290 ("U" -28800)
291 ("PST" -28800)
293 ("T" -25200)
294 ("MST" -25200)
295 ("PDT" -25200)
297 ("S" -21600)
298 ("CST" -21600)
299 ("MDT" -21600)
301 ("R" -18000)
302 ("EST" -18000)
303 ("CDT" -18000)
305 ("Q" -14400)
306 ("AST" -14400)
307 ("EDT" -14400)
309 ("P" -10800)
310 ("ADT" -10800)
312 ("O" -7200)
314 ("N" -3600)
316 ("Z" 0)
317 ("GMT" 0)
318 ("UT" 0)
319 ("UTC" 0)
320 ("WET" 0)
322 ("A" 3600)
323 ("CET" 3600)
324 ("MET" 3600)
325 ("MEZ" 3600)
326 ("BST" 3600)
327 ("WEST" 3600)
329 ("B" 7200)
330 ("EET" 7200)
331 ("CEST" 7200)
332 ("MEST" 7200)
333 ("MESZ" 7200)
335 ("C" 10800)
337 ("D" 14400)
339 ("E" 18000)
341 ("F" 21600)
343 ("G" 25200)
345 ("H" 28800)
347 ("I" 32400)
348 ("JST" 32400)
350 ("K" 36000)
351 ("GST" 36000)
353 ("L" 39600)
355 ("M" 43200)
356 ("NZST" 43200)
358 ("NZDT" 46800))))
359 ;; List of NNDiary headers that specify the time spec. Each header name is
360 ;; followed by either two integers (specifying a range of possible values
361 ;; for this header) or one list (specifying all the possible values for this
362 ;; header). In the latter case, the list does NOT include the unspecified
363 ;; spec (*).
364 ;; For time zone values, we have symbolic time zone names associated with
365 ;; the (relative) number of seconds ahead GMT.
368 (defsubst nndiary-schedule ()
369 (let (head)
370 (condition-case arg
371 (mapcar
372 (lambda (elt)
373 (setq head (nth 0 elt))
374 (nndiary-parse-schedule (nth 0 elt) (nth 1 elt) (nth 2 elt)))
375 nndiary-headers)
376 (error
377 (nnheader-report 'nndiary "X-Diary-%s header parse error: %s."
378 head (cdr arg))
379 nil))
382 ;;; Interface functions =====================================================
384 (nnoo-define-basics nndiary)
386 (deffoo nndiary-retrieve-headers (sequence &optional group server fetch-old)
387 (when (nndiary-possibly-change-directory group server)
388 (with-current-buffer nntp-server-buffer
389 (erase-buffer)
390 (let* ((file nil)
391 (number (length sequence))
392 (count 0)
393 (file-name-coding-system nnmail-pathname-coding-system)
394 beg article
395 (nndiary-check-directory-twice
396 (and nndiary-check-directory-twice
397 ;; To speed up, disable it in some case.
398 (or (not (numberp nnmail-large-newsgroup))
399 (<= number nnmail-large-newsgroup)))))
400 (if (stringp (car sequence))
401 'headers
402 (if (nndiary-retrieve-headers-with-nov sequence fetch-old)
403 'nov
404 (while sequence
405 (setq article (car sequence))
406 (setq file (nndiary-article-to-file article))
407 (when (and file
408 (file-exists-p file)
409 (not (file-directory-p file)))
410 (insert (format "221 %d Article retrieved.\n" article))
411 (setq beg (point))
412 (nnheader-insert-head file)
413 (goto-char beg)
414 (if (search-forward "\n\n" nil t)
415 (forward-char -1)
416 (goto-char (point-max))
417 (insert "\n\n"))
418 (insert ".\n")
419 (delete-region (point) (point-max)))
420 (setq sequence (cdr sequence))
421 (setq count (1+ count))
422 (and (numberp nnmail-large-newsgroup)
423 (> number nnmail-large-newsgroup)
424 (zerop (% count 20))
425 (nnheader-message 6 "nndiary: Receiving headers... %d%%"
426 (/ (* count 100) number))))
428 (and (numberp nnmail-large-newsgroup)
429 (> number nnmail-large-newsgroup)
430 (nnheader-message 6 "nndiary: Receiving headers...done"))
432 (nnheader-fold-continuation-lines)
433 'headers))))))
435 (deffoo nndiary-open-server (server &optional defs)
436 (nnoo-change-server 'nndiary server defs)
437 (when (not (file-exists-p nndiary-directory))
438 (ignore-errors (make-directory nndiary-directory t)))
439 (cond
440 ((not (file-exists-p nndiary-directory))
441 (nndiary-close-server)
442 (nnheader-report 'nndiary "Couldn't create directory: %s"
443 nndiary-directory))
444 ((not (file-directory-p (file-truename nndiary-directory)))
445 (nndiary-close-server)
446 (nnheader-report 'nndiary "Not a directory: %s" nndiary-directory))
448 (nnheader-report 'nndiary "Opened server %s using directory %s"
449 server nndiary-directory)
450 t)))
452 (deffoo nndiary-request-regenerate (server)
453 (nndiary-possibly-change-directory nil server)
454 (nndiary-generate-nov-databases server)
457 (deffoo nndiary-request-article (id &optional group server buffer)
458 (nndiary-possibly-change-directory group server)
459 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
460 (file-name-coding-system nnmail-pathname-coding-system)
461 path gpath group-num)
462 (if (stringp id)
463 (when (and (setq group-num (nndiary-find-group-number id))
464 (cdr
465 (assq (cdr group-num)
466 (nnheader-article-to-file-alist
467 (setq gpath
468 (nnmail-group-pathname
469 (car group-num)
470 nndiary-directory))))))
471 (setq path (concat gpath (int-to-string (cdr group-num)))))
472 (setq path (nndiary-article-to-file id)))
473 (cond
474 ((not path)
475 (nnheader-report 'nndiary "No such article: %s" id))
476 ((not (file-exists-p path))
477 (nnheader-report 'nndiary "No such file: %s" path))
478 ((file-directory-p path)
479 (nnheader-report 'nndiary "File is a directory: %s" path))
480 ((not (save-excursion (let ((nnmail-file-coding-system
481 nndiary-file-coding-system))
482 (nnmail-find-file path))))
483 (nnheader-report 'nndiary "Couldn't read file: %s" path))
485 (nnheader-report 'nndiary "Article %s retrieved" id)
486 ;; We return the article number.
487 (cons (if group-num (car group-num) group)
488 (string-to-number (file-name-nondirectory path)))))))
490 (deffoo nndiary-request-group (group &optional server dont-check info)
491 (let ((file-name-coding-system nnmail-pathname-coding-system))
492 (cond
493 ((not (nndiary-possibly-change-directory group server))
494 (nnheader-report 'nndiary "Invalid group (no such directory)"))
495 ((not (file-exists-p nndiary-current-directory))
496 (nnheader-report 'nndiary "Directory %s does not exist"
497 nndiary-current-directory))
498 ((not (file-directory-p nndiary-current-directory))
499 (nnheader-report 'nndiary "%s is not a directory"
500 nndiary-current-directory))
501 (dont-check
502 (nnheader-report 'nndiary "Group %s selected" group)
505 (nnheader-re-read-dir nndiary-current-directory)
506 (nnmail-activate 'nndiary)
507 (let ((active (nth 1 (assoc group nndiary-group-alist))))
508 (if (not active)
509 (nnheader-report 'nndiary "No such group: %s" group)
510 (nnheader-report 'nndiary "Selected group %s" group)
511 (nnheader-insert "211 %d %d %d %s\n"
512 (max (1+ (- (cdr active) (car active))) 0)
513 (car active) (cdr active) group)))))))
515 (deffoo nndiary-request-scan (&optional group server)
516 ;; Use our own mail sources and split methods while Gnus doesn't let us have
517 ;; multiple back ends for retrieving mail.
518 (let ((mail-sources nndiary-mail-sources)
519 (nnmail-split-methods nndiary-split-methods))
520 (setq nndiary-article-file-alist nil)
521 (nndiary-possibly-change-directory group server)
522 (nnmail-get-new-mail 'nndiary 'nndiary-save-nov nndiary-directory group)))
524 (deffoo nndiary-close-group (group &optional server)
525 (setq nndiary-article-file-alist nil)
528 (deffoo nndiary-request-create-group (group &optional server args)
529 (nndiary-possibly-change-directory nil server)
530 (nnmail-activate 'nndiary)
531 (cond
532 ((assoc group nndiary-group-alist)
534 ((and (file-exists-p (nnmail-group-pathname group nndiary-directory))
535 (not (file-directory-p (nnmail-group-pathname
536 group nndiary-directory))))
537 (nnheader-report 'nndiary "%s is a file"
538 (nnmail-group-pathname group nndiary-directory)))
540 (let (active)
541 (push (list group (setq active (cons 1 0)))
542 nndiary-group-alist)
543 (nndiary-possibly-create-directory group)
544 (nndiary-possibly-change-directory group server)
545 (let ((articles (nnheader-directory-articles nndiary-current-directory)))
546 (when articles
547 (setcar active (apply 'min articles))
548 (setcdr active (apply 'max articles))))
549 (nnmail-save-active nndiary-group-alist nndiary-active-file)
550 (run-hook-with-args 'nndiary-request-create-group-functions
551 (gnus-group-prefixed-name group
552 (list "nndiary" server)))
556 (deffoo nndiary-request-list (&optional server)
557 (save-excursion
558 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
559 (file-name-coding-system nnmail-pathname-coding-system))
560 (nnmail-find-file nndiary-active-file))
561 (setq nndiary-group-alist (nnmail-get-active))
564 (deffoo nndiary-request-newgroups (date &optional server)
565 (nndiary-request-list server))
567 (deffoo nndiary-request-list-newsgroups (&optional server)
568 (save-excursion
569 (nnmail-find-file nndiary-newsgroups-file)))
571 (deffoo nndiary-request-expire-articles (articles group &optional server force)
572 (nndiary-possibly-change-directory group server)
573 (let ((active-articles
574 (nnheader-directory-articles nndiary-current-directory))
575 article rest number)
576 (nnmail-activate 'nndiary)
577 ;; Articles not listed in active-articles are already gone,
578 ;; so don't try to expire them.
579 (setq articles (gnus-intersection articles active-articles))
580 (while articles
581 (setq article (nndiary-article-to-file (setq number (pop articles))))
582 (if (and (nndiary-deletable-article-p group number)
583 ;; Don't use nnmail-expired-article-p. Our notion of expiration
584 ;; is a bit peculiar ...
585 (or force (nndiary-expired-article-p article)))
586 (progn
587 ;; Allow a special target group.
588 (unless (eq nnmail-expiry-target 'delete)
589 (with-temp-buffer
590 (nndiary-request-article number group server (current-buffer))
591 (let ((nndiary-current-directory nil))
592 (nnmail-expiry-target-group nnmail-expiry-target group)))
593 (nndiary-possibly-change-directory group server))
594 (nnheader-message 5 "Deleting article %s in %s" number group)
595 (condition-case ()
596 (funcall nnmail-delete-file-function article)
597 (file-error (push number rest)))
598 (setq active-articles (delq number active-articles))
599 (nndiary-nov-delete-article group number))
600 (push number rest)))
601 (let ((active (nth 1 (assoc group nndiary-group-alist))))
602 (when active
603 (setcar active (or (and active-articles
604 (apply 'min active-articles))
605 (1+ (cdr active)))))
606 (nnmail-save-active nndiary-group-alist nndiary-active-file))
607 (nndiary-save-nov)
608 (nconc rest articles)))
610 (deffoo nndiary-request-move-article
611 (article group server accept-form &optional last move-is-internal)
612 (let ((buf (get-buffer-create " *nndiary move*"))
613 result)
614 (nndiary-possibly-change-directory group server)
615 (nndiary-update-file-alist)
616 (and
617 (nndiary-deletable-article-p group article)
618 (nndiary-request-article article group server)
619 (let (nndiary-current-directory
620 nndiary-current-group
621 nndiary-article-file-alist)
622 (with-current-buffer buf
623 (insert-buffer-substring nntp-server-buffer)
624 (setq result (eval accept-form))
625 (kill-buffer (current-buffer))
626 result))
627 (progn
628 (nndiary-possibly-change-directory group server)
629 (condition-case ()
630 (funcall nnmail-delete-file-function
631 (nndiary-article-to-file article))
632 (file-error nil))
633 (nndiary-nov-delete-article group article)
634 (when last
635 (nndiary-save-nov)
636 (nnmail-save-active nndiary-group-alist nndiary-active-file))))
637 result))
639 (deffoo nndiary-request-accept-article (group &optional server last)
640 (nndiary-possibly-change-directory group server)
641 (nnmail-check-syntax)
642 (run-hooks 'nndiary-request-accept-article-functions)
643 (when (nndiary-schedule)
644 (let (result)
645 (when nnmail-cache-accepted-message-ids
646 (nnmail-cache-insert (nnmail-fetch-field "message-id")
647 group
648 (nnmail-fetch-field "subject")))
649 (if (stringp group)
650 (and
651 (nnmail-activate 'nndiary)
652 (setq result
653 (car (nndiary-save-mail
654 (list (cons group (nndiary-active-number group))))))
655 (progn
656 (nnmail-save-active nndiary-group-alist nndiary-active-file)
657 (and last (nndiary-save-nov))))
658 (and
659 (nnmail-activate 'nndiary)
660 (if (and (not (setq result
661 (nnmail-article-group 'nndiary-active-number)))
662 (yes-or-no-p "Moved to `junk' group; delete article? "))
663 (setq result 'junk)
664 (setq result (car (nndiary-save-mail result))))
665 (when last
666 (nnmail-save-active nndiary-group-alist nndiary-active-file)
667 (when nnmail-cache-accepted-message-ids
668 (nnmail-cache-close))
669 (nndiary-save-nov))))
670 result))
673 (deffoo nndiary-request-post (&optional server)
674 (nnmail-do-request-post 'nndiary-request-accept-article server))
676 (deffoo nndiary-request-replace-article (article group buffer)
677 (nndiary-possibly-change-directory group)
678 (with-current-buffer buffer
679 (nndiary-possibly-create-directory group)
680 (let ((chars (nnmail-insert-lines))
681 (art (concat (int-to-string article) "\t"))
682 headers)
683 (when (ignore-errors
684 (nnmail-write-region
685 (point-min) (point-max)
686 (or (nndiary-article-to-file article)
687 (expand-file-name (int-to-string article)
688 nndiary-current-directory))
689 nil (if (nnheader-be-verbose 5) nil 'nomesg))
691 (setq headers (nndiary-parse-head chars article))
692 ;; Replace the NOV line in the NOV file.
693 (with-current-buffer (nndiary-open-nov group)
694 (goto-char (point-min))
695 (if (or (looking-at art)
696 (search-forward (concat "\n" art) nil t))
697 ;; Delete the old NOV line.
698 (delete-region (progn (beginning-of-line) (point))
699 (progn (forward-line 1) (point)))
700 ;; The line isn't here, so we have to find out where
701 ;; we should insert it. (This situation should never
702 ;; occur, but one likes to make sure...)
703 (while (and (looking-at "[0-9]+\t")
704 (< (string-to-number
705 (buffer-substring
706 (match-beginning 0) (match-end 0)))
707 article)
708 (zerop (forward-line 1)))))
709 (beginning-of-line)
710 (nnheader-insert-nov headers)
711 (nndiary-save-nov)
712 t)))))
714 (deffoo nndiary-request-delete-group (group &optional force server)
715 (nndiary-possibly-change-directory group server)
716 (when force
717 ;; Delete all articles in GROUP.
718 (let ((articles
719 (directory-files
720 nndiary-current-directory t
721 (concat nnheader-numerical-short-files
722 "\\|" (regexp-quote nndiary-nov-file-name) "$")))
723 article)
724 (while articles
725 (setq article (pop articles))
726 (when (file-writable-p article)
727 (nnheader-message 5 "Deleting article %s in %s..." article group)
728 (funcall nnmail-delete-file-function article))))
729 ;; Try to delete the directory itself.
730 (ignore-errors (delete-directory nndiary-current-directory)))
731 ;; Remove the group from all structures.
732 (setq nndiary-group-alist
733 (delq (assoc group nndiary-group-alist) nndiary-group-alist)
734 nndiary-current-group nil
735 nndiary-current-directory nil)
736 ;; Save the active file.
737 (nnmail-save-active nndiary-group-alist nndiary-active-file)
740 (deffoo nndiary-request-rename-group (group new-name &optional server)
741 (nndiary-possibly-change-directory group server)
742 (let ((new-dir (nnmail-group-pathname new-name nndiary-directory))
743 (old-dir (nnmail-group-pathname group nndiary-directory)))
744 (when (ignore-errors
745 (make-directory new-dir t)
747 ;; We move the articles file by file instead of renaming
748 ;; the directory -- there may be subgroups in this group.
749 ;; One might be more clever, I guess.
750 (let ((files (nnheader-article-to-file-alist old-dir)))
751 (while files
752 (rename-file
753 (concat old-dir (cdar files))
754 (concat new-dir (cdar files)))
755 (pop files)))
756 ;; Move .overview file.
757 (let ((overview (concat old-dir nndiary-nov-file-name)))
758 (when (file-exists-p overview)
759 (rename-file overview (concat new-dir nndiary-nov-file-name))))
760 (when (<= (length (directory-files old-dir)) 2)
761 (ignore-errors (delete-directory old-dir)))
762 ;; That went ok, so we change the internal structures.
763 (let ((entry (assoc group nndiary-group-alist)))
764 (when entry
765 (setcar entry new-name))
766 (setq nndiary-current-directory nil
767 nndiary-current-group nil)
768 ;; Save the new group alist.
769 (nnmail-save-active nndiary-group-alist nndiary-active-file)
770 t))))
772 (deffoo nndiary-set-status (article name value &optional group server)
773 (nndiary-possibly-change-directory group server)
774 (let ((file (nndiary-article-to-file article)))
775 (cond
776 ((not (file-exists-p file))
777 (nnheader-report 'nndiary "File %s does not exist" file))
779 (with-temp-file file
780 (nnheader-insert-file-contents file)
781 (nnmail-replace-status name value))
782 t))))
785 ;;; Interface optional functions ============================================
787 (deffoo nndiary-request-update-info (group info &optional server)
788 (nndiary-possibly-change-directory group)
789 (let ((timestamp (gnus-group-parameter-value (gnus-info-params info)
790 'timestamp t)))
791 (if (not timestamp)
792 (nnheader-report 'nndiary "Group %s doesn't have a timestamp" group)
793 ;; else
794 ;; Figure out which articles should be re-new'ed
795 (let ((articles (nndiary-flatten (gnus-info-read info) 0))
796 article file unread buf)
797 (save-excursion
798 (setq buf (nnheader-set-temp-buffer " *nndiary update*"))
799 (while (setq article (pop articles))
800 (setq file (concat nndiary-current-directory
801 (int-to-string article)))
802 (and (file-exists-p file)
803 (nndiary-renew-article-p file timestamp)
804 (push article unread)))
805 ;;(message "unread: %s" unread)
806 (sit-for 1)
807 (kill-buffer buf))
808 (setq unread (sort unread '<))
809 (and unread
810 (gnus-info-set-read info (gnus-update-read-articles
811 (gnus-info-group info) unread t)))
813 (run-hook-with-args 'nndiary-request-update-info-functions
814 (gnus-info-group info))
819 ;;; Internal functions ======================================================
821 (defun nndiary-article-to-file (article)
822 (nndiary-update-file-alist)
823 (let (file)
824 (if (setq file (cdr (assq article nndiary-article-file-alist)))
825 (expand-file-name file nndiary-current-directory)
826 ;; Just to make sure nothing went wrong when reading over NFS --
827 ;; check once more.
828 (if nndiary-check-directory-twice
829 (when (file-exists-p
830 (setq file (expand-file-name (number-to-string article)
831 nndiary-current-directory)))
832 (nndiary-update-file-alist t)
833 file)))))
835 (defun nndiary-deletable-article-p (group article)
836 "Say whether ARTICLE in GROUP can be deleted."
837 (let (path)
838 (when (setq path (nndiary-article-to-file article))
839 (when (file-writable-p path)
840 (or (not nnmail-keep-last-article)
841 (not (eq (cdr (nth 1 (assoc group nndiary-group-alist)))
842 article)))))))
844 ;; Find an article number in the current group given the Message-ID.
845 (defun nndiary-find-group-number (id)
846 (with-current-buffer (get-buffer-create " *nndiary id*")
847 (let ((alist nndiary-group-alist)
848 number)
849 ;; We want to look through all .overview files, but we want to
850 ;; start with the one in the current directory. It seems most
851 ;; likely that the article we are looking for is in that group.
852 (if (setq number (nndiary-find-id nndiary-current-group id))
853 (cons nndiary-current-group number)
854 ;; It wasn't there, so we look through the other groups as well.
855 (while (and (not number)
856 alist)
857 (or (string= (caar alist) nndiary-current-group)
858 (setq number (nndiary-find-id (caar alist) id)))
859 (or number
860 (setq alist (cdr alist))))
861 (and number
862 (cons (caar alist) number))))))
864 (defun nndiary-find-id (group id)
865 (erase-buffer)
866 (let ((nov (expand-file-name nndiary-nov-file-name
867 (nnmail-group-pathname group
868 nndiary-directory)))
869 number found)
870 (when (file-exists-p nov)
871 (nnheader-insert-file-contents nov)
872 (while (and (not found)
873 (search-forward id nil t)) ; We find the ID.
874 ;; And the id is in the fourth field.
875 (if (not (and (search-backward "\t" nil t 4)
876 (not (search-backward"\t" (point-at-bol) t))))
877 (forward-line 1)
878 (beginning-of-line)
879 (setq found t)
880 ;; We return the article number.
881 (setq number
882 (ignore-errors (read (current-buffer))))))
883 number)))
885 (defun nndiary-retrieve-headers-with-nov (articles &optional fetch-old)
886 (if (or gnus-nov-is-evil nndiary-nov-is-evil)
888 (let ((nov (expand-file-name nndiary-nov-file-name
889 nndiary-current-directory)))
890 (when (file-exists-p nov)
891 (with-current-buffer nntp-server-buffer
892 (erase-buffer)
893 (nnheader-insert-file-contents nov)
894 (if (and fetch-old
895 (not (numberp fetch-old)))
896 t ; Don't remove anything.
897 (nnheader-nov-delete-outside-range
898 (if fetch-old (max 1 (- (car articles) fetch-old))
899 (car articles))
900 (car (last articles)))
901 t))))))
903 (defun nndiary-possibly-change-directory (group &optional server)
904 (when (and server
905 (not (nndiary-server-opened server)))
906 (nndiary-open-server server))
907 (if (not group)
909 (let ((pathname (nnmail-group-pathname group nndiary-directory))
910 (file-name-coding-system nnmail-pathname-coding-system))
911 (when (not (equal pathname nndiary-current-directory))
912 (setq nndiary-current-directory pathname
913 nndiary-current-group group
914 nndiary-article-file-alist nil))
915 (file-exists-p nndiary-current-directory))))
917 (defun nndiary-possibly-create-directory (group)
918 (let ((dir (nnmail-group-pathname group nndiary-directory)))
919 (unless (file-exists-p dir)
920 (make-directory (directory-file-name dir) t)
921 (nnheader-message 5 "Creating mail directory %s" dir))))
923 (defun nndiary-save-mail (group-art)
924 "Called narrowed to an article."
925 (let (chars headers)
926 (setq chars (nnmail-insert-lines))
927 (nnmail-insert-xref group-art)
928 (run-hooks 'nnmail-prepare-save-mail-hook)
929 (run-hooks 'nndiary-prepare-save-mail-hook)
930 (goto-char (point-min))
931 (while (looking-at "From ")
932 (replace-match "X-From-Line: ")
933 (forward-line 1))
934 ;; We save the article in all the groups it belongs in.
935 (let ((ga group-art)
936 first)
937 (while ga
938 (nndiary-possibly-create-directory (caar ga))
939 (let ((file (concat (nnmail-group-pathname
940 (caar ga) nndiary-directory)
941 (int-to-string (cdar ga)))))
942 (if first
943 ;; It was already saved, so we just make a hard link.
944 (funcall nnmail-crosspost-link-function first file t)
945 ;; Save the article.
946 (nnmail-write-region (point-min) (point-max) file nil
947 (if (nnheader-be-verbose 5) nil 'nomesg))
948 (setq first file)))
949 (setq ga (cdr ga))))
950 ;; Generate a nov line for this article. We generate the nov
951 ;; line after saving, because nov generation destroys the
952 ;; header.
953 (setq headers (nndiary-parse-head chars))
954 ;; Output the nov line to all nov databases that should have it.
955 (let ((ga group-art))
956 (while ga
957 (nndiary-add-nov (caar ga) (cdar ga) headers)
958 (setq ga (cdr ga))))
959 group-art))
961 (defun nndiary-active-number (group)
962 "Compute the next article number in GROUP."
963 (let ((active (cadr (assoc group nndiary-group-alist))))
964 ;; The group wasn't known to nndiary, so we just create an active
965 ;; entry for it.
966 (unless active
967 ;; Perhaps the active file was corrupt? See whether
968 ;; there are any articles in this group.
969 (nndiary-possibly-create-directory group)
970 (nndiary-possibly-change-directory group)
971 (unless nndiary-article-file-alist
972 (setq nndiary-article-file-alist
973 (sort
974 (nnheader-article-to-file-alist nndiary-current-directory)
975 'car-less-than-car)))
976 (setq active
977 (if nndiary-article-file-alist
978 (cons (caar nndiary-article-file-alist)
979 (caar (last nndiary-article-file-alist)))
980 (cons 1 0)))
981 (push (list group active) nndiary-group-alist))
982 (setcdr active (1+ (cdr active)))
983 (while (file-exists-p
984 (expand-file-name (int-to-string (cdr active))
985 (nnmail-group-pathname group nndiary-directory)))
986 (setcdr active (1+ (cdr active))))
987 (cdr active)))
989 (defun nndiary-add-nov (group article headers)
990 "Add a nov line for the GROUP base."
991 (with-current-buffer (nndiary-open-nov group)
992 (goto-char (point-max))
993 (mail-header-set-number headers article)
994 (nnheader-insert-nov headers)))
996 (defsubst nndiary-header-value ()
997 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
999 (defun nndiary-parse-head (chars &optional number)
1000 "Parse the head of the current buffer."
1001 (save-excursion
1002 (save-restriction
1003 (unless (zerop (buffer-size))
1004 (narrow-to-region
1005 (goto-char (point-min))
1006 (if (search-forward "\n\n" nil t) (1- (point)) (point-max))))
1007 (let ((headers (nnheader-parse-naked-head)))
1008 (mail-header-set-chars headers chars)
1009 (mail-header-set-number headers number)
1010 headers))))
1012 (defun nndiary-open-nov (group)
1013 (or (cdr (assoc group nndiary-nov-buffer-alist))
1014 (let ((buffer (get-buffer-create (format " *nndiary overview %s*"
1015 group))))
1016 (with-current-buffer buffer
1017 (set (make-local-variable 'nndiary-nov-buffer-file-name)
1018 (expand-file-name
1019 nndiary-nov-file-name
1020 (nnmail-group-pathname group nndiary-directory)))
1021 (erase-buffer)
1022 (when (file-exists-p nndiary-nov-buffer-file-name)
1023 (nnheader-insert-file-contents nndiary-nov-buffer-file-name)))
1024 (push (cons group buffer) nndiary-nov-buffer-alist)
1025 buffer)))
1027 (defun nndiary-save-nov ()
1028 (save-excursion
1029 (while nndiary-nov-buffer-alist
1030 (when (buffer-name (cdar nndiary-nov-buffer-alist))
1031 (set-buffer (cdar nndiary-nov-buffer-alist))
1032 (when (buffer-modified-p)
1033 (nnmail-write-region 1 (point-max) nndiary-nov-buffer-file-name
1034 nil 'nomesg))
1035 (set-buffer-modified-p nil)
1036 (kill-buffer (current-buffer)))
1037 (setq nndiary-nov-buffer-alist (cdr nndiary-nov-buffer-alist)))))
1039 ;;;###autoload
1040 (defun nndiary-generate-nov-databases (&optional server)
1041 "Generate NOV databases in all nndiary directories."
1042 (interactive (list (or (nnoo-current-server 'nndiary) "")))
1043 ;; Read the active file to make sure we don't re-use articles
1044 ;; numbers in empty groups.
1045 (nnmail-activate 'nndiary)
1046 (unless (nndiary-server-opened server)
1047 (nndiary-open-server server))
1048 (setq nndiary-directory (expand-file-name nndiary-directory))
1049 ;; Recurse down the directories.
1050 (nndiary-generate-nov-databases-1 nndiary-directory nil t)
1051 ;; Save the active file.
1052 (nnmail-save-active nndiary-group-alist nndiary-active-file))
1054 (defun nndiary-generate-nov-databases-1 (dir &optional seen no-active)
1055 "Regenerate the NOV database in DIR."
1056 (interactive "DRegenerate NOV in: ")
1057 (setq dir (file-name-as-directory dir))
1058 ;; Only scan this sub-tree if we haven't been here yet.
1059 (unless (member (file-truename dir) seen)
1060 (push (file-truename dir) seen)
1061 ;; We descend recursively
1062 (let ((dirs (directory-files dir t nil t))
1063 dir)
1064 (while (setq dir (pop dirs))
1065 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
1066 (file-directory-p dir))
1067 (nndiary-generate-nov-databases-1 dir seen))))
1068 ;; Do this directory.
1069 (let ((nndiary-files (sort (nnheader-article-to-file-alist dir)
1070 'car-less-than-car)))
1071 (if (not nndiary-files)
1072 (let* ((group (nnheader-file-to-group
1073 (directory-file-name dir) nndiary-directory))
1074 (info (cadr (assoc group nndiary-group-alist))))
1075 (when info
1076 (setcar info (1+ (cdr info)))))
1077 (funcall nndiary-generate-active-function dir)
1078 ;; Generate the nov file.
1079 (nndiary-generate-nov-file dir nndiary-files)
1080 (unless no-active
1081 (nnmail-save-active nndiary-group-alist nndiary-active-file))))))
1083 (defvar nndiary-files) ; dynamically bound in nndiary-generate-nov-databases-1
1084 (defun nndiary-generate-active-info (dir)
1085 ;; Update the active info for this group.
1086 (let* ((group (nnheader-file-to-group
1087 (directory-file-name dir) nndiary-directory))
1088 (entry (assoc group nndiary-group-alist))
1089 (last (or (caadr entry) 0)))
1090 (setq nndiary-group-alist (delq entry nndiary-group-alist))
1091 (push (list group
1092 (cons (or (caar nndiary-files) (1+ last))
1093 (max last
1094 (or (caar (last nndiary-files))
1095 0))))
1096 nndiary-group-alist)))
1098 (defun nndiary-generate-nov-file (dir files)
1099 (let* ((dir (file-name-as-directory dir))
1100 (nov (concat dir nndiary-nov-file-name))
1101 (nov-buffer (get-buffer-create " *nov*"))
1102 chars file headers)
1103 ;; Init the nov buffer.
1104 (with-current-buffer nov-buffer
1105 (buffer-disable-undo)
1106 (erase-buffer)
1107 (set-buffer nntp-server-buffer)
1108 ;; Delete the old NOV file.
1109 (when (file-exists-p nov)
1110 (funcall nnmail-delete-file-function nov))
1111 (while files
1112 (unless (file-directory-p (setq file (concat dir (cdar files))))
1113 (erase-buffer)
1114 (nnheader-insert-file-contents file)
1115 (narrow-to-region
1116 (goto-char (point-min))
1117 (progn
1118 (search-forward "\n\n" nil t)
1119 (setq chars (- (point-max) (point)))
1120 (max 1 (1- (point)))))
1121 (unless (zerop (buffer-size))
1122 (goto-char (point-min))
1123 (setq headers (nndiary-parse-head chars (caar files)))
1124 (with-current-buffer nov-buffer
1125 (goto-char (point-max))
1126 (nnheader-insert-nov headers)))
1127 (widen))
1128 (setq files (cdr files)))
1129 (with-current-buffer nov-buffer
1130 (nnmail-write-region 1 (point-max) nov nil 'nomesg)
1131 (kill-buffer (current-buffer))))))
1133 (defun nndiary-nov-delete-article (group article)
1134 (with-current-buffer (nndiary-open-nov group)
1135 (when (nnheader-find-nov-line article)
1136 (delete-region (point) (progn (forward-line 1) (point)))
1137 (when (bobp)
1138 (let ((active (cadr (assoc group nndiary-group-alist)))
1139 num)
1140 (when active
1141 (if (eobp)
1142 (setf (car active) (1+ (cdr active)))
1143 (when (and (setq num (ignore-errors (read (current-buffer))))
1144 (numberp num))
1145 (setf (car active) num)))))))
1148 (defun nndiary-update-file-alist (&optional force)
1149 (when (or (not nndiary-article-file-alist)
1150 force)
1151 (setq nndiary-article-file-alist
1152 (nnheader-article-to-file-alist nndiary-current-directory))))
1155 (defun nndiary-string-to-number (str min &optional max)
1156 ;; Like `string-to-number' but barf if STR is not exactly an integer, and not
1157 ;; within the specified bounds.
1158 ;; Signals are caught by `nndiary-schedule'.
1159 (if (not (string-match "^[ \t]*[0-9]+[ \t]*$" str))
1160 (nndiary-error "not an integer value")
1161 ;; else
1162 (let ((val (string-to-number str)))
1163 (and (or (< val min)
1164 (and max (> val max)))
1165 (nndiary-error "value out of range"))
1166 val)))
1168 (defun nndiary-parse-schedule-value (str min-or-values max)
1169 ;; Parse the schedule string STR, or signal an error.
1170 ;; Signals are caught by `nndiary-schedule'.
1171 (if (string-match "[ \t]*\\*[ \t]*" str)
1172 ;; unspecified
1174 ;; specified
1175 (if (listp min-or-values)
1176 ;; min-or-values is values
1177 ;; #### NOTE: this is actually only a hack for time zones.
1178 (let ((val (and (string-match "[ \t]*\\([^ \t]+\\)[ \t]*" str)
1179 (match-string 1 str))))
1180 (if (and val (setq val (assoc val min-or-values)))
1181 (list (cadr val))
1182 (nndiary-error "invalid syntax")))
1183 ;; min-or-values is min
1184 (mapcar
1185 (lambda (val)
1186 (let ((res (split-string val "-")))
1187 (cond
1188 ((= (length res) 1)
1189 (nndiary-string-to-number (car res) min-or-values max))
1190 ((= (length res) 2)
1191 ;; don't know if crontab accepts this, but ensure
1192 ;; that BEG is <= END
1193 (let ((beg (nndiary-string-to-number (car res) min-or-values max))
1194 (end (nndiary-string-to-number (cadr res) min-or-values max)))
1195 (cond ((< beg end)
1196 (cons beg end))
1197 ((= beg end)
1198 beg)
1200 (cons end beg)))))
1202 (nndiary-error "invalid syntax")))
1204 (split-string str ",")))
1207 ;; ### FIXME: remove this function if it's used only once.
1208 (defun nndiary-parse-schedule (head min-or-values max)
1209 ;; Parse the cron-like value of header X-Diary-HEAD in current buffer.
1210 ;; - Returns nil if `*'
1211 ;; - Otherwise returns a list of integers and/or ranges (BEG . END)
1212 ;; The exception is the Timze-Zone value which is always of the form (STR).
1213 ;; Signals are caught by `nndiary-schedule'.
1214 (let ((header (format "^X-Diary-%s: \\(.*\\)$" head)))
1215 (goto-char (point-min))
1216 (if (not (re-search-forward header nil t))
1217 (nndiary-error "header missing")
1218 ;; else
1219 (nndiary-parse-schedule-value (match-string 1) min-or-values max))
1222 (defun nndiary-max (spec)
1223 ;; Returns the max of specification SPEC, or nil for permanent schedules.
1224 (unless (null spec)
1225 (let ((elts spec)
1226 (max 0)
1227 elt)
1228 (while (setq elt (pop elts))
1229 (if (integerp elt)
1230 (and (> elt max) (setq max elt))
1231 (and (> (cdr elt) max) (setq max (cdr elt)))))
1232 max)))
1234 (defun nndiary-flatten (spec min &optional max)
1235 ;; flatten the spec by expanding ranges to all possible values.
1236 (let (flat n)
1237 (cond ((null spec)
1238 ;; this happens when I flatten something else than one of my
1239 ;; schedules (a list of read articles for instance).
1240 (unless (null max)
1241 (setq n min)
1242 (while (<= n max)
1243 (push n flat)
1244 (setq n (1+ n)))))
1246 (let ((elts spec)
1247 elt)
1248 (while (setq elt (pop elts))
1249 (if (integerp elt)
1250 (push elt flat)
1251 ;; else
1252 (setq n (car elt))
1253 (while (<= n (cdr elt))
1254 (push n flat)
1255 (setq n (1+ n))))))))
1256 flat))
1258 (defun nndiary-unflatten (spec)
1259 ;; opposite of flatten: build ranges if possible
1260 (setq spec (sort spec '<))
1261 (let (min max res)
1262 (while (setq min (pop spec))
1263 (setq max min)
1264 (while (and (car spec) (= (car spec) (1+ max)))
1265 (setq max (1+ max))
1266 (pop spec))
1267 (if (= max min)
1268 (setq res (append res (list min)))
1269 (setq res (append res (list (cons min max))))))
1270 res))
1272 (defun nndiary-compute-reminders (date)
1273 ;; Returns a list of times corresponding to the reminders of date DATE.
1274 ;; See the comment in `nndiary-reminders' about rounding.
1275 (let* ((reminders nndiary-reminders)
1276 (date-elts (decode-time date))
1277 ;; ### NOTE: out-of-range values are accepted by encode-time. This
1278 ;; makes our life easier.
1279 (monday (- (nth 3 date-elts)
1280 (if nndiary-week-starts-on-monday
1281 (if (zerop (nth 6 date-elts))
1283 (- (nth 6 date-elts) 1))
1284 (nth 6 date-elts))))
1285 reminder res)
1286 ;; remove the DOW and DST entries
1287 (setcdr (nthcdr 5 date-elts) (nthcdr 8 date-elts))
1288 (while (setq reminder (pop reminders))
1289 (push
1290 (cond ((eq (cdr reminder) 'minute)
1291 (subtract-time
1292 (apply 'encode-time 0 (nthcdr 1 date-elts))
1293 (seconds-to-time (* (car reminder) 60.0))))
1294 ((eq (cdr reminder) 'hour)
1295 (subtract-time
1296 (apply 'encode-time 0 0 (nthcdr 2 date-elts))
1297 (seconds-to-time (* (car reminder) 3600.0))))
1298 ((eq (cdr reminder) 'day)
1299 (subtract-time
1300 (apply 'encode-time 0 0 0 (nthcdr 3 date-elts))
1301 (seconds-to-time (* (car reminder) 86400.0))))
1302 ((eq (cdr reminder) 'week)
1303 (subtract-time
1304 (apply 'encode-time 0 0 0 monday (nthcdr 4 date-elts))
1305 (seconds-to-time (* (car reminder) 604800.0))))
1306 ((eq (cdr reminder) 'month)
1307 (subtract-time
1308 (apply 'encode-time 0 0 0 1 (nthcdr 4 date-elts))
1309 (seconds-to-time (* (car reminder) 18748800.0))))
1310 ((eq (cdr reminder) 'year)
1311 (subtract-time
1312 (apply 'encode-time 0 0 0 1 1 (nthcdr 5 date-elts))
1313 (seconds-to-time (* (car reminder) 400861056.0)))))
1314 res))
1315 (sort res 'time-less-p)))
1317 (defun nndiary-last-occurence (sched)
1318 ;; Returns the last occurrence of schedule SCHED as an Emacs time struct, or
1319 ;; nil for permanent schedule or errors.
1320 (let ((minute (nndiary-max (nth 0 sched)))
1321 (hour (nndiary-max (nth 1 sched)))
1322 (year (nndiary-max (nth 4 sched)))
1323 (time-zone (or (and (nth 6 sched) (car (nth 6 sched)))
1324 (current-time-zone))))
1325 (when year
1326 (or minute (setq minute 59))
1327 (or hour (setq hour 23))
1328 ;; I'll just compute all possible values and test them by decreasing
1329 ;; order until one succeeds. This is probably quite rude, but I got
1330 ;; bored in finding a good algorithm for doing that ;-)
1331 ;; ### FIXME: remove identical entries.
1332 (let ((dom-list (nth 2 sched))
1333 (month-list (sort (nndiary-flatten (nth 3 sched) 1 12) '>))
1334 (year-list (sort (nndiary-flatten (nth 4 sched) 1971) '>))
1335 (dow-list (nth 5 sched)))
1336 ;; Special case: an asterisk in one of the days specifications means
1337 ;; that only the other should be taken into account. If both are
1338 ;; unspecified, you would get all possible days in both.
1339 (cond ((null dow-list)
1340 ;; this gets all days if dom-list is nil
1341 (setq dom-list (nndiary-flatten dom-list 1 31)))
1342 ((null dom-list)
1343 ;; this also gets all days if dow-list is nil
1344 (setq dow-list (nndiary-flatten dow-list 0 6)))
1346 (setq dom-list (nndiary-flatten dom-list 1 31))
1347 (setq dow-list (nndiary-flatten dow-list 0 6))))
1349 (catch 'found
1350 (while (setq year (pop year-list))
1351 (let ((months month-list)
1352 month)
1353 (while (setq month (pop months))
1354 ;; Now we must merge the Dows with the Doms. To do that, we
1355 ;; have to know which day is the 1st one for this month.
1356 ;; Maybe there's simpler, but decode-time(encode-time) will
1357 ;; give us the answer.
1358 (let ((first (nth 6 (decode-time
1359 (encode-time 0 0 0 1 month year
1360 time-zone))))
1361 (max (cond ((= month 2)
1362 (if (date-leap-year-p year) 29 28))
1363 ((<= month 7)
1364 (if (zerop (% month 2)) 30 31))
1366 (if (zerop (% month 2)) 31 30))))
1367 (doms dom-list)
1368 (dows dow-list)
1369 day days)
1370 ;; first, review the doms to see if they are valid.
1371 (while (setq day (pop doms))
1372 (and (<= day max)
1373 (push day days)))
1374 ;; second add all possible dows
1375 (while (setq day (pop dows))
1376 ;; days start at 1.
1377 (setq day (1+ (- day first)))
1378 (and (< day 0) (setq day (+ 7 day)))
1379 (while (<= day max)
1380 (push day days)
1381 (setq day (+ 7 day))))
1382 ;; Finally, if we have some days, they are valid
1383 (when days
1384 (sort days '>)
1385 (throw 'found
1386 (encode-time 0 minute hour
1387 (car days) month year time-zone)))
1388 )))))
1389 ;; There's an upper limit, but we didn't find any last occurrence.
1390 ;; This means that the schedule is undecidable. This can happen if
1391 ;; you happen to say something like "each Feb 31 until 2038".
1392 (progn
1393 (nnheader-report 'nndiary "Undecidable schedule")
1394 nil))
1395 ))))
1397 (defun nndiary-next-occurence (sched now)
1398 ;; Returns the next occurrence of schedule SCHED, starting from time NOW.
1399 ;; If there's no next occurrence, returns the last one (if any) which is then
1400 ;; in the past.
1401 (let* ((today (decode-time now))
1402 (this-minute (nth 1 today))
1403 (this-hour (nth 2 today))
1404 (this-day (nth 3 today))
1405 (this-month (nth 4 today))
1406 (this-year (nth 5 today))
1407 (minute-list (sort (nndiary-flatten (nth 0 sched) 0 59) '<))
1408 (hour-list (sort (nndiary-flatten (nth 1 sched) 0 23) '<))
1409 (dom-list (nth 2 sched))
1410 (month-list (sort (nndiary-flatten (nth 3 sched) 1 12) '<))
1411 (years (if (nth 4 sched)
1412 (sort (nndiary-flatten (nth 4 sched) 1971) '<)
1414 (dow-list (nth 5 sched))
1415 (year (1- this-year))
1416 (time-zone (or (and (nth 6 sched) (car (nth 6 sched)))
1417 (current-time-zone))))
1418 ;; Special case: an asterisk in one of the days specifications means that
1419 ;; only the other should be taken into account. If both are unspecified,
1420 ;; you would get all possible days in both.
1421 (cond ((null dow-list)
1422 ;; this gets all days if dom-list is nil
1423 (setq dom-list (nndiary-flatten dom-list 1 31)))
1424 ((null dom-list)
1425 ;; this also gets all days if dow-list is nil
1426 (setq dow-list (nndiary-flatten dow-list 0 6)))
1428 (setq dom-list (nndiary-flatten dom-list 1 31))
1429 (setq dow-list (nndiary-flatten dow-list 0 6))))
1430 ;; Remove past years.
1431 (unless (eq years t)
1432 (while (and (car years) (< (car years) this-year))
1433 (pop years)))
1434 (if years
1435 ;; Because we might not be limited in years, we must guard against
1436 ;; infinite loops. Appart from cases like Feb 31, there are probably
1437 ;; other ones, (no monday XXX 2nd etc). I don't know any algorithm to
1438 ;; decide this, so I assume that if we reach 10 years later, the
1439 ;; schedule is undecidable.
1441 (catch 'found
1442 (while (if (eq years t)
1443 (and (setq year (1+ year))
1444 (<= year (+ 10 this-year)))
1445 (setq year (pop years)))
1446 (let ((months month-list)
1447 month)
1448 ;; Remove past months for this year.
1449 (and (= year this-year)
1450 (while (and (car months) (< (car months) this-month))
1451 (pop months)))
1452 (while (setq month (pop months))
1453 ;; Now we must merge the Dows with the Doms. To do that, we
1454 ;; have to know which day is the 1st one for this month.
1455 ;; Maybe there's simpler, but decode-time(encode-time) will
1456 ;; give us the answer.
1457 (let ((first (nth 6 (decode-time
1458 (encode-time 0 0 0 1 month year
1459 time-zone))))
1460 (max (cond ((= month 2)
1461 (if (date-leap-year-p year) 29 28))
1462 ((<= month 7)
1463 (if (zerop (% month 2)) 30 31))
1465 (if (zerop (% month 2)) 31 30))))
1466 (doms dom-list)
1467 (dows dow-list)
1468 day days)
1469 ;; first, review the doms to see if they are valid.
1470 (while (setq day (pop doms))
1471 (and (<= day max)
1472 (push day days)))
1473 ;; second add all possible dows
1474 (while (setq day (pop dows))
1475 ;; days start at 1.
1476 (setq day (1+ (- day first)))
1477 (and (< day 0) (setq day (+ 7 day)))
1478 (while (<= day max)
1479 (push day days)
1480 (setq day (+ 7 day))))
1481 ;; Aaaaaaall right. Now we have a valid list of DAYS for
1482 ;; this month and this year.
1483 (when days
1484 (setq days (sort days '<))
1485 ;; Remove past days for this year and this month.
1486 (and (= year this-year)
1487 (= month this-month)
1488 (while (and (car days) (< (car days) this-day))
1489 (pop days)))
1490 (while (setq day (pop days))
1491 (let ((hours hour-list)
1492 hour)
1493 ;; Remove past hours for this year, this month and
1494 ;; this day.
1495 (and (= year this-year)
1496 (= month this-month)
1497 (= day this-day)
1498 (while (and (car hours)
1499 (< (car hours) this-hour))
1500 (pop hours)))
1501 (while (setq hour (pop hours))
1502 (let ((minutes minute-list)
1503 minute)
1504 ;; Remove past hours for this year, this month,
1505 ;; this day and this hour.
1506 (and (= year this-year)
1507 (= month this-month)
1508 (= day this-day)
1509 (= hour this-hour)
1510 (while (and (car minutes)
1511 (< (car minutes) this-minute))
1512 (pop minutes)))
1513 (while (setq minute (pop minutes))
1514 ;; Ouch! Here, we've got a complete valid
1515 ;; schedule. It's a good one if it's in the
1516 ;; future.
1517 (let ((time (encode-time 0 minute hour day
1518 month year
1519 time-zone)))
1520 (and (time-less-p now time)
1521 (throw 'found time)))
1522 ))))
1526 (nndiary-last-occurence sched))
1527 ;; else
1528 (nndiary-last-occurence sched))
1531 (defun nndiary-expired-article-p (file)
1532 (with-temp-buffer
1533 (if (nnheader-insert-head file)
1534 (let ((sched (nndiary-schedule)))
1535 ;; An article has expired if its last schedule (if any) is in the
1536 ;; past. A permanent schedule never expires.
1537 (and sched
1538 (setq sched (nndiary-last-occurence sched))
1539 (time-less-p sched (current-time))))
1540 ;; else
1541 (nnheader-report 'nndiary "Could not read file %s" file)
1542 nil)
1545 (defun nndiary-renew-article-p (file timestamp)
1546 (erase-buffer)
1547 (if (nnheader-insert-head file)
1548 (let ((now (current-time))
1549 (sched (nndiary-schedule)))
1550 ;; The article should be re-considered as unread if there's a reminder
1551 ;; between the group timestamp and the current time.
1552 (when (and sched (setq sched (nndiary-next-occurence sched now)))
1553 (let ((reminders ;; add the next occurrence itself at the end.
1554 (append (nndiary-compute-reminders sched) (list sched))))
1555 (while (and reminders (time-less-p (car reminders) timestamp))
1556 (pop reminders))
1557 ;; The reminders might be empty if the last date is in the past,
1558 ;; or we've got at least the next occurrence itself left. All past
1559 ;; dates are renewed.
1560 (or (not reminders)
1561 (time-less-p (car reminders) now)))
1563 ;; else
1564 (nnheader-report 'nndiary "Could not read file %s" file)
1565 nil))
1567 ;; The end... ===============================================================
1569 (dolist (header nndiary-headers)
1570 (setq header (intern (format "X-Diary-%s" (car header))))
1571 ;; Required for building NOV databases and some other stuff.
1572 (add-to-list 'gnus-extra-headers header)
1573 (add-to-list 'nnmail-extra-headers header))
1575 (unless (assoc "nndiary" gnus-valid-select-methods)
1576 (gnus-declare-backend "nndiary" 'post-mail 'respool 'address))
1578 (provide 'nndiary)
1580 ;;; nndiary.el ends here