#include <string.h>
#include <stdlib.h>
#include "global.h"
#include "constants.h"
#include "window.h"
#include "defines.h"
#include "notify.h"

extern server_ptr gsrv;

void add_notify(server_ptr serv, char *str)
{
  notify_ptr temp, f, b = NULL;
  int res = -1;

  if (!serv) return;
  f = serv->notify_list;
  if (strlen(str)>=MAXNICKLEN) str[MAXNICKLEN-1] = 0;
  while (f && ((res = strcasecmp(str, f->nick)) > 0)) {
    b = f;
    f = f->next;
  }
  if (res==0) return;
  if ((temp = (notify_ptr)malloc(sizeof(struct notify_node))) == NULL) return;
  (void)strcpy(temp->nick, str);
  temp->present = FALSE;
  if (!b) serv->notify_list = temp;
  else b->next = temp;
  temp->next = f;
  say("%s has been added to the notification list.", str);
}

void delete_notify(server_ptr serv, char *str)
{
  notify_ptr front, back;

  if (!serv) return;
  front = serv->notify_list;
  back = NULL;
  while ((front) && (strcasecmp(front->nick, str) != 0)) {
    back = front;
    front=front->next;
  }
  if (!front) return;
  if (!back)  serv->notify_list = serv->notify_list->next;
  else        back->next = front->next;
  free(front);
  say("%s has been removed from the notification list.", str);
}

void show_notify(server_ptr serv, int only_pres)
{
  notify_ptr walk;
  char present[MAXLEN], absent[MAXLEN], *str;

  if (!serv) return;
  (void)strcpy(present, "*** Currently present: ");
  (void)strcpy(absent, "*** Currently absent: ");

  walk = serv->notify_list;

  while (walk) {
    if (walk->present) str = present;
    else str = absent;
    (void)strcat(str, walk->nick);
    (void)strcat(str, " ");
    walk = walk->next;
  }
  say("%s", present);
  if (!only_pres) say("%s", absent);
}

void notify(server_ptr serv, char *str)
{
  char word[MAXLEN];

  if (!*str)
    show_notify(serv, FALSE);
  else if (strcmp(str, "+") == 0)
    show_notify(serv, TRUE);
  else
    while(*str) {
      (void)grab_word(&str, ' ', word);
      if (*word=='-') delete_notify(serv, &word[1]);
      else add_notify(serv, word);
    }
}

#define PRESTRING "ISON :"

void send_notify(server_ptr serv)
{
  notify_ptr walk;
  char msg[512], errmsg[80];
  int len;

  (void)strcpy(msg, PRESTRING);
  len = strlen(msg);
  if (!serv) return;
  walk = serv->notify_list;
  while (walk) {
    if (len>(sizeof(msg)-MAXNICKLEN-3)) {
      (void)strcat(msg, "\n");
      if (tcp_send(serv->iochan, msg, strlen(msg)) == -1)
      {
        get_socket_error(errmsg);
        say("error sending %s [%s]", errmsg, msg);
      }
      (void)sprintf(msg, "%s%s ", PRESTRING, walk->nick);
      len = strlen(msg);
    } else {
      (void)strcat(msg, walk->nick);
      if (walk->next) (void)strcat(msg, " ");
      len = len + strlen(walk->nick) + 1;
    }
    walk = walk->next;
  }
  if (len!=strlen(PRESTRING)) {
    (void)strcat(msg, "\n");
    if (tcp_send(serv->iochan, msg, strlen(msg)) == -1)
    {
      get_socket_error(errmsg);
      say("error sending %s [%s]", errmsg, msg);
    }
  }
}

void han_notify(server_ptr serv, char *string)
{
  int len;
  char *temp, *test, nick[MAXLEN];
  notify_ptr old;

  if (!serv) return;
  temp = string;
  old = serv->last_notify;
  if (!serv->last_notify) serv->last_notify = serv->notify_list;
  len = grab_word(&temp, ' ', nick);
  while (len && serv->last_notify) {
    while (serv->last_notify && (strcasecmp(serv->last_notify->nick, nick) != 0)) {
      if (serv->last_notify->present == TRUE)
        if (!handle_on(o_notify_signoff, serv->last_notify->nick, 0))
          say("*** Signoff by %s detected", serv->last_notify->nick);
      serv->last_notify->present = FALSE;
      serv->last_notify = serv->last_notify->next;
      if (old == serv->last_notify) break;
      if (!serv->last_notify) serv->last_notify = serv->notify_list;
    }
    if (serv->last_notify && (serv->last_notify->present == FALSE)) {
      if (!handle_on(o_notify_signon, serv->last_notify->nick, 0))
        say("*** Signon by %s detected", serv->last_notify->nick);
      serv->last_notify->present = TRUE;
    }
    if (serv->last_notify) serv->last_notify = serv->last_notify->next;
    len = grab_word(&temp, ' ', nick);
  }
}
