/***********************************************
 * released under (E) licensing ...            *
 *        (E) RULES AND REGULATIONS            *
 * permission to use/rewrite/add     : granted *
 * permission to trojan/steal        : denied  *
 * permission to use illegally       : denied  *
 * permission to use on /dev/urandom : denied  *
 ***********************************************/
/* contact el8@press.co.jp for full license    */
/* code copyrighted by ~el8 -- don't infringe! */

#define VERSION .01
/* FILE: d0td0t.c */
/* AUTHOR: ()P3R4TI0N R4MPR4T */
/* ./d0td0t -h f()r us4ge. */

/* CHEQ THIS OUT, THERE ARE SMALL BUGZ LEFT BEHIND, IF U CAN FIND THEN U GET
   GREETS IN ~EL8
*/

/*
   bUlljUnkZ:
   defining DEBUG w1ll give debugging ()utpUt.
   define LEDLIGHT as the led u want to light. 1, 2, 3, 4
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#if __linux__
#include <linux/kd.h>
#endif

/* i want it allz */
#define NUM 1
#define SCROLL 2
#define CAPS 3
#define ALL 4
#define LEDLIGHT ALL /* NUM, SCROLL, CAPS, ALL */

struct statz {
    unsigned int m4tched, n0tm4tched;
    signed int round;
    signed int tfd;		/* :()()() */
};

struct statz st4tz;

extern char *optarg;
extern int optopt, opterr, optind;

void usage(void);
void sigc4tch(int);
void prnt_st4tz(void);
int izattyfuqt(void);
int wew4ntledz(void);

