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