void sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan, void (*cb)( int sts, void *aux ), void *aux ) { sync_vars_t *svars; int t; svars = nfcalloc( sizeof(*svars) ); svars->t[1] = 1; svars->ref_count = 1; svars->cb = cb; svars->aux = aux; svars->ctx[0] = ctx[0]; svars->ctx[1] = ctx[1]; svars->chan = chan; svars->uidval[0] = svars->uidval[1] = -1; svars->srecadd = &svars->srecs; for (t = 0; t < 2; t++) { ctx[t]->orig_name = (!names[t] || (ctx[t]->conf->map_inbox && !strcmp( ctx[t]->conf->map_inbox, names[t] ))) ? "INBOX" : names[t]; ctx[t]->name = nfstrdup( ctx[t]->orig_name ); if (ctx[t]->conf->flat_delim && map_name( ctx[t]->name, '/', ctx[t]->conf->flat_delim ) < 0) { error( "Error: canonical mailbox name '%s' contains flattened hierarchy delimiter\n", ctx[t]->name ); svars->ret = SYNC_FAIL; sync_bail3( svars ); return; } ctx[t]->uidvalidity = -1; set_bad_callback( ctx[t], store_bad, AUX ); svars->drv[t] = ctx[t]->conf->driver; } /* Both boxes must be fully set up at this point, so that error exit paths * don't run into uninitialized variables. */ for (t = 0; t < 2; t++) { info( "Selecting %s %s...\n", str_ms[t], ctx[t]->orig_name ); DRIVER_CALL(select( ctx[t], (chan->ops[t] & OP_CREATE) != 0, box_selected, AUX )); } }
int load_config( const char *where, int pseudo ) { conffile_t cfile; store_conf_t *store, **storeapp = &stores; channel_conf_t *channel, **channelapp = &channels; group_conf_t *group, **groupapp = &groups; string_list_t *chanlist, **chanlistapp; char *arg, *p; int len, cops, gcops, max_size, ms, i; char path[_POSIX_PATH_MAX]; char buf[1024]; if (!where) { assert( !pseudo ); nfsnprintf( path, sizeof(path), "%s/." EXE "rc", Home ); cfile.file = path; } else cfile.file = where; if (!pseudo) info( "Reading configuration file %s\n", cfile.file ); if (!(cfile.fp = fopen( cfile.file, "r" ))) { sys_error( "Cannot open config file '%s'", cfile.file ); return 1; } buf[sizeof(buf) - 1] = 0; cfile.buf = buf; cfile.bufl = sizeof(buf) - 1; cfile.line = 0; cfile.err = 0; cfile.rest = 0; gcops = 0; global_conf.expire_unread = -1; reloop: while (getcline( &cfile )) { if (!cfile.cmd) continue; for (i = 0; i < N_DRIVERS; i++) if (drivers[i]->parse_store( &cfile, &store )) { if (store) { if (!store->max_size) store->max_size = INT_MAX; *storeapp = store; storeapp = &store->next; *storeapp = 0; } goto reloop; } if (!strcasecmp( "Channel", cfile.cmd )) { channel = nfcalloc( sizeof(*channel) ); channel->name = nfstrdup( cfile.val ); channel->max_messages = global_conf.max_messages; channel->expire_unread = global_conf.expire_unread; channel->use_internal_date = global_conf.use_internal_date; cops = 0; max_size = -1; while (getcline( &cfile ) && cfile.cmd) { if (!strcasecmp( "MaxSize", cfile.cmd )) max_size = parse_size( &cfile ); else if (!strcasecmp( "Pattern", cfile.cmd ) || !strcasecmp( "Patterns", cfile.cmd )) { arg = cfile.val; do add_string_list( &channel->patterns, arg ); while ((arg = get_arg( &cfile, ARG_OPTIONAL, 0 ))); } else if (!strcasecmp( "Master", cfile.cmd )) { ms = M; goto linkst; } else if (!strcasecmp( "Slave", cfile.cmd )) { ms = S; linkst: if (*cfile.val != ':' || !(p = strchr( cfile.val + 1, ':' ))) { error( "%s:%d: malformed mailbox spec\n", cfile.file, cfile.line ); cfile.err = 1; continue; } *p = 0; for (store = stores; store; store = store->next) if (!strcmp( store->name, cfile.val + 1 )) { channel->stores[ms] = store; goto stpcom; } error( "%s:%d: unknown store '%s'\n", cfile.file, cfile.line, cfile.val + 1 ); cfile.err = 1; continue; stpcom: if (*++p) channel->boxes[ms] = nfstrdup( p ); } else if (!getopt_helper( &cfile, &cops, channel )) { error( "%s:%d: unknown keyword '%s'\n", cfile.file, cfile.line, cfile.cmd ); cfile.err = 1; } } if (!channel->stores[M]) { error( "channel '%s' refers to no master store\n", channel->name ); cfile.err = 1; } else if (!channel->stores[S]) { error( "channel '%s' refers to no slave store\n", channel->name ); cfile.err = 1; } else if (merge_ops( cops, channel->ops )) cfile.err = 1; else { if (max_size >= 0) { if (!max_size) max_size = INT_MAX; channel->stores[M]->max_size = channel->stores[S]->max_size = max_size; } *channelapp = channel; channelapp = &channel->next; } } else if (!strcasecmp( "Group", cfile.cmd )) { group = nfmalloc( sizeof(*group) ); group->name = nfstrdup( cfile.val ); *groupapp = group; groupapp = &group->next; *groupapp = 0; chanlistapp = &group->channels; *chanlistapp = 0; while ((arg = get_arg( &cfile, ARG_OPTIONAL, 0 ))) { addone: len = strlen( arg ); chanlist = nfmalloc( sizeof(*chanlist) + len ); memcpy( chanlist->string, arg, len + 1 ); *chanlistapp = chanlist; chanlistapp = &chanlist->next; *chanlistapp = 0; } while (getcline( &cfile )) { if (!cfile.cmd) goto reloop; if (!strcasecmp( "Channel", cfile.cmd ) || !strcasecmp( "Channels", cfile.cmd )) { arg = cfile.val; goto addone; } else { error( "%s:%d: unknown keyword '%s'\n", cfile.file, cfile.line, cfile.cmd ); cfile.err = 1; } } break; } else if (!strcasecmp( "FSync", cfile.cmd )) { UseFSync = parse_bool( &cfile ); } else if (!strcasecmp( "FieldDelimiter", cfile.cmd )) { if (strlen( cfile.val ) != 1) { error( "%s:%d: Field delimiter must be exactly one character long\n", cfile.file, cfile.line ); cfile.err = 1; } else { FieldDelimiter = cfile.val[0]; if (!ispunct( FieldDelimiter )) { error( "%s:%d: Field delimiter must be a punctuation character\n", cfile.file, cfile.line ); cfile.err = 1; } } } else if (!strcasecmp( "BufferLimit", cfile.cmd )) { BufferLimit = parse_size( &cfile ); if (BufferLimit <= 0) { error( "%s:%d: BufferLimit must be positive\n", cfile.file, cfile.line ); cfile.err = 1; } } else if (!getopt_helper( &cfile, &gcops, &global_conf )) { error( "%s:%d: unknown section keyword '%s'\n", cfile.file, cfile.line, cfile.cmd ); cfile.err = 1; while (getcline( &cfile )) if (!cfile.cmd) goto reloop; break; } } fclose (cfile.fp); cfile.err |= merge_ops( gcops, global_conf.ops ); if (!global_conf.sync_state) global_conf.sync_state = expand_strdup( "~/." EXE "/" ); if (!cfile.err && pseudo) unlink( where ); return cfile.err; }
static void box_loaded( int sts, void *aux ) { DECL_SVARS; sync_rec_t *srec; sync_rec_map_t *srecmap; message_t *tmsg; copy_vars_t *cv; flag_vars_t *fv; int uid, minwuid, *mexcs, nmexcs, rmexcs, no[2], del[2], todel, t1, t2; int sflags, nflags, aflags, dflags, nex; unsigned hashsz, idx; char fbuf[16]; /* enlarge when support for keywords is added */ if (check_ret( sts, aux )) return; INIT_SVARS(aux); svars->state[t] |= ST_LOADED; info( "%s: %d messages, %d recent\n", str_ms[t], svars->ctx[t]->count, svars->ctx[t]->recent ); if (svars->state[t] & S_FIND) { svars->state[t] &= ~S_FIND; debug( "matching previously copied messages on %s\n", str_ms[t] ); match_tuids( svars, t ); } debug( "matching messages on %s against sync records\n", str_ms[t] ); hashsz = bucketsForSize( svars->nsrecs * 3 ); srecmap = nfcalloc( hashsz * sizeof(*srecmap) ); for (srec = svars->srecs; srec; srec = srec->next) { if (srec->status & S_DEAD) continue; uid = srec->uid[t]; idx = (unsigned)((unsigned)uid * 1103515245U) % hashsz; while (srecmap[idx].uid) if (++idx == hashsz) idx = 0; srecmap[idx].uid = uid; srecmap[idx].srec = srec; } for (tmsg = svars->ctx[t]->msgs; tmsg; tmsg = tmsg->next) { if (tmsg->srec) /* found by TUID */ continue; uid = tmsg->uid; if (DFlags & DEBUG) { make_flags( tmsg->flags, fbuf ); printf( svars->ctx[t]->opts & OPEN_SIZE ? " message %5d, %-4s, %6lu: " : " message %5d, %-4s: ", uid, fbuf, tmsg->size ); } idx = (unsigned)((unsigned)uid * 1103515245U) % hashsz; while (srecmap[idx].uid) { if (srecmap[idx].uid == uid) { srec = srecmap[idx].srec; goto found; } if (++idx == hashsz) idx = 0; } tmsg->srec = 0; debug( "new\n" ); continue; found: tmsg->srec = srec; srec->msg[t] = tmsg; debug( "pairs %5d\n", srec->uid[1-t] ); } free( srecmap ); if ((t == S) && svars->smaxxuid) { debug( "preparing master selection - max expired slave uid is %d\n", svars->smaxxuid ); mexcs = 0; nmexcs = rmexcs = 0; minwuid = INT_MAX; for (srec = svars->srecs; srec; srec = srec->next) { if (srec->status & S_DEAD) continue; if (srec->status & S_EXPIRED) { if (!srec->uid[S] || ((svars->ctx[S]->opts & OPEN_OLD) && !srec->msg[S])) { srec->status |= S_EXP_S; continue; } } else { if (svars->smaxxuid >= srec->uid[S]) continue; } if (minwuid > srec->uid[M]) minwuid = srec->uid[M]; } debug( " min non-orphaned master uid is %d\n", minwuid ); for (srec = svars->srecs; srec; srec = srec->next) { if (srec->status & S_DEAD) continue; if (srec->status & S_EXP_S) { if (minwuid > srec->uid[M] && svars->maxuid[M] >= srec->uid[M]) { debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] ); srec->status = S_DEAD; Fprintf( svars->jfp, "- %d %d\n", srec->uid[M], srec->uid[S] ); } else if (srec->uid[S]) { debug( " -> orphaning (%d,[%d])\n", srec->uid[M], srec->uid[S] ); Fprintf( svars->jfp, "> %d %d 0\n", srec->uid[M], srec->uid[S] ); srec->uid[S] = 0; } } else if (minwuid > srec->uid[M]) { if (srec->uid[S] < 0) { if (svars->maxuid[M] >= srec->uid[M]) { debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] ); srec->status = S_DEAD; Fprintf( svars->jfp, "- %d %d\n", srec->uid[M], srec->uid[S] ); } } else if (srec->uid[M] > 0 && srec->uid[S] && (svars->ctx[M]->opts & OPEN_OLD) && (!(svars->ctx[M]->opts & OPEN_NEW) || svars->maxuid[M] >= srec->uid[M])) { if (nmexcs == rmexcs) { rmexcs = rmexcs * 2 + 100; mexcs = nfrealloc( mexcs, rmexcs * sizeof(int) ); } mexcs[nmexcs++] = srec->uid[M]; } } } debugn( " exception list is:" ); for (t = 0; t < nmexcs; t++) debugn( " %d", mexcs[t] ); debug( "\n" ); load_box( svars, M, minwuid, mexcs, nmexcs ); return; } if (!(svars->state[1-t] & ST_LOADED)) return; if (svars->uidval[M] < 0 || svars->uidval[S] < 0) { svars->uidval[M] = svars->ctx[M]->uidvalidity; svars->uidval[S] = svars->ctx[S]->uidvalidity; Fprintf( svars->jfp, "| %d %d\n", svars->uidval[M], svars->uidval[S] ); } info( "Synchronizing...\n" ); debug( "synchronizing new entries\n" ); svars->osrecadd = svars->srecadd; for (t = 0; t < 2; t++) { Fprintf( svars->jfp, "%c %d\n", "{}"[t], svars->ctx[t]->uidnext ); for (tmsg = svars->ctx[1-t]->msgs; tmsg; tmsg = tmsg->next) if (tmsg->srec ? tmsg->srec->uid[t] < 0 && (tmsg->srec->uid[t] == -1 ? (svars->chan->ops[t] & OP_RENEW) : (svars->chan->ops[t] & OP_NEW)) : (svars->chan->ops[t] & OP_NEW)) { debug( "new message %d on %s\n", tmsg->uid, str_ms[1-t] ); if ((svars->chan->ops[t] & OP_EXPUNGE) && (tmsg->flags & F_DELETED)) debug( " -> not %sing - would be expunged anyway\n", str_hl[t] ); else { if (tmsg->srec) { srec = tmsg->srec; srec->status |= S_DONE; debug( " -> pair(%d,%d) exists\n", srec->uid[M], srec->uid[S] ); } else { srec = nfmalloc( sizeof(*srec) ); srec->next = 0; *svars->srecadd = srec; svars->srecadd = &srec->next; svars->nsrecs++; srec->status = S_DONE; srec->flags = 0; srec->tuid[0] = 0; srec->uid[1-t] = tmsg->uid; srec->uid[t] = -2; Fprintf( svars->jfp, "+ %d %d\n", srec->uid[M], srec->uid[S] ); debug( " -> pair(%d,%d) created\n", srec->uid[M], srec->uid[S] ); } if ((tmsg->flags & F_FLAGGED) || !svars->chan->stores[t]->max_size || tmsg->size <= svars->chan->stores[t]->max_size) { if (tmsg->flags) { srec->flags = tmsg->flags; Fprintf( svars->jfp, "* %d %d %u\n", srec->uid[M], srec->uid[S], srec->flags ); debug( " -> updated flags to %u\n", tmsg->flags ); } for (t1 = 0; t1 < TUIDL; t1++) { t2 = arc4_getbyte() & 0x3f; srec->tuid[t1] = t2 < 26 ? t2 + 'A' : t2 < 52 ? t2 + 'a' - 26 : t2 < 62 ? t2 + '0' - 52 : t2 == 62 ? '+' : '/'; } svars->new_total[t]++; stats( svars ); cv = nfmalloc( sizeof(*cv) ); cv->cb = msg_copied; cv->aux = AUX; cv->srec = srec; cv->msg = tmsg; Fprintf( svars->jfp, "# %d %d %." stringify(TUIDL) "s\n", srec->uid[M], srec->uid[S], srec->tuid ); if (FSyncLevel >= FSYNC_THOROUGH) fdatasync( fileno( svars->jfp ) ); debug( " -> %sing message, TUID %." stringify(TUIDL) "s\n", str_hl[t], srec->tuid ); if (copy_msg( cv )) return; } else { if (tmsg->srec) { debug( " -> not %sing - still too big\n", str_hl[t] ); continue; } debug( " -> not %sing - too big\n", str_hl[t] ); msg_copied_p2( svars, srec, t, tmsg, -1 ); } } } svars->state[t] |= ST_SENT_NEW; msgs_copied( svars, t ); } debug( "synchronizing old entries\n" ); for (srec = svars->srecs; srec != *svars->osrecadd; srec = srec->next) { if (srec->status & (S_DEAD|S_DONE)) continue; debug( "pair (%d,%d)\n", srec->uid[M], srec->uid[S] ); no[M] = !srec->msg[M] && (svars->ctx[M]->opts & OPEN_OLD); no[S] = !srec->msg[S] && (svars->ctx[S]->opts & OPEN_OLD); if (no[M] && no[S]) { debug( " vanished\n" ); /* d.1) d.5) d.6) d.10) d.11) */ srec->status = S_DEAD; Fprintf( svars->jfp, "- %d %d\n", srec->uid[M], srec->uid[S] ); } else { del[M] = no[M] && (srec->uid[M] > 0); del[S] = no[S] && (srec->uid[S] > 0); for (t = 0; t < 2; t++) { srec->aflags[t] = srec->dflags[t] = 0; if (srec->msg[t] && (srec->msg[t]->flags & F_DELETED)) srec->status |= S_DEL(t); /* excludes (push) c.3) d.2) d.3) d.4) / (pull) b.3) d.7) d.8) d.9) */ if (!srec->uid[t]) { /* b.1) / c.1) */ debug( " no more %s\n", str_ms[t] ); } else if (del[1-t]) { /* c.4) d.9) / b.4) d.4) */ if (srec->msg[t] && (srec->msg[t]->status & M_FLAGS) && srec->msg[t]->flags != srec->flags) info( "Info: conflicting changes in (%d,%d)\n", srec->uid[M], srec->uid[S] ); if (svars->chan->ops[t] & OP_DELETE) { debug( " %sing delete\n", str_hl[t] ); svars->flags_total[t]++; stats( svars ); fv = nfmalloc( sizeof(*fv) ); fv->aux = AUX; fv->srec = srec; DRIVER_CALL(set_flags( svars->ctx[t], srec->msg[t], srec->uid[t], F_DELETED, 0, flags_set_del, fv )); } else debug( " not %sing delete\n", str_hl[t] ); } else if (!srec->msg[1-t]) /* c.1) c.2) d.7) d.8) / b.1) b.2) d.2) d.3) */ ; else if (srec->uid[t] < 0) /* b.2) / c.2) */ ; /* handled as new messages (sort of) */ else if (!del[t]) { /* a) & b.3) / c.3) */ if (svars->chan->ops[t] & OP_FLAGS) { sflags = srec->msg[1-t]->flags; if ((srec->status & (S_EXPIRE|S_EXPIRED)) && !t) sflags &= ~F_DELETED; srec->aflags[t] = sflags & ~srec->flags; srec->dflags[t] = ~sflags & srec->flags; if (DFlags & DEBUG) { char afbuf[16], dfbuf[16]; /* enlarge when support for keywords is added */ make_flags( srec->aflags[t], afbuf ); make_flags( srec->dflags[t], dfbuf ); debug( " %sing flags: +%s -%s\n", str_hl[t], afbuf, dfbuf ); } } else debug( " not %sing flags\n", str_hl[t] ); } /* else b.4) / c.4) */ } } } if ((svars->chan->ops[S] & (OP_NEW|OP_RENEW|OP_FLAGS)) && svars->chan->max_messages) { /* Flagged and not yet synced messages older than the first not * expired message are not counted. */ todel = svars->ctx[S]->count + svars->new_total[S] - svars->chan->max_messages; debug( "scheduling %d excess messages for expiration\n", todel ); for (tmsg = svars->ctx[S]->msgs; tmsg && todel > 0; tmsg = tmsg->next) if (!(tmsg->status & M_DEAD) && (srec = tmsg->srec) && ((tmsg->flags | srec->aflags[S]) & ~srec->dflags[S] & F_DELETED) && !(srec->status & (S_EXPIRE|S_EXPIRED))) todel--; debug( "%d non-deleted excess messages\n", todel ); for (tmsg = svars->ctx[S]->msgs; tmsg; tmsg = tmsg->next) { if (tmsg->status & M_DEAD) continue; if (!(srec = tmsg->srec) || srec->uid[M] <= 0) todel--; else { nflags = (tmsg->flags | srec->aflags[S]) & ~srec->dflags[S]; if (!(nflags & F_DELETED) || (srec->status & (S_EXPIRE|S_EXPIRED))) { if (nflags & F_FLAGGED) todel--; else if ((!(tmsg->status & M_RECENT) || (tmsg->flags & F_SEEN)) && (todel > 0 || ((srec->status & (S_EXPIRE|S_EXPIRED)) == (S_EXPIRE|S_EXPIRED)) || ((srec->status & (S_EXPIRE|S_EXPIRED)) && (tmsg->flags & F_DELETED)))) { srec->status |= S_NEXPIRE; debug( " pair(%d,%d)\n", srec->uid[M], srec->uid[S] ); todel--; } } } } debug( "%d excess messages remain\n", todel ); for (srec = svars->srecs; srec; srec = srec->next) { if ((srec->status & (S_DEAD|S_DONE)) || !srec->msg[S]) continue; nex = (srec->status / S_NEXPIRE) & 1; if (nex != ((srec->status / S_EXPIRED) & 1)) { if (nex != ((srec->status / S_EXPIRE) & 1)) { Fprintf( svars->jfp, "~ %d %d %d\n", srec->uid[M], srec->uid[S], nex ); debug( " pair(%d,%d): %d (pre)\n", srec->uid[M], srec->uid[S], nex ); srec->status = (srec->status & ~S_EXPIRE) | (nex * S_EXPIRE); } else debug( " pair(%d,%d): %d (pending)\n", srec->uid[M], srec->uid[S], nex ); } } } debug( "synchronizing flags\n" ); for (srec = svars->srecs; srec != *svars->osrecadd; srec = srec->next) { if (srec->status & (S_DEAD|S_DONE)) continue; for (t = 0; t < 2; t++) { aflags = srec->aflags[t]; dflags = srec->dflags[t]; if ((t == S) && ((mvBit(srec->status, S_EXPIRE, S_EXPIRED) ^ srec->status) & S_EXPIRED)) { if (srec->status & S_NEXPIRE) aflags |= F_DELETED; else dflags |= F_DELETED; } if ((svars->chan->ops[t] & OP_EXPUNGE) && (((srec->msg[t] ? srec->msg[t]->flags : 0) | aflags) & ~dflags & F_DELETED) && (!svars->ctx[t]->conf->trash || svars->ctx[t]->conf->trash_only_new)) { srec->aflags[t] &= F_DELETED; aflags &= F_DELETED; srec->dflags[t] = dflags = 0; } if (srec->msg[t] && (srec->msg[t]->status & M_FLAGS)) { aflags &= ~srec->msg[t]->flags; dflags &= srec->msg[t]->flags; } if (aflags | dflags) { svars->flags_total[t]++; stats( svars ); fv = nfmalloc( sizeof(*fv) ); fv->aux = AUX; fv->srec = srec; fv->aflags = aflags; fv->dflags = dflags; DRIVER_CALL(set_flags( svars->ctx[t], srec->msg[t], srec->uid[t], aflags, dflags, flags_set_sync, fv )); } else flags_set_sync_p2( svars, srec, t ); } } for (t = 0; t < 2; t++) { svars->drv[t]->commit( svars->ctx[t] ); svars->state[t] |= ST_SENT_FLAGS; if (msgs_flags_set( svars, t )) return; } }