/*
**
**
** Last updated: Mon Sep 07 22:54:02 1998
*/

UBYTE *version = "$VER: UserNote 1.1 "__AMIGADATE__;

void do_reminder(void);

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

void main( int argc, char **argv )
{
   char where [ 32 ] = "";

   if((argc < 2) || !(CNetCBase = OpenLibrary("cnetc.library", 4L)))
   {
      return;
   }

   if(!(CNetBase = OpenLibrary( "cnet.library", 4 )))
   {
      CloseLibrary(CNetCBase);
      return;
   }

   if(!(context = CNC_InitContext(argv[1])))
   {
      CloseLibrary(CNetCBase);
      CloseLibrary(CNetBase);
      return;
   }

   strcpy(where, z->Doing);

   do_reminder();

   CNC_SetDoing(where);

   CNC_ShutDown(NULL);
   CNC_DisposeContext();
   CloseLibrary(CNetCBase);
   CloseLibrary(CNetBase);

   return;
}

void do_reminder(void)
{
   BPTR   fp;

   int i,
       text;

   char arg   [ 256 ] = "",
        date  [  32 ] = "",
        file  [ 256 ] = "";

   int id;

   CNC_SetDoing("UserNotes");

   for(i = 1; i < z->npitems; i++)
   {
      strcat(arg, z->pitem[i]);
      if(i != (z->npitems - 1))
         strcat(arg, " ");
   }

   if(!*arg)
   {
      if(CNC_EnterLine(20, ELINE_NOLEADSPACE | ELINE_ALLOWCHAT | ELINE_INPUTBOX,
                   "n1Who would you like to sent a note to?: "))
         strcpy(arg, z->InBuffer);
   }
      
   if(id = CNC_FindAccount(arg, &z->user2, FALSE))
   {
      for(i = 0; i < myp->HiPort; i++)
      {
         if(myp->pc[i].online)
         {
            if((myp->PortZ[i]->id == id) && !(myp->PortZ[i]->IUCBits[z->InPort] & PORTRIGHT_HIDDEN))
            {
               sprintf(z->ABuffer, "n1c2You do know that %s is on port %d?n1"
                                   "You could just send %s an OLM instead. :-)n1",
                        z->user2.Handle, z->user2.z->InPort, z->user2.Sex ? "him":"her");
               CNC_PutA();
               return;
            }
         }
      }

      CNC_MakeDate(&z->user2.LastCall, date);
      CNC_PutText("n1c1");
      sprintf(z->ABuffer, "%s'sc7 last call was c2%sc7.n1", z->user2.Handle, date);
      CNC_PutA();
      sprintf(z->ABuffer, "Enter what you want to send to %s. Hit <enter> when done.n2", z->user2.Handle);
      CNC_PutA();

      if(!(text = CNC_EnterText(NULL, 800, z->user1.TermWidth-2,10)))
      {
         sprintf(z->ABuffer, "n1Nothing will be sent to %s.n1", z->user2.Handle);
         CNC_PutA();
         return;
      }

      sprintf(file, "Mail:Users/%s/event.log", z->user2.UUCP);

      if(fp = OpenAppend(file))
      {
         CNC_MakeDate(&z->Today, date);
         FPuts(fp,"z4c7 The following note(s) where left since your last logonz0n1");
         FPrintf(fp,"n1z4}c7}Fromc2:z0}c7} %s c2(%ld)n1z4}c7}On  c2:z0}c7} %s", z->user1.Handle, z->id, date);
         for(i = 0; i < text;i++)
            FPrintf(fp,"n1c3%sc5", z->GBuffer[i]);
         FPuts(fp,"c2n2[Pause] g0[8D[Kh8");
         Close(fp);
      }

      CNC_PutText("n1c1Note(s) will be displayed to user on next logon.n1q1");
   }

   return;
}
