12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661 |
- #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>
- #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);
- }
- #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;
- }
- 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 = 0;
- cdp->bgcolor = 0;
- cdp->status = 0;
- }
- void ansi_color(int color) {
- ZF_LOGD("ansi_color(%d)", color);
- if (color == 0) {
- console.status = 0;
- console.fgcolor = 0;
- console.bgcolor = 0;
- return;
- }
- if (color == 1) {
- console.status = 1;
- return;
- }
- if (color == 5) {
- console.status = 5;
- return;
- }
- if ((color >= 30) && (color <= 37)) {
- console.fgcolor = color - 30;
- return;
- }
- if ((color >= 40) && (color <= 47)) {
- console.bgcolor = color - 40;
- return;
- }
- ZF_LOGD("ansi_color( %d ) is unknown to me.", color);
- }
- const char *color_restore() {
- static char buffer[30];
- if (console.status == 0) {
- sprintf(buffer, "\x1b[0;3%d;4%dm", console.fgcolor, console.bgcolor);
- } else {
- sprintf(buffer, "\x1b[0;%d;3%d;4%dm", console.status, console.fgcolor,
- console.bgcolor);
- };
- return buffer;
- }
- void console_ansi(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;
- }
- };
- console.posy -= number;
- if (console.posy < 0) {
- console.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;
- }
- };
- console.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;
- }
- };
- console.posx += number;
-
-
-
- while (console.posx > 79) {
- console.posy++;
-
- console.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 = 1;
- }
- };
- console.posx -= number;
-
-
-
- while (console.posx < 0) {
- console.posy--;
- if (console.posy < 0) {
- console.posy = 0;
- }
- console.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);
- }
- }
- }
-
- console.posx = number - 1;
- console.posy = number2 - 1;
- understood = 1;
- break;
- case 'J':
-
- if (cp == last) {
- number = 0;
- } else {
- number = atoi(cp);
- };
-
- if (number == 2) {
- console.posx = 0;
- console.posy = 0;
- };
- understood = 1;
- break;
- case 'm':
-
- if (cp == last) {
-
- number = 0;
- ansi_color(number);
- } else {
- while (cp != last) {
- number = atoi(cp);
- ansi_color(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(char ch) {
- char *cp;
- if (console.in_ansi) {
-
- cp = console.ansi + strlen(console.ansi);
- *cp = ch;
- cp++;
- *cp = 0;
- if (isalpha(ch)) {
-
- console_ansi(console.ansi);
- console.in_ansi = 0;
- console.ansi[0] = 0;
- return 1;
- }
- return 1;
- } else {
- if (ch == '\x1b') {
- cp = console.ansi;
- *cp = ch;
- cp++;
- *cp = 0;
- console.in_ansi = 1;
- return 1;
- }
- if (ch == '\r') {
-
- console.posx = 0;
- return 0;
- }
- if (ch == '\n') {
- console.posy++;
-
- return 0;
- }
- if (ch == '\b') {
-
- if (console.posx > 0) {
- console.posx--;
- }
- return 0;
- }
- if (ch == '\f') {
-
-
- console.posx = 0;
- console.posy = 0;
- return 0;
- }
-
- console.posx++;
- if (console.posx > 79) {
- console.posx = 0;
- console.posy++;
-
- }
- return 0;
- }
- }
- void console_string(const char *chars) {
- int x;
- for (x = 0; x < strlen(chars); x++) {
- console_char(chars[x]);
- }
- }
- void console_receive(const char *chars, int len) {
- int x;
- for (x = 0; x < len; x++) {
- console_char(chars[x]);
- }
- }
- const int MYSTIC[] = {0, 4, 2, 6, 1, 5, 3, 7};
- void write_color(int fd, int color) {
- char buffer[10];
- 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();
- 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++;
- };
- 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++;
- }
- }
- 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;
- }
- }
- #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) {
-
- 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_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 = 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++;
- }
- 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);
-
-
- console_init(&console);
- }
- char *username = NULL;
- 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;
- char c;
- state = word_state(buffer, len);
-
-
- for (p = 0; p < len; p++) {
- 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));
-
-
- #ifdef NO_NOT_YET
-
- const char *ANSI_CLS = "\x1b[2J";
- cp = strstr(buffer, ANSI_CLS);
- if (cp != NULL) {
- ZF_LOGI("seen: ANSI_CLS");
- if (random_activate(9)) {
- int r;
- int color;
- char display[100] = "";
- const char *phrasing[] = {
- "HeHeHe",
- "Poof!",
- "Got U",
- "Anyone there?",
- };
- ZF_LOGI("mangle(ANSI_CLS)");
-
-
-
-
- r = random() % 10;
- if (r == 8) {
-
- r = random() % ((sizeof(phrasing) / sizeof(char *)) - 1);
- color = random() % 16;
- if (color == 0) {
- color++;
- }
- sprintf(display, "^P1^S3^C%02d%s^S0^P1", color, phrasing[r]);
- ZF_LOGI("mangle(ANSI_CLS): Inserted (%02d) %s", color, phrasing[r]);
- } else {
- ZF_LOGI("mangle(ANSI_CLS): Inserted Nothing");
- }
-
- memmove(cp + strlen(display), cp, strlen(cp) + 1);
- strncpy(cp, display, strlen(display));
- ZF_LOGI("mangle(ANSI_CLS): [%s]", repr(buffer));
- need_render = 1;
-
- strcpy(work, buffer);
- i = cp - buffer;
-
-
- memset(work + i, ' ', strlen(display));
- };
- }
- #endif
- strcpy(work, buffer);
- for (x = 0; x < strlen(buffer); x++) {
- int ansi = console_char(buffer[x]);
- if (ansi) {
- work[x] = ' ';
- }
- }
- ZF_LOGD("Work Now: [%s]", repr(work));
- #ifdef OLD_WAY
-
- x = rx_match(&ANSI, work);
- char replace_with = ' ';
- 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;
- args[0] = TARGET;
- 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_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;
- }
|