mystic.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h> // usleep()
  4. #include <fcntl.h>
  5. #include <pty.h>
  6. #include <termios.h>
  7. #include <sys/select.h>
  8. #include <sys/wait.h>
  9. // #include <signal.h> // handle Ctrl-C/SIGINT
  10. #include <strings.h> // strcasecmp
  11. #include <time.h> // usleep(), nanonsleep() ?
  12. #include <ctype.h>
  13. #include <stdlib.h> // random()
  14. #include <regex.h>
  15. // LOGGING with file output
  16. #include "zf_log.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. FILE *g_log_file;
  20. static void file_output_callback(const zf_log_message *msg, void *arg) {
  21. (void)arg;
  22. *msg->p = '\n';
  23. fwrite(msg->buf, msg->p - msg->buf + 1, 1, g_log_file);
  24. fflush(g_log_file);
  25. }
  26. static void file_output_close(void) { fclose(g_log_file); }
  27. static void file_output_open(const char *const log_path) {
  28. g_log_file = fopen(log_path, "a");
  29. if (!g_log_file) {
  30. ZF_LOGW("Failed to open log file %s", log_path);
  31. return;
  32. }
  33. atexit(file_output_close);
  34. zf_log_set_output_v(ZF_LOG_PUT_STD, 0, file_output_callback);
  35. }
  36. // END LOGGING
  37. /*
  38. What is the name of the actual, real Mystic executable
  39. that we'll be executing and mangling?
  40. */
  41. #define TARGET "./mySTIC"
  42. // Size of our input and output buffers.
  43. #define BSIZE 128
  44. /**
  45. * Display a repr of the given string.
  46. *
  47. * This converts most \n\r\v\f\t codes,
  48. * defaults to \xHH (hex value).
  49. */
  50. const char *repr(const char *data) {
  51. static char buffer[4096];
  52. char *cp;
  53. strcpy(buffer, data);
  54. cp = buffer;
  55. while (*cp != 0) {
  56. char c = *cp;
  57. if (isspace(c)) {
  58. if (c == ' ') {
  59. cp++;
  60. continue;
  61. };
  62. /* Ok, it's form-feed ('\f'), newline ('\n'), carriage return ('\r'),
  63. * horizontal tab ('\t'), and vertical tab ('\v') */
  64. memmove(cp + 1, cp, strlen(cp) + 1);
  65. *cp = '\\';
  66. cp++;
  67. switch (c) {
  68. case '\f':
  69. *cp = 'f';
  70. cp++;
  71. break;
  72. case '\n':
  73. *cp = 'n';
  74. cp++;
  75. break;
  76. case '\r':
  77. *cp = 'r';
  78. cp++;
  79. break;
  80. case '\t':
  81. *cp = 't';
  82. cp++;
  83. break;
  84. case '\v':
  85. *cp = 'v';
  86. cp++;
  87. break;
  88. default:
  89. *cp = '?';
  90. cp++;
  91. break;
  92. }
  93. continue;
  94. }
  95. if (isprint(c)) {
  96. cp++;
  97. continue;
  98. };
  99. // Ok, default to \xHH output.
  100. memmove(cp + 3, cp, strlen(cp) + 1);
  101. *cp = '\\';
  102. cp++;
  103. *cp = 'x';
  104. cp++;
  105. char buffer[3];
  106. sprintf(buffer, "%02x", (int)c & 0xff);
  107. *cp = buffer[0];
  108. cp++;
  109. *cp = buffer[1];
  110. cp++;
  111. continue;
  112. }
  113. return buffer;
  114. }
  115. /*
  116. * string_insert()
  117. * Inserts a string into a given position.
  118. * This safely checks to make sure the buffer isn't overrun.
  119. */
  120. int string_insert( char * buffer, int max_length, int pos, const char * insert ) {
  121. if (strlen(buffer) + strlen(insert) >= max_length) {
  122. ZF_LOGD("string_insert() failed inserting [%s]", repr(insert));
  123. return 0;
  124. }
  125. memmove(buffer + pos + strlen(insert), buffer + pos, strlen(buffer + pos) + 1 );
  126. // cp + strlen(display), cp, strlen(cp) + 1);
  127. strncpy(buffer+pos, insert, strlen(insert));
  128. // (cp, display, strlen(display));
  129. return 1; // success
  130. }
  131. // Should this be passed around to the functions that use it?
  132. struct render {
  133. int speed;
  134. int effect;
  135. } current_render;
  136. int render_overlimit = 0;
  137. void reset_render(void) {
  138. current_render.speed = 0;
  139. current_render.effect = 0;
  140. render_overlimit = 0;
  141. }
  142. #define TRIGGER "^"
  143. // Max limit we'll sleep before ignoring effects/speed.
  144. #define SLEEP_LIMIT 30
  145. int ms_sleep(unsigned int ms) {
  146. int result = 0;
  147. struct timespec ts = {ms / 1000, (ms % 1000) * 1000000L};
  148. do {
  149. struct timespec ts_sleep = ts;
  150. result = nanosleep(&ts_sleep, &ts);
  151. } while ((-1 == result));
  152. return result;
  153. }
  154. void render_sleep(void) {
  155. if (render_overlimit)
  156. return;
  157. if (current_render.speed) { // * 100 still too slow.
  158. ms_sleep(current_render.speed * 10);
  159. }
  160. }
  161. /*
  162. Terminal tracking
  163. Actually, I believe I only really need to track the color information.
  164. Everything else, I'm not sure I really care about.
  165. */
  166. struct console_details {
  167. int posx, posy;
  168. int savedx, savedy;
  169. char ansi[20]; // current ANSI command being processed.
  170. int in_ansi;
  171. int fgcolor; // 0-7 // not 0-15
  172. int bgcolor; // 0-7
  173. int status; // 0, 1 or 5 (Blink)
  174. } console;
  175. void console_init(struct console_details *cdp) {
  176. cdp->posx = 0;
  177. cdp->posy = 0;
  178. cdp->savedx = 0;
  179. cdp->savedy = 0;
  180. cdp->in_ansi = 0;
  181. cdp->fgcolor = 7;
  182. cdp->bgcolor = 0;
  183. cdp->status = 0;
  184. }
  185. void ansi_color(struct console_details *cdp, int color) {
  186. ZF_LOGD("ansi_color(%d)", color);
  187. if (color == 0) {
  188. cdp->status = 0;
  189. cdp->fgcolor = 7;
  190. cdp->bgcolor = 0;
  191. return;
  192. }
  193. if (color == 1) {
  194. cdp->status = 1;
  195. return;
  196. }
  197. if (color == 5) {
  198. cdp->status = 5;
  199. return;
  200. }
  201. if ((color >= 30) && (color <= 37)) {
  202. cdp->fgcolor = color - 30;
  203. return;
  204. }
  205. if ((color >= 40) && (color <= 47)) {
  206. cdp->bgcolor = color - 40;
  207. return;
  208. }
  209. ZF_LOGD("ansi_color( %d ) is unknown to me.", color);
  210. }
  211. const char *color_restore(struct console_details *cdp) {
  212. static char buffer[30];
  213. if (cdp->status == 0) {
  214. sprintf(buffer, "\x1b[0;3%d;4%dm", cdp->fgcolor, cdp->bgcolor);
  215. } else {
  216. sprintf(buffer, "\x1b[0;%d;3%d;4%dm", cdp->status, cdp->fgcolor,
  217. cdp->bgcolor);
  218. };
  219. return buffer;
  220. }
  221. void console_ansi(struct console_details *cdp, const char *ansi) {
  222. int understood = 0;
  223. const char *cp = ansi;
  224. const char *last = ansi + strlen(ansi) - 1;
  225. int number, number2;
  226. if (*cp == '\x1b') {
  227. cp++;
  228. // Ok, that's expected.
  229. if (*cp == '[') {
  230. cp++;
  231. // https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences
  232. switch (*last) {
  233. case 'A':
  234. // cursor up
  235. if (cp == last) {
  236. number = 1;
  237. } else {
  238. number = atoi(cp);
  239. if (number < 1) {
  240. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  241. number);
  242. number = 1;
  243. }
  244. };
  245. cdp->posy -= number;
  246. if (cdp->posy < 0) {
  247. cdp->posy = 0;
  248. ZF_LOGD(
  249. "console_ansi( %s ): attempt to move above top of screen (%d)",
  250. repr(ansi), number);
  251. }
  252. understood = 1;
  253. return;
  254. case 'B':
  255. // cursor down
  256. if (cp == last) {
  257. number = 1;
  258. } else {
  259. number = atoi(cp);
  260. if (number < 1) {
  261. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  262. number);
  263. number = 1;
  264. }
  265. };
  266. cdp->posy += number;
  267. // check range/"scroll"
  268. understood = 1;
  269. return;
  270. case 'C':
  271. // cursor forward
  272. if (cp == last) {
  273. number = 1;
  274. } else {
  275. number = atoi(cp);
  276. if (number < 1) {
  277. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  278. number);
  279. number = 1;
  280. }
  281. };
  282. cdp->posx += number;
  283. // Well. According to the "spec", the screen limits are hard
  284. // If the cursor is already at the edge of the screen, this has no
  285. // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
  286. while (cdp->posx > 79) {
  287. cdp->posy++;
  288. // check range/"scroll"
  289. cdp->posx -= 79;
  290. }
  291. understood = 1;
  292. return;
  293. case 'D':
  294. // cursor backwards
  295. if (cp == last) {
  296. number = 1;
  297. } else {
  298. number = atoi(cp);
  299. if (number < 1) {
  300. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  301. number);
  302. number = 0;
  303. }
  304. };
  305. cdp->posx -= number;
  306. // Well. According to the "spec", the screen limits are hard
  307. // If the cursor is already at the edge of the screen, this has no
  308. // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
  309. while (cdp->posx < 0) {
  310. cdp->posy--;
  311. if (cdp->posy < 0) {
  312. cdp->posy = 0;
  313. }
  314. cdp->posx += 79;
  315. }
  316. understood = 1;
  317. return;
  318. case 'H':
  319. // cursor position
  320. if (*cp == ';') {
  321. // Missing first number
  322. number = 1;
  323. cp++;
  324. if (cp == last) {
  325. // missing 2nd number as well?
  326. number2 = 1;
  327. } else {
  328. number2 = atoi(cp);
  329. }
  330. } else {
  331. // Ok, find the first number
  332. number = atoi(cp);
  333. cp = strchr(cp, ';');
  334. if (cp == NULL) {
  335. // Missing 2nd number
  336. number2 = 1;
  337. } else {
  338. // 2nd number found, maybe.
  339. cp++;
  340. if (cp == last) {
  341. number2 = 1;
  342. } else {
  343. number2 = atoi(cp);
  344. }
  345. }
  346. }
  347. // Our positions start at zero, not one.
  348. cdp->posx = number - 1;
  349. cdp->posy = number2 - 1;
  350. understood = 1;
  351. break;
  352. case 'J':
  353. // clear
  354. if (cp == last) {
  355. number = 0;
  356. } else {
  357. number = atoi(cp);
  358. };
  359. // clears ... part of the screen.
  360. if (number == 2) {
  361. cdp->posx = 0;
  362. cdp->posy = 0;
  363. };
  364. understood = 1;
  365. break;
  366. case 'm':
  367. // color
  368. if (cp == last) {
  369. // nothing given, default to 0.
  370. number = 0;
  371. ansi_color(cdp, number);
  372. } else {
  373. while (cp != last) {
  374. number = atoi(cp);
  375. ansi_color(cdp, number);
  376. cp++;
  377. while ((cp != last) && (isdigit(*cp))) {
  378. cp++;
  379. };
  380. if (cp != last) {
  381. if (*cp == ';') {
  382. cp++;
  383. }
  384. }
  385. }
  386. }
  387. understood = 1;
  388. break;
  389. case 't':
  390. case 'r':
  391. case 'h':
  392. case '!':
  393. // These are ones that I don't care about.
  394. case 'n': // This is terminal detect -- give me cursor position
  395. understood = 1;
  396. break;
  397. default:
  398. // unsure -- possibly not important
  399. ZF_LOGD("console_ansi( %s ): ???", repr(ansi));
  400. understood = 0;
  401. }
  402. }
  403. };
  404. if (!understood) {
  405. ZF_LOGD("console_ansi( %s ): was not understood.", repr(ansi));
  406. }
  407. }
  408. /*
  409. * console_char()
  410. * return whether or not we are still in_ansi
  411. */
  412. int console_char(struct console_details *cdp, char ch) {
  413. char *cp;
  414. if (cdp->in_ansi) {
  415. // Ok, append this char
  416. cp = cdp->ansi + strlen(cdp->ansi);
  417. *cp = ch;
  418. cp++;
  419. *cp = 0;
  420. if (isalpha(ch)) {
  421. // Ok!
  422. console_ansi(cdp, cdp->ansi);
  423. cdp->in_ansi = 0;
  424. cdp->ansi[0] = 0;
  425. return 1;
  426. }
  427. return 1;
  428. } else {
  429. if (ch == '\x1b') {
  430. cp = cdp->ansi;
  431. *cp = ch;
  432. cp++;
  433. *cp = 0;
  434. cdp->in_ansi = 1;
  435. return 1;
  436. }
  437. if (ch == '\r') {
  438. // Carriage return
  439. cdp->posx = 0;
  440. return 0;
  441. }
  442. if (ch == '\n') {
  443. cdp->posy++;
  444. // check range/"scroll"
  445. return 0;
  446. }
  447. if (ch == '\b') {
  448. // Backspace.
  449. if (cdp->posx > 0) {
  450. cdp->posx--;
  451. }
  452. return 0;
  453. }
  454. if (ch == '\f') {
  455. // form feed
  456. // treat as clear screen
  457. cdp->posx = 0;
  458. cdp->posy = 0;
  459. return 0;
  460. }
  461. /*
  462. I don't believe that anything else can possibly be here, other then an
  463. actual printable character. So!
  464. */
  465. cdp->posx++;
  466. if (cdp->posx > 79) {
  467. cdp->posx = 0;
  468. cdp->posy++;
  469. // check range/"scroll"
  470. }
  471. return 0;
  472. }
  473. }
  474. void console_string(struct console_details *cdp, const char *chars) {
  475. int x;
  476. for (x = 0; x < strlen(chars); x++) {
  477. console_char(cdp, chars[x]);
  478. }
  479. }
  480. void console_receive(struct console_details *cdp, const char *chars, int len) {
  481. int x;
  482. for (x = 0; x < len; x++) {
  483. console_char(cdp, chars[x]);
  484. }
  485. }
  486. /*
  487. Well SNAP! Mystic numbers don't remotely match ANSI color codes.
  488. 00 : Sets the current foreground to Black 0;30
  489. 01 : Sets the current foreground to Dark Blue 0;34
  490. 02 : Sets the current foreground to Dark Green 0;32
  491. 03 : Sets the current foreground to Dark Cyan 0;36
  492. 04 : Sets the current foreground to Dark Red 0;31
  493. 05 : Sets the current foreground to Dark Magenta 0;35
  494. 06 : Sets the current foreground to Brown 0;33
  495. 07 : Sets the current foreground to Grey 0;37
  496. 08 : Sets the current foreground to Dark Grey 1;30
  497. 09 : Sets the current foreground to Light Blue 1;34
  498. 10 : Sets the current foreground to Light Green 1;32
  499. 11 : Sets the current foreground to Light Cyan 1;36
  500. 12 : Sets the current foreground to Light Red 1;31
  501. 13 : Sets the current foreground to Light Magenta 1;35
  502. 14 : Sets the current foreground to Yellow 1;33
  503. 15 : Sets the current foreground to White 1;37
  504. 16 : Sets the current background to Black 40
  505. 17 : Sets the current background to Blue 44
  506. 18 : Sets the current background to Green 42
  507. 19 : Sets the current background to Cyan 46
  508. 20 : Sets the current background to Red 41
  509. 21 : Sets the current background to Magenta 45
  510. 22 : Sets the current background to Brown 43
  511. 23 : Sets the current background to Grey 47
  512. 24 : Sets the current background to black with blinking foreground 5;40
  513. 25 : Sets the current background to blue with blinking foreground 5;44
  514. 26 : Sets the current background to green with blinking foreground 5;42
  515. 27 : Sets the current background to cyan with blinking foreground 5;46
  516. 28 : Sets the current background to red with blinking foreground 5;41
  517. 29 : Sets the current background to magenta with blinking foreground 5;45
  518. 30 : Sets the current background to brown with blinking foreground 5;43
  519. 31 : Sets the current background to grey with blinking foreground 5;47
  520. Other things that Mystic does ...
  521. [A## - Move the cursor up ## lines
  522. [B## - Move the cursor down ## lines
  523. [C## - Move the cursor forward (to the right) ## columns
  524. [D## - Move the cursor backwards (to the left) ## columns
  525. [K - Clear from the current cursor position to the end of the line
  526. [L - Move cursor and erase data backwards from current column to column ##
  527. [X## - Move cursor to X coordinate ##
  528. [Y## - Move cursor to Y coordinate ##
  529. BS - Sends 1 destructive backspace sequence (ASCII 8-32-8)
  530. CL - Clears the screen (ANSI 1,1 locate and [2J or ASCII 12)
  531. CR - Send a carrage return and line feed (move to next line)
  532. RA - Restore the saved text attribute color
  533. RS - Restore the saved user's terminal screen
  534. SA - Save the current text attribute color
  535. SS - Save the entire user's terminal screen
  536. */
  537. // Covert MYSTIC color to (Proper) ANSI COLOR.
  538. const int MYSTIC[] = {0, 4, 2, 6, 1, 5, 3, 7};
  539. // ANSI_color = MYSTIC[ odd_mystic_color % 8 ]
  540. void write_color(int fd, int color) {
  541. char buffer[12];
  542. switch (color) {
  543. case 0:
  544. case 1:
  545. case 2:
  546. case 3:
  547. case 4:
  548. case 5:
  549. case 6:
  550. case 7:
  551. sprintf(buffer, "\x1b[0;3%dm", MYSTIC[color]);
  552. break;
  553. case 8:
  554. case 9:
  555. case 10:
  556. case 11:
  557. case 12:
  558. case 13:
  559. case 14:
  560. case 15:
  561. sprintf(buffer, "\x1b[0;1;3%dm", MYSTIC[color - 8]);
  562. break;
  563. case 16:
  564. case 17:
  565. case 18:
  566. case 19:
  567. case 20:
  568. case 21:
  569. case 22:
  570. case 23:
  571. sprintf(buffer, "\x1b[4%dm", MYSTIC[color - 16]);
  572. break;
  573. case 24:
  574. case 25:
  575. case 26:
  576. case 27:
  577. case 28:
  578. case 29:
  579. case 30:
  580. case 31:
  581. sprintf(buffer, "\x1b[5;4%dm", MYSTIC[color - 24]);
  582. break;
  583. default:
  584. buffer[0] = 0;
  585. break;
  586. }
  587. ZF_LOGD("write_color( %d ): %s", color, repr(buffer));
  588. write(fd, buffer, strlen(buffer));
  589. }
  590. /**
  591. * process_trigger( fd, *cp )
  592. *
  593. * This process a command trigger.
  594. * It has seen TRIGGER, and now it is
  595. * processing whatever comes after it.
  596. * It will perform the process, and
  597. * return the char * of whatever is next.
  598. */
  599. const char *process_trigger(int fd, const char *cp) {
  600. char ch;
  601. int i, x, y;
  602. ch = toupper(*cp);
  603. cp++;
  604. switch (ch) {
  605. case 'D':
  606. i = 0;
  607. if (isdigit(*cp)) {
  608. i = (*cp) - '0';
  609. cp++;
  610. };
  611. if (isdigit(*cp)) {
  612. i *= 10;
  613. i += (*cp) - '0';
  614. cp++;
  615. };
  616. if ((i > 0) && (i < 80)) {
  617. ZF_LOGI("DEL %02d", i);
  618. for (x = 0; x < i; x++) {
  619. write(fd, "\b \b", 3);
  620. }
  621. };
  622. break;
  623. case 'C':
  624. i = 0;
  625. if (*cp == 'R') {
  626. cp++;
  627. const char *restore = color_restore(&console);
  628. write(fd, restore, strlen(restore));
  629. break;
  630. }
  631. if (isdigit(*cp)) {
  632. i = (*cp) - '0';
  633. cp++;
  634. };
  635. if (isdigit(*cp)) {
  636. i *= 10;
  637. i += (*cp) - '0';
  638. cp++;
  639. };
  640. write_color(fd, i);
  641. break;
  642. case 'G':
  643. x = 0;
  644. if (isdigit(*cp)) {
  645. x = (*cp) - '0';
  646. cp++;
  647. };
  648. if (isdigit(*cp)) {
  649. x *= 10;
  650. x += (*cp) - '0';
  651. cp++;
  652. };
  653. y = 0;
  654. if (isdigit(*cp)) {
  655. y = (*cp) - '0';
  656. cp++;
  657. };
  658. if (isdigit(*cp)) {
  659. y *= 10;
  660. y += (*cp) - '0';
  661. cp++;
  662. };
  663. char buffer[20]; // row ; column H
  664. ZF_LOGD("GOTO (%d,%d)", x, y);
  665. sprintf(buffer, "\x1b[%d;%dH", y, x);
  666. write(fd, buffer, strlen(buffer));
  667. break;
  668. case 'R':
  669. i = 0;
  670. if (isdigit(*cp)) {
  671. i = (*cp) - '0';
  672. cp++;
  673. };
  674. if ((i > 0) && (i < 10)) {
  675. ZF_LOGI("RENDER %d", i);
  676. current_render.effect = i;
  677. } else {
  678. current_render.effect = 0;
  679. }
  680. break;
  681. case 'S':
  682. i = 0;
  683. if (isdigit(*cp)) {
  684. i = (*cp) - '0';
  685. cp++;
  686. };
  687. if ((i > 0) && (i < 10)) {
  688. ZF_LOGI("SPEED %d", i);
  689. current_render.speed = i;
  690. } else {
  691. current_render.speed = 0;
  692. }
  693. break;
  694. case 'P':
  695. i = 0;
  696. if (isdigit(*cp)) {
  697. i = (*cp) - '0';
  698. cp++;
  699. };
  700. if ((i > 0) && (i < 10)) {
  701. ZF_LOGI("PAWS %d", i);
  702. // sleep(i);
  703. if (!render_overlimit) {
  704. sleep(i);
  705. };
  706. }
  707. break;
  708. }
  709. return cp;
  710. }
  711. /**
  712. * render_effect( fd, ch )
  713. *
  714. * Displays the given character with whatever
  715. * rendering effect is currently active.
  716. * (If any).
  717. */
  718. void render_effect(int fd, char ch) {
  719. int effect = current_render.effect;
  720. int l;
  721. char space = ' ';
  722. char bs = '\b';
  723. switch (effect) {
  724. case 1:
  725. // CHAR + SPC + BS
  726. render_sleep();
  727. write(fd, &ch, 1);
  728. render_sleep();
  729. write(fd, &space, 1);
  730. render_sleep();
  731. render_sleep();
  732. write(fd, &bs, 1);
  733. break;
  734. case 2:
  735. // CHAR + 8 spaces + 8 BS
  736. render_sleep();
  737. write(fd, &ch, 1);
  738. for (l = 0; l < 8; l++) {
  739. render_sleep();
  740. write(fd, &space, 1);
  741. }
  742. for (l = 0; l < 8; l++) {
  743. render_sleep();
  744. write(fd, &bs, 1);
  745. }
  746. break;
  747. case 0:
  748. default:
  749. // NORMAL
  750. render_sleep();
  751. write(fd, &ch, 1);
  752. break;
  753. }
  754. }
  755. /**
  756. * render( fd, string_out )
  757. *
  758. * Render an entire string.
  759. * Handles TRIGGER.
  760. * Renders with effects.
  761. */
  762. void render(int fd, const char *string_out) {
  763. const char *cp = string_out;
  764. const char *trigger = cp;
  765. time_t start = time(NULL);
  766. int elapsed;
  767. int over = 0;
  768. reset_render();
  769. ZF_LOGD("render(%d, %s)", fd, repr(string_out));
  770. // Check our time from time to time.
  771. // If we start running long, disable sleeps.
  772. while ((trigger = strstr(cp, TRIGGER)) != NULL) {
  773. // There is special things to handle in here.
  774. while (cp != trigger) {
  775. elapsed = time(NULL) - start;
  776. if (elapsed > SLEEP_LIMIT) {
  777. render_overlimit = 1;
  778. current_render.speed = 0;
  779. };
  780. // write(fd, cp, 1 );
  781. render_effect(fd, *cp);
  782. cp++;
  783. };
  784. // ZF_LOGI( "at trigger: (%s)", cp);
  785. cp += strlen(TRIGGER);
  786. // Ok, we're pointing at the trigger -- do something.
  787. cp = process_trigger(fd, cp);
  788. // ZF_LOGI( "after trigger: (%s)", cp);
  789. };
  790. // We still might be under a rendering effect.
  791. while (*cp != 0) {
  792. elapsed = time(NULL) - start;
  793. if (elapsed > SLEEP_LIMIT) {
  794. render_overlimit = 1;
  795. current_render.speed = 0;
  796. };
  797. // write(fd, cp, 1);
  798. render_effect(fd, *cp);
  799. cp++;
  800. }
  801. }
  802. // Beanzilla's no repeats
  803. /**
  804. * have_seen( list, len, item )
  805. *
  806. * Returns 1 (true) if item is in the list.
  807. * Rotates the list [x] = [x+1], and
  808. * list[len-1] = item, return 0 (false)
  809. */
  810. int have_seen(int *list, int len, int item) {
  811. int x;
  812. for (x = 0; x < len; x++) {
  813. if (list[x] == item) {
  814. return 1;
  815. }
  816. };
  817. // Ok, it is something different
  818. for (x = 0; x < len - 1; x++) {
  819. list[x] = list[x + 1];
  820. };
  821. list[x] = item;
  822. return 0;
  823. }
  824. /**
  825. * init_have_seen( list, len )
  826. *
  827. * Initialize the have_seen list with -1.
  828. * (-1 isn't a valid index, so we start
  829. * out with all invalid.)
  830. */
  831. void init_have_seen(int *list, int len) {
  832. int x;
  833. for (x = 0; x < len; x++) {
  834. list[x] = -1;
  835. }
  836. }
  837. /*
  838. These are harry "timeout" events.
  839. These happen when we've been sitting around awhile.
  840. */
  841. #define MAX_HARRY_EVENT_DUPS 2
  842. int last_seen_harry_event[MAX_HARRY_EVENT_DUPS];
  843. #ifdef CPP_MADMAN_STL_CODE
  844. const char *random_phrase(const char *words, int len, int last_seen) {
  845. // ooh. a map of char *s to last_seen_events. :P
  846. static map<const char *, array<int>> tracker;
  847. map<const char *, array<int>>::iterator it;
  848. array<int, last_seen> it = tracker.find(words);
  849. if (it == tracker.end()) {
  850. // key does not exist.
  851. array<int, last_seen> last;
  852. for (int i = 0; i < last_seen; i++) {
  853. last[i] = -1;
  854. };
  855. tracker.insert(words, last);
  856. it = tracker.find(words);
  857. };
  858. }
  859. #endif
  860. void harry_idle_event(int fd) {
  861. // Make something happen
  862. char buffer[100];
  863. int r;
  864. // This is no where near finished, BUT!
  865. const char *phrases[] = {"Hahaha", "Snicker, snicker", "Boo!",
  866. "MeOW", "I see U", "Arrooo!",
  867. "Ahh-wooo!", "Aaaooo!"};
  868. const char *cp;
  869. // Remember the last phrase used,
  870. // and don't repeat (the last two)!
  871. do {
  872. r = random() % ((sizeof(phrases) / sizeof(char *)) - 1);
  873. } while (have_seen(last_seen_harry_event, MAX_HARRY_EVENT_DUPS, r));
  874. ZF_LOGD("%d => %d %d", r, last_seen_harry_event[0], last_seen_harry_event[1]);
  875. cp = phrases[r];
  876. int color = random() % 16;
  877. if (color == 0) {
  878. color++;
  879. } // If it's 0 let's make it 1. // color = (random() % 15) + 1
  880. sprintf(buffer, "^S2^C%02d%s^P2^CR^D%02d", color, cp, (int)strlen(cp));
  881. ZF_LOGD("harry_event: render(%d, \"%s\")", fd, buffer);
  882. render(fd, buffer);
  883. }
  884. void init_harry() {
  885. init_have_seen(last_seen_harry_event, MAX_HARRY_EVENT_DUPS);
  886. // ZF_LOGD("init => %d %d", last_seen_harry_event[0],
  887. // last_seen_harry_event[1]);
  888. console_init(&console);
  889. }
  890. /*
  891. The code to get the username and fullname is useless on telnet
  892. connections.
  893. */
  894. char *username = NULL;
  895. char *fullname = NULL;
  896. /*
  897. Pascal String Copy. Copy from pascal string, to C String.
  898. First char is pascal string length. (Max 255).
  899. */
  900. void pcopy(char *pstring, char *str) {
  901. int len = (int)*pstring;
  902. strncpy(str, pstring + 1, len);
  903. str[len] = 0;
  904. }
  905. /*
  906. This only works for those few idiots that use the
  907. horribly broken SSH crap that Mystic uses.
  908. */
  909. int locate_user(const char *alias) {
  910. FILE *user;
  911. char buffer[0x600];
  912. char temp[100];
  913. user = fopen("data/users.dat", "rb");
  914. if (user == NULL)
  915. return 0;
  916. // Carry on!
  917. while (fread(buffer, 0x600, 1, user) == 1) {
  918. pcopy(buffer + 0x6d, temp);
  919. if (strcasecmp(temp, username) == 0) {
  920. pcopy(buffer + 0x8c, temp);
  921. fullname = strdup(temp);
  922. break;
  923. }
  924. /*
  925. printf("Alias: %s\n", temp);
  926. pcopy(buffer + 0x8c, temp );
  927. printf("Full Name: %s\n", temp );
  928. */
  929. }
  930. fclose(user);
  931. return 1;
  932. }
  933. // Buffers are BSIZE + 1, so a buffer that size can strcpy safely.
  934. regex_t ANSI;
  935. regex_t WORDS;
  936. regex_t WORD;
  937. int init_regex(void) {
  938. int ret;
  939. char ansi[] = "\x1b\[[0-9]+(;[0-9]+)*?[a-zA-Z]";
  940. char words[] = "[a-zA-Z]+( [a-zA-Z]+)+";
  941. char word[] = "[a-zA-Z]+";
  942. char errorbuf[100];
  943. if (ret = regcomp(&ANSI, ansi, REG_EXTENDED | REG_NEWLINE)) {
  944. regerror(ret, &ANSI, errorbuf, sizeof(errorbuf));
  945. ZF_LOGW("Regex %s failed to compile: %s", ansi, errorbuf);
  946. return 0;
  947. };
  948. if (ret = regcomp(&WORDS, words, REG_EXTENDED | REG_NEWLINE)) {
  949. regerror(ret, &WORDS, errorbuf, sizeof(errorbuf));
  950. ZF_LOGW("Regex %s failed to compile: %s", words, errorbuf);
  951. return 0;
  952. };
  953. if (ret = regcomp(&WORD, word, REG_EXTENDED | REG_NEWLINE)) {
  954. regerror(ret, &WORD, errorbuf, sizeof(errorbuf));
  955. ZF_LOGW("Regex %s failed to compile: %s", word, errorbuf);
  956. return 0;
  957. };
  958. return 1;
  959. }
  960. int regmatch(regex_t *preg, const char *string, size_t nmatch,
  961. regmatch_t pmatch[], int eflags) {
  962. // returns number of matches found. (Max nmatch)
  963. int matches = 0;
  964. int offset = 0;
  965. int ret;
  966. while (matches < nmatch) {
  967. ret = regexec(preg, string + offset, nmatch - matches, pmatch + matches,
  968. eflags);
  969. if (!ret) {
  970. int current = offset;
  971. offset += pmatch[matches].rm_eo;
  972. pmatch[matches].rm_so += current;
  973. pmatch[matches].rm_eo += current;
  974. matches++;
  975. } else if (ret == REG_NOMATCH) {
  976. break;
  977. } else {
  978. break;
  979. }
  980. }
  981. return matches;
  982. }
  983. #define MAX_MATCH 32
  984. regmatch_t rxmatch[MAX_MATCH];
  985. int rx_match(regex_t *regex, const char *buffer) {
  986. int ret;
  987. ret = regmatch(regex, buffer, MAX_MATCH, rxmatch, 0);
  988. if (0) {
  989. for (int i = 0; i < ret; i++) {
  990. ZF_LOGI("%d : (%d-%d)", i, rxmatch[i].rm_so, rxmatch[i].rm_eo);
  991. }
  992. }
  993. return ret;
  994. }
  995. /*
  996. Terminal processing section.
  997. Monitor lines, position, color.
  998. What screen size do I want to emulate?
  999. ANSI codes.
  1000. Do I need this??
  1001. */
  1002. /**
  1003. * random_activate()
  1004. *
  1005. * Is a weight (1-10),
  1006. * tests if random number is < weight * 10.
  1007. *
  1008. * So random_activate(9) happens more frequently
  1009. * then random_activate(8) or lower.
  1010. *
  1011. * This probably needs to be fixed.
  1012. * We need a better randint(RANGE) code.
  1013. */
  1014. int random_activate(int w) {
  1015. int r = random() % 100;
  1016. if (r <= (w * 10)) {
  1017. return 1;
  1018. };
  1019. return 0;
  1020. }
  1021. /*
  1022. word_state():
  1023. -1 only lower
  1024. +1 only upper
  1025. 0 mixed
  1026. */
  1027. int word_state(const char *buffer, int len) {
  1028. int p;
  1029. int upper = 0;
  1030. int lower = 0;
  1031. int ret;
  1032. float pct;
  1033. for (p = 0; p < len; p++) {
  1034. char c = buffer[p];
  1035. if (isalpha(c)) {
  1036. if (isupper(c)) {
  1037. upper++;
  1038. };
  1039. if (islower(c)) {
  1040. lower++;
  1041. };
  1042. }
  1043. }
  1044. if (upper == lower) {
  1045. return 0;
  1046. }
  1047. if (upper > lower) {
  1048. ret = 1;
  1049. pct = ((float)lower / (float)upper) * 100.0;
  1050. } else {
  1051. ret = -1;
  1052. pct = ((float)upper / (float)lower) * 100.0;
  1053. }
  1054. // ZF_LOGD("So far %d with %f %%", ret, pct);
  1055. if (pct < 40.0) {
  1056. return ret;
  1057. }
  1058. return 0;
  1059. }
  1060. /*
  1061. Given a buffer and length, mangle away.
  1062. We *REALLY* want this to do something, so better run some tests.
  1063. Or, maybe we don't. This treats words consistently the same way.
  1064. HMM.
  1065. toupper, tolower, flipper
  1066. */
  1067. int word_mangler(char *buffer, int len) {
  1068. int p;
  1069. int count = 0;
  1070. int state;
  1071. state = word_state(buffer, len);
  1072. // ZF_LOGD("word_state(%.*s) %d", len, buffer, state);
  1073. // TODO: Transposer
  1074. for (p = 0; p < len; p++) {
  1075. char c = buffer[p];
  1076. switch (state) {
  1077. case -1:
  1078. // upper
  1079. if (islower(c)) {
  1080. count++;
  1081. buffer[p] = toupper(c);
  1082. }
  1083. break;
  1084. case 1:
  1085. // lower
  1086. if (isupper(c)) {
  1087. count++;
  1088. buffer[p] = tolower(c);
  1089. }
  1090. break;
  1091. case 0:
  1092. // flipper
  1093. if (islower(c)) {
  1094. count++;
  1095. buffer[p] = toupper(c);
  1096. } else {
  1097. if (isupper(c)) {
  1098. count++;
  1099. buffer[p] = tolower(c);
  1100. }
  1101. }
  1102. break;
  1103. }
  1104. }
  1105. return count;
  1106. }
  1107. /*
  1108. * The buffer that we've been given is much larger now.
  1109. *
  1110. */
  1111. int mangle(int fd, char *buffer) {
  1112. int x, i;
  1113. int need_render = 0; // changing word case around doesn't need the render
  1114. int mangled = 0;
  1115. int mangled_chars = 0;
  1116. char *cp;
  1117. // Ok, we want to look for words to:
  1118. // MaNGlE , or transpose (both?!)
  1119. // or possibly transpose words
  1120. char work[(BSIZE * 4) + 1];
  1121. // Use terminal - clean out ANSI
  1122. ZF_LOGI("mangle(%s)", repr(buffer));
  1123. // strcpy(work, buffer);
  1124. /*
  1125. NOTE: We copy the buffer, so we can clear out ANSI codes, etc.
  1126. Otherwise we might mess some ANSI up in the manglying
  1127. process.
  1128. */
  1129. /*
  1130. (random) Look for ANSI CLS and:
  1131. display random spooky texts around, with delays ... then CLS.
  1132. display ANSI graphic file, with delays ... then CLS
  1133. */
  1134. const char *ANSI_CLS = "\x1b[2J";
  1135. cp = strstr(buffer, ANSI_CLS);
  1136. if (cp != NULL) {
  1137. ZF_LOGI("seen: ANSI_CLS");
  1138. if (random_activate(4)) {
  1139. int r;
  1140. char display[100] = "";
  1141. const char *phrasing[] = {
  1142. "^R1Haha^P1ha^P1ha",
  1143. "Poof!",
  1144. "Got U",
  1145. "Anyone there?",
  1146. "^R1Knock, ^P1Knock"
  1147. };
  1148. // import magic
  1149. struct console_details temp_console;
  1150. // Make exact copy of our current console state.
  1151. memcpy(&temp_console, &console, sizeof(console));
  1152. // Play the buffer into the console
  1153. console_receive(&temp_console, buffer, cp - buffer);
  1154. char restore_color[30]; // ansi color
  1155. strcpy( restore_color, color_restore(&temp_console));
  1156. ZF_LOGI("mangle(ANSI_CLS)");
  1157. // sprintf( display, "^P2...");
  1158. // This string actually screws up ANSI detection (takes too long)
  1159. // strcpy(display, "^P2^S501234567890^P1abcdef^P2g^P3h^P4i^S0^P2");
  1160. // strcpy(display, "^P2^S301234^P15^S0^P2");
  1161. // Add in random text, plus color!
  1162. r = random() % ((sizeof(phrasing) / sizeof(char *)) - 1);
  1163. int color = random() % 15 + 1;
  1164. int x = random() % 30 + 1;
  1165. int y = random() % 15 + 1;
  1166. /*
  1167. Don't have it pause there before moving the cursor.
  1168. Move the cursor, get the color changed, THEN pause.
  1169. Then act all crazy.
  1170. NOTE: Make sure if you use any ^R Render effects, turn them off before
  1171. trying to display the restore_color. :P ^R0
  1172. Also, make sure you re-home the cursor ^G0101 because that's where they
  1173. are expecting the cursor to be! (At least it's how Mystic does it.)
  1174. HOME, CLS, HOME, ... Not sure what others do there. We'll see.
  1175. */
  1176. sprintf(display, "^G%02d%02d^S3^C%02d^P1%s^S0^R0%s^P1^G0101", x, y, color, phrasing[r], restore_color);
  1177. //sprintf(display, "^P1^S3^C%02d%s^S0^R0%s^P1", color, phrasing[r], restore_color);
  1178. ZF_LOGI("mangle(ANSI_CLS): Inserted (%02d) %s", color, phrasing[r]);
  1179. // Move the buffer so there's room for the display string.
  1180. if ( string_insert(buffer, BSIZE * 4, cp - buffer, display)) {
  1181. ZF_LOGI("mangle(ANSI_CLS): [%s]", repr(buffer));
  1182. need_render = 1;
  1183. } else {
  1184. ZF_LOGD("insert failed [%s].", repr(display));
  1185. }
  1186. // memmove(cp + strlen(display), cp, strlen(cp) + 1);
  1187. // strncpy(cp, display, strlen(display));
  1188. /*
  1189. Copy the new buffer over, but hide our "render" code
  1190. from the remaining mangler steps.
  1191. */
  1192. strcpy(work, buffer);
  1193. i = cp - buffer;
  1194. // find offset into "buffer"
  1195. // apply to work.
  1196. memset(work + i, ' ', strlen(display));
  1197. };
  1198. }
  1199. strcpy(work, buffer); // sure.
  1200. const char replace_with = ' ';
  1201. for (x = 0; x < strlen(buffer); x++) {
  1202. int ansi = console_char(&console, buffer[x]);
  1203. if (ansi) {
  1204. work[x] = replace_with;
  1205. }
  1206. }
  1207. ZF_LOGD("Work Now: [%s]", repr(work));
  1208. #ifdef OLD_WAY
  1209. /* work -- clear out ANSI so we don't mangle ANSI codes. */
  1210. x = rx_match(&ANSI, work);
  1211. if (x > 0) {
  1212. ZF_LOGD("found %d ANSI", x);
  1213. for (i = 0; i < x; i++) {
  1214. memset(work + rxmatch[i].rm_so, replace_with,
  1215. rxmatch[i].rm_eo - rxmatch[i].rm_so);
  1216. };
  1217. ZF_LOGD("Work Now: [%s]", repr(work));
  1218. }
  1219. #endif
  1220. // ZF_LOGI("mangle: %s", repr(work));
  1221. /*
  1222. (random) Locate words (in work), and possibly flip them around.
  1223. Transpose words. Transpose case. Transpose letters.
  1224. */
  1225. x = rx_match(&WORDS, work);
  1226. ZF_LOGD("found %d word groups", x);
  1227. if (x > 0) {
  1228. for (i = 0; i < x; i++) {
  1229. // Yes! Be random!
  1230. if (random_activate(8)) {
  1231. int c = word_mangler(buffer + rxmatch[i].rm_so,
  1232. rxmatch[i].rm_eo - rxmatch[i].rm_so);
  1233. if (c) {
  1234. mangled++;
  1235. mangled_chars += c;
  1236. }
  1237. }
  1238. // Do things here.
  1239. /*
  1240. // NO! This is predictable
  1241. if (i % 3 == 0) {
  1242. mangled++;
  1243. for (int p = rxmatch[i].rm_so; p < rxmatch[i].rm_eo; p++) {
  1244. buffer[p] = tolower(buffer[p]);
  1245. mangled_chars++;
  1246. }
  1247. } else {
  1248. if (i % 3 == 1) {
  1249. mangled++;
  1250. for (int p = rxmatch[i].rm_so; p < rxmatch[i].rm_eo; p++) {
  1251. buffer[p] = toupper(buffer[p]);
  1252. mangled_chars++;
  1253. }
  1254. }
  1255. }
  1256. */
  1257. }
  1258. }
  1259. /*
  1260. (random) Locate single words, and transpose words.
  1261. Transpose letters.
  1262. */
  1263. /*
  1264. (random) Display up to certain point. Delay.
  1265. Print some characters slowly. Delay.
  1266. */
  1267. if (need_render) {
  1268. ZF_LOGD("HH %d : (%d) %s", need_render, (int)strlen(buffer), repr(buffer));
  1269. } else {
  1270. if (mangled) {
  1271. ZF_LOGD("Mangled %d words, %d chars : %s", mangled, mangled_chars,
  1272. repr(buffer));
  1273. }
  1274. }
  1275. if (need_render) {
  1276. render(fd, buffer);
  1277. } else {
  1278. write(fd, buffer, strlen(buffer));
  1279. };
  1280. return need_render && mangled;
  1281. }
  1282. int harry_happens(time_t *last_event, int wakeup) {
  1283. time_t now = time(NULL);
  1284. int elapsed = now - *last_event;
  1285. if (elapsed > wakeup) {
  1286. // Ok! It's been too long since we've done something.
  1287. *last_event = now;
  1288. return 1;
  1289. }
  1290. return 0;
  1291. }
  1292. int main(int argc, char *argv[]) {
  1293. int master;
  1294. pid_t pid;
  1295. int node = -1;
  1296. file_output_open("horrible_harry.log");
  1297. init_harry();
  1298. srandom(time(NULL));
  1299. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown -Ubugz
  1300. // -PUWISHPASSWORD
  1301. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
  1302. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown -Ubugz
  1303. // -PUWISH
  1304. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
  1305. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
  1306. // ./mystic -TID9 -IP192.168.0.1 -HOSTUnknown -ML0 -SL1 -ST0 -CUnknown
  1307. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown -Ubugz
  1308. // -PDUMBWAYTODOTHIS
  1309. // ./mystic -TID9 -IP192.168.0.1 -HOSTUnknown -ML1 -SL1 -ST2 -CUnknown -Ubugz
  1310. // -PIDONTUSEPASCAL
  1311. // SSH: -ML1 -ST2
  1312. // Telnet: -ML0 -ST0
  1313. // Locate username (if given) in the command line
  1314. // -U<username>
  1315. for (int x = 0; x < argc; x++) {
  1316. if (strncmp("-U", argv[x], 2) == 0) {
  1317. username = argv[x] + 2;
  1318. ZF_LOGI("Username: [%s]", username);
  1319. };
  1320. if (strncmp("-SL", argv[x], 3) == 0) {
  1321. node = argv[x][3] - '0' + 1;
  1322. ZF_LOGI("Node: %d", node);
  1323. }
  1324. }
  1325. if (username != NULL) {
  1326. locate_user(username);
  1327. ZF_LOGD("Username: [%s] A.K.A. [%s]", username, fullname);
  1328. }
  1329. if (!init_regex())
  1330. return 2;
  1331. // With IGNBRK I don't think I need this anymore. (Nope!)
  1332. // signal(SIGINT, SIG_IGN);
  1333. pid = forkpty(&master, NULL, NULL, NULL);
  1334. // impossible to fork
  1335. if (pid < 0) {
  1336. return 1;
  1337. }
  1338. // child
  1339. else if (pid == 0) {
  1340. char *args[20]; // max 20 args
  1341. int x;
  1342. args[0] = TARGET;
  1343. for (x = 1; x < argc; x++) {
  1344. args[x] = argv[x];
  1345. };
  1346. args[x] = NULL;
  1347. // run Mystic, run!
  1348. execvp(TARGET, args);
  1349. }
  1350. // parent
  1351. else {
  1352. // remove the echo
  1353. // ICANON - raw mode. No more line buffering!
  1354. struct termios tios, orig1;
  1355. struct timeval timeout;
  1356. time_t last_event = 0; // time(NULL);
  1357. ZF_LOGD("starting");
  1358. tcgetattr(master, &tios);
  1359. tios.c_lflag &= ~(ECHO | ECHONL | ICANON);
  1360. tcsetattr(master, TCSAFLUSH, &tios);
  1361. tcgetattr(1, &orig1);
  1362. tios = orig1;
  1363. tios.c_iflag &= ~(ICRNL | IXON); // Disable software flow control
  1364. tios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  1365. // https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html
  1366. // ISIG should be Ctrl-C and Ctrl-Z IGNBRK +
  1367. tcsetattr(1, TCSAFLUSH, &tios);
  1368. for (;;) {
  1369. // define estruturas para o select, que serve para verificar qual
  1370. // se tornou "pronto pra uso"
  1371. fd_set read_fd;
  1372. fd_set write_fd;
  1373. fd_set except_fd;
  1374. // inicializa as estruturas
  1375. FD_ZERO(&read_fd);
  1376. FD_ZERO(&write_fd);
  1377. FD_ZERO(&except_fd);
  1378. // atribui o descritor master, obtido pelo forkpty, ao read_fd
  1379. FD_SET(master, &read_fd);
  1380. // atribui o stdin ao read_fd
  1381. FD_SET(STDIN_FILENO, &read_fd);
  1382. // o descritor tem que ser unico para o programa, a documentacao
  1383. // recomenda um calculo entre os descritores sendo usados + 1
  1384. /*
  1385. TODO: Figure out how this would work.
  1386. I'm thinking something like timeouts 30-50 seconds?
  1387. And as we get closer, 15-25 seconds.
  1388. */
  1389. // we're in luck! The last parameter is time interval/timeout. :D
  1390. timeout.tv_sec = 10;
  1391. timeout.tv_usec = 0;
  1392. // select(master+1, &read_fd, &write_fd, &except_fd, NULL);
  1393. if (select(master + 1, &read_fd, &write_fd, &except_fd, &timeout) == 0) {
  1394. // This means timeout!
  1395. ZF_LOGI("TIMEOUT");
  1396. harry_idle_event(STDOUT_FILENO);
  1397. }
  1398. char input[BSIZE + 1];
  1399. static char output[(BSIZE * 4) + 1];
  1400. int total;
  1401. // read_fd esta atribuido com read_fd?
  1402. if (FD_ISSET(master, &read_fd)) {
  1403. // leia o que bc esta mandando
  1404. if ((total = read(master, &output, BSIZE)) != -1) {
  1405. // e escreva isso na saida padrao
  1406. output[total] = 0;
  1407. // if ( harry_happens( &last_event, 5)) {
  1408. if (1) {
  1409. ZF_LOGI("harry_happens");
  1410. if (mangle(STDOUT_FILENO, output) == 0) {
  1411. // failed, so. Try again.
  1412. last_event = 0;
  1413. }
  1414. } else {
  1415. write(STDOUT_FILENO, &output, total);
  1416. // This is OUTPUT from the BBS
  1417. // ZF_LOGI( ">> %s", repr(output));
  1418. ZF_LOGI(">> %d chars", (int)strlen(output));
  1419. // I think repr is flipping out here. :(
  1420. // ZF_LOGI( ">> %d", (int)strlen(repr(output)));
  1421. }
  1422. } else
  1423. break;
  1424. }
  1425. // read_fd esta atribuido com a entrada padrao?
  1426. if (FD_ISSET(STDIN_FILENO, &read_fd)) {
  1427. // leia a entrada padrao
  1428. total = read(STDIN_FILENO, &input, BSIZE);
  1429. input[total] = 0;
  1430. // e escreva no bc
  1431. ZF_LOGI("<< %s", repr(input));
  1432. write(master, &input, total);
  1433. // This is INPUT from the USER
  1434. // ZF_LOGI_MEM( input, strlen(input), "<< ");
  1435. }
  1436. }
  1437. // Restore terminal
  1438. tcsetattr(1, TCSAFLUSH, &orig1);
  1439. ZF_LOGD("exit");
  1440. }
  1441. return 0;
  1442. }