/*
 * BiffSocko
 * mailwatch.c
 *
 * WHAT IT DOES:
 * mailwatch will monitor a users mailbox for incomming mail.
 * In the case of new email a message and timestamp will be 
 * reported to stdout. Before beginning it's monitoring, mailwatch 
 * will first check to see if the user has an account on the system.
 * In the case that the user does not have an account, mailwatch will
 * exit.  
 *
 * EXIT CODES:
 *  0			success
 * -1 			user does not have an account
 * -2			can't open passwd file
 * -3			wrong usage
 * -4 			can't find mail directory (see define statements)
 *
 * USAGE:
 * mailwatch <username>
 *
 * COMPILE INSTRUCTIONS:
 * this has been compiled and tested on RedHat LINUX version 4.2
 * and on Solaris 2.7 (had to change the MAILDIR for solaris)
 * gcc -o mailwatch mailwatch.c
 *
 * OTHER INSTRUCTIONS:
 * you may need to change the path of MAILDIR in the 
 * section marked DEFINES.  The path listed currently will work with
 * RedHat Linux version 4.2
 * (C) All Rights Reserved 1999, Copyright BiffSocko
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, for NON COMMERCIAL USE are permitted provided that:
 * (1) source code distributions retain the above copyright notice and this
 * paragraph in its entirety, and (2) distributions including binary code
 * include the above copyright notice and this paragraph in its entirety in
 * the documentation or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 */
/***************************************************************
 * 			INCLUDES
 ***************************************************************/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>

/***************************************************************
 * 			DEFINES
 ***************************************************************/
#define MAILDIR "/var/spool/mail"
#define HEAD "\033[31mBiffSocko \033[34mmailwatch\033[36m.\033[34mc\033[0m"
#define VERSION "\033[34mversion 1\033[36m.\033[34m0\033[0m"
#define PASSFILE "/etc/passwd"

/***************************************************************
 *			FUNCTIONS
 ***************************************************************/
int checkusr(char *x);
char *now_time();

int main (int argc, char *argv[])
{
	char *name;
	struct stat buf;
	FILE *Fp=NULL;
	int lastsize=0;
	char *current_time;

/* 
 * check for usage 
 */
if(argc != 2){
	fprintf(stderr, "\033[31musage: %s <username>\033[0m\n", argv[0]);
	exit( -3);
}

/* 
 * shameless self promotion 
 */
printf("%s %s\n",HEAD, VERSION);

/*
 * check to see if user has an account
 */
name=argv[1];
if(checkusr(name) != 0){
	fprintf(stderr, "%s does not have an account on this server\n",
		name);
	fclose(Fp);
	exit( -2);
}
else{
	fprintf(stdout, "\033[36mchecking mail for user %s\033[0m\n",
		name);
}

/*
 * move to mail directory and start an infinate loop
 */
if((chdir(MAILDIR)) != 0){
	fprintf(stderr, "error finding %s\n", MAILDIR);
	exit( -4);
}

while(1){
	if(stat(name, &buf) == -1)
		buf.st_size=0;
	
	/* 
	 * check file size of mail file of user
	 */
	if(buf.st_size > lastsize){
		fprintf(stdout, "user %s has mail ",name);
		fprintf(stdout,"@ %s", now_time());
		lastsize=buf.st_size;
	}

	sleep(5);
}
exit( 0);
}

/********************************************************************
 * checkusr() will check for a string (x) that matches a user
 * in the /etc/passwd file
 ********************************************************************/
int checkusr(char *x)
{
	FILE *Fp=NULL;
	char String[ 80];
	char cname[ 80];
	char *cnamep;
	int counter;
	int flag=1;

memset(cname,0,80);

/* 
 * open passwd file 
 */
if(!(Fp=fopen(PASSFILE, "r"))){
	fprintf(stderr, "can't open password file\n");
        exit( -1);
}

while(fgets(String, 80, Fp)){
	memset(cname,0,80);
	counter=0;
	while(String[counter] != ':'){
		cname[counter] = String[counter];	
		counter++;
	}

	/*
	 * compare username and input string
	 */
	if(strcmp(x, cname) == 0){
		flag = 0;
		break;
	}
}

fclose(Fp);
return flag;
}

/********************************************************************
 * now_time() will return the current time to what ever called it
 ********************************************************************/
char *now_time()
{
	time_t seconds, *ptr;

ptr = &seconds;

seconds=time(NULL);
return ctime(ptr);
}


