/* Copyright 1989, 1990 by James Aspnes, David Applegate, and Bennet Yee */
/* See the file COPYING for distribution information */
#include "db.h"
#include "globals.h"
#include "interface.h"
#include "externs.h"

static char tell_buffer[TELL_BUFFER_SIZE];
static int tell_top;
datum tell_victim;

void tell_init (datum victim)
{
  struct object *v;

  tell_top = 0;

  if ((v = object (victim)) == 0 || !oflag_set (v, F_CONNECTED)) {
    tell_victim = NOTHING;
  } else {
    tell_victim = victim;
    if (oflag_set (v, F_PARANOID)) {
      sprintf (tell_buffer, "%ld:", me);
      tell_top = strlen (tell_buffer);
    }
  }
}

static void buffer_string (const char *s)
{
  int len;

  if (s != 0) {
    len = strlen (s);
    if (len + tell_top >= TELL_BUFFER_SIZE - 1) {
      tell_victim = NOTHING;
    } else {
      strcpy (tell_buffer + tell_top, s);
      tell_top += len;
    }
  }
}

void add_buffer (datum str)
{
  if (tell_victim != NOTHING) {
    buffer_string (string (str));
  }
}

void add_num_buffer (datum num)
{
  char buf[256];

  if (tell_victim != NOTHING) {
    sprintf (buf, "%ld", num);
    buffer_string (buf);
  }
}

void add_time_buffer (datum t)
{
  if (tell_victim != NOTHING) {
    buffer_string (time_string (t));
  }
}

datum do_tell (void)
{
  if (tell_victim != NOTHING) {
    tell_buffer[tell_top] = '\0';
    notify (tell_victim, tell_buffer);
    return 1;
  } else {
    return 0;
  }
}