nnmaildir.el 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. ;;; nnmaildir.el --- maildir backend for Gnus
  2. ;; This file is in the public domain.
  3. ;; Author: Paul Jarc <prj@po.cwru.edu>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; Maildir format is documented at <URL:http://cr.yp.to/proto/maildir.html>
  17. ;; and in the maildir(5) man page from qmail (available at
  18. ;; <URL:http://www.qmail.org/man/man5/maildir.html>). nnmaildir also stores
  19. ;; extra information in the .nnmaildir/ directory within a maildir.
  20. ;;
  21. ;; Some goals of nnmaildir:
  22. ;; * Everything Just Works, and correctly. E.g., NOV data is automatically
  23. ;; regenerated when stale; no need for manually running
  24. ;; *-generate-nov-databases.
  25. ;; * Perfect reliability: [C-g] will never corrupt its data in memory, and
  26. ;; SIGKILL will never corrupt its data in the filesystem.
  27. ;; * Allow concurrent operation as much as possible. If files change out
  28. ;; from under us, adapt to the changes or degrade gracefully.
  29. ;; * We use the filesystem as a database, so that, e.g., it's easy to
  30. ;; manipulate marks from outside Gnus.
  31. ;; * All information about a group is stored in the maildir, for easy backup,
  32. ;; copying, restoring, etc.
  33. ;;
  34. ;; Todo:
  35. ;; * When moving an article for expiry, copy all the marks except 'expire
  36. ;; from the original article.
  37. ;; * Add a hook for when moving messages from new/ to cur/, to support
  38. ;; nnmail's duplicate detection.
  39. ;; * Improve generated Xrefs, so crossposts are detectable.
  40. ;; * Improve code readability.
  41. ;;; Code:
  42. ;; eval this before editing
  43. [(progn
  44. (put 'nnmaildir--with-nntp-buffer 'lisp-indent-function 0)
  45. (put 'nnmaildir--with-work-buffer 'lisp-indent-function 0)
  46. (put 'nnmaildir--with-nov-buffer 'lisp-indent-function 0)
  47. (put 'nnmaildir--with-move-buffer 'lisp-indent-function 0)
  48. (put 'nnmaildir--condcase 'lisp-indent-function 2)
  49. )
  50. ]
  51. ;; For Emacs <22.2 and XEmacs.
  52. (eval-and-compile
  53. (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
  54. (eval-and-compile
  55. (require 'nnheader)
  56. (require 'gnus)
  57. (require 'gnus-util)
  58. (require 'gnus-range)
  59. (require 'gnus-start)
  60. (require 'gnus-int)
  61. (require 'message))
  62. (eval-when-compile
  63. (require 'cl)
  64. (require 'nnmail))
  65. (defconst nnmaildir-version "Gnus")
  66. (defvar nnmaildir-article-file-name nil
  67. "*The filename of the most recently requested article. This variable is set
  68. by nnmaildir-request-article.")
  69. ;; The filename of the article being moved/copied:
  70. (defvar nnmaildir--file nil)
  71. ;; Variables to generate filenames of messages being delivered:
  72. (defvar nnmaildir--delivery-time "")
  73. (defconst nnmaildir--delivery-pid (concat "P" (number-to-string (emacs-pid))))
  74. (defvar nnmaildir--delivery-count nil)
  75. ;; An obarry containing symbols whose names are server names and whose values
  76. ;; are servers:
  77. (defvar nnmaildir--servers (make-vector 3 0))
  78. ;; The current server:
  79. (defvar nnmaildir--cur-server nil)
  80. ;; A copy of nnmail-extra-headers
  81. (defvar nnmaildir--extra nil)
  82. ;; A NOV structure looks like this (must be prin1-able, so no defstruct):
  83. ["subject\tfrom\tdate"
  84. "references\tchars\lines"
  85. "To: you\tIn-Reply-To: <your.mess@ge>"
  86. (12345 67890) ;; modtime of the corresponding article file
  87. (to in-reply-to)] ;; contemporary value of nnmail-extra-headers
  88. (defconst nnmaildir--novlen 5)
  89. (defmacro nnmaildir--nov-new (beg mid end mtime extra)
  90. `(vector ,beg ,mid ,end ,mtime ,extra))
  91. (defmacro nnmaildir--nov-get-beg (nov) `(aref ,nov 0))
  92. (defmacro nnmaildir--nov-get-mid (nov) `(aref ,nov 1))
  93. (defmacro nnmaildir--nov-get-end (nov) `(aref ,nov 2))
  94. (defmacro nnmaildir--nov-get-mtime (nov) `(aref ,nov 3))
  95. (defmacro nnmaildir--nov-get-extra (nov) `(aref ,nov 4))
  96. (defmacro nnmaildir--nov-set-beg (nov value) `(aset ,nov 0 ,value))
  97. (defmacro nnmaildir--nov-set-mid (nov value) `(aset ,nov 1 ,value))
  98. (defmacro nnmaildir--nov-set-end (nov value) `(aset ,nov 2 ,value))
  99. (defmacro nnmaildir--nov-set-mtime (nov value) `(aset ,nov 3 ,value))
  100. (defmacro nnmaildir--nov-set-extra (nov value) `(aset ,nov 4 ,value))
  101. (defstruct nnmaildir--art
  102. (prefix nil :type string) ;; "time.pid.host"
  103. (suffix nil :type string) ;; ":2,flags"
  104. (num nil :type natnum) ;; article number
  105. (msgid nil :type string) ;; "<mess.age@id>"
  106. (nov nil :type vector)) ;; cached nov structure, or nil
  107. (defstruct nnmaildir--grp
  108. (name nil :type string) ;; "group.name"
  109. (new nil :type list) ;; new/ modtime
  110. (cur nil :type list) ;; cur/ modtime
  111. (min 1 :type natnum) ;; minimum article number
  112. (count 0 :type natnum) ;; count of articles
  113. (nlist nil :type list) ;; list of articles, ordered descending by number
  114. (flist nil :type vector) ;; obarray mapping filename prefix->article
  115. (mlist nil :type vector) ;; obarray mapping message-id->article
  116. (cache nil :type vector) ;; nov cache
  117. (index nil :type natnum) ;; index of next cache entry to replace
  118. (mmth nil :type vector)) ;; obarray mapping mark name->dir modtime
  119. ; ("Mark Mod Time Hash")
  120. (defstruct nnmaildir--srv
  121. (address nil :type string) ;; server address string
  122. (method nil :type list) ;; (nnmaildir "address" ...)
  123. (prefix nil :type string) ;; "nnmaildir+address:"
  124. (dir nil :type string) ;; "/expanded/path/to/server/dir/"
  125. (ls nil :type function) ;; directory-files function
  126. (groups nil :type vector) ;; obarray mapping group name->group
  127. (curgrp nil :type nnmaildir--grp) ;; current group, or nil
  128. (error nil :type string) ;; last error message, or nil
  129. (mtime nil :type list) ;; modtime of dir
  130. (gnm nil) ;; flag: split from mail-sources?
  131. (target-prefix nil :type string)) ;; symlink target prefix
  132. (defun nnmaildir--expired-article (group article)
  133. (setf (nnmaildir--art-nov article) nil)
  134. (let ((flist (nnmaildir--grp-flist group))
  135. (mlist (nnmaildir--grp-mlist group))
  136. (min (nnmaildir--grp-min group))
  137. (count (1- (nnmaildir--grp-count group)))
  138. (prefix (nnmaildir--art-prefix article))
  139. (msgid (nnmaildir--art-msgid article))
  140. (new-nlist nil)
  141. (nlist-pre '(nil . nil))
  142. nlist-post num)
  143. (unless (zerop count)
  144. (setq nlist-post (nnmaildir--grp-nlist group)
  145. num (nnmaildir--art-num article))
  146. (if (eq num (caar nlist-post))
  147. (setq new-nlist (cdr nlist-post))
  148. (setq new-nlist nlist-post
  149. nlist-pre nlist-post
  150. nlist-post (cdr nlist-post))
  151. (while (/= num (caar nlist-post))
  152. (setq nlist-pre nlist-post
  153. nlist-post (cdr nlist-post)))
  154. (setq nlist-post (cdr nlist-post))
  155. (if (eq num min)
  156. (setq min (caar nlist-pre)))))
  157. (let ((inhibit-quit t))
  158. (setf (nnmaildir--grp-min group) min)
  159. (setf (nnmaildir--grp-count group) count)
  160. (setf (nnmaildir--grp-nlist group) new-nlist)
  161. (setcdr nlist-pre nlist-post)
  162. (unintern prefix flist)
  163. (unintern msgid mlist))))
  164. (defun nnmaildir--nlist-art (group num)
  165. (let ((entry (assq num (nnmaildir--grp-nlist group))))
  166. (if entry
  167. (cdr entry))))
  168. (defmacro nnmaildir--flist-art (list file)
  169. `(symbol-value (intern-soft ,file ,list)))
  170. (defmacro nnmaildir--mlist-art (list msgid)
  171. `(symbol-value (intern-soft ,msgid ,list)))
  172. (defun nnmaildir--pgname (server gname)
  173. (let ((prefix (nnmaildir--srv-prefix server)))
  174. (if prefix (concat prefix gname)
  175. (setq gname (gnus-group-prefixed-name gname
  176. (nnmaildir--srv-method server)))
  177. (setf (nnmaildir--srv-prefix server) (gnus-group-real-prefix gname))
  178. gname)))
  179. (defun nnmaildir--param (pgname param)
  180. (setq param (gnus-group-find-parameter pgname param 'allow-list))
  181. (if (vectorp param) (setq param (aref param 0)))
  182. (eval param))
  183. (defmacro nnmaildir--with-nntp-buffer (&rest body)
  184. `(with-current-buffer nntp-server-buffer
  185. ,@body))
  186. (defmacro nnmaildir--with-work-buffer (&rest body)
  187. `(with-current-buffer (get-buffer-create " *nnmaildir work*")
  188. ,@body))
  189. (defmacro nnmaildir--with-nov-buffer (&rest body)
  190. `(with-current-buffer (get-buffer-create " *nnmaildir nov*")
  191. ,@body))
  192. (defmacro nnmaildir--with-move-buffer (&rest body)
  193. `(with-current-buffer (get-buffer-create " *nnmaildir move*")
  194. ,@body))
  195. (defmacro nnmaildir--subdir (dir subdir)
  196. `(file-name-as-directory (concat ,dir ,subdir)))
  197. (defmacro nnmaildir--srvgrp-dir (srv-dir gname)
  198. `(nnmaildir--subdir ,srv-dir ,gname))
  199. (defmacro nnmaildir--tmp (dir) `(nnmaildir--subdir ,dir "tmp"))
  200. (defmacro nnmaildir--new (dir) `(nnmaildir--subdir ,dir "new"))
  201. (defmacro nnmaildir--cur (dir) `(nnmaildir--subdir ,dir "cur"))
  202. (defmacro nnmaildir--nndir (dir) `(nnmaildir--subdir ,dir ".nnmaildir"))
  203. (defmacro nnmaildir--nov-dir (dir) `(nnmaildir--subdir ,dir "nov"))
  204. (defmacro nnmaildir--marks-dir (dir) `(nnmaildir--subdir ,dir "marks"))
  205. (defmacro nnmaildir--num-dir (dir) `(nnmaildir--subdir ,dir "num"))
  206. (defmacro nnmaildir--unlink (file-arg)
  207. `(let ((file ,file-arg))
  208. (if (file-attributes file) (delete-file file))))
  209. (defun nnmaildir--mkdir (dir)
  210. (or (file-exists-p (file-name-as-directory dir))
  211. (make-directory-internal (directory-file-name dir))))
  212. (defun nnmaildir--mkfile (file)
  213. (write-region "" nil file nil 'no-message))
  214. (defun nnmaildir--delete-dir-files (dir ls)
  215. (when (file-attributes dir)
  216. (mapc 'delete-file (funcall ls dir 'full "\\`[^.]" 'nosort))
  217. (delete-directory dir)))
  218. (defun nnmaildir--group-maxnum (server group)
  219. (catch 'return
  220. (if (zerop (nnmaildir--grp-count group)) (throw 'return 0))
  221. (let ((dir (nnmaildir--srvgrp-dir (nnmaildir--srv-dir server)
  222. (nnmaildir--grp-name group)))
  223. (number-opened 1)
  224. attr ino-opened nlink number-linked)
  225. (setq dir (nnmaildir--nndir dir)
  226. dir (nnmaildir--num-dir dir))
  227. (while t
  228. (setq attr (file-attributes
  229. (concat dir (number-to-string number-opened))))
  230. (or attr (throw 'return (1- number-opened)))
  231. (setq ino-opened (nth 10 attr)
  232. nlink (nth 1 attr)
  233. number-linked (+ number-opened nlink))
  234. (if (or (< nlink 1) (< number-linked nlink))
  235. (signal 'error '("Arithmetic overflow")))
  236. (setq attr (file-attributes
  237. (concat dir (number-to-string number-linked))))
  238. (or attr (throw 'return (1- number-linked)))
  239. (unless (equal ino-opened (nth 10 attr))
  240. (setq number-opened number-linked))))))
  241. ;; Make the given server, if non-nil, be the current server. Then make the
  242. ;; given group, if non-nil, be the current group of the current server. Then
  243. ;; return the group object for the current group.
  244. (defun nnmaildir--prepare (server group)
  245. (let (x groups)
  246. (catch 'return
  247. (if (null server)
  248. (unless (setq server nnmaildir--cur-server)
  249. (throw 'return nil))
  250. (unless (setq server (intern-soft server nnmaildir--servers))
  251. (throw 'return nil))
  252. (setq server (symbol-value server)
  253. nnmaildir--cur-server server))
  254. (unless (setq groups (nnmaildir--srv-groups server))
  255. (throw 'return nil))
  256. (unless (nnmaildir--srv-method server)
  257. (setq x (concat "nnmaildir:" (nnmaildir--srv-address server))
  258. x (gnus-server-to-method x))
  259. (unless x (throw 'return nil))
  260. (setf (nnmaildir--srv-method server) x))
  261. (if (null group)
  262. (unless (setq group (nnmaildir--srv-curgrp server))
  263. (throw 'return nil))
  264. (unless (setq group (intern-soft group groups))
  265. (throw 'return nil))
  266. (setq group (symbol-value group)))
  267. group)))
  268. (defun nnmaildir--tab-to-space (string)
  269. (let ((pos 0))
  270. (while (string-match "\t" string pos)
  271. (aset string (match-beginning 0) ? )
  272. (setq pos (match-end 0))))
  273. string)
  274. (defmacro nnmaildir--condcase (errsym body &rest handler)
  275. `(condition-case ,errsym
  276. (let ((system-messages-locale "C")) ,body)
  277. (error . ,handler)))
  278. (defun nnmaildir--emlink-p (err)
  279. (and (eq (car err) 'file-error)
  280. (string= (downcase (caddr err)) "too many links")))
  281. (defun nnmaildir--enoent-p (err)
  282. (and (eq (car err) 'file-error)
  283. (string= (downcase (caddr err)) "no such file or directory")))
  284. (defun nnmaildir--eexist-p (err)
  285. (eq (car err) 'file-already-exists))
  286. (defun nnmaildir--new-number (nndir)
  287. "Allocate a new article number by atomically creating a file under NNDIR."
  288. (let ((numdir (nnmaildir--num-dir nndir))
  289. (make-new-file t)
  290. (number-open 1)
  291. number-link previous-number-link path-open path-link ino-open)
  292. (nnmaildir--mkdir numdir)
  293. (catch 'return
  294. (while t
  295. (setq path-open (concat numdir (number-to-string number-open)))
  296. (if (not make-new-file)
  297. (setq previous-number-link number-link)
  298. (nnmaildir--mkfile path-open)
  299. ;; If Emacs had O_CREAT|O_EXCL, we could return number-open here.
  300. (setq make-new-file nil
  301. previous-number-link 0))
  302. (let* ((attr (file-attributes path-open))
  303. (nlink (nth 1 attr)))
  304. (setq ino-open (nth 10 attr)
  305. number-link (+ number-open nlink))
  306. (if (or (< nlink 1) (< number-link nlink))
  307. (signal 'error '("Arithmetic overflow"))))
  308. (if (= number-link previous-number-link)
  309. ;; We've already tried this number, in the previous loop iteration,
  310. ;; and failed.
  311. (signal 'error `("Corrupt internal nnmaildir data" ,path-open)))
  312. (setq path-link (concat numdir (number-to-string number-link)))
  313. (nnmaildir--condcase err
  314. (progn
  315. (add-name-to-file path-open path-link)
  316. (throw 'return number-link))
  317. (cond
  318. ((nnmaildir--emlink-p err)
  319. (setq make-new-file t
  320. number-open number-link))
  321. ((nnmaildir--eexist-p err)
  322. (let ((attr (file-attributes path-link)))
  323. (unless (equal (nth 10 attr) ino-open)
  324. (setq number-open number-link
  325. number-link 0))))
  326. (t (signal (car err) (cdr err)))))))))
  327. (defun nnmaildir--update-nov (server group article)
  328. (let ((nnheader-file-coding-system 'binary)
  329. (srv-dir (nnmaildir--srv-dir server))
  330. (storage-version 1) ;; [version article-number msgid [...nov...]]
  331. dir gname pgname msgdir prefix suffix file attr mtime novdir novfile
  332. nov msgid nov-beg nov-mid nov-end field val old-extra num numdir
  333. deactivate-mark)
  334. (catch 'return
  335. (setq gname (nnmaildir--grp-name group)
  336. pgname (nnmaildir--pgname server gname)
  337. dir (nnmaildir--srvgrp-dir srv-dir gname)
  338. msgdir (if (nnmaildir--param pgname 'read-only)
  339. (nnmaildir--new dir) (nnmaildir--cur dir))
  340. prefix (nnmaildir--art-prefix article)
  341. suffix (nnmaildir--art-suffix article)
  342. file (concat msgdir prefix suffix)
  343. attr (file-attributes file))
  344. (unless attr
  345. (nnmaildir--expired-article group article)
  346. (throw 'return nil))
  347. (setq mtime (nth 5 attr)
  348. attr (nth 7 attr)
  349. nov (nnmaildir--art-nov article)
  350. dir (nnmaildir--nndir dir)
  351. novdir (nnmaildir--nov-dir dir)
  352. novfile (concat novdir prefix))
  353. (unless (equal nnmaildir--extra nnmail-extra-headers)
  354. (setq nnmaildir--extra (copy-sequence nnmail-extra-headers)))
  355. (nnmaildir--with-nov-buffer
  356. ;; First we'll check for already-parsed NOV data.
  357. (cond ((not (file-exists-p novfile))
  358. ;; The NOV file doesn't exist; we have to parse the message.
  359. (setq nov nil))
  360. ((not nov)
  361. ;; The file exists, but the data isn't in memory; read the file.
  362. (erase-buffer)
  363. (nnheader-insert-file-contents novfile)
  364. (setq nov (read (current-buffer)))
  365. (if (not (and (vectorp nov)
  366. (/= 0 (length nov))
  367. (equal storage-version (aref nov 0))))
  368. ;; This NOV data seems to be in the wrong format.
  369. (setq nov nil)
  370. (unless (nnmaildir--art-num article)
  371. (setf (nnmaildir--art-num article) (aref nov 1)))
  372. (unless (nnmaildir--art-msgid article)
  373. (setf (nnmaildir--art-msgid article) (aref nov 2)))
  374. (setq nov (aref nov 3)))))
  375. ;; Now check whether the already-parsed data (if we have any) is
  376. ;; usable: if the message has been edited or if nnmail-extra-headers
  377. ;; has been augmented since this data was parsed from the message,
  378. ;; then we have to reparse. Otherwise it's up-to-date.
  379. (when (and nov (equal mtime (nnmaildir--nov-get-mtime nov)))
  380. ;; The timestamp matches. Now check nnmail-extra-headers.
  381. (setq old-extra (nnmaildir--nov-get-extra nov))
  382. (when (equal nnmaildir--extra old-extra) ;; common case
  383. ;; Save memory; use a single copy of the list value.
  384. (nnmaildir--nov-set-extra nov nnmaildir--extra)
  385. (throw 'return nov))
  386. ;; They're not equal, but maybe the new is a subset of the old.
  387. (if (null nnmaildir--extra)
  388. ;; The empty set is a subset of every set.
  389. (throw 'return nov))
  390. (if (not (memq nil (mapcar (lambda (e) (memq e old-extra))
  391. nnmaildir--extra)))
  392. (throw 'return nov)))
  393. ;; Parse the NOV data out of the message.
  394. (erase-buffer)
  395. (nnheader-insert-file-contents file)
  396. (insert "\n")
  397. (goto-char (point-min))
  398. (save-restriction
  399. (if (search-forward "\n\n" nil 'noerror)
  400. (progn
  401. (setq nov-mid (count-lines (point) (point-max)))
  402. (narrow-to-region (point-min) (1- (point))))
  403. (setq nov-mid 0))
  404. (goto-char (point-min))
  405. (delete-char 1)
  406. (setq nov (nnheader-parse-naked-head)
  407. field (or (mail-header-lines nov) 0)))
  408. (unless (or (zerop field) (nnmaildir--param pgname 'distrust-Lines:))
  409. (setq nov-mid field))
  410. (setq nov-mid (number-to-string nov-mid)
  411. nov-mid (concat (number-to-string attr) "\t" nov-mid))
  412. (save-match-data
  413. (setq field (or (mail-header-references nov) ""))
  414. (nnmaildir--tab-to-space field)
  415. (setq nov-mid (concat field "\t" nov-mid)
  416. nov-beg (mapconcat
  417. (lambda (f) (nnmaildir--tab-to-space (or f "")))
  418. (list (mail-header-subject nov)
  419. (mail-header-from nov)
  420. (mail-header-date nov)) "\t")
  421. nov-end (mapconcat
  422. (lambda (extra)
  423. (setq field (symbol-name (car extra))
  424. val (cdr extra))
  425. (nnmaildir--tab-to-space field)
  426. (nnmaildir--tab-to-space val)
  427. (concat field ": " val))
  428. (mail-header-extra nov) "\t")))
  429. (setq msgid (mail-header-id nov))
  430. (if (or (null msgid) (nnheader-fake-message-id-p msgid))
  431. (setq msgid (concat "<" prefix "@nnmaildir>")))
  432. (nnmaildir--tab-to-space msgid)
  433. ;; The data is parsed; create an nnmaildir NOV structure.
  434. (setq nov (nnmaildir--nov-new nov-beg nov-mid nov-end mtime
  435. nnmaildir--extra)
  436. num (nnmaildir--art-num article))
  437. (unless num
  438. (setq num (nnmaildir--new-number dir))
  439. (setf (nnmaildir--art-num article) num))
  440. ;; Store this new NOV data in a file
  441. (erase-buffer)
  442. (prin1 (vector storage-version num msgid nov) (current-buffer))
  443. (setq file (concat novfile ":"))
  444. (nnmaildir--unlink file)
  445. (gmm-write-region (point-min) (point-max) file nil 'no-message nil
  446. 'excl))
  447. (rename-file file novfile 'replace)
  448. (setf (nnmaildir--art-msgid article) msgid)
  449. nov)))
  450. (defun nnmaildir--cache-nov (group article nov)
  451. (let ((cache (nnmaildir--grp-cache group))
  452. (index (nnmaildir--grp-index group))
  453. goner)
  454. (unless (nnmaildir--art-nov article)
  455. (setq goner (aref cache index))
  456. (if goner (setf (nnmaildir--art-nov goner) nil))
  457. (aset cache index article)
  458. (setf (nnmaildir--grp-index group) (% (1+ index) (length cache))))
  459. (setf (nnmaildir--art-nov article) nov)))
  460. (defun nnmaildir--grp-add-art (server group article)
  461. (let ((nov (nnmaildir--update-nov server group article))
  462. count num min nlist nlist-cdr insert-nlist)
  463. (when nov
  464. (setq count (1+ (nnmaildir--grp-count group))
  465. num (nnmaildir--art-num article)
  466. min (if (= count 1) num
  467. (min num (nnmaildir--grp-min group)))
  468. nlist (nnmaildir--grp-nlist group))
  469. (if (or (null nlist) (> num (caar nlist)))
  470. (setq nlist (cons (cons num article) nlist))
  471. (setq insert-nlist t
  472. nlist-cdr (cdr nlist))
  473. (while (and nlist-cdr (< num (caar nlist-cdr)))
  474. (setq nlist nlist-cdr
  475. nlist-cdr (cdr nlist))))
  476. (let ((inhibit-quit t))
  477. (setf (nnmaildir--grp-count group) count)
  478. (setf (nnmaildir--grp-min group) min)
  479. (if insert-nlist
  480. (setcdr nlist (cons (cons num article) nlist-cdr))
  481. (setf (nnmaildir--grp-nlist group) nlist))
  482. (set (intern (nnmaildir--art-prefix article)
  483. (nnmaildir--grp-flist group))
  484. article)
  485. (set (intern (nnmaildir--art-msgid article)
  486. (nnmaildir--grp-mlist group))
  487. article)
  488. (set (intern (nnmaildir--grp-name group)
  489. (nnmaildir--srv-groups server))
  490. group))
  491. (nnmaildir--cache-nov group article nov)
  492. t)))
  493. (defun nnmaildir--group-ls (server pgname)
  494. (or (nnmaildir--param pgname 'directory-files)
  495. (nnmaildir--srv-ls server)))
  496. (defun nnmaildir-article-number-to-file-name
  497. (number group-name server-address-string)
  498. (let ((group (nnmaildir--prepare server-address-string group-name))
  499. article dir pgname)
  500. (catch 'return
  501. (unless group
  502. ;; The given group or server does not exist.
  503. (throw 'return nil))
  504. (setq article (nnmaildir--nlist-art group number))
  505. (unless article
  506. ;; The given article number does not exist in this group.
  507. (throw 'return nil))
  508. (setq pgname (nnmaildir--pgname nnmaildir--cur-server group-name)
  509. dir (nnmaildir--srv-dir nnmaildir--cur-server)
  510. dir (nnmaildir--srvgrp-dir dir group-name)
  511. dir (if (nnmaildir--param pgname 'read-only)
  512. (nnmaildir--new dir) (nnmaildir--cur dir)))
  513. (concat dir (nnmaildir--art-prefix article)
  514. (nnmaildir--art-suffix article)))))
  515. (defun nnmaildir-article-number-to-base-name
  516. (number group-name server-address-string)
  517. (let ((x (nnmaildir--prepare server-address-string group-name)))
  518. (when x
  519. (setq x (nnmaildir--nlist-art x number))
  520. (and x (cons (nnmaildir--art-prefix x)
  521. (nnmaildir--art-suffix x))))))
  522. (defun nnmaildir-base-name-to-article-number
  523. (base-name group-name server-address-string)
  524. (let ((x (nnmaildir--prepare server-address-string group-name)))
  525. (when x
  526. (setq x (nnmaildir--grp-flist x)
  527. x (nnmaildir--flist-art x base-name))
  528. (and x (nnmaildir--art-num x)))))
  529. (defun nnmaildir--nlist-iterate (nlist ranges func)
  530. (let (entry high low nlist2)
  531. (if (eq ranges 'all)
  532. (setq ranges `((1 . ,(caar nlist)))))
  533. (while ranges
  534. (setq entry (car ranges) ranges (cdr ranges))
  535. (while (and ranges (eq entry (car ranges)))
  536. (setq ranges (cdr ranges))) ;; skip duplicates
  537. (if (numberp entry)
  538. (setq low entry
  539. high entry)
  540. (setq low (car entry)
  541. high (cdr entry)))
  542. (setq nlist2 nlist) ;; Don't assume any sorting of ranges
  543. (catch 'iterate-loop
  544. (while nlist2
  545. (if (<= (caar nlist2) high) (throw 'iterate-loop nil))
  546. (setq nlist2 (cdr nlist2))))
  547. (catch 'iterate-loop
  548. (while nlist2
  549. (setq entry (car nlist2) nlist2 (cdr nlist2))
  550. (if (< (car entry) low) (throw 'iterate-loop nil))
  551. (funcall func (cdr entry)))))))
  552. (defun nnmaildir--up2-1 (n)
  553. (if (zerop n) 1 (1- (lsh 1 (1+ (logb n))))))
  554. (defun nnmaildir--system-name ()
  555. (gnus-replace-in-string
  556. (gnus-replace-in-string
  557. (gnus-replace-in-string
  558. (system-name)
  559. "\\\\" "\\134" 'literal)
  560. "/" "\\057" 'literal)
  561. ":" "\\072" 'literal))
  562. (defun nnmaildir-request-type (group &optional article)
  563. 'mail)
  564. (defun nnmaildir-status-message (&optional server)
  565. (nnmaildir--prepare server nil)
  566. (nnmaildir--srv-error nnmaildir--cur-server))
  567. (defun nnmaildir-server-opened (&optional server)
  568. (and nnmaildir--cur-server
  569. (if server
  570. (string-equal server (nnmaildir--srv-address nnmaildir--cur-server))
  571. t)
  572. (nnmaildir--srv-groups nnmaildir--cur-server)
  573. t))
  574. (defun nnmaildir-open-server (server &optional defs)
  575. (let ((x server)
  576. dir size)
  577. (catch 'return
  578. (setq server (intern-soft x nnmaildir--servers))
  579. (if server
  580. (and (setq server (symbol-value server))
  581. (nnmaildir--srv-groups server)
  582. (setq nnmaildir--cur-server server)
  583. (throw 'return t))
  584. (setq server (make-nnmaildir--srv :address x))
  585. (let ((inhibit-quit t))
  586. (set (intern x nnmaildir--servers) server)))
  587. (setq dir (assq 'directory defs))
  588. (unless dir
  589. (setf (nnmaildir--srv-error server)
  590. "You must set \"directory\" in the select method")
  591. (throw 'return nil))
  592. (setq dir (cadr dir)
  593. dir (eval dir)
  594. dir (expand-file-name dir)
  595. dir (file-name-as-directory dir))
  596. (unless (file-exists-p dir)
  597. (setf (nnmaildir--srv-error server) (concat "No such directory: " dir))
  598. (throw 'return nil))
  599. (setf (nnmaildir--srv-dir server) dir)
  600. (setq x (assq 'directory-files defs))
  601. (if (null x)
  602. (setq x (if nnheader-directory-files-is-safe 'directory-files
  603. 'nnheader-directory-files-safe))
  604. (setq x (cadr x))
  605. (unless (functionp x)
  606. (setf (nnmaildir--srv-error server)
  607. (concat "Not a function: " (prin1-to-string x)))
  608. (throw 'return nil)))
  609. (setf (nnmaildir--srv-ls server) x)
  610. (setq size (length (funcall x dir nil "\\`[^.]" 'nosort))
  611. size (nnmaildir--up2-1 size))
  612. (and (setq x (assq 'get-new-mail defs))
  613. (setq x (cdr x))
  614. (car x)
  615. (setf (nnmaildir--srv-gnm server) t)
  616. (require 'nnmail))
  617. (setq x (assq 'target-prefix defs))
  618. (if x
  619. (progn
  620. (setq x (cadr x)
  621. x (eval x))
  622. (setf (nnmaildir--srv-target-prefix server) x))
  623. (setq x (assq 'create-directory defs))
  624. (if x
  625. (progn
  626. (setq x (cadr x)
  627. x (eval x)
  628. x (file-name-as-directory x))
  629. (setf (nnmaildir--srv-target-prefix server) x))
  630. (setf (nnmaildir--srv-target-prefix server) "")))
  631. (setf (nnmaildir--srv-groups server) (make-vector size 0))
  632. (setq nnmaildir--cur-server server)
  633. t)))
  634. (defun nnmaildir--parse-filename (file)
  635. (let ((prefix (car file))
  636. timestamp len)
  637. (if (string-match "\\`\\([0-9]+\\)\\(\\..*\\)\\'" prefix)
  638. (progn
  639. (setq timestamp (concat "0000" (match-string 1 prefix))
  640. len (- (length timestamp) 4))
  641. (vector (string-to-number (substring timestamp 0 len))
  642. (string-to-number (substring timestamp len))
  643. (match-string 2 prefix)
  644. file))
  645. file)))
  646. (defun nnmaildir--sort-files (a b)
  647. (catch 'return
  648. (if (consp a)
  649. (throw 'return (and (consp b) (string-lessp (car a) (car b)))))
  650. (if (consp b) (throw 'return t))
  651. (if (< (aref a 0) (aref b 0)) (throw 'return t))
  652. (if (> (aref a 0) (aref b 0)) (throw 'return nil))
  653. (if (< (aref a 1) (aref b 1)) (throw 'return t))
  654. (if (> (aref a 1) (aref b 1)) (throw 'return nil))
  655. (string-lessp (aref a 2) (aref b 2))))
  656. (defun nnmaildir--scan (gname scan-msgs groups method srv-dir srv-ls)
  657. (catch 'return
  658. (let ((36h-ago (- (car (current-time)) 2))
  659. absdir nndir tdir ndir cdir nattr cattr isnew pgname read-only ls
  660. files num dir flist group x)
  661. (setq absdir (nnmaildir--srvgrp-dir srv-dir gname)
  662. nndir (nnmaildir--nndir absdir))
  663. (unless (file-exists-p absdir)
  664. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  665. (concat "No such directory: " absdir))
  666. (throw 'return nil))
  667. (setq tdir (nnmaildir--tmp absdir)
  668. ndir (nnmaildir--new absdir)
  669. cdir (nnmaildir--cur absdir)
  670. nattr (file-attributes ndir)
  671. cattr (file-attributes cdir))
  672. (unless (and (file-exists-p tdir) nattr cattr)
  673. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  674. (concat "Not a maildir: " absdir))
  675. (throw 'return nil))
  676. (setq group (nnmaildir--prepare nil gname)
  677. pgname (nnmaildir--pgname nnmaildir--cur-server gname))
  678. (if group
  679. (setq isnew nil)
  680. (setq isnew t
  681. group (make-nnmaildir--grp :name gname :index 0))
  682. (nnmaildir--mkdir nndir)
  683. (nnmaildir--mkdir (nnmaildir--nov-dir nndir))
  684. (nnmaildir--mkdir (nnmaildir--marks-dir nndir)))
  685. (setq read-only (nnmaildir--param pgname 'read-only)
  686. ls (or (nnmaildir--param pgname 'directory-files) srv-ls))
  687. (unless read-only
  688. (setq x (nth 11 (file-attributes tdir)))
  689. (unless (and (equal x (nth 11 nattr)) (equal x (nth 11 cattr)))
  690. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  691. (concat "Maildir spans filesystems: " absdir))
  692. (throw 'return nil))
  693. (dolist (file (funcall ls tdir 'full "\\`[^.]" 'nosort))
  694. (setq x (file-attributes file))
  695. (if (or (> (cadr x) 1) (< (car (nth 4 x)) 36h-ago))
  696. (delete-file file))))
  697. (or scan-msgs
  698. isnew
  699. (throw 'return t))
  700. (setq nattr (nth 5 nattr))
  701. (if (equal nattr (nnmaildir--grp-new group))
  702. (setq nattr nil))
  703. (if read-only (setq dir (and (or isnew nattr) ndir))
  704. (when (or isnew nattr)
  705. (dolist (file (funcall ls ndir nil "\\`[^.]" 'nosort))
  706. (setq x (concat ndir file))
  707. (and (time-less-p (nth 5 (file-attributes x)) (current-time))
  708. (rename-file x (concat cdir file ":2,"))))
  709. (setf (nnmaildir--grp-new group) nattr))
  710. (setq cattr (nth 5 (file-attributes cdir)))
  711. (if (equal cattr (nnmaildir--grp-cur group))
  712. (setq cattr nil))
  713. (setq dir (and (or isnew cattr) cdir)))
  714. (unless dir (throw 'return t))
  715. (setq files (funcall ls dir nil "\\`[^.]" 'nosort)
  716. files (save-match-data
  717. (mapcar
  718. (lambda (f)
  719. (string-match "\\`\\([^:]*\\)\\(\\(:.*\\)?\\)\\'" f)
  720. (cons (match-string 1 f) (match-string 2 f)))
  721. files)))
  722. (when isnew
  723. (setq num (nnmaildir--up2-1 (length files)))
  724. (setf (nnmaildir--grp-flist group) (make-vector num 0))
  725. (setf (nnmaildir--grp-mlist group) (make-vector num 0))
  726. (setf (nnmaildir--grp-mmth group) (make-vector 1 0))
  727. (setq num (nnmaildir--param pgname 'nov-cache-size))
  728. (if (numberp num) (if (< num 1) (setq num 1))
  729. (setq num 16
  730. cdir (nnmaildir--marks-dir nndir)
  731. ndir (nnmaildir--subdir cdir "tick")
  732. cdir (nnmaildir--subdir cdir "read"))
  733. (dolist (file files)
  734. (setq file (car file))
  735. (if (or (not (file-exists-p (concat cdir file)))
  736. (file-exists-p (concat ndir file)))
  737. (setq num (1+ num)))))
  738. (setf (nnmaildir--grp-cache group) (make-vector num nil))
  739. (let ((inhibit-quit t))
  740. (set (intern gname groups) group))
  741. (or scan-msgs (throw 'return t)))
  742. (setq flist (nnmaildir--grp-flist group)
  743. files (mapcar
  744. (lambda (file)
  745. (and (null (nnmaildir--flist-art flist (car file)))
  746. file))
  747. files)
  748. files (delq nil files)
  749. files (mapcar 'nnmaildir--parse-filename files)
  750. files (sort files 'nnmaildir--sort-files))
  751. (dolist (file files)
  752. (setq file (if (consp file) file (aref file 3))
  753. x (make-nnmaildir--art :prefix (car file) :suffix (cdr file)))
  754. (nnmaildir--grp-add-art nnmaildir--cur-server group x))
  755. (if read-only (setf (nnmaildir--grp-new group) nattr)
  756. (setf (nnmaildir--grp-cur group) cattr)))
  757. t))
  758. (defun nnmaildir-request-scan (&optional scan-group server)
  759. (let ((coding-system-for-write nnheader-file-coding-system)
  760. (buffer-file-coding-system nil)
  761. (file-coding-system-alist nil)
  762. (nnmaildir-get-new-mail t)
  763. (nnmaildir-group-alist nil)
  764. (nnmaildir-active-file nil)
  765. x srv-ls srv-dir method groups target-prefix group dirs grp-dir seen
  766. deactivate-mark)
  767. (nnmaildir--prepare server nil)
  768. (setq srv-ls (nnmaildir--srv-ls nnmaildir--cur-server)
  769. srv-dir (nnmaildir--srv-dir nnmaildir--cur-server)
  770. method (nnmaildir--srv-method nnmaildir--cur-server)
  771. groups (nnmaildir--srv-groups nnmaildir--cur-server)
  772. target-prefix (nnmaildir--srv-target-prefix nnmaildir--cur-server))
  773. (nnmaildir--with-work-buffer
  774. (save-match-data
  775. (if (stringp scan-group)
  776. (if (nnmaildir--scan scan-group t groups method srv-dir srv-ls)
  777. (if (nnmaildir--srv-gnm nnmaildir--cur-server)
  778. (nnmail-get-new-mail 'nnmaildir nil nil scan-group))
  779. (unintern scan-group groups))
  780. (setq x (nth 5 (file-attributes srv-dir))
  781. scan-group (null scan-group))
  782. (if (equal x (nnmaildir--srv-mtime nnmaildir--cur-server))
  783. (if scan-group
  784. (mapatoms (lambda (sym)
  785. (nnmaildir--scan (symbol-name sym) t groups
  786. method srv-dir srv-ls))
  787. groups))
  788. (setq dirs (funcall srv-ls srv-dir nil "\\`[^.]" 'nosort)
  789. dirs (if (zerop (length target-prefix))
  790. dirs
  791. (gnus-remove-if
  792. (lambda (dir)
  793. (and (>= (length dir) (length target-prefix))
  794. (string= (substring dir 0
  795. (length target-prefix))
  796. target-prefix)))
  797. dirs))
  798. seen (nnmaildir--up2-1 (length dirs))
  799. seen (make-vector seen 0))
  800. (dolist (grp-dir dirs)
  801. (if (nnmaildir--scan grp-dir scan-group groups method srv-dir
  802. srv-ls)
  803. (intern grp-dir seen)))
  804. (setq x nil)
  805. (mapatoms (lambda (group)
  806. (setq group (symbol-name group))
  807. (unless (intern-soft group seen)
  808. (setq x (cons group x))))
  809. groups)
  810. (dolist (grp x)
  811. (unintern grp groups))
  812. (setf (nnmaildir--srv-mtime nnmaildir--cur-server)
  813. (nth 5 (file-attributes srv-dir))))
  814. (and scan-group
  815. (nnmaildir--srv-gnm nnmaildir--cur-server)
  816. (nnmail-get-new-mail 'nnmaildir nil nil))))))
  817. t)
  818. (defun nnmaildir-request-list (&optional server)
  819. (nnmaildir-request-scan 'find-new-groups server)
  820. (let (pgname ro deactivate-mark)
  821. (nnmaildir--prepare server nil)
  822. (nnmaildir--with-nntp-buffer
  823. (erase-buffer)
  824. (mapatoms (lambda (group)
  825. (setq pgname (symbol-name group)
  826. pgname (nnmaildir--pgname nnmaildir--cur-server pgname)
  827. group (symbol-value group)
  828. ro (nnmaildir--param pgname 'read-only))
  829. (insert (gnus-replace-in-string
  830. (nnmaildir--grp-name group) " " "\\ " t)
  831. " ")
  832. (princ (nnmaildir--group-maxnum nnmaildir--cur-server group)
  833. nntp-server-buffer)
  834. (insert " ")
  835. (princ (nnmaildir--grp-min group) nntp-server-buffer)
  836. (insert " " (if ro "n" "y") "\n"))
  837. (nnmaildir--srv-groups nnmaildir--cur-server))))
  838. t)
  839. (defun nnmaildir-request-newgroups (date &optional server)
  840. (nnmaildir-request-list server))
  841. (defun nnmaildir-retrieve-groups (groups &optional server)
  842. (let (group deactivate-mark)
  843. (nnmaildir--prepare server nil)
  844. (nnmaildir--with-nntp-buffer
  845. (erase-buffer)
  846. (dolist (gname groups)
  847. (setq group (nnmaildir--prepare nil gname))
  848. (if (null group) (insert "411 no such news group\n")
  849. (insert "211 ")
  850. (princ (nnmaildir--grp-count group) nntp-server-buffer)
  851. (insert " ")
  852. (princ (nnmaildir--grp-min group) nntp-server-buffer)
  853. (insert " ")
  854. (princ (nnmaildir--group-maxnum nnmaildir--cur-server group)
  855. nntp-server-buffer)
  856. (insert " "
  857. (gnus-replace-in-string gname " " "\\ " t)
  858. "\n")))))
  859. 'group)
  860. (defun nnmaildir-request-marks (gname info &optional server)
  861. (let ((group (nnmaildir--prepare server gname))
  862. pgname flist always-marks never-marks old-marks dotfile num dir
  863. markdirs marks mark ranges markdir article read end new-marks ls
  864. old-mmth new-mmth mtime mark-sym existing missing deactivate-mark
  865. article-list)
  866. (catch 'return
  867. (unless group
  868. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  869. (concat "No such group: " gname))
  870. (throw 'return nil))
  871. (setq gname (nnmaildir--grp-name group)
  872. pgname (nnmaildir--pgname nnmaildir--cur-server gname)
  873. flist (nnmaildir--grp-flist group))
  874. (when (zerop (nnmaildir--grp-count group))
  875. (gnus-info-set-read info nil)
  876. (gnus-info-set-marks info nil 'extend)
  877. (throw 'return info))
  878. (setq old-marks (cons 'read (gnus-info-read info))
  879. old-marks (cons old-marks (gnus-info-marks info))
  880. always-marks (nnmaildir--param pgname 'always-marks)
  881. never-marks (nnmaildir--param pgname 'never-marks)
  882. existing (nnmaildir--grp-nlist group)
  883. existing (mapcar 'car existing)
  884. existing (nreverse existing)
  885. existing (gnus-compress-sequence existing 'always-list)
  886. missing (list (cons 1 (nnmaildir--group-maxnum
  887. nnmaildir--cur-server group)))
  888. missing (gnus-range-difference missing existing)
  889. dir (nnmaildir--srv-dir nnmaildir--cur-server)
  890. dir (nnmaildir--srvgrp-dir dir gname)
  891. dir (nnmaildir--nndir dir)
  892. dir (nnmaildir--marks-dir dir)
  893. ls (nnmaildir--group-ls nnmaildir--cur-server pgname)
  894. markdirs (funcall ls dir nil "\\`[^.]" 'nosort)
  895. new-mmth (nnmaildir--up2-1 (length markdirs))
  896. new-mmth (make-vector new-mmth 0)
  897. old-mmth (nnmaildir--grp-mmth group))
  898. (dolist (mark markdirs)
  899. (setq markdir (nnmaildir--subdir dir mark)
  900. mark-sym (intern mark)
  901. ranges nil)
  902. (catch 'got-ranges
  903. (if (memq mark-sym never-marks) (throw 'got-ranges nil))
  904. (when (memq mark-sym always-marks)
  905. (setq ranges existing)
  906. (throw 'got-ranges nil))
  907. (setq mtime (nth 5 (file-attributes markdir)))
  908. (set (intern mark new-mmth) mtime)
  909. (when (equal mtime (symbol-value (intern-soft mark old-mmth)))
  910. (setq ranges (assq mark-sym old-marks))
  911. (if ranges (setq ranges (cdr ranges)))
  912. (throw 'got-ranges nil))
  913. (setq article-list nil)
  914. (dolist (prefix (funcall ls markdir nil "\\`[^.]" 'nosort))
  915. (setq article (nnmaildir--flist-art flist prefix))
  916. (if article
  917. (setq article-list
  918. (cons (nnmaildir--art-num article) article-list))))
  919. (setq ranges (gnus-add-to-range ranges (sort article-list '<))))
  920. (if (eq mark-sym 'read) (setq read ranges)
  921. (if ranges (setq marks (cons (cons mark-sym ranges) marks)))))
  922. (gnus-info-set-read info (gnus-range-add read missing))
  923. (gnus-info-set-marks info marks 'extend)
  924. (setf (nnmaildir--grp-mmth group) new-mmth)
  925. info)))
  926. (defun nnmaildir-request-group (gname &optional server fast info)
  927. (let ((group (nnmaildir--prepare server gname))
  928. deactivate-mark)
  929. (catch 'return
  930. (unless group
  931. ;; (insert "411 no such news group\n")
  932. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  933. (concat "No such group: " gname))
  934. (throw 'return nil))
  935. (setf (nnmaildir--srv-curgrp nnmaildir--cur-server) group)
  936. (if fast (throw 'return t))
  937. (nnmaildir--with-nntp-buffer
  938. (erase-buffer)
  939. (insert "211 ")
  940. (princ (nnmaildir--grp-count group) nntp-server-buffer)
  941. (insert " ")
  942. (princ (nnmaildir--grp-min group) nntp-server-buffer)
  943. (insert " ")
  944. (princ (nnmaildir--group-maxnum nnmaildir--cur-server group)
  945. nntp-server-buffer)
  946. (insert " " (gnus-replace-in-string gname " " "\\ " t) "\n")
  947. t))))
  948. (defun nnmaildir-request-create-group (gname &optional server args)
  949. (nnmaildir--prepare server nil)
  950. (catch 'return
  951. (let ((target-prefix (nnmaildir--srv-target-prefix nnmaildir--cur-server))
  952. srv-dir dir groups)
  953. (when (zerop (length gname))
  954. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  955. "Invalid (empty) group name")
  956. (throw 'return nil))
  957. (when (eq (aref "." 0) (aref gname 0))
  958. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  959. "Group names may not start with \".\"")
  960. (throw 'return nil))
  961. (when (save-match-data (string-match "[\0/\t]" gname))
  962. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  963. (concat "Invalid characters (null, tab, or /) in group name: "
  964. gname))
  965. (throw 'return nil))
  966. (setq groups (nnmaildir--srv-groups nnmaildir--cur-server))
  967. (when (intern-soft gname groups)
  968. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  969. (concat "Group already exists: " gname))
  970. (throw 'return nil))
  971. (setq srv-dir (nnmaildir--srv-dir nnmaildir--cur-server))
  972. (if (file-name-absolute-p target-prefix)
  973. (setq dir (expand-file-name target-prefix))
  974. (setq dir srv-dir
  975. dir (file-truename dir)
  976. dir (concat dir target-prefix)))
  977. (setq dir (nnmaildir--subdir dir gname))
  978. (nnmaildir--mkdir dir)
  979. (nnmaildir--mkdir (nnmaildir--tmp dir))
  980. (nnmaildir--mkdir (nnmaildir--new dir))
  981. (nnmaildir--mkdir (nnmaildir--cur dir))
  982. (unless (string= target-prefix "")
  983. (make-symbolic-link (concat target-prefix gname)
  984. (concat srv-dir gname)))
  985. (nnmaildir-request-scan 'find-new-groups))))
  986. (defun nnmaildir-request-rename-group (gname new-name &optional server)
  987. (let ((group (nnmaildir--prepare server gname))
  988. (coding-system-for-write nnheader-file-coding-system)
  989. (buffer-file-coding-system nil)
  990. (file-coding-system-alist nil)
  991. srv-dir x groups)
  992. (catch 'return
  993. (unless group
  994. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  995. (concat "No such group: " gname))
  996. (throw 'return nil))
  997. (when (zerop (length new-name))
  998. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  999. "Invalid (empty) group name")
  1000. (throw 'return nil))
  1001. (when (eq (aref "." 0) (aref new-name 0))
  1002. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1003. "Group names may not start with \".\"")
  1004. (throw 'return nil))
  1005. (when (save-match-data (string-match "[\0/\t]" new-name))
  1006. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1007. (concat "Invalid characters (null, tab, or /) in group name: "
  1008. new-name))
  1009. (throw 'return nil))
  1010. (if (string-equal gname new-name) (throw 'return t))
  1011. (when (intern-soft new-name
  1012. (nnmaildir--srv-groups nnmaildir--cur-server))
  1013. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1014. (concat "Group already exists: " new-name))
  1015. (throw 'return nil))
  1016. (setq srv-dir (nnmaildir--srv-dir nnmaildir--cur-server))
  1017. (condition-case err
  1018. (rename-file (concat srv-dir gname)
  1019. (concat srv-dir new-name))
  1020. (error
  1021. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1022. (concat "Error renaming link: " (prin1-to-string err)))
  1023. (throw 'return nil)))
  1024. (setq x (nnmaildir--srv-groups nnmaildir--cur-server)
  1025. groups (make-vector (length x) 0))
  1026. (mapatoms (lambda (sym)
  1027. (unless (eq (symbol-value sym) group)
  1028. (set (intern (symbol-name sym) groups)
  1029. (symbol-value sym))))
  1030. x)
  1031. (setq group (copy-sequence group))
  1032. (setf (nnmaildir--grp-name group) new-name)
  1033. (set (intern new-name groups) group)
  1034. (setf (nnmaildir--srv-groups nnmaildir--cur-server) groups)
  1035. t)))
  1036. (defun nnmaildir-request-delete-group (gname force &optional server)
  1037. (let ((group (nnmaildir--prepare server gname))
  1038. pgname grp-dir target dir ls deactivate-mark)
  1039. (catch 'return
  1040. (unless group
  1041. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1042. (concat "No such group: " gname))
  1043. (throw 'return nil))
  1044. (setq gname (nnmaildir--grp-name group)
  1045. pgname (nnmaildir--pgname nnmaildir--cur-server gname)
  1046. grp-dir (nnmaildir--srv-dir nnmaildir--cur-server)
  1047. target (car (file-attributes (concat grp-dir gname)))
  1048. grp-dir (nnmaildir--srvgrp-dir grp-dir gname))
  1049. (unless (or force (stringp target))
  1050. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1051. (concat "Not a symlink: " gname))
  1052. (throw 'return nil))
  1053. (if (eq group (nnmaildir--srv-curgrp nnmaildir--cur-server))
  1054. (setf (nnmaildir--srv-curgrp nnmaildir--cur-server) nil))
  1055. (unintern gname (nnmaildir--srv-groups nnmaildir--cur-server))
  1056. (if (not force)
  1057. (progn
  1058. (setq grp-dir (directory-file-name grp-dir))
  1059. (nnmaildir--unlink grp-dir))
  1060. (setq ls (nnmaildir--group-ls nnmaildir--cur-server pgname))
  1061. (if (nnmaildir--param pgname 'read-only)
  1062. (progn (delete-directory (nnmaildir--tmp grp-dir))
  1063. (nnmaildir--unlink (nnmaildir--new grp-dir))
  1064. (delete-directory (nnmaildir--cur grp-dir)))
  1065. (nnmaildir--delete-dir-files (nnmaildir--tmp grp-dir) ls)
  1066. (nnmaildir--delete-dir-files (nnmaildir--new grp-dir) ls)
  1067. (nnmaildir--delete-dir-files (nnmaildir--cur grp-dir) ls))
  1068. (setq dir (nnmaildir--nndir grp-dir))
  1069. (dolist (subdir `(,(nnmaildir--nov-dir dir) ,(nnmaildir--num-dir dir)
  1070. ,@(funcall ls (nnmaildir--marks-dir dir)
  1071. 'full "\\`[^.]" 'nosort)))
  1072. (nnmaildir--delete-dir-files subdir ls))
  1073. (setq dir (nnmaildir--nndir grp-dir))
  1074. (nnmaildir--unlink (concat dir "markfile"))
  1075. (nnmaildir--unlink (concat dir "markfile{new}"))
  1076. (delete-directory (nnmaildir--marks-dir dir))
  1077. (delete-directory dir)
  1078. (if (not (stringp target))
  1079. (delete-directory grp-dir)
  1080. (setq grp-dir (directory-file-name grp-dir)
  1081. dir target)
  1082. (unless (eq (aref "/" 0) (aref dir 0))
  1083. (setq dir (concat (file-truename
  1084. (nnmaildir--srv-dir nnmaildir--cur-server))
  1085. dir)))
  1086. (delete-directory dir)
  1087. (nnmaildir--unlink grp-dir)))
  1088. t)))
  1089. (defun nnmaildir-retrieve-headers (articles &optional gname server fetch-old)
  1090. (let ((group (nnmaildir--prepare server gname))
  1091. srv-dir dir nlist mlist article num start stop nov nlist2 insert-nov
  1092. deactivate-mark)
  1093. (setq insert-nov
  1094. (lambda (article)
  1095. (setq nov (nnmaildir--update-nov nnmaildir--cur-server group
  1096. article))
  1097. (when nov
  1098. (nnmaildir--cache-nov group article nov)
  1099. (setq num (nnmaildir--art-num article))
  1100. (princ num nntp-server-buffer)
  1101. (insert "\t" (nnmaildir--nov-get-beg nov) "\t"
  1102. (nnmaildir--art-msgid article) "\t"
  1103. (nnmaildir--nov-get-mid nov) "\tXref: nnmaildir "
  1104. (gnus-replace-in-string gname " " "\\ " t) ":")
  1105. (princ num nntp-server-buffer)
  1106. (insert "\t" (nnmaildir--nov-get-end nov) "\n"))))
  1107. (catch 'return
  1108. (unless group
  1109. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1110. (if gname (concat "No such group: " gname) "No current group"))
  1111. (throw 'return nil))
  1112. (nnmaildir--with-nntp-buffer
  1113. (erase-buffer)
  1114. (setq mlist (nnmaildir--grp-mlist group)
  1115. nlist (nnmaildir--grp-nlist group)
  1116. gname (nnmaildir--grp-name group)
  1117. srv-dir (nnmaildir--srv-dir nnmaildir--cur-server)
  1118. dir (nnmaildir--srvgrp-dir srv-dir gname))
  1119. (cond
  1120. ((null nlist))
  1121. ((and fetch-old (not (numberp fetch-old)))
  1122. (nnmaildir--nlist-iterate nlist 'all insert-nov))
  1123. ((null articles))
  1124. ((stringp (car articles))
  1125. (dolist (msgid articles)
  1126. (setq article (nnmaildir--mlist-art mlist msgid))
  1127. (if article (funcall insert-nov article))))
  1128. (t
  1129. (if fetch-old
  1130. ;; Assume the article range list is sorted ascending
  1131. (setq stop (car articles)
  1132. start (car (last articles))
  1133. stop (if (numberp stop) stop (car stop))
  1134. start (if (numberp start) start (cdr start))
  1135. stop (- stop fetch-old)
  1136. stop (if (< stop 1) 1 stop)
  1137. articles (list (cons stop start))))
  1138. (nnmaildir--nlist-iterate nlist articles insert-nov)))
  1139. (sort-numeric-fields 1 (point-min) (point-max))
  1140. 'nov))))
  1141. (defun nnmaildir-request-article (num-msgid &optional gname server to-buffer)
  1142. (let ((group (nnmaildir--prepare server gname))
  1143. (case-fold-search t)
  1144. list article dir pgname deactivate-mark)
  1145. (catch 'return
  1146. (unless group
  1147. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1148. (if gname (concat "No such group: " gname) "No current group"))
  1149. (throw 'return nil))
  1150. (if (numberp num-msgid)
  1151. (setq article (nnmaildir--nlist-art group num-msgid))
  1152. (setq list (nnmaildir--grp-mlist group)
  1153. article (nnmaildir--mlist-art list num-msgid))
  1154. (if article (setq num-msgid (nnmaildir--art-num article))
  1155. (catch 'found
  1156. (mapatoms
  1157. (lambda (group-sym)
  1158. (setq group (symbol-value group-sym)
  1159. list (nnmaildir--grp-mlist group)
  1160. article (nnmaildir--mlist-art list num-msgid))
  1161. (when article
  1162. (setq num-msgid (nnmaildir--art-num article))
  1163. (throw 'found nil)))
  1164. (nnmaildir--srv-groups nnmaildir--cur-server))))
  1165. (unless article
  1166. (setf (nnmaildir--srv-error nnmaildir--cur-server) "No such article")
  1167. (throw 'return nil)))
  1168. (setq gname (nnmaildir--grp-name group)
  1169. pgname (nnmaildir--pgname nnmaildir--cur-server gname)
  1170. dir (nnmaildir--srv-dir nnmaildir--cur-server)
  1171. dir (nnmaildir--srvgrp-dir dir gname)
  1172. dir (if (nnmaildir--param pgname 'read-only)
  1173. (nnmaildir--new dir) (nnmaildir--cur dir))
  1174. nnmaildir-article-file-name
  1175. (concat dir
  1176. (nnmaildir--art-prefix article)
  1177. (nnmaildir--art-suffix article)))
  1178. (unless (file-exists-p nnmaildir-article-file-name)
  1179. (nnmaildir--expired-article group article)
  1180. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1181. "Article has expired")
  1182. (throw 'return nil))
  1183. (with-current-buffer (or to-buffer nntp-server-buffer)
  1184. (erase-buffer)
  1185. (nnheader-insert-file-contents nnmaildir-article-file-name))
  1186. (cons gname num-msgid))))
  1187. (defun nnmaildir-request-post (&optional server)
  1188. (let (message-required-mail-headers)
  1189. (funcall message-send-mail-function)))
  1190. (defun nnmaildir-request-replace-article (number gname buffer)
  1191. (let ((group (nnmaildir--prepare nil gname))
  1192. (coding-system-for-write nnheader-file-coding-system)
  1193. (buffer-file-coding-system nil)
  1194. (file-coding-system-alist nil)
  1195. dir file article suffix tmpfile deactivate-mark)
  1196. (catch 'return
  1197. (unless group
  1198. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1199. (concat "No such group: " gname))
  1200. (throw 'return nil))
  1201. (when (nnmaildir--param (nnmaildir--pgname nnmaildir--cur-server gname)
  1202. 'read-only)
  1203. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1204. (concat "Read-only group: " group))
  1205. (throw 'return nil))
  1206. (setq dir (nnmaildir--srv-dir nnmaildir--cur-server)
  1207. dir (nnmaildir--srvgrp-dir dir gname)
  1208. article (nnmaildir--nlist-art group number))
  1209. (unless article
  1210. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1211. (concat "No such article: " (number-to-string number)))
  1212. (throw 'return nil))
  1213. (setq suffix (nnmaildir--art-suffix article)
  1214. file (nnmaildir--art-prefix article)
  1215. tmpfile (concat (nnmaildir--tmp dir) file))
  1216. (when (file-exists-p tmpfile)
  1217. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1218. (concat "File exists: " tmpfile))
  1219. (throw 'return nil))
  1220. (with-current-buffer buffer
  1221. (gmm-write-region (point-min) (point-max) tmpfile nil 'no-message nil
  1222. 'excl))
  1223. (unix-sync) ;; no fsync :(
  1224. (rename-file tmpfile (concat (nnmaildir--cur dir) file suffix) 'replace)
  1225. t)))
  1226. (defun nnmaildir-request-move-article (article gname server accept-form
  1227. &optional last move-is-internal)
  1228. (let ((group (nnmaildir--prepare server gname))
  1229. pgname suffix result nnmaildir--file deactivate-mark)
  1230. (catch 'return
  1231. (unless group
  1232. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1233. (concat "No such group: " gname))
  1234. (throw 'return nil))
  1235. (setq gname (nnmaildir--grp-name group)
  1236. pgname (nnmaildir--pgname nnmaildir--cur-server gname)
  1237. article (nnmaildir--nlist-art group article))
  1238. (unless article
  1239. (setf (nnmaildir--srv-error nnmaildir--cur-server) "No such article")
  1240. (throw 'return nil))
  1241. (setq suffix (nnmaildir--art-suffix article)
  1242. nnmaildir--file (nnmaildir--srv-dir nnmaildir--cur-server)
  1243. nnmaildir--file (nnmaildir--srvgrp-dir nnmaildir--file gname)
  1244. nnmaildir--file (if (nnmaildir--param pgname 'read-only)
  1245. (nnmaildir--new nnmaildir--file)
  1246. (nnmaildir--cur nnmaildir--file))
  1247. nnmaildir--file (concat nnmaildir--file
  1248. (nnmaildir--art-prefix article)
  1249. suffix))
  1250. (unless (file-exists-p nnmaildir--file)
  1251. (nnmaildir--expired-article group article)
  1252. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1253. "Article has expired")
  1254. (throw 'return nil))
  1255. (nnmaildir--with-move-buffer
  1256. (erase-buffer)
  1257. (nnheader-insert-file-contents nnmaildir--file)
  1258. (setq result (eval accept-form)))
  1259. (unless (or (null result) (nnmaildir--param pgname 'read-only))
  1260. (nnmaildir--unlink nnmaildir--file)
  1261. (nnmaildir--expired-article group article))
  1262. result)))
  1263. (defun nnmaildir-request-accept-article (gname &optional server last)
  1264. (let ((group (nnmaildir--prepare server gname))
  1265. (coding-system-for-write nnheader-file-coding-system)
  1266. (buffer-file-coding-system nil)
  1267. (file-coding-system-alist nil)
  1268. srv-dir dir file time tmpfile curfile 24h article)
  1269. (catch 'return
  1270. (unless group
  1271. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1272. (concat "No such group: " gname))
  1273. (throw 'return nil))
  1274. (setq gname (nnmaildir--grp-name group))
  1275. (when (nnmaildir--param (nnmaildir--pgname nnmaildir--cur-server gname)
  1276. 'read-only)
  1277. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1278. (concat "Read-only group: " gname))
  1279. (throw 'return nil))
  1280. (setq srv-dir (nnmaildir--srv-dir nnmaildir--cur-server)
  1281. dir (nnmaildir--srvgrp-dir srv-dir gname)
  1282. time (current-time)
  1283. file (format-time-string "%s." time))
  1284. (unless (string-equal nnmaildir--delivery-time file)
  1285. (setq nnmaildir--delivery-time file
  1286. nnmaildir--delivery-count 0))
  1287. (when (and (consp (cdr time))
  1288. (consp (cddr time)))
  1289. (setq file (concat file "M" (number-to-string (caddr time)))))
  1290. (setq file (concat file nnmaildir--delivery-pid)
  1291. file (concat file "Q" (number-to-string nnmaildir--delivery-count))
  1292. file (concat file "." (nnmaildir--system-name))
  1293. tmpfile (concat (nnmaildir--tmp dir) file)
  1294. curfile (concat (nnmaildir--cur dir) file ":2,"))
  1295. (when (file-exists-p tmpfile)
  1296. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1297. (concat "File exists: " tmpfile))
  1298. (throw 'return nil))
  1299. (when (file-exists-p curfile)
  1300. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1301. (concat "File exists: " curfile))
  1302. (throw 'return nil))
  1303. (setq nnmaildir--delivery-count (1+ nnmaildir--delivery-count)
  1304. 24h (run-with-timer 86400 nil
  1305. (lambda ()
  1306. (nnmaildir--unlink tmpfile)
  1307. (setf (nnmaildir--srv-error
  1308. nnmaildir--cur-server)
  1309. "24-hour timer expired")
  1310. (throw 'return nil))))
  1311. (condition-case nil (add-name-to-file nnmaildir--file tmpfile)
  1312. (error
  1313. (gmm-write-region (point-min) (point-max) tmpfile nil 'no-message nil
  1314. 'excl)
  1315. (when (fboundp 'unix-sync)
  1316. (unix-sync)))) ;; no fsync :(
  1317. (nnheader-cancel-timer 24h)
  1318. (condition-case err
  1319. (add-name-to-file tmpfile curfile)
  1320. (error
  1321. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1322. (concat "Error linking: " (prin1-to-string err)))
  1323. (nnmaildir--unlink tmpfile)
  1324. (throw 'return nil)))
  1325. (nnmaildir--unlink tmpfile)
  1326. (setq article (make-nnmaildir--art :prefix file :suffix ":2,"))
  1327. (if (nnmaildir--grp-add-art nnmaildir--cur-server group article)
  1328. (cons gname (nnmaildir--art-num article))))))
  1329. (defun nnmaildir-save-mail (group-art)
  1330. (catch 'return
  1331. (unless group-art
  1332. (throw 'return nil))
  1333. (let (ga gname x groups nnmaildir--file deactivate-mark)
  1334. (save-excursion
  1335. (goto-char (point-min))
  1336. (save-match-data
  1337. (while (looking-at "From ")
  1338. (replace-match "X-From-Line: ")
  1339. (forward-line 1))))
  1340. (setq groups (nnmaildir--srv-groups nnmaildir--cur-server)
  1341. ga (car group-art) group-art (cdr group-art)
  1342. gname (car ga))
  1343. (or (intern-soft gname groups)
  1344. (nnmaildir-request-create-group gname)
  1345. (throw 'return nil)) ;; not that nnmail bothers to check :(
  1346. (unless (nnmaildir-request-accept-article gname)
  1347. (throw 'return nil))
  1348. (setq nnmaildir--file (nnmaildir--srv-dir nnmaildir--cur-server)
  1349. nnmaildir--file (nnmaildir--srvgrp-dir nnmaildir--file gname)
  1350. x (nnmaildir--prepare nil gname)
  1351. x (nnmaildir--grp-nlist x)
  1352. x (cdar x)
  1353. nnmaildir--file (concat nnmaildir--file
  1354. (nnmaildir--art-prefix x)
  1355. (nnmaildir--art-suffix x)))
  1356. (delq nil
  1357. (mapcar
  1358. (lambda (ga)
  1359. (setq gname (car ga))
  1360. (and (or (intern-soft gname groups)
  1361. (nnmaildir-request-create-group gname))
  1362. (nnmaildir-request-accept-article gname)
  1363. ga))
  1364. group-art)))))
  1365. (defun nnmaildir-active-number (gname)
  1366. 0)
  1367. (declare-function gnus-group-mark-article-read "gnus-group" (group article))
  1368. (defun nnmaildir-request-expire-articles (ranges &optional gname server force)
  1369. (let ((no-force (not force))
  1370. (group (nnmaildir--prepare server gname))
  1371. pgname time boundary bound-iter high low target dir nlist nlist2
  1372. stop article didnt nnmaildir--file nnmaildir-article-file-name
  1373. deactivate-mark)
  1374. (catch 'return
  1375. (unless group
  1376. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1377. (if gname (concat "No such group: " gname) "No current group"))
  1378. (throw 'return (gnus-uncompress-range ranges)))
  1379. (setq gname (nnmaildir--grp-name group)
  1380. pgname (nnmaildir--pgname nnmaildir--cur-server gname))
  1381. (if (nnmaildir--param pgname 'read-only)
  1382. (throw 'return (gnus-uncompress-range ranges)))
  1383. (setq time (nnmaildir--param pgname 'expire-age))
  1384. (unless time
  1385. (setq time (or (and nnmail-expiry-wait-function
  1386. (funcall nnmail-expiry-wait-function gname))
  1387. nnmail-expiry-wait))
  1388. (if (eq time 'immediate)
  1389. (setq time 0)
  1390. (if (numberp time)
  1391. (setq time (round (* time 86400))))))
  1392. (when no-force
  1393. (unless (integerp time) ;; handle 'never
  1394. (throw 'return (gnus-uncompress-range ranges)))
  1395. (setq boundary (current-time)
  1396. high (- (car boundary) (/ time 65536))
  1397. low (- (cadr boundary) (% time 65536)))
  1398. (if (< low 0)
  1399. (setq low (+ low 65536)
  1400. high (1- high)))
  1401. (setcar (cdr boundary) low)
  1402. (setcar boundary high))
  1403. (setq dir (nnmaildir--srv-dir nnmaildir--cur-server)
  1404. dir (nnmaildir--srvgrp-dir dir gname)
  1405. dir (nnmaildir--cur dir)
  1406. nlist (nnmaildir--grp-nlist group)
  1407. ranges (reverse ranges))
  1408. (nnmaildir--with-move-buffer
  1409. (nnmaildir--nlist-iterate
  1410. nlist ranges
  1411. (lambda (article)
  1412. (setq nnmaildir--file (nnmaildir--art-prefix article)
  1413. nnmaildir--file (concat dir nnmaildir--file
  1414. (nnmaildir--art-suffix article))
  1415. time (file-attributes nnmaildir--file))
  1416. (cond
  1417. ((null time)
  1418. (nnmaildir--expired-article group article))
  1419. ((and no-force
  1420. (progn
  1421. (setq time (nth 5 time)
  1422. bound-iter boundary)
  1423. (while (and bound-iter time
  1424. (= (car bound-iter) (car time)))
  1425. (setq bound-iter (cdr bound-iter)
  1426. time (cdr time)))
  1427. (and bound-iter time
  1428. (car-less-than-car bound-iter time))))
  1429. (setq didnt (cons (nnmaildir--art-num article) didnt)))
  1430. (t
  1431. (setq nnmaildir-article-file-name nnmaildir--file
  1432. target (if force nil
  1433. (save-excursion
  1434. (save-restriction
  1435. (nnmaildir--param pgname 'expire-group)))))
  1436. (when (and (stringp target)
  1437. (not (string-equal target pgname))) ;; Move it.
  1438. (erase-buffer)
  1439. (nnheader-insert-file-contents nnmaildir--file)
  1440. (let ((group-art (gnus-request-accept-article
  1441. target nil nil 'no-encode)))
  1442. (when (consp group-art)
  1443. ;; Maybe also copy: dormant forward reply save tick
  1444. ;; (gnus-add-mark? gnus-request-set-mark?)
  1445. (gnus-group-mark-article-read target (cdr group-art)))))
  1446. (if (equal target pgname)
  1447. ;; Leave it here.
  1448. (setq didnt (cons (nnmaildir--art-num article) didnt))
  1449. (nnmaildir--unlink nnmaildir--file)
  1450. (nnmaildir--expired-article group article))))))
  1451. (erase-buffer))
  1452. didnt)))
  1453. (defun nnmaildir-request-set-mark (gname actions &optional server)
  1454. (let ((group (nnmaildir--prepare server gname))
  1455. (coding-system-for-write nnheader-file-coding-system)
  1456. (buffer-file-coding-system nil)
  1457. (file-coding-system-alist nil)
  1458. del-mark del-action add-action set-action marksdir nlist
  1459. ranges begin end article all-marks todo-marks mdir mfile
  1460. pgname ls permarkfile deactivate-mark)
  1461. (setq del-mark
  1462. (lambda (mark)
  1463. (setq mfile (nnmaildir--subdir marksdir (symbol-name mark))
  1464. mfile (concat mfile (nnmaildir--art-prefix article)))
  1465. (nnmaildir--unlink mfile))
  1466. del-action (lambda (article) (mapcar del-mark todo-marks))
  1467. add-action
  1468. (lambda (article)
  1469. (mapcar
  1470. (lambda (mark)
  1471. (setq mdir (nnmaildir--subdir marksdir (symbol-name mark))
  1472. permarkfile (concat mdir ":")
  1473. mfile (concat mdir (nnmaildir--art-prefix article)))
  1474. (nnmaildir--condcase err (add-name-to-file permarkfile mfile)
  1475. (cond
  1476. ((nnmaildir--eexist-p err))
  1477. ((nnmaildir--enoent-p err)
  1478. (nnmaildir--mkdir mdir)
  1479. (nnmaildir--mkfile permarkfile)
  1480. (add-name-to-file permarkfile mfile))
  1481. ((nnmaildir--emlink-p err)
  1482. (let ((permarkfilenew (concat permarkfile "{new}")))
  1483. (nnmaildir--mkfile permarkfilenew)
  1484. (rename-file permarkfilenew permarkfile 'replace)
  1485. (add-name-to-file permarkfile mfile)))
  1486. (t (signal (car err) (cdr err))))))
  1487. todo-marks))
  1488. set-action (lambda (article)
  1489. (funcall add-action article)
  1490. (mapcar (lambda (mark)
  1491. (unless (memq mark todo-marks)
  1492. (funcall del-mark mark)))
  1493. all-marks)))
  1494. (catch 'return
  1495. (unless group
  1496. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1497. (concat "No such group: " gname))
  1498. (dolist (action actions)
  1499. (setq ranges (gnus-range-add ranges (car action))))
  1500. (throw 'return ranges))
  1501. (setq nlist (nnmaildir--grp-nlist group)
  1502. marksdir (nnmaildir--srv-dir nnmaildir--cur-server)
  1503. marksdir (nnmaildir--srvgrp-dir marksdir gname)
  1504. marksdir (nnmaildir--nndir marksdir)
  1505. marksdir (nnmaildir--marks-dir marksdir)
  1506. gname (nnmaildir--grp-name group)
  1507. pgname (nnmaildir--pgname nnmaildir--cur-server gname)
  1508. ls (nnmaildir--group-ls nnmaildir--cur-server pgname)
  1509. all-marks (funcall ls marksdir nil "\\`[^.]" 'nosort)
  1510. all-marks (mapcar 'intern all-marks))
  1511. (dolist (action actions)
  1512. (setq ranges (car action)
  1513. todo-marks (caddr action))
  1514. (dolist (mark todo-marks)
  1515. (add-to-list 'all-marks mark))
  1516. (if (numberp (cdr ranges)) (setq ranges (list ranges)))
  1517. (nnmaildir--nlist-iterate nlist ranges
  1518. (cond ((eq 'del (cadr action)) del-action)
  1519. ((eq 'add (cadr action)) add-action)
  1520. ((eq 'set (cadr action)) set-action))))
  1521. nil)))
  1522. (defun nnmaildir-close-group (gname &optional server)
  1523. (let ((group (nnmaildir--prepare server gname))
  1524. pgname ls dir msgdir files flist dirs)
  1525. (if (null group)
  1526. (progn
  1527. (setf (nnmaildir--srv-error nnmaildir--cur-server)
  1528. (concat "No such group: " gname))
  1529. nil)
  1530. (setq pgname (nnmaildir--pgname nnmaildir--cur-server gname)
  1531. ls (nnmaildir--group-ls nnmaildir--cur-server pgname)
  1532. dir (nnmaildir--srv-dir nnmaildir--cur-server)
  1533. dir (nnmaildir--srvgrp-dir dir gname)
  1534. msgdir (if (nnmaildir--param pgname 'read-only)
  1535. (nnmaildir--new dir) (nnmaildir--cur dir))
  1536. dir (nnmaildir--nndir dir)
  1537. dirs (cons (nnmaildir--nov-dir dir)
  1538. (funcall ls (nnmaildir--marks-dir dir) 'full "\\`[^.]"
  1539. 'nosort))
  1540. dirs (mapcar
  1541. (lambda (dir)
  1542. (cons dir (funcall ls dir nil "\\`[^.]" 'nosort)))
  1543. dirs)
  1544. files (funcall ls msgdir nil "\\`[^.]" 'nosort)
  1545. flist (nnmaildir--up2-1 (length files))
  1546. flist (make-vector flist 0))
  1547. (save-match-data
  1548. (dolist (file files)
  1549. (string-match "\\`\\([^:]*\\)\\(:.*\\)?\\'" file)
  1550. (intern (match-string 1 file) flist)))
  1551. (dolist (dir dirs)
  1552. (setq files (cdr dir)
  1553. dir (file-name-as-directory (car dir)))
  1554. (dolist (file files)
  1555. (unless (or (intern-soft file flist) (string= file ":"))
  1556. (setq file (concat dir file))
  1557. (delete-file file))))
  1558. t)))
  1559. (defun nnmaildir-close-server (&optional server)
  1560. (let (flist ls dirs dir files file x)
  1561. (nnmaildir--prepare server nil)
  1562. (when nnmaildir--cur-server
  1563. (setq server nnmaildir--cur-server
  1564. nnmaildir--cur-server nil)
  1565. (unintern (nnmaildir--srv-address server) nnmaildir--servers)))
  1566. t)
  1567. (defun nnmaildir-request-close ()
  1568. (let (servers buffer)
  1569. (mapatoms (lambda (server)
  1570. (setq servers (cons (symbol-name server) servers)))
  1571. nnmaildir--servers)
  1572. (mapc 'nnmaildir-close-server servers)
  1573. (setq buffer (get-buffer " *nnmaildir work*"))
  1574. (if buffer (kill-buffer buffer))
  1575. (setq buffer (get-buffer " *nnmaildir nov*"))
  1576. (if buffer (kill-buffer buffer))
  1577. (setq buffer (get-buffer " *nnmaildir move*"))
  1578. (if buffer (kill-buffer buffer)))
  1579. t)
  1580. (provide 'nnmaildir)
  1581. ;; Local Variables:
  1582. ;; indent-tabs-mode: t
  1583. ;; fill-column: 77
  1584. ;; End:
  1585. ;;; nnmaildir.el ends here