forked from g8bpq/linbpq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatUtilities.c
369 lines (269 loc) · 6.67 KB
/
ChatUtilities.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
// Mail and Chat Server for BPQ32 Packet Switch
//
// Utility Routines
#include "BPQChat.h"
#include "Winspool.h"
int LogAge = 7;
int SEMCLASHES = 0;
VOID __cdecl Debugprintf(const char * format, ...)
{
char Mess[65536];
va_list(arglist);int Len;
va_start(arglist, format);
Len = vsprintf_s(Mess, sizeof(Mess), format, arglist);
ChatWriteLogLine(NULL, '!',Mess, Len, LOG_DEBUGx);
// #ifdef _DEBUG
strcat(Mess, "\r\n");
OutputDebugString(Mess);
// #endif
return;
}
VOID __cdecl Logprintf(int LogMode, ChatCIRCUIT * conn, int InOut, const char * format, ...)
{
char Mess[65536];
va_list(arglist);int Len;
va_start(arglist, format);
Len = vsprintf_s(Mess, sizeof(Mess), format, arglist);
ChatWriteLogLine(conn, InOut, Mess, Len, LogMode);
return;
}
void GetSemaphore(struct SEM * Semaphore, int ID)
{
//
// Wait for it to be free
//
if (Semaphore->Flag != 0)
{
Semaphore->Clashes++;
// Debugprintf("MailChat Semaphore Clash");
}
loop1:
while (Semaphore->Flag != 0)
{
Sleep(10);
}
//
// try to get semaphore
//
_asm{
mov eax,1
mov ebx, Semaphore
xchg [ebx],eax // this instruction is locked
cmp eax,0
jne loop1 // someone else got it - try again
;
; ok, we've got the semaphore
;
}
return;
}
void FreeSemaphore(struct SEM * Semaphore)
{
Semaphore->Flag=0;
return;
}
VOID __cdecl nodeprintf(ChatCIRCUIT * conn, const char * format, ...)
{
char Mess[65536];
int len;
va_list(arglist);
va_start(arglist, format);
len = vsprintf_s(Mess, sizeof(Mess), format, arglist);
ChatQueueMsg(conn, Mess, len);
ChatWriteLogLine(conn, '>',Mess, len-1, LOG_CHAT);
return;
}
// Costimised message handling routines.
/*
Variables - a subset of those used by FBB
$C : Number of the next message.
$I : First name of the connected user.
$L : Number of the latest message.
$N : Number of active messages.
$U : Callsign of the connected user.
$W : Inserts a carriage return.
$Z : Last message read by the user (L command).
%X : Number of messages for the user.
%x : Number of new messages for the user.
*/
VOID ExpandAndSendMessage(ChatCIRCUIT * conn, char * Msg, int LOG)
{
char NewMessage[10000];
char * OldP = Msg;
char * NewP = NewMessage;
char * ptr, * pptr;
int len;
char Dollar[] = "$";
char CR[] = "\r";
ptr = strchr(OldP, '$');
while (ptr)
{
len = ptr - OldP; // Chars before $
memcpy(NewP, OldP, len);
NewP += len;
switch (*++ptr)
{
case 'I': // First name of the connected user.
pptr = conn->UserPointer->Name;
break;
case 'W': // Inserts a carriage return.
pptr = CR;
break;
default:
pptr = Dollar; // Just Copy $
}
len = strlen(pptr);
memcpy(NewP, pptr, len);
NewP += len;
OldP = ++ptr;
ptr = strchr(OldP, '$');
}
strcpy(NewP, OldP);
len = RemoveLF(NewMessage, strlen(NewMessage));
ChatWriteLogLine(conn, '>', NewMessage, len, LOG);
ChatQueueMsg(conn, NewMessage, len);
}
BOOL isdigits(char * string)
{
// Returns TRUE id sting is decimal digits
int i, n = strlen(string);
for (i = 0; i < n; i++)
{
if (isdigit(string[i]) == FALSE) return FALSE;
}
return TRUE;
}
BOOL wildcardcompare(char * Target, char * Match)
{
// Do a compare with string *string string* *string*
// Strings should all be UC
char Pattern[100];
char * firststar;
strcpy(Pattern, Match);
firststar = strchr(Pattern,'*');
if (firststar)
{
int Len = strlen(Pattern);
if (Pattern[0] == '*' && Pattern[Len - 1] == '*') // * at start and end
{
Pattern[Len - 1] = 0;
return (BOOL)(strstr(Target, &Pattern[1]));
}
if (Pattern[0] == '*') // * at start
{
// Compare the last len - 1 chars of Target
int Targlen = strlen(Target);
int Comparelen = Targlen - (Len - 1);
if (Len == 1) // Just *
return TRUE;
if (Comparelen < 0) // Too Short
return FALSE;
return (memcmp(&Target[Comparelen], &Pattern[1], Len - 1) == 0);
}
// Must be * at end - compare first Len-1 char
return (memcmp(Target, Pattern, Len - 1) == 0);
}
// No WildCards - straight strcmp
return (strcmp(Target, Pattern) == 0);
}
int DeleteLogFiles()
{
WIN32_FIND_DATA ffd;
char szDir[MAX_PATH];
char File[MAX_PATH];
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError=0;
LARGE_INTEGER ft;
time_t now = time(NULL);
int Age;
// Prepare string for use with FindFile functions. First, copy the
// string to a buffer, then append '\*' to the directory name.
strcpy(szDir, GetLogDirectory());
strcat(szDir, "\\logs\\Log_*.txt");
// Find the first file in the directory.
hFind = FindFirstFile(szDir, &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
return dwError;
}
// List all the files in the directory with some info about them.
do
{
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
OutputDebugString(ffd.cFileName);
}
else
{
ft.HighPart = ffd.ftCreationTime.dwHighDateTime;
ft.LowPart = ffd.ftCreationTime.dwLowDateTime;
ft.QuadPart -= 116444736000000000;
ft.QuadPart /= 10000000;
Age = (now - ft.LowPart) / 86400;
if (Age > LogAge)
{
sprintf(File, "%s/logs/%s%c", GetLogDirectory(), ffd.cFileName, 0);
DeleteFile(File);
}
}
}
while (FindNextFile(hFind, &ffd) != 0);
dwError = GetLastError();
FindClose(hFind);
return dwError;
}
int RemoveLF(char * Message, int len)
{
// Remove lf chars
char * ptr1, * ptr2;
ptr1 = ptr2 = Message;
while (len-- > 0)
{
*ptr2 = *ptr1;
if (*ptr1 == '\r')
if (*(ptr1+1) == '\n')
{
ptr1++;
len--;
}
ptr1++;
ptr2++;
}
return (ptr2 - Message);
}
char * ReadInfoFile(char * File)
{
int FileSize;
char MsgFile[MAX_PATH];
FILE * hFile;
char * MsgBytes;
struct stat STAT;
char * ptr1 = 0, * ptr2;
sprintf_s(MsgFile, sizeof(MsgFile), "%s/%s", GetBPQDirectory(), File);
if (stat(MsgFile, &STAT) == -1)
return NULL;
FileSize = STAT.st_size;
hFile = fopen(MsgFile, "rb");
if (hFile == NULL)
return NULL;
MsgBytes=malloc(FileSize+1);
fread(MsgBytes, 1, FileSize, hFile);
fclose(hFile);
MsgBytes[FileSize]=0;
// Replace LF or CRLF with CR
// First remove cr from crlf
ptr1 = MsgBytes;
while(ptr2 = strstr(ptr1, "\r\n"))
{
memmove(ptr2, ptr2 + 1, strlen(ptr2));
}
// Now replace lf with cr
ptr1 = MsgBytes;
while (*ptr1)
{
if (*ptr1 == '\n')
*(ptr1) = '\r';
ptr1++;
}
return MsgBytes;
}