Ejemplo n.º 1
0
        private void FriendLoad(string[] commandargs, NetworkStream stream)
        {
            if (commandargs.Length != 2)
            {
                return;
            }
            string username = commandargs[1];

            try
            {
                Console.WriteLine("MiscController: loading the firendlist of " + username);
                List <string> mutualLikeList        = DatabaseController.instance().GetMutualFriending(username);
                List <string> onlyLovedBySenderList = DatabaseController.instance().GetOnlyLovedBySender(username);
                List <string> onlySenderLovedByList = DatabaseController.instance().GetOnlySenderLovedBy(username);


                string replypart1 = String.Join("|", mutualLikeList);
                string replypart2 = String.Join("|", onlyLovedBySenderList);
                string replypart3 = String.Join("|", onlySenderLovedByList);

                string reply = replypart1 + "!" + replypart2 + "!" + replypart3;

                //Console.WriteLine("MiscController TEMP: " + reply);

                try
                {
                    byte[] msg = Encoding.Unicode.GetBytes(reply);
                    stream.Write(msg);
                }
                catch (Exception e)
                {
                    Console.WriteLine("MiscController error: could not reach client, error message: " + e.Message);
                    return;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("MiscController error: could not reach client, error message: " + e.Message);
                return;
            }
        }
Ejemplo n.º 2
0
        private void ConvLoad(string[] commandargs, NetworkStream stream)
        {
            try
            {
                if (commandargs.Length != 4)
                {
                    return;
                }
                string convid = commandargs[1] + "|" + commandargs[2] + "|" + commandargs[3];
                string res    = DatabaseController.instance().GetChatHistoryText(convid);

                //if(res == "") { res = "!"; }

                byte[] reply = Encoding.Unicode.GetBytes(res);

                stream.Write(reply);
            }
            catch (Exception e)
            {
                Console.WriteLine("MiscController error: could not send convtext message to client! Error message: " + e.Message);
            }
        }
Ejemplo n.º 3
0
        private void ListLoad(string[] commandargs, NetworkStream stream)
        {
            try
            {
                if (commandargs.Length != 2)
                {
                    return;
                }
                string username = commandargs[1];
                Console.WriteLine("MiscController: trying to get the list of " + username);

                List <string> reslist = DatabaseController.instance().GetChatHistoryIDs(username);
                string        res     = String.Join("!", reslist);

                /*foreach(string thingy in res)
                 * {
                 *  Console.Write(thingy + "   ");
                 * }
                 *
                 * string answer = String.Join("!", res);
                 * answer = answer + "!";*/

                if (res == "")
                {
                    res = "!";
                }

                byte[] reply = Encoding.Unicode.GetBytes(res);

                stream.Write(reply);
            }
            catch (Exception e)
            {
                Console.WriteLine("MiscController error: could not send back convlist message to client! Error message: " + e.Message);
            }
        }
Ejemplo n.º 4
0
        public void logincontrol()
        {
            TcpListener server = new TcpListener(IPAddress.Any, PortManager.instance().Loginport);

            server.Start();


            while (true)
            {
                // Buffer for reading data
                byte[]    bytes   = new Byte[256];
                string    message = null;
                TcpClient client  = new TcpClient();

                try
                {
                    client = server.AcceptTcpClient();
                    NetworkStream stream = client.GetStream();

                    KeyValuePair <bool, string> pair = Utility.ReadFromNetworkStream(stream);

                    if (pair.Key == false)
                    {
                        Console.WriteLine("LoginController: invalid syntax on message, discarding request."); continue;
                    }

                    message = pair.Value;

                    Console.WriteLine("LoginController:  recieved during login or register attempt: " + message);


                    ///<<------------------->>///

                    /*int buffersize = 256;
                     * byte[] data = new byte[buffersize];
                     * stream.Read(data, 0, buffersize);
                     * message = System.Text.Encoding.ASCII.GetString(data);
                     * Console.WriteLine("Recieved during login: "******"|");
                    bool     success  = false;
                    string   username = "";

                    if (raw_text[0] == "LOGIN")
                    {
                        if (raw_text.Length != 3)
                        {
                            success = false;
                        }
                        else
                        {
                            username = raw_text[1];
                            string password = raw_text[2];

                            if (!username.All(char.IsLetterOrDigit) || username.Length < 4 || password.Length < 4 || username.Length > 512)
                            {
                                success = false;
                            }
                            else
                            {
                                success = DatabaseController.instance().successfulLogin(username, password);
                            }
                        }
                    }
                    else if (raw_text[0] == "REGISTER")
                    {
                        if (raw_text.Length != 5)
                        {
                            success = false;
                        }
                        else
                        {
                            username = raw_text[1];
                            string password = raw_text[2];

                            if (!username.All(char.IsLetterOrDigit) || username.Length < 5 || password.Length < 5 || username.Length > 512)
                            {
                                success = false;
                            }
                            else
                            {
                                int age = Int32.Parse(raw_text[3]);
                                int sex = Int32.Parse(raw_text[4]);

                                success = DatabaseController.instance().successfulRegister(username, password, age, sex);
                            }
                        }
                    }///more features to be added if needed

                    byte[] msg;
                    string log;
                    if (success)
                    {
                        log = "OK|";
                        log = log + DatabaseController.instance().GetAgeAndGender(username);
                        msg = Encoding.Unicode.GetBytes(log);

                        Console.WriteLine("LoginController: Successful login, login data: " + log);
                    }
                    else
                    {
                        log = "ER|-1|-1";
                        msg = Encoding.Unicode.GetBytes(log);
                    }


                    // Send back a response.
                    stream.Write(msg, 0, msg.Length);
                    Console.WriteLine("LoginController: " + log + "data was sent to the client!");
                }
                catch (Exception e)
                {
                    Console.WriteLine("LoginController exception: probably someone left during login attempt, error message: " + e.Message);
                }
                finally
                {
                    // Shutdown and end connection
                    client.Close();
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Determines if the curUser is looking for the candidate. If not, it will be added to the cantMatch set
        /// </summary>
        /// <param name="curUser"></param>
        /// <param name="candidate"></param>
        /// <returns></returns>
        private bool lookingForThem(MatchUser curUser, MatchUser candidate)
        {
            if (curUser.Username == candidate.Username) ///if we spam the start and stop searching button
            {
                return(false);
            }


            /*MatchUser candidateArch = new MatchUser();
             * candidateArch.setArch(candidate);*/

            bool success = (candidate.Age == curUser.Age);                                                                                                                                                ///first step

            success = success && (candidate.Sex == curUser.LookingForSex || curUser.LookingForSex == GENDER.ANY) && DatabaseController.instance().WasntBlockedBy(curUser.Username, candidate.Username);   ///we are cool if the candidate is in the gender we are looking for OR if we dont care about it at all
            success = success && (curUser.Sex == candidate.LookingForSex || candidate.LookingForSex == GENDER.ANY) && DatabaseController.instance().WasntBlockedBy(candidate.Username, curUser.Username); ///and vice versa

            /*if (success == false) ///then add it to the "unmatchable" group.
             * {
             *  cantMatch.Add(new MatchUser(curUser), false);
             *
             *  if (curUser.LookingForSex == GENDER.ANY) ///if it was that flexible, add all gender into the unmatchable group
             *  {
             *      foreach (GENDER sex in Enum.GetValues(typeof(GENDER)))
             *      {
             *          MatchUser archuser = new MatchUser();
             *          archuser.setArch(sex, curUser.Age);
             *          cantMatch.Add(archuser, false);
             *      }
             *  }
             * }*/
            return(success);
        }
Ejemplo n.º 6
0
        private void ConvSave(string[] commandargs, NetworkStream stream)
        {
            if (commandargs.Length != 5)
            {
                return;
            }
            string savename = commandargs[1] + "|" + commandargs[2] + "|" + commandargs[3];;
            string inserter = commandargs[4];
            bool   wasSaved = DatabaseController.instance().AlreadySavedChatHistory(savename);

            if (wasSaved)
            {
                try
                {
                    bool success = DatabaseController.instance().InsertMessageHistoryConnection(savename, inserter);
                    Console.WriteLine("MiscController: already inserted message history for " + inserter + " as " + savename);

                    if (success)
                    {
                        byte[] okmsg = Encoding.Unicode.GetBytes("OK");
                        stream.Write(okmsg);
                    }
                    else
                    {
                        byte[] okmsg = Encoding.Unicode.GetBytes("ER");
                        stream.Write(okmsg);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("MiscController error: could not reach client, error message: " + e.Message);
                    return;
                }
            }
            else
            {
                try
                {
                    byte[] okmsg = Encoding.Unicode.GetBytes("INSERT");
                    stream.Write(okmsg);


                    Thread.Sleep(100);
                    //Console.WriteLine("reading history...");
                    KeyValuePair <bool, string> pair = Utility.ReadFromNetworkStream(stream);

                    if (pair.Key == false)
                    {
                        Console.WriteLine("MiscController: invalid syntax on message, discarding request."); return;
                    }

                    string history = pair.Value;


                    //string history = Utility.ReadFromNetworkStream(stream);
                    if (DatabaseController.instance().InsertMessageHistoryConnection(savename, inserter))
                    {
                        if (DatabaseController.instance().InsertMessageHistoryText(savename, history))
                        {
                            okmsg = Encoding.Unicode.GetBytes("OK");
                            stream.Write(okmsg);
                            Console.WriteLine("MiscController: inserted message history for " + inserter + " as " + savename + " with text!");
                        }
                        else
                        {
                            okmsg = Encoding.Unicode.GetBytes("ER");
                            Console.WriteLine("MiscController notice: could not insert history text! Consider deleting manually the connections!");
                            stream.Write(okmsg);
                        }
                    }
                    else
                    {
                        Console.WriteLine("MiscController notice: could not insert history connection!");
                        okmsg = Encoding.Unicode.GetBytes("ER");
                        stream.Write(okmsg);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("MiscController notice: could not reach client after inserting history text, exception message: " + e.Message);
                    return;
                }
            }
        }