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