/*
 * output.c: handles a variety of tasks dealing with the output from the irc
 * program 
 *
 *
 * Written By Michael Sandrof
 *
 * Copyright(c) 1990 
 *
 * See the COPYRIGHT file, or do a HELP IRCII COPYRIGHT 
 */
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/file.h>
#include <stdio.h>
#include "irc.h"
#include "output.h"
#include "vars.h"
#include "input.h"
#include "term.h"
#include "lastlog.h"
#include "window.h"
#include "screen.h"
#include "hook.h"
#include "ctcp.h"
#include "log.h"

int	in_help = 0;

/* make this buffer *much* bigger than needed */
static char putbuf[BIG_BUFFER_SIZE + 1];

/*
 * refresh_screen: Whenever the REFRESH_SCREEN function is activated, this
 * swoops into effect 
 */
/*ARGSUSED*/
SIGTYP	refresh_screen(key, ptr)
char	*key;
void	(*ptr)();
{
	term_clear_screen();
	if (term_resize())
		recalculate_windows();
	else
		redraw_all_windows();
	update_all_windows();
	update_input(UPDATE_ALL);
}

/* init_windows:  */
void	init_screen()
{
	term_init();
	term_clear_screen();
	term_resize();
	new_window();
	recalculate_windows();
	update_all_windows();
	init_input();
	term_move_cursor(0, 0);
}

/* put_file: uses put_it() to display the contents of a file to the display */
void	put_file(filename)
char	*filename;
{
	FILE	*fp;
	char	line[256];		/* too big?  too small?  who cares? */
	int	len;

	if ((fp = fopen(filename, "r")) != null(FILE *))
	{
		while (fgets(line, 256, fp))
		{
			len = strlen(line);
			if (*(line + len - 1) == '\n')
				*(line + len - 1) = null(char);
			put_it("%s", line);
		}
		fclose(fp);
	}
}

/*
 * put_it: the irc display routine.  Use this routine to display anything to
 * the main irc window.  It handles sending text to the display or stdout as
 * needed, add stuff to the lastlog and log file, etc.  Things NOT to do:
 * Dont send any text that contains \n, very unpredictable.  Tabs will also
 * screw things up.  The calling routing is responsible for not overwriting
 * the 1K buffer allocated.  
 *
 * For Ultrix machines, you can't call put_it() with floating point arguements.
 * It just doesn't work.  - phone, jan 1993.
 * wheeeeeee.. does too.  - phone, april 1993.
 */
/*VARARGS*/
void	put_it(format, arg1, arg2, arg3, arg4, arg5,
		arg6, arg7, arg8, arg9, arg10)
char	*format;
char	*arg1,
	*arg2,
	*arg3,
	*arg4,
	*arg5,
	*arg6,
	*arg7,
	*arg8,
	*arg9,
	*arg10;
{
	if (window_display)
	{
		sprintf(putbuf, format, arg1, arg2, arg3, arg4, arg5,
		    arg6, arg7, arg8, arg9, arg10);
		add_to_log(irclog_fp, putbuf);
		add_to_screen(putbuf);
	}
}

/* This is an alternative form of put_it which writes three asterisks
 * before actually putting things out.
 */
void	say(format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
char	*format;
char	*arg1,
	*arg2,
	*arg3,
	*arg4,
	*arg5,
	*arg6,
	*arg7,
	*arg8,
	*arg9,
	*arg10;
{
	if (window_display)
	{
		putbuf[0] = putbuf[1] = putbuf[2] = '*';
		putbuf[3] = ' ';
		sprintf(&putbuf[4], format, arg1, arg2, arg3, arg4, arg5,
		    arg6, arg7, arg8, arg9, arg10);
		add_to_log(irclog_fp, putbuf);
		add_to_screen(putbuf);
	}
}

void	yell(format, arg1, arg2, arg3, arg4, arg5,
		arg6, arg7, arg8, arg9, arg10)
char	*format;
char	*arg1,
	*arg2,
	*arg3,
	*arg4,
	*arg5,
	*arg6,
	*arg7,
	*arg8,
	*arg9,
	*arg10;
{
	sprintf(putbuf, format, arg1, arg2, arg3, arg4, arg5,
	    arg6, arg7, arg8, arg9, arg10);
	add_to_log(irclog_fp, putbuf);
	add_to_screen(putbuf);
}


/* help_put_it: works just like put_it, but is specially used by help */
void	help_put_it(topic, format, arg1, arg2, arg3, arg4, arg5,
arg6, arg7, arg8, arg9, arg10)
char	*format,
	*topic;
char	*arg1,
	*arg2,
	*arg3,
	*arg4,
	*arg5,
	*arg6,
	*arg7,
	*arg8,
	*arg9,
	*arg10;
{
	sprintf(putbuf, format, arg1, arg2, arg3, arg4, arg5,
	    arg6, arg7, arg8, arg9, arg10);
	in_help = 1;
	if (do_hook(HELP_LIST, "%s %s", topic, putbuf))
	{
		if (window_display)
		{
			add_to_log(irclog_fp, putbuf);
			add_to_screen(putbuf);
		}
	}
	in_help = 0;
}
