/*
	Schrodinger's Box Haven  code version -0.1 alpha
        Copyright (C) 1994, Gordon Chan

	You can do whatever ya want with this code as long as you leave this
        copyright & disclaimer intact.  You can hack it up all you want, etc...
        No guarantees.

	Send comments, flames, bug reports, etc ... to
		au233@freenet.carleton.ca  <---- (preferred address for now)
		gjc@achilles.net
		gchan@ccs.carleton.ca
*/


/* Here's the copyrights & disclaimers from the Opium Den and unixCB codes */

/* Opium Den */
/*
  val's haven code v -0.85

        Copyright (C) 1994 Joel Ward

        Warranty:
                Don't blame me for anything.

        You can do whatever the fuck you want with this code as long as you
don't remove this copyright/disclaimer notice.
*/

/* unixCB */
/*
  
  cbd.c
  
  "cbd.c" is the main code for the CB simulator.
  
  Copyright (c) 1992, Gary Grossman.  All rights reserved.
  Send comments and questions to: garyg@soda.berkeley.edu
  
  */

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

#include "main.h"
#include "macros.h"
#include "linked_lists.h"


void write_user (desc, mesg)
  int desc;
  char *mesg;
{
  write(desc, mesg, strlen(mesg));
}


int get_input (desc, buffer, length)
  int desc, length;
  char *buffer;
{
  int n = read(desc, buffer, length);
  buffer[n] = '\0';
  return n;
}


int read_user (user, buffer, length)
  struct user *user;
  char *buffer;
  int length;
{
  char temp[120];
  int size = get_input(user->desc, buffer, length);
   
  if (!size) {
    sprintf(temp, ">> %s on line %d has been DISCO-nnected.\r\n",
     user->name, user->desc);
    delete_user(user, ">> Later Dude!\r\n", 0, temp);
    return 0;
  } else if (size==length) { 
    sprintf(temp, ">> %s on line %d has been kicked off for Spamming!\r\n",
     user->name, user->desc);
    delete_user(user, ">> Too much input!!!\r\n", 0, temp);
    return 0;
  }
  return size;
}
    

void write_err (mesg)
	char *mesg;
{
#ifdef DEBUG
	write_user (FD_ERR, mesg);
#endif
}


char *int2str (i)
	int i;
{
	static char str[10];
	str [0] = '\0';
	
	sprintf (str, "%d", i);
	return str;
}
	


char *convert_time (t)
	time_t t;
{
	static char buffer[15];
	int months, days, hours, mins, secs;
	int field;

	buffer[0] = '\0';
	field = 1;

	secs = t % 60;
	t /= 60;
	mins = t % 60;
	t /= 60;
	hours = t % 24;
	t /= 24;
	days = t % 30;
	t /= 30;
	months = t;
	
	if (months) {
		strcat (buffer, int2str(months) );
		strcat (buffer, "M");
		if (++field > 1)
			return buffer;
	}
	if (days) {
		strcat (buffer, int2str(days) );
		strcat (buffer, "d");
		if (++field > 1)
			return buffer;
	}
	if (hours) {
		strcat (buffer, int2str(hours) );
		strcat (buffer, "h");
		if (++field > 1)
			return buffer;
	}
	if (mins) {
		strcat (buffer, int2str(mins) );
		strcat (buffer, "m");
		if (++field > 1)
			return buffer;
	}
	if (secs) {
		strcat (buffer, int2str(secs) );
		strcat (buffer, "s");
		if (++field > 1)
			return buffer;
	}
	return buffer;
}


void write_file_to (user, filename, errormessage)
	struct user *user;
	char *filename, *errormessage;
{
	FILE *fptr;
	char buffer[256], *b;

	fptr = fopen (filename, "rb");

	if (!fptr) {
		write_user (user->desc, errormessage);
		return;
	}

	fgets (buffer, 255, fptr);
	while (!feof(fptr)) {
		strip_cr(buffer);
		write_user (user->desc, buffer);
		write_user (user->desc, "\r\n");
		fgets (buffer, 255, fptr);
	}
	
	fclose (fptr);
}

/* carriage return and newline characters are stripped */

void strip_cr (text)
	char *text;
{
	char *b;
	b = text + strlen(text)-1;

	while (*b && (*b == '\r' || *b == '\n') ) {
		*b = '\0'; 
			b--;
	}
}
