clii/src/main.c
reemo c07a007731 HOLY SHIT IT WORKS
EXCEPT I GET A SIGABRT WHEN THE LINE IS SILENT GOD DAMNIT
2023-12-20 18:21:13 -06:00

110 lines
2 KiB
C

#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "wsock.h"
/** CONSTS & UTILITIES **/
/**************************/
const char FII_ADDR[] = "chatsrv-neru.flashii.net";
const char USAGE[] =
"clii -- Command-Line Flashii Chat\n"
"USAGE: clii [session key]\n\n"
"Session key will be stored for future use until\n"
"a new session key is given to the program.\n\n"
"If you do not know your session key, visit\n"
" https://flashii.net/_sockchat/token\n"
"on any web browser that is logged in.";
const char* _home(const char*);
/** GLOBALS **/
/***************/
struct {
char session[256];
} _G;
/** MAIN **/
/************/
int main(int argc, char** argv) {
srand(time(NULL));
FILE* fp;
switch(argc) {
case 1:
printf("Loading session key ...\n");
if((fp = fopen(_home(".fiikey"), "r")) == NULL) {
printf("No previous session key found.\n");
printf("%s\n", USAGE);
return -1;
}
fgets(_G.session, sizeof(_G.session), fp);
fclose(fp);
break;
case 2:
if(strncmp(argv[1], "--h", 3) == 0) {
printf("%s\n", USAGE);
return 0;
}
printf("New session key provided. Storing ...\n");
strcpy(_G.session, argv[1]);
if((fp = fopen(_home(".fiikey"), "w")) != NULL) {
fputs(_G.session, fp);
fclose(fp);
} else
printf("Could not write to ~/.fiikey. Session key will not be stored.\n");
break;
default:
printf("%s\n", USAGE);
return -1;
}
char buf[2048], *get;
printf("Connecting to Flashii ...\n");
wsock_t* sock = wsock_open(FII_ADDR, "/", 80);
printf("Authenticating ...\n");
sprintf(buf, "1\tMisuzu\t%s", _G.session);
wsock_send_str(sock, buf);
for(;;) {
wsock_recv(sock, &get);
printf("%s\n", get);
free(get);
}
initscr();
raw(); noecho();
keypad(stdscr, TRUE);
/*for(;;) {
}*/
printw("hi mom");
refresh();
halfdelay(1);
while(getch() == ERR);
//getch();
endwin();
return 0;
}
const char* _home(const char* path) {
static char full_path[4096];
strcpy(full_path, getenv("HOME"));
strcat(full_path, "/");
strcat(full_path, path);
return full_path;
}