+/*
+ SanitizeString strips color tags from the string in
+ and writes the result on string out
+*/
+void SanitizeString(char *in, char *out)
+{
+ while(*in)
+ {
+ if(*in == STRING_COLOR_TAG)
+ {
+ ++in;
+ if(!*in)
+ {
+ out[0] = STRING_COLOR_TAG;
+ out[1] = 0;
+ return;
+ }
+ else if (*in >= '0' && *in <= '9') // ^[0-9] found
+ {
+ ++in;
+ if(!*in)
+ {
+ *out = 0;
+ return;
+ } else if (*in == STRING_COLOR_TAG) // ^[0-9]^ found, don't print ^[0-9]
+ continue;
+ }
+ else if (*in == STRING_COLOR_RGB_TAG_CHAR) // ^x found
+ {
+ if ( isxdigit(in[1]) && isxdigit(in[2]) && isxdigit(in[3]) )
+ {
+ in+=4;
+ if (!*in)
+ {
+ *out = 0;
+ return;
+ } else if (*in == STRING_COLOR_TAG) // ^xrgb^ found, don't print ^xrgb
+ continue;
+ }
+ else in--;
+ }
+ else if (*in != STRING_COLOR_TAG)
+ --in;
+ }
+ *out = qfont_table[*(unsigned char*)in];
+ ++in;
+ ++out;
+ }
+ *out = 0;
+}
+
+// Now it becomes TRICKY :D --blub
+static char Nicks_list[MAX_SCOREBOARD][MAX_SCOREBOARDNAME]; // contains the nicks with colors and all that
+static char Nicks_sanlist[MAX_SCOREBOARD][MAX_SCOREBOARDNAME]; // sanitized list for completion when there are other possible matches.
+// means: when somebody uses a cvar's name as his name, we won't ever get his colors in there...
+static int Nicks_offset[MAX_SCOREBOARD]; // when nicks use a space, we need this to move the completion list string starts to avoid invalid memcpys
+static int Nicks_matchpos;
+
+// co against <<:BLASTER:>> is true!?
+int Nicks_strncasecmp_nospaces(char *a, char *b, unsigned int a_len)
+{
+ while(a_len)
+ {
+ if(tolower(*a) == tolower(*b))
+ {
+ if(*a == 0)
+ return 0;
+ --a_len;
+ ++a;
+ ++b;
+ continue;
+ }
+ if(!*a)
+ return -1;
+ if(!*b)
+ return 1;
+ if(*a == ' ')
+ return (*a < *b) ? -1 : 1;
+ if(*b == ' ')
+ ++b;
+ else
+ return (*a < *b) ? -1 : 1;
+ }
+ return 0;
+}
+int Nicks_strncasecmp(char *a, char *b, unsigned int a_len)
+{
+ char space_char;
+ if(!(con_nickcompletion_flags.integer & NICKS_ALPHANUMERICS_ONLY))
+ {
+ if(con_nickcompletion_flags.integer & NICKS_NO_SPACES)
+ return Nicks_strncasecmp_nospaces(a, b, a_len);
+ return strncasecmp(a, b, a_len);
+ }
+
+ space_char = (con_nickcompletion_flags.integer & NICKS_NO_SPACES) ? 'a' : ' ';
+
+ // ignore non alphanumerics of B
+ // if A contains a non-alphanumeric, B must contain it as well though!
+ while(a_len)
+ {
+ qboolean alnum_a, alnum_b;
+
+ if(tolower(*a) == tolower(*b))
+ {
+ if(*a == 0) // end of both strings, they're equal
+ return 0;
+ --a_len;
+ ++a;
+ ++b;
+ continue;
+ }
+ // not equal, end of one string?
+ if(!*a)
+ return -1;
+ if(!*b)
+ return 1;
+ // ignore non alphanumerics
+ alnum_a = ( (*a >= 'a' && *a <= 'z') || (*a >= 'A' && *a <= 'Z') || (*a >= '0' && *a <= '9') || *a == space_char);
+ alnum_b = ( (*b >= 'a' && *b <= 'z') || (*b >= 'A' && *b <= 'Z') || (*b >= '0' && *b <= '9') || *b == space_char);
+ if(!alnum_a) // b must contain this
+ return (*a < *b) ? -1 : 1;
+ if(!alnum_b)
+ ++b;
+ // otherwise, both are alnum, they're just not equal, return the appropriate number
+ else
+ return (*a < *b) ? -1 : 1;
+ }
+ return 0;
+}
+
+
+/* Nicks_CompleteCountPossible
+
+ Count the number of possible nicks to complete
+ */
+int Nicks_CompleteCountPossible(char *line, int pos, char *s, qboolean isCon)
+{
+ char name[128];
+ int i, p;
+ int match;
+ int spos;
+ int count = 0;
+
+ if(!con_nickcompletion.integer)
+ return 0;
+
+ // changed that to 1
+ if(!line[0])// || !line[1]) // we want at least... 2 written characters
+ return 0;
+
+ for(i = 0; i < cl.maxclients; ++i)
+ {
+ p = i;
+ if(!cl.scores[p].name[0])
+ continue;
+
+ SanitizeString(cl.scores[p].name, name);
+ //Con_Printf(" ^2Sanitized: ^7%s -> %s", cl.scores[p].name, name);
+
+ if(!name[0])
+ continue;
+
+ match = -1;
+ spos = pos - 1; // no need for a minimum of characters :)
+
+ while(spos >= 0)
+ {
+ if(spos > 0 && line[spos-1] != ' ' && line[spos-1] != ';' && line[spos-1] != '\"' && line[spos-1] != '\'')
+ {
+ if(!(isCon && line[spos-1] == ']' && spos == 1) && // console start
+ !(spos > 1 && line[spos-1] >= '0' && line[spos-1] <= '9' && line[spos-2] == STRING_COLOR_TAG)) // color start
+ {
+ --spos;
+ continue;
+ }
+ }
+ if(isCon && spos == 0)
+ break;
+ if(Nicks_strncasecmp(line+spos, name, pos-spos) == 0)
+ match = spos;
+ --spos;
+ }
+ if(match < 0)
+ continue;
+ //Con_Printf("Possible match: %s|%s\n", cl.scores[p].name, name);
+ strlcpy(Nicks_list[count], cl.scores[p].name, sizeof(Nicks_list[count]));
+
+ // the sanitized list
+ strlcpy(Nicks_sanlist[count], name, sizeof(Nicks_sanlist[count]));
+ if(!count)
+ {
+ Nicks_matchpos = match;
+ }
+
+ Nicks_offset[count] = s - (&line[match]);
+ //Con_Printf("offset for %s: %i\n", name, Nicks_offset[count]);
+
+ ++count;
+ }
+ return count;
+}
+
+void Cmd_CompleteNicksPrint(int count)
+{
+ int i;
+ for(i = 0; i < count; ++i)
+ Con_Printf("%s\n", Nicks_list[i]);
+}
+
+void Nicks_CutMatchesNormal(int count)
+{
+ // cut match 0 down to the longest possible completion
+ int i;
+ unsigned int c, l;
+ c = strlen(Nicks_sanlist[0]) - 1;
+ for(i = 1; i < count; ++i)
+ {
+ l = strlen(Nicks_sanlist[i]) - 1;
+ if(l < c)
+ c = l;
+
+ for(l = 0; l <= c; ++l)
+ if(tolower(Nicks_sanlist[0][l]) != tolower(Nicks_sanlist[i][l]))
+ {
+ c = l-1;
+ break;
+ }
+ }
+ Nicks_sanlist[0][c+1] = 0;
+ //Con_Printf("List0: %s\n", Nicks_sanlist[0]);
+}
+
+unsigned int Nicks_strcleanlen(const char *s)
+{
+ unsigned int l = 0;
+ while(*s)
+ {
+ if( (*s >= 'a' && *s <= 'z') ||
+ (*s >= 'A' && *s <= 'Z') ||
+ (*s >= '0' && *s <= '9') ||
+ *s == ' ')
+ ++l;
+ ++s;
+ }
+ return l;
+}
+
+void Nicks_CutMatchesAlphaNumeric(int count)
+{
+ // cut match 0 down to the longest possible completion
+ int i;
+ unsigned int c, l;
+ char tempstr[sizeof(Nicks_sanlist[0])];
+ char *a, *b;
+ char space_char = (con_nickcompletion_flags.integer & NICKS_NO_SPACES) ? 'a' : ' '; // yes this is correct, we want NO spaces when no spaces
+
+ c = strlen(Nicks_sanlist[0]);
+ for(i = 0, l = 0; i < (int)c; ++i)
+ {
+ if( (Nicks_sanlist[0][i] >= 'a' && Nicks_sanlist[0][i] <= 'z') ||
+ (Nicks_sanlist[0][i] >= 'A' && Nicks_sanlist[0][i] <= 'Z') ||
+ (Nicks_sanlist[0][i] >= '0' && Nicks_sanlist[0][i] <= '9') || Nicks_sanlist[0][i] == space_char) // this is what's COPIED
+ {
+ tempstr[l++] = Nicks_sanlist[0][i];
+ }
+ }
+ tempstr[l] = 0;
+
+ for(i = 1; i < count; ++i)
+ {
+ a = tempstr;
+ b = Nicks_sanlist[i];
+ while(1)
+ {
+ if(!*a)
+ break;
+ if(!*b)
+ {
+ *a = 0;
+ break;
+ }
+ if(tolower(*a) == tolower(*b))
+ {
+ ++a;
+ ++b;
+ continue;
+ }
+ if( (*b >= 'a' && *b <= 'z') || (*b >= 'A' && *b <= 'Z') || (*b >= '0' && *b <= '9') || *b == space_char)
+ {
+ // b is alnum, so cut
+ *a = 0;
+ break;
+ }
+ ++b;
+ }
+ }
+ // Just so you know, if cutmatchesnormal doesn't kill the first entry, then even the non-alnums fit
+ Nicks_CutMatchesNormal(count);
+ //if(!Nicks_sanlist[0][0])
+ if(Nicks_strcleanlen(Nicks_sanlist[0]) < strlen(tempstr))
+ {
+ // if the clean sanitized one is longer than the current one, use it, it has crap chars which definitely are in there
+ strlcpy(Nicks_sanlist[0], tempstr, sizeof(tempstr));
+ }
+}
+
+void Nicks_CutMatchesNoSpaces(int count)
+{
+ // cut match 0 down to the longest possible completion
+ int i;
+ unsigned int c, l;
+ char tempstr[sizeof(Nicks_sanlist[0])];
+ char *a, *b;
+
+ c = strlen(Nicks_sanlist[0]);
+ for(i = 0, l = 0; i < (int)c; ++i)
+ {
+ if(Nicks_sanlist[0][i] != ' ') // here it's what's NOT copied
+ {
+ tempstr[l++] = Nicks_sanlist[0][i];
+ }
+ }
+ tempstr[l] = 0;
+
+ for(i = 1; i < count; ++i)
+ {
+ a = tempstr;
+ b = Nicks_sanlist[i];
+ while(1)
+ {
+ if(!*a)
+ break;
+ if(!*b)
+ {
+ *a = 0;
+ break;
+ }
+ if(tolower(*a) == tolower(*b))
+ {
+ ++a;
+ ++b;
+ continue;
+ }
+ if(*b != ' ')
+ {
+ *a = 0;
+ break;
+ }
+ ++b;
+ }
+ }
+ // Just so you know, if cutmatchesnormal doesn't kill the first entry, then even the non-alnums fit
+ Nicks_CutMatchesNormal(count);
+ //if(!Nicks_sanlist[0][0])
+ //Con_Printf("TS: %s\n", tempstr);
+ if(Nicks_strcleanlen(Nicks_sanlist[0]) < strlen(tempstr))
+ {
+ // if the clean sanitized one is longer than the current one, use it, it has crap chars which definitely are in there
+ strlcpy(Nicks_sanlist[0], tempstr, sizeof(tempstr));
+ }
+}
+
+void Nicks_CutMatches(int count)
+{
+ if(con_nickcompletion_flags.integer & NICKS_ALPHANUMERICS_ONLY)
+ Nicks_CutMatchesAlphaNumeric(count);
+ else if(con_nickcompletion_flags.integer & NICKS_NO_SPACES)
+ Nicks_CutMatchesNoSpaces(count);
+ else
+ Nicks_CutMatchesNormal(count);
+}
+
+const char **Nicks_CompleteBuildList(int count)
+{
+ const char **buf;
+ int bpos = 0;
+ // the list is freed by Con_CompleteCommandLine, so create a char**
+ buf = (const char **)Mem_Alloc(tempmempool, count * sizeof(const char *) + sizeof (const char *));
+
+ for(; bpos < count; ++bpos)
+ buf[bpos] = Nicks_sanlist[bpos] + Nicks_offset[bpos];
+
+ Nicks_CutMatches(count);
+
+ buf[bpos] = NULL;
+ return buf;
+}
+
+/*
+ Nicks_AddLastColor
+ Restores the previous used color, after the autocompleted name.
+*/
+int Nicks_AddLastColor(char *buffer, int pos)
+{
+ qboolean quote_added = false;
+ int match;
+ int color = STRING_COLOR_DEFAULT + '0';
+ char r = 0, g = 0, b = 0;
+
+ if(con_nickcompletion_flags.integer & NICKS_ADD_QUOTE && buffer[Nicks_matchpos-1] == '\"')
+ {
+ // we'll have to add a quote :)
+ buffer[pos++] = '\"';
+ quote_added = true;
+ }
+
+ if((!quote_added && con_nickcompletion_flags.integer & NICKS_ADD_COLOR) || con_nickcompletion_flags.integer & NICKS_FORCE_COLOR)
+ {
+ // add color when no quote was added, or when flags &4?
+ // find last color
+ for(match = Nicks_matchpos-1; match >= 0; --match)
+ {
+ if(buffer[match] == STRING_COLOR_TAG)
+ {
+ if( isdigit(buffer[match+1]) )
+ {
+ color = buffer[match+1];
+ break;
+ }
+ else if(buffer[match+1] == STRING_COLOR_RGB_TAG_CHAR)
+ {
+ if ( isxdigit(buffer[match+2]) && isxdigit(buffer[match+3]) && isxdigit(buffer[match+4]) )
+ {
+ r = buffer[match+2];
+ g = buffer[match+3];
+ b = buffer[match+4];
+ color = -1;
+ break;
+ }
+ }
+ }
+ }
+ if(!quote_added)
+ {
+ if( pos >= 2 && buffer[pos-2] == STRING_COLOR_TAG && isdigit(buffer[pos-1]) ) // when thes use &4
+ pos -= 2;
+ else if( pos >= 5 && buffer[pos-5] == STRING_COLOR_TAG && buffer[pos-4] == STRING_COLOR_RGB_TAG_CHAR
+ && isxdigit(buffer[pos-3]) && isxdigit(buffer[pos-2]) && isxdigit(buffer[pos-1]) )
+ pos -= 5;
+ }
+ buffer[pos++] = STRING_COLOR_TAG;
+ if (color == -1)
+ {
+ buffer[pos++] = STRING_COLOR_RGB_TAG_CHAR;
+ buffer[pos++] = r;
+ buffer[pos++] = g;
+ buffer[pos++] = b;
+ }
+ else
+ buffer[pos++] = color;
+ }
+ return pos;
+}
+
+int Nicks_CompleteChatLine(char *buffer, size_t size, unsigned int pos)
+{
+ int n;
+ /*if(!con_nickcompletion.integer)
+ return; is tested in Nicks_CompletionCountPossible */
+ n = Nicks_CompleteCountPossible(buffer, pos, &buffer[pos], false);
+ if(n == 1)
+ {
+ size_t len;
+ char *msg;
+
+ msg = Nicks_list[0];
+ len = min(size - Nicks_matchpos - 3, strlen(msg));
+ memcpy(&buffer[Nicks_matchpos], msg, len);
+ if( len < (size - 7) ) // space for color (^[0-9] or ^xrgb) and space and \0
+ len = Nicks_AddLastColor(buffer, Nicks_matchpos+len);
+ buffer[len++] = ' ';
+ buffer[len] = 0;
+ return len;
+ } else if(n > 1)
+ {
+ int len;
+ char *msg;
+ Con_Printf("\n%i possible nicks:\n", n);
+ Cmd_CompleteNicksPrint(n);
+
+ Nicks_CutMatches(n);
+
+ msg = Nicks_sanlist[0];
+ len = min(size - Nicks_matchpos, strlen(msg));
+ memcpy(&buffer[Nicks_matchpos], msg, len);
+ buffer[Nicks_matchpos + len] = 0;
+ //pos += len;
+ return Nicks_matchpos + len;
+ }
+ return pos;
+}
+
+