
struct message_header_type  {
  char fromUserName[36];
  char toUserName[36];
  char subject[72];
  char DateTime[20];
  unsigned char timesRead_loworder;
  unsigned char timesRead_highorder;
  unsigned char destNode_loworder;
  unsigned char destNode_highorder;
  unsigned char origNode_loworder;
  unsigned char origNode_highorder;
  unsigned char cost_loworder;
  unsigned char cost_highorder;
  unsigned char origNet_loworder;
  unsigned char origNet_highorder;
  unsigned char destNet_loworder;
  unsigned char destNet_highorder;
  char fill[8];
  unsigned char replyTo_loworder;
  unsigned char replyTo_highorder;
  unsigned char AttributeWord_loworder;
  unsigned char AttributeWord_highorder;
  unsigned char nextReply_loworder;
  unsigned char nextReply_highorder;
  }  incoming_header,outgoing_header;

char flag_private=0,flag_crash=0,flag_recd=0,flag_sent=0,flag_fileattached=0,
  flag_intransit=0,flag_orphan=0,flag_killsent=0,flag_local=0,
  flag_holdforpickup=0,flag_unused=0,flag_filerequest=0,
  flag_returnreceiptrequest=0,flag_isreturnreceipt=0,flag_auditrequest=0,
  flag_fileupdatereq=0;   /* Used for decoding message flag bits. */

FILE *message_body_file;
char message_body_filename[]="fidonet.tmp";
char NULL_string[1],CRLF_string[3];

void decode_attribute_word(unsigned short the_word)
{
unsigned short temp_word;
temp_word = the_word;
flag_private = 0;    /* First init all to 0. */
flag_crash = 0;
flag_recd = 0;
flag_sent = 0;
flag_fileattached = 0;
flag_intransit = 0;
flag_orphan = 0;
flag_killsent = 0;
flag_local = 0;
flag_holdforpickup = 0;
flag_unused = 0;
flag_filerequest = 0;
flag_returnreceiptrequest = 0;
flag_isreturnreceipt = 0;
flag_auditrequest = 0;
flag_fileupdatereq = 0;
if (temp_word >= 32768) { temp_word -= 32768; flag_fileupdatereq        = 1; }
if (temp_word >= 16384) { temp_word -= 16384; flag_auditrequest         = 1; }
if (temp_word >= 8096)  { temp_word -= 8096;  flag_isreturnreceipt      = 1; }
if (temp_word >= 4096)  { temp_word -= 4096;  flag_returnreceiptrequest = 1; }
if (temp_word >= 2048)  { temp_word -= 2048;  flag_filerequest          = 1; }
if (temp_word >= 1024)  { temp_word -= 1024;  flag_unused               = 1; }
if (temp_word >= 512)   { temp_word -= 512;   flag_holdforpickup        = 1; }
if (temp_word >= 256)   { temp_word -= 256;   flag_local                = 1; }
if (temp_word >= 128)   { temp_word -= 128;   flag_killsent             = 1; }
if (temp_word >= 64)    { temp_word -= 64;    flag_orphan               = 1; }
if (temp_word >= 32)    { temp_word -= 32;    flag_intransit            = 1; }
if (temp_word >= 16)    { temp_word -= 16;    flag_fileattached         = 1; }
if (temp_word >= 8)     { temp_word -= 8;     flag_sent                 = 1; }
if (temp_word >= 4)     { temp_word -= 4;     flag_recd                 = 1; }
if (temp_word >= 2)     { temp_word -= 2;     flag_crash                = 1; }
if (temp_word >= 1)     { temp_word -= 1;     flag_private              = 1; }
}


int init_message_body_file()
{
char program_id_line[]="1:397/5264 -- Sysop/programmer: Alton Moore";
CRLF_string[0] = 13;
CRLF_string[1] = 10;
CRLF_string[2] = 0;
message_body_file = fopen(message_body_filename,"wb");
if (message_body_file==NULL)
	{
	printf("?Error initing message_body_file %s\n",message_body_filename);
	return(1);
	}
fwrite(program_id_line,strlen(program_id_line),1,message_body_file);
fwrite(CRLF_string,1,1,message_body_file);
fclose(message_body_file);  /* Leaving behind a 0-byte file. */
return(0);
}

int add_to_message_body_file(char *the_string)
{
CRLF_string[0] = 13;
CRLF_string[1] = 10;
CRLF_string[2] = 0;
message_body_file = fopen(message_body_filename,"rb+");
if (message_body_file==NULL)
	{
	printf("?Error opening message_body_file %s to write string %s\n",message_body_filename,the_string);
	return(1);
	}
fseek(message_body_file,0,SEEK_END);
fwrite(the_string,strlen(the_string),1,message_body_file);
fwrite(CRLF_string,2,1,message_body_file);  /* Dos files need CR/LF at EOL. */
fclose(message_body_file);
return(0);
}




