-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommon.cs
225 lines (201 loc) · 7.55 KB
/
Common.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
namespace FLogS
{
enum ErrorCode
{
None,
BAD_REGEX,
DEST_NOT_DIRECTORY,
DEST_NOT_FILE,
DEST_NOT_FOUND,
DEST_SENSITIVE,
NO_DEST,
NO_DEST_DIR,
NO_REGEX,
NO_SOURCE,
NO_SOURCES,
SOURCE_CONFLICT,
SOURCE_EQUALS_DEST,
SOURCE_NOT_FOUND,
SOURCES_NOT_FOUND,
}
enum WarningCode
{
None,
MULTI_OVERWRITE,
SINGLE_OVERWRITE,
}
/// <summary>
/// Static helper functions and variables serving purely logical purposes in either the front- or backend.
/// </summary>
static class Common
{
public readonly static string dateFormat = "yyyy-MM-dd HH:mm:ss"; // ISO 8601.
public readonly static string defaultLogDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "fchat", "data");
private readonly static DateTime epoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public readonly static string errorFile = "FLogS_ERROR.txt";
public static Dictionary<string, FileInfo> fileListing = [];
public readonly static Dictionary<string, string> htmlEntities = new()
{
{ "&", "&" },
{ "\"", """ },
{ "\'", "'" },
{ "<", "<" },
{ ">", ">" },
{ "¢", "¢" },
{ "£", "£" },
{ "¥", "¥" },
{ "€", "€" },
{ "©", "©" },
{ "®", "®" },
{ "\n", "<br />" },
};
public readonly static string htmlFooter = "</body>\n</html>";
public readonly static string htmlHeader = @"
<!DOCTYPE html>
<html>
<head>
<meta charset=""UTF-8"" />
<base target=""_blank"">
<title>F-Chat Exported Logs</title>
<style>
body { padding: 10px; background-color: #191932; display: block; word-wrap: break-word; -ms-hyphens: auto; -moz-hyphens: auto; -webkit-hyphens: auto; hyphens: auto; max-width: 100%; position: relative; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,Liberation Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji; font-size: 1rem; font-weight: 400; line-height: 1.5; color: #EDEDF5; text-align: left; }
script { display: block; }
span { position: relative; }
.pf { color: #6766AD; text-decoration: none; font-weight: bold; }
.url { color: #FFFFFF; text-decoration: underline; }
.warn { color: #909090; }
.us { background-color: #2A2A54; padding-top: 3px; padding-bottom: 3px; padding-right: 3px; margin-top: 3px; margin-bottom: 3px; margin-right: 3px; }
.ss { color: #D6D6FF; text-decoration: underline; }
.ts { color: #C0C0C0; }
.ec { width: 50px; height: 50px; vertical-align: middle; display: inline; }
.av { width: 15px; height: 15px; vertical-align: middle; display: inline; margin-left: 2px; margin-right: 2px; }
.sp { background-color: #0D0D0F; color: #0D0D0F; }
.sp * { background-color: #0D0D0F; color: #0D0D0F; }
.sp .ec { filter: brightness(0%); }
.sp:hover { color: #FFFFFF; }
.sp:hover * { color: #FFFFFF; }
.sp:hover .ts { color: #C0C0C0; }
.sp:hover .ss { color: #D6D6FF; }
.sp:hover .pf { color: #6766AD; }
.sp:hover .warn { color: #909090; }
.sp:hover .ec { filter: brightness(100%); }
</style>
</head>
<body>";
public static string lastException = string.Empty;
public static uint lastTimestamp = 0U;
public static bool plaintext = true;
public static bool processing = false;
public readonly static Dictionary<string, string> tagClosings = new()
{
{ "b", "</b>" },
{ "big", "</span>" },
{ "color", "</span>" },
{ "eicon", ".gif\" />" },
{ "i", "</i>" },
{ "icon", ".png\" /></a>" },
{ "noparse", "</script>" },
{ "s", "</s>" },
{ "session", "</a>" },
{ "spoiler", "</span>" },
{ "sub", "</sub>" },
{ "sup", "</sup>" },
{ "u", "</u>" },
{ "url", "</a>" },
{ "user", "</span></a>" },
};
public static DateTime timeBegin;
public static uint BEInt(byte[] buffer) => buffer[0]
+ buffer[1] * 256U
+ buffer[2] * 65536U
+ buffer[3] * 16777216U;
public static DateTime DTFromStamp(uint stamp)
{
try
{
return epoch.AddSeconds(stamp);
}
catch (Exception)
{
return new DateTime();
}
}
public static string GetErrorMessage(ErrorCode eCode, WarningCode wCode) => (eCode, wCode) switch
{
(ErrorCode.BAD_REGEX, _) => "Search text contains an invalid RegEx pattern.",
(ErrorCode.DEST_NOT_DIRECTORY, _) => "Destination is not a directory.",
(ErrorCode.DEST_NOT_FILE, _) => "Destination is not a file.",
(ErrorCode.DEST_NOT_FOUND, _) => "Destination directory does not exist.",
(ErrorCode.DEST_SENSITIVE, _) => "Destination appears to contain source log data.",
(ErrorCode.NO_DEST, _) => "No destination file selected.",
(ErrorCode.NO_DEST_DIR, _) => "No destination directory selected.",
(ErrorCode.NO_REGEX, _) => "No search text entered.",
(ErrorCode.NO_SOURCE, _) => "No source log file selected.",
(ErrorCode.NO_SOURCES, _) => "No source log files selected.",
(ErrorCode.SOURCE_CONFLICT, _) => "One or more source files exist in the destination.",
(ErrorCode.SOURCE_EQUALS_DEST, _) => "Source and destination files are identical.",
(ErrorCode.SOURCE_NOT_FOUND, _) => "Source log file does not exist.",
(ErrorCode.SOURCES_NOT_FOUND, _) => "One or more source files do not exist.",
(ErrorCode.None, WarningCode.MULTI_OVERWRITE) => "One or more files will be overwritten.",
(ErrorCode.None, WarningCode.SINGLE_OVERWRITE) => "Destination file will be overwritten.",
(ErrorCode.None, _) => string.Empty,
(_, _) => "An unknown error has occurred.",
};
public static bool IsValidPattern(string? pattern = null)
{
if (pattern is null)
return false;
try
{
Regex.IsMatch(string.Empty, pattern);
}
catch (Exception)
{
return false;
}
return true;
}
public static bool IsValidTimestamp(uint timestamp, bool LogTestOverride = false) =>
timestamp >= 1 // If it came before Jan. 1, 1970, there's a problem.
&& timestamp <= UNIXTimestamp() // If it's in the future, also a problem.
&& !string.Empty.Equals(DTFromStamp(timestamp).ToString(dateFormat) ?? string.Empty) // If it can't be translated to a date, also a problem.
&& (timestamp >= lastTimestamp || LogTestOverride); // If it isn't sequential, also a problem, because F-Chat would never save it that way.
public static void LogException(Exception e)
{
lastException = e.Message;
File.AppendAllText(errorFile, DateTime.Now.ToString(dateFormat) + " - " + lastException + "\n");
File.AppendAllText(errorFile, e?.TargetSite?.DeclaringType?.FullName + "." + e?.TargetSite?.Name + "\n\n");
}
public static bool LogTest(string targetFile)
{
if (!File.Exists(targetFile))
return false;
byte[] idBuffer = new byte[4];
byte[] srcBuffer;
using FileStream srcFS = new FileInfo(targetFile).OpenRead();
if (srcFS.Read(idBuffer, 0, 4) < 4)
return false;
if (!IsValidTimestamp(BEInt(idBuffer), true))
return false;
if (srcFS.ReadByte() > 6)
return false;
int profLen = srcFS.ReadByte();
if (profLen < 1)
return false;
srcBuffer = new byte[profLen];
if (srcFS.Read(srcBuffer, 0, profLen) < profLen)
return false;
if (srcFS.Read(idBuffer, 0, 2) < 2)
return false;
// We assume a valid log file starting from here, as the header format of the first message is now confirmed.
// TODO: Consider testing for two or even three messages to increase certainty. What if the first message in a log file happens to be corrupted?
return true;
}
public static (T, T) Swap<T>(ref (T, T) tuple) => tuple = (tuple.Item2, tuple.Item1);
public static uint UNIXTimestamp() => (uint)Math.Floor(DateTime.UtcNow.Subtract(epoch).TotalSeconds);
}
}