ClientMain.c 7.8 KB

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