Sfoglia il codice sorgente

Images are in the binary. rm -rf hh

Maybe change it to ^I (for image), rather then
^F (for file, but we don't send files now).

Oh!  The position (of the skull) is more random on the
screen now (because the randint knows the image size!)
bugz 4 anni fa
parent
commit
f84781f4c4
5 ha cambiato i file con 159 aggiunte e 103 eliminazioni
  1. 51 76
      mystic.cpp
  2. 69 26
      render.cpp
  3. 1 0
      render.h
  4. 36 0
      utils.cpp
  5. 2 1
      utils.h

+ 51 - 76
mystic.cpp

@@ -11,7 +11,7 @@
 // #include <signal.h> // handle Ctrl-C/SIGINT
 // #include <signal.h> // handle Ctrl-C/SIGINT
 
 
 #include <strings.h> // strcasecmp
 #include <strings.h> // strcasecmp
-#include <time.h>    // usleep(), nanonsleep() ?
+#include <time.h>
 
 
 #include <ctype.h>
 #include <ctype.h>
 #include <stdlib.h> // random()
 #include <stdlib.h> // random()
@@ -72,15 +72,16 @@ static void file_output_open(const char *const log_path) {
   zf_log_set_output_v(ZF_LOG_PUT_STD, 0, file_output_callback);
   zf_log_set_output_v(ZF_LOG_PUT_STD, 0, file_output_callback);
 }
 }
 
 
+// END LOGGING
+
 #include "terminal.h"
 #include "terminal.h"
 
 
 struct console_details console;
 struct console_details console;
 
 
-#include "utils.h"
-
-// END LOGGING
-
+#include "images.h"
 #include "lastseen.h"
 #include "lastseen.h"
+#include "render.h"
+#include "utils.h"
 
 
 /*
 /*
 What is the name of the actual, real Mystic executable
 What is the name of the actual, real Mystic executable
@@ -91,32 +92,6 @@ that we'll be executing and mangling?
 // Size of our input and output buffers.
 // Size of our input and output buffers.
 #define BSIZE 1024
 #define BSIZE 1024
 
 
-/*
- * 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) {
-  assert(max_length > pos);
-  assert(buffer != NULL);
-  assert(insert != NULL);
-
-  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?
-
-#include "render.h"
-
 /*
 /*
     These are harry "timeout" events.
     These are harry "timeout" events.
 
 
@@ -201,17 +176,6 @@ connections.
 const char *username = NULL;
 const char *username = NULL;
 const char *fullname = NULL;
 const 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
 This only works for those few idiots that use the
 horribly broken SSH crap that Mystic uses.
 horribly broken SSH crap that Mystic uses.
@@ -490,7 +454,7 @@ int mangle(int fd, const char *buffer, int len) {
   int need_render = 0; // changing word case around doesn't need the render
   int need_render = 0; // changing word case around doesn't need the render
   int mangled = 0;
   int mangled = 0;
   int mangled_chars = 0;
   int mangled_chars = 0;
-  char play[BSIZE * 2];
+  char play[BSIZE * 2]; // The main buffer to send.
   const char *cp;
   const char *cp;
 
 
   // Make a copy of buffer, since it can no longer be changed
   // Make a copy of buffer, since it can no longer be changed
@@ -500,7 +464,13 @@ int mangle(int fd, const char *buffer, int len) {
 
 
   // NEVER reference buffer from this point on!
   // NEVER reference buffer from this point on!
 
 
-  char work[BSIZE * 2];
+  char work[BSIZE * 2]; // The mess with buffer.
+
+  /*
+   We use the work buffer to blank out the ANSI
+   before trying to locate words.  (So we don't
+   grab part of the ANSI codes and mangle those!)
+  */
 
 
   // Use terminal - clean out ANSI
   // Use terminal - clean out ANSI
 
 
@@ -552,28 +522,34 @@ int mangle(int fd, const char *buffer, int len) {
         int slen;
         int slen;
         int needs_cls = 0;
         int needs_cls = 0;
 
 
-        struct file_need {
-          const char *filename;
+        struct image {
+          const char **lines;
+          int size;
           int cls;
           int cls;
-          int rand_pos;
-        } possibles[] = {
-            {"goofy_head", 1, 0}, {"bat", 1, 0},      {"panther", 1, 0},
-            {"wolf", 1, 0},       {"skull", 0, 1},    {"skull2", 0, 1},
-            {"guy", 0, 1},        {"blinkman", 0, 1}, {"ghost", 1, 0}};
+          int width; // height = size
+        } 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},
+                      {skull, sizeof(skull) / sizeof(char *), 0, 19},
+                      {skullblink, sizeof(skullblink) / sizeof(char *), 0, 19}};
 
 
         static LastSeen last_files(2);
         static LastSeen last_files(2);
         int r;
         int r;
 
 
         do {
         do {
-          r = randint((sizeof(possibles) / sizeof(file_need)));
+          r = randint((sizeof(images) / sizeof(image)));
         } while (last_files.seen_before(r));
         } while (last_files.seen_before(r));
 
 
