public IRCClient(TcpClient client) { tcpClient = client; messageQueue = new Queue <IMessage>(); lastCmd = DateTime.Now; greeted = false; //lastPong = DateTime.Now + TimeSpan.FromSeconds(85); nick = ""; user = ""; real = ""; modes = ""; away = ""; try { host = Dns.GetHostEntry(((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address).HostName; SendMessage(IRCMessage.GetStatic().CreateMessage(this, BaseIRCLib.Server.GetServer().HostString, "NOTICE", new string[] { "Your hostname is " + host })); } catch (SocketException ex) { Console.WriteLine("SocketException when finding hostname: {0}", ex.Message); host = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString(); SendMessage(IRCMessage.GetStatic().CreateMessage(this, BaseIRCLib.Server.GetServer().HostString, "NOTICE", new string[] { "Couldn't find your hostname" })); } t = new Thread(new ThreadStart(UpdateMessages)); t.Start(); }
public static void MeetAndGreet(IClient client) { if (!client.Greeted) { client.Greeted = true; Console.WriteLine("{0} Has finished connecting as {1}", client.HostName, client.NickName); client.AddMessage(IRCMessage.GetStatic().CreateMessage(client, "MOTD")); string joins = "&Friends"; int numChats = Server.clientFriends.GetChatRoomCount(); for (int i = 0; i < numChats; i++) { Steam4NET.CSteamID id = Server.clientFriends.GetChatRoomByIndex(i); IChannel c = (BaseIRCLib.Server.GetServer() as Server).GetSteamChannel(id); if (c == null) { continue; } joins += ",&" + c.ChannelName; } client.AddMessage(IRCMessage.GetStatic().CreateMessage(client, client.UserString, "JOIN", new string[] { joins })); client.LastPong = DateTime.Now; } }
public static void JoinClientToChannel(IMessage message) { //#Chan,#Chan2 ChanKey,ChanKey2 string[] channels = null; string[] chanKeys = null; if (message.Params.Count() > 1) { if (message.Params[1].Contains(',')) { chanKeys = message.Params[1].Split(','); } else { chanKeys = new string[] { message.Params[1] } }; } if (message.Params[0].Contains(',')) { channels = message.Params[0].Split(','); } else { channels = new string[] { message.Params[0] } }; foreach (string s in channels) { if (Database.GetDatabase().HasChannel(s.Remove(0, 1))) { IChannel j = Database.GetDatabase().GetChannel(s.Remove(0, 1)); if (chanKeys != null && chanKeys.Count() > Array.IndexOf <string>(channels, s)) { if (j.Modes.ContainsKey('k') && j.Modes['k'] == chanKeys[Array.IndexOf <string>(channels, s)]) { j.AddClient(message.Owner); } else { message.Owner.SendMessage(IRCMessage.GetStatic().CreateMessage(message.Owner, "", Reply.ERR_BADCHANNELKEY, new string[] { s, "Cannot join channel (+k)" })); } } else { j.AddClient(message.Owner); } } else { message.Owner.SendMessage(IRCMessage.GetStatic().CreateMessage(message.Owner, "", Reply.ERR_NOSUCHCHANNEL, new string[] { s, "No such channel" })); } } }
public static void ModifyClientModes(IClient client, string modes) { if (modes == null) { } else if (modes.StartsWith("+")) { client.Modes = ModifyString(client.Modes, "isw", modes.Remove(0, 1).ToCharArray()); } else if (modes.StartsWith("-")) { client.Modes = ModifyString(client.Modes, "iswo", null, modes.Remove(0, 1).ToCharArray()); } client.SendMessage(IRCMessage.GetStatic().CreateMessage(client, client.NickName, Reply.RPL_UMODEIS, new string[] { client.NickName, "+" + client.Modes })); }
public static void ModifyChannelModes(IClient client, Channel channel, string modes, string more = null) { if (modes.Length == 2) { string mode = modes.Substring(0, 1); string mode2 = modes.Substring(1, 1); if ("psitnm".Contains(mode2)) { foreach (char m in more) { if (mode == "+") { if (!channel.Modes.ContainsKey(m)) { channel.Modes.Add(m, ""); } } else if (mode == "-") { if (channel.Modes.ContainsKey(m)) { channel.Modes.Remove(m); } } } } else if ("ov".Contains(mode2)) { } else if (mode2 == "l") { } else if (mode2 == "b") { } else if (mode2 == "k") { } } client.SendMessage(IRCMessage.GetStatic().CreateMessage(client, "", Reply.RPL_CHANNELMODEIS, new string[] { client.NickName, "&" + channel.ChannelName, "+" + channel.GetModes() })); }
private void UpdateMessages() { while (!disposed) { if (tcpClient.Client == null) { Dispose(); return; } if (!tcpClient.Connected) { Dispose(); } if (!tcpClient.Client.Connected) { Dispose(); } if (disposed) { return; } if (tcpClient.Available > 0) { byte[] recv = new byte[tcpClient.Available]; tcpClient.Client.Receive(recv); string data = Encoding.UTF8.GetString(recv); data = data.Replace("\r", "\n"); data = data.Replace("\n\n", "\n"); data = unfinished + data; if (!data.EndsWith("\n")) { if (data.Contains("\n")) { unfinished = data.Substring(data.LastIndexOf("\n") + 1); data = data.Remove(data.LastIndexOf("\n") + 1); } else { unfinished = data; continue; } } string[] msgs = data.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (string msg in msgs) { #if !DEBUG try { #endif IMessage m = IRCMessage.GetStatic().CreateMessage(this, msg); if (string.IsNullOrEmpty(m.Prefix)) { m.Prefix = this.UserString; } messageQueue.Enqueue(m); #if !DEBUG } catch (Exception ex) { System.IO.File.WriteAllText("error.log", string.Format("[{0}] ERROR {1} occured!\n[{0}] Trace: {2}\n", DateTime.Now, ex.Message, ex.StackTrace)); } #endif } continue; } Thread.Sleep(50); } }