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