1 ;;; nndiary.el --- A diary back end for Gnus
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 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 3 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; Contents management by FCM version 0.1.
34 ;; nndiary is a mail back end designed to handle mails as diary event
35 ;; reminders. It is now fully documented in the Gnus manual.
41 ;; * Respooling doesn't work because contrary to the request-scan function,
42 ;; Gnus won't allow me to override the split methods when calling the
43 ;; respooling back end functions.
44 ;; * There's a bug in the time zone mechanism with variable TZ locations.
45 ;; * We could allow a keyword like `ask' in X-Diary-* headers, that would mean
46 ;; "ask for value upon reception of the message".
47 ;; * We could add an optional header X-Diary-Reminders to specify a special
48 ;; reminders value for this message. Suggested by Jody Klymak.
49 ;; * We should check messages validity in other circumstances than just
50 ;; moving an article from somewhere else (request-accept). For instance,
51 ;; when editing / saving and so on.
57 ;; * nnoo. NNDiary is very similar to nnml. This makes the idea of using nnoo
58 ;; (to derive nndiary from nnml) natural. However, my experience with nnoo
59 ;; is that for reasonably complex back ends like this one, noo is a burden
60 ;; rather than an help. It's tricky to use, not everything can be inherited,
61 ;; what can be inherited and when is not very clear, and you've got to be
62 ;; very careful because a little mistake can fuck up your other back ends,
63 ;; especially because their variables will be use instead of your real ones.
64 ;; Finally, I found it easier to just clone the needed parts of nnml, and
65 ;; tracking nnml updates is not a big deal.
67 ;; IMHO, nnoo is actually badly designed. A much simpler, and yet more
68 ;; powerful one would be to make *real* functions and variables for a new
69 ;; back end based on another. Lisp is a reflexive language so that's a very
70 ;; easy thing to do: inspect the function's form, replace occurences of
71 ;; <nnfrom> (even in strings) with <nnto>, and you're done.
73 ;; * nndiary-get-new-mail, nndiary-mail-source and nndiary-split-methods:
74 ;; NNDiary has some experimental parts, in the sense Gnus normally uses only
75 ;; one mail back ends for mail retreival and splitting. This back end is
76 ;; also an attempt to make it behave differently. For Gnus developpers: as
77 ;; you can see if you snarf into the code, that was not a very difficult
78 ;; thing to do. Something should be done about the respooling breakage
87 (eval-when-compile (require 'cl))
92 ;; Compatibility Functions =================================================
95 (if (fboundp 'signal-error)
96 (defun nndiary-error (&rest args)
97 (apply #'signal-error 'nndiary args))
98 (defun nndiary-error (&rest args)
99 (apply #'error args))))
102 ;; Back End behavior customization ===========================================
104 (defgroup nndiary nil
105 "The Gnus Diary back end."
109 (defcustom nndiary-mail-sources
110 `((file :path ,(expand-file-name "~/.nndiary")))
111 "*NNDiary specific mail sources.
112 This variable is used by nndiary in place of the standard `mail-sources'
113 variable when `nndiary-get-new-mail' is set to non-nil. These sources
114 must contain diary messages ONLY."
119 (defcustom nndiary-split-methods '(("diary" ""))
120 "*NNDiary specific split methods.
121 This variable is used by nndiary in place of the standard
122 `nnmail-split-methods' variable when `nndiary-get-new-mail' is set to
126 :type '(choice (repeat :tag "Alist" (group (string :tag "Name") regexp))
127 (function-item nnmail-split-fancy)
128 (function :tag "Other")))
131 (defcustom nndiary-reminders '((0 . day))
132 "*Different times when you want to be reminded of your appointments.
133 Diary articles will appear again, as if they'd been just received.
135 Entries look like (3 . day) which means something like \"Please
136 Hortense, would you be so kind as to remind me of my appointments 3 days
137 before the date, thank you very much. Anda, hmmm... by the way, are you
138 doing anything special tonight ?\".
140 The units of measure are 'minute 'hour 'day 'week 'month and 'year (no,
141 not 'century, sorry).
143 NOTE: the units of measure actually express dates, not durations: if you
144 use 'week, messages will pop up on Sundays at 00:00 (or Mondays if
145 `nndiary-week-starts-on-monday' is non-nil) and *not* 7 days before the
146 appointment, if you use 'month, messages will pop up on the first day of
147 each months, at 00:00 and so on.
149 If you really want to specify a duration (like 24 hours exactly), you can
150 use the equivalent in minutes (the smallest unit). A fuzz of 60 seconds
151 maximum in the reminder is not that painful, I think. Although this
152 scheme might appear somewhat weird at a first glance, it is very powerful.
153 In order to make this clear, here are some examples:
155 - '(0 . day): this is the default value of `nndiary-reminders'. It means
156 pop up the appointments of the day each morning at 00:00.
158 - '(1 . day): this means pop up the appointments the day before, at 00:00.
160 - '(6 . hour): for an appointment at 18:30, this would pop up the
161 appointment message at 12:00.
163 - '(360 . minute): for an appointment at 18:30 and 15 seconds, this would
164 pop up the appointment message at 12:30."
166 :type '(repeat (cons :format "%v\n"
167 (integer :format "%v")
168 (choice :format "%[%v(s)%] before...\n"
170 (const :format "%v" minute)
171 (const :format "%v" hour)
172 (const :format "%v" day)
173 (const :format "%v" week)
174 (const :format "%v" month)
175 (const :format "%v" year)))))
177 (defcustom nndiary-week-starts-on-monday nil
178 "*Whether a week starts on monday (otherwise, sunday)."
183 (defcustom nndiary-request-create-group-hooks nil
184 "*Hooks to run after `nndiary-request-create-group' is executed.
185 The hooks will be called with the full group name as argument."
189 (defcustom nndiary-request-update-info-hooks nil
190 "*Hooks to run after `nndiary-request-update-info-group' is executed.
191 The hooks will be called with the full group name as argument."
195 (defcustom nndiary-request-accept-article-hooks nil
196 "*Hooks to run before accepting an article.
197 Executed near the beginning of `nndiary-request-accept-article'.
198 The hooks will be called with the article in the current buffer."
202 (defcustom nndiary-check-directory-twice t
203 "*If t, check directories twice to avoid NFS failures."
208 ;; Back End declaration ======================================================
210 ;; Well, most of this is nnml clonage.
212 (nnoo-declare nndiary)
214 (defvoo nndiary-directory (nnheader-concat gnus-directory "diary/")
215 "Spool directory for the nndiary back end.")
217 (defvoo nndiary-active-file
218 (expand-file-name "active" nndiary-directory)
219 "Active file for the nndiary back end.")
221 (defvoo nndiary-newsgroups-file
222 (expand-file-name "newsgroups" nndiary-directory)
223 "Newsgroups description file for the nndiary back end.")
225 (defvoo nndiary-get-new-mail nil
226 "Whether nndiary gets new mail and split it.
227 Contrary to traditional mail back ends, this variable can be set to t
228 even if your primary mail back end also retreives mail. In such a case,
229 NDiary uses its own mail-sources and split-methods.")
231 (defvoo nndiary-nov-is-evil nil
232 "If non-nil, Gnus will never use nov databases for nndiary groups.
233 Using nov databases will speed up header fetching considerably.
234 This variable shouldn't be flipped much. If you have, for some reason,
235 set this to t, and want to set it to nil again, you should always run
236 the `nndiary-generate-nov-databases' command. The function will go
237 through all nnml directories and generate nov databases for them
238 all. This may very well take some time.")
240 (defvoo nndiary-prepare-save-mail-hook nil
241 "*Hook run narrowed to an article before saving.")
243 (defvoo nndiary-inhibit-expiry nil
244 "If non-nil, inhibit expiry.")
248 (defconst nndiary-version "0.2-b14"
249 "Current Diary back end version.")
251 (defun nndiary-version ()
252 "Current Diary back end version."
254 (message "NNDiary version %s" nndiary-version))
256 (defvoo nndiary-nov-file-name ".overview")
258 (defvoo nndiary-current-directory nil)
259 (defvoo nndiary-current-group nil)
260 (defvoo nndiary-status-string "" )
261 (defvoo nndiary-nov-buffer-alist nil)
262 (defvoo nndiary-group-alist nil)
263 (defvoo nndiary-active-timestamp nil)
264 (defvoo nndiary-article-file-alist nil)
266 (defvoo nndiary-generate-active-function 'nndiary-generate-active-info)
267 (defvoo nndiary-nov-buffer-file-name nil)
268 (defvoo nndiary-file-coding-system nnmail-file-coding-system)
270 (defconst nndiary-headers
277 ("Time-Zone" (("Y" -43200)
354 ;; List of NNDiary headers that specify the time spec. Each header name is
355 ;; followed by either two integers (specifying a range of possible values
356 ;; for this header) or one list (specifying all the possible values for this
357 ;; header). In the latter case, the list does NOT include the unspecifyed
359 ;; For time zone values, we have symbolic time zone names associated with
360 ;; the (relative) number of seconds ahead GMT.
363 (defsubst nndiary-schedule ()
368 (setq head (nth 0 elt))
369 (nndiary-parse-schedule (nth 0 elt) (nth 1 elt) (nth 2 elt)))
372 (nnheader-report 'nndiary "X-Diary-%s header parse error: %s."
377 ;;; Interface functions =====================================================
379 (nnoo-define-basics nndiary)
381 (deffoo nndiary-retrieve-headers (sequence &optional group server fetch-old)
382 (when (nndiary-possibly-change-directory group server)
384 (set-buffer nntp-server-buffer)
387 (number (length sequence))
389 (file-name-coding-system nnmail-pathname-coding-system)
391 (nndiary-check-directory-twice
392 (and nndiary-check-directory-twice
393 ;; To speed up, disable it in some case.
394 (or (not (numberp nnmail-large-newsgroup))
395 (<= number nnmail-large-newsgroup)))))
396 (if (stringp (car sequence))
398 (if (nndiary-retrieve-headers-with-nov sequence fetch-old)
401 (setq article (car sequence))
402 (setq file (nndiary-article-to-file article))
405 (not (file-directory-p file)))
406 (insert (format "221 %d Article retrieved.\n" article))
408 (nnheader-insert-head file)
410 (if (search-forward "\n\n" nil t)
412 (goto-char (point-max))
415 (delete-region (point) (point-max)))
416 (setq sequence (cdr sequence))
417 (setq count (1+ count))
418 (and (numberp nnmail-large-newsgroup)
419 (> number nnmail-large-newsgroup)
421 (nnheader-message 6 "nndiary: Receiving headers... %d%%"
422 (/ (* count 100) number))))
424 (and (numberp nnmail-large-newsgroup)
425 (> number nnmail-large-newsgroup)
426 (nnheader-message 6 "nndiary: Receiving headers...done"))
428 (nnheader-fold-continuation-lines)
431 (deffoo nndiary-open-server (server &optional defs)
432 (nnoo-change-server 'nndiary server defs)
433 (when (not (file-exists-p nndiary-directory))
434 (ignore-errors (make-directory nndiary-directory t)))
436 ((not (file-exists-p nndiary-directory))
437 (nndiary-close-server)
438 (nnheader-report 'nndiary "Couldn't create directory: %s"
440 ((not (file-directory-p (file-truename nndiary-directory)))
441 (nndiary-close-server)
442 (nnheader-report 'nndiary "Not a directory: %s" nndiary-directory))
444 (nnheader-report 'nndiary "Opened server %s using directory %s"
445 server nndiary-directory)
448 (deffoo nndiary-request-regenerate (server)
449 (nndiary-possibly-change-directory nil server)
450 (nndiary-generate-nov-databases server)
453 (deffoo nndiary-request-article (id &optional group server buffer)
454 (nndiary-possibly-change-directory group server)
455 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
456 (file-name-coding-system nnmail-pathname-coding-system)
457 path gpath group-num)
459 (when (and (setq group-num (nndiary-find-group-number id))
461 (assq (cdr group-num)
462 (nnheader-article-to-file-alist
464 (nnmail-group-pathname
466 nndiary-directory))))))
467 (setq path (concat gpath (int-to-string (cdr group-num)))))
468 (setq path (nndiary-article-to-file id)))
471 (nnheader-report 'nndiary "No such article: %s" id))
472 ((not (file-exists-p path))
473 (nnheader-report 'nndiary "No such file: %s" path))
474 ((file-directory-p path)
475 (nnheader-report 'nndiary "File is a directory: %s" path))
476 ((not (save-excursion (let ((nnmail-file-coding-system
477 nndiary-file-coding-system))
478 (nnmail-find-file path))))
479 (nnheader-report 'nndiary "Couldn't read file: %s" path))
481 (nnheader-report 'nndiary "Article %s retrieved" id)
482 ;; We return the article number.
483 (cons (if group-num (car group-num) group)
484 (string-to-number (file-name-nondirectory path)))))))
486 (deffoo nndiary-request-group (group &optional server dont-check)
487 (let ((file-name-coding-system nnmail-pathname-coding-system))
489 ((not (nndiary-possibly-change-directory group server))
490 (nnheader-report 'nndiary "Invalid group (no such directory)"))
491 ((not (file-exists-p nndiary-current-directory))
492 (nnheader-report 'nndiary "Directory %s does not exist"
493 nndiary-current-directory))
494 ((not (file-directory-p nndiary-current-directory))
495 (nnheader-report 'nndiary "%s is not a directory"
496 nndiary-current-directory))
498 (nnheader-report 'nndiary "Group %s selected" group)
501 (nnheader-re-read-dir nndiary-current-directory)
502 (nnmail-activate 'nndiary)
503 (let ((active (nth 1 (assoc group nndiary-group-alist))))
505 (nnheader-report 'nndiary "No such group: %s" group)
506 (nnheader-report 'nndiary "Selected group %s" group)
507 (nnheader-insert "211 %d %d %d %s\n"
508 (max (1+ (- (cdr active) (car active))) 0)
509 (car active) (cdr active) group)))))))
511 (deffoo nndiary-request-scan (&optional group server)
512 ;; Use our own mail sources and split methods while Gnus doesn't let us have
513 ;; multiple back ends for retrieving mail.
514 (let ((mail-sources nndiary-mail-sources)
515 (nnmail-split-methods nndiary-split-methods))
516 (setq nndiary-article-file-alist nil)
517 (nndiary-possibly-change-directory group server)
518 (nnmail-get-new-mail 'nndiary 'nndiary-save-nov nndiary-directory group)))
520 (deffoo nndiary-close-group (group &optional server)
521 (setq nndiary-article-file-alist nil)
524 (deffoo nndiary-request-create-group (group &optional server args)
525 (nndiary-possibly-change-directory nil server)
526 (nnmail-activate 'nndiary)
528 ((assoc group nndiary-group-alist)
530 ((and (file-exists-p (nnmail-group-pathname group nndiary-directory))
531 (not (file-directory-p (nnmail-group-pathname
532 group nndiary-directory))))
533 (nnheader-report 'nndiary "%s is a file"
534 (nnmail-group-pathname group nndiary-directory)))
537 (push (list group (setq active (cons 1 0)))
539 (nndiary-possibly-create-directory group)
540 (nndiary-possibly-change-directory group server)
541 (let ((articles (nnheader-directory-articles nndiary-current-directory)))
543 (setcar active (apply 'min articles))
544 (setcdr active (apply 'max articles))))
545 (nnmail-save-active nndiary-group-alist nndiary-active-file)
546 (run-hook-with-args 'nndiary-request-create-group-hooks
547 (gnus-group-prefixed-name group
548 (list "nndiary" server)))
552 (deffoo nndiary-request-list (&optional server)
554 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
555 (file-name-coding-system nnmail-pathname-coding-system))
556 (nnmail-find-file nndiary-active-file))
557 (setq nndiary-group-alist (nnmail-get-active))
560 (deffoo nndiary-request-newgroups (date &optional server)
561 (nndiary-request-list server))
563 (deffoo nndiary-request-list-newsgroups (&optional server)
565 (nnmail-find-file nndiary-newsgroups-file)))
567 (deffoo nndiary-request-expire-articles (articles group &optional server force)
568 (nndiary-possibly-change-directory group server)
569 (let ((active-articles
570 (nnheader-directory-articles nndiary-current-directory))
572 (nnmail-activate 'nndiary)
573 ;; Articles not listed in active-articles are already gone,
574 ;; so don't try to expire them.
575 (setq articles (gnus-intersection articles active-articles))
577 (setq article (nndiary-article-to-file (setq number (pop articles))))
578 (if (and (nndiary-deletable-article-p group number)
579 ;; Don't use nnmail-expired-article-p. Our notion of expiration
580 ;; is a bit peculiar ...
581 (or force (nndiary-expired-article-p article)))
583 ;; Allow a special target group.
584 (unless (eq nnmail-expiry-target 'delete)
586 (nndiary-request-article number group server (current-buffer))
587 (let ((nndiary-current-directory nil))
588 (nnmail-expiry-target-group nnmail-expiry-target group)))
589 (nndiary-possibly-change-directory group server))
590 (nnheader-message 5 "Deleting article %s in %s" number group)
592 (funcall nnmail-delete-file-function article)
593 (file-error (push number rest)))
594 (setq active-articles (delq number active-articles))
595 (nndiary-nov-delete-article group number))
597 (let ((active (nth 1 (assoc group nndiary-group-alist))))
599 (setcar active (or (and active-articles
600 (apply 'min active-articles))
602 (nnmail-save-active nndiary-group-alist nndiary-active-file))
604 (nconc rest articles)))
606 (deffoo nndiary-request-move-article
607 (article group server accept-form &optional last move-is-internal)
608 (let ((buf (get-buffer-create " *nndiary move*"))
610 (nndiary-possibly-change-directory group server)
611 (nndiary-update-file-alist)
613 (nndiary-deletable-article-p group article)
614 (nndiary-request-article article group server)
615 (let (nndiary-current-directory
616 nndiary-current-group
617 nndiary-article-file-alist)
620 (insert-buffer-substring nntp-server-buffer)
621 (setq result (eval accept-form))
622 (kill-buffer (current-buffer))
625 (nndiary-possibly-change-directory group server)
627 (funcall nnmail-delete-file-function
628 (nndiary-article-to-file article))
630 (nndiary-nov-delete-article group article)
633 (nnmail-save-active nndiary-group-alist nndiary-active-file))))
636 (deffoo nndiary-request-accept-article (group &optional server last)
637 (nndiary-possibly-change-directory group server)
638 (nnmail-check-syntax)
639 (run-hooks 'nndiary-request-accept-article-hooks)
640 (when (nndiary-schedule)
642 (when nnmail-cache-accepted-message-ids
643 (nnmail-cache-insert (nnmail-fetch-field "message-id")
645 (nnmail-fetch-field "subject")))
648 (nnmail-activate 'nndiary)
650 (car (nndiary-save-mail
651 (list (cons group (nndiary-active-number group))))))
653 (nnmail-save-active nndiary-group-alist nndiary-active-file)
654 (and last (nndiary-save-nov))))
656 (nnmail-activate 'nndiary)
657 (if (and (not (setq result
658 (nnmail-article-group 'nndiary-active-number)))
659 (yes-or-no-p "Moved to `junk' group; delete article? "))
661 (setq result (car (nndiary-save-mail result))))
663 (nnmail-save-active nndiary-group-alist nndiary-active-file)
664 (when nnmail-cache-accepted-message-ids
665 (nnmail-cache-close))
666 (nndiary-save-nov))))
670 (deffoo nndiary-request-post (&optional server)
671 (nnmail-do-request-post 'nndiary-request-accept-article server))
673 (deffoo nndiary-request-replace-article (article group buffer)
674 (nndiary-possibly-change-directory group)
677 (nndiary-possibly-create-directory group)
678 (let ((chars (nnmail-insert-lines))
679 (art (concat (int-to-string article) "\t"))
683 (point-min) (point-max)
684 (or (nndiary-article-to-file article)
685 (expand-file-name (int-to-string article)
686 nndiary-current-directory))
687 nil (if (nnheader-be-verbose 5) nil 'nomesg))
689 (setq headers (nndiary-parse-head chars article))
690 ;; Replace the NOV line in the NOV file.
692 (set-buffer (nndiary-open-nov group))
693 (goto-char (point-min))
694 (if (or (looking-at art)
695 (search-forward (concat "\n" art) nil t))
696 ;; Delete the old NOV line.
697 (delete-region (progn (beginning-of-line) (point))
698 (progn (forward-line 1) (point)))
699 ;; The line isn't here, so we have to find out where
700 ;; we should insert it. (This situation should never
701 ;; occur, but one likes to make sure...)
702 (while (and (looking-at "[0-9]+\t")
705 (match-beginning 0) (match-end 0)))
707 (zerop (forward-line 1)))))
709 (nnheader-insert-nov headers)
713 (deffoo nndiary-request-delete-group (group &optional force server)
714 (nndiary-possibly-change-directory group server)
716 ;; Delete all articles in GROUP.
719 nndiary-current-directory t
720 (concat nnheader-numerical-short-files
721 "\\|" (regexp-quote nndiary-nov-file-name) "$")))