Ejemplo n.º 1
0
        /// <summary>
        /// Add the specific Download File/Data to the DCC File List
        /// </summary>
        private void AddDCCFile(DccFileStruct dcc)
        {
            if (this.InvokeRequired)
            {
                AddDCCFileDelegate add = new AddDCCFileDelegate(AddDCCFile);
                this.Invoke(add, new object[] { dcc });
            }
            else
            {
                if (dcc.Resume)
                {
                    //try and find a match and continue on with that one
                    foreach (ListViewItem l in dccFileList.Items)
                    {
                        if (l.Text == dcc.FileName && l.SubItems[1].Text == dcc.Nick && l.SubItems[7].Text == dcc.Connection.ServerSetting.ID.ToString())
                        {
                            //close enough for a match, use this file
                            l.Tag = dcc.ListingTag;
                            return;
                        }
                    }
                }

                ListViewItem lvi = new ListViewItem(dcc.FileName);
                lvi.SubItems.Add(dcc.Nick);
                lvi.SubItems.Add(dcc.FileSize.ToString());
                lvi.SubItems.Add("");   //speed blank initially

                if (dcc.Resume)
                    lvi.SubItems.Add("Resuming");
                else
                    lvi.SubItems.Add("Status");

                lvi.SubItems.Add(dcc.Style);
                lvi.SubItems.Add(dcc.Connection.ServerSetting.ID.ToString());
                lvi.Tag = dcc.ListingTag;
                lvi.ToolTipText = dcc.FileName;

                dccFiles.Add(dcc);
                dccFileList.Items.Add(lvi);
            }
        }
Ejemplo n.º 2
0
        internal void RequestDCCFile(IRCConnection connection , string nick, string file)
        {
            //send out a dccfile request
            string localIP = "";
            if (FormMain.Instance.IceChatOptions.DCCLocalIP != null && FormMain.Instance.IceChatOptions.DCCLocalIP.Length > 0)
            {
                localIP = IPAddressToLong(IPAddress.Parse(FormMain.Instance.IceChatOptions.DCCLocalIP)).ToString();
            }
            else
            {
                if (connection.ServerSetting.LocalIP == null || connection.ServerSetting.LocalIP.ToString().Length == 0)
                {
                    //error. no local IP found
                    FormMain.Instance.WindowMessage(connection, "Console", "\x000304DCC ERROR, no Router/Firewall IP Address specified in DCC Settings", "", true);
                    return;
                }
                else
                {
                    localIP = IPAddressToLong(connection.ServerSetting.LocalIP).ToString();
                }
            }

            Random port = new Random();
            int p = port.Next(FormMain.Instance.IceChatOptions.DCCPortLower, FormMain.Instance.IceChatOptions.DCCPortUpper);

            //DccFileStruct dcc = new DccFileStruct();
            DccFileStruct dcc = new DccFileStruct();
            dcc.FileStream = new FileStream(file, FileMode.Open);

            FileInfo f = new FileInfo(file);
            dcc.FileSize = (uint)f.Length;
            dcc.StartFileSize = 0;

            dcc.FileName = file;
            dcc.Nick = nick;
            dcc.Style = "Upload";
            dcc.Connection = connection;
            dcc.Port = p.ToString();
            dcc.LocalIP = localIP;

            dcc.ListingTag = RandomListingTag();
            string fname = dcc.FileName.Replace(' ', '_'); // strip spaces from filename
            //get the file from the path
            fname = Path.GetFileName(fname);
            dcc.SendFileName = fname;

            dcc.ListenerSocket = new TcpListener(new IPEndPoint(IPAddress.Any, Convert.ToInt32(p)));
            dcc.ListenerThread = new Thread(new ParameterizedThreadStart(ListenForConnection));
            dcc.ListenerThread.Name = "DCCListenerThread";
            dcc.ListenerThread.Start(dcc);

            //dcc.Connection.SendData("PRIVMSG " + dcc.Nick + " :DCC SEND " + fname + " " + localIP + " " + p.ToString() + " " + dcc.FileSize.ToString() + "");

            dcc.timeoutTimer = new System.Timers.Timer();
            dcc.timeoutTimer.Interval = 1000 * FormMain.Instance.IceChatOptions.DCCChatTimeOut;
            dcc.timeoutTimer.Elapsed += new System.Timers.ElapsedEventHandler(timeoutTimer_Elapsed);
            dcc.timeoutTimer.Start();

            AddDCCFile(dcc);
        }
Ejemplo n.º 3
0
        internal void StartDCCPassive(IRCConnection connection, string nick, string host, string ip, string file, uint fileSize, string id)
        {
            //open a new dcc listening port, and send back to the client
            System.Diagnostics.Debug.WriteLine("start passive dcc - open listener:" + id);
            DccFileStruct dcc = new DccFileStruct();
            dcc.FileName = file;
            dcc.FileSize = fileSize;
            dcc.StartFileSize = 0;
            dcc.Nick = nick;
            dcc.Host = host;
            dcc.Connection = connection;
            dcc.Ip = ip;
            dcc.passiveID = id;

            dcc.Style = "Passive";

            //pick a random incoming port
            Random port = new Random();
            int p = port.Next(FormMain.Instance.IceChatOptions.DCCPortLower, FormMain.Instance.IceChatOptions.DCCPortUpper);
            dcc.Port = p.ToString();

            //create a random number for a tag
            dcc.ListingTag = RandomListingTag();

            try
            {
                string dccPath = FormMain.Instance.IceChatOptions.DCCReceiveFolder;
                //check to make sure the folder exists
                if (!Directory.Exists(dccPath))
                {
                    //add a folder browsing dialog here
                    FolderBrowserDialog fbd = new FolderBrowserDialog();

                    if (fbd.ShowDialog() == DialogResult.OK)
                        dccPath = fbd.SelectedPath;
                    else
                    {
                        //no folder selected, out we go
                        System.Diagnostics.Debug.WriteLine("PASSIVE No folder selected, non-existant dcc receive folder");
                        FormMain.Instance.WindowMessage(connection, "Console", "\x000304DCC Passive File Received Failed : DCC Receive Path does not exists", "", true);
                        return;
                    }
                }

                //check if the file exists
                if (File.Exists(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName))
                {
                    //check the local file size and compare to what is being sent
                    FileInfo fi = new FileInfo(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName);
                    if (fi.Length <= dcc.FileSize)
                    {

                        System.Diagnostics.Debug.WriteLine("PASSIVE appending file:" + fi.Length + ":" + dcc.FileSize + ":" + connection.IsFullyConnected);
                        //send DCC RESUME
                        //wait for a DCC ACCEPT from client, and start resume on this port
                        connection.SendData("PRIVMSG " + nick + " :\x0001DCC RESUME \"" + dcc.FileName + "\" " + dcc.Port + " " + fi.Length.ToString() + "\x0001");
                        dcc.Resume = true;
                        dcc.TotalBytesRead = (uint)fi.Length;
                        dcc.FileStream = new FileStream(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName, FileMode.Append);
                        dcc.Path = dccPath;
                        dcc.StartFileSize = dcc.TotalBytesRead;
                        System.Diagnostics.Debug.WriteLine(dccFiles.Count + ":" + dccFileList.Items.Count);

                        dccFiles.Add(dcc);
                        return;
                    }
                    else
                    {
                        //file exists, and already complete // set a new filename adding [#] to the end of the fielname
                        int extPos = dcc.FileName.LastIndexOf('.');
                        if (extPos == -1)
                        {
                            int i = 0;
                            do
                            {
                                i++;
                            } while (File.Exists(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName + "(" + i.ToString() + ")"));
                            dcc.FileName += "(" + i.ToString() + ")";
                        }
                        else
                        {
                            string fileName = dcc.FileName.Substring(0, extPos);
                            string ext = dcc.FileName.Substring(extPos + 1);
                            int i = 0;
                            do
                            {
                                i++;
                            } while (File.Exists(dccPath + System.IO.Path.DirectorySeparatorChar + fileName + "(" + i.ToString() + ")." + ext));
                            dcc.FileName = fileName + "(" + i.ToString() + ")." + ext;
                        }
                    }
                }

                dcc.FileStream = new FileStream(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName, FileMode.Create);
                dcc.Path = dccPath;

                dcc.PassiveSocket = new TcpListener(new IPEndPoint(IPAddress.Any, Convert.ToInt32(p)));
                dcc.PassiveThread = new Thread(new ParameterizedThreadStart(StartPassiveSocket));
                dcc.PassiveThread.Name = "DCCPassiveThread";
                dcc.PassiveThread.Start(dcc);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("passive dcc file error:" + ex.Message);

                dcc.FileStream.Close();
            }
        }
Ejemplo n.º 4
0
        internal void StartDCCFile(IRCConnection connection, string nick, string host, string ip, string port, string file, uint fileSize)
        {
            DccFileStruct dcc = new DccFileStruct();
            dcc.FileName = file;
            dcc.FileSize = fileSize;
            dcc.StartFileSize = 0;
            dcc.Socket = new TcpClient();
            dcc.Nick = nick;
            dcc.Host = host;
            dcc.Style = "Download";
            dcc.Connection = connection;
            dcc.Port = port;
            dcc.Ip = ip;

            //create a random number for a tag
            dcc.ListingTag = RandomListingTag();

            try
            {
                string dccPath = FormMain.Instance.IceChatOptions.DCCReceiveFolder;
                //check to make sure the folder exists
                if (!Directory.Exists(dccPath))
                {
                    //add a folder browsing dialog here
                    FolderBrowserDialog fbd = new FolderBrowserDialog();

                    if (fbd.ShowDialog() == DialogResult.OK)
                        dccPath = fbd.SelectedPath;
                    else
                    {
                        //no folder selected, out we go
                        System.Diagnostics.Debug.WriteLine("No folder selected, non-existant dcc receive folder");
                        FormMain.Instance.WindowMessage(connection, "Console", "\x000304DCC File Received Failed : DCC Receive Path does not exists", "", true);
                        return;
                    }
                }

                dcc.Path = dccPath;

                //check if the file exists
                if (File.Exists(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName))
                {
                    //check the local file size and compare to what is being sent
                    FileInfo fi = new FileInfo(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName);
                    if (fi.Length < dcc.FileSize)
                    {
                        try
                        {
                            //appending file:12320768:87618872:True
                            System.Diagnostics.Debug.WriteLine("appending file:" + fi.Length + ":" + dcc.FileSize + ":" + connection.IsFullyConnected);
                            //send DCC RESUME
                            //wait for a DCC ACCEPT from client, and start resume on this port
                            //System.Diagnostics.Debug.WriteLine("PRIVMSG " + nick + " :DCC RESUME \"" + dcc.FileName + "\" " + port + " " + fi.Length.ToString());
                            //connection.SendData("PRIVMSG " + nick + " :\x0001DCC RESUME \"" + dcc.FileName + "\" " + port + " " + fi.Length.ToString() + "\x0001");
                            connection.SendData("PRIVMSG " + nick + " :\x0001DCC RESUME file.ext " + port + " " + fi.Length.ToString() + "\x0001");

                            bool foundMatch = false;

                            if (dccFiles.Count > 0)
                            {
                                foreach (DccFileStruct d in dccFiles)
                                {
                                    //System.Diagnostics.Debug.WriteLine(d.FileName);
                                    if (d.Port == port)
                                    {
                                        System.Diagnostics.Debug.WriteLine("found a match on port " + port);
                                        foundMatch = true;
                                    }
                                }
                            }
                            if (!foundMatch)
                            {
                                dcc.Resume = true;
                                dcc.TotalBytesRead = (uint)fi.Length;
                                dcc.FileStream = new FileStream(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName, FileMode.Append);
                                dcc.StartFileSize = dcc.TotalBytesRead;

                                System.Diagnostics.Debug.WriteLine(dccFiles.Count + ":" + dccFileList.Items.Count);

                                dccFiles.Add(dcc);

                                AddDCCFile(dcc);
                                UpdateDCCFileStatus(dcc, "W-RESUME");

                            }

                            /*

                            bool foundMatch = false;
                            if (dccFiles.Count > 0)
                            {
                                foreach (DccFileStruct d in dccFiles)
                                {
                                    //System.Diagnostics.Debug.WriteLine(d.FileName);
                                    if (d.Port == port)
                                    {
                                        System.Diagnostics.Debug.WriteLine("found a match on port " + port);
                                        foundMatch = true;
                                        d.Resume = true;
                                        d.TotalBytesRead = (uint)fi.Length;
                                        d.StartFileSize = (uint)fi.Length;

                                        d.Socket = null;
                                        d.Socket = new TcpClient();

                                        d.FileStream.Close();
                                        d.FileStream = null;
                                        d.FileStream = new FileStream(dccPath + System.IO.Path.DirectorySeparatorChar + file, FileMode.Append);
                                        UpdateDCCFileStatus(d, "W-RESUME");

                                        d.Thread = new Thread(new ParameterizedThreadStart(ConnectDCC));
                                        d.Thread.Name = "StartDCCFileThreadResume";
                                        d.Thread.Start(d);

                                        break;
                                    }
                                }

                            }

                            if (!foundMatch)
                            {
                                dcc.Resume = true;
                                dcc.TotalBytesRead = (uint)fi.Length;
                                dcc.FileStream = new FileStream(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName, FileMode.Append);
                                dcc.StartFileSize = dcc.TotalBytesRead;

                                System.Diagnostics.Debug.WriteLine(dccFiles.Count + ":" + dccFileList.Items.Count);

                                dccFiles.Add(dcc);

                                AddDCCFile(dcc);
                                UpdateDCCFileStatus(dcc, "W-RESUME");

                                dcc.Thread = new Thread(new ParameterizedThreadStart(ConnectDCC));
                                dcc.Thread.Name = "StartDCCFileThread";
                                dcc.Thread.Start(dcc);
                            }

                            //appending file:62103552:87618872:True
                            //'PRIVMSG Snerf8 :DCC RESUME "epson13792.exe" 5010 62103552
                            //2:1
                            //A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll
                            //InvalidArgument=Value of '7' is not valid for 'index'.
                            //Parameter name: index
                            */

                        }
                        catch (Exception e)
                        {
                            System.Diagnostics.Debug.WriteLine(e.Message);
                        }
                        return;
                    }
                    else
                    {
                        //file exists, and already complete // set a new filename adding [#] to the end of the fielname
                        int extPos = dcc.FileName.LastIndexOf('.');
                        if (extPos == -1)
                        {
                            int i = 0;
                            do
                            {
                                i++;
                            } while (File.Exists(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName + "(" + i.ToString() + ")"));
                            dcc.FileName += "(" + i.ToString() + ")";
                        }
                        else
                        {
                            string fileName = dcc.FileName.Substring(0, extPos);
                            string ext = dcc.FileName.Substring(extPos + 1);
                            int i = 0;
                            do
                            {
                                i++;
                            } while (File.Exists(dccPath + System.IO.Path.DirectorySeparatorChar + fileName + "(" + i.ToString() + ")." + ext));
                            dcc.FileName = fileName + "(" + i.ToString() + ")." + ext;
                        }
                    }
                }

                System.Diagnostics.Debug.WriteLine("next part of start dcc:" + dcc.FileName);
                AddDCCFile(dcc);
                UpdateDCCFileStatus(dcc, "Waiting");

                dcc.Thread = new Thread(new ParameterizedThreadStart(ConnectDCC));
                dcc.Thread.Name = "StartDCCFileThread";
                dcc.Thread.Start(dcc);

                /*
                dcc.Socket.Connect(ep);
                if (dcc.Socket.Connected)
                {
                    dcc.FileStream = new FileStream(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName, FileMode.Create);
                    dcc.Path = dccPath;

                    //start the thread to get the data
                    dcc.Thread = new Thread(new ParameterizedThreadStart(GetDCCData));
                    dcc.Thread.Start(dcc);

                }
                */
            }
            catch (SocketException se)
            {
                System.Diagnostics.Debug.WriteLine("DCC file connection error:" + se.Message);
                FormMain.Instance.WindowMessage(connection, "Console", "\x000304DCC File Receive Error from " + dcc.Nick + " for file " + dcc.FileName + " : " + se.Message, "", true);

                //AddDCCFile(dcc);
                dcc.Errored = true;
                dcc.Finished = false;
                dcc.StartTime = 0;
                UpdateDCCFileProgress(dcc);

            }
        }
