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