/*
   Aris threatcon meter by #phrack v1.0
   gcc -o threatbar threatbar.c -lncurses
*/


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netdb.h>
#include <ncurses.h>


#define SERVER			"phrack.efnet.ru"
#define DELAY			500
#define THREATCON_MAX		7
#define PATH_WALL		"/usr/bin/wall"


int
main () 
{
	struct hostent *he;
	struct sockaddr_in s_in;
	static char url[] = "GET http://phrack.efnet.ru/threatcon\r\n\r\n";
	char buf[2048], buf2[2048];
	char *filename, *ptr;
	unsigned int threatcon, i;
	int fd, ffd, xmax, ymax;

	if ((he = gethostbyname (SERVER)) == NULL)
	{
		herror ("gethostbyname");
		exit (EXIT_FAILURE);
	}

	memset (&s_in, 0, sizeof (s_in));
	s_in.sin_family = AF_INET;
	s_in.sin_port = htons (80);
	memcpy (&s_in.sin_addr.s_addr, he->h_addr, 4);
	memset (&buf, 0, sizeof (buf)), memset (&buf2, 0, sizeof (buf2));

	if (initscr () == NULL)
	{
		perror ("initscr");
		exit (EXIT_FAILURE);
	}

	getmaxyx (stdscr, ymax, xmax);

	if ((has_colors () == FALSE) || (start_color () == ERR))
{
		fprintf (stderr, "ALERT! Colors disabled: your threatcon bar will not appear in red!\n");
exit (1);}

	init_pair (COLOR_RED, COLOR_RED, COLOR_RED);

while (1)
{

	if ((fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
	{
		perror ("socket");
		exit (EXIT_FAILURE);
	}

	if (connect (fd, (struct sockaddr *) &s_in, sizeof (struct sockaddr_in)) < 0)
	{
		perror ("connect");
		exit (EXIT_FAILURE);
	}

	if (write (fd, url, strlen (url)) != strlen (url))
	{
		perror ("write");
		exit (EXIT_FAILURE);
	}

	memcpy (&buf2, &buf, sizeof (buf2));
	memset (&buf, 0, sizeof (buf));

	if (read (fd, buf, sizeof (buf) - 1) <= 0)
	{
		perror ("read");
		exit (EXIT_FAILURE);
	}

	close (fd);

	if (!strcmp (buf, buf2))
		goto sleeping;

	ptr = buf;
	while (isdigit (*ptr))
		ptr++;

	if (!*ptr)
	{
		fprintf (stderr, "Error receiving threatcon reading!\n");
		exit (EXIT_FAILURE);
	}

	*ptr++ = 0;
	threatcon = atoi (buf);
	clear ();
	wprintw (stdscr, "#phrack ARIS THREATCON MONITOR v1.0\n");
	wprintw (stdscr, "-----------------------------------\n");
	wprintw (stdscr, "current threatcon is: %u / possible %u\n\n",
		 threatcon, THREATCON_MAX);

	color_set (COLOR_PAIR (4), NULL);
	for (i = 0; i < (xmax * ((threatcon * 100) / THREATCON_MAX) / 100); i++)
		addch (ACS_BLOCK | COLOR_PAIR (COLOR_RED));

	wprintw (stdscr, "\n\n");
	wprintw (stdscr, "%s\n", ptr);

	refresh ();

	if ((filename = tmpnam (NULL)) == NULL)
	{
		perror ("tmpname");
		exit (EXIT_FAILURE);
	}

	if ((ffd = open (filename, O_WRONLY|O_CREAT)) < 0)
	{
		perror ("open");
		exit (EXIT_FAILURE);
	}

	if (write (ffd, ptr, strlen (ptr)) != strlen (ptr))
	{
		perror ("write");
		exit (EXIT_FAILURE);
	}

	close (ffd);

	switch (fork ())
	{
		case -1:
			perror ("fork");
			exit (EXIT_FAILURE);
			break;
		case 0:
			
			if ((ffd = open (filename, O_RDONLY)) < 0)
			{
				perror ("open");
				exit (EXIT_FAILURE);
			}

			if (dup2 (ffd, STDIN_FILENO) < 0)
			{
				perror ("dup2");
				exit (EXIT_FAILURE);
			}

			execl (PATH_WALL, PATH_WALL, NULL);
			perror ("execl");
			exit (EXIT_FAILURE);
			break;
		default:
			wait (NULL);
			break;
	}

	unlink (filename);

sleeping:
	sleep (DELAY);

}

	exit (EXIT_SUCCESS);	/* unlike the last line in an electronicsouls local exploit, THIS ACTUALLY NEVER IS REACHED */
}
