예제 #1
0
        private static void AddEvent(Packets.AgendaEventPacket packet, Server server, IPEndPoint ip)
        {
            // Adding the event to database
            using (var db = new Models.ServerDatabase())
            {
                if (db.AgendaEvents.Any(c => c.Client.Username == packet.Username &&
                                        c.Date.Equals(packet.Date) &&
                                        c.EventInfo.Equals(packet.EventInfo) &&
                                        c.EventName.Equals(packet.EventName)))
                {
                    server.Udp.SendMessage(new Packets.AckErrorPacket(Packets.PacketTypes.Error, "New event already exists").Pack(), ip);
                    server.DebugInfo("NEw Agenda event: Event already exists.");
                    return;
                }

                db.AgendaEvents.InsertOnSubmit(new Models.AgendaEvent()
                {
                    Client    = db.Clients.Single(c => c.Username == packet.Username),
                    Date      = packet.Date,
                    EventInfo = packet.EventInfo,
                    EventName = packet.EventName
                });
                db.SubmitChanges();
            }

            server.DebugInfo("Event added correctly by " + packet.Username);
            // Send the ack
            server.Udp.SendMessage(new Packets.AckErrorPacket(Packets.PacketTypes.NewAgendaEventAck, "New event added").Pack(), ip);
        }
예제 #2
0
        public static void DeleteLinkSocialNet(byte[] data, IPEndPoint ip, Server server)
        {
            try
            {
                Packets.LinkSocialNetworkPacket packet = Packets.Packet.Unpack <Packets.LinkSocialNetworkPacket>(data);

                server.DebugInfo("Social Network Link Delete Request: Request recieved");
                server.DebugInfo("Packet DeleteLinkSocialNetworkReq: " + packet.ToString());

                // Who is making the query
                ClientLogic.ClientStatus client = server.GetClient(packet.ClientUsername);

                // Check the basics of connection
                if (ClientLogic.ClientsManagement.CheckBasics(client, ClientLogic.ClientStatus.Status.Disconnected, packet.Alea, out string message))
                {
                    // Check if the link already exists
                    using (var db = new Models.ServerDatabase())
                    {
                        if (db.LinkedSocialNetworks
                            .Any(l => l.Client.Username.Equals(packet.ClientUsername) &&
                                 l.SocialNetwork.Name.Equals(packet.SocialNetName)))
                        {
                            // If exist delete
                            db.LinkedSocialNetworks.DeleteOnSubmit(
                                db.LinkedSocialNetworks
                                .Single(l => l.Client.Username.Equals(packet.ClientUsername) &&
                                        l.SocialNetwork.Name.Equals(packet.SocialNetName)));

                            server.DebugInfo("Delete Social Network Link: The link has been deleted.");

                            // Submit changes
                            db.SubmitChanges();

                            // Send the ack
                            server.Udp.SendMessage(new Packets.AckErrorPacket(Packets.PacketTypes.DeleteLinkSocialNetAck,
                                                                              "Social net deleted correctly.").Pack(), ip);
                            return;
                        }
                        else
                        {
                            server.DebugInfo("Delete Social Network Link: Social net doesn't exist, can't be deleted.");
                            // Send the ack
                            server.Udp.SendMessage(new Packets.AckErrorPacket(Packets.PacketTypes.Error,
                                                                              "Social net doesn't exist.").Pack(), ip);
                        }
                    }
                }
                else
                {
                    server.DebugInfo("Delete Social Network Link: " + message);
                    server.Udp.SendError(message, ip);
                }
            }
            catch (SqlException)
            {
                server.DebugInfo("Delete Social Network Link: Database error.");
                server.Udp.SendError("Database error. Probably no social net found on database.", ip);
                return;
            }
        }
