/*
 * ImapProxy - a caching IMAP proxy daemon
 * Copyright (C) 2002 Steven Van Acker
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */


#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <time.h>

#include <output.h>
#include <network.h>
#include <configfile.h>
#include <misc.h>
#include <processing_parent.h>
#include <processing_child.h>
#include <database.h>
#include <defines.h>

/* the current time. */
int Globaltime = 0;

int main(int argc,char **argv)
{
    int mainfd = 0;
    int laststatstime = 0;
   
    /* parse the options */
    parse_options(argc,argv);
   
    if(option_version)
	version();
    if(option_help)
	usage(argv[0]);
    
    /* read the configfile */
    if(config_file_read(option_config_file))
    {
	fprintf(stderr,"main(): Error reading configfile!\n");
	return -1;
    }

    if(option_print_config)
	configfile_print(option_config_file);

    if(option_version || option_help || option_print_config)
	exit(0);

#ifndef I_DID_NOT_LISTEN_TO_STEVEN_AND_WANT_TO_RUN_AS_ROOT_ANYWAY
    /* running stuff as root is bad bad
     * so don't do it
     */
    if(geteuid() == 0)
    {
	char *rant = "<RANT>\n"
		     "Daemons that require very little privileges, should not be run as root.\n"
		     "People say it all the time, and for a good reason too. Since your root user is a superuser,\n"
		     "it is allowed to do ANYTHING on the system. This includes deleting any file, reading every file,\n"
		     "using any resource as is sees fit, shutting down the system or even causing permanent unrepairable\n"
		     "damage to the system.\n"
		     "For this reason, I strongly recommend that you run this program as a NON-PRIVILEGED user !\n"
		     "If you do not agree, and your system annihilates itself, your dog dies, or the universe implodes,\n"
		     "then don't blame me ! I warned you.\n"
		     "</RANT>\n";

	fprintf(stderr,"%s",rant);
	log(LOG_WARNING,"%s",rant);

	exit(0);
    }
    
#endif
    
    if(my_daemon() < 0)
    {
	log(LOG_WARNING,"Couldn't go into background.\n");
	exit(-1);
    }
    
    log(LOG_INFO,"Program started.\n");
    signal(SIGHUP,sighup);
    signal(SIGCHLD,sigchldhandler);

    /* set up the main socket */
    if((mainfd = nonblock_socket())<0)
	return -1;

    /* make it listen on the configged port */
    if(setup_listener(config_local_address,mainfd,htons(config_local_port)) < 0)
    {
	log(LOG_WARNING,"Error: Couldn't setup listener !\n");
	exit(-1);
    }
    

    laststatstime = time(NULL);
    
    while(1)
    {
	/* forkedchild is only set in the child ! */
	if(forkedchild)
	    child_conversate();
	else
	{
	    Globaltime = time(NULL);
	    
	    /* log some stats */
	    if(config_stats_frequency > 0 && Globaltime - laststatstime > config_stats_frequency)
	    {
		db_log_stats();
		laststatstime = Globaltime;
	    }
	    
	    /* clean up/handle any idle connections */
	    parent_handle_idlers();

	    parent_conversate(mainfd);
	}
    }
    
    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1