/********************************************************************************/
/* Red Dwarf Monitor - Who's online door for DLG Pro - Author: Wim Rumping      */
/********************************************************************************/
/* RD_Monitor is a door for use with the DLG Professional BBS Software.         */
/*                                                                              */
/* Usage: RD_Monitor                                                            */
/********************************************************************************/

#include "RD_Monitor.includes"                  /* Includes list for this door  */

/********************************************************************************/
/* Globale variabelen                                                           */
/********************************************************************************/

UBYTE *vers = "$VER: Red Dwarf Monitor v1.04 (Only fools use version full!)";

struct Library *DLGBase = 0;    	/* Init library base to 0 (= FALSE)     */
SHORT Program_Break=0;			/* 64 = Goodbye	(AND NOT 128)		*/
					/* 128= Break				*/

int mode=-1;				/* 0=Help, 1=Red Dwarf, 2=Broadcast	*/
					/* 3=UserView				*/

char textbuffer[150][100];		/* Textfiles must fit in this array 	*/
int textlines=0;			/* Lines currently in textbuffer	*/
int pagelength;				/* Pagelength for text.			*/
int pages;				/* # pages the current textbuffer needs	*/
int page=0;				/* Current page number			*/

int ports=0;				/* # of ports in portlist		*/
int Statuswindowposy;			/* Start of Who's online window		*/
struct USER_DATA User[18];		/* User info (0 = current user)		*/
struct Ram_File Ram[18];		/* Ram file  (0 = current user)		*/
char port[18][4];			/* Portnames (0 = current user)		*/
char onlinestring[18][100];		/* Online string for Who's online	*/
BOOL loaded[18];			/* User info loaded (user is online)	*/
BOOL changed;				/* Has the cursor position changed	*/

char broadcasts[50][81];		/* Broadcast buffer 			*/
char bcstring[90];			/* Captured Broadcast string (at bottom)*/
SHORT secondsshown=0;			/* Timer for Broadcast capture		*/

char actionstring[85];			/* Default actionstring from Ram[0]	*/
char portlist[80];			/* From the config file			*/
char Doorpath[80];			/* From the config file			*/
char defbcstring[90];			/* From the config file			*/

char lineinput[90];			/* Line entered by user in Getline	*/
char toets=0x00;			/* Last key pressed			*/
char prevtoets=0x00;			/* Last key pressed (copy)		*/
char prevtoets2=0x00;			/* Previous key pressed			*/
char prevtoets3=0x00;			/* And the key before that one		*/

time_t tp;				/* Timepointer				*/
struct tm *tm;				/* Time structure			*/

char tempstring[256];

/********************************************************************************/
/* ^C checking OFF!                                                             */
/********************************************************************************/

int Break_Detected() { Program_Break=Program_Break|128; return(0);}


/********************************************************************************/
/* Main program                                                                 */
/********************************************************************************/

void main(int argc, char *argv[])
{
  BOOL einde=FALSE;

  Initialize();
  mode=2;
  Drawwindow();
  Drawmodescreen();

/*  Inithelpmode();*/
  while (einde==FALSE && Program_Break<64)
    {
      toets=0x00;
      changed=TRUE;
      while (toets==0x00 && Program_Break<64)
        {
          if ((toets!='') && (toets!='[')) Who();
          if (changed)
            {
              printf("[%i;1H[K",Statuswindowposy-1);
              fflush(stdout);
              changed=FALSE;
            }
          toets=ReadChar(1200000);
          toets=toupper(toets);
          Breakcheck();
        }
      fflush(stdout);
      prevtoets3=prevtoets2;
      prevtoets2=prevtoets;
      prevtoets=toets;
      if (Program_Break<64)
        {
          switch (toets)
            {
            case 'Z'  :
            case 'Q'  : Program_Break=Program_Break | 64;
                        break;
            case 'S'  : Nodemessage();
                        break;
            case '?'  : Inithelpmode();
                        break;
            case 'B'   : if (mode!=2)
                          {
                            mode=2;
                            Drawmodescreen();
                          }
                        break;
            case 'V'  : ViewUser();
                        break;
            case 'R'  : Drawwindow();
			Drawmodescreen();
                        break;
            case 'C'  : if ((prevtoets2=='[' && prevtoets3=='') | prevtoets2==0x9b)
			  {
			    if ((mode==0 || mode==3) && pages!=1)
			      {
			        page=(page%pages)+1;
				Printpage();
			      }
			  }
                        break;
            case 'D'  : if ((prevtoets2=='[' && prevtoets3=='') | prevtoets2==0x9b)
			  {
			    if ((mode==0 || mode==3) && pages!=1)
			      {
                                page--;
                                if (page==0) page=pages;
 			        Printpage();
                              }
			  }
                        break;
            default   : break;
            }       
        }
    }

  Program_Break=64|Program_Break;			 /* Goodbye */
  Breakcheck();
}


