summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Makefile6
-rw-r--r--README.md1
-rw-r--r--lor416
-rw-r--r--src/api.c266
-rw-r--r--src/i18n.h4
6 files changed, 584 insertions, 111 deletions
diff --git a/.gitignore b/.gitignore
index 6097dbc..b2108a9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,6 @@
lib/cJSON.h
lib/cJSON.c
+lib/linenoise.c
+lib/linenoise.h
# the following is the binary
discord.c
diff --git a/Makefile b/Makefile
index 253209f..cb37143 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,9 @@
default:
- gcc -g -Ilib -Isrc -I. src/api.c -lcurl -odiscord.c
+ gcc -g -Ilib -Isrc -I. -pthread src/api.c -lcurl -odiscord.c
prepare:
wget https://raw.githubusercontent.com/DaveGamble/cJSON/master/cJSON.c -O lib/cJSON.c
wget https://raw.githubusercontent.com/DaveGamble/cJSON/master/cJSON.h -O lib/cJSON.h
+ wget https://raw.githubusercontent.com/antirez/linenoise/master/linenoise.h -O lib/linenoise.h
+ wget https://raw.githubusercontent.com/antirez/linenoise/master/linenoise.c -O lib/linenoise.c
clean:
- rm lib/cJSON.c lib/cJSON.h
+ rm lib/cJSON.c lib/cJSON.h lib/linenoise.h lib/linenoise.c
diff --git a/README.md b/README.md
index f6c9ba6..243c05d 100644
--- a/README.md
+++ b/README.md
@@ -9,6 +9,7 @@ an alternative client for the discord messaging platform, written in the C progr
* GNU compiler collection
* GNU Make
* libcurl 7.17.0 or newer with HTTPS support
+* [linenoise](//github.com/antirez/linenoise) library
## instructions
diff --git a/lor b/lor
new file mode 100644
index 0000000..222f4aa
--- /dev/null
+++ b/lor
@@ -0,0 +1,416 @@
+gcc -g -Ilib -Isrc -I. -pthread src/api.c -lcurl -odiscord.c
+src/api.c: In function ‘dc_client_init’:
+src/api.c:167:64: error: pasting "->" and "c" does not give a valid preprocessing token
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~
+src/api.c:168:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->authorization);
+ ^~~~~~~
+src/api.c:167:64: error: ‘struct dc_client’ has no member named ‘c’
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~
+src/api.c:168:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->authorization);
+ ^~~~~~~
+src/api.c:167:40: error: too many arguments to function ‘pthread_rwlock_destroy’
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~~~~~~~~~~~~~~~~~~~~~
+src/api.c:168:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->authorization);
+ ^~~~~~~
+In file included from src/api.c:16:
+/usr/include/pthread.h:894:12: note: declared here
+ extern int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock)
+ ^~~~~~~~~~~~~~~~~~~~~~
+src/api.c:167:64: error: pasting "->" and "c" does not give a valid preprocessing token
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~
+src/api.c:169:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->username);
+ ^~~~~~~
+src/api.c:167:64: error: ‘struct dc_client’ has no member named ‘c’
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~
+src/api.c:169:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->username);
+ ^~~~~~~
+src/api.c:167:40: error: too many arguments to function ‘pthread_rwlock_destroy’
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~~~~~~~~~~~~~~~~~~~~~
+src/api.c:169:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->username);
+ ^~~~~~~
+In file included from src/api.c:16:
+/usr/include/pthread.h:894:12: note: declared here
+ extern int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock)
+ ^~~~~~~~~~~~~~~~~~~~~~
+src/api.c:167:64: error: pasting "->" and "c" does not give a valid preprocessing token
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~
+src/api.c:170:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->guilds);
+ ^~~~~~~
+src/api.c:167:64: error: ‘struct dc_client’ has no member named ‘c’
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~
+src/api.c:170:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->guilds);
+ ^~~~~~~
+src/api.c:167:40: error: too many arguments to function ‘pthread_rwlock_destroy’
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~~~~~~~~~~~~~~~~~~~~~
+src/api.c:170:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->guilds);
+ ^~~~~~~
+In file included from src/api.c:16:
+/usr/include/pthread.h:894:12: note: declared here
+ extern int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock)
+ ^~~~~~~~~~~~~~~~~~~~~~
+src/api.c:167:64: error: pasting "->" and "c" does not give a valid preprocessing token
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~
+src/api.c:171:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->errors);
+ ^~~~~~~
+src/api.c:167:64: error: ‘struct dc_client’ has no member named ‘c’
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~
+src/api.c:171:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->errors);
+ ^~~~~~~
+src/api.c:167:40: error: too many arguments to function ‘pthread_rwlock_destroy’
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~~~~~~~~~~~~~~~~~~~~~
+src/api.c:171:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->errors);
+ ^~~~~~~
+In file included from src/api.c:16:
+/usr/include/pthread.h:894:12: note: declared here
+ extern int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock)
+ ^~~~~~~~~~~~~~~~~~~~~~
+src/api.c:167:64: error: pasting "->" and "c" does not give a valid preprocessing token
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~
+src/api.c:172:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->sent_messages);
+ ^~~~~~~
+src/api.c:167:64: error: ‘struct dc_client’ has no member named ‘c’
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~
+src/api.c:172:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->sent_messages);
+ ^~~~~~~
+src/api.c:167:40: error: too many arguments to function ‘pthread_rwlock_destroy’
+ #define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(c->##name##_lock, NULL)
+ ^~~~~~~~~~~~~~~~~~~~~~
+src/api.c:172:2: note: in expansion of macro ‘DC_CILD’
+ DC_CILD(c->sent_messages);
+ ^~~~~~~
+In file included from src/api.c:16:
+/usr/include/pthread.h:894:12: note: declared here
+ extern int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock)
+ ^~~~~~~~~~~~~~~~~~~~~~
+src/api.c: In function ‘dc_fetch_guilds’:
+src/api.c:403:29: error: expected ‘;’ before ‘{’ token
+ DC_CWLE(c, c->guilds_lock) {rs = -5; goto rc;}
+ ^
+In file included from src/api.c:11:
+src/api.c:405:21: error: ‘guild’ undeclared (first use in this function); did you mean ‘dc_guild’?
+ cJSON_ArrayForEach(guild, json) {
+ ^~~~~
+lib/cJSON.h:283:48: note: in definition of macro ‘cJSON_ArrayForEach’
+ #define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
+ ^~~~~~~
+src/api.c:405:21: note: each undeclared identifier is reported only once for each function it appears in
+ cJSON_ArrayForEach(guild, json) {
+ ^~~~~
+lib/cJSON.h:283:48: note: in definition of macro ‘cJSON_ArrayForEach’
+ #define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
+ ^~~~~~~
+src/api.c:424:28: error: expected ‘;’ before ‘{’ token
+ DC_CUE(c, c->guilds_lock) {rs = -6; goto rc;}
+ ^
+src/api.c:401:3: error: label ‘rc’ used but not defined
+ goto rc;
+ ^~~~
+src/api.c: In function ‘dc_fetch_channels’:
+src/api.c:435:26: warning: initialization of ‘struct dc_channel *’ from incompatible pointer type ‘struct dc_client *’ [-Wincompatible-pointer-types]
+ struct dc_channel * c = g->client;
+ ^
+src/api.c:438:7: error: ‘struct dc_channel’ has no member named ‘discriminator’
+ if (c->discriminator < 0)
+ ^~
+src/api.c:439:22: warning: passing argument 1 of ‘dc_login’ from incompatible pointer type [-Wincompatible-pointer-types]
+ if ((rs = dc_login(c)) < 0) {
+ ^
+src/api.c:262:34: note: expected ‘struct dc_client *’ but argument is of type ‘struct dc_channel *’
+ int dc_login (struct dc_client * c) { /* noui */
+ ~~~~~~~~~~~~~~~~~~~^
+src/api.c:22:46: error: ‘struct dc_channel’ has no member named ‘errors’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:49: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:440:4: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "dc_login(c) " DC_I18N_FAILED " (%d)", rs);
+ ^~~~~~~~~~~~~~~
+src/api.c:22:60: error: ‘struct dc_channel’ has no member named ‘errors_sizeof’; did you mean ‘messages_sizeof’?
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~~~~~~~~~~~~
+src/api.c:21:52: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:440:4: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "dc_login(c) " DC_I18N_FAILED " (%d)", rs);
+ ^~~~~~~~~~~~~~~
+src/api.c:22:76: error: ‘struct dc_channel’ has no member named ‘errors_lock’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:55: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:440:4: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "dc_login(c) " DC_I18N_FAILED " (%d)", rs);
+ ^~~~~~~~~~~~~~~
+In file included from /usr/include/x86_64-linux-gnu/curl/curl.h:2826,
+ from src/api.c:6:
+src/api.c:449:20: error: ‘struct dc_channel’ has no member named ‘curl’
+ curl_easy_setopt(c->curl, CURLOPT_URL, url);
+ ^~
+src/api.c:450:20: error: ‘struct dc_channel’ has no member named ‘curl’
+ curl_easy_setopt(c->curl, CURLOPT_HTTPGET, 1L);
+ ^~
+src/api.c:451:20: error: ‘struct dc_channel’ has no member named ‘curl’
+ curl_easy_setopt(c->curl, CURLOPT_WRITEDATA, &s);
+ ^~
+src/api.c:452:27: error: ‘struct dc_channel’ has no member named ‘curl’
+ res = curl_easy_perform(c->curl);
+ ^~
+src/api.c:22:46: error: ‘struct dc_channel’ has no member named ‘errors’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:49: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:454:3: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "curl_easy_perform(c->curl) " DC_I18N_FAILED ": %s", curl_easy_strerror(res));
+ ^~~~~~~~~~~~~~~
+src/api.c:22:60: error: ‘struct dc_channel’ has no member named ‘errors_sizeof’; did you mean ‘messages_sizeof’?
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~~~~~~~~~~~~
+src/api.c:21:52: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:454:3: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "curl_easy_perform(c->curl) " DC_I18N_FAILED ": %s", curl_easy_strerror(res));
+ ^~~~~~~~~~~~~~~
+src/api.c:22:76: error: ‘struct dc_channel’ has no member named ‘errors_lock’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:55: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:454:3: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "curl_easy_perform(c->curl) " DC_I18N_FAILED ": %s", curl_easy_strerror(res));
+ ^~~~~~~~~~~~~~~
+src/api.c:22:46: error: ‘struct dc_channel’ has no member named ‘errors’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:49: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:462:4: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "cJSON_Parse " DC_I18N_FAILED ": " DC_I18N_JSON_ERROR_BEFORE ": %s", error_ptr);
+ ^~~~~~~~~~~~~~~
+src/api.c:22:60: error: ‘struct dc_channel’ has no member named ‘errors_sizeof’; did you mean ‘messages_sizeof’?
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~~~~~~~~~~~~
+src/api.c:21:52: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:462:4: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "cJSON_Parse " DC_I18N_FAILED ": " DC_I18N_JSON_ERROR_BEFORE ": %s", error_ptr);
+ ^~~~~~~~~~~~~~~
+src/api.c:22:76: error: ‘struct dc_channel’ has no member named ‘errors_lock’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:55: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:462:4: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "cJSON_Parse " DC_I18N_FAILED ": " DC_I18N_JSON_ERROR_BEFORE ": %s", error_ptr);
+ ^~~~~~~~~~~~~~~
+src/api.c:22:46: error: ‘struct dc_channel’ has no member named ‘errors’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:49: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:464:4: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "cJSON_Parse " DC_I18N_FAILED);
+ ^~~~~~~~~~~~~~~
+src/api.c:22:60: error: ‘struct dc_channel’ has no member named ‘errors_sizeof’; did you mean ‘messages_sizeof’?
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~~~~~~~~~~~~
+src/api.c:21:52: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:464:4: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "cJSON_Parse " DC_I18N_FAILED);
+ ^~~~~~~~~~~~~~~
+src/api.c:22:76: error: ‘struct dc_channel’ has no member named ‘errors_lock’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:55: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:464:4: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "cJSON_Parse " DC_I18N_FAILED);
+ ^~~~~~~~~~~~~~~
+src/api.c:22:46: error: ‘struct dc_channel’ has no member named ‘errors’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:49: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:470:3: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "!cJSON_IsArray(json), s.ptr = %s", s.ptr);
+ ^~~~~~~~~~~~~~~
+src/api.c:22:60: error: ‘struct dc_channel’ has no member named ‘errors_sizeof’; did you mean ‘messages_sizeof’?
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~~~~~~~~~~~~
+src/api.c:21:52: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:470:3: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "!cJSON_IsArray(json), s.ptr = %s", s.ptr);
+ ^~~~~~~~~~~~~~~
+src/api.c:22:76: error: ‘struct dc_channel’ has no member named ‘errors_lock’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:55: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:470:3: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "!cJSON_IsArray(json), s.ptr = %s", s.ptr);
+ ^~~~~~~~~~~~~~~
+src/api.c:475:18: error: ‘struct dc_channel’ has no member named ‘guilds_lock’
+ if (DC_CWLE(c, c->guilds_lock)) {rs = -7; goto rc;}
+ ^~
+src/api.c:26:49: note: in definition of macro ‘DC_CWLE’
+ #define DC_CWLE(c, name) (pthread_rwlock_wrlock(name) ? (DC_CLIENT_ERROR(c, DC_I18N_LOCKING " " #name " " DC_I18N_FAILED) || 1) : 0)
+ ^~~~
+src/api.c:22:46: error: ‘struct dc_channel’ has no member named ‘errors’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:49: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:26:58: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ #define DC_CWLE(c, name) (pthread_rwlock_wrlock(name) ? (DC_CLIENT_ERROR(c, DC_I18N_LOCKING " " #name " " DC_I18N_FAILED) || 1) : 0)
+ ^~~~~~~~~~~~~~~
+src/api.c:475:6: note: in expansion of macro ‘DC_CWLE’
+ if (DC_CWLE(c, c->guilds_lock)) {rs = -7; goto rc;}
+ ^~~~~~~
+src/api.c:22:60: error: ‘struct dc_channel’ has no member named ‘errors_sizeof’; did you mean ‘messages_sizeof’?
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~~~~~~~~~~~~
+src/api.c:21:52: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:26:58: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ #define DC_CWLE(c, name) (pthread_rwlock_wrlock(name) ? (DC_CLIENT_ERROR(c, DC_I18N_LOCKING " " #name " " DC_I18N_FAILED) || 1) : 0)
+ ^~~~~~~~~~~~~~~
+src/api.c:475:6: note: in expansion of macro ‘DC_CWLE’
+ if (DC_CWLE(c, c->guilds_lock)) {rs = -7; goto rc;}
+ ^~~~~~~
+src/api.c:22:76: error: ‘struct dc_channel’ has no member named ‘errors_lock’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:55: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:26:58: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ #define DC_CWLE(c, name) (pthread_rwlock_wrlock(name) ? (DC_CLIENT_ERROR(c, DC_I18N_LOCKING " " #name " " DC_I18N_FAILED) || 1) : 0)
+ ^~~~~~~~~~~~~~~
+src/api.c:475:6: note: in expansion of macro ‘DC_CWLE’
+ if (DC_CWLE(c, c->guilds_lock)) {rs = -7; goto rc;}
+ ^~~~~~~
+In file included from src/api.c:11:
+src/api.c:476:21: error: ‘channel’ undeclared (first use in this function); did you mean ‘dc_channel’?
+ cJSON_ArrayForEach(channel, json) {
+ ^~~~~~~
+lib/cJSON.h:283:48: note: in definition of macro ‘cJSON_ArrayForEach’
+ #define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
+ ^~~~~~~
+src/api.c:22:46: error: ‘struct dc_channel’ has no member named ‘errors’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:49: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:482:4: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "!id || !name || type == NAN");
+ ^~~~~~~~~~~~~~~
+src/api.c:22:60: error: ‘struct dc_channel’ has no member named ‘errors_sizeof’; did you mean ‘messages_sizeof’?
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~~~~~~~~~~~~
+src/api.c:21:52: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:482:4: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "!id || !name || type == NAN");
+ ^~~~~~~~~~~~~~~
+src/api.c:22:76: error: ‘struct dc_channel’ has no member named ‘errors_lock’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:55: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:482:4: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ DC_CLIENT_ERROR(c, "!id || !name || type == NAN");
+ ^~~~~~~~~~~~~~~
+src/api.c:503:17: error: ‘struct dc_channel’ has no member named ‘guilds_lock’
+ if (DC_CUE(c, c->guilds_lock)) {rs = -8; goto rc;}
+ ^~
+src/api.c:28:48: note: in definition of macro ‘DC_CUE’
+ #define DC_CUE(c, name) (pthread_rwlock_unlock(name) ?(DC_CLIENT_ERROR(c, DC_I18N_UNLOCKING " " #name " " DC_I18N_FAILED) || 1) : 0)
+ ^~~~
+src/api.c:22:46: error: ‘struct dc_channel’ has no member named ‘errors’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:49: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:28:56: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ #define DC_CUE(c, name) (pthread_rwlock_unlock(name) ?(DC_CLIENT_ERROR(c, DC_I18N_UNLOCKING " " #name " " DC_I18N_FAILED) || 1) : 0)
+ ^~~~~~~~~~~~~~~
+src/api.c:503:6: note: in expansion of macro ‘DC_CUE’
+ if (DC_CUE(c, c->guilds_lock)) {rs = -8; goto rc;}
+ ^~~~~~
+src/api.c:22:60: error: ‘struct dc_channel’ has no member named ‘errors_sizeof’; did you mean ‘messages_sizeof’?
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~~~~~~~~~~~~
+src/api.c:21:52: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:28:56: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ #define DC_CUE(c, name) (pthread_rwlock_unlock(name) ?(DC_CLIENT_ERROR(c, DC_I18N_UNLOCKING " " #name " " DC_I18N_FAILED) || 1) : 0)
+ ^~~~~~~~~~~~~~~
+src/api.c:503:6: note: in expansion of macro ‘DC_CUE’
+ if (DC_CUE(c, c->guilds_lock)) {rs = -8; goto rc;}
+ ^~~~~~
+src/api.c:22:76: error: ‘struct dc_channel’ has no member named ‘errors_lock’
+ #define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+ ^~
+src/api.c:21:55: note: in definition of macro ‘DC_ERROR’
+ #define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+ ^
+src/api.c:28:56: note: in expansion of macro ‘DC_CLIENT_ERROR’
+ #define DC_CUE(c, name) (pthread_rwlock_unlock(name) ?(DC_CLIENT_ERROR(c, DC_I18N_UNLOCKING " " #name " " DC_I18N_FAILED) || 1) : 0)
+ ^~~~~~~~~~~~~~~
+src/api.c:503:6: note: in expansion of macro ‘DC_CUE’
+ if (DC_CUE(c, c->guilds_lock)) {rs = -8; goto rc;}
+ ^~~~~~
+make: *** [Makefile:2: default] Error 1
diff --git a/src/api.c b/src/api.c
index 93d9094..4cb7174 100644
--- a/src/api.c
+++ b/src/api.c
@@ -1,5 +1,6 @@
-#pragma once
-#define _XOPEN_SOURCE
+#define _XOPEN_SOURCE 600
+#define _POSIX_C_SOURCE 200809L
+#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <curl/curl.h>
@@ -17,11 +18,14 @@
#define DC_API_PREFIX "https://discord.com/api/v8/" /* this can be a format string, DO NOT use format characters inside */
#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_ERROR(e, s, m, ...) dc_push_error(e, s, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
-#define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
+#define DC_ERROR(e, s, l, m, ...) dc_push_error(e, s, l, __func__, __FILE__, __LINE__, 0##__VA_OPT__(1), m __VA_OPT__(,) __VA_ARGS__)
+#define DC_CLIENT_ERROR(c, m, ...) DC_ERROR(c->errors, &c->errors_sizeof, c->errors_lock, m __VA_OPT__(,) __VA_ARGS__) /* yeah, that m is not a typo */
#define DC_API(curl, body, endpoint, ...) dc_api(curl, body, 0##__VA_OPT__(1), endpoint __VA_OPT__(,) __VA_ARGS__)
#define cJSON_GetObjectItem2(root, name1, name2) (cJSON_GetObjectItem(root, name1) ? cJSON_GetObjectItem(cJSON_GetObjectItem(root, name1), name2) : NULL)
#define DC_TIMESTAMP_FORMAT "%Y-%m-%dT%H:%M:%S.XXXXXX%z"
+#define DC_CWLE(c, name) (pthread_rwlock_wrlock(name) ? (DC_CLIENT_ERROR(c, DC_I18N_LOCKING " " #name " " DC_I18N_FAILED) || 1) : 0)
+#define DC_CRLE(c, name) (pthread_rwlock_rdlock(name) ? (DC_CLIENT_ERROR(c, DC_I18N_LOCKING " " #name " " DC_I18N_FAILED) || 1) : 0)
+#define DC_CUE(c, name) (pthread_rwlock_unlock(name) ?(DC_CLIENT_ERROR(c, DC_I18N_UNLOCKING " " #name " " DC_I18N_FAILED) || 1) : 0)
/*
struct members comment information:
- noui: the ui thread must not use the member (for example curl pointers are noui)
@@ -30,22 +34,25 @@
- yesfree: the (string) pointer is of a dynamically-allocated string, free it before losing pointer to the struct (most strings)
- nouiw: ui must not write
- noapiw: api must not write
+ - nolock: no lock is required if only reading is done after a single write to the memory area or only one thread accesses it
guidelines:
- when threading, you must use mutexes to prevent accessing structs (and their pointers because of realloc!) (optionaly with timeouts that are optionaly reported with DC_CLIENT_ERROR)
- error reporting function shall not report an error to itself when mutex lock times out for obvious reasons
- - _sizeofs shall represent the size of arrays in atomic types and when appending to the array shall be incremented **after** the element is appended, so reading threads may function normally
- - keep arrays as arrays of pointers, not direct structs, as that allows reading an array when appending is in progress by another thread
- - threading and races 101: when popping arrays, always lock in a non-shared way, same obviously applies for sorting, but not necessarily for appending (when only a shared lock is needed)
+ - _sizeofs shall represent the size of arrays in atomic types
+ - keep arrays as arrays of pointers, not direct structs, as that allows pointing to individual elements without them changing pointer addresses
+ - threading and races 101: when popping arrays, always lock in a non-shared way, same obviously applies for sorting, writing, appending (because of reallocs of pointer arrays)
mutexes:
- mutexes for child members are kept in the struct with the object that is being modified. if realloc is used on strings or those child objects (pointers change), the whole struct must be locked.
- - when reading arrays of pointers, you shared lock it, you also shared lock it when appending
+ - when reading arrays of pointers, you shared lock it
- when writing, lock the memory in a non shared way, take note of the pointer in case of realloc
struct pointed to from multiple sections (a great example is a channel)
- when a channel is created, DO NOT destroy it - never free channels if you don't free messages, never free guilds if you don't free channels. that's because channels in guilds are backpointed to parent guilds and messages are backpointed to channels, realloc would lead to a segfault.
- - use posixes pthread_rwlock_init(3POSIX) mutexes to allow reading and appending to arrays and other types
+ - use posixes pthread_rwlock_init(3POSIX) mutexes to allow reading to arrays and other types
+ - do not have the illusion that you can append to an array of pointers without non-shared locking, because you realloc that array of pointers, appending must also be non-shared locked
+ - _free functions shall never fail - do not use timed locks
TODO: make it work.
- i changed arrays from type arrays to type pointer arrays, so code that allocates must be reimplemented, also struct free functions and -> instead of . in array elements
- - make mutex locks in api (and ui thread)
+ - make mutex locks in api and ui thread
- make the ui thread
*/
struct dc_error {
@@ -56,7 +63,7 @@ struct dc_error {
time_t time;
short unsigned int reported /* 0: error was not yet shown to the user, 1: error was already shown to the user */;
};
-int dc_error_free(struct dc_error * e) {
+int dc_error_free(struct dc_error * e) { /* noui, noapi, nolock - only called by dc_client_free */
free(e->message); e->message = NULL; /* other strings are static */
e->line = 0;
e->function = NULL;
@@ -69,12 +76,12 @@ struct dc_message {
int discriminator;
char * content; /* yesfree */
time_t time;
- struct dc_channel * channel; /* nofree */
+ _Atomic(struct dc_channel *) channel; /* nofree */
unsigned short int tts;
unsigned long long int id; /* nouiw */
unsigned short int status; /* noapiw - 0 if it was not yet printed on the display, 1 if it was */
};
-int dc_message_free (struct dc_message * m) {
+int dc_message_free (struct dc_message * m) { /* noui, noapi, nolock - only called by dc_channel_free */
free(m->username); m->username = NULL;
free(m->content); m->content = NULL;
m->channel = NULL;
@@ -85,8 +92,8 @@ int dc_message_free (struct dc_message * m) {
free(m);
}
int dc_message_compare (const void * a, const void * b) {
- struct dc_message * c = (struct dc_message *) a;
- struct dc_message * d = (struct dc_message *) b;
+ struct dc_message * c = *(struct dc_message **) a;
+ struct dc_message * d = *(struct dc_message **) b;
/* strežnik v arrayu najprej pošlje sporočila iz sedanjosti, nato tista iz preteklosti, zato bo tudi sortiran array takšen */
return c->time > d->time ? -1 : c->time < d->time ? 1 : 0;
}
@@ -94,17 +101,18 @@ struct dc_channel {
char * name; /* yesfree, nouiw */
char * topic; /* yesfree, nouiw */
unsigned long long int id; /* nouiw */
- struct dc_guild * guild; /* nofree, nouiw */
+ _Atomic(struct dc_guild *) guild; /* nofree, nouiw */
struct dc_message ** messages; /* yesfree, nouiw */
_Atomic(size_t) messages_sizeof; /* nouiw */
};
-int dc_channel_free (struct dc_channel * ch) {
+int dc_channel_free (struct dc_channel * ch) { /* noui, noapi, nolock - only called by dc_guild_free */
free(ch->name); ch->name = NULL;
free(ch->topic); ch->topic = NULL;
ch->guild = NULL;
ch->id = 0;
for (int i = 0; i < ch->messages_sizeof; i++)
dc_message_free(ch->messages[i]);
+ free(ch->messages);
ch->messages_sizeof = 0;
free(ch);
}
@@ -113,32 +121,59 @@ struct dc_guild {
unsigned long long id; /* nouiw */
_Atomic(size_t) channels_sizeof; /* nouiw */
struct dc_channel ** channels; /* yesfree, nouiw */
+ _Atomic(struct dc_client *) client; /* nofree - obviously */
};
-int dc_guild_free (struct dc_guild * g) {
+int dc_guild_free (struct dc_guild * g) { /* noui, noapi, nolock - only called by dc_client_free */
free(g->name); g->name = NULL;
g->id = 0;
for (int i = 0; i < g->channels_sizeof; i++)
dc_channel_free(g->channels[i]);
+ free(g->channels);
g->channels_sizeof = 0;
free(g);
}
struct dc_client {
- CURL * curl; /* noui */
- struct curl_slist * curl_headers; /* noui */
- char * authorization; /* noui */
- char * email; /* yesfree, noapiw */
- char * password; /* yesfree, noapiw */
- char * username; /* yesfree, nouiw */
- int discriminator; /* nouiw */
+ CURL * curl; /* noui, nolock */
+ struct curl_slist * curl_headers; /* noui, nolock */
+ char * authorization; /* nouiw */
+ pthread_rwlock_t * authorization_lock;
+ char * email; /* yesfree, noapiw, nolock */
+ char * password; /* yesfree, noapiw, nolock */
+ char * username; /* yesfree, nouiw */
+ pthread_rwlock_t * username_lock;
+ _Atomic(int) discriminator; /* nouiw - < 0 indicates that the user is not logged in - set last in login procedure */
struct dc_guild ** guilds; /* yesfree, nouiw */
_Atomic(size_t) guilds_sizeof; /* nouiw */
- struct dc_channel * joinedchannel; /* nofree, nouiw */
+ pthread_rwlock_t * guilds_lock;
+ _Atomic(struct dc_channel *) joinedchannel; /* nofree, noapiw */
struct dc_error ** errors; /* yesfree */
_Atomic(size_t) errors_sizeof;
+ pthread_rwlock_t * errors_lock;
struct dc_message ** sent_messages; /* yesfree - ui appends, api pops and moves to messages */
_Atomic(size_t) sent_messages_sizeof;
+ pthread_rwlock_t * sent_messages_lock;
};
-int dc_client_free (struct dc_client * c) {
+struct dc_client * dc_client_init () { /* gives you a prepared dc_client */
+ struct dc_client * c = calloc(1, sizeof(struct dc_client));
+ c->discriminator = -1;
+#define DC_CILI(name) /* Client Init Lock Init */ if (pthread_rwlock_init(name##_lock, NULL)) goto fuck;
+ DC_CILI(c->authorization);
+ DC_CILI(c->username);
+ DC_CILI(c->guilds);
+ DC_CILI(c->errors);
+ DC_CILI(c->sent_messages);
+ return c;
+ fuck:
+#define DC_CILD(name) if (name##_lock) pthread_rwlock_destroy(name##_lock)
+ DC_CILD(c->authorization);
+ DC_CILD(c->username);
+ DC_CILD(c->guilds);
+ DC_CILD(c->errors);
+ DC_CILD(c->sent_messages);
+ free(c);
+ return NULL; /* don't even check for this, if pthread_rwlock_init fails we'll segfault */
+}
+int dc_client_free (struct dc_client * c) { /* noui, noapi, nolock - only called by main on exit */
curl_easy_cleanup(c->curl);
curl_slist_free_all(c->curl_headers);
free(c->authorization); c->authorization = NULL;
@@ -149,17 +184,23 @@ int dc_client_free (struct dc_client * c) {
c->joinedchannel = NULL;
for (int i = 0; i < c->guilds_sizeof; i++)
dc_guild_free(c->guilds[i]);
+ free(c->guilds);
c->guilds_sizeof = 0;
for (int i = 0; i < c->errors_sizeof; i++)
dc_error_free(c->errors[i]);
+ free(c->errors);
c->errors_sizeof = 0;
for (int i = 0; i < c->sent_messages_sizeof; c++)
dc_message_free(c->sent_messages[i]);
+ free(c->sent_messages);
c->sent_messages_sizeof = 0;
free(c);
}
-int dc_push_error (struct dc_error * e, size_t * s, const char * c, char * f, size_t l, unsigned short int isfmt, char * m, ...) {
- e = realloc(e, sizeof(struct dc_error)*++*s); /* note: format arguments are evaluated twice */
+int dc_push_error (struct dc_error ** e, _Atomic size_t * s, pthread_rwlock_t * lock, const char * c, char * f, size_t l, unsigned short int isfmt, char * m, ...) {
+ if (lock && pthread_rwlock_wrlock(lock))
+ return -1; /* does not report an error as that may make things even worse. I could try writing to stderr here but meh */
+ e = realloc(e, sizeof(struct dc_error *)*++*s); /* note: format arguments are evaluated twice */
+ e[*s-1] = malloc(sizeof(struct dc_error));
size_t strlenm = strlen(m);
size_t va_count = parse_printf_format(m, 0, NULL);
if (isfmt && va_count > 0) {
@@ -167,19 +208,21 @@ int dc_push_error (struct dc_error * e, size_t * s, const char * c, char * f, si
va_start(ap, m);
va_copy(ap2, ap);
strlenm = vsnprintf(NULL, 0, m, ap);
- e[*s-1].message = malloc(sizeof(char)*strlenm+1);
- vsnprintf(e[*s-1].message, strlenm+1, m, ap2);
+ e[*s-1]->message = malloc(sizeof(char)*strlenm+1);
+ vsnprintf(e[*s-1]->message, strlenm+1, m, ap2);
va_end(ap);
va_end(ap2);
} else {
- e[*s-1].message = malloc(sizeof(char)*strlenm+1);
- strcpy(e[*s-1].message, m);
+ e[*s-1]->message = malloc(sizeof(char)*strlenm+1);
+ strcpy(e[*s-1]->message, m);
}
- e[*s-1].file = f;
- e[*s-1].line = l;
- e[*s-1].function = c /* Caller */;
- e[*s-1].time = time(NULL);
- e[*s-1].reported = 0;
+ e[*s-1]->file = f;
+ e[*s-1]->line = l;
+ e[*s-1]->function = c /* Caller */;
+ e[*s-1]->time = time(NULL);
+ e[*s-1]->reported = 0;
+ if (lock && pthread_rwlock_unlock(lock))
+ return -2;
return 1;
}
cJSON * dc_api (CURL * curl, char * body, int isfmt, char * endpoint, ...) { /* note: format arguments are evaluated twice */
@@ -216,7 +259,7 @@ cJSON * dc_api (CURL * curl, char * body, int isfmt, char * endpoint, ...) { /*
free(s.ptr); s.ptr = NULL;
return json;
}
-int dc_login (struct dc_client * c) {
+int dc_login (struct dc_client * c) { /* noui */
int rs = 1;
struct writefunc_string s;
char * data = NULL;
@@ -268,11 +311,13 @@ int dc_login (struct dc_client * c) {
rs = -6;
goto rc;
}
+ if (DC_CWLE(c, c->authorization_lock)) {rs = -7; goto rc;}
c->authorization = realloc(c->authorization, strlen(token->valuestring)+1);
strcpy(c->authorization, token->valuestring);
data = realloc(data, strlen(c->authorization)+strlen("Authorization: ")+1);
strcpy(data, "Authorization: ");
strcat(data, c->authorization);
+ if (DC_CUE(c, c->authorization_lock)) {rs = -8; goto rc;}
free(s.ptr); s.ptr = NULL;
init_writefunc_string(&s);
curl_easy_setopt(c->curl, CURLOPT_URL, DC_API_PREFIX "users/@me");
@@ -282,7 +327,7 @@ int dc_login (struct dc_client * c) {
res = curl_easy_perform(c->curl);
if (res != CURLE_OK) {
DC_CLIENT_ERROR(c, "curl_easy_perform() " DC_I18N_FAILED ": %s", curl_easy_strerror(res));
- rs = -7;
+ rs = -9;
goto rc;
}
cJSON_Delete(json);
@@ -294,18 +339,20 @@ int dc_login (struct dc_client * c) {
} else {
DC_CLIENT_ERROR(c, "cJSON_Parse " DC_I18N_FAILED);
}
- rs = -8;
+ rs = -10;
goto rc;
}
token = cJSON_GetObjectItem(json, "username");
cJSON * token2 = cJSON_GetObjectItem(json, "discriminator");
if (!cJSON_IsString(token) || !cJSON_IsString(token2)) {
DC_CLIENT_ERROR(c, "!cJSON_IsString(token) || !cJSON_IsString(token2)");
- rs = -9;
+ rs = -11;
goto rc;
}
+ if (DC_CWLE(c, c->username_lock)) {rs = -11; goto rc;}
c->username = realloc(c->username, strlen(token->valuestring)+1);
strcpy(c->username, token->valuestring);
+ if (DC_CUE(c, c->username_lock)) {rs = -12; goto rc;}
c->discriminator = strtol(token2->valuestring, NULL, 10);
rc:
free(s.ptr); s.ptr = NULL;
@@ -322,7 +369,7 @@ int dc_fetch_guilds (struct dc_client * c) {
char * value2 = NULL;
CURLcode res;
cJSON * json = NULL;
- if (!c->username || !c->username[0])
+ if (c->discriminator < 0)
if ((rs = dc_login(c)) < 0) {
DC_CLIENT_ERROR(c, "dc_login(c) " DC_I18N_FAILED " (%d)", rs);
return -1;
@@ -353,49 +400,52 @@ int dc_fetch_guilds (struct dc_client * c) {
rs = -4;
goto rc;
}
- for (int i = 0; i < c->guilds_sizeof; i++)
- dc_guild_free(&c->guilds[i]);
- c->guilds = NULL;
- c->guilds_sizeof = 0;
+ if(DC_CWLE(c, c->guilds_lock)) {rs = -5; goto rc;}
cJSON * guild = NULL;
cJSON_ArrayForEach(guild, json) {
value = cJSON_GetStringValue(cJSON_GetObjectItem(json, "id"));
- value2= cJSON_GetStringValue(cJSON_GetObjectItem(json, "name"));
+ value2 = cJSON_GetStringValue(cJSON_GetObjectItem(json, "name"));
if (!value || !value2) {
DC_CLIENT_ERROR(c, "!cJSON_GetStringValue(cJSON_GetObjectItem(json, \"id\" || \"name\"))");
continue;
}
+ unsigned long long int idull = strtoull(value2, NULL, 10);
+ for (int i = 0; i < c->guilds_sizeof; i++)
+ if (idull == c->guilds[i]->id)
+ continue; /* remove duplicates */
c->guilds = realloc(c->guilds, ++c->guilds_sizeof);
- c->guilds[c->guilds_sizeof-1].name = malloc(strlen(value)+1);
- strcpy(c->guilds[c->guilds_sizeof-1].name, value);
- c->guilds[c->guilds_sizeof-1].id = strtoull(value2, NULL, 10);
- c->guilds[c->guilds_sizeof-1].channels_sizeof = 0;
- c->guilds[c->guilds_sizeof-1].channels = NULL;
+ c->guilds[c->guilds_sizeof-1]->name = malloc(strlen(value)+1);
+ strcpy(c->guilds[c->guilds_sizeof-1]->name, value);
+ c->guilds[c->guilds_sizeof-1]->id = idull;
+ c->guilds[c->guilds_sizeof-1]->channels_sizeof = 0;
+ c->guilds[c->guilds_sizeof-1]->channels = NULL;
+ c->guilds[c->guilds_sizeof-1]->client = c;
}
+ if(DC_CUE(c, c->guilds_lock)) {rs = -6; goto rc;}
rc:
free(s.ptr); s.ptr = NULL;
cJSON_Delete(json); json = NULL;
return rs;
}
-int dc_fetch_channels (struct dc_client * c, struct dc_guild * g) {
+int dc_fetch_channels (struct dc_guild * g) {
int rs = 1;
- if (!c)
+ if (!g || !g->id) {
return -1;
- if (!c->username || !c->username[0])
+ }
+ struct dc_client * c = g->client;
+ if (!c)
+ return -2;
+ if (c->discriminator < 0)
if ((rs = dc_login(c)) < 0) {
DC_CLIENT_ERROR(c, "dc_login(c) " DC_I18N_FAILED " (%d)", rs);
- return -2;
+ return -3;
} else rs = 1;
- if (!g || !g->id) {
- DC_CLIENT_ERROR(c, "!g || !g->id");
- return -3;
- }
CURLcode res;
struct writefunc_string s;
init_writefunc_string(&s);
cJSON * json = NULL;
char * url = malloc(strlen(DC_API_PREFIX "guilds/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/channels")+1);
- sprintf(url, DC_API_PREFIX "guilds/%llu/channels", g->id);
+ sprintf(url, DC_API_PREFIX "guilds/%llu/channels", g->id); /* only api thread writes ids, so this is safe without locks */
curl_easy_setopt(c->curl, CURLOPT_URL, url);
curl_easy_setopt(c->curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(c->curl, CURLOPT_WRITEDATA, &s);
@@ -422,9 +472,8 @@ int dc_fetch_channels (struct dc_client * c, struct dc_guild * g) {
goto rc;
}
cJSON * channel = NULL;
- for (int i = 0; i < g->channels_sizeof; i++)
- dc_channel_free(&g->channels[i]);
- g->channels_sizeof = 0;
+ /* we lock all client guilds when doing stuff with channels */
+ if (DC_CWLE(c, c->guilds_lock)) {rs = -7; goto rc;}
cJSON_ArrayForEach(channel, json) {
char * topic = cJSON_GetStringValue(cJSON_GetObjectItem(json, "topic"));
char * name = cJSON_GetStringValue(cJSON_GetObjectItem(json, "name"));
@@ -438,37 +487,39 @@ int dc_fetch_channels (struct dc_client * c, struct dc_guild * g) {
continue;
if (!topic)
topic = "";
+ unsigned long long int idull = strtoull(id, NULL, 10);
+ for (int i = 0; i < g->channels_sizeof; i++)
+ if (idull == g->channels[i]->id)
+ continue; /* remove duplicates */
g->channels = realloc(g->channels, ++g->channels_sizeof);
- g->channels[g->channels_sizeof-1].name = malloc(strlen(name)+1);
- strcpy(g->channels[g->channels_sizeof-1].name, name);
- g->channels[g->channels_sizeof-1].topic = malloc(strlen(topic)+1);
- strcpy(g->channels[g->channels_sizeof-1].topic, topic);
- g->channels[g->channels_sizeof-1].id = strtoull(id, NULL, 10);
- g->channels[g->channels_sizeof-1].guild = g;
- g->channels[g->channels_sizeof-1].messages = NULL;
- g->channels[g->channels_sizeof-1].messages_sizeof = 0;
+ g->channels[g->channels_sizeof-1]->name = malloc(strlen(name)+1);
+ strcpy(g->channels[g->channels_sizeof-1]->name, name);
+ g->channels[g->channels_sizeof-1]->topic = malloc(strlen(topic)+1);
+ strcpy(g->channels[g->channels_sizeof-1]->topic, topic);
+ g->channels[g->channels_sizeof-1]->id = strtoull(id, NULL, 10);
+ g->channels[g->channels_sizeof-1]->guild = g;
+ g->channels[g->channels_sizeof-1]->messages = NULL;
+ g->channels[g->channels_sizeof-1]->messages_sizeof = 0;
}
+ if (DC_CUE(c, c->guilds_lock)) {rs = -8; goto rc;}
rc:
free(s.ptr); s.ptr = NULL;
free(url); url = NULL;
cJSON_Delete(json); json = NULL;
return rs;
}
-int dc_send_message (struct dc_client * c, struct dc_message * m) {
+int dc_send_message (struct dc_message * m) { /* nolock - once message is appended to the c->sent_messages queue you must not remove it or alter it */
int rs = 1;
- struct tm tm; /* not used at all, you can delete it, I am autistic xD */
- if (!c)
+ if (!m)
return -1;
+ struct dc_client * c = m->channel->guild->client; /* segfault senpai */
+ if (!c)
+ return -2;
if (!c->username || !c->username[0])
if ((rs = dc_login(c)) < 0) {
DC_CLIENT_ERROR(c, "dc_login(c) " DC_I18N_FAILED " (%d)", rs);
- return -2;
+ return -3;
} else rs = 1;
- /* it'd be bad to assume joinedchannel if the following is false though */
- if (!m) {
- DC_CLIENT_ERROR(c, "!m");
- return -3;
- }
cJSON * json = cJSON_CreateObject();
cJSON * nons = cJSON_CreateNumber(rand());
cJSON_AddItemToObject(json, "nonce", nons);
@@ -484,7 +535,7 @@ int dc_send_message (struct dc_client * c, struct dc_message * m) {
}
cJSON_Delete(json); json = NULL;
/* {content: "yeet", nonce: "820762917392613376", tts: false} */
- json = DC_API(c->curl, body, DC_API_PREFIX "channels/%llu/messages", m->channel->id);
+ json = DC_API(c->curl, body, DC_API_PREFIX "channels/%llu/messages", m->channel->id); /* ids are only written by api thread, nolock */
if (!json) {
const char *error_ptr = cJSON_GetErrorPtr();
if (!error_ptr) {
@@ -515,22 +566,23 @@ int dc_send_message (struct dc_client * c, struct dc_message * m) {
cJSON_Delete(json); json = NULL;
return rs;
}
-int dc_fetch_messages (struct dc_client * c, struct dc_channel * ch) {
+int dc_fetch_messages (struct dc_channel * ch) {
int rs = 1;
struct tm tm;
- if (!c)
+ if (!ch || !ch->id)
return -1;
- if (!c->username || !c->username[0])
+ struct dc_guild * g = ch->guild;
+ if (!g || !g->client)
+ return -2;
+ struct dc_client * c = g->client;
+ if (!c)
+ return -3;
+ if (c->discriminator < 0)
if ((rs = dc_login(c)) < 0) {
DC_CLIENT_ERROR(c, "dc_login(c) " DC_I18N_FAILED " (%d)", rs);
- return -2;
+ return -4;
} else rs = 1;
- /* it'd be bad to assume joinedchannel if the following is false though */
- if (!ch || !ch->id) {
- DC_CLIENT_ERROR(c, "!ch || !ch->id");
- return -3;
- }
- cJSON * json = DC_API(c->curl, NULL, DC_API_PREFIX "channels/%llu/messages?limit=100&_=", ch->id, rand());
+ cJSON * json = DC_API(c->curl, NULL, DC_API_PREFIX "channels/%llu/messages?limit=100&_=%d", ch->id, rand());
if (!json) {
const char *error_ptr = cJSON_GetErrorPtr();
if (!error_ptr) {
@@ -546,12 +598,8 @@ int dc_fetch_messages (struct dc_client * c, struct dc_channel * ch) {
rs = -6;
goto rc;
}
+ if (DC_CWLE(c, c->guilds_lock)) {rs = -7; goto rc;} /* we lock all guilds of a client when writing messages */
cJSON * message = NULL;
- /*
- for (int i = 0; i < ch->messages_sizeof; i++)
- dc_message_free(&ch->messages[i]);
- ch->messages_sizeof = 0;
- */ /* we'll rather add messages to existing ones */
cJSON_ArrayForEach(message, json) {
char * timestamp = cJSON_GetStringValue(cJSON_GetObjectItem(json, "timestamp"));
char * content = cJSON_GetStringValue(cJSON_GetObjectItem(json, "content"));
@@ -574,20 +622,21 @@ int dc_fetch_messages (struct dc_client * c, struct dc_channel * ch) {
}
unsigned long long int idull = strtoull(id, NULL, 10);
for (int i = 0; i < ch->messages_sizeof; i++)
- if (idull == ch->messages[i].id)
+ if (idull == ch->messages[i]->id)
continue; /* remove duplicates */
ch->messages = realloc(ch->messages, ++ch->messages_sizeof);
#define DC_FMTM /* fetch messages this message */ ch->messages[ch->messages_sizeof-1]
- DC_FMTM.time = mktime(&tm);
- DC_FMTM.content = malloc(strlen(content)+1);
- strcpy(DC_FMTM.content, content);
- DC_FMTM.username = malloc(strlen(username)+1);
- strcpy(DC_FMTM.username, username);
- DC_FMTM.id = idull;
- DC_FMTM.discriminator = strtol(discriminator, NULL, 10);
- DC_FMTM.channel = ch;
+ DC_FMTM->time = mktime(&tm);
+ DC_FMTM->content = malloc(strlen(content)+1);
+ strcpy(DC_FMTM->content, content);
+ DC_FMTM->username = malloc(strlen(username)+1);
+ strcpy(DC_FMTM->username, username);
+ DC_FMTM->id = idull;
+ DC_FMTM->discriminator = strtol(discriminator, NULL, 10);
+ DC_FMTM->channel = ch;
}
qsort(ch->messages, ch->messages_sizeof, sizeof(struct dc_message), dc_message_compare); /* we sort so that present messages are in the start of the array and old messages are to the end of the array */
+ if (DC_CUE(c, c->guilds_lock)) {rs = -8; goto rc;}
rc:
cJSON_Delete(json); json = NULL;
return rs;
@@ -595,7 +644,8 @@ int dc_fetch_messages (struct dc_client * c, struct dc_channel * ch) {
struct dc_api_thread_control {
unsigned short int power; /* 1 if the thread should run, set it to 0 for the thread to return at the end of the loop */
struct dc_client ** clients; /* "array" of pointers to clients the thread should manage, ONLY ONE dc_api_thread PER PROCESS! */
- size_t dc_clients_sizeof;
+ _Atomic(size_t) dc_clients_sizeof;
+ pthread_rwlock_t * clients_lock;
};
int dc_api_thread (struct dc_api_thread_control * t) { /* updates messages and sends messages when they are in the outbox */
return 1;
diff --git a/src/i18n.h b/src/i18n.h
index 2a982d7..81e7d70 100644
--- a/src/i18n.h
+++ b/src/i18n.h
@@ -1,4 +1,4 @@
-#define DC_I18N_FAILED "neuspel" /**/
+#define DC_I18N_FAILED "neuspelo" /**/
#define DC_I18N_UNREC_ARG "neprepoznan argument %c, poskusi -h"
#define DC_I18N_USAGE "uporaba: %s -e naslov@example -p geslo"
#define DC_I18N_ARG_ALREADY_SET "argument %c že ima nastavljeno vrednost (mogoče okoljske spremenljivke)"
@@ -10,3 +10,5 @@
#define DC_I18N_CHANNEL_NOT_SET "kanal ni izbran!"
#define DC_I18N_MESSAGES_GET_FAIL "pri pridobivanju sporočil je prišlo do napake"
#define DC_I18N_JSON_ERROR_BEFORE "JSON napaka pred"
+#define DC_I18N_LOCKING "zaklepanje"
+#define DC_I18N_UNLOCKING "odklepanje"