<commit_before>﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Threading;
using Naboo.AppUtil;

namespace Naboo.MitbbsReader
{
    public class MitbbsWebSession
    {
        public bool IsLoggedIn { get; private set; }

        public event EventHandler<MitbbsWebSessionEventArgs> LogInCompleted;
        public event EventHandler<MitbbsWebSessionEventArgs> LogOutCompleted;
        public DateTime LastConnectTime { get; private set; }

        private String _username;
        private String _password;
        
        private CookieContainer _cookies = new CookieContainer();
        private HtmlWeb _web = new HtmlWeb();

        private String _logInStartPageUrl = "/mwap/home/index.php";
        private String _logInPageUrl = "/mwap/login.php";
        private String _logOutPageUrl = "/mwap/logout.php";

        private Gb2312Encoding _encoding = new Gb2312Encoding();
        private uint _retries = 0;

        public CookieContainer Cookies
        {
            get
            {
                return _cookies;
            }
        }

        public String Username
        {
            get
            {
                if (IsConnecting)
                {
                    return "正在登录...";
                }
                else if (IsLoggedIn)
                {
                    return _username;
                }
                else
                {
                    return "未登录";
                }
            }
        }

        public bool IsConnecting { get; private set; }

        public MitbbsWebSession()
        {
            IsLoggedIn = false;
            _web.Cookies = _cookies;
            _web.GenerateFormElements = true;
            _web.Encoding = _encoding;

            IsConnecting = false;
            LastConnectTime = DateTime.MinValue;
        }

        public void StartLogIn(String username, String password, uint retries = 1)
        {
            bool createNewWebObj = !IsLoggedIn;

            if (IsConnecting)
            {
                MitbbsWebSessionEventArgs logInCompleteArgs = new MitbbsWebSessionEventArgs();
                logInCompleteArgs.Success = false;

                if (LogInCompleted != null)
                {
                    LogInCompleted(this, logInCompleteArgs);
                }

                return;
            }

            IsConnecting = true;

            if (IsLoggedIn)
            {
                IsLoggedIn = false;
                MitbbsWebSessionEventArgs logOutCompleteArgs = new MitbbsWebSessionEventArgs();
                logOutCompleteArgs.Success = true;

                if (LogOutCompleted != null)
                {
                    LogOutCompleted(this, logOutCompleteArgs);
                }
            }

            //_cookies = new CookieContainer();

            if (username == null)
            {
                username = "";
            }

            if (password == null)
            {
                password = "";
            }

            _username = username;
            _password = password;

            IsLoggedIn = false;

            if (createNewWebObj)
            {
                if (_web != null)
                {
                    _web.LoadCompleted -= OnLogInStartPageLoaded;
                    _web.LoadCompleted -= OnLogInPageLoaded;
                    _web.LoadCompleted -= OnLogOutPageLoaded;
                }

                _web = new HtmlWeb();
                _web.Cookies = _cookies;
                _web.GenerateFormElements = true;
                _web.Encoding = _encoding;
            }

            _retries = retries;
            _web.LoadCompleted += OnLogInStartPageLoaded;
            _web.LoadAsync(App.Settings.BuildUrl(_logInStartPageUrl));
        }

        public void StartLogOut()
        {
            if (IsConnecting)
            {
                MitbbsWebSessionEventArgs logOutCompleteArgs = new MitbbsWebSessionEventArgs();
                logOutCompleteArgs.Success = false;

                if (LogOutCompleted != null)
                {
                    LogOutCompleted(this, logOutCompleteArgs);
                }

                return;
            }

            IsConnecting = true;
            IsLoggedIn = false;
            _web.LoadCompleted += OnLogOutPageLoaded;
            _web.LoadAsync(App.Settings.BuildUrl(_logOutPageUrl));
        }

