public static IPrivateMessageFolder CreateNewFolder(string name)
        {
            IPrivateMessageFolder result = null;
            IPrivateMessageFolderRequest request = null;
            var signal = new AutoResetEvent(false);
            Service.BeginEditFolderRequest((a, r) =>
                {
                    request = r;
                    signal.Set();
                });

            signal.WaitOne();

            if (request != null)
            {
                request.CreateFolder(name);
                bool success = false;
                request.SendRequest(a =>
                    {
                        success = a == ActionResult.Success;
                        signal.Set();
                    });
                signal.WaitOne();
                if (success) { result = new AwfulPrivateMessageFolder() { Name = name }; }
            }

            return result;
        }
 static AwfulPrivateMessageFolder()
 {
     Service = Services.AwfulServiceManager.PrivateMessageService;
     Inbox = new AwfulPrivateMessageFolder() { FolderID = 0 };
     SentItems = new AwfulPrivateMessageFolder() { FolderID = 1 };
 }
        private static bool RenameFolder(AwfulPrivateMessageFolder folder, string name)
        {
            bool value = false;
            if (folder.FolderID != UNKNOWN_FOLDER_ID)
            {
                var signal = new AutoResetEvent(false);
                IPrivateMessageFolderRequest request = null;
                Service.BeginEditFolderRequest((a, result) => { request = result; signal.Set(); });
                signal.WaitOne();
                if (request != null)
                {
                    request.RenameFolder(folder, name);
                    Service.SendEditFolderRequest(request, result =>
                    {
                        value = result == Models.ActionResult.Success;
                        signal.Set();
                    });

                    signal.WaitOne();
                    if (value) { folder.Name = name; }
                }
            }
            return value;
        }
 private static IPrivateMessageFolder RefreshFolder(AwfulPrivateMessageFolder folder)
 {
     var signal = new AutoResetEvent(false);
     IPrivateMessageFolder result = null;
     Service.FetchFolderAsync(folder.FolderID, (s, newFolder) =>
     {
         result = newFolder;
         signal.Set();
     });
     signal.WaitOne();
     return result;
 }
        /// <summary>
        /// TODO: ADD SUMMARY.
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        public static ICollection<IPrivateMessageFolder> ParseFolderList(HtmlDocument doc)
        {
            List<IPrivateMessageFolder> folders = new List<IPrivateMessageFolder>();
            // get select option nodes
            var top = doc.DocumentNode;
            var selectNode = top.Descendants("select")
                .Where(node => node.GetAttributeValue("name", "").Equals("folderid"))
                .FirstOrDefault();

            if (selectNode != null)
            {
                var optionNodes = selectNode.Descendants("option");
                foreach (var option in optionNodes)
                {
                    try
                    {
                        AwfulPrivateMessageFolder folder = new AwfulPrivateMessageFolder();
                        string name = option.NextSibling.InnerText.Trim();
                        string value = option.GetAttributeValue("value", "");
                        int id = 0;
                        if (int.TryParse(value, out id)) { folder.FolderID = id; }
                        folder.Name = name;
                        folders.Add(folder);
                    }
                    catch (Exception) { }
                }
            }
            return folders;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="htmlDocument"></param>
        /// <returns></returns>
        public static IPrivateMessageFolder ParsePrivateMessageFolder(HtmlDocument htmlDocument)
        {
            IPrivateMessageFolder folder = null;
            var top = htmlDocument.DocumentNode;
            var currentFolderNode = top.Descendants("div")
                .Where(node => node.GetAttributeValue("class", "").Equals("breadcrumbs"))
                .FirstOrDefault();

            if (currentFolderNode != null)
            {
                int folderId = 0;
                // *************************************************************
                // THIS IS CURRENTLY BROKEN ON SA -- THE TITLE OF THE FOLDER IS *NOT* WHAT'S DISPLAYED ON THE BREADCRUMBS //
                string name = currentFolderNode.ParseTitleFromBreadcrumbsNode();
                // *************************************************************
                var idString = GetInputValue(top.Descendants("input").ToArray(), "thisfolder");

                if (int.TryParse(idString, out folderId))
                    folder = new AwfulPrivateMessageFolder() { FolderID = folderId };
                else
                    folder = new AwfulPrivateMessageFolder();

                var messages = ParseMessageList(htmlDocument);
                foreach (var message in messages)
                {
                    // I hate casting, but whatever...
                    (message as AwfulPrivateMessage).FolderID = folder.FolderID;
                    folder.Messages.Add(message);
                }
            }

            return folder;
        }