/* Author: Ram Samudrala (me@ram.org)
 * Version: O1.0
 * Detail: <http://www.ram.org/computing/login/login.html>
 * November 22, 1996.
 *
 * See the URL above for more information.
 *
 * O1.0 - initial register script
 */

#include "cgi_common.h"
#include "cgi_defines.h"
#include "cgi_error_handlers.h"
#include "cgi_display.h"
#include "cgi_password.h"
#include "login_defines.h"

int content_type_displayed = FALSE;

void addnewuser();

/******************************************************************/

int main()
{
  check_method("POST", "main");
  check_content_type("main");
  addnewuser();

  return TRUE;
}

/******************************************************************/

void addnewuser()
{
  entry entries[MAX_ENTRIES];
  FILE *login_fp, *group_fp, *tmp_fp;
  int number_of_entries, i;
  char line[MAX_GROUP_LINE_LENGTH], email[STRING_LENGTH], username[STRING_LENGTH];
  char buf[STRING_LENGTH];
  
  number_of_entries = read_entries(entries);

  for(i = 0; i <= number_of_entries; i++)
    {
      if (entries[i].val[0] == '\0')
	display_error("addnewuser(): unspecified field encountered; you must enter values for all fields!");

      if (strcmp(entries[i].name, "Username") == 0)
	strcpy(username, entries[i].val);
      if (strcmp(entries[i].name, "E-mail") == 0)
	strcpy(email, entries[i].val);
      if (strstr(entries[i].val, PROFILE_DELIMITER) != NULL)
	{
	  sprintf(line, "addnewuser(): forbidden character (%s) in entry %s!", 
		  PROFILE_DELIMITER, entries[i].name);
	  display_error(line);
	}
    }

  /* Build in a locking feature here? */

  open_file(&group_fp, GROUP_FILENAME, "r", "addnewuser");
  fgets(line, MAX_GROUP_LINE_LENGTH, group_fp);
  close_file(&group_fp, GROUP_FILENAME, "addnewuser");
  if (strstr(line, username) != NULL)
    display_error("addnewuser(): username already exists; please try again with a new username!");

  line[strlen(line)-1] = '\0';
  strcat(line, " ");
  strcat(line, username);

  open_file(&group_fp, GROUP_FILENAME, "w", "addnewuser");
  fprintf(group_fp, "%s\n", line);
  close_file(&group_fp, GROUP_FILENAME, "addnewuser");

  open_file(&login_fp, PROFILE_FILENAME, "a", "addnewuser");
  for(i = 0; i <= number_of_entries; i++)
    {
      fprintf(login_fp, "%s%s", entries[i].val, PROFILE_DELIMITER);
    }
  fprintf(login_fp, "\n");
  close_file(&login_fp, PROFILE_FILENAME, "addnewuser");

  /* Add the password to the password file using routines from htpasswd */
  sprintf(buf, "%d", (int) getpid());
  add_password(username, buf, PASSWORD_FILENAME);

  sprintf(buf, "%s.%d", TMP_FILENAME_STRING, (int) getpid());
  open_file(&tmp_fp, buf, "w", "addnewuser");
  fprintf(tmp_fp, "Subject: Registration conformation\n\n");
  fprintf(tmp_fp, "Here is the registration information you have submitted:\n\n");
  for(i = 0; i <= number_of_entries; i++)
    fprintf(tmp_fp, "%s: %s\n", entries[i].name, entries[i].val);
  fprintf(tmp_fp, "\nYour password is %d\n\n", (int) getpid());
  close_file(&tmp_fp, buf, "addnewuser");
  sprintf(line, "%s %s < %s", SENDMAIL_LOCATION, email, buf);
  system(line); 
  remove(buf);

  display_content_type("Successful registration!");
  printf("<h1> Your registration was successful! </h1>\n");
  printf("<hr>\n");
  printf("<p> You will be sent an e-mail message with a default password shortly.\n");
  printf("Once you have this password, you can then login and change/edit your profile.\n");
  printf("</p>\n");
  display_signature();
  
  return;
}
      
/******************************************************************/
