#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define Request_Buffer_Size 8192
#define FINGERPORTSTRING "79"
#define DORKNAME   "brnstnd@kramden.acf.nyu.edu" /* this guy likes identd */

char *strip_whitespace();

void main()
{
  char MyBuffer[Request_Buffer_Size], WorkBuf[Request_Buffer_Size];
  char *localport, *foreignport, *strtmp;
  int  InputLen;

  fgets(MyBuffer, Request_Buffer_Size, stdin);

  strtmp = MyBuffer + strlen(MyBuffer) - 1;

  /* This next chunk of code should strip off the last character if it's
     \r or \n, and if it was, then it'll strip off the penultimate character
     if it's \r or \n. */

  if ((*strtmp == '\r') || (*strtmp == '\n')) {
    *strtmp = '\0';
    strtmp--;
    if ((*strtmp == '\r') || (*strtmp == '\n')) {
      *strtmp = '\0';
      strtmp--;
    }
  }
  
  strcpy(WorkBuf, MyBuffer);

  strtmp = strchr(WorkBuf, ',');
  if (strtmp == NULL) {
    printf("%s: ERROR : INVALID-PORT\r\n", MyBuffer);
    exit(1);
  }
  *strtmp = '\0';
  
  localport   = WorkBuf;
  foreignport = (strtmp + 1);

  localport   = strip_whitespace(localport);
  foreignport = strip_whitespace(foreignport);

  if ((*localport == '\0') || (*foreignport == '\0')) {
    printf("%s: ERROR: INVALID-PORT\r\n", MyBuffer);
    exit(1);
  }

/*  The server accepts simple text query requests of the form           */
/*                                                                      */
/*     <local-port>, <foreign-port>                                     */
/*                                                                      */
/*  where <local-port> is the TCP port (decimal) on the target (server) */
/*  system, and <foreign-port> is the TCP port (decimal) on the source  */
/*  (user) system.                                                      */


  if (strcmp(FINGERPORTSTRING, localport) == 0) {
    printf("%s , %s : USERID : OTHER : wwwuser\r\n", localport, foreignport);
  } else {
    printf("%s , %s : USERID : OTHER : %s\r\n", localport, foreignport,
	   DORKNAME);
  }

  exit(0);
}

char *strip_whitespace(char *str) {
  char *tmp;
  char *whitespace = " \t";

  while ((strchr(whitespace, *str) != NULL) && (*str != '\0')) {
    str++;
  }
  
  tmp = str;

  while ((strchr(whitespace, *tmp) == NULL) && (*tmp != '\0')) {
    tmp ++;
  }

  *tmp = '\0';

  return str;
}