/********************************************************************************/
/* Who - Makes a printout on the status window about who's online		*/
/********************************************************************************/

void Who()
{
  char timeleft[30];
  FILE *User_File;
  char uname[40];
  char tijdelijk[80];
  int teller;
  int teller2;
 
  for(teller = 1; teller <= ports; teller++)
    {
      if(ReadRam(&Ram[teller],port[teller]))
        {
          if (!(loaded[teller]))
            {
              strcpy(uname,Ram[teller].Name);
              UnderScore(uname);

              sprintf(tempstring, "USER:%s/User.Data",uname);
              if(User_File = fopen(tempstring, "r"))
                {
                  fread(&User[teller], sizeof(struct USER_DATA), 1, User_File);
                  fclose(User_File);
                  loaded[teller]=TRUE;
                }
            }
          Ram[teller].Name[20]=0x00;
          Ram[teller].Action[42]=0x00;
          if (loaded[teller])
            {
              if (TranslateBuffer("%TLCALL",timeleft,29,&User[teller],&Ram[teller],port[teller])>5)
                strcpy(timeleft,"Dead");
              sprintf(tempstring,"[1;37m%s  %-20s %5i %4s %s[K",port[teller],Ram[teller].Name,Ram[teller].Baud_Rate,timeleft,Ram[teller].Action);
            }
          else
            sprintf(tempstring,"[1;37m%s  %-20s %5i      %s[K",port[teller],Ram[teller].Name,Ram[teller].Baud_Rate,Ram[teller].Action);
        }
      else
        {
          sprintf(tempstring,"[1;35m%s  No Carrier[K",port[teller]);
          loaded[teller]=FALSE;
        }      

      if (strcmp(onlinestring[teller],tempstring))
        {
          strcpy(onlinestring[teller],tempstring);
          printf("[%i;1H%s",Statuswindowposy+teller,onlinestring[teller]);
          changed=TRUE;
        }
    }

    if (secondsshown<=8)
      {
        if (BCGet(port[0],bcstring)==BCNOERR)
          {
            tp = time(NULL);
            tm = localtime(&tp);

	    if (strchr(bcstring,'\n')!=NULL) *strchr(bcstring,'\n') = 0x00;

            for(teller2 = Statuswindowposy-2; teller2>=1; teller2--)
              {
                strcpy(broadcasts[teller2], broadcasts[teller2-1]);
              }
            strcpy(tijdelijk,bcstring);
            tijdelijk[70]=0x00;
            strftime(tempstring, 10, "%H:%M:%S",tm);
            sprintf(broadcasts[0],"%s %s[K",tempstring, tijdelijk);

            if (mode==2)
              {
                printf("[3;1H[M[%d;1H[L[0;35m%s",Statuswindowposy-3,broadcasts[0]);
              }

            bcstring[79]=0x00;
            if (mode!=2) printf("\a");
            printf("[%i;1H[1;37m%s [K",User[0].Screen_Len,bcstring);
            secondsshown=12;
            changed=TRUE;
          }
        else
          if (secondsshown==1)
            {
              sprintf(bcstring,defbcstring);
              printf("[%i;1H%s",User[0].Screen_Len,bcstring);
              changed=TRUE;
            }
      }

    if (secondsshown>=1)
      secondsshown--;

}


/********************************************************************************/
/* Breakcheck				 					*/
/********************************************************************************/

/********************************/
/* 64 = Goodbye (AND NOT 128)   */
/* 128= Break                   */
/********************************/

void Breakcheck()
{
  if (Program_Break>=64)
    {
      ClearScreen;
      if(DLGBase)
        CloseLibrary(DLGBase);                         /* !!!!! Important !!!!! */
      rawcon(0);
      exit(0);
    }
}


/********************************************************************************/
/* Getcharacter                                                                 */
/********************************************************************************/