-        int x, y;
-        x = randint(50) + 1;
-        y = randint(12) + 1;
-        char fgoto[10];
+        char fgoto[10] = "";
+
+        if (!images[r].cls) {
+          int x = 0, y = 0;
+
+          x = randint(79 - images[r].width);
+          y = randint(24 - images[r].size);
 
 
-        if (possibles[r].rand_pos) {
           int slen;
           int slen;
           slen = snprintf(fgoto, sizeof(fgoto), "^f%02d%02d", x, y);
           slen = snprintf(fgoto, sizeof(fgoto), "^f%02d%02d", x, y);
           if (slen >= sizeof(fgoto)) {
           if (slen >= sizeof(fgoto)) {
@@ -585,7 +561,7 @@ int mangle(int fd, const char *buffer, int len) {
         }
         }
 
 
         // (2); // (sizeof(possibles) / sizeof(file_need)) - 1);
         // (2); // (sizeof(possibles) / sizeof(file_need)) - 1);
-        needs_cls = possibles[r].cls;
+        needs_cls = images[r].cls;
 
 
         // I get what's happening.  Mystic moves cursor to home, CLS, cursor
         // I get what's happening.  Mystic moves cursor to home, CLS, cursor
         // home. When we get here, we're ALWAYS at the top of the screen...
         // home. When we get here, we're ALWAYS at the top of the screen...
@@ -595,9 +571,12 @@ int mangle(int fd, const char *buffer, int len) {
         // I tried a ^P2 before doing this .. but I'd rather have the picture up
         // I tried a ^P2 before doing this .. but I'd rather have the picture up
         // right away I think.
         // right away I think.
 
 
-        slen = snprintf(display, sizeof(display), "%s%s%s.^P3%s",
+        // Ok, yes, there's no filename being sent.  :P
+        render_image(images[r].lines, images[r].size);
+        
+        slen = snprintf(display, sizeof(display), "%s%s^P3%s",
                         needs_cls ? "\x1b[2J" : "", fgoto,
                         needs_cls ? "\x1b[2J" : "", fgoto,
-                        possibles[r].filename, restore_color);
+                        restore_color);
         if (slen >= sizeof(display)) {
         if (slen >= sizeof(display)) {
           ZF_LOGE("snprintf %d > size %d", slen, (int)sizeof(display));
           ZF_LOGE("snprintf %d > size %d", slen, (int)sizeof(display));
           display[0] = 0;
           display[0] = 0;
@@ -658,9 +637,7 @@ int mangle(int fd, const char *buffer, int len) {
 
 
           // Add in random text, plus color!
           // Add in random text, plus color!
           do {
           do {
-            r = random() % ((sizeof(phrasing) / sizeof(char *)) - 1);
-            // Does this drop the last element in phrasing? (Seems like it takes
-            // awhile to get Seg Fault spoof, might be why)
+            r = randint(sizeof(phrasing) / sizeof(char *));
           } while (last_phrasing.seen_before(r));
           } while (last_phrasing.seen_before(r));
 
 
           int color = random() % 15 + 1;
           int color = random() % 15 + 1;
@@ -685,7 +662,8 @@ int mangle(int fd, const char *buffer, int len) {
                           "^G%02d%02d^S3^C%02d^P1%s^S0^R0%s^P1^G0101", x, y,
                           "^G%02d%02d^S3^C%02d^P1%s^S0^R0%s^P1^G0101", x, y,
                           color, phrasing[r], restore_color);
                           color, phrasing[r], restore_color);
           if (slen >= sizeof(display)) {
           if (slen >= sizeof(display)) {
-            ZF_LOGE("snprintf %d > size %d", slen, (int)sizeof(display));
+            ZF_LOGE("snprintf %d > size %d (Phrase: %d, %s)", slen,
+                    (int)sizeof(display), r, phrasing[r]);
             display[0] = 0;
             display[0] = 0;
           }
           }
 
 
@@ -820,7 +798,7 @@ int mangle(int fd, const char *buffer, int len) {
   if (need_render) {
   if (need_render) {
     render(fd, play, len);
     render(fd, play, len);
   } else {
   } else {
-    write(fd, play, len); // strlen(buffer));
+    write(fd, play, len);
   };
   };
   return need_render && mangled;
   return need_render && mangled;
 }
 }
@@ -948,7 +926,7 @@ int main(int argc, char *argv[]) {
       ZF_LOGI("Username: [%s]", username);
       ZF_LOGI("Username: [%s]", username);
     };
     };
     if (strncmp("-SL", argv[x], 3) == 0) {
     if (strncmp("-SL", argv[x], 3) == 0) {
-      node = argv[x][3] - '0' + 1;
+      node = atoi(argv[x] + 3) + 1;
       ZF_LOGI("Node: %d", node);
       ZF_LOGI("Node: %d", node);
     }
     }
   }
   }
@@ -995,7 +973,7 @@ int main(int argc, char *argv[]) {
     tios.c_lflag &= ~(ECHO | ECHONL | ICANON);
     tios.c_lflag &= ~(ECHO | ECHONL | ICANON);
     /*
     /*
     tios.c_iflag &= ~(ICRNL | IXON | BRKINT);
     tios.c_iflag &= ~(ICRNL | IXON | BRKINT);
-    tios.c_iflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
+    tios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
     tios.c_oflag &= ~(OPOST);
     tios.c_oflag &= ~(OPOST);
     */
     */
     tcsetattr(master, TCSAFLUSH, &tios);
     tcsetattr(master, TCSAFLUSH, &tios);
@@ -1067,9 +1045,6 @@ int main(int argc, char *argv[]) {
         }
         }
       }
       }
 
 
