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



#include "global.h"
#include "custom.h"		/* for date FORMAT strings */

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_authenticate.h"
#include "manhat-lib/shared_strtrm.h"
#include "manhat-lib/shared_cs_util.h"



static void
read_parameters(char *key, char *password, int *verify)
{
   char temp[10];
   cs_get_required_parameter("id", key, MAX_KEY);    /* shared_cgi_util.c */
   
   cs_get_optional_parameter("verify",temp,9);  /* shared_cgi_util.c */
   if(strlen(temp))
    *verify = atoi(temp);
   else
    *verify =0;
   if(*verify >0)
   {
      cs_get_required_parameter("password",password,MAX_PASSWORD);  /* shared_cgi_util.c */
      strtrm(password);
   }

}




static void write_melange_conf(const char *password)
{

#define STANDARD_CONF "# UNIQUE no\n"\
                      "CHANGENICKS no\n"\
                      "# IDLETIME 0\n"\
                      "# GUESTLOGIN yes\n"\
                      "ANONYMOUS no\n"\
                      "# ALLOWCHANNELS yes\n"\
                      "# KICKOUTTIME 3600\n"\
		      "LOGFILE ./melange.log\n"\
                      "# MSGLOGFILE ./melange_msg.log\n"\
                      "#LOGLEVEL 3\n"\
                      "# SYSMSG off\n"\
                      "# PROFILE deny\n"\
                      "# ALLOW trusted.com\n"\
                      "# DENY microsoft.com\n"
                      



FILE *fp;


  
  fp = fopen(CHAT_CONF_PATH, "w");
  if(!fp)
     cs_critical_error(ERR_FOPEN_WRITE_FAILED, "");

  /* write the password to line 1 */

  if(strlen(password))
      fprintf(fp,"PASSWD %s\n", password);
     
     
 /* now re-write the rest of the file with standard values */
 
   fprintf(fp, STANDARD_CONF);
       
  fclose(fp);
              
#undef STANDARD_CONF
              
}       
          
     


int
main ()
{
  char key[MAX_KEY + 1];	/* from id=? command line */
  SESSION user; 
  char password[MAX_PASSWORD + 1];    /* from command line */
  int verify =0;

  cs_cgi_init();
  read_parameters (key, password, &verify);
  validate_super_key (key, &user);	/* shared_super_util.c*/
  if(verify)
    write_melange_conf ( password);
  else
    cs_set_int_value("display_form",1);

  cs_cgi_display("super_chat_passwd", 1);
  cs_cgi_destroy();
    
  return 0;			/* exit successfully */

}