int main(int argc, char *argv[])
{
    unsigned int x, y;
    unsigned int x2, y2;
    char buff[200];
    unsigned int cnt;
    unsigned int round;
    unsigned int copt;
    signed int maxrounds;
    struct termios t3rmio;
    signed int tfd;
    char k33pntr4ck[255];
    signed int kntfd;
    FILE *kntFILE;
    char *tmpstr = (char *) NULL;
    signed    int tmpint;
    signed    int tmpint2;
    unsigned int index;
    signed int useleds;

    maxrounds = -1;

    kntfd = -2;

    useleds = 0;

    do {
	switch (copt) {
	case 'r':
	    {
		maxrounds = atoi(optarg);
		break;
	    }
	case 't':
	    {
		bzero(&k33pntr4ck, sizeof(k33pntr4ck));
		strncpy(k33pntr4ck, optarg, sizeof(k33pntr4ck));
		kntfd = 0;
		break;
	    }
	case 'l':
	    {
		useleds=1;
		break;
	    }
	case 'h':
	    {
		(void) usage();
	    }
	default:
	    break;
	}
    }
    while ((copt = getopt(argc, argv, "r:t:hl")) != EOF);

    bzero(&st4tz, sizeof(st4tz));

    bzero(&t3rmio, sizeof(t3rmio));

    x = y = x2 = y2 = cnt = st4tz.m4tched = st4tz.n0tm4tched = round = 0;
    st4tz.round = x;
    index = x;

    if ((tfd = open("/dev/tty", O_RDWR)) < 0) {
	perror("open(...)");
	exit(-1);
    }

    st4tz.tfd = tfd;

    if (tcgetattr(tfd, &t3rmio) < 0) {
	perror("tcgetattr(...)");
	exit(-1);
    }

    if (!(t3rmio.c_lflag & ICANON)) {
#define DIDNTHAVEICANON 1
	t3rmio.c_lflag & ICANON;
    }

    if ((tcsetattr(tfd, TCSANOW, &t3rmio)) < 0) {
	perror("tcsetattr(...)");
	exit(-1);
    }

    puts("D0TD0T BY ()P3R4TI0N R4MPR4T");

    puts("UZ3 '!' T0 EX1T D0TD0T!");

    round += 1;

    if (!(kntfd)) {
#ifdef DEBUG
	printf("Using tracker file %s\n", k33pntr4ck);
#endif
	if ((kntFILE = fopen(k33pntr4ck, "r+")) == NULL)
	    perror("fopen(...)");
    }

    signal(SIGINT, &sigc4tch);
    signal(SIGHUP, &sigc4tch);
    signal(SIGQUIT, &sigc4tch);
    signal(SIGKILL, &sigc4tch);
    signal(SIGSEGV, &sigc4tch);
    signal(SIGPIPE, &sigc4tch);
    signal(SIGTERM, &sigc4tch);
    signal(SIGSTOP, &sigc4tch);
    signal(SIGTSTP, &sigc4tch);

    while (1) {

	x = y = x2 = y2 = 0;

	bzero(&buff, sizeof(buff));

	t3rmio.c_lflag ^= ECHO;
	if ((tcsetattr(tfd, TCSANOW, &t3rmio)) < 0) {
	    perror("tcsetattr(...)");
	    exit(-1);
	}

	if (fgets(buff, sizeof buff, stdin) == NULL)
	    break;

	if (buff[0] == '!') {
	    --round;
	    break;
	}

	t3rmio.c_lflag |= ECHO;
	if ((tcsetattr(tfd, TCSANOW, &t3rmio)) < 0) {
	    perror("tcsetattr(...)");
	    exit(-1);
	}

	if (strlen(buff) == 1) {

	    printf("STR LEN OF BUFF = 1 :"
		   " ILLEG4L OPER4TI0N! ST4RTING ROUND[%i] 0VER!\n",
		   round);

	    continue;
	}

	buff[strlen(buff) - 1] = '\0';

	for (cnt = 0; cnt <= strlen(buff); cnt++) {
	    if (buff[cnt] == '.') {
		x = cnt;
		break;
	    }
	}

	for (cnt = 0; cnt <= strlen(buff); cnt++)
	    if ((buff[cnt] == '.') && (cnt != x))
		y = cnt;

	if (y == 0) {
	    puts("S0M3H0W U MESS3D UP!");
	    exit(-1);
	}
#ifdef DEBUG
	printf("%i %i\n", x, y);
#endif

	bzero(&buff, sizeof(buff));

	if (fgets(buff, sizeof buff, stdin) == NULL)
	    break;

	if (buff[0] == '!') {
	    round--;
	    break;
	}

	if (strlen(buff) == 1) {

	    printf("STR LEN OF BUFF = 1 :"
		   " ILLEG4L OPER4TI0N! ST4RTING ROUND[%i] 0VER!\n",
		   round);

	    continue;
	}

	for (cnt = 0; cnt <= strlen(buff); cnt++) {
	    if ((isalpha(buff[cnt])) && (buff[cnt] != ' ')) {
		x2 = cnt;
		break;
	    }
	}

	for (cnt = 0; cnt <= strlen(buff); cnt++) {
	    if ((isalpha(buff[cnt])) && (buff[cnt] != ' ') && (cnt != x2))
		y2 = cnt;
	}

	if (y2 == 0) {
	    puts("S0M3H0W U MESS3D UP!");
	    exit(-1);
	}
#ifdef DEBUG
	printf("%i %i\n", x2, y2);
#endif

	if ((x == x2) && (y == y2)) {
	    printf("a match!\a\n");
	    st4tz.m4tched += 1;
	    if(useleds)
		{
		wew4ntledz();
		}
	} else
	    st4tz.n0tm4tched += 1;

	if (round == maxrounds)
	    break;

	st4tz.round = round;
	round++;

	if (!(kntfd)) {
	    if ((x == x2) && (y == y2)) {
		bzero(&k33pntr4ck, sizeof(k33pntr4ck));
		if (fgets(k33pntr4ck, sizeof(k33pntr4ck), kntFILE) == NULL)
		{
#ifdef DEBUG
		    puts("EOF ON tR4ck3R f1LE");
#endif
		fprintf(kntFILE,"ME>1\n");
		fseek(kntFILE, 0, SEEK_SET);
		continue;
		}

		tmpint = 0;

		for(index=0;index<=strlen(k33pntr4ck);index++)
		if(k33pntr4ck[index]=='>')
		{
		tmpint=1;
		break;
		}

		if(!(tmpint)) {
		puts("MAN WTF, I M4DE THIZ TR4CK3R FILE SHIT SO EZ"
		     "\nU'R L4YYYYYYYME!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		     "\n");
		break;
		}	

		tmpint = 0;
		tmpstr = strtok(k33pntr4ck, ">");
		tmpstr = strtok(NULL, "");
		tmpint = atoi(tmpstr);

		fseek(kntFILE, 0, SEEK_SET);
		fprintf(kntFILE, "%s>%i\n", k33pntr4ck, tmpint + 1);
		fseek(kntFILE, 0, SEEK_SET);

	    } else {
		bzero(&k33pntr4ck, sizeof(k33pntr4ck));
		tmpint = 0;
		if (fgets(k33pntr4ck, sizeof(k33pntr4ck), kntFILE) == NULL)
		{
#ifdef DEBUG
		    puts("EOF ON tR4ck3R f1LE");
#endif
		fprintf(kntFILE,"ME>0\n");
		fprintf(kntFILE, "YOU>1\n");
		fseek(kntFILE, 0, SEEK_SET);
		continue;
		}

                tmpint = 0;

                for(index=0;index<=strlen(k33pntr4ck);index++)
                if(k33pntr4ck[index]=='>')
                {
                tmpint=1;
                break;
                }

                if(!(tmpint)) {
                puts("MAN WTF, I M4DE THIZ TR4CK3R FILE SHIT SO EZ"
                     "\nU'R L4YYYYYYME!!!!!!!!!!!!!!!!!!!!!!!!!!!"
                     "\n");
                break;
                }

		tmpint2 = 0;
		tmpint2 = strlen(k33pntr4ck);
		bzero(&k33pntr4ck, sizeof(k33pntr4ck));

		if (fgets(k33pntr4ck, sizeof(k33pntr4ck), kntFILE) == NULL)
		{
#ifdef DEBUG
		    puts("EOF ON tR4ck3R f1LE");
#endif
		fprintf(kntFILE, "YOU>1\n");
		fseek(kntFILE, 0, SEEK_SET);
		continue;
		}

                tmpint = 0;

                for(index=0;index<=strlen(k33pntr4ck);index++)
                if(k33pntr4ck[index]=='>')
                {
                tmpint=1;
                break;
                }

                if(!(tmpint)) {
                puts("MAN WTF, I M4DE THIZ TR4CK3R FILE SHIT SO EZ"
                     "\nIM GL4D IM GN0T U!!!!!!!!!!!!!!!!!!!!!!!!!!!"
                     "\n");
                break;
                }

		tmpstr = strtok(k33pntr4ck, ">");
		tmpstr = strtok(NULL, "");
		tmpint = atoi(tmpstr);

		fseek(kntFILE, tmpint2, SEEK_SET);
		fprintf(kntFILE, "%s>%i\n", k33pntr4ck, tmpint + 1);
		fseek(kntFILE, 0, SEEK_SET);
	    }
	}
    }

    printf("ST4TZ FOR THIS MATCH! MATCHED %i, NOTMATCHED %i, ROUNDZ[%i]\n",
	   st4tz.m4tched, st4tz.n0tm4tched, st4tz.round);

    st4tz.m4tched > st4tz.n0tm4tched ? puts("You win!!!") : puts("You loose@!@(N0T13ZIND0TD0T!)");

    if (!(t3rmio.c_lflag & ECHO))
	t3rmio.c_lflag |= ECHO;
#ifdef DIDNTHAVEICANON
    t3rmio.c_lflag |= ICANON;
#endif

    if ((tcsetattr(tfd, TCSANOW, &t3rmio)) < 0) {
	perror("tcsetattr(...)");
	exit(-1);
    }

    if (tfd)
	close(tfd);
    close(2);
    close(1);
    close(0);
    return 1;
}

void usage(void)
{
    puts("D0TD0T BY ()P3R4TI0N R4MPR4T");
    puts("Command line options--");
    puts("-r <maximum_rounds>");
    puts("--Command line options");
    puts("Howto-Play--");
    puts("On your first move you must place two points at random anywhere>");
    puts(".                      .");
    puts("On your second move you try and match two chars with the points>");
    puts("x                      y");
    puts("--Howto-Play");
    puts("Tracker-FILE--");
    puts("First line is your score: blah>0 '>' seperates the score.");
    puts("Second line is opponent score(y0rb4ds3lf):");
    puts("\tisuqsc0re>9999. '>' seperates the score.");
    puts("L()()K at the example c()nfig just b4 EOF :Q");
    puts("--Tracker-FILE");
    puts("<EOH>");
    puts("D0TD0T BY ()P3R4TI0N R4MPR4T");
    exit(1);
}

void sigc4tch(int signum)
{

    switch (signum) {
    case SIGINT:
	{
	    puts("C()uGHT: SiGiNT");
	    izattyfuqt();		
	    exit(1);
	}
    case SIGHUP:
	{
            puts("C()uGHT: SiGHuP");
            izattyfuqt();
	    exit(1);
	}
    case SIGQUIT:
	{
            puts("C()uGHT: SiGQUiT");
            izattyfuqt();
	    exit(1);
	}
    case SIGKILL:
	{
            puts("C()uGHT: SiGKiLL");
            izattyfuqt();
	    exit(1);
	}
    case SIGSEGV:

	{
            puts("C()uGHT: SiGSEgV");
            izattyfuqt();
	    exit(1);
        }
    case SIGPIPE:
	{
            puts("C()uGHT: SiGPiPE");
            izattyfuqt();
	    exit(1);
	}
    case SIGTERM:
	{
            puts("C()uGHT: SiGTeRM");
            izattyfuqt();
	    exit(1);
	}
    case SIGSTOP:
	{
            puts("C()uGHT: SiGSToP");
            izattyfuqt();
	    exit(1);
	}
    case SIGTSTP:
	{
            puts("C()uGHT: SiGTsTP");
            izattyfuqt();
	    exit(1);
	}
    default:
	{
	    puts("WEIERD!!!! sigc4tch()");
	    exit(-1);
	}
    }
    return;
}

void prnt_st4tz(void)
{
    printf("ST4TZ FOR THIS MATCH! MATCHED %i, NOTMATCHED %i, ROUNDZ[%i]\n",
	   st4tz.m4tched, st4tz.n0tm4tched, st4tz.round);

    st4tz.m4tched > st4tz.n0tm4tched ? puts("You win!!!") : puts("You loose@!@(N0T13ZIND0TD0T!)");
    return;
}

int izattyfuqt(void)
{
    struct termios t3rmio;

    bzero(&t3rmio, sizeof(struct termios));

    if (tcgetattr(st4tz.tfd, &t3rmio) < 0)
        perror("tcgetattr(...)");

            (void) prnt_st4tz();
            if (!(t3rmio.c_lflag & ECHO))
                t3rmio.c_lflag |= ECHO;
#ifdef DIDNTHAVEICANON
            t3rmio.c_lflag |= ICANON;
#endif
            if ((tcsetattr(st4tz.tfd, TCSANOW, &t3rmio)) < 0) {
                perror("tcsetattr(...)");
                exit(-1);
            }
return 1;
}

int wew4ntledz(void)
{
int ofd;
int ledst4te;

ledst4te = 0;

#if __linux__
	if((ofd = open("/dev/console", O_RDWR|O_NOCTTY))<0)
	{
		perror("open(...)");
		return -1;
	}

	switch(LEDLIGHT)
	{
		case NUM:
	{
		ledst4te = LED_NUM;
		break;
	}
		case SCROLL:
	{
		ledst4te = LED_SCR;
		break;
	}
		case CAPS:
	{
		ledst4te = LED_CAP;
		break;
	}
		case ALL:
	{
		ledst4te |= LED_NUM;
		ledst4te |= LED_SCR;
		ledst4te |= LED_CAP;
		break;
	}
	default:
		ledst4te = LED_NUM;
		break;
	}

	if(ioctl(ofd, KDSETLED, ledst4te)<0)
	{
		perror("ioctl(...)");
		return(-1);
	}

	sleep(1);

	if(ioctl(ofd, KDSETLED, 0)<0)
	{
		perror("ioctl(...)");
		return -1;
	}

	close(ofd);
#endif
return 1;
}

/*
EXAMPLE TRACKER FILE !!!!!!!!!!!!!

open tr4ck3R
------------
(LINE[1]) ME>0
(LINE[2]) MEBUTNOTME>0
-----eof----
open che4ttr4ck3r
------------
ESC'w'
ME>101312
J0HNK4RPETER>0
ESC':wq'
-----eof----
L00K IM BEATING J0HN K4RPETER 101312 TO 0!!!!!!!!
After every match, your score will be updated, after every NOTMATCH!@#@
	the second line score will be updated!!!!!!!!!!!!!!!!!
exit
------- LOGOUT --------

PS: trY cre4ting y()r 0wn st4tz f!le k0z th1ngz k1n g3t h3ct1c if th3 f1le
	iz g0t nUff1ngk in1t()!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
EOTF!
*/


syntax highlighted by Code2HTML, v. 0.9.1