diff --git a/clii b/clii new file mode 100755 index 0000000..aa4ba07 Binary files /dev/null and b/clii differ diff --git a/makefile b/makefile new file mode 100644 index 0000000..3da9d52 --- /dev/null +++ b/makefile @@ -0,0 +1,17 @@ +CC = gcc +CFLAGS = -Wall -g +LDFLAGS = -lncurses + +OBJS = main.o wsock.o +DEPS = main.c + +all: clii +clean: + rm -f $(OBJS) + +clii: $(OBJS) + $(CC) -o $@ $^ $(LDFLAGS) +%.o: src/%.c + $(CC) -o $@ $(CFLAGS) -c $< + +.PHONY: all clean diff --git a/src/.main.c.swp b/src/.main.c.swp new file mode 100644 index 0000000..6701183 Binary files /dev/null and b/src/.main.c.swp differ diff --git a/src/main.c b/src/main.c index 755648a..6f4136e 100644 --- a/src/main.c +++ b/src/main.c @@ -3,18 +3,15 @@ #include #include -#include -#include -#include +#include "wsock.h" -/************************/ -/* CONSTS & UTILITIES */ -/************************/ +/** CONSTS & UTILITIES **/ +/**************************/ const char FII_ADDR[] = "chatsrv-neru.flashii.net"; const char USAGE[] = - "flashii -- Terminal-Based Flashii Chat\n" - "USAGE: flashii [session key]\n\n" + "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" @@ -24,47 +21,25 @@ const char USAGE[] = const char* _home(const char*); -/*****************/ -/* DATA BUFFER */ -/*****************/ - -struct buffer_t { - char* data; - int length; - struct buffer_t* next; -}; -typedef struct buffer_t buffer_t; - - -/****************/ -/* WEB SOCKET */ -/****************/ - -typedef struct { - int sock; -} wsock_t; - -wsock_t* wsock_open(const char*, uint16_t); - -void wsock_close(wsock_t*); - - -/*************/ -/* GLOBALS */ -/*************/ +/** GLOBALS **/ +/***************/ struct { char session[256]; } _G; +/** MAIN **/ +/************/ + int main(int argc, char** argv) { FILE* fp; - printf("Loading session key ..."); 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; } @@ -78,22 +53,22 @@ int main(int argc, char** argv) { 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; } - printf("Session key loaded.\n"); printf("Connecting to Flashii ...\n"); - struct hostent* fiihost; - fiihost = gethostbyname(FII_ADDR); + for(;;); initscr(); raw(); noecho(); diff --git a/src/wsock.c b/src/wsock.c new file mode 100644 index 0000000..73f31f7 --- /dev/null +++ b/src/wsock.c @@ -0,0 +1,3 @@ +#include "wsock.h" + + diff --git a/src/wsock.h b/src/wsock.h new file mode 100644 index 0000000..9e451db --- /dev/null +++ b/src/wsock.h @@ -0,0 +1,80 @@ +#include +#include +#include + +/** DATA BUFFER **/ +/*******************/ + +struct buffer_t { + char* data; + int length; + struct buffer_t* next; +}; +typedef struct buffer_t buffer_t; + +buffer_t* buffer_create(); + +int buffer_length(buffer_t*); +void buffer_append(buffer_t*, const char*, int); +void buffer_append_str(buffer_t*, const char*); + +char* buffer_read(buffer_t*, int*); +char* buffer_read_str(buffer_t*, int*); +char* buffer_flush(buffer_t*, int*); +char* buffer_flush_str(buffer_t*, int*); + +void buffer_clear(buffer_t*); +void buffer_free(buffer_t*); + + +/** WEB SOCKET **/ +/******************/ + +typedef enum { + // block on system calls + WS_BLOCK = 0, + // do not block on system calls + WS_NOBLOCK = 1, + // block until a full packet is received + WS_FULL_RECV = 2, + // block until a full packet is sent + WS_FULL_SEND = 4 +} ws_mode_t; +// mode options that are not mutually exclusive can be +// concatenated using the OR (|) operator. note that +// WS_BLOCK and WS_NOBLOCK control blocking only on the +// system socket functions, whereas WS_FULL_RECV and +// WS_FULL_SEND will block on the protocol level, until +// a packet is fully sent or received + +// for example, WS_BLOCK alone with block until data is +// available, but will immediately return nothing if the +// data received is not a full packet. however, using +// WS_BLOCK | WS_FULL_RECV will block until data is +// available, and then until a full websocket packet is +// fully constructed, after which the data is returned. +// noblock behaves in the same way except the system +// calls will not block, but with WS_FULL_RECV enabled +// it will then block until a full packet is constructed + +// currently WS_FULL_SEND is set to be always enabled +// when the mode is changed because i do not currently +// see a need for partial sends in this client, thus +// all sends will block until the full packet is sent +// over the wire. if anyone else modifying this client +// has an EXTREME THIRST for partial sends, they can +// implement this behavior themselves + +typedef struct { + int sock, mode; + buffer_t buffer; +} wsock_t; + +wsock_t* wsock_open(const char*, const char*, uint16_t); +int wsock_mode_set(wsock_t*, int); +int wsock_mode_get(wsock_t*); + + + +int wsock_is_open(wsock_t*); +void wsock_close(wsock_t*);