lpath.el: Fbind current-idle-time for XEmacs 21.4 and SXEmacs 22.1.
[gnus] / lisp / nndiary.el
1 ;;; nndiary.el --- A diary back end for Gnus
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;;   2008, 2009, 2010  Free Software Foundation, Inc.
5
6 ;; Author:        Didier Verna <didier@xemacs.org>
7 ;; Maintainer:    Didier Verna <didier@xemacs.org>
8 ;; Created:       Fri Jul 16 18:55:42 1999
9 ;; Keywords:      calendar mail news
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
25
26
27 ;;; Commentary:
28
29 ;; Contents management by FCM version 0.1.
30
31 ;; Description:
32 ;; ===========
33
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.
36
37
38 ;; Bugs / Todo:
39 ;; ===========
40
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.
52
53
54 ;; Remarks:
55 ;; =======
56
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.
66
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 occurrences of
71 ;;   <nnfrom> (even in strings) with <nnto>, and you're done.
72
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
79 ;;   though.
80
81
82 ;;; Code:
83
84 (require 'nnoo)
85 (require 'nnheader)
86 (require 'nnmail)
87 (eval-when-compile (require 'cl))
88
89 (require 'gnus-start)
90 (require 'gnus-sum)
91
92 ;; Compatibility Functions  =================================================
93
94 (eval-and-compile
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))))
100
101
102 ;; Back End behavior customization ===========================================
103
104 (defgroup nndiary nil
105   "The Gnus Diary back end."
106   :version "22.1"
107   :group 'gnus-diary)
108
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."
115   :group 'nndiary
116   :group 'mail-source
117   :type 'sexp)
118
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
123 non-nil."
124   :group 'nndiary
125   :group 'nnmail-split
126   :type '(choice (repeat :tag "Alist" (group (string :tag "Name") regexp))
127                  (function-item nnmail-split-fancy)
128                  (function :tag "Other")))
129
130
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.
134
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 ?\".
139
140 The units of measure are 'minute 'hour 'day 'week 'month and 'year (no,
141 not 'century, sorry).
142
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.
148
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:
154
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.
157
158 - '(1 . day): this means pop up the appointments the day before, at 00:00.
159
160 - '(6 . hour): for an appointment at 18:30, this would pop up the
161   appointment message at 12:00.
162
163 - '(360 . minute): for an appointment at 18:30 and 15 seconds, this would
164   pop up the appointment message at 12:30."
165   :group 'nndiary
166   :type '(repeat (cons :format "%v\n"
167                        (integer :format "%v")
168                        (choice :format "%[%v(s)%] before...\n"
169                                :value day
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)))))
176
177 (defcustom nndiary-week-starts-on-monday nil
178   "*Whether a week starts on monday (otherwise, sunday)."
179   :type 'boolean
180   :group 'nndiary)
181
182
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."
186   :group 'nndiary
187   :type 'hook)
188
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."
192   :group 'nndiary
193   :type 'hook)
194
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."
199   :group 'nndiary
200   :type 'hook)
201
202 (defcustom nndiary-check-directory-twice t
203   "*If t, check directories twice to avoid NFS failures."
204   :group 'nndiary
205   :type 'boolean)
206
207
208 ;; Back End declaration ======================================================
209
210 ;; Well, most of this is nnml clonage.
211
212 (nnoo-declare nndiary)
213
214 (defvoo nndiary-directory (nnheader-concat gnus-directory "diary/")
215   "Spool directory for the nndiary back end.")
216
217 (defvoo nndiary-active-file
218     (expand-file-name "active" nndiary-directory)
219   "Active file for the nndiary back end.")
220
221 (defvoo nndiary-newsgroups-file
222     (expand-file-name "newsgroups" nndiary-directory)
223   "Newsgroups description file for the nndiary back end.")
224
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.")
230
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.")
239
240 (defvoo nndiary-prepare-save-mail-hook nil
241   "*Hook run narrowed to an article before saving.")
242
243 (defvoo nndiary-inhibit-expiry nil
244   "If non-nil, inhibit expiry.")
245
246 \f
247
248 (defconst nndiary-version "0.2-b14"
249   "Current Diary back end version.")
250
251 (defun nndiary-version ()
252   "Current Diary back end version."
253   (interactive)
254   (message "NNDiary version %s" nndiary-version))
255
256 (defvoo nndiary-nov-file-name ".overview")
257
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)
265
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)
269
270 (defconst nndiary-headers
271   '(("Minute" 0 59)
272     ("Hour" 0 23)
273     ("Dom" 1 31)
274     ("Month" 1 12)
275     ("Year" 1971)
276     ("Dow" 0 6)
277     ("Time-Zone" (("Y" -43200)
278
279                   ("X" -39600)
280
281                   ("W" -36000)
282
283                   ("V" -32400)
284
285                   ("U" -28800)
286                   ("PST" -28800)
287
288                   ("T"   -25200)
289                   ("MST" -25200)
290                   ("PDT" -25200)
291
292                   ("S"   -21600)
293                   ("CST" -21600)
294                   ("MDT" -21600)
295
296                   ("R"   -18000)
297                   ("EST" -18000)
298                   ("CDT" -18000)
299
300                   ("Q"   -14400)
301                   ("AST" -14400)
302                   ("EDT" -14400)
303
304                   ("P"   -10800)
305                   ("ADT" -10800)
306
307                   ("O" -7200)
308
309                   ("N" -3600)
310
311                   ("Z"   0)
312                   ("GMT" 0)
313                   ("UT"  0)
314                   ("UTC" 0)
315                   ("WET" 0)
316
317                   ("A"    3600)
318                   ("CET"  3600)
319                   ("MET"  3600)
320                   ("MEZ"  3600)
321                   ("BST"  3600)
322                   ("WEST" 3600)
323
324                   ("B"    7200)
325                   ("EET"  7200)
326                   ("CEST" 7200)
327                   ("MEST" 7200)
328                   ("MESZ" 7200)
329
330                   ("C" 10800)
331
332                   ("D" 14400)
333
334                   ("E" 18000)
335
336                   ("F" 21600)
337
338                   ("G" 25200)
339
340                   ("H" 28800)
341
342                   ("I"   32400)
343                   ("JST" 32400)
344
345                   ("K"   36000)
346                   ("GST" 36000)
347
348                   ("L" 39600)
349
350                   ("M"    43200)
351                   ("NZST" 43200)
352
353                   ("NZDT" 46800))))
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
358   ;; spec (*).
359   ;; For time zone values, we have symbolic time zone names associated with
360   ;; the (relative) number of seconds ahead GMT.
361   )
362
363 (defsubst nndiary-schedule ()
364   (let (head)
365     (condition-case arg
366         (mapcar
367          (lambda (elt)
368            (setq head (nth 0 elt))
369            (nndiary-parse-schedule (nth 0 elt) (nth 1 elt) (nth 2 elt)))
370          nndiary-headers)
371       (error
372        (nnheader-report 'nndiary "X-Diary-%s header parse error: %s."
373                         head (cdr arg))
374        nil))
375     ))
376
377 ;;; Interface functions =====================================================
378
379 (nnoo-define-basics nndiary)
380
381 (deffoo nndiary-retrieve-headers (sequence &optional group server fetch-old)
382   (when (nndiary-possibly-change-directory group server)
383     (with-current-buffer nntp-server-buffer
384       (erase-buffer)
385       (let* ((file nil)
386              (number (length sequence))
387              (count 0)
388              (file-name-coding-system nnmail-pathname-coding-system)
389              beg article
390              (nndiary-check-directory-twice
391               (and nndiary-check-directory-twice
392                    ;; To speed up, disable it in some case.
393                    (or (not (numberp nnmail-large-newsgroup))
394                        (<= number nnmail-large-newsgroup)))))
395         (if (stringp (car sequence))
396             'headers
397           (if (nndiary-retrieve-headers-with-nov sequence fetch-old)
398               'nov
399             (while sequence
400               (setq article (car sequence))
401               (setq file (nndiary-article-to-file article))
402               (when (and file
403                          (file-exists-p file)
404                          (not (file-directory-p file)))
405                 (insert (format "221 %d Article retrieved.\n" article))
406                 (setq beg (point))
407                 (nnheader-insert-head file)
408                 (goto-char beg)
409                 (if (search-forward "\n\n" nil t)
410                     (forward-char -1)
411                   (goto-char (point-max))
412                   (insert "\n\n"))
413                 (insert ".\n")
414                 (delete-region (point) (point-max)))
415               (setq sequence (cdr sequence))
416               (setq count (1+ count))
417               (and (numberp nnmail-large-newsgroup)
418                    (> number nnmail-large-newsgroup)
419                    (zerop (% count 20))
420                    (nnheader-message 6 "nndiary: Receiving headers... %d%%"
421                                      (/ (* count 100) number))))
422
423             (and (numberp nnmail-large-newsgroup)
424                  (> number nnmail-large-newsgroup)
425                  (nnheader-message 6 "nndiary: Receiving headers...done"))
426
427             (nnheader-fold-continuation-lines)
428             'headers))))))
429
430 (deffoo nndiary-open-server (server &optional defs)
431   (nnoo-change-server 'nndiary server defs)
432   (when (not (file-exists-p nndiary-directory))
433     (ignore-errors (make-directory nndiary-directory t)))
434   (cond
435    ((not (file-exists-p nndiary-directory))
436     (nndiary-close-server)
437     (nnheader-report 'nndiary "Couldn't create directory: %s"
438                      nndiary-directory))
439    ((not (file-directory-p (file-truename nndiary-directory)))
440     (nndiary-close-server)
441     (nnheader-report 'nndiary "Not a directory: %s" nndiary-directory))
442    (t
443     (nnheader-report 'nndiary "Opened server %s using directory %s"
444                      server nndiary-directory)
445     t)))
446
447 (deffoo nndiary-request-regenerate (server)
448   (nndiary-possibly-change-directory nil server)
449   (nndiary-generate-nov-databases server)
450   t)
451
452 (deffoo nndiary-request-article (id &optional group server buffer)
453   (nndiary-possibly-change-directory group server)
454   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
455          (file-name-coding-system nnmail-pathname-coding-system)
456          path gpath group-num)
457     (if (stringp id)
458         (when (and (setq group-num (nndiary-find-group-number id))
459                    (cdr
460                     (assq (cdr group-num)
461                           (nnheader-article-to-file-alist
462                            (setq gpath
463                                  (nnmail-group-pathname
464                                   (car group-num)
465                                   nndiary-directory))))))
466           (setq path (concat gpath (int-to-string (cdr group-num)))))
467       (setq path (nndiary-article-to-file id)))
468     (cond
469      ((not path)
470    &nb