예제 #1
0
        public async Task HandleNSUsr(string[] contents, NSClient client)
        {
            if (client.Protocol == null)
            {
                await client.SendAsync($"715 {contents[1]}", true);
            }

            if (contents[2] != "MD5")
            {
                await client.SendAsync($"911 {contents[1]}", true);
            }

            switch (contents[3])
            {
            case "I":
                client.UserHandle = contents[4];
                client.AuthEpoch  = (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
                await client.SendAsync($"USR {contents[1]} MD5 S {client.AuthEpoch}");

                break;

            case "S":
                var user = await client.Database.Users.Where(x => x.Email == client.UserHandle).FirstOrDefaultAsync();

                if (user == null)
                {
                    await client.SendAsync("911 " + contents[1], true);
                }

                var code = (client.AuthEpoch + userManager.GetPassword(user)).ToMD5().ToLower();
                if (code != contents[4])
                {
                    await client.SendAsync("911 " + contents[1], true);
                }

                var con = NSClients.Where(x => client.Database.Users.Find(x.UserID) != null).Where(x => client.Database.Users.Find(x.UserID).Email == client.UserHandle);
                foreach (var u in con)
                {
                    await u.SendAsync("OUT OTH", true);
                }

                client.UserID = user.ID;

                await client.SendAsync($"USR {contents[1]} OK {client.UserHandle} {user.SafeNickname}");

                break;

            default:
                await client.SendAsync("911 " + contents[1], true);

                break;
            }
        }
예제 #2
0
        public async Task HandleSBAns(string[] contents, SBClient client)
        {
            var list = NSClients.Where(x => x.UserHandle == contents[2]).Select(nsc => nsc.SBAuths).FirstOrDefault();

            if (list == null || !list.Contains(contents[3]))
            {
                await client.SendAsync($"911 {contents[1]}", true);

                return;
            }

            var session = Sessions.Where(x => x.ID.ToString() == contents[4]).FirstOrDefault();

            if (session == null)
            {
                client.Connection.Client.Close();
                return;
            }

            var nsc = NSClients.Where(x => x.UserHandle == contents[2]).FirstOrDefault();

            nsc.SBAuths.Remove(contents[3]);
            client.NSClient   = nsc;
            client.AuthString = contents[3];
            session.Clients.Add(client);
            client.Session = session;

            for (int i = 0; i < session.Clients.Count; i++)
            {
                var participant = session.Clients[i];
                if (participant == client)
                {
                    continue;
                }
                var partUser = await client.NSClient.Database.Users.FindAsync(participant.NSClient.UserID);

                await client.SendAsync($"IRO {contents[1]} {i + 1} {session.Clients.Count - 1} {partUser.Email} {partUser.SafeNickname}");
            }

            await client.SendAsync($"ANS {contents[1]} OK");

            var usr = await client.NSClient.Database.Users.FindAsync(nsc.UserID);

            foreach (var cl in session.Clients.Where(x => x != client))
            {
                await cl.SendAsync($"JOI {usr.Email} {usr.SafeNickname}");
            }
        }
예제 #3
0
        public async Task HandleSBUsr(string[] contents, SBClient client)
        {
            var list = NSClients.Where(x => x.UserHandle == contents[2]).Select(nsc => nsc.SBAuths).FirstOrDefault();

            if (list == null || !list.Contains(contents[3]))
            {
                await client.SendAsync($"911 {contents[1]}", true);

                return;
            }

            var nsc = NSClients.Where(x => x.UserHandle == contents[2]).FirstOrDefault();

            nsc.SBAuths.Remove(contents[3]);
            client.NSClient   = nsc;
            client.AuthString = contents[3];
            await client.SendAsync($"USR {contents[1]} OK {nsc.UserHandle} {(await nsc.Database.Users.FindAsync(nsc.UserID)).SafeNickname}");
        }
예제 #4
0
        public async Task HandleSBCal(string[] contents, SBClient client)
        {
            await RequireSBAuthentication(contents, client);

            if (!contents[2].IsEmail())
            {
                await client.SendAsync($"208 {contents[1]}", true);

                return;
            }
            var user = await client.NSClient.Database.Users.FindAsync(client.NSClient.UserID);

            var match = NSClients.Where(x => x.UserHandle == contents[2]).FirstOrDefault();

            if (match == null || match.Presence == Presence.Hidden)
            {
                await client.SendAsync($"217 {contents[1]}", true);

                return;
            }

            var matchUser = await client.NSClient.Database.Users.FindAsync(match.UserID);

            Session session = client.Session ?? new Session
            {
                ID = (uint)RandomNumberGenerator.GetInt32(10000000),
            };

            if (session.Clients.Contains(client))
            {
                await client.SendAsync($"215 {contents[1]}", true);

                return;
            }


            if ((matchUser.MessagePrivacy && userManager.AreUsersInGroup(ContactList.BlockList, user, matchUser)) ||
                (!matchUser.MessagePrivacy && userManager.AreUsersInGroup(ContactList.AllowList, user, matchUser)))
            {
                await client.SendAsync($"216 {contents[1]}", true);

                return;
            }


            session.Clients.Add(client);
            client.Session = session;
            Sessions.Add(session);

            var authString = $"{(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds}.{RandomNumberGenerator.GetInt32(999999)}";

            match.SBAuths.Add(authString);

            // AL and is on block list
            // OR
            // BL and it isn't on Allow List.
            if ((matchUser.MessagePrivacy && userManager.AreUsersInGroup(ContactList.BlockList, user, matchUser)) ||
                (!matchUser.MessagePrivacy && userManager.AreUsersInGroup(ContactList.AllowList, user, matchUser)))
            {
                await client.SendAsync($"216 {contents[1]}", true);

                return;
            }


            await match.SendAsync($"RNG {session.ID} {configuration.GetValue<string>("Switchboard")} CKI {authString} {matchUser.Email} {matchUser.SafeNickname}");

            await client.SendAsync($"CAL {contents[1]} RINGING 0");
        }
예제 #5
0
        public async Task HandleNSAdd(string[] contents, NSClient client)
        {
            await RequireNSAuthentication(contents, client);

            if (!contents[3].IsEmail())
            {
                await client.SendAsync("201 " + contents[1]);

                return;
            }

            var match = await client.Database.Users.Where(x => x.Email == contents[3]).FirstOrDefaultAsync();

            var current = await client.Database.Users.Where(x => x.ID == client.UserID).FirstAsync();

            if (match == null)
            {
                await client.SendAsync("205 " + contents[1]);

                return;
            }

            switch (contents[2])
            {
            case "RL":
                client.Connection.Client.Close();
                break;

            case "FL":
                if (userManager.AreUsersInGroup(ContactList.ForwardList, current, match))
                {
                    await client.SendAsync("215 " + contents[1]);

                    return;
                }

                client.Database.Contacts.Add(new Contact
                {
                    ContactList = ContactList.ForwardList,
                    User        = current.ID,
                    Target      = match.ID
                });

                client.Database.Contacts.Add(new Contact
                {
                    ContactList = ContactList.ReverseList,
                    User        = match.ID,
                    Target      = current.ID
                });

                match.SyncID++;
                current.SyncID++;

                break;

            case "AL":
                if (userManager.AreUsersInGroup(ContactList.AllowList, current, match))
                {
                    await client.SendAsync("215 " + contents[1]);

                    return;
                }

                if (userManager.AreUsersInGroup(ContactList.BlockList, current, match))
                {
                    await client.SendAsync("219 " + contents[1]);

                    return;
                }

                client.Database.Contacts.Add(new Contact
                {
                    ContactList = ContactList.AllowList,
                    User        = current.ID,
                    Target      = match.ID
                });

                current.SyncID++;

                break;

            case "BL":
                if (userManager.AreUsersInGroup(ContactList.BlockList, current, match))
                {
                    await client.SendAsync("215 " + contents[1]);

                    return;
                }

                if (userManager.AreUsersInGroup(ContactList.AllowList, current, match))
                {
                    await client.SendAsync("219 " + contents[1]);

                    return;
                }

                client.Database.Contacts.Add(new Contact
                {
                    ContactList = ContactList.BlockList,
                    User        = current.ID,
                    Target      = match.ID
                });
                current.SyncID++;

                break;

            default:
                await client.SendAsync("224 " + contents[1]);

                break;
            }
            await client.Database.SaveChangesAsync();

            await client.SendAsync($"ADD {contents[1]} {contents[2]} {current.SyncID} {contents[3]} {contents[4]}");

            var clientMatch = NSClients.Where(x => x.UserID == match.ID).FirstOrDefault();

            if (clientMatch != null && contents[2] == "AL")
            {
                await clientMatch.SendAsync($"ADD 0 RL {match.SyncID} {current.Email} {current.SafeNickname}");
            }

            var status = clientMatch != null?ToPresenceAcronym(clientMatch.Presence) : "FLN";

            await client.SendAsync($"ILN {contents[1]} {status} {contents[3]} {match.SafeNickname}");
        }