        private void OnLogInStartPageLoaded(object sender, HtmlDocumentLoadCompleted args)
        {
            _web.LoadCompleted -= OnLogInStartPageLoaded;

            if ((args.Document != null) && (_web.FormElements.ContainsKey("id")))
            {
                _web.FormElements["id"] = _username;
                _web.FormElements["passwd"] = <PASSWORD>;
                _web.LoadCompleted += OnLogInPageLoaded;
                _web.LoadAsync(App.Settings.BuildUrl(_logInPageUrl), HtmlWeb.OpenMode.Post);
            }
            else
            {
                MitbbsWebSessionEventArgs logInCompleteArgs = new MitbbsWebSessionEventArgs();
                logInCompleteArgs.Success = false;

                if ((args.Document != null))
                {
                    IEnumerable<HtmlNode> textNodes = args.Document.DocumentNode.Descendants("#text");
                    foreach (HtmlNode textNode in textNodes)
                    {
                        if (HtmlUtilities.GetPlainHtmlText(textNode.InnerText) == "您已经登录!")
                        {
                            // Already logged in
                            //
                            logInCompleteArgs.Success = true;
                            IsLoggedIn = true;
                            break;
                        }
                    }
                }

                IsConnecting = false;

                if (!IsLoggedIn)
                {
                    if (_retries <= 0)
                    {
                        MessageBoxResult result = MessageBox.Show("需要再重试一次吗？", "[" + _username + "]用户登录失败", MessageBoxButton.OKCancel);
                        if (result == MessageBoxResult.OK)
                        {
                            StartLogIn(_username, _password);
                            return;
                        }
                    }<commit_msg>#1 Fixed login check<commit_after>﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Threading;
using Naboo.AppUtil;

namespace Naboo.MitbbsReader
{
    public class MitbbsWebSession
    {
        public bool IsLoggedIn { get; private set; }

        public event EventHandler<MitbbsWebSessionEventArgs> LogInCompleted;
        public event EventHandler<MitbbsWebSessionEventArgs> LogOutCompleted;
        public DateTime LastConnectTime { get; private set; }

        private String _username;
        private String _password;
        
        private CookieContainer _cookies = new CookieContainer();
        private HtmlWeb _web = new HtmlWeb();

        private String _logInStartPageUrl = "/mwap/home/index.php";
        private String _logInPageUrl = "/mwap/login.php";
        private String _logOutPageUrl = "/mwap/logout.php";

        private Gb2312Encoding _encoding = new Gb2312Encoding();
        private uint _retries = 0;

        public CookieContainer Cookies
        {
            get
            {
                return _cookies;
            }
        }

        public String Username
        {
            get
            {
                if (IsConnecting)
                {
                    return "正在登录...";
                }
                else if (IsLoggedIn)
                {
                    return _username;
                }
                else
                {
                    return "未登录";
                }
            }
        }

        public bool IsConnecting { get; private set; }

        public MitbbsWebSession()
        {
            IsLoggedIn = false;
            _web.Cookies = _cookies;
            _web.GenerateFormElements = true;
            _web.Encoding = _encoding;

            IsConnecting = false;
            LastConnectTime = DateTime.MinValue;
        }

        public void StartLogIn(String username, String password, uint retries = 1)
        {
            bool createNewWebObj = !IsLoggedIn;

            if (IsConnecting)
            {
                MitbbsWebSessionEventArgs logInCompleteArgs = new MitbbsWebSessionEventArgs();
                logInCompleteArgs.Success = false;

                if (LogInCompleted != null)
                {
                    LogInCompleted(this, logInCompleteArgs);
                }

                return;
            }

            IsConnecting = true;

            if (IsLoggedIn)
            {
                IsLoggedIn = false;
                MitbbsWebSessionEventArgs logOutCompleteArgs = new MitbbsWebSessionEventArgs();
                logOutCompleteArgs.Success = true;

                if (LogOutCompleted != null)
                {
                    LogOutCompleted(this, logOutCompleteArgs);
                }
            }

            //_cookies = new CookieContainer();

            if (username == null)
            {
                username = "";
            }

            if (password == null)
            {
                password = "";
            }

            _username = username;
            _password = password;

            IsLoggedIn = false;

            if (createNewWebObj)
            {
                if (_web != null)
                {
                    _web.LoadCompleted -= OnLogInStartPageLoaded;
                    _web.LoadCompleted -= OnLogInPageLoaded;
                    _web.LoadCompleted -= OnLogOutPageLoaded;
                }

                _web = new HtmlWeb();
                _web.Cookies = _cookies;
                _web.GenerateFormElements = true;
                _web.Encoding = _encoding;
            }

            _retries = retries;
            _web.LoadCompleted += OnLogInStartPageLoaded;
            _web.LoadAsync(App.Settings.BuildUrl(_logInStartPageUrl));
        }

        public void StartLogOut()
        {
            if (IsConnecting)
            {
                MitbbsWebSessionEventArgs logOutCompleteArgs = new MitbbsWebSessionEventArgs();
                logOutCompleteArgs.Success = false;

                if (LogOutCompleted != null)
                {
                    LogOutCompleted(this, logOutCompleteArgs);
                }

                return;
            }

            IsConnecting = true;
            IsLoggedIn = false;
            _web.LoadCompleted += OnLogOutPageLoaded;
            _web.LoadAsync(App.Settings.BuildUrl(_logOutPageUrl));
        }

        private void OnLogInStartPageLoaded(object sender, HtmlDocumentLoadCompleted args)
        {
            _web.LoadCompleted -= OnLogInStartPageLoaded;

            if ((args.Document != null) && (_web.FormElements.ContainsKey("id")))
            {
                _web.FormElements["id"] = _username;
                _web.FormElements["passwd"] = <PASSWORD>;
                _web.LoadCompleted += OnLogInPageLoaded;
                _web.LoadAsync(App.Settings.BuildUrl(_logInPageUrl), HtmlWeb.OpenMode.Post);
            }
            else
            {
                MitbbsWebSessionEventArgs logInCompleteArgs = new MitbbsWebSessionEventArgs();
                logInCompleteArgs.Success = false;

                if ((args.Document != null))
                {
                    IEnumerable<HtmlNode> textNodes = args.Document.DocumentNode.Descendants("#text");
                    foreach (HtmlNode textNode in textNodes)
                    {
                        if (HtmlUtilities.GetPlainHtmlText(textNode.InnerText) == "家页")
                        {
                            // Already logged in
                            //
                            logInCompleteArgs.Success = true;
                            IsLoggedIn = true;
                            break;
                        }
                    }
                }

                IsConnecting = false;

                if (!IsLoggedIn)
                {
                    if (_retries <= 0)
                    {
                        MessageBoxResult result = MessageBox.Show("需要再重试一次吗？", "[" + _username + "]用户登录失败", MessageBoxButton.OKCancel);
                        if (result == MessageBoxResult.OK)
                        {
                            StartLogIn(_username, _password);
                            return;
                        }
                    }
{'fraction_non_alphanumeric': 0.07390545564913878, 'fraction_numerical': 0.0017452006980802793, 'mean_word_length': 1.2786998616874135, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1889851', 'n_tokens_mistral': 3368, 'n_tokens_neox': 3137, 'n_words': 625}