Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / motd.c
index e4be1c28762e1585c2cc244ef69469b1c877046b..2dc5daaafc497eb11a756ab6b72e3a09b5bfc112 100644 (file)
@@ -29,6 +29,7 @@
 #include "fileio.h"
 #include "ircd.h"
 #include "ircd_alloc.h"
+#include "ircd_features.h"
 #include "ircd_reply.h"
 #include "ircd_string.h"
 #include "match.h"
@@ -50,6 +51,7 @@ static struct {
   struct Motd* local;
   struct Motd* remote;
   struct Motd* other;
+  struct Motd* freelist;
 } MotdList;
 
 /* Create a struct Motd and initialize it */
@@ -57,32 +59,41 @@ static struct Motd *
 motd_create(const char *hostmask, const char *path, int maxcount)
 {
   struct Motd* tmp;
-  int class = -1;
   int type = MOTD_UNIVERSAL;
   const char* s;
 
   assert(0 != path);
 
   if (hostmask) { /* figure out if it's a class or hostmask */
+    type = MOTD_CLASS; /* all digits, convert to class */
+
     for (s = hostmask; *s; s++)
       if (!IsDigit(*s)) { /* not a digit, not a class... */
        type = MOTD_HOSTMASK;
        break;
       }
-
-    type = MOTD_CLASS; /* all digits, convert to class */
-    class = atoi(hostmask);
   }
 
   /* allocate memory and initialize the structure */
-  tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
+  if (MotdList.freelist) {
+    tmp = MotdList.freelist;
+    MotdList.freelist = tmp->next;
+  } else
+    tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
 
   tmp->next = 0;
   tmp->type = type;
-  if (type == MOTD_HOSTMASK)
+
+  switch (type) {
+  case MOTD_HOSTMASK:
     DupString(tmp->id.hostmask, hostmask);
-  else if (type == MOTD_CLASS)
-    tmp->id.class = class;
+    break;
+
+  case MOTD_CLASS:
+    tmp->id.class = atoi(hostmask);
+    break;
+  }
+
   DupString(tmp->path, path);
   tmp->maxcount = maxcount;
   tmp->cache = 0;
@@ -175,7 +186,8 @@ motd_destroy(struct Motd *motd)
   if (motd->cache) /* drop the cache */
     motd_decache(motd);
 
-  MyFree(motd); /* free the structure */
+  motd->next = MotdList.freelist;
+  MotdList.freelist = motd;
 }
 
 /* We use this routine to look up the struct Motd to send to any given
@@ -199,7 +211,7 @@ motd_lookup(struct Client *cptr)
     if (ptr->type == MOTD_CLASS && ptr->id.class == class)
       return ptr;
     else if (ptr->type == MOTD_HOSTMASK &&
-            !match(ptr->id.hostmask, cptr->sockhost))
+            !match(ptr->id.hostmask, cli_sockhost(cptr)))
       return ptr;
   }
 
@@ -218,7 +230,7 @@ motd_forward(struct Client *cptr, struct MotdCache *cache)
     return send_reply(cptr, ERR_NOMOTD);
 
   /* send the motd */
-  send_reply(cptr, RPL_MOTDSTART, me.name);
+  send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
   send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":- %d-%d-%d %d:%02d",
             cache->modtime.tm_year + 1900, cache->modtime.tm_mon + 1,
             cache->modtime.tm_mday, cache->modtime.tm_hour,
@@ -247,18 +259,18 @@ motd_signon(struct Client* cptr)
 
   cache = motd_cache(motd_lookup(cptr));
 
-#ifdef NODEFAULTMOTD
-  send_reply(cptr, RPL_MOTDSTART, me.name);
-  send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the AUP "
-            "before continuing using this service.\002");
-  send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was last "
-            "changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
-            cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
-            cache->modtime.tm_hour, cache->modtime.tm_min);
-  send_reply(cptr, RPL_ENDOFMOTD);
-#else
-  motd_forward(cptr, cache);
-#endif
+  if (!feature_bool(FEAT_NODEFAULTMOTD))
+    motd_forward(cptr, cache);
+  else {
+    send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
+    send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":\002Type /MOTD to read the "
+              "AUP before continuing using this service.\002");
+    send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":The message of the day was "
+              "last changed: %d-%d-%d %d:%d", cache->modtime.tm_year + 1900,
+              cache->modtime.tm_mon + 1, cache->modtime.tm_mday,
+              cache->modtime.tm_hour, cache->modtime.tm_min);
+    send_reply(cptr, RPL_ENDOFMOTD);
+  }
 }
 
 /* motd_recache causes all the MOTD caches to be cleared */
@@ -284,11 +296,11 @@ motd_recache(void)
 void
 motd_init(void)
 {
-  MotdList.local = motd_create(0, MPATH, MOTD_MAXLINES); /* init local */
-  motd_cache(MotdList.local); /* and cache it */
+  MotdList.local = motd_create(0, feature_str(FEAT_MPATH), MOTD_MAXLINES);
+  motd_cache(MotdList.local); /* init local and cache it */
 
-  MotdList.remote = motd_create(0, RPATH, MOTD_MAXREMOTE); /* init remote */
-  motd_cache(MotdList.remote); /* and cache it */
+  MotdList.remote = motd_create(0, feature_str(FEAT_RPATH), MOTD_MAXREMOTE);
+  motd_cache(MotdList.remote); /* init remote and cache it */
 
   MotdList.other = 0; /* no T-lines processed yet */
 }
@@ -320,6 +332,8 @@ motd_clear(void)
       motd_destroy(ptr);
     }
 
+  MotdList.other = 0;
+
   /* now recache local and remote MOTDs */
   motd_cache(MotdList.local);
   motd_cache(MotdList.remote);