123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- #define _POSIX_C_SOURCE 1
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <string.h>
- #include "Client.h"
- #include "String.h"
- #include <termios.h>
- 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;
- }
|