Пример #1
0
 public string returnObject_message(MessageItem anItem)
 {
     return anItem.message;
 }
Пример #2
0
 public string returnObject_timeStamp(MessageItem anItem)
 {
     return anItem.timeStamp;
 }
Пример #3
0
 public Boolean returnObject_isEncrypted(MessageItem anItem)
 {
     return anItem.isEncrypted;
 }
Пример #4
0
 public string returnObject_isEncryptedString(MessageItem anItem)
 {
     return anItem.isEncrypted.ToString();
 }
Пример #5
0
 public string returnObject_userSent(MessageItem anItem)
 {
     return anItem.userSent;
 }
Пример #6
0
 public string returnObject_conversation(MessageItem anItem)
 {
     return anItem.conversation;
 }
Пример #7
0
        //Loads conversation for selected user
        /*private async void loadLastConversation()
        {
            //GET request to server (fetch new messages from server at the same time)
            using (HttpClient client = new HttpClient()) //using block makes the object disposable (one time use)
            {
                using (HttpResponseMessage response = await client.GetAsync("http://159.203.252.197/messages"))      //BUG:  HANDLE OFFLINE MODE
                {
                    if (App.DEBUG_MODE)
                    {
                        Debug.WriteLine("GET Status Code:  " + response.StatusCode);
                        Debug.WriteLine("GET Reason: " + response.ReasonPhrase);
                    }
                    using (HttpContent content = response.Content)
                    {
                        string content_string = await content.ReadAsStringAsync();
                        System.Net.Http.Headers.HttpContentHeaders content_headers = content.Headers;
                        if (App.DEBUG_MODE)
                        {
                            Debug.WriteLine("GET content:  " + content_string);
                            Debug.WriteLine("GET content headers:  " + content_headers);
                        }

                        //Load messages into chat list window
                        List<MessageItem> incomingMessages = JsonConvert.DeserializeObject<List<MessageItem>>(content_string);
                        for (int x = 0; x < incomingMessages.Count; x++)
                        {
                            ListViewItem incomingItems = new ListViewItem();

                            //Outputs desired text into the actual chat window
                            incomingItems.Content = incomingMessages[x].messageText();         
                                         
                            //References the MessageItem object
                            incomingItems.Tag = incomingMessages[x];                                                
                            chatListView.Items.Add(incomingItems);
                        }
                    }
                }
            }
        } */








        //POST FUNCTION
        private async void postMessage(String input, Boolean secureStatus)
        {
            if (input != String.Empty && App.userManagement.conversationIsNotNull())
            {

                //Encrypt text from input box if encryption is on
                // String timeStamp = DateTime.Now.ToString("MM/d/yy h:mm tt");           
                String finalInput;

                if(secureStatus == false) {
                    finalInput = input;
                } else {
                    finalInput = Crypto.Encrypt(App.strAsymmetricAlgName, Crypto.returnPublicKey_OTHER_USER(App.secrets), input);
                }

                //Load the text and associated message data into a messageItem object
                MessageItem newMessage = new MessageItem { conversation= App.userManagement.getConversationURL_string(),
                                                           message = finalInput,
                                                           userSent = App.userManagement.getOtherUserURL_string(),
                                                           //timeStamp = timeStamp,
                                                           userid = App.userManagement.getCurrentUser_string(),
                                                           isEncrypted = secureStatus
                                                         };
                
                //Create a Key and Pair match with the new messageItem object data in preparation of Http request
                var values = new Dictionary<string, string>
                {
                    { "conversation", newMessage.returnObject_conversation(newMessage) },
                    { "encrypted", newMessage.returnObject_isEncryptedString(newMessage) },
                    { "sentTo", "http://159.203.252.197/users/1/" },
                    { "text" , newMessage.returnObject_message(newMessage) }

                };

                //Encode the key pair match in preparation of Http request
                var theContent = new FormUrlEncodedContent(values);


                //POST request to the server with the encoded data (create HttpClient)
                using (HttpClient client = new HttpClient()) //using block makes the object disposable (one time use)
                {

                    //Check HTTP data
                    if (App.DEBUG_MODE == true)
                    {
                        Debug.WriteLine("POST CREDENTIALS:  "+App.userManagement.getCurrentUser_string() + ":" + currentUserPasswordActual.Password);
                    }

                    //Load User credentials from currentUser and password field located on user interface into the client object
                    var byteArray = Encoding.ASCII.GetBytes(App.userManagement.getCurrentUser_string() + ":"+currentUserPasswordActual.Password);
                    var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                    client.DefaultRequestHeaders.Authorization = header;

                    //Post the message to the server
                    using (HttpResponseMessage response = await client.PostAsync("http://159.203.252.197/messages/", theContent)) {

                        //Check HTTP data
                        if (App.DEBUG_MODE) {
                            Debug.WriteLine("POST Status Code:  " + response.StatusCode);
                            Debug.WriteLine("POST Reason: " + response.ReasonPhrase);

                            using (HttpContent content = response.Content) {
                                string content_string = await content.ReadAsStringAsync();
                                Debug.WriteLine("POST contents:  "+content_string);
                            }
                        }

                        //Refreshes ChatListView with recently POSTed string.  If credentials are wrong, output INVALID CREDENTIALS to chatwindow
                        if (response.IsSuccessStatusCode == true) {
                            //GET request to server to update chatListView with recentely POSTed message
                            using (HttpClient client2 = new HttpClient()) //using block makes the object disposable (one time use)
                            {
                                using (HttpResponseMessage response2 = await client2.GetAsync("http://159.203.252.197/messages/"))
                                {
                                    //Check HTTP data
                                    if (App.DEBUG_MODE)
                                    {
                                        Debug.WriteLine("GET Status Code:  " + response2.StatusCode);
                                        Debug.WriteLine("GET Reason: " + response2.ReasonPhrase);
                                    }

                                    using (HttpContent content2 = response2.Content)
                                    {
                                        string content_string2 = await content2.ReadAsStringAsync();
                                        System.Net.Http.Headers.HttpContentHeaders content_headers2 = content2.Headers;

                                        //Check HTTP data
                                        if (App.DEBUG_MODE)
                                        {
                                            Debug.WriteLine("GET content:  " + content_string2);
                                            Debug.WriteLine("GET content headers:  " + content_headers2);
                                        }

                                        //TEMPORARY METHOD TO POST MESSAGES FROM SERVER TO CHAT VIEW
                                        List<MessageItem> incomingMessages = JsonConvert.DeserializeObject<List<MessageItem>>(content_string2);
                                        ListViewItem incomingItems = new ListViewItem();
                                        int lastMessage = incomingMessages.Count - 1;
                                        incomingItems.Content = incomingMessages[lastMessage].messageText();                      //Outputs desired text into the actual chat window
                                        incomingItems.Tag = incomingMessages[lastMessage];                                                 //References the MessageItem object
                                        chatListView.Items.Add(incomingItems);
                                    }
                                }
                            }
                        } else {
                            var dialog = new MessageDialog("");
                            dialog.Title = "INVALID USER/PASSWORD COMBINATION";
                            dialog.Content = "You have entered the wrong password for the selected user, please try again";
                            dialog.Commands.Add(new UICommand { Label = "Ok", Id = 0 });
                            var res = await dialog.ShowAsync();
                        }
                    }
                }
            }
            //Clear textbox and auto scroll to the bottom
            inputBox.Text = String.Empty;
            chatListViewScroller.UpdateLayout();
            chatListViewScroller.ScrollToVerticalOffset(chatListView.ActualHeight);
        }
Пример #8
0
 public string returnObject_userid(MessageItem anItem)
 {
     return anItem.userid;
 }
Пример #9
0
 public string returnObject_message(MessageItem anItem)
 {
     return(anItem.message);
 }
Пример #10
0
 public string returnObject_userid(MessageItem anItem)
 {
     return(anItem.userid);
 }
Пример #11
0
 public string returnObject_conversation(MessageItem anItem)
 {
     return(anItem.conversation);
 }
Пример #12
0
 public Boolean returnObject_isEncrypted(MessageItem anItem)
 {
     return(anItem.isEncrypted);
 }
Пример #13
0
 public string returnObject_userSent(MessageItem anItem)
 {
     return(anItem.userSent);
 }
Пример #14
0
 public string returnObject_isEncryptedString(MessageItem anItem)
 {
     return(anItem.isEncrypted.ToString());
 }
Пример #15
0
 public string returnObject_timeStamp(MessageItem anItem)
 {
     return(anItem.timeStamp);
 }