mystic.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <pty.h>
  5. #include <termios.h>
  6. #include <fcntl.h>
  7. #include <sys/select.h>
  8. #include <sys/wait.h>
  9. #include <signal.h> // handle Ctrl-C/SIGINT
  10. #include <time.h> // usleep(), nanonsleep() ?
  11. #include <strings.h> // strcasecmp
  12. #include <stdlib.h> // random()
  13. #define TARGET "./mySTIC"
  14. #define BSIZE 1024
  15. #define LOGGING
  16. const char * it_is_now(void) {
  17. static char buffer[100];
  18. time_t timer;
  19. struct tm* tm_info;
  20. timer = time(NULL);
  21. tm_info = localtime(&timer);
  22. strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_info);
  23. return buffer;
  24. }
  25. void slow_write(int fd, int speed, char * buffer, int len) {
  26. int x;
  27. for( x = 0; x < len; x++) {
  28. usleep(speed);
  29. write( fd, &buffer[x], 1);
  30. }
  31. }
  32. void harry_event(int fd) {
  33. // Make something happen
  34. char clear[] = "\b \b";
  35. int x;
  36. char haha[] = "Hahaha hahaha!!!";
  37. char beep = '\a';
  38. slow_write(fd, 11500, haha, strlen(haha) );
  39. write(fd, &beep, 1);
  40. sleep(3);
  41. for( x = 0; x < strlen(haha); x++ ) {
  42. write(fd, clear, strlen(clear) );
  43. };
  44. }
  45. /*
  46. NOTE: Logging is single file, don't use in production!
  47. It won't handle multiple writers.
  48. */
  49. char * username = NULL;
  50. char * fullname = NULL;
  51. void pcopy(char * pstring, char * str) {
  52. int len = (int)*pstring;
  53. strncpy( str, pstring+1, len );
  54. str[len] = 0;
  55. }
  56. int locate_user(const char *alias) {
  57. FILE * user;
  58. char buffer[0x600];
  59. char temp[100];
  60. user = fopen("data/users.dat", "rb");
  61. if (user == NULL)
  62. return 0;
  63. // Carry on!
  64. while (fread(buffer, 0x600, 1, user) == 1) {
  65. pcopy( buffer + 0x6d, temp );
  66. if ( strcasecmp( temp, username) == 0) {
  67. pcopy(buffer + 0x8c, temp );
  68. fullname = strdup(temp);
  69. break;
  70. }
  71. /*
  72. printf("Alias: %s\n", temp);
  73. pcopy(buffer + 0x8c, temp );
  74. printf("Full Name: %s\n", temp );
  75. */
  76. }
  77. fclose(user);
  78. return 1;
  79. }
  80. // Buffers are BSIZE + 1, so a buffer that size can strcpy safely.
  81. void mangle( char * buffer ) {
  82. // Ok, we want to look for words to:
  83. // MaNGlE , or transpose (both?!)
  84. // or possibly transpose words
  85. }
  86. int main(int argc, char * argv[])
  87. {
  88. int master;
  89. pid_t pid;
  90. #ifdef LOGGING
  91. FILE * fp;
  92. fp = fopen("mystic_pipe.data", "wb");
  93. if ( fp == NULL ) {
  94. return 2;
  95. }
  96. #endif
  97. srandom(time(NULL));
  98. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown -Ubugz -PUWISHPASSWORD
  99. // -U<username>
  100. for (int x = 0; x < argc; x++) {
  101. if (strncmp("-U", argv[x], 2) == 0) {
  102. username = argv[x] + 2;
  103. #ifdef LOGGING
  104. fprintf(fp, "Username: [%s]\n", username);
  105. #endif
  106. break;
  107. }
  108. }
  109. if (username != NULL) {
  110. locate_user(username);
  111. #ifdef LOGGING
  112. fprintf(fp, "Username: [%s] A.K.A. [%s]\n", username, fullname);
  113. #endif
  114. }
  115. // With IGNBRK I don't think I need this anymore.
  116. // signal(SIGINT, SIG_IGN);
  117. pid = forkpty(&master, NULL, NULL, NULL);
  118. // impossible to fork
  119. if (pid < 0) {
  120. return 1;
  121. }
  122. // child
  123. else if (pid == 0) {
  124. char *args[20]; // max 20 args
  125. int x;
  126. args[0] = TARGET;
  127. for ( x = 1; x < argc; x++ ) {
  128. args[x] = argv[x];
  129. };
  130. args[x] = NULL;
  131. // char *args[] = { "vim", "test.txt", NULL }; // argv; //{ NULL };
  132. // run Mystic, run!
  133. execvp( TARGET, args);
  134. }
  135. // parent
  136. else {
  137. // remove the echo
  138. // ICANON - raw mode. No more line buffering!
  139. struct termios tios, orig1;
  140. struct timeval timeout;
  141. int last_event;
  142. tcgetattr(master, &tios);
  143. tios.c_lflag &= ~(ECHO | ECHONL | ICANON );
  144. tcsetattr(master, TCSAFLUSH, &tios);
  145. tcgetattr(1, &orig1);
  146. tios = orig1;
  147. tios.c_iflag &= ~(ICRNL | IXON); // Disable software flow control
  148. tios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN );
  149. // https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html
  150. // ISIG should be Ctrl-C and Ctrl-Z IGNBRK +
  151. tcsetattr(1, TCSAFLUSH, &tios);
  152. for (;;) {
  153. // define estruturas para o select, que serve para verificar qual
  154. // se tornou "pronto pra uso"
  155. fd_set read_fd;
  156. fd_set write_fd;
  157. fd_set except_fd;
  158. // inicializa as estruturas
  159. FD_ZERO(&read_fd);
  160. FD_ZERO(&write_fd);
  161. FD_ZERO(&except_fd);
  162. // atribui o descritor master, obtido pelo forkpty, ao read_fd
  163. FD_SET(master, &read_fd);
  164. // atribui o stdin ao read_fd
  165. FD_SET(STDIN_FILENO, &read_fd);
  166. // o descritor tem que ser unico para o programa, a documentacao
  167. // recomenda um calculo entre os descritores sendo usados + 1
  168. // we're in luck! The last parameter is time interval/timeout. :D
  169. timeout.tv_sec = 10;
  170. timeout.tv_usec = 0;
  171. // select(master+1, &read_fd, &write_fd, &except_fd, NULL);
  172. if ( select(master+1, &read_fd, &write_fd, &except_fd, &timeout) == 0 ) {
  173. // This means timeout!
  174. harry_event(STDOUT_FILENO);
  175. #ifdef LOGGING
  176. fprintf(fp, "%s : TICK\n", it_is_now());
  177. #endif
  178. }
  179. char input[BSIZE + 1];
  180. char output[BSIZE + 1];
  181. int total;
  182. // read_fd esta atribuido com read_fd?
  183. if (FD_ISSET(master, &read_fd))
  184. {
  185. // leia o que bc esta mandando
  186. if ((total = read(master, &output, BSIZE)) != -1) {
  187. // e escreva isso na saida padrao
  188. output[total] = 0;
  189. if (random() % 20 < 3) {
  190. mangle( output );
  191. }
  192. write(STDOUT_FILENO, &output, total);
  193. // This is OUTPUT from the BBS
  194. #ifdef LOGGING
  195. fprintf(fp, ">> [%s]\n", output);
  196. #endif
  197. } else
  198. break;
  199. }
  200. // read_fd esta atribuido com a entrada padrao?
  201. if (FD_ISSET(STDIN_FILENO, &read_fd))
  202. {
  203. // leia a entrada padrao
  204. total = read(STDIN_FILENO, &input, BSIZE);
  205. input[total] = 0;
  206. // e escreva no bc
  207. write(master, &input, total);
  208. // This is INPUT from the USER
  209. #ifdef LOGGING
  210. fprintf(fp, "<< [%s]\n", input);
  211. #endif
  212. }
  213. }
  214. tcsetattr(1, TCSAFLUSH, &orig1);
  215. #ifdef LOGGING
  216. fclose(fp);
  217. #endif
  218. }
  219. return 0;
  220. }