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