/*
   changed list

  made it so that user can enter blank line on the To: prompt to cancel the message..

  SYSOP is valid in the TO: string..

*/

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

#include <dos/dos.h>
#include <exec/types.h>
#include <exec/memory.h>

#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>

#include <HBBS/ANSI_Codes.h>
#include <HBBS/Defines.h>
#include <HBBS/types.h>
#include <HBBS/structures.h>
#include <HBBS/hbbscommon_protos.h>
#include <HBBS/hbbscommon_pragmas.h>
#include <HBBS/Hbbsnode_protos.h>
#include <HBBS/Hbbsnode_pragmas.h>

#include "maildoor.h"
#include "mailscan.h"
#include "mailread.h"
#include "mailedit.h"

char UserNames[MAX_PATH_LENGTH];
char UserIDs[MAX_PATH_LENGTH];

int MAILEDIT_CheckValidUser( char *UserName )
{
  int ValidsFound = 0, r=0;
  char str[BUFFER_LENGTH], temp[BUFFER_LENGTH],*copyofname, outstr[BUFFER_LENGTH];
  struct UserData UserDat;
  BOOL Done=FALSE;
  short where;

  if (copyofname=DupStr(UserName))
  {
    do
    {
      str[0]=0;
      if ((where=position(",",copyofname))>=0)
      {
        strNcpy(str,copyofname,where);
        strfcpy(copyofname,copyofname,where+1);
      }
      else
      {
        Done=TRUE;
        strcpy(str,copyofname);
      }
      if (str[0])
      {
        if (stricmp("SYSOP",str)==0)  // replace SYSOP string with sysop's name..
        {
          strcpy(str,BBSGlobal->SysopAccount);
        }


        if( HBBS_ValidUserHandle( str, &UserDat ) )
        {
          ValidsFound++;
          sprintf( temp, "%d", UserDat.UserID );
          if( ValidsFound > 1 )
          {
            strcat( UserIDs, "," );
            strcat( UserNames, "," );
          }
          strcat( UserIDs, temp );
          strcat( UserNames, str );
        }
        else
        {
          sprintf( outstr, ANSI_FG_RED" ERROR : "ANSI_FG_PURPLE"%s is an invalid name!\r\n\r\n"ANSI_FG_YELLOW"Enter name to replace %s with "ANSI_FG_WHITE":", str, str );
          DOOR_WriteText( outstr );
          DOOR_GetLine( TRUE, TRUE, 0 );
          if( N_ND->OnlineStatus==OS_ONLINE )
          {
            if( N_ND->CurrentLine[0] )
            {
              if( ValidsFound >= 1 )
              {
                strcat( UserIDs, "," );
                strcat( UserNames, "," );
              }
              r = MAILEDIT_CheckValidUser( N_ND->CurrentLine );
              if( r == 0 )
              {
                if( ValidsFound > 1)
                {
                  UserIDs[strlen(UserIDs)-1]=0;
                  UserNames[strlen(UserNames)-1]=0;
                }
              }
              else
                ValidsFound += r;
            }
          }
        }
      }
      else
      {
        Done=TRUE;
      }

    } while (!Done);

    FreeStr(copyofname);
  }
  return( ValidsFound );
}

BOOL MAILEDIT_LineEditor( char *filename )
{
  char outstr[BUFFER_LENGTH];
  BOOL Done = FALSE;
  BPTR file;
  int n, N=0;

  if( file = Open( filename, MODE_NEWFILE ))
  {
    DOOR_WriteText( "\r\n    /----"ANSI_FG_GREEN"*"ANSI_FG_WHITE"----"ANSI_FG_GREEN"*"ANSI_FG_WHITE"----"ANSI_FG_GREEN"*"ANSI_FG_WHITE"----"ANSI_FG_GREEN"*"ANSI_FG_WHITE"----"ANSI_FG_GREEN"*"ANSI_FG_WHITE"----"ANSI_FG_GREEN"*"ANSI_FG_WHITE"----"ANSI_FG_GREEN"*"ANSI_FG_WHITE"----"ANSI_FG_GREEN"*"ANSI_FG_WHITE"----"ANSI_FG_GREEN"*"ANSI_FG_WHITE"----"ANSI_FG_GREEN"*"ANSI_FG_WHITE"----"ANSI_FG_GREEN"*"ANSI_FG_WHITE"----"ANSI_FG_GREEN"*"ANSI_FG_WHITE"----"ANSI_FG_GREEN"*"ANSI_FG_WHITE"----"ANSI_FG_GREEN"*"ANSI_FG_WHITE"----/\r\n\r\n" );
    n=0;
    while( !Done )
    {
      n++;
      sprintf( outstr, "%03d ", n );
      DOOR_WriteText( outstr );
      DOOR_GetLine( TRUE, TRUE, 0 );
      if( N_ND->OnlineStatus==OS_ONLINE )
      {
        if( N_ND->CurrentLine[0] )
        {
          sprintf( outstr, "%s\n", N_ND->CurrentLine );
          FPuts( file, outstr );
          N=0;
        }
        else
        {
          N++;
          if( N==2 ) Done = TRUE;
        }
      }
      else Done = TRUE;
    }
    Close( file );
  }
  return( TRUE );
}

