#include "md5.h"
#include "xgdipc.h"

void connect_to(GtkWidget *widget) {
  GtkWidget *window;
  GtkWidget *table;
  GtkWidget *button;
  GtkWidget *label1, *label2, *btnlabel;
  GtkWidget *box1;
  md5_state_t context;

  int sock, i, didx = 0, length;
  char buf[128];
  char *srv_out;
  char mode;
  char *sharedsecret;
  char server_userpass[128];
  unsigned char digest[16];
  unsigned char tmpdigest[16];
  char longdigest[35] = "";

  FILE *fout;

  if (GTK_TOGGLE_BUTTON(mode_update)->active) {
    mode = '0';
  } else if (GTK_TOGGLE_BUTTON(mode_offline)->active) {
    mode = '1';
  }

  if (GTK_TOGGLE_BUTTON(checkbutton)->active) {
    gdip_savepass = 'y';
  } else {
    gdip_savepass = 'n';
  }

  if (GTK_TOGGLE_BUTTON(checkbutton1)->active) {
    gdip_autoquit = 'y';
  } else {
    gdip_autoquit = 'n';
  }

  strcpy(gdip_username, gtk_entry_get_text(GTK_ENTRY(text1)));
  strcpy(gdip_server, gtk_entry_get_text(GTK_ENTRY(text3)));
  strcpy(gdip_domain, gtk_entry_get_text(GTK_ENTRY(text4)));

  if ((strcmp(gdip_pass, gtk_entry_get_text(GTK_ENTRY(text2)))) != 0) {
    strcpy(gdip_pass, gtk_entry_get_text(GTK_ENTRY(text2)));
    md5_init(&context);
    md5_append(&context, gdip_pass, strlen(gdip_pass));
    md5_finish(&context, tmpdigest);

    for (i = 0; i < 16; i++) {
      sprintf(longdigest, "%s%02x", longdigest, tmpdigest[i]);
    }
    strcpy(gdip_pass, longdigest);
  }

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_signal_connect(GTK_OBJECT(window), "destroy",
                     GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
  gtk_window_set_title(GTK_WINDOW(window), "X-GnuDIP2 Connecting");
  gtk_container_border_width(GTK_CONTAINER(window), 10);
  gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);

  table = gtk_table_new(6, 3, TRUE);
  gtk_container_add(GTK_CONTAINER(window), table);

  label1 = gtk_label_new("Connecting. . .");
  gtk_table_attach_defaults(GTK_TABLE(table), label1, 1, 2, 0, 1);
  gtk_widget_show(label1);

  label2 = gtk_label_new("Initalizing . . .");
  gtk_table_attach_defaults(GTK_TABLE(table), label2, 0, 3, 2, 3);
  gtk_widget_show(label2);

  button = gtk_button_new();

  btnlabel = gtk_label_new("Cancel");
  gtk_container_add(GTK_CONTAINER(button), btnlabel);
  gtk_widget_show(btnlabel);

  gtk_table_attach_defaults(GTK_TABLE(table), button, 1, 2, 5, 6);
  gtk_widget_show(button);

  gtk_widget_show(table);
  gtk_widget_show(window);

  gtk_label_set(GTK_LABEL(label2), "Connecting to host . . .");

  if ((sock = esock_new(gdip_server, 3495)) != -1) {
    gtk_label_set(GTK_LABEL(label2), "Waiting for SharedSecret . . .");

    sharedsecret = esock_read(sock);
    sharedsecret[strlen(sharedsecret) - 1] = '\0';

    snprintf(buf, 127, "%s.%s", gdip_pass, sharedsecret);
    md5_init(&context);
    md5_append(&context, buf, strlen(buf));
    md5_finish(&context, digest);

    longdigest[0] = '\0';
    for (i = 0; i < 16; i++) {
      sprintf(longdigest, "%s%02x", longdigest, digest[i]);
    }

    gtk_label_set(GTK_LABEL(label2), "Encrypting Password . . .");

    snprintf(server_userpass, 127, "%s:%s:%s:%c\n", gdip_username, longdigest,
             gdip_domain, mode);

    gtk_label_set(GTK_LABEL(label2), "Sending Username and Password . . .");

    esock_write(sock, server_userpass);

    gtk_label_set(GTK_LABEL(label2), "Checking status . . .");
    srv_out = esock_read(sock);

    if (srv_out[0] == '1') {
      gtk_label_set(GTK_LABEL(label2), "ERROR: Invalid Login");

      gtk_signal_connect(GTK_OBJECT(button), "clicked",
                         GTK_SIGNAL_FUNC(restore_window), window);

    } else if (srv_out[0] == '0') {
      if ((fout = fopen(path_xgdip2, "w")) != NULL) {
        if (GTK_TOGGLE_BUTTON(checkbutton)->active) {
          fprintf(fout, "%c %c %s %s %s %s", gdip_savepass, gdip_autoquit,
                  gdip_server, gdip_username, gdip_domain, gdip_pass);
        } else {
          fprintf(fout, "%c %c %s %s %s x", gdip_savepass, gdip_autoquit,
                  gdip_server, gdip_username, gdip_domain);
        }
        fclose(fout);
      } else
        printf("Unable to open %s\n", path_xgdip2);
      gtk_label_set(GTK_LABEL(label2), "Successful Hostname Update");

      gtk_signal_connect(GTK_OBJECT(button), "clicked",
                         GTK_SIGNAL_FUNC(gtk_main_quit), NULL);

      gtk_label_set(GTK_LABEL(btnlabel), "Close");

      if (GTK_TOGGLE_BUTTON(checkbutton1)->active)
        gtk_main_quit();

    } else if (srv_out[0] == '2') {

      fout = fopen(path_xgdip2, "w");
      if (GTK_TOGGLE_BUTTON(checkbutton)->active) {
        fprintf(fout, "%c %c %s %s %s %s", gdip_savepass, gdip_autoquit,
                gdip_server, gdip_username, gdip_domain, gdip_pass);
      } else {
        fprintf(fout, "%c %c %s %s %s x", gdip_savepass, gdip_autoquit,
                gdip_server, gdip_username, gdip_domain);
      }
      fclose(fout);

      gtk_label_set(GTK_LABEL(label2), "Successfully Set Offline");

      gtk_signal_connect(GTK_OBJECT(button), "clicked",
                         GTK_SIGNAL_FUNC(gtk_main_quit), NULL);

      gtk_label_set(GTK_LABEL(btnlabel), "Close");

      if (GTK_TOGGLE_BUTTON(checkbutton1)->active)
        gtk_main_quit();

    } else if (strncmp(srv_out, "-1", 2) == 0) {
      gtk_label_set(GTK_LABEL(label2), "A Server Error Occurred");

      gtk_signal_connect(GTK_OBJECT(button), "clicked",
                         GTK_SIGNAL_FUNC(restore_window), window);

    } else {
      gtk_label_set(GTK_LABEL(label2), srv_out);
      gtk_signal_connect(GTK_OBJECT(button), "clicked",
                         GTK_SIGNAL_FUNC(restore_window), window);
      gtk_label_set(GTK_LABEL(btnlabel), "Ok");
    }
  } else {
    gtk_label_set(GTK_LABEL(label2), "ERROR: Could Not Connect To Host");
    gtk_signal_connect(GTK_OBJECT(button), "clicked",
                       GTK_SIGNAL_FUNC(restore_window), window);
    gtk_label_set(GTK_LABEL(btnlabel), "Ok");
  }
}
