forked from g8bpq/OldLinBPQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BBSUtilities.c
10394 lines (7773 loc) · 216 KB
/
BBSUtilities.c
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
/*
Copyright 2001-2015 John Wiseman G8BPQ
This file is part of LinBPQ/BPQ32.
LinBPQ/BPQ32 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LinBPQ/BPQ32 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LinBPQ/BPQ32. If not, see http://www.gnu.org/licenses
*/
// Mail and Chat Server for BPQ32 Packet Switch
//
// Utility Routines
#include "BPQMail.h"
#ifdef WIN32
#include "Winspool.h"
#endif
BOOL Bells;
BOOL FlashOnBell; // Flash instead of Beep
BOOL StripLF;
BOOL WarnWrap;
BOOL FlashOnConnect;
BOOL WrapInput;
BOOL CloseWindowOnBye;
RECT ConsoleRect;
BOOL OpenConsole;
BOOL OpenMon;
//#define BBSIDLETIME 120
//#define USERIDLETIME 300
#define BBSIDLETIME 900
#define USERIDLETIME 900
unsigned long _beginthread( void( *start_address )(VOID * DParam),
unsigned stack_size, VOID * DParam);
int APIENTRY GetRaw(int stream, char * msg, int * len, int * count);
void GetSemaphore(struct SEM * Semaphore, int ID);
void FreeSemaphore(struct SEM * Semaphore);
int EncryptPass(char * Pass, char * Encrypt);
VOID DecryptPass(char * Encrypt, unsigned char * Pass, unsigned int len);
void DeletetoRecycle(char * FN);
VOID DoImportCmd(CIRCUIT * conn, struct UserInfo * user, char * Arg1, char * Context);
VOID DoExportCmd(CIRCUIT * conn, struct UserInfo * user, char * Arg1, char * Context);
VOID TidyPrompts();
char * ReadMessageFileEx(struct MsgInfo * MsgRec);
UCHAR * APIENTRY GetBPQDirectory();
BOOL SendARQMail(CIRCUIT * conn);
int APIENTRY ChangeSessionIdletime(int Stream, int idletime);
int APIENTRY GetApplNum(int Stream);
VOID FormatTime(char * Time, time_t cTime);
BOOL CheckifPacket(char * Via);
UCHAR * APIENTRY GetVersionString();
void ListFiles(ConnectionInfo * conn, struct UserInfo * user, char * filename);
void ReadBBSFile(ConnectionInfo * conn, struct UserInfo * user, char * filename);
int GetCMSHash(char * Challenge, char * Password);
BOOL SendAMPRSMTP(CIRCUIT * conn);
config_t cfg;
config_setting_t * group;
extern ULONG BBSApplMask;
//static int SEMCLASHES = 0;
char SecureMsg[80] = ""; // CMS Secure Signon Response
int NumberofStreams;
extern char VersionStringWithBuild[50];
#define MaxSockets 64
extern struct SEM OutputSEM;
extern ConnectionInfo Connections[MaxSockets+1];
extern struct UserInfo ** UserRecPtr;
extern int NumberofUsers;
extern struct UserInfo * BBSChain; // Chain of users that are BBSes
extern struct MsgInfo ** MsgHddrPtr;
extern int NumberofMessages;
extern int FirstMessageIndextoForward; // Lowest Message wirh a forward bit set - limits search
extern char UserDatabaseName[MAX_PATH];
extern char UserDatabasePath[MAX_PATH];
extern char MsgDatabasePath[MAX_PATH];
extern char MsgDatabaseName[MAX_PATH];
extern char BIDDatabasePath[MAX_PATH];
extern char BIDDatabaseName[MAX_PATH];
extern char WPDatabasePath[MAX_PATH];
extern char WPDatabaseName[MAX_PATH];
extern char BadWordsPath[MAX_PATH];
extern char BadWordsName[MAX_PATH];
extern char BaseDir[MAX_PATH];
extern char BaseDirRaw[MAX_PATH]; // As set in registry - may contain %NAME%
extern char ProperBaseDir[MAX_PATH]; // BPQ Directory/BPQMailChat
extern char MailDir[MAX_PATH];
extern BIDRec ** BIDRecPtr;
extern int NumberofBIDs;
extern BIDRec ** TempBIDRecPtr;
extern int NumberofTempBIDs;
extern WPRec ** WPRecPtr;
extern int NumberofWPrecs;
extern char ** BadWords;
extern int NumberofBadWords;
extern char * BadFile;
extern int LatestMsg;
extern struct SEM MsgNoSemaphore; // For locking updates to LatestMsg
extern int HighestBBSNumber;
extern int MaxMsgno;
extern int BidLifetime;
extern int MaxAge;
extern int MaintInterval;
extern int MaintTime;
extern int ProgramErrors;
extern BOOL MonBBS;
extern BOOL MonCHAT;
extern BOOL MonTCP;
BOOL SendNewUserMessage = TRUE;
BOOL AllowAnon = FALSE;
#define BPQHOSTSTREAMS 64
// Although externally streams are numbered 1 to 64, internally offsets are 0 - 63
extern BPQVECSTRUC BPQHOSTVECTOR[BPQHOSTSTREAMS + 5];
FILE * LogHandle[4] = {NULL, NULL, NULL, NULL};
char FilesNames[4][100] = {"", "", "", ""};
char * Logs[4] = {"BBS", "CHAT", "TCP", "DEBUG"};
BOOL OpenLogfile(int Flags)
{
UCHAR FN[MAX_PATH];
time_t LT;
struct tm * tm;
LT = time(NULL);
tm = gmtime(<);
sprintf(FN,"%s/logs/log_%02d%02d%02d_%s.txt", GetBPQDirectory(), tm->tm_year-100, tm->tm_mon+1, tm->tm_mday, Logs[Flags]);
LogHandle[Flags] = fopen(FN, "ab");
#ifndef WIN32
if (strcmp(FN, &FilesNames[Flags][0]))
{
UCHAR SYMLINK[MAX_PATH];
sprintf(SYMLINK,"%s/logLatest_%s.txt", GetBPQDirectory(), Logs[Flags]);
unlink(SYMLINK);
strcpy(&FilesNames[Flags][0], FN);
symlink(FN, SYMLINK);
}
#endif
return (LogHandle[Flags] != NULL);
}
struct SEM LogSEM = {0, 0};
void WriteLogLine(CIRCUIT * conn, int Flag, char * Msg, int MsgLen, int Flags)
{
char CRLF[2] = {0x0d,0x0a};
struct tm * tm;
char Stamp[20];
time_t LT;
// struct _EXCEPTION_POINTERS exinfo;
#ifndef LINBPQ
__try
{
#endif
#ifndef LINBPQ
if (hMonitor)
{
if (Flags == LOG_TCP && MonTCP)
{
WritetoMonitorWindow((char *)&Flag, 1);
WritetoMonitorWindow(Msg, MsgLen);
WritetoMonitorWindow(CRLF , 1);
}
else if (Flags == LOG_CHAT && MonCHAT)
{
WritetoMonitorWindow((char *)&Flag, 1);
if (conn && conn->Callsign[0])
{
char call[20];
sprintf(call, "%s ", conn->Callsign);
WritetoMonitorWindow(call, 10);
}
else
WritetoMonitorWindow(" ", 10);
WritetoMonitorWindow(Msg, MsgLen);
if (Msg[MsgLen-1] != '\r')
WritetoMonitorWindow(CRLF , 1);
}
else if (Flags == LOG_BBS && MonBBS)
{
WritetoMonitorWindow((char *)&Flag, 1);
if (conn && conn->Callsign[0])
{
char call[20];
sprintf(call, "%s ", conn->Callsign);
WritetoMonitorWindow(call, 10);
}
else
WritetoMonitorWindow(" ", 10);
WritetoMonitorWindow(Msg, MsgLen);
WritetoMonitorWindow(CRLF , 1);
}
else if (Flags == LOG_DEBUG_X)
{
WritetoMonitorWindow((char *)&Flag, 1);
WritetoMonitorWindow(Msg, MsgLen);
WritetoMonitorWindow(CRLF , 1);
}
}
#endif
if (Flags == LOG_TCP && !LogTCP)
return;
if (Flags == LOG_BBS && !LogBBS)
return;
if (Flags == LOG_CHAT && !LogCHAT)
return;
GetSemaphore(&LogSEM, 0);
if (LogHandle[Flags] == NULL) OpenLogfile(Flags);
if (LogHandle[Flags] == NULL)
{
FreeSemaphore(&LogSEM);
return;
}
LT = time(NULL);
tm = gmtime(<);
sprintf(Stamp,"%02d%02d%02d %02d:%02d:%02d %c",
tm->tm_year-100, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, Flag);
fwrite(Stamp, 1, strlen(Stamp), LogHandle[Flags]);
if (conn && conn->Callsign[0])
{
char call[20];
sprintf(call, "%s ", conn->Callsign);
fwrite(call, 1, 10, LogHandle[Flags]);
}
else
fwrite(" ", 1, 10, LogHandle[Flags]);
fwrite(Msg, 1, MsgLen, LogHandle[Flags]);
if (Flags == LOG_CHAT && Msg[MsgLen-1] == '\r')
fwrite(&CRLF[1], 1, 1, LogHandle[Flags]);
else
fwrite(CRLF, 1, 2, LogHandle[Flags]);
if (LogHandle[Flags])
fclose(LogHandle[Flags]);
LogHandle[Flags] = NULL;
FreeSemaphore(&LogSEM);
#ifndef LINBPQ
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
}
#endif
}
int CriticalErrorHandler(char * error)
{
Debugprintf("Critical Error %s", error);
ProgramErrors = 25;
CheckProgramErrors(); // Force close
return 0;
}
VOID __cdecl Debugprintf(const char * format, ...)
{
char Mess[1000];
va_list(arglist);int Len;
va_start(arglist, format);
Len = vsprintf(Mess, format, arglist);
#ifndef LINBPQ
WriteLogLine(NULL, '!',Mess, Len, LOG_DEBUG_X);
#endif
// #ifdef _DEBUG
strcat(Mess, "\r\n");
OutputDebugString(Mess);
// #endif
return;
}
VOID __cdecl Logprintf(int LogMode, CIRCUIT * conn, int InOut, const char * format, ...)
{
char Mess[1000];
va_list(arglist);int Len;
va_start(arglist, format);
Len = vsprintf(Mess, format, arglist);
WriteLogLine(conn, InOut, Mess, Len, LogMode);
return;
}
struct MsgInfo * GetMsgFromNumber(int msgno)
{
if (msgno < 1 || msgno > 999999)
return NULL;
return MsgnotoMsg[msgno];
}
struct UserInfo * AllocateUserRecord(char * Call)
{
struct UserInfo * User = zalloc(sizeof (struct UserInfo));
strcpy(User->Call, Call);
User->Length = sizeof (struct UserInfo);
GetSemaphore(&AllocSemaphore, 0);
UserRecPtr=realloc(UserRecPtr,(++NumberofUsers+1)*4);
UserRecPtr[NumberofUsers]= User;
FreeSemaphore(&AllocSemaphore);
return User;
}
struct MsgInfo * AllocateMsgRecord()
{
struct MsgInfo * Msg = zalloc(sizeof (struct MsgInfo));
GetSemaphore(&AllocSemaphore, 0);
MsgHddrPtr=realloc(MsgHddrPtr,(++NumberofMessages+1)*4);
MsgHddrPtr[NumberofMessages] = Msg;
FreeSemaphore(&AllocSemaphore);
return Msg;
}
BIDRec * AllocateBIDRecord()
{
BIDRec * BID = zalloc(sizeof (BIDRec));
GetSemaphore(&AllocSemaphore, 0);
BIDRecPtr=realloc(BIDRecPtr,(++NumberofBIDs+1)*4);
BIDRecPtr[NumberofBIDs] = BID;
FreeSemaphore(&AllocSemaphore);
return BID;
}
BIDRec * AllocateTempBIDRecord()
{
BIDRec * BID = zalloc(sizeof (BIDRec));
GetSemaphore(&AllocSemaphore, 0);
TempBIDRecPtr=realloc(TempBIDRecPtr,(++NumberofTempBIDs+1)*4);
TempBIDRecPtr[NumberofTempBIDs] = BID;
FreeSemaphore(&AllocSemaphore);
return BID;
}
struct UserInfo * LookupCall(char * Call)
{
struct UserInfo * ptr = NULL;
int i;
for (i=1; i <= NumberofUsers; i++)
{
ptr = UserRecPtr[i];
if (_stricmp(ptr->Call, Call) == 0) return ptr;
}
return NULL;
}
VOID GetUserDatabase()
{
struct UserInfo UserRec;
FILE * Handle;
int ReadLen;
struct UserInfo * user;
time_t UserLimit = time(NULL) - (UserLifetime * 86400); // Oldest user to keep
Handle = fopen(UserDatabasePath, "rb");
if (Handle == NULL)
{
// Initialise a new File
UserRecPtr=malloc(4);
UserRecPtr[0]= malloc(sizeof (struct UserInfo));
memset(UserRecPtr[0], 0, sizeof (struct UserInfo));
UserRecPtr[0]->Length = sizeof (struct UserInfo);
NumberofUsers = 0;
return;
}
// Get First Record
ReadLen = fread(&UserRec, 1, sizeof (UserRec), Handle);
if (ReadLen == 0)
{
// Duff file
memset(&UserRec, 0, sizeof (struct UserInfo));
UserRec.Length = sizeof (struct UserInfo);
}
else
{
// See if format has changed
if (UserRec.Length == 0)
{
// Old format without a Length field
struct OldUserInfo * OldRec = (struct OldUserInfo *)&UserRec;
int Users = OldRec->ConnectsIn; // User Count in control record
char Backup1[MAX_PATH];
// Create a backup in case reversion is needed and Reposition to first User record
fclose(Handle);
strcpy(Backup1, UserDatabasePath);
strcat(Backup1, ".oldformat");
CopyFile(UserDatabasePath, Backup1, FALSE); // Copy to .bak
Handle = fopen(UserDatabasePath, "rb");
ReadLen = fread(&UserRec, 1, sizeof (struct OldUserInfo), Handle); // Skip Control Record
// Set up control record
UserRecPtr=malloc(4);
UserRecPtr[0]= malloc(sizeof (struct UserInfo));
memcpy(UserRecPtr[0], &UserRec, sizeof (UserRec));
UserRecPtr[0]->Length = sizeof (UserRec);
NumberofUsers = 0;
OldNext:
ReadLen = fread(&UserRec, 1, sizeof (struct OldUserInfo), Handle);
if (ReadLen > 0)
{
if (OldRec->Call[0] < '0')
goto OldNext; // Blank record
user = AllocateUserRecord(OldRec->Call);
user->Temp = zalloc(sizeof (struct TempUserInfo));
// Copy info from Old record
user->lastmsg = OldRec->lastmsg;
user->Total.ConnectsIn = OldRec->ConnectsIn;
user->TimeLastConnected = OldRec->TimeLastConnected;
user->flags = OldRec->flags;
user->PageLen = OldRec->PageLen;
user->BBSNumber = OldRec->BBSNumber;
memcpy(user->Name, OldRec->Name, 18);
memcpy(user->Address, OldRec->Address, 61);
user->Total.MsgsReceived[0] = OldRec->MsgsReceived;
user->Total.MsgsSent[0] = OldRec->MsgsSent;
user->Total.MsgsRejectedIn[0] = OldRec->MsgsRejectedIn; // Messages we reject
user->Total.MsgsRejectedOut[0] = OldRec->MsgsRejectedOut; // Messages Rejectd by other end
user->Total.BytesForwardedIn[0] = OldRec->BytesForwardedIn;
user->Total.BytesForwardedOut[0] = OldRec->BytesForwardedOut;
user->Total.ConnectsOut = OldRec->ConnectsOut; // Forwarding Connects Out
user->RMSSSIDBits = OldRec->RMSSSIDBits; // SSID's to poll in RMS
memcpy(user->HomeBBS, OldRec->HomeBBS, 41);
memcpy(user->QRA, OldRec->QRA, 7);
memcpy(user->pass, OldRec->pass, 13);
memcpy(user->ZIP, OldRec->ZIP, 9);
// Read any forwarding info, even if not a BBS.
// This allows a BBS to be temporarily set as a
// normal user without loosing forwarding info
SetupForwardingStruct(user);
if (user->flags & F_BBS)
{
// Defined as BBS - allocate and initialise forwarding structure
// Add to BBS Chain;
user->BBSNext = BBSChain;
BBSChain = user;
// Save Highest BBS Number
if (user->BBSNumber > HighestBBSNumber) HighestBBSNumber = user->BBSNumber;
}
goto OldNext;
}
SortBBSChain();
fclose(Handle);
return;
}
}
// Set up control record
UserRecPtr=malloc(4);
UserRecPtr[0]= malloc(sizeof (struct UserInfo));
memcpy(UserRecPtr[0], &UserRec, sizeof (UserRec));
UserRecPtr[0]->Length = sizeof (UserRec);
NumberofUsers = 0;
Next:
ReadLen = fread(&UserRec, 1, sizeof (UserRec), Handle);
if (ReadLen > 0)
{
if (UserRec.Call[0] < '0')
goto Next; // Blank record
if ((UserRec.flags & F_BBS) == 0) // Not BBS - Check Age
if (UserLifetime) // if limit set
if (UserRec.TimeLastConnected) // Dont delete manually added Users that havent yet connected
if (UserRec.TimeLastConnected < UserLimit)
goto Next; // Too Old - ignore
user = AllocateUserRecord(UserRec.Call);
memcpy(user, &UserRec, sizeof (UserRec));
user->Temp = zalloc(sizeof (struct TempUserInfo));
user->ForwardingInfo = NULL; // In case left behind on crash
user->BBSNext = NULL;
user->POP3Locked = FALSE;
if (user->lastmsg < 0 || user->lastmsg > LatestMsg)
user->lastmsg = LatestMsg;
// Read any forwarding info, even if not a BBS.
// This allows a BBS to be temporarily set as a
// normal user without loosing forwarding info
SetupForwardingStruct(user);
if (user->flags & F_BBS)
{
// Add to BBS Chain;
user->BBSNext = BBSChain;
BBSChain = user;
// Save Highest BBS Number
if (user->BBSNumber > HighestBBSNumber) HighestBBSNumber = user->BBSNumber;
}
goto Next;
}
SortBBSChain();
fclose(Handle);
}
VOID CopyUserDatabase()
{
char Backup1[MAX_PATH];
char Backup2[MAX_PATH];
// Keep 4 Generations
strcpy(Backup2, UserDatabasePath);
strcat(Backup2, ".bak.3");
strcpy(Backup1, UserDatabasePath);
strcat(Backup1, ".bak.2");
DeleteFile(Backup2); // Remove old .bak.3
MoveFile(Backup1, Backup2); // Move .bak.2 to .bak.3
strcpy(Backup2, UserDatabasePath);
strcat(Backup2, ".bak.1");
MoveFile(Backup2, Backup1); // Move .bak.1 to .bak.2
strcpy(Backup1, UserDatabasePath);
strcat(Backup1, ".bak");
MoveFile(Backup1, Backup2); //Move .bak to .bak.1
CopyFile(UserDatabasePath, Backup1, FALSE); // Copy to .bak
}
VOID CopyConfigFile(char * ConfigName)
{
char Backup1[MAX_PATH];
char Backup2[MAX_PATH];
// Keep 4 Generations
strcpy(Backup2, ConfigName);
strcat(Backup2, ".bak.3");
strcpy(Backup1, ConfigName);
strcat(Backup1, ".bak.2");
DeleteFile(Backup2); // Remove old .bak.3
MoveFile(Backup1, Backup2); // Move .bak.2 to .bak.3
strcpy(Backup2, ConfigName);
strcat(Backup2, ".bak.1");
MoveFile(Backup2, Backup1); // Move .bak.1 to .bak.2
strcpy(Backup1, ConfigName);
strcat(Backup1, ".bak");
MoveFile(Backup1, Backup2); // Move .bak to .bak.1
CopyFile(ConfigName, Backup1, FALSE); // Copy to .bak
}
VOID SaveUserDatabase()
{
FILE * Handle;
int WriteLen;
int i;
Handle = fopen(UserDatabasePath, "wb");
UserRecPtr[0]->Total.ConnectsIn = NumberofUsers;
for (i=0; i <= NumberofUsers; i++)
{
WriteLen = fwrite(UserRecPtr[i], 1, sizeof (struct UserInfo), Handle);
}
fclose(Handle);
return;
}
VOID GetMessageDatabase()
{
struct MsgInfo MsgRec;
FILE * Handle;
int ReadLen;
struct MsgInfo * Msg;
char * MsgBytes;
int FileRecsize = sizeof(struct MsgInfo); // May be changed if reformating
BOOL Reformatting = FALSE;
Handle = fopen(MsgDatabasePath, "rb");
if (Handle == NULL)
{
// Initialise a new File
MsgHddrPtr=malloc(4);
MsgHddrPtr[0]= zalloc(sizeof (MsgRec));
NumberofMessages = 0;
MsgHddrPtr[0]->status = 1;
return;
}
// Get First Record
ReadLen = fread(&MsgRec, 1, FileRecsize, Handle);
if (ReadLen == 0)
{
// Duff file
memset(&MsgRec, 0, sizeof (MsgRec));
MsgRec.status = 1;
}
// Set up control record
MsgHddrPtr=malloc(4);
MsgHddrPtr[0]= malloc(sizeof (MsgRec));
memcpy(MsgHddrPtr[0], &MsgRec, sizeof (MsgRec));
LatestMsg=MsgHddrPtr[0]->length;
NumberofMessages = 0;
if (MsgRec.status == 1) // Used as file format version
// 0 = original, 1 = Extra email from addr, 2 = More BBS's.
{
char Backup1[MAX_PATH];
// Create a backup in case reversion is needed and Reposition to first User record
fclose(Handle);
strcpy(Backup1, MsgDatabasePath);
strcat(Backup1, ".oldformat");
CopyFile(MsgDatabasePath, Backup1, FALSE); // Copy to .oldformat
Handle = fopen(MsgDatabasePath, "rb");
FileRecsize = sizeof(struct OldMsgInfo);
ReadLen = fread(&MsgRec, 1, FileRecsize, Handle);
MsgHddrPtr[0]->status = 2;
}
Next:
ReadLen = fread(&MsgRec, 1, FileRecsize, Handle);
if (ReadLen > 0)
{
// Validate Header
if (FileRecsize == sizeof(struct MsgInfo))
{
if (MsgRec.type == 0 || MsgRec.number == 0)
goto Next;
MsgBytes = ReadMessageFileEx(&MsgRec);
if (MsgBytes)
{
// MsgRec.length = strlen(MsgBytes);
free(MsgBytes);
}
else
goto Next;
Msg = AllocateMsgRecord();
memcpy(Msg, &MsgRec, +sizeof (MsgRec));
}
else
{
// Resizing - record from file is an OldRecInfo
struct OldMsgInfo * OldMessage = (struct OldMsgInfo *) &MsgRec;
if (OldMessage->type == 0)
goto Next;
Msg = AllocateMsgRecord();
Msg->B2Flags = OldMessage->B2Flags;
memcpy(Msg->bbsfrom, OldMessage->bbsfrom, 7);
memcpy(Msg->bid, OldMessage->bid, 13);
Msg->datechanged = OldMessage->datechanged;
Msg->datecreated = OldMessage->datecreated;
Msg->datereceived = OldMessage->datereceived;
memcpy(Msg->emailfrom, OldMessage->emailfrom, 41);
memcpy(Msg->fbbs , OldMessage->fbbs, 10);
memcpy(Msg->forw , OldMessage->forw, 10);
memcpy(Msg->from, OldMessage->from, 7);
Msg->length = OldMessage->length;
Msg->nntpnum = OldMessage->nntpnum;
Msg->number = OldMessage->number;
Msg->status = OldMessage->status;
memcpy(Msg->title, OldMessage->title, 61);
memcpy(Msg->to, OldMessage->to, 7);
Msg->type = OldMessage->type;
memcpy(Msg->via, OldMessage->via, 41);
}
MsgnotoMsg[Msg->number] = Msg;
// Fix Corrupted NTS Messages
if (Msg->type == 'N')
Msg->type = 'T';
// Look for corrupt FROM address (ending in @)
strlop(Msg->from, '@');
BuildNNTPList(Msg); // Build NNTP Groups list
Msg->Locked = 0; // In case left locked
Msg->Defered = 0; // In case left set.
// If any forward bits are set, increment count on corresponding BBS record.
if (memcmp(Msg->fbbs, zeros, NBMASK) != 0)
{
if (FirstMessageIndextoForward == 0)
FirstMessageIndextoForward = NumberofMessages; // limit search
}
goto Next;
}
if (FirstMessageIndextoForward == 0)
FirstMessageIndextoForward = NumberofMessages; // limit search
fclose(Handle);
}
VOID CopyMessageDatabase()
{
char Backup1[MAX_PATH];
char Backup2[MAX_PATH];
// Keep 4 Generations
strcpy(Backup2, MsgDatabasePath);
strcat(Backup2, ".bak.3");
strcpy(Backup1, MsgDatabasePath);
strcat(Backup1, ".bak.2");
DeleteFile(Backup2); // Remove old .bak.3
MoveFile(Backup1, Backup2); // Move .bak.2 to .bak.3
strcpy(Backup2, MsgDatabasePath);
strcat(Backup2, ".bak.1");
MoveFile(Backup2, Backup1); // Move .bak.1 to .bak.2
strcpy(Backup1, MsgDatabasePath);
strcat(Backup1, ".bak");
MoveFile(Backup1, Backup2); //Move .bak to .bak.1
strcpy(Backup2, MsgDatabasePath);
strcat(Backup2, ".bak");
CopyFile(MsgDatabasePath, Backup2, FALSE); // Copy to .bak
}
VOID SaveMessageDatabase()
{
FILE * Handle;
int WriteLen;
int i;
Handle = fopen(MsgDatabasePath, "wb");
MsgHddrPtr[0]->number = NumberofMessages;
MsgHddrPtr[0]->length = LatestMsg;
for (i=0; i <= NumberofMessages; i++)
{
WriteLen = fwrite(MsgHddrPtr[i], 1, sizeof (struct MsgInfo), Handle);
}
fclose(Handle);
return;
}
VOID GetBIDDatabase()
{
BIDRec BIDRec;
FILE * Handle;
int ReadLen;
BIDRecP BID;
Handle = fopen(BIDDatabasePath, "rb");
if (Handle == NULL)
{
// Initialise a new File
BIDRecPtr=malloc(4);
BIDRecPtr[0]= malloc(sizeof (BIDRec));
memset(BIDRecPtr[0], 0, sizeof (BIDRec));
NumberofBIDs = 0;
return;
}
// Get First Record
ReadLen = fread(&BIDRec, 1, sizeof (BIDRec), Handle);
if (ReadLen == 0)
{
// Duff file
memset(&BIDRec, 0, sizeof (BIDRec));
}
// Set up control record
BIDRecPtr=malloc(4);
BIDRecPtr[0]= malloc(sizeof (BIDRec));
memcpy(BIDRecPtr[0], &BIDRec, sizeof (BIDRec));
NumberofBIDs = 0;
Next:
ReadLen = fread(&BIDRec, 1, sizeof (BIDRec), Handle);
if (ReadLen > 0)
{
BID = AllocateBIDRecord();
memcpy(BID, &BIDRec, sizeof (BIDRec));
if (BID->u.timestamp == 0)
BID->u.timestamp = LOWORD(time(NULL)/86400);
goto Next;
}
fclose(Handle);
}
VOID CopyBIDDatabase()
{
char Backup[MAX_PATH];
strcpy(Backup, BIDDatabasePath);
strcat(Backup, ".bak");
CopyFile(BIDDatabasePath, Backup, FALSE);
}