-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathptout.pas
1062 lines (919 loc) · 27.1 KB
/
ptout.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Unit PTOut;
Interface
{$Q-}
Uses
{$IfDef UNIX}
linux,
{$EndIf}
DOS,
Types, GeneralP,
Log,
TickCons, TickType;
Type
pOutbound = ^tOutbound;
pBTOutbound = ^tBTOutbound;
pFDOutbound = ^tFDOutbound;
tOutbound =
object
Constructor Init;
Destructor Done; Virtual;
{check/set/unset BusyFlags for an user}
Function IsBusy(User: pUser): Boolean; Virtual;
Procedure SetBusy(User: pUser); Virtual;
Procedure UnSetBusy(User: pUser); Virtual;
{send a file to a user, check if it already was sent}
Procedure SendFile(User: pUser; FName: String; Action: Byte); Virtual;
Function CheckFileSent(User: pUser; FName: String): Boolean; Virtual;
{get a name for an archive, remove zero-length archives}
Function ArchiveName(User: pUser): String; Virtual;
Procedure PurgeArchs; Virtual;
end;
tBTOutbound =
object(tOutbound)
Constructor Init(_Cfg: PTickCfg; _lh: Byte; _BaseDir: String;
_PrimAKA: tNetAddr);
Destructor Done; Virtual;
{check/set/unset BusyFlags for an user}
Function IsBusy(User: pUser): Boolean; Virtual;
Procedure SetBusy(User: pUser); Virtual;
Procedure UnSetBusy(User: pUser); Virtual;
{send a file to a user, check if it already was sent}
Procedure SendFile(User: pUser; FName: String; Action: Byte); Virtual;
Function CheckFileSent(User: pUser; FName: String): Boolean; Virtual;
{get a name for an archive, remove zero-length archives}
Function ArchiveName(User: pUser): String; Virtual;
Procedure PurgeArchs; Virtual;
private
BaseDir: String; {directory of primary zone}
PrimAKA: tNetAddr; {primary AKA}
lh: Byte;
Cfg: PTickCfg;
Function FloName(Usr: pUser): String;
Procedure PurgeArchsDir(Dir: String);
end;
tFDOutbound =
object (tOutbound)
Constructor Init(_STQFile: String; _LCKFile: String; _lh: Byte; _TicDir: String; _FlagDir: String);
Destructor Done; Virtual;
{check/set/unset BusyFlags for an user}
Function IsBusy(User: pUser): Boolean; Virtual;
Procedure SetBusy(User: pUser); Virtual;
Procedure UnSetBusy(User: pUser); Virtual;
{send a file to a user, check if it already was sent}
Procedure SendFile(User: pUser; FName: String; Action: Byte); Virtual;
Function CheckFileSent(User: pUser; FName: String): Boolean; Virtual;
{get a name for an archive, remove zero-length archives}
Function ArchiveName(User: pUser): String; Virtual;
Procedure PurgeArchs; Virtual;
private
STQFile: String;
LCKFile: String;
TicDir: String;
FlagDir: String;
STQ: file;
ValidQueue: Boolean;
Rev: Word;
lh: Byte;
{global}
TimeCreated, TimePacked, ReservedLong, PackRecovery: LongInt;
{single record}
EntryTime, Flags, TimeStamp: LongInt;
Address, FileName, TFA: String;
Procedure ReadHdr;
Procedure WriteHdr;
Procedure ReadEntry;
Procedure WriteEntry;
Function OpenSTQ: Boolean;
Procedure ForceRescan;
Function FileBusy(FName: String): Boolean;
Procedure PurgeArchsDir(Dir: String);
end;
Implementation
Const
{FD}
FQflgKFS =$00000001; {Kill file after sending, w/checking}
FQflgKFSNoCheck =$00000002; {Like KFS, but w/o checking}
FQflgTFS =$00000004; {Trunc file after sending w/checking}
FQflgTFSNoCheck =$00000008; {Like TFS, but w/o checking}
FQflgIsARCmail =$00000020; {ARCmail attach}
FQflgSendStart =$00000040; {FD has started to send this file}
FQflgSendAfter =$00020000; {Date contains entry release date}
FQflgKeepExpired=$00040000; {Keep expired entry}
FQflgSendUntil =$00080000; {Date contains entry expiration date}
FQflgIsHold =$00100000; {Hold}
FQflgIsCrash =$00200000; {Crash}
FQflgIsIMM =$00400000; {Immediate}
FQflgNoPickup =$00800000; {Must be delivered}
FQflgIsSpool =$01000000; {Spool mask (Permanent + KFSNoCheck)}
FQflgIsFREQ =$02000000; {Is file request}
FQflgIsFile =$04000000; {Is file attach}
FQflgTFA =$08000000; {TFA field contains alias filename}
FQflgHidden =$20000000; {Hidden entry, don't display}
FQflgLocked =$40000000; {Locked entry, ignore}
FQflgDeleted =$80000000; {Entry has been deleted}
FQflgPassword =$08000000; {Used for File Requests}
FQmacHasFilename=(FQflgIsFREQ or FQflgIsFile);
Procedure Abstract; Begin Halt(211); End;
Constructor tOutbound.Init;
Begin Fail; End;
Destructor tOutbound.Done;
Begin Abstract; End;
Function tOutbound.IsBusy(User: pUser): Boolean;
Begin Abstract; End;
Procedure tOutbound.SetBusy(User: pUser);
Begin Abstract; End;
Procedure tOutbound.UnSetBusy(User: pUser);
Begin Abstract; End;
Procedure tOutbound.SendFile(User: pUser; FName: String; Action: Byte);
Begin Abstract; End;
Function tOutbound.CheckFileSent(User: pUser; FName: String): Boolean;
Begin Abstract; End;
Function tOutbound.ArchiveName(User: pUser): String;
Begin Abstract; End;
Procedure tOutbound.PurgeArchs;
Begin Abstract; End;
Constructor tBTOutbound.Init(_Cfg: PTickCfg; _lh: Byte; _BaseDir: String;
_PrimAKA: tNetAddr);
Begin
Cfg := _Cfg;
lh := _lh;
BaseDir := _BaseDir;
PrimAKA := _PrimAKA;
LogSetCurLevel(lh, 5);
LogWriteLn(lh, 'BaseDir = "'+_BaseDir+'"/"'+BaseDir+'"');
End;
Destructor tBTOutbound.Done;
Begin
End;
Function tBTOutbound.IsBusy(User: pUser): Boolean;
Var
Tmp: String;
Begin
Tmp := FLOName(User);
Tmp[0] := Char(Byte(Tmp[0])-3); {remove 'flo'}
IsBusy := FileExist(Tmp + 'bsy');
End;
Procedure tBTOutbound.SetBusy(User: pUser);
Var
Tmp: String;
f: File;
Begin
Tmp := FLOName(User);
Tmp[0] := Char(Byte(Tmp[0])-3); {remove 'flo'}
Assign(f, Tmp + 'bsy');
{$I-} ReWrite(f); {$I+}
If (IOResult = 0) then Close(f)
Else
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'Could not create BusyFile "'+Tmp+'bsy"!');
End;
End;
Procedure tBTOutbound.UnSetBusy(User: pUser);
Var
Tmp: String;
Begin
Tmp := FLOName(User);
Tmp[0] := Char(Byte(Tmp[0])-3); {remove 'flo'}
If not DelFile(Tmp + 'bsy') then
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'Could not remove BusyFile "'+Tmp+'bsy"!');
End;
End;
Procedure tBTOutbound.SendFile(User: pUser; FName: String; Action: Byte);
Var
f: Text;
FlowName: String;
Error1, Error2: Integer;
Begin
FlowName := FloName(User);
Assign(f, FlowName);
{$I-} Append(f); {$I+}
Error1 := IOResult;
If (Error1 <> 0) then
Begin
Assign(f, FlowName);
{$I-} ReWrite(f); {$I+}
Error2 := IOResult;
If (Error2 <> 0) then
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'Couldn''t open "'+FlowName+'": Error '+
IntToStr(Error1)+', '+IntToStr(Error2)+'!');
Exit;
End;
End;
{$I-}
Case Action of
ac_Nothing : WriteLn(f, FName);
ac_Del : WriteLn(f, '^'+FName);
ac_Trunc : WriteLn(f, '#'+FName);
else
WriteLn(f, FName);
end;
If (IOResult <> 0) then
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'Error writing "'+FlowName+'"!');
End;
Close(f); {$I+}
If (IOResult <> 0) then
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'Couldn''t close "'+FlowName+'"!');
End
Else
Begin
{$IfDef UNIX}
Chmod(FlowName, FilePerm);
{$EndIf}
End;
End;
Function tBTOutbound.CheckFileSent(User: pUser; FName: String): Boolean;
Var
Tmp: String;
f: Text;
Line: String;
Found: Boolean;
Begin
{$IfNDef UNIX}
FName := UpStr(FName);
{$EndIf}
Found := False;
Tmp := FLOName(User);
Tmp[Length(Tmp)-2] := 'f'; {flavour normal}
Assign(f, Tmp);
{$I-} ReSet(f); {$I+}
If (IOResult = 0) then While not (EOF(f) or Found) do
Begin
ReadLn(f, Line);
If (Line[1] = '~') then Continue; {skip sent files}
If (Line[1] in ['#', '^']) then Delete(Line, 1, 1);
{$IfDef UNIX}
Found := Found or (Line = FName);
{$Else}
Found := Found or (Upstr(Line) = FName);
{$EndIf}
End;
If not Found then
Begin
Tmp[Length(Tmp)-2] := 'c'; {flavour crash}
Assign(f, Tmp);
{$I-} ReSet(f); {$I+}
If (IOResult = 0) then While not (EOF(f) or Found) do
Begin
ReadLn(f, Line);
If (Line[1] = '~') then Continue; {skip sent files}
If (Line[1] in ['#', '^']) then Delete(Line, 1, 1);
{$IfDef UNIX}
Found := Found or (Line = FName);
{$Else}
Found := Found or (Upstr(Line) = FName);
{$EndIf}
End;
End;
If not Found then
Begin
Tmp[Length(Tmp)-2] := 'd'; {flavour direct}
Assign(f, Tmp);
{$I-} ReSet(f); {$I+}
If (IOResult = 0) then While not (EOF(f) or Found) do
Begin
ReadLn(f, Line);
If (Line[1] = '~') then Continue; {skip sent files}
If (Line[1] in ['#', '^']) then Delete(Line, 1, 1);
{$IfDef UNIX}
Found := Found or (Line = FName);
{$Else}
Found := Found or (Upstr(Line) = FName);
{$EndIf}
End;
End;
If not Found then
Begin
Tmp[Length(Tmp)-2] := 'h'; {flavour hold}
Assign(f, Tmp);
{$I-} ReSet(f); {$I+}
If (IOResult = 0) then While not (EOF(f) or Found) do
Begin
ReadLn(f, Line);
If (Line[1] = '~') then Continue; {skip sent files}
If (Line[1] in ['#', '^']) then Delete(Line, 1, 1);
{$IfDef UNIX}
Found := Found or (Line = FName);
{$Else}
Found := Found or (Upstr(Line) = FName);
{$EndIf}
End;
End;
CheckFileSent := not Found;
End;
Function tBTOutbound.ArchiveName(User: pUser): String;
Var
CurName: String;
Begin
CurName := FLOName(User);
CurName[0] := Char(Byte(CurName[0])-3); {remove 'flo'}
CurName := CurName + 'c00';
While (FileExist(CurName) and (GetFSize(CurName) = 0)) do
Begin
If (CurName[Length(CurName)] = '9') then
Begin
If (CurName[Length(CurName)-1] = '9') then
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'no free archive name for "'+User^.Name+'" ('+
Addr2Str(User^.Addr)+')!');
ArchiveName := '';
Exit;
End
Else Inc(CurName[Length(CurName)-1]);
End
Else Inc(CurName[Length(CurName)]);
End;
ArchiveName := CurName;
End;
Procedure tBTOutbound.PurgeArchs;
Begin
{ LogSetCurLevel(lh, 5);
LogWriteLn(lh, 'BaseDir = "'+BaseDir+'"');
LogWriteLn(lh, 'Calling PurchArchsDir("'+ Copy(BaseDir, 1, LastPos(DirSep, BaseDir)-1)+ '")');}
PurgeArchsDir(Copy(BaseDir, 1, LastPos(DirSep, BaseDir)-1));
End;
Procedure tBTOutbound.PurgeArchsDir(Dir: String);
Var
{$IfDef SPEED}
SRec: TSearchRec;
{$Else}
SRec: SearchRec;
{$EndIf}
l: Byte;
Begin
{ LogSetCurLevel(lh, 5);
LogWriteLn(lh, 'tBTOutbound.PurgeArchsDir("'+Dir+'") called');}
SRec.Name := Dir + DirSep+ '*.*';
{ LogWriteLn(lh, 'Calling FindFirst("'+ SRec.Name+ ', AnyFile, SRec)');}
FindFirst(SRec.Name, AnyFile, SRec);
While (DosError = 0) do
Begin
LogSetCurLevel(lh, 5);
{ LogWriteLn(lh, 'DosError = 0');}
l := Length(SRec.Name);
{ LogWriteLn(lh, 'Length(SRec.Name) = '+ IntToStr(l));}
If (SRec.Attr and Directory) = 0 then
Begin
LogSetCurLevel(lh, 5);
{ LogWriteLn(lh, 'not Directory');}
If (SRec.Name[l-3] = '.') and (UpCase(SRec.Name[l-2]) = 'C') then
Begin
{ LogWriteLn(lh, '*.[Cc]?? found');}
If not ((SRec.Name[l-1] < '0') or (SRec.Name[l-1] > '9')
or (SRec.Name[l] < '0') or (SRec.Name[l] > '9')) then
Begin
{ LogWriteLn(lh, '*.[Cc][0-9][0-9] found');}
If (GetFSize(Dir + DirSep + SRec.Name) = 0) then
Begin
Write('Deleting '+Dir + DirSep + SRec.Name+'...');
If Not DelFile(Dir + DirSep + SRec.Name) then
Begin
WriteLn;
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'Couldn''t delete '+Dir+DirSep+SRec.Name+'!');
End
Else WriteLn(' Done');
End;
End;
End;
End
Else
Begin
{ LogSetCurLevel(lh, 5);
LogWriteLn(lh, 'Directory');}
If (SRec.Name[1] <> '.') then PurgeArchsDir(Dir + DirSep + SRec.Name);
End;
LogSetCurLevel(lh, 5);
{ LogWriteLn(lh, 'Calling FindNext');}
FindNext(SRec);
End;
End;
Function tBTOutbound.FloName(Usr: pUser): String;
Var
s, s1: String;
FlowName: String;
Dir: String;
Addr: TNetAddr;
i: Byte;
Begin
If CompAddr(Usr^.ArcAddr, EmptyAddr) then Addr := Usr^.Addr Else Addr := Usr^.ArcAddr;
If ((Addr.Domain = PrimAKA.Domain) or (Addr.Domain = '') or (PrimAKA.Domain = '')) then
Begin
If (Addr.Zone <> PrimAKA.Zone) then
Begin
If (Addr.Zone < 4096) then FlowName := Cfg^.OutBound+'.'+Copy(WordToHex(word(Addr.Zone)), 2, 3)+DirSep
Else FlowName := Cfg^.OutBound+'.'+WordToHex(Word(Addr.Zone))+DirSep;
End
Else FlowName := Cfg^.OutBound + DirSep;
End
Else
Begin
s := BaseDir;
While (s[Length(s)] <> DirSep) do Delete(s, Length(s), 1);
s1 := Addr.Domain;
If (Cfg^.NumDomains > 0) then
Begin
For i := 1 to Cfg^.NumDomains do
If (UpStr(Addr.Domain) = UpStr(Cfg^.Domains[i].Domain)) then
s1 := Cfg^.Domains[i].Abbrev;
End;
FlowName := s + s1 + '.'+Copy(WordToHex(word(Addr.Zone)), 2, 3)+DirSep;
End;
Dir := Copy(FlowName, 1, Length(FlowName) - 1);
FlowName := FlowName + WordToHex(word(Addr.Net)) + WordToHex(word(Addr.Node));
If (Addr.Point <> 0) then
Begin
Dir := FlowName + '.pnt';
FlowName := FlowName + '.pnt'+DirSep+'0000' + WordToHex(word(Addr.Point));
End;
Case Usr^.MailFlags of
ml_Normal : FlowName := FlowName + '.flo';
ml_Direct : FlowName := FlowName + '.dlo';
ml_Hold : FlowName := FlowName + '.hlo';
ml_Crash : FlowName := FlowName + '.clo';
end;
If not DirExist(Dir) then If not MakeDir(Dir) then
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'Couldn''t create directory "'+Dir+'"!');
End
Else
Begin
LogSetCurLevel(lh, 2);
LogWriteLn(lh, 'Created directory "'+Dir+'"');
End;
FloName := FlowName;
End;
Constructor tFDOutbound.Init(_STQFile: String; _LCKFile: String; _lh: Byte; _TicDir: String; _FlagDir: String);
Begin
STQFile := _STQFile;
LCKFile := _LCKFile;
lh := _lh;
TicDir := _TicDir;
FlagDir := _FlagDir;
End;
Destructor tFDOutbound.Done;
Begin
End;
Function tFDOutbound.IsBusy(User: pUser): Boolean;
Begin
{does nothing}
IsBusy := False;
End;
Procedure tFDOutbound.SetBusy(User: pUser);
Begin
{does nothing}
End;
Procedure tFDOutbound.UnSetBusy(User: pUser);
Begin
{does nothing}
End;
Procedure tFDOutbound.SendFile(User: pUser; FName: String; Action: Byte);
Var
i: Byte;
DT: TimeTyp;
Begin
If not OpenSTQ then Exit;
ReadHdr;
{set entry values}
Today(DT); Now(DT); EntryTime := DTToUnixDate(DT);
TimeStamp := EntryTime;
Address := Addr2StrND(User^.ArcAddr);
FileName := FName;
TFA := '';
Flags := FQflgIsFile;
If (Action = ac_Del) then Flags := Flags or FQflgKFSNoCheck
Else if (Action = ac_Trunc) then Flags := Flags or FQflgTFSNoCheck;
Case User^.MailFlags of
ml_Crash: Flags := Flags or FQflgIsCrash;
ml_Direct: Flags := Flags or FQflgIsIMM;
ml_Hold: Flags := Flags or FQflgIsHold;
{ ml_Normal: do nothing}
End;
{check lock}
i := 1;
While (FileExist(LCKFile) and (i < 12)) do
Begin
Inc(i);
Delay(1000);
End;
If FileExist(LCKFile) then
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'tFDOutbound: Could not send file "'+FName+'" to User "'+
User^.Name+'" ('+Addr2Str(User^.Addr)+'): STQ locked for more than 10 seconds!');
Close(STQ);
End
Else
Begin
{set lock}
CreateSem(LCKFile);
{append entry}
Seek(STQ, filesize(STQ));
WriteEntry;
{reset lock}
DelFile(LCKFile);
Close(STQ);
{force rescan?}
If (User^.MailFlags <> ml_Hold) then ForceReScan;
End;
End;
Function tFDOutbound.CheckFileSent(User: pUser; FName: String): Boolean;
Var
Error: Integer;
DT: TimeTyp;
Addr: TNetAddr;
Begin
If not OpenSTQ then Exit;
ReadHdr;
If (not EOF(STQ)) then
Begin
Repeat
ReadEntry;
Str2Addr(Address, Addr);
Until (EOF(STQ) or (CompAddr(Addr, User^.ArcAddr) and (FName = FileName)));
CheckFileSent := not CompAddr(Addr, User^.ArcAddr) or (FName <> FileName)
or ((Flags and FQflgDeleted) > 0); {no entry or deleted entry => file already sent}
End
Else CheckFileSent := True; {no entry in STQ => file already sent}
Close(STQ);
End;
Function tFDOutbound.ArchiveName(User: pUser): String;
Var
CurName: String;
Begin
{directory structure:
Cfg^.TicDir
|
+-zone.001 Dir
|
+-zone.002 Dir
| |
| +-098301a8.pnt Dir
| | |
| | +-00000001.c00 File
| |
| +-09830000.c00 File
|
+-zone.02c Dir
}
{calculate first name, create necessary dirs}
CurName := TicDir+DirSep+'zone.';
If (User^.ArcAddr.Zone < 4096) then CurName := CurName+Copy(WordToHex(Word(User^.ArcAddr.Zone)), 2, 3)
Else CurName := CurName+WordToHex(Word(User^.ArcAddr.Zone));
MakeDir(CurName);
CurName := CurName+DirSep + WordToHex(word(User^.ArcAddr.Net)) +
WordToHex(word(User^.ArcAddr.Node));
If (User^.ArcAddr.Point <> 0) then
Begin
CurName := CurName + '.pnt' + DirSep + '0000' + WordToHex(word(User^.ArcAddr.Point));
MakeDir(CurName+'.pnt');
End;
CurName := CurName + '.c00';
{Find unused name}
While (FileExist(CurName) and ((GetFSize(CurName) = 0) or FileBusy(CurName))) do
Begin
If (CurName[Length(CurName)] = '9') then
Begin
If (CurName[Length(CurName)-1] = '9') then
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'no free archive name for "'+User^.Name+'" ('+
Addr2Str(User^.Addr)+')!');
ArchiveName := '';
Exit;
End
Else Inc(CurName[Length(CurName)-1]);
End
Else Inc(CurName[Length(CurName)]);
End;
ArchiveName := CurName;
End;
Procedure tFDOutbound.PurgeArchs;
Begin
PurgeArchsDir(TicDir);
End;
Procedure tFDOutbound.ReadHdr;
Var
Sig: String[22];
Maj, Min: Byte;
Long1, Long2, Long3, Long4: Byte;
DT: TimeTyp;
Begin
Sig[0] := Char(22);
BlockRead(STQ, Sig[1], 22);
If (Sig = 'FrontDoor File Queue'#26#0) then
Begin
BlockRead(STQ, Min, 1);
BlockRead(STQ, Maj, 1);
Rev := Min + (Maj * 256);
If (Rev = $0100) then
Begin
BlockRead(STQ, Long1, 1); BlockRead(STQ, Long2, 1);
BlockRead(STQ, Long3, 1); BlockRead(STQ, Long4, 1);
TimeCreated := (LongInt(Long1) + (LongInt(Long2) * 256)) + ((LongInt(Long3) + (LongInt(Long4) * 256)) * 65536);
UnixToDT(TimeCreated, DT);
BlockRead(STQ, Long1, 1); BlockRead(STQ, Long2, 1);
BlockRead(STQ, Long3, 1); BlockRead(STQ, Long4, 1);
TimePacked := (LongInt(Long1) + (LongInt(Long2) * 256)) + ((LongInt(Long3) + (LongInt(Long4) * 256)) * 65536);
UnixToDT(TimePacked, DT);
BlockRead(STQ, Long1, 1); BlockRead(STQ, Long2, 1);
BlockRead(STQ, Long3, 1); BlockRead(STQ, Long4, 1);
ReservedLong := (LongInt(Long1) + (LongInt(Long2) * 256)) + ((LongInt(Long3) + (LongInt(Long4) * 256)) * 65536);
BlockRead(STQ, Long1, 1); BlockRead(STQ, Long2, 1);
BlockRead(STQ, Long3, 1); BlockRead(STQ, Long4, 1);
PackRecovery := (LongInt(Long1) + (LongInt(Long2) * 256)) + ((LongInt(Long3) + (LongInt(Long4) * 256)) * 65536);
Seek(STQ, 1024); {0-based => pos = 1025}
ValidQueue := True;
End
Else
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'tFDOutbound: Invalid Queue revision!');
ValidQueue := False;
End;
End
Else
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'tFDOutbound: Invalid Queue signature!');
ValidQueue := False;
End;
End;
Procedure tFDOutbound.WriteHdr;
Const Sig: String[22] = 'FrontDoor File Queue'#26#00;
Var
Maj, Min: Byte;
Long1, Long2, Long3, Long4: Byte;
i: Word;
HdrBuf: Array[0..1023] of Byte;
Begin
{Signature}
for i := 1 to 22 do HdrBuf[i - 1] := Byte(Sig[i]);
{rev $0100}
HdrBuf[22] := $00;
HdrBuf[23] := $01;
{TimeCreated}
Long1 := TimeCreated mod 256; Long2 := (TimeCreated div 256) mod 256;
Long3 := (TimeCreated div 65536) mod 256; Long4 := (TimeCreated div 16777216);
HdrBuf[24] := Long1; HdrBuf[25] := Long2;
HdrBuf[26] := Long3; HdrBuf[27] := Long4;
{TimePacked}
Long1 := TimePacked mod 256; Long2 := (TimePacked div 256) mod 256;
Long3 := (TimePacked div 65536) mod 256; Long4 := (TimePacked div 16777216);
HdrBuf[28] := Long1; HdrBuf[29] := Long2;
HdrBuf[30] := Long3; HdrBuf[31] := Long4;
{ReservedLong}
Long1 := 0; Long2 := 0; Long3 := 0; Long4 := 0;
HdrBuf[32] := Long1; HdrBuf[33] := Long2;
HdrBuf[34] := Long3; HdrBuf[35] := Long4;
{PackRecovery}
Long1 := 0; Long2 := 0; Long3 := 0; Long4 := 0;
HdrBuf[36] := Long1; HdrBuf[37] := Long2;
HdrBuf[38] := Long3; HdrBuf[39] := Long4;
{fill up to 1024 Bytes}
For i := 40 to 1023 do HdrBuf[i] := 0;
BlockWrite(STQ, HdrBuf, 1024);
ValidQueue := True;
End;
Procedure tFDOutbound.ReadEntry;
Var
EntryLen: Word;
Min, Maj: Byte;
Long1, Long2, Long3, Long4: Byte;
DT: TimeTyp;
Begin
BlockRead(STQ, Min, 1);
BlockRead(STQ, Maj, 1);
EntryLen := Word(Min) + (Word(Maj) * 256);
If (EntryLen >= 15) then
Begin
BlockRead(STQ, Long1, 1); BlockRead(STQ, Long2, 1);
BlockRead(STQ, Long3, 1); BlockRead(STQ, Long4, 1);
EntryTime := (LongInt(Long1) + (LongInt(Long2) * 256)) + ((LongInt(Long3) + (LongInt(Long4) * 256)) * 65536);
UnixToDT(EntryTime, DT);
BlockRead(STQ, Long1, 1); BlockRead(STQ, Long2, 1);
BlockRead(STQ, Long3, 1); BlockRead(STQ, Long4, 1);
Flags := (LongInt(Long1) + (LongInt(Long2) * 256)) + ((LongInt(Long3) + (LongInt(Long4) * 256)) * 65536);
BlockRead(STQ, Long1, 1); BlockRead(STQ, Long2, 1);
BlockRead(STQ, Long3, 1); BlockRead(STQ, Long4, 1);
TimeStamp := (LongInt(Long1) + (LongInt(Long2) * 256)) + ((LongInt(Long3) + (LongInt(Long4) * 256)) * 65536);
UnixToDT(TimeStamp, DT);
BlockRead(STQ, Address[0], 1);
If (Byte(Address[0]) > 0) then BlockRead(STQ, Address[1], Byte(Address[0]));
If (EntryLen > (Byte(Address[0])+14)) then
Begin
BlockRead(STQ, Filename[0], 1);
If (Byte(Filename[0]) > 0) then BlockRead(STQ, Filename[1], Byte(Filename[0]));
If (EntryLen >= (Byte(Address[0])+Byte(Filename[0])+15)) then
Begin
BlockRead(STQ, TFA[0], 1);
If (EntryLen = (Byte(Address[0])+Byte(Filename[0])+15)) then TFA[0] := Char(0);
If (Byte(TFA[0]) > 0) then BlockRead(STQ, TFA[1], Byte(TFA[0]));
{skip last bytes if entry is too long}
If (EntryLen > (Byte(Address[0])+Byte(Filename[0])+Byte(TFA[0])+15)) then
Begin
Seek(STQ, FilePos(STQ)+EntryLen-(Byte(Address[0])+Byte(Filename[0])+Byte(TFA[0])+15));
LogSetCurLevel(lh, 2);
LogWriteLn(lh, 'tFDOutbound: skipped '+IntToStr(EntryLen-(Byte(Address[0])+
Byte(Filename[0])+Byte(TFA[0])+15))+' Bytes of garbage.');
End;
End
Else TFA[0] := Char(0);
End;
End
Else
Begin
LogSetCurLevel(lh, 2);
LogWriteLn(lh, 'tFDOutbound: Entry too small => skipping '+IntToStr(EntryLen)+
' Bytes.');
Seek(STQ, FilePos(STQ)+EntryLen);
End;
End;
Procedure tFDOutbound.WriteEntry;
Var
EntryLen: Word;
Min, Maj: Byte;
Long1, Long2, Long3, Long4: Byte;
EntryBuf: PChar2;
EntryPos: Word;
i: Word;
Begin
{EntryLen}
EntryLen := (Byte(Address[0])+Byte(Filename[0])+Byte(TFA[0])+15);
GetMem(EntryBuf, EntryLen+2);
Min := EntryLen mod 256; Maj := (EntryLen div 256);
EntryBuf^[0] := Char(Min); EntryBuf^[1] := Char(Maj);
Long1 := EntryTime mod 256; Long2 := (EntryTime div 256) mod 256;
Long3 := (EntryTime div 65536) mod 256; Long4 := (EntryTime div 16777216);
EntryBuf^[2] := Char(Long1); EntryBuf^[3] := Char(Long2);
EntryBuf^[4] := Char(Long3); EntryBuf^[5] := Char(Long4);
Long1 := Flags mod 256; Long2 := (Flags div 256) mod 256;
Long3 := (Flags div 65536) mod 256; Long4 := (Flags div 16777216);
EntryBuf^[6] := Char(Long1); EntryBuf^[7] := Char(Long2);
EntryBuf^[8] := Char(Long3); EntryBuf^[9] := Char(Long4);
Long1 := TimeStamp mod 256; Long2 := (TimeStamp div 256) mod 256;
Long3 := (TimeStamp div 65536) mod 256; Long4 := (TimeStamp div 16777216);
EntryBuf^[10] := Char(Long1); EntryBuf^[11] := Char(Long2);
EntryBuf^[12] := Char(Long3); EntryBuf^[13] := Char(Long4);
For i := 0 to Byte(Address[0]) do EntryBuf^[14+i] := Address[i];
EntryPos := 15+Byte(Address[0]);
For i := 0 to Byte(Filename[0]) do EntryBuf^[EntryPos+i] := Filename[i];
EntryPos := EntryPos + Byte(Filename[0])+1;
For i := 0 to Byte(TFA[0]) do EntryBuf^[EntryPos+i] := TFA[i];
BlockWrite(STQ, EntryBuf^, EntryLen+2);
FreeMem(EntryBuf, EntryLen+2);
End;
Function tFDOutbound.OpenSTQ: Boolean;
Var
Error: Integer;
DT: TimeTyp;
Begin
OpenSTQ := False;
Assign(STQ, STQFile);
{$I-} ReSet(STQ, 1); {$I+}
Error := IOResult;
If (Error <> 0) then
Begin
If (Error = 5) then {file locked}
Begin
Delay(5);
Assign(STQ, STQFile);
{$I-} ReSet(STQ, 1); {$I+}
Error := IOResult;
If (Error <> 0) then
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'tFDOutbound: Could not open "'+STQFile+'": Error #'+
IntToStr(Error)+'!');
Exit;
End;
End
Else If (Error = 2) then {file not found}
Begin
If FileExist(LCKFile) then
Begin
Delay(10);
Assign(STQ, STQFile);
{$I-} ReSet(STQ, 1); {$I+}
Error := IOResult;
If (Error <> 0) then
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'tFDOutbound: Could not open "'+STQFile+'": Error #'+
IntToStr(Error)+'!');
Exit;
End;
End
Else
Begin
{$I-} ReWrite(STQ, 1); {$I+}
Error := IOResult;
If (Error <> 0) then
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'tFDOutbound: Could not create "'+STQFile+'": Error #'+
IntToStr(Error)+'!');
Exit;
End;
Now(DT); TimeCreated := DTToUnixDate(DT); TimePacked := TimeCreated;
LogSetCurLevel(lh, 3);
LogWriteLn(lh, 'Creating new STQ');
WriteHdr;
Close(STQ);
{$IfDef UNIX}
ChMod(STQFile, FilePerm);
{$EndIf}
ReSet(STQ, 1);
End;
End
Else
Begin
LogSetCurLevel(lh, 1);
LogWriteLn(lh, 'tFDOutbound: Could not open "'+STQFile+'": Error #'+
IntToStr(Error)+'!');
Exit;
End;
End;
OpenSTQ := True;
End;
Procedure tFDOutbound.ForceRescan;
Begin
CreateSem(FlagDir + 'FDRESCAN.NOW');
End;
Function tFDOutbound.FileBusy(FName: String): Boolean;
Var
Addr: TNetAddr;
Begin
If not OpenSTQ then Exit;
ReadHdr;
If (not EOF(STQ)) then
Begin
Repeat
ReadEntry;
Str2Addr(Address, Addr);