char Getcharacter()
{
  char karakter;

  Breakcheck();
  fflush(stdout);
  karakter=ReadChar(500000);
  return (karakter);
}


/********************************************************************************/
/* Getline                                                                      */
/********************************************************************************/

void Getline()
{
  int domtellertje=0;
  int index = 0;
  unsigned char A_Char=0x00;
  
  changed=FALSE;

  while((Program_Break<64) && (A_Char != 0x0D))
  {
    domtellertje++;
    domtellertje=domtellertje%10;
    if (domtellertje==0)
      {
        Who();
        if (changed)
          {
            printf("[%i;%iH[1;37m",Statuswindowposy-1,index+17);
            changed=FALSE;
          }
      }
    A_Char = Getcharacter();
    if (index==0) A_Char=toupper(A_Char);

    if(isprint(A_Char) || (A_Char==' '))
    {
      if ((index < 63) && (!((index==0) && (A_Char==' '))))
        {
          /* Character can be inserted into string */
          lineinput[index] = A_Char;
          index++;
          printf("%c", A_Char);
        }
    }
    else
      if((A_Char == 0x08) && (index > 0))
      {
        /* Backspace hit */
        printf("\x08 \x08");
        index--;
      }
  }
  lineinput[index] = 0x00;
  index--;
  while ((index>0) && (lineinput[index]==' '))
    {
      lineinput[index] = 0x00;
      index--;
    }
  if (index<2) lineinput[0]=0x00;
}


/********************************************************************************/
/* Nodemessage 									*/
/********************************************************************************/

void Nodemessage()
{
  char lijst[18][4];
  struct Query Querydata;
  struct UserInfo Userdata;
  SHORT grootte=1;
  char bc2port[40];
  char validchars[25];
  char broadcastthis[90];
  int teller;
  int teller2;

  strcpy(Ram[0].Action,"Entering a broadcast message");
  WriteRam(&Ram[0],port[0]);

  printf("[%i;1H[K",Statuswindowposy-1);

  strcpy(lijst[0],"ALL");
  for(teller = 1; teller <= ports; teller++)
    {
      if (strcmp(port[0],port[teller])!=0)
        {
          if (loaded[teller])
            {
              strcpy(lijst[grootte],port[teller]);
              grootte++;  
            }
        }        
    }
  if (grootte>2)
    {
      /* DLGquery guess mode */

      Userdata.User=&User[0];
      Userdata.Ram=&Ram[0];

      sprintf(tempstring,"[%i;1H[1;37mSend Message to which port => ",(Statuswindowposy-1));
      sprintf(validchars,"0123456789ALRTalrt");

      fflush(stdout);

      Querydata.prompt=tempstring;
      Querydata.template=lijst[0];
      Querydata.string=bc2port;
      Querydata.defstring=0;
      Querydata.valid=validchars;
      Querydata.length=4;
      Querydata.typelength=grootte;
      Querydata.flags=QUERY_GUESS+QUERY_UPCASE+QUERY_NOSTACK;

      DLGQuery(&Querydata,&Userdata);
      Ram[0].Command_Stack[0]=0x00;

      printf("[%i;1H[K",Statuswindowposy-1);
    }
  else
    if (grootte==2)
      {
        strcpy(bc2port,lijst[1]);
      }
  if ((grootte>1) && (strlen(bc2port)==3))
    {
      Who();

      printf("[%i;1H[1;37mMessage to %s> [37m",Statuswindowposy-1,bc2port);

      sprintf(Ram[0].Action,"Entering a message to broadcast to %s",bc2port);
      WriteRam(&Ram[0],port[0]);

      Getline();
      if (lineinput[0]!=0x00) sprintf(broadcastthis,"%s> %s",port[0],lineinput);

      if (!(strcmp("ALL",bc2port)))
        {
          bc2port[0]=0x00;
          for(teller = 1; teller <= grootte; teller++)
           {
             for(teller2 = 1; teller2 < ports; teller2++)
               {
                 if (!(strcmp(lijst[teller],port[teller2])))
                   {
                     if (loaded[teller2]) strcat(bc2port,lijst[teller]);
                   }
               }                         
           }
        }
      else
        for(teller2 = 1; teller2 < ports; teller2++)
          {
            if (!(strcmp(bc2port,port[teller2])))
              {
                 if (!loaded[teller2]) bc2port[0]=0x00;
              }
          }                         
      if ((lineinput[0]!=0x00) && (bc2port[0]!=0x00))
        {
          BroadCast(bc2port,broadcastthis,NULL);
        }
      else
        { 
          printf("[%i;1H[1;33mBroadcast failed...[K",Statuswindowposy-1);
          fflush(stdout);
          Who();
          fflush(stdout);
          ReadChar(1500000);
        }
      printf("[%i;1H[1;37m[K",Statuswindowposy-1);
    }      

  strcpy(Ram[0].Action,actionstring);
  WriteRam(&Ram[0],port[0]);
}