/* This routine returns non-zero if a problem occurred. */
int fidonet_send_message(char *fromUserName,   /* Usually the name of this program.  */
                         char *toUserName,
                         char *filespec_to_send_as_message_body,
                         char *filespec_to_attach_to_message,
                         char *netmail_directory_spec,
                         char destNode_loworder,
                         char destNode_highorder,
                         char origNode_loworder,
                         char origNode_highorder,
                         char origNet_loworder,
                         char origNet_highorder,
                         char destNet_loworder,
                         char destNet_highorder,
                         char debug_flag)
{
FILE *outgoing_file;
char temp_char,temp_string[100],outgoing_file_name[64];
int message_number=1;
div_t results;

NULL_string[0] = 0;  /* Initialize this string */
while (1)   /* Do this until we break out. */
  {
  strcpy(outgoing_file_name,netmail_directory_spec);
  itoa(message_number,temp_string,10);
  strcat(outgoing_file_name,temp_string);
  strcat(outgoing_file_name,".MSG");
  outgoing_file = fopen(outgoing_file_name,"rb");
  if (outgoing_file == NULL)   /* Found no message file?  */
    break;
  fclose(outgoing_file);     /*  Close and look for next. */
  message_number++;
  }
if (debug_flag)
  printf("-Creating message %d.\n",message_number);
outgoing_file = fopen(outgoing_file_name,"wb");
if (outgoing_file == NULL)
  {
  printf("?Error opening outgoing message file %s\n",outgoing_file_name);
  return(1);
  }
memset(&outgoing_header,0,sizeof(struct message_header_type)); /* Init header to nulls. */
if (filespec_to_attach_to_message[0])
  {
  results = div(401,256);
  strcpy(outgoing_header.subject,filespec_to_attach_to_message);
  }
else
  {
  results = div(385,256);
  }
outgoing_header.AttributeWord_loworder  = results.rem;
outgoing_header.AttributeWord_highorder = results.quot;
strncpy(outgoing_header.fromUserName,fromUserName,sizeof(outgoing_header.fromUserName));
strncpy(outgoing_header.toUserName,toUserName,sizeof(outgoing_header.toUserName));
outgoing_header.destNode_loworder  = origNode_loworder;
outgoing_header.destNode_highorder = origNode_highorder;
outgoing_header.origNode_loworder  = destNode_loworder;
outgoing_header.origNode_highorder = destNode_highorder;
outgoing_header.origNet_loworder   = destNet_loworder;
outgoing_header.origNet_highorder  = destNet_highorder;
outgoing_header.destNet_loworder   = origNet_loworder;
outgoing_header.destNet_highorder  = origNet_highorder;
/* Write outgoing header. */
if (debug_flag)
  printf(" Writing outgoing message header.\n");
fwrite(&outgoing_header,sizeof(struct message_header_type),1,outgoing_file);
/* Write null message, message body from file, or message body from string after ';'. */
switch (filespec_to_send_as_message_body[0])
  {
  case 0  : {
            if (debug_flag)
              printf(" No message body file; null message.\n");
            break;
            }
  case ';': {
            if (debug_flag)
              printf(" Writing single string to message body.\n");
            fwrite(filespec_to_send_as_message_body+1,strlen(filespec_to_send_as_message_body+1),1,outgoing_file); /* If ; is 1st char. of this field, this field is a message instead of a filename. */
            break;
            }
  default : {
            if (debug_flag)
              printf(" Sending %s as message body.\n",filespec_to_send_as_message_body);
            message_body_file = fopen(filespec_to_send_as_message_body,"rb");
            if (message_body_file == NULL)
              {
              printf("?Could not open message_body_file %s.\n",filespec_to_send_as_message_body);
              return(1);
              }
            while (1)    /* Read character-by-character! */
              {
              temp_char = fgetc(message_body_file);
              if (temp_char == EOF)
                break;
              fwrite(&temp_char,1,1,outgoing_file);
              }
            fclose(message_body_file);
            /* unlink(message_body_filename); */  /* Save for now. */
            }
  }
/* Close outgoing message. */
fwrite(NULL_string,1,1,outgoing_file);  /* Write terminating null to message. */
fclose(outgoing_file);
if (debug_flag)
  printf(" Wrote terminating null and closed outgoing file.\n");
return(0);
}

