main.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006,2010-2017 Oswald Buddenhagen <ossi@users.sf.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * As a special exception, mbsync may be linked with the OpenSSL library,
  20. * despite that library's more restrictive license.
  21. */
  22. #include "sync.h"
  23. #include <stdlib.h>
  24. #include <stddef.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <fcntl.h>
  28. #include <signal.h>
  29. #include <time.h>
  30. #include <sys/wait.h>
  31. int DFlags;
  32. int JLimit;
  33. int UseFSync = 1;
  34. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__CYGWIN__)
  35. char FieldDelimiter = ';';
  36. #else
  37. char FieldDelimiter = ':';
  38. #endif
  39. int Pid; /* for maildir and imap */
  40. char Hostname[256]; /* for maildir */
  41. const char *Home; /* for config */
  42. int BufferLimit = 10 * 1024 * 1024;
  43. int chans_total, chans_done;
  44. int boxes_total, boxes_done;
  45. int new_total[2], new_done[2];
  46. int flags_total[2], flags_done[2];
  47. int trash_total[2], trash_done[2];
  48. static void ATTR_NORETURN
  49. version( void )
  50. {
  51. puts( PACKAGE " " VERSION );
  52. exit( 0 );
  53. }
  54. static void ATTR_NORETURN
  55. usage( int code )
  56. {
  57. fputs(
  58. PACKAGE " " VERSION " - mailbox synchronizer\n"
  59. "Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>\n"
  60. "Copyright (C) 2002-2006,2008,2010-2017 Oswald Buddenhagen <ossi@users.sf.net>\n"
  61. "Copyright (C) 2004 Theodore Ts'o <tytso@mit.edu>\n"
  62. "usage:\n"
  63. " " EXE " [flags] {{channel[:box,...]|group} ...|-a}\n"
  64. " -a, --all operate on all defined channels\n"
  65. " -l, --list list mailboxes instead of syncing them\n"
  66. " -n, --new propagate new messages\n"
  67. " -d, --delete propagate message deletions\n"
  68. " -f, --flags propagate message flag changes\n"
  69. " -N, --renew propagate previously not propagated new messages\n"
  70. " -L, --pull propagate from master to slave\n"
  71. " -H, --push propagate from slave to master\n"
  72. " -C, --create create mailboxes if nonexistent\n"
  73. " -X, --expunge expunge deleted messages\n"
  74. " -c, --config CONFIG read an alternate config file (default: ~/." EXE "rc)\n"
  75. " -D, --debug debugging modes (see manual)\n"
  76. " -V, --verbose display what is happening\n"
  77. " -q, --quiet don't display progress counters\n"
  78. " -v, --version display version\n"
  79. " -h, --help display this help message\n"
  80. "\nIf neither --pull nor --push are specified, both are active.\n"
  81. "If neither --new, --delete, --flags nor --renew are specified, all are active.\n"
  82. "Direction and operation can be concatenated like --pull-new, etc.\n"
  83. "--create and --expunge can be suffixed with -master/-slave. Read the man page.\n"
  84. "\nSupported mailbox formats are: IMAP4rev1, Maildir\n"
  85. "\nCompile time options:\n"
  86. #ifdef HAVE_LIBSSL
  87. " +HAVE_LIBSSL"
  88. #else
  89. " -HAVE_LIBSSL"
  90. #endif
  91. #ifdef HAVE_LIBSASL
  92. " +HAVE_LIBSASL"
  93. #else
  94. " -HAVE_LIBSASL"
  95. #endif
  96. #ifdef HAVE_LIBZ
  97. " +HAVE_LIBZ"
  98. #else
  99. " -HAVE_LIBZ"
  100. #endif
  101. #ifdef USE_DB
  102. " +USE_DB"
  103. #else
  104. " -USE_DB"
  105. #endif
  106. #ifdef HAVE_IPV6
  107. " +HAVE_IPV6\n"
  108. #else
  109. " -HAVE_IPV6\n"
  110. #endif
  111. , code ? stderr : stdout );
  112. exit( code );
  113. }
  114. static void ATTR_PRINTFLIKE(1, 2)
  115. debug( const char *msg, ... )
  116. {
  117. va_list va;
  118. va_start( va, msg );
  119. vdebug( DEBUG_MAIN, msg, va );
  120. va_end( va );
  121. }
  122. #ifdef __linux__
  123. static void
  124. crashHandler( int n )
  125. {
  126. int dpid;
  127. char pbuf[10], pabuf[20];
  128. close( 0 );
  129. open( "/dev/tty", O_RDWR );
  130. dup2( 0, 1 );
  131. dup2( 0, 2 );
  132. error( "*** " EXE " caught signal %d. Starting debugger ...\n", n );
  133. switch ((dpid = fork())) {
  134. case -1:
  135. perror( "fork()" );
  136. break;
  137. case 0:
  138. sprintf( pbuf, "%d", Pid );
  139. sprintf( pabuf, "/proc/%d/exe", Pid );
  140. execlp( "gdb", "gdb", pabuf, pbuf, (char *)0 );
  141. perror( "execlp()" );
  142. _exit( 1 );
  143. default:
  144. waitpid( dpid, 0, 0 );
  145. break;
  146. }
  147. exit( 3 );
  148. }
  149. #endif
  150. void
  151. stats( void )
  152. {
  153. char buf[3][64];
  154. char *cs;
  155. int t, l, ll, cls;
  156. static int cols = -1;
  157. if (!(DFlags & PROGRESS))
  158. return;
  159. if (cols < 0 && (!(cs = getenv( "COLUMNS" )) || !(cols = atoi( cs ))))
  160. cols = 80;
  161. ll = sprintf( buf[2], "C: %d/%d B: %d/%d", chans_done, chans_total, boxes_done, boxes_total );
  162. cls = (cols - ll - 10) / 2;
  163. for (t = 0; t < 2; t++) {
  164. l = sprintf( buf[t], "+%d/%d *%d/%d #%d/%d",
  165. new_done[t], new_total[t],
  166. flags_done[t], flags_total[t],
  167. trash_done[t], trash_total[t] );
  168. if (l > cls)
  169. buf[t][cls - 1] = '~';
  170. }
  171. progress( "\r%s M: %.*s S: %.*s", buf[2], cls, buf[0], cls, buf[1] );
  172. }
  173. static int
  174. matches( const char *t, const char *p )
  175. {
  176. for (;;) {
  177. if (!*p)
  178. return !*t;
  179. if (*p == '*') {
  180. p++;
  181. do {
  182. if (matches( t, p ))
  183. return 1;
  184. } while (*t++);
  185. return 0;
  186. } else if (*p == '%') {
  187. p++;
  188. do {
  189. if (*t == '/')
  190. return 0;
  191. if (matches( t, p ))
  192. return 1;
  193. } while (*t++);
  194. return 0;
  195. } else {
  196. if (*p != *t)
  197. return 0;
  198. p++, t++;
  199. }
  200. }
  201. }
  202. static int
  203. is_inbox( const char *name )
  204. {
  205. return starts_with( name, -1, "INBOX", 5 ) && (!name[5] || name[5] == '/');
  206. }
  207. static int
  208. cmp_box_names( const void *a, const void *b )
  209. {
  210. const char *as = *(const char **)a;
  211. const char *bs = *(const char **)b;
  212. int ai = is_inbox( as );
  213. int bi = is_inbox( bs );
  214. int di = bi - ai;
  215. if (di)
  216. return di;
  217. return strcmp( as, bs );
  218. }
  219. static char **
  220. filter_boxes( string_list_t *boxes, const char *prefix, string_list_t *patterns )
  221. {
  222. string_list_t *cpat;
  223. char **boxarr = 0;
  224. const char *ps;
  225. int not, fnot, pfxl, num = 0, rnum = 0;
  226. pfxl = prefix ? strlen( prefix ) : 0;
  227. for (; boxes; boxes = boxes->next) {
  228. if (!starts_with( boxes->string, -1, prefix, pfxl ))
  229. continue;
  230. fnot = 1;
  231. for (cpat = patterns; cpat; cpat = cpat->next) {
  232. ps = cpat->string;
  233. if (*ps == '!') {
  234. ps++;
  235. not = 1;
  236. } else
  237. not = 0;
  238. if (matches( boxes->string + pfxl, ps )) {
  239. fnot = not;
  240. break;
  241. }
  242. }
  243. if (!fnot) {
  244. if (num + 1 >= rnum)
  245. boxarr = nfrealloc( boxarr, (rnum = (rnum + 10) * 2) * sizeof(*boxarr) );
  246. boxarr[num++] = nfstrdup( boxes->string + pfxl );
  247. boxarr[num] = 0;
  248. }
  249. }
  250. qsort( boxarr, num, sizeof(*boxarr), cmp_box_names );
  251. return boxarr;
  252. }
  253. static void
  254. merge_actions( channel_conf_t *chan, int ops[], int have, int mask, int def )
  255. {
  256. if (ops[M] & have) {
  257. chan->ops[M] &= ~mask;
  258. chan->ops[M] |= ops[M] & mask;
  259. chan->ops[S] &= ~mask;
  260. chan->ops[S] |= ops[S] & mask;
  261. } else if (!(chan->ops[M] & have)) {
  262. if (global_conf.ops[M] & have) {
  263. chan->ops[M] |= global_conf.ops[M] & mask;
  264. chan->ops[S] |= global_conf.ops[S] & mask;
  265. } else {
  266. chan->ops[M] |= def;
  267. chan->ops[S] |= def;
  268. }
  269. }
  270. }
  271. typedef struct box_ent {
  272. struct box_ent *next;
  273. char *name;
  274. int present[2];
  275. } box_ent_t;
  276. typedef struct chan_ent {
  277. struct chan_ent *next;
  278. channel_conf_t *conf;
  279. box_ent_t *boxes;
  280. char boxlist;
  281. } chan_ent_t;
  282. static chan_ent_t *
  283. add_channel( chan_ent_t ***chanapp, channel_conf_t *chan, int ops[] )
  284. {
  285. chan_ent_t *ce = nfcalloc( sizeof(*ce) );
  286. ce->conf = chan;
  287. merge_actions( chan, ops, XOP_HAVE_TYPE, OP_MASK_TYPE, OP_MASK_TYPE );
  288. merge_actions( chan, ops, XOP_HAVE_CREATE, OP_CREATE, 0 );
  289. merge_actions( chan, ops, XOP_HAVE_REMOVE, OP_REMOVE, 0 );
  290. merge_actions( chan, ops, XOP_HAVE_EXPUNGE, OP_EXPUNGE, 0 );
  291. **chanapp = ce;
  292. *chanapp = &ce->next;
  293. chans_total++;
  294. return ce;
  295. }
  296. static chan_ent_t *
  297. add_named_channel( chan_ent_t ***chanapp, char *channame, int ops[] )
  298. {
  299. channel_conf_t *chan;
  300. chan_ent_t *ce;
  301. box_ent_t *boxes = 0, **mboxapp = &boxes, *mbox;
  302. char *boxp, *nboxp;
  303. int boxl, boxlist = 0;
  304. if ((boxp = strchr( channame, ':' )))
  305. *boxp++ = 0;
  306. for (chan = channels; chan; chan = chan->next)
  307. if (!strcmp( chan->name, channame ))
  308. goto gotchan;
  309. error( "No channel or group named '%s' defined.\n", channame );
  310. return 0;
  311. gotchan:
  312. if (boxp) {
  313. if (!chan->patterns) {
  314. error( "Cannot override mailbox in channel '%s' - no Patterns.\n", channame );
  315. return 0;
  316. }
  317. boxlist = 1;
  318. do {
  319. nboxp = strpbrk( boxp, ",\n" );
  320. if (nboxp) {
  321. boxl = nboxp - boxp;
  322. *nboxp++ = 0;
  323. } else {
  324. boxl = strlen( boxp );
  325. }
  326. mbox = nfmalloc( sizeof(*mbox) );
  327. if (boxl)
  328. mbox->name = nfstrndup( boxp, boxl );
  329. else
  330. mbox->name = nfstrndup( "INBOX", 5 );
  331. mbox->present[M] = mbox->present[S] = BOX_POSSIBLE;
  332. mbox->next = 0;
  333. *mboxapp = mbox;
  334. mboxapp = &mbox->next;
  335. boxes_total++;
  336. boxp = nboxp;
  337. } while (boxp);
  338. } else {
  339. if (!chan->patterns)
  340. boxes_total++;
  341. }
  342. ce = add_channel( chanapp, chan, ops );
  343. ce->boxes = boxes;
  344. ce->boxlist = boxlist;
  345. return ce;
  346. }
  347. typedef struct {
  348. int t[2];
  349. channel_conf_t *chan;
  350. driver_t *drv[2];
  351. store_t *ctx[2];
  352. chan_ent_t *chanptr;
  353. box_ent_t *boxptr;
  354. string_list_t *boxes[2];
  355. char *names[2];
  356. int ret, all, list, state[2];
  357. char done, skip, cben;
  358. } main_vars_t;
  359. #define AUX &mvars->t[t]
  360. #define MVARS(aux) \
  361. int t = *(int *)aux; \
  362. main_vars_t *mvars = (main_vars_t *)(((char *)(&((int *)aux)[-t])) - offsetof(main_vars_t, t));
  363. #define E_START 0
  364. #define E_OPEN 1
  365. #define E_SYNC 2
  366. static void sync_chans( main_vars_t *mvars, int ent );
  367. int
  368. main( int argc, char **argv )
  369. {
  370. main_vars_t mvars[1];
  371. chan_ent_t *chans = 0, **chanapp = &chans;
  372. group_conf_t *group;
  373. channel_conf_t *chan;
  374. string_list_t *channame;
  375. char *config = 0, *opt, *ochar;
  376. int oind, cops = 0, op, ops[2] = { 0, 0 }, pseudo = 0;
  377. tzset();
  378. gethostname( Hostname, sizeof(Hostname) );
  379. if ((ochar = strchr( Hostname, '.' )))
  380. *ochar = 0;
  381. Pid = getpid();
  382. if (!(Home = getenv("HOME"))) {
  383. fputs( "Fatal: $HOME not set\n", stderr );
  384. return 1;
  385. }
  386. arc4_init();
  387. memset( mvars, 0, sizeof(*mvars) );
  388. mvars->t[1] = 1;
  389. for (oind = 1, ochar = 0; ; ) {
  390. if (!ochar || !*ochar) {
  391. if (oind >= argc)
  392. break;
  393. if (argv[oind][0] != '-')
  394. break;
  395. if (argv[oind][1] == '-') {
  396. opt = argv[oind++] + 2;
  397. if (!*opt)
  398. break;
  399. if (!strcmp( opt, "config" )) {
  400. if (oind >= argc) {
  401. error( "--config requires an argument.\n" );
  402. return 1;
  403. }
  404. config = argv[oind++];
  405. } else if (starts_with( opt, -1, "config=", 7 ))
  406. config = opt + 7;
  407. else if (!strcmp( opt, "all" ))
  408. mvars->all = 1;
  409. else if (!strcmp( opt, "list" ))
  410. mvars->list = 1;
  411. else if (!strcmp( opt, "help" ))
  412. usage( 0 );
  413. else if (!strcmp( opt, "version" ))
  414. version();
  415. else if (!strcmp( opt, "quiet" )) {
  416. if (DFlags & QUIET)
  417. DFlags |= VERYQUIET;
  418. else
  419. DFlags |= QUIET;
  420. } else if (!strcmp( opt, "verbose" )) {
  421. DFlags |= VERBOSE;
  422. } else if (starts_with( opt, -1, "debug", 5 )) {
  423. opt += 5;
  424. if (!*opt)
  425. op = VERBOSE | DEBUG_ALL;
  426. else if (!strcmp( opt, "-crash" ))
  427. op = DEBUG_CRASH;
  428. else if (!strcmp( opt, "-driver" ))
  429. op = VERBOSE | DEBUG_DRV;
  430. else if (!strcmp( opt, "-driver-all" ))
  431. op = VERBOSE | DEBUG_DRV | DEBUG_DRV_ALL;
  432. else if (!strcmp( opt, "-maildir" ))
  433. op = VERBOSE | DEBUG_MAILDIR;
  434. else if (!strcmp( opt, "-main" ))
  435. op = VERBOSE | DEBUG_MAIN;
  436. else if (!strcmp( opt, "-net" ))
  437. op = VERBOSE | DEBUG_NET;
  438. else if (!strcmp( opt, "-net-all" ))
  439. op = VERBOSE | DEBUG_NET | DEBUG_NET_ALL;
  440. else if (!strcmp( opt, "-sync" ))
  441. op = VERBOSE | DEBUG_SYNC;
  442. else
  443. goto badopt;
  444. DFlags |= op;
  445. } else if (!strcmp( opt, "pull" ))
  446. cops |= XOP_PULL, ops[M] |= XOP_HAVE_TYPE;
  447. else if (!strcmp( opt, "push" ))
  448. cops |= XOP_PUSH, ops[M] |= XOP_HAVE_TYPE;
  449. else if (starts_with( opt, -1, "create", 6 )) {
  450. opt += 6;
  451. op = OP_CREATE|XOP_HAVE_CREATE;
  452. lcop:
  453. if (!*opt)
  454. cops |= op;
  455. else if (!strcmp( opt, "-master" ))
  456. ops[M] |= op;
  457. else if (!strcmp( opt, "-slave" ))
  458. ops[S] |= op;
  459. else
  460. goto badopt;
  461. ops[M] |= op & (XOP_HAVE_CREATE|XOP_HAVE_REMOVE|XOP_HAVE_EXPUNGE);
  462. } else if (starts_with( opt, -1, "remove", 6 )) {
  463. opt += 6;
  464. op = OP_REMOVE|XOP_HAVE_REMOVE;
  465. goto lcop;
  466. } else if (starts_with( opt, -1, "expunge", 7 )) {
  467. opt += 7;
  468. op = OP_EXPUNGE|XOP_HAVE_EXPUNGE;
  469. goto lcop;
  470. } else if (!strcmp( opt, "no-expunge" ))
  471. ops[M] |= XOP_HAVE_EXPUNGE;
  472. else if (!strcmp( opt, "no-create" ))
  473. ops[M] |= XOP_HAVE_CREATE;
  474. else if (!strcmp( opt, "no-remove" ))
  475. ops[M] |= XOP_HAVE_REMOVE;
  476. else if (!strcmp( opt, "full" ))
  477. ops[M] |= XOP_HAVE_TYPE|XOP_PULL|XOP_PUSH;
  478. else if (!strcmp( opt, "noop" ))
  479. ops[M] |= XOP_HAVE_TYPE;
  480. else if (starts_with( opt, -1, "pull", 4 )) {
  481. op = XOP_PULL;
  482. lcac:
  483. opt += 4;
  484. if (!*opt)
  485. cops |= op;
  486. else if (*opt == '-') {
  487. opt++;
  488. goto rlcac;
  489. } else
  490. goto badopt;
  491. } else if (starts_with( opt, -1, "push", 4 )) {
  492. op = XOP_PUSH;
  493. goto lcac;
  494. } else {
  495. op = 0;
  496. rlcac:
  497. if (!strcmp( opt, "new" ))
  498. op |= OP_NEW;
  499. else if (!strcmp( opt, "renew" ))
  500. op |= OP_RENEW;
  501. else if (!strcmp( opt, "delete" ))
  502. op |= OP_DELETE;
  503. else if (!strcmp( opt, "flags" ))
  504. op |= OP_FLAGS;
  505. else {
  506. badopt:
  507. error( "Unknown option '%s'\n", argv[oind - 1] );
  508. return 1;
  509. }
  510. switch (op & XOP_MASK_DIR) {
  511. case XOP_PULL: ops[S] |= op & OP_MASK_TYPE; break;
  512. case XOP_PUSH: ops[M] |= op & OP_MASK_TYPE; break;
  513. default: cops |= op; break;
  514. }
  515. ops[M] |= XOP_HAVE_TYPE;
  516. }
  517. continue;
  518. }
  519. ochar = argv[oind++] + 1;
  520. if (!*ochar) {
  521. error( "Invalid option '-'\n" );
  522. return 1;
  523. }
  524. }
  525. switch (*ochar++) {
  526. case 'a':
  527. mvars->all = 1;
  528. break;
  529. case 'l':
  530. mvars->list = 1;
  531. break;
  532. case 'c':
  533. if (*ochar == 'T') {
  534. ochar++;
  535. pseudo = 1;
  536. }
  537. if (oind >= argc) {
  538. error( "-c requires an argument.\n" );
  539. return 1;
  540. }
  541. config = argv[oind++];
  542. break;
  543. case 'C':
  544. op = OP_CREATE|XOP_HAVE_CREATE;
  545. cop:
  546. if (*ochar == 'm')
  547. ops[M] |= op, ochar++;
  548. else if (*ochar == 's')
  549. ops[S] |= op, ochar++;
  550. else if (*ochar == '-')
  551. ochar++;
  552. else
  553. cops |= op;
  554. ops[M] |= op & (XOP_HAVE_CREATE|XOP_HAVE_REMOVE|XOP_HAVE_EXPUNGE);
  555. break;
  556. case 'R':
  557. op = OP_REMOVE|XOP_HAVE_REMOVE;
  558. goto cop;
  559. case 'X':
  560. op = OP_EXPUNGE|XOP_HAVE_EXPUNGE;
  561. goto cop;
  562. case 'F':
  563. cops |= XOP_PULL|XOP_PUSH;
  564. FALLTHROUGH
  565. case '0':
  566. ops[M] |= XOP_HAVE_TYPE;
  567. break;
  568. case 'n':
  569. case 'd':
  570. case 'f':
  571. case 'N':
  572. --ochar;
  573. op = 0;
  574. cac:
  575. for (;; ochar++) {
  576. if (*ochar == 'n')
  577. op |= OP_NEW;
  578. else if (*ochar == 'd')
  579. op |= OP_DELETE;
  580. else if (*ochar == 'f')
  581. op |= OP_FLAGS;
  582. else if (*ochar == 'N')
  583. op |= OP_RENEW;
  584. else
  585. break;
  586. }
  587. if (op & OP_MASK_TYPE)
  588. switch (op & XOP_MASK_DIR) {
  589. case XOP_PULL: ops[S] |= op & OP_MASK_TYPE; break;
  590. case XOP_PUSH: ops[M] |= op & OP_MASK_TYPE; break;
  591. default: cops |= op; break;
  592. }
  593. else
  594. cops |= op;
  595. ops[M] |= XOP_HAVE_TYPE;
  596. break;
  597. case 'L':
  598. op = XOP_PULL;
  599. goto cac;
  600. case 'H':
  601. op = XOP_PUSH;
  602. goto cac;
  603. case 'q':
  604. if (DFlags & QUIET)
  605. DFlags |= VERYQUIET;
  606. else
  607. DFlags |= QUIET;
  608. break;
  609. case 'V':
  610. DFlags |= VERBOSE;
  611. break;
  612. case 'D':
  613. for (op = 0; *ochar; ochar++) {
  614. switch (*ochar) {
  615. case 'C':
  616. op |= DEBUG_CRASH;
  617. break;
  618. case 'd':
  619. op |= DEBUG_DRV | VERBOSE;
  620. break;
  621. case 'D':
  622. op |= DEBUG_DRV | DEBUG_DRV_ALL | VERBOSE;
  623. break;
  624. case 'm':
  625. op |= DEBUG_MAILDIR | VERBOSE;
  626. break;
  627. case 'M':
  628. op |= DEBUG_MAIN | VERBOSE;
  629. break;
  630. case 'n':
  631. op |= DEBUG_NET | VERBOSE;
  632. break;
  633. case 'N':
  634. op |= DEBUG_NET | DEBUG_NET_ALL | VERBOSE;
  635. break;
  636. case 's':
  637. op |= DEBUG_SYNC | VERBOSE;
  638. break;
  639. default:
  640. error( "Unknown -D flag '%c'\n", *ochar );
  641. return 1;
  642. }
  643. }
  644. if (!op)
  645. op = DEBUG_ALL | VERBOSE;
  646. DFlags |= op;
  647. break;
  648. case 'J':
  649. DFlags |= KEEPJOURNAL;
  650. JLimit = strtol( ochar, &ochar, 10 );
  651. break;
  652. case 'Z':
  653. DFlags |= ZERODELAY;
  654. break;
  655. case 'v':
  656. version();
  657. case 'h':
  658. usage( 0 );
  659. default:
  660. error( "Unknown option '-%c'\n", *(ochar - 1) );
  661. return 1;
  662. }
  663. }
  664. if (!(DFlags & (QUIET | DEBUG_ALL)) && isatty( 1 ))
  665. DFlags |= PROGRESS;
  666. #ifdef __linux__
  667. if (DFlags & DEBUG_CRASH) {
  668. signal( SIGSEGV, crashHandler );
  669. signal( SIGBUS, crashHandler );
  670. signal( SIGILL, crashHandler );
  671. }
  672. #endif
  673. if (merge_ops( cops, ops ))
  674. return 1;
  675. if (load_config( config, pseudo ))
  676. return 1;
  677. if (!channels) {
  678. fputs( "No channels defined. Try 'man " EXE "'\n", stderr );
  679. return 1;
  680. }
  681. if (mvars->all) {
  682. for (chan = channels; chan; chan = chan->next) {
  683. add_channel( &chanapp, chan, ops );
  684. if (!chan->patterns)
  685. boxes_total++;
  686. }
  687. } else {
  688. for (; argv[oind]; oind++) {
  689. for (group = groups; group; group = group->next) {
  690. if (!strcmp( group->name, argv[oind] )) {
  691. for (channame = group->channels; channame; channame = channame->next)
  692. if (!add_named_channel( &chanapp, channame->string, ops ))
  693. mvars->ret = 1;
  694. goto gotgrp;
  695. }
  696. }
  697. if (!add_named_channel( &chanapp, argv[oind], ops ))
  698. mvars->ret = 1;
  699. gotgrp: ;
  700. }
  701. }
  702. if (!chans) {
  703. fputs( "No channel specified. Try '" EXE " -h'\n", stderr );
  704. return 1;
  705. }
  706. mvars->chanptr = chans;
  707. if (!mvars->list)
  708. stats();
  709. mvars->cben = 1;
  710. sync_chans( mvars, E_START );
  711. main_loop();
  712. if (!mvars->list)
  713. flushn();
  714. return mvars->ret;
  715. }
  716. #define ST_FRESH 0
  717. #define ST_CONNECTED 1
  718. #define ST_OPEN 2
  719. #define ST_CANCELING 3
  720. #define ST_CLOSED 4
  721. static void
  722. cancel_prep_done( void *aux )
  723. {
  724. MVARS(aux)
  725. mvars->drv[t]->free_store( mvars->ctx[t] );
  726. mvars->state[t] = ST_CLOSED;
  727. sync_chans( mvars, E_OPEN );
  728. }
  729. static void
  730. store_bad( void *aux )
  731. {
  732. MVARS(aux)
  733. mvars->drv[t]->cancel_store( mvars->ctx[t] );
  734. mvars->state[t] = ST_CLOSED;
  735. mvars->ret = mvars->skip = 1;
  736. sync_chans( mvars, E_OPEN );
  737. }
  738. static void store_connected( int sts, void *aux );
  739. static void store_listed( int sts, string_list_t *boxes, void *aux );
  740. static int sync_listed_boxes( main_vars_t *mvars, box_ent_t *mbox );
  741. static void done_sync_2_dyn( int sts, void *aux );
  742. static void done_sync( int sts, void *aux );
  743. #define nz(a,b) ((a)?(a):(b))
  744. static void
  745. sync_chans( main_vars_t *mvars, int ent )
  746. {
  747. box_ent_t *mbox, *nmbox, **mboxapp;
  748. char **boxes[2];
  749. const char *labels[2];
  750. int t, mb, sb, cmp;
  751. if (!mvars->cben)
  752. return;
  753. switch (ent) {
  754. case E_OPEN: goto opened;
  755. case E_SYNC: goto syncone;
  756. }
  757. do {
  758. mvars->chan = mvars->chanptr->conf;
  759. info( "Channel %s\n", mvars->chan->name );
  760. mvars->skip = mvars->cben = 0;
  761. for (t = 0; t < 2; t++) {
  762. int st = mvars->chan->stores[t]->driver->get_fail_state( mvars->chan->stores[t] );
  763. if (st != FAIL_TEMP) {
  764. info( "Skipping due to %sfailed %s store %s.\n",
  765. (st == FAIL_WAIT) ? "temporarily " : "", str_ms[t], mvars->chan->stores[t]->name );
  766. mvars->skip = 1;
  767. }
  768. }
  769. if (mvars->skip)
  770. goto next2;
  771. mvars->state[M] = mvars->state[S] = ST_FRESH;
  772. if ((DFlags & DEBUG_DRV) || (mvars->chan->stores[M]->driver->get_caps( 0 ) & mvars->chan->stores[S]->driver->get_caps( 0 ) & DRV_VERBOSE))
  773. labels[M] = "M: ", labels[S] = "S: ";
  774. else
  775. labels[M] = labels[S] = "";
  776. for (t = 0; t < 2; t++) {
  777. driver_t *drv = mvars->chan->stores[t]->driver;
  778. store_t *ctx = drv->alloc_store( mvars->chan->stores[t], labels[t] );
  779. if (DFlags & DEBUG_DRV) {
  780. drv = &proxy_driver;
  781. ctx = proxy_alloc_store( ctx, labels[t] );
  782. }
  783. mvars->drv[t] = drv;
  784. mvars->ctx[t] = ctx;
  785. drv->set_bad_callback( ctx, store_bad, AUX );
  786. }
  787. for (t = 0; ; t++) {
  788. info( "Opening %s store %s...\n", str_ms[t], mvars->chan->stores[t]->name );
  789. mvars->drv[t]->connect_store( mvars->ctx[t], store_connected, AUX );
  790. if (t || mvars->skip)
  791. break;
  792. }
  793. mvars->cben = 1;
  794. opened:
  795. if (mvars->skip)
  796. goto next;
  797. if (mvars->state[M] != ST_OPEN || mvars->state[S] != ST_OPEN)
  798. return;
  799. if (!mvars->chanptr->boxlist && mvars->chan->patterns) {
  800. mvars->chanptr->boxlist = 2;
  801. boxes[M] = filter_boxes( mvars->boxes[M], mvars->chan->boxes[M], mvars->chan->patterns );
  802. boxes[S] = filter_boxes( mvars->boxes[S], mvars->chan->boxes[S], mvars->chan->patterns );
  803. mboxapp = &mvars->chanptr->boxes;
  804. for (mb = sb = 0; ; ) {
  805. char *mname = boxes[M] ? boxes[M][mb] : 0;
  806. char *sname = boxes[S] ? boxes[S][sb] : 0;
  807. if (!mname && !sname)
  808. break;
  809. mbox = nfmalloc( sizeof(*mbox) );
  810. if (!(cmp = !mname - !sname) && !(cmp = cmp_box_names( &mname, &sname ))) {
  811. mbox->name = mname;
  812. free( sname );
  813. mbox->present[M] = mbox->present[S] = BOX_PRESENT;
  814. mb++;
  815. sb++;
  816. } else if (cmp < 0) {
  817. mbox->name = mname;
  818. mbox->present[M] = BOX_PRESENT;
  819. mbox->present[S] = (!mb && !strcmp( mbox->name, "INBOX" )) ? BOX_PRESENT : BOX_ABSENT;
  820. mb++;
  821. } else {
  822. mbox->name = sname;
  823. mbox->present[M] = (!sb && !strcmp( mbox->name, "INBOX" )) ? BOX_PRESENT : BOX_ABSENT;
  824. mbox->present[S] = BOX_PRESENT;
  825. sb++;
  826. }
  827. mbox->next = 0;
  828. *mboxapp = mbox;
  829. mboxapp = &mbox->next;
  830. boxes_total++;
  831. }
  832. free( boxes[M] );
  833. free( boxes[S] );
  834. if (!mvars->list)
  835. stats();
  836. }
  837. mvars->boxptr = mvars->chanptr->boxes;
  838. if (mvars->list && chans_total > 1)
  839. printf( "%s:\n", mvars->chan->name );
  840. syncml:
  841. mvars->done = mvars->cben = 0;
  842. if (mvars->chanptr->boxlist) {
  843. while ((mbox = mvars->boxptr)) {
  844. mvars->boxptr = mbox->next;
  845. if (sync_listed_boxes( mvars, mbox ))
  846. goto syncw;
  847. }
  848. } else {
  849. if (!mvars->list) {
  850. int present[] = { BOX_POSSIBLE, BOX_POSSIBLE };
  851. sync_boxes( mvars->ctx, mvars->chan->boxes, present, mvars->chan, done_sync, mvars );
  852. mvars->skip = 1;
  853. syncw:
  854. mvars->cben = 1;
  855. if (!mvars->done)
  856. return;
  857. syncone:
  858. if (!mvars->skip)
  859. goto syncml;
  860. } else
  861. printf( "%s <=> %s\n", nz( mvars->chan->boxes[M], "INBOX" ), nz( mvars->chan->boxes[S], "INBOX" ) );
  862. }
  863. next:
  864. mvars->cben = 0;
  865. for (t = 0; t < 2; t++) {
  866. free_string_list( mvars->boxes[t] );
  867. mvars->boxes[t] = 0;
  868. if (mvars->state[t] == ST_FRESH) {
  869. /* An unconnected store may be only cancelled. */
  870. mvars->state[t] = ST_CLOSED;
  871. mvars->drv[t]->cancel_store( mvars->ctx[t] );
  872. } else if (mvars->state[t] == ST_CONNECTED || mvars->state[t] == ST_OPEN) {
  873. mvars->state[t] = ST_CANCELING;
  874. mvars->drv[t]->cancel_cmds( mvars->ctx[t], cancel_prep_done, AUX );
  875. }
  876. }
  877. mvars->cben = 1;
  878. if (mvars->state[M] != ST_CLOSED || mvars->state[S] != ST_CLOSED) {
  879. mvars->skip = 1;
  880. return;
  881. }
  882. if (mvars->chanptr->boxlist == 2) {
  883. for (nmbox = mvars->chanptr->boxes; (mbox = nmbox); ) {
  884. nmbox = mbox->next;
  885. free( mbox->name );
  886. free( mbox );
  887. }
  888. mvars->chanptr->boxes = 0;
  889. mvars->chanptr->boxlist = 0;
  890. }
  891. next2:
  892. if (!mvars->list) {
  893. chans_done++;
  894. stats();
  895. }
  896. } while ((mvars->chanptr = mvars->chanptr->next));
  897. for (t = 0; t < N_DRIVERS; t++)
  898. drivers[t]->cleanup();
  899. }
  900. static void
  901. store_connected( int sts, void *aux )
  902. {
  903. MVARS(aux)
  904. string_list_t *cpat;
  905. int cflags;
  906. switch (sts) {
  907. case DRV_CANCELED:
  908. return;
  909. case DRV_OK:
  910. if (!mvars->skip && !mvars->chanptr->boxlist && mvars->chan->patterns) {
  911. for (cflags = 0, cpat = mvars->chan->patterns; cpat; cpat = cpat->next) {
  912. const char *pat = cpat->string;
  913. if (*pat != '!') {
  914. char buf[8];
  915. int bufl = snprintf( buf, sizeof(buf), "%s%s", nz( mvars->chan->boxes[t], "" ), pat );
  916. int flags = 0;
  917. /* Partial matches like "INB*" or even "*" are not considered,
  918. * except implicity when the INBOX lives under Path. */
  919. if (starts_with( buf, bufl, "INBOX", 5 )) {
  920. char c = buf[5];
  921. if (!c) {
  922. /* User really wants the INBOX. */
  923. flags |= LIST_INBOX;
  924. } else if (c == '/') {
  925. /* Flattened sub-folders of INBOX actually end up in Path. */
  926. if (mvars->ctx[t]->conf->flat_delim[0])
  927. flags |= LIST_PATH;
  928. else
  929. flags |= LIST_INBOX;
  930. } else if (c == '*' || c == '%') {
  931. /* It can be both INBOX and Path, but don't require Path to be configured. */
  932. flags |= LIST_INBOX | LIST_PATH_MAYBE;
  933. } else {
  934. /* It's definitely not the INBOX. */
  935. flags |= LIST_PATH;
  936. }
  937. } else {
  938. flags |= LIST_PATH;
  939. }
  940. debug( "pattern '%s' (effective '%s'): %sPath, %sINBOX\n",
  941. pat, buf, (flags & LIST_PATH) ? "" : "no ", (flags & LIST_INBOX) ? "" : "no ");
  942. cflags |= flags;
  943. }
  944. }
  945. mvars->state[t] = ST_CONNECTED;
  946. mvars->drv[t]->list_store( mvars->ctx[t], cflags, store_listed, AUX );
  947. return;
  948. }
  949. mvars->state[t] = ST_OPEN;
  950. break;
  951. default:
  952. mvars->ret = mvars->skip = 1;
  953. mvars->state[t] = ST_OPEN;
  954. break;
  955. }
  956. sync_chans( mvars, E_OPEN );
  957. }
  958. static void
  959. store_listed( int sts, string_list_t *boxes, void *aux )
  960. {
  961. MVARS(aux)
  962. string_list_t *box;
  963. switch (sts) {
  964. case DRV_CANCELED:
  965. return;
  966. case DRV_OK:
  967. for (box = boxes; box; box = box->next) {
  968. if (mvars->ctx[t]->conf->flat_delim[0]) {
  969. string_list_t *nbox;
  970. if (map_name( box->string, (char **)&nbox, offsetof(string_list_t, string), mvars->ctx[t]->conf->flat_delim, "/" ) < 0) {
  971. error( "Error: flattened mailbox name '%s' contains canonical hierarchy delimiter\n", box->string );
  972. mvars->ret = mvars->skip = 1;
  973. } else {
  974. nbox->next = mvars->boxes[t];
  975. mvars->boxes[t] = nbox;
  976. }
  977. } else {
  978. add_string_list( &mvars->boxes[t], box->string );
  979. }
  980. }
  981. if (mvars->ctx[t]->conf->map_inbox) {
  982. debug( "adding mapped inbox to %s: %s\n", str_ms[t], mvars->ctx[t]->conf->map_inbox );
  983. add_string_list( &mvars->boxes[t], mvars->ctx[t]->conf->map_inbox );
  984. }
  985. break;
  986. default:
  987. mvars->ret = mvars->skip = 1;
  988. break;
  989. }
  990. mvars->state[t] = ST_OPEN;
  991. sync_chans( mvars, E_OPEN );
  992. }
  993. static int
  994. sync_listed_boxes( main_vars_t *mvars, box_ent_t *mbox )
  995. {
  996. if (mvars->chan->boxes[M] || mvars->chan->boxes[S]) {
  997. const char *mpfx = nz( mvars->chan->boxes[M], "" );
  998. const char *spfx = nz( mvars->chan->boxes[S], "" );
  999. if (!mvars->list) {
  1000. nfasprintf( &mvars->names[M], "%s%s", mpfx, mbox->name );
  1001. nfasprintf( &mvars->names[S], "%s%s", spfx, mbox->name );
  1002. sync_boxes( mvars->ctx, (const char **)mvars->names, mbox->present, mvars->chan, done_sync_2_dyn, mvars );
  1003. return 1;
  1004. }
  1005. printf( "%s%s <=> %s%s\n", mpfx, mbox->name, spfx, mbox->name );
  1006. } else {
  1007. if (!mvars->list) {
  1008. mvars->names[M] = mvars->names[S] = mbox->name;
  1009. sync_boxes( mvars->ctx, (const char **)mvars->names, mbox->present, mvars->chan, done_sync, mvars );
  1010. return 1;
  1011. }
  1012. puts( mbox->name );
  1013. }
  1014. return 0;
  1015. }
  1016. static void
  1017. done_sync_2_dyn( int sts, void *aux )
  1018. {
  1019. main_vars_t *mvars = (main_vars_t *)aux;
  1020. free( mvars->names[M] );
  1021. free( mvars->names[S] );
  1022. done_sync( sts, aux );
  1023. }
  1024. static void
  1025. done_sync( int sts, void *aux )
  1026. {
  1027. main_vars_t *mvars = (main_vars_t *)aux;
  1028. mvars->done = 1;
  1029. boxes_done++;
  1030. stats();
  1031. if (sts) {
  1032. mvars->ret = 1;
  1033. if (sts & (SYNC_BAD(M) | SYNC_BAD(S))) {
  1034. if (sts & SYNC_BAD(M))
  1035. mvars->state[M] = ST_CLOSED;
  1036. if (sts & SYNC_BAD(S))
  1037. mvars->state[S] = ST_CLOSED;
  1038. mvars->skip = 1;
  1039. }
  1040. }
  1041. sync_chans( mvars, E_SYNC );
  1042. }