nndiary.el 54 KB

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