Author: Greg Sikorski <gte@atomicrevs.demon.co.uk>
authorGreg Sikorski <gte@atomicrevs.demon.co.uk>
Sun, 4 Jun 2000 05:19:30 +0000 (05:19 +0000)
committerGreg Sikorski <gte@atomicrevs.demon.co.uk>
Sun, 4 Jun 2000 05:19:30 +0000 (05:19 +0000)
Log message:

At present, a "/names #mychan" will iterate over the GlobalChannelList to produce
the reply.
It loops over all 25,000 channels each time someone does a /join #mychan (!)

This patch refactors /names to deal with the 2 special cases seperately, and
reduce the need for this wasted effort. (Will also try and get some gperf stats
ready soon).

Case 1. A standard /names #channel[,#channel2,#channel3 ...] - quick/simple lookup
        and list.

Case 2. A "/names 0", to list all the visibile channels & associated visible users
        followed by the remaining users not in any channels, or in secret channels.

Status:
Compiled, thoroughly testing.

Exploits such as /names 0,0,0,0,0,0,,,,,,,, [etc] are avoided, the routine drops
out after the first 0 encountered.

Testing needed:

Test remote /names, ensure invisible/secret chans aren't displayed anywhere they
shouldn't be.

git-svn-id: file:///home/klmitch/undernet-ircu/undernet-ircu-svn/ircu2/trunk@249 c9e4aea6-c8fd-4c43-8297-357d70d61c8c

ChangeLog
ircd/m_names.c

index 715e41dc54db122b2d3d428ee6815d3e08f656b2..70d34d118968d978f86a1d12e1c7cb8e10e851a2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2000-06-04  Greg Sikorski <gte@atomicrevs.demon.co.uk>
+
+       * ircd/m_names.c: Re-factor code to remove unneccessary
+       GlobalChannelList iteration every time someone joins a channel.
+
 2000-06-02  Kevin L. Mitchell  <klmitch@mit.edu>
 
        * ircd/s_user.c: add struct Gline * argument to register_user;
 #
 # ChangeLog for ircu2.10.11
 #
-# $Id: ChangeLog,v 1.143 2000-06-03 03:01:10 kev Exp $
+# $Id: ChangeLog,v 1.144 2000-06-04 05:19:29 gte Exp $
 #
 # Insert new changes at beginning of the change list.
 #
index 525db66baf8f6dee6476e39445e72f86d0f02b3f..ed3e38a3f772832f5f017d0eee81d503b0319374 100644 (file)
 #include "numnicks.h"
 #include "s_user.h"
 #include "send.h"
-
 #include <assert.h>
 #include <string.h>
 
+#define NAMES_ALL 1 /* List all users in channel */
+#define NAMES_VIS 2 /* List only visible users in non-secret channels */
+
+/*
+ *  Sends a suitably formatted 'names' reply to 'sptr' consisting of nicks within
+ *  'chptr', depending on 'filter'.
+ *
+ *  NAMES_ALL - Lists all users on channel.
+ *  NAMES_VIS - Only list visible (-i) users. --Gte (04/06/2000).
+ *
+ */
+
+void do_names(struct Client* sptr, struct Channel* chptr, int filter)
+{ 
+  int mlen;
+  int idx;
+  int flag;
+  int needs_space; 
+  int len; 
+  char buf[BUFSIZE];
+  struct Client *c2ptr;
+  struct Membership* member;
+
+  /* Tag Pub/Secret channels accordingly. */
+
+  strcpy(buf, "* ");
+  if (PubChannel(chptr))
+    *buf = '=';
+  else if (SecretChannel(chptr))
+    *buf = '@';
+  len = strlen(chptr->chname);
+  strcpy(buf + 2, chptr->chname);
+  strcpy(buf + 2 + len, " :");
+
+  idx = len + 4;
+  flag = 1;
+  needs_space = 0;
+
+  if (!ShowChannel(sptr, chptr)) /* Don't list private channels unless we are on them. */
+    return;
+
+  /* Iterate over all channel members, and build up the list. */
+
+  mlen = strlen(me.name) + 10 + strlen(sptr->name);
+  
+  for (member = chptr->members; member; member = member->next_member)
+  {
+    c2ptr = member->user;
+
+    if ((filter == NAMES_VIS) && IsInvisible(c2ptr))
+      continue;
+
+    if (needs_space) {
+       strcat(buf, " ");
+      idx++;
+    }
+    needs_space=1;
+    if (IsZombie(member))
+    {
+      if (member->user != sptr)
+        continue;
+      else
+      {
+        strcat(buf, "!");
+        idx++;
+      }
+    }
+    else if (IsChanOp(member))
+    {
+      strcat(buf, "@");
+      idx++;
+    }
+    else if (HasVoice(member))
+    {
+      strcat(buf, "+");
+      idx++;
+    }
+    strcat(buf, c2ptr->name);
+    idx += strlen(c2ptr->name) + 1;
+    flag = 1;
+    if (mlen + idx + NICKLEN + 5 > BUFSIZE)
+      /* space, modifier, nick, \r \n \0 */
+    {
+      send_reply(sptr, RPL_NAMREPLY, buf);
+      strcpy(buf, "* ");
+      ircd_strncpy(buf + 2, chptr->chname, len + 1);
+      buf[len + 2] = 0;
+      strcat(buf, " :");
+      if (PubChannel(chptr))
+        *buf = '=';
+      else if (SecretChannel(chptr))
+        *buf = '@';
+      idx = len + 4;
+      flag = 0;
+      needs_space=0;
+    }
+  }
+  if (flag)
+    send_reply(sptr, RPL_NAMREPLY, buf); 
+}
+
 /*
  * m_names - generic message handler
  *
  * parv[0] = sender prefix
  * parv[1] = channel
  */
