/// <summary> /// Packets handled in this function are 'internal' and cannot be overriden. /// </summary> internal bool HandlePacket(PacketEventArgs e) { if (IsCaptcha) { switch ((AresId)e.Packet.Id) { case AresId.MSG_CHAT_CLIENT_FASTPING: FastPing = true; return(true); case AresId.MSG_CHAT_CLIENT_DUMMY: return(true); case AresId.MSG_CHAT_CLIENT_AUTOLOGIN: AutoLogin login = (AutoLogin)e.Packet; AresCommands.HandleAutoLogin(server, this, login.Sha1Password); return(true); case AresId.MSG_CHAT_CLIENT_PUBLIC: ClientPublic pub = (ClientPublic)e.Packet; FinishCaptcha(pub.Message); return(true); case AresId.MSG_CHAT_CLIENT_EMOTE: ClientEmote emote = (ClientEmote)e.Packet; FinishCaptcha(emote.Message); return(true); case AresId.MSG_CHAT_CLIENT_ADDSHARE: return(true); case AresId.MSG_CHAT_CLIENT_UPDATE_STATUS: ClientUpdate update = (ClientUpdate)e.Packet; LastUpdate = DateTime.Now; NodeIp = update.NodeIp; NodePort = update.NodePort; return(true); default: break; } return(false); } else if (LoggedIn) { switch ((AresId)e.Packet.Id) { case AresId.MSG_CHAT_CLIENT_FASTPING: FastPing = true; return(true); case AresId.MSG_CHAT_CLIENT_DUMMY: return(true); case AresId.MSG_CHAT_CLIENT_PUBLIC: ClientPublic pub = (ClientPublic)e.Packet; if (AresCommands.HandlePreCommand(server, this, pub.Message)) { return(true); } if (Muzzled) { server.SendAnnounce(this, Strings.AreMuzzled); return(true); } break; case AresId.MSG_CHAT_CLIENT_EMOTE: ClientEmote emote = (ClientEmote)e.Packet; if (AresCommands.HandlePreCommand(server, this, emote.Message)) { return(true); } if (Muzzled) { server.SendAnnounce(this, Strings.AreMuzzled); return(true); } break; case AresId.MSG_CHAT_CLIENT_COMMAND: Command cmd = (Command)e.Packet; if (AresCommands.HandleCommand(server, this, cmd.Message)) { return(true); } break; case AresId.MSG_CHAT_CLIENT_PVT: Private pvt = (Private)e.Packet; if (Muzzled && !server.Config.MuzzledPMs) { pvt.Message = "[" + Strings.AreMuzzled + "]"; SendPacket(pvt); return(true); } break; case AresId.MSG_CHAT_CLIENT_AUTHREGISTER: { AuthRegister reg = (AuthRegister)e.Packet; AresCommands.HandleRegister(server, this, reg.Password); return(true); } case AresId.MSG_CHAT_CLIENT_AUTHLOGIN: { AuthLogin login = (AuthLogin)e.Packet; AresCommands.HandleLogin(server, this, login.Password); return(true); } case AresId.MSG_CHAT_CLIENT_AUTOLOGIN: { AutoLogin login = (AutoLogin)e.Packet; AresCommands.HandleAutoLogin(server, this, login.Sha1Password); return(true); } case AresId.MSG_CHAT_CLIENT_ADDSHARE: return(true); case AresId.MSG_CHAT_CLIENT_IGNORELIST: Ignored ignore = (Ignored)e.Packet; if (ignore.Ignore) { lock (Ignored) { if (!Ignored.Contains(ignore.Username)) { Ignored.Add(ignore.Username); server.SendAnnounce(this, String.Format(Strings.Ignored, ignore.Username)); } } } else { lock (Ignored) { if (Ignored.Contains(ignore.Username)) { Ignored.Remove(ignore.Username); server.SendAnnounce(this, String.Format(Strings.Unignored, ignore.Username)); } } } return(true); case AresId.MSG_CHAT_CLIENT_UPDATE_STATUS: ClientUpdate update = (ClientUpdate)e.Packet; LastUpdate = DateTime.Now; NodeIp = update.NodeIp; NodePort = update.NodePort; server.SendPacket((s) => s.Vroom == Vroom && s.CanSee(this), new ServerUpdate(this)); return(true); case AresId.MSG_CHAT_CLIENT_DIRCHATPUSH: ClientDirectPush push = (ClientDirectPush)e.Packet; if (Encoding.UTF8.GetByteCount(push.Username) < 2) { SendPacket(new DirectPushError(4)); return(true); } if (push.TextSync.Length < 16) { SendPacket(new DirectPushError(3)); return(true); } IClient target = server.FindUser(s => s.Name == push.Username); if (target == null) { SendPacket(new DirectPushError(1)); return(true); } if (target.Ignored.Contains(Name)) { SendPacket(new DirectPushError(2)); return(true); } SendPacket(new DirectPushError(0)); server.SendPacket(target, new ServerDirectPush(this, push)); return(true); case AresId.MSG_CHAT_CLIENT_BROWSE: SendPacket(new BrowseError(((Browse)e.Packet).BrowseId)); return(true); case AresId.MSG_CHAT_CLIENT_SEARCH: SendPacket(new SearchEnd(((Search)e.Packet).SearchId)); return(true); case AresId.MSG_CHAT_CLIENTCOMPRESSED: { Compressed packet = (Compressed)e.Packet; byte[] payload = Zlib.Decompress(packet.Data); var reader = new PacketReader(payload) { Position = 0L }; while (reader.Remaining >= 3) { ushort count = reader.ReadUInt16(); byte id = reader.ReadByte(); IPacket msg = Socket.Formatter.Unformat(id, reader.ReadBytes(count)); OnPacketReceived(Socket, new PacketEventArgs(msg, WebSocketMessageType.Binary, 0)); } break; } default: break; } return(false);//wasn't handled } else { //not captcha, not logged, error? Logging.Info("AresClient", "Client {0} sent {1} before logging in.", this.ExternalIp, e.Packet.Id); return(true); } }
/// <summary> /// Packets handled in this function are 'internal' cannot be overriden. /// </summary> internal bool HandlePacket(PacketEventArgs e) { if (IsCaptcha) { switch ((AresId)e.Packet.Id) { case AresId.MSG_CHAT_CLIENT_FASTPING: fastping = true; break; case AresId.MSG_CHAT_CLIENT_AUTOLOGIN: AutoLogin login = (AutoLogin)e.Packet; Commands.HandleAutoLogin(server, this, login.Sha1Password); break; case AresId.MSG_CHAT_CLIENT_PUBLIC: ClientPublic pub = (ClientPublic)e.Packet; FinishCaptcha(pub.Message); break; case AresId.MSG_CHAT_CLIENT_EMOTE: ClientEmote emote = (ClientEmote)e.Packet; FinishCaptcha(emote.Message); break; case AresId.MSG_CHAT_CLIENT_ADDSHARE: SharedFile addfile = (SharedFile)e.Packet; if (server.PluginManager.OnFileReceived(this, addfile)) { lock (files) files.Add(addfile); } break; case AresId.MSG_CHAT_CLIENT_UPDATE_STATUS: ClientUpdate update = (ClientUpdate)e.Packet; lastupdate = TimeBank.CurrentTime; NodeIp = update.NodeIp; NodePort = update.NodePort; Age = (update.Age != 0) ? update.Age : Age; Gender = (update.Gender != 0) ? update.Gender : Gender; Country = (update.Country != 0) ? update.Country : Country; Region = !String.IsNullOrEmpty(update.Region) ? update.Region : Region; break; } return(true); //don't handle any other packets yet } else if (LoggedIn) { switch ((AresId)e.Packet.Id) { case AresId.MSG_CHAT_CLIENT_FASTPING: fastping = true; return(true); case AresId.MSG_CHAT_CLIENT_DUMMY: return(true); case AresId.MSG_CHAT_CLIENT_PUBLIC: ClientPublic pub = (ClientPublic)e.Packet; if (!String.IsNullOrEmpty(pub.Message)) { if (pub.Message.StartsWith("#") && Commands.HandlePreCommand(server, this, pub.Message.Substring(1))) { return(true); } } if (Muzzled) { server.SendAnnounce(this, Strings.AreMuzzled); return(true); } return(false); case AresId.MSG_CHAT_CLIENT_EMOTE: ClientEmote emote = (ClientEmote)e.Packet; if (!String.IsNullOrEmpty(emote.Message)) { if (emote.Message.StartsWith("#") && Commands.HandlePreCommand(server, this, emote.Message.Substring(1))) { return(true); } } if (Muzzled) { server.SendAnnounce(this, Strings.AreMuzzled); return(true); } return(false); case AresId.MSG_CHAT_CLIENT_COMMAND: Command cmd = (Command)e.Packet; Commands.HandlePreCommand(server, this, cmd.Message); break; case AresId.MSG_CHAT_CLIENT_PVT: Private pvt = (Private)e.Packet; if (Muzzled && !server.Config.MuzzledPMs) { pvt.Message = "[" + Strings.AreMuzzled + "]"; server.SendPacket(this, pvt); return(true); } return(false); case AresId.MSG_CHAT_CLIENT_AUTHREGISTER: { AuthRegister reg = (AuthRegister)e.Packet; Commands.HandleRegister(server, this, reg.Password); } return(true); case AresId.MSG_CHAT_CLIENT_AUTHLOGIN: { AuthLogin login = (AuthLogin)e.Packet; Commands.HandleLogin(server, this, login.Password); } return(true); case AresId.MSG_CHAT_CLIENT_AUTOLOGIN: { AutoLogin login = (AutoLogin)e.Packet; Commands.HandleAutoLogin(server, this, login.Sha1Password); } return(true); case AresId.MSG_CHAT_CLIENT_ADDSHARE: SharedFile addfile = (SharedFile)e.Packet; if (server.PluginManager.OnFileReceived(this, addfile)) { lock (files) files.Add(addfile); } return(true); case AresId.MSG_CHAT_CLIENT_IGNORELIST: Ignored ignore = (Ignored)e.Packet; if (ignore.Ignore) { lock (ignored) { if (!ignored.Contains(ignore.Username)) { ignored.Add(ignore.Username); server.SendAnnounce(this, String.Format(Strings.Ignored, ignore.Username)); } } } else { lock (ignored) { if (ignored.Contains(ignore.Username)) { ignored.Remove(ignore.Username); server.SendAnnounce(this, String.Format(Strings.Unignored, ignore.Username)); } } } return(true); case AresId.MSG_CHAT_CLIENT_UPDATE_STATUS: ClientUpdate update = (ClientUpdate)e.Packet; lastupdate = TimeBank.CurrentTime; NodeIp = update.NodeIp; NodePort = update.NodePort; Age = (update.Age != 0) ? update.Age : Age; Gender = (update.Gender != 0) ? update.Gender : Gender; Country = (update.Country != 0) ? update.Country : Country; Region = !String.IsNullOrEmpty(update.Region) ? update.Region : Region; server.SendPacket((s) => s.Vroom == Vroom, new ServerUpdate(this)); return(true); case AresId.MSG_CHAT_CLIENT_DIRCHATPUSH: ClientDirectPush push = (ClientDirectPush)e.Packet; if (Encoding.UTF8.GetByteCount(push.Username) < 2) { server.SendPacket(this, new DirectPushError(4)); return(true); } if (push.TextSync.Length < 16) { server.SendPacket(this, new DirectPushError(3)); return(true); } IClient target = server.FindUser(s => s.Name == push.Username); if (target == null) { server.SendPacket(this, new DirectPushError(1)); return(true); } if (target.Ignored.Contains(Name)) { server.SendPacket(this, new DirectPushError(2)); return(true); } server.SendPacket(this, new DirectPushError(0)); server.SendPacket(target, new ServerDirectPush(this, push)); return(true); case AresId.MSG_CHAT_CLIENT_BROWSE: Browse browse = (Browse)e.Packet; browse.Type = (byte)((browse.Type == 0) ? (byte)255 : browse.Type); browse.Type = (byte)((browse.Type == 8) ? (byte)0 : browse.Type); IClient browse_target = server.FindUser(s => s.Vroom == Vroom && s.Name == browse.Username); if (browse_target == null) { return(true); } else if (browse_target.Files.Count == 0) { server.SendPacket(this, new BrowseError(browse.BrowseId)); } else { server.SendPacket(this, new BrowseStart(browse.BrowseId, (ushort)browse_target.Files.Count)); foreach (var file in browse_target.Files) { if (browse.Type == 255 || browse.Type == file.Type) { server.SendPacket(this, new BrowseItem(browse.BrowseId, file)); } } server.SendPacket(this, new BrowseEnd(browse.BrowseId)); } return(true); case AresId.MSG_CHAT_CLIENT_SEARCH: Search search = (Search)e.Packet; search.Type = (byte)((search.Type == 0) ? (byte)255 : search.Type); search.Type = (byte)((search.Type == 8) ? (byte)0 : search.Type); foreach (var user in server.Users) { if (user != this && user.Vroom == Vroom) { foreach (var file in user.Files) { if (file.SearchWords.ContainsAny(search.SearchWords.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries))) { server.SendPacket(this, new SearchHit(search.SearchId, user, file)); } } } } server.SendPacket(this, new SearchEnd(search.SearchId)); return(true); } return(false);//wasn't handled } else { return(true);//not captcha, not logged, error } }