From 42bdf4a3ee0ad23b4eaf5e1b6a7e61a1f999d879 Mon Sep 17 00:00:00 2001 From: sijanec Date: Wed, 5 May 2021 12:56:41 +0200 Subject: dodal podporo za priponke in debian --- src/api.c | 23 +++++++++++++++-------- src/i18n.h | 1 + src/ui.c | 5 ++++- 3 files changed, 20 insertions(+), 9 deletions(-) (limited to 'src') 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; diff --git a/src/i18n.h b/src/i18n.h index 1468e83..acb8946 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -26,3 +26,4 @@ #define DC_I18N_UI_SLOWMODE "na tem kanalu po poslanem sporočilu novega ne smeš poslati %ds. počakaj še %ds in poskusi znova." #define DC_I18N_UI_LINE_BEFORE_NETWORK "discord.c | najprej zaženi mrežno nit z ukazom /n 1" #define DC_I18N_DMS "zasebni pogovori (virtualna skupina kanalov)" +#define DC_I18N_ATTACHMENT "priponka" diff --git a/src/ui.c b/src/ui.c index e741335..725e7cc 100644 --- a/src/ui.c +++ b/src/ui.c @@ -28,7 +28,9 @@ int dc_ui_print_message (WINDOW * textwin, struct dc_message * msg2do) { DC_SIMPLEPRINT(textwin, 4, "%.33s", msg2do->username); getyx(textwin, y, x); wmove(textwin, y, 37); /* quick mafs */ - DC_SIMPLEPRINT(textwin, 3, ": %s\n", msg2do->content); + DC_SIMPLEPRINT(textwin, 3, ": %s%s", msg2do->content ? msg2do->content : "", msg2do->attachment ? " " : "\n"); + if (msg2do->attachment) + DC_SIMPLEPRINT(textwin, 8, "[" DC_I18N_ATTACHMENT "]: %s\n", msg2do->attachment); msg2do->status = 1; if (x); /* set but not used */ return 1; @@ -194,6 +196,7 @@ int dc_ui_thread (struct dc_thread_control * t) { init_pair(5, COLOR_CYAN, COLOR_BLACK); init_pair(6, COLOR_BLACK, COLOR_CYAN); init_pair(7, COLOR_MAGENTA, COLOR_BLACK); + init_pair(8, COLOR_BLUE, COLOR_WHITE); keypad(stdscr, TRUE); getmaxyx(stdscr, y, x); /* to je macro, zato y in x nista kazalca (;: */ ui.maxy = y; -- cgit v1.2.3