Merge branch 'master' into comment-cache
[emacs.git] / lisp / gnus / nndiary.el
blobbed35b55b3a5f22628e4366d547a94bb5ef3e91d
1 ;;; nndiary.el --- A diary back end for Gnus
3 ;; Copyright (C) 1999-2017 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 ;; Back End behavior customization ===========================================
93 (defgroup nndiary nil
94 "The Gnus Diary back end."
95 :version "22.1"
96 :group 'gnus-diary)
98 (defcustom nndiary-mail-sources
99 `((file :path ,(expand-file-name "~/.nndiary")))
100 "NNDiary specific mail sources.
101 This variable is used by nndiary in place of the standard `mail-sources'
102 variable when `nndiary-get-new-mail' is set to non-nil. These sources
103 must contain diary messages ONLY."
104 :group 'nndiary
105 :group 'mail-source
106 :type 'sexp)
108 (defcustom nndiary-split-methods '(("diary" ""))
109 "NNDiary specific split methods.
110 This variable is used by nndiary in place of the standard
111 `nnmail-split-methods' variable when `nndiary-get-new-mail' is set to
112 non-nil."
113 :group 'nndiary
114 :group 'nnmail-split
115 :type '(choice (repeat :tag "Alist" (group (string :tag "Name") regexp))
116 (function-item nnmail-split-fancy)
117 (function :tag "Other")))
120 (defcustom nndiary-reminders '((0 . day))
121 "Different times when you want to be reminded of your appointments.
122 Diary articles will appear again, as if they'd been just received.
124 Entries look like (3 . day) which means something like \"Please
125 Hortense, would you be so kind as to remind me of my appointments 3 days
126 before the date, thank you very much. Anda, hmmm... by the way, are you
127 doing anything special tonight ?\".
129 The units of measure are 'minute 'hour 'day 'week 'month and 'year (no,
130 not 'century, sorry).
132 NOTE: the units of measure actually express dates, not durations: if you
133 use 'week, messages will pop up on Sundays at 00:00 (or Mondays if
134 `nndiary-week-starts-on-monday' is non-nil) and *not* 7 days before the
135 appointment, if you use 'month, messages will pop up on the first day of
136 each months, at 00:00 and so on.
138 If you really want to specify a duration (like 24 hours exactly), you can
139 use the equivalent in minutes (the smallest unit). A fuzz of 60 seconds
140 maximum in the reminder is not that painful, I think. Although this
141 scheme might appear somewhat weird at a first glance, it is very powerful.
142 In order to make this clear, here are some examples:
144 - (0 . day): this is the default value of `nndiary-reminders'. It means
145 pop up the appointments of the day each morning at 00:00.
147 - (1 . day): this means pop up the appointments the day before, at 00:00.
149 - (6 . hour): for an appointment at 18:30, this would pop up the
150 appointment message at 12:00.
152 - (360 . minute): for an appointment at 18:30 and 15 seconds, this would
153 pop up the appointment message at 12:30."
154 :group 'nndiary
155 :type '(repeat (cons :format "%v\n"
156 (integer :format "%v")
157 (choice :format "%[%v(s)%] before...\n"
158 :value day
159 (const :format "%v" minute)
160 (const :format "%v" hour)
161 (const :format "%v" day)
162 (const :format "%v" week)
163 (const :format "%v" month)
164 (const :format "%v" year)))))
166 (defcustom nndiary-week-starts-on-monday nil
167 "Whether a week starts on monday (otherwise, sunday)."
168 :type 'boolean
169 :group 'nndiary)
172 (define-obsolete-variable-alias 'nndiary-request-create-group-hooks
173 'nndiary-request-create-group-functions "24.3")
174 (defcustom nndiary-request-create-group-functions nil
175 "Hook run after `nndiary-request-create-group' is executed.
176 The hook functions will be called with the full group name as argument."
177 :group 'nndiary
178 :type 'hook)
180 (define-obsolete-variable-alias 'nndiary-request-update-info-hooks
181 'nndiary-request-update-info-functions "24.3")
182 (defcustom nndiary-request-update-info-functions nil
183 "Hook run after `nndiary-request-update-info-group' is executed.
184 The hook functions will be called with the full group name as argument."
185 :group 'nndiary
186 :type 'hook)
188 (define-obsolete-variable-alias 'nndiary-request-accept-article-hooks
189 'nndiary-request-accept-article-functions "24.3")
190 (defcustom nndiary-request-accept-article-functions nil
191 "Hook run before accepting an article.
192 Executed near the beginning of `nndiary-request-accept-article'.
193 The hook functions will be called with the article in the current buffer."
194 :group 'nndiary
195 :type 'hook)
197 (defcustom nndiary-check-directory-twice t
198 "If t, check directories twice to avoid NFS failures."
199 :group 'nndiary
200 :type 'boolean)
203 ;; Back End declaration ======================================================
205 ;; Well, most of this is nnml clonage.
207 (nnoo-declare nndiary)
209 (defvoo nndiary-directory (nnheader-concat gnus-directory "diary/")
210 "Spool directory for the nndiary back end.")
212 (defvoo nndiary-active-file
213 (expand-file-name "active" nndiary-directory)
214 "Active file for the nndiary back end.")
216 (defvoo nndiary-newsgroups-file
217 (expand-file-name "newsgroups" nndiary-directory)
218 "Newsgroups description file for the nndiary back end.")
220 (defvoo nndiary-get-new-mail nil
221 "Whether nndiary gets new mail and split it.
222 Contrary to traditional mail back ends, this variable can be set to t
223 even if your primary mail back end also retrieves mail. In such a case,
224 NDiary uses its own mail-sources and split-methods.")
226 (defvoo nndiary-nov-is-evil nil
227 "If non-nil, Gnus will never use nov databases for nndiary groups.
228 Using nov databases will speed up header fetching considerably.
229 This variable shouldn't be flipped much. If you have, for some reason,
230 set this to t, and want to set it to nil again, you should always run
231 the `nndiary-generate-nov-databases' command. The function will go
232 through all nnml directories and generate nov databases for them
233 all. This may very well take some time.")
235 (defvoo nndiary-prepare-save-mail-hook nil
236 "*Hook run narrowed to an article before saving.")
238 (defvoo nndiary-inhibit-expiry nil
239 "If non-nil, inhibit expiry.")
243 (defconst nndiary-version "0.2-b14"
244 "Current Diary back end version.")
246 (defun nndiary-version ()
247 "Current Diary back end version."
248 (interactive)
249 (message "NNDiary version %s" nndiary-version))
251 (defvoo nndiary-nov-file-name ".overview")
253 (defvoo nndiary-current-directory nil)
254 (defvoo nndiary-current-group nil)
255 (defvoo nndiary-status-string "" )
256 (defvoo nndiary-nov-buffer-alist nil)
257 (defvoo nndiary-group-alist nil)
258 (defvoo nndiary-active-timestamp nil)
259 (defvoo nndiary-article-file-alist nil)
261 (defvoo nndiary-generate-active-function 'nndiary-generate-active-info)
262 (defvoo nndiary-nov-buffer-file-name nil)
263 (defvoo nndiary-file-coding-system nnmail-file-coding-system)
265 (defconst nndiary-headers
266 '(("Minute" 0 59)
267 ("Hour" 0 23)
268 ("Dom" 1 31)
269 ("Month" 1 12)
270 ("Year" 1971)
271 ("Dow" 0 6)
272 ("Time-Zone" (("Y" -43200)
274 ("X" -39600)
276 ("W" -36000)
278 ("V" -32400)
280 ("U" -28800)
281 ("PST" -28800)
283 ("T" -25200)
284 ("MST" -25200)
285 ("PDT" -25200)
287 ("S" -21600)
288 ("CST" -21600)
289 ("MDT" -21600)
291 ("R" -18000)
292 ("EST" -18000)
293 ("CDT" -18000)
295 ("Q" -14400)
296 ("AST" -14400)
297 ("EDT" -14400)
299 ("P" -10800)
300 ("ADT" -10800)
302 ("O" -7200)
304 ("N" -3600)
306 ("Z" 0)
307 ("GMT" 0)
308 ("UT" 0)
309 ("UTC" 0)
310 ("WET" 0)
312 ("A" 3600)
313 ("CET" 3600)
314 ("MET" 3600)
315 ("MEZ" 3600)
316 ("BST" 3600)
317 ("WEST" 3600)
319 ("B" 7200)
320 ("EET" 7200)
321 ("CEST" 7200)
322 ("MEST" 7200)
323 ("MESZ" 7200)
325 ("C" 10800)
327 ("D" 14400)
329 ("E" 18000)
331 ("F" 21600)
333 ("G" 25200)
335 ("H" 28800)
337 ("I" 32400)
338 ("JST" 32400)
340 ("K" 36000)
341 ("GST" 36000)
343 ("L" 39600)
345 ("M" 43200)
346 ("NZST" 43200)
348 ("NZDT" 46800))))
349 ;; List of NNDiary headers that specify the time spec. Each header name is
350 ;; followed by either two integers (specifying a range of possible values
351 ;; for this header) or one list (specifying all the possible values for this
352 ;; header). In the latter case, the list does NOT include the unspecified
353 ;; spec (*).
354 ;; For time zone values, we have symbolic time zone names associated with
355 ;; the (relative) number of seconds ahead GMT.
358 (defsubst nndiary-schedule ()
359 (let (head)
360 (condition-case arg
361 (mapcar
362 (lambda (elt)
363 (setq head (nth 0 elt))
364 (nndiary-parse-schedule (nth 0 elt) (nth 1 elt) (nth 2 elt)))
365 nndiary-headers)
366 (error
367 (nnheader-report 'nndiary "X-Diary-%s header parse error: %s."
368 head (cdr arg))
369 nil))
372 ;;; Interface functions =====================================================
374 (nnoo-define-basics nndiary)
376 (deffoo nndiary-retrieve-headers (sequence &optional group server fetch-old)
377 (when (nndiary-possibly-change-directory group server)
378 (with-current-buffer nntp-server-buffer
379 (erase-buffer)
380 (let* ((file nil)
381 (number (length sequence))
382 (count 0)
383 (file-name-coding-system nnmail-pathname-coding-system)
384 beg article
385 (nndiary-check-directory-twice
386 (and nndiary-check-directory-twice
387 ;; To speed up, disable it in some case.
388 (or (not (numberp nnmail-large-newsgroup))
389 (<= number nnmail-large-newsgroup)))))
390 (if (stringp (car sequence))
391 'headers
392 (if (nndiary-retrieve-headers-with-nov sequence fetch-old)
393 'nov
394 (while sequence
395 (setq article (car sequence))
396 (setq file (nndiary-article-to-file article))
397 (when (and file
398 (file-exists-p file)
399 (not (file-directory-p file)))
400 (insert (format "221 %d Article retrieved.\n" article))
401 (setq beg (point))
402 (nnheader-insert-head file)
403 (goto-char beg)
404 (if (search-forward "\n\n" nil t)
405 (forward-char -1)
406 (goto-char (point-max))
407 (insert "\n\n"))
408 (insert ".\n")
409 (delete-region (point) (point-max)))
410 (setq sequence (cdr sequence))
411 (setq count (1+ count))
412 (and (numberp nnmail-large-newsgroup)
413 (> number nnmail-large-newsgroup)
414 (zerop (% count 20))
415 (nnheader-message 6 "nndiary: Receiving headers... %d%%"
416 (floor (* count 100.0) number))))
418 (and (numberp nnmail-large-newsgroup)
419 (> number nnmail-large-newsgroup)
420 (nnheader-message 6 "nndiary: Receiving headers...done"))
422 (nnheader-fold-continuation-lines)
423 'headers))))))
425 (deffoo nndiary-open-server (server &optional defs)
426 (nnoo-change-server 'nndiary server defs)
427 (when (not (file-exists-p nndiary-directory))
428 (ignore-errors (make-directory nndiary-directory t)))
429 (cond
430 ((not (file-exists-p nndiary-directory))
431 (nndiary-close-server)
432 (nnheader-report 'nndiary "Couldn't create directory: %s"
433 nndiary-directory))
434 ((not (file-directory-p (file-truename nndiary-directory)))
435 (nndiary-close-server)
436 (nnheader-report 'nndiary "Not a directory: %s" nndiary-directory))
438 (nnheader-report 'nndiary "Opened server %s using directory %s"
439 server nndiary-directory)
440 t)))
442 (deffoo nndiary-request-regenerate (server)
443 (nndiary-possibly-change-directory nil server)
444 (nndiary-generate-nov-databases server)
447 (deffoo nndiary-request-article (id &optional group server buffer)
448 (nndiary-possibly-change-directory group server)
449 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
450 (file-name-coding-system nnmail-pathname-coding-system)
451 path gpath group-num)
452 (if (stringp id)
453 (when (and (setq group-num (nndiary-find-group-number id))
454 (cdr
455 (assq (cdr group-num)
456 (nnheader-article-to-file-alist
457 (setq gpath
458 (nnmail-group-pathname
459 (car group-num)
460 nndiary-directory))))))
461 (setq path (concat gpath (int-to-string (cdr group-num)))))
462 (setq path (nndiary-article-to-file id)))
463 (cond
464 ((not path)
465 (nnheader-report 'nndiary "No such article: %s" id))
466 ((not (file-exists-p path))
467 (nnheader-report 'nndiary "No such file: %s" path))
468 ((file-directory-p path)
469 (nnheader-report 'nndiary "File is a directory: %s" path))
470 ((not (save-excursion (let ((nnmail-file-coding-system
471 nndiary-file-coding-system))
472 (nnmail-find-file path))))
473 (nnheader-report 'nndiary "Couldn't read file: %s" path))
475 (nnheader-report 'nndiary "Article %s retrieved" id)
476 ;; We return the article number.
477 (cons (if group-num (car group-num) group)
478 (string-to-number (file-name-nondirectory path)))))))
480 (deffoo nndiary-request-group (group &optional server dont-check info)
481 (let ((file-name-coding-system nnmail-pathname-coding-system))
482 (cond
483 ((not (nndiary-possibly-change-directory group server))
484 (nnheader-report 'nndiary "Invalid group (no such directory)"))
485 ((not (file-exists-p nndiary-current-directory))
486 (nnheader-report 'nndiary "Directory %s does not exist"
487 nndiary-current-directory))
488 ((not (file-directory-p nndiary-current-directory))
489 (nnheader-report 'nndiary "%s is not a directory"
490 nndiary-current-directory))
491 (dont-check
492 (nnheader-report 'nndiary "Group %s selected" group)
495 (nnheader-re-read-dir nndiary-current-directory)
496 (nnmail-activate 'nndiary)
497 (let ((active (nth 1 (assoc group nndiary-group-alist))))
498 (if (not active)
499 (nnheader-report 'nndiary "No such group: %s" group)
500 (nnheader-report 'nndiary "Selected group %s" group)
501 (nnheader-insert "211 %d %d %d %s\n"
502 (max (1+ (- (cdr active) (car active))) 0)
503 (car active) (cdr active) group)))))))
505 (deffoo nndiary-request-scan (&optional group server)
506 ;; Use our own mail sources and split methods while Gnus doesn't let us have
507 ;; multiple back ends for retrieving mail.
508 (let ((mail-sources nndiary-mail-sources)
509 (nnmail-split-methods nndiary-split-methods))
510 (setq nndiary-article-file-alist nil)
511 (nndiary-possibly-change-directory group server)
512 (nnmail-get-new-mail 'nndiary 'nndiary-save-nov nndiary-directory group)))
514 (deffoo nndiary-close-group (group &optional server)
515 (setq nndiary-article-file-alist nil)
518 (deffoo nndiary-request-create-group (group &optional server args)
519 (nndiary-possibly-change-directory nil server)
520 (nnmail-activate 'nndiary)
521 (cond
522 ((assoc group nndiary-group-alist)
524 ((and (file-exists-p (nnmail-group-pathname group nndiary-directory))
525 (not (file-directory-p (nnmail-group-pathname
526 group nndiary-directory))))
527 (nnheader-report 'nndiary "%s is a file"
528 (nnmail-group-pathname group nndiary-directory)))
530 (let (active)
531 (push (list group (setq active (cons 1 0)))
532 nndiary-group-alist)
533 (nndiary-possibly-create-directory group)
534 (nndiary-possibly-change-directory group server)
535 (let ((articles (nnheader-directory-articles nndiary-current-directory)))
536 (when articles
537 (setcar active (apply 'min articles))
538 (setcdr active (apply 'max articles))))
539 (nnmail-save-active nndiary-group-alist nndiary-active-file)
540 (run-hook-with-args 'nndiary-request-create-group-functions
541 (gnus-group-prefixed-name group
542 (list "nndiary" server)))
546 (deffoo nndiary-request-list (&optional server)
547 (save-excursion
548 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
549 (file-name-coding-system nnmail-pathname-coding-system))
550 (nnmail-find-file nndiary-active-file))
551 (setq nndiary-group-alist (nnmail-get-active))
554 (deffoo nndiary-request-newgroups (date &optional server)
555 (nndiary-request-list server))
557 (deffoo nndiary-request-list-newsgroups (&optional server)
558 (save-excursion
559 (nnmail-find-file nndiary-newsgroups-file)))
561 (deffoo nndiary-request-expire-articles (articles group &optional server force)
562 (nndiary-possibly-change-directory group server)
563 (let ((active-articles
564 (nnheader-directory-articles nndiary-current-directory))
565 article rest number)
566 (nnmail-activate 'nndiary)
567 ;; Articles not listed in active-articles are already gone,
568 ;; so don't try to expire them.
569 (setq articles (gnus-intersection articles active-articles))
570 (while articles
571 (setq article (nndiary-article-to-file (setq number (pop articles))))
572 (if (and (nndiary-deletable-article-p group number)
573 ;; Don't use nnmail-expired-article-p. Our notion of expiration
574 ;; is a bit peculiar ...
575 (or force (nndiary-expired-article-p article)))
576 (progn
577 ;; Allow a special target group.
578 (unless (eq nnmail-expiry-target 'delete)
579 (with-temp-buffer
580 (nndiary-request-article number group server (current-buffer))
581 (let ((nndiary-current-directory nil))
582 (nnmail-expiry-target-group nnmail-expiry-target group)))
583 (nndiary-possibly-change-directory group server))
584 (nnheader-message 5 "Deleting article %s in %s" number group)
585 (condition-case ()
586 (funcall nnmail-delete-file-function article)
587 (file-error (push number rest)))
588 (setq active-articles (delq number active-articles))
589 (nndiary-nov-delete-article group number))
590 (push number rest)))
591 (let ((active (nth 1 (assoc group nndiary-group-alist))))
592 (when active
593 (setcar active (or (and active-articles
594 (apply 'min active-articles))
595 (1+ (cdr active)))))
596 (nnmail-save-active nndiary-group-alist nndiary-active-file))
597 (nndiary-save-nov)
598 (nconc rest articles)))
600 (deffoo nndiary-request-move-article
601 (article group server accept-form &optional last move-is-internal)
602 (let ((buf (get-buffer-create " *nndiary move*"))
603 result)
604 (nndiary-possibly-change-directory group server)
605 (nndiary-update-file-alist)
606 (and
607 (nndiary-deletable-article-p group article)
608 (nndiary-request-article article group server)
609 (let (nndiary-current-directory
610 nndiary-current-group
611 nndiary-article-file-alist)
612 (with-current-buffer buf
613 (insert-buffer-substring nntp-server-buffer)
614 (setq result (eval accept-form))
615 (kill-buffer (current-buffer))
616 result))
617 (progn
618 (nndiary-possibly-change-directory group server)
619 (condition-case ()
620 (funcall nnmail-delete-file-function
621 (nndiary-article-to-file article))
622 (file-error nil))
623 (nndiary-nov-delete-article group article)
624 (when last
625 (nndiary-save-nov)
626 (nnmail-save-active nndiary-group-alist nndiary-active-file))))
627 result))
629 (deffoo nndiary-request-accept-article (group &optional server last)
630 (nndiary-possibly-change-directory group server)
631 (nnmail-check-syntax)
632 (run-hooks 'nndiary-request-accept-article-functions)
633 (when (nndiary-schedule)
634 (let (result)
635 (when nnmail-cache-accepted-message-ids
636 (nnmail-cache-insert (nnmail-fetch-field "message-id")
637 group
638 (nnmail-fetch-field "subject")))
639 (if (stringp group)
640 (and
641 (nnmail-activate 'nndiary)
642 (setq result
643 (car (nndiary-save-mail
644 (list (cons group (nndiary-active-number group))))))
645 (progn
646 (nnmail-save-active nndiary-group-alist nndiary-active-file)
647 (and last (nndiary-save-nov))))
648 (and
649 (nnmail-activate 'nndiary)
650 (if (and (not (setq result
651 (nnmail-article-group 'nndiary-active-number)))
652 (yes-or-no-p "Moved to `junk' group; delete article? "))
653 (setq result 'junk)
654 (setq result (car (nndiary-save-mail result))))
655 (when last
656 (nnmail-save-active nndiary-group-alist nndiary-active-file)
657 (when nnmail-cache-accepted-message-ids
658 (nnmail-cache-close))
659 (nndiary-save-nov))))
660 result))
663 (deffoo nndiary-request-post (&optional server)
664 (nnmail-do-request-post 'nndiary-request-accept-article server))
666 (deffoo nndiary-request-replace-article (article group buffer)
667 (nndiary-possibly-change-directory group)
668 (with-current-buffer buffer
669 (nndiary-possibly-create-directory group)
670 (let ((chars (nnmail-insert-lines))
671 (art (concat (int-to-string article) "\t"))
672 headers)
673 (when (ignore-errors
674 (nnmail-write-region
675 (point-min) (point-max)
676 (or (nndiary-article-to-file article)
677 (expand-file-name (int-to-string article)
678 nndiary-current-directory))
679 nil (if (nnheader-be-verbose 5) nil 'nomesg))
681 (setq headers (nndiary-parse-head chars article))
682 ;; Replace the NOV line in the NOV file.
683 (with-current-buffer (nndiary-open-nov group)
684 (goto-char (point-min))
685 (if (or (looking-at art)
686 (search-forward (concat "\n" art) nil t))
687 ;; Delete the old NOV line.
688 (delete-region (progn (beginning-of-line) (point))
689 (progn (forward-line 1) (point)))
690 ;; The line isn't here, so we have to find out where
691 ;; we should insert it. (This situation should never
692 ;; occur, but one likes to make sure...)
693 (while (and (looking-at "[0-9]+\t")
694 (< (string-to-number
695 (buffer-substring
696 (match-beginning 0) (match-end 0)))
697 article)
698 (zerop (forward-line 1)))))
699 (beginning-of-line)
700 (nnheader-insert-nov headers)
701 (nndiary-save-nov)
702 t)))))
704 (deffoo nndiary-request-delete-group (group &optional force server)
705 (nndiary-possibly-change-directory group server)
706 (when force
707 ;; Delete all articles in GROUP.
708 (let ((articles
709 (directory-files
710 nndiary-current-directory t
711 (concat nnheader-numerical-short-files
712 "\\|" (regexp-quote nndiary-nov-file-name) "$")))
713 article)
714 (while articles
715 (setq article (pop articles))
716 (when (file-writable-p article)
717 (nnheader-message 5 "Deleting article %s in %s..." article group)
718 (funcall nnmail-delete-file-function article))))
719 ;; Try to delete the directory itself.
720 (ignore-errors (delete-directory nndiary-current-directory)))
721 ;; Remove the group from all structures.
722 (setq nndiary-group-alist
723 (delq (assoc group nndiary-group-alist) nndiary-group-alist)
724 nndiary-current-group nil
725 nndiary-current-directory nil)
726 ;; Save the active file.
727 (nnmail-save-active nndiary-group-alist nndiary-active-file)
730 (deffoo nndiary-request-rename-group (group new-name &optional server)
731 (nndiary-possibly-change-directory group server)
732 (let ((new-dir (nnmail-group-pathname new-name nndiary-directory))
733 (old-dir (nnmail-group-pathname group nndiary-directory)))
734 (when (ignore-errors
735 (make-directory new-dir t)
737 ;; We move the articles file by file instead of renaming
738 ;; the directory -- there may be subgroups in this group.
739 ;; One might be more clever, I guess.
740 (let ((files (nnheader-article-to-file-alist old-dir)))
741 (while files
742 (rename-file
743 (concat old-dir (cdar files))
744 (concat new-dir (cdar files)))
745 (pop files)))
746 ;; Move .overview file.
747 (let ((overview (concat old-dir nndiary-nov-file-name)))
748 (when (file-exists-p overview)
749 (rename-file overview (concat new-dir nndiary-nov-file-name))))
750 (when (<= (length (directory-files old-dir)) 2)
751 (ignore-errors (delete-directory old-dir)))
752 ;; That went ok, so we change the internal structures.
753 (let ((entry (assoc group nndiary-group-alist)))
754 (when entry
755 (setcar entry new-name))
756 (setq nndiary-current-directory nil
757 nndiary-current-group nil)
758 ;; Save the new group alist.
759 (nnmail-save-active nndiary-group-alist nndiary-active-file)
760 t))))
762 (deffoo nndiary-set-status (article name value &optional group server)
763 (nndiary-possibly-change-directory group server)
764 (let ((file (nndiary-article-to-file article)))
765 (cond
766 ((not (file-exists-p file))
767 (nnheader-report 'nndiary "File %s does not exist" file))
769 (with-temp-file file
770 (nnheader-insert-file-contents file)
771 (nnmail-replace-status name value))
772 t))))
775 ;;; Interface optional functions ============================================
777 (deffoo nndiary-request-update-info (group info &optional server)
778 (nndiary-possibly-change-directory group)
779 (let ((timestamp (gnus-group-parameter-value (gnus-info-params info)
780 'timestamp t)))
781 (if (not timestamp)
782 (nnheader-report 'nndiary "Group %s doesn't have a timestamp" group)
783 ;; else
784 ;; Figure out which articles should be re-new'ed
785 (let ((articles (nndiary-flatten (gnus-info-read info) 0))
786 article file unread buf)
787 (save-excursion
788 (setq buf (nnheader-set-temp-buffer " *nndiary update*"))
789 (while (setq article (pop articles))
790 (setq file (concat nndiary-current-directory
791 (int-to-string article)))
792 (and (file-exists-p file)
793 (nndiary-renew-article-p file timestamp)
794 (push article unread)))
795 ;;(message "unread: %s" unread)
796 (sit-for 1)
797 (kill-buffer buf))
798 (setq unread (sort unread '<))
799 (and unread
800 (gnus-info-set-read info (gnus-update-read-articles
801 (gnus-info-group info) unread t)))
803 (run-hook-with-args 'nndiary-request-update-info-functions
804 (gnus-info-group info))
809 ;;; Internal functions ======================================================
811 (defun nndiary-article-to-file (article)
812 (nndiary-update-file-alist)
813 (let (file)
814 (if (setq file (cdr (assq article nndiary-article-file-alist)))
815 (expand-file-name file nndiary-current-directory)
816 ;; Just to make sure nothing went wrong when reading over NFS --
817 ;; check once more.
818 (if nndiary-check-directory-twice
819 (when (file-exists-p
820 (setq file (expand-file-name (number-to-string article)
821 nndiary-current-directory)))
822 (nndiary-update-file-alist t)
823 file)))))
825 (defun nndiary-deletable-article-p (group article)
826 "Say whether ARTICLE in GROUP can be deleted."
827 (let (path)
828 (when (setq path (nndiary-article-to-file article))
829 (when (file-writable-p path)
830 (or (not nnmail-keep-last-article)
831 (not (eq (cdr (nth 1 (assoc group nndiary-group-alist)))
832 article)))))))
834 ;; Find an article number in the current group given the Message-ID.
835 (defun nndiary-find-group-number (id)
836 (with-current-buffer (get-buffer-create " *nndiary id*")
837 (let ((alist nndiary-group-alist)
838 number)
839 ;; We want to look through all .overview files, but we want to
840 ;; start with the one in the current directory. It seems most
841 ;; likely that the article we are looking for is in that group.
842 (if (setq number (nndiary-find-id nndiary-current-group id))
843 (cons nndiary-current-group number)
844 ;; It wasn't there, so we look through the other groups as well.
845 (while (and (not number)
846 alist)
847 (or (string= (caar alist) nndiary-current-group)
848 (setq number (nndiary-find-id (caar alist) id)))
849 (or number
850 (setq alist (cdr alist))))
851 (and number
852 (cons (caar alist) number))))))
854 (defun nndiary-find-id (group id)
855 (erase-buffer)
856 (let ((nov (expand-file-name nndiary-nov-file-name
857 (nnmail-group-pathname group
858 nndiary-directory)))
859 number found)
860 (when (file-exists-p nov)
861 (nnheader-insert-file-contents nov)
862 (while (and (not found)
863 (search-forward id nil t)) ; We find the ID.
864 ;; And the id is in the fourth field.
865 (if (not (and (search-backward "\t" nil t 4)
866 (not (search-backward"\t" (point-at-bol) t))))
867 (forward-line 1)
868 (beginning-of-line)
869 (setq found t)
870 ;; We return the article number.
871 (setq number
872 (ignore-errors (read (current-buffer))))))
873 number)))
875 (defun nndiary-retrieve-headers-with-nov (articles &optional fetch-old)
876 (if (or gnus-nov-is-evil nndiary-nov-is-evil)
878 (let ((nov (expand-file-name nndiary-nov-file-name
879 nndiary-current-directory)))
880 (when (file-exists-p nov)
881 (with-current-buffer nntp-server-buffer
882 (erase-buffer)
883 (nnheader-insert-file-contents nov)
884 (if (and fetch-old
885 (not (numberp fetch-old)))
886 t ; Don't remove anything.
887 (nnheader-nov-delete-outside-range
888 (if fetch-old (max 1 (- (car articles) fetch-old))
889 (car articles))
890 (car (last articles)))
891 t))))))
893 (defun nndiary-possibly-change-directory (group &optional server)
894 (when (and server
895 (not (nndiary-server-opened server)))
896 (nndiary-open-server server))
897 (if (not group)
899 (let ((pathname (nnmail-group-pathname group nndiary-directory))
900 (file-name-coding-system nnmail-pathname-coding-system))
901 (when (not (equal pathname nndiary-current-directory))
902 (setq nndiary-current-directory pathname
903 nndiary-current-group group
904 nndiary-article-file-alist nil))
905 (file-exists-p nndiary-current-directory))))
907 (defun nndiary-possibly-create-directory (group)
908 (let ((dir (nnmail-group-pathname group nndiary-directory)))
909 (unless (file-exists-p dir)
910 (make-directory (directory-file-name dir) t)
911 (nnheader-message 5 "Creating mail directory %s" dir))))
913 (defun nndiary-save-mail (group-art)
914 "Called narrowed to an article."
915 (let (chars headers)
916 (setq chars (nnmail-insert-lines))
917 (nnmail-insert-xref group-art)
918 (run-hooks 'nnmail-prepare-save-mail-hook)
919 (run-hooks 'nndiary-prepare-save-mail-hook)
920 (goto-char (point-min))
921 (while (looking-at "From ")
922 (replace-match "X-From-Line: ")
923 (forward-line 1))
924 ;; We save the article in all the groups it belongs in.
925 (let ((ga group-art)
926 first)
927 (while ga
928 (nndiary-possibly-create-directory (caar ga))
929 (let ((file (concat (nnmail-group-pathname
930 (caar ga) nndiary-directory)
931 (int-to-string (cdar ga)))))
932 (if first
933 ;; It was already saved, so we just make a hard link.
934 (funcall nnmail-crosspost-link-function first file t)
935 ;; Save the article.
936 (nnmail-write-region (point-min) (point-max) file nil
937 (if (nnheader-be-verbose 5) nil 'nomesg))
938 (setq first file)))
939 (setq ga (cdr ga))))
940 ;; Generate a nov line for this article. We generate the nov
941 ;; line after saving, because nov generation destroys the
942 ;; header.
943 (setq headers (nndiary-parse-head chars))
944 ;; Output the nov line to all nov databases that should have it.
945 (let ((ga group-art))
946 (while ga
947 (nndiary-add-nov (caar ga) (cdar ga) headers)
948 (setq ga (cdr ga))))
949 group-art))
951 (defun nndiary-active-number (group)
952 "Compute the next article number in GROUP."
953 (let ((active (cadr (assoc group nndiary-group-alist))))
954 ;; The group wasn't known to nndiary, so we just create an active
955 ;; entry for it.
956 (unless active
957 ;; Perhaps the active file was corrupt? See whether
958 ;; there are any articles in this group.
959 (nndiary-possibly-create-directory group)
960 (nndiary-possibly-change-directory group)
961 (unless nndiary-article-file-alist
962 (setq nndiary-article-file-alist
963 (sort
964 (nnheader-article-to-file-alist nndiary-current-directory)
965 'car-less-than-car)))
966 (setq active
967 (if nndiary-article-file-alist
968 (cons (caar nndiary-article-file-alist)
969 (caar (last nndiary-article-file-alist)))
970 (cons 1 0)))
971 (push (list group active) nndiary-group-alist))
972 (setcdr active (1+ (cdr active)))
973 (while (file-exists-p
974 (expand-file-name (int-to-string (cdr active))
975 (nnmail-group-pathname group nndiary-directory)))
976 (setcdr active (1+ (cdr active))))
977 (cdr active)))
979 (defun nndiary-add-nov (group article headers)
980 "Add a nov line for the GROUP base."
981 (with-current-buffer (nndiary-open-nov group)
982 (goto-char (point-max))
983 (mail-header-set-number headers article)
984 (nnheader-insert-nov headers)))
986 (defsubst nndiary-header-value ()
987 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
989 (defun nndiary-parse-head (chars &optional number)
990 "Parse the head of the current buffer."
991 (save-excursion
992 (save-restriction
993 (unless (zerop (buffer-size))
994 (narrow-to-region
995 (goto-char (point-min))
996 (if (search-forward "\n\n" nil t) (1- (point)) (point-max))))
997 (let ((headers (nnheader-parse-naked-head)))
998 (mail-header-set-chars headers chars)
999 (mail-header-set-number headers number)
1000 headers))))
1002 (defun nndiary-open-nov (group)
1003 (or (cdr (assoc group nndiary-nov-buffer-alist))
1004 (let ((buffer (get-buffer-create (format " *nndiary overview %s*"
1005 group))))
1006 (with-current-buffer buffer
1007 (set (make-local-variable 'nndiary-nov-buffer-file-name)
1008 (expand-file-name
1009 nndiary-nov-file-name
1010 (nnmail-group-pathname group nndiary-directory)))
1011 (erase-buffer)
1012 (when (file-exists-p nndiary-nov-buffer-file-name)
1013 (nnheader-insert-file-contents nndiary-nov-buffer-file-name)))
1014 (push (cons group buffer) nndiary-nov-buffer-alist)
1015 buffer)))
1017 (defun nndiary-save-nov ()
1018 (save-excursion
1019 (while nndiary-nov-buffer-alist
1020 (when (buffer-name (cdar nndiary-nov-buffer-alist))
1021 (set-buffer (cdar nndiary-nov-buffer-alist))
1022 (when (buffer-modified-p)
1023 (nnmail-write-region 1 (point-max) nndiary-nov-buffer-file-name
1024 nil 'nomesg))
1025 (set-buffer-modified-p nil)
1026 (kill-buffer (current-buffer)))
1027 (setq nndiary-nov-buffer-alist (cdr nndiary-nov-buffer-alist)))))
1029 ;;;###autoload
1030 (defun nndiary-generate-nov-databases (&optional server)
1031 "Generate NOV databases in all nndiary directories."
1032 (interactive (list (or (nnoo-current-server 'nndiary) "")))
1033 ;; Read the active file to make sure we don't re-use articles
1034 ;; numbers in empty groups.
1035 (nnmail-activate 'nndiary)
1036 (unless (nndiary-server-opened server)
1037 (nndiary-open-server server))
1038 (setq nndiary-directory (expand-file-name nndiary-directory))
1039 ;; Recurse down the directories.
1040 (nndiary-generate-nov-databases-1 nndiary-directory nil t)
1041 ;; Save the active file.
1042 (nnmail-save-active nndiary-group-alist nndiary-active-file))
1044 (defun nndiary-generate-nov-databases-1 (dir &optional seen no-active)
1045 "Regenerate the NOV database in DIR."
1046 (interactive "DRegenerate NOV in: ")
1047 (setq dir (file-name-as-directory dir))
1048 ;; Only scan this sub-tree if we haven't been here yet.
1049 (unless (member (file-truename dir) seen)
1050 (push (file-truename dir) seen)
1051 ;; We descend recursively
1052 (let ((dirs (directory-files dir t nil t))
1053 dir)
1054 (while (setq dir (pop dirs))
1055 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
1056 (file-directory-p dir))
1057 (nndiary-generate-nov-databases-1 dir seen))))
1058 ;; Do this directory.
1059 (let ((nndiary-files (sort (nnheader-article-to-file-alist dir)
1060 'car-less-than-car)))
1061 (if (not nndiary-files)
1062 (let* ((group (nnheader-file-to-group
1063 (directory-file-name dir) nndiary-directory))
1064 (info (cadr (assoc group nndiary-group-alist))))
1065 (when info
1066 (setcar info (1+ (cdr info)))))
1067 (funcall nndiary-generate-active-function dir)
1068 ;; Generate the nov file.
1069 (nndiary-generate-nov-file dir nndiary-files)
1070 (unless no-active
1071 (nnmail-save-active nndiary-group-alist nndiary-active-file))))))
1073 (defvar nndiary-files) ; dynamically bound in nndiary-generate-nov-databases-1
1074 (defun nndiary-generate-active-info (dir)
1075 ;; Update the active info for this group.
1076 (let* ((group (nnheader-file-to-group
1077 (directory-file-name dir) nndiary-directory))
1078 (entry (assoc group nndiary-group-alist))
1079 (last (or (caadr entry) 0)))
1080 (setq nndiary-group-alist (delq entry nndiary-group-alist))
1081 (push (list group
1082 (cons (or (caar nndiary-files) (1+ last))
1083 (max last
1084 (or (caar (last nndiary-files))
1085 0))))
1086 nndiary-group-alist)))
1088 (defun nndiary-generate-nov-file (dir files)
1089 (let* ((dir (file-name-as-directory dir))
1090 (nov (concat dir nndiary-nov-file-name))
1091 (nov-buffer (get-buffer-create " *nov*"))
1092 chars file headers)
1093 ;; Init the nov buffer.
1094 (with-current-buffer nov-buffer
1095 (buffer-disable-undo)
1096 (erase-buffer)
1097 (set-buffer nntp-server-buffer)
1098 ;; Delete the old NOV file.
1099 (when (file-exists-p nov)
1100 (funcall nnmail-delete-file-function nov))
1101 (while files
1102 (unless (file-directory-p (setq file (concat dir (cdar files))))
1103 (erase-buffer)
1104 (nnheader-insert-file-contents file)
1105 (narrow-to-region
1106 (goto-char (point-min))
1107 (progn
1108 (search-forward "\n\n" nil t)
1109 (setq chars (- (point-max) (point)))
1110 (max 1 (1- (point)))))
1111 (unless (zerop (buffer-size))
1112 (goto-char (point-min))
1113 (setq headers (nndiary-parse-head chars (caar files)))
1114 (with-current-buffer nov-buffer
1115 (goto-char (point-max))
1116 (nnheader-insert-nov headers)))
1117 (widen))
1118 (setq files (cdr files)))
1119 (with-current-buffer nov-buffer
1120 (nnmail-write-region 1 (point-max) nov nil 'nomesg)
1121 (kill-buffer (current-buffer))))))
1123 (defun nndiary-nov-delete-article (group article)
1124 (with-current-buffer (nndiary-open-nov group)
1125 (when (nnheader-find-nov-line article)
1126 (delete-region (point) (progn (forward-line 1) (point)))
1127 (when (bobp)
1128 (let ((active (cadr (assoc group nndiary-group-alist)))
1129 num)
1130 (when active
1131 (if (eobp)
1132 (setf (car active) (1+ (cdr active)))
1133 (when (and (setq num (ignore-errors (read (current-buffer))))
1134 (numberp num))
1135 (setf (car active) num)))))))
1138 (defun nndiary-update-file-alist (&optional force)
1139 (when (or (not nndiary-article-file-alist)
1140 force)
1141 (setq nndiary-article-file-alist
1142 (nnheader-article-to-file-alist nndiary-current-directory))))
1145 (defun nndiary-string-to-number (str min &optional max)
1146 ;; Like `string-to-number' but barf if STR is not exactly an integer, and not
1147 ;; within the specified bounds.
1148 ;; Signals are caught by `nndiary-schedule'.
1149 (if (not (string-match "^[ \t]*[0-9]+[ \t]*$" str))
1150 (error "Not an integer value")
1151 ;; else
1152 (let ((val (string-to-number str)))
1153 (and (or (< val min)
1154 (and max (> val max)))
1155 (error "Value out of range"))
1156 val)))
1158 (defun nndiary-parse-schedule-value (str min-or-values max)
1159 ;; Parse the schedule string STR, or signal an error.
1160 ;; Signals are caught by `nndiary-schedule'.
1161 (if (string-match "[ \t]*\\*[ \t]*" str)
1162 ;; unspecified
1164 ;; specified
1165 (if (listp min-or-values)
1166 ;; min-or-values is values
1167 ;; #### NOTE: this is actually only a hack for time zones.
1168 (let ((val (and (string-match "[ \t]*\\([^ \t]+\\)[ \t]*" str)
1169 (match-string 1 str))))
1170 (if (and val (setq val (assoc val min-or-values)))
1171 (list (cadr val))
1172 (error "Invalid syntax")))
1173 ;; min-or-values is min
1174 (mapcar
1175 (lambda (val)
1176 (let ((res (split-string val "-")))
1177 (cond
1178 ((= (length res) 1)
1179 (nndiary-string-to-number (car res) min-or-values max))
1180 ((= (length res) 2)
1181 ;; don't know if crontab accepts this, but ensure
1182 ;; that BEG is <= END
1183 (let ((beg (nndiary-string-to-number (car res) min-or-values max))
1184 (end (nndiary-string-to-number (cadr res) min-or-values max)))
1185 (cond ((< beg end)
1186 (cons beg end))
1187 ((= beg end)
1188 beg)
1190 (cons end beg)))))
1192 (error "Invalid syntax")))
1194 (split-string str ",")))
1197 ;; ### FIXME: remove this function if it's used only once.
1198 (defun nndiary-parse-schedule (head min-or-values max)
1199 ;; Parse the cron-like value of header X-Diary-HEAD in current buffer.
1200 ;; - Returns nil if `*'
1201 ;; - Otherwise returns a list of integers and/or ranges (BEG . END)
1202 ;; The exception is the Timze-Zone value which is always of the form (STR).
1203 ;; Signals are caught by `nndiary-schedule'.
1204 (let ((header (format "^X-Diary-%s: \\(.*\\)$" head)))
1205 (goto-char (point-min))
1206 (if (not (re-search-forward header nil t))
1207 (error "Header missing")
1208 ;; else
1209 (nndiary-parse-schedule-value (match-string 1) min-or-values max))
1212 (defun nndiary-max (spec)
1213 ;; Returns the max of specification SPEC, or nil for permanent schedules.
1214 (unless (null spec)
1215 (let ((elts spec)
1216 (max 0)
1217 elt)
1218 (while (setq elt (pop elts))
1219 (if (integerp elt)
1220 (and (> elt max) (setq max elt))
1221 (and (> (cdr elt) max) (setq max (cdr elt)))))
1222 max)))
1224 (defun nndiary-flatten (spec min &optional max)
1225 ;; flatten the spec by expanding ranges to all possible values.
1226 (let (flat n)
1227 (cond ((null spec)
1228 ;; this happens when I flatten something else than one of my
1229 ;; schedules (a list of read articles for instance).
1230 (unless (null max)
1231 (setq n min)
1232 (while (<= n max)
1233 (push n flat)
1234 (setq n (1+ n)))))
1236 (let ((elts spec)
1237 elt)
1238 (while (setq elt (pop elts))
1239 (if (integerp elt)
1240 (push elt flat)
1241 ;; else
1242 (setq n (car elt))
1243 (while (<= n (cdr elt))
1244 (push n flat)
1245 (setq n (1+ n))))))))
1246 flat))
1248 (defun nndiary-unflatten (spec)
1249 ;; opposite of flatten: build ranges if possible
1250 (setq spec (sort spec '<))
1251 (let (min max res)
1252 (while (setq min (pop spec))
1253 (setq max min)
1254 (while (and (car spec) (= (car spec) (1+ max)))
1255 (setq max (1+ max))
1256 (pop spec))
1257 (if (= max min)
1258 (setq res (append res (list min)))
1259 (setq res (append res (list (cons min max))))))
1260 res))
1262 (defun nndiary-compute-reminders (date)
1263 ;; Returns a list of times corresponding to the reminders of date DATE.
1264 ;; See the comment in `nndiary-reminders' about rounding.
1265 (let* ((reminders nndiary-reminders)
1266 (date-elts (decode-time date))
1267 ;; ### NOTE: out-of-range values are accepted by encode-time. This
1268 ;; makes our life easier.
1269 (monday (- (nth 3 date-elts)
1270 (if nndiary-week-starts-on-monday
1271 (if (zerop (nth 6 date-elts))
1273 (- (nth 6 date-elts) 1))
1274 (nth 6 date-elts))))
1275 reminder res)
1276 ;; remove the DOW and DST entries
1277 (setcdr (nthcdr 5 date-elts) (nthcdr 8 date-elts))
1278 (while (setq reminder (pop reminders))
1279 (push
1280 (cond ((eq (cdr reminder) 'minute)
1281 (time-subtract
1282 (apply 'encode-time 0 (nthcdr 1 date-elts))
1283 (seconds-to-time (* (car reminder) 60.0))))
1284 ((eq (cdr reminder) 'hour)
1285 (time-subtract
1286 (apply 'encode-time 0 0 (nthcdr 2 date-elts))
1287 (seconds-to-time (* (car reminder) 3600.0))))
1288 ((eq (cdr reminder) 'day)
1289 (time-subtract
1290 (apply 'encode-time 0 0 0 (nthcdr 3 date-elts))
1291 (seconds-to-time (* (car reminder) 86400.0))))
1292 ((eq (cdr reminder) 'week)
1293 (time-subtract
1294 (apply 'encode-time 0 0 0 monday (nthcdr 4 date-elts))
1295 (seconds-to-time (* (car reminder) 604800.0))))
1296 ((eq (cdr reminder) 'month)
1297 (time-subtract
1298 (apply 'encode-time 0 0 0 1 (nthcdr 4 date-elts))
1299 (seconds-to-time (* (car reminder) 18748800.0))))
1300 ((eq (cdr reminder) 'year)
1301 (time-subtract
1302 (apply 'encode-time 0 0 0 1 1 (nthcdr 5 date-elts))
1303 (seconds-to-time (* (car reminder) 400861056.0)))))
1304 res))
1305 (sort res 'time-less-p)))
1307 ;; FIXME: "occurrence" is misspelled in this function name.
1309 (defun nndiary-last-occurence (sched)
1310 ;; Returns the last occurrence of schedule SCHED as an Emacs time struct, or
1311 ;; nil for permanent schedule or errors.
1312 (let ((minute (nndiary-max (nth 0 sched)))
1313 (hour (nndiary-max (nth 1 sched)))
1314 (year (nndiary-max (nth 4 sched)))
1315 (time-zone (or (and (nth 6 sched) (car (nth 6 sched)))
1316 (current-time-zone))))
1317 (when year
1318 (or minute (setq minute 59))
1319 (or hour (setq hour 23))
1320 ;; I'll just compute all possible values and test them by decreasing
1321 ;; order until one succeeds. This is probably quite rude, but I got
1322 ;; bored in finding a good algorithm for doing that ;-)
1323 ;; ### FIXME: remove identical entries.
1324 (let ((dom-list (nth 2 sched))
1325 (month-list (sort (nndiary-flatten (nth 3 sched) 1 12) '>))
1326 (year-list (sort (nndiary-flatten (nth 4 sched) 1971) '>))
1327 (dow-list (nth 5 sched)))
1328 ;; Special case: an asterisk in one of the days specifications means
1329 ;; that only the other should be taken into account. If both are
1330 ;; unspecified, you would get all possible days in both.
1331 (cond ((null dow-list)
1332 ;; this gets all days if dom-list is nil
1333 (setq dom-list (nndiary-flatten dom-list 1 31)))
1334 ((null dom-list)
1335 ;; this also gets all days if dow-list is nil
1336 (setq dow-list (nndiary-flatten dow-list 0 6)))
1338 (setq dom-list (nndiary-flatten dom-list 1 31))
1339 (setq dow-list (nndiary-flatten dow-list 0 6))))
1341 (catch 'found
1342 (while (setq year (pop year-list))
1343 (let ((months month-list)
1344 month)
1345 (while (setq month (pop months))
1346 ;; Now we must merge the Dows with the Doms. To do that, we
1347 ;; have to know which day is the 1st one for this month.
1348 ;; Maybe there's simpler, but decode-time(encode-time) will
1349 ;; give us the answer.
1350 (let ((first (nth 6 (decode-time
1351 (encode-time 0 0 0 1 month year
1352 time-zone))))
1353 (max (cond ((= month 2)
1354 (if (date-leap-year-p year) 29 28))
1355 ((<= month 7)
1356 (if (zerop (% month 2)) 30 31))
1358 (if (zerop (% month 2)) 31 30))))
1359 (doms dom-list)
1360 (dows dow-list)
1361 day days)
1362 ;; first, review the doms to see if they are valid.
1363 (while (setq day (pop doms))
1364 (and (<= day max)
1365 (push day days)))
1366 ;; second add all possible dows
1367 (while (setq day (pop dows))
1368 ;; days start at 1.
1369 (setq day (1+ (- day first)))
1370 (and (< day 0) (setq day (+ 7 day)))
1371 (while (<= day max)
1372 (push day days)
1373 (setq day (+ 7 day))))
1374 ;; Finally, if we have some days, they are valid
1375 (when days
1376 (sort days '>)
1377 (throw 'found
1378 (encode-time 0 minute hour
1379 (car days) month year time-zone)))
1380 )))))
1381 ;; There's an upper limit, but we didn't find any last occurrence.
1382 ;; This means that the schedule is undecidable. This can happen if
1383 ;; you happen to say something like "each Feb 31 until 2038".
1384 (progn
1385 (nnheader-report 'nndiary "Undecidable schedule")
1386 nil))
1387 ))))
1389 ;; FIXME: "occurrence" is misspelled in this function name.
1391 (defun nndiary-next-occurence (sched now)
1392 ;; Returns the next occurrence of schedule SCHED, starting from time NOW.
1393 ;; If there's no next occurrence, returns the last one (if any) which is then
1394 ;; in the past.
1395 (let* ((today (decode-time now))
1396 (this-minute (nth 1 today))
1397 (this-hour (nth 2 today))
1398 (this-day (nth 3 today))
1399 (this-month (nth 4 today))
1400 (this-year (nth 5 today))
1401 (minute-list (sort (nndiary-flatten (nth 0 sched) 0 59) '<))
1402 (hour-list (sort (nndiary-flatten (nth 1 sched) 0 23) '<))
1403 (dom-list (nth 2 sched))
1404 (month-list (sort (nndiary-flatten (nth 3 sched) 1 12) '<))
1405 (years (if (nth 4 sched)
1406 (sort (nndiary-flatten (nth 4 sched) 1971) '<)
1408 (dow-list (nth 5 sched))
1409 (year (1- this-year))
1410 (time-zone (or (and (nth 6 sched) (car (nth 6 sched)))
1411 (current-time-zone))))
1412 ;; Special case: an asterisk in one of the days specifications means that
1413 ;; only the other should be taken into account. If both are unspecified,
1414 ;; you would get all possible days in both.
1415 (cond ((null dow-list)
1416 ;; this gets all days if dom-list is nil
1417 (setq dom-list (nndiary-flatten dom-list 1 31)))
1418 ((null dom-list)
1419 ;; this also gets all days if dow-list is nil
1420 (setq dow-list (nndiary-flatten dow-list 0 6)))
1422 (setq dom-list (nndiary-flatten dom-list 1 31))
1423 (setq dow-list (nndiary-flatten dow-list 0 6))))
1424 ;; Remove past years.
1425 (unless (eq years t)
1426 (while (and (car years) (< (car years) this-year))
1427 (pop years)))
1428 (if years
1429 ;; Because we might not be limited in years, we must guard against
1430 ;; infinite loops. Appart from cases like Feb 31, there are probably
1431 ;; other ones, (no monday XXX 2nd etc). I don't know any algorithm to
1432 ;; decide this, so I assume that if we reach 10 years later, the
1433 ;; schedule is undecidable.
1435 (catch 'found
1436 (while (if (eq years t)
1437 (and (setq year (1+ year))
1438 (<= year (+ 10 this-year)))
1439 (setq year (pop years)))
1440 (let ((months month-list)
1441 month)
1442 ;; Remove past months for this year.
1443 (and (= year this-year)
1444 (while (and (car months) (< (car months) this-month))
1445 (pop months)))
1446 (while (setq month (pop months))
1447 ;; Now we must merge the Dows with the Doms. To do that, we
1448 ;; have to know which day is the 1st one for this month.
1449 ;; Maybe there's simpler, but decode-time(encode-time) will
1450 ;; give us the answer.
1451 (let ((first (nth 6 (decode-time
1452 (encode-time 0 0 0 1 month year
1453 time-zone))))
1454 (max (cond ((= month 2)
1455 (if (date-leap-year-p year) 29 28))
1456 ((<= month 7)
1457 (if (zerop (% month 2)) 30 31))
1459 (if (zerop (% month 2)) 31 30))))
1460 (doms dom-list)
1461 (dows dow-list)
1462 day days)
1463 ;; first, review the doms to see if they are valid.
1464 (while (setq day (pop doms))
1465 (and (<= day max)
1466 (push day days)))
1467 ;; second add all possible dows
1468 (while (setq day (pop dows))
1469 ;; days start at 1.
1470 (setq day (1+ (- day first)))
1471 (and (< day 0) (setq day (+ 7 day)))
1472 (while (<= day max)
1473 (push day days)
1474 (setq day (+ 7 day))))
1475 ;; Aaaaaaall right. Now we have a valid list of DAYS for
1476 ;; this month and this year.
1477 (when days
1478 (setq days (sort days '<))
1479 ;; Remove past days for this year and this month.
1480 (and (= year this-year)
1481 (= month this-month)
1482 (while (and (car days) (< (car days) this-day))
1483 (pop days)))
1484 (while (setq day (pop days))
1485 (let ((hours hour-list)
1486 hour)
1487 ;; Remove past hours for this year, this month and
1488 ;; this day.
1489 (and (= year this-year)
1490 (= month this-month)
1491 (= day this-day)
1492 (while (and (car hours)
1493 (< (car hours) this-hour))
1494 (pop hours)))
1495 (while (setq hour (pop hours))
1496 (let ((minutes minute-list)
1497 minute)
1498 ;; Remove past hours for this year, this month,
1499 ;; this day and this hour.
1500 (and (= year this-year)
1501 (= month this-month)
1502 (= day this-day)
1503 (= hour this-hour)
1504 (while (and (car minutes)
1505 (< (car minutes) this-minute))
1506 (pop minutes)))
1507 (while (setq minute (pop minutes))
1508 ;; Ouch! Here, we've got a complete valid
1509 ;; schedule. It's a good one if it's in the
1510 ;; future.
1511 (let ((time (encode-time 0 minute hour day
1512 month year
1513 time-zone)))
1514 (and (time-less-p now time)
1515 (throw 'found time)))
1516 ))))
1520 (nndiary-last-occurence sched))
1521 ;; else
1522 (nndiary-last-occurence sched))
1525 (defun nndiary-expired-article-p (file)
1526 (with-temp-buffer
1527 (if (nnheader-insert-head file)
1528 (let ((sched (nndiary-schedule)))
1529 ;; An article has expired if its last schedule (if any) is in the
1530 ;; past. A permanent schedule never expires.
1531 (and sched
1532 (setq sched (nndiary-last-occurence sched))
1533 (time-less-p sched (current-time))))
1534 ;; else
1535 (nnheader-report 'nndiary "Could not read file %s" file)
1536 nil)
1539 (defun nndiary-renew-article-p (file timestamp)
1540 (erase-buffer)
1541 (if (nnheader-insert-head file)
1542 (let ((now (current-time))
1543 (sched (nndiary-schedule)))
1544 ;; The article should be re-considered as unread if there's a reminder
1545 ;; between the group timestamp and the current time.
1546 (when (and sched (setq sched (nndiary-next-occurence sched now)))
1547 (let ((reminders ;; add the next occurrence itself at the end.
1548 (append (nndiary-compute-reminders sched) (list sched))))
1549 (while (and reminders (time-less-p (car reminders) timestamp))
1550 (pop reminders))
1551 ;; The reminders might be empty if the last date is in the past,
1552 ;; or we've got at least the next occurrence itself left. All past
1553 ;; dates are renewed.
1554 (or (not reminders)
1555 (time-less-p (car reminders) now)))
1557 ;; else
1558 (nnheader-report 'nndiary "Could not read file %s" file)
1559 nil))
1561 ;; The end... ===============================================================
1563 (dolist (header nndiary-headers)
1564 (setq header (intern (format "X-Diary-%s" (car header))))
1565 ;; Required for building NOV databases and some other stuff.
1566 (add-to-list 'gnus-extra-headers header)
1567 (add-to-list 'nnmail-extra-headers header))
1569 (unless (assoc "nndiary" gnus-valid-select-methods)
1570 (gnus-declare-backend "nndiary" 'post-mail 'respool 'address))
1572 (provide 'nndiary)
1574 ;;; nndiary.el ends here