public Client GetClient(string nick) { string n = nick; //first determine what sort of nick it is if (Client.IsFullNick(nick)) { //not the clean way :P Client c = new Client(); c.AssignNick(nick); n = c.Nick; } if (Clients.ContainsKey(n)) { return(Clients[n]); } else { Client c = new Client(); c.AssignNick(nick); Clients.Add(n, c); return(c); } }
public void HandleCommand(string source, string cmd, string paras) { //First, parse the source //We will have to determine if it is the server or a nick who did the action //If it was a user, we will have to get the nick Client clt = null; if (Client.IsFullNick(source)) { clt = GetClient(source); } //So now, handle the command //Check if it is a RAW int raw; if (int.TryParse(cmd, out raw)) { _OnRaw(raw, paras); return; } string[] split = paras.Split(' '); switch (cmd) { default: { if (Debug) { Console.WriteLine("Unknown command: " + cmd); } } break; case "JOIN": { if (OnJoin != null) { OnJoin(this, clt, GetChannel(paras)); } //<- :[n8][email protected] JOIN #orden-der-seraphim if (clt.Nick == Nick) { SendMode(split[0]); } if (!clt.Channels.ContainsKey(paras)) { clt.Channels.Add(paras, GetChannel(paras)); } if (!GetChannel(paras).Clients.ContainsKey(clt.Nick)) { GetChannel(paras).Clients.Add(clt.Nick, clt); } } break; case "PRIVMSG": { //:[email protected] PRIVMSG #Uthgard :.skill string chanS = split[0]; string txt = paras.Split(new char[] { ':' }, 2)[1]; object target; if (chanS != Nick) { target = GetChannel(chanS); } else { target = clt; } if (txt.StartsWith(C().ToString()) && txt.EndsWith(C().ToString())) { if (txt.Trim(C()).ToLower() == "version") { SendCTCP(clt, "VERSION " + B() + "C# IRC Framework by Metty" + B() + " (ver: 2.0; 23.07.2006)"); } if (OnCTCP != null) { OnCTCP(this, clt, target, txt.Trim(C())); } } else { if (OnMessage != null) { OnMessage(this, clt, target, txt); } } } break; case "NOTICE": { //:[email protected] NOTICE #Uthgard :.skill string chanS = split[0]; string txt = paras.Split(new char[] { ':' }, 2)[1]; object target; if (chanS != Nick) { target = GetChannel(chanS); } else { target = clt; } if (OnNotice != null) { OnNotice(this, clt, target, txt); } } break; case "PART": { if (OnPart != null) { OnPart(this, clt, GetChannel(paras)); } if (clt.Channels.ContainsKey(paras)) { clt.Channels.Remove(paras); } if (GetChannel(paras).Clients.ContainsKey(clt.Nick)) { GetChannel(paras).Clients.Remove(clt.Nick); } } break; case "NICK": { string old = clt.Nick; string nick = paras.Split(new char[] { ':' }, 2)[1]; Clients.Remove(clt.Nick); clt.Nick = nick; Clients.Add(nick, clt); foreach (Channel chan in Channels.Values) { if (chan.Clients.ContainsKey(old)) { chan.Clients.Remove(old); chan.Clients.Add(nick, clt); } } if (OnNick != null) { OnNick(this, clt, old, nick); } } break; case "QUIT": { string msg = paras.Split(new char[] { ':' }, 2)[1]; if (OnQuit != null) { OnQuit(this, clt, msg); } foreach (Channel chan in Channels.Values) { if (chan.Clients.ContainsKey(clt.Nick)) { chan.Clients.Remove(clt.Nick); } } Clients.Remove(clt.Nick); } break; case "MODE": { string[] ms = paras.Split(new char[] { ' ' }, 2); object loc; if (ms[0] == Nick) { loc = ThisClient; } else { loc = GetChannel(ms[0]); } string premode = ""; int pos = 1; string[] s = ms[1].Split(' '); if (OnMode != null) { foreach (char c in s[0]) { if (c == '+') { premode = "+"; } else if (c == '-') { premode = "-"; } else { string suf = ""; if (c == 'k') { suf = " " + s[pos]; pos++; } OnMode(this, clt, loc, (premode + c + suf)); } } } } break; } }