@matheusbsbs / matheus-bagliano / commits / 3a7cba95b7
Fork 0

imap-send: remove useless uid code

The imap-send code is based on code from isync, a program for syncing imap mailboxes. Because of this, it has inherited some code that makes sense for isync, but not for imap-send. In particular, when storing a message, it does one of: - if the server supports it, note the server-assigned unique identifier (UID) given to each message - otherwise, assigned a random UID and store it in the message header as X-TUID Presumably this is used in isync to be able to synchronize mailstores multiple times without duplication. But for imap-send, the values are useless; we never do anything with them and simply forget them at the end of the program. This patch removes the useless code. Not only is it nice for maintainability to get rid of dead code, but the removed code relied on the existence of /dev/urandom, which made it a portability problem for non-Unix platforms. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 19, 2009 at 17:42 UTC 3a7cba95b74dd2460f8b9bf4f7cefa3c21d31fdd
1 file changed +11 -144
imap-send.c
+11 -144
@@ -123,9 +123,6 @@ static int nfvasprintf(char **strp, const char *fmt, va_list ap)
123 return len;
124 }
125
126 -static void arc4_init(void);
127 -static unsigned char arc4_getbyte(void);
128 -
126 struct imap_server_conf {
127 char *name;
128 char *tunnel;
@@ -489,52 +486,6 @@ static int nfsnprintf(char *buf, int blen, const char *fmt, ...)
486 return ret;
487 }
488
492 -static struct {
493 - unsigned char i, j, s[256];
494 -} rs;
495 -
496 -static void arc4_init(void)
497 -{
498 - int i, fd;
499 - unsigned char j, si, dat[128];
500 -
501 - if ((fd = open("/dev/urandom", O_RDONLY)) < 0 && (fd = open("/dev/random", O_RDONLY)) < 0) {
502 - fprintf(stderr, "Fatal: no random number source available.\n");
503 - exit(3);
504 - }
505 - if (read_in_full(fd, dat, 128) != 128) {
506 - fprintf(stderr, "Fatal: cannot read random number source.\n");
507 - exit(3);
508 - }
509 - close(fd);
510 -
511 - for (i = 0; i < 256; i++)
512 - rs.s[i] = i;
513 - for (i = j = 0; i < 256; i++) {
514 - si = rs.s[i];
515 - j += si + dat[i & 127];
516 - rs.s[i] = rs.s[j];
517 - rs.s[j] = si;
518 - }
519 - rs.i = rs.j = 0;
520 -
521 - for (i = 0; i < 256; i++)
522 - arc4_getbyte();
523 -}
524 -
525 -static unsigned char arc4_getbyte(void)
526 -{
527 - unsigned char si, sj;
528 -
529 - rs.i++;
530 - si = rs.s[rs.i];
531 - rs.j += si;
532 - sj = rs.s[rs.j];
533 - rs.s[rs.i] = sj;
534 - rs.s[rs.j] = si;
535 - return rs.s[(si + sj) & 0xff];
536 -}
537 -
489 static struct imap_cmd *v_issue_imap_cmd(struct imap_store *ctx,
490 struct imap_cmd_cb *cb,
491 const char *fmt, va_list ap)
@@ -1198,88 +1149,20 @@ static int imap_make_flags(int flags, char *buf)
1149 return d;
1150 }
1151
1201 -#define TUIDL 8
1202 -
1203 -static int imap_store_msg(struct store *gctx, struct msg_data *data, int *uid)
1152 +static int imap_store_msg(struct store *gctx, struct msg_data *data)
1153 {
1154 struct imap_store *ctx = (struct imap_store *)gctx;
1155 struct imap *imap = ctx->imap;
1156 struct imap_cmd_cb cb;
1208 - char *fmap, *buf;
1157 const char *prefix, *box;
1210 - int ret, i, j, d, len, extra, nocr;
1211 - int start, sbreak = 0, ebreak = 0;
1212 - char flagstr[128], tuid[TUIDL * 2 + 1];
1158 + int ret, d;
1159 + char flagstr[128];
1160
1161 memset(&cb, 0, sizeof(cb));
1162
1216 - fmap = data->data;
1217 - len = data->len;
1218 - nocr = !data->crlf;
1219 - extra = 0, i = 0;
1220 - if (!CAP(UIDPLUS) && uid) {
1221 - nloop:
1222 - start = i;
1223 - while (i < len)
1224 - if (fmap[i++] == '\n') {
1225 - extra += nocr;
1226 - if (i - 2 + nocr == start) {
1227 - sbreak = ebreak = i - 2 + nocr;
1228 - goto mktid;
1229 - }
1230 - if (!memcmp(fmap + start, "X-TUID: ", 8)) {
1231 - extra -= (ebreak = i) - (sbreak = start) + nocr;
1232 - goto mktid;
1233 - }
1234 - goto nloop;
1235 - }
1236 - /* invalid message */
1237 - free(fmap);
1238 - return DRV_MSG_BAD;
1239 - mktid:
1240 - for (j = 0; j < TUIDL; j++)
1241 - sprintf(tuid + j * 2, "%02x", arc4_getbyte());
1242 - extra += 8 + TUIDL * 2 + 2;
1243 - }
1244 - if (nocr)
1245 - for (; i < len; i++)
1246 - if (fmap[i] == '\n')
1247 - extra++;
1248 -
1249 - cb.dlen = len + extra;
1250 - buf = cb.data = xmalloc(cb.dlen);
1251 - i = 0;
1252 - if (!CAP(UIDPLUS) && uid) {
1253 - if (nocr) {
1254 - for (; i < sbreak; i++)
1255 - if (fmap[i] == '\n') {
1256 - *buf++ = '\r';
1257 - *buf++ = '\n';
1258 - } else
1259 - *buf++ = fmap[i];
1260 - } else {
1261 - memcpy(buf, fmap, sbreak);
1262 - buf += sbreak;
1263 - }
1264 - memcpy(buf, "X-TUID: ", 8);
1265 - buf += 8;
1266 - memcpy(buf, tuid, TUIDL * 2);
1267 - buf += TUIDL * 2;
1268 - *buf++ = '\r';
1269 - *buf++ = '\n';
1270 - i = ebreak;
1271 - }
1272 - if (nocr) {
1273 - for (; i < len; i++)
1274 - if (fmap[i] == '\n') {
1275 - *buf++ = '\r';
1276 - *buf++ = '\n';
1277 - } else
1278 - *buf++ = fmap[i];
1279 - } else
1280 - memcpy(buf, fmap + i, len - i);
1281 -
1282 - free(fmap);
1163 + cb.dlen = data->len;
1164 + cb.data = xmalloc(cb.dlen);
1165 + memcpy(cb.data, data->data, data->len);
1166
1167 d = 0;
1168 if (data->flags) {
@@ -1288,26 +1171,14 @@ static int imap_store_msg(struct store *gctx, struct msg_data *data, int *uid)
1171 }
1172 flagstr[d] = 0;
1173
1291 - if (!uid) {
1292 - box = gctx->conf->trash;
1293 - prefix = ctx->prefix;
1294 - cb.create = 1;
1295 - if (ctx->trashnc)
1296 - imap->caps = imap->rcaps & ~(1 << LITERALPLUS);
1297 - } else {
1298 - box = gctx->name;
1299 - prefix = !strcmp(box, "INBOX") ? "" : ctx->prefix;
1300 - cb.create = 0;
1301 - }
1302 - cb.ctx = uid;
1174 + box = gctx->name;
1175 + prefix = !strcmp(box, "INBOX") ? "" : ctx->prefix;
1176 + cb.create = 0;
1177 ret = imap_exec_m(ctx, &cb, "APPEND \"%s%s\" %s", prefix, box, flagstr);
1178 imap->caps = imap->rcaps;
1179 if (ret != DRV_OK)
1180 return ret;
1307 - if (!uid)
1308 - ctx->trashnc = 0;
1309 - else
1310 - gctx->count++;
1181 + gctx->count++;
1182
1183 return DRV_OK;
1184 }
@@ -1483,7 +1354,6 @@ int main(int argc, char **argv)
1354 {
1355 struct msg_data all_msgs, msg;
1356 struct store *ctx = NULL;
1486 - int uid = 0;
1357 int ofs = 0;
1358 int r;
1359 int total, n = 0;
@@ -1491,9 +1361,6 @@ int main(int argc, char **argv)
1361
1362 git_extract_argv0_path(argv[0]);
1363
1494 - /* init the random number generator */
1495 - arc4_init();
1496 -
1364 setup_git_directory_gently(&nongit_ok);
1365 git_config(git_imap_config, NULL);
1366
@@ -1540,7 +1407,7 @@ int main(int argc, char **argv)
1407 break;
1408 if (server.use_html)
1409 wrap_in_html(&msg);
1543 - r = imap_store_msg(ctx, &msg, &uid);
1410 + r = imap_store_msg(ctx, &msg);
1411 if (r != DRV_OK)
1412 break;
1413 n++;