- Added some tools to detect memory leaks.
[ircu2.10.12-pk.git] / ircd / motd.c
index 262e2eaa0a72351a0a04f938f8e2d727fa86673b..393d1ee4c1d2768ce6ed243c75e1aa57027724a9 100644 (file)
@@ -63,40 +63,27 @@ motd_create(const char *hostmask, const char *path, int maxcount)
 {
   struct Motd* tmp;
   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;
-      }
-  }
-
+  
+  if (hostmask != NULL && find_class(hostmask))
+    type = MOTD_CLASS;
+  else
+    type = MOTD_HOSTMASK;
   /* allocate memory and initialize the structure */
-  if (MotdList.freelist) {
+  if (MotdList.freelist)
+  {
     tmp = MotdList.freelist;
     MotdList.freelist = tmp->next;
   } else
     tmp = (struct Motd *)MyMalloc(sizeof(struct Motd));
-
+  
   tmp->next = 0;
   tmp->type = type;
-
-  switch (type) {
-  case MOTD_HOSTMASK:
-    DupString(tmp->id.hostmask, hostmask);
-    break;
-
-  case MOTD_CLASS:
-    tmp->id.class = atoi(hostmask);
-    break;
-  }
-
+  
+  if (hostmask != NULL)
+    DupString(tmp->hostmask, hostmask);
+  
   DupString(tmp->path, path);
   tmp->maxcount = maxcount;
   tmp->cache = 0;
@@ -169,9 +156,11 @@ motd_cache(struct Motd *motd)
   fbclose(file); /* close the file */
 
   /* trim memory usage a little */
-  motd->cache = (struct MotdCache *)MyRealloc(cache, sizeof(struct MotdCache) +
-                                             (MOTD_LINESIZE *
-                                              (cache->count - 1)));
+  motd->cache = (struct MotdCache*)MyMalloc(sizeof(struct MotdCache) +
+                                            (MOTD_LINESIZE * (cache->count - 1)));
+  memcpy(motd->cache, cache, sizeof(struct MotdCache) +
+         (MOTD_LINESIZE * (cache->count - 1)));
+  MyFree(cache);
 
   /* now link it in... */
   motd->cache->next = MotdList.cachelist;
@@ -213,8 +202,7 @@ motd_destroy(struct Motd *motd)
   assert(0 != motd);
 
   MyFree(motd->path); /* we always must have a path */
-  if (motd->type == MOTD_HOSTMASK) /* free a host mask if any */
-    MyFree(motd->id.hostmask);
+  MyFree(motd->hostmask);
   if (motd->cache) /* drop the cache */
     motd_decache(motd);
 
@@ -229,7 +217,7 @@ static struct Motd *
 motd_lookup(struct Client *cptr)
 {
   struct Motd *ptr;
-  int class = -1;
+  char *class = NULL;
 
   assert(0 != cptr);
 
@@ -238,12 +226,14 @@ motd_lookup(struct Client *cptr)
 
   class = get_client_class(cptr);
 
-  /* check the T-lines first */
-  for (ptr = MotdList.other; ptr; ptr = ptr->next) {
-    if (ptr->type == MOTD_CLASS && ptr->id.class == class)
+  /* check the motd blocks first */
+  for (ptr = MotdList.other; ptr; ptr = ptr->next)
+  {
+    if (ptr->type == MOTD_CLASS &&
+        !match(ptr->hostmask, class))
       return ptr;
-    else if (ptr->type == MOTD_HOSTMASK &&
-            !match(ptr->id.hostmask, cli_sockhost(cptr)))
+    else if (ptr->type == MOTD_HOSTMASK && class != NULL &&
+             !match(ptr->hostmask, cli_sockhost(cptr)))
       return ptr;
   }
 
@@ -288,6 +278,7 @@ void
 motd_signon(struct Client* cptr)
 {
   struct MotdCache *cache;
+  const char *banner = NULL;
 
   cache = motd_cache(motd_lookup(cptr));
 
@@ -295,6 +286,8 @@ motd_signon(struct Client* cptr)
     motd_forward(cptr, cache);
   else {
     send_reply(cptr, RPL_MOTDSTART, cli_name(&me));
+    if ((banner = feature_str(FEAT_MOTD_BANNER)))
+      send_reply(cptr, SND_EXPLICIT | RPL_MOTD, ":%s", banner);
     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 "
@@ -363,7 +356,8 @@ motd_clear(void)
   motd_decache(MotdList.remote);
 
   if (MotdList.other) /* destroy other MOTDs */
-    for (ptr = MotdList.other; ptr; ptr = next) {
+    for (ptr = MotdList.other; ptr; ptr = next)
+    { 
       next = ptr->next;
       motd_destroy(ptr);
     }
@@ -381,11 +375,7 @@ motd_report(struct Client *to)
 {
   struct Motd *ptr;
 
-  for (ptr = MotdList.other; ptr; ptr = ptr->next) {
-    if (ptr->type == MOTD_CLASS) /* class requires special handling */
-      send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %d %s", ptr->id.class,
-                ptr->path);
-    else if (ptr->type == MOTD_HOSTMASK)
-      send_reply(to, RPL_STATSTLINE, 'T', ptr->id.hostmask, ptr->path);
-  }
+  for (ptr = MotdList.other; ptr; ptr = ptr->next)
+    send_reply(to, SND_EXPLICIT | RPL_STATSTLINE, "T %s %s",
+               ptr->hostmask, ptr->path);
 }