Suppress pointless display of oplevels in ?chaninfo.
[srvx.git] / src / proto-p10.c
index 9c8862b93ccd96b4b172cda728ab9e585d123dc1..48b65274fa8f1bc7a350616fb921318e3f8494db 100644 (file)
@@ -299,7 +299,17 @@ static struct userNode *AddUser(struct server* uplink, const char *nick, const c
 
 extern int off_channel;
 
-static int parse_oplevel(char *str);
+/*
+ * Oplevel parsing
+ */
+static int
+parse_oplevel(char *str)
+{
+    int oplevel = 0;
+    while (isdigit(*str))
+        oplevel = oplevel * 10 + *str++ - '0';
+    return oplevel;
+}
 
 /* Numerics can be XYY, XYYY, or XXYYY; with X's identifying the
  * server and Y's indentifying the client on that server. */
@@ -395,7 +405,7 @@ irc_p10_pton(irc_in_addr_t *ip, const char *input)
         do {
             if (*input == '_') {
                 unsigned int left;
-                for (left = (25 - strlen(input)) / 3; left; left--)
+                for (left = (25 - strlen(input)) / 3 - pos; left; left--)
                     ip->in6[pos++] = 0;
                 input++;
             } else {
@@ -446,6 +456,7 @@ irc_p10_ntop(char *output, const irc_in_addr_t *ip)
             } else {
                 inttobase64(output, ntohs(ip->in6[ii]), 3);
                 output += 3;
+                ii += 1;
             }
         }
         *output = '\0';
@@ -479,9 +490,9 @@ irc_account(struct userNode *user, const char *stamp, unsigned long timestamp, u
 }
 
 void
-irc_fakehost(struct userNode *user, const char *host)
+irc_fakehost(struct userNode *user, const char *host, const char *ident, int force)
 {
-    putsock("%s " P10_FAKEHOST " %s %s", self->numeric, user->numeric, host);
+    putsock("%s " P10_FAKEHOST " %s %s %s%s", self->numeric, user->numeric, ident, host, force ? " FORCE" : "");
 }
 
 void
@@ -665,6 +676,32 @@ irc_ungline(const char *mask)
     putsock("%s " P10_GLINE " * -%s", self->numeric, mask);
 }
 
