
/* This program makes a new fidonet message file in the current directory. */
/*  It has a null message on it with the  FLAGS KFS  kludge line (Kill     */
/*  File Sent).  I usually use the message attribute word 401, which is    */
/*  256 (Local) + 128 (KillSent) + 16 (FileAttached) + 1 (Private).        */
/*  Your mailer might see the KFS kludge line a little strangely if the    */
/*  FileAttached bit is not set so choose an appropriate value!            */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>

FILE *message_file;

struct message_header_type  {
  char fromUserName[36];
  char toUserName[36];
  char subject[72];
  char DateTime[20];
  char timesRead_loworder;
  char timesRead_highorder;
  char destNode_loworder;
  char destNode_highorder;
  char origNode_loworder;
  char origNode_highorder;
  char cost_loworder;
  char cost_highorder;
  char origNet_loworder;
  char origNet_highorder;
  char destNet_loworder;
  char destNet_highorder;
  char fill[8];
  char replyTo_loworder;
  char replyTo_highorder;
  char AttributeWord_loworder;
  char AttributeWord_highorder;
  char nextReply_loworder;
  char nextReply_highorder;
  char text[64];
  }  message_header;
/* Text field is not part of the header, but I have stuck it in there  */
/*  and left it a fixed size for now -- good enough for this program.  */

int message_number = 0;    /* The message number to create; we search from  */
                           /*  1 until an "unused" number is found.         */
int temp_int;
char temp_string[80],
  message_file_suffix[]=".msg",
  message_file_name[64];
div_t results;


/*---------------------------- Subroutines --------------------------------*/

int get_numeric_input()     /* Returns 0 if the input is "bad". */
{
char the_input[8];
int temp;
gets(the_input);
temp = atoi(the_input);
return(temp);
}


/*--------------------------- The Code -------------------------------------*/
int main()
{
message_file = NULL;   /* Find the next message number useable.... */
while (1)   /* Do this until we break out. */
  {
  message_number++;
  message_file_name[0] = 0;  /* Init. the filename. */
  itoa(message_number,temp_string,10);
  strcat(message_file_name,temp_string);
  strcat(message_file_name,message_file_suffix);
  message_file = fopen(message_file_name,"rb");
  if (message_file != NULL)   /* Found a message file?     */
	fclose(message_file);     /*  Close and look for next. */
  else
	break;
  }

memset(&message_header,0,sizeof(struct message_header_type));  /* Init to nulls. */

printf("From user? ");
gets(temp_string);
strncpy(message_header.fromUserName,temp_string,sizeof(message_header.fromUserName));
printf("From user %s\n",temp_string);

printf("To user?   ");
gets(temp_string);
strncpy(message_header.toUserName,temp_string,sizeof(message_header.toUserName));
printf("To user %s\n",temp_string);

printf("Subject / attached file(s)? ");
gets(temp_string);
strncpy(message_header.subject,temp_string,sizeof(message_header.subject));
printf("Subject / attached file(s): ",temp_string);

printf("Originating net? ");
temp_int = get_numeric_input();
results = div(temp_int,256);
message_header.origNet_loworder  = results.rem;
message_header.origNet_highorder = results.quot;
printf("Originating net: %d\n",temp_int);

printf("           node? ");
temp_int = get_numeric_input();
results = div(temp_int,256);
message_header.origNode_loworder  = results.rem;
message_header.origNode_highorder = results.quot;
printf("Originating node: %d\n",temp_int);

printf("Destination net? ");
temp_int = get_numeric_input();
results = div(temp_int,256);
message_header.destNet_loworder  = results.rem;
message_header.destNet_highorder = results.quot;
printf("Destination node: %d\n",temp_int);

printf("           node? ");
temp_int = get_numeric_input();
results = div(temp_int,256);
message_header.destNode_loworder  = results.rem;
message_header.destNode_highorder = results.quot;
printf("Destination node: %d\n",temp_int);

printf("Attribute word value? ");
temp_int = get_numeric_input();
results = div(temp_int,256);
message_header.AttributeWord_loworder  = results.rem;
message_header.AttributeWord_highorder = results.quot;
printf("Attribute word value: %d\n",temp_int);

message_header.text[0] = 01;
strncpy(message_header.text+1,"FLAGS KFS",9);
message_header.text[10] = 13;
/*
  This next line is commented out so that the mailer on the receiving
  end will delete the null file-attach message after it gets there (if
  the mailer is set to do this).
strncpy(message_header.text+11,"This message created by MAKEMESS 1.0",sizeof(message_header.text)-11);
*/
message_header.text[11] = 0;

printf("Creating message file name %s in the current directory....\n",message_file_name);
message_file = fopen(message_file_name,"wb");   /* Create the file. */
if (message_file == NULL)  {
  printf("Error creating message file!\n");
  return(1);               }
fwrite(&message_header,sizeof(struct message_header_type),1,message_file);
fclose(message_file);
return(0);
}

