nndiary.el 55 KB

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