Esempio n. 1
0
        void DoLogOut()
        {
            protocol.StopProcessing();

            foreach (var friend in friends.Values)
            {
                friend.Dispose();
            }

            foreach (var group in groups.Values)
            {
                group.Dispose();
            }

            friends.Clear();
            groups.Clear();
            userCache.Clear();
            me = null;
            loggedIn = false;
        }
Esempio n. 2
0
        void HandleDetailUsers(JObject message)
        {
            List<User> addedUsers = new List<User>();
            List<User> changedUsers = new List<User>();
            foreach (var rawUser in message["users"])
            {
                // Convert the JSON object into a User
                var description = rawUser.ToObject<UserDescription>();
                var username = description.Name;

                // Set the change type based on whether or not we've seen the user before
                User user;
                if (userCache.TryGetValue(username, out user))
                {
                    user.Update(description);
                    changedUsers.Add(user);
                }
                else
                {
                    user = new User(this, description);
                    userCache[username] = user;
                    addedUsers.Add(user);
                }

                // Add or remove the user from our friends list
                if (user.Relation == UserRelation.Friend)
                    friends[username] = user;
                else if (friends.ContainsKey(username))
                    friends.Remove(username);
            }

            if (UserDetailsChange != null)
                UserDetailsChange(this, new UserDetailsEventArgs(addedUsers, changedUsers));
        }
Esempio n. 3
0
        public void LogIn(string username, string password, EventHandler<LoginReplyEventArgs> callback)
        {
            VerifyConnected();

            Contract.Requires(protocol != null);
            Contract.Requires(username != null);
            Contract.Requires(password != null);

            string hashedPassword;
            using (var algorithm = SHA256.Create())
            {
                algorithm.ComputeHash(Encoding.UTF8.GetBytes(password));

                var sb = new StringBuilder(32);
                foreach (byte b in algorithm.Hash)
                    sb.Append(b.ToString("x2"));
                hashedPassword = sb.ToString();
            }

            var message = protocol.CreateMessage("login");
            message["username"] = username;
            message["password"] = hashedPassword;
            message["initial_status"] = UserStatus.Available.ToString();

            protocol.SendMessage(message, (messageName, result, reply) =>
                {
                    loggedIn = true;

                    LoginReplyEventArgs args;
                    switch (result)
                    {
                        case Network.ReplyResult.Success:
                            var resultCode = Convert.ToInt32(reply["result"]);
                            var resultMessage = Convert.ToString(reply["result_message"]);
                            if (protocol.CheckObjectFields("login", reply, "me"))
                            {
                                me = new User(this, reply["me"].ToObject<UserDescription>());
                                userCache[me.Name] = me;
                                loggedIn = true;

                                args = new LoginReplyEventArgs(resultCode, resultMessage, Me);
                            }
                            else
                                args = new LoginReplyEventArgs(resultCode, resultMessage, null);
                            break;

                        case Network.ReplyResult.Fail:
                            args = new LoginReplyEventArgs((int) ResultCode.NotSent, MessageNotSentString, null);
                            break;

                        case Network.ReplyResult.Expired:
                            args = new LoginReplyEventArgs((int) ResultCode.NoReply, NoReplyString, null);
                            break;

                        default:
                            throw new Exception("This should not happen");
                    }
                    if (callback != null)
                        callback(this, args);

                    // Need to detail the logged-in user
                    if (result == Network.ReplyResult.Success)
                        UserDetailsChange.SafeInvoke(this, new UserDetailsEventArgs(new IUser[] { Me }, Enumerable.Empty<IUser>()));
                });

            log.InfoFormat("Trying to log in as {0}", username);
        }