// Decompiled with JetBrains decompiler
// Type: Meebey.SmartIrc4net.DccChat
// Assembly: svchost, Version=3.2.4.0, Culture=neutral, PublicKeyToken=null
// MVID: 8F51F928-E1DB-4815-857F-7D580385B815
// Assembly location: C:\Users\Administrateur\Downloads\Virusshare-00000-msil\Backdoor.MSIL.IrcBot.lp-4dd7df88cffbf85a5894bc1b38660fa613c24b0e6c004ac00b2b119c03a35d3f.exe

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Meebey.SmartIrc4net
{
  public class DccChat : DccConnection
  {
    private StreamReader _sr;
    private StreamWriter _sw;
    private int _lines;

    public int Lines => this._lines;

    internal DccChat(
      IrcFeatures irc,
      string user,
      IPAddress externalIpAdress,
      bool passive,
      Priority priority)
    {
      this.Irc = irc;
      this.ExternalIPAdress = externalIpAdress;
      this.User = user;
      if (passive)
      {
        irc.SendMessage(SendType.CtcpRequest, user, "DCC CHAT chat " + this.HostToDccInt(externalIpAdress).ToString() + " 0 " + (object) this.session, priority);
        this.Disconnect();
      }
      else
      {
        this.DccServer = new TcpListener(new IPEndPoint(IPAddress.Any, 0));
        this.DccServer.Start();
        this.LocalEndPoint = (IPEndPoint) this.DccServer.LocalEndpoint;
        irc.SendMessage(SendType.CtcpRequest, user, "DCC CHAT chat " + this.HostToDccInt(externalIpAdress).ToString() + " " + (object) this.LocalEndPoint.Port, priority);
      }
    }

    internal DccChat(IrcFeatures irc, IPAddress externalIpAdress, CtcpEventArgs e)
    {
      this.Irc = irc;
      this.ExternalIPAdress = externalIpAdress;
      this.User = e.Data.Nick;
      if (e.Data.MessageArray.Length > 4)
      {
        long result1;
        bool flag1 = long.TryParse(e.Data.MessageArray[3], out result1);
        int result2;
        bool flag2 = int.TryParse(this.FilterMarker(e.Data.MessageArray[4]), out result2);
        if (e.Data.MessageArray[2] == "chat" && flag1 && flag2)
        {
          this.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(this.DccIntToHost(result1)), result2);
          if (e.Data.MessageArray.Length > 5 && e.Data.MessageArray[5] != "T")
          {
            this.AcceptRequest();
            return;
          }
          this.DccChatRequestEvent(new DccEventArgs((DccConnection) this));
          return;
        }
        irc.SendMessage(SendType.CtcpReply, e.Data.Nick, "ERRMSG DCC Chat Parameter Error");
      }
      else
        irc.SendMessage(SendType.CtcpReply, e.Data.Nick, "ERRMSG DCC Chat not enough parameters");
      this.isValid = false;
    }

    internal override void InitWork(object stateInfo)
    {
      if (!this.Valid)
        return;
      if (this.DccServer != null)
      {
        this.Connection = this.DccServer.AcceptTcpClient();
        this.RemoteEndPoint = (IPEndPoint) this.Connection.Client.RemoteEndPoint;
        this.DccServer.Stop();
        this.isConnected = true;
      }
      else
      {
        while (!this.isConnected)
        {
          Thread.Sleep(500);
          if (this.reject)
          {
            this.isValid = false;
            return;
          }
        }
      }
      this.DccChatStartEvent(new DccEventArgs((DccConnection) this));
      this._sr = new StreamReader((Stream) this.Connection.GetStream(), this.Irc.Encoding);
      this._sw = new StreamWriter((Stream) this.Connection.GetStream(), this.Irc.Encoding);
      this._sw.AutoFlush = true;
      string messageLine;
      while ((messageLine = this._sr.ReadLine()) != null && this.isConnected)
      {
        this.DccChatReceiveLineEvent(new DccChatEventArgs((DccConnection) this, messageLine));
        ++this._lines;
      }
      this.isValid = false;
      this.isConnected = false;
      this.DccChatStopEvent(new DccEventArgs((DccConnection) this));
    }

    public bool AcceptRequest()
    {
      if (this.isConnected)
        return false;
      try
      {
        if (this.RemoteEndPoint.Port == 0)
        {
          this.DccServer = new TcpListener(new IPEndPoint(IPAddress.Any, 0));
          this.DccServer.Start();
          this.LocalEndPoint = (IPEndPoint) this.DccServer.LocalEndpoint;
          this.Irc.SendMessage(SendType.CtcpRequest, this.User, "DCC CHAT chat " + this.HostToDccInt(this.ExternalIPAdress).ToString() + " " + (object) this.LocalEndPoint.Port);
        }
        else
        {
          this.Connection = new TcpClient();
          this.Connection.Connect(this.RemoteEndPoint);
          this.isConnected = true;
        }
        return true;
      }
      catch (Exception ex)
      {
        this.isValid = false;
        this.isConnected = false;
        return false;
      }
    }

    public void WriteLine(string message)
    {
      if (!this.isConnected)
        throw new NotConnectedException("DCC Chat is not Connected");
      this._sw.WriteLine(message);
      ++this._lines;
      this.DccChatSentLineEvent(new DccChatEventArgs((DccConnection) this, message));
    }
  }
}