/********************************************************************************/
/* Drawwindow									*/
/********************************************************************************/

void Drawwindow()
{
  int teller;

  ClearScreen;
  printf("[%d;1H[1;33mPort[35mÄ[33mUser[35mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ[33mSpeed[35mÄ[33mTime[35mÄ[33mStatus[35mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n",Statuswindowposy);
  for(teller = 1; teller <= ports; teller++)
    printf("%s\n",onlinestring[teller]);
  printf("[1;35mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ[1;37m\n",Statuswindowposy);
  printf("%s",bcstring);
}


/********************************************************************************/
/* Drawmodescreen								*/
/********************************************************************************/

void Drawmodescreen()
{
  int teller;

  if (mode==2)
    {
      printf("[H[1;35mÄ[33mBroadcast[35mÄ[33mMode[35mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");
      printf("[K");
      printf("[3;1H[0;35m");
      for(teller = Statuswindowposy-6; teller >= 0; teller--)
        {
          printf("%s[K\n",broadcasts[teller]);
        }
      printf("[K");
    }
  else
    if (mode==1)
      {
        printf("[H[1;35mÄ[33mRed[35mÄ[33mDwarf[35mÄ[33mMode[35mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");
        for(teller = Statuswindowposy-4; teller >= 0; teller--)
          {
            printf("[K\n",broadcasts[teller]);
          }
      }
    else
      if (mode==3)
        {
          printf("[H[1;35mÄ[33mView[35mÄ[33mUser[35mÄ[33mMode[35mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n[K");
          Printpage();
        }
      else
        if (mode==0)
          {
            printf("[H[1;35mÄ[33mHelp[35mÄ[33mMode[35mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n[K");
            Printpage();
          }
}


/********************************************************************************/
/* Inithelpmode									*/
/********************************************************************************/

void Inithelpmode()
{
  FILE *Helpfile;
 
  if (mode!=0 && textlines!=1)
    {
      mode=0;
      page=1;
      sprintf(tempstring, "%sText/Help.txt", Doorpath);
      if(Helpfile = fopen(tempstring, "r"))
        {
          textlines=0;
          while (fgets(textbuffer[textlines], 100, Helpfile)!=NULL)
            {
	      *strchr(textbuffer[textlines],'\n') = 0x00;
              textlines++;
            }
          fclose(Helpfile);
        }
      else
        {
          Delay(50);
          if(Helpfile = fopen(tempstring, "r"))
            {
              textlines=0;
              while (fgets(textbuffer[textlines], 100, Helpfile)!=NULL)
                {
		  *strchr(textbuffer[textlines],'\n') = 0x00;
                  textlines++;
                }
              fclose(Helpfile);
            }
          else
            {
              textlines=1;
              sprintf(textbuffer[0],"[35mSorry, couldn't open the helpmode file... Retry by pressing []33mESC ?[35m] again!\n");
	      mode=-1;
            }
        }

      pages=textlines/pagelength;
      if ((textlines%pagelength)!=0)
        pages++;
      Drawmodescreen();

    }
}


/********************************************************************************/
/* Printpage									*/
/********************************************************************************/

void Printpage()
{
  int teller;

   printf("[3;1H[1;35m");
   
   for(teller = 0; teller < pagelength; teller++)
     {
       if (((page-1)*pagelength+teller)>=textlines)
         printf("[K\n");
       else
         printf("%s[K\n",textbuffer[((page-1)*pagelength+teller)]);
     }

   printf("[K[36C[1;35m[Page [33m%d [35mof[33m %d[35m] Switch with Cursor left/right\n",page,pages);
}


/********************************************************************************/
/* ViewUser 									*/
/********************************************************************************/

