]>
diplodocus.org Git - nmh/blob - uip/mhlogin.c
a08305c846d318fd0fa582b40fa4a131fa5db8ac
2 * mhlogin.c -- login to external (OAuth) services
4 * This code is Copyright (c) 2014, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
15 #define MHLOGIN_SWITCHES \
16 X("saslmech", 1, SASLMECHSW) \
17 X("authservice", -11, AUTHSERVICESW) \
18 X("snoop", 1, SNOOPSW) \
19 X("help", 1, HELPSW) \
20 X("version", 1, VERSIONSW) \
22 #define X(sw, minchars, id) id,
23 DEFINE_SWITCH_ENUM(MHLOGIN
);
26 #define X(sw, minchars, id) { sw, minchars, id },
27 DEFINE_SWITCH_ARRAY(MHLOGIN
, switches
);
31 /* XXX copied from install-mh.c */
36 static char line
[BUFSIZ
];
38 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
40 if ((cp
= strchr(line
, '\n')))
46 do_login(const char *svc
, int snoop
)
52 int failed_to_lock
= 0;
56 adios(NULL
, "missing -authservice switch");
59 if (!mh_oauth_new(&ctx
, svc
)) {
60 adios(NULL
, mh_oauth_get_err_string(ctx
));
64 mh_oauth_log_to(stderr
, ctx
);
67 fn
= getcpy(mh_oauth_cred_fn(ctx
));
69 if ((url
= mh_oauth_get_authorize_url(ctx
)) == NULL
) {
70 adios(NULL
, mh_oauth_get_err_string(ctx
));
73 printf("Load the following URL in your browser and authorize nmh"
76 "Enter the authorization code: ",
77 mh_oauth_svc_display_name(ctx
), url
);
81 while ((cred
= mh_oauth_authorize(code
, ctx
)) == NULL
82 && mh_oauth_get_err_code(ctx
) == MH_OAUTH_BAD_GRANT
) {
83 printf("Code rejected; try again? ");
88 advise(NULL
, "error exchanging code for OAuth2 token");
89 adios(NULL
, mh_oauth_get_err_string(ctx
));
92 cred_file
= lkfopendata(fn
, "w", &failed_to_lock
);
93 if (cred_file
== NULL
|| failed_to_lock
) {
96 if (!mh_oauth_cred_save(cred_file
, cred
)) {
97 adios(NULL
, mh_oauth_get_err_string(ctx
));
99 if (lkfclosedata(cred_file
, fn
) != 0) {
103 mh_oauth_cred_free(cred
);
111 main(int argc
, char **argv
)
113 char *cp
, **argp
, **arguments
;
114 char *saslmech
= NULL
, *svc
= NULL
;
117 if (nmh_init(argv
[0], 1)) { return 1; }
119 arguments
= getarguments (invo_name
, argc
, argv
, 1);
122 while ((cp
= *argp
++)) {
125 switch (smatch (++cp
, switches
)) {
127 ambigsw (cp
, switches
);
130 adios (NULL
, "-%s unknown", cp
);
133 snprintf(help
, sizeof(help
), "%s [switches]",
135 print_help (help
, switches
, 1);
138 print_version(invo_name
);
142 if (!(saslmech
= *argp
++) || *saslmech
== '-')
143 adios (NULL
, "missing argument to %s", argp
[-2]);
147 if (!(svc
= *argp
++) || *svc
== '-')
148 adios (NULL
, "missing argument to %s", argp
[-2]);
156 adios(NULL
, "extraneous arguments");
160 return do_login(svc
, snoop
);
164 adios(NULL
, "not built with OAuth support");