/* Collection of code snippets by Arne VajhÃ¸j */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
/*
 *  Author     : Arne Vajhøj
 *
 *  Programmed : march 1996
 *
 *  Purpose    : Guestbook
 *
 *  Modified   : september 2002
 *               Small HTML corrections.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <descrip.h>

#include "cgilib.h"
#include "scriptlib.h"

#ifdef VAXC
int shell$to_vms();
#else
int decc$to_vms();
#endif

int check_access();
int check_url_access();
void replace(char *s,char *s1,char *s2);

char vmsfnm[256];

int guest_doit_vms(char *s,int i)
{
   strcpy(vmsfnm,s);
   return 1;
}

main(int argc,char *argv[])
{
   FILE *fp1,*fp2;
   int l,vmsspec;
   char dir[256],fnm1[256],fnm2[256],fnm3[256],url[256],line[256],a[1024],*p,*p2;
   char name[41],email[41],comment[601],ts[41],cmd[101];
   time_t t;
   cgi_init_env(argc,argv);
   /* get directory */
   p=cgi_info("PATH_INFO");
   if(strstr(p,"[")!=NULL) {
      /* VMS directory spec */
      p++;
      strcpy(dir,p);
      vmsspec=1;
   } else {
      /* URL spec */
      cgi_translate_path(p,1,url,sizeof(url)-1,&l);
      url[l-1]='\0';
#ifdef VAXC
      shell$to_vms(url,guest_doit_vms,0,0);
#else
      decc$to_vms(url,guest_doit_vms,0,0);
#endif
      if(strlen(vmsfnm)==0) strcpy(vmsfnm,"nodisk:[nodirectory]");
      strcpy(dir,vmsfnm);
      vmsspec=0;
   }
   /* check access */
   if(vmsspec) {
      sprintf(fnm1,"%s%s",dir,"GB.TPL");
      sprintf(fnm2,"%s%s",dir,"GB.INC");
      l=check_access(fnm1)&&check_access(fnm2);
   } else {
      sprintf(fnm1,"%s%s",p,"GB.TPL");
      sprintf(fnm2,"%s%s",p,"GB.INC");
      l=check_url_access(fnm1)&&check_url_access(fnm2);
      sprintf(fnm1,"%s%s",dir,"GB.TPL");
      sprintf(fnm2,"%s%s",dir,"GB.INC");
   }
   if(!l) {
      cgi_begin_output(1);
      cgi_printf("content-type: text/plain\n\n");
      cgi_printf("No access to guestbook\n");
      exit(1);
   }
   sprintf(fnm3,"%s%s",dir,"GB.DIS");
   /* get form input */
   l=atoi(cgi_info("CONTENT_LENGTH"));
   cgi_read(a,l);
   net_unescape_string(a,&l);
   p=strstr(a,"name=");
   p2=strstr(p,"&");
   if(p2==NULL) p2=a+l;
   strncpy(name,p+5,p2-p-5);
   name[p2-p-5]='\0';
   replace(name,"+"," ");
   p=strstr(a,"email=");
   p2=strstr(p,"&");
   if(p2==NULL) p2=a+l;
   strncpy(email,p+6,p2-p-6);
   email[p2-p-6]='\0';
   replace(email,"+"," ");
   p=strstr(a,"comment=");
   p2=strstr(p,"&");
   if(p2==NULL) p2=a+l;
   strncpy(comment,p+8,p2-p-8);
   comment[p2-p-8]='\0';
   replace(comment,"+"," ");
   replace(comment,"\r","");
   replace(comment,"\n\n","\n");
   replace(comment,"\n\n","\n");
   replace(comment,"\n\n","\n");
   replace(comment,"\n\n","\n");
   replace(comment,"\n\n","\n");
   replace(comment,"\n\n","\n");
   t = time(NULL);
   strcpy(ts,ctime(&t));
   ts[strlen(ts)-1] = '\0';
   /* write HTML header */
   cgi_begin_output(1);
   cgi_printf("content-type: text/plain\n\n");
   /* update guestbook */
   fp1=fopen(fnm1,"r");
   fp2=fopen(fnm2,"a");
   while (fgets(line,sizeof(line),fp1)) {
      replace(line,"%n",name);
      replace(line,"%e",email);
      replace(line,"%c",comment);
      replace(line,"%t",ts);
      fputs(line,fp2);
   }
   fclose(fp1);
   fclose(fp2);
   /* send notification email */
   sprintf(cmd,"MAIL NLA0: \"@%s\" /SUBJ=\"GuestBook notification for %s\"",
           fnm3,dir);
   system(cmd);
   /* report OK */
   cgi_printf("Added successfull\n");
}
