accidentally deleted a bunch of stuff p:

This commit is contained in:
reemo 2023-12-16 14:51:21 -06:00
parent 6c099af4a1
commit 3f81db50c8
6 changed files with 116 additions and 41 deletions

BIN
clii Executable file

Binary file not shown.

17
makefile Normal file
View file

@ -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

BIN
src/.main.c.swp Normal file

Binary file not shown.

View file

@ -3,18 +3,15 @@
#include <stdlib.h>
#include <stdint.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#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();

3
src/wsock.c Normal file
View file

@ -0,0 +1,3 @@
#include "wsock.h"

80
src/wsock.h Normal file
View file

@ -0,0 +1,80 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
/** 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*);