fetch-pack: refactor parsing in get_ack
[gitweb.git] / imap-send.c
index 6f5cc4f782ea82fc14ee596c7bac96e2b6f907e7..83a6ed2ac338d7fc82f3bed6fcb93ffe8dae5739 100644 (file)
@@ -23,9 +23,9 @@
  */
 
 #include "cache.h"
+#include "credential.h"
 #include "exec_cmd.h"
 #include "run-command.h"
-#include "prompt.h"
 #ifdef NO_OPENSSL
 typedef void *SSL;
 #endif
@@ -946,12 +946,13 @@ static int auth_cram_md5(struct imap_store *ctx, struct imap_cmd *cmd, const cha
 
 static struct imap_store *imap_open_store(struct imap_server_conf *srvc)
 {
+       struct credential cred = CREDENTIAL_INIT;
        struct imap_store *ctx;
        struct imap *imap;
        char *arg, *rsp;
        int s = -1, preauth;
 
-       ctx = xcalloc(sizeof(*ctx), 1);
+       ctx = xcalloc(1, sizeof(*ctx));
 
        ctx->imap = imap = xcalloc(sizeof(*imap), 1);
        imap->buf.sock.fd[0] = imap->buf.sock.fd[1] = -1;
@@ -1096,25 +1097,23 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc)
                }
 #endif
                imap_info("Logging in...\n");
-               if (!srvc->user) {
-                       fprintf(stderr, "Skipping server %s, no user\n", srvc->host);
-                       goto bail;
-               }
-               if (!srvc->pass) {
-                       struct strbuf prompt = STRBUF_INIT;
-                       strbuf_addf(&prompt, "Password (%s@%s): ", srvc->user, srvc->host);
-                       arg = git_getpass(prompt.buf);
-                       strbuf_release(&prompt);
-                       if (!*arg) {
-                               fprintf(stderr, "Skipping account %s@%s, no password\n", srvc->user, srvc->host);
-                               goto bail;
-                       }
-                       /*
-                        * getpass() returns a pointer to a static buffer.  make a copy
-                        * for long term storage.
-                        */
-                       srvc->pass = xstrdup(arg);
+               if (!srvc->user || !srvc->pass) {
+                       cred.protocol = xstrdup(srvc->use_ssl ? "imaps" : "imap");
+                       cred.host = xstrdup(srvc->host);
+
+                       if (srvc->user)
+                               cred.username = xstrdup(srvc->user);
+                       if (srvc->pass)
+                               cred.password = xstrdup(srvc->pass);
+
+                       credential_fill(&cred);
+
+                       if (!srvc->user)
+                               srvc->user = xstrdup(cred.username);
+                       if (!srvc->pass)
+                               srvc->pass = xstrdup(cred.password);
                }
+
                if (CAP(NOLOGIN)) {
                        fprintf(stderr, "Skipping account %s@%s, server forbids LOGIN\n", srvc->user, srvc->host);
                        goto bail;
@@ -1153,10 +1152,18 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc)
                }
        } /* !preauth */
 
+       if (cred.username)
+               credential_approve(&cred);
+       credential_clear(&cred);
+
        ctx->prefix = "";
        return ctx;
 
 bail:
+       if (cred.username)
+               credential_reject(&cred);
+       credential_clear(&cred);
+
        imap_close_store(ctx);
        return NULL;
 }
@@ -1263,7 +1270,7 @@ static int count_messages(struct strbuf *all_msgs)
        char *p = all_msgs->buf;
 
        while (1) {
-               if (!prefixcmp(p, "From ")) {
+               if (starts_with(p, "From ")) {
                        p = strstr(p+5, "\nFrom: ");
                        if (!p) break;
                        p = strstr(p+7, "\nDate: ");
@@ -1297,7 +1304,7 @@ static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs)
        data = &all_msgs->buf[*ofs];
        len = all_msgs->len - *ofs;
 
-       if (len < 5 || prefixcmp(data, "From "))
+       if (len < 5 || !starts_with(data, "From "))
                return 0;
 
        p = strchr(data, '\n');
@@ -1339,13 +1346,13 @@ static int git_imap_config(const char *key, const char *val, void *cb)
        if (!strcmp("folder", key)) {
                imap_folder = xstrdup(val);
        } else if (!strcmp("host", key)) {
-               if (!prefixcmp(val, "imap:"))
+               if (starts_with(val, "imap:"))
                        val += 5;
-               else if (!prefixcmp(val, "imaps:")) {
+               else if (starts_with(val, "imaps:")) {
                        val += 6;
                        server.use_ssl = 1;
                }
-               if (!prefixcmp(val, "//"))
+               if (starts_with(val, "//"))
                        val += 2;
                server.host = xstrdup(val);
        } else if (!strcmp("user", key))