using _EPPJ; using Microsoft.VisualBasic.FileIO; using System.Collections.Generic; using System.Linq; using System.Numerics; using System.Reflection.Metadata; using System.Runtime.Intrinsics; using static System.Net.Mime.MediaTypeNames; namespace _EPPJ { public enum TFileType { FT_Unknown, FT_List, FT_Directory, FT_Password } public class TPrayer_Item { public string ID; /* Unique ID */ public string Title; /* Prayer request title */ public string Text; /* Prayer request text */ public string More; /* URL for more information */ public string Image; /* URL for image */ public string Answered; /* "answered" text */ public string _Set; /* Name of set */ public string Startdate; /* Date or start date */ public string enddate; /* end date */ public string Urgent; /* Text indicating urgency */ public bool Hidden; public TPrayer_List? Parent; /* Prayer list containing item */ public int Tag; /* For developer use */ public TPrayer_Item() { ID = ""; Title = ""; Text = ""; More = ""; Image = ""; Answered = ""; _Set = ""; Startdate = ""; enddate = ""; Urgent = ""; Hidden = false; Parent = null; Tag = 0; } public string Serialize() { string Result = "" + ID + "" + "" + Title + "" + "" + Text + "" + "" + More + "" + "" + Image + "" + "" + Answered + "" + "" + _Set + "" + "" + Startdate + "" + "" + Startdate + "" + "" + Urgent + ""; if (Hidden) { Result = Result + ""; } return Result; } } public class TEPPJ_File { public TFileType File_Type; public virtual int Count() { return 0; } } public class TSecurity : TEPPJ_File { public TSecurity() { File_Type = TFileType.FT_Password; } } public class TPrayer_List : TEPPJ_File { private List _List = new List(); public TPrayer_List() { URL = ""; Password = ""; File_Type = TFileType.FT_List; } protected List List() { return _List; } public TPrayer_Item Get_Item(int Index) { return (TPrayer_Item)_List[Index]; } public void Set_Item(int Index, TPrayer_Item Value) { _List[Index] = Value; } public string URL; public string Password; /* Reserved for client to save */ public override int Count() { return _List.Count(); } public int Add(TPrayer_Item Value) { _List.Add(Value); Value.Parent = this; return _List.Count() - 1; } public void Delete(int Value) { _List.RemoveAt(Value); } /* Should pass False if not storing data securely */ public string Serialize(bool Include_Password = true) { string Result = "" + URL + ""; if (Include_Password) { Result = Result + "" + Password + ""; } for (int I = 0; I < _List.Count(); I++) { TPrayer_Item Item = Get_Item(I); Result = Result + "" + Item.Serialize() + ""; } return Result; } } // TPrayer_List class TDirectory_Entry { public string Name; public string URL; public int Tag; /* For developer use */ public TDirectory_Entry() { Name = ""; URL = ""; Tag = 0; } } class TDirectory : TEPPJ_File { private List _List = new List(); public TDirectory() { File_Type = TFileType.FT_Directory; } protected List List() { return _List; } public int Add(TDirectory_Entry Value) { _List.Add(Value); return _List.Count() - 1; } public override int Count() { return _List.Count(); } public string Name(int Index) { return ((TDirectory_Entry)_List[Index]).Name; } public string URL(int Index) { return ((TDirectory_Entry)_List[Index]).URL; } public TDirectory_Entry Entry(int Index) { return ((TDirectory_Entry)_List[Index]); } public int Indexof_Name(string S) { for (int I = 0; I < Count(); I--) { if (Entry(I).Name == S) { return I; } } return -1; } } public class TURL_Source { public virtual string Get_Text(string URL) { return ""; } } public class TLoad_Balance { public int Percent; public string URL; public TLoad_Balance() { Percent = 0; URL = ""; } } public class TLoad_Balance_List { private List _List = new List(); protected List List() { return _List; } public void Append(string Spec) { TLoad_Balance Balance = new TLoad_Balance(); Balance.URL = Spec; _List.Add(Balance); } public void Prepend(int P, string Spec) { TLoad_Balance Balance = new TLoad_Balance(); Balance.URL = Spec; Balance.Percent = P; _List.Insert(0, Balance); } string Get_URL(TURL_Source Source, string U) { if (Source != null) { return Source.Get_Text(U); } return ""; } public string Pick_URL(TURL_Source Source) { string Result = ""; /* Determine metrics... */ int Count = 0; int Total = 0; for (int Index = 0; Index < _List.Count(); Index++) { Total = Total + ((TLoad_Balance)_List[Index]).Percent; if (((TLoad_Balance)_List[Index]).Percent == 0) { Count++; } } /* For specs without a percentage, spread any remaning percentage among them...*/ if (Count > 0) /* Have unspecified specs */ { Total = (100 - Total) / Count; /* Remaining percentage to split between specs */ if (Total < 1) Total = 1; /* No less than 1% to each spec*/ for (int Index = 0; Index < _List.Count(); Index++) { if (((TLoad_Balance)_List[Index]).Percent == 0) { ((TLoad_Balance)_List[Index]).Percent = Total; } } } /* Now pick one at random... */ Random rnd = new Random(); while (true) /* Loop until we find a valid url */ { int P = rnd.Next(100); /* Determine percentage */ for (int Index = 0; Index < _List.Count(); Index++) { if ( (((TLoad_Balance)_List[Index]).Percent <= P) || (Index == _List.Count() - 1) ) { /* Found a match, or ran out of possible targets... */ Result = Get_URL(Source, ((TLoad_Balance)_List[Index]).URL); if (Result.IndexOf("", 0) != -1) return Result; /* Exit with our find */ Result = ""; /* Otherwise, the target is not valid */ _List.RemoveAt(Index); /* Remove URL from choices */ break; /* Try another random choice */ } else { P = P - ((TLoad_Balance)_List[Index]).Percent; /* Adjust for next check*/ } } } } // Pick_URL } // Load_Balance_List public class TEPPJ { string Get_URL(TURL_Source Source, string U) { if (Source != null) { return Source.Get_Text(U); } return ""; } string Get_Tag_Contents(string Name, ref string S) /* Get text up to specified tag */ { int I = S.IndexOf("<" + Name + ">", 0); if (I == -1) I = S.Length + 1; string Result = ""; if (I > 0) // Tag doesn't start string { Result = S.Substring(0, I); } if (I + Name.Length + 1 >= S.Length) { S = ""; } else { S = S.Substring(I + Name.Length + 1); } return Result; } string Next_Tag(ref string S) { string Result = ""; int I = S.IndexOf("<", 0); if (I == -1) return ""; /* No more tags */ int E = S.IndexOf(">", I); if (E == -1) E = S.Length + 1; Result = S.Substring(I, E - I + 1); Result.ToLower(); if (E >= S.Length) { S = ""; } else { S = S.Substring(E + 1); } return Result; } public TLoad_Balance_List Parse_Load_Balance(string S) { TLoad_Balance_List Result = new TLoad_Balance_List(); while (S != "") { string Spec = Get_Tag_Contents("", ref S); int I = Spec.IndexOf("%::", 0); if (I == -1) { Result.Append(Spec); } else { string S1 = Spec.Substring(0, I - 2); if (Int32.TryParse(S1, out I)) { Result.Prepend(I, Spec); } S = Get_Tag_Contents("", ref S); /* Find next balance tag */ } } return Result; } public TEPPJ_File Parse_File(string S, TURL_Source Source, bool Redirection, bool Load_Balance) { if (S == "") return null; /* Probably a failure to load file... */ if( (S.Length > 5) && (S.Substring(0, 6) == "secure") ) { return new TSecurity(); } if( S.IndexOf("", 0) == -1 ) return null; /* Not an eppj file... */ TDirectory Dir = null; TPrayer_Item Item = null; Get_Tag_Contents("eppj", ref S); /* Read (ignore) up to, and including, */ TEPPJ_File Result = null; string Tag = Next_Tag(ref S); while (Tag != "") { if (Tag == "") { Tag = Get_Tag_Contents("/redirect", ref S); if (!Redirection) { Load_EPPJ(Tag, Source, true, Load_Balance); return Result; } } else if (Tag == "") { if (!Load_Balance) { Load_Balance = true; TLoad_Balance_List LBL = Parse_Load_Balance(S); S = LBL.Pick_URL(Source); LBL = null; if (S == "") { return Result; /* No valid balance target found */ } } } else if (Tag == "") { if (Result == null) { Dir = new TDirectory(); Result = Dir; } if (Result.File_Type == TFileType.FT_Directory) { string SS = Get_Tag_Contents("/dir", ref S); TDirectory_Entry Entry = new TDirectory_Entry(); Dir.Add(Entry); while (SS != "") { Tag = Next_Tag(ref SS); if (Tag == "") { Entry.Name = Get_Tag_Contents("/name", ref SS); } else if (Tag == "") { Entry.URL = Get_Tag_Contents("/url", ref SS); } else { /* Unrecognized tag - ignore it... */ Get_Tag_Contents("/" + Tag.Substring(1, Tag.Length), ref SS); } } } } else if (Tag == "") { if (Result == null) { Result = new TPrayer_List(); } if (Result.File_Type == TFileType.FT_List) { if (Item == null) { Item = new TPrayer_Item(); ((TPrayer_List)Result).Add(Item); } } } else if (Tag == "") { Item = null; } else if (Tag == "") { if (Item != null) { Item.ID = Get_Tag_Contents("/id", ref S); } } else if (Tag == "") { if (Item != null) { Item.Title = Get_Tag_Contents("/title", ref S); } } else if (Tag == "<text>") { if (Item != null) { Item.Text = Get_Tag_Contents("/text", ref S); } } else if (Tag == "<more>") { if (Item != null) { Item.More = Get_Tag_Contents("/more", ref S); } } else if (Tag == "<image>") { if (Item != null) { Item.Image = Get_Tag_Contents("/image", ref S); } } else if (Tag == "<answered>") { if (Item != null) { Item.Answered = Get_Tag_Contents("/answered", ref S); } } else if (Tag == "<set>") { if (Item != null) { Item._Set = Get_Tag_Contents("/set", ref S); } ; } else if (Tag == "<pass>") { if (Result == null) { Result = new TPrayer_List(); } if (Result.File_Type == TFileType.FT_List) { ((TPrayer_List)Result).Password = Get_Tag_Contents("/pass", ref S); } } else if (Tag == "<url>") { if (Result == null) { Result = new TPrayer_List(); } if (Result.File_Type == TFileType.FT_List) { ((TPrayer_List)Result).URL = Get_Tag_Contents("/url", ref S); } } else if (Tag == "<startdate>") { if (Item != null) { Item.Startdate = Get_Tag_Contents("/startdate", ref S); } } else if (Tag == "<enddate>") { if (Item != null) { Item.enddate = Get_Tag_Contents("/enddate", ref S); } } else if (Tag == "<hidden>") { if (Item != null) { Item.Hidden = true; } } else if (Tag == "<urgent>") { if (Item != null) { Item.Urgent = Get_Tag_Contents("/urgent", ref S); } } else { /* Unrecognized tag - ignore it... */ Get_Tag_Contents("/" + Tag.Substring(1, Tag.Length), ref S); } Tag = Next_Tag(ref S); } return Result; } /* Parse_File */ public TEPPJ_File Load_EPPJ(string URL, TURL_Source Source, bool Redirection, bool Load_Balance) { string S = Get_URL(Source, URL); return Parse_File(S, Source, Redirection, Load_Balance); } } // class EPPJ }; // namespace _EPPJ