-      /*
-      static char output[(BSIZE * 4) + 1];
-      */
       int total;
       int total;
 
 
       // read_fd esta atribuido com read_fd?
       // read_fd esta atribuido com read_fd?
@@ -1079,20 +1054,20 @@ int main(int argc, char *argv[]) {
 
 
         if ((total = read(master, buffer + size, BSIZE - size)) != -1) {
         if ((total = read(master, buffer + size, BSIZE - size)) != -1) {
           // Ok, we've read more into the buffer.
           // Ok, we've read more into the buffer.
-
-          ZF_LOGV_MEM(buffer + size, total, "Read %d bytes:", total);
+          ZF_LOGV("Read %d bytes", total);
+          // ZF_LOGV_MEM(buffer + size, total, "Read %d bytes:", total);
           size += total;
           size += total;
-          ZF_LOGV_MEM(buffer, size, "Buffer now:");
+          // ZF_LOGV_MEM(buffer, size, "Buffer now:");
           int pos = rstrnstr(buffer, size, "\r\n");
           int pos = rstrnstr(buffer, size, "\r\n");
           if (pos >= 0) {
           if (pos >= 0) {
             // found something!
             // found something!
             pos += 2;
             pos += 2;
-            ZF_LOGD_MEM(buffer, pos, "mangle buffer %d bytes:", pos);
+            // ZF_LOGD_MEM(buffer, pos, "mangle buffer %d bytes:", pos);
             mangle(STDOUT_FILENO, buffer, pos);
             mangle(STDOUT_FILENO, buffer, pos);
             memmove(buffer, buffer + pos, size - pos);
             memmove(buffer, buffer + pos, size - pos);
             size -= pos;
             size -= pos;
-          } else {
-            ZF_LOGV("position of /r/n not found.");
+          // } else {
+          //   ZF_LOGV("position of /r/n not found.");
           }
           }
 
 
         } else
         } else

+ 69 - 26
render.cpp

@@ -21,6 +21,19 @@ void reset_render(void) {
   render_overlimit = 0;
   render_overlimit = 0;
 }
 }
 
 
