aboutsummaryrefslogtreecommitdiff
path: root/client/acronc/main.c
blob: 760b4265aec8e28df976ca3b83006e10f284375c (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Created by yuuta on 7/23/22.
 */

#include "log.h"
#include "handler.h"
#include "config.h"
#include "helpers.h"
#include "client.h"

#include <libac.h>
#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
#include <string.h>

#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif

static uv_loop_t lop;
uv_loop_t *loop = &lop;
static int current_req = -1;
static bool tty = true;

static void on_close(uv_handle_t *handle);

static ac_connection_parameters_t params = {
        .sock = NULL,
        .version = 0,
        .port = 25575,
        .host = NULL,
        .id = NULL,
        .token = NULL,
        .on_send = NULL,
};

static void cleanup(void) {
    LOGD("cleanup()");
    /* sock must be closed before cleanup(). */
    uv_loop_close(loop);
    ac_free();
    uv_tty_reset_mode();
}

static void on_stdin_closed(void) {
    sock_ext(true);
}

static void on_sock_closed(void) {
    uv_stop(loop);
}

static void prompt(void) {
    if (tty) {
        fprintf(stderr, "%s@%s > ",
                params.id,
                params.host);
    }
}

static int on_input(ac_request_t *req) {
    if (req->type == AC_REQUEST_CMD && ((ac_request_cmd_t *) req)->cmd[0] == '\0') {
        prompt();
        return 0;
    }
    current_req = req->id;
    stdin_stop();
    LOGD("Stdin is paused.");
    /* The socket will close itself upon errors. So do the input stream. */
    return sock_request(req);
}

static int on_sock_ready(void) {
    prompt();
    return h_stdin(on_input, on_stdin_closed);
}

static int on_recv(ac_obj_t *obj) {
    bool p = false;
    if (AC_IS_RESPONSE(obj->type)) {
        ac_response_t *resp = (ac_response_t *) obj;
        if (resp->id == current_req) {
            if (resp->type == AC_RESPONSE_ERROR ||
            resp->type == AC_RESPONSE_CMD_RESULT) {
                int r = stdin_start();
                if (r) {
                    LOGEV("Cannot resume stdin: %s", uv_strerror(r));
                } else {
                    LOGD("Stdin is resumed.");
                    p = true;
                }
            }
        }
    }
    const int retval = handle_object(obj);
    if (p) {
        prompt();
    }
    return retval;
}

static void on_resolv(int status, const struct addrinfo *ai, void (*on_connected)(bool)) {
    if (status) {
        uv_stop(loop);
        return;
    }

    if (h_socket(&params,
                      ai,
                      on_connected,
                      on_sock_ready,
                      on_recv,
                      on_sock_closed)) {
        /* Mark as true to clean up resources and prevent next retry. */
        on_connected(true);
        uv_stop(loop);
    }
}

static void on_int(void) {
    current_req = -1;
    int r = stdin_start();
    if (r && r != UV_EALREADY) {
        LOGEV("Cannot resume stdin: %s", uv_strerror(r));
    } else {
        printf("Interrupt\n");
        prompt();
    }
}

int main(int argc, const char **argv) {
    int r;

    if ((r = config_parse(argc, argv, &params))) return r;
    atexit(cleanup);
    if (!isatty(fileno(stdin))) {
        tty = false;
    }
    libac_config_t config = {
#ifdef DEBUG
            .out = NULL,
#else
            .out = NULL,
#endif
            .tok = NULL
    };
    if ((r = ac_init(&config))) {
	    LOGEV("Cannot initialize Acron library: %d", r);
	    return r;
    }

    if ((r = uv_loop_init(loop))) goto uviniterr;
    if ((r = h_signal(on_int, on_stdin_closed))) goto uviniterr;
    if ((r = a_dns(params.host, params.port, on_resolv))) goto uviniterr;

    if ((r = uv_run(loop, UV_RUN_DEFAULT))) {
        if (r == 1) {
            /* Seems to return 1 if uv_stop is called. */
            return 0;
        }
        LOGEV("Cannot run: %s", uv_strerror(r));
	return -r;
    }
    return 0;
    uviniterr:
    LOGEV("Cannot initialize: %s", uv_strerror(r));
    return -r;
}