+/* Return negative if *(struct modeNode**)pa is "less than" pb,
+ * positive if pa is "larger than" pb.  Comparison is based on sorting
+ * so that non-voiced/non-opped users are first, voiced-only users are
+ * next, and the "strongest" oplevels are before "weaker" oplevels.
+ * Within those sets, ordering is arbitrary.
+ */
+static int
+modeNode_sort_p10(const void *pa, const void *pb)
+{
+        struct modeNode *a = *(struct modeNode**)pa;
+        struct modeNode *b = *(struct modeNode**)pb;
+
+        if (a->modes & MODE_CHANOP) {
+            if (!(b->modes & MODE_CHANOP))
+                return 1;
+            else if ((a->modes & MODE_VOICE) != (b->modes & MODE_VOICE))
+                return (a->modes & MODE_VOICE) - (b->modes & MODE_VOICE);
+            else if (a->oplevel != b->oplevel)
+                return a->oplevel - b->oplevel;
+        } else if (b->modes & MODE_CHANOP)
+            return -1;
+        else if ((a->modes & MODE_VOICE) != (b->modes & MODE_VOICE))
+            return (a->modes & MODE_VOICE) - (b->modes & MODE_VOICE);
+        return (a < b) ? -1 : 1;
+}
+
 static void
 irc_burst(struct chanNode *chan)
 {
@@ -672,7 +709,9 @@ irc_burst(struct chanNode *chan)
     int pos, base_len, len;
     struct modeNode *mn;
     struct banNode *bn;
-    long last_mode=-1;
+    int last_oplevel = 0;
+    int last_mode = 0;
+    int new_modes;
     unsigned int first_ban;
     unsigned int n;
 
@@ -684,6 +723,9 @@ irc_burst(struct chanNode *chan)
     if (len > 0 && chan->members.used > 0)
         burst_line[pos++] = ' ';
 
+    /* sort the users for oplevel-sending purposes */
+    qsort(chan->members.list, chan->members.used, sizeof(chan->members.list[0]), modeNode_sort_p10);
+
     /* dump the users */
     for (n=0; n<chan->members.used; n++) {
         mn = chan->members.list[n];
@@ -691,17 +733,32 @@ irc_burst(struct chanNode *chan)
             burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
             putsock("%s", burst_line);
             pos = base_len;
-            last_mode = -1;
+            last_mode = 0;
+            last_oplevel = 0;
         }
         memcpy(burst_line+pos, mn->user->numeric, strlen(mn->user->numeric));
         pos += strlen(mn->user->numeric);
-        if (mn->modes && (mn->modes != last_mode)) {
-            last_mode = mn->modes;
+        new_modes = mn->modes & (MODE_CHANOP | MODE_VOICE);
+        if (new_modes != last_mode) {
+            last_mode = new_modes;
             burst_line[pos++] = ':';
-            if (last_mode & MODE_CHANOP)
-                burst_line[pos++] = 'o';
-            if (last_mode & MODE_VOICE)
+            if (new_modes & MODE_VOICE)
                 burst_line[pos++] = 'v';
+            /* Note: :vNNN (oplevel NNN with voice) resets the
+             * implicit oplevel back to zero, so we always use the raw
+             * oplevel value here.  Read ircu's m_burst.c for more
+             * examples.
+             */
+            if (new_modes & MODE_CHANOP) {
+                last_oplevel = mn->oplevel;
+                if (mn->oplevel < MAXOPLEVEL)
+                    pos += sprintf(burst_line + pos, "%u", mn->oplevel);
+                else
+                    burst_line[pos++] = 'o';
+            }
+        } else if ((last_mode & MODE_CHANOP) && (mn->oplevel != last_oplevel)) {
+            pos += sprintf(burst_line + pos, ":%u", mn->oplevel - last_oplevel);
+            last_oplevel = mn->oplevel;
         }
         if ((n+1)<chan->members.used)
             burst_line[pos++] = ',';
@@ -761,8 +818,8 @@ void
 irc_kill(struct userNode *from, struct userNode *target, const char *message)
 {
     if (from) {
-        putsock("%s " P10_KILL " %s :%s!%s (%s)",
-                from->numeric, target->numeric, self->name, from->nick, message);
+        putsock("%s " P10_KILL " %s :%s (%s)",
+                from->numeric, target->numeric, from->nick, message);
     } else {
         putsock("%s " P10_KILL " %s :%s (%s)",
                 self->numeric, target->numeric, self->name, message);
@@ -888,7 +945,11 @@ static CMD_FUNC(cmd_whois)
         return 1;
     }
 
-    if (IsFakeHost(who) && IsHiddenHost(who))
+    if (IsFakeHost(who) && IsFakeIdent(who) && IsHiddenHost(who))
+        irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->fakeident, who->fakehost, who->info);
+    else if (IsFakeIdent(who) && IsHiddenHost(who))
+        irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->fakeident, who->hostname, who->info);
+    else if (IsFakeHost(who) && IsHiddenHost(who))
         irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->ident, who->fakehost, who->info);
     else if (IsHiddenHost(who) && who->handle_info && hidden_host_suffix)
         irc_numeric(from, RPL_WHOISUSER, "%s %s %s.%s * :%s", who->nick, who->ident, who->handle_info->handle, hidden_host_suffix, who->info);
@@ -1185,12 +1246,22 @@ static CMD_FUNC(cmd_account)
 static CMD_FUNC(cmd_fakehost)
 {
     struct userNode *user;
+    const char *host, *ident;
 
     if ((argc < 3) || !origin || !GetServerH(origin))
         return 0;
     if (!(user = GetUserN(argv[1])))
         return 1;
-    assign_fakehost(user, argv[2], 0);
+
+    if (argc > 3) {
+        ident = argv[2];
+        host = argv[3];
+    } else {
+        ident = NULL;
+        host = argv[2];
+    }
+
+    assign_fakehost(user, host, ident, 0, 0);
     return 1;
 }
 
