drv_maildir.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006,2010-2013 Oswald Buddenhagen <ossi@users.sf.net>
  5. * Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program 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. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * As a special exception, mbsync may be linked with the OpenSSL library,
  21. * despite that library's more restrictive license.
  22. */
  23. #include "driver.h"
  24. #include <assert.h>
  25. #include <limits.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <dirent.h>
  30. #include <fcntl.h>
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. #include <sys/stat.h>
  34. #include <sys/file.h>
  35. #include <errno.h>
  36. #include <time.h>
  37. #include <utime.h>
  38. #ifdef __linux__
  39. # define LEGACY_FLOCK 1
  40. #endif
  41. #if !defined(_POSIX_SYNCHRONIZED_IO) || _POSIX_SYNCHRONIZED_IO <= 0
  42. # define fdatasync fsync
  43. #endif
  44. #ifdef USE_DB
  45. #include <db.h>
  46. #endif /* USE_DB */
  47. #define SUB_UNSET 0
  48. #define SUB_VERBATIM 1
  49. #define SUB_MAILDIRPP 2
  50. #define SUB_LEGACY 3
  51. typedef struct maildir_store_conf {
  52. store_conf_t gen;
  53. char *inbox;
  54. #ifdef USE_DB
  55. int alt_map;
  56. #endif /* USE_DB */
  57. char info_delimiter;
  58. char sub_style;
  59. char failed;
  60. char *info_prefix, *info_stop; /* precalculated from info_delimiter */
  61. } maildir_store_conf_t;
  62. typedef struct maildir_message {
  63. message_t gen;
  64. char *base;
  65. } maildir_message_t;
  66. typedef struct maildir_store {
  67. store_t gen;
  68. int uvfd, uvok, nuid, is_inbox, fresh[3];
  69. int minuid, maxuid, newuid, nexcs, *excs;
  70. char *trash;
  71. #ifdef USE_DB
  72. DB *db;
  73. char *usedb;
  74. #endif /* USE_DB */
  75. wakeup_t lcktmr;
  76. } maildir_store_t;
  77. #ifdef USE_DB
  78. static DBT key, value; /* no need to be reentrant, and this saves lots of memset()s */
  79. #endif /* USE_DB */
  80. static struct flock lck;
  81. static int MaildirCount;
  82. static void ATTR_PRINTFLIKE(1, 2)
  83. debug( const char *msg, ... )
  84. {
  85. va_list va;
  86. va_start( va, msg );
  87. vdebug( DEBUG_SYNC, msg, va );
  88. va_end( va );
  89. }
  90. static const char Flags[] = { 'D', 'F', 'R', 'S', 'T' };
  91. static uchar
  92. maildir_parse_flags( const char *info_prefix, const char *base )
  93. {
  94. const char *s;
  95. uint i;
  96. uchar flags;
  97. flags = 0;
  98. if ((s = strstr( base, info_prefix )))
  99. for (s += 3, i = 0; i < as(Flags); i++)
  100. if (strchr( s, Flags[i] ))
  101. flags |= (1 << i);
  102. return flags;
  103. }
  104. static char *
  105. maildir_join_path( maildir_store_conf_t *conf, const char *prefix, const char *box )
  106. {
  107. char *out, *p;
  108. int pl, bl, n, sub = 0;
  109. char c;
  110. pl = strlen( prefix );
  111. for (bl = 0, n = 0; (c = box[bl]); bl++)
  112. if (c == '/') {
  113. if (conf->sub_style == SUB_UNSET) {
  114. error( "Maildir error: accessing subfolder '%s', but store '%s' does not specify SubFolders style\n",
  115. box, conf->gen.name );
  116. return 0;
  117. }
  118. n++;
  119. } else if (c == '.' && conf->sub_style == SUB_MAILDIRPP) {
  120. error( "Maildir error: store '%s', folder '%s': SubFolders style Maildir++ does not support dots in mailbox names\n",
  121. conf->gen.name, box );
  122. return 0;
  123. }
  124. out = nfmalloc( pl + bl + n + 1 );
  125. memcpy( out, prefix, pl );
  126. p = out + pl;
  127. while ((c = *box++)) {
  128. if (c == '/') {
  129. switch (conf->sub_style) {
  130. case SUB_VERBATIM:
  131. *p++ = c;
  132. break;
  133. case SUB_MAILDIRPP:
  134. if (!sub) {
  135. sub = 1;
  136. *p++ = c;
  137. }
  138. *p++ = '.';
  139. break;
  140. case SUB_LEGACY:
  141. *p++ = c;
  142. *p++ = '.';
  143. break;
  144. }
  145. } else {
  146. *p++ = c;
  147. }
  148. }
  149. *p = 0;
  150. return out;
  151. }
  152. static int
  153. maildir_ensure_path( maildir_store_conf_t *conf )
  154. {
  155. if (!conf->gen.path) {
  156. error( "Maildir error: store '%s' has no Path\n", conf->gen.name );
  157. conf->failed = FAIL_FINAL;
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. static int
  163. maildir_validate_path( maildir_store_conf_t *conf )
  164. {
  165. struct stat st;
  166. if (stat( conf->gen.path, &st ) || !S_ISDIR(st.st_mode)) {
  167. error( "Maildir error: cannot open store '%s'\n", conf->gen.path );
  168. conf->failed = FAIL_FINAL;
  169. return -1;
  170. }
  171. return 0;
  172. }
  173. static void lcktmr_timeout( void *aux );
  174. static store_t *
  175. maildir_alloc_store( store_conf_t *gconf, const char *label ATTR_UNUSED )
  176. {
  177. maildir_store_t *ctx;
  178. ctx = nfcalloc( sizeof(*ctx) );
  179. ctx->gen.conf = gconf;
  180. ctx->uvfd = -1;
  181. init_wakeup( &ctx->lcktmr, lcktmr_timeout, ctx );
  182. return &ctx->gen;
  183. }
  184. static void
  185. maildir_connect_store( store_t *gctx,
  186. void (*cb)( int sts, void *aux ), void *aux )
  187. {
  188. maildir_store_t *ctx = (maildir_store_t *)gctx;
  189. maildir_store_conf_t *conf = (maildir_store_conf_t *)ctx->gen.conf;
  190. if (conf->gen.path && maildir_validate_path( conf ) < 0) {
  191. cb( DRV_STORE_BAD, aux );
  192. return;
  193. }
  194. if (conf->gen.trash) {
  195. if (maildir_ensure_path( conf ) < 0) {
  196. cb( DRV_STORE_BAD, aux );
  197. return;
  198. }
  199. if (!(ctx->trash = maildir_join_path( conf, conf->gen.path, conf->gen.trash ))) {
  200. cb( DRV_STORE_BAD, aux );
  201. return;
  202. }
  203. }
  204. cb( DRV_OK, aux );
  205. }
  206. static void
  207. free_maildir_messages( message_t *msg )
  208. {
  209. message_t *tmsg;
  210. for (; (tmsg = msg); msg = tmsg) {
  211. tmsg = msg->next;
  212. free( ((maildir_message_t *)msg)->base );
  213. free( msg );
  214. }
  215. }
  216. static void
  217. maildir_cleanup( store_t *gctx )
  218. {
  219. maildir_store_t *ctx = (maildir_store_t *)gctx;
  220. free_maildir_messages( gctx->msgs );
  221. #ifdef USE_DB
  222. if (ctx->db)
  223. ctx->db->close( ctx->db, 0 );
  224. free( ctx->usedb );
  225. #endif /* USE_DB */
  226. free( gctx->path );
  227. free( ctx->excs );
  228. if (ctx->uvfd >= 0)
  229. close( ctx->uvfd );
  230. conf_wakeup( &ctx->lcktmr, -1 );
  231. }
  232. static void
  233. maildir_free_store( store_t *gctx )
  234. {
  235. maildir_store_t *ctx = (maildir_store_t *)gctx;
  236. maildir_cleanup( gctx );
  237. wipe_wakeup( &ctx->lcktmr );
  238. free( ctx->trash );
  239. free_string_list( gctx->boxes );
  240. free( gctx );
  241. }
  242. static void
  243. maildir_cleanup_drv( void )
  244. {
  245. }
  246. static void
  247. maildir_invoke_bad_callback( store_t *ctx )
  248. {
  249. ctx->bad_callback( ctx->bad_callback_aux );
  250. }
  251. static int maildir_list_inbox( store_t *gctx, int flags, const char *basePath );
  252. static int maildir_list_path( store_t *gctx, int flags, const char *inbox );
  253. static int
  254. maildir_list_recurse( store_t *gctx, int isBox, int flags,
  255. const char *inbox, int inboxLen, const char *basePath, int basePathLen,
  256. char *path, int pathLen, char *name, int nameLen )
  257. {
  258. DIR *dir;
  259. int style = ((maildir_store_conf_t *)gctx->conf)->sub_style;
  260. int pl, nl, i;
  261. struct dirent *de;
  262. struct stat st;
  263. if (!(dir = opendir( path ))) {
  264. if (isBox && (errno == ENOENT || errno == ENOTDIR))
  265. return 0;
  266. sys_error( "Maildir error: cannot list %s", path );
  267. return -1;
  268. }
  269. if (isBox > 1 && style == SUB_UNSET) {
  270. error( "Maildir error: found subfolder '%.*s', but store '%s' does not specify SubFolders style\n",
  271. nameLen - 1, name, gctx->conf->name );
  272. closedir( dir );
  273. return -1;
  274. }
  275. while ((de = readdir( dir ))) {
  276. const char *ent = de->d_name;
  277. if (ent[0] == '.' && (!ent[1] || (ent[1] == '.' && !ent[2])))
  278. continue;
  279. pl = nfsnprintf( path + pathLen, _POSIX_PATH_MAX - pathLen, "%s", ent );
  280. if (pl == 3 && (!memcmp( ent, "cur", 3 ) || !memcmp( ent, "new", 3 ) || !memcmp( ent, "tmp", 3 )))
  281. continue;
  282. pl += pathLen;
  283. if (inbox && equals( path, pl, inbox, inboxLen )) {
  284. /* Inbox nested into Path. List now if it won't be listed separately anyway. */
  285. if (!(flags & LIST_INBOX) && maildir_list_inbox( gctx, flags, 0 ) < 0) {
  286. closedir( dir );
  287. return -1;
  288. }
  289. } else if (basePath && equals( path, pl, basePath, basePathLen )) {
  290. /* Path nested into Inbox. List now if it won't be listed separately anyway. */
  291. if (!(flags & (LIST_PATH | LIST_PATH_MAYBE)) && maildir_list_path( gctx, flags, 0 ) < 0) {
  292. closedir( dir );
  293. return -1;
  294. }
  295. } else {
  296. if (style == SUB_MAILDIRPP || style == SUB_LEGACY) {
  297. if (*ent == '.') {
  298. if (!isBox)
  299. continue;
  300. ent++;
  301. } else {
  302. if (isBox)
  303. continue;
  304. }
  305. }
  306. nl = nameLen + nfsnprintf( name + nameLen, _POSIX_PATH_MAX - nameLen, "%s", ent );
  307. if (style == SUB_MAILDIRPP && isBox) {
  308. for (i = nameLen; i < nl; i++) {
  309. if (name[i] == '.')
  310. name[i] = '/';
  311. }
  312. }
  313. if (!nameLen && equals( name, nl, "INBOX", 5 ) && (!name[5] || name[5] == '/')) {
  314. path[pathLen] = 0;
  315. warn( "Maildir warning: ignoring INBOX in %s\n", path );
  316. continue;
  317. }
  318. path[pl++] = '/';
  319. nfsnprintf( path + pl, _POSIX_PATH_MAX - pl, "cur" );
  320. if (!stat( path, &st ) && S_ISDIR(st.st_mode))
  321. add_string_list( &gctx->boxes, name );
  322. if (style == SUB_MAILDIRPP && isBox) {
  323. /* Maildir++ subfolder - don't recurse further. */
  324. continue;
  325. }
  326. path[pl] = 0;
  327. name[nl++] = '/';
  328. if (maildir_list_recurse( gctx, isBox + 1, flags, inbox, inboxLen, basePath, basePathLen, path, pl, name, nl ) < 0) {
  329. closedir( dir );
  330. return -1;
  331. }
  332. }
  333. }
  334. closedir (dir);
  335. return 0;
  336. }
  337. static int
  338. maildir_list_inbox( store_t *gctx, int flags, const char *basePath )
  339. {
  340. char path[_POSIX_PATH_MAX], name[_POSIX_PATH_MAX];
  341. add_string_list( &gctx->boxes, "INBOX" );
  342. return maildir_list_recurse(
  343. gctx, 1, flags, 0, 0, basePath, basePath ? strlen( basePath ) - 1 : 0,
  344. path, nfsnprintf( path, _POSIX_PATH_MAX, "%s/", ((maildir_store_conf_t *)gctx->conf)->inbox ),
  345. name, nfsnprintf( name, _POSIX_PATH_MAX, "INBOX/" ) );
  346. }
  347. static int
  348. maildir_list_path( store_t *gctx, int flags, const char *inbox )
  349. {
  350. char path[_POSIX_PATH_MAX], name[_POSIX_PATH_MAX];
  351. if (maildir_ensure_path( (maildir_store_conf_t *)gctx->conf ) < 0)
  352. return -1;
  353. return maildir_list_recurse(
  354. gctx, 0, flags, inbox, inbox ? strlen( inbox ) : 0, 0, 0,
  355. path, nfsnprintf( path, _POSIX_PATH_MAX, "%s", gctx->conf->path ),
  356. name, 0 );
  357. }
  358. static void
  359. maildir_list_store( store_t *gctx, int flags,
  360. void (*cb)( int sts, void *aux ), void *aux )
  361. {
  362. if ((((flags & LIST_PATH) || ((flags & LIST_PATH_MAYBE) && gctx->conf->path))
  363. && maildir_list_path( gctx, flags, ((maildir_store_conf_t *)gctx->conf)->inbox ) < 0) ||
  364. ((flags & LIST_INBOX) && maildir_list_inbox( gctx, flags, gctx->conf->path ) < 0)) {
  365. maildir_invoke_bad_callback( gctx );
  366. cb( DRV_CANCELED, aux );
  367. } else {
  368. cb( DRV_OK, aux );
  369. }
  370. }
  371. static const char *subdirs[] = { "cur", "new", "tmp" };
  372. typedef struct {
  373. char *base;
  374. int size;
  375. uint uid:31, recent:1;
  376. char tuid[TUIDL];
  377. } msg_t;
  378. typedef struct {
  379. msg_t *ents;
  380. int nents, nalloc;
  381. } msglist_t;
  382. static void
  383. maildir_free_scan( msglist_t *msglist )
  384. {
  385. int i;
  386. if (msglist->ents) {
  387. for (i = 0; i < msglist->nents; i++)
  388. free( msglist->ents[i].base );
  389. free( msglist->ents );
  390. }
  391. }
  392. #define _24_HOURS (3600 * 24)
  393. static int
  394. maildir_clear_tmp( char *buf, int bufsz, int bl )
  395. {
  396. DIR *dirp;
  397. struct dirent *entry;
  398. time_t now;
  399. struct stat st;
  400. memcpy( buf + bl, "tmp/", 5 );
  401. bl += 4;
  402. if (!(dirp = opendir( buf ))) {
  403. sys_error( "Maildir error: cannot list %s", buf );
  404. return DRV_BOX_BAD;
  405. }
  406. time( &now );
  407. while ((entry = readdir( dirp ))) {
  408. nfsnprintf( buf + bl, bufsz - bl, "%s", entry->d_name );
  409. if (stat( buf, &st )) {
  410. if (errno != ENOENT)
  411. sys_error( "Maildir error: cannot access %s", buf );
  412. } else if (S_ISREG(st.st_mode) && now - st.st_ctime >= _24_HOURS) {
  413. /* This should happen infrequently enough that it won't be
  414. * bothersome to the user to display when it occurs.
  415. */
  416. notice( "Maildir notice: removing stale file %s\n", buf );
  417. if (unlink( buf ) && errno != ENOENT)
  418. sys_error( "Maildir error: cannot remove %s", buf );
  419. }
  420. }
  421. closedir( dirp );
  422. return DRV_OK;
  423. }
  424. static int
  425. make_box_dir( char *buf, int bl )
  426. {
  427. char *p;
  428. if (!mkdir( buf, 0700 ) || errno == EEXIST)
  429. return 0;
  430. p = memrchr( buf, '/', bl - 1 );
  431. *p = 0;
  432. if (make_box_dir( buf, (int)(p - buf) ))
  433. return -1;
  434. *p = '/';
  435. return mkdir( buf, 0700 );
  436. }
  437. static int
  438. maildir_validate( const char *box, int create, maildir_store_t *ctx )
  439. {
  440. int i, bl, ret;
  441. struct stat st;
  442. char buf[_POSIX_PATH_MAX];
  443. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", box );
  444. if (stat( buf, &st )) {
  445. if (errno != ENOENT) {
  446. sys_error( "Maildir error: cannot access mailbox '%s'", box );
  447. return DRV_BOX_BAD;
  448. }
  449. if (!create)
  450. return DRV_BOX_BAD;
  451. if (make_box_dir( buf, bl )) {
  452. sys_error( "Maildir error: cannot create mailbox '%s'", box );
  453. ((maildir_store_conf_t *)ctx->gen.conf)->failed = FAIL_FINAL;
  454. maildir_invoke_bad_callback( &ctx->gen );
  455. return DRV_CANCELED;
  456. }
  457. } else if (!S_ISDIR(st.st_mode)) {
  458. notdir:
  459. error( "Maildir error: '%s' is no valid mailbox\n", box );
  460. return DRV_BOX_BAD;
  461. }
  462. for (i = 0; i < 3; i++) {
  463. memcpy( buf + bl, subdirs[i], 4 );
  464. if (stat( buf, &st )) {
  465. /* We always create new/ and tmp/ if they are missing. cur/ is the presence indicator. */
  466. if (!i && !create)
  467. return DRV_BOX_BAD;
  468. if (mkdir( buf, 0700 )) {
  469. sys_error( "Maildir error: cannot create directory %s", buf );
  470. return DRV_BOX_BAD;
  471. }
  472. ctx->fresh[i] = 1;
  473. } else if (!S_ISDIR(st.st_mode)) {
  474. goto notdir;
  475. } else {
  476. if (i == 2) {
  477. if ((ret = maildir_clear_tmp( buf, sizeof(buf), bl )) != DRV_OK)
  478. return ret;
  479. }
  480. }
  481. }
  482. return DRV_OK;
  483. }
  484. #ifdef USE_DB
  485. static void
  486. make_key( const char *info_stop, DBT *tkey, char *name )
  487. {
  488. char *u = strpbrk( name, info_stop );
  489. tkey->data = name;
  490. tkey->size = u ? (size_t)(u - name) : strlen( name );
  491. }
  492. #endif /* USE_DB */
  493. static int
  494. maildir_store_uidval( maildir_store_t *ctx )
  495. {
  496. int n;
  497. #ifdef USE_DB
  498. int ret, uv[2];
  499. #endif
  500. char buf[128];
  501. #ifdef USE_DB
  502. if (ctx->db) {
  503. key.data = (void *)"UIDVALIDITY";
  504. key.size = 11;
  505. uv[0] = ctx->gen.uidvalidity;
  506. uv[1] = ctx->nuid;
  507. value.data = uv;
  508. value.size = sizeof(uv);
  509. if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 ))) {
  510. ctx->db->err( ctx->db, ret, "Maildir error: db->put()" );
  511. return DRV_BOX_BAD;
  512. }
  513. if ((ret = ctx->db->sync( ctx->db, 0 ))) {
  514. ctx->db->err( ctx->db, ret, "Maildir error: db->sync()" );
  515. return DRV_BOX_BAD;
  516. }
  517. } else
  518. #endif /* USE_DB */
  519. {
  520. n = sprintf( buf, "%d\n%d\n", ctx->gen.uidvalidity, ctx->nuid );
  521. lseek( ctx->uvfd, 0, SEEK_SET );
  522. if (write( ctx->uvfd, buf, n ) != n || ftruncate( ctx->uvfd, n ) || (UseFSync && fdatasync( ctx->uvfd ))) {
  523. error( "Maildir error: cannot write UIDVALIDITY.\n" );
  524. return DRV_BOX_BAD;
  525. }
  526. }
  527. conf_wakeup( &ctx->lcktmr, 2 );
  528. return DRV_OK;
  529. }
  530. static int
  531. maildir_init_uidval( maildir_store_t *ctx )
  532. {
  533. ctx->gen.uidvalidity = time( 0 );
  534. ctx->nuid = 0;
  535. ctx->uvok = 0;
  536. #ifdef USE_DB
  537. if (ctx->db) {
  538. u_int32_t count;
  539. ctx->db->truncate( ctx->db, 0, &count, 0 );
  540. }
  541. #endif /* USE_DB */
  542. return maildir_store_uidval( ctx );
  543. }
  544. static int
  545. maildir_init_uidval_new( maildir_store_t *ctx )
  546. {
  547. notice( "Maildir notice: no UIDVALIDITY, creating new.\n" );
  548. return maildir_init_uidval( ctx );
  549. }
  550. static int
  551. maildir_uidval_lock( maildir_store_t *ctx )
  552. {
  553. int n;
  554. #ifdef USE_DB
  555. int ret;
  556. struct stat st;
  557. #endif
  558. char buf[128];
  559. if (pending_wakeup( &ctx->lcktmr )) {
  560. /* The unlock timer is active, so we are obviously already locked. */
  561. return DRV_OK;
  562. }
  563. #ifdef LEGACY_FLOCK
  564. /* This is legacy only */
  565. if (flock( ctx->uvfd, LOCK_EX ) < 0) {
  566. error( "Maildir error: cannot flock UIDVALIDITY.\n" );
  567. return DRV_BOX_BAD;
  568. }
  569. #endif
  570. /* This (theoretically) works over NFS. Let's hope nobody else did
  571. the same in the opposite order, as we'd deadlock then. */
  572. #if SEEK_SET != 0
  573. lck.l_whence = SEEK_SET;
  574. #endif
  575. lck.l_type = F_WRLCK;
  576. if (fcntl( ctx->uvfd, F_SETLKW, &lck )) {
  577. error( "Maildir error: cannot fcntl lock UIDVALIDITY.\n" );
  578. return DRV_BOX_BAD;
  579. }
  580. #ifdef USE_DB
  581. if (ctx->usedb) {
  582. if (fstat( ctx->uvfd, &st )) {
  583. sys_error( "Maildir error: cannot fstat UID database" );
  584. return DRV_BOX_BAD;
  585. }
  586. if (db_create( &ctx->db, 0, 0 )) {
  587. fputs( "Maildir error: db_create() failed\n", stderr );
  588. return DRV_BOX_BAD;
  589. }
  590. if ((ret = (ctx->db->open)( ctx->db, 0, ctx->usedb, 0, DB_HASH,
  591. st.st_size ? 0 : DB_CREATE | DB_TRUNCATE, 0 ))) {
  592. ctx->db->err( ctx->db, ret, "Maildir error: db->open(%s)", ctx->usedb );
  593. return DRV_BOX_BAD;
  594. }
  595. key.data = (void *)"UIDVALIDITY";
  596. key.size = 11;
  597. if ((ret = ctx->db->get( ctx->db, 0, &key, &value, 0 ))) {
  598. if (ret != DB_NOTFOUND) {
  599. ctx->db->err( ctx->db, ret, "Maildir error: db->get()" );
  600. return DRV_BOX_BAD;
  601. }
  602. return maildir_init_uidval_new( ctx );
  603. }
  604. ctx->gen.uidvalidity = ((int *)value.data)[0];
  605. ctx->nuid = ((int *)value.data)[1];
  606. } else
  607. #endif
  608. {
  609. lseek( ctx->uvfd, 0, SEEK_SET );
  610. if ((n = read( ctx->uvfd, buf, sizeof(buf) - 1 )) <= 0 ||
  611. (buf[n] = 0, sscanf( buf, "%d\n%d", &ctx->gen.uidvalidity, &ctx->nuid ) != 2)) {
  612. #if 1
  613. /* In a generic driver, resetting the UID validity would be the right thing.
  614. * But this would mess up the sync state completely. So better bail out and
  615. * give the user a chance to fix the mailbox. */
  616. if (n) {
  617. error( "Maildir error: cannot read UIDVALIDITY.\n" );
  618. return DRV_BOX_BAD;
  619. }
  620. #endif
  621. return maildir_init_uidval_new( ctx );
  622. }
  623. }
  624. ctx->uvok = 1;
  625. conf_wakeup( &ctx->lcktmr, 2 );
  626. return DRV_OK;
  627. }
  628. static void
  629. maildir_uidval_unlock( maildir_store_t *ctx )
  630. {
  631. #ifdef USE_DB
  632. if (ctx->db) {
  633. ctx->db->close( ctx->db, 0 );
  634. ctx->db = 0;
  635. }
  636. #endif /* USE_DB */
  637. lck.l_type = F_UNLCK;
  638. fcntl( ctx->uvfd, F_SETLK, &lck );
  639. #ifdef LEGACY_FLOCK
  640. /* This is legacy only */
  641. flock( ctx->uvfd, LOCK_UN );
  642. #endif
  643. }
  644. static void
  645. lcktmr_timeout( void *aux )
  646. {
  647. maildir_uidval_unlock( (maildir_store_t *)aux );
  648. }
  649. static int
  650. maildir_obtain_uid( maildir_store_t *ctx, int *uid )
  651. {
  652. int ret;
  653. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  654. return ret;
  655. *uid = ++ctx->nuid;
  656. return maildir_store_uidval( ctx );
  657. }
  658. #ifdef USE_DB
  659. static int
  660. maildir_set_uid( maildir_store_t *ctx, const char *name, int *uid )
  661. {
  662. int ret;
  663. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  664. return ret;
  665. *uid = ++ctx->nuid;
  666. make_key( ((maildir_store_conf_t *)ctx->gen.conf)->info_stop, &key, (char *)name );
  667. value.data = uid;
  668. value.size = sizeof(*uid);
  669. if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 ))) {
  670. ctx->db->err( ctx->db, ret, "Maildir error: db->put()" );
  671. return DRV_BOX_BAD;
  672. }
  673. return maildir_store_uidval( ctx );
  674. }
  675. #endif
  676. static int
  677. maildir_compare( const void *l, const void *r )
  678. {
  679. msg_t *lm = (msg_t *)l, *rm = (msg_t *)r;
  680. char *ldot, *rdot, *ldot2, *rdot2, *lseq, *rseq;
  681. int ret, llen, rlen;
  682. if ((ret = lm->uid - rm->uid))
  683. return ret;
  684. /* No UID, so sort by arrival date. We should not do this, but we rely
  685. on the suggested unique file name scheme - we have no choice. */
  686. /* The first field are always the seconds. Alphabetical sort should be
  687. faster than numeric. */
  688. if (!(ldot = strchr( lm->base, '.' )) || !(rdot = strchr( rm->base, '.' )))
  689. goto stronly; /* Should never happen ... */
  690. llen = ldot - lm->base, rlen = rdot - rm->base;
  691. /* The shorter number is smaller. Really. This won't trigger with any
  692. mail created after Sep 9 2001 anyway. */
  693. if ((ret = llen - rlen))
  694. return ret;
  695. if ((ret = memcmp( lm->base, rm->base, llen )))
  696. return ret;
  697. ldot++, rdot++;
  698. if ((llen = strtol( ldot, &ldot2, 10 ))) {
  699. if (!(rlen = strtol( rdot, &rdot2, 10 )))
  700. goto stronly; /* Comparing apples to oranges ... */
  701. /* Classical PID specs */
  702. if ((ret = llen - rlen)) {
  703. retpid:
  704. /* Handle PID wraparound. This works only on systems
  705. where PIDs are not reused too fast */
  706. if (ret > 20000 || ret < -20000)
  707. ret = -ret;
  708. return ret;
  709. }
  710. return (*ldot2 != '_' ? 0 : atoi( ldot2 + 1 )) -
  711. (*rdot2 != '_' ? 0 : atoi( rdot2 + 1 ));
  712. }
  713. if (!(ldot2 = strchr( ldot, '.' )) || !(rdot2 = strchr( rdot, '.' )))
  714. goto stronly; /* Should never happen ... */
  715. llen = ldot2 - ldot, rlen = rdot2 - rdot;
  716. if (((lseq = memchr( ldot, '#', llen )) && (rseq = memchr( rdot, '#', rlen ))) ||
  717. ((lseq = memchr( ldot, 'M', llen )) && (rseq = memchr( rdot, 'M', rlen ))))
  718. return atoi( lseq + 1 ) - atoi( rseq + 1 );
  719. if ((lseq = memchr( ldot, 'P', llen )) && (rseq = memchr( rdot, 'P', rlen ))) {
  720. if ((ret = atoi( lseq + 1 ) - atoi( rseq + 1 )))
  721. goto retpid;
  722. if ((lseq = memchr( ldot, 'Q', llen )) && (rseq = memchr( rdot, 'Q', rlen )))
  723. return atoi( lseq + 1 ) - atoi( rseq + 1 );
  724. }
  725. stronly:
  726. /* Fall-back, so the sort order is defined at all */
  727. return strcmp( lm->base, rm->base );
  728. }
  729. static int
  730. maildir_scan( maildir_store_t *ctx, msglist_t *msglist )
  731. {
  732. maildir_store_conf_t *conf = (maildir_store_conf_t *)ctx->gen.conf;
  733. DIR *d;
  734. FILE *f;
  735. struct dirent *e;
  736. const char *u, *ru;
  737. #ifdef USE_DB
  738. DB *tdb;
  739. DBC *dbc;
  740. #endif /* USE_DB */
  741. msg_t *entry;
  742. int i, j, uid, bl, fnl, ret;
  743. time_t now, stamps[2];
  744. struct stat st;
  745. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  746. again:
  747. msglist->ents = 0;
  748. msglist->nents = msglist->nalloc = 0;
  749. ctx->gen.count = ctx->gen.recent = 0;
  750. if (ctx->uvok || ctx->maxuid == INT_MAX) {
  751. #ifdef USE_DB
  752. if (ctx->usedb) {
  753. if (db_create( &tdb, 0, 0 )) {
  754. fputs( "Maildir error: db_create() failed\n", stderr );
  755. return DRV_BOX_BAD;
  756. }
  757. if ((tdb->open)( tdb, 0, 0, 0, DB_HASH, DB_CREATE, 0 )) {
  758. fputs( "Maildir error: tdb->open() failed\n", stderr );
  759. bork:
  760. tdb->close( tdb, 0 );
  761. return DRV_BOX_BAD;
  762. }
  763. }
  764. #endif /* USE_DB */
  765. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", ctx->gen.path );
  766. restat:
  767. now = time( 0 );
  768. for (i = 0; i < 2; i++) {
  769. memcpy( buf + bl, subdirs[i], 4 );
  770. if (stat( buf, &st )) {
  771. sys_error( "Maildir error: cannot stat %s", buf );
  772. goto dfail;
  773. }
  774. if (st.st_mtime == now && !(DFlags & ZERODELAY) && !ctx->fresh[i]) {
  775. /* If the modification happened during this second, we wouldn't be able to
  776. * tell if there were further modifications during this second. So wait.
  777. * This has the nice side effect that we wait for "batches" of changes to
  778. * complete. On the downside, it can potentially block indefinitely. */
  779. notice( "Maildir notice: sleeping due to recent directory modification.\n" );
  780. sleep( 1 ); /* FIXME: should make this async */
  781. goto restat;
  782. }
  783. stamps[i] = st.st_mtime;
  784. }
  785. for (i = 0; i < 2; i++) {
  786. memcpy( buf + bl, subdirs[i], 4 );
  787. if (!(d = opendir( buf ))) {
  788. sys_error( "Maildir error: cannot list %s", buf );
  789. rfail:
  790. maildir_free_scan( msglist );
  791. dfail:
  792. #ifdef USE_DB
  793. if (ctx->usedb)
  794. tdb->close( tdb, 0 );
  795. #endif /* USE_DB */
  796. return DRV_BOX_BAD;
  797. }
  798. while ((e = readdir( d ))) {
  799. if (*e->d_name == '.')
  800. continue;
  801. ctx->gen.count++;
  802. ctx->gen.recent += i;
  803. #ifdef USE_DB
  804. if (ctx->usedb) {
  805. if (maildir_uidval_lock( ctx ) != DRV_OK)
  806. goto mbork;
  807. make_key( conf->info_stop, &key, e->d_name );
  808. if ((ret = ctx->db->get( ctx->db, 0, &key, &value, 0 ))) {
  809. if (ret != DB_NOTFOUND) {
  810. ctx->db->err( ctx->db, ret, "Maildir error: db->get()" );
  811. mbork:
  812. maildir_free_scan( msglist );
  813. closedir( d );
  814. goto bork;
  815. }
  816. uid = INT_MAX;
  817. } else {
  818. value.size = 0;
  819. if ((ret = tdb->put( tdb, 0, &key, &value, 0 ))) {
  820. tdb->err( tdb, ret, "Maildir error: tdb->put()" );
  821. goto mbork;
  822. }
  823. uid = *(int *)value.data;
  824. }
  825. } else
  826. #endif /* USE_DB */
  827. {
  828. uid = (ctx->uvok && (u = strstr( e->d_name, ",U=" ))) ? atoi( u + 3 ) : 0;
  829. if (!uid)
  830. uid = INT_MAX;
  831. }
  832. if (uid <= ctx->maxuid) {
  833. if (uid < ctx->minuid) {
  834. for (j = 0; j < ctx->nexcs; j++)
  835. if (ctx->excs[j] == uid)
  836. goto oke;
  837. continue;
  838. oke: ;
  839. }
  840. if (msglist->nalloc == msglist->nents) {
  841. msglist->nalloc = msglist->nalloc * 2 + 100;
  842. msglist->ents = nfrealloc( msglist->ents, msglist->nalloc * sizeof(msg_t) );
  843. }
  844. entry = &msglist->ents[msglist->nents++];
  845. entry->base = nfstrdup( e->d_name );
  846. entry->uid = uid;
  847. entry->recent = i;
  848. entry->size = 0;
  849. entry->tuid[0] = 0;
  850. }
  851. }
  852. closedir( d );
  853. }
  854. for (i = 0; i < 2; i++) {
  855. memcpy( buf + bl, subdirs[i], 4 );
  856. if (stat( buf, &st )) {
  857. sys_error( "Maildir error: cannot re-stat %s", buf );
  858. goto rfail;
  859. }
  860. if (st.st_mtime != stamps[i]) {
  861. /* Somebody messed with the mailbox since we started listing it. */
  862. #ifdef USE_DB
  863. if (ctx->usedb)
  864. tdb->close( tdb, 0 );
  865. #endif /* USE_DB */
  866. maildir_free_scan( msglist );
  867. goto again;
  868. }
  869. }
  870. #ifdef USE_DB
  871. if (ctx->usedb) {
  872. if (maildir_uidval_lock( ctx ) != DRV_OK)
  873. ;
  874. else if ((ret = ctx->db->cursor( ctx->db, 0, &dbc, 0 )))
  875. ctx->db->err( ctx->db, ret, "Maildir error: db->cursor()" );
  876. else {
  877. for (;;) {
  878. if ((ret = dbc->c_get( dbc, &key, &value, DB_NEXT ))) {
  879. if (ret != DB_NOTFOUND)
  880. ctx->db->err( ctx->db, ret, "Maildir error: db->c_get()" );
  881. break;
  882. }
  883. if (!equals( key.data, key.size, "UIDVALIDITY", 11 ) &&
  884. (ret = tdb->get( tdb, 0, &key, &value, 0 ))) {
  885. if (ret != DB_NOTFOUND) {
  886. tdb->err( tdb, ret, "Maildir error: tdb->get()" );
  887. break;
  888. }
  889. if ((ret = dbc->c_del( dbc, 0 ))) {
  890. ctx->db->err( ctx->db, ret, "Maildir error: db->c_del()" );
  891. break;
  892. }
  893. }
  894. }
  895. dbc->c_close( dbc );
  896. }
  897. tdb->close( tdb, 0 );
  898. }
  899. #endif /* USE_DB */
  900. qsort( msglist->ents, msglist->nents, sizeof(msg_t), maildir_compare );
  901. for (uid = i = 0; i < msglist->nents; i++) {
  902. entry = &msglist->ents[i];
  903. if (entry->uid != INT_MAX) {
  904. if (uid == entry->uid) {
  905. #if 1
  906. /* See comment in maildir_uidval_lock() why this is fatal. */
  907. error( "Maildir error: duplicate UID %d.\n", uid );
  908. maildir_free_scan( msglist );
  909. return DRV_BOX_BAD;
  910. #else
  911. notice( "Maildir notice: duplicate UID; changing UIDVALIDITY.\n");
  912. if ((ret = maildir_init_uid( ctx )) != DRV_OK) {
  913. maildir_free_scan( msglist );
  914. return ret;
  915. }
  916. maildir_free_scan( msglist );
  917. goto again;
  918. #endif
  919. }
  920. uid = entry->uid;
  921. if (uid > ctx->nuid) {
  922. /* In principle, we could just warn and top up nuid. However, getting into this
  923. * situation might indicate some serious trouble, so let's not make it worse. */
  924. error( "Maildir error: UID %d is beyond highest assigned UID %d.\n", uid, ctx->nuid );
  925. maildir_free_scan( msglist );
  926. return DRV_BOX_BAD;
  927. }
  928. if ((ctx->gen.opts & OPEN_SIZE) || ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid))
  929. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s", subdirs[entry->recent], entry->base );
  930. #ifdef USE_DB
  931. } else if (ctx->usedb) {
  932. if ((ret = maildir_set_uid( ctx, entry->base, &uid )) != DRV_OK) {
  933. maildir_free_scan( msglist );
  934. return ret;
  935. }
  936. entry->uid = uid;
  937. if ((ctx->gen.opts & OPEN_SIZE) || ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid))
  938. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s", subdirs[entry->recent], entry->base );
  939. #endif /* USE_DB */
  940. } else {
  941. if ((ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK) {
  942. maildir_free_scan( msglist );
  943. return ret;
  944. }
  945. entry->uid = uid;
  946. if ((u = strstr( entry->base, ",U=" )))
  947. for (ru = u + 3; isdigit( (uchar)*ru ); ru++);
  948. else
  949. u = ru = strchr( entry->base, conf->info_delimiter );
  950. fnl = (u ?
  951. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%.*s,U=%d%s", subdirs[entry->recent], (int)(u - entry->base), entry->base, uid, ru ) :
  952. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s,U=%d", subdirs[entry->recent], entry->base, uid ))
  953. + 1 - 4;
  954. memcpy( nbuf, buf, bl + 4 );
  955. nfsnprintf( nbuf + bl + 4, sizeof(nbuf) - bl - 4, "%s", entry->base );
  956. if (rename( nbuf, buf )) {
  957. if (errno != ENOENT) {
  958. sys_error( "Maildir error: cannot rename %s to %s", nbuf, buf );
  959. fail:
  960. maildir_free_scan( msglist );
  961. return DRV_BOX_BAD;
  962. }
  963. retry:
  964. maildir_free_scan( msglist );
  965. goto again;
  966. }
  967. free( entry->base );
  968. entry->base = nfmalloc( fnl );
  969. memcpy( entry->base, buf + bl + 4, fnl );
  970. }
  971. if (ctx->gen.opts & OPEN_SIZE) {
  972. if (stat( buf, &st )) {
  973. if (errno != ENOENT) {
  974. sys_error( "Maildir error: cannot stat %s", buf );
  975. goto fail;
  976. }
  977. goto retry;
  978. }
  979. entry->size = st.st_size;
  980. }
  981. if ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid) {
  982. if (!(f = fopen( buf, "r" ))) {
  983. if (errno != ENOENT) {
  984. sys_error( "Maildir error: cannot open %s", buf );
  985. goto fail;
  986. }
  987. goto retry;
  988. }
  989. while (fgets( nbuf, sizeof(nbuf), f )) {
  990. if (!nbuf[0] || nbuf[0] == '\n')
  991. break;
  992. if (starts_with( nbuf, -1, "X-TUID: ", 8 ) && nbuf[8 + TUIDL] == '\n') {
  993. memcpy( entry->tuid, nbuf + 8, TUIDL );
  994. break;
  995. }
  996. }
  997. fclose( f );
  998. }
  999. }
  1000. ctx->uvok = 1;
  1001. }
  1002. return DRV_OK;
  1003. }
  1004. static void
  1005. maildir_init_msg( maildir_store_t *ctx, maildir_message_t *msg, msg_t *entry )
  1006. {
  1007. msg->base = entry->base;
  1008. entry->base = 0; /* prevent deletion */
  1009. msg->gen.size = entry->size;
  1010. msg->gen.srec = 0;
  1011. strncpy( msg->gen.tuid, entry->tuid, TUIDL );
  1012. if (entry->recent)
  1013. msg->gen.status |= M_RECENT;
  1014. if (ctx->gen.opts & OPEN_FLAGS) {
  1015. msg->gen.status |= M_FLAGS;
  1016. msg->gen.flags = maildir_parse_flags( ((maildir_store_conf_t *)ctx->gen.conf)->info_prefix, msg->base );
  1017. } else
  1018. msg->gen.flags = 0;
  1019. }
  1020. static void
  1021. maildir_app_msg( maildir_store_t *ctx, message_t ***msgapp, msg_t *entry )
  1022. {
  1023. maildir_message_t *msg = nfmalloc( sizeof(*msg) );
  1024. msg->gen.next = **msgapp;
  1025. **msgapp = &msg->gen;
  1026. *msgapp = &msg->gen.next;
  1027. msg->gen.uid = entry->uid;
  1028. msg->gen.status = 0;
  1029. maildir_init_msg( ctx, msg, entry );
  1030. }
  1031. static int
  1032. maildir_select_box( store_t *gctx, const char *name )
  1033. {
  1034. maildir_store_conf_t *conf = (maildir_store_conf_t *)gctx->conf;
  1035. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1036. maildir_cleanup( gctx );
  1037. gctx->msgs = 0;
  1038. ctx->excs = 0;
  1039. ctx->uvfd = -1;
  1040. #ifdef USE_DB
  1041. ctx->db = 0;
  1042. ctx->usedb = 0;
  1043. #endif /* USE_DB */
  1044. ctx->fresh[0] = ctx->fresh[1] = 0;
  1045. if (starts_with( name, -1, "INBOX", 5 ) && (!name[5] || name[5] == '/')) {
  1046. gctx->path = maildir_join_path( conf, conf->inbox, name + 5 );
  1047. ctx->is_inbox = !name[5];
  1048. } else {
  1049. if (maildir_ensure_path( conf ) < 0) {
  1050. gctx->path = 0;
  1051. return DRV_CANCELED;
  1052. }
  1053. gctx->path = maildir_join_path( conf, conf->gen.path, name );
  1054. ctx->is_inbox = 0;
  1055. }
  1056. return gctx->path ? DRV_OK : DRV_BOX_BAD;
  1057. }
  1058. static void
  1059. maildir_open_box( store_t *gctx,
  1060. void (*cb)( int sts, void *aux ), void *aux )
  1061. {
  1062. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1063. int ret;
  1064. char uvpath[_POSIX_PATH_MAX];
  1065. if ((ret = maildir_validate( gctx->path, ctx->is_inbox, ctx )) != DRV_OK)
  1066. goto bail;
  1067. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", gctx->path );
  1068. #ifndef USE_DB
  1069. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) < 0) {
  1070. sys_error( "Maildir error: cannot write %s", uvpath );
  1071. cb( DRV_BOX_BAD, aux );
  1072. return;
  1073. }
  1074. #else
  1075. ctx->usedb = 0;
  1076. if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
  1077. nfsnprintf( uvpath, sizeof(uvpath), "%s/.isyncuidmap.db", gctx->path );
  1078. if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
  1079. if (((maildir_store_conf_t *)gctx->conf)->alt_map) {
  1080. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
  1081. goto dbok;
  1082. } else {
  1083. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", gctx->path );
  1084. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
  1085. goto fnok;
  1086. }
  1087. sys_error( "Maildir error: cannot write %s", uvpath );
  1088. cb( DRV_BOX_BAD, aux );
  1089. return;
  1090. } else {
  1091. dbok:
  1092. ctx->usedb = nfstrdup( uvpath );
  1093. }
  1094. }
  1095. fnok:
  1096. #endif /* USE_DB */
  1097. ret = maildir_uidval_lock( ctx );
  1098. bail:
  1099. cb( ret, aux );
  1100. }
  1101. static void
  1102. maildir_create_box( store_t *gctx,
  1103. void (*cb)( int sts, void *aux ), void *aux )
  1104. {
  1105. cb( maildir_validate( gctx->path, 1, (maildir_store_t *)gctx ), aux );
  1106. }
  1107. static int
  1108. maildir_confirm_box_empty( store_t *gctx )
  1109. {
  1110. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1111. msglist_t msglist;
  1112. ctx->nexcs = ctx->minuid = ctx->maxuid = ctx->newuid = 0;
  1113. if (maildir_scan( ctx, &msglist ) != DRV_OK)
  1114. return DRV_BOX_BAD;
  1115. maildir_free_scan( &msglist );
  1116. return gctx->count ? DRV_BOX_BAD : DRV_OK;
  1117. }
  1118. static void
  1119. maildir_delete_box( store_t *gctx,
  1120. void (*cb)( int sts, void *aux ), void *aux )
  1121. {
  1122. int i, bl, ret = DRV_OK;
  1123. struct stat st;
  1124. char buf[_POSIX_PATH_MAX];
  1125. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", gctx->path );
  1126. if (stat( buf, &st )) {
  1127. if (errno != ENOENT) {
  1128. sys_error( "Maildir error: cannot access mailbox '%s'", gctx->path );
  1129. ret = DRV_BOX_BAD;
  1130. }
  1131. } else if (!S_ISDIR(st.st_mode)) {
  1132. error( "Maildir error: '%s' is no valid mailbox\n", gctx->path );
  1133. ret = DRV_BOX_BAD;
  1134. } else if ((ret = maildir_clear_tmp( buf, sizeof(buf), bl )) == DRV_OK) {
  1135. nfsnprintf( buf + bl, sizeof(buf) - bl, ".uidvalidity" );
  1136. if (unlink( buf ) && errno != ENOENT)
  1137. goto badrm;
  1138. #ifdef USE_DB
  1139. nfsnprintf( buf + bl, sizeof(buf) - bl, ".isyncuidmap.db" );
  1140. if (unlink( buf ) && errno != ENOENT)
  1141. goto badrm;
  1142. #endif
  1143. /* We delete cur/ last, as it is the indicator for a present mailbox.
  1144. * That way an interrupted operation can be resumed. */
  1145. for (i = 3; --i >= 0; ) {
  1146. memcpy( buf + bl, subdirs[i], 4 );
  1147. if (rmdir( buf ) && errno != ENOENT) {
  1148. badrm:
  1149. sys_error( "Maildir error: cannot remove '%s'", buf );
  1150. ret = DRV_BOX_BAD;
  1151. break;
  1152. }
  1153. }
  1154. }
  1155. cb( ret, aux );
  1156. }
  1157. static int
  1158. maildir_finish_delete_box( store_t *gctx )
  1159. {
  1160. /* Subfolders are not deleted; the deleted folder is only "stripped of its mailboxness".
  1161. * Consequently, the rmdir may legitimately fail. This behavior follows the IMAP spec. */
  1162. if (rmdir( gctx->path ) && errno != ENOENT && errno != ENOTEMPTY) {
  1163. sys_error( "Maildir warning: cannot remove '%s'", gctx->path );
  1164. return DRV_BOX_BAD;
  1165. }
  1166. return DRV_OK;
  1167. }
  1168. static void
  1169. maildir_prepare_load_box( store_t *gctx, int opts )
  1170. {
  1171. if (opts & OPEN_SETFLAGS)
  1172. opts |= OPEN_OLD;
  1173. if (opts & OPEN_EXPUNGE)
  1174. opts |= OPEN_OLD|OPEN_NEW|OPEN_FLAGS;
  1175. gctx->opts = opts;
  1176. }
  1177. static void
  1178. maildir_load_box( store_t *gctx, int minuid, int maxuid, int newuid, int *excs, int nexcs,
  1179. void (*cb)( int sts, void *aux ), void *aux )
  1180. {
  1181. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1182. message_t **msgapp;
  1183. msglist_t msglist;
  1184. int i;
  1185. ctx->minuid = minuid;
  1186. ctx->maxuid = maxuid;
  1187. ctx->newuid = newuid;
  1188. ctx->excs = nfrealloc( excs, nexcs * sizeof(int) );
  1189. ctx->nexcs = nexcs;
  1190. if (maildir_scan( ctx, &msglist ) != DRV_OK) {
  1191. cb( DRV_BOX_BAD, aux );
  1192. return;
  1193. }
  1194. msgapp = &ctx->gen.msgs;
  1195. for (i = 0; i < msglist.nents; i++)
  1196. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  1197. maildir_free_scan( &msglist );
  1198. cb( DRV_OK, aux );
  1199. }
  1200. static int
  1201. maildir_rescan( maildir_store_t *ctx )
  1202. {
  1203. message_t **msgapp;
  1204. maildir_message_t *msg;
  1205. msglist_t msglist;
  1206. int i;
  1207. ctx->fresh[0] = ctx->fresh[1] = 0;
  1208. if (maildir_scan( ctx, &msglist ) != DRV_OK)
  1209. return DRV_BOX_BAD;
  1210. for (msgapp = &ctx->gen.msgs, i = 0;
  1211. (msg = (maildir_message_t *)*msgapp) || i < msglist.nents; )
  1212. {
  1213. if (!msg) {
  1214. #if 0
  1215. debug( "adding new message %d\n", msglist.ents[i].uid );
  1216. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  1217. #else
  1218. debug( "ignoring new message %d\n", msglist.ents[i].uid );
  1219. #endif
  1220. i++;
  1221. } else if (i >= msglist.nents) {
  1222. debug( "purging deleted message %d\n", msg->gen.uid );
  1223. msg->gen.status = M_DEAD;
  1224. msgapp = &msg->gen.next;
  1225. } else if (msglist.ents[i].uid < msg->gen.uid) {
  1226. /* this should not happen, actually */
  1227. #if 0
  1228. debug( "adding new message %d\n", msglist.ents[i].uid );
  1229. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  1230. #else
  1231. debug( "ignoring new message %d\n", msglist.ents[i].uid );
  1232. #endif
  1233. i++;
  1234. } else if (msglist.ents[i].uid > msg->gen.uid) {
  1235. debug( "purging deleted message %d\n", msg->gen.uid );
  1236. msg->gen.status = M_DEAD;
  1237. msgapp = &msg->gen.next;
  1238. } else {
  1239. debug( "updating message %d\n", msg->gen.uid );
  1240. msg->gen.status &= ~(M_FLAGS|M_RECENT);
  1241. free( msg->base );
  1242. maildir_init_msg( ctx, msg, msglist.ents + i );
  1243. i++, msgapp = &msg->gen.next;
  1244. }
  1245. }
  1246. maildir_free_scan( &msglist );
  1247. return DRV_OK;
  1248. }
  1249. static int
  1250. maildir_again( maildir_store_t *ctx, maildir_message_t *msg,
  1251. const char *err, const char *fn, const char *fn2 )
  1252. {
  1253. int ret;
  1254. if (errno != ENOENT) {
  1255. sys_error( err, fn, fn2 );
  1256. return DRV_BOX_BAD;
  1257. }
  1258. if ((ret = maildir_rescan( ctx )) != DRV_OK)
  1259. return ret;
  1260. return (msg->gen.status & M_DEAD) ? DRV_MSG_BAD : DRV_OK;
  1261. }
  1262. static void
  1263. maildir_fetch_msg( store_t *gctx, message_t *gmsg, msg_data_t *data,
  1264. void (*cb)( int sts, void *aux ), void *aux )
  1265. {
  1266. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1267. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1268. int fd, ret;
  1269. struct stat st;
  1270. char buf[_POSIX_PATH_MAX];
  1271. for (;;) {
  1272. nfsnprintf( buf, sizeof(buf), "%s/%s/%s", gctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
  1273. if ((fd = open( buf, O_RDONLY )) >= 0)
  1274. break;
  1275. if ((ret = maildir_again( ctx, msg, "Cannot open %s", buf, 0 )) != DRV_OK) {
  1276. cb( ret, aux );
  1277. return;
  1278. }
  1279. }
  1280. fstat( fd, &st );
  1281. data->len = st.st_size;
  1282. if (data->date == -1)
  1283. data->date = st.st_mtime;
  1284. data->data = nfmalloc( data->len );
  1285. if (read( fd, data->data, data->len ) != data->len) {
  1286. sys_error( "Maildir error: cannot read %s", buf );
  1287. close( fd );
  1288. cb( DRV_MSG_BAD, aux );
  1289. return;
  1290. }
  1291. close( fd );
  1292. if (!(gmsg->status & M_FLAGS))
  1293. data->flags = maildir_parse_flags( ((maildir_store_conf_t *)gctx->conf)->info_prefix, msg->base );
  1294. cb( DRV_OK, aux );
  1295. }
  1296. static int
  1297. maildir_make_flags( char info_delimiter, int flags, char *buf )
  1298. {
  1299. uint i, d;
  1300. buf[0] = info_delimiter;
  1301. buf[1] = '2';
  1302. buf[2] = ',';
  1303. for (d = 3, i = 0; i < as(Flags); i++)
  1304. if (flags & (1 << i))
  1305. buf[d++] = Flags[i];
  1306. buf[d] = 0;
  1307. return d;
  1308. }
  1309. static void
  1310. maildir_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
  1311. void (*cb)( int sts, int uid, void *aux ), void *aux )
  1312. {
  1313. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1314. const char *box;
  1315. int ret, fd, bl, uid;
  1316. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX], fbuf[NUM_FLAGS + 3], base[128];
  1317. bl = nfsnprintf( base, sizeof(base), "%ld.%d_%d.%s", (long)time( 0 ), Pid, ++MaildirCount, Hostname );
  1318. if (!to_trash) {
  1319. #ifdef USE_DB
  1320. if (ctx->usedb) {
  1321. if ((ret = maildir_set_uid( ctx, base, &uid )) != DRV_OK) {
  1322. free( data->data );
  1323. cb( ret, 0, aux );
  1324. return;
  1325. }
  1326. } else
  1327. #endif /* USE_DB */
  1328. {
  1329. if ((ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK) {
  1330. free( data->data );
  1331. cb( ret, 0, aux );
  1332. return;
  1333. }
  1334. nfsnprintf( base + bl, sizeof(base) - bl, ",U=%d", uid );
  1335. }
  1336. box = gctx->path;
  1337. } else {
  1338. uid = 0;
  1339. box = ctx->trash;
  1340. }
  1341. maildir_make_flags( ((maildir_store_conf_t *)gctx->conf)->info_delimiter, data->flags, fbuf );
  1342. nfsnprintf( buf, sizeof(buf), "%s/tmp/%s%s", box, base, fbuf );
  1343. if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
  1344. if (errno != ENOENT || !to_trash) {
  1345. sys_error( "Maildir error: cannot create %s", buf );
  1346. free( data->data );
  1347. cb( DRV_BOX_BAD, 0, aux );
  1348. return;
  1349. }
  1350. if ((ret = maildir_validate( box, 1, ctx )) != DRV_OK) {
  1351. free( data->data );
  1352. cb( ret, 0, aux );
  1353. return;
  1354. }
  1355. if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
  1356. sys_error( "Maildir error: cannot create %s", buf );
  1357. free( data->data );
  1358. cb( DRV_BOX_BAD, 0, aux );
  1359. return;
  1360. }
  1361. }
  1362. ret = write( fd, data->data, data->len );
  1363. free( data->data );
  1364. if (ret != data->len || (UseFSync && (ret = fsync( fd )))) {
  1365. if (ret < 0)
  1366. sys_error( "Maildir error: cannot write %s", buf );
  1367. else
  1368. error( "Maildir error: cannot write %s. Disk full?\n", buf );
  1369. close( fd );
  1370. cb( DRV_BOX_BAD, 0, aux );
  1371. return;
  1372. }
  1373. if (close( fd ) < 0) {
  1374. /* Quota exceeded may cause this. */
  1375. sys_error( "Maildir error: cannot write %s", buf );
  1376. cb( DRV_BOX_BAD, 0, aux );
  1377. return;
  1378. }
  1379. if (data->date) {
  1380. /* Set atime and mtime according to INTERNALDATE or mtime of source message */
  1381. struct utimbuf utimebuf;
  1382. utimebuf.actime = utimebuf.modtime = data->date;
  1383. if (utime( buf, &utimebuf ) < 0) {
  1384. sys_error( "Maildir error: cannot set times for %s", buf );
  1385. cb( DRV_BOX_BAD, 0, aux );
  1386. return;
  1387. }
  1388. }
  1389. /* Moving seen messages to cur/ is strictly speaking incorrect, but makes mutt happy. */
  1390. nfsnprintf( nbuf, sizeof(nbuf), "%s/%s/%s%s", box, subdirs[!(data->flags & F_SEEN)], base, fbuf );
  1391. if (rename( buf, nbuf )) {
  1392. sys_error( "Maildir error: cannot rename %s to %s", buf, nbuf );
  1393. cb( DRV_BOX_BAD, 0, aux );
  1394. return;
  1395. }
  1396. cb( DRV_OK, uid, aux );
  1397. }
  1398. static void
  1399. maildir_find_new_msgs( store_t *gctx ATTR_UNUSED, int newuid ATTR_UNUSED,
  1400. void (*cb)( int sts, void *aux ) ATTR_UNUSED, void *aux ATTR_UNUSED )
  1401. {
  1402. assert( !"maildir_find_new_msgs is not supposed to be called" );
  1403. }
  1404. static void
  1405. maildir_set_msg_flags( store_t *gctx, message_t *gmsg, int uid ATTR_UNUSED, int add, int del,
  1406. void (*cb)( int sts, void *aux ), void *aux )
  1407. {
  1408. maildir_store_conf_t *conf = (maildir_store_conf_t *)gctx->conf;
  1409. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1410. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1411. char *s, *p;
  1412. uint i;
  1413. int j, ret, ol, fl, bbl, bl, tl;
  1414. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  1415. bbl = nfsnprintf( buf, sizeof(buf), "%s/", gctx->path );
  1416. memcpy( nbuf, gctx->path, bbl - 1 );
  1417. memcpy( nbuf + bbl - 1, "/cur/", 5 );
  1418. for (;;) {
  1419. bl = bbl + nfsnprintf( buf + bbl, sizeof(buf) - bbl, "%s/", subdirs[gmsg->status & M_RECENT] );
  1420. ol = strlen( msg->base );
  1421. if ((int)sizeof(buf) - bl < ol + 3 + NUM_FLAGS)
  1422. oob();
  1423. memcpy( buf + bl, msg->base, ol + 1 );
  1424. memcpy( nbuf + bl, msg->base, ol + 1 );
  1425. if ((s = strstr( nbuf + bl, conf->info_prefix ))) {
  1426. s += 3;
  1427. fl = ol - (s - (nbuf + bl));
  1428. for (i = 0; i < as(Flags); i++) {
  1429. if ((p = strchr( s, Flags[i] ))) {
  1430. if (del & (1 << i)) {
  1431. memmove( p, p + 1, fl - (p - s) );
  1432. fl--;
  1433. }
  1434. } else if (add & (1 << i)) {
  1435. for (j = 0; j < fl && Flags[i] > s[j]; j++);
  1436. fl++;
  1437. memmove( s + j + 1, s + j, fl - j );
  1438. s[j] = Flags[i];
  1439. }
  1440. }
  1441. tl = ol + 3 + fl;
  1442. } else {
  1443. tl = ol + maildir_make_flags( conf->info_delimiter, msg->gen.flags, nbuf + bl + ol );
  1444. }
  1445. if (!rename( buf, nbuf ))
  1446. break;
  1447. if ((ret = maildir_again( ctx, msg, "Maildir error: cannot rename %s to %s", buf, nbuf )) != DRV_OK) {
  1448. cb( ret, aux );
  1449. return;
  1450. }
  1451. }
  1452. free( msg->base );
  1453. msg->base = nfmalloc( tl + 1 );
  1454. memcpy( msg->base, nbuf + bl, tl + 1 );
  1455. msg->gen.flags |= add;
  1456. msg->gen.flags &= ~del;
  1457. gmsg->status &= ~M_RECENT;
  1458. cb( DRV_OK, aux );
  1459. }
  1460. #ifdef USE_DB
  1461. static int
  1462. maildir_purge_msg( maildir_store_t *ctx, const char *name )
  1463. {
  1464. int ret;
  1465. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  1466. return ret;
  1467. make_key( ((maildir_store_conf_t *)ctx->gen.conf)->info_stop, &key, (char *)name );
  1468. if ((ret = ctx->db->del( ctx->db, 0, &key, 0 ))) {
  1469. ctx->db->err( ctx->db, ret, "Maildir error: db->del()" );
  1470. return DRV_BOX_BAD;
  1471. }
  1472. return DRV_OK;
  1473. }
  1474. #endif /* USE_DB */
  1475. static void
  1476. maildir_trash_msg( store_t *gctx, message_t *gmsg,
  1477. void (*cb)( int sts, void *aux ), void *aux )
  1478. {
  1479. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1480. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1481. char *s;
  1482. int ret;
  1483. struct stat st;
  1484. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  1485. for (;;) {
  1486. nfsnprintf( buf, sizeof(buf), "%s/%s/%s", gctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
  1487. s = strstr( msg->base, ((maildir_store_conf_t *)gctx->conf)->info_prefix );
  1488. nfsnprintf( nbuf, sizeof(nbuf), "%s/%s/%ld.%d_%d.%s%s", ctx->trash,
  1489. subdirs[gmsg->status & M_RECENT], (long)time( 0 ), Pid, ++MaildirCount, Hostname, s ? s : "" );
  1490. if (!rename( buf, nbuf ))
  1491. break;
  1492. if (!stat( buf, &st )) {
  1493. if ((ret = maildir_validate( ctx->trash, 1, ctx )) != DRV_OK) {
  1494. cb( ret, aux );
  1495. return;
  1496. }
  1497. if (!rename( buf, nbuf ))
  1498. break;
  1499. if (errno != ENOENT) {
  1500. sys_error( "Maildir error: cannot move %s to %s", buf, nbuf );
  1501. cb( DRV_BOX_BAD, aux );
  1502. return;
  1503. }
  1504. }
  1505. if ((ret = maildir_again( ctx, msg, "Maildir error: cannot move %s to %s", buf, nbuf )) != DRV_OK) {
  1506. cb( ret, aux );
  1507. return;
  1508. }
  1509. }
  1510. gmsg->status |= M_DEAD;
  1511. gctx->count--;
  1512. #ifdef USE_DB
  1513. if (ctx->usedb) {
  1514. cb( maildir_purge_msg( ctx, msg->base ), aux );
  1515. return;
  1516. }
  1517. #endif /* USE_DB */
  1518. cb( DRV_OK, aux );
  1519. }
  1520. static void
  1521. maildir_close_box( store_t *gctx,
  1522. void (*cb)( int sts, void *aux ), void *aux )
  1523. {
  1524. #ifdef USE_DB
  1525. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1526. #endif /* USE_DB */
  1527. message_t *msg;
  1528. int basel, retry, ret;
  1529. char buf[_POSIX_PATH_MAX];
  1530. for (;;) {
  1531. retry = 0;
  1532. basel = nfsnprintf( buf, sizeof(buf), "%s/", gctx->path );
  1533. for (msg = gctx->msgs; msg; msg = msg->next)
  1534. if (!(msg->status & M_DEAD) && (msg->flags & F_DELETED)) {
  1535. nfsnprintf( buf + basel, sizeof(buf) - basel, "%s/%s", subdirs[msg->status & M_RECENT], ((maildir_message_t *)msg)->base );
  1536. if (unlink( buf )) {
  1537. if (errno == ENOENT)
  1538. retry = 1;
  1539. else
  1540. sys_error( "Maildir error: cannot remove %s", buf );
  1541. } else {
  1542. msg->status |= M_DEAD;
  1543. gctx->count--;
  1544. #ifdef USE_DB
  1545. if (ctx->db && (ret = maildir_purge_msg( ctx, ((maildir_message_t *)msg)->base )) != DRV_OK) {
  1546. cb( ret, aux );
  1547. return;
  1548. }
  1549. #endif /* USE_DB */
  1550. }
  1551. }
  1552. if (!retry) {
  1553. cb( DRV_OK, aux );
  1554. return;
  1555. }
  1556. if ((ret = maildir_rescan( (maildir_store_t *)gctx )) != DRV_OK) {
  1557. cb( ret, aux );
  1558. return;
  1559. }
  1560. }
  1561. }
  1562. static void
  1563. maildir_cancel_cmds( store_t *gctx ATTR_UNUSED,
  1564. void (*cb)( void *aux ), void *aux )
  1565. {
  1566. cb( aux );
  1567. }
  1568. static void
  1569. maildir_commit_cmds( store_t *gctx )
  1570. {
  1571. (void) gctx;
  1572. }
  1573. static int
  1574. maildir_memory_usage( store_t *gctx ATTR_UNUSED )
  1575. {
  1576. return 0;
  1577. }
  1578. static int
  1579. maildir_fail_state( store_conf_t *gconf )
  1580. {
  1581. return ((maildir_store_conf_t *)gconf)->failed;
  1582. }
  1583. static int
  1584. maildir_parse_store( conffile_t *cfg, store_conf_t **storep )
  1585. {
  1586. maildir_store_conf_t *store;
  1587. if (strcasecmp( "MaildirStore", cfg->cmd ))
  1588. return 0;
  1589. store = nfcalloc( sizeof(*store) );
  1590. store->info_delimiter = FieldDelimiter;
  1591. store->gen.driver = &maildir_driver;
  1592. store->gen.name = nfstrdup( cfg->val );
  1593. while (getcline( cfg ) && cfg->cmd)
  1594. if (!strcasecmp( "Inbox", cfg->cmd ))
  1595. store->inbox = expand_strdup( cfg->val );
  1596. else if (!strcasecmp( "Path", cfg->cmd ))
  1597. store->gen.path = expand_strdup( cfg->val );
  1598. #ifdef USE_DB
  1599. else if (!strcasecmp( "AltMap", cfg->cmd ))
  1600. store->alt_map = parse_bool( cfg );
  1601. #endif /* USE_DB */
  1602. else if (!strcasecmp( "InfoDelimiter", cfg->cmd )) {
  1603. if (strlen( cfg->val ) != 1) {
  1604. error( "%s:%d: Info delimiter must be exactly one character long\n", cfg->file, cfg->line );
  1605. cfg->err = 1;
  1606. continue;
  1607. }
  1608. store->info_delimiter = cfg->val[0];
  1609. if (!ispunct( store->info_delimiter )) {
  1610. error( "%s:%d: Info delimiter must be a punctuation character\n", cfg->file, cfg->line );
  1611. cfg->err = 1;
  1612. continue;
  1613. }
  1614. } else if (!strcasecmp( "SubFolders", cfg->cmd )) {
  1615. if (!strcasecmp( "Verbatim", cfg->val )) {
  1616. store->sub_style = SUB_VERBATIM;
  1617. } else if (!strcasecmp( "Maildir++", cfg->val )) {
  1618. store->sub_style = SUB_MAILDIRPP;
  1619. } else if (!strcasecmp( "Legacy", cfg->val )) {
  1620. store->sub_style = SUB_LEGACY;
  1621. } else {
  1622. error( "%s:%d: Unrecognized SubFolders style\n", cfg->file, cfg->line );
  1623. cfg->err = 1;
  1624. }
  1625. } else
  1626. parse_generic_store( &store->gen, cfg );
  1627. if (!store->inbox)
  1628. store->inbox = expand_strdup( "~/Maildir" );
  1629. nfasprintf( &store->info_prefix, "%c2,", store->info_delimiter );
  1630. nfasprintf( &store->info_stop, "%c,", store->info_delimiter );
  1631. *storep = &store->gen;
  1632. return 1;
  1633. }
  1634. struct driver maildir_driver = {
  1635. 0, /* XXX DRV_CRLF? */
  1636. maildir_parse_store,
  1637. maildir_cleanup_drv,
  1638. maildir_alloc_store,
  1639. maildir_connect_store,
  1640. maildir_free_store,
  1641. maildir_free_store, /* _cancel_, but it's the same */
  1642. maildir_list_store,
  1643. maildir_select_box,
  1644. maildir_create_box,
  1645. maildir_open_box,
  1646. maildir_confirm_box_empty,
  1647. maildir_delete_box,
  1648. maildir_finish_delete_box,
  1649. maildir_prepare_load_box,
  1650. maildir_load_box,
  1651. maildir_fetch_msg,
  1652. maildir_store_msg,
  1653. maildir_find_new_msgs,
  1654. maildir_set_msg_flags,
  1655. maildir_trash_msg,
  1656. maildir_close_box,
  1657. maildir_cancel_cmds,
  1658. maildir_commit_cmds,
  1659. maildir_memory_usage,
  1660. maildir_fail_state,
  1661. };