#ifndef __COMMON_H__
#include "common.h"
#endif /* __COMMON_H__ */

#ifndef __AREXX_CMDS_H__
#include "arexx_cmds.h"
#endif /* __AREXX_CMDS_H__ */

extern BOOL running;
extern struct PopData *pd;

BOOL initARexx(struct PopData *pd)
{
   BOOL result = FALSE;

   if(pd->pd_ARexx = ARexxObject,
      AREXX_HostName, pd->pd_ARexxPort ? pd->pd_ARexxPort : (UBYTE*)"IPOP.1",
      AREXX_Commands, ARexxCommands,
      AREXX_NoSlot, TRUE,
   End)
   {
      GetAttr(AREXX_SigMask, pd->pd_ARexx, &pd->pd_rexxSigBit);
      result = TRUE;
   }
   else
   {
      char buffer[64];

      sprintf(buffer, "ARexx port %s already exists", pd->pd_ARexxPort);
      requestUser(buffer);
   }
   return(result);
}

/*****************************************************************************/

VOID disposeARexx(struct PopData *pd)
{
   if(pd->pd_ARexx)
   {
      DisposeObject(pd->pd_ARexx);
   }
}

VOID __saveds __asm rexx_Check(register __a0 struct ARexxCmd *ac, register __a1 struct RexxMsg *rm)
{
   if(ac->ac_ArgList[0])
   {
      pd->pd_Check = *((LONG*)ac->ac_ArgList[0]) * 60;
      AbortIO((struct IORequest*)pd->pd_TimeRequest);
      WaitIO((struct IORequest*)pd->pd_TimeRequest);

      pd->pd_TimeRequest->tr_node.io_Command = TR_ADDREQUEST;
      pd->pd_TimeRequest->tr_time.tv_secs = pd->pd_Check * 60;
      pd->pd_TimeRequest->tr_time.tv_micro = 0;
      SendIO((struct IORequest*)pd->pd_TimeRequest);

   }
   else
   {
      doPop(pd);
   }
   ac->ac_Result = "1";
}

VOID __saveds __asm rexx_Quiet(register __a0 struct ARexxCmd *ac, register __a1 struct RexxMsg *rm)
{
   if(pd->pd_Quiet)
   {
      pd->pd_Quiet = FALSE;
      ac->ac_Result = "0";
   }
   else
   {
      pd->pd_Quiet = TRUE;
      ac->ac_Result = "1";
   }
}

VOID __saveds __asm rexx_Version(register __a0 struct ARexxCmd *ac, register __a1 struct RexxMsg *rm)
{
   ac->ac_Result = VERS;
}

VOID __saveds __asm rexx_Quit(register __a0 struct ARexxCmd *ac, register __a1 struct RexxMsg *rm)
{
   running = FALSE;
   ac->ac_Result = "1";
}

VOID __saveds __asm rexx_Author(register __a0 struct ARexxCmd *ac, register __a1 struct RexxMsg *rm)
{
   ac->ac_Result = AUTHOR;
}

VOID __saveds __asm rexx_Waiting(register __a0 struct ARexxCmd *ac, register __a1 struct RexxMsg *rm)
{
   static UBYTE buffer[5];

   sprintf(buffer, "%ld", pd->pd_TotalMsg);
   ac->ac_Result = buffer;
}