@@ -1203,7 +1274,7 @@ static CMD_FUNC(cmd_burst)
     struct userNode *un;
     struct modeNode *mNode;
     long mode;
-    int oplevel = -1;
+    int oplevel = 0;
     char *user, *end, sep;
     unsigned long in_timestamp;
 
@@ -1247,16 +1318,13 @@ static CMD_FUNC(cmd_burst)
             while ((sep = *end++)) {
                 if (sep == 'o') {
                     mode |= MODE_CHANOP;
-                    oplevel = -1;
+                    oplevel = MAXOPLEVEL;
                 } else if (sep == 'v') {
                     mode |= MODE_VOICE;
-                    oplevel = -1;
+                    oplevel = 0;
                 } else if (isdigit(sep)) {
                     mode |= MODE_CHANOP;
-                    if (oplevel >= 0)
-                        oplevel += parse_oplevel(end);
-                    else
-                        oplevel = parse_oplevel(end);
+                    oplevel += parse_oplevel(end - 1);
                     while (isdigit(*end)) end++;
                 } else
                     break;
@@ -2243,16 +2311,25 @@ void mod_usermode(struct userNode *user, const char *mode_change) {
                 call_account_func(user, tag, ts, id);
             }
             break;
-        case 'f':
+        case 'h':
             if (*word) {
-                char host[MAXLEN];
+                char mask[MAXLEN];
+                char *host, *ident;
                 unsigned int ii;
                 for (ii=0; (*word != ' ') && (*word != '\0'); )
-                    host[ii++] = *word++;
-                host[ii] = 0;
+                    mask[ii++] = *word++;
+                mask[ii] = 0;
                 while (*word == ' ')
                     word++;
-                assign_fakehost(user, host, 0);
+
+                if ((host = strrchr(mask, '@'))) {
+                    ident = mask;
+                    *host++ = '\0';
+                } else {
+                    ident = NULL;
+                    host = mask;
+                }
+                assign_fakehost(user, host, ident, 0, 0);
             }
             break;
         }
@@ -2366,7 +2443,7 @@ mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, un
         case 'A':
             if (add) {
                 if ((in_arg >= argc)
-                    || keyncpy(change->new_upass, modes[in_arg++], sizeof(change->new_upass)))
+                    || keyncpy(change->new_apass, modes[in_arg++], sizeof(change->new_apass)))
                     goto error;
                 change->modes_set |= MODE_APASS;
             } else {
@@ -2408,7 +2485,7 @@ mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, un
             else if (channel->modes & MODE_UPASS)
                 oplevel = base_oplevel + 1;
             else
-                oplevel = -1;
+                oplevel = MAXOPLEVEL;
 
             /* Check that oplevel is within bounds. */
             if (oplevel > MAXOPLEVEL)
@@ -2429,8 +2506,9 @@ mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, un
                 continue;
             if ((change->args[ch_arg].u.member = GetUserMode(channel, victim)))
             {
-                /* Apply the oplevel change */
-                change->args[ch_arg].u.member->oplevel = oplevel;
+                /* Apply the oplevel change if the user is being (de-)opped */
+                if (modes[0][ii] == 'o')
+                    change->args[ch_arg].u.member->oplevel = oplevel;
                 ch_arg++;
             }
             break;
@@ -2847,15 +2925,3 @@ send_burst(void)
     for (it = dict_first(channels); it; it = iter_next(it))
         dict_insert(unbursted_channels, iter_key(it), iter_data(it));
 }
-
-/*
- * Oplevel parsing
- */
-static int
-parse_oplevel(char *str)
-{
-    int oplevel = 0;
-    while (isdigit(*str))
-        oplevel = oplevel * 10 + *str++ - '0';
-    return oplevel;
-}