#include "tor_ctl.h"

int tor_ctl_connect(struct tor_ctl* ctx, char* addr, char* service) {
  int sock = create_tcp_client (addr, service);
  int sock2 = dup(sock);
  ctx->rsock = NULL;
  ctx->wsock = NULL;

  ctx->rsock = fdopen(sock, "r");
  if (ctx->rsock == NULL) {
    return -1;
  }
  setbuf(ctx->rsock, NULL);

  ctx->wsock = fdopen(sock2, "w");
  if (ctx->wsock == NULL) {
    return -1;
  }
  setbuf(ctx->wsock, NULL);

  fprintf (ctx->wsock, "authenticate \"\"\n");
  int error_code = 0;
  fscanf (ctx->rsock, "%d", &error_code);
  if (error_code != 250) {
    tor_ctl_close (ctx);
    return -1;
  }
  fscanf(ctx->rsock," OK");

  return 0;
}

void tor_ctl_list_onions(struct tor_ctl* ctx) {
  fprintf(ctx->wsock, "getinfo onions/current\n");

  char delimiter, buffer[1024] = {0};
  fscanf(ctx->rsock, " 250%c", &delimiter);
  if (delimiter == '-') {
    fscanf(ctx->rsock, "onions/current=%s", buffer);
    if (strlen(buffer) > 0) printf("key: %s\n", buffer);
  } else if (delimiter == '+') {
    fscanf(ctx->rsock, "onions/current= ");
    while (1) {
      fgets(buffer, 1024, ctx->rsock);
      if (strcmp(buffer, "250 OK\r\n") == 0) break;
      if (strcmp(buffer, ".\r\n") == 0) continue;
      printf("line: %s", buffer);
    }
  } else {
    printf("deli:%c\n", delimiter);
  }
}

int tor_ctl_add_onion(struct tor_ctl* ctx, struct tor_os_str* tos, uint16_t* port, uint64_t port_per_os, enum TOR_ONION_FLAGS flags) {
  int err = 0;
  char buffer1[1024] = {0};
  char buffer2[1024] = {0};
  int to_create = tos->size - tos->filled;

  /* Add onion services loaded from file */
  for (int i = 0; i < tos->filled; i++) {
    fprintf(ctx->wsock, "add_onion %s ", tos->keys[i].priv);
    for (int j = 0; j < port_per_os; j++) {
      fprintf(ctx->wsock, "Port=%d,127.13.3.7:%d ", port[i*port_per_os+j], port[i*port_per_os+j]);
    }
    if (flags == TOR_ONION_FLAG_NONE) fprintf(ctx->wsock, "\n");
    else {
      fprintf(ctx->wsock, "Flags=");
      if (flags & TOR_ONION_FLAG_NON_ANONYMOUS) fprintf(ctx->wsock, "NonAnonymous,");
      fprintf(ctx->wsock, "\n");
    }

    fscanf(ctx->rsock, "%d", &err);
    if (err != 250) {
      printf("err: %d\n", err);
      return -1;
    }
    fscanf(ctx->rsock, "-ServiceID=%s\n", buffer1);
    printf("Added onion service %s.onion from file\n", buffer1);
    fscanf(ctx->rsock, "250 OK");
  }

  /* Complete by creating new onion services */
  for (int i = tos->filled; i < tos->size; i++) {
    fprintf(ctx->wsock, "add_onion NEW:ED25519-V3 ");
    for (int j = 0; j < port_per_os; j++) {
      fprintf(ctx->wsock, "Port=%d,127.13.3.7:%d ", port[i*port_per_os+j], port[i*port_per_os+j]);
    }
    if (flags == TOR_ONION_FLAG_NONE) fprintf(ctx->wsock, "\n");
    else {
      fprintf(ctx->wsock, "Flags=");
      if (flags & TOR_ONION_FLAG_NON_ANONYMOUS) fprintf(ctx->wsock, "NonAnonymous,");
      fprintf(ctx->wsock, "\n");
    }

    //fprintf(ctx->wsock, "add_onion NEW:RSA1024 Port=%d\n", port[i]);

    fscanf(ctx->rsock, "%d", &err);

    if (err != 250) return -2;
    err = fscanf(ctx->rsock, "-ServiceID=%s\n", buffer1);
    if (err <= 0) return -3;
    printf("Created onion service %s.onion\n", buffer1);
    err = fscanf(ctx->rsock, "250-PrivateKey=%s\n", buffer2);
    if (err <= 0) return -4;
    //printf("Onion service private key: %s\n", buffer);
    if (tor_os_append(tos, buffer1, buffer2) != 0) return -5;
    err = fscanf(ctx->rsock, "250 OK");
    if (err < 0) return -6;
  }

  if (to_create > 0) {
    tor_os_persist(tos);
  }

  return 0;
}

void tor_ctl_close(struct tor_ctl* ctx) {
  fclose(ctx->rsock);
  fclose(ctx->wsock);
}
