summaryrefslogtreecommitdiffstats
path: root/src/api.c
diff options
context:
space:
mode:
authorsijanec <anton@sijanec.eu>2021-05-05 12:56:41 +0200
committersijanec <anton@sijanec.eu>2021-05-05 12:56:41 +0200
commit42bdf4a3ee0ad23b4eaf5e1b6a7e61a1f999d879 (patch)
treeeb9f13bfc2962a62d716975f0fd9a43f683612c0 /src/api.c
parentdodal podporo za direct messages (diff)
downloaddiscord.c-42bdf4a3ee0ad23b4eaf5e1b6a7e61a1f999d879.tar
discord.c-42bdf4a3ee0ad23b4eaf5e1b6a7e61a1f999d879.tar.gz
discord.c-42bdf4a3ee0ad23b4eaf5e1b6a7e61a1f999d879.tar.bz2
discord.c-42bdf4a3ee0ad23b4eaf5e1b6a7e61a1f999d879.tar.lz
discord.c-42bdf4a3ee0ad23b4eaf5e1b6a7e61a1f999d879.tar.xz
discord.c-42bdf4a3ee0ad23b4eaf5e1b6a7e61a1f999d879.tar.zst
discord.c-42bdf4a3ee0ad23b4eaf5e1b6a7e61a1f999d879.zip
Diffstat (limited to 'src/api.c')
-rw-r--r--src/api.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/src/api.c b/src/api.c
index cf24448..168fcf2 100644
--- a/src/api.c
+++ b/src/api.c
@@ -52,10 +52,7 @@
- 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 the ui thread
*/
struct dc_error {
size_t line;
@@ -77,6 +74,7 @@ struct dc_message {
char * username; /* yesfree */
int discriminator;
char * content; /* yesfree */
+ char * attachment; /* yesfree - url to the attachment should it exist */
time_t time;
_Atomic(struct dc_channel *) channel; /* nofree */
unsigned short int tts;
@@ -86,6 +84,7 @@ struct dc_message {
void 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;
+ free(m->attachment); m->attachment = NULL;
m->channel = NULL;
m->tts = 0;
m->discriminator = -1;
@@ -572,7 +571,7 @@ int dc_send_message (struct dc_message * m) { /* nolock - once message is append
cJSON * json = cJSON_CreateObject();
cJSON * nons = cJSON_CreateNumber(rand());
cJSON_AddItemToObject(json, "nonce", nons);
- cJSON * content = cJSON_CreateString(m->content);
+ cJSON * content = cJSON_CreateString(m->content ? m->content : "dc_send_message(): !m->content");
cJSON_AddItemToObject(json, "content", content);
cJSON * tts = m->tts ? cJSON_CreateTrue() : cJSON_CreateFalse();
cJSON_AddItemToObject(json, "tts", tts);
@@ -649,8 +648,14 @@ int dc_fetch_messages (struct dc_channel * ch) {
char * id = cJSON_GetStringValue(cJSON_GetObjectItem(message, "id"));
char * discriminator = cJSON_GetStringValue(cJSON_GetObjectItem2(message, "author", "discriminator"));
char * username = cJSON_GetStringValue(cJSON_GetObjectItem2(message, "author", "username"));
- if (!id || !timestamp || !content || !username || !discriminator) {
- DC_CLIENT_ERROR(c, "!id || !timestamp || !content || !username || discriminator < 0");
+ char * attachment = NULL;
+ cJSON * attachmentj = NULL;
+ cJSON * attachments = cJSON_GetObjectItem(message, "attachments");
+ cJSON_ArrayForEach(attachmentj, attachments)
+ if ((attachment = cJSON_GetStringValue(cJSON_GetObjectItem(attachmentj, "url"))))
+ break; /* we only extract the first attachment */
+ if (!id || !timestamp || (!content && !attachment) || !username || !discriminator) {
+ DC_CLIENT_ERROR(c, "!id || (!timestamp && !attachment) || !content || !username || discriminator < 0");
continue;
}
int kratekts = 0;
@@ -690,8 +695,10 @@ int dc_fetch_messages (struct dc_channel * ch) {
/* DC_CLIENT_ERROR(c, "recvd msg %llu", idull); */ /* remember: continue in a nested forloop is not useful in some cases (: */
DC_FMTM = calloc(1, sizeof(struct dc_message));
DC_FMTM->time = mktime(&tm);
- DC_FMTM->content = malloc(strlen(content)+1);
- strcpy(DC_FMTM->content, content);
+ if (content)
+ strcpy(DC_FMTM->content = malloc(strlen(content)+1), content);
+ if (attachment)
+ strcpy(DC_FMTM->attachment = malloc(strlen(attachment)+1), attachment);
DC_FMTM->username = malloc(strlen(username)+1);
strcpy(DC_FMTM->username, username);
DC_FMTM->id = idull;