summaryrefslogtreecommitdiffstats
path: root/src/api.c
blob: b3a756a2d8e0499ce8dc7ea1e3cd62c356b9b0a0 (plain) (blame)
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
172
173
174
175
176
177
178
#define DC_LOGIN_FORMAT "{\"login\":\"%s\",\"password\":\"%s\",\"undelete\":false,\"captcha_key\":null,\"login_source\":null,\"gift_code_sku_id\":null}"
#define DC_USER_AGENT "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"
#define DC_SERVER_ADDRESS "localhost"
#define DC_API_PREFIX "/api/v9/"
#define DC_LWS_ABLE_TO_PARSE_HEADERS 1
/* libwebsockets information: libwebsockets works with event loops and discord.c primary targets debian for building (it must work on debian), but debian does not ship libwebsockets with glib or libevent event loop support, so loops are implemented in the classic poll() style. this reduces performance probably. if you use discord.c API with support for a platform specific event loop, you may rewrite LWS to do things differently. currently calls into API (dc_api_i and dc_api_o) will both do LWS stuff. */
void dc_api_stack (struct dc_api_io i) { /* stack output struct to be delivered via dc_api_o 2usr */
	DC_MR(i.program->api_ios);
	i.program->api_ios[i.program->api_ios_length] = malloc(sizeof(i));
	*(i.program->api_ios[i.program->api_ios_length++]) = i;
	return;
}
static int dc_lws_cb (struct lws * wsi, enum lws_callback_reasons rs, void * us, void * in, size_t len) {
	struct dc_lws_pass * pass = (struct dc_lws_pass *) us;
	switch (rs) {
		case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: /* TODO: handle and report somehow */
			lwsl_err("CLIENT_CONNECTION_ERROR: %s\n", in ? (char *) in : "(null)");
			break;
		case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: /* lws gives us len space after *in */
			unsigned char ** p = (unsigned char **) in, * end = (*p)+len;
			for (int i = 0; i < DC_LWS_HEADERS_LENGTH; i++)
				if (pass.headers[i][0])
					if (lws_add_http_header_by_name(wsi, (const unsigned char *) dc_lws_headers[i], (const unsigned char *) pass.headers[i], strlen(pass.headers[i]), p, end))
						return -1;
			if (!lws_http_is_redirected_to_get(wsi)) {
				lwsl_user("doing POST flow\n");
				lws_client_http_body_pending(wsi, 1);
				lws_callback_on_writable(wsi);
			} else
				lwsl_user("doing GET flow, got redirected - this is a bug, report!\n");
			break;
		case LWS_CALLBACK_CLIENT_HTTP_WRITEABLE: /* see minimal post example on how to send */
			if (lws_http_is_redirected_to_get(wsi)) /* multipart with files, for example */
				break;	/* for setting avatar pictures or uploading files. Yecch */
			lwsl_user("LWS_CALLBACK_CLIENT_HTTP_WRITABLE");
			if (!(pass.api_io.status & DC_BODY_SENT)) {
				if (lws_write(wsi, (uint8_t *) pass.body, pass.body_length, LWS_WRITE_HTTP) != (int) pass.body_length) /* TODO: fix to allow sending large bodies */
					return 1; /* we send body in one whole chunk OR FAIL! */
				pass.api_io.status |= DC_BODY_SENT;
				lws_callback_on_writable(wsi);
			} else {
				if (lws_write(wsi, NULL, 0, LWS_WRITE_HTTP_FINAL) == -1 /* ret snt by */)
					return 1; /* _FINAL is necessary to support H2, srv need know */
				lws_client_http_body_pending(wsi, 0);
			}
			if (pass.api_io.status & DC_MUST_FREE)
				free(body);
			break;
		case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:
			pass.status = lws_http_client_http_response(wsi); /* 200 OK, 404 Not Found, ...*/
			fprintf(stderr, "Connected with server response: %d\n", pass.status);
#ifdef DC_LWS_ABLE_TO_PARSE_HEADERS
			/* how to query custom headers (http 1.x only at the moment) */
			for (int i = 0; i < DC_LWS_HEADERS_LENGTH; i++) {
				pass.headers[i][0] = '\0'; /* to clear any headers we requested with */
				int n = lws_hdr_custom_length(wsi, dc_lws_headers[i], strlen(dc_lws_headers[i]));
				if (n < -1)
					lwsl_notice("No header %s.\n", dc_lws_headers[i]);
				else
					if (lws_hdr_custom_copy(wsi, pass.headers[i], sizeof(DC_MAX_HEADER_LENGTH), dc_lws_headers[i], strlen(dc_lws_headers[i])) < 0)
						lwsl_notice("Header %s too long.\n", dc_lws_headers[i]);
					else
						lwsl_notice("%s %s\n", dc_lws_headers[i], pass.headers[i]);
			}
#endif
			break;
		case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ: /* chunked body, without headers */
			lwsl_user("RECEIVE_CLIENT_HTTP_READ: read %d\n", (int) len);
			lwsl_hexdump_notice(in, len); /* for debugging purposes, kek */
			pass->body_length = len;
			pass->body = in;
			((char *)in+1)[len] = '\0'; /* NULL terminating note0 src/h.c */
			if (pass->api_io.pass != pass)
				fprintf(stderr, "[!!!] REPORT THIS BUG: pass->api_io.pass != pass");
			dc_api_i(pass.api_io);
			return 0; /* don't pass to lws_callback_http_dummy */
		case LWS_CALLBACK_RECEIVE_CLIENT_HTTP: /* uninterpreted http content */
			; /* we add 1 to buffer size for NULL terminanting, note0 src/h.c */
			char buffer[DC_LWS_BUF + LWS_PRE + 1 /* lws needs this many bytes b4 the ptr */];
			char * ptr = buffer + LWS_PRE; /* see, BEFORE the pointer - lower addr */
			int len2 /* DC_LWS_BUF again */ = sizeof(buffer) - LWS_PRE;
			if (lws_http_client_read(wsi, &ptr, &len2) < 0)
				return -1;
			return 0; /* don't pass to lws_callback_http_dummy */
		case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
			lwsl_user("LWS_CALLBACK_COMPLETED_CLIENT_HTTP\n");
			break;
		case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
			lwsl_user("LWS_CALLBACK_CLOSED_CLIENT_HTTP\n");
			break;
		case LWS_CALLBACK_WSI_DESTROY: /* if I understand the docs correctly, this is allways */
			free(pass); /* called at the final moment when user pointer and wsi is still */
		default: /* accessible, so we can now free the struct that was passed in as a heap ptr */
			break;
	}
	return lws_callback_http_dummy(wsi, rs, us, in, len);
}
void dc_api_i (struct dc_api_io i) { /* this function does not call attached functions, only output does that */
	if (!i.program)
		return;
	if (i.program->lws_context)
		lws_service(i.program->lws_context, 0);
	switch (i.type) {
		case DC_API_MESSAGE:
			break;
		case DC_API_CHANNEL:
			break;
		case DC_API_GUILD:
			break;
		case DC_API_LOGIN: /* library currently only works for a single client */
			if (!i.program->clients_length) /* already attempted login */
				DC_MR(i.program->clients);
			i.program->clients[0] = i.client;
			struct lws_client_connect_info info;
			memset(&info, 0, sizeof(info));
			info.context = i.program->lws_context;
			/* info.ssl_connection = LCCSCF_USE_SSL | LCCSCF_HTTP_MULTIPART_MIME; */
			info.port = 80;
			info.address = DC_SERVER_ADDRESS;
			info.path = DC_API_PREFIX "auth/login";
			info.host = info.address;
			info.origin = info.address;
			info.method = "POST";
			struct dc_lws_pass pass; /* for passing data to/from lws callback function */
			memset(&pass, 0, sizeof(pass));
			i.status |= DC_MUST_FREE;
			pass.body_length = smprintf(&pass.body, DC_LOGIN_FORMAT, i.client->email, i.client->password);
			i.type = DC_API_LOGIN_CB;
			memcpy(&pass.api_io, &i, sizeof(i))
			info.userdata = malloc(sizeof(pass));
			memcpy(info.userdata, &pass, sizeof(pass));
#ifdef DC_LWS_ABLE_TO_PARSE_HEADERS
			info.alpn = "http/1.1";
#endif
			info.protocol = dc_lws_protocols[0].name;
			if (i.client->authorization) { /* TODO: attempt cookie login without user/pass */
				;
			}
			lws_client_connect_via_info(&info);
			break;
		case DC_API_LOGIN_CB:
			fprintf(stderr, "DEBUG: %s\n", i.pass->body);
			break;
		case DC_API_REGISTER:
			break;
		case DC_API_STATUS:
			break;
		case DC_API_USER:
			break;
		case DC_API_ROLE:
			break;
		case DC_API_NONE:
			break;
		case DC_API_ATTACH:
			if (i.attached_function->type == DC_API_NONE)
				break;
			DC_MR(i.program->attached_functions); /* makes room for at least one */
			break;
	}
}
struct dc_api_io dc_api_o (struct dc_api_io i /* for ->program */) {
	if (i.program->lws_context)
		lws_service(i.program->lws_context, 0);
	struct dc_api_io o = {
		.type = DC_API_NONE,
		.program = i.program
	};
	/* dc_api_stack pop: */
	if (i.program->api_ios_length) {
		o = *(i.program->api_ios[--i.program->api_ios_length]);
		dc_api_io_free(i.program->api_ios[i.program->api_ios_length]);
	}
	for (size_t j = 0; j < i.program->attached_functions_length; j++) { /* call attached functions */
		if (i.program->attached_functions[j]->type == o.type) /* wow, it's possible to attach to NONE */
			i.program->attached_functions[j]->function(o, i.program->attached_functions[j]->user_data);
	}
	return o;
}