public void Read() { while (true) { IrcMessage message; try { message = Stream.ReadMessage(); } catch (Exception) { IrcServer.UserLeft(this, "connection reset by peer"); Socket.Close(); return; } Idle = DateTime.Now; if (message.Command == null) { continue; } string line = message.ToString(); IrcCommand command = IrcCommand.Find(message.Command); if (command == null) { Console.WriteLine(line); continue; } if (command.RequireRegistered && !Mode.IsRegistered) { Write(new IrcNumericResponce { NumericId = IrcNumericResponceId.ERR_NOTREGISTERED, Message = "You're not registered.", Extra = command.Name.ToUpper() }); } command.Run(this, message); } }
/// <summary> /// Joins this IrcChannel, provided the key is correct. /// </summary> public void Join(IIrcUser client, string key = "") { if (Mode.Key != key) { return; } if (!client.ChannelModes.ContainsKey(this)) { client.ChannelModes.Add(this, new IrcChannelUserMode()); } IrcChannelUserMode mode = client.ChannelModes[this]; if (Mode.IsInviteOnly && !mode.IsInvited) { if (Mode.InviteMask.Where(mask => mask.PatternMatch(client.UserString)).Count() == 0) { client.Write(new IrcNumericResponce { NumericId = IrcNumericResponceId.ERR_INVITEONLYCHAN, To = Name, Message = "You are not invited." }); return; } } if (Mode.BanMask.Where(mask => mask.PatternMatch(client.UserString)).Count() >= 1) { if (Mode.ExceptionMask.Where(mask => mask.PatternMatch(client.UserString)).Count() == 0) { // TODO: deny access. return; } } if (Users.Count == 0 && (Created - DateTime.Now).TotalSeconds < 5) { // Flag the first member as the channel's creator. mode.IsCreator = true; } Users.Add(client); Command(client, "JOIN", Name); if (string.IsNullOrEmpty(Mode.Topic)) { client.Write(new IrcNumericResponce { NumericId = IrcNumericResponceId.RPL_NOTOPIC, To = Name, Message = "No topic" }); } else { client.Write(new IrcNumericResponce { NumericId = IrcNumericResponceId.RPL_TOPIC, To = Name, Message = Mode.Topic }); } IrcCommand.Find("names").Run(client, new IrcMessage { Params = new string[] { Name } }); }