forked from g8bpq/OldLinBPQ
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinBPQ.c
1453 lines (1060 loc) · 27.8 KB
/
LinBPQ.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
*/
// Control Routine for LinBPQ
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#include "CHeaders.h"
#include "BPQMail.h"
#ifdef WIN32
#include "C:\Program Files (x86)\GnuWin32\include\iconv.h"
#else
#include <iconv.h>
#if defined(__Linux__)
#include <sys/prctl.h>
#endif /* __Linux__ */
#endif
#include "ExtInit.h"
#define Connect(stream) SessionControl(stream,1,0)
#define Disconnect(stream) SessionControl(stream,2,0)
#define ReturntoNode(stream) SessionControl(stream,3,0)
#define ConnectUsingAppl(stream, appl) SessionControl(stream, 0, appl)
BOOL APIENTRY Rig_Init();
void GetSemaphore(struct SEM * Semaphore, int ID);
void FreeSemaphore(struct SEM * Semaphore);
VOID CopyConfigFile(char * ConfigName);
VOID SendMailForThread(VOID * Param);
BOOL IncludesMail = FALSE;
BOOL IncludesChat = FALSE;
BOOL RunMail = FALSE;
BOOL RunChat = FALSE;
VOID Poll_AGW();
BOOL AGWAPIInit();
int AGWAPITerminate();
BOOL AGWActive = FALSE;
extern int AGWPort;
BOOL RigActive = FALSE;
extern ULONG ChatApplMask;
extern char Verstring[];
extern char SignoffMsg[];
extern char AbortedMsg[];
extern char InfoBoxText[]; // Text to display in Config Info Popup
extern int LastVer[4]; // In case we need to do somthing the first time a version is run
extern HWND MainWnd;
extern char BaseDir[];
extern char BaseDirRaw[];
extern char MailDir[];
extern char WPDatabasePath[];
extern char RlineVer[50];
extern BOOL LogBBS;
extern BOOL LogCHAT;
extern BOOL LogTCP;
extern BOOL ForwardToMe;
extern int LatestMsg;
extern char BBSName[];
extern char SYSOPCall[];
extern char BBSSID[];
extern char NewUserPrompt[];
extern int NumberofStreams;
extern int MaxStreams;
extern ULONG BBSApplMask;
extern int BBSApplNum;
extern int ChatApplNum;
extern int NUMBEROFTNCPORTS;
extern int EnableUI;
#define MaxSockets 64
extern ConnectionInfo Connections[MaxSockets+1];
int LastTrafficTime;
extern int MaintTime;
#define LOG_BBS 0
#define LOG_CHAT 1
#define LOG_TCP 2
#define LOG_DEBUG_X 3
int _MYTIMEZONE = 0;
// flags equates
#define F_Excluded 0x0001
#define F_LOC 0x0002
#define F_Expert 0x0004
#define F_SYSOP 0x0008
#define F_BBS 0x0010
#define F_PAG 0x0020
#define F_GST 0x0040
#define F_MOD 0x0080
#define F_PRV 0x0100
#define F_UNP 0x0200
#define F_NEW 0x0400
#define F_PMS 0x0800
#define F_EMAIL 0x1000
#define F_HOLDMAIL 0x2000
#define F_POLLRMS 0x4000
#define F_SYSOP_IN_LM 0x8000
#define F_Temp_B2_BBS 0x10000
/* #define F_PWD 0x1000 */
UCHAR BPQDirectory[260];
BOOL GetConfig(char * ConfigName);
VOID DecryptPass(char * Encrypt, unsigned char * Pass, unsigned int len);
int EncryptPass(char * Pass, char * Encrypt);
int APIENTRY FindFreeStream();
int PollStreams();
int APIENTRY SetAppl(int stream, int flags, int mask);
int APIENTRY SessionState(int stream, int * state, int * change);
int APIENTRY SessionControl(int stream, int command, int Mask);
BOOL ChatInit();
VOID CloseChat();
VOID CloseTNCEmulator();
config_t cfg;
config_setting_t * group;
BOOL MonBBS = TRUE;
BOOL MonCHAT = TRUE;
BOOL MonTCP = TRUE;
BOOL LogBBS = TRUE;
BOOL LogCHAT = TRUE;
BOOL LogTCP = TRUE;
BOOL UIEnabled[33];
BOOL UINull[33];
char * UIDigi[33];
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 NTSAliasesPath[MAX_PATH];
extern char NTSAliasesName[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 time_t MaintClock; // Time to run housekeeping
#ifdef WIN32
BOOL KEEPGOING = 30; // 5 secs to shut down
#else
BOOL KEEPGOING = 50; // 5 secs to shut down
#endif
BOOL Restarting = FALSE;
BOOL CLOSING = FALSE;
int ProgramErrors;
int Slowtimer = 0;
#define REPORTINTERVAL 15 * 549; // Magic Ticks Per Minute for PC's nominal 100 ms timer
int ReportTimer = 0;
VOID CheckProgramErrors()
{
if (Restarting)
exit(0); // Make sure can't loop in restarting
ProgramErrors++;
if (ProgramErrors > 25)
{
Restarting = TRUE;
Logprintf(LOG_DEBUG_X, NULL, '!', "Too Many Program Errors - Closing");
/*
SInfo.cb=sizeof(SInfo);
SInfo.lpReserved=NULL;
SInfo.lpDesktop=NULL;
SInfo.lpTitle=NULL;
SInfo.dwFlags=0;
SInfo.cbReserved2=0;
SInfo.lpReserved2=NULL;
GetModuleFileName(NULL, ProgName, 256);
Debugprintf("Attempting to Restart %s", ProgName);
CreateProcess(ProgName, "MailChat.exe WAIT", NULL, NULL, FALSE, 0, NULL, NULL, &SInfo, &PInfo);
*/
exit(0);
}
}
#ifdef WIN32
WINBASEAPI
HWND
APIENTRY
GetConsoleWindow(
VOID
);
BOOL CtrlHandler(DWORD fdwCtrlType)
{
switch( fdwCtrlType )
{
// Handle the CTRL-C signal.
case CTRL_C_EVENT:
printf( "Ctrl-C event\n\n" );
CLOSING = TRUE;
Beep( 750, 300 );
return( TRUE );
// CTRL-CLOSE: confirm that the user wants to exit.
case CTRL_CLOSE_EVENT:
CLOSING = TRUE;
printf( "Ctrl-Close event\n\n" );
Sleep(20000);
Beep( 750, 300 );
return( TRUE );
// Pass other signals to the next handler.
case CTRL_BREAK_EVENT:
Beep( 900, 200 );
printf( "Ctrl-Break event\n\n" );
CLOSING = TRUE;
Beep( 750, 300 );
return FALSE;
case CTRL_LOGOFF_EVENT:
Beep( 1000, 200 );
printf( "Ctrl-Logoff event\n\n" );
return FALSE;
case CTRL_SHUTDOWN_EVENT:
Beep( 750, 500 );
printf( "Ctrl-Shutdown event\n\n" );
CLOSING = TRUE;
Beep( 750, 300 );
return FALSE;
default:
return FALSE;
}
}
#else
// Linux Signal Handlers
static void sigterm_handler(int sig)
{
syslog(LOG_INFO, "terminating on SIGTERM\n");
CLOSING = TRUE;
}
static void sigint_handler(int sig)
{
printf("terminating on SIGINT\n");
CLOSING = TRUE;
}
static void sigusr1_handler(int sig)
{
signal(SIGUSR1, sigusr1_handler);
}
#endif
#ifndef WIN32
BOOL CopyFile(char * In, char * Out, BOOL Failifexists)
{
FILE * Handle;
DWORD FileSize;
char * Buffer;
struct stat STAT;
if (stat(In, &STAT) == -1)
return FALSE;
FileSize = STAT.st_size;
Handle = fopen(In, "rb");
if (Handle == NULL)
return FALSE;
Buffer = malloc(FileSize+1);
FileSize = fread(Buffer, 1, STAT.st_size, Handle);
fclose(Handle);
if (FileSize != STAT.st_size)
{
free(Buffer);
return FALSE;
}
Handle = fopen(Out, "wb");
if (Handle == NULL)
{
free(Buffer);
return FALSE;
}
FileSize = fwrite(Buffer, 1, STAT.st_size, Handle);
fclose(Handle);
free(Buffer);
return TRUE;
}
#endif
int RefreshMainWindow()
{
return 0;
}
int LastSemGets = 0;
extern int SemHeldByAPI;
VOID MonitorThread(int x)
{
// Thread to detect stuck semaphore
do
{
if (Semaphore.Gets == LastSemGets && Semaphore.Flag)
{
// It is stuck - try to release
Debugprintf ("Semaphore locked - Process ID = %d, Held By %d",
Semaphore.SemProcessID, SemHeldByAPI);
Semaphore.Flag = 0;
}
LastSemGets = Semaphore.Gets;
Sleep(30000);
} while (TRUE);
}
VOID TIMERINTERRUPT();
BOOL Start();
VOID INITIALISEPORTS();
Dll BOOL APIENTRY Init_APRS();
VOID APRSClose();
Dll VOID APIENTRY Poll_APRS();
VOID HTTPTimer();
Dll VOID APIENTRY md5 (char *arg, unsigned char * checksum);
#define CKernel
#include "Versions.h"
extern struct SEM Semaphore;
int SemHeldByAPI = 0;
BOOL IGateEnabled = TRUE;
BOOL APRSActive = FALSE;
BOOL ReconfigFlag;
BOOL IPActive = FALSE;
extern BOOL IPRequired;
extern struct WL2KInfo * WL2KReports;
int InitDone;
char pgm[256] = "LINBPQ";
char SESSIONHDDR[80] = "";
int SESSHDDRLEN = 0;
// Next 3 should be uninitialised so they are local to each process
UCHAR MCOM;
UCHAR MUIONLY;
UCHAR MTX;
ULONG MMASK;
UCHAR AuthorisedProgram; // Local Variable. Set if Program is on secure list
int SAVEPORT = 0;
VOID SetApplPorts();
char VersionString[50] = Verstring;
char VersionStringWithBuild[50]=Verstring;
int Ver[4] = {Vers};
char TextVerstring[50] = Verstring;
extern UCHAR PWLEN;
extern char PWTEXT[];
extern int ISPort;
extern char ChatConfigName[250];
char * GetBPQDirectory()
{
return BPQDirectory;
}
int main(int argc, char * argv[])
{
int i;
struct UserInfo * user = NULL;
ConnectionInfo * conn;
struct stat STAT;
PEXTPORTDATA PORTVEC;
UCHAR LogDir[260];
#ifdef WIN32
WSADATA WsaData; // receives data from WSAStartup
HWND hWnd = GetForegroundWindow();
WSAStartup(MAKEWORD(2, 0), &WsaData);
SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE);
// disable the [x] button.
if (hWnd != NULL)
{
HMENU hMenu = GetSystemMenu(hWnd, 0);
if (hMenu != NULL)
{
DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
DrawMenuBar(hWnd);
}
}
#else
openlog("LINBPQ", LOG_PID, LOG_DAEMON);
#if defined(__Linux__)
prctl(PR_SET_DUMPABLE, 1); // Enable Core Dumps even with setcap
#endif /* __Linux__ */
#endif
printf("G8BPQ AX25 Packet Switch System Version %s %s\n", TextVerstring, Datestring);
printf("%s\n", VerCopyright);
if (argc > 1 && _stricmp(argv[1], "-v") == 0)
return 0;
Debugprintf("G8BPQ AX25 Packet Switch System Version %s %s", TextVerstring, Datestring);
#if defined(__Linux__)
_MYTIMEZONE = timezone;
#endif /* __Linux__ */
if (_MYTIMEZONE < -86400 || _MYTIMEZONE > 86400)
_MYTIMEZONE = 0;
#ifdef WIN32
GetCurrentDirectory(256, BPQDirectory);
#else
getcwd(BPQDirectory, 256);
#endif
Consoleprintf("Current Directory is %s\n", BPQDirectory);
// Make sure logs directory exists
sprintf(LogDir, "%s/logs", BPQDirectory);
#ifdef WIN32
CreateDirectory(LogDir, NULL);
#else
mkdir(LogDir, S_IRWXU | S_IRWXG | S_IRWXO);
chmod(LogDir, S_IRWXU | S_IRWXG | S_IRWXO);
#endif
if (!ProcessConfig())
{
WritetoConsoleLocal("Configuration File Error\n");
return (0);
}
#ifdef MACBPQ
SESSHDDRLEN = sprintf(SESSIONHDDR, "G8BPQ Network System %s for MAC (", TextVerstring);
#else
SESSHDDRLEN = sprintf(SESSIONHDDR, "G8BPQ Network System %s for Linux (", TextVerstring);
#endif
GetSemaphore(&Semaphore, 0);
if (Start() != 0)
{
FreeSemaphore(&Semaphore);
return (0);
}
for (i=0;PWTEXT[i] > 0x20;i++); //Scan for cr or null
PWLEN=i;
SetApplPorts();
GetUIConfig();
INITIALISEPORTS();
if (IPRequired) IPActive = Init_IP();
APRSActive = Init_APRS();
if (ISPort == 0)
IGateEnabled = 0;
RigActive = Rig_Init();
FreeSemaphore(&Semaphore);
OpenReportingSockets();
InitDone = TRUE;
_beginthread(MonitorThread, 0, 0);
#ifdef WIN32
#else
openlog("LINBPQ", LOG_PID, LOG_DAEMON);
signal(SIGHUP, SIG_IGN);
signal(SIGINT, sigint_handler);
signal(SIGTERM, sigterm_handler);
signal(SIGPIPE, SIG_IGN);
#endif
if (IncludesChat ||
(argc > 1 && _stricmp(argv[1], "chat") == 0) ||
(argc > 2 && _stricmp(argv[2], "chat") == 0) ||
(argc > 3 && _stricmp(argv[3], "chat") == 0))
{
RunChat = IncludesChat = TRUE;
printf("Starting Chat\n");
sprintf (ChatConfigName, "%s/chatconfig.cfg", BPQDirectory);
printf("Config File is %s\n", ChatConfigName);
if (stat(ChatConfigName, &STAT) == -1)
{
printf("Chat Config File not found - creating a default config\n");
SaveChatConfig(ChatConfigName);
}
if (GetChatConfig(ChatConfigName) == EXIT_FAILURE)
{
printf("Chat Config File seems corrupt - check before continuing\n");
return -1;
}
if (ChatApplNum)
{
if (ChatInit() == 0)
{
printf("Chat Init Failed\n");
RunChat = 0;
}
else
{
printf("Chat Started\n");
}
}
else
{
printf("Chat APPLNUM not defined\n");
RunChat = 0;
}
CopyConfigFile(ChatConfigName);
}
// Start Mail if requested by command line or config
if (IncludesMail ||
(argc > 1 && _stricmp(argv[1], "mail") == 0) ||
(argc > 2 && _stricmp(argv[2], "mail") == 0) ||
(argc > 3 && _stricmp(argv[3], "mail") == 0))
{
RunMail = IncludesMail = TRUE;
printf("Starting Mail\n");
sprintf (ConfigName, "%s/linmail.cfg", BPQDirectory);
printf("Config File is %s\n", ConfigName);
if (stat(ConfigName, &STAT) == -1)
{
printf("Config File not found - creating a default config\n");
strcpy(BBSName, MYNODECALL);
strlop(BBSName, '-');
strlop(BBSName, ' ');
SaveConfig(ConfigName);
}
if (GetConfig(ConfigName) == EXIT_FAILURE)
{
printf("BBS Config File seems corrupt - check before continuing\n");
return -1;
}
printf("Config Processed\n");
BBSApplMask = 1<<(BBSApplNum-1);
// See if we need to warn of possible problem with BaseDir moved by installer
sprintf(BaseDir, "%s", BPQDirectory);
// Set up file and directory names
strcpy(UserDatabasePath, BaseDir);
strcat(UserDatabasePath, "/");
strcat(UserDatabasePath, UserDatabaseName);
strcpy(MsgDatabasePath, BaseDir);
strcat(MsgDatabasePath, "/");
strcat(MsgDatabasePath, MsgDatabaseName);
strcpy(BIDDatabasePath, BaseDir);
strcat(BIDDatabasePath, "/");
strcat(BIDDatabasePath, BIDDatabaseName);
strcpy(WPDatabasePath, BaseDir);
strcat(WPDatabasePath, "/");
strcat(WPDatabasePath, WPDatabaseName);
strcpy(BadWordsPath, BaseDir);
strcat(BadWordsPath, "/");
strcat(BadWordsPath, BadWordsName);
strcpy(NTSAliasesPath, BaseDir);
strcat(NTSAliasesPath, "/");
strcat(NTSAliasesPath, NTSAliasesName);
strcpy(MailDir, BaseDir);
strcat(MailDir, "/");
strcat(MailDir, "Mail");
#ifdef WIN32
CreateDirectory(MailDir, NULL); // Just in case
#else
mkdir(MailDir, S_IRWXU | S_IRWXG | S_IRWXO);
chmod(MailDir, S_IRWXU | S_IRWXG | S_IRWXO);
#endif
// Make backup copies of Databases
CopyConfigFile(ConfigName);
CopyBIDDatabase();
CopyMessageDatabase();
CopyUserDatabase();
CopyWPDatabase();
SetupMyHA();
SetupFwdAliases();
SetupNTSAliases(NTSAliasesPath);
GetWPDatabase();
GetMessageDatabase();
GetUserDatabase();
GetBIDDatabase();
GetBadWordFile();
// Make sure there is a user record for the BBS, with BBS bit set.
user = LookupCall(BBSName);
if (user == NULL)
{
user = AllocateUserRecord(BBSName);
user->Temp = zalloc(sizeof (struct TempUserInfo));
}
if ((user->flags & F_BBS) == 0)
{
// Not Defined as a BBS
if(SetupNewBBS(user))
user->flags |= F_BBS;
}
// if forwarding AMPR mail make sure User/BBS AMPR exists
if (SendAMPRDirect)
{
BOOL NeedSave = FALSE;
user = LookupCall("AMPR");
if (user == NULL)
{
user = AllocateUserRecord("AMPR");
user->Temp = zalloc(sizeof (struct TempUserInfo));
NeedSave = TRUE;
}
if ((user->flags & F_BBS) == 0)
{
// Not Defined as a BBS
if (SetupNewBBS(user))
user->flags |= F_BBS;
NeedSave = TRUE;
}
if (NeedSave)
SaveUserDatabase();
}
// Make sure SYSOPCALL is set
if (SYSOPCall[0] == 0)
strcpy(SYSOPCall, BBSName);
// Allocate Streams
strcpy(pgm, "BBS");
for (i=0; i < MaxStreams; i++)
{
conn = &Connections[i];
conn->BPQStream = FindFreeStream();
if (conn->BPQStream == 255) break;
NumberofStreams++;
// BPQSetHandle(conn->BPQStream, hWnd);
SetAppl(conn->BPQStream, (i == 0 && EnableUI) ? 0x82 : 2, BBSApplMask);
Disconnect(conn->BPQStream);
}
strcpy(pgm, "LINBPQ");
InitialiseTCP();
InitialiseNNTP();
SetupListenSet(); // Master set of listening sockets
if (EnableUI || MailForInterval)
SetupUIInterface();
if (MailForInterval)
_beginthread(SendMailForThread, 0, 0);
// Calulate time to run Housekeeping
{
struct tm *tm;
time_t now;
now = time(NULL);
tm = gmtime(&now);
tm->tm_hour = MaintTime / 100;
tm->tm_min = MaintTime % 100;
tm->tm_sec = 0;
MaintClock = mktime(tm) - (time_t)_MYTIMEZONE;
if (MaintClock < now)
MaintClock += 86400;
Debugprintf("Maint Clock %d NOW %d Time to HouseKeeping %d", MaintClock, now, MaintClock - now);
if (LastHouseKeepingTime)
{
if ((MaintClock - LastHouseKeepingTime) > 86400)
{
DoHouseKeeping(FALSE);
}
}
for (i = 1; i < argc; i++)
{
if (_stricmp(argv[i], "tidymail") == 0)
DeleteRedundantMessages();
if (_stricmp(argv[i], "nohomebbs") == 0)
DontNeedHomeBBS = TRUE;
}
printf("Mail Started\n");
}
}
if (NUMBEROFTNCPORTS)
InitializeTNCEmulator();
AGWActive = AGWAPIInit();
#ifndef WIN32
if ((argc > 1 && stricmp(argv[1], "daemon") == 0) ||
(argc > 2 && stricmp(argv[2], "daemon") == 0) ||
(argc > 3 && stricmp(argv[3], "daemon") == 0))
{
// Convert to daemon
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
if (pid > 0)
exit(EXIT_SUCCESS);
/* Change the file mode mask */
umask(0);
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0)
exit(EXIT_FAILURE);
/* Change the current working directory */
if ((chdir("/")) < 0)
exit(EXIT_FAILURE);
/* Close out the standard file descriptors */
printf("Entering daemon mode\n");
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
#endif
while (KEEPGOING)
{
Sleep(100);
GetSemaphore(&Semaphore, 2);
if (QCOUNT < 10)
{
if (CLOSING == FALSE)
FindLostBuffers();
CLOSING = TRUE;
}
if (CLOSING)
{
if (RunChat)
{
CloseChat();
RunChat = FALSE;
}
if (RunMail)
{
int BPQStream, n;
RunMail = FALSE;
for (n = 0; n < NumberofStreams; n++)
{
BPQStream = Connections[n].BPQStream;
if (BPQStream)
{
SetAppl(BPQStream, 0, 0);
Disconnect(BPQStream);
DeallocateStream(BPQStream);
}
}
SaveUserDatabase();
SaveMessageDatabase();
SaveBIDDatabase();
SaveConfig(ConfigName);
}
KEEPGOING--; // Give time for links to close
setbuf(stdout, NULL);
printf("Closing... %d \r", KEEPGOING);
}
if (ReconfigFlag)
{
int i;
BPQVECSTRUC * HOSTVEC;
PEXTPORTDATA PORTVEC=(PEXTPORTDATA)PORTTABLE;
ReconfigFlag = FALSE;
// SetupBPQDirectory();
WritetoConsoleLocal("Reconfiguring ...\n\n");
OutputDebugString("BPQ32 Reconfiguring ...\n");
for (i=0;i<NUMBEROFPORTS;i++)
{
if (PORTVEC->PORTCONTROL.PORTTYPE == 0x10) // External
{
if (PORTVEC->PORT_EXT_ADDR)
{
// SaveWindowPos(PORTVEC->PORTCONTROL.PORTNUMBER);
// SaveAXIPWindowPos(PORTVEC->PORTCONTROL.PORTNUMBER);
// CloseDriverWindow(PORTVEC->PORTCONTROL.PORTNUMBER);
PORTVEC->PORT_EXT_ADDR(5,PORTVEC->PORTCONTROL.PORTNUMBER, NULL, 0); // Close External Ports
}
}
PORTVEC->PORTCONTROL.PORTCLOSECODE(&PORTVEC->PORTCONTROL);
PORTVEC=(PEXTPORTDATA)PORTVEC->PORTCONTROL.PORTPOINTER;
}