ClientMain.c 6.5 KB

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