/*
** CNetC Feedback Replacement
** by Kelly Cochran
** Last updated: Wed Feb 11 20:24:31 1998
*/

const char *version = "\0$VER: Feedback 4.2 "__AMIGADATE__;

#define MAX_SYSOPS 9

#define DATAFILE   "SysData:FB.Sysops"

void do_feedback(void);
void do_config(void);
BOOL CarrierCheck(void);

struct MainPort     *myp;            /* Pointer to CNet port--ALL info! */
struct PortData     *z;
char                **bm;
struct Library      *CNetBase  = NULL;
struct Library      *CNet4Base = NULL;
struct Library      *CNetCBase = NULL;
struct CNetCContext *context = NULL;

void main(void)
{
   struct args
   {
      ULONG Port;
   };

   struct RDArgs *rda;
   struct args    myargs;

   if(rda = ReadArgs("PORT/A", (LONG *)&myargs, NULL))
   {
      if(myargs.Port)
      {
         if(CNetBase = OpenLibrary("cnet.library", 4))
         {
            if(CNetCBase = OpenLibrary("cnetc.library", 4))
            {
               if(context = CNC_InitContext((char *)myargs.Port))
               {
                  if(CNet4Base = OpenLibrary("cnet4.library", 4))
                  {
                     z   = context->z;
                     myp = context->myp;
                     bm  = z->bm;

                     if ((z->SysMaint) && (!stricmp("config",z->pitem[1])))
                        do_config();
                     else
                        do_feedback();

                     CloseLibrary(CNet4Base);
                  }
                  CNC_ShutDown(NULL);
                  CNC_DisposeContext();
               }
               CloseLibrary(CNetCBase);
            }
            CloseLibrary(CNetBase);
         }
      }
      FreeArgs(rda);
   }
}

void getword(char *word, char *line, char stop)
{
   int x,
       y;

   for(x=0;((line[x]) && (line[x] != stop));x++)
       word[x] = line[x];

   word[x] = '\0';
   if(line[x])
      ++x;
   y=0;

   while(line[y++] = line[x++]);
}

void do_mail(int id)
{
   struct MailToList     MTL;
   struct MailHeader4    MH4;

   BPTR mh4, mtl;

   long time;

   char ebuf [ 40 ] = "";
   char fmh4 [ 80 ] = "";
   char fmtl [ 80 ] = "";

   memset(&MTL, 0, sizeof(struct MailToList));
   memset(&MH4, 0, sizeof(struct MailHeader4));

   z->vis_subj = "Feedback";
   z->vis_to   = myp->Key[id-1].Handle;

   CNC_CallEditor(z->user1.MyPrivs.EditorLines,FALSE);

   if (z->edbuff < 1)
      return;

   time = CNetImplodeTime(&z->Today);

   strcpy(MH4.Subject,"Feedback");
   strcpy(MH4.From,z->user1.Handle);
   MH4.Date      = time;
   MH4.Number    = time;
   MH4.ByID      = z->user1.IDNumber;
   MH4.ByAccount = IDToAccount(z->user1.IDNumber);
   MH4.ToID      = myp->Key[id-1].IDNumber;
   MH4.ToAccount = IDToAccount(myp->Key[id-1].IDNumber);

   MakeEd(ebuf);

   MH4.Length    = FileSize(ebuf);

   sprintf(fmh4,CNETOUTBOX"/%08x.cmh",time);

   if(mh4 = Open(fmh4,MODE_NEWFILE))
   {
      Write(mh4,&MH4,sizeof(struct MailHeader4));
      SaveEditor(mh4,FALSE);
      Close(mh4);
   }

   strcpy(MTL.Name,myp->Key[id-1].Handle);
   strcpy(MTL.UUCP,myp->Key[id-1].UUCP);
   MTL.MailType = 1;

   sprintf(fmtl,CNETOUTBOX"/%08x.mtt",time);

   if(mtl = Open(fmtl,MODE_NEWFILE))
   {
      FPuts(mtl,fmh4);
      FPutC(mtl,0x0A);
      Flush(mtl);
      Write(mtl,&MTL,sizeof(struct MailToList));
      Close(mtl);
   }

   sprintf(z->ABuffer,CNETOUTBOX"/%08x.mtl",time);

   Rename(fmtl,z->ABuffer);
}

int do_parse(char *line)
{
   int sysop;
   char sysops [ 4 ] = "";

   getword(sysops,line,'|');
   sysop = atoi(sysops);
   return sysop;
}

