#endif
static int verbosity;
+static int list_folders;
static int use_curl = USE_CURL_DEFAULT;
static char *opt_folder;
-static const char * const imap_send_usage[] = { "git imap-send [-v] [-q] [--[no-]curl] [(--folder|-f) <folder>] < <mbox>", NULL };
+static char const * const imap_send_usage[] = {
+ N_("git imap-send [-v] [-q] [--[no-]curl] [(--folder|-f) <folder>] < <mbox>"),
+ "git imap-send --list",
+ NULL
+};
static struct option imap_send_options[] = {
OPT__VERBOSITY(&verbosity),
OPT_BOOL(0, "curl", &use_curl, "use libcurl to communicate with the IMAP server"),
OPT_STRING('f', "folder", &opt_folder, "folder", "specify the IMAP folder"),
+ OPT_BOOL(0, "list", &list_folders, "list all folders on the IMAP server"),
OPT_END()
};
if (b->buf[b->offset + 1] == '\n') {
b->buf[b->offset] = 0; /* terminate the string */
b->offset += 2; /* next line */
- if (0 < verbosity)
+ if ((0 < verbosity) || (list_folders && strstr(*s, "* LIST")))
puts(*s);
return 0;
}
return 0;
}
+static int list_imap_folders(struct imap_server_conf *server)
+{
+ struct imap_store *ctx = imap_open_store(server, "INBOX");
+ if (!ctx) {
+ fprintf(stderr, "failed to connect to IMAP server\n");
+ return 1;
+ }
+
+ fprintf(stderr, "Fetching the list of available folders...\n");
+ /* Issue the LIST command and print the results */
+ if (imap_exec(ctx, NULL, "LIST \"\" \"*\"") != RESP_OK) {
+ fprintf(stderr, "failed to list folders\n");
+ imap_close_store(ctx);
+ return 1;
+ }
+
+ imap_close_store(ctx);
+ return 0;
+}
+
#ifdef USE_CURL_FOR_IMAP_SEND
static CURL *setup_curl(struct imap_server_conf *srvc, struct credential *cred)
{
if (!path.len || path.buf[path.len - 1] != '/')
strbuf_addch(&path, '/');
- uri_encoded_folder = curl_easy_escape(curl, srvc->folder, 0);
- if (!uri_encoded_folder)
- die("failed to encode server folder");
- strbuf_addstr(&path, uri_encoded_folder);
- curl_free(uri_encoded_folder);
+ if (!list_folders) {
+ uri_encoded_folder = curl_easy_escape(curl, srvc->folder, 0);
+ if (!uri_encoded_folder)
+ die("failed to encode server folder");
+ strbuf_addstr(&path, uri_encoded_folder);
+ curl_free(uri_encoded_folder);
+ }
curl_easy_setopt(curl, CURLOPT_URL, path.buf);
strbuf_release(&path);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, srvc->ssl_verify);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, srvc->ssl_verify);
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
-
- curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
-
if (0 < verbosity || getenv("GIT_CURL_VERBOSE"))
http_trace_curl_no_data();
setup_curl_trace(curl);
struct credential cred = CREDENTIAL_INIT;
curl = setup_curl(server, &cred);
+
+ curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
+ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
+
curl_easy_setopt(curl, CURLOPT_READDATA, &msgbuf);
fprintf(stderr, "sending %d message%s\n", total, (total != 1) ? "s" : "");
return res != CURLE_OK;
}
+
+static int curl_list_imap_folders(struct imap_server_conf *server)
+{
+ CURL *curl;
+ CURLcode res = CURLE_OK;
+ struct credential cred = CREDENTIAL_INIT;
+
+ fprintf(stderr, "Fetching the list of available folders...\n");
+ curl = setup_curl(server, &cred);
+ res = curl_easy_perform(curl);
+
+ curl_easy_cleanup(curl);
+ curl_global_cleanup();
+
+ if (cred.username) {
+ if (res == CURLE_OK)
+ credential_approve(the_repository, &cred);
+ else if (res == CURLE_LOGIN_DENIED)
+ credential_reject(the_repository, &cred);
+ }
+
+ credential_clear(&cred);
+
+ return res != CURLE_OK;
+}
#endif
int cmd_main(int argc, const char **argv)
if (!server.port)
server.port = server.use_ssl ? 993 : 143;
- if (!server.folder) {
- fprintf(stderr, "no imap store specified\n");
- ret = 1;
- goto out;
- }
if (!server.host) {
if (!server.tunnel) {
fprintf(stderr, "no imap host specified\n");
server.host = xstrdup("tunnel");
}
+ if (list_folders) {
+ if (server.tunnel)
+ ret = list_imap_folders(&server);
+#ifdef USE_CURL_FOR_IMAP_SEND
+ else if (use_curl)
+ ret = curl_list_imap_folders(&server);
+#endif
+ else
+ ret = list_imap_folders(&server);
+ goto out;
+ }
+
+ if (!server.folder) {
+ fprintf(stderr, "no imap store specified\n");
+ ret = 1;
+ goto out;
+ }
+
/* read the messages */
if (strbuf_read(&all_msgs, 0, 0) < 0) {
error_errno(_("could not read from stdin"));