%{
#include "main.h"

int state = 0;
extern char* yytext;
%}
%union {
	int ival;
	char* sval;
}
%token <ival> AUTH
%token <ival> GETMSG
%token <ival> LOGIN
%token <ival> PASSWORD
%token <sval> PARAM
%token <ival> QUIT
%token <ival> HELP
%%
session: login getmessage

login: auth LOGIN PARAM PASSWORD PARAM {auth($3, $5); free($3); free($5);}
	| auth PASSWORD PARAM LOGIN PARAM {auth($5, $3); free($3); free($5);}
	| authfirst login
	| help login
	| exit
	;

getmessage: getmsg name getmessage
	| getmsg name
	| cnf getmessage
	| help getmessage
	| exit
	;

cnf: PARAM {printf("001 Command not found. try help\n");}

authfirst: PARAM {printf("003 auth first or help for help \n");}

getmsg: GETMSG {state = GETMSG;}
name: PARAM {
	if (state == GETMSG) {
		getmsg(yytext);
		state = 0;
	}
}

auth: AUTH
	;

exit: QUIT {printf("000 goodbye\n"); exit(0);}

help: HELP {
	printf("220-1 HELP\n");
	printf("220-1 -----------------------------------------------------------\n");
	printf("220-1 AUTH FIRST:\n");
	printf("220-1 auth user: <USERNAME> pwd: <PASSWORD>\n");
	printf("220-1 Fetch messages from user:\n");
	printf("220-1 get <USER>\n");
	printf("220 0 [EOH]\n");
}
	;
%%