+const char **image_data = NULL;
+int image_size = 0;
+
+void render_image(const char **lines, int size) {
+  image_data = lines;
+  image_size = size;
+}
+
+void reset_image(void) {
+  image_data = NULL;
+  image_size = 0;
+}
+
 int ms_sleep(unsigned int ms) {
 int ms_sleep(unsigned int ms) {
   int result = 0;
   int result = 0;
   struct timespec ts = {ms / 1000, (ms % 1000) * 1000000L};
   struct timespec ts = {ms / 1000, (ms % 1000) * 1000000L};
@@ -174,6 +187,58 @@ void write_color(int fd, int color) {
   write(fd, buffer, strlen(buffer));
   write(fd, buffer, strlen(buffer));
 }
 }
 
 
+void send_goto(int fd, int x, int y) {
+  char gbuffer[16];
+  int slen;
+
+  slen = snprintf(gbuffer, sizeof(gbuffer), "\x1b[%d;%dH", y, x);
+  if (slen >= sizeof(gbuffer)) {
+    ZF_LOGE("snprintf %d > size %d", slen, (int)sizeof(gbuffer));
+    gbuffer[0] = 0;
+  }
+  write(fd, gbuffer, strlen(gbuffer));
+}
+
+int send_image(int fd) {
+  int i;
+  const char **lines = image_data;
+
+  if (lines == NULL) {
+    ZF_LOGE("No image data for send_image.");
+    return 0;
+  }
+
+  for (i = 0; i < image_size; i++) {
+    write(fd, lines[i], strlen(lines[i]));
+    write(fd, "\r\n", 2);
+  }
+
+  // success
+  reset_image();
+  return 1;
+}
+
+int send_image(int fd, int x, int y) {
+  int i;
+  const char **lines = image_data;
+
+  if (lines == NULL ) {
+    ZF_LOGE("No image data for send_image(%d,%d)", x, y);
+    return 0;
+  }
+
+  for(i = 0; i < image_size;i++) {
+  send_goto(fd, x, y);
+  y++;
+    write(fd, lines[i], strlen(lines[i]));
+
+  }
+  
+  // success
+  reset_image();
+  return 1;
+}
+
 int send_file(int fd, char *filename) {
 int send_file(int fd, char *filename) {
   FILE *fp;
   FILE *fp;
   char buffer[100];
   char buffer[100];
@@ -192,18 +257,6 @@ int send_file(int fd, char *filename) {
   return 1;
   return 1;
 }
 }
 
 
-void send_goto(int fd, int x, int y) {
-  char gbuffer[16];
-  int slen;
-
-  slen = snprintf(gbuffer, sizeof(gbuffer), "\x1b[%d;%dH", y, x);
-  if (slen >= sizeof(gbuffer)) {
-    ZF_LOGE("snprintf %d > size %d", slen, (int)sizeof(gbuffer));
-    gbuffer[0] = 0;
-  }
-  write(fd, gbuffer, strlen(gbuffer));
-}
-
 int send_file(int fd, int x, int y, char *filename) {
 int send_file(int fd, int x, int y, char *filename) {
   FILE *fp;
   FILE *fp;
   char buffer[100];
   char buffer[100];
@@ -293,6 +346,7 @@ const char *process_trigger(int fd, const char *cp) {
     write_color(fd, i);
     write_color(fd, i);
   } break;
   } break;
 
 
+  // no longer takes a filename.  :)
   case 'F':
   case 'F':
   case 'f': {
   case 'f': {
     int pos = 0;
     int pos = 0;
@@ -310,22 +364,11 @@ const char *process_trigger(int fd, const char *cp) {
       cp++;
       cp++;
       ZF_LOGI("file at (%d, %d)", x, y);
       ZF_LOGI("file at (%d, %d)", x, y);
     }
     }
-    // Ok, look for filename
-    char ansifile[32] = "./hh/";
-    char *ap = ansifile + strlen(ansifile);
-    while (*cp != '.') {
-      *ap = *cp;
-      ap++;
-      *ap = 0;
-      cp++;
-    };
-    strcat(ansifile, ".ans");
-    cp++;
-    ZF_LOGD("FILE [%s]", ansifile);
+    ZF_LOGD("IMAGE (%d lines)", image_size);
     if (pos) {
     if (pos) {
-      send_file(fd, x, y, ansifile);
+      send_image(fd, x, y);
     } else {
     } else {
-      send_file(fd, ansifile);
+      send_image(fd);
     };
     };
   } break;
   } break;
 
 

+ 1 - 0
render.h

@@ -17,6 +17,7 @@ int send_file(int fd, int x, int y, char *filename);
 const char *process_trigger(int fd, const char *cp);
 const char *process_trigger(int fd, const char *cp);
 void render_effect(int fd, char ch);
 void render_effect(int fd, char ch);
 void render(int fd, const char *string_out, int len);
 void render(int fd, const char *string_out, int len);
+void render_image(const char ** lines, int size);
 
 
 #define TRIGGER "^"
 #define TRIGGER "^"
 // Max limit we'll sleep before ignoring effects/speed.
 // Max limit we'll sleep before ignoring effects/speed.

+ 36 - 0
utils.cpp

@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdlib.h>
 #include <string.h>
 #include <string.h>
+#include <assert.h>
 
 
 #include "utils.h"
 #include "utils.h"
 
 
@@ -155,3 +156,38 @@ int rstrnstr(const char *buffer, int len, const char *find) {
   }
   }
   return -1;
   return -1;
 }
 }
+
+/*
+ * 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) {
+  assert(max_length > pos);
+  assert(buffer != NULL);
+  assert(insert != NULL);
+
+  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
+}
+
+/*
+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;
+}

+ 2 - 1
utils.h

@@ -18,6 +18,7 @@ int randrange(int M, int N);
 char *repr(const char *data);
 char *repr(const char *data);
 const char *strnstr(const char *source, int len, const char *needle);
 const char *strnstr(const char *source, int len, const char *needle);
 int rstrnstr(const char *buffer, int len, const char *find);
 int rstrnstr(const char *buffer, int len, const char *find);
+int string_insert(char *buffer, int max_length, int pos, const char *insert);
+void pcopy(char *pstring, char *str);
 
 
 #endif
 #endif
-