Skip to content

Commit 297c59d

Browse files
AdityaGarg8gitster
authored andcommitted
imap-send: add ability to list the available folders
Various IMAP servers have different ways to name common folders. For example, the folder where all deleted messages are stored is often named "[Gmail]/Trash" on Gmail servers, and "Deleted" on Outlook. Similarly, the Drafts folder is simply named "Drafts" on Outlook, but on Gmail it is named "[Gmail]/Drafts". This commit adds a `--list` command to the `imap-send` tool that lists the available folders on the IMAP server, allowing users to see which folders are available and how they are named. A sample output looks like this when run against a Gmail server: Fetching the list of available folders... * LIST (\HasNoChildren) "/" "INBOX" * LIST (\HasChildren \Noselect) "/" "[Gmail]" * LIST (\All \HasNoChildren) "/" "[Gmail]/All Mail" * LIST (\Drafts \HasNoChildren) "/" "[Gmail]/Drafts" * LIST (\HasNoChildren \Important) "/" "[Gmail]/Important" * LIST (\HasNoChildren \Sent) "/" "[Gmail]/Sent Mail" * LIST (\HasNoChildren \Junk) "/" "[Gmail]/Spam" * LIST (\Flagged \HasNoChildren) "/" "[Gmail]/Starred" * LIST (\HasNoChildren \Trash) "/" "[Gmail]/Trash" For OpenSSL, this is achived by running the 'IMAP LIST' command and parsing the response. This command is specified in RFC6154: https://datatracker.ietf.org/doc/html/rfc6154#section-5.1 For libcurl, the example code published in the libcurl documentation is used to implement this functionality: https://curl.se/libcurl/c/imap-list.html Signed-off-by: Aditya Garg <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 15b4e9b commit 297c59d

File tree

2 files changed

+87
-17
lines changed

2 files changed

+87
-17
lines changed

Documentation/git-imap-send.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SYNOPSIS
1010
--------
1111
[verse]
1212
'git imap-send' [-v] [-q] [--[no-]curl] [(--folder|-f) <folder>]
13+
'git imap-send' --list
1314

1415

1516
DESCRIPTION
@@ -54,6 +55,8 @@ OPTIONS
5455
using libcurl. Ignored if Git was built with the NO_OPENSSL option
5556
set.
5657

58+
--list::
59+
Run the IMAP LIST command to output a list of all the folders present.
5760

5861
CONFIGURATION
5962
-------------
@@ -123,7 +126,8 @@ it. Alternatively, use OAuth2.0 authentication as described below.
123126

124127
[NOTE]
125128
You might need to instead use: `folder = "[Google Mail]/Drafts"` if you get an error
126-
that the "Folder doesn't exist".
129+
that the "Folder doesn't exist". You can also run `git imap-send --list` to get a
130+
list of available folders.
127131

128132
[NOTE]
129133
If your Gmail account is set to another language than English, the name of the "Drafts"

imap-send.c

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,21 @@
4545
#endif
4646

4747
static int verbosity;
48+
static int list_folders = 0;
4849
static int use_curl = USE_CURL_DEFAULT;
4950
static char *opt_folder = NULL;
5051

51-
static const char * const imap_send_usage[] = { "git imap-send [-v] [-q] [--[no-]curl] [(--folder|-f) <folder>] < <mbox>", NULL };
52+
static char const * const imap_send_usage[] = {
53+
N_("git imap-send [-v] [-q] [--[no-]curl] [(--folder|-f) <folder>] < <mbox>"),
54+
"git imap-send --list",
55+
NULL
56+
};
5257

5358
static struct option imap_send_options[] = {
5459
OPT__VERBOSITY(&verbosity),
5560
OPT_BOOL(0, "curl", &use_curl, "use libcurl to communicate with the IMAP server"),
5661
OPT_STRING('f', "folder", &opt_folder, "folder", "specify the IMAP folder"),
62+
OPT_BOOL(0, "list", &list_folders, "list all folders on the IMAP server"),
5763
OPT_END()
5864
};
5965

@@ -429,7 +435,7 @@ static int buffer_gets(struct imap_buffer *b, char **s)
429435
if (b->buf[b->offset + 1] == '\n') {
430436
b->buf[b->offset] = 0; /* terminate the string */
431437
b->offset += 2; /* next line */
432-
if (0 < verbosity)
438+
if ((0 < verbosity) || (list_folders && strstr(*s, "* LIST")))
433439
puts(*s);
434440
return 0;
435441
}
@@ -1626,6 +1632,26 @@ static int append_msgs_to_imap(struct imap_server_conf *server,
16261632
return 0;
16271633
}
16281634

