-/*
- * mhlogin.c -- login to external (OAuth) services
+/* mhlogin.c -- login to external (OAuth) services
*
* This code is Copyright (c) 2014, by the authors of nmh. See the
* COPYRIGHT file in the root directory of the nmh distribution for
#include <string.h>
#include <h/mh.h>
+#include <h/utils.h>
#include <h/oauth.h>
+#include "sbr/lock_file.h"
#define MHLOGIN_SWITCHES \
X("user username", 0, USERSW) \
static char *
geta (void)
{
- char *cp;
static char line[BUFSIZ];
if (fgets(line, sizeof(line), stdin) == NULL)
done (1);
- if ((cp = strchr(line, '\n')))
- *cp = 0;
+ trim_suffix_c(line, '\n');
+
return line;
}
}
if (!mh_oauth_new(&ctx, svc)) {
- adios(NULL, mh_oauth_get_err_string(ctx));
+ adios(NULL, "%s", mh_oauth_get_err_string(ctx));
}
if (snoop) {
mh_oauth_log_to(stderr, ctx);
}
- fn = getcpy(mh_oauth_cred_fn(svc));
+ 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));
+ adios(NULL, "%s", mh_oauth_get_err_string(ctx));
}
if (browser) {
fflush(stdout);
code = geta();
- while (!*code || ||
+ 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? ");
code = geta();
}
if (cred == NULL) {
- advise(NULL, "error exchanging code for OAuth2 token");
- adios(NULL, mh_oauth_get_err_string(ctx));
+ inform("error exchanging code for OAuth2 token");
+ adios(NULL, "%s", mh_oauth_get_err_string(ctx));
}
cred_file = lkfopendata(fn, "r+", &failed_to_lock);
adios(fn, "oops");
}
if (!mh_oauth_cred_save(cred_file, cred, user)) {
- adios(NULL, mh_oauth_get_err_string(ctx));
+ adios(NULL, "%s", mh_oauth_get_err_string(ctx));
}
if (lkfclosedata(cred_file, fn) != 0) {
adios (fn, "oops");