+
 int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
 {
-  struct Channel *chptr;
+  struct Channel *chptr; 
+  struct Channel *ch2ptr; 
   struct Client *c2ptr;
-  struct Membership* member;
-  struct Channel *ch2ptr = 0;
-  int idx;
-  int flag;
-  int len;
-  int mlen;
-  int needs_space;
+  struct Membership* member; 
   char* s;
-  char* para = parc > 1 ? parv[1] : 0;
-  char buf[BUFSIZE];
+  char* para = parc > 1 ? parv[1] : 0; 
 
   if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc,
                                  parv))
-    return 0;
-
-  mlen = strlen(me.name) + 10 + strlen(sptr->name);
+    return 0; 
 
   if (EmptyString(para))
     return 0;
   else if (*para == '0')
     *para = '\0';
   
-  s = strchr(para, ',');
+  s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
   if (s) {
     parv[1] = ++s;
     m_names(cptr, sptr, parc, parv);
   }
-  clean_channelname(para);
-  ch2ptr = FindChannel(para);
-
   /*
-   * First, do all visible channels (public and the one user self is)
+   * Special Case 1: "/names 0". 
+   * Full list as per RFC. 
    */
 
-  for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
-  {
-    if ((chptr != ch2ptr) && !EmptyString(para))
-      continue;                 /* -- wanted a specific channel */
-    if (!MyConnect(sptr) && EmptyString(para))
-      continue;
-#ifndef GODMODE
-    if (!ShowChannel(sptr, chptr))
-      continue;                 /* -- users on this are not listed */
-#endif
+  if (!*para) { 
+    int idx; 
+    int mlen;
+    int flag;
+    struct Channel *ch3ptr;
+    char buf[BUFSIZE]; 
 
-    /* Find users on same channel (defined by chptr) */
+    mlen = strlen(me.name) + 10 + strlen(sptr->name);
 
-    strcpy(buf, "* ");
-    len = strlen(chptr->chname);
-    strcpy(buf + 2, chptr->chname);
-    strcpy(buf + 2 + len, " :");
+    /* List all visible channels/visible members */ 
 
-    if (PubChannel(chptr))
-      *buf = '=';
-    else if (SecretChannel(chptr))
-      *buf = '@';
-    idx = len + 4;
-    flag = 1;
-    needs_space = 0;
-    for (member = chptr->members; member; member = member->next_member)
-    {
-      c2ptr = member->user;
-      if (needs_space) {
-       strcat(buf, " ");
-        idx++;
-      }
-      needs_space=1;
-      if (IsZombie(member))
-      {
-        if (member->user != sptr)
-          continue;
-        else
-        {
-          strcat(buf, "!");
-          idx++;
-        }
-      }
-      else if (IsChanOp(member))
+    for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
+    { 
+      if (!ShowChannel(sptr, ch2ptr))
+        continue;                 /* Don't show secret chans. */ 
+
+      if (find_channel_member(sptr, ch2ptr))
       {
-        strcat(buf, "@");
-        idx++;
+        do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
+      } else { 
+        do_names(sptr, ch2ptr, NAMES_VIS);
       }
-      else if (HasVoice(member))
+    } 
+
+    /* List all remaining users on channel '*' */
+
+    strcpy(buf, "* * :");
+    idx = 5;
+    flag = 0;
+
+    for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
+    {
+      int showflag = 0;
+
+      if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
+        continue;
+
+      member = c2ptr->user->channel; 
+
+      while (member)
       {
-        strcat(buf, "+");
-        idx++;
+        ch3ptr = member->channel;
+  
+        if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
+          showflag = 1;
+        member = member->next_channel;
       }
+
+      if (showflag)               /* Have we already shown them? */
+        continue;
       strcat(buf, c2ptr->name);
+      strcat(buf, " ");
       idx += strlen(c2ptr->name) + 1;
       flag = 1;
-      if (mlen + idx + NICKLEN + 5 > BUFSIZE)
-        /* space, modifier, nick, \r \n \0 */
+
+      if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
       {
-       send_reply(sptr, RPL_NAMREPLY, buf);
-        strcpy(buf, "* ");
-        ircd_strncpy(buf + 2, chptr->chname, len + 1);
-        buf[len + 2] = 0;
-        strcat(buf, " :");
-        if (PubChannel(chptr))
-          *buf = '=';
-        else if (SecretChannel(chptr))
-          *buf = '@';
-        idx = len + 4;
+        send_reply(sptr, RPL_NAMREPLY, buf);
+        strcpy(buf, "* * :");
+        idx = 5;
         flag = 0;
-        needs_space=0;
       }
     }
     if (flag)
       send_reply(sptr, RPL_NAMREPLY, buf);
-  }
-  if (!EmptyString(para))
-  {
-    send_reply(sptr, RPL_ENDOFNAMES, ch2ptr ? ch2ptr->chname : para);
-    return (1);
-  }
+    send_reply(sptr, RPL_ENDOFNAMES, "*");
+    return 1; 
+  } 
 
-  /* Second, do all non-public, non-secret channels in one big sweep */
+  /*
+   *  Special Case 2: User is on this channel, requesting full names list.
+   *  (As performed with each /join) - ** High frequency usage **
+   */
 
-  strcpy(buf, "* * :");
-  idx = 5;
-  flag = 0;
-  for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
-  {
-    struct Channel *ch3ptr;
-    int showflag = 0, secret = 0;
+  clean_channelname(para);
+  chptr = FindChannel(para); 
+  member = find_member_link(chptr, sptr); 
+  if (member)
+  { 
+    if (chptr) do_names(sptr, chptr, NAMES_ALL);
+    if (!EmptyString(para))
+    {
+      send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
+      return 1;
+    }
 
-#ifndef GODMODE
-    if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
-#else
-    if (!IsUser(c2ptr))
-#endif
-      continue;
-    member = c2ptr->user->channel;
+  }
+    else
+  {
     /*
-     * Don't show a client if they are on a secret channel or when
-     * they are on a channel sptr is on since they have already
-     * been show earlier. -avalon
+     *  Special Case 3: User isn't on this channel, show all visible users, in 
+     *  non secret channels.
      */
-    while (member)
-    {
-      ch3ptr = member->channel;
-#ifndef GODMODE
-      if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
-#endif
-        showflag = 1;
-      if (SecretChannel(ch3ptr))
-        secret = 1;
-      member = member->next_channel;
-    }
-    if (showflag)               /* Have we already shown them ? */
-      continue;
-#ifndef GODMODE
-    if (secret)                 /* On any secret channels ? */
-      continue;
-#endif
-    strcat(buf, c2ptr->name);
-    strcat(buf, " ");
-    idx += strlen(c2ptr->name) + 1;
-    flag = 1;
-#ifdef GODMODE
+     
+    if (chptr) do_names(sptr, chptr, NAMES_VIS);
+    if (!EmptyString(para))
     {
-      char yxx[6];
-      sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
-      assert(c2ptr == findNUser(yxx));
-      sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
-      idx += 6;
+      send_reply(sptr, RPL_ENDOFNAMES, para);
+      return 1;
     }
-#endif
-#ifdef GODMODE
-    if (mlen + idx + NICKLEN + 9 > BUFSIZE)
-#else
-    if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
-#endif
-    {
-      send_reply(sptr, RPL_NAMREPLY, buf);
-      strcpy(buf, "* * :");
-      idx = 5;
-      flag = 0;
-    }
-  }
-  if (flag)
-    send_reply(sptr, RPL_NAMREPLY, buf);
-  send_reply(sptr, RPL_ENDOFNAMES, "*");
-  return 1;
-  return 0;
+  } 
 }
 
 /*
  * ms_names - server message handler
  *
@@ -307,198 +355,139 @@ int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
  */
 int ms_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
 {
-  struct Channel *chptr;
+  struct Channel *chptr; 
+  struct Channel *ch2ptr; 
   struct Client *c2ptr;
-  struct Membership* member;
-  struct Channel *ch2ptr = 0;
-  int idx, flag, len, mlen;
-  char *s, *para = parc > 1 ? parv[1] : 0;
-  char buf[BUFSIZE];
+  struct Membership* member; 
+  char* s;
+  char* para = parc > 1 ? parv[1] : 0; 
 
   if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc,
                                  parv))
