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