ClientMain.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #define _POSIX_C_SOURCE 1
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <signal.h>
  6. #include <string.h>
  7. #include "Client.h"
  8. #include "String.h"
  9. #include <termios.h>
  10. Client client;
  11. void interruptHandler(int signal)
  12. {
  13. clientRemove(&client);
  14. exit(EXIT_SUCCESS);
  15. }
  16. void toggleTerminalPrinting()
  17. {
  18. struct termios old, new;
  19. if(tcgetattr(fileno(stdin), &old) != 0)
  20. {
  21. return;
  22. }
  23. new = old;
  24. new.c_lflag ^= ECHO;
  25. if(tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0)
  26. {
  27. return;
  28. }
  29. }
  30. void writeMessageNumber(Stream* out)
  31. {
  32. String number;
  33. stringInit(&number);
  34. printf("Type a message identifier: ");
  35. stringRead(&number);
  36. streamWriteChars(out, number.data);
  37. streamWriteChar(out, '\n');
  38. stringRemove(&number);
  39. }
  40. void waitForData()
  41. {
  42. String s;
  43. stringInit(&s);
  44. while(1)
  45. {
  46. printf("Type a command:\n");
  47. stringRead(&s);
  48. if(stringCompareNoCase(&s, "SEND"))
  49. {
  50. printf("Sending a mail to the server...\n");
  51. //outgoing stream
  52. Stream out;
  53. streamInit(&out, 128);
  54. streamWriteChars(&out, "SEND\n");
  55. // receiver
  56. printf("Type a receiver with at most 8 characters: ");
  57. String receiver;
  58. stringInit(&receiver);
  59. stringRead(&receiver);
  60. int l = stringGetLength(&receiver);
  61. while(l < 1 || l > 8 || stringCompare(&receiver, ".") || stringCompare(&receiver, ".."))
  62. {
  63. printf("Please type a valid receiver: ");
  64. stringRead(&receiver);
  65. l = stringGetLength(&receiver);
  66. }
  67. streamWriteChars(&out, receiver.data);
  68. streamWriteChar(&out, '\n');
  69. stringRemove(&receiver);
  70. // subject
  71. printf("Type a subject with at most 80 characters: ");
  72. String subject;
  73. stringInit(&subject);
  74. stringRead(&subject);
  75. l = stringGetLength(&subject);
  76. while(l < 1 || l > 80)
  77. {
  78. printf("Please type a valid subject: ");
  79. stringRead(&subject);
  80. l = stringGetLength(&subject);
  81. }
  82. streamWriteChars(&out, subject.data);
  83. streamWriteChar(&out, '\n');
  84. stringRemove(&subject);
  85. // message
  86. String message;
  87. stringInit(&message);
  88. while(1)
  89. {
  90. printf("Type a message: ");
  91. stringRead(&message);
  92. if(stringCompare(&message, "."))
  93. {
  94. streamWriteChars(&out, ".\n");
  95. break;
  96. }
  97. streamWriteChars(&out, message.data);
  98. streamWriteChar(&out, '\n');
  99. }
  100. stringRemove(&message);
  101. // send and clear stream
  102. clientSendData(&client, &out);
  103. streamRemove(&out);
  104. }
  105. else if(stringCompareNoCase(&s, "LIST"))
  106. {
  107. //outgoing stream
  108. Stream out;
  109. streamInit(&out, 16);
  110. streamWriteChars(&out, "LIST\n");
  111. // send and clear stream
  112. clientSendData(&client, &out);
  113. streamRemove(&out);
  114. }
  115. else if(stringCompareNoCase(&s, "READ"))
  116. {
  117. //outgoing stream
  118. Stream out;
  119. streamInit(&out, 16);
  120. streamWriteChars(&out, "READ\n");
  121. // message number
  122. writeMessageNumber(&out);
  123. // send and clear stream
  124. clientSendData(&client, &out);
  125. streamRemove(&out);
  126. }
  127. else if(stringCompareNoCase(&s, "DEL"))
  128. {
  129. //outgoing stream
  130. Stream out;
  131. streamInit(&out, 16);
  132. streamWriteChars(&out, "DEL\n");
  133. // message number
  134. writeMessageNumber(&out);
  135. // send and clear stream
  136. clientSendData(&client, &out);
  137. streamRemove(&out);
  138. }
  139. else if(stringCompareNoCase(&s, "QUIT"))
  140. {
  141. break;
  142. }
  143. else if(stringCompareNoCase(&s, "HELP"))
  144. {
  145. printf("- LOGIN: log in at the server\n");
  146. printf("- SEND: sends a message to a receiver\n");
  147. printf("- LIST: shows all your mails\n");
  148. printf("- READ: read a mail\n");
  149. printf("- DEL: delete a mail\n");
  150. printf("- QUIT: close this program\n");
  151. }
  152. else if(stringCompareNoCase(&s, "LOGIN"))
  153. {
  154. //outgoing stream
  155. Stream out;
  156. streamInit(&out, 16);
  157. streamWriteChars(&out, "LOGIN\n");
  158. // user
  159. printf("Type a user with at most 8 characters: ");
  160. String sender;
  161. stringInit(&sender);
  162. stringRead(&sender);
  163. int l = stringGetLength(&sender);
  164. while(l < 1 || l > 8)
  165. {
  166. printf("Please type a valid user: ");
  167. stringRead(&sender);
  168. l = stringGetLength(&sender);
  169. }
  170. streamWriteChars(&out, sender.data);
  171. streamWriteChar(&out, '\n');
  172. stringRemove(&sender);
  173. // password
  174. printf("Type a password: ");
  175. String password;
  176. stringInit(&password);
  177. toggleTerminalPrinting();
  178. stringRead(&password);
  179. toggleTerminalPrinting();
  180. l = stringGetLength(&password);
  181. while(l == 0)
  182. {
  183. printf("Please type a valid password: ");
  184. toggleTerminalPrinting();
  185. stringRead(&password);
  186. toggleTerminalPrinting();
  187. l = stringGetLength(&password);
  188. }
  189. streamWriteChars(&out, password.data);
  190. streamWriteChar(&out, '\n');
  191. stringRemove(&password);
  192. // send and clear stream
  193. clientSendData(&client, &out);
  194. streamRemove(&out);
  195. }
  196. }
  197. stringRemove(&s);
  198. }
  199. void package(Stream* in)
  200. {
  201. char c;
  202. while(streamGetChar(in, &c) != -1)
  203. {
  204. fputc(c, stdout);
  205. }
  206. fputc('\n', stdout);
  207. printf("Type a command:\n");
  208. }
  209. int main(int argc, char** argv)
  210. {
  211. if(argc < 3)
  212. {
  213. printf("Usage: %s server_address port\n", argv[0]);
  214. return EXIT_FAILURE;
  215. }
  216. int port = atoi(argv[2]);
  217. if(port <= 0 || port >= 65536)
  218. {
  219. printf("invalid port");
  220. return EXIT_FAILURE;
  221. }
  222. clientInitDefaults(&client);
  223. signal(SIGINT, interruptHandler);
  224. signal(SIGKILL, interruptHandler);
  225. if(clientInit(&client, argv[1], port) == -1)
  226. {
  227. return EXIT_FAILURE;
  228. }
  229. clientRegisterHandler(&client, package);
  230. waitForData();
  231. clientRemove(&client);
  232. return EXIT_SUCCESS;
  233. }