Skip to content

Commit

Permalink
Added colours to distinguish general messages and other client messag…
Browse files Browse the repository at this point in the history
…es. Removed duplicate config files fixing #31. Messages received in the client from other clients will no longer output additional lines which fixes #32. Added chat logging command on client, debugs no longer use a log file. Improved client and server key file handling fixes #36. Tidied up shutdown command routines. Added git issues refs to 'todo' actions. Fixed code warnings found with oclint. Commented out websocket features for now ref #29.
  • Loading branch information
triforce committed Dec 26, 2014
1 parent c124223 commit 3681f22
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 120 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CFLAGS=-lpthread --std=gnu99
SRC=src
BIN=build
CONF=conf
WS=-lwebsockets

# Check for Linux environment
ENV:=$(shell uname -s | cut -d _ -f1)
Expand All @@ -14,7 +15,7 @@ endef
all:
$(os_check)
rm -f $(BIN)/shhchatd $(BIN)/shhclient
$(CC) -o $(BIN)/shhchatd $(SRC)/shhchatd/server.c $(CFLAGS) -DVERSION='"_beta"' -lwebsockets
$(CC) -o $(BIN)/shhchatd $(SRC)/shhchatd/server.c $(CFLAGS) -DVERSION='"_beta"'
$(CC) -o $(BIN)/shhclient $(SRC)/chatclient/client.c $(CFLAGS) -DVERSION='"_beta"'
cp $(CONF)/* -t $(BIN)/cfg
@echo Finished shhchat build
Expand All @@ -26,7 +27,7 @@ clean:
debug:
$(os_check)
rm -f $(BIN)/shhchatd $(BIN)/shhclient
$(CC) -o $(BIN)/shhchatd $(SRC)/shhchatd/server.c $(CFLAGS) -DDEBUG -lwebsockets -g
$(CC) -o $(BIN)/shhchatd $(SRC)/shhchatd/server.c $(CFLAGS) -DDEBUG -g
$(CC) -o $(BIN)/shhclient $(SRC)/chatclient/client.c $(CFLAGS) -DDEBUG
cp $(CONF)/* -t $(BIN)/cfg
@echo Finished shhchat debug build
1 change: 0 additions & 1 deletion build/cfg/key

This file was deleted.

3 changes: 0 additions & 3 deletions build/cfg/users

This file was deleted.

145 changes: 100 additions & 45 deletions src/chatclient/client.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
shhchat client
alpha
*/

#include <stdio.h>
Expand All @@ -21,33 +20,38 @@ alpha
#define BUFFER_MAX 1024

#ifndef VERSION
#define VERSION "_debug"
#define VERSION "_beta"
#endif

// Colours
#define RED "\033[22;31m"
#define RESET_COLOR "\e[m"
#define RED "\e[1;31m"
#define GREEN "\e[1;32m"
#define YELLOW "\e[1;33m"
#define BLUE "\e[1;34m"
#define RESET_COLOR "\e[0m"

int sockfd, n, x, y, count;
struct sockaddr_in serv_addr;
char buffer[BUFFER_MAX];
char buf[10];
char plain[] = "";
char key[] = "123456";
char serverKey[BUFFER_MAX];
void *interrupt_Handler();
void *chat_write(int);
void *chat_read(int);
void *zzz();
void printDebug(char *string);
void printLog(char *string);
void writeLog(FILE *fp, char *str);
void addYou();
void *xor_encrypt(char *key, char *string, int n);
void initLog(char logname[]);
int createPaths(char log_name_default[], char log_name_new[]);
void exitLogError();
bool debugsOn = false;
char plain[] = "";
char key[] = "123456";
bool logsOn = false;
bool addedYou = false;
FILE *fp_l;
char serverKey[BUFFER_MAX];

