123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707 |
- #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 <strings.h> // strcasecmp
- #include <time.h> // usleep(), nanonsleep() ?
- #include <ctype.h>
- #include <stdlib.h> // random()
- #include <regex.h>
- #include "zf_log.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);
- }
- #include "lastseen.h"
- int randint(int N) { return rand() / (RAND_MAX / N + 1); }
- int randrange(int M, int N) {
- return M + rand() / (RAND_MAX / (N - M + 1) + 1);
- }
- #define TARGET "./mySTIC"
- #define BSIZE 128
- 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;
- };
-
- 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;
- };
-
- 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;
- }
- 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);
-
- strncpy(buffer + pos, insert, strlen(insert));
-
- return 1;
- }
- 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 "^"
- #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) {
- ms_sleep(current_render.speed * 10);
- }
- }
- struct console_details {
- int posx, posy;
- int savedx, savedy;
- char ansi[20];
- int in_ansi;
- int fgcolor;
- int bgcolor;
- int status;
- } 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_LOGV("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++;
-
- if (*cp == '[') {
- cp++;
-
- switch (*last) {
- case 'A':
-
- 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':
-
- 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;
-
- understood = 1;
- return;
- case 'C':
-
- 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;
-
-
-
- while (cdp->posx > 79) {
- cdp->posy++;
-
- cdp->posx -= 79;
- }
- understood = 1;
- return;
- case 'D':
-
- 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;
-
-
-
- while (cdp->posx < 0) {
- cdp->posy--;
- if (cdp->posy < 0) {
- cdp->posy = 0;
- }
- cdp->posx += 79;
- }
- understood = 1;
- return;
- case 'H':
-
- if (*cp == ';') {
-
- number = 1;
- cp++;
- if (cp == last) {
-
- number2 = 1;
- } else {
- number2 = atoi(cp);
- }
- } else {
-
- number = atoi(cp);
- cp = strchr(cp, ';');
- if (cp == NULL) {
-
- number2 = 1;
- } else {
-
- cp++;
- if (cp == last) {
- number2 = 1;
- } else {
- number2 = atoi(cp);
- }
- }
- }
-
- cdp->posx = number - 1;
- cdp->posy = number2 - 1;
- understood = 1;
- break;
- case 'J':
-
- if (cp == last) {
- number = 0;
- } else {
- number = atoi(cp);
- };
-
- if (number == 2) {
- cdp->posx = 0;
- cdp->posy = 0;
- };
- understood = 1;
- break;
- case 'm':
-
- if (cp == last) {
-
- 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 '!':
-
- case 'n':
- understood = 1;
- break;
- default:
-
- ZF_LOGD("console_ansi( %s ): ???", repr(ansi));
- understood = 0;
- }
- }
- };
- if (!understood) {
- ZF_LOGD("console_ansi( %s ): was not understood.", repr(ansi));
- }
- }
- int console_char(struct console_details *cdp, char ch) {
- char *cp;
- if (cdp->in_ansi) {
-
- cp = cdp->ansi + strlen(cdp->ansi);
- *cp = ch;
- cp++;
- *cp = 0;
- if (isalpha(ch)) {
-
- 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') {
-
- cdp->posx = 0;
- return 0;
- }
- if (ch == '\n') {
- cdp->posy++;
-
- return 0;
- }
- if (ch == '\b') {
-
- if (cdp->posx > 0) {
- cdp->posx--;
- }
- return 0;
- }
- if (ch == '\f') {
-
-
- cdp->posx = 0;
- cdp->posy = 0;
- return 0;
- }
-
- cdp->posx++;
- if (cdp->posx > 79) {
- cdp->posx = 0;
- cdp->posy++;
-
- }
- 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]);
- }
- }
- const int MYSTIC[] = {0, 4, 2, 6, 1, 5, 3, 7};
- 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));
- }
- 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];
- 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);
-
- if (!render_overlimit) {
- sleep(i);
- };
- }
- break;
- }
- return cp;
- }
- void render_effect(int fd, char ch) {
- int effect = current_render.effect;
- int l;
- char space = ' ';
- char bs = '\b';
- switch (effect) {
- case 1:
-
- render_sleep();
- write(fd, &ch, 1);
- render_sleep();
- write(fd, &space, 1);
- render_sleep();
- render_sleep();
- write(fd, &bs, 1);
- break;
- case 2:
-
- 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:
-
- render_sleep();
- write(fd, &ch, 1);
- break;
- }
- }
- 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));
-
-
- while ((trigger = strstr(cp, TRIGGER)) != NULL) {
-
- while (cp != trigger) {
- elapsed = time(NULL) - start;
- if (elapsed > SLEEP_LIMIT) {
- render_overlimit = 1;
- current_render.speed = 0;
- };
-
- render_effect(fd, *cp);
- cp++;
- };
-
- cp += strlen(TRIGGER);
-
- cp = process_trigger(fd, cp);
-
- };
-
- while (*cp != 0) {
- elapsed = time(NULL) - start;
- if (elapsed > SLEEP_LIMIT) {
- render_overlimit = 1;
- current_render.speed = 0;
- };
-
- render_effect(fd, *cp);
- cp++;
- }
- }
- #ifdef NOPE
- int have_seen(int *list, int len, int item) {
- int x;
- for (x = 0; x < len; x++) {
- if (list[x] == item) {
- return 1;
- }
- };
-
- for (x = 0; x < len - 1; x++) {
- list[x] = list[x + 1];
- };
- list[x] = item;
- return 0;
- }
- void init_have_seen(int *list, int len) {
- int x;
- for (x = 0; x < len; x++) {
- list[x] = -1;
- }
- }
- #endif
- LastSeen last_seen_harry_event(2);
- #ifdef CPP_MADMAN_STL_CODE
- const char *random_phrase(const char *words, int len, int last_seen) {
-
- 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()) {
-
- 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) {
-
- char buffer[100];
- int r;
-
- const char *phrases[] = {"Hahaha", "Snicker, snicker", "Boo!",
- "MeOW", "I see U", "Arrooo!",
- "Ahh-wooo!", "Aaaooo!"};
- const char *cp;
-
-
- do {
- r = randint((sizeof(phrases) / sizeof(char *)));
-
- } while (last_seen_harry_event.seen_before(r));
-
-
- cp = phrases[r];
- int 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() {
-
-
-
- console_init(&console);
- }
- const char *username = NULL;
- const char *fullname = NULL;
- void pcopy(char *pstring, char *str) {
- int len = (int)*pstring;
- strncpy(str, pstring + 1, len);
- str[len] = 0;
- }
- 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;
-
- while (fread(buffer, 0x600, 1, user) == 1) {
- pcopy(buffer + 0x6d, temp);
- if (strcasecmp(temp, username) == 0) {
- pcopy(buffer + 0x8c, temp);
- fullname = strdup(temp);
- break;
- }
-
- }
- fclose(user);
- return 1;
- }
- 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) {
-
- 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;
- }
- int random_activate(int w) {
- int r = random() % 100;
- if (r <= (w * 10)) {
- return 1;
- };
- return 0;
- }
- 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;
- }
-
- if (pct < 40.0) {
- return ret;
- }
- return 0;
- }
- int word_mangler(char *buffer, int len) {
- int p;
- int count = 0;
- int state;
- state = word_state(buffer, len);
-
-
- for (p = 0; p < len; p++) {
- char c = buffer[p];
- switch (state) {
- case -1:
-
- if (islower(c)) {
- count++;
- buffer[p] = toupper(c);
- }
- break;
- case 1:
-
- if (isupper(c)) {
- count++;
- buffer[p] = tolower(c);
- }
- break;
- case 0:
-
- if (islower(c)) {
- count++;
- buffer[p] = toupper(c);
- } else {
- if (isupper(c)) {
- count++;
- buffer[p] = tolower(c);
- }
- }
- break;
- }
- }
- return count;
- }
- int mangle(int fd, char *buffer) {
- int x, i;
- int need_render = 0;
- int mangled = 0;
- int mangled_chars = 0;
- char *cp;
-
-
-
- char work[(BSIZE * 4) + 1];
-
- ZF_LOGI("mangle(%s)", repr(buffer));
-
-
-
- const char *ANSI_CLS = "\x1b[2J";
- static int ANSI_CLS_count = 0;
- cp = strstr(buffer, ANSI_CLS);
- if (cp != NULL) {
- ZF_LOGI("seen: ANSI_CLS");
- ANSI_CLS_count++;
-
-
- if ((ANSI_CLS_count > 1) && (random_activate(4))) {
- int r;
- char display[100] = "";
- const char *phrasing[] = {"^R1Haha^P1ha^P1ha", "Poof!", "Got U",
- "Anyone there?", "^R1Knock, ^P1Knock"};
-
- struct console_details temp_console;
-
- memcpy(&temp_console, &console, sizeof(console));
-
- console_receive(&temp_console, buffer, cp - buffer);
- char restore_color[30];
- strcpy(restore_color, color_restore(&temp_console));
- ZF_LOGI("mangle(ANSI_CLS)");
-
-
-
-
-
- r = random() % ((sizeof(phrasing) / sizeof(char *)) - 1);
- int color = random() % 15 + 1;
- int x = random() % 30 + 1;
- int y = random() % 15 + 1;
-
- sprintf(display, "^G%02d%02d^S3^C%02d^P1%s^S0^R0%s^P1^G0101", x, y, color,
- phrasing[r], restore_color);
-
-
- ZF_LOGI("mangle(ANSI_CLS): Inserted (%02d) %s", color, phrasing[r]);
-
- 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));
- }
-
-
-
- strcpy(work, buffer);
- i = cp - buffer;
-
-
- memset(work + i, ' ', strlen(display));
- };
- }
- strcpy(work, buffer);
- 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
-
- 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
-
-
- x = rx_match(&WORDS, work);
- ZF_LOGD("found %d word groups", x);
- if (x > 0) {
- for (i = 0; i < x; i++) {
-
- 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;
- }
- }
-
-
- }
- }
-
-
- 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) {
-
- *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));
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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;
-
-
- pid = forkpty(&master, NULL, NULL, NULL);
-
- if (pid < 0) {
- return 1;
- }
-
- else if (pid == 0) {
- char *args[20];
- int x;
- char new_exec[] = TARGET;
- args[0] = new_exec;
- for (x = 1; x < argc; x++) {
- args[x] = argv[x];
- };
- args[x] = NULL;
-
- execvp(TARGET, args);
- }
-
- else {
-
-
- struct termios tios, orig1;
- struct timeval timeout;
- time_t last_event = 0;
- 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);
- tios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
-
-
- tcsetattr(1, TCSAFLUSH, &tios);
- for (;;) {
-
-
- fd_set read_fd;
- fd_set write_fd;
- fd_set except_fd;
-
- FD_ZERO(&read_fd);
- FD_ZERO(&write_fd);
- FD_ZERO(&except_fd);
-
- FD_SET(master, &read_fd);
-
- FD_SET(STDIN_FILENO, &read_fd);
-
-
-
-
- timeout.tv_sec = 10;
- timeout.tv_usec = 0;
-
- if (select(master + 1, &read_fd, &write_fd, &except_fd, &timeout) == 0) {
-
- ZF_LOGI("TIMEOUT");
- harry_idle_event(STDOUT_FILENO);
- }
- char input[BSIZE + 1];
- static char output[(BSIZE * 4) + 1];
- int total;
-
- if (FD_ISSET(master, &read_fd)) {
-
- if ((total = read(master, &output, BSIZE)) != -1) {
-
- output[total] = 0;
-
- if (1) {
- ZF_LOGI("harry_happens");
- if (mangle(STDOUT_FILENO, output) == 0) {
-
- last_event = 0;
- }
- } else {
- write(STDOUT_FILENO, &output, total);
-
-
- ZF_LOGI(">> %d chars", (int)strlen(output));
-
-
- }
- } else
- break;
- }
-
- if (FD_ISSET(STDIN_FILENO, &read_fd)) {
-
- total = read(STDIN_FILENO, &input, BSIZE);
- input[total] = 0;
-
- ZF_LOGI("<< %s", repr(input));
- write(master, &input, total);
-
-
- }
- }
-
- tcsetattr(1, TCSAFLUSH, &orig1);
- ZF_LOGD("exit");
- }
- return 0;
- }
|