#include <stdio.h>      
#include <sys/file.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include "inet.h"

#define CR		'\015'
#define LF		'\012'
#define EOM             ".\r\n"
#define EOM_LEN         3
#define CRLF            "\015\012"
#define SOCKBUF		4096
#define FALSE           0
#define TRUE            1

char            outbuf[SOCKBUF];
char            inbuf[SOCKBUF];

char           *host, *port;
static int      ids[6], level, node, pipssock,list[600],listsize;
static char    *subj[6], *title, *line, *cp;


main()
{				/* parsing man items to add as docs
				 * to pips */

    FILE           *fopen(), *ifp, *ofp;
    char           *malloc();
    int             len, i,x;
    title = malloc(80);
    line = malloc(120);
    /* establish connect with pips */
    host = "alex";
    port = "9000";

    pipssock = inet_establish_connection(host, port, 0);
    if (pipssock < 0) {
	puts("Fatal error: could not establish connection.");
	exit(1);
    }
    get_msg(pipssock, inbuf, SOCKBUF);
printf("banner=%s\n",inbuf);
bzero(inbuf,strlen(inbuf));
    send_msg(pipssock,"p:thorne:tool",13); 
    get_msg(pipssock, inbuf, SOCKBUF);
printf("msg=<%s>\n",inbuf);
bzero(inbuf,strlen(inbuf));

    ofp = fopen("rfc.list", "r"); /* listing of the files in the rfc dir */
    x = 0;
    while (fgets(line, 80, ofp) != 0) {
          len = strlen(line);
	  line[len-1] = '\0';
	  list[x] = atoi(line+3);
	  printf("%d\n",list[x]);
	  x++;
    }
    listsize = --x;
    fclose(ofp);
    ifp = fopen("RFC-BY-TITLE.TXT", "r");
    while (fgets(line, 80, ifp) != 0) {
          len = strlen(line);
	  line[len-1] = '\0';
		add_subj();
    }
    fclose(ifp);
    close(pipssock);
}
add_subj()
{
    char            transline[150];
    char            linktrans[50];
    char            ret_msg[50];
    char            num[7],*title; 
    int             nm,found,x;

   bzero(transline,120);
   bzero(linktrans,50);
   nm = atoi(line);
   found = FALSE;
   for (x = 0; x < listsize; x++) /* testing to see if the file exists */
     if (list[x] == nm) {
       found = TRUE;
       break; }
  if (found) {
   sprintf(num,"%d",nm);
   title = line+6;

sprintf(transline,"a:9:4:9:RFC:%s:RFC:thorne:/afs/athena.mit.edu/contrib/rfc/RFC%s.TXT",title,num);
    printf("%s|%d\n", transline,strlen(transline));
 
   send_msg(pipssock,transline,strlen(transline));
    get_msg(pipssock,ret_msg,50);
    printf("ret msg <%s>\n",ret_msg);
    node = atoi(ret_msg);
    sprintf(linktrans,"l:22078:%d",node); 
    printf("%s\n",linktrans); 
    bzero(ret_msg,50);
    send_msg(pipssock,linktrans,strlen(linktrans));
    get_msg(pipssock,ret_msg,50);
    printf("ret msg2 <%s>\n",ret_msg);
 }  /* if found */
}

get_msg(sock, buff, buffsize)	/* read message off socket */
    int             sock;
    char           *buff;
    int             buffsize;
{
    int             red;
    char           *cp,tempbuff[256];

*buff = '\0';   
while ((red = read(sock, tempbuff, buffsize)) > 0) {
  if (!strncmp(&tempbuff[red - 3], EOM, EOM_LEN)) {
    tempbuff[red-4] = '\0';
    strcat(buff,tempbuff); 
    if (strlen(buff) > 0)   
      break; }
  strncat(buff,tempbuff,red);
}
}

send_msg(sock,str,len)
int sock;
char *str;
int len;
{
int wrote;
char msg[150];
sprintf(msg,"%s%s",str,CRLF);
wrote = write(sock,str,len + 2);
printf("wrote %d\n",wrote);
/* printf("wrote end %d\n",wrote); */
}
