]> diplodocus.org Git - nmh/blobdiff - uip/mhlogin.c
Alter HasSuffixC()'s char * to be const.
[nmh] / uip / mhlogin.c
index 093c4f0acd72a6043374b32b7fbff6711c72746d..ffa44f3f0a3bee6ea0d499a34631c85e4d206513 100644 (file)
@@ -6,13 +6,16 @@
  * complete copyright information.
  */
 
  * complete copyright information.
  */
 
+#include <errno.h>
 #include <stdio.h>
 #include <string.h>
 
 #include <h/mh.h>
 #include <stdio.h>
 #include <string.h>
 
 #include <h/mh.h>
+#include <h/utils.h>
 #include <h/oauth.h>
 
 #define MHLOGIN_SWITCHES \
 #include <h/oauth.h>
 
 #define MHLOGIN_SWITCHES \
+    X("user username", 0, USERSW) \
     X("saslmech", 0, SASLMECHSW) \
     X("authservice", 0, AUTHSERVICESW) \
     X("browser", 0, BROWSERSW) \
     X("saslmech", 0, SASLMECHSW) \
     X("authservice", 0, AUTHSERVICESW) \
     X("browser", 0, BROWSERSW) \
@@ -33,18 +36,17 @@ DEFINE_SWITCH_ARRAY(MHLOGIN, switches);
 static char *
 geta (void)
 {
 static char *
 geta (void)
 {
-    char *cp;
     static char line[BUFSIZ];
 
     if (fgets(line, sizeof(line), stdin) == NULL)
        done (1);
     static char line[BUFSIZ];
 
     if (fgets(line, sizeof(line), stdin) == NULL)
        done (1);
-    if ((cp = strchr(line, '\n')))
-       *cp = 0;
+    TrimSuffixC(line, '\n');
+
     return line;
 }
 
 static int
     return line;
 }
 
 static int
-do_login(const char *svc, const char *browser, int snoop)
+do_login(const char *svc, const char *user, const char *browser, int snoop)
 {
     char *fn, *code;
     mh_oauth_ctx *ctx;
 {
     char *fn, *code;
     mh_oauth_ctx *ctx;
@@ -57,6 +59,10 @@ do_login(const char *svc, const char *browser, int snoop)
         adios(NULL, "missing -authservice switch");
     }
 
         adios(NULL, "missing -authservice switch");
     }
 
+    if (user == NULL) {
+        adios(NULL, "missing -user switch");
+    }
+
     if (!mh_oauth_new(&ctx, svc)) {
         adios(NULL, mh_oauth_get_err_string(ctx));
     }
     if (!mh_oauth_new(&ctx, svc)) {
         adios(NULL, mh_oauth_get_err_string(ctx));
     }
@@ -65,7 +71,7 @@ do_login(const char *svc, const char *browser, int snoop)
         mh_oauth_log_to(stderr, ctx);
     }
 
         mh_oauth_log_to(stderr, ctx);
     }
 
-    fn = getcpy(mh_oauth_cred_fn(ctx));
+    fn = mh_xstrdup(mh_oauth_cred_fn(svc));
 
     if ((url = mh_oauth_get_authorize_url(ctx)) == NULL) {
       adios(NULL, mh_oauth_get_err_string(ctx));
 
     if ((url = mh_oauth_get_authorize_url(ctx)) == NULL) {
       adios(NULL, mh_oauth_get_err_string(ctx));
@@ -95,9 +101,10 @@ do_login(const char *svc, const char *browser, int snoop)
     fflush(stdout);
     code = geta();
 
     fflush(stdout);
     code = geta();
 
-    while ((cred = mh_oauth_authorize(code, ctx)) == NULL
-           && mh_oauth_get_err_code(ctx) == MH_OAUTH_BAD_GRANT) {
-      printf("Code rejected; try again? ");
+    while (!*code ||
+           ((cred = mh_oauth_authorize(code, ctx)) == NULL
+            && mh_oauth_get_err_code(ctx) == MH_OAUTH_BAD_GRANT)) {
+      printf(!*code  ?  "Empty code; try again? "  :  "Code rejected; try again? ");
       fflush(stdout);
       code = geta();
     }
       fflush(stdout);
       code = geta();
     }
@@ -106,11 +113,14 @@ do_login(const char *svc, const char *browser, int snoop)
       adios(NULL, mh_oauth_get_err_string(ctx));
     }
 
       adios(NULL, mh_oauth_get_err_string(ctx));
     }
 
-    cred_file = lkfopendata(fn, "w", &failed_to_lock);
+    cred_file = lkfopendata(fn, "r+", &failed_to_lock);
+    if (cred_file == NULL && errno == ENOENT) {
+        cred_file = lkfopendata(fn, "w+", &failed_to_lock);
+    }
     if (cred_file == NULL || failed_to_lock) {
       adios(fn, "oops");
     }
     if (cred_file == NULL || failed_to_lock) {
       adios(fn, "oops");
     }
-    if (!mh_oauth_cred_save(cred_file, cred)) {
+    if (!mh_oauth_cred_save(cred_file, cred, user)) {
       adios(NULL, mh_oauth_get_err_string(ctx));
     }
     if (lkfclosedata(cred_file, fn) != 0) {
       adios(NULL, mh_oauth_get_err_string(ctx));
     }
     if (lkfclosedata(cred_file, fn) != 0) {
@@ -129,7 +139,7 @@ int
 main(int argc, char **argv)
 {
     char *cp, **argp, **arguments;
 main(int argc, char **argv)
 {
     char *cp, **argp, **arguments;
-    const char *saslmech = NULL, *svc = NULL, *browser = NULL;
+    const char *user = NULL, *saslmech = NULL, *svc = NULL, *browser = NULL;
     int snoop = 0;
 
     if (nmh_init(argv[0], 1)) { return 1; }
     int snoop = 0;
 
     if (nmh_init(argv[0], 1)) { return 1; }
@@ -156,6 +166,11 @@ main(int argc, char **argv)
                print_version(invo_name);
                done (0);
 
                print_version(invo_name);
                done (0);
 
+           case USERSW:
+               if (!(user = *argp++) || *user == '-')
+                   adios (NULL, "missing argument to %s", argp[-2]);
+               continue;
+
            case SASLMECHSW:
                if (!(saslmech = *argp++) || *saslmech == '-')
                    adios (NULL, "missing argument to %s", argp[-2]);
            case SASLMECHSW:
                if (!(saslmech = *argp++) || *saslmech == '-')
                    adios (NULL, "missing argument to %s", argp[-2]);
@@ -186,7 +201,7 @@ main(int argc, char **argv)
     free(arguments);
 
 #ifdef OAUTH_SUPPORT
     free(arguments);
 
 #ifdef OAUTH_SUPPORT
-    return do_login(svc, browser, snoop);
+    return do_login(svc, user, browser, snoop);
 #else
     NMH_UNUSED(svc);
     NMH_UNUSED(browser);
 #else
     NMH_UNUSED(svc);
     NMH_UNUSED(browser);