#define _POSIX_C_SOURCE 1 #include #include #include #include #include #include "Client.h" #include "String.h" #include Client client; void interruptHandler(int signal) { clientRemove(&client); exit(EXIT_SUCCESS); } void toggleTerminalPrinting() { struct termios old, new; if(tcgetattr(fileno(stdin), &old) != 0) { return; } new = old; new.c_lflag ^= ECHO; if(tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0) { return; } } void writeMessageNumber(Stream* out) { String number; stringInit(&number); printf("Type a message identifier: "); stringRead(&number); streamWriteChars(out, number.data); streamWriteChar(out, '\n'); stringRemove(&number); } void waitForData() { String s; stringInit(&s); while(1) { printf("Type a command:\n"); stringRead(&s); if(stringCompareNoCase(&s, "SEND")) { printf("Sending a mail to the server...\n"); //outgoing stream Stream out; streamInit(&out, 128); streamWriteChars(&out, "SEND\n"); // receiver printf("Type a receiver with at most 8 characters: "); String receiver; stringInit(&receiver); stringRead(&receiver); int l = stringGetLength(&receiver); while(l < 1 || l > 8 || stringCompare(&receiver, ".") || stringCompare(&receiver, "..")) { printf("Please type a valid receiver: "); stringRead(&receiver); l = stringGetLength(&receiver); } streamWriteChars(&out, receiver.data); streamWriteChar(&out, '\n'); stringRemove(&receiver); // subject printf("Type a subject with at most 80 characters: "); String subject; stringInit(&subject); stringRead(&subject); l = stringGetLength(&subject); while(l < 1 || l > 80) { printf("Please type a valid subject: "); stringRead(&subject); l = stringGetLength(&subject); } streamWriteChars(&out, subject.data); streamWriteChar(&out, '\n'); stringRemove(&subject); // message String message; stringInit(&message); while(1) { printf("Type a message: "); stringRead(&message); if(stringCompare(&message, ".")) { streamWriteChars(&out, ".\n"); break; } streamWriteChars(&out, message.data); streamWriteChar(&out, '\n'); } stringRemove(&message); // send and clear stream clientSendData(&client, &out); streamRemove(&out); } else if(stringCompareNoCase(&s, "LIST")) { //outgoing stream Stream out; streamInit(&out, 16); streamWriteChars(&out, "LIST\n"); // send and clear stream clientSendData(&client, &out); streamRemove(&out); } else if(stringCompareNoCase(&s, "READ")) { //outgoing stream Stream out; streamInit(&out, 16); streamWriteChars(&out, "READ\n"); // message number writeMessageNumber(&out); // send and clear stream clientSendData(&client, &out); streamRemove(&out); } else if(stringCompareNoCase(&s, "DEL")) { //outgoing stream Stream out; streamInit(&out, 16); streamWriteChars(&out, "DEL\n"); // message number writeMessageNumber(&out); // send and clear stream clientSendData(&client, &out); streamRemove(&out); } else if(stringCompareNoCase(&s, "QUIT")) { break; } else if(stringCompareNoCase(&s, "HELP")) { printf("- LOGIN: log in at the server\n"); printf("- SEND: sends a message to a receiver\n"); printf("- LIST: shows all your mails\n"); printf("- READ: read a mail\n"); printf("- DEL: delete a mail\n"); printf("- QUIT: close this program\n"); } else if(stringCompareNoCase(&s, "LOGIN")) { //outgoing stream Stream out; streamInit(&out, 16); streamWriteChars(&out, "LOGIN\n"); // user printf("Type a user with at most 8 characters: "); String sender; stringInit(&sender); stringRead(&sender); int l = stringGetLength(&sender); while(l < 1 || l > 8) { printf("Please type a valid user: "); stringRead(&sender); l = stringGetLength(&sender); } streamWriteChars(&out, sender.data); streamWriteChar(&out, '\n'); stringRemove(&sender); // password printf("Type a password: "); String password; stringInit(&password); toggleTerminalPrinting(); stringRead(&password); toggleTerminalPrinting(); l = stringGetLength(&password); while(l == 0) { printf("Please type a valid password: "); toggleTerminalPrinting(); stringRead(&password); toggleTerminalPrinting(); l = stringGetLength(&password); } streamWriteChars(&out, password.data); streamWriteChar(&out, '\n'); stringRemove(&password); // send and clear stream clientSendData(&client, &out); streamRemove(&out); } } stringRemove(&s); } void package(Stream* in) { char c; while(streamGetChar(in, &c) != -1) { fputc(c, stdout); } fputc('\n', stdout); printf("Type a command:\n"); } int main(int argc, char** argv) { if(argc < 3) { printf("Usage: %s server_address port\n", argv[0]); return EXIT_FAILURE; } int port = atoi(argv[2]); if(port <= 0 || port >= 65536) { printf("invalid port"); return EXIT_FAILURE; } clientInitDefaults(&client); signal(SIGINT, interruptHandler); signal(SIGKILL, interruptHandler); if(clientInit(&client, argv[1], port) == -1) { return EXIT_FAILURE; } clientRegisterHandler(&client, package); waitForData(); clientRemove(&client); return EXIT_SUCCESS; }