imap_send: setup_curl: retreive credentials if not set in config file

Up to this point, the curl mode only supported getting the username and password from the gitconfig file while the legacy mode could also fetch them using the credential API. Signed-off-by: Nicolas Morey-Chaisemartin <nicolas@morey-chaisemartin.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nicolas Morey-Chaisemartin committed Sep 14, 2017 at 09:52 UTC 19079b3e7cdb6f14646e838cb04919e8eb9976a9
1 file changed +17 -2
imap-send.c
+17 -2
@@ -1398,7 +1398,7 @@ static int append_msgs_to_imap(struct imap_server_conf *server,
1398 }
1399
1400 #ifdef USE_CURL_FOR_IMAP_SEND
1401 -static CURL *setup_curl(struct imap_server_conf *srvc)
1401 +static CURL *setup_curl(struct imap_server_conf *srvc, struct credential *cred)
1402 {
1403 CURL *curl;
1404 struct strbuf path = STRBUF_INIT;
@@ -1411,6 +1411,7 @@ static CURL *setup_curl(struct imap_server_conf *srvc)
1411 if (!curl)
1412 die("curl_easy_init failed");
1413
1414 + server_fill_credential(&server, cred);
1415 curl_easy_setopt(curl, CURLOPT_USERNAME, server.user);
1416 curl_easy_setopt(curl, CURLOPT_PASSWORD, server.pass);
1417
@@ -1460,8 +1461,9 @@ static int curl_append_msgs_to_imap(struct imap_server_conf *server,
1461 struct buffer msgbuf = { STRBUF_INIT, 0 };
1462 CURL *curl;
1463 CURLcode res = CURLE_OK;
1464 + struct credential cred = CREDENTIAL_INIT;
1465
1464 - curl = setup_curl(server);
1466 + curl = setup_curl(server, &cred);
1467 curl_easy_setopt(curl, CURLOPT_READDATA, &msgbuf);
1468
1469 fprintf(stderr, "sending %d message%s\n", total, (total != 1) ? "s" : "");
@@ -1496,6 +1498,19 @@ static int curl_append_msgs_to_imap(struct imap_server_conf *server,
1498 curl_easy_cleanup(curl);
1499 curl_global_cleanup();
1500
1501 + if (cred.username) {
1502 + if (res == CURLE_OK)
1503 + credential_approve(&cred);
1504 +#if LIBCURL_VERSION_NUM >= 0x070d01
1505 + else if (res == CURLE_LOGIN_DENIED)
1506 +#else
1507 + else
1508 +#endif
1509 + credential_reject(&cred);
1510 + }
1511 +
1512 + credential_clear(&cred);
1513 +
1514 return res != CURLE_OK;
1515 }
1516 #endif