-    return 0;
-
-  mlen = strlen(me.name) + 10 + strlen(sptr->name);
+    return 0; 
 
-  if (!EmptyString(para))
-  {
-    s = strchr(para, ',');
-    if (s)
-    {
-      parv[1] = ++s;
-      m_names(cptr, sptr, parc, parv);
-    }
-    clean_channelname(para);
-    ch2ptr = FindChannel(para);
+  if (EmptyString(para))
+    return 0;
+  else if (*para == '0')
+    *para = '\0';
+  
+  s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
+  if (s) {
+    parv[1] = ++s;
+    m_names(cptr, sptr, parc, parv);
   }
-
   /*
-   * First, do all visible channels (public and the one user self is)
+   * Special Case 1: "/names 0".
+   * Full list as per RFC. 
    */
 
-  for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
-  {
-    if ((chptr != ch2ptr) && !EmptyString(para))
-      continue;                 /* -- wanted a specific channel */
-    if (!MyConnect(sptr) && EmptyString(para))
-      continue;
-#ifndef GODMODE
-    if (!ShowChannel(sptr, chptr))
-      continue;                 /* -- users on this are not listed */
-#endif
+  if (!*para) { 
+    int idx; 
+    int mlen;
+    int flag;
+    struct Channel *ch3ptr;
+    char buf[BUFSIZE]; 
 
-    /* Find users on same channel (defined by chptr) */
+    mlen = strlen(me.name) + 10 + strlen(sptr->name);
 
-    strcpy(buf, "* ");
-    len = strlen(chptr->chname);
-    strcpy(buf + 2, chptr->chname);
-    strcpy(buf + 2 + len, " :");
+    /* List all visible channels/visible members */ 
 
-    if (PubChannel(chptr))
-      *buf = '=';
-    else if (SecretChannel(chptr))
-      *buf = '@';
-    idx = len + 4;
-    flag = 1;
-    for (member = chptr->members; member; member = member->next_member)
-    {
-      c2ptr = member->user;
-#ifndef GODMODE
-      if (sptr != c2ptr && IsInvisible(c2ptr) && !find_channel_member(sptr, chptr))
-        continue;
-#endif
-      if (IsZombie(member))
-      {
-        if (member->user != sptr)
-          continue;
-        else
-        {
-          strcat(buf, "!");
-          idx++;
-        }
-      }
-      else if (IsChanOp(member))
+    for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
+    { 
+      if (!ShowChannel(sptr, ch2ptr))
+        continue;                 /* Don't show secret chans. */ 
+
+      if (find_channel_member(sptr, ch2ptr))
       {
-        strcat(buf, "@");
-        idx++;
+        do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
+      } else { 
+        do_names(sptr, ch2ptr, NAMES_VIS);
       }
-      else if (HasVoice(member))
+    } 
+    /* List all remaining users on channel '*' */
+
+    strcpy(buf, "* * :");
+    idx = 5;
+    flag = 0;
+
+    for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
+    {
+      int showflag = 0;
+
+      if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
+        continue;
+
+      member = c2ptr->user->channel; 
+
+      while (member)
       {
-        strcat(buf, "+");
-        idx++;
+        ch3ptr = member->channel;
+  
+        if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
+          showflag = 1;
+        member = member->next_channel;
       }
+
+      if (showflag)               /* Have we already shown them? */
+        continue;
       strcat(buf, c2ptr->name);
       strcat(buf, " ");
       idx += strlen(c2ptr->name) + 1;
       flag = 1;
-#ifdef GODMODE
-      {
-        char yxx[6];
-        sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
-        assert(c2ptr == findNUser(yxx));
-        sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
-        idx += 6;
-      }
-      if (mlen + idx + NICKLEN + 11 > BUFSIZE)
-#else
-      if (mlen + idx + NICKLEN + 5 > BUFSIZE)
-#endif
-        /* space, modifier, nick, \r \n \0 */
+
+      if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
       {
-       send_reply(sptr, RPL_NAMREPLY, buf);
-        strcpy(buf, "* ");
-        ircd_strncpy(buf + 2, chptr->chname, len + 1);
-        buf[len + 2] = 0;
-        strcat(buf, " :");
-        if (PubChannel(chptr))
-          *buf = '=';
-        else if (SecretChannel(chptr))
-          *buf = '@';
-        idx = len + 4;
+        send_reply(sptr, RPL_NAMREPLY, buf);
+        strcpy(buf, "* * :");
+        idx = 5;
         flag = 0;
       }
     }
     if (flag)
       send_reply(sptr, RPL_NAMREPLY, buf);
-  }
-  if (!EmptyString(para))
-  {
-    send_reply(sptr, RPL_ENDOFNAMES, ch2ptr ? ch2ptr->chname : para);
-    return (1);
-  }
+    send_reply(sptr, RPL_ENDOFNAMES, "*");
+    return 1; 
+  } 
 
-  /* Second, do all non-public, non-secret channels in one big sweep */
+  /*
+   *  Special Case 2: User is on this channel, requesting full names list.
+   *  (As performed with each /join) - ** High frequency usage **
+   */
 
-  strcpy(buf, "* * :");
-  idx = 5;
-  flag = 0;
-  for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
-  {
-    struct Channel *ch3ptr;
-    int showflag = 0, secret = 0;
+  clean_channelname(para);
+  chptr = FindChannel(para); 
+  member = find_member_link(chptr, sptr); 
+  if (member)
+  { 
+    if (chptr) do_names(sptr, chptr, NAMES_ALL);
+    if (!EmptyString(para))
+    {
+      send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
+      return 1;
+    }
 
-#ifndef GODMODE
-    if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
-#else
-    if (!IsUser(c2ptr))
-#endif
-      continue;
-    member = c2ptr->user->channel;
+  }
+    else
+  {
     /*
-     * Don't show a client if they are on a secret channel or when
-     * they are on a channel sptr is on since they have already
-     * been show earlier. -avalon
+     *  Special Case 3: User isn't on this channel, show all visible users, in 
+     *  non secret channels.
      */
-    while (member)
+     
+    if (chptr) do_names(sptr, chptr, NAMES_VIS);
+    if (!EmptyString(para))
     {
-      ch3ptr = member->channel;
-#ifndef GODMODE
-      if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
-#endif
-        showflag = 1;
-      if (SecretChannel(ch3ptr))
-        secret = 1;
-      member = member->next_channel;
+      send_reply(sptr, RPL_ENDOFNAMES, para);
+      return 1;
     }
-    if (showflag)               /* Have we already shown them ? */
-      continue;
-#ifndef GODMODE
-    if (secret)                 /* On any secret channels ? */
-      continue;
-#endif
-    strcat(buf, c2ptr->name);
-    strcat(buf, " ");
-    idx += strlen(c2ptr->name) + 1;
-    flag = 1;
-#ifdef GODMODE
-    {
-      char yxx[6];
-      sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
-      assert(c2ptr == findNUser(yxx));
-      sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
-      idx += 6;
-    }
-#endif
-#ifdef GODMODE
-    if (mlen + idx + NICKLEN + 9 > BUFSIZE)
-#else
-    if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
-#endif
-    {
-      send_reply(sptr, RPL_NAMREPLY, buf);
-      strcpy(buf, "* * :");
-      idx = 5;
-      flag = 0;
-    }
-  }
-  if (flag)
-    send_reply(sptr, RPL_NAMREPLY, buf);
-  send_reply(sptr, RPL_ENDOFNAMES, "*");
-  return 1;
-  return 0;
+  } 
 }
-
 
 #if 0
 /*