예제 #3
0
        private static void DeleteEvent(Packets.AgendaEventPacket packet, Server server, IPEndPoint ip)
        {
            // Deleting the event of database
            using (var db = new Models.ServerDatabase())
            {
                if (db.AgendaEvents.Any(c => c.Client.Username == packet.Username &&
                                        c.Date.Equals(packet.Date) &&
                                        c.EventInfo.Equals(packet.EventInfo) &&
                                        c.EventName.Equals(packet.EventName)))
                {
                    db.AgendaEvents.DeleteAllOnSubmit(
                        db.AgendaEvents.Where(c => c.Client.Username == packet.Username &&
                                              c.Date.Equals(packet.Date) &&
                                              c.EventInfo.Equals(packet.EventInfo) &&
                                              c.EventName.Equals(packet.EventName))

                        );
                    db.SubmitChanges();
                }
                else
                {
                    server.DebugInfo("Delete Agenda Event: Unable to find on the database.");
                    server.Udp.SendError("Unable to find on the database.", ip);
                    return;
                }

                server.DebugInfo("Delete Agenda Event: Deletd event correctly");
                server.Udp.SendMessage(new Packets.AckErrorPacket(Packets.PacketTypes.DeleteAgendaEventAck, "Event deleted").Pack(), ip);
            }
        }
예제 #4
0
        public static bool AckOrRegContactReq(Packets.ContactReqPacket packet,
                                              ClientStatus current,
                                              bool ack,
                                              out string message)
        {
            try
            {
                if (CheckBasics(current, ClientStatus.Status.Disconnected, packet.Alea, out message))
                {
                    using (var db = new Models.ServerDatabase())
                    {
                        // Check if requests exists on any of both sides
                        if (!db.ContactRequests.Any(c => (c.From.Username == packet.From && c.To.Username == packet.To)))
                        {
                            message = "Contact request doesn't exists.";
                            return(false);
                        }

                        // Remove the contact request
                        db.ContactRequests.DeleteOnSubmit(
                            db.ContactRequests
                            .Single(r => r.From.Username == packet.From && r.To.Username == packet.To)
                            );

                        if (ack)
                        {
                            // Create new record to Contact Table
                            db.Contacts.InsertOnSubmit(new Models.Contact()
                            {
                                Client1 = db.Clients.Single(c => c.Username == packet.From),
                                Client2 = db.Clients.Single(c => c.Username == packet.To)
                            });
                            message = "Contact added correctly.";
                        }
                        else
                        {
                            message = "Contact request refused correctly.";
                        }

                        db.SubmitChanges();
                    }
                }
                else
                {
                    return(false);
                }
                return(true);
            }
            catch (SqlException)
            {
                message = "New contact request error: Database error";
                return(false);
            }
        }
예제 #5
0
        // Resgister a new client to database
        public static bool RegisterClient(Packets.ProfilePacket packet,
                                          IPEndPoint ip,
                                          ref Models.Client c,
                                          out string message)
        {
            try
            {
                // Some checks before registering the user
                if (!packet.Alea.Equals("0000000"))
                {
                    message = "Alea must be 7 0s";
                    return(false);
                }

                // Declaring the new client
                c = new Models.Client()
                {
                    FirstName   = packet.FirstName,
                    LastName    = packet.LastName,
                    Age         = packet.Age,
                    PhoneNumber = packet.PhoneNumber,
                    Gender      = packet.Gender,
                    Username    = packet.Username,
                    Password    = packet.Password,
                    Email       = packet.Email,
                    Ip          = ip.Address.GetAddressBytes(),
                };

                // Open database and check if client already exists
                using (Models.ServerDatabase db = new Models.ServerDatabase())
                {
                    if (db.Clients.Where(a => a.Username == packet.Username).Count() != 0)
                    {
                        message = "Username " + c.Username + " already exists.";
                        return(false);
                    }
                    db.Clients.InsertOnSubmit(c);
                    db.SubmitChanges();
                }
                message = "Cool!";
                return(true);
            }
            catch (Exception e)
            {
                message = "Exception thrown: " + e.ToString();
                return(false);
            }
        }
