public PostHeader Copy() { PostHeader copy = new PostHeader(); copy.Source = Source; copy.Target = Target; copy.ProjectID = ProjectID; copy.PostID = PostID; copy.ParentID = ParentID; copy.Time = Time; copy.Scope = Scope; copy.Archived = Archived; copy.Version = Version; copy.EditTime = EditTime; copy.FileKey = FileKey; copy.FileHash = FileHash; copy.FileSize = FileSize; copy.FileStart = FileStart; copy.SourceID = SourceID; copy.TargetID = TargetID; return(copy); }
public void SetHeader(string content) { PostHeader.SafeInvokeScript("SetElement", new String[] { "content", content }); }
public PostHeader Copy() { PostHeader copy = new PostHeader(); copy.Source = Source; copy.Target = Target; copy.ProjectID = ProjectID; copy.PostID = PostID; copy.ParentID = ParentID; copy.Time = Time; copy.Scope = Scope; copy.Archived = Archived; copy.Version = Version; copy.EditTime = EditTime; copy.FileKey = FileKey; copy.FileHash = FileHash; copy.FileSize = FileSize; copy.FileStart = FileStart; copy.SourceID = SourceID; copy.TargetID = TargetID; return copy; }
public static PostHeader Decode(G2Header root) { PostHeader header = new PostHeader(); G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) continue; switch (child.Name) { case Packet_Source: header.Source = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); header.SourceID = Utilities.KeytoID(header.Source); break; case Packet_Target: header.Target = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); header.TargetID = Utilities.KeytoID(header.Target); break; case Packet_ProjectID: header.ProjectID = BitConverter.ToUInt32(child.Data, child.PayloadPos); break; case Packet_PostID: header.PostID = BitConverter.ToUInt32(child.Data, child.PayloadPos); break; case Packet_ParentID: header.ParentID = BitConverter.ToUInt32(child.Data, child.PayloadPos); break; case Packet_Time: header.Time = DateTime.FromBinary(BitConverter.ToInt64(child.Data, child.PayloadPos)); break; case Packet_EditTime: header.EditTime = DateTime.FromBinary(BitConverter.ToInt64(child.Data, child.PayloadPos)); break; case Packet_Archived: header.Archived = BitConverter.ToBoolean(child.Data, child.PayloadPos); break; case Packet_Version: header.Version = BitConverter.ToUInt16(child.Data, child.PayloadPos); break; case Packet_Scope: header.Scope = (ScopeType)child.Data[child.PayloadPos]; break; case Packet_FileKey: header.FileKey = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_FileHash: header.FileHash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_FileSize: header.FileSize = BitConverter.ToInt64(child.Data, child.PayloadPos); break; case Packet_FileStart: header.FileStart = BitConverter.ToInt64(child.Data, child.PayloadPos); break; } } return header; }
private void Process_PostHeader(DataReq data, SignedData signed, PostHeader header) { Core.IndexKey(header.SourceID, ref header.Source); Core.IndexKey(header.TargetID, ref header.Target); PostUID uid = new PostUID(header); OpPost current = GetPost(header.TargetID, uid); // if link loaded if (current != null) { // lower version, send update if (header.Version < current.Header.Version) { if (data != null && data.Source != null) Store.Send_StoreReq(data.Source, data.LocalProxy, new DataReq(null, header.TargetID, ServiceID, 0, current.SignedHeader)); return; } // higher version else if (header.Version > current.Header.Version) { CachePost(signed, header); } // equal version do nothing } // else load file, set new header after file loaded else CachePost(signed, header); }
void FinishPost(PostHeader header) { if (Core.InvokeRequired) { Core.RunInCoreBlocked(delegate() { FinishPost(header); }); return; } CachePost(new SignedData(Protocol, Core.User.Settings.KeyPair, header), header); // publish to network and local region of target OpPost post = GetPost(header); if (Network.Established) Network.Store.PublishNetwork(header.TargetID, ServiceID, 0, GetPost(header).SignedHeader); else if (post != null) post.Unique = true; // publish when connected List<LocationData> locations = new List<LocationData>(); Trust.GetLocs(header.TargetID, header.ProjectID, 1, 1, locations); Trust.GetLocs(header.TargetID, header.ProjectID, 0, 1, locations); Store.PublishDirect(locations, header.TargetID, ServiceID, 0, GetPost(header).SignedHeader); // save right off, dont wait for timer, or sim to be on SaveHeader(header.TargetID); }
void DownloadPost(SignedData signed, PostHeader header) { if (!Utilities.CheckSignedData(header.Source, signed.Data, signed.Signature)) return; FileDetails details = new FileDetails(ServiceID, 0, header.FileHash, header.FileSize, new PostUID(header).ToBytes()); Core.Transfers.StartDownload(header.TargetID, details, GetPostPath(header), new EndDownloadHandler(EndDownload), new object[] { signed, header }); }
private void CachePost(SignedData signedHeader, PostHeader header) { if (Core.InvokeRequired) Debug.Assert(false); if (header.ParentID == 0 && header.SourceID != header.TargetID) { Network.UpdateLog("Board", "Post made to board other than source's"); return; } if (!File.Exists(GetPostPath(header))) { DownloadPost(signedHeader, header); return; } // check if current version loaded OpPost post = GetPost(header); if (post != null && post.Header.Version >= header.Version ) { UpdateGui(post); return; } // put into map OpBoard board = GetBoard(header.TargetID); if (board == null) { board = new OpBoard(header.Target); BoardMap.SafeAdd(header.TargetID, board); } PostUID uid = new PostUID(header); post = new OpPost(); post.Header = header; post.SignedHeader = signedHeader.Encode(Network.Protocol); post.Ident = header.TargetID.GetHashCode() ^ uid.GetHashCode(); post.Unique = Loading; // remove previous version of file, if its different OpPost prevPost = board.GetPost(uid); if (prevPost != null && GetPostPath(prevPost.Header) != GetPostPath(header)) try { File.Delete(GetPostPath(prevPost.Header)); } catch { } board.Posts.SafeAdd(uid, post); // update replies if (post.Header.ParentID == 0) board.UpdateReplies(post); else { PostUID parentUid = new PostUID(board.UserID, post.Header.ProjectID, post.Header.ParentID); OpPost parentPost = board.GetPost(parentUid); if (parentPost != null) { board.UpdateReplies(parentPost); UpdateGui(post); } } lock (SaveHeaders) if (!SaveHeaders.Contains(header.TargetID)) SaveHeaders.Add(header.TargetID); ushort replies = 0; if (SavedReplyCount.SafeTryGetValue(post.Ident, out replies)) { post.Replies = replies; SavedReplyCount.SafeRemove(post.Ident); } UpdateGui(post); if (Core.NewsWorthy(header.TargetID, header.ProjectID, true)) Core.MakeNews(ServiceIDs.Board, "Board updated by " + Core.GetName(header.SourceID), header.SourceID, 0, false); }
public void PostMessage(ulong user, uint project, uint parent, ScopeType scope, string subject, string message, TextFormat format, string quip, List<AttachedFile> files, OpPost edit) { // post header PostHeader header = new PostHeader(); header.Source = Core.User.Settings.KeyPublic; header.SourceID = Core.UserID; header.Target = Core.KeyMap[user]; header.TargetID = user; header.ParentID = parent; header.ProjectID = project; header.Scope = scope; if (edit == null) { header.Time = Core.TimeNow.ToUniversalTime(); byte[] rnd = new byte[4]; Core.RndGen.NextBytes(rnd); header.PostID = BitConverter.ToUInt32(rnd, 0); } else { header.PostID = edit.Header.PostID; header.Version = (ushort) (edit.Header.Version + 1); header.Time = edit.Header.Time; header.EditTime = Core.TimeNow.ToUniversalTime(); } header.FileKey = Utilities.GenerateKey(Core.StrongRndGen, 256); // setup temp file string tempPath = Core.GetTempPath(); using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, header.FileKey)) { int written = 0; // write post file written += Protocol.WriteToFile(new PostInfo(subject, format, quip, Core.RndGen), stream); byte[] msgBytes = UTF8Encoding.UTF8.GetBytes(message); written += Protocol.WriteToFile(new PostFile("body", msgBytes.Length), stream); foreach (AttachedFile attached in files) written += Protocol.WriteToFile(new PostFile(attached.Name, attached.Size), stream); stream.WriteByte(0); // end packets header.FileStart = (long)written + 1; // write files stream.Write(msgBytes, 0, msgBytes.Length); if (files != null) { int buffSize = 4096; byte[] buffer = new byte[buffSize]; foreach (AttachedFile attached in files) using (FileStream embed = File.OpenRead(attached.FilePath)) { int read = buffSize; while (read == buffSize) { read = embed.Read(buffer, 0, buffSize); stream.Write(buffer, 0, read); } } } stream.WriteByte(0); // signal last packet stream.FlushFinalBlock(); } // finish building header Utilities.HashTagFile(tempPath, Network.Protocol, ref header.FileHash, ref header.FileSize); string finalPath = GetPostPath(header); File.Move(tempPath, finalPath); FinishPost(header); }
public string GetPostPath(PostHeader header) { string targetDir = GetTargetDir(header.TargetID); if (!Directory.Exists(targetDir)) Directory.CreateDirectory(targetDir); byte[] ident = new byte[PostUID.SIZE + header.FileHash.Length]; new PostUID(header).ToBytes().CopyTo(ident, 0); header.FileHash.CopyTo(ident, PostUID.SIZE); return targetDir + Path.DirectorySeparatorChar + Utilities.CryptFilename(Core, header.TargetID, ident); }
public OpPost GetPost(PostHeader header) { return GetPost(header.TargetID, new PostUID(header)); }
public PostUID(PostHeader header) { SenderID = header.SourceID ; ProjectID = header.ProjectID; PostID = header.PostID; }
public static PostHeader Decode(G2Header root) { PostHeader header = new PostHeader(); G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { continue; } switch (child.Name) { case Packet_Source: header.Source = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); header.SourceID = Utilities.KeytoID(header.Source); break; case Packet_Target: header.Target = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); header.TargetID = Utilities.KeytoID(header.Target); break; case Packet_ProjectID: header.ProjectID = BitConverter.ToUInt32(child.Data, child.PayloadPos); break; case Packet_PostID: header.PostID = BitConverter.ToUInt32(child.Data, child.PayloadPos); break; case Packet_ParentID: header.ParentID = BitConverter.ToUInt32(child.Data, child.PayloadPos); break; case Packet_Time: header.Time = DateTime.FromBinary(BitConverter.ToInt64(child.Data, child.PayloadPos)); break; case Packet_EditTime: header.EditTime = DateTime.FromBinary(BitConverter.ToInt64(child.Data, child.PayloadPos)); break; case Packet_Archived: header.Archived = BitConverter.ToBoolean(child.Data, child.PayloadPos); break; case Packet_Version: header.Version = BitConverter.ToUInt16(child.Data, child.PayloadPos); break; case Packet_Scope: header.Scope = (ScopeType)child.Data[child.PayloadPos]; break; case Packet_FileKey: header.FileKey = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_FileHash: header.FileHash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_FileSize: header.FileSize = BitConverter.ToInt64(child.Data, child.PayloadPos); break; case Packet_FileStart: header.FileStart = BitConverter.ToInt64(child.Data, child.PayloadPos); break; } } return(header); }