<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;

namespace Signum.Engine.Mailing.Pop3
{
    public class Pop3Client : IDisposable
    {
        string userName;
        string password;
        string server;
        bool useSSL;

        TcpClient connection;
        Stream stream;
        StreamReader reader;

        public Pop3Client(string userName, string password, string server, int port, bool useSSL)
        {
            this.userName = userName;
            this.password = password;
            this.server = server;
            this.useSSL = useSSL;
        }

        public void Connect()
        {
            connection = new TcpClient();
            connection.Connect(server, 110);

            if (useSSL)
            {
                var sslStream = new SslStream(connection.GetStream(), false);
                sslStream.AuthenticateAsClient(server);
                stream = sslStream;
            }
            else
            {
                stream = connection.GetStream();
            }

            reader = new StreamReader(stream, Encoding.ASCII);

            string response = reader.ReadLine();
            if (!response.StartsWith("+OK"))
                throw new Pop3ClientException("Not ready for Log-in " + response);

            SendCommand("USER " + userName + "\r\n");
            response = reader.ReadLine();
            if (!response.StartsWith("+OK"))
                throw new Pop3ClientException("Log-in not accepted " + response);

            SendCommand("PASS " + password + "\r\n");
            response = reader.ReadLine();
            if (!response.StartsWith("+OK"))
                throw new Pop3ClientException("Password not accepted " + response);
        }

        private void SendCommand(string p)
        {
            byte[] buffer = Encoding.ASCII.GetBytes(p);
            stream.Write(buffer, 0, buffer.Length);
        }

        public Dictionary<int, int> GetMessageSizes()
        {
            SendCommand("LIST\r\n");
            string response = reader.ReadLine();
            if (!response.StartsWith("+OK"))
                throw new Pop3ClientException("List emails not accepted: " + response);

            Dictionary<int, int> returnValue = new Dictionary<int, int>();
            while (!(response = reader.ReadLine()).Equals("."))<commit_msg>Pop3Client ASCII -> UTF8<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;

namespace Signum.Engine.Mailing.Pop3
{
    public class Pop3Client : IDisposable
    {
        string userName;
        string password;
        string server;
        bool useSSL;

        TcpClient connection;
        Stream stream;
        StreamReader reader;

        public Pop3Client(string userName, string password, string server, int port, bool useSSL)
        {
            this.userName = userName;
            this.password = password;
            this.server = server;
            this.useSSL = useSSL;
        }

        public void Connect()
        {
            connection = new TcpClient();
            connection.Connect(server, 110);

            if (useSSL)
            {
                var sslStream = new SslStream(connection.GetStream(), false);
                sslStream.AuthenticateAsClient(server);
                stream = sslStream;
            }
            else
            {
                stream = connection.GetStream();
            }

            reader = new StreamReader(stream, Encoding.UTF8);

            string response = reader.ReadLine();
            if (!response.StartsWith("+OK"))
                throw new Pop3ClientException("Not ready for Log-in " + response);

            SendCommand("USER " + userName + "\r\n");
            response = reader.ReadLine();
            if (!response.StartsWith("+OK"))
                throw new Pop3ClientException("Log-in not accepted " + response);

            SendCommand("PASS " + password + "\r\n");
            response = reader.ReadLine();
            if (!response.StartsWith("+OK"))
                throw new Pop3ClientException("Password not accepted " + response);
        }

        private void SendCommand(string p)
        {
            byte[] buffer = Encoding.ASCII.GetBytes(p);
            stream.Write(buffer, 0, buffer.Length);
        }

        public Dictionary<int, int> GetMessageSizes()
        {
            SendCommand("LIST\r\n");
            string response = reader.ReadLine();
            if (!response.StartsWith("+OK"))
                throw new Pop3ClientException("List emails not accepted: " + response);

            Dictionary<int, int> returnValue = new Dictionary<int, int>();
            while (!(response = reader.ReadLine()).Equals("."))
{'fraction_non_alphanumeric': 0.10573403822692151, 'fraction_numerical': 0.005083367222448149, 'mean_word_length': 1.8286371477860839, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '5520934', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1224, 'n_words': 305}