예제 #6
0
        public static void LinkSocialNet(byte[] data, IPEndPoint ip, Server server)
        {
            try {
                Packets.LinkSocialNetworkPacket packet = Packets.Packet.Unpack <Packets.LinkSocialNetworkPacket>(data);

                server.DebugInfo("Social Network Link: Request recieved");
                server.DebugInfo("Packet LinkSocialNetworkReq: " + packet.ToString());

                // Who is making the query
                ClientLogic.ClientStatus client = server.GetClient(packet.ClientUsername);

                // Check the basics of connection
                if (ClientLogic.ClientsManagement.CheckBasics(client, ClientLogic.ClientStatus.Status.Disconnected, packet.Alea, out string message))
                {
                    // Check if the link already exists
                    using (var db = new Models.ServerDatabase())
                    {
                        if (db.LinkedSocialNetworks
                            .Any(l => l.Client.Username.Equals(packet.ClientUsername) &&
                                 l.SocialNetwork.Name.Equals(packet.SocialNetName)))
                        {
                            // Link exists on database -> Override it
                            var sn = db.LinkedSocialNetworks.Single(l => l.Client.Username.Equals(packet.ClientUsername) &&
                                                                    l.SocialNetwork.Name.Equals(packet.SocialNetName));

                            sn.Username = packet.SocialNetUsername;
                            sn.Password = packet.SocialNetPassword;

                            server.DebugInfo("Social Network Link: The link already exists, overriding it.");
                            return;
                        }
                        else
                        {
                            server.DebugInfo("Social Network Link: Social net linked correctly.");
                            db.LinkedSocialNetworks.InsertOnSubmit(new Models.LinkedSocialNetwork()
                            {
                                Client        = db.Clients.Single(c => c.Username.Equals(packet.ClientUsername)),
                                SocialNetwork = db.SocialNetworks.Single(c => c.Name.Equals(packet.SocialNetName)),
                                Username      = packet.SocialNetUsername,
                                Password      = packet.SocialNetPassword
                            });
                        }

                        // Submit changes
                        db.SubmitChanges();

                        // Send the ack
                        server.Udp.SendMessage(new Packets.AckErrorPacket(Packets.PacketTypes.LinkSocialNetworkAck,
                                                                          "Social net linked correctly.").Pack(), ip);
                    }
                }
                else
                {
                    server.DebugInfo("Social Network Link: " + message);
                    server.Udp.SendError(message, ip);
                }
            }
            catch (SqlException e)
            {
                server.DebugInfo("Social Network Link: Database error.");
                server.Udp.SendError("Database error. Probably no social net found on database.", ip);
                return;
            }
        }
예제 #7
0
        public static bool NewContactRequest(Packets.ContactReqPacket packet,
                                             ClientStatus current,
                                             out string message)
        {
            try
            {
                if (packet.To == packet.From)
                {
                    message = "You can't add yourself as a friend....";
                    return(false);
                }
                if (CheckBasics(current, ClientStatus.Status.Disconnected, packet.Alea, out message))
                {
                    using (var db = new Models.ServerDatabase())
                    {
                        // Check if destination exists
                        if (!db.Clients.Any(c => c.Username == packet.To))
                        {
                            message = "Destination doesn't exist.";
                            return(false);
                        }

                        // Check if contact exists
                        if (db.Contacts.Any(c => (c.Client1.Username == packet.To && c.Client2.Username == packet.From) ||
                                            (c.Client1.Username == packet.From && c.Client2.Username == packet.To)))
                        {
                            message = "Contact already exists.";
                            return(false);
                        }

                        // Check if requests exists on any of both sides
                        if (db.ContactRequests.Any(c => (c.From.Username == packet.To && c.To.Username == packet.From) ||
                                                   (c.From.Username == packet.From && c.To.Username == packet.To)))
                        {
                            message = "Contact request already exists.";
                            return(false);
                        }

                        // Create new record to ContactRequests Table
                        db.ContactRequests.InsertOnSubmit(new Models.ContactRequest()
                        {
                            From = db.Clients.Single(c => c.Username == packet.From),
                            To   = db.Clients.Single(c => c.Username == packet.To)
                        });

                        db.SubmitChanges();
                    }
                }
                else
                {
                    return(false);
                }
                message = "Cool!";
                return(true);
            }
            catch (SqlException)
            {
                message = "New contact request error: Database error";
                return(false);
            }
        }
