This repository was archived by the owner on Aug 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathchime-get-token.c
105 lines (90 loc) · 2.38 KB
/
chime-get-token.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Obtain a login token through the command line.
*/
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include "chime/chime-connection.h"
static GMainLoop *loop;
static int status;
static gchar *read_string(const char *prompt, gboolean echo)
{
#define MAX_LEN 128
struct termios conf, save;
char buf[MAX_LEN], *ret;
fputs(prompt, stdout);
fflush(stdout);
if (!echo) {
if (tcgetattr(STDIN_FILENO, &conf) == -1)
return NULL;
save = conf;
conf.c_lflag &= ~ECHO;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &conf) == -1)
return NULL;
}
ret = fgets(buf, MAX_LEN, stdin);
if (!echo) {
tcsetattr(STDIN_FILENO, TCSANOW, &save);
fputs("\n", stdout);
}
return g_strdup(g_strchomp(ret));
}
static void authenticate(ChimeConnection *conn, gpointer state, gboolean user_required)
{
gchar *user = NULL, *password;
if (user_required) {
user = read_string("Username: ", TRUE);
}
password = read_string("Password: ", FALSE);
chime_connection_authenticate(state, user, password);
g_free(user);
g_free(password);
}
static void token_acquired(ChimeConnection *conn, GParamSpec *pspec, gpointer ignored)
{
printf("Session token:\t%s\n", chime_connection_get_session_token(conn));
g_main_loop_quit(loop);
}
static void disconnected(ChimeConnection *conn, GError *error)
{
if (error) {
fprintf(stderr, "ERROR: %s\n", error->message);
status = EXIT_FAILURE;
} else {
status = EXIT_SUCCESS;
}
if (g_main_loop_is_running(loop))
g_main_loop_quit(loop);
}
static void connected(ChimeConnection *conn, gpointer display_name, gpointer ignored)
{
printf("Disconnecting...\n");
chime_connection_disconnect(conn);
}
int main(int argc, char *argv[])
{
ChimeConnection *conn;
gchar *account;
if (argc < 2)
account = read_string("Account e-mail: ", TRUE);
else
account = argv[1];
loop = g_main_loop_new(NULL, FALSE);
status = EXIT_SUCCESS;
conn = chime_connection_new(account, NULL, "foo", NULL);
g_signal_connect(conn, "authenticate",
G_CALLBACK(authenticate), NULL);
g_signal_connect(conn, "notify::session-token",
G_CALLBACK(token_acquired), NULL);
g_signal_connect(conn, "disconnected",
G_CALLBACK(disconnected), NULL);
g_signal_connect(conn, "connected",
G_CALLBACK(connected), NULL);
chime_connection_connect(conn);
g_main_loop_run(loop);
chime_connection_disconnect(conn);
g_object_unref(conn);
g_main_loop_unref(loop);
return status;
}