/*
   This file is part of the IRC Bounce source code
   copyright (c) 1998 v00d00 Net Services
   and is distributed according to the GNU general public license.

   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., 675 Mass Ave, Cambridge, MA 02139, USA.

   The author (Robert Clark) can be reached at: v00d00@hempseed.com

   This code is based on Irc Proxy (c)1997-98 James Seter
*/

#include "common.h"
#include <stdarg.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/wait.h>

#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <pwd.h>

tconf conf;

void
putlog(char *format, ...)
{
   va_list va; char out[1024]; FILE *f;
   time_t tt=time(NULL); struct tm *now=localtime(&tt);
   va_start(va, format); vsnprintf(out,sizeof(out),format,va); va_end(va);
   if (conf.logf!=NULL) {
      fprintf(conf.logf,"[%-.2u:%-.2u:%-.2u %-.2u/%-.2u/%-.4u] %s\n",
         now->tm_hour,now->tm_min,now->tm_sec,now->tm_mday,now->tm_mon,
         now->tm_year+1900,out);
      fflush(conf.logf);
   }
}

void
do_error(char *s,int err)
{
   printf("Fatal Error (%s): %s\n",s,strerror(err));
   putlog("%s: %s",s,strerror(err));
   fclose(conf.logf);
   exit(0);
}

void
death(int s)
{
   putlog("iBNC Shutdown");
   fclose(conf.logf);
   exit(0);
}

int
main(int argc,char **argv)
{
   int i; char s[11]; FILE *f;

   printf("\nIRC Bounce %s (c)1998 v00d00 Net Services\n",VERSION);
   printf("Based on: Irc Proxy (c)1997-98 James Seter\n");
#ifdef BETA
   printf("THIS IS A BETA RELEASE! EXPECT PROBLEMS :)\n");
#endif
   printf("Please report any bugs to v00d00@hempseed.com\n\n");

   memset(&conf,0,sizeof(conf));
   strcpy(conf.pidfile,"bnc.pid");
   strcpy(conf.logfile,"bnc.log");
   strcpy(conf.cfgfile,"bnc.conf");
   conf.lport=6667;
   conf.dport=6667;
   conf.maxusers=100;

   if (argc > 1) {
      for (i=1; i<argc; i++)
         if (!strcasecmp(argv[i],"-c")) {
            i++;
            strncpy(conf.cfgfile,argv[i],strlen(argv[i])+1);
         }
      for (i=1; i<argc; i++)
         if (!strcasecmp(argv[i],"-l")) {
            show_license();
            exit(0);
         } else if (!strcasecmp(argv[i],"-h")||!strcasecmp(argv[i],"--help")) {
            show_help(argv[0]);
            exit(0);
         } else if (!strcasecmp(argv[i],"-i")) {
            loadconf(&conf);
            saveconf(&conf);
            exit(0);
         } else if (!strcasecmp(argv[i],"-f")) {
            saveconf(&conf);
            exit(0);
         } else if (!strcasecmp(argv[i],"-v")) exit(0);
         else if (!strcasecmp(argv[i],"-c")) i++;
         else {
            printf("Invalid command-line option \"%s\"\n",argv[i]);
            exit(0);
         }
   }

   f=fopen(conf.pidfile,"r");
   if (f!=NULL) {
      fgets(s,10,f); fclose(f); i=atoi(s);
      kill(i,SIGCHLD);
      if (errno!=ESRCH) {
         printf("I seem to be already running from this directory.\n");
         printf("If this is incorrect, erase the '%s' file.\n",conf.pidfile);
         printf("(Running on PID %u)\n\n",i);
         exit(-1);
      }
   }

   if (!loadconf(&conf)) printf("Configuration (Using defaults):\n");
   else printf("Configuration:\n");
   saveconf(&conf);

   if (*conf.cloak) {
      strncpy(argv[0],conf.cloak,sizeof(conf.cloak));
      printf(" |- Cloaked as.......:%s\n",conf.cloak);
   }
   printf(" |- Daemon host......:%s\n",(*conf.lhost ? conf.lhost : "Default"));
   printf(" |- Daemon port......:%u\n",conf.lport);
   printf(" |- Default conn host:%s\n",(*conf.vhosts.host ? conf.vhosts.host : "Default"));
   printf(" |- Default conn port:%u\n",conf.dport);
   printf(" `- Max users........:");
   if (conf.maxusers) printf("%u",conf.maxusers);
   else printf("Unlimited");
   printf("\n\n");

   conf.logf=fopen(conf.logfile,"a+");
   initproxy(&conf);

   i=fork();
   switch(i) {
      case -1:
         fprintf(stderr,"fatal: unable to fork()\n");
         putlog("fatal: unable to fork() during iBNC startup");
         exit(-1);
      case 0:
         setsid();
         break;
      default:
         printf("Forked into the background (PID %u)\n",i);
         putlog("iBNC Started (PID %u)",i);
         unlink(conf.pidfile);
         f=fopen(conf.pidfile,"w"); if (f!=NULL) {
            fprintf(f,"%u\n",i);
            fclose(f);
         } else {
            printf("* Warning!  Could not write %s file!\n",conf.pidfile);
            putlog("Unable to write %s file",conf.pidfile);
         }
         exit(0);
   }
   signal(SIGINT,death);
   signal(SIGTERM,death);
   signal(SIGKILL,death);
   signal(SIGQUIT,death);
   proxy(&conf);
}
