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