-
Notifications
You must be signed in to change notification settings - Fork 0
/
caseGenerator_trace_email.pas
434 lines (380 loc) · 13 KB
/
caseGenerator_trace_email.pas
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
unit caseGenerator_trace_email;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.DateTimeCtrls, FMX.Calendar, FMX.Edit, FMX.StdCtrls, FMX.Layouts,
FMX.ListBox, FMX.Controls.Presentation, System.JSON, System.JSON.Types,
System.JSON.Readers, FMX.ScrollBox, FMX.Memo, caseGenerator_util,
FMX.Memo.Types;
type
TformTraceEmail = class(TForm)
Label1: TLabel;
lbMessage: TListBox;
btnClose: TButton;
btnAddMessage: TButton;
btnRemoveMessage: TButton;
Label6: TLabel;
panelFrom: TPanel;
Label5: TLabel;
Label8: TLabel;
panelTo: TPanel;
cbEmailTo: TComboBox;
Label11: TLabel;
Label13: TLabel;
edApplication: TEdit;
memoMessageText: TMemo;
cbMonth: TComboBox;
emailTime: TTimeEdit;
cbYear: TComboBox;
Label3: TLabel;
btnCancel: TButton;
btnModifyMessage: TButton;
cbDay: TComboBox;
Label2: TLabel;
cbEmailFrom: TComboBox;
procedure btnAddMessageClick(Sender: TObject);
procedure btnRemoveMessageClick(Sender: TObject);
procedure btnCloseClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure lbMessageChange(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure btnModifyMessageClick(Sender: TObject);
private
FuuidCase: string;
FpathCase: String;
procedure SetuuidCase(const Value: string);
procedure SetpathCase(const Value: String);
property uuidCase: string read FuuidCase write SetuuidCase;
property pathCase: String read FpathCase write SetpathCase;
function JsonTokenToString(const t: TJsonToken): string;
procedure readTraceEmailAccountFromFile;
function extractID(line: String): String;
function extractLastID(line: String): String;
function prepareItemMessage(operation: String): String;
{ Private declarations }
public
procedure ShowWithParamater(pathCase: String; uuidCase: String);
{ Public declarations }
end;
var
formTraceEmail: TformTraceEmail;
implementation
{$R *.fmx}
uses StrUtils;
{ TForm1 }
procedure TformTraceEmail.btnRemoveMessageClick(Sender: TObject);
begin
if lbMessage.ItemIndex > -1 then
lbMessage.Items.Delete(lbMessage.ItemIndex);
end;
function TformTraceEmail.extractID(line: String): String;
begin
Result := Copy(line, Pos('@', line) + 1, Length(line));
end;
function TformTraceEmail.extractLastID(line: String): String;
begin
Result := Copy(line, LastDelimiter('@', line) + 1, Length(line));
end;
procedure TformTraceEmail.FormShow(Sender: TObject);
var
idx: Integer;
begin
for idx:=2000 to 2020 do
cbYear.Items.Add(IntToStr(idx));
edApplication.Text := '';
memoMessageText.Text := '';
cbDay.ItemIndex := -1;
cbMonth.ItemIndex := -1;
cbYear.ItemIndex := -1;
cbEmailFrom.ItemIndex := -1;
cbEmailTo.ItemIndex := -1;
readTraceEmailAccountFromFile;
end;
function TformTraceEmail.JsonTokenToString(const t: TJsonToken): string;
begin
case t of
TJsonToken.None: Result := 'None';
TJsonToken.StartObject: Result := 'StartObject' ;
TJsonToken.StartArray: Result := 'StartArray' ;
TJsonToken.StartConstructor: Result := 'StartConstructor' ;
TJsonToken.PropertyName: Result := 'PropertyName' ;
TJsonToken.Comment: Result := 'Comment' ;
TJsonToken.Raw: Result := 'Raw' ;
TJsonToken.Integer: Result := 'Integer' ;
TJsonToken.Float: Result := 'Float' ;
TJsonToken.String: Result := 'String' ;
TJsonToken.Boolean: Result := 'Boolean' ;
TJsonToken.Null: Result := 'Null' ;
TJsonToken.Undefined: Result := 'Undefined' ;
TJsonToken.EndObject: Result := 'EndObject' ;
TJsonToken.EndArray: Result := 'EndArray' ;
TJsonToken.EndConstructor: Result := 'EndConstructor' ;
TJsonToken.Date: Result := 'Date' ;
TJsonToken.Bytes: Result := 'Bytes' ;
TJsonToken.Oid: Result := 'Oid' ;
TJsonToken.RegEx: Result := 'RegEx' ;
TJsonToken.DBRef: Result := 'DBRef' ;
TJsonToken.CodeWScope: Result := 'CodeWScope' ;
TJsonToken.MinKey: Result := 'MinKey' ;
TJsonToken.MaxKey: Result := 'MaxKey' ;
end;
end;
procedure TformTraceEmail.lbMessageChange(Sender: TObject);
var
line, uuidTo, sentDate, sDay, sMonth, sYear, sDate, messageType, value: String;
idx, posTo: Integer;
mobileTo: TStringList;
begin
if lbMessage.ItemIndex > - 1 then
begin
line := lbMessage.Items[lbMessage.ItemIndex];
edApplication.Text := ExtractField(line, '"uco-observable:application":"');
memoMessageText.Lines.Text := ExtractField(line, '"uco-observable:body":"');
value := ExtractRefId(line, '"uco-observable:fromRef":{');
for idx:=0 to cbEmailFrom.Items.Count - 1 do
begin
if AnsiContainsStr(cbEmailFrom.Items[idx], value) then
begin
cbEmailFrom.ItemIndex := idx;
Break;
end;
end;
value := ExtractRefId(line, '"uco-observable:toRef":[{');
for idx:=0 to cbEmailTo.Count - 1 do
begin
if AnsiContainsStr(cbEmailTo.Items[idx], value) then
begin
cbEmailTo.ItemIndex := idx;
Break;
end;
end;
sentDate := ExtractDateValue(line, '"uco-observable:sentTime":{');
sDate := Copy(sentDate, 1, 10);
sDay := Copy(sDate, 9, 2);
for idx:=0 to cbDay.Items.Count - 1 do
begin
if cbDay.Items[idx] = sDay then
begin
cbDay.ItemIndex := idx;
break;
end;
end;
sMonth := Copy(sDate, 6, 2);
for idx:=0 to cbMonth.Items.Count - 1 do
begin
if cbMonth.Items[idx] = sMonth then
begin
cbMonth.ItemIndex := idx;
break;
end;
end;
sYear := Copy(sDate, 1, 4);
for idx:=0 to cbYear.Items.Count - 1 do
begin
if cbYear.Items[idx] = sYear then
begin
cbYear.ItemIndex := idx;
break;
end;
end;
emailTime.Text := Copy(sentDate, 11, 8);
end;
end;
function TformTraceEmail.prepareItemMessage(operation: String): String;
var
line, recSep, idLine, indent, guidNoBraces, msgTime: string;
Uid: TGUID;
idx: Integer;
begin
idx:= 0;
recSep := #30 + #30;
indent := ' ';
line := '{' + recSep;
if operation = 'add' then
begin
CreateGUID(Uid);
guidNoBraces := 'kb:' + Copy(GuidToString(Uid), 2, Length(GuidToString(Uid)) - 2);
end
else
guidNoBraces := ExtractField(lbMessage.Items[lbMessage.ItemIndex], '"@id":"');
line := line + indent + '"@id":"' + guidNoBraces + '",' + recSep;
line := line + indent + '"@type":"uco-observable:CyberItem",' + recSep;
line := line + indent + '"uco-core:facets":[' + recSep;
line := line + indent + '{' + recSep;
line := line + RepeatString(indent, 2) + '"@type":"uco-observable:EmailMessage",' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:application":"' + edApplication.Text + '",' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:sentTime":{' + recSep;
line := line + '"@type":"xsd:dateTime",' + recSep;
msgTime := cbYear.Items[cbYear.ItemIndex] + '-';
msgTime := msgTime + cbMonth.Items[cbMonth.ItemIndex] + '-';
msgTime := msgTime + cbDay.Items[cbDay.ItemIndex] + 'T';
msgTime := msgTime + TimeToStr(emailTime.Time);
line := line + '"@value":"' + msgTime + '"},' + recSep;
idLine := ExtractID(cbEmailFrom.Items[cbEmailFrom.ItemIndex]);
line := line + '"uco-observable:fromRef":{"@id":"' + idLine + '"},' + recSep;
idLine := ExtractID(cbEmailTo.Items[cbEmailTo.ItemIndex]);
line := line + '"uco-observable:toRef":[{"@id":"' + idLine + '"}],' + recSep;
line := line + '"uco-observable:ccRefs":[],' + recSep;
line := line + '"uco-observable:bccRefs":[],' + recSep;
line := line + '"uco-observable:body":"' + memoMessageText.Text + '"' + recSep;
line := line + indent + '}]' + recSep + '}' + recSep;
Result := line;
end;
procedure TformTraceEmail.readTraceEmailAccountFromFile;
var
json, recSep, crlf: string;
sreader: TStringReader;
jreader: TJsonTextReader;
inID, inEmail: Boolean;
id, email: String;
listTrace: TStringList;
idx, nHypens, posField: integer;
begin
//dir := GetCurrentDir;
recSep := #30 + #30;
crlf := #13 + #10;
// read file JSON uuidCase-identity.json: fill in cbSourceIdentity component
if FileExists(FpathCase + FuuidCase + '-traceEMAIL_ACCOUNT.json') then
begin
listTrace := TStringList.Create;
listTrace.LoadFromFile(FpathCase + FuuidCase + '-traceEMAIL_ACCOUNT.json');
//JSON string here
json := stringreplace(listTrace.Text, recSep, crlf,[rfReplaceAll]);
try
sreader := TStringReader.Create(json);
jreader := TJsonTextReader.Create(sreader);
while jreader.Read do
begin
if JsonTokenToString(jreader.TokenType) = 'PropertyName' then
begin
if jreader.Value.AsString = '@id' then
inID := True
else
inID := False;
if jreader.Value.AsString = 'uco-observable:emailAddress' then
inEmail := True
else
inEmail := False;
end;
if JsonTokenToString(jreader.TokenType) = 'String' then
begin
if inID then
id := jreader.Value.AsString;
if inEmail then
begin
nHypens := CountOccurrences('-', id);
(*--- if nHypens > 4 then it is the case of id related to @type inside an Object,
for instance for Identity it can be @id:"...-...-SimpleName" ---*)
if nHypens > 4 then
id := Copy(id, 1, LastDelimiter('-', id) - 1);
email := jreader.Value.AsString;
cbEmailFrom.Items.Add(email + StringOfChar(' ', 100) + '@' + id);
cbEmailTo.Items.Add(email + StringOfChar(' ', 100) + '@' + id);
end;
end;
end;
finally
jreader.Free;
sreader.Free;
end;
end;
end;
procedure TformTraceEmail.btnCancelClick(Sender: TObject);
begin
formTraceEmail.Close;
end;
procedure TformTraceEmail.btnCloseClick(Sender: TObject);
var
fileJSON: TextFile;
line: String;
idx: integer;
begin
if lbMessage.Items.Count > 0 then
begin
//dir := GetCurrentDir;
idx := 0;
// create file JSON uuidCase-traceMESSAGE.json
AssignFile(fileJSON, FpathCase + FuuidCase + '-traceEMAIL.json', CP_UTF8);
Rewrite(fileJSON); // create new file
WriteLn(fileJSON, '{');
line := #9 + '"OBJECTS_EMAIL":[';
WriteLn(fileJSON, UTF8Encode(line));
for idx:= 0 to lbMessage.Items.Count - 2 do
WriteLn(fileJSON, UTF8Encode(#9#9 + lbMessage.Items[idx] + ','));
WriteLn(fileJSON, UTF8Encode(#9#9 + lbMessage.Items[idx]));
WriteLn(fileJSON, UTF8Encode(#9#9 + ']'));
Write(fileJSON,'}');
CloseFile(fileJSON);
end
else
deleteFile(FpathCase + FuuidCase + '-traceEMAIL.json');
formTraceEmail.Close;
end;
procedure TformTraceEmail.btnModifyMessageClick(Sender: TObject);
begin
if lbMessage.ItemIndex > - 1 then
lbMessage.Items[lbMessage.ItemIndex] := prepareItemMessage('modify');
end;
procedure TformTraceEmail.btnAddMessageClick(Sender: TObject);
var
line, recSep, idLine: string;
Uid: TGUID;
idx: Integer;
begin
if (cbEmailFrom.ItemIndex = -1) or (cbEmailTo.ItemIndex = -1) then
ShowMessage('Email address FROM or TO is empty!')
else
begin
lbMessage.Items.Add(prepareItemMessage('add'));
edApplication.Text := '';
memoMessageText.Lines.Clear;
cbEmailFrom.ItemIndex := -1;
cbEmailTo.ItemIndex := -1;
cbDay.ItemIndex := -1;
cbMonth.ItemIndex := -1;
cbYear.ItemIndex := -1;
end;
end;
procedure TformTraceEmail.SetpathCase(const Value: String);
begin
FpathCase := Value;
end;
procedure TformTraceEmail.SetuuidCase(const Value: string);
begin
FuuidCase := Value;
end;
procedure TformTraceEmail.ShowWithParamater(pathCase: String; uuidCase: String);
var
fileJSON: TextFile;
line, subLine: String;
begin
SetUuidCase(uuidCase);
SetPathCase(pathCase);
//dir := GetCurrentDir;
// read file JSON uuidCase-identity.json
if FileExists(FpathCase + FuuidCase + '-traceEMAIL.json') then
begin
AssignFile(fileJSON, FpathCase + FuuidCase + '-traceEMAIL.json', CP_UTF8);
Reset(fileJSON);
lbMessage.Items.Clear;
while not Eof(fileJSON) do
begin
ReadLn(fileJSON, line);
line := Trim(line);
if (line = '{') or (line = '}') or (line = ']') or (AnsiContainsStr(line, 'OBJECTS_')) then // first or last line or root element
else
begin
subLine := Copy(line, Length(line), 1); // rule out of comma
if subLine = ',' then
line := Copy(line, 1, Length(line) - 1);
lbMessage.Items.Add(line);
end;
end;
CloseFile(fileJSON);
end;
// else
// ShowMessage(dir + uuidCase + '-identity.json' + ' doesn''t exist');
formTraceEmail.ShowModal;
end;
end.