예제 #8
0
        public static bool UpdateProfile(Packets.ProfilePacket packet, ref ClientStatus current, out string message)
        {
            try
            {
                if (CheckBasics(current, ClientStatus.Status.Disconnected, packet.Alea, out message))
                {
                    using (var db = new Models.ServerDatabase())
                    {
                        // Check fields that can be changed
                        Models.Client c = db.Clients.Single(r => r.Username == packet.Username);

                        if (c.FirstName != packet.FirstName)
                        {
                            message = "Update Profile Error: First Name can't be updated";
                            return(false);
                        }

                        if (c.LastName != packet.LastName)
                        {
                            message = "Update Profile Error: Last Name can't be updated";
                            return(false);
                        }

                        if (c.Age != packet.Age)
                        {
                            message = "Update Profile Error: Age can't be updated";
                            return(false);
                        }

                        if (c.Gender != packet.Gender)
                        {
                            message = "Update Profile Error: Gender can't be updated";
                            return(false);
                        }

                        // update the profile
                        c.Password    = packet.Password;
                        c.PhoneNumber = packet.PhoneNumber;
                        c.Email       = packet.Email;


                        // Set new reference to current
                        current.Client = c;

                        db.SubmitChanges();
                    }

                    message = "Cool!";
                    return(true);
                }
                else
                {
                    message = "Update Profile Error: " + message;
                    return(false);
                }
            }
            catch (SqlException e)
            {
                message = e.ToString();
                return(false);
            }
        }
예제 #9
0
        public static void SendMessage(byte[] data, TcpClient client, Server server)
        {
            try
            {
                string message = "";

                // Unpack the data
                MessagePacket packet = Packet.Unpack <MessagePacket>(data);

                server.DebugInfo("SendMessageReq Recieved: " + packet.ToString());

                // Who is making the query
                ClientStatus c = server.GetClient(packet.From.Username);

                // Check te basics again and again
                if (ClientsManagement.CheckBasics(c, ClientStatus.Status.Disconnected, packet.Alea, out message))
                {
                    // Check if destination exists
                    if (!server.Clients.Any(d => d.Client.Username == packet.To.Username))
                    {
                        server.DebugInfo("Send Message Error: " + "Destination doesn't exist");
                        server.Tcp.SendError("Destination doesn't exist", client);
                        return;
                    }

                    // Store the message on database
                    using (var db = new Models.ServerDatabase())
                    {
                        // Create the message
                        Models.Message m = new Models.Message()
                        {
                            From    = db.Clients.Single(n => n.Username == c.Client.Username),
                            To      = db.Clients.Single(n => n.Username == packet.To.Username),
                            Date    = packet.Date,
                            Content = packet.Content
                        };

                        // Check if destination is on chat
                        if (server.ClientsOnChat.Any(d => d.Key.Equals(packet.To.Username)))
                        {
                            // If exist save the message and send him as well as readed message
                            m.Read = true;
                            server.DebugInfo("Message from : " + packet.From.Username + " sended to: " + packet.To.Username);
                            // Send the message to destination changing the type of package
                            packet.Type = (byte)PacketTypes.SendMessageAck;
                            server.Tcp.SendMessage(packet.Pack(), server.ClientsOnChat[packet.To.Username]);
                        }
                        else
                        {
                            m.Read = false;
                        }


                        db.Messages.InsertOnSubmit(m);
                        db.SubmitChanges();
                    }
                }
                else
                {
                    server.DebugInfo("Send Message Error: " + message);
                    server.Tcp.SendError(message, client);
                }
            }
            catch (SqlException)
            {
                server.DebugInfo("Send Message Error: database error.");
                server.Tcp.SendError("Database error", client);
            }
        }