Beispiel #1
0
 public void CreateUserReply(object sender, CreateUserReplyEventArgs args)
 {
     if (lblResult.InvokeRequired)
     { // Needed for multi-threading cross calls.
         this.Invoke(new Action <object, CreateUserReplyEventArgs>(this.CreateUserReply), new object[] { sender, args });
     }
     else
     {
         if (_waitingForCreateUserReply)
         {
             _waitingForCreateUserReply = false;
             timer1.Stop();
             if (args.Success)
             {
                 lblResult.Text = "User created successful!";
                 //_newUserId = args.ID;
                 this.Close();
                 this.DialogResult = System.Windows.Forms.DialogResult.Yes;
                 return;
             }
             else
             {
                 lblResult.ForeColor   = Color.Red;
                 lblResult.Text        = args.Message;
                 btnRegister.Enabled   = true;
                 btnCancel.Enabled     = true;
                 tbxUsername.ReadOnly  = false;
                 tbxPassword1.ReadOnly = false;
                 tbxPassword2.ReadOnly = false;
             }
         }
     }
 }
Beispiel #2
0
        private static void OnCreateUserReply(CreateUserReplyEventArgs e)
        {
            EventHandler <CreateUserReplyEventArgs> handler = CreateUserReply;

            if (handler != null)
            {
                handler(null, e);
            }
        }
Beispiel #3
0
        public static void MessageReceivedHandler(object sender, PacketReceivedEventArgs args)
        {
            if (args.Data[0] == 0x92)
            {
                string sharedSecret;
                // Position of the Type byte is 30 (SignatureByteLength + MacByteLength + TimezByteLength + UserIdByteLength).
                ChatTwo_Protocol.MessageType type = (ChatTwo_Protocol.MessageType)args.Data[ChatTwo_Protocol.SignatureByteLength + ByteHelper.HashByteLength + 4 + 4];
                // Position of the UserID bytes is 26 (SignatureByteLength + MacByteLength + TimezByteLength) with a length of 4.
                int senderId = ByteHelper.ToInt32(args.Data, ChatTwo_Protocol.SignatureByteLength + ByteHelper.HashByteLength + 4);
                if (type == ChatTwo_Protocol.MessageType.CreateUserReply)
                {
                    sharedSecret = ChatTwo_Protocol.DefaultSharedSecret;
                }
                else if (senderId == 0)
                {
                    sharedSecret = ServerSharedSecret;
                }
                else
                {
                    sharedSecret = _contacts.Find(x => x.ID == senderId).Secret;

                    // Testing!!!! REMOVE THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    sharedSecret = ChatTwo_Protocol.DefaultSharedSecret;
                }

                if (ChatTwo_Protocol.ValidateMac(args.Data, sharedSecret))
                {
                    Message message = ChatTwo_Protocol.MessageReceivedHandler(args);

                    switch (message.Type)
                    {
                    case ChatTwo_Protocol.MessageType.CreateUserReply:
                    {
                        // Fire an OnCreateUserReply event.
                        CreateUserReplyEventArgs argsCreateUser = new CreateUserReplyEventArgs();
                        argsCreateUser.Success = message.Data[0] == 0x00;
                        switch (message.Data[0])
                        {
                        case 0:             // Success.
                            break;

                        case 1:             // Username already exist.
                            argsCreateUser.Message = "A user already exist with that name.";
                            break;

                        case 2:
                            argsCreateUser.Message = "Username is too short or too long.";
                            break;
                        }
                        OnCreateUserReply(argsCreateUser);
                        break;
                    }

                    case ChatTwo_Protocol.MessageType.LoginReply:
                    {
                        // Fire an OnLoginReply event.
                        LoginReplyEventArgs argsLogin = new LoginReplyEventArgs();
                        argsLogin.Success = message.Data[0] == 0x00;
                        switch (message.Data[0])
                        {
                        case 0:             // Success.
                            int    userId   = ByteHelper.ToInt32(message.Data, 1);
                            string username = Encoding.Unicode.GetString(ByteHelper.SubArray(message.Data, 5));
                            LogIn(userId);
                            _user.Name     = username;
                            argsLogin.Name = username;
                            break;

                        case 1:             // Wrong password.
                            argsLogin.Message = "Wrong username or password.";
                            break;

                        case 2:             // Already online.
                            argsLogin.Message = "That user is already online.";
                            break;
                        }
                        OnLoginReply(argsLogin);
                        break;
                    }

                    case ChatTwo_Protocol.MessageType.ContactRequestReply:
                    {
                        // Fire an OnAddContactReply event.
                        AddContactReplyEventArgs argsAddContact = new AddContactReplyEventArgs();
                        argsAddContact.Success = message.Data[0] == 0x00;
                        switch (message.Data[0])
                        {
                        case 0:             // Success.
                            break;

                        case 1:             // No user with that name.
                            argsAddContact.Message = "No user with that name.";
                            break;

                        case 2:             // You can't add your self.
                            argsAddContact.Message = "You can't add your self.";
                            break;

                        case 3:             // User is already a contact.
                            argsAddContact.Message = "User is already a contact.";
                            break;
                        }
                        OnAddContactReply(argsAddContact);
                        break;
                    }

                    case ChatTwo_Protocol.MessageType.ContactStatus:
                    {
                        int        contactId  = ByteHelper.ToInt32(message.Data, 0);
                        int        nameLength = ((31 & message.Data[4]) * 2);
                        ContactObj contact;
                        if (_contacts.Any(x => x.ID == contactId))
                        {
                            contact = _contacts.Find(x => x.ID == contactId);
                        }
                        else
                        {
                            contact      = new ContactObj();
                            contact.ID   = contactId;
                            contact.Name = Encoding.Unicode.GetString(message.Data, 5, nameLength);
                            _contacts.Add(contact);
                        }
                        contact.Online           = ByteHelper.CheckBitCodeIndex(message.Data[4], 7);
                        contact.RelationshipTo   = ByteHelper.CheckBitCodeIndex(message.Data[4], 6);
                        contact.RelationshipFrom = ByteHelper.CheckBitCodeIndex(message.Data[4], 5);
                        if (contact.Online)
                        {
                            int port = ByteHelper.ToInt32(message.Data, 5 + nameLength);
                            contact.Socket = new IPEndPoint(new IPAddress(ByteHelper.SubArray(message.Data, 5 + nameLength + 4)), port);
                        }
                        // Fire an OnContactUpdate event.
                        OnContactUpdate();
                        break;
                    }

                    case ChatTwo_Protocol.MessageType.Message:
                    {
                        ContactObj contact;
                        if (_contacts.Any(x => x.ID == message.From && x.RelationshipTo && x.RelationshipFrom))
                        {
                            contact = _contacts.Find(x => x.ID == message.From);
                            OpenChat(contact.ID);
                            message.Text = Encoding.Unicode.GetString(message.Data);
                            contact.ChatWindow.ReceiveMessage(message.Text);
                        }
                        else
#if DEBUG
                        { throw new NotImplementedException("You received a message from someone that isn't your contact?"); }
#else
                        { return; }
#endif
                        break;
                    }
                    }
                }
#if DEBUG
                else
                {
                    throw new NotImplementedException("Could not validate the MAC of received message.");
                }
                // Need to add a simple debug message here, but this works as a great breakpoint until then.
#endif
            }
#if DEBUG
            else
            {
                throw new NotImplementedException("Could not validate the signature of the received message. The signature was \"0x" + args.Data[0] + "\" but only \"0x92\" is allowed.");
            }
            // Need to add a simple debug message here, but this works as a great breakpoint until then.
#endif
        }