void do_feedback(void)
{
   int i;
   int id;
   int defid = 0;
   int sysop = 0;
   int col = 0;
   int sys [ MAX_SYSOPS ];

   BPTR fp;

   char *oldwhere;
   char *feedq = "Enter the number of the co/sysop c6(0=Quit/Default=%d)q1: ";
   char lines  [ 20 ] = "";
   char line   [ MAX_SYSOPS ][ 50 ];

   oldwhere = z->MyDoing;

   CNC_SetDoing(myp->FirstTT->bm[293]);

   if(!CNC_PutQ(myp->FirstTT->bm[295]))
   {
      CNC_SetDoing(oldwhere);
      return;
   }

   if(fp = Open(DATAFILE,MODE_OLDFILE))
   {
      FGets(fp,lines,sizeof(lines));
      sysop = do_parse(lines);
      defid = atoi(lines);
      for (i = 0;i < sysop;i++)
      {
         FGets(fp,line[i],sizeof(line));
         sys[i] = do_parse(line[i]);
      }
      Close(fp);

      if (sysop < defid)
         defid = 1;
   }
   else
   {
      if(z->SysMaint)
         do_config();
      else
      {
         CNC_PutText("No "DATAFILE" datafile!\nPlease alert your sysop to fix this!\n");
         return;
      }
   }

   CNC_ReadGraphics("SysText:TGGH/Feedback",FALSE);

   CNC_PutText("q1Who would you like to direct this feedback to?n2");

   for (i = 0;i < sysop;i++)
   {
      col++;
      if (col > 7)
         col = 1;
      sprintf(z->ABuffer,"%3d - c%d%-20sq1 - %-s",i+1,col,
              myp->Key[sys[i]-1].Handle,line[i]);
      CNC_PutA();
   }

   CNC_DoReturn();
   sprintf(z->ABuffer,feedq,defid);
   CNC_EnterLine(3,ELINE_NUMBERSONLY | ELINE_INPUTBOX,z->ABuffer);

   id = atoi(z->InBuffer);

   if(!id)
   {
      CNC_SetDoing(oldwhere);
      return;
   }

   if ((!id) | (id > sysop))
      id = defid;

   do_mail(sys[id-1]);

   CNC_SetDoing(oldwhere);
}

void do_config(void)
{
   struct UserData udata;

   int i;
   int x;
   int id;
   int def;

   char line [ MAX_SYSOPS ][ 50 ];

   char *hndlq = "\n Handle or account # of co/sysop #%d: ";
   char *descq = "Short description for %s: ";
   char *defq  = "\nDefault co/sysop for feedback (1-%d): ";

   BPTR fdat;

   CNC_PutText("\nc7Before we go on we need to know the following items:\n");
   CNC_PutText("c21. All the included co/sysop's account numbers/handles.\n");
   CNC_PutText("c32. A short description of each of their duties.\n\n");

   if(!CNC_PutQ("q1Are you ready to begin [c2Yesq1]? ?1"))
      return;

   for (i = 1;i < MAX_SYSOPS;i++)
   {
      sprintf(z->ABuffer,hndlq,i);
      CNC_EnterLine(21,ELINE_HANDLESPECIAL | ELINE_INPUTBOX & ELINE_CAPWORDSTART,
                z->ABuffer);

      if(!CarrierCheck())
         return;

      if (!*z->InBuffer)
         break;

      if(!(id = CNC_FindAccount(z->InBuffer,&udata,FALSE)))
      {
         i--;
         continue;
      }

      sprintf(line[i],"%d|",id);
      sprintf(z->ABuffer,descq,myp->Key[id-1].Handle);
      CNC_EnterLine(30,ELINE_INPUTBOX | ELINE_CAPWORDSTART,z->ABuffer);

      if(!CarrierCheck())
         return;

      if (!*z->InBuffer)
         break;

      strcat(line[i],z->InBuffer);
   }
   i--;

   if(!i)
      return;

   sprintf(z->ABuffer,defq,i);
   CNC_EnterLine(3,ELINE_NUMBERSONLY | ELINE_INPUTBOX,z->ABuffer);

   if(!CarrierCheck())
      return;

   def = atoi(z->InBuffer);

   if((!def) | (def > i))
      return;

   if (fdat = Open(DATAFILE,MODE_NEWFILE))
   {
      sprintf(z->ABuffer,"%d|%d\n",i,def);
      FPuts(fdat,z->ABuffer);
      for(x = 1;x <= i;x++)
      {
         sprintf(z->ABuffer,"%s\n",line[x]);
         FPuts(fdat,z->ABuffer);
      }
      Close(fdat);
   }
}

BOOL CarrierCheck(void)
{
   if( !z->Carrier || !z->TimeLeft || z->Dumped )
      return FALSE;
   return TRUE;
}
