register("recolour", recolour, "Assigns the specified colour to the user, or a random colour if none is specified.  If no user is specified, all users are recoloured. e.g. ,recolour Arthur Wk");

userColours = loaddb("userColours", new Object());

function setColour(user, colour)
{
	userColours[user.toLowerCase()] = colour;
	storedb("userColours", userColours);
}

function recolour(unused, user, colour)
{
	if(user)
	{
		delete userColours[user.toLowerCase()];
		if(colour)
		{
			setColour(user.toLowerCase(), "\033" + colour);
		}
	}
	else
	{
		totalColours = colourCount;
		userColours = new Object();
		storedb("userColours", userColours);
		storedb("totalColours", totalColours);
	}
}

/**
 * Returns userString coloured with the colour assigned to this user.
 */
function colourUser(user, userString)
{
	if(!userColours[user.toLowerCase()])
	{
		if(totalColours > 0)
		{
			setColour(user.toLowerCase(), colour[--totalColours]);
			storedb("totalColours", totalColours);
		}
		else
		{
			//Fall back to random allocation if each colour has 
			//been used once.
			var c = Math.floor(Math.random() * colourCount);
			setColour(user.toLowerCase(), colour[c]);
		}
	}
	return userColours[user.toLowerCase()] + userString + "\033--";
}

//Define colours.  Lots of messy initialisation, so let's bung it at the end.
//Colours are allocated from the end, so alter the order to define which
//colours will be used.
totalColours = 0;
colour = new Array();
colour[totalColours++] = "\033Yy";
colour[totalColours++] = "\033Yw";
colour[totalColours++] = "\033Yr";
colour[totalColours++] = "\033yr";
colour[totalColours++] = "\033Ym";
colour[totalColours++] = "\033ym";
colour[totalColours++] = "\033Yk";
colour[totalColours++] = "\033yk";
colour[totalColours++] = "\033Yg";
colour[totalColours++] = "\033yg";
colour[totalColours++] = "\033Yc";
colour[totalColours++] = "\033yc";
colour[totalColours++] = "\033Yb";
colour[totalColours++] = "\033yb";
colour[totalColours++] = "\033Wy";
colour[totalColours++] = "\033wy";
colour[totalColours++] = "\033Ww";
colour[totalColours++] = "\033Wr";
colour[totalColours++] = "\033wr";
colour[totalColours++] = "\033Wm";
colour[totalColours++] = "\033wm";
colour[totalColours++] = "\033Wk";
colour[totalColours++] = "\033wk";
colour[totalColours++] = "\033Wg";
colour[totalColours++] = "\033wg";
colour[totalColours++] = "\033Wc";
colour[totalColours++] = "\033wc";
colour[totalColours++] = "\033Wb";
colour[totalColours++] = "\033wb";
colour[totalColours++] = "\033Ry";
colour[totalColours++] = "\033ry";
colour[totalColours++] = "\033Rw";
colour[totalColours++] = "\033rw";
colour[totalColours++] = "\033Rr";
colour[totalColours++] = "\033Rm";
colour[totalColours++] = "\033rm";
colour[totalColours++] = "\033Rk";
colour[totalColours++] = "\033rk";
colour[totalColours++] = "\033Rg";
colour[totalColours++] = "\033rg";
colour[totalColours++] = "\033Rc";
colour[totalColours++] = "\033rc";
colour[totalColours++] = "\033Rb";
colour[totalColours++] = "\033rb";
colour[totalColours++] = "\033My";
colour[totalColours++] = "\033my";
colour[totalColours++] = "\033Mw";
colour[totalColours++] = "\033mw";
colour[totalColours++] = "\033Mr";
colour[totalColours++] = "\033mr";
colour[totalColours++] = "\033Mm";
colour[totalColours++] = "\033Mk";
colour[totalColours++] = "\033mk";
colour[totalColours++] = "\033Mg";
colour[totalColours++] = "\033mg";
colour[totalColours++] = "\033Mc";
colour[totalColours++] = "\033mc";
colour[totalColours++] = "\033Mb";
colour[totalColours++] = "\033mb";
colour[totalColours++] = "\033Ky";
colour[totalColours++] = "\033ky";
colour[totalColours++] = "\033Kw";
colour[totalColours++] = "\033kw";
colour[totalColours++] = "\033Kr";
colour[totalColours++] = "\033kr";
colour[totalColours++] = "\033Km";
colour[totalColours++] = "\033km";
colour[totalColours++] = "\033Kk";
colour[totalColours++] = "\033Kg";
colour[totalColours++] = "\033kg";
colour[totalColours++] = "\033Kc";
colour[totalColours++] = "\033kc";
colour[totalColours++] = "\033Kb";
colour[totalColours++] = "\033kb";
colour[totalColours++] = "\033Gy";
colour[totalColours++] = "\033gy";
colour[totalColours++] = "\033Gw";
colour[totalColours++] = "\033gw";
colour[totalColours++] = "\033Gr";
colour[totalColours++] = "\033gr";
colour[totalColours++] = "\033Gm";
colour[totalColours++] = "\033gm";
colour[totalColours++] = "\033Gk";
colour[totalColours++] = "\033gk";
colour[totalColours++] = "\033Gb";
colour[totalColours++] = "\033gb";
colour[totalColours++] = "\033Cy";
colour[totalColours++] = "\033cy";
colour[totalColours++] = "\033Cw";
colour[totalColours++] = "\033cw";
colour[totalColours++] = "\033Cr";
colour[totalColours++] = "\033cr";
colour[totalColours++] = "\033Cm";
colour[totalColours++] = "\033cm";
colour[totalColours++] = "\033Ck";
colour[totalColours++] = "\033ck";
colour[totalColours++] = "\033Cb";
colour[totalColours++] = "\033cb";
colour[totalColours++] = "\033By";
colour[totalColours++] = "\033by";
colour[totalColours++] = "\033Bw";
colour[totalColours++] = "\033bw";
colour[totalColours++] = "\033Br";
colour[totalColours++] = "\033br";
colour[totalColours++] = "\033Bm";
colour[totalColours++] = "\033bm";
colour[totalColours++] = "\033Bk";
colour[totalColours++] = "\033bk";
colour[totalColours++] = "\033Bg";
colour[totalColours++] = "\033bg";
colour[totalColours++] = "\033Bc";
colour[totalColours++] = "\033bc";
colourCount = totalColours;
totalColours = loaddb("totalColours", colourCount);