int main(int argc, char *argv[]) {
pthread_t thr1, thr2;
Expand All @@ -57,6 +61,7 @@ int main(int argc, char *argv[]) {
size_t len = 0;
ssize_t read;
bool haveKey = false;
int buf_size;

if (argc < 2) {
printf("Usage: ./shhclient <server_ip> <optional_port>\n");
Expand All @@ -67,25 +72,37 @@ int main(int argc, char *argv[]) {
port = atoi(argv[2]);
}

printf("\e[1;34mshhchat client v%s started\n\e[0m", VERSION);
initLog ("shh_log");
printDebug("Reading contents of config file\n");

// TODO Create the key file if it doesn't exist
printf("%sshhchat client v%s started\n%s", GREEN, VERSION, RESET_COLOR);
printDebug("Searching for key file...\n");

// Try and open key file
fp = fopen("cfg/key", "r");
// Find key file for use in current session
fp = fopen(".sshkey", "r");

if (fp == NULL) {
printDebug("Failed to read key file\n");
exit(EXIT_FAILURE);
printf("%sKey file not found in current dir - Searching in standard build filepath...\n%s", GREEN, RESET_COLOR);
fp = fopen("cfg/key", "r");

if (fp == NULL) {
printf("%sKey file not found in current standard build dir - Searching in /etc/shhchat...\n%s", RED, RESET_COLOR);
fp = fopen("/etc/shhchat/key", "r");

if (fp == NULL) {
printf("%sKey file not found...Exiting\n%s", RED, RESET_COLOR);
exit(EXIT_FAILURE);
}
else {
printf("%sKey found in /etc/shhchat dir.\n%s", GREEN, RESET_COLOR);
}
}
else {
printf("%sKey found in cfg dir.\n%s", GREEN, RESET_COLOR);
}
}

// Read contents of key file
while ((read = getline(&line, &len, fp)) != -1) {
// printf("Key found %zu :\n", read);
printDebug("Key found\n");
strncpy(key, line, sizeof(line));
buf_size = sizeof(line);
strncpy(key, line, buf_size);
// Don't print key out!
// printf("%s", key);
haveKey = true;
Expand All @@ -103,27 +120,24 @@ int main(int argc, char *argv[]) {
sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd == -1)
printf ("Failed to open socket - Is the port already in use?\n");
else
printf("\e[1;34mSocket opened.\n\e[0m");
printf ("%sFailed to open socket - Is the port already in use?\n%s", RED, RESET_COLOR);

bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);

bzero(buf, 10);
printf("\e[1;34mChoose username for this chat session: \e[0m");
printf("Choose username for this chat session: ");
fgets(buf, 10, stdin);
__fpurge(stdin);
buf[strlen(buf)-1] = ':';

if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == -1) {
printf("Failed to connect - Is the server running?\n");
printf("%sFailed to connect - Is the server running?\n%s", RED, RESET_COLOR);
exit(0);
}
else
printf("%s connected.\n",buf);
} else
printf("%s%s connected.\n%s", GREEN, buf, RESET_COLOR);

addYou();

Expand All @@ -149,7 +163,7 @@ void *chat_read(int sockfd) {
n = recv(sockfd, buffer, sizeof(buffer), 0);

if (n == 0) {
printf("Lost connection to the server.\n\n");
printf("%sLost connection to the server.\n\n%s", RED, RESET_COLOR);
exit(0);
}

Expand All @@ -158,9 +172,8 @@ void *chat_read(int sockfd) {
y = n;
xor_encrypt(key, buffer, y);

// TODO Add '!!'
if (strncmp(buffer, "shutdown", 8) == 0) {
printf("\n");
if (strncmp(buffer, "!!shutdown", 10) == 0) {
printf("%s\nServer has shutdown.\n%s", GREEN, RESET_COLOR);
exit(0);
}

Expand All @@ -170,11 +183,19 @@ void *chat_read(int sockfd) {
continue;
}

printf("\n%s", buffer);
if (addedYou) {
printLog(buffer);
printf("\r%s%s%s", BLUE, buffer, RESET_COLOR);
} else {
printf("\n%s%s%s", BLUE, buffer, RESET_COLOR);
addedYou = false;
}

fflush(stdout);
addYou();
}
}
return 0;
}

void *chat_write(int sockfd) {
Expand All @@ -184,31 +205,58 @@ void *chat_write(int sockfd) {
// Continuously read from stdin
clear = true;
bzero(buffer, BUFFER_MAX);
fflush(stdout);
__fpurge(stdin);
fgets(buffer, sizeof(buffer), stdin);

if (strlen(buffer)-1 > sizeof(buffer)) {
printf("\e[1;34mBuffer full, reduce size of message.\n\e[0m");
printf("%sBuffer full, reduce size of message.%s\n", GREEN, RESET_COLOR);
bzero(buffer, BUFFER_MAX);
__fpurge(stdin);
}

if (strlen(buffer) > 1) {

// ========================
// Server Side Requests

if (strncmp(buffer, "??who", 5) == 0) {
printf("\e[1;34mUser's currently connected:\e[0m");
printf("%sUser's currently connected:%s\n", GREEN, RESET_COLOR);
clear = false;
}

if (strncmp(buffer, "??list", 6) == 0) {
printf("\e[1;34mUser's in db:\e[0m");
printf("%sUser's in db:%s\n", GREEN, RESET_COLOR);
clear = false;
}

if (strncmp(buffer, "??quit", 6) == 0) {
exit(0);
}

// End Server Side Requests
// ========================


// ==================
// Local Requests

if (strncmp(buffer, "??logon", 7) == 0) {
logsOn = true;
initLog ("shh_log");
continue;
}

if (strncmp(buffer, "??logoff", 8) == 0) {
logsOn = false;
continue;
}

// End Local Requests
// ==================

printLog(buffer);

char *tmp = strdup(buffer);
strncpy(buffer, serverKey, sizeof(serverKey));
strcat(buffer, tmp);
Expand All @@ -218,29 +266,33 @@ void *chat_write(int sockfd) {
y = strlen(buffer);
xor_encrypt(key, buffer, y);
n = send(sockfd, buffer, y, 0);
}
else
} else
__fpurge(stdin);

bzero(buffer, BUFFER_MAX);

if (clear)
addYou();
}
return 0;
}

void *interrupt_Handler() {
printf("\rType '??quit' to exit.\n");
printf("\r%sType '??quit' to exit.\n%s", YELLOW, RESET_COLOR);
return 0;
}
void *zzz() {
printf("\rType '??quit' to exit.\n");
printf("\r%sType '??quit' to exit.\n%s", YELLOW, RESET_COLOR);
return 0;
}

void printDebug(char *string) {
// If debugs not on, write to log file
if (debugsOn)
printf(string);
else
printf("%s", string);
}

void printLog(char *string) {
if (logsOn)
writeLog(fp_l, string);
}

Expand All @@ -249,7 +301,7 @@ void initLog(char logname[]) {
char log_name_default[1024] = "/";
char log_name_new[1024] = "/";

// TODO Check if there are over 5 logfiles and if so remove the oldest before adding a new one
// TODO #34 - Check if there are over 5 log files and if so remove the oldest before adding a new one

strncat(log_name_default, logname, strlen(logname));
strncat(log_name_new, logname, strlen(logname));
Expand All @@ -258,6 +310,7 @@ void initLog(char logname[]) {
if (createPaths(log_name_default, log_name_new) == 0)
exitLogError();

// TODO #35 - Handle error while copying
result = rename(log_name_default, log_name_new);

// Open log file
Expand Down Expand Up @@ -289,7 +342,7 @@ int createPaths(char log_name_default[], char log_name_new[]) {
}

void exitLogError() {
printf("Log file error, shutting down.\n");
printf("%sLog file error, shutting down.\n%s", RED, RESET_COLOR);
exit(0);
}

Expand All @@ -298,6 +351,7 @@ void writeLog(FILE *fp, char *str) {
}

void addYou() {
addedYou = true;
printf("You: ");
}

Expand All @@ -309,5 +363,6 @@ void *xor_encrypt(char *key, char *string, int n) {
for (i = 0;i < n;i++) {
string[i] = string[i]^key[i%keyLength];
}
return 0;
}

Loading

0 comments on commit 3681f22

Please sign in to comment.