void ViewUser()
{
  char lijst[18][16];
  struct Query Querydata;
  struct UserInfo Userdata;
  struct USER_DATA VUser;
  SHORT grootte;
  char name[40];
  FILE *User_File;
  char uname[40];
  SHORT def=0;
  ULONG size;
  void *users;
  int teller;

  strcpy(Ram[0].Action,"Viewing User Information");
  WriteRam(&Ram[0],port[0]);

  Who();
  fflush(stdout);

  printf("[%i;1H[K",Statuswindowposy-1);
  strcpy(lijst[0],"OTHER USER");
 
  grootte=1;

  for(teller = 1; teller <= ports; teller++)
    {
      if (loaded[teller])
        {
          strcpy(lijst[grootte],port[teller]);
          if (!(strcmp(lijst[grootte],port[0]))) def=grootte;
          grootte++;  
        }
    }

  Userdata.User=&User[0];
  Userdata.Ram=&Ram[0];

  if (grootte>1)
    {
      /* DLGquery guess mode */

      sprintf(tempstring,"[%i;1H[1;37mView Info about user on port => ",(Statuswindowposy-1));

      fflush(stdout);

      Querydata.prompt=tempstring;
      Querydata.template=lijst[0];
      Querydata.string=uname;
      Querydata.defstring=(char *)def;
      Querydata.valid=" 0123456789EHLORSTUehlorstu";
      Querydata.length=16;
      Querydata.typelength=grootte;
      Querydata.flags=QUERY_GUESS+QUERY_UPCASE+QUERY_NOSTACK;

      def=DLGQuery(&Querydata,&Userdata);
      Ram[0].Command_Stack[0]=0x00;

      printf("[%i;1H[K",Statuswindowposy-1);

      if (def!=0)
        {
          for(teller = 0; teller <= ports; teller++)
            {
              if (!(strcmp(port[teller],uname)))
	        {
                  strcpy(name,Ram[teller].Name);
                }
	    }
        }
    }

  if ((grootte<=1) || (def==0))
      {

        if(FileSize("DLGConfig:Misc/Users.bbs",&size)==-1) strcpy(name,"Awoogha!");
        else
          {
            if (users=malloc(size))
	      {
                if(User_File = fopen("DLGConfig:Misc/Users.bbs", "r"))
                  {
                    fread(users, 36, (size/36), User_File);
                    fclose(User_File);

                    Who();
                    fflush(stdout);

                    sprintf(tempstring,"[%i;1H[1;37mView Info of which user => ",(Statuswindowposy-1));

                    Querydata.prompt=tempstring;
                    Querydata.template=(char *)users;
                    Querydata.string=name;
                    Querydata.defstring=(char *)0;
                    Querydata.valid="-'0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
                    Querydata.length=36;
                    Querydata.typelength=size/36;
                    Querydata.flags=QUERY_GUESS+QUERY_NOSTACK+QUERY_CAPITAL;
              
                    DLGQuery(&Querydata,&Userdata);
                    Ram[0].Command_Stack[0]=0x00;

                    printf("[%i;1H[K",Statuswindowposy-1);
                  }
		else
                  strcpy(name,"Awoogha!");
                free(users);
              }
            else
              strcpy(name,"Awoogha!");
             
	  }
      }
  Who();
  fflush(stdout);

  Capitalize(name);
  strcpy(uname,name);
  UnderScore(uname);

  sprintf(tempstring, "USER:%s/User.Data",uname);
  if(User_File = fopen(tempstring, "r"))
    {
      fread(&VUser, sizeof(struct USER_DATA), 1, User_File);
      fclose(User_File);
      mode=3;
      page=1;
      sprintf(textbuffer[0],"[KUsername[9C- [33m%s",name);
      sprintf(textbuffer[1],"[K[35mAlias[12C- [33m%s",VUser.Alias);
      sprintf(textbuffer[2],"[K[35mCity[13C- [33m%s",VUser.City);
      sprintf(textbuffer[3],"[K[35mBirthday[9C- [33m%u-%u-19%u",VUser.Birth_Day,VUser.Birth_Month,VUser.Birth_Year);
      sprintf(textbuffer[4],"[K[35mUserlevel[8C- [33m%u",VUser.User_Level);
      sprintf(textbuffer[5],"");
      sprintf(textbuffer[6],"[35mTime per day     - [33m%d [35mminutes ([33m%d [35mper login)",VUser.Daily_Limit,VUser.Session_Limit);
      sprintf(textbuffer[7],"Total Calls      - [33m%d [35m(Total time online: [33m%d [35mminutes)",VUser.Total_Calls,VUser.Online_Time);
      sprintf(textbuffer[8],"Last login       - [33m%s)",VUser.Last_Login);
      sprintf(textbuffer[9],"");
      if (VUser.Ratio==0)
        sprintf(textbuffer[10],"[35m[KFileratio[8C- [33mNo ratio!");
      else
        sprintf(textbuffer[10],"[35m[KFileratio[8C- [33m1:%u",VUser.Ratio);
      sprintf(textbuffer[11],"[35mFiles uploaded   - [33m%d [35m([33m%u [35mbytes)",VUser.Files_Uploaded,VUser.Bytes_Uploaded);
      sprintf(textbuffer[12],"[35mFiles downloaded - [33m%d [35m([33m%u [35mbytes)",VUser.Files_Downloaded,VUser.Bytes_Downloaded);
      sprintf(textbuffer[13],"");
      sprintf(textbuffer[14],"Messages entered - [33m%u",VUser.Messages_Entered);
      sprintf(textbuffer[15],"[35mMessages read    - [33m%u",VUser.Messages_Read);
      sprintf(textbuffer[16],"");
      sprintf(textbuffer[17],"[35mSysop Pages      - [33m%u",VUser.Sysop_Pages);

      textlines=18;
      pages=textlines/pagelength;
      if ((textlines%pagelength)!=0)
        pages++;

      Drawmodescreen();
    }
  else
    { 
      printf("[%i;1H[1;33mCouldn't open user data...[K",Statuswindowposy-1);
      fflush(stdout);
      Who();
      fflush(stdout);
      ReadChar(1500000);
    }
  printf("[%i;1H[1;37m[K",Statuswindowposy-1);
  
  strcpy(Ram[0].Action,actionstring);
  WriteRam(&Ram[0],port[0]);
}


