| 1626 | |
| 1627 | #ifdef USE_CURL_FOR_IMAP_SEND |
| 1628 | static CURL *setup_curl(struct imap_server_conf *srvc, struct credential *cred) |
| 1629 | { |
| 1630 | CURL *curl; |
| 1631 | struct strbuf path = STRBUF_INIT; |
| 1632 | char *uri_encoded_folder; |
| 1633 | |
| 1634 | if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) |
| 1635 | die("curl_global_init failed"); |
| 1636 | |
| 1637 | curl = curl_easy_init(); |
| 1638 | |
| 1639 | if (!curl) |
| 1640 | die("curl_easy_init failed"); |
| 1641 | |
| 1642 | server_fill_credential(srvc, cred); |
| 1643 | curl_easy_setopt(curl, CURLOPT_USERNAME, srvc->user); |
| 1644 | |
| 1645 | /* |
| 1646 | * Use CURLOPT_PASSWORD irrespective of whether there is |
| 1647 | * an auth method specified or not, unless it's OAuth2.0, |
| 1648 | * where we use CURLOPT_XOAUTH2_BEARER. |
| 1649 | */ |
| 1650 | if (!srvc->auth_method || |
| 1651 | (strcmp(srvc->auth_method, "XOAUTH2") && |
| 1652 | strcmp(srvc->auth_method, "OAUTHBEARER"))) |
| 1653 | curl_easy_setopt(curl, CURLOPT_PASSWORD, srvc->pass); |
| 1654 | |
| 1655 | strbuf_addstr(&path, srvc->use_ssl ? "imaps://" : "imap://"); |
| 1656 | strbuf_addstr(&path, srvc->host); |
| 1657 | if (!path.len || path.buf[path.len - 1] != '/') |
| 1658 | strbuf_addch(&path, '/'); |
| 1659 | |
| 1660 | if (!list_folders) { |
| 1661 | uri_encoded_folder = curl_easy_escape(curl, srvc->folder, 0); |
| 1662 | if (!uri_encoded_folder) |
| 1663 | die("failed to encode server folder"); |
| 1664 | strbuf_addstr(&path, uri_encoded_folder); |
| 1665 | curl_free(uri_encoded_folder); |
| 1666 | } |
| 1667 | |
| 1668 | curl_easy_setopt(curl, CURLOPT_URL, path.buf); |
| 1669 | strbuf_release(&path); |
| 1670 | curl_easy_setopt(curl, CURLOPT_PORT, (long)srvc->port); |
| 1671 | |
| 1672 | if (srvc->auth_method) { |
| 1673 | if (!strcmp(srvc->auth_method, "XOAUTH2") || |
| 1674 | !strcmp(srvc->auth_method, "OAUTHBEARER")) { |
| 1675 | |
| 1676 | /* |
| 1677 | * While CURLOPT_XOAUTH2_BEARER looks as if it only supports XOAUTH2, |
| 1678 | * upon debugging, it has been found that it is capable of detecting |
| 1679 | * the best option out of OAUTHBEARER and XOAUTH2. |
| 1680 | */ |
| 1681 | curl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, srvc->pass); |
| 1682 | } else { |
| 1683 | struct strbuf auth = STRBUF_INIT; |
| 1684 | strbuf_addstr(&auth, "AUTH="); |
| 1685 | strbuf_addstr(&auth, srvc->auth_method); |
no test coverage detected