/* Part of DoorLib v1.12

   Name   : userdata
   Version: 1.12
   Date   : Saturday, 1st June 1996

   Purpose: DoorLib's user data functions.

   Creator: Richard Murray
*/

#include <stdio.h>
#include <stdlib.h>

#include "kernel.h"
#include "strings.h"
#include "doorlib.h"



int doors_isportavailable(int bs_port)
/* Is the port available?
   Returns '1' if free and '0' if another door is running. */
{
   _kernel_swi_regs bs_rin, bs_rout;
   int bs_status = 0;

   bs_rin.r[0] = bs_port;
   _kernel_swi(ARCbbsDoors_ReadStatus,&bs_rin,&bs_rout);
   bs_status = bs_rout.r[0];

   if (bs_status!=0) return (0);

   return(1);
}


struct bs_userdatum_type0 *doors_genericfetchdata_typezero(int bs_port)
/* This is a routine that is mainly for future internal use. It fetches all the
   user data into a big struct and returns it...
   For ease of use, you should call the routine for what you require. :-) */
{
   _kernel_swi_regs bs_rin, bs_rout;

   struct bs_userdatum_type0 bs_userdata_0;

   int bs_reply=0;

   bs_userdata_0.bs_usernumber = 0;
   bs_rin.r[0] = bs_port;
   bs_rin.r[1] = (int)&bs_userdata_0;
   _kernel_swi(ARCbbsDoors_SendRequest,&bs_rin,&bs_rout);

   do
   {
      poll_the_wimp();
      bs_rin.r[0] = bs_port;
      bs_rin.r[1] = (int)&bs_userdata_0;
      _kernel_swi(ARCbbsDoors_GetReply,&bs_rin,&bs_rout);
      bs_reply = bs_rout.r[0];
   } while (bs_reply!=0);

   return &bs_userdata_0;
}


struct bs_userdatum_type1 *doors_genericfetchdata_typeone(int bs_port)
/* This is a routine that is mainly for future internal use. It fetches all the
   user data into a big struct and returns it...
   For ease of use, you should call the routine for what you require. :-) */
{
   _kernel_swi_regs bs_rin, bs_rout;

   struct bs_userdatum_type1 bs_userdata_1;

   int bs_reply=0;

   bs_userdata_1.bs_username[0] = 1;
   bs_userdata_1.bs_username[1] = 0;
   bs_userdata_1.bs_username[2] = 0;
   bs_userdata_1.bs_username[3] = 0;
   bs_rin.r[0] = bs_port;
   bs_rin.r[1] = (int)&bs_userdata_1;
   _kernel_swi(ARCbbsDoors_SendRequest,&bs_rin,&bs_rout);

   do
   {
      poll_the_wimp();
      bs_rin.r[0] = bs_port;
      bs_rin.r[1] = (int)&bs_userdata_1;
      _kernel_swi(ARCbbsDoors_GetReply,&bs_rin,&bs_rout);
      bs_reply = bs_rout.r[0];
   } while (bs_reply!=0);

   return &bs_userdata_1;
}


int doors_checkchatirq(int bs_port)
/* This checks the "NO_CHAT_REQUESTS" setting and returns 0 if set (no chats)
   and 1 if unset. */
{
   int bs_stat;
   struct bs_userdatum_type0 *bs_userdat;

   bs_userdat=doors_genericfetchdata_typezero(bs_port);

   bs_stat = bs_userdat->bs_userflags;
   if ((bs_stat & 0x8000000) !=0) return(1);   /* It's set */

   return(0);
}


char *doors_getusername(int bs_port)
/* This little routine will extract the user's name (for the port specified)
   and return it as a string. */
{
   struct bs_userdatum_type0 *bs_userdat;
   bs_userdat=doors_genericfetchdata_typezero(bs_port);
   return(bs_userdat->bs_username);
}


int doors_gettermtype(int bs_port)
/* This will extract the user's port type (for the port specified).
   TTY = 0; VT52 = 1; VT100 = 2; ANSI = 3. Note ArmBBS only replies 0 or 3. */
{
   struct bs_userdatum_type0 *bs_userdat;
   bs_userdat=doors_genericfetchdata_typezero(bs_port);
   return(bs_userdat->bs_terminaltype);
}


int doors_gettimeleft(int bs_port)
/* This will return the time remaining for the specified port. Um, I think it is
   in seconds. Can't remember offhand! */
{
   struct bs_userdatum_type0 *bs_userdat;
   bs_userdat=doors_genericfetchdata_typezero(bs_port);
   return(bs_userdat->bs_timeleftthiscall);
}


int doors_getuserlevel(int bs_port)
/* This will return the user level of the user on the specified port.
   Remember, its in hexadecimal! */
{
   struct bs_userdatum_type0 *bs_userdat;
   bs_userdat=doors_genericfetchdata_typezero(bs_port);
   return(bs_userdat->bs_userlevel);
}


char *doors_getrealname(int bs_port)
/* This will return the real name of the user on the specified port. */
{
   struct bs_userdatum_type1 *bs_userdat;
   bs_userdat=doors_genericfetchdata_typeone(bs_port);
   return(bs_userdat->bs_realname);
}


void doors_includeX4(void)
{
   printf("BUDGIESOFT DOORLIB :: USERDATA");
}
