
#include "bbs.h"
void SendChatFlag(int opt);
extern int UserIsDoing;

#ifdef JOECODE
extern char mybbstitle[];
extern struct StartOption *Sopt;
void RegServer(void);
extern int UserIsDoing;
char MstrServe[]="ServerRP ";
char MultiRP[] = "MultiRP ";
struct MsgPort *ServerRP;

struct Master
{
    struct Message Msg;
  char User[50];
  char Location[50];
  char Action[50];
  char Baud[10];
  int Data;
  int Command;
  int Node;
  int LineNum;
  struct Commands *MyCmds;
  struct StartOption *Sopt;
} *MasterMsg=NULL;
void CreateServerRP(int node);
void DeleteServerRP(void);
int SetEnvStat(int Code)
{
  char Status[200];
  char Environ[200];
  
 
  if(!Sopt->Toggles[10]) UserIsDoing=Code;
  else
  {ObtainSemaphore((struct SignalSemaphore *)SingleNode);
  strcpy(SingleNode->Handle,User.Name);
  strcpy(SingleNode->Location,User.Location);
  strcpy(SingleNode->Misc1,"");
  strcpy(SingleNode->Misc2,"");
  UserIsDoing=Code;
  if(QuietNode) SingleNode->Status=0-(UserIsDoing+2);else
  SingleNode->Status=UserIsDoing;
  ReleaseSemaphore((struct SignalSemaphore *)SingleNode);
  }

  sprintf(Environ,"STATS@%d",Cmds->AcLvl[LVL_NODE_NUMBER]);
  sprintf(Status,"%-35s-%02d",User.Name,Code);
  SetVar(Environ,Status,-1L,LV_VAR|GVF_GLOBAL_ONLY);
 
SendMaster();



  return(1);
}

