123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <string.h>
- #include "Client.h"
- #include "String.h"
- Client client;
- void interruptHandler(int signal)
- {
- clientRemove(&client);
- exit(EXIT_SUCCESS);
- }
- int isSpecialFolder(char* folder)
- {
- return strcmp(folder, ".") == 0 || strcmp(folder, "..") == 0;
- }
- int readNumber(const char* text)
- {
- while(1)
- {
- int number;
- printf("%s", text);
- int result = scanf("%d", &number);
- char c = getchar();
- while(c != EOF && c != '\n')
- {
- c = getchar();
- }
-
- if(result != 0)
- {
- return number;
- }
- }
- return -1;
- }
- void waitForData()
- {
- while(1)
- {
- printf("Type a command:\n");
- String s;
- stringInit(&s);
- stringRead(&s);
-
- if(stringCompareNoCase(&s, "SEND"))
- {
- printf("Sending a mail to the server...\n");
-
- //outgoing stream
- Stream out;
- streamInit(&out, 128);
- streamWriteInt(&out, 0);
-
- // 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 || isSpecialFolder(receiver.data))
- {
- printf("Please type a valid receiver: ");
- stringRemove(&receiver);
- stringInit(&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: ");
- stringRemove(&subject);
- stringInit(&subject);
- stringRead(&subject);
- l = stringGetLength(&subject);
- }
- streamWriteChars(&out, subject.data);
- streamWriteChar(&out, '\n');
- stringRemove(&subject);
-
- // message
- printf("Type a message: ");
- String message;
- stringInit(&message);
- stringRead(&message);
- streamWriteChars(&out, message.data);
- streamWriteChars(&out, "\n.\n");
- stringRemove(&message);
-
- // send and clear stream
- clientSendData(&client, &out);
- streamRemove(&out);
- }
- else if(stringCompareNoCase(&s, "LIST"))
- {
- //outgoing stream
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 1);
-
- // send and clear stream
- clientSendData(&client, &out);
- streamRemove(&out);
- }
- else if(stringCompareNoCase(&s, "READ"))
- {
- //outgoing stream
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 2);
-
- // message number
- int number = readNumber("Type a message number: ");
- streamWriteInt(&out, number);
- streamWriteChar(&out, '\n');
-
- // send and clear stream
- clientSendData(&client, &out);
- streamRemove(&out);
- }
- else if(stringCompareNoCase(&s, "DEL"))
- {
- //outgoing stream
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 3);
-
- // message number
- int number = readNumber("Type a message number: ");
- streamWriteInt(&out, number);
- streamWriteChar(&out, '\n');
-
- // send and clear stream
- clientSendData(&client, &out);
- streamRemove(&out);
- }
- else if(stringCompareNoCase(&s, "QUIT"))
- {
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 4);
- clientSendData(&client, &out);
- streamRemove(&out);
- }
- 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: disconnects your from the server\n");
- }
- else if(stringCompareNoCase(&s, "LOGIN"))
- {
- //outgoing stream
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 5);
-
- // 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: ");
- stringRemove(&sender);
- stringInit(&sender);
- stringRead(&sender);
- l = stringGetLength(&sender);
- }
- streamWriteChars(&out, sender.data);
- streamWriteChar(&out, '\n');
- stringRemove(&sender);
-
- // password
- printf("Type a password: ");
- String password;
- stringInit(&password);
- stringRead(&password);
- l = stringGetLength(&password);
- while(l == 0)
- {
- printf("Please type a valid password: ");
- stringRemove(&password);
- stringInit(&password);
- stringRead(&password);
- l = stringGetLength(&password);
- }
- streamWriteChars(&out, password.data);
- streamWriteChar(&out, '\n');
- stringRemove(&sender);
-
- // send and clear stream
- clientSendData(&client, &out);
- streamRemove(&out);
- }
-
- stringRemove(&s);
- }
- }
- void packageAnswer(Stream* in)
- {
- char buffer[32];
- streamGetChars(in, buffer, 32);
- printf("%s\n", buffer);
-
- printf("Type a command:\n");
- }
- void packageList(Stream* in)
- {
- int number;
- if(streamGetInt(in, &number) == -1)
- {
- return;
- }
- if(number == 1)
- {
- printf("\nYou have %d mail.\n", number);
- }
- else
- {
- printf("\nYou have %d mails.\n", number);
- }
-
- char c;
- while(streamGetChar(in, &c) != -1)
- {
- fputc(c, stdout);
- }
- fputc('\n', stdout);
-
- printf("Type a command: \n");
- }
- void packageReadDel(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, packageAnswer);
- clientRegisterHandler(&client, packageList);
- clientRegisterHandler(&client, packageReadDel);
- waitForData();
- clientRemove(&client);
- return EXIT_SUCCESS;
- }
|