|  | @@ -7,6 +7,8 @@
 | 
	
		
			
				|  |  |  #include <termios.h>
 | 
	
		
			
				|  |  |  #include <unistd.h>
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +#include <fstream>
 | 
	
		
			
				|  |  | +#include <iostream>
 | 
	
		
			
				|  |  |  #include <sstream>
 | 
	
		
			
				|  |  |  #include <string>
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -114,6 +116,7 @@ connections.
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  |  string username;
 | 
	
		
			
				|  |  |  string fullname;
 | 
	
		
			
				|  |  | +int node;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /*
 | 
	
		
			
				|  |  |  This only works for those few idiots that use the
 | 
	
	
		
			
				|  | @@ -146,6 +149,76 @@ int locate_user(const char *alias) {
 | 
	
		
			
				|  |  |    return 1;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +ifstream logfile;
 | 
	
		
			
				|  |  | +streampos log_pos;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +void open_mystic_log(void) {
 | 
	
		
			
				|  |  | +  string mystic_logfile;
 | 
	
		
			
				|  |  | +  {
 | 
	
		
			
				|  |  | +    ostringstream buffer;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    buffer << "logs/node" << node << ".log";
 | 
	
		
			
				|  |  | +    mystic_logfile = buffer.str();
 | 
	
		
			
				|  |  | +  };
 | 
	
		
			
				|  |  | +  logfile.open(mystic_logfile, ios_base::in | ios_base::ate);
 | 
	
		
			
				|  |  | +  // Ok, we're at the end of the file.  Or should be.
 | 
	
		
			
				|  |  | +  if (logfile.is_open()) {
 | 
	
		
			
				|  |  | +    ZF_LOGD("Log %s open", (const char *)mystic_logfile.c_str());
 | 
	
		
			
				|  |  | +    log_pos = logfile.tellg();
 | 
	
		
			
				|  |  | +  } else {
 | 
	
		
			
				|  |  | +    ZF_LOGE("Failed to open: %s", (const char *)mystic_logfile.c_str());
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +void scan_mystic_log(void) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  if (logfile.is_open()) {
 | 
	
		
			
				|  |  | +    int again = 0;
 | 
	
		
			
				|  |  | +    do {
 | 
	
		
			
				|  |  | +      string line = find_new_text(logfile, log_pos);
 | 
	
		
			
				|  |  | +      if (line.empty())
 | 
	
		
			
				|  |  | +        return;
 | 
	
		
			
				|  |  | +      again = 1;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +      ZF_LOGD("mystic log: %s", (const char *)line.c_str());
 | 
	
		
			
				|  |  | +      // Ok, we have a line, look for interesting details
 | 
	
		
			
				|  |  | +      if (line.find("New user application") != string::npos) {
 | 
	
		
			
				|  |  | +        ZF_LOGE("New User");
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +      int pos;
 | 
	
		
			
				|  |  | +      pos = line.find("Created Account: ");
 | 
	
		
			
				|  |  | +      if (pos != string::npos) {
 | 
	
		
			
				|  |  | +        pos += 18 - 1;
 | 
	
		
			
				|  |  | +        // Ok, find the end '#'
 | 
	
		
			
				|  |  | +        int len = line.find('#', pos);
 | 
	
		
			
				|  |  | +        if (len != string::npos) {
 | 
	
		
			
				|  |  | +          username = line.substr(pos, len - pos - 1);
 | 
	
		
			
				|  |  | +          ZF_LOGE("New User: %s", (const char *)username.c_str());
 | 
	
		
			
				|  |  | +          // once we know this works -- lookup user's record
 | 
	
		
			
				|  |  | +          locate_user(username.c_str());
 | 
	
		
			
				|  |  | +          ZF_LOGD("Username: [%s] A.K.A. [%s]", (const char *)username.c_str(),
 | 
	
		
			
				|  |  | +                  (const char *)fullname.c_str());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +      pos = line.find(" logged in");
 | 
	
		
			
				|  |  | +      if (pos != string::npos) {
 | 
	
		
			
				|  |  | +        --pos;
 | 
	
		
			
				|  |  | +        int len = line.rfind(' ', pos);
 | 
	
		
			
				|  |  | +        if (len != string::npos) {
 | 
	
		
			
				|  |  | +          len++;
 | 
	
		
			
				|  |  | +          username = line.substr(len, pos + 1 - len);
 | 
	
		
			
				|  |  | +          ZF_LOGE("User: %s", (const char *)username.c_str());
 | 
	
		
			
				|  |  | +          // verify this works, lookup
 | 
	
		
			
				|  |  | +          locate_user(username.c_str());
 | 
	
		
			
				|  |  | +          ZF_LOGD("Username: [%s] A.K.A. [%s]", (const char *)username.c_str(),
 | 
	
		
			
				|  |  | +                  (const char *)fullname.c_str());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +    } while (again);
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  /*
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  This is done.  :D  My buffering system works with stack'em.
 | 
	
	
		
			
				|  | @@ -167,22 +240,23 @@ Here's the "plan":
 | 
	
		
			
				|  |  |    NOT EMPTY:
 | 
	
		
			
				|  |  |        // we're in luck!  The last parameter is time interval/timeout.  :D
 | 
	
		
			
				|  |  |        timeout.tv_sec = 0;
 | 
	
		
			
				|  |  | -      timeout.tv_usec = 10;   // Wild Guess Here?  Maybe higher, maybe lower?
 | 
	
		
			
				|  |  | -      time_idle = 0;
 | 
	
		
			
				|  |  | +      timeout.tv_usec = 10;   // Wild Guess Here?  Maybe higher, maybe
 | 
	
		
			
				|  |  | +lower? time_idle = 0;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    ON READ:
 | 
	
		
			
				|  |  |      read/append to current buffer.
 | 
	
		
			
				|  |  | -    We can't use nulls -- what if they are using ZModem, there's nulls in the
 | 
	
		
			
				|  |  | -file! Look for trailing  / the very last "\r\n".
 | 
	
		
			
				|  |  | +    We can't use nulls -- what if they are using ZModem, there's nulls in
 | 
	
		
			
				|  |  | +the file! Look for trailing  / the very last "\r\n".
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      (I could mangle/chunk it line by line.  But I'm not sure I'd need to do
 | 
	
		
			
				|  |  |  that.)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    Optional "mangle" buffer up to that very point -- and send up to that point.
 | 
	
		
			
				|  |  | +    Optional "mangle" buffer up to that very point -- and send up to that
 | 
	
		
			
				|  |  | +point.
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    Option #2:  Maybe we send everything if program has been running for under
 | 
	
		
			
				|  |  | -    20 seconds.  This would allow the ANSI detect to not get screwed up by this
 | 
	
		
			
				|  |  | -    new idea.
 | 
	
		
			
				|  |  | +    Option #2:  Maybe we send everything if program has been running for
 | 
	
		
			
				|  |  | +under 20 seconds.  This would allow the ANSI detect to not get screwed up by
 | 
	
		
			
				|  |  | +this new idea.
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    ON TIMEOUT:
 | 
	
		
			
				|  |  |      if time_idle:
 | 
	
	
		
			
				|  | @@ -194,8 +268,9 @@ that.)
 | 
	
		
			
				|  |  |        Maybe this is a prompt, and there won't be a \r\n.
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  This allows for cleaner process of "lines" of buffer.  We shouldn't break
 | 
	
		
			
				|  |  | -in the midDLE OF A WORD.  Downside is that we sit on buffer contents a little
 | 
	
		
			
				|  |  | -while / some amount of time -- which will add some lag to prompts showing up.
 | 
	
		
			
				|  |  | +in the midDLE OF A WORD.  Downside is that we sit on buffer contents a
 | 
	
		
			
				|  |  | +little while / some amount of time -- which will add some lag to prompts
 | 
	
		
			
				|  |  | +showing up.
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  (LAG?  Are you kidding?)
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -225,7 +300,7 @@ ZModem:
 | 
	
		
			
				|  |  |  int main(int argc, char *argv[]) {
 | 
	
		
			
				|  |  |    int master;
 | 
	
		
			
				|  |  |    pid_t pid;
 | 
	
		
			
				|  |  | -  int node = -1;
 | 
	
		
			
				|  |  | +  node = -1;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    init_harry();
 | 
	
		
			
				|  |  |    srandom(time(NULL));
 | 
	
	
		
			
				|  | @@ -281,7 +356,6 @@ int main(int argc, char *argv[]) {
 | 
	
		
			
				|  |  |      return 2;
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |    string logfile;
 | 
	
		
			
				|  |  |    {
 | 
	
		
			
				|  |  |      ostringstream buffer;
 | 
	
	
		
			
				|  | @@ -299,6 +373,7 @@ int main(int argc, char *argv[]) {
 | 
	
		
			
				|  |  |      ZF_LOGD("Username: [%s] A.K.A. [%s]", (const char *)username.c_str(),
 | 
	
		
			
				|  |  |              (const char *)fullname.c_str());
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  | +  open_mystic_log();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    pid = forkpty(&master, NULL, NULL, NULL);
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -332,6 +407,7 @@ int main(int argc, char *argv[]) {
 | 
	
		
			
				|  |  |      struct termios tios, orig1;
 | 
	
		
			
				|  |  |      struct timeval timeout;
 | 
	
		
			
				|  |  |      time_t last_event = 0; // time(NULL);
 | 
	
		
			
				|  |  | +    time_t last_logscan = time(NULL);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      ZF_LOGD("starting");
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -405,6 +481,14 @@ int main(int argc, char *argv[]) {
 | 
	
		
			
				|  |  |          time_idle = 0;
 | 
	
		
			
				|  |  |        }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +      if (last_logscan < time(NULL)) {
 | 
	
		
			
				|  |  | +        scan_mystic_log();
 | 
	
		
			
				|  |  | +        if (username.empty())
 | 
	
		
			
				|  |  | +          last_logscan = time(NULL) + 2;
 | 
	
		
			
				|  |  | +        else
 | 
	
		
			
				|  |  | +          last_logscan = time(NULL) + 30;
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |        if (select(master + 1, &read_fd, &write_fd, &except_fd, &timeout) == 0) {
 | 
	
		
			
				|  |  |          ZF_LOGI("TIMEOUT");
 | 
	
		
			
				|  |  |          // This means timeout!
 |