Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / ircd.c
index 872ec4b202aeb3b00f8bccd04a8cfec253e6d954..24d966de22ce7ff9ca73618cdc98bbfb7f42b10b 100644 (file)
  * $Id$
  */
 #include "ircd.h"
-
 #include "IPcheck.h"
 #include "class.h"
 #include "client.h"
 #include "crule.h"
 #include "hash.h"
 #include "ircd_alloc.h"
+#include "ircd_features.h"
 #include "ircd_log.h"
 #include "ircd_reply.h"
 #include "ircd_signal.h"
@@ -34,6 +34,7 @@
 #include "jupe.h"
 #include "list.h"
 #include "match.h"
+#include "motd.h"
 #include "msg.h"
 #include "numeric.h"
 #include "numnicks.h"
@@ -83,6 +84,7 @@ enum {
  * Global data (YUCK!)
  *--------------------------------------------------------------------------*/
 struct Client  me;                      // That's me
+struct Connection me_con;              // That's me too
 struct Client *GlobalClientList  = &me; // Pointer to beginning of Client list
 time_t         TSoffset          = 0;   // Offset of timestamps to system clock
 int            GlobalRehashFlag  = 0;   // do a rehash if set
@@ -105,8 +107,8 @@ static struct Daemon thisServer  = { 0 };     // server process info
  * API: server_die
  *--------------------------------------------------------------------------*/
 void server_die(const char* message) {
-  ircd_log(L_CRIT, "Server terminating: %s", message);
-  sendto_opmask_butone(0, SNO_OLDSNO, "Server terminating: %s", message);
+  /* log_write will send out message to both log file and as server notice */
+  log_write(LS_SYSTEM, L_CRIT, 0, "Server terminating: %s", message);
   flush_connections(0);
   close_connections(1);
   thisServer.running = 0;
@@ -119,22 +121,27 @@ void server_die(const char* message) {
 void server_restart(const char* message) {
   static int restarting = 0;
 
-  ircd_log(L_WARNING, "Restarting Server: %s", message);
-  if (restarting)
+  /* inhibit sending any server notices; we may be in a loop */
+  log_write(LS_SYSTEM, L_WARNING, LOG_NOSNOTICE, "Restarting Server: %s",
+           message);
+  if (restarting++) /* increment restarting to prevent looping */
     return;
 
   sendto_opmask_butone(0, SNO_OLDSNO, "Restarting server: %s", message);
   Debug((DEBUG_NOTICE, "Restarting server..."));
   flush_connections(0);
 
-  close_log();
+  log_close();
+
   close_connections(!(thisServer.bootopt & (BOOT_TTY | BOOT_DEBUG)));
 
   execv(SPATH, thisServer.argv);
 
   /* Have to reopen since it has been closed above */
-  open_log(*thisServer.argv);
-  ircd_log(L_CRIT, "execv(%s,%s) failed: %m\n", SPATH, *thisServer.argv);
+  log_reopen();
+
+  log_write(LS_SYSTEM, L_CRIT, 0, "execv(%s,%s) failed: %m", SPATH,
+           *thisServer.argv);
 
   Debug((DEBUG_FATAL, "Couldn't restart server \"%s\": %s",
          SPATH, (strerror(errno)) ? strerror(errno) : ""));
@@ -157,14 +164,16 @@ static void outofmemory(void) {
 static void write_pidfile(void) {
   FILE *pidf;
 
-  if (!(pidf = fopen(PPATH, "w+"))) {
+  if (!(pidf = fopen(feature_str(FEAT_PPATH), "w+"))) {
     Debug((DEBUG_NOTICE, 
-          "Error opening pid file \"%s\": %s", PPATH, strerror(errno)));
+          "Error opening pid file \"%s\": %s", feature_str(FEAT_PPATH),
+          strerror(errno)));
     return;
   }
     
   if (fprintf(pidf, "%5d\n", getpid()) < 5)
-    Debug((DEBUG_NOTICE, "Error writing to pid file %s", PPATH));
+    Debug((DEBUG_NOTICE, "Error writing to pid file %s",
+          feature_str(FEAT_PPATH)));
 
   fclose(pidf);
 }
@@ -186,7 +195,7 @@ static time_t try_connections(void) {
   int               connecting;
   int               confrq;
   time_t            next        = 0;
-  struct ConfClass* cltmp;
+  struct ConnectionClass* cltmp;
   struct ConfItem*  con_conf    = 0;
   struct Jupe*      ajupe;
   unsigned int      con_class   = 0;
@@ -212,7 +221,7 @@ static time_t try_connections(void) {
       continue;
     }
 
-    cltmp = aconf->confClass;
+    cltmp = aconf->conn_class;
     confrq = get_con_freq(cltmp);
     aconf->hold = CurrentTime + confrq;
 
@@ -280,33 +289,33 @@ static time_t check_pings(void) {
 
     /* Remove dead clients. */
     if (IsDead(cptr)) {
-      exit_client(cptr, cptr, &me, cptr->info);
+      exit_client(cptr, cptr, &me, cli_info(cptr));
       continue;
     }
 
-    max_ping = IsRegistered(cptr) ? get_client_ping(cptr) : CONNECTTIMEOUT;
+    max_ping = IsRegistered(cptr) ? client_get_ping(cptr) : CONNECTTIMEOUT;
    
     Debug((DEBUG_DEBUG, "check_pings(%s)=status:%s limit: %d current: %d",
-          cptr->name, (cptr->flags & FLAGS_PINGSENT) ? "[Ping Sent]" : "[]", 
-          max_ping, (int)(CurrentTime - cptr->lasttime)));
+          cli_name(cptr), (cli_flags(cptr) & FLAGS_PINGSENT) ? "[Ping Sent]" : "[]", 
+          max_ping, (int)(CurrentTime - cli_lasttime(cptr))));
           
 
     /* Ok, the thing that will happen most frequently, is that someone will
      * have sent something recently.  Cover this first for speed.
      */
-    if (CurrentTime-cptr->lasttime < max_ping) {
-      expire = cptr->lasttime + max_ping;
+    if (CurrentTime-cli_lasttime(cptr) < max_ping) {
+      expire = cli_lasttime(cptr) + max_ping;
       if (expire < next_check) 
        next_check = expire;
       continue;
     }
 
     /* Quit the client after max_ping*2 - they should have answered by now */
-    if (CurrentTime-cptr->lasttime >= (max_ping*2) ) {
+    if (CurrentTime-cli_lasttime(cptr) >= (max_ping*2) ) {
       /* If it was a server, then tell ops about it. */
       if (IsServer(cptr) || IsConnecting(cptr) || IsHandshake(cptr))
        sendto_opmask_butone(0, SNO_OLDSNO,
-                            "No response from %s, closing link", cptr->name);
+                            "No response from %s, closing link", cli_name(cptr));
       exit_client_msg(cptr, cptr, &me, "Ping timeout");
       continue;
     }
@@ -320,7 +329,7 @@ static time_t check_pings(void) {
       /* Display message if they have sent a NICK and a USER but no
        * nospoof PONG.
        */
-      if (*cptr->name && cptr->user && *cptr->user->username) {
+      if (*(cli_name(cptr)) && cli_user(cptr) && *(cli_user(cptr))->username) {
        send_reply(cptr, SND_EXPLICIT | ERR_BADPING,
                   ":Your client may not be compatible with this server.");
        send_reply(cptr, SND_EXPLICIT | ERR_BADPING,
@@ -331,22 +340,22 @@ static time_t check_pings(void) {
       continue;
     }
     
-    if (!(cptr->flags & FLAGS_PINGSENT)) {
+    if (!(cli_flags(cptr) & FLAGS_PINGSENT)) {
       /* If we havent PINGed the connection and we havent heard from it in a
        * while, PING it to make sure it is still alive.
        */
-      cptr->flags |= FLAGS_PINGSENT;
+      cli_flags(cptr) |= FLAGS_PINGSENT;
 
       /* If we're late in noticing don't hold it against them :) */
-      cptr->lasttime = CurrentTime - max_ping;
+      cli_lasttime(cptr) = CurrentTime - max_ping;
       
       if (IsUser(cptr))
-       sendrawto_one(cptr, MSG_PING " :%s", me.name);
+       sendrawto_one(cptr, MSG_PING " :%s", cli_name(&me));
       else
-       sendcmdto_one(&me, CMD_PING, cptr, ":%s", me.name);
+       sendcmdto_one(&me, CMD_PING, cptr, ":%s", cli_name(&me));
     }
     
-    expire = cptr->lasttime + max_ping * 2;
+    expire = cli_lasttime(cptr) + max_ping * 2;
     if (expire < next_check)
       next_check=expire;
   }
@@ -381,7 +390,7 @@ static void parse_command_line(int argc, char** argv) {
     case 't':  thisServer.bootopt |= BOOT_TTY;         break;
     case 'd':  dpath      = optarg;                    break;
     case 'f':  configfile = optarg;                    break;
-    case 'h':  ircd_strncpy(me.name, optarg, HOSTLEN); break;
+    case 'h':  ircd_strncpy(cli_name(&me), optarg, HOSTLEN); break;
     case 'v':  printf("ircd %s\n", version);           exit(0);
       
     case 'x':
@@ -482,7 +491,7 @@ static void event_loop(void) {
     /* timeout pending queries that haven't been responded to */
     timeout_auth_queries(CurrentTime);
 
-    ip_registry_expire();
+    IPcheck_expire();
 
     if (GlobalRehashFlag) {
       rehash(&me, 1);
@@ -494,7 +503,6 @@ static void event_loop(void) {
   }
 }
 
-
 /*----------------------------------------------------------------------------
  * check_file_access:  random helper function to make sure that a file is
  *                     accessible in a certain way, and complain if not.
@@ -599,7 +607,7 @@ int main(int argc, char **argv) {
 
 #ifdef CHROOTDIR
   if (!set_chroot_environment())
-    exit(1);
+    return 1;
 #endif
 
 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_CORE)
@@ -608,87 +616,89 @@ int main(int argc, char **argv) {
 
   umask(077);                   /* better safe than sorry --SRB */
   memset(&me, 0, sizeof(me));
-  me.fd = -1;
+  memset(&me_con, 0, sizeof(me_con));
+  cli_connect(&me) = &me_con;
+  cli_fd(&me) = -1;
 
   parse_command_line(argc, argv);
 
   if (chdir(dpath)) {
     fprintf(stderr, "Fail: Cannot chdir(%s): %s, check DPATH\n", dpath, strerror(errno));
-    exit(2);
+    return 2;
   }
 
   if (!set_userid_if_needed())
-    exit(3);
+    return 3;
 
   /* Check paths for accessibility */
   if (!check_file_access(SPATH, 'S', X_OK) ||
-      !check_file_access(configfile, 'C', R_OK) ||
-      !check_file_access(MPATH, 'M', R_OK) ||
-      !check_file_access(RPATH, 'R', R_OK))
-    exit(4);
+      !check_file_access(configfile, 'C', R_OK))
+    return 4;
       
 #ifdef DEBUG
   if (!check_file_access(LPATH, 'L', W_OK))
-    exit(5);
+    return 5;
 #endif
 
   debug_init(thisServer.bootopt & BOOT_TTY);
   daemon_init(thisServer.bootopt & BOOT_TTY);
 
   setup_signals();
-  open_log(*argv);
+  feature_mark(); /* initialize features... */
+  log_init(*argv);
 
   set_nomem_handler(outofmemory);
+  
+  if (!init_string()) {
+    log_write(LS_SYSTEM, L_CRIT, 0, "Failed to initialize string module");
+    return 6;
+  }
 
   initload();
   init_list();
-  hash_init();
-  initclass();
+  init_hash();
+  init_class();
   initwhowas();
   initmsgtree();
   initstats();
 
   init_resolver();
 
-  if (!conf_init()) {
-    ircd_log(L_CRIT, "Failed to read configuration file %s", configfile);
-    exit(6);
-  }
+  motd_init();
 
-  if (!init_server_identity()) {
-    ircd_log(L_CRIT, "Failed to initialize server identity");
-    exit(7);
+  if (!init_conf()) {
+    log_write(LS_SYSTEM, L_CRIT, 0, "Failed to read configuration file %s",
+             configfile);
+    return 7;
   }
 
+  init_server_identity();
+
   uping_init();
-  read_tlines();
 
-  rmotd       = read_motd(RPATH);
-  motd        = read_motd(MPATH);
   CurrentTime = time(NULL);
 
   SetMe(&me);
-  me.from = &me;
+  cli_from(&me) = &me;
   make_server(&me);
 
-  me.serv->timestamp = TStime();  /* Abuse own link timestamp as start TS */
-  me.serv->prot      = atoi(MAJOR_PROTOCOL);
-  me.serv->up        = &me;
-  me.serv->down      = NULL;
-  me.handler         = SERVER_HANDLER;
+  cli_serv(&me)->timestamp = TStime();  /* Abuse own link timestamp as start TS */
+  cli_serv(&me)->prot      = atoi(MAJOR_PROTOCOL);
+  cli_serv(&me)->up        = &me;
+  cli_serv(&me)->down      = NULL;
+  cli_handler(&me)         = SERVER_HANDLER;
 
   SetYXXCapacity(&me, MAXCLIENTS);
 
-  me.lasttime = me.since = me.firsttime = CurrentTime;
+  cli_lasttime(&me) = cli_since(&me) = cli_firsttime(&me) = CurrentTime;
 
   hAddClient(&me);
 
-  check_class();
   write_pidfile();
   init_counters();
 
   Debug((DEBUG_NOTICE, "Server ready..."));
-  ircd_log(L_NOTICE, "Server Ready");
+  log_write(LS_SYSTEM, L_NOTICE, 0, "Server Ready");
 
   event_loop();