public void AddFile(sFile file, Peer peer) { int idxRow = g_files.Rows.Add(new object[] { file.name, file.StringSize(), file.etat, ((file.send) ? "Send" : "Receive"), peer.Pseudo }); g_files.Rows[idxRow].Tag = new FileItem { file = file, peer = peer }; }
public void ReceiveFile(sFile f, Byte[] data) { try { FileStream fs = new FileStream(Program.incomingFolder + f.name, FileMode.OpenOrCreate); fs.Position = f.position; fs.Write(data, 0, data.Length); fs.Close(); } catch (Exception e) { Program.Log("ReceiveFile - " + e.Message); } }
private int GetRowIndexBySFile(sFile file) { bool found = false; int idx = 0; for (int n = g_files.Rows.Count; idx < n && !found; idx++) { sFile tmpfile = (sFile)g_files.Rows[idx].Tag; if (tmpfile.hash == file.hash) { found = true; } } return((found) ? --idx : -1); }
private void HandleData(Data myData) { if (myData.type == DataType.Announce) { DataAnnounce d = (DataAnnounce)myData; IPAddress ip = d.ip; int port = d.port; Program.myForm.Connect(ip, port); } else if (myData.type == DataType.Handshake) { DataHandshake d = (DataHandshake)myData; id = d.id; pseudo = d.pseudo; isEncrypted = d.encrypted; if (d.encrypted && Program.encrypted) { InitKey(); } Program.tabChannel[Program.currentChannel].tabPeer.Add(this); Program.myForm.WriteNotice("Nouveau peer : " + pseudo + ((d.encrypted && Program.encrypted) ? "" : "(liaison non chiffrée)"), Chat.MessageType.Main); Program.myForm.RefreshPeersList(); } else if (myData.type == DataType.Key) { if (Program.encrypted) { DataKey d = (DataKey)myData; if (d.init) { rsa.publickey = d.rsaPublicKey; aes.RegenerateKey(); aes.receiveKey = aes.Key; aes.receiveIV = aes.IV; SendData(new DataKey { aesKey = rsa.Encrypt(aes.receiveKey), IV = rsa.Encrypt(aes.receiveIV), init = false }); } else { aes.sendKey = rsa.DecryptToBytes(d.aesKey); aes.sendIV = rsa.DecryptToBytes(d.IV); } } } else if (myData.type == DataType.Message) { if (!isIgnored) { DataMessage d = (DataMessage)myData; String msg = d.msg; if (isEncrypted != d.encrypted) { isEncrypted = d.encrypted; Program.myForm.RefreshPeersList(); } Program.Log("received : " + msg); if (d.encrypted) { msg = aes.DecryptToString(msg, aes.receiveKey, aes.receiveIV); } //msg = rsa.DecryptToString(msg); //en cas de mp, remplacement de l'id local par celui du peer if (d.channel == Program.id) { d.channel = id; } Program.myForm.WriteText(pseudo, msg, d.channel, Chat.MessageType.Channel | ((msg.Contains(Program.pseudo) ? Chat.MessageType.Mention : Chat.MessageType.Message))); } } else if (myData.type == DataType.Voice) { if (!isIgnored) { DataVoice d = (DataVoice)myData; if (Program.myForm.IsPeerInCurrentChannel(id)) { Byte[] buffer = d.buffer; if (isEncrypted) { buffer = aes.DecryptToBytes(buffer, aes.receiveKey, aes.receiveIV); } Program.myForm.snd.Play(buffer); } else { int idxChan = Program.myForm.GetChanIndexById(d.channel); if (idxChan != -1) { Program.tabChannel[idxChan].PlayingSound(); } } } } else if (myData.type == DataType.Disconnect) { Program.myForm.WriteNotice(pseudo + " vient de se déconnecter", Chat.MessageType.Channel | Chat.MessageType.Main); Kill(); } else if (myData.type == DataType.ChangeNick) { DataChangeNick d = (DataChangeNick)myData; String newpseudo = d.newPseudo; int idxChan = Program.myForm.GetChanIndexById(id); if (idxChan > -1) { Program.myForm.TabChatTitle(idxChan, newpseudo); } foreach (Channel c in Program.tabChannel) { if (c.tabPeer.IndexOf(this) > -1) { Program.myForm.WriteNotice(pseudo + " s'appelle maintenant " + newpseudo, c.name, Chat.MessageType.Main | Chat.MessageType.Channel); } } pseudo = newpseudo; Program.myForm.RefreshPeersList(); } else if (myData.type == DataType.Channel) { DataChannel d = (DataChannel)myData; int idxChan = Program.myForm.GetChanIndexById(d.channel); if (idxChan != -1) { if (Program.tabChannel[idxChan].isPrivate && !Program.tabChannel[idxChan].tabPeer.Contains(this)) { return; } if (d.option.HasFlag(ChannelOption.Info)) { if (d.idPeers != null) { for (int i = 0, n = d.idPeers.Length; i < n; i++) { int idxPeer = Program.myForm.GetPeerIndexById(d.idPeers[i]); if (idxPeer > -1) { if (!Program.tabChannel[idxChan].tabPeer.Contains(Program.tabPeer[idxPeer])) { Program.tabChannel[idxChan].tabPeer.Add(Program.tabPeer[idxPeer]); } } } } if (!Program.tabChannel[idxChan].tabPeer.Contains(this)) { Program.tabChannel[idxChan].tabPeer.Add(this); } } else if (d.option.HasFlag(ChannelOption.Join)) { IEnumerable <String> idPeers = from p in Program.tabPeer select p.id; Program.myForm.WriteNotice(pseudo + " a rejoint le channel", d.channel, Chat.MessageType.Channel); SendData(new DataChannel { channel = d.channel, idPeers = idPeers.ToArray(), option = ChannelOption.Info }); Program.tabChannel[idxChan].tabPeer.Add(this); } else if (d.option.HasFlag(ChannelOption.Leave)) { Program.myForm.WriteNotice(pseudo + " a quitté le channel", d.channel, Chat.MessageType.Channel); Program.tabChannel[idxChan].tabPeer.Remove(this); } } } else if (myData.type == DataType.Ping) { DataPing d = (DataPing)myData; if (!d.pong) { SendData(new DataPing { pong = true }); } else { nbPingTries = 0; waitingForPong = false; if (!isActive) { Program.myForm.WriteNotice(pseudo + " back to life", Chat.MessageType.Channel | Chat.MessageType.Main); } isActive = true; } } else if (myData.type == DataType.File) { DataFile d = (DataFile)myData; sFile f = d.fileinfo; int idxFile = tabFile.IndexOf(f); if (f.etat.HasFlag(FileStatus.Waiting)) { f.send = false; tabFile.Add(f); Program.myForm.RefreshFilesList(); Program.myForm.WriteNotice(pseudo + " veut vous envoyer " + f.name + " (" + f.StringSize() + ")", f.channel, Chat.MessageType.Channel); } else if (f.etat.HasFlag(FileStatus.InProgress) || f.etat.HasFlag(FileStatus.Finished)) { tabFile[idxFile].etat = f.etat; Program.myForm.RefreshFilesList(); Byte[] data = d.data; if (isEncrypted) { data = aes.DecryptToBytes(data, aes.receiveKey, aes.receiveIV); } ReceiveFile(f, data); if (f.etat.HasFlag(FileStatus.Finished)) { if (f.hash == sFile.Hash(Program.incomingFolder + f.name)) { Program.myForm.WriteNotice(f.name + " reçu", f.channel, Chat.MessageType.Channel); f.transferSucceded = true; } else { Program.myForm.WriteNotice(f.name + " reçu avec des erreurs", f.channel, Chat.MessageType.Channel); f.transferSucceded = false; } f.etat = FileStatus.Received; Program.myForm.RefreshFilesList(); SendData(new DataFile { fileinfo = f }); } } else if (f.etat.HasFlag(FileStatus.Accepted)) { tabFile[idxFile].etat = FileStatus.InProgress; Program.myForm.RefreshFilesList(); SendFile(f); } else if (f.etat.HasFlag(FileStatus.Rejected)) { tabFile[idxFile].etat = FileStatus.Rejected; Program.myForm.RefreshFilesList(); Program.myForm.WriteNotice(pseudo + " a rejeté le fichier " + f.name, f.channel, Chat.MessageType.Channel); } else if (f.etat.HasFlag(FileStatus.Received)) { tabFile[idxFile].etat = f.etat; Program.myForm.RefreshFilesList(); Program.myForm.WriteNotice(f.name + " envoyé à " + pseudo + " (" + ((f.transferSucceded) ? "avec succès" : "avec erreurs") + ")", f.channel, Chat.MessageType.Channel); } } }
public void RemoveFile(sFile file) { int idxRow = GetRowIndexBySFile(file); g_files.Rows.RemoveAt(idxRow); }