mystic.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046
  1. #include <assert.h>
  2. #include <fcntl.h>
  3. #include <pty.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <termios.h>
  7. #include <unistd.h> // usleep()
  8. #include <sys/select.h>
  9. #include <sys/wait.h>
  10. // #include <signal.h> // handle Ctrl-C/SIGINT
  11. #include <strings.h> // strcasecmp
  12. #include <time.h> // usleep(), nanonsleep() ?
  13. #include <ctype.h>
  14. #include <stdlib.h> // random()
  15. #include <regex.h>
  16. /* Log level guideline:
  17. * - ZF_LOG_FATAL - happened something impossible and absolutely unexpected.
  18. * Process can't continue and must be terminated.
  19. * Example: division by zero, unexpected modifications from other thread.
  20. * - ZF_LOG_ERROR - happened something possible, but highly unexpected. The
  21. * process is able to recover and continue execution.
  22. * Example: out of memory (could also be FATAL if not handled properly).
  23. * - ZF_LOG_WARN - happened something that *usually* should not happen and
  24. * significantly changes application behavior for some period of time.
  25. * Example: configuration file not found, auth error.
  26. * - ZF_LOG_INFO - happened significant life cycle event or major state
  27. * transition.
  28. * Example: app started, user logged in.
  29. * - ZF_LOG_DEBUG - minimal set of events that could help to reconstruct the
  30. * execution path. Usually disabled in release builds.
  31. * - ZF_LOG_VERBOSE - all other events. Usually disabled in release builds.
  32. *
  33. * *Ideally*, log file of debugged, well tested, production ready application
  34. * should be empty or very small. Choosing a right log level is as important as
  35. * providing short and self descriptive log message.
  36. */
  37. /*
  38. #define ZF_LOG_VERBOSE 1
  39. #define ZF_LOG_DEBUG 2
  40. #define ZF_LOG_INFO 3
  41. #define ZF_LOG_WARN 4
  42. #define ZF_LOG_ERROR 5
  43. #define ZF_LOG_FATAL 6
  44. */
  45. // LOGGING with file output
  46. #include "zf_log.h"
  47. FILE *g_log_file;
  48. static void file_output_callback(const zf_log_message *msg, void *arg) {
  49. (void)arg;
  50. *msg->p = '\n';
  51. fwrite(msg->buf, msg->p - msg->buf + 1, 1, g_log_file);
  52. fflush(g_log_file);
  53. }
  54. static void file_output_close(void) { fclose(g_log_file); }
  55. static void file_output_open(const char *const log_path) {
  56. g_log_file = fopen(log_path, "a");
  57. if (!g_log_file) {
  58. ZF_LOGW("Failed to open log file %s", log_path);
  59. return;
  60. }
  61. atexit(file_output_close);
  62. zf_log_set_output_v(ZF_LOG_PUT_STD, 0, file_output_callback);
  63. }
  64. /**
  65. * Display a repr of the given string.
  66. *
  67. * This converts most \n\r\v\f\t codes,
  68. * defaults to \xHH (hex value).
  69. */
  70. const char *repr(const char *data) {
  71. static char buffer[4096];
  72. char *cp;
  73. strcpy(buffer, data);
  74. cp = buffer;
  75. while (*cp != 0) {
  76. char c = *cp;
  77. if (isspace(c)) {
  78. if (c == ' ') {
  79. cp++;
  80. continue;
  81. };
  82. /* Ok, it's form-feed ('\f'), newline ('\n'), carriage return ('\r'),
  83. * horizontal tab ('\t'), and vertical tab ('\v') */
  84. memmove(cp + 1, cp, strlen(cp) + 1);
  85. *cp = '\\';
  86. cp++;
  87. switch (c) {
  88. case '\f':
  89. *cp = 'f';
  90. cp++;
  91. break;
  92. case '\n':
  93. *cp = 'n';
  94. cp++;
  95. break;
  96. case '\r':
  97. *cp = 'r';
  98. cp++;
  99. break;
  100. case '\t':
  101. *cp = 't';
  102. cp++;
  103. break;
  104. case '\v':
  105. *cp = 'v';
  106. cp++;
  107. break;
  108. default:
  109. *cp = '?';
  110. cp++;
  111. break;
  112. }
  113. continue;
  114. }
  115. if (isprint(c)) {
  116. cp++;
  117. continue;
  118. };
  119. // Ok, default to \xHH output.
  120. memmove(cp + 3, cp, strlen(cp) + 1);
  121. *cp = '\\';
  122. cp++;
  123. *cp = 'x';
  124. cp++;
  125. char buffer[3];
  126. sprintf(buffer, "%02x", (int)c & 0xff);
  127. *cp = buffer[0];
  128. cp++;
  129. *cp = buffer[1];
  130. cp++;
  131. continue;
  132. }
  133. return buffer;
  134. }
  135. // END LOGGING
  136. #include "lastseen.h"
  137. // http://c-faq.com/lib/randrange.html
  138. int randint(int N) { return rand() / (RAND_MAX / N + 1); }
  139. // http://c-faq.com/lib/randrange.html
  140. // numbers in the range [M, N] could be generated with something like
  141. int randrange(int M, int N) {
  142. return M + rand() / (RAND_MAX / (N - M + 1) + 1);
  143. }
  144. /*
  145. * strnstr()
  146. *
  147. * buffer safe version that looks for a string.
  148. */
  149. const char *strnstr(const char *source, int len, const char *needle) {
  150. int pos;
  151. for (pos = 0; pos < len; pos++) {
  152. if (source[pos] == needle[0]) {
  153. if (strncmp(source + pos, needle, strlen(needle)) == 0) {
  154. return source + pos;
  155. }
  156. }
  157. }
  158. return NULL;
  159. }
  160. /*
  161. What is the name of the actual, real Mystic executable
  162. that we'll be executing and mangling?
  163. */
  164. #define TARGET "./mySTIC"
  165. // Size of our input and output buffers.
  166. #define BSIZE 1024
  167. /*
  168. * string_insert()
  169. * Inserts a string into a given position.
  170. * This safely checks to make sure the buffer isn't overrun.
  171. */
  172. int string_insert(char *buffer, int max_length, int pos, const char *insert) {
  173. assert(max_length > pos);
  174. assert(buffer != NULL);
  175. assert(insert != NULL);
  176. if (strlen(buffer) + strlen(insert) >= max_length) {
  177. ZF_LOGD("string_insert() failed inserting [%s]", repr(insert));
  178. return 0;
  179. }
  180. memmove(buffer + pos + strlen(insert), buffer + pos,
  181. strlen(buffer + pos) + 1);
  182. // cp + strlen(display), cp, strlen(cp) + 1);
  183. strncpy(buffer + pos, insert, strlen(insert));
  184. // (cp, display, strlen(display));
  185. return 1; // success
  186. }
  187. // Should this be passed around to the functions that use it?
  188. struct render {
  189. int speed;
  190. int effect;
  191. } current_render;
  192. int render_overlimit = 0;
  193. void reset_render(void) {
  194. current_render.speed = 0;
  195. current_render.effect = 0;
  196. render_overlimit = 0;
  197. }
  198. #define TRIGGER "^"
  199. // Max limit we'll sleep before ignoring effects/speed.
  200. #define SLEEP_LIMIT 30
  201. int ms_sleep(unsigned int ms) {
  202. int result = 0;
  203. struct timespec ts = {ms / 1000, (ms % 1000) * 1000000L};
  204. do {
  205. struct timespec ts_sleep = ts;
  206. result = nanosleep(&ts_sleep, &ts);
  207. } while ((-1 == result));
  208. return result;
  209. }
  210. void render_sleep(void) {
  211. if (render_overlimit)
  212. return;
  213. if (current_render.speed) { // * 100 still too slow.
  214. ms_sleep(current_render.speed * 10);
  215. }
  216. }
  217. /*
  218. Terminal tracking
  219. Actually, I believe I only really need to track the color information.
  220. Everything else, I'm not sure I really care about.
  221. */
  222. struct console_details {
  223. int posx, posy;
  224. int savedx, savedy;
  225. char ansi[20]; // current ANSI command being processed.
  226. int in_ansi;
  227. int fgcolor; // 0-7 // not 0-15
  228. int bgcolor; // 0-7
  229. int status; // 0, 1 or 5 (Blink)
  230. } console;
  231. void console_init(struct console_details *cdp) {
  232. cdp->posx = 0;
  233. cdp->posy = 0;
  234. cdp->savedx = 0;
  235. cdp->savedy = 0;
  236. cdp->in_ansi = 0;
  237. cdp->fgcolor = 7;
  238. cdp->bgcolor = 0;
  239. cdp->status = 0;
  240. }
  241. void ansi_color(struct console_details *cdp, int color) {
  242. // ZF_LOGV("ansi_color(%d)", color);
  243. if (color == 0) {
  244. cdp->status = 0;
  245. cdp->fgcolor = 7;
  246. cdp->bgcolor = 0;
  247. return;
  248. }
  249. if (color == 1) {
  250. cdp->status = 1;
  251. return;
  252. }
  253. if (color == 5) {
  254. cdp->status = 5;
  255. return;
  256. }
  257. if ((color >= 30) && (color <= 37)) {
  258. cdp->fgcolor = color - 30;
  259. return;
  260. }
  261. if ((color >= 40) && (color <= 47)) {
  262. cdp->bgcolor = color - 40;
  263. return;
  264. }
  265. ZF_LOGD("ansi_color( %d ) is unknown to me.", color);
  266. }
  267. const char *color_restore(struct console_details *cdp) {
  268. static char buffer[30];
  269. if (cdp->status == 0) {
  270. sprintf(buffer, "\x1b[0;3%d;4%dm", cdp->fgcolor, cdp->bgcolor);
  271. } else {
  272. sprintf(buffer, "\x1b[0;%d;3%d;4%dm", cdp->status, cdp->fgcolor,
  273. cdp->bgcolor);
  274. };
  275. return buffer;
  276. }
  277. void console_ansi(struct console_details *cdp, const char *ansi) {
  278. int understood = 0;
  279. const char *cp = ansi;
  280. const char *last = ansi + strlen(ansi) - 1;
  281. int number, number2;
  282. if (*cp == '\x1b') {
  283. cp++;
  284. // Ok, that's expected.
  285. if (*cp == '[') {
  286. cp++;
  287. // https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences
  288. switch (*last) {
  289. case 'A':
  290. // cursor up
  291. if (cp == last) {
  292. number = 1;
  293. } else {
  294. number = atoi(cp);
  295. if (number < 1) {
  296. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  297. number);
  298. number = 1;
  299. }
  300. };
  301. cdp->posy -= number;
  302. if (cdp->posy < 0) {
  303. cdp->posy = 0;
  304. ZF_LOGD(
  305. "console_ansi( %s ): attempt to move above top of screen (%d)",
  306. repr(ansi), number);
  307. }
  308. understood = 1;
  309. return;
  310. case 'B':
  311. // cursor down
  312. if (cp == last) {
  313. number = 1;
  314. } else {
  315. number = atoi(cp);
  316. if (number < 1) {
  317. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  318. number);
  319. number = 1;
  320. }
  321. };
  322. cdp->posy += number;
  323. // check range/"scroll"
  324. understood = 1;
  325. return;
  326. case 'C':
  327. // cursor forward
  328. if (cp == last) {
  329. number = 1;
  330. } else {
  331. number = atoi(cp);
  332. if (number < 1) {
  333. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  334. number);
  335. number = 1;
  336. }
  337. };
  338. cdp->posx += number;
  339. // Well. According to the "spec", the screen limits are hard
  340. // If the cursor is already at the edge of the screen, this has no
  341. // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
  342. while (cdp->posx > 79) {
  343. cdp->posy++;
  344. // check range/"scroll"
  345. cdp->posx -= 79;
  346. }
  347. understood = 1;
  348. return;
  349. case 'D':
  350. // cursor backwards
  351. if (cp == last) {
  352. number = 1;
  353. } else {
  354. number = atoi(cp);
  355. if (number < 1) {
  356. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  357. number);
  358. number = 0;
  359. }
  360. };
  361. cdp->posx -= number;
  362. // Well. According to the "spec", the screen limits are hard
  363. // If the cursor is already at the edge of the screen, this has no
  364. // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
  365. while (cdp->posx < 0) {
  366. cdp->posy--;
  367. if (cdp->posy < 0) {
  368. cdp->posy = 0;
  369. }
  370. cdp->posx += 79;
  371. }
  372. understood = 1;
  373. return;
  374. case 'H':
  375. // cursor position
  376. if (*cp == ';') {
  377. // Missing first number
  378. number = 1;
  379. cp++;
  380. if (cp == last) {
  381. // missing 2nd number as well?
  382. number2 = 1;
  383. } else {
  384. number2 = atoi(cp);
  385. }
  386. } else {
  387. // Ok, find the first number
  388. number = atoi(cp);
  389. cp = strchr(cp, ';');
  390. if (cp == NULL) {
  391. // Missing 2nd number
  392. number2 = 1;
  393. } else {
  394. // 2nd number found, maybe.
  395. cp++;
  396. if (cp == last) {
  397. number2 = 1;
  398. } else {
  399. number2 = atoi(cp);
  400. }
  401. }
  402. }
  403. // Our positions start at zero, not one.
  404. cdp->posx = number - 1;
  405. cdp->posy = number2 - 1;
  406. understood = 1;
  407. break;
  408. case 'J':
  409. // clear
  410. if (cp == last) {
  411. number = 0;
  412. } else {
  413. number = atoi(cp);
  414. };
  415. // clears ... part of the screen.
  416. if (number == 2) {
  417. cdp->posx = 0;
  418. cdp->posy = 0;
  419. };
  420. understood = 1;
  421. break;
  422. case 'm':
  423. // color
  424. if (cp == last) {
  425. // nothing given, default to 0.
  426. number = 0;
  427. ansi_color(cdp, number);
  428. } else {
  429. while (cp != last) {
  430. number = atoi(cp);
  431. ansi_color(cdp, number);
  432. cp++;
  433. while ((cp != last) && (isdigit(*cp))) {
  434. cp++;
  435. };
  436. if (cp != last) {
  437. if (*cp == ';') {
  438. cp++;
  439. }
  440. }
  441. }
  442. }
  443. understood = 1;
  444. break;
  445. case 't':
  446. case 'r':
  447. case 'h':
  448. case '!':
  449. // These are ones that I don't care about.
  450. case 'n': // This is terminal detect -- give me cursor position
  451. understood = 1;
  452. break;
  453. default:
  454. // unsure -- possibly not important
  455. ZF_LOGD("console_ansi( %s ): ???", repr(ansi));
  456. understood = 0;
  457. }
  458. }
  459. };
  460. if (!understood) {
  461. ZF_LOGD("console_ansi( %s ): was not understood.", repr(ansi));
  462. }
  463. }
  464. /*
  465. * console_char()
  466. * return whether or not we are still in_ansi
  467. */
  468. int console_char(struct console_details *cdp, char ch) {
  469. char *cp;
  470. if (cdp->in_ansi) {
  471. // Ok, append this char
  472. cp = cdp->ansi + strlen(cdp->ansi);
  473. *cp = ch;
  474. cp++;
  475. *cp = 0;
  476. if (isalpha(ch)) {
  477. // Ok!
  478. console_ansi(cdp, cdp->ansi);
  479. cdp->in_ansi = 0;
  480. cdp->ansi[0] = 0;
  481. return 1;
  482. }
  483. return 1;
  484. } else {
  485. if (ch == '\x1b') {
  486. cp = cdp->ansi;
  487. *cp = ch;
  488. cp++;
  489. *cp = 0;
  490. cdp->in_ansi = 1;
  491. return 1;
  492. }
  493. if (ch == '\r') {
  494. // Carriage return
  495. cdp->posx = 0;
  496. return 0;
  497. }
  498. if (ch == '\n') {
  499. cdp->posy++;
  500. // check range/"scroll"
  501. return 0;
  502. }
  503. if (ch == '\b') {
  504. // Backspace.
  505. if (cdp->posx > 0) {
  506. cdp->posx--;
  507. }
  508. return 0;
  509. }
  510. if (ch == '\f') {
  511. // form feed
  512. // treat as clear screen
  513. cdp->posx = 0;
  514. cdp->posy = 0;
  515. return 0;
  516. }
  517. /*
  518. I don't believe that anything else can possibly be here, other then an
  519. actual printable character. So!
  520. */
  521. cdp->posx++;
  522. if (cdp->posx > 79) {
  523. cdp->posx = 0;
  524. cdp->posy++;
  525. // check range/"scroll"
  526. }
  527. return 0;
  528. }
  529. }
  530. void console_string(struct console_details *cdp, const char *chars) {
  531. int x;
  532. for (x = 0; x < strlen(chars); x++) {
  533. console_char(cdp, chars[x]);
  534. }
  535. }
  536. void console_receive(struct console_details *cdp, const char *chars, int len) {
  537. int x;
  538. for (x = 0; x < len; x++) {
  539. console_char(cdp, chars[x]);
  540. }
  541. }
  542. /*
  543. Well SNAP! Mystic numbers don't remotely match ANSI color codes.
  544. 00 : Sets the current foreground to Black 0;30
  545. 01 : Sets the current foreground to Dark Blue 0;34
  546. 02 : Sets the current foreground to Dark Green 0;32
  547. 03 : Sets the current foreground to Dark Cyan 0;36
  548. 04 : Sets the current foreground to Dark Red 0;31
  549. 05 : Sets the current foreground to Dark Magenta 0;35
  550. 06 : Sets the current foreground to Brown 0;33
  551. 07 : Sets the current foreground to Grey 0;37
  552. 08 : Sets the current foreground to Dark Grey 1;30
  553. 09 : Sets the current foreground to Light Blue 1;34
  554. 10 : Sets the current foreground to Light Green 1;32
  555. 11 : Sets the current foreground to Light Cyan 1;36
  556. 12 : Sets the current foreground to Light Red 1;31
  557. 13 : Sets the current foreground to Light Magenta 1;35
  558. 14 : Sets the current foreground to Yellow 1;33
  559. 15 : Sets the current foreground to White 1;37
  560. 16 : Sets the current background to Black 40
  561. 17 : Sets the current background to Blue 44
  562. 18 : Sets the current background to Green 42
  563. 19 : Sets the current background to Cyan 46
  564. 20 : Sets the current background to Red 41
  565. 21 : Sets the current background to Magenta 45
  566. 22 : Sets the current background to Brown 43
  567. 23 : Sets the current background to Grey 47
  568. 24 : Sets the current background to black with blinking foreground 5;40
  569. 25 : Sets the current background to blue with blinking foreground 5;44
  570. 26 : Sets the current background to green with blinking foreground 5;42
  571. 27 : Sets the current background to cyan with blinking foreground 5;46
  572. 28 : Sets the current background to red with blinking foreground 5;41
  573. 29 : Sets the current background to magenta with blinking foreground 5;45
  574. 30 : Sets the current background to brown with blinking foreground 5;43
  575. 31 : Sets the current background to grey with blinking foreground 5;47
  576. Other things that Mystic does ...
  577. [A## - Move the cursor up ## lines
  578. [B## - Move the cursor down ## lines
  579. [C## - Move the cursor forward (to the right) ## columns
  580. [D## - Move the cursor backwards (to the left) ## columns
  581. [K - Clear from the current cursor position to the end of the line
  582. [L - Move cursor and erase data backwards from current column to column ##
  583. [X## - Move cursor to X coordinate ##
  584. [Y## - Move cursor to Y coordinate ##
  585. BS - Sends 1 destructive backspace sequence (ASCII 8-32-8)
  586. CL - Clears the screen (ANSI 1,1 locate and [2J or ASCII 12)
  587. CR - Send a carrage return and line feed (move to next line)
  588. RA - Restore the saved text attribute color
  589. RS - Restore the saved user's terminal screen
  590. SA - Save the current text attribute color
  591. SS - Save the entire user's terminal screen
  592. */
  593. // Covert MYSTIC color to (Proper) ANSI COLOR.
  594. const int MYSTIC[] = {0, 4, 2, 6, 1, 5, 3, 7};
  595. // ANSI_color = MYSTIC[ odd_mystic_color % 8 ]
  596. void write_color(int fd, int color) {
  597. char buffer[12];
  598. switch (color) {
  599. case 0:
  600. case 1:
  601. case 2:
  602. case 3:
  603. case 4:
  604. case 5:
  605. case 6:
  606. case 7:
  607. sprintf(buffer, "\x1b[0;3%dm", MYSTIC[color]);
  608. break;
  609. case 8:
  610. case 9:
  611. case 10:
  612. case 11:
  613. case 12:
  614. case 13:
  615. case 14:
  616. case 15:
  617. sprintf(buffer, "\x1b[0;1;3%dm", MYSTIC[color - 8]);
  618. break;
  619. case 16:
  620. case 17:
  621. case 18:
  622. case 19:
  623. case 20:
  624. case 21:
  625. case 22:
  626. case 23:
  627. sprintf(buffer, "\x1b[4%dm", MYSTIC[color - 16]);
  628. break;
  629. case 24:
  630. case 25:
  631. case 26:
  632. case 27:
  633. case 28:
  634. case 29:
  635. case 30:
  636. case 31:
  637. sprintf(buffer, "\x1b[5;4%dm", MYSTIC[color - 24]);
  638. break;
  639. default:
  640. buffer[0] = 0;
  641. break;
  642. }
  643. ZF_LOGD("write_color( %d ): %s", color, repr(buffer));
  644. write(fd, buffer, strlen(buffer));
  645. }
  646. int send_file(int fd, char *filename) {
  647. FILE *fp;
  648. char buffer[100];
  649. int read;
  650. fp = fopen(filename, "rb");
  651. if (fp == NULL) {
  652. ZF_LOGD("Failed to open %s", filename);
  653. return 0;
  654. }
  655. while ((read = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
  656. write(fd, buffer, read);
  657. };
  658. fclose(fp);
  659. return 1;
  660. }
  661. void send_goto(int fd, int x, int y) {
  662. char gbuffer[16];
  663. sprintf(gbuffer, "\x1b[%d;%dH", y, x);
  664. write(fd, gbuffer, strlen(gbuffer));
  665. }
  666. int send_file(int fd, int x, int y, char *filename) {
  667. FILE *fp;
  668. char buffer[100];
  669. int read;
  670. fp = fopen(filename, "rb");
  671. if (fp == NULL) {
  672. ZF_LOGD("Failed to open %s", filename);
  673. return 0;
  674. }
  675. send_goto(fd, x, y);
  676. y++;
  677. while ((read = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
  678. char *cp, *last_cp;
  679. buffer[read] = 0;
  680. last_cp = buffer;
  681. while ((cp = strchr(last_cp, '\n')) != NULL) {
  682. *cp = 0;
  683. write(fd, last_cp, strlen(last_cp));
  684. send_goto(fd, x, y);
  685. y++;
  686. last_cp = cp + 1;
  687. };
  688. write(fd, last_cp, strlen(last_cp));
  689. };
  690. fclose(fp);
  691. return 1;
  692. }
  693. /**
  694. * process_trigger( fd, *cp )
  695. *
  696. * This process a command trigger.
  697. * It has seen TRIGGER, and now it is
  698. * processing whatever comes after it.
  699. * It will perform the process, and
  700. * return the char * of whatever is next.
  701. */
  702. const char *process_trigger(int fd, const char *cp) {
  703. char ch;
  704. int i, x, y;
  705. ch = *cp;
  706. cp++;
  707. switch (ch) {
  708. case 'D':
  709. i = 0;
  710. if (isdigit(*cp)) {
  711. i = (*cp) - '0';
  712. cp++;
  713. };
  714. if (isdigit(*cp)) {
  715. i *= 10;
  716. i += (*cp) - '0';
  717. cp++;
  718. };
  719. if ((i > 0) && (i < 80)) {
  720. ZF_LOGI("DEL %02d", i);
  721. for (x = 0; x < i; x++) {
  722. write(fd, "\b \b", 3);
  723. }
  724. };
  725. break;
  726. case 'C': {
  727. i = 0;
  728. if (*cp == 'R') {
  729. cp++;
  730. const char *restore = color_restore(&console);
  731. write(fd, restore, strlen(restore));
  732. break;
  733. }
  734. if (isdigit(*cp)) {
  735. i = (*cp) - '0';
  736. cp++;
  737. };
  738. if (isdigit(*cp)) {
  739. i *= 10;
  740. i += (*cp) - '0';
  741. cp++;
  742. };
  743. write_color(fd, i);
  744. } break;
  745. case 'F':
  746. case 'f': {
  747. int pos = 0;
  748. if (ch == 'f') {
  749. pos = 1;
  750. x = (*cp) - '0';
  751. cp++;
  752. x *= 10;
  753. x += (*cp) - '0';
  754. cp++;
  755. y = (*cp) - '0';
  756. cp++;
  757. y *= 10;
  758. y += (*cp) - '0';
  759. cp++;
  760. ZF_LOGI("file at (%d, %d)", x, y);
  761. }
  762. // Ok, look for filename
  763. char ansifile[32] = "./hh/";
  764. char *ap = ansifile + strlen(ansifile);
  765. while (*cp != '.') {
  766. *ap = *cp;
  767. ap++;
  768. *ap = 0;
  769. cp++;
  770. };
  771. strcat(ansifile, ".ans");
  772. cp++;
  773. ZF_LOGD("FILE [%s]", ansifile);
  774. if (pos) {
  775. send_file(fd, x, y, ansifile);
  776. } else {
  777. send_file(fd, ansifile);
  778. };
  779. } break;
  780. case 'G': {
  781. x = 0;
  782. if (isdigit(*cp)) {
  783. x = (*cp) - '0';
  784. cp++;
  785. };
  786. if (isdigit(*cp)) {
  787. x *= 10;
  788. x += (*cp) - '0';
  789. cp++;
  790. };
  791. y = 0;
  792. if (isdigit(*cp)) {
  793. y = (*cp) - '0';
  794. cp++;
  795. };
  796. if (isdigit(*cp)) {
  797. y *= 10;
  798. y += (*cp) - '0';
  799. cp++;
  800. };
  801. char buffer[20]; // row ; column H
  802. ZF_LOGD("GOTO (%d,%d)", x, y);
  803. sprintf(buffer, "\x1b[%d;%dH", y, x);
  804. write(fd, buffer, strlen(buffer));
  805. } break;
  806. case 'R': {
  807. i = 0;
  808. if (isdigit(*cp)) {
  809. i = (*cp) - '0';
  810. cp++;
  811. };
  812. if ((i > 0) && (i < 10)) {
  813. ZF_LOGI("RENDER %d", i);
  814. current_render.effect = i;
  815. } else {
  816. current_render.effect = 0;
  817. }
  818. } break;
  819. case 'S': {
  820. i = 0;
  821. if (isdigit(*cp)) {
  822. i = (*cp) - '0';
  823. cp++;
  824. };
  825. if ((i > 0) && (i < 10)) {
  826. ZF_LOGI("SPEED %d", i);
  827. current_render.speed = i;
  828. } else {
  829. current_render.speed = 0;
  830. }
  831. } break;
  832. case 'P': {
  833. i = 0;
  834. if (isdigit(*cp)) {
  835. i = (*cp) - '0';
  836. cp++;
  837. };
  838. if ((i > 0) && (i < 10)) {
  839. ZF_LOGI("PAWS %d", i);
  840. // sleep(i);
  841. if (!render_overlimit) {
  842. sleep(i);
  843. };
  844. }
  845. } break;
  846. }
  847. return cp;
  848. }
  849. /**
  850. * render_effect( fd, ch )
  851. *
  852. * Displays the given character with whatever
  853. * rendering effect is currently active.
  854. * (If any).
  855. */
  856. void render_effect(int fd, char ch) {
  857. int effect = current_render.effect;
  858. int l;
  859. char space = ' ';
  860. char bs = '\b';
  861. switch (effect) {
  862. case 1:
  863. // CHAR + SPC + BS
  864. render_sleep();
  865. write(fd, &ch, 1);
  866. render_sleep();
  867. write(fd, &space, 1);
  868. render_sleep();
  869. render_sleep();
  870. write(fd, &bs, 1);
  871. break;
  872. case 2:
  873. // CHAR + 8 spaces + 8 BS
  874. render_sleep();
  875. write(fd, &ch, 1);
  876. for (l = 0; l < 8; l++) {
  877. render_sleep();
  878. write(fd, &space, 1);
  879. }
  880. for (l = 0; l < 8; l++) {
  881. render_sleep();
  882. write(fd, &bs, 1);
  883. }
  884. break;
  885. case 0:
  886. default:
  887. // NORMAL
  888. render_sleep();
  889. write(fd, &ch, 1);
  890. break;
  891. }
  892. }
  893. /**
  894. * render( fd, string_out )
  895. *
  896. * Render an entire string.
  897. * Handles TRIGGER.
  898. * Renders with effects.
  899. */
  900. void render(int fd, const char *string_out, int len) {
  901. const char *cp = string_out;
  902. const char *trigger = cp;
  903. time_t start = time(NULL);
  904. int elapsed;
  905. int over = 0;
  906. reset_render();
  907. // ZF_LOGV("render(%d, %s)", fd, repr(string_out));
  908. ZF_LOGV_MEM(string_out, len, "render(%d, %d bytes):", fd, len);
  909. // Check our time from time to time.
  910. // If we start running long, disable sleeps.
  911. while ((trigger = strnstr(cp, len - (cp - trigger), TRIGGER)) != NULL) {
  912. // There is special things to handle in here.
  913. while (cp != trigger) {
  914. elapsed = time(NULL) - start;
  915. if (elapsed > SLEEP_LIMIT) {
  916. render_overlimit = 1;
  917. current_render.speed = 0;
  918. };
  919. // write(fd, cp, 1 );
  920. render_effect(fd, *cp);
  921. cp++;
  922. };
  923. // ZF_LOGI( "at trigger: (%s)", cp);
  924. cp += strlen(TRIGGER);
  925. // Ok, we're pointing at the trigger -- do something.
  926. cp = process_trigger(fd, cp);
  927. // ZF_LOGI( "after trigger: (%s)", cp);
  928. };
  929. // We still might be under a rendering effect.
  930. while (cp < (string_out + len)) {
  931. elapsed = time(NULL) - start;
  932. if (elapsed > SLEEP_LIMIT) {
  933. render_overlimit = 1;
  934. current_render.speed = 0;
  935. };
  936. // write(fd, cp, 1);
  937. render_effect(fd, *cp);
  938. cp++;
  939. }
  940. }
  941. /*
  942. These are harry "timeout" events.
  943. These happen when we've been sitting around awhile.
  944. */
  945. #ifdef CPP_MADMAN_STL_CODE
  946. const char *random_phrase(const char *words, int len, int last_seen) {
  947. // ooh. a map of char *s to last_seen_events. :P
  948. static map<const char *, array<int>> tracker;
  949. map<const char *, array<int>>::iterator it;
  950. array<int, last_seen> it = tracker.find(words);
  951. if (it == tracker.end()) {
  952. // key does not exist.
  953. array<int, last_seen> last;
  954. for (int i = 0; i < last_seen; i++) {
  955. last[i] = -1;
  956. };
  957. tracker.insert(words, last);
  958. it = tracker.find(words);
  959. };
  960. }
  961. #endif
  962. void harry_idle_event(int fd) {
  963. // Make something happen
  964. char buffer[100];
  965. int r;
  966. // This is no where near finished, BUT!
  967. const char *phrases[] = {"Hahaha", "Snicker, snicker", "Boo!",
  968. "MeOW", "I see U", "Arrooo!",
  969. "Ahh-wooo!", "Aaaooo!"};
  970. const char *cp;
  971. static LastSeen last_seen_harry_event(2);
  972. // Remember the last phrase used,
  973. // and don't repeat (the last two)!
  974. do {
  975. r = randint((sizeof(phrases) / sizeof(char *)));
  976. // r = random() % ((sizeof(phrases) / sizeof(char *)) - 1);
  977. } while (last_seen_harry_event.seen_before(r));
  978. // ZF_LOGD("%d => %d %d", r, last_seen_harry_event[0],
  979. // last_seen_harry_event[1]);
  980. cp = phrases[r];
  981. int color = random() % 15 + 1;
  982. /*
  983. int color = random() % 16;
  984. if (color == 0) {
  985. color++;
  986. } // If it's 0 let's make it 1. // color = (random() % 15) + 1
  987. */
  988. sprintf(buffer, "^S2^C%02d%s^P2^CR^D%02d", color, cp, (int)strlen(cp));
  989. ZF_LOGD("harry_event: render(%d, \"%s\")", fd, buffer);
  990. render(fd, buffer, strlen(buffer));
  991. }
  992. void init_harry() {
  993. // init_have_seen(last_seen_harry_event, MAX_HARRY_EVENT_DUPS);
  994. // ZF_LOGD("init => %d %d", last_seen_harry_event[0],
  995. // last_seen_harry_event[1]);
  996. console_init(&console);
  997. }
  998. /*
  999. The code to get the username and fullname is useless on telnet
  1000. connections.
  1001. */
  1002. const char *username = NULL;
  1003. const char *fullname = NULL;
  1004. /*
  1005. Pascal String Copy. Copy from pascal string, to C String.
  1006. First char is pascal string length. (Max 255).
  1007. */
  1008. void pcopy(char *pstring, char *str) {
  1009. int len = (int)*pstring;
  1010. strncpy(str, pstring + 1, len);
  1011. str[len] = 0;
  1012. }
  1013. /*
  1014. This only works for those few idiots that use the
  1015. horribly broken SSH crap that Mystic uses.
  1016. */
  1017. int locate_user(const char *alias) {
  1018. FILE *user;
  1019. char buffer[0x600];
  1020. char temp[100];
  1021. user = fopen("data/users.dat", "rb");
  1022. if (user == NULL)
  1023. return 0;
  1024. // Carry on!
  1025. while (fread(buffer, 0x600, 1, user) == 1) {
  1026. pcopy(buffer + 0x6d, temp);
  1027. if (strcasecmp(temp, username) == 0) {
  1028. pcopy(buffer + 0x8c, temp);
  1029. fullname = strdup(temp);
  1030. break;
  1031. }
  1032. /*
  1033. printf("Alias: %s\n", temp);
  1034. pcopy(buffer + 0x8c, temp );
  1035. printf("Full Name: %s\n", temp );
  1036. */
  1037. }
  1038. fclose(user);
  1039. return 1;
  1040. }
  1041. // Buffers are BSIZE + 1, so a buffer that size can strcpy safely.
  1042. regex_t ANSI;
  1043. regex_t WORDS;
  1044. regex_t WORD;
  1045. int init_regex(void) {
  1046. int ret;
  1047. char ansi[] = "\x1b\[[0-9]+(;[0-9]+)*?[a-zA-Z]";
  1048. char words[] = "[a-zA-Z]+( [a-zA-Z]+)+";
  1049. char word[] = "[a-zA-Z]+";
  1050. char errorbuf[100];
  1051. if (ret = regcomp(&ANSI, ansi, REG_EXTENDED | REG_NEWLINE)) {
  1052. regerror(ret, &ANSI, errorbuf, sizeof(errorbuf));
  1053. ZF_LOGW("Regex %s failed to compile: %s", ansi, errorbuf);
  1054. return 0;
  1055. };
  1056. if (ret = regcomp(&WORDS, words, REG_EXTENDED | REG_NEWLINE)) {
  1057. regerror(ret, &WORDS, errorbuf, sizeof(errorbuf));
  1058. ZF_LOGW("Regex %s failed to compile: %s", words, errorbuf);
  1059. return 0;
  1060. };
  1061. if (ret = regcomp(&WORD, word, REG_EXTENDED | REG_NEWLINE)) {
  1062. regerror(ret, &WORD, errorbuf, sizeof(errorbuf));
  1063. ZF_LOGW("Regex %s failed to compile: %s", word, errorbuf);
  1064. return 0;
  1065. };
  1066. return 1;
  1067. }
  1068. int regmatch(regex_t *preg, const char *string, size_t nmatch,
  1069. regmatch_t pmatch[], int eflags) {
  1070. // returns number of matches found. (Max nmatch)
  1071. int matches = 0;
  1072. int offset = 0;
  1073. while (matches < nmatch) {
  1074. int ret = regexec(preg, string + offset, nmatch - matches, pmatch + matches,
  1075. eflags);
  1076. if (!ret) {
  1077. int current = offset;
  1078. offset += pmatch[matches].rm_eo;
  1079. pmatch[matches].rm_so += current;
  1080. pmatch[matches].rm_eo += current;
  1081. matches++;
  1082. } else if (ret == REG_NOMATCH) {
  1083. break;
  1084. } else {
  1085. break;
  1086. }
  1087. }
  1088. return matches;
  1089. }
  1090. #define MAX_MATCH 32
  1091. regmatch_t rxmatch[MAX_MATCH];
  1092. int rx_match(regex_t *regex, const char *buffer) {
  1093. int ret;
  1094. ret = regmatch(regex, buffer, MAX_MATCH, rxmatch, 0);
  1095. if (0) {
  1096. for (int i = 0; i < ret; i++) {
  1097. ZF_LOGI("%d : (%d-%d)", i, rxmatch[i].rm_so, rxmatch[i].rm_eo);
  1098. }
  1099. }
  1100. return ret;
  1101. }
  1102. /*
  1103. Terminal processing section.
  1104. Monitor lines, position, color.
  1105. What screen size do I want to emulate?
  1106. ANSI codes.
  1107. Do I need this??
  1108. */
  1109. /**
  1110. * random_activate()
  1111. *
  1112. * Is a weight (1-10),
  1113. * tests if random number is < weight * 10.
  1114. *
  1115. * So random_activate(9) happens more frequently
  1116. * then random_activate(8) or lower.
  1117. *
  1118. * This probably needs to be fixed.
  1119. * We need a better randint(RANGE) code.
  1120. */
  1121. int random_activate(int w) {
  1122. int r = randint(100);
  1123. if (r <= (w * 10)) {
  1124. return 1;
  1125. };
  1126. return 0;
  1127. }
  1128. /*
  1129. word_state():
  1130. -1 only lower
  1131. +1 only upper
  1132. 0 mixed
  1133. */
  1134. int word_state(const char *buffer, int len) {
  1135. int p;
  1136. int upper = 0;
  1137. int lower = 0;
  1138. int ret;
  1139. float pct;
  1140. for (p = 0; p < len; p++) {
  1141. char c = buffer[p];
  1142. if (isalpha(c)) {
  1143. if (isupper(c)) {
  1144. upper++;
  1145. };
  1146. if (islower(c)) {
  1147. lower++;
  1148. };
  1149. }
  1150. }
  1151. if (upper == lower) {
  1152. return 0;
  1153. }
  1154. if (upper > lower) {
  1155. ret = 1;
  1156. pct = ((float)lower / (float)upper) * 100.0;
  1157. } else {
  1158. ret = -1;
  1159. pct = ((float)upper / (float)lower) * 100.0;
  1160. }
  1161. // ZF_LOGD("So far %d with %f %%", ret, pct);
  1162. if (pct < 40.0) {
  1163. return ret;
  1164. }
  1165. return 0;
  1166. }
  1167. /*
  1168. Given a buffer and length, mangle away.
  1169. We *REALLY* want this to do something, so better run some tests.
  1170. Or, maybe we don't. This treats words consistently the same way.
  1171. HMM.
  1172. toupper, tolower, flipper
  1173. */
  1174. int word_mangler(char *buffer, int len) {
  1175. int p;
  1176. int count = 0;
  1177. int state;
  1178. // state = word_state(buffer, len);
  1179. // ZF_LOGD("word_state(%.*s) %d", len, buffer, state);
  1180. state = randrange(-1, 1);
  1181. // TODO: Transposer
  1182. for (p = 0; p < len; p++) {
  1183. char c = buffer[p];
  1184. if (randint(len) == p) {
  1185. break;
  1186. }
  1187. switch (state) {
  1188. case -1:
  1189. // upper
  1190. if (islower(c)) {
  1191. count++;
  1192. buffer[p] = toupper(c);
  1193. }
  1194. break;
  1195. case 1:
  1196. // lower
  1197. if (isupper(c)) {
  1198. count++;
  1199. buffer[p] = tolower(c);
  1200. }
  1201. break;
  1202. case 0:
  1203. // flipper
  1204. if (islower(c)) {
  1205. count++;
  1206. buffer[p] = toupper(c);
  1207. } else {
  1208. if (isupper(c)) {
  1209. count++;
  1210. buffer[p] = tolower(c);
  1211. }
  1212. }
  1213. break;
  1214. }
  1215. }
  1216. return count;
  1217. }
  1218. int word_wrangler(char *buffer, int len) {
  1219. int p;
  1220. int count;
  1221. int state;
  1222. // state = word_state(buffer, len);
  1223. // ZF_LOGD("word_state(%.*s) %d", len, buffer, state);
  1224. if (len < 5) {
  1225. return 0;
  1226. }
  1227. p = randint(len - 4) + 2;
  1228. for (count = 0; count < 4; count++) {
  1229. if (!isalpha(buffer[p + count]))
  1230. break;
  1231. }
  1232. ZF_LOGV_MEM(buffer, len, "wrangler %d len %d:", p, count);
  1233. if (count >= 2) {
  1234. for (int x = 0; x < count / 2; x++) {
  1235. char ch = buffer[p + x];
  1236. buffer[p + x] = buffer[p + count - 1 - x];
  1237. buffer[p + count - 1 - x] = ch;
  1238. }
  1239. ZF_LOGV_MEM(buffer, len, "word now:");
  1240. return 1;
  1241. } else
  1242. return 0;
  1243. }
  1244. int buffer_insert(char *buffer, int len, int max_length, int pos,
  1245. const char *insert) {
  1246. if (len + strlen(insert) > max_length) {
  1247. ZF_LOGD("buffer_insert failed [%s]", repr(insert));
  1248. return 0;
  1249. }
  1250. memmove(buffer + pos + strlen(insert), buffer + pos, len - pos);
  1251. strncpy(buffer + pos, insert, strlen(insert));
  1252. return 1;
  1253. }
  1254. /*
  1255. * The buffer that we've been given is much larger now.
  1256. *
  1257. * We can no longer mangle or insert into the given buffer.
  1258. * Why? Because we don't know what is behind it now!
  1259. */
  1260. int mangle(int fd, const char *buffer, int len) {
  1261. int x, i;
  1262. int need_render = 0; // changing word case around doesn't need the render
  1263. int mangled = 0;
  1264. int mangled_chars = 0;
  1265. char play[BSIZE * 2];
  1266. const char *cp;
  1267. // Make a copy of buffer, since it can no longer be changed
  1268. // inserted into.
  1269. memcpy(play, buffer, len);
  1270. // NEVER reference buffer from this point on!
  1271. char work[BSIZE * 2];
  1272. // Use terminal - clean out ANSI
  1273. // ZF_LOGI("mangle:");
  1274. ZF_LOGI_MEM(play, len, "Mangle (%u bytes):", len);
  1275. // strcpy(work, buffer);
  1276. /*
  1277. NOTE: We copy the buffer, so we can clear out ANSI codes, etc.
  1278. Otherwise we might mess some ANSI up in the manglying
  1279. process.
  1280. */
  1281. /*
  1282. (random) Look for ANSI CLS and:
  1283. display random spooky texts around, with delays ... then CLS.
  1284. display ANSI graphic file, with delays ... then CLS
  1285. */
  1286. const char *ANSI_CLS = "\x1b[2J";
  1287. cp = strnstr(play, len, ANSI_CLS); // strstr(buffer, ANSI_CLS);
  1288. if (cp != NULL) {
  1289. static int ANSI_CLS_count = 0; // count the number we've seen
  1290. ZF_LOGI("seen: ANSI_CLS");
  1291. ANSI_CLS_count++;
  1292. // Don't activate on the very first CLS. Too soon, don't screw up the ANSI
  1293. // detection.
  1294. if (ANSI_CLS_count > 1) {
  1295. // Ok, figure out the restore color, just in case
  1296. struct console_details temp_console;
  1297. // Make exact copy of our current console state.
  1298. memcpy(&temp_console, &console, sizeof(console));
  1299. // Play the buffer into the console
  1300. console_receive(&temp_console, play, cp - play);
  1301. char restore_color[30]; // ansi color
  1302. strcpy(restore_color, color_restore(&temp_console));
  1303. if (random_activate(3)) {
  1304. char display[100] = "";
  1305. int needs_cls = 0;
  1306. struct file_need {
  1307. const char *filename;
  1308. int cls;
  1309. int rand_pos;
  1310. } possibles[] = {
  1311. {"goofy_head", 1, 0}, {"bat", 1, 0}, {"panther", 1, 0},
  1312. {"wolf", 1, 0}, {"skull", 0, 1}, {"skull2", 0, 1},
  1313. {"guy", 0, 1}, {"blinkman", 0, 1}, {"ghost", 1, 0}};
  1314. static LastSeen last_files(2);
  1315. int r;
  1316. do {
  1317. r = randint((sizeof(possibles) / sizeof(file_need)));
  1318. } while (last_files.seen_before(r));
  1319. int x, y;
  1320. x = randint(50) + 1;
  1321. y = randint(12) + 1;
  1322. char fgoto[10];
  1323. if (possibles[r].rand_pos) {
  1324. sprintf(fgoto, "^f%02d%02d", x, y);
  1325. } else {
  1326. strcpy(fgoto, "^F");
  1327. }
  1328. // (2); // (sizeof(possibles) / sizeof(file_need)) - 1);
  1329. needs_cls = possibles[r].cls;
  1330. // I get what's happening. Mystic moves cursor to home, CLS, cursor
  1331. // home. When we get here, we're ALWAYS at the top of the screen...
  1332. // Hence our bat isn't displayed at the end of the screen.
  1333. // This is before the actual CLS, so we CLS before displaying our files.
  1334. // I tried a ^P2 before doing this .. but I'd rather have the picture up
  1335. // right away I think.
  1336. sprintf(display, "%s%s%s.^P3%s", needs_cls ? "\x1b[2J" : "", fgoto,
  1337. possibles[r].filename, restore_color);
  1338. ZF_LOGI("mangle(ANSI_CLS): %d file inserted %s", r, repr(display));
  1339. // Move the buffer so there's room for the display string.
  1340. if (buffer_insert(play, len, sizeof(play), cp - play, display)) {
  1341. len += strlen(display);
  1342. // if (string_insert(buffer, 1024, cp - buffer, display)) {
  1343. ZF_LOGI_MEM(play, len, "mangle(ANSI_CLS) (%u bytes):", len);
  1344. // ZF_LOGI("mangle(ANSI_CLS):");
  1345. // ZF_LOGI_REPR(buffer);
  1346. // ZF_LOGI("mangle(ANSI_CLS): [%s]", repr(buffer));
  1347. need_render = 1;
  1348. /*
  1349. Copy the new buffer over, but hide our "render" code
  1350. from the remaining mangler steps.
  1351. */
  1352. memcpy(work, play, len);
  1353. // strcpy(work, buffer);
  1354. i = cp - play;
  1355. // find offset into "buffer"
  1356. // apply to work.
  1357. memset(work + i, ' ', strlen(display));
  1358. } else {
  1359. ZF_LOGD("insert failed [%s].", repr(display));
  1360. }
  1361. } else {
  1362. if (random_activate(4)) {
  1363. int r;
  1364. char display[100] = "";
  1365. const char *phrasing[] = {"^R1Haha^P1ha^P1ha", "Poof!", "Got U",
  1366. "Anyone there?", "^R1Knock, ^P1Knock"};
  1367. static LastSeen last_phrasing(2);
  1368. ZF_LOGI("mangle(ANSI_CLS)");
  1369. // sprintf( display, "^P2...");
  1370. // This string actually screws up ANSI detection (takes too long)
  1371. // strcpy(display, "^P2^S501234567890^P1abcdef^P2g^P3h^P4i^S0^P2");
  1372. // strcpy(display, "^P2^S301234^P15^S0^P2");
  1373. // Add in random text, plus color!
  1374. do {
  1375. r = random() % ((sizeof(phrasing) / sizeof(char *)) - 1);
  1376. } while (last_phrasing.seen_before(r));
  1377. int color = random() % 15 + 1;
  1378. int x = random() % 30 + 1;
  1379. int y = random() % 15 + 1;
  1380. /*
  1381. Don't have it pause there before moving the cursor.
  1382. Move the cursor, get the color changed, THEN pause.
  1383. Then act all crazy.
  1384. NOTE: Make sure if you use any ^R Render effects, turn them off
  1385. before trying to display the restore_color. :P ^R0 Also, make
  1386. sure you re-home the cursor ^G0101 because that's where they are
  1387. expecting the cursor to be! (At least it's how Mystic does it.)
  1388. HOME, CLS, HOME, ... Not sure what others do there. We'll see.
  1389. */
  1390. sprintf(display, "^G%02d%02d^S3^C%02d^P1%s^S0^R0%s^P1^G0101", x, y,
  1391. color, phrasing[r], restore_color);
  1392. // sprintf(display, "^P1^S3^C%02d%s^S0^R0%s^P1", color, phrasing[r],
  1393. // restore_color);
  1394. // ZF_LOGI("mangle(ANSI_CLS): Inserted (%02d) %s", color,
  1395. // phrasing[r]);
  1396. ZF_LOGI_MEM(play, len, "mangle(ANSI_CLS) :");
  1397. // Move the buffer so there's room for the display string.
  1398. if (buffer_insert(play, len, sizeof(play), cp - play, display)) {
  1399. len += strlen(display);
  1400. // if (string_insert(buffer, BSIZE * 4, cp - buffer, display)) {
  1401. ZF_LOGI_MEM(play, len, "mangle(ANSI_CLS) + :");
  1402. // ZF_LOGI("mangle(ANSI_CLS):");
  1403. // ZF_LOGI_REPR(buffer);
  1404. need_render = 1;
  1405. /*
  1406. Copy the new buffer over, but hide our "render" code
  1407. from the remaining mangler steps.
  1408. */
  1409. memcpy(work, play, len);
  1410. // strcpy(work, buffer);
  1411. i = cp - play;
  1412. // find offset into "buffer"
  1413. // apply to work.
  1414. memset(work + i, ' ', strlen(display));
  1415. } else {
  1416. ZF_LOGD("insert failed [%s].", repr(display));
  1417. }
  1418. }
  1419. }
  1420. }
  1421. }
  1422. memcpy(work, play, len);
  1423. // strcpy(work, buffer); // sure.
  1424. const char replace_with = ' ';
  1425. for (x = 0; x < len; x++) {
  1426. int ansi = console_char(&console, play[x]);
  1427. if (ansi) {
  1428. work[x] = replace_with;
  1429. }
  1430. // fixup "work" so it's a valid C string
  1431. if (buffer[x] == 0) {
  1432. work[x] = replace_with;
  1433. }
  1434. }
  1435. // fixup "work" buffer so it's a valid c string
  1436. // (required for regex to work.)
  1437. work[len] = 0;
  1438. ZF_LOGI_MEM(work, len, "Work now:");
  1439. /*
  1440. (random) Locate words (in work), and possibly flip them around.
  1441. Transpose words. Transpose case. Transpose letters.
  1442. Ok, what would be interesting, is if we could find
  1443. W\x1[0;34mORDS with color changes in them, and work with them.
  1444. without screwing up the color changes, of course. :P
  1445. */
  1446. x = rx_match(&WORDS, work);
  1447. ZF_LOGD("found %d word groups", x);
  1448. if (x > 0) {
  1449. for (i = 0; i < x; i++) {
  1450. // Yes! Be random!
  1451. if (random_activate(8)) {
  1452. int c = word_mangler(play + rxmatch[i].rm_so,
  1453. rxmatch[i].rm_eo - rxmatch[i].rm_so);
  1454. if (c) {
  1455. mangled++;
  1456. mangled_chars += c;
  1457. }
  1458. }
  1459. if (random_activate(4)) {
  1460. word_wrangler(play + rxmatch[i].rm_so,
  1461. rxmatch[i].rm_eo - rxmatch[i].rm_so);
  1462. }
  1463. }
  1464. }
  1465. /*
  1466. (random) Locate single words, and transpose words.
  1467. Transpose letters.
  1468. */
  1469. /*
  1470. (random) Display up to certain point. Delay.
  1471. Print some characters slowly. Delay.
  1472. */
  1473. if (need_render) {
  1474. ZF_LOGD_MEM(play, len, "Ready to render:");
  1475. // ZF_LOGD("HH %d : (%d) %s", need_render, (int)strlen(buffer),
  1476. // repr(buffer));
  1477. } else {
  1478. if (mangled) {
  1479. ZF_LOGD_MEM(play, len, "Mangled %d words, %d chars:", mangled,
  1480. mangled_chars);
  1481. /* ZF_LOGD("Mangled %d words, %d chars : %s", mangled, mangled_chars,
  1482. repr(buffer)); */
  1483. }
  1484. }
  1485. if (need_render) {
  1486. render(fd, play, len);
  1487. } else {
  1488. write(fd, play, len); // strlen(buffer));
  1489. };
  1490. return need_render && mangled;
  1491. }
  1492. int harry_happens(time_t *last_event, int wakeup) {
  1493. time_t now = time(NULL);
  1494. int elapsed = now - *last_event;
  1495. if (elapsed > wakeup) {
  1496. // Ok! It's been too long since we've done something.
  1497. *last_event = now;
  1498. return 1;
  1499. }
  1500. return 0;
  1501. }
  1502. /*
  1503. TO FIX: Stop using c strings, must use char * buffer + int length.
  1504. MAY CONTAIN NULL VALUES.
  1505. Rework some things here.
  1506. Here's the "plan":
  1507. if buffer is EMPTY:
  1508. time_idle = 1;
  1509. // setup for "random timeout value mess"
  1510. // we're in luck! The last parameter is time interval/timeout. :D
  1511. timeout.tv_sec = 10; // randrange(10-25)
  1512. timeout.tv_usec = 0;
  1513. NOT EMPTY:
  1514. // we're in luck! The last parameter is time interval/timeout. :D
  1515. timeout.tv_sec = 0;
  1516. timeout.tv_usec = 10; // Wild Guess Here? Maybe higher, maybe lower?
  1517. time_idle = 0;
  1518. ON READ:
  1519. read/append to current buffer.
  1520. We can't use nulls -- what if they are using ZModem, there's nulls in the
  1521. file! Look for trailing / the very last "\r\n".
  1522. (I could mangle/chunk it line by line. But I'm not sure I'd need to do
  1523. that.)
  1524. Optional "mangle" buffer up to that very point -- and send up to that point.
  1525. Option #2: Maybe we send everything if program has been running for under
  1526. 20 seconds. This would allow the ANSI detect to not get screwed up by this
  1527. new idea.
  1528. ON TIMEOUT:
  1529. if time_idle:
  1530. Activate funny harry timeout events.
  1531. else:
  1532. Ok, we *STILL* haven't received any more characters into the buffer --
  1533. even after waiting. (Maybe we haven't waited long enough?)
  1534. send the pending information in the buffer and clear it out.
  1535. Maybe this is a prompt, and there won't be a \r\n.
  1536. This allows for cleaner process of "lines" of buffer. We shouldn't break
  1537. in the midDLE OF A WORD. Downside is that we sit on buffer contents a little
  1538. while / some amount of time -- which will add some lag to prompts showing up.
  1539. ZModem:
  1540. start: "rz^M**"...
  1541. 05-12 18:12:15.916 >> rz^M**^XB00000000000000^M<8A>^Q
  1542. 05-12 18:12:15.928 << **\x18B0100000023be50\r\n\x11
  1543. 05-12 18:12:15.928 >> *^XC^D
  1544. 05-12 18:12:15.939 << **\x18B0900000000a87c\r\n\x11
  1545. 05-12 18:12:15.940 >> *^XC
  1546. # Start of PK zipfile.
  1547. 05-12 18:12:15.941 >> PK^C^D^T
  1548. end:
  1549. 05-12 18:26:38.700 << **\x18B0100000023be50\r\n\x11
  1550. 05-12 18:26:38.700 >> **^XB0823a77600344c^M<8A>
  1551. 05-12 18:26:38.711 << **\x18B0800000000022d\r\n
  1552. 05-12 18:26:38.712 >> OO^MESC[0m
  1553. */
  1554. /*
  1555. * rstrnstr() Reverse string find in a string
  1556. *
  1557. * This obeys the len, and handles nulls in buffer.
  1558. * find is a c-string (null terminated)
  1559. */
  1560. int rstrnstr(const char *buffer, int len, const char *find) {
  1561. int flen = strlen(find);
  1562. if (len < flen) {
  1563. // I can't find a string in a buffer smaller then it is!
  1564. return -1;
  1565. }
  1566. int pos = len - flen;
  1567. while (pos > 0) {
  1568. if (buffer[pos] == find[0]) {
  1569. // First chars match, check them all.
  1570. if (strncmp(buffer + pos, find, flen) == 0) {
  1571. return pos;
  1572. }
  1573. }
  1574. pos--;
  1575. }
  1576. return -1;
  1577. }
  1578. int main(int argc, char *argv[]) {
  1579. int master;
  1580. pid_t pid;
  1581. int node = -1;
  1582. file_output_open("horrible_harry.log");
  1583. init_harry();
  1584. srandom(time(NULL));
  1585. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown
  1586. // -Ubugz -PUWISHPASSWORD
  1587. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
  1588. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown
  1589. // -Ubugz -PUWISH
  1590. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
  1591. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
  1592. // ./mystic -TID9 -IP192.168.0.1 -HOSTUnknown -ML0 -SL1 -ST0 -CUnknown
  1593. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown
  1594. // -Ubugz -PDUMBWAYTODOTHIS
  1595. // ./mystic -TID9 -IP192.168.0.1 -HOSTUnknown -ML1 -SL1 -ST2 -CUnknown
  1596. // -Ubugz -PIDONTUSEPASCAL
  1597. // SSH: -ML1 -ST2
  1598. // Telnet: -ML0 -ST0
  1599. // Locate username (if given) in the command line
  1600. // -U<username>
  1601. for (int x = 0; x < argc; x++) {
  1602. if (strncmp("-U", argv[x], 2) == 0) {
  1603. username = argv[x] + 2;
  1604. ZF_LOGI("Username: [%s]", username);
  1605. };
  1606. if (strncmp("-SL", argv[x], 3) == 0) {
  1607. node = argv[x][3] - '0' + 1;
  1608. ZF_LOGI("Node: %d", node);
  1609. }
  1610. }
  1611. if (username != NULL) {
  1612. locate_user(username);
  1613. ZF_LOGD("Username: [%s] A.K.A. [%s]", username, fullname);
  1614. }
  1615. if (!init_regex())
  1616. return 2;
  1617. pid = forkpty(&master, NULL, NULL, NULL);
  1618. // impossible to fork
  1619. if (pid < 0) {
  1620. return 1;
  1621. }
  1622. // child
  1623. else if (pid == 0) {
  1624. char *args[20]; // max 20 args
  1625. int x;
  1626. char new_exec[] = TARGET;
  1627. args[0] = new_exec;
  1628. for (x = 1; x < argc; x++) {
  1629. args[x] = argv[x];
  1630. };
  1631. args[x] = NULL;
  1632. // run Mystic, run!
  1633. execvp(TARGET, args);
  1634. }
  1635. // parent
  1636. else {
  1637. struct termios tios, orig1;
  1638. struct timeval timeout;
  1639. time_t last_event = 0; // time(NULL);
  1640. ZF_LOGD("starting");
  1641. tcgetattr(master, &tios);
  1642. tios.c_lflag &= ~(ECHO | ECHONL | ICANON);
  1643. /*
  1644. tios.c_iflag &= ~(ICRNL | IXON | BRKINT);
  1645. tios.c_iflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  1646. tios.c_oflag &= ~(OPOST);
  1647. */
  1648. tcsetattr(master, TCSAFLUSH, &tios);
  1649. tcgetattr(1, &orig1);
  1650. tios = orig1;
  1651. tios.c_iflag &= ~(ICRNL | IXON | BRKINT);
  1652. tios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  1653. tios.c_oflag &= ~(OPOST);
  1654. // https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html
  1655. tcsetattr(1, TCSAFLUSH, &tios);
  1656. char buffer[BSIZE + 1];
  1657. int size = 0;
  1658. for (;;) {
  1659. int time_idle;
  1660. // define estruturas para o select, que serve para verificar qual
  1661. // se tornou "pronto pra uso"
  1662. fd_set read_fd;
  1663. fd_set write_fd;
  1664. fd_set except_fd;
  1665. // inicializa as estruturas
  1666. FD_ZERO(&read_fd);
  1667. FD_ZERO(&write_fd);
  1668. FD_ZERO(&except_fd);
  1669. // atribui o descritor master, obtido pelo forkpty, ao read_fd
  1670. FD_SET(master, &read_fd);
  1671. // atribui o stdin ao read_fd
  1672. FD_SET(STDIN_FILENO, &read_fd);
  1673. // o descritor tem que ser unico para o programa, a documentacao
  1674. // recomenda um calculo entre os descritores sendo usados + 1
  1675. /*
  1676. TODO: Figure out how this would work.
  1677. I'm thinking something like timeouts 30-50 seconds?
  1678. And as we get closer, 15-25 seconds.
  1679. */
  1680. if (size == 0) {
  1681. // buffer is empty
  1682. timeout.tv_sec = randrange(10, 20);
  1683. timeout.tv_usec = 0;
  1684. time_idle = 1;
  1685. } else {
  1686. // buffer is not empty
  1687. timeout.tv_sec = 0;
  1688. timeout.tv_usec = 1;
  1689. time_idle = 0;
  1690. }
  1691. if (select(master + 1, &read_fd, &write_fd, &except_fd, &timeout) == 0) {
  1692. ZF_LOGI("TIMEOUT");
  1693. // This means timeout!
  1694. if (time_idle) {
  1695. harry_idle_event(STDOUT_FILENO);
  1696. } else {
  1697. ZF_LOGV_MEM(buffer, size, "TIMEOUT buffer size=%d", size);
  1698. console_receive(&console, buffer, size);
  1699. write(STDOUT_FILENO, buffer, size);
  1700. size = 0;
  1701. // buffer is empty now
  1702. }
  1703. }
  1704. /*
  1705. static char output[(BSIZE * 4) + 1];
  1706. */
  1707. int total;
  1708. // read_fd esta atribuido com read_fd?
  1709. if (FD_ISSET(master, &read_fd)) {
  1710. // leia o que bc esta mandando
  1711. // ZF_LOGD("read (%d) %d bytes", size, BSIZE - size);
  1712. if ((total = read(master, buffer + size, BSIZE - size)) != -1) {
  1713. // Ok, we've read more into the buffer.
  1714. ZF_LOGV_MEM(buffer + size, total, "Read %d bytes:", total);
  1715. size += total;
  1716. ZF_LOGV_MEM(buffer, size, "Buffer now:");
  1717. int pos = rstrnstr(buffer, size, "\r\n");
  1718. if (pos >= 0) {
  1719. // found something!
  1720. pos += 2;
  1721. ZF_LOGD_MEM(buffer, pos, "mangle buffer %d bytes:", pos);
  1722. mangle(STDOUT_FILENO, buffer, pos);
  1723. memmove(buffer, buffer + pos, size - pos);
  1724. size -= pos;
  1725. } else {
  1726. ZF_LOGV("position of /r/n not found.");
  1727. }
  1728. } else
  1729. break;
  1730. }
  1731. // read_fd esta atribuido com a entrada padrao?
  1732. if (FD_ISSET(STDIN_FILENO, &read_fd)) {
  1733. // leia a entrada padrao
  1734. char input[BSIZE];
  1735. int r = read(STDIN_FILENO, &input, BSIZE);
  1736. input[r] = 0;
  1737. // e escreva no bc
  1738. ZF_LOGI("<< %s", repr(input));
  1739. write(master, &input, r);
  1740. // This is INPUT from the USER
  1741. // ZF_LOGI_MEM( input, strlen(input), "<< ");
  1742. }
  1743. }
  1744. // Restore terminal
  1745. tcsetattr(1, TCSAFLUSH, &orig1);
  1746. ZF_LOGD("exit");
  1747. }
  1748. return 0;
  1749. }