1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687 |
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h> // usleep()
- #include <fcntl.h>
- #include <pty.h>
- #include <termios.h>
- #include <sys/select.h>
- #include <sys/wait.h>
- // #include <signal.h> // handle Ctrl-C/SIGINT
- #include <strings.h> // strcasecmp
- #include <time.h> // usleep(), nanonsleep() ?
- #include <ctype.h>
- #include <stdlib.h> // random()
- #include <regex.h>
- // LOGGING with file output
- #include "zf_log.h"
- #include <stdio.h>
- #include <stdlib.h>
- FILE *g_log_file;
- static void file_output_callback(const zf_log_message *msg, void *arg) {
- (void)arg;
- *msg->p = '\n';
- fwrite(msg->buf, msg->p - msg->buf + 1, 1, g_log_file);
- fflush(g_log_file);
- }
- static void file_output_close(void) { fclose(g_log_file); }
- static void file_output_open(const char *const log_path) {
- g_log_file = fopen(log_path, "a");
- if (!g_log_file) {
- ZF_LOGW("Failed to open log file %s", log_path);
- return;
- }
- atexit(file_output_close);
- zf_log_set_output_v(ZF_LOG_PUT_STD, 0, file_output_callback);
- }
- // END LOGGING
- /*
- What is the name of the actual, real Mystic executable
- that we'll be executing and mangling?
- */
- #define TARGET "./mySTIC"
- // Size of our input and output buffers.
- #define BSIZE 128
- /**
- * Display a repr of the given string.
- *
- * This converts most \n\r\v\f\t codes,
- * defaults to \xHH (hex value).
- */
- const char *repr(const char *data) {
- static char buffer[4096];
- char *cp;
- strcpy(buffer, data);
- cp = buffer;
- while (*cp != 0) {
- char c = *cp;
- if (isspace(c)) {
- if (c == ' ') {
- cp++;
- continue;
- };
- /* Ok, it's form-feed ('\f'), newline ('\n'), carriage return ('\r'),
- * horizontal tab ('\t'), and vertical tab ('\v') */
- memmove(cp + 1, cp, strlen(cp) + 1);
- *cp = '\\';
- cp++;
- switch (c) {
- case '\f':
- *cp = 'f';
- cp++;
- break;
- case '\n':
- *cp = 'n';
- cp++;
- break;
- case '\r':
- *cp = 'r';
- cp++;
- break;
- case '\t':
- *cp = 't';
- cp++;
- break;
- case '\v':
- *cp = 'v';
- cp++;
- break;
- default:
- *cp = '?';
- cp++;
- break;
- }
- continue;
- }
- if (isprint(c)) {
- cp++;
- continue;
- };
- // Ok, default to \xHH output.
- memmove(cp + 3, cp, strlen(cp) + 1);
- *cp = '\\';
- cp++;
- *cp = 'x';
- cp++;
- char buffer[3];
- sprintf(buffer, "%02x", (int)c & 0xff);
- *cp = buffer[0];
- cp++;
- *cp = buffer[1];
- cp++;
- continue;
- }
- return buffer;
- }
- /*
- * string_insert()
- * Inserts a string into a given position.
- * This safely checks to make sure the buffer isn't overrun.
- */
- int string_insert( char * buffer, int max_length, int pos, const char * insert ) {
- if (strlen(buffer) + strlen(insert) >= max_length) {
- ZF_LOGD("string_insert() failed inserting [%s]", repr(insert));
- return 0;
- }
- memmove(buffer + pos + strlen(insert), buffer + pos, strlen(buffer + pos) + 1 );
- // cp + strlen(display), cp, strlen(cp) + 1);
- strncpy(buffer+pos, insert, strlen(insert));
- // (cp, display, strlen(display));
- return 1; // success
- }
- // Should this be passed around to the functions that use it?
- struct render {
- int speed;
- int effect;
- } current_render;
- int render_overlimit = 0;
- void reset_render(void) {
- current_render.speed = 0;
- current_render.effect = 0;
- render_overlimit = 0;
- }
- #define TRIGGER "^"
- // Max limit we'll sleep before ignoring effects/speed.
- #define SLEEP_LIMIT 30
- int ms_sleep(unsigned int ms) {
- int result = 0;
- struct timespec ts = {ms / 1000, (ms % 1000) * 1000000L};
- do {
- struct timespec ts_sleep = ts;
- result = nanosleep(&ts_sleep, &ts);
- } while ((-1 == result));
- return result;
- }
- void render_sleep(void) {
- if (render_overlimit)
- return;
- if (current_render.speed) { // * 100 still too slow.
- ms_sleep(current_render.speed * 10);
- }
- }
- /*
- Terminal tracking
- Actually, I believe I only really need to track the color information.
- Everything else, I'm not sure I really care about.
- */
- struct console_details {
- int posx, posy;
- int savedx, savedy;
- char ansi[20]; // current ANSI command being processed.
- int in_ansi;
- int fgcolor; // 0-7 // not 0-15
- int bgcolor; // 0-7
- int status; // 0, 1 or 5 (Blink)
- } console;
- void console_init(struct console_details *cdp) {
- cdp->posx = 0;
- cdp->posy = 0;
- cdp->savedx = 0;
- cdp->savedy = 0;
- cdp->in_ansi = 0;
- cdp->fgcolor = 7;
- cdp->bgcolor = 0;
- cdp->status = 0;
- }
- void ansi_color(struct console_details *cdp, int color) {
- ZF_LOGD("ansi_color(%d)", color);
- if (color == 0) {
- cdp->status = 0;
- cdp->fgcolor = 7;
- cdp->bgcolor = 0;
- return;
- }
- if (color == 1) {
- cdp->status = 1;
- return;
- }
- if (color == 5) {
- cdp->status = 5;
- return;
- }
- if ((color >= 30) && (color <= 37)) {
- cdp->fgcolor = color - 30;
- return;
- }
- if ((color >= 40) && (color <= 47)) {
- cdp->bgcolor = color - 40;
- return;
- }
- ZF_LOGD("ansi_color( %d ) is unknown to me.", color);
- }
- const char *color_restore(struct console_details *cdp) {
- static char buffer[30];
- if (cdp->status == 0) {
- sprintf(buffer, "\x1b[0;3%d;4%dm", cdp->fgcolor, cdp->bgcolor);
- } else {
- sprintf(buffer, "\x1b[0;%d;3%d;4%dm", cdp->status, cdp->fgcolor,
- cdp->bgcolor);
- };
- return buffer;
- }
- void console_ansi(struct console_details *cdp, const char *ansi) {
- int understood = 0;
- const char *cp = ansi;
- const char *last = ansi + strlen(ansi) - 1;
- int number, number2;
- if (*cp == '\x1b') {
- cp++;
- // Ok, that's expected.
- if (*cp == '[') {
- cp++;
- // https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences
- switch (*last) {
- case 'A':
- // cursor up
- if (cp == last) {
- number = 1;
- } else {
- number = atoi(cp);
- if (number < 1) {
- ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
- number);
- number = 1;
- }
- };
- cdp->posy -= number;
- if (cdp->posy < 0) {
- cdp->posy = 0;
- ZF_LOGD(
- "console_ansi( %s ): attempt to move above top of screen (%d)",
- repr(ansi), number);
- }
- understood = 1;
- return;
- case 'B':
- // cursor down
- if (cp == last) {
- number = 1;
- } else {
- number = atoi(cp);
- if (number < 1) {
- ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
- number);
- number = 1;
- }
- };
- cdp->posy += number;
- // check range/"scroll"
- understood = 1;
- return;
- case 'C':
- // cursor forward
- if (cp == last) {
- number = 1;
- } else {
- number = atoi(cp);
- if (number < 1) {
- ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
- number);
- number = 1;
- }
- };
- cdp->posx += number;
- // Well. According to the "spec", the screen limits are hard
- // If the cursor is already at the edge of the screen, this has no
- // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
- while (cdp->posx > 79) {
- cdp->posy++;
- // check range/"scroll"
- cdp->posx -= 79;
- }
- understood = 1;
- return;
- case 'D':
- // cursor backwards
- if (cp == last) {
- number = 1;
- } else {
- number = atoi(cp);
- if (number < 1) {
- ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
- number);
- number = 0;
- }
- };
- cdp->posx -= number;
- // Well. According to the "spec", the screen limits are hard
- // If the cursor is already at the edge of the screen, this has no
- // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
- while (cdp->posx < 0) {
- cdp->posy--;
- if (cdp->posy < 0) {
- cdp->posy = 0;
- }
- cdp->posx += 79;
- }
- understood = 1;
- return;
- case 'H':
- // cursor position
- if (*cp == ';') {
- // Missing first number
- number = 1;
- cp++;
- if (cp == last) {
- // missing 2nd number as well?
- number2 = 1;
- } else {
- number2 = atoi(cp);
- }
- } else {
- // Ok, find the first number
- number = atoi(cp);
- cp = strchr(cp, ';');
- if (cp == NULL) {
- // Missing 2nd number
- number2 = 1;
- } else {
- // 2nd number found, maybe.
- cp++;
- if (cp == last) {
- number2 = 1;
- } else {
- number2 = atoi(cp);
- }
- }
- }
- // Our positions start at zero, not one.
- cdp->posx = number - 1;
- cdp->posy = number2 - 1;
- understood = 1;
- break;
- case 'J':
- // clear
- if (cp == last) {
- number = 0;
- } else {
- number = atoi(cp);
- };
- // clears ... part of the screen.
- if (number == 2) {
- cdp->posx = 0;
- cdp->posy = 0;
- };
- understood = 1;
- break;
- case 'm':
- // color
- if (cp == last) {
- // nothing given, default to 0.
- number = 0;
- ansi_color(cdp, number);
- } else {
- while (cp != last) {
- number = atoi(cp);
- ansi_color(cdp, number);
- cp++;
- while ((cp != last) && (isdigit(*cp))) {
- cp++;
- };
- if (cp != last) {
- if (*cp == ';') {
- cp++;
- }
- }
- }
- }
- understood = 1;
- break;
- case 't':
- case 'r':
- case 'h':
- case '!':
- // These are ones that I don't care about.
- case 'n': // This is terminal detect -- give me cursor position
- understood = 1;
- break;
- default:
- // unsure -- possibly not important
- ZF_LOGD("console_ansi( %s ): ???", repr(ansi));
- understood = 0;
- }
- }
- };
- if (!understood) {
- ZF_LOGD("console_ansi( %s ): was not understood.", repr(ansi));
- }
- }
- /*
- * console_char()
- * return whether or not we are still in_ansi
- */
- int console_char(struct console_details *cdp, char ch) {
- char *cp;
- if (cdp->in_ansi) {
- // Ok, append this char
- cp = cdp->ansi + strlen(cdp->ansi);
- *cp = ch;
- cp++;
- *cp = 0;
- if (isalpha(ch)) {
- // Ok!
- console_ansi(cdp, cdp->ansi);
- cdp->in_ansi = 0;
- cdp->ansi[0] = 0;
- return 1;
- }
- return 1;
- } else {
- if (ch == '\x1b') {
- cp = cdp->ansi;
- *cp = ch;
- cp++;
- *cp = 0;
- cdp->in_ansi = 1;
- return 1;
- }
- if (ch == '\r') {
- // Carriage return
- cdp->posx = 0;
- return 0;
- }
- if (ch == '\n') {
- cdp->posy++;
- // check range/"scroll"
- return 0;
- }
- if (ch == '\b') {
- // Backspace.
- if (cdp->posx > 0) {
- cdp->posx--;
- }
- return 0;
- }
- if (ch == '\f') {
- // form feed
- // treat as clear screen
- cdp->posx = 0;
- cdp->posy = 0;
- return 0;
- }
- /*
- I don't believe that anything else can possibly be here, other then an
- actual printable character. So!
- */
- cdp->posx++;
- if (cdp->posx > 79) {
- cdp->posx = 0;
- cdp->posy++;
- // check range/"scroll"
- }
- return 0;
- }
- }
- void console_string(struct console_details *cdp, const char *chars) {
- int x;
- for (x = 0; x < strlen(chars); x++) {
- console_char(cdp, chars[x]);
- }
- }
- void console_receive(struct console_details *cdp, const char *chars, int len) {
- int x;
- for (x = 0; x < len; x++) {
- console_char(cdp, chars[x]);
- }
- }
- /*
- Well SNAP! Mystic numbers don't remotely match ANSI color codes.
- 00 : Sets the current foreground to Black 0;30
- 01 : Sets the current foreground to Dark Blue 0;34
- 02 : Sets the current foreground to Dark Green 0;32
- 03 : Sets the current foreground to Dark Cyan 0;36
- 04 : Sets the current foreground to Dark Red 0;31
- 05 : Sets the current foreground to Dark Magenta 0;35
- 06 : Sets the current foreground to Brown 0;33
- 07 : Sets the current foreground to Grey 0;37
- 08 : Sets the current foreground to Dark Grey 1;30
- 09 : Sets the current foreground to Light Blue 1;34
- 10 : Sets the current foreground to Light Green 1;32
- 11 : Sets the current foreground to Light Cyan 1;36
- 12 : Sets the current foreground to Light Red 1;31
- 13 : Sets the current foreground to Light Magenta 1;35
- 14 : Sets the current foreground to Yellow 1;33
- 15 : Sets the current foreground to White 1;37
- 16 : Sets the current background to Black 40
- 17 : Sets the current background to Blue 44
- 18 : Sets the current background to Green 42
- 19 : Sets the current background to Cyan 46
- 20 : Sets the current background to Red 41
- 21 : Sets the current background to Magenta 45
- 22 : Sets the current background to Brown 43
- 23 : Sets the current background to Grey 47
- 24 : Sets the current background to black with blinking foreground 5;40
- 25 : Sets the current background to blue with blinking foreground 5;44
- 26 : Sets the current background to green with blinking foreground 5;42
- 27 : Sets the current background to cyan with blinking foreground 5;46
- 28 : Sets the current background to red with blinking foreground 5;41
- 29 : Sets the current background to magenta with blinking foreground 5;45
- 30 : Sets the current background to brown with blinking foreground 5;43
- 31 : Sets the current background to grey with blinking foreground 5;47
- Other things that Mystic does ...
- [A## - Move the cursor up ## lines
- [B## - Move the cursor down ## lines
- [C## - Move the cursor forward (to the right) ## columns
- [D## - Move the cursor backwards (to the left) ## columns
- [K - Clear from the current cursor position to the end of the line
- [L - Move cursor and erase data backwards from current column to column ##
- [X## - Move cursor to X coordinate ##
- [Y## - Move cursor to Y coordinate ##
- BS - Sends 1 destructive backspace sequence (ASCII 8-32-8)
- CL - Clears the screen (ANSI 1,1 locate and [2J or ASCII 12)
- CR - Send a carrage return and line feed (move to next line)
- RA - Restore the saved text attribute color
- RS - Restore the saved user's terminal screen
- SA - Save the current text attribute color
- SS - Save the entire user's terminal screen
- */
- // Covert MYSTIC color to (Proper) ANSI COLOR.
- const int MYSTIC[] = {0, 4, 2, 6, 1, 5, 3, 7};
- // ANSI_color = MYSTIC[ odd_mystic_color % 8 ]
- void write_color(int fd, int color) {
- char buffer[12];
- switch (color) {
- case 0:
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- case 7:
- sprintf(buffer, "\x1b[0;3%dm", MYSTIC[color]);
- break;
- case 8:
- case 9:
- case 10:
- case 11:
- case 12:
- case 13:
- case 14:
- case 15:
- sprintf(buffer, "\x1b[0;1;3%dm", MYSTIC[color - 8]);
- break;
- case 16:
- case 17:
- case 18:
- case 19:
- case 20:
- case 21:
- case 22:
- case 23:
- sprintf(buffer, "\x1b[4%dm", MYSTIC[color - 16]);
- break;
- case 24:
- case 25:
- case 26:
- case 27:
- case 28:
- case 29:
- case 30:
- case 31:
- sprintf(buffer, "\x1b[5;4%dm", MYSTIC[color - 24]);
- break;
- default:
- buffer[0] = 0;
- break;
- }
- ZF_LOGD("write_color( %d ): %s", color, repr(buffer));
- write(fd, buffer, strlen(buffer));
- }
- /**
- * process_trigger( fd, *cp )
- *
- * This process a command trigger.
- * It has seen TRIGGER, and now it is
- * processing whatever comes after it.
- * It will perform the process, and
- * return the char * of whatever is next.
- */
- const char *process_trigger(int fd, const char *cp) {
- char ch;
- int i, x, y;
- ch = toupper(*cp);
- cp++;
- switch (ch) {
- case 'D':
- i = 0;
- if (isdigit(*cp)) {
- i = (*cp) - '0';
- cp++;
- };
- if (isdigit(*cp)) {
- i *= 10;
- i += (*cp) - '0';
- cp++;
- };
- if ((i > 0) && (i < 80)) {
- ZF_LOGI("DEL %02d", i);
- for (x = 0; x < i; x++) {
- write(fd, "\b \b", 3);
- }
- };
- break;
- case 'C':
- i = 0;
- if (*cp == 'R') {
- cp++;
- const char *restore = color_restore(&console);
- write(fd, restore, strlen(restore));
- break;
- }
- if (isdigit(*cp)) {
- i = (*cp) - '0';
- cp++;
- };
- if (isdigit(*cp)) {
- i *= 10;
- i += (*cp) - '0';
- cp++;
- };
- write_color(fd, i);
- break;
- case 'G':
- x = 0;
- if (isdigit(*cp)) {
- x = (*cp) - '0';
- cp++;
- };
- if (isdigit(*cp)) {
- x *= 10;
- x += (*cp) - '0';
- cp++;
- };
- y = 0;
- if (isdigit(*cp)) {
- y = (*cp) - '0';
- cp++;
- };
- if (isdigit(*cp)) {
- y *= 10;
- y += (*cp) - '0';
- cp++;
- };
- char buffer[20]; // row ; column H
- ZF_LOGD("GOTO (%d,%d)", x, y);
- sprintf(buffer, "\x1b[%d;%dH", y, x);
- write(fd, buffer, strlen(buffer));
- break;
- case 'R':
- i = 0;
- if (isdigit(*cp)) {
- i = (*cp) - '0';
- cp++;
- };
- if ((i > 0) && (i < 10)) {
- ZF_LOGI("RENDER %d", i);
- current_render.effect = i;
- } else {
- current_render.effect = 0;
- }
- break;
- case 'S':
- i = 0;
- if (isdigit(*cp)) {
- i = (*cp) - '0';
- cp++;
- };
- if ((i > 0) && (i < 10)) {
- ZF_LOGI("SPEED %d", i);
- current_render.speed = i;
- } else {
- current_render.speed = 0;
- }
- break;
- case 'P':
- i = 0;
- if (isdigit(*cp)) {
- i = (*cp) - '0';
- cp++;
- };
- if ((i > 0) && (i < 10)) {
- ZF_LOGI("PAWS %d", i);
- // sleep(i);
- if (!render_overlimit) {
- sleep(i);
- };
- }
- break;
- }
- return cp;
- }
- /**
- * render_effect( fd, ch )
- *
- * Displays the given character with whatever
- * rendering effect is currently active.
- * (If any).
- */
- void render_effect(int fd, char ch) {
- int effect = current_render.effect;
- int l;
- char space = ' ';
- char bs = '\b';
- switch (effect) {
- case 1:
- // CHAR + SPC + BS
- render_sleep();
- write(fd, &ch, 1);
- render_sleep();
- write(fd, &space, 1);
- render_sleep();
- render_sleep();
- write(fd, &bs, 1);
- break;
- case 2:
- // CHAR + 8 spaces + 8 BS
- render_sleep();
- write(fd, &ch, 1);
- for (l = 0; l < 8; l++) {
- render_sleep();
- write(fd, &space, 1);
- }
- for (l = 0; l < 8; l++) {
- render_sleep();
- write(fd, &bs, 1);
- }
- break;
- case 0:
- default:
- // NORMAL
- render_sleep();
- write(fd, &ch, 1);
- break;
- }
- }
- /**
- * render( fd, string_out )
- *
- * Render an entire string.
- * Handles TRIGGER.
- * Renders with effects.
- */
- void render(int fd, const char *string_out) {
- const char *cp = string_out;
- const char *trigger = cp;
- time_t start = time(NULL);
- int elapsed;
- int over = 0;
- reset_render();
- ZF_LOGD("render(%d, %s)", fd, repr(string_out));
- // Check our time from time to time.
- // If we start running long, disable sleeps.
- while ((trigger = strstr(cp, TRIGGER)) != NULL) {
- // There is special things to handle in here.
- while (cp != trigger) {
- elapsed = time(NULL) - start;
- if (elapsed > SLEEP_LIMIT) {
- render_overlimit = 1;
- current_render.speed = 0;
- };
- // write(fd, cp, 1 );
- render_effect(fd, *cp);
- cp++;
- };
- // ZF_LOGI( "at trigger: (%s)", cp);
- cp += strlen(TRIGGER);
- // Ok, we're pointing at the trigger -- do something.
- cp = process_trigger(fd, cp);
- // ZF_LOGI( "after trigger: (%s)", cp);
- };
- // We still might be under a rendering effect.
- while (*cp != 0) {
- elapsed = time(NULL) - start;
- if (elapsed > SLEEP_LIMIT) {
- render_overlimit = 1;
- current_render.speed = 0;
- };
- // write(fd, cp, 1);
- render_effect(fd, *cp);
- cp++;
- }
- }
- // Beanzilla's no repeats
- /**
- * have_seen( list, len, item )
- *
- * Returns 1 (true) if item is in the list.
- * Rotates the list [x] = [x+1], and
- * list[len-1] = item, return 0 (false)
- */
- int have_seen(int *list, int len, int item) {
- int x;
- for (x = 0; x < len; x++) {
- if (list[x] == item) {
- return 1;
- }
- };
- // Ok, it is something different
- for (x = 0; x < len - 1; x++) {
- list[x] = list[x + 1];
- };
- list[x] = item;
- return 0;
- }
- /**
- * init_have_seen( list, len )
- *
- * Initialize the have_seen list with -1.
- * (-1 isn't a valid index, so we start
- * out with all invalid.)
- */
- void init_have_seen(int *list, int len) {
- int x;
- for (x = 0; x < len; x++) {
- list[x] = -1;
- }
- }
- /*
- These are harry "timeout" events.
- These happen when we've been sitting around awhile.
- */
- #define MAX_HARRY_EVENT_DUPS 2
- int last_seen_harry_event[MAX_HARRY_EVENT_DUPS];
- #ifdef CPP_MADMAN_STL_CODE
- const char *random_phrase(const char *words, int len, int last_seen) {
- // ooh. a map of char *s to last_seen_events. :P
- static map<const char *, array<int>> tracker;
- map<const char *, array<int>>::iterator it;
- array<int, last_seen> it = tracker.find(words);
- if (it == tracker.end()) {
- // key does not exist.
- array<int, last_seen> last;
- for (int i = 0; i < last_seen; i++) {
- last[i] = -1;
- };
- tracker.insert(words, last);
- it = tracker.find(words);
- };
- }
- #endif
- void harry_idle_event(int fd) {
- // Make something happen
- char buffer[100];
- int r;
- // This is no where near finished, BUT!
- const char *phrases[] = {"Hahaha", "Snicker, snicker", "Boo!",
- "MeOW", "I see U", "Arrooo!",
- "Ahh-wooo!", "Aaaooo!"};
- const char *cp;
- // Remember the last phrase used,
- // and don't repeat (the last two)!
- do {
- r = random() % ((sizeof(phrases) / sizeof(char *)) - 1);
- } while (have_seen(last_seen_harry_event, MAX_HARRY_EVENT_DUPS, r));
- ZF_LOGD("%d => %d %d", r, last_seen_harry_event[0], last_seen_harry_event[1]);
- cp = phrases[r];
- int color = random() % 16;
- if (color == 0) {
- color++;
- } // If it's 0 let's make it 1. // color = (random() % 15) + 1
- sprintf(buffer, "^S2^C%02d%s^P2^CR^D%02d", color, cp, (int)strlen(cp));
- ZF_LOGD("harry_event: render(%d, \"%s\")", fd, buffer);
- render(fd, buffer);
- }
- void init_harry() {
- init_have_seen(last_seen_harry_event, MAX_HARRY_EVENT_DUPS);
- // ZF_LOGD("init => %d %d", last_seen_harry_event[0],
- // last_seen_harry_event[1]);
- console_init(&console);
- }
- /*
- The code to get the username and fullname is useless on telnet
- connections.
- */
- char *username = NULL;
- char *fullname = NULL;
- /*
- Pascal String Copy. Copy from pascal string, to C String.
- First char is pascal string length. (Max 255).
- */
- void pcopy(char *pstring, char *str) {
- int len = (int)*pstring;
- strncpy(str, pstring + 1, len);
- str[len] = 0;
- }
- /*
- This only works for those few idiots that use the
- horribly broken SSH crap that Mystic uses.
- */
- int locate_user(const char *alias) {
- FILE *user;
- char buffer[0x600];
- char temp[100];
- user = fopen("data/users.dat", "rb");
- if (user == NULL)
- return 0;
- // Carry on!
- while (fread(buffer, 0x600, 1, user) == 1) {
- pcopy(buffer + 0x6d, temp);
- if (strcasecmp(temp, username) == 0) {
- pcopy(buffer + 0x8c, temp);
- fullname = strdup(temp);
- break;
- }
- /*
- printf("Alias: %s\n", temp);
- pcopy(buffer + 0x8c, temp );
- printf("Full Name: %s\n", temp );
- */
- }
- fclose(user);
- return 1;
- }
- // Buffers are BSIZE + 1, so a buffer that size can strcpy safely.
- regex_t ANSI;
- regex_t WORDS;
- regex_t WORD;
- int init_regex(void) {
- int ret;
- char ansi[] = "\x1b\[[0-9]+(;[0-9]+)*?[a-zA-Z]";
- char words[] = "[a-zA-Z]+( [a-zA-Z]+)+";
- char word[] = "[a-zA-Z]+";
- char errorbuf[100];
- if (ret = regcomp(&ANSI, ansi, REG_EXTENDED | REG_NEWLINE)) {
- regerror(ret, &ANSI, errorbuf, sizeof(errorbuf));
- ZF_LOGW("Regex %s failed to compile: %s", ansi, errorbuf);
- return 0;
- };
- if (ret = regcomp(&WORDS, words, REG_EXTENDED | REG_NEWLINE)) {
- regerror(ret, &WORDS, errorbuf, sizeof(errorbuf));
- ZF_LOGW("Regex %s failed to compile: %s", words, errorbuf);
- return 0;
- };
- if (ret = regcomp(&WORD, word, REG_EXTENDED | REG_NEWLINE)) {
- regerror(ret, &WORD, errorbuf, sizeof(errorbuf));
- ZF_LOGW("Regex %s failed to compile: %s", word, errorbuf);
- return 0;
- };
- return 1;
- }
- int regmatch(regex_t *preg, const char *string, size_t nmatch,
- regmatch_t pmatch[], int eflags) {
- // returns number of matches found. (Max nmatch)
- int matches = 0;
- int offset = 0;
- int ret;
- while (matches < nmatch) {
- ret = regexec(preg, string + offset, nmatch - matches, pmatch + matches,
- eflags);
- if (!ret) {
- int current = offset;
- offset += pmatch[matches].rm_eo;
- pmatch[matches].rm_so += current;
- pmatch[matches].rm_eo += current;
- matches++;
- } else if (ret == REG_NOMATCH) {
- break;
- } else {
- break;
- }
- }
- return matches;
- }
- #define MAX_MATCH 32
- regmatch_t rxmatch[MAX_MATCH];
- int rx_match(regex_t *regex, const char *buffer) {
- int ret;
- ret = regmatch(regex, buffer, MAX_MATCH, rxmatch, 0);
- if (0) {
- for (int i = 0; i < ret; i++) {
- ZF_LOGI("%d : (%d-%d)", i, rxmatch[i].rm_so, rxmatch[i].rm_eo);
- }
- }
- return ret;
- }
- /*
- Terminal processing section.
- Monitor lines, position, color.
- What screen size do I want to emulate?
- ANSI codes.
- Do I need this??
- */
- /**
- * random_activate()
- *
- * Is a weight (1-10),
- * tests if random number is < weight * 10.
- *
- * So random_activate(9) happens more frequently
- * then random_activate(8) or lower.
- *
- * This probably needs to be fixed.
- * We need a better randint(RANGE) code.
- */
- int random_activate(int w) {
- int r = random() % 100;
- if (r <= (w * 10)) {
- return 1;
- };
- return 0;
- }
- /*
- word_state():
- -1 only lower
- +1 only upper
- 0 mixed
- */
- int word_state(const char *buffer, int len) {
- int p;
- int upper = 0;
- int lower = 0;
- int ret;
- float pct;
- for (p = 0; p < len; p++) {
- char c = buffer[p];
- if (isalpha(c)) {
- if (isupper(c)) {
- upper++;
- };
- if (islower(c)) {
- lower++;
- };
- }
- }
- if (upper == lower) {
- return 0;
- }
- if (upper > lower) {
- ret = 1;
- pct = ((float)lower / (float)upper) * 100.0;
- } else {
- ret = -1;
- pct = ((float)upper / (float)lower) * 100.0;
- }
- // ZF_LOGD("So far %d with %f %%", ret, pct);
- if (pct < 40.0) {
- return ret;
- }
- return 0;
- }
- /*
- Given a buffer and length, mangle away.
- We *REALLY* want this to do something, so better run some tests.
- Or, maybe we don't. This treats words consistently the same way.
- HMM.
- toupper, tolower, flipper
- */
- int word_mangler(char *buffer, int len) {
- int p;
- int count = 0;
- int state;
- state = word_state(buffer, len);
- // ZF_LOGD("word_state(%.*s) %d", len, buffer, state);
- // TODO: Transposer
- for (p = 0; p < len; p++) {
- char c = buffer[p];
- switch (state) {
- case -1:
- // upper
- if (islower(c)) {
- count++;
- buffer[p] = toupper(c);
- }
- break;
- case 1:
- // lower
- if (isupper(c)) {
- count++;
- buffer[p] = tolower(c);
- }
- break;
- case 0:
- // flipper
- if (islower(c)) {
- count++;
- buffer[p] = toupper(c);
- } else {
- if (isupper(c)) {
- count++;
- buffer[p] = tolower(c);
- }
- }
- break;
- }
- }
- return count;
- }
- /*
- * The buffer that we've been given is much larger now.
- *
- */
- int mangle(int fd, char *buffer) {
- int x, i;
- int need_render = 0; // changing word case around doesn't need the render
- int mangled = 0;
- int mangled_chars = 0;
- char *cp;
- // Ok, we want to look for words to:
- // MaNGlE , or transpose (both?!)
- // or possibly transpose words
- char work[(BSIZE * 4) + 1];
- // Use terminal - clean out ANSI
- ZF_LOGI("mangle(%s)", repr(buffer));
- // strcpy(work, buffer);
- /*
- NOTE: We copy the buffer, so we can clear out ANSI codes, etc.
- Otherwise we might mess some ANSI up in the manglying
- process.
- */
- /*
- (random) Look for ANSI CLS and:
- display random spooky texts around, with delays ... then CLS.
- display ANSI graphic file, with delays ... then CLS
- */
- const char *ANSI_CLS = "\x1b[2J";
- cp = strstr(buffer, ANSI_CLS);
- if (cp != NULL) {
- ZF_LOGI("seen: ANSI_CLS");
- if (random_activate(4)) {
- int r;
- char display[100] = "";
- const char *phrasing[] = {
- "^R1Haha^P1ha^P1ha",
- "Poof!",
- "Got U",
- "Anyone there?",
- "^R1Knock, ^P1Knock"
- };
- // import magic
- struct console_details temp_console;
- // Make exact copy of our current console state.
- memcpy(&temp_console, &console, sizeof(console));
- // Play the buffer into the console
- console_receive(&temp_console, buffer, cp - buffer);
- char restore_color[30]; // ansi color
- strcpy( restore_color, color_restore(&temp_console));
- ZF_LOGI("mangle(ANSI_CLS)");
- // sprintf( display, "^P2...");
- // This string actually screws up ANSI detection (takes too long)
- // strcpy(display, "^P2^S501234567890^P1abcdef^P2g^P3h^P4i^S0^P2");
- // strcpy(display, "^P2^S301234^P15^S0^P2");
- // Add in random text, plus color!
- r = random() % ((sizeof(phrasing) / sizeof(char *)) - 1);
- int color = random() % 15 + 1;
- int x = random() % 30 + 1;
- int y = random() % 15 + 1;
- /*
- Don't have it pause there before moving the cursor.
- Move the cursor, get the color changed, THEN pause.
- Then act all crazy.
- NOTE: Make sure if you use any ^R Render effects, turn them off before
- trying to display the restore_color. :P ^R0
- Also, make sure you re-home the cursor ^G0101 because that's where they
- are expecting the cursor to be! (At least it's how Mystic does it.)
- HOME, CLS, HOME, ... Not sure what others do there. We'll see.
- */
- sprintf(display, "^G%02d%02d^S3^C%02d^P1%s^S0^R0%s^P1^G0101", x, y, color, phrasing[r], restore_color);
- //sprintf(display, "^P1^S3^C%02d%s^S0^R0%s^P1", color, phrasing[r], restore_color);
- ZF_LOGI("mangle(ANSI_CLS): Inserted (%02d) %s", color, phrasing[r]);
- // Move the buffer so there's room for the display string.
- if ( string_insert(buffer, BSIZE * 4, cp - buffer, display)) {
- ZF_LOGI("mangle(ANSI_CLS): [%s]", repr(buffer));
- need_render = 1;
- } else {
- ZF_LOGD("insert failed [%s].", repr(display));
- }
- // memmove(cp + strlen(display), cp, strlen(cp) + 1);
- // strncpy(cp, display, strlen(display));
- /*
- Copy the new buffer over, but hide our "render" code
- from the remaining mangler steps.
- */
- strcpy(work, buffer);
- i = cp - buffer;
- // find offset into "buffer"
- // apply to work.
- memset(work + i, ' ', strlen(display));
- };
- }
- strcpy(work, buffer); // sure.
- const char replace_with = ' ';
- for (x = 0; x < strlen(buffer); x++) {
- int ansi = console_char(&console, buffer[x]);
- if (ansi) {
- work[x] = replace_with;
- }
- }
- ZF_LOGD("Work Now: [%s]", repr(work));
- #ifdef OLD_WAY
- /* work -- clear out ANSI so we don't mangle ANSI codes. */
- x = rx_match(&ANSI, work);
- if (x > 0) {
- ZF_LOGD("found %d ANSI", x);
- for (i = 0; i < x; i++) {
- memset(work + rxmatch[i].rm_so, replace_with,
- rxmatch[i].rm_eo - rxmatch[i].rm_so);
- };
- ZF_LOGD("Work Now: [%s]", repr(work));
- }
- #endif
- // ZF_LOGI("mangle: %s", repr(work));
- /*
- (random) Locate words (in work), and possibly flip them around.
- Transpose words. Transpose case. Transpose letters.
- */
- x = rx_match(&WORDS, work);
- ZF_LOGD("found %d word groups", x);
- if (x > 0) {
- for (i = 0; i < x; i++) {
- // Yes! Be random!
- if (random_activate(8)) {
- int c = word_mangler(buffer + rxmatch[i].rm_so,
- rxmatch[i].rm_eo - rxmatch[i].rm_so);
- if (c) {
- mangled++;
- mangled_chars += c;
- }
- }
- // Do things here.
- /*
- // NO! This is predictable
- if (i % 3 == 0) {
- mangled++;
- for (int p = rxmatch[i].rm_so; p < rxmatch[i].rm_eo; p++) {
- buffer[p] = tolower(buffer[p]);
- mangled_chars++;
- }
- } else {
- if (i % 3 == 1) {
- mangled++;
- for (int p = rxmatch[i].rm_so; p < rxmatch[i].rm_eo; p++) {
- buffer[p] = toupper(buffer[p]);
- mangled_chars++;
- }
- }
- }
- */
- }
- }
- /*
- (random) Locate single words, and transpose words.
- Transpose letters.
- */
- /*
- (random) Display up to certain point. Delay.
- Print some characters slowly. Delay.
- */
- if (need_render) {
- ZF_LOGD("HH %d : (%d) %s", need_render, (int)strlen(buffer), repr(buffer));
- } else {
- if (mangled) {
- ZF_LOGD("Mangled %d words, %d chars : %s", mangled, mangled_chars,
- repr(buffer));
- }
- }
- if (need_render) {
- render(fd, buffer);
- } else {
- write(fd, buffer, strlen(buffer));
- };
- return need_render && mangled;
- }
- int harry_happens(time_t *last_event, int wakeup) {
- time_t now = time(NULL);
- int elapsed = now - *last_event;
- if (elapsed > wakeup) {
- // Ok! It's been too long since we've done something.
- *last_event = now;
- return 1;
- }
- return 0;
- }
- int main(int argc, char *argv[]) {
- int master;
- pid_t pid;
- int node = -1;
- file_output_open("horrible_harry.log");
- init_harry();
- srandom(time(NULL));
- // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown -Ubugz
- // -PUWISHPASSWORD
- // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
- // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown -Ubugz
- // -PUWISH
- // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
- // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
- // ./mystic -TID9 -IP192.168.0.1 -HOSTUnknown -ML0 -SL1 -ST0 -CUnknown
- // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown -Ubugz
- // -PDUMBWAYTODOTHIS
- // ./mystic -TID9 -IP192.168.0.1 -HOSTUnknown -ML1 -SL1 -ST2 -CUnknown -Ubugz
- // -PIDONTUSEPASCAL
- // SSH: -ML1 -ST2
- // Telnet: -ML0 -ST0
- // Locate username (if given) in the command line
- // -U<username>
- for (int x = 0; x < argc; x++) {
- if (strncmp("-U", argv[x], 2) == 0) {
- username = argv[x] + 2;
- ZF_LOGI("Username: [%s]", username);
- };
- if (strncmp("-SL", argv[x], 3) == 0) {
- node = argv[x][3] - '0' + 1;
- ZF_LOGI("Node: %d", node);
- }
- }
- if (username != NULL) {
- locate_user(username);
- ZF_LOGD("Username: [%s] A.K.A. [%s]", username, fullname);
- }
- if (!init_regex())
- return 2;
- // With IGNBRK I don't think I need this anymore. (Nope!)
- // signal(SIGINT, SIG_IGN);
- pid = forkpty(&master, NULL, NULL, NULL);
- // impossible to fork
- if (pid < 0) {
- return 1;
- }
- // child
- else if (pid == 0) {
- char *args[20]; // max 20 args
- int x;
- args[0] = TARGET;
- for (x = 1; x < argc; x++) {
- args[x] = argv[x];
- };
- args[x] = NULL;
- // run Mystic, run!
- execvp(TARGET, args);
- }
- // parent
- else {
- // remove the echo
- // ICANON - raw mode. No more line buffering!
- struct termios tios, orig1;
- struct timeval timeout;
- time_t last_event = 0; // time(NULL);
- ZF_LOGD("starting");
- tcgetattr(master, &tios);
- tios.c_lflag &= ~(ECHO | ECHONL | ICANON);
- tcsetattr(master, TCSAFLUSH, &tios);
- tcgetattr(1, &orig1);
- tios = orig1;
- tios.c_iflag &= ~(ICRNL | IXON); // Disable software flow control
- tios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
- // https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html
- // ISIG should be Ctrl-C and Ctrl-Z IGNBRK +
- tcsetattr(1, TCSAFLUSH, &tios);
- for (;;) {
- // define estruturas para o select, que serve para verificar qual
- // se tornou "pronto pra uso"
- fd_set read_fd;
- fd_set write_fd;
- fd_set except_fd;
- // inicializa as estruturas
- FD_ZERO(&read_fd);
- FD_ZERO(&write_fd);
- FD_ZERO(&except_fd);
- // atribui o descritor master, obtido pelo forkpty, ao read_fd
- FD_SET(master, &read_fd);
- // atribui o stdin ao read_fd
- FD_SET(STDIN_FILENO, &read_fd);
- // o descritor tem que ser unico para o programa, a documentacao
- // recomenda um calculo entre os descritores sendo usados + 1
- /*
- TODO: Figure out how this would work.
- I'm thinking something like timeouts 30-50 seconds?
- And as we get closer, 15-25 seconds.
- */
- // we're in luck! The last parameter is time interval/timeout. :D
- timeout.tv_sec = 10;
- timeout.tv_usec = 0;
- // select(master+1, &read_fd, &write_fd, &except_fd, NULL);
- if (select(master + 1, &read_fd, &write_fd, &except_fd, &timeout) == 0) {
- // This means timeout!
- ZF_LOGI("TIMEOUT");
- harry_idle_event(STDOUT_FILENO);
- }
- char input[BSIZE + 1];
- static char output[(BSIZE * 4) + 1];
- int total;
- // read_fd esta atribuido com read_fd?
- if (FD_ISSET(master, &read_fd)) {
- // leia o que bc esta mandando
- if ((total = read(master, &output, BSIZE)) != -1) {
- // e escreva isso na saida padrao
- output[total] = 0;
- // if ( harry_happens( &last_event, 5)) {
- if (1) {
- ZF_LOGI("harry_happens");
- if (mangle(STDOUT_FILENO, output) == 0) {
- // failed, so. Try again.
- last_event = 0;
- }
- } else {
- write(STDOUT_FILENO, &output, total);
- // This is OUTPUT from the BBS
- // ZF_LOGI( ">> %s", repr(output));
- ZF_LOGI(">> %d chars", (int)strlen(output));
- // I think repr is flipping out here. :(
- // ZF_LOGI( ">> %d", (int)strlen(repr(output)));
- }
- } else
- break;
- }
- // read_fd esta atribuido com a entrada padrao?
- if (FD_ISSET(STDIN_FILENO, &read_fd)) {
- // leia a entrada padrao
- total = read(STDIN_FILENO, &input, BSIZE);
- input[total] = 0;
- // e escreva no bc
- ZF_LOGI("<< %s", repr(input));
- write(master, &input, total);
- // This is INPUT from the USER
- // ZF_LOGI_MEM( input, strlen(input), "<< ");
- }
- }
- // Restore terminal
- tcsetattr(1, TCSAFLUSH, &orig1);
- ZF_LOGD("exit");
- }
- return 0;
- }
|