void SendMaster(void)
{
  char temp[10];
  struct MsgPort *port;
  strcpy(MasterMsg->User,User.Name);
  strcpy(MasterMsg->Location,User.Location);
  sprintf(temp,"%d",UserIsDoing);
  strcpy(MasterMsg->Action,temp);
  sprintf(temp,"%5d",Online_Baud);
  strcpy(MasterMsg->Baud,temp);
  if((port=(struct MsgPort *)FindPort("AE.Master")))
  {
     PutMsg(port,(struct Message *)MasterMsg);
     (void)WaitPort(ServerRP);
	(void)GetMsg(ServerRP);
  }
}
void SetEnvMsg(char *s)
{
  char temp[10];
  struct MsgPort *port;
  strcpy(MasterMsg->User,s);
  //strcpy(MasterMsg->Location,User.Location);
  sprintf(temp,"%d",SV_NEWMSG);
  strcpy(MasterMsg->Action,temp);

  if((port=(struct MsgPort *)FindPort("AE.Master")))
  {
     PutMsg(port,(struct Message *)MasterMsg);
     (void)WaitPort(ServerRP);
	(void)GetMsg(ServerRP);
  }
}
void SendMasterUpload(char *filename)
{
  char temp[10];
  struct MsgPort *port;
  strcpy(MasterMsg->User,filename);
  strcpy(MasterMsg->Location,User.Location);
  sprintf(temp,"%d",UserIsDoing);
  strcpy(MasterMsg->Action,temp);
  MasterMsg->Command=3;

  if((port=(struct MsgPort *)FindPort("AE.Master")))
  {
     PutMsg(port,(struct Message *)MasterMsg);
     (void)WaitPort(ServerRP);
	(void)GetMsg(ServerRP);
  }
  if(Sopt->Toggles[10])
  {
  ObtainSemaphore((struct SignalSemaphore *)SingleNode);
  strcpy(SingleNode->Misc1,filename);
  ReleaseSemaphore((struct SignalSemaphore *)SingleNode);
  }
  MasterMsg->Command=1;
}
void SendMasterDownload(char *filename)
{
  char temp[10];
  char temp1[43];
  struct MsgPort *port;
  sprintf(temp1,"%-16.16s",FilePart(filename));
  strcpy(MasterMsg->User,temp1);
  strcpy(MasterMsg->Location,User.Location);
  sprintf(temp,"%d",UserIsDoing);
  strcpy(MasterMsg->Action,temp);
  MasterMsg->Command=2;

  if((port=(struct MsgPort *)FindPort("AE.Master")))
  {
     PutMsg(port,(struct Message *)MasterMsg);
     (void)WaitPort(ServerRP);
	(void)GetMsg(ServerRP);
  }
  if(Sopt->Toggles[10])
  {
   ObtainSemaphore((struct SignalSemaphore *)SingleNode);
    strcpy(SingleNode->Misc1,FilePart(filename));
    ReleaseSemaphore((struct SignalSemaphore *)SingleNode); 
  }
  MasterMsg->Command=1;
}
void SendChatFlag(int opt)
{
  struct MsgPort *port;
  strcpy(MasterMsg->User,"");
  strcpy(MasterMsg->Location,"");
  strcpy(MasterMsg->Action,"");
  MasterMsg->Command=opt+4;

  if((port=(struct MsgPort *)FindPort("AE.Master")))
  {
     PutMsg(port,(struct Message *)MasterMsg);
     (void)WaitPort(ServerRP);
	(void)GetMsg(ServerRP);
  }
  MasterMsg->Command=1;
}
void SendQuietFlag(int opt)
{
  struct MsgPort *port;
  strcpy(MasterMsg->User,"");
  strcpy(MasterMsg->Location,"");
  strcpy(MasterMsg->Action,"");
  MasterMsg->Command=opt+6;

  if((port=(struct MsgPort *)FindPort("AE.Master")))
  {
     PutMsg(port,(struct Message *)MasterMsg);
     (void)WaitPort(ServerRP);
	(void)GetMsg(ServerRP);
  }
  MasterMsg->Command=1;
  if(Sopt->Toggles[10])
  {
   ObtainSemaphore((struct SignalSemaphore *)SingleNode);
  strcpy(SingleNode->Handle,User.Name);
  strcpy(SingleNode->Location,User.Location);
  strcpy(SingleNode->Misc1,"");
  strcpy(SingleNode->Misc2,"");
  if(QuietNode) SingleNode->Status=0-(UserIsDoing+2);else
  SingleNode->Status=UserIsDoing;
  ReleaseSemaphore((struct SignalSemaphore *)SingleNode);
  }
}
void CreateServerRP(int node)
{
  MstrServe[8]= node+'0';//Cmds->AcLvl[LVL_NODE_NUMBER]+'0';
  MultiRP[7]= node+'0';
  if(FindPort(MstrServe)) exit(0);
  ServerRP=CreatePort(MstrServe,0L);
  MasterMsg=(struct Master *)AllocMem(sizeof(struct Master),MEMF_PUBLIC|MEMF_CLEAR);
  MasterMsg->Node=node;//Cmds->AcLvl[LVL_NODE_NUMBER];
  MasterMsg->Command=1;
  MasterMsg->Msg.mn_Node.ln_Type=NT_MESSAGE;
  MasterMsg->Msg.mn_Length=sizeof(struct Master);
  MasterMsg->Msg.mn_ReplyPort=ServerRP;
  RegServer();
}
void SendACPCommand(char *string,int command,int node)
{
  struct MsgPort *port;
  strcpy(MasterMsg->User,string);
  strcpy(MasterMsg->Location,"");
  strcpy(MasterMsg->Action,"");
  MasterMsg->Command=8;
  MasterMsg->Node=node;
  MasterMsg->Data=command;

  if((port=(struct MsgPort *)FindPort("AE.Master")))
  {
     PutMsg(port,(struct Message *)MasterMsg);
     (void)WaitPort(ServerRP);
	(void)GetMsg(ServerRP);
  }
  MasterMsg->Node=Cmds->AcLvl[LVL_NODE_NUMBER];
  MasterMsg->Command=1;
}
void RegServer(void)
{
  struct MsgPort *port;
  MasterMsg->Command=SV_START;
  while(!(port=(struct MsgPort *)FindPort("AE.Master")));

     PutMsg(port,(struct Message *)MasterMsg);
     (void)WaitPort(ServerRP);
	(void)GetMsg(ServerRP);
  strcpy(mybbstitle,MasterMsg->User);
  Cmds=MasterMsg->MyCmds;
  Sopt=MasterMsg->Sopt;
  MasterMsg->Command=1;
}
void DeleteServerRP(void)
{
  DeletePort((struct MsgPort *)ServerRP);
  FreeMem(MasterMsg,sizeof(struct Master));
}

#endif
