summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorsijanec <anton@sijanec.eu>2021-03-14 11:00:41 +0100
committersijanec <anton@sijanec.eu>2021-03-14 11:00:41 +0100
commitf01a37cb38b80c357a169aed61d05024c1f75ba9 (patch)
tree98b41c4f9d495c821372da3e8bdee8a9eac5dc9c /src
parentinitial commit - TODO: receiving messages (diff)
downloaddiscord.c-f01a37cb38b80c357a169aed61d05024c1f75ba9.tar
discord.c-f01a37cb38b80c357a169aed61d05024c1f75ba9.tar.gz
discord.c-f01a37cb38b80c357a169aed61d05024c1f75ba9.tar.bz2
discord.c-f01a37cb38b80c357a169aed61d05024c1f75ba9.tar.lz
discord.c-f01a37cb38b80c357a169aed61d05024c1f75ba9.tar.xz
discord.c-f01a37cb38b80c357a169aed61d05024c1f75ba9.tar.zst
discord.c-f01a37cb38b80c357a169aed61d05024c1f75ba9.zip
Diffstat (limited to 'src')
-rw-r--r--src/i18n.h1
-rw-r--r--src/main.c57
2 files changed, 44 insertions, 14 deletions
diff --git a/src/i18n.h b/src/i18n.h
index 8232538..6439daa 100644
--- a/src/i18n.h
+++ b/src/i18n.h
@@ -8,3 +8,4 @@
#define DC_I18N_NOT_JOINED "preden lahko pišeš, se moraš pridružiti kanalu"
#define DC_I18N_GUILD_NOT_SET "skupina ni izbrana!"
#define DC_I18N_CHANNEL_NOT_SET "kanal ni izbran!"
+#define DC_I18N_MESSAGES_GET_FAIL "pri pridobivanju sporočil je prišlo do napake"
diff --git a/src/main.c b/src/main.c
index ed6a677..dec4c7e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -12,6 +12,7 @@
#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_DEBUG 0
#define DC_COMMAND_LINE_LENGTH 2000
+#define cJSON_Izbrisi(item) do { item != NULL ? cJSON_Delete(item) : 0; } while (0)
#define DC_COMMAND_CHAR '/'
int main(int argc, char ** argv) {
srand(time(NULL));
@@ -81,10 +82,10 @@ int main(int argc, char ** argv) {
strcpy(authorization, "Authorization: ");
strcat(authorization, token->valuestring);
if(DC_DEBUG) fprintf(stderr, "d: %s\n", s.ptr);
- cJSON_Delete(json);
+ cJSON_Izbrisi(json);
} else {
fprintf(stderr, DC_I18N_LOGIN_FAILED "\n", s.ptr);
- cJSON_Delete(json);
+ cJSON_Izbrisi(json);
returnstatus = 3;
free(s.ptr); s.ptr = NULL;
goto returncleanly;
@@ -110,7 +111,7 @@ int main(int argc, char ** argv) {
fprintf(stdout, DC_I18N_LOGGED_IN "\n", handle);
} else {
fprintf(stderr, DC_I18N_LOGIN_FAILED "\n", s.ptr);
- cJSON_Delete(json);
+ cJSON_Izbrisi(json);
returnstatus = 4;
free(s.ptr); s.ptr = NULL;
goto returncleanly;
@@ -123,6 +124,7 @@ int main(int argc, char ** argv) {
long long int setchannel = 0;
char * joinedchannelname = NULL;
char * joinedchannelid = NULL;
+ char * tempstring = NULL;
while (1) {
fprintf(stderr, "%s%s> ", joinedchannelname ? "#" : "", joinedchannelname ? joinedchannelname : "");
fflush(stderr);
@@ -150,7 +152,7 @@ int main(int argc, char ** argv) {
res = curl_easy_perform(curl); \
if (res != CURLE_OK) \
fprintf(stderr, "curl_easy_perform() " DC_I18N_FAILED ": %s\n", curl_easy_strerror(res)); \
- output == NULL ? cJSON_Delete(output):rand(); \
+ cJSON_Izbrisi(output); \
output = cJSON_Parse(s.ptr); } while (0)
DC_API(DC_API_PREFIX "users/@me/guilds", GET, "", guilds);
}
@@ -170,14 +172,15 @@ int main(int argc, char ** argv) {
break;
}
setguild = strtoll(strchr(cmd, ' ')+1, NULL, 10);
+ if (!guilds || !cJSON_IsObject(guilds))
+ DC_API(DC_API_PREFIX "users/@me/guilds", GET, "", guilds);
cJSON_ArrayForEach(guild, guilds) {
if (setguild-- == 0) {
cJSON * guildchannels = cJSON_GetObjectItem(guild, "channels");
- if (!cJSON_IsObject(guildchannels) ||
- strchr(strchr(cmd,' ')+1,' ') ? 1 : 0) {
+ if (!cJSON_IsArray(guildchannels) || strchr(strchr(cmd,' ')+1,' ') ? 1 : 0) {
#define DC_UPDATE_CHANNELS(guild) do {\
token = cJSON_GetObjectItem(guild, "id"); \
- char * tempstring = malloc(strlen(token->valuestring)+strlen("guilds//channels")+1+ strlen(DC_API_PREFIX)); \
+ tempstring = realloc(tempstring, strlen(token->valuestring)+strlen("guilds//channels")+1+ strlen(DC_API_PREFIX)); \
sprintf(tempstring, DC_API_PREFIX "guilds/%s/channels", token->valuestring); \
guildchannels = cJSON_CreateObject(); \
DC_API(tempstring, GET, "", guildchannels); \
@@ -222,14 +225,40 @@ int main(int argc, char ** argv) {
cJSON * guildchannel = NULL;
cJSON_ArrayForEach(guildchannel, guildchannels) {
cJSON * type = cJSON_GetObjectItem(guildchannel, "type");
- if (type->valuedouble != 0) setchannel++;
+ if (type->valueint != 0) setchannel++;
if (setchannel-- == 0) {
- joinedchannelid = cJSON_GetObjectItem(guildchannel, "id")->valuestring;
- joinedchannelname = cJSON_GetObjectItem(guildchannel, "name")->valuestring;
+ char * joinedchannel_pointer = (cJSON_GetObjectItem(guildchannel, "id")->valuestring);
+ joinedchannelid = realloc(joinedchannelid, strlen(joinedchannel_pointer)+1);
+ strcpy(joinedchannelid, joinedchannel_pointer);
+ joinedchannel_pointer = cJSON_GetObjectItem(guildchannel, "name")->valuestring;
+ joinedchannelname = realloc(joinedchannelname, strlen(joinedchannel_pointer)+1);
+ strcpy(joinedchannelname, joinedchannel_pointer);
}
}
}
}
+ char * queryparam = malloc(strlen(DC_API_PREFIX "channels//messages?limit=100")+strlen(joinedchannelid)+1);
+ sprintf(queryparam, DC_API_PREFIX "channels/%s/messages?limit=100", joinedchannelid);
+ DC_API(queryparam, GET, "", token);
+ if (!cJSON_IsArray(token)) {
+ fprintf(stderr, DC_I18N_MESSAGES_GET_FAIL "\n");
+ free(queryparam); queryparam = NULL;
+ joinedchannelid = NULL;
+ joinedchannelname = NULL;
+ break;
+ }
+ cJSON * sporocilo = cJSON_GetArrayItem(token, cJSON_GetArraySize(token)-1); /* last item in array is the first chat */
+ do {
+#define cJSON_GetObjectItem2(root, name1, name2) (cJSON_GetObjectItem(root, name1) ? cJSON_GetObjectItem(cJSON_GetObjectItem(root, name1), name2) : NULL)
+ fprintf(stdout, "%.19s %s#%s: %s\n",
+ cJSON_GetObjectItem(sporocilo, "timestamp")->valuestring,
+ cJSON_GetObjectItem2(sporocilo, "author", "username")->valuestring,
+ cJSON_GetObjectItem2(sporocilo, "author", "discriminator")->valuestring,
+ cJSON_GetObjectItem(sporocilo, "content")->valuestring
+ );
+ sporocilo = sporocilo->prev;
+ } while (sporocilo->prev->next != NULL);
+ free(queryparam); queryparam = NULL;
break;
case 'q':
case 'Q':
@@ -255,16 +284,16 @@ int main(int argc, char ** argv) {
char * jsonmessage = cJSON_Print(message);
char * target = malloc(strlen(joinedchannelid)+strlen("channels//messages")+strlen(DC_API_PREFIX)+1);
sprintf(target, DC_API_PREFIX "channels/%s/messages", joinedchannelid);
- cJSON * jsonresponse;
+ cJSON * jsonresponse = NULL;
DC_API(target, POST, jsonmessage, jsonresponse);
free(jsonmessage); jsonmessage = NULL;
free(target); target = NULL;
- cJSON_Delete(message);
- cJSON_Delete(jsonresponse); /* jsonresponse izgleda sploh nismo uporabili */
+ cJSON_Izbrisi(message);
+ cJSON_Izbrisi(jsonresponse); /* jsonresponse izgleda sploh nismo uporabili */
}
}
returncleanly:
- cJSON_Delete(guilds);
+ cJSON_Izbrisi(guilds);
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
return 0;