/********************************************************************************/
/* Initialize 									*/
/********************************************************************************/

void Initialize()

{
  int teller;

  if(onbreak(&Break_Detected))
    {
      printf("Could not install ^C abort handler, exiting...");
      exit(100);
    }

  rawcon(1);

  /* Open dlg.library */
  DLGBase = OpenLibrary("dlg.library", 0L);

  if(GetDevName(port[0])==-1)
    {
      printf("Unable to determine port\n");
      Program_Break=128|Program_Break;			 /* Exit program */
    }
  Breakcheck();

  if(!ReadUser(&Ram[0],&User[0],port[0]))
    {
      printf("Unable to read Userdata\n");
      Program_Break=128|Program_Break;			 /* Exit program */
    }    
  Breakcheck();

  Readconfig();

  strcpy(actionstring,Ram[0].Action);

  if (strlen(portlist)%3!=0)
    {
      printf("Portlist isn't correct.\n");
      Program_Break=128|Program_Break;			 /* Exit program */
    }
  Breakcheck();

  teller=0;
  while (portlist[teller]!=0x00)
    {
      ports++;
      port[ports][0]=portlist[teller];
      port[ports][1]=portlist[teller+1];
      port[ports][2]=portlist[teller+2];
      port[ports][3]=0x00;
      teller=teller+3;
      onlinestring[ports][0]=0x00;
      loaded[ports]=FALSE;
    }
 
  if (ports==0)
    {
      printf("No ports specified in Portlist.\n");
      Program_Break=128|Program_Break;			 /* Exit program */
    }
  Breakcheck();

  if (User[0].Screen_Len>55) User[0].Screen_Len=55;
  if (User[0].Screen_Len<23)
    {
      printf("Your screenlength is too short.\n");
      printf("Minimum screenlength is 23 lines...\n");
      Program_Break=128|Program_Break;			 /* Exit program */
    }
  Breakcheck();

  Statuswindowposy=(User[0].Screen_Len-(2+ports));
  pagelength=Statuswindowposy-5;

  TSetFlags(T_CTLD,port[0]);
  TUnSetFlags(T_PAUSE,port[0]);

  for(teller = 0; teller <= 50; teller++)
    broadcasts[teller][0]=0x00;

  sprintf(bcstring,defbcstring);
}

#include "RD_Monitor_Routines.h"                  /* Include some extra functions */