1635+
static int list_imap_folders(struct imap_server_conf *server)
1636+
{
1637+
struct imap_store *ctx = imap_open_store(server, "INBOX");
1638+
if (!ctx) {
1639+
fprintf(stderr, "failed to connect to IMAP server\n");
1640+
return 1;
1641+
}
1642+
1643+
fprintf(stderr, "Fetching the list of available folders...\n");
1644+
/* Issue the LIST command and print the results */
1645+
if (imap_exec(ctx, NULL, "LIST \"\" \"*\"") != RESP_OK) {
1646+
fprintf(stderr, "failed to list folders\n");
1647+
imap_close_store(ctx);
1648+
return 1;
1649+
}
1650+
1651+
imap_close_store(ctx);
1652+
return 0;
1653+
}
1654+
16291655
#ifdef USE_CURL_FOR_IMAP_SEND
16301656
static CURL *setup_curl(struct imap_server_conf *srvc, struct credential *cred)
16311657
{
@@ -1654,11 +1680,13 @@ static CURL *setup_curl(struct imap_server_conf *srvc, struct credential *cred)
16541680
if (!path.len || path.buf[path.len - 1] != '/')
16551681
strbuf_addch(&path, '/');
16561682

1657-
uri_encoded_folder = curl_easy_escape(curl, srvc->folder, 0);
1658-
if (!uri_encoded_folder)
1659-
die("failed to encode server folder");
1660-
strbuf_addstr(&path, uri_encoded_folder);
1661-
curl_free(uri_encoded_folder);
1683+
if (!list_folders) {
1684+
uri_encoded_folder = curl_easy_escape(curl, srvc->folder, 0);
1685+
if (!uri_encoded_folder)
1686+
die("failed to encode server folder");
1687+
strbuf_addstr(&path, uri_encoded_folder);
1688+
curl_free(uri_encoded_folder);
1689+
}
16621690

16631691
curl_easy_setopt(curl, CURLOPT_URL, path.buf);
16641692
strbuf_release(&path);
@@ -1689,10 +1717,6 @@ static CURL *setup_curl(struct imap_server_conf *srvc, struct credential *cred)
16891717
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, srvc->ssl_verify);
16901718
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, srvc->ssl_verify);
16911719

1692-
curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
1693-
1694-
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
1695-
16961720
if (0 < verbosity || getenv("GIT_CURL_VERBOSE"))
16971721
http_trace_curl_no_data();
16981722
setup_curl_trace(curl);
@@ -1711,6 +1735,10 @@ static int curl_append_msgs_to_imap(struct imap_server_conf *server,
17111735
struct credential cred = CREDENTIAL_INIT;
17121736

17131737
curl = setup_curl(server, &cred);
1738+
1739+
curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
1740+
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
1741+
17141742
curl_easy_setopt(curl, CURLOPT_READDATA, &msgbuf);
17151743

17161744
fprintf(stderr, "Sending %d message%s to %s folder...\n",
@@ -1757,6 +1785,31 @@ static int curl_append_msgs_to_imap(struct imap_server_conf *server,
17571785

17581786
return res != CURLE_OK;
17591787
}
1788+
1789+
static int curl_list_imap_folders(struct imap_server_conf *server)
1790+
{
1791+
CURL *curl;
1792+
CURLcode res = CURLE_OK;
1793+
struct credential cred = CREDENTIAL_INIT;
1794+
1795+
fprintf(stderr, "Fetching the list of available folders...\n");
1796+
curl = setup_curl(server, &cred);
1797+
res = curl_easy_perform(curl);
1798+
1799+
curl_easy_cleanup(curl);
1800+
curl_global_cleanup();
1801+
1802+
if (cred.username) {
1803+
if (res == CURLE_OK)
1804+
credential_approve(the_repository, &cred);
1805+
else if (res == CURLE_LOGIN_DENIED)
1806+
credential_reject(the_repository, &cred);
1807+
}
1808+
1809+
credential_clear(&cred);
1810+
1811+
return res != CURLE_OK;
1812+
}
17601813
#endif
17611814

17621815
int cmd_main(int argc, const char **argv)
@@ -1797,11 +1850,6 @@ int cmd_main(int argc, const char **argv)
17971850
if (!server.port)
17981851
server.port = server.use_ssl ? 993 : 143;
17991852

1800-
if (!server.folder) {
1801-
fprintf(stderr, "no IMAP store specified\n");
1802-
ret = 1;
1803-
goto out;
1804-
}
18051853
if (!server.host) {
18061854
if (!server.tunnel) {
18071855
fprintf(stderr, "no IMAP host specified\n");
@@ -1811,6 +1859,24 @@ int cmd_main(int argc, const char **argv)
18111859
server.host = xstrdup("tunnel");
18121860
}
18131861

1862+
if (list_folders) {
1863+
if (server.tunnel)
1864+
ret = list_imap_folders(&server);
1865+
#ifdef USE_CURL_FOR_IMAP_SEND
1866+
else if (use_curl)
1867+
ret = curl_list_imap_folders(&server);
1868+
#endif
1869+
else
1870+
ret = list_imap_folders(&server);
1871+
goto out;
1872+
}
1873+
1874+
if (!server.folder) {
1875+
fprintf(stderr, "no IMAP store specified\n");
1876+
ret = 1;
1877+
goto out;
1878+
}
1879+
18141880
/* read the messages */
18151881
if (strbuf_read(&all_msgs, 0, 0) < 0) {
18161882
error_errno(_("could not read from stdin"));

0 commit comments

Comments
 (0)