Compare commits
No commits in common. "23296963772e44e28bed945455bd27fcb88a0391" and "6c099af4a16d783a06d5dffa21e5f63bbab43f3c" have entirely different histories.
2329696377
...
6c099af4a1
5 changed files with 41 additions and 116 deletions
BIN
clii
BIN
clii
Binary file not shown.
17
makefile
17
makefile
|
@ -1,17 +0,0 @@
|
|||
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
|
57
src/main.c
57
src/main.c
|
@ -3,15 +3,18 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "wsock.h"
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <netdb.h>
|
||||
|
||||
/** CONSTS & UTILITIES **/
|
||||
/**************************/
|
||||
/************************/
|
||||
/* 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"
|
||||
"flashii -- Terminal-Based Flashii Chat\n"
|
||||
"USAGE: flashii [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"
|
||||
|
@ -21,25 +24,47 @@ const char USAGE[] =
|
|||
const char* _home(const char*);
|
||||
|
||||
|
||||
/** GLOBALS **/
|
||||
/***************/
|
||||
/*****************/
|
||||
/* 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 */
|
||||
/*************/
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -53,22 +78,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");
|
||||
|
||||
for(;;);
|
||||
struct hostent* fiihost;
|
||||
fiihost = gethostbyname(FII_ADDR);
|
||||
|
||||
initscr();
|
||||
raw(); noecho();
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
#include "wsock.h"
|
||||
|
||||
|
80
src/wsock.h
80
src/wsock.h
|
@ -1,80 +0,0 @@
|
|||
#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*);
|
Loading…
Reference in a new issue