12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010 |
- #include "images.h"
- #include "lastseen.h"
- #include "render.h"
- #include "terminal.h"
- #include "utils.h"
- #include <iomanip>
- #include <regex>
- #include <sstream>
- #include <string>
- #include <vector>
- #include "zf_log.h"
- #include <unistd.h> // write
- extern struct console_details console;
- #define BSIZE 512
- void harry_idle_event(int fd) {
-
- std::ostringstream buffer;
- int r;
-
-
- const char *phrases[] = {"Hahaha", "Snicker, snicker", "Boo!",
- "MeOW", "I see U", "Arrooo!",
- "Ahh-wooo!", "Aaaooo!"};
- static LastSeen last_seen_harry_event(2);
- do {
- r = randint((sizeof(phrases) / sizeof(char *)));
- } while (last_seen_harry_event.seen_before(r));
- int color = random() % 15 + 1;
-
- buffer << "^S2^C" << std::setfill('0') << std::setw(2) << color << phrases[r]
- << "^P2^CR^D" << std::setw(2) << strlen(phrases[r]);
-
- std::string str = buffer.str();
- ZF_LOGD("harry_event: render(%d, \"%s\")", fd, str.c_str());
- render(fd, str);
- }
- void init_harry() {
-
-
-
- console_init(&console);
- }
- #ifdef UNWANTED
- 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;
- while (matches < nmatch) {
- int 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;
- }
- #endif
- int random_activate(int w) {
- int r = randint(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(std::string &buffer, std::string& word, std::string& text, std::vector<int> &text_offsets, std::pair<int,int> pos_len) {
- }
- #ifdef UNWANTED
- int word_mangler(char *buffer, int len) {
- int p;
- int count = 0;
- int state;
-
-
- state = randrange(-1, 1);
-
- for (p = 0; p < len; p++) {
- char c = buffer[p];
- if (randint(len) == p) {
- break;
- }
- 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 word_wrangler(char *buffer, int len) {
- int p;
- int count;
- int state;
-
-
- if (len < 5) {
- return 0;
- }
- p = randint(len - 4) + 2;
- for (count = 0; count < 4; count++) {
- if (!isalpha(buffer[p + count]))
- break;
- }
- ZF_LOGV_MEM(buffer, len, "wrangler %d len %d:", p, count);
- if (count >= 2) {
- for (int x = 0; x < count / 2; x++) {
- char ch = buffer[p + x];
- buffer[p + x] = buffer[p + count - 1 - x];
- buffer[p + count - 1 - x] = ch;
- }
- ZF_LOGV_MEM(buffer, len, "word now:");
- return 1;
- } else
- return 0;
- }
- int buffer_insert(char *buffer, int len, int max_length, int pos,
- const char *insert) {
- if (len + strlen(insert) > max_length) {
- ZF_LOGD("buffer_insert failed [%s]", repr(insert));
- return 0;
- }
- memmove(buffer + pos + strlen(insert), buffer + pos, len - pos);
- strncpy(buffer + pos, insert, strlen(insert));
- return 1;
- }
- #endif
- #ifdef UNWANTED
- int mangle(int fd, const char *buffer, int len) {
- int x, i;
- int need_render = 0;
- int mangled = 0;
- int mangled_chars = 0;
- char play[BSIZE * 2];
- const char *cp;
-
-
- memcpy(play, buffer, len);
-
- char work[BSIZE * 2];
-
-
-
- ZF_LOGI_MEM(play, len, "Mangle (%u bytes):", len);
-
-
-
-
- const char *ANSI_CLS = "\x1b[2J";
- cp = strnstr(play, len, ANSI_CLS);
- if (cp != NULL) {
- static int ANSI_CLS_count = 0;
- ZF_LOGI("seen: ANSI_CLS");
- ANSI_CLS_count++;
-
-
- if (ANSI_CLS_count > 1) {
-
- struct console_details temp_console;
-
- memcpy(&temp_console, &console, sizeof(console));
-
- console_receive(&temp_console, play, cp - play);
- char restore_color[30];
- strcpy(restore_color, color_restore(&temp_console));
- if (random_activate(3)) {
- char display[100] = "";
- int slen;
- int needs_cls = 0;
- struct image {
- const char **lines;
- int size;
- int cls;
- int width;
- } images[] = {{ghost, sizeof(ghost) / sizeof(char *), 1, 0},
- {ghead, sizeof(ghead) / sizeof(char *), 1, 0},
- {wolf, sizeof(wolf) / sizeof(char *), 1, 0},
- {panther, sizeof(panther) / sizeof(char *), 1, 0},
- {bat, sizeof(bat) / sizeof(char *), 1, 0},
- {icu, sizeof(icu) / sizeof(char *), 0, 20},
- {skull, sizeof(skull) / sizeof(char *), 0, 19},
- {skullblink, sizeof(skullblink) / sizeof(char *), 0, 19}};
- static LastSeen last_files(2);
- int r;
- do {
- r = randint((sizeof(images) / sizeof(image)));
- } while (last_files.seen_before(r));
- char fgoto[32];
- if (!images[r].cls) {
- int x = 0, y = 0;
- x = randint(79 - images[r].width) + 1;
- y = randint(24 - images[r].size) + 1;
- int slen;
-
- slen = snprintf(fgoto, sizeof(fgoto), "^f%02d%02d\x1b[1;1H", x, y);
- if (slen >= sizeof(fgoto)) {
- ZF_LOGE("snprintf %d > size %d", slen, (int)sizeof(fgoto));
- fgoto[0] = 0;
- }
- } else {
- strcpy(fgoto, "^F");
- }
-
- needs_cls = images[r].cls;
-
-
-
-
-
-
-
- render_image(images[r].lines, images[r].size);
- slen = snprintf(display, sizeof(display), "%s%s%s^P3",
- needs_cls ? "\x1b[2J" : "", fgoto, restore_color);
- if (slen >= sizeof(display)) {
- ZF_LOGE("snprintf %d > size %d", slen, (int)sizeof(display));
- display[0] = 0;
- }
- ZF_LOGI("mangle(ANSI_CLS): %d file inserted %s", r, repr(display));
-
- if (buffer_insert(play, len, sizeof(play), cp - play, display)) {
- len += strlen(display);
-
- ZF_LOGI_MEM(play, len, "mangle(ANSI_CLS) (%u bytes):", len);
-
-
-
- need_render = 1;
-
- memcpy(work, play, len);
-
- i = cp - play;
-
-
- memset(work + i, ' ', strlen(display));
- } else {
- ZF_LOGD("insert failed [%s].", repr(display));
- }
- } else {
- if (random_activate(4)) {
- int r;
- char display[100] = "";
- int slen;
-
- const char *phrasing[] = {
- "^R1Haha^P1ha^P1ha", "Poof!", "Got U", "Anyone there?",
- "^R1Knock, ^P1Knock",
-
- "^G0101^C07^S9Segmentation fault (core dumped)^P2"};
- static LastSeen last_phrasing(2);
- ZF_LOGI("mangle(ANSI_CLS)");
-
-
-
-
-
- do {
- r = randint(sizeof(phrasing) / sizeof(char *));
- } while (last_phrasing.seen_before(r));
- int color = random() % 15 + 1;
- int x = random() % 30 + 1;
- int y = random() % 15 + 1;
-
- slen = snprintf(display, sizeof(display),
- "^G%02d%02d^S3^C%02d^P1%s^S0^R0%s^P1^G0101", x, y,
- color, phrasing[r], restore_color);
- if (slen >= sizeof(display)) {
- ZF_LOGE("snprintf %d > size %d (Phrase: %d, %s)", slen,
- (int)sizeof(display), r, phrasing[r]);
- display[0] = 0;
- }
-
-
-
-
- ZF_LOGD("mangle(ANSI_CLS): Inserted color=%02d r=%d phrase='%s'",
- color, r, phrasing[r]);
- ZF_LOGI_MEM(play, len, "mangle(ANSI_CLS) :");
-
- if (buffer_insert(play, len, sizeof(play), cp - play, display)) {
- len += strlen(display);
-
- ZF_LOGI_MEM(play, len, "mangle(ANSI_CLS) + :");
-
-
- need_render = 1;
-
- memcpy(work, play, len);
-
- i = cp - play;
-
-
- memset(work + i, ' ', strlen(display));
- } else {
- ZF_LOGD("insert failed [%s].", repr(display));
- }
- }
- }
- }
- }
- memcpy(work, play, len);
-
-
-
- const char replace_with = ' ';
- for (x = 0; x < len; x++) {
- termchar tc = console_char(&console, play[x]);
- int ansi = tc.in_ansi;
- if (ansi) {
- work[x] = replace_with;
- if (tc.ansi != START) {
- ZF_LOGD("ANSI type %d at %d", tc.ansi, x);
- }
- }
-
- if (buffer[x] == 0) {
- work[x] = replace_with;
- }
- }
-
-
- work[len] = 0;
- ZF_LOGV_MEM(work, len, "Work now:");
-
- 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(play + rxmatch[i].rm_so,
- rxmatch[i].rm_eo - rxmatch[i].rm_so);
- if (c) {
- mangled++;
- mangled_chars += c;
- }
- }
- if (random_activate(4)) {
- word_wrangler(play + rxmatch[i].rm_so,
- rxmatch[i].rm_eo - rxmatch[i].rm_so);
- }
- }
- }
-
-
- if (mangled)
- ZF_LOGI("Mangled %d word, %d chars (render %d)", mangled, mangled_chars,
- need_render);
- if (need_render) {
- ZF_LOGD_MEM(play, len, "Ready to render:");
-
-
- }
- if (need_render) {
- render(fd, play, len);
- } else {
- write(fd, play, len);
- };
- return need_render && mangled;
- }
- #endif
- int find_words(std::string &text, std::vector<std::pair<int, int>> &words) {
- words.clear();
- std::regex words_re("[a-zA-Z&]+( [a-zA-Z&]+)+");
- int count = 0;
- for (auto it = std::sregex_iterator(text.begin(), text.end(), words_re);
- it != std::sregex_iterator(); ++it) {
- if (it->length() > 3) {
- words.push_back(std::make_pair(it->position(), it->length()));
- count++;
- }
- }
- return count;
- }
- int mangle_clrscr(std::string &buffer, std::string &work, size_t pos) {
- static int ANSI_CLS_count = 0;
- ZF_LOGI("seen: ANSI_CLS");
- ANSI_CLS_count++;
- if (ANSI_CLS_count > 1) {
-
- struct console_details temp_console;
- memcpy(&temp_console, &console, sizeof(console));
- console_receive(&temp_console, buffer.substr(0, pos));
- std::string restore_color;
- restore_color.assign(color_restore(&temp_console));
- if (random_activate(3)) {
- std::ostringstream display;
- int needs_cls = 0;
- struct image {
- const char **lines;
- int size;
- int cls;
- int width;
- } images[] = {{ghost, sizeof(ghost) / sizeof(char *), 1, 0},
- {ghead, sizeof(ghead) / sizeof(char *), 1, 0},
- {wolf, sizeof(wolf) / sizeof(char *), 1, 0},
- {panther, sizeof(panther) / sizeof(char *), 1, 0},
- {bat, sizeof(bat) / sizeof(char *), 1, 0},
- {icu, sizeof(icu) / sizeof(char *), 0, 20},
- {skull, sizeof(skull) / sizeof(char *), 0, 19},
- {skullblink, sizeof(skullblink) / sizeof(char *), 0, 19}};
- static LastSeen last_files(2);
- int r;
- do {
- r = randint((sizeof(images) / sizeof(image)));
- } while (last_files.seen_before(r));
- std::string fgoto;
- if (!images[r].cls) {
- int x = 0, y = 0;
- x = randint(79 - images[r].width) + 1;
- y = randint(24 - images[r].size) + 1;
- display << "^f" << std::setfill('0') << std::setw(2) << x
- << std::setw(2) << y << "\x1b[1;1H";
- fgoto = display.str();
-
- display.str(std::string());
- display.clear();
-
-
- } else {
- fgoto.assign("^F");
- }
- needs_cls = images[r].cls;
-
-
-
-
-
-
-
- render_image(images[r].lines, images[r].size);
- display << (needs_cls ? "\x1b[2J" : "") << fgoto << restore_color
- << "^P3";
-
-
- std::string display_output = display.str();
- ZF_LOGI("mangle(ANSI_CLS): %d file inserted %s", r,
- repr(display_output.c_str()));
-
- buffer.insert(pos, display_output);
- work.insert(pos, std::string(display_output.size(), ' '));
- return 1;
- } else {
- if (random_activate(4)) {
- int r;
- std::ostringstream display;
- const char *phrasing[] = {
- "^R1Haha^P1ha^P1ha", "Poof!", "Got U", "Anyone there?",
- "^R1Knock, ^P1Knock",
-
- "^G0101^C07^S9Segmentation fault (core dumped)^P2"};
- static LastSeen last_phrasing(2);
- ZF_LOGI("mangle(ANSI_CLS)");
- do {
- r = randint(sizeof(phrasing) / sizeof(char *));
- } while (last_phrasing.seen_before(r));
- int color = randint(14) + 1;
- int x = randint(30) + 1;
- int y = randint(15) + 1;
-
- if (strncmp(phrasing[r], "^G", 2) == 0) {
- display << "^S3^P1" << phrasing[r] << "^S0^R0" << restore_color
- << "^P1^G0101";
-
-
-
-
- } else {
- display << "^G" << std::setw(2) << std::setfill('0') << x
- << std::setw(2) << y << "^S3^C" << std::setw(2) << color
- << "^P1" << phrasing[r] << "^S0^R0" << restore_color
- << "^P1^G0101";
-
-
-
- };
- std::string display_output = display.str();
-
-
-
-
- ZF_LOGD("mangle(ANSI_CLS): Inserted color=%02d r=%d phrase='%s'", color,
- r, phrasing[r]);
- ZF_LOGI("mangle(ANSI_CLS): %d %s", r, repr(display_output.c_str()));
-
- buffer.insert(pos, display_output);
- work.insert(pos, std::string(display_output.size(), ' '));
- return 1;
- }
- }
- }
- }
- int mangle(int fd, std::string &buffer) {
-
- ZF_LOGV_MEM(buffer.data(), buffer.size(), "mangle(%d): %lu bytes", fd,
- buffer.size());
- int need_render = 0;
- int mangled = 0;
- int mangled_chars = 0;
- static std::string work;
- static size_t work_size = 0;
- work.assign(buffer);
-
- if (work.capacity() != work_size) {
- ZF_LOGD("work cap %lu -> %lu", work_size, work.capacity());
- work_size = work.capacity();
- }
- const char *ANSI_CLS = "\x1b[2J";
- size_t pos = buffer.find(ANSI_CLS);
- if (pos != std::string::npos) {
- if (mangle_clrscr(buffer, work, pos)) {
- need_render = 1;
- }
- }
-
- static std::string text;
- static std::vector<int> text_offsets;
- size_t stri;
- text.clear();
- text_offsets.clear();
- for (stri = 0; stri < buffer.size(); ++stri) {
- termchar tc = console_char(&console, work[stri]);
- if (tc.in_ansi) {
- if (tc.ansi != START) {
-
-
- switch (tc.ansi) {
- case CURSOR:
- case CLEAR:
- case OTHER:
- text.append(1, '.');
- text_offsets.push_back(-1);
- break;
- case COLOR:
-
-
- break;
- }
- }
- } else {
-
- if (text.size() != text_offsets.size()) {
- ZF_LOGE("Error: text != text_offsets %lu != %lu", text.size(),
- text_offsets.size());
- }
- text.append(1, work[stri]);
- text_offsets.push_back(stri);
- }
- }
-
- ZF_LOGD_MEM(buffer.data(), buffer.size(), "Buffer:");
- ZF_LOGD_MEM(work.data(), work.size(), "Work:");
- ZF_LOGD_MEM(text.data(), text.size(), "Text Buffer:");
-
- std::ostringstream oss;
- int comma = 0;
- for (auto it = std::begin(text_offsets); it != std::end(text_offsets); ++it) {
- if (comma) {
- oss << ", ";
- };
- comma++;
- oss << *it;
- if (comma == 30) {
- std::string temp_output = oss.str();
- ZF_LOGD("Vector: %s", temp_output.c_str());
-
- oss.str(std::string());
- oss.clear();
- comma = 0;
- }
- }
- std::string vector_output = oss.str();
- ZF_LOGD("Vector: %s", vector_output.c_str());
-
- std::vector<std::pair<int, int>> words;
- int found = find_words(text, words);
- ZF_LOGD("Found %d word groups in text.", found);
- if (found > 0) {
- for (int i = 0; i < found; i++) {
- std::pair<int, int> pos_len = words[i];
-
- if (random_activate(8)) {
- int c = word_mangler(buffer, work, text, text_offsets, pos_len);
-
-
- if (c) {
- mangled++;
- mangled_chars += c;
- }
- }
- if (random_activate(4)) {
-
- }
- }
- }
- if (need_render) {
- render(fd, buffer);
- } else {
- write(fd, buffer.data(), buffer.size());
- }
- return need_render;
- }
|