Ejemplo n.º 5
0
 private void UpdateDCCFileStatus(DccFileStruct dcc, string value)
 {
     if (this.InvokeRequired)
     {
         UpdateDCCFileStatusDelegate u = new UpdateDCCFileStatusDelegate(UpdateDCCFileStatus);
         this.Invoke(u, new object[] { dcc, value });
     }
     else
     {
         foreach (ListViewItem lvi in dccFileList.Items)
         {
             if (lvi.Tag.ToString() == dcc.ListingTag.ToString())
                 lvi.SubItems[5].Text = value;
         }
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Show the updated file progress in the DCC File List
        /// </summary>
        private void UpdateDCCFileProgress(DccFileStruct dcc)
        {
            if (this.InvokeRequired)
            {
                UpdateDCCFileProgressDelegate u = new UpdateDCCFileProgressDelegate(UpdateDCCFileProgress);
                this.Invoke(u, new object[] { dcc });
            }
            else
            {
                foreach (ListViewItem lvi in dccFileList.Items)
                {
                    if (lvi.Tag.ToString() == dcc.ListingTag.ToString())
                    {
                        lvi.SubItems[2].Text = FormatBytes(dcc.TotalBytesRead) + "/" + FormatBytes(dcc.FileSize);

                        //calculate the bp/sec
                        long elasped = DateTime.Now.Ticks - dcc.StartTime;

                        if (elasped > 0 && (dcc.TotalBytesRead > dcc.StartFileSize))
                        {
                            float b = (elasped / 10000000f);
                            float bps = (dcc.TotalBytesRead - dcc.StartFileSize) / b;

                            lvi.SubItems[3].Text = FormatBytes(bps) +"/s";
                        }
                        else
                            lvi.SubItems[3].Text = "0 Bytes/s";

                        if (dcc.StartTime == 0)
                            lvi.SubItems[4].Text = "";
                        else
                            lvi.SubItems[4].Text = GetDurationTicks(elasped);

                        if (dcc.TotalBytesRead == dcc.FileSize || dcc.Finished)
                        {
                            lvi.SubItems[5].Text = "Completed";

                            //the dcc file download is completed
                            PluginArgs args = new PluginArgs(dcc.Connection);
                            args.fileName = dcc.FileName;
                            args.fileSize = dcc.FileSize;
                            args.dccPort = dcc.Ip;

                            foreach (Plugin p in FormMain.Instance.LoadedPlugins)
                            {
                                IceChatPlugin ipc = p as IceChatPlugin;
                                if (ipc != null)
                                {
                                    if (ipc.plugin.Enabled == true)
                                        ipc.plugin.DCCFileComplete(args);
                                }
                            }
                        }
                        else if (dcc.Errored)
                        {
                            if (dcc.Incomplete == true)
                                lvi.SubItems[5].Text = "INCOMPLETE";
                            else
                                lvi.SubItems[5].Text = "ERROR";

                            //the dcc file download has errored
                            PluginArgs args = new PluginArgs(dcc.Connection);
                            args.fileName = dcc.FileName;
                            args.fileSize = dcc.FileSize;
                            args.dccPort = dcc.Ip;

                            foreach (Plugin p in  FormMain.Instance.LoadedPlugins)
                            {
                                IceChatPlugin ipc = p as IceChatPlugin;
                                if (ipc != null)
                                {
                                    if (ipc.plugin.Enabled == true)
                                        ipc.plugin.DCCFileError(args);
                                }
                            }
                        }
                        else if (dcc.Resume)
                            lvi.SubItems[5].Text = "Resuming";
                        else
                            lvi.SubItems[5].Text = "Downloading";

                        return;
                    }
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Remove the specific Download File/Data from the DCC File List
        /// </summary>
        private void RemoveDCCFile(DccFileStruct dcc)
        {
            if (this.InvokeRequired)
            {
                RemoveDCCFileDelegate remove = new RemoveDCCFileDelegate(RemoveDCCFile);
                this.Invoke(remove, new object[] { dcc });
            }
            else
            {
                foreach (ListViewItem l in dccFileList.Items)
                {
                    if (l.Tag.ToString() == dcc.ListingTag.ToString())
                    {
                        //close enough for a match, use this file
                        dccFileList.Items.Remove(l);
                        //remove the item from dccFiles
                        try
                        {
                            if (dcc.FileStream != null)
                            {
                                dcc.FileStream.Close();
                            }

                            if (dcc.Thread != null)
                                if (dcc.Thread.IsAlive)
                                    dcc.Thread.Abort();

                            if (dcc.Socket != null)
                            {
                                if (dcc.Socket.Connected)
                                    dcc.Socket.Close();

                                dcc.Socket = null;
                            }

                            if (dcc.ListenerSocket != null)
                            {
                                dcc.ListenerSocket.Stop();
                                dcc.ListenerSocket = null;
                            }

                            if (dcc.ListenerThread != null)
                            {
                                //dcc.keepListening = false;
                            }

                            if (dcc.timeoutTimer != null)
                            {
                                dcc.timeoutTimer.Stop();
                                dcc.timeoutTimer = null;
                            }

                            dccFiles.Remove(dcc);

                        }
                        catch (SocketException se)
                        {
                            System.Diagnostics.Debug.WriteLine("remove dcc socket error:" + se.Message);
                        }
                        catch (Exception ee)
                        {
                            System.Diagnostics.Debug.WriteLine("remove dcc error:" + ee.Message);
                        }

                        return;
                    }
                }
            }
        }