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", "DCC ERROR, no Router/Firewall IP Address specified in DCC Settings", 4, 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.ListingTag = RandomListingTag(); 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); string fname = dcc.FileName.Replace(' ', '_'); // strip spaces from filename //get the file from the path fname = Path.GetFileName(fname); 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); }
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", "DCC Passive File Received Failed : DCC Receive Path does not exists", 4, 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; 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(); } }
/// <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); } }
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; } } } }
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", "DCC File Received Failed : DCC Receive Path does not exists", 4, 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 { 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 connection.SendData("PRIVMSG " + nick + " :\x0001DCC RESUME \"" + dcc.FileName + "\" " + 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.StartFileSize = dcc.TotalBytesRead; dccFiles.Add(dcc); AddDCCFile(dcc); UpdateDCCFileStatus(dcc, "W-RESUME"); } 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"); 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", "DCC File Receive Error from " + dcc.Nick + " for file " + dcc.FileName + " : " + se.Message, 4, true); //AddDCCFile(dcc); dcc.Errored = true; dcc.Finished = false; dcc.StartTime = 0; UpdateDCCFileProgress(dcc); } }
/// <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 = dcc.TotalBytesRead + "/" + 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; System.Diagnostics.Debug.WriteLine(elasped + ":" + (dcc.TotalBytesRead - dcc.StartFileSize) + ":" + bps); lvi.SubItems[3].Text = bps.ToString() + " b/s"; if (bps > 0) { //calculate speed and set to bytes/kb/mb/gb } } else lvi.SubItems[3].Text = "0 b/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"; else if (dcc.Errored) lvi.SubItems[5].Text = "ERROR"; else if (dcc.Resume) lvi.SubItems[5].Text = "Resuming"; else lvi.SubItems[5].Text = "Downloading"; return; } } } }
/// <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.Flush(); 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(); 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; } } catch { } dccFiles.Remove(dcc); return; } } } }
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.Thread.ManagedThreadId.ToString()) { lvi.SubItems[2].Text = dcc.TotalBytesRead + "/" + dcc.FileSize; if (dcc.TotalBytesRead.ToString() == dcc.FileSize) lvi.SubItems[3].Text = "Completed"; else lvi.SubItems[3].Text = "Downloading"; return; } } } }
/// <summary> /// Add the specific Download File/Data to the DCC File List /// </summary> /// <param name="dcc"></param> private void AddDCCFile(DccFileStruct dcc) { if (this.InvokeRequired) { AddDCCFileDelegate add = new AddDCCFileDelegate(AddDCCFile); this.Invoke(add, new object[] { dcc }); } else { ListViewItem lvi = new ListViewItem(dcc.FileName); lvi.SubItems.Add(dcc.Nick); lvi.SubItems.Add(dcc.FileSize); lvi.SubItems.Add("Status"); lvi.SubItems.Add(dcc.Style); lvi.Tag = dcc.Thread.ManagedThreadId; dccFileList.Items.Add(lvi); } }
internal void StartDCCFile(IRCConnection connection, string nick, string host, string ip, string port, string file, string fileSize) { DccFileStruct dcc = new DccFileStruct(); dcc.FileName = file; dcc.FileSize = fileSize; dcc.Socket = new TcpClient(); dcc.Nick = nick; dcc.Host = host; dcc.Style = "Download"; IPAddress ipAddr = LongToIPAddress(ip); IPEndPoint ep = new IPEndPoint(ipAddr, Convert.ToInt32(port)); dcc.IPAddress = ipAddr; try { dcc.Socket.Connect(ep); if (dcc.Socket.Connected) { string dccPath = FormMain.Instance.IceChatOptions.DCCReceiveFolder; //check if the file exists if (File.Exists(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName)) { //file exists // 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; //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); } }