#include "process.h"

/* user_startup is called right after the program sets up signal
   handling and goes into the background. Use this for any startup
   you have to do. Remember that the tty is not your friend, so
   use ttyprintf() and not printf(), because stdout probably doesn't
   point at anything good. */
void user_startup(void)
{
	ttyprintf("\nNetSHELL! (process ID %d)\n",getpid());
}

/* greet is the function that is called at every new connection. */
void greet(int fd, struct sockaddr_in *addr)
{
	fdprintf(fd, "\nHello, new connection from %s/port %u on sockfd %d\n",
			 inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), fd);
}

/* process -- This is where *your* operations go.
    This function is called every time a read() returns > 0.

   To disconnect a socket, use disconnect().
   To read from a socket, use socket_read instead of read;
    the former has the capabilities to deal with satanic things
    like reads that die with EINTR.
   If it's time for the server to die gracefully, then call
    shut_down().
*/
void process(int socket, int len, char *data)
{
	data[len] = 0;
	fdprintf(socket, "[%d(%d)] >>%s<<\n", socket, len, data);
	if (strncasecmp(data, "off", 3) == 0)
	{
		fdprintf(socket, "\n* disconnecting *\n");
		disconnect(socket);
	}
	else if (strncasecmp(data, "shutdown", 8) == 0)
	{
		fdprintf(socket, "\n Shutting down the server\n\n");
		shut_down();
	}
}