BOOL MAILEDIT_SaveMessageData( struct MAIL_Message *MM, int ConfNum, int MsgNum )
{
  char outstr[BUFFER_LENGTH], filename[MAX_PATH_LENGTH];
  struct Node *ConfPtr;
  struct CfgFileData *CfgFile;

  ConfPtr = GetNode( BBSGlobal->ConfList, ConfNum-1 );

  sprintf( filename, "%sMail/Messages/%d.DAT\0", ((struct ConfData *)ConfPtr)->ConfPath, MsgNum );

  if( CfgFile = HBBS_CreateConfig( filename) )
  {
    if( HBBS_AddCfgItem( CfgFile, "From", MM->MM_From ))
    {
      if( HBBS_AddCfgItem( CfgFile, "For", MM->MM_For ))
        if( HBBS_AddCfgItem( CfgFile, "ForIDs", MM->MM_ForIDs ))
          if( HBBS_AddCfgItem( CfgFile, "Subject", MM->MM_Subject ))
          {
            sprintf( outstr, "%d", MM->MM_Read );
            if( HBBS_AddCfgItem( CfgFile, "Read", outstr ))
            {
              sprintf( outstr, "%d", MM->MM_DownLoaded );
              if( HBBS_AddCfgItem( CfgFile, "Downloaded", outstr ))
              {
                sprintf( outstr, "%d", MM->MM_FilesDL );
                if( HBBS_AddCfgItem( CfgFile, "FilesDL", outstr ))
                {
                  sprintf( outstr, "%d", MM->MM_NAttachedFiles );
                  HBBS_AddCfgItem( CfgFile, "NAttached", outstr );
                }
              }
            }
          }

      HBBS_SaveConfig( CfgFile );
    }
    HBBS_FlushConfig( CfgFile );
  }
  else return( FALSE );
  return( TRUE );
}

void MYDOOR_DoEnterMessage( char *ToName )
{
  char outstr[BUFFER_LENGTH],filename[MAX_PATH_LENGTH], NAME[BUFFER_LENGTH];
  struct MAIL_Message *NewMessage;
  int ConfNum, MsgNum, FirstMessage, LastMessage;
  struct Node *ConfPtr;

  ConfNum = N_ND->CurrentConf->ConfNum;
  ConfPtr = GetNode( BBSGlobal->ConfList, ConfNum-1 );

  FirstMessage = MAILSCAN_GetFirstMessage( ConfNum );
  LastMessage = MAILSCAN_GetLastMessage( ConfNum );

  MsgNum = LastMessage + 1;

  if( NewMessage = AllocVec( sizeof( struct MAIL_Message ), MEMF_CLEAR|MEMF_PUBLIC ))
  {
    strcpy( NewMessage->MM_From, N_ND->User.CallData.Handle );
    NewMessage->MM_Read = 0;
    NewMessage->MM_DownLoaded = 0;
    NewMessage->MM_FilesDL = 0;
    NewMessage->MM_NAttachedFiles = 0;

    UserIDs[0] = 0;
    UserNames[0] = 0;
    do
    {
      DOOR_WriteText(ANSI_FG_YELLOW "To      "ANSI_FG_WHITE":" );
      if( ToName == NULL )
      {
        DOOR_GetLine( TRUE, TRUE, 0 );
        strcpy( NAME, N_ND->CurrentLine );
      }
      else
      {
        DOOR_WriteText( "SYSOP\r\n" );
        strcpy( NAME, ToName );
      }
    }while((N_ND->CurrentLine[0]) && (!(MAILEDIT_CheckValidUser( NAME )))&&(N_ND->OnlineStatus==OS_ONLINE));
    if(N_ND->OnlineStatus==OS_ONLINE)
    {
      if (N_ND->CurrentLine[0]==0)
      {
        DOOR_WriteText("Message Cancelled\n\r");
      }
      else
      {
        strcpy( NewMessage->MM_ForIDs, UserIDs );
        strcpy( NewMessage->MM_For, UserNames );
        DOOR_WriteText(ANSI_FG_YELLOW "Subject "ANSI_FG_WHITE":" );
        DOOR_GetLine( TRUE, TRUE, 0 );
        strcpy( NewMessage->MM_Subject, N_ND->CurrentLine );
        if( N_ND->OnlineStatus==OS_ONLINE )
        {
          sprintf( filename, "%sMail/Messages/%d.TXT\0", ((struct ConfData *)ConfPtr)->ConfPath, MsgNum );
          if( MAILEDIT_LineEditor( filename ))
          {
            strcpy( NewMessage->MM_ForIDs, UserIDs );
            if( !(MAILEDIT_SaveMessageData( NewMessage, ConfNum, MsgNum )))
            {
              DOOR_WriteText( ANSI_FG_RED"Problems Saving Message\r\n"ANSI_FG_WHITE );
            }
            else
            {
              sprintf( outstr, "%d", MsgNum );
              if( (FirstMessage == 0 ) && (LastMessage == 0 ) )
                MAILREAD_UpdateConfMailData( ConfNum, FirstMessage, LastMessage, outstr, MailSys_Both );
              else
                MAILREAD_UpdateConfMailData( ConfNum, FirstMessage, LastMessage, outstr, MailSys_OFFSET_LastMessage );
              DOOR_WriteText( ANSI_FG_GREEN"Message Saved!\r\n"ANSI_FG_WHITE );
            }
          }
        }
      }
    }
  }
}
