forked from Nable80/wwwconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiles.cpp
1121 lines (955 loc) · 38.4 KB
/
profiles.cpp
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
/***************************************************************************
profiles.h - profiles support
-------------------
begin : Wed Mar 14 2001
copyright : (C) 2001 by Alexander Bilichenko
email : [email protected]
***************************************************************************/
#include "basetypes.h"
#include "hashindex.h"
#include "profiles.h"
#include "freedb.h"
#include "error.h"
bool CProfiles::IsLoginValid(const char *s)
{
size_t len = strlen(s);
if (len < 3 || len >= PROFILES_MAX_USERNAME_LENGTH) {
return false;
}
while (*s) {
unsigned char c = (unsigned char)(*s);
// Forbid control characters + 0x98 (undefined in cp1251).
// Should we forbid NBSP (0xA0) and soft hyphen (0xAD) too?
// TODO: detect whitespace-only and zero-width names.
if (c < 0x20 || c == 0x7F || c == 0x98) {
return false;
}
s++;
}
return true;
}
/* write SProfile_FullUserInfo structure, allocating space for it automatically
* return 1 if successfull, otherwise zero
*/
int CProfiles::WriteFullInfo(DWORD *idx, SProfile_FullUserInfo *FI)
{
/* prepare about string length */
if(FI->AboutUser != NULL)
FI->size = (DWORD)strlen(FI->AboutUser);
else
FI->size = 0;
/* alloc free space */
CFreeDBFile fdf(F_PROF_FREEBODY, PROFILE_WASTED_FINFO_SIZE);
if(fdf.errnum != FREEDBFILE_ERROR_ALLOK)
return 0;
if((*idx = fdf.AllocFreeSpace(sizeof(SProfile_FullUserInfo) - sizeof(char*) +
FI->size)) == 0xFFFFFFFF) {
if(fdf.errnum != FREEDBFILE_ERROR_ALLOK)
return 0;
if(wcfseek(Fp_b, 0, SEEK_END) != 0)
return 0;
*idx = wcftell(Fp_b);
}
else {
if(wcfseek(Fp_b, *idx, SEEK_SET) != 0)
return 0;
}
/* write SProfile_FullUserInfo */
if(!fCheckedWrite(FI, sizeof(SProfile_FullUserInfo) - sizeof(char*), Fp_b))
return 0;
/* AboutUser may be NULL or empty string, skip the second write if it's true */
if (FI->size) {
if(!fCheckedWrite(FI->AboutUser, FI->size, Fp_b)) {
return 0;
}
}
return 1;
}
/* read SProfile_FullUserInfo structure
* return 1 if successfull, otherwise zero
*/
int CProfiles::ReadFullInfo(DWORD idx, SProfile_FullUserInfo *FI)
{
// read SProfile_FullUserInfo
if(wcfseek(Fp_b, idx, SEEK_SET) != 0)
return 0;
if(!fCheckedRead(FI, sizeof(SProfile_FullUserInfo) - sizeof(char*), Fp_b))
return 0;
// prepare "about" string length
FI->AboutUser = (char*)malloc(FI->size + 1);
if (FI->size) {
if (!fCheckedRead(FI->AboutUser, FI->size, Fp_b)) {
free(FI->AboutUser);
FI->AboutUser = NULL;
return 0;
}
}
// set final zero at the end of string
FI->AboutUser[FI->size] = 0;
return 1;
}
/* delete SProfile_FullUserInfo structure from profile body database
* by index [idx] and mark space as free
* return 1 if successfull, otherwise zero
*/
int CProfiles::DeleteFullInfo(DWORD idx)
{
/* read old Full Info */
SProfile_FullUserInfo fui;
if(ReadFullInfo(idx, &fui) == 0) {
return 0;
}
/* mark free space */
CFreeDBFile fdf(F_PROF_FREEBODY, PROFILE_WASTED_FINFO_SIZE);
if(fdf.errnum != FREEDBFILE_ERROR_ALLOK) {
return 0;
}
DWORD rr = sizeof(fui) - sizeof(char*) + fui.size;
if(fdf.MarkFreeSpace(idx, rr) != FREEDBFILE_ERROR_ALLOK)
return 0;
return 1;
}
/* read count structures SProfile_UserInfo at index idx */
int CProfiles::ReadUInfo(DWORD idx, SProfile_UserInfo *FI)
{
if(wcfseek(Fp_i, idx, SEEK_SET) != 0)
return 0;
if(!fCheckedRead(FI, sizeof(SProfile_UserInfo), Fp_i))
return 0;
return 1;
}
/* write new info structure SProfile_UserInfo and return it index in *idx
* and seek to the find place
* DESTROY curent position of Fp_i file !!!
* return 1 if successfull, otherwise zero returned
*/
int CProfiles::GetSpaceforUInfo(DWORD *idx)
{
/* alloc free space */
CFreeDBFile fdf(F_PROF_FREENIDX, 0);
if(fdf.errnum != FREEDBFILE_ERROR_ALLOK)
return 0;
if((*idx = fdf.AllocFreeSpace(sizeof(SProfile_UserInfo))) == 0xFFFFFFFF) {
if(fdf.errnum != FREEDBFILE_ERROR_ALLOK)
return 0;
if(wcfseek(Fp_i, 0, SEEK_END) != 0)
return 0;
*idx = wcftell(Fp_i);
}
else {
if(wcfseek(Fp_i, *idx, SEEK_SET) != 0)
return 0;
}
return 1;
}
/* write count structures SProfile_UserInfo at index idx */
int CProfiles::WriteUInfo(DWORD idx, const SProfile_UserInfo *FI)
{
if(wcfseek(Fp_i, idx, SEEK_SET) != 0)
return 0;
if(!fCheckedWrite(FI, sizeof(SProfile_UserInfo), Fp_i))
return 0;
return 1;
}
int CProfiles::DeleteUInfo(DWORD idx)
{
/* mark free space */
CFreeDBFile fdf(F_PROF_FREENIDX, sizeof(SProfile_UserInfo));
if(fdf.errnum != FREEDBFILE_ERROR_ALLOK)
return 0;
if(fdf.MarkFreeSpace(idx, sizeof(SProfile_UserInfo)) != FREEDBFILE_ERROR_ALLOK)
return 0;
return 1;
}
/* add new user to profile database
* return PROFILE_RETURN_ALLOK if successfull, otherwise standart error codes
*/
int CProfiles::AddNewUser(SProfile_UserInfo *newprf, SProfile_FullUserInfo *FullUI, DWORD *ui_index)
{
int ret, i;
DWORD idx;
DWORD rr;
DWORD ucount;
Fp_i = NULL;
Fp_b = NULL;
if (!IsLoginValid(newprf->username)) {
return PROFILE_RETURN_INVALID_LOGIN;
}
if((ret = GetIndexOfString(newprf->username, &rr)) == HASHINDEX_ER_NOT_FOUND ||
ret == HASHINDEX_ER_IO_READ) {
/* prepare SProfile_UserInfo and sb structures */
newprf->postcount = 0;
FullUI->CreateDate = time(NULL);
newprf->LoginDate = 0;
newprf->persmsg = 0xffffffff;
newprf->persmescnt = 0;
newprf->readpersmescnt = 0;
newprf->postedmescnt = 0;
newprf->postedpersmsg = 0xffffffff;
newprf->RefreshCount = 0;
newprf->vs.dsm = CONFIGURE_SETTING_DEFAULT_dsm;
newprf->vs.topics = CONFIGURE_SETTING_DEFAULT_topics;
newprf->vs.tv = CONFIGURE_SETTING_DEFAULT_tv;
newprf->vs.tc = CONFIGURE_SETTING_DEFAULT_tc;
newprf->vs.ss = CONFIGURE_SETTING_DEFAULT_ss;
newprf->vs.lsel = CONFIGURE_SETTING_DEFAULT_lsel;
newprf->vs.tt = CONFIGURE_SETTING_DEFAULT_tt;
newprf->vs.tz = DATETIME_DEFAULT_TIMEZONE;
// New status = 0
newprf->Status = 0;
for( i=0; i<PROFILES_FAV_THREADS_COUNT; i++)
newprf->favs[i]=0;
/* create new index */
if((Fp_i = wcfopen(F_PROF_NINDEX, FILE_ACCESS_MODES_RW)) == NULL)
goto Unlock_and_return;
if((Fp_b = wcfopen(F_PROF_BODY, FILE_ACCESS_MODES_RW)) == NULL)
goto Unlock_and_return;
/********* lock Fp_i, Fp_b files *********/
lock_file(Fp_i);
lock_file(Fp_b);
// read unique user ID
if(!fCheckedRead(&(newprf->UniqID), sizeof(newprf->UniqID), Fp_i))
goto Unlock_and_return;
(newprf->UniqID)++;
// read user count
if(!fCheckedRead(&ucount, sizeof(ucount), Fp_i))
goto Unlock_and_return;
// increment user count
ucount++;
// get free index for user info structure
if(GetSpaceforUInfo(&idx) == 0)
goto Unlock_and_return;
if(ui_index != NULL) *ui_index = idx;
if(wcfseek(Fp_i, idx, SEEK_SET) != 0)
goto Unlock_and_return;
// write SProfile_FullUserInfo
if(WriteFullInfo(&(newprf->FullInfo_ID), FullUI) == 0) {
goto Unlock_and_return;
}
// write SProfile_UserInfo
if(WriteUInfo(idx, newprf) == 0) {
goto Unlock_and_return;
}
if(AddStringToHashedIndex(newprf->username, idx) != HASHINDEX_ER_OK) {
DeleteUInfo(idx);
DeleteFullInfo(newprf->FullInfo_ID);
goto Unlock_and_return;
}
// write new unique user ID and current user count
if(wcfseek(Fp_i, 0, SEEK_SET) != 0)
goto Unlock_and_return;
if(!fCheckedWrite(&(newprf->UniqID), sizeof(newprf->UniqID), Fp_i))
goto Unlock_and_return;
if(!fCheckedWrite(&ucount, sizeof(ucount), Fp_i))
goto Unlock_and_return;
unlock_file(Fp_i);
unlock_file(Fp_b);
/********* unlock Fp_i, Fp_b files *********/
wcfclose(Fp_i);
wcfclose(Fp_b);
return PROFILE_RETURN_ALLOK;
Unlock_and_return:
// error - unlock and exit
if(Fp_i) {
unlock_file(Fp_i);
wcfclose(Fp_i);
}
if(Fp_b) {
unlock_file(Fp_b);
wcfclose(Fp_b);
}
return PROFILE_RETURN_DB_ERROR;
}
return PROFILE_RETURN_ALREADY_EXIST;
}
/* delete user [name] from profile database
* return PROFILE_RETURN_ALLOK if successfull, otherwise standart error codes
*/
int CProfiles::DeleteUser(const char *name)
{
DWORD idx, ucount;
SProfile_UserInfo pi;
Fp_i = NULL;
Fp_b = NULL;
int ret = GetIndexOfString(name, &idx);
switch(ret) {
case HASHINDEX_ER_NOT_FOUND:
return PROFILE_RETURN_INVALID_LOGIN;
case HASHINDEX_ER_OK:
// index exist - try to read block
if((Fp_i = wcfopen(F_PROF_NINDEX, FILE_ACCESS_MODES_RW)) == NULL)
goto Do_Exit;
if((Fp_b = wcfopen(F_PROF_BODY, FILE_ACCESS_MODES_RW)) == NULL)
goto Do_Exit;
/********* lock Fp_i, Fp_b *********/
lock_file(Fp_i);
lock_file(Fp_b);
if(wcfseek(Fp_i, idx, SEEK_SET) != 0)
goto Do_Exit;
if(!fCheckedRead(&pi, sizeof(pi), Fp_i))
goto Do_Exit;
if(strcmp(pi.username, name) == 0) {
if(DeleteFullInfo(pi.FullInfo_ID) == 0)
goto Do_Exit;
if(DeleteUInfo(idx) == 0)
goto Do_Exit;
if(DeleteStringFromHashedIndex(name) != HASHINDEX_ER_OK)
goto Do_Exit;
// update user count
if(wcfseek(Fp_i, sizeof(DWORD), SEEK_SET) != 0)
goto Do_Exit;
if(!fCheckedRead(&ucount, sizeof(ucount), Fp_i))
goto Do_Exit;
ucount--;
if(wcfseek(Fp_i, sizeof(DWORD), SEEK_SET) != 0)
goto Do_Exit;
if(!fCheckedWrite(&ucount, sizeof(ucount), Fp_i))
goto Do_Exit;
unlock_file(Fp_i);
unlock_file(Fp_b);
/******** unlock Fp_i, Fp_b ********/
wcfclose(Fp_i);
wcfclose(Fp_b);
Fp_i = NULL;
Fp_b = NULL;
return PROFILE_RETURN_ALLOK;
}
unlock_file(Fp_i);
unlock_file(Fp_b);
/******** unlock Fp_i, Fp_b ********/
wcfclose(Fp_i);
wcfclose(Fp_b);
Fp_i = NULL;
Fp_b = NULL;
return PROFILE_RETURN_INVALID_LOGIN;
case HASHINDEX_ER_FORMAT:
return PROFILE_RETURN_INVALID_LOGIN;
case HASHINDEX_ER_IO_READ:
case HASHINDEX_ER_IO_CREATE:
goto Do_Exit;
default:
return PROFILE_RETURN_UNKNOWN_ERROR;
}
Do_Exit:
if(Fp_i) {
unlock_file(Fp_i);
wcfclose(Fp_i);
Fp_i = NULL;
}
if(Fp_b) {
unlock_file(Fp_b);
wcfclose(Fp_b);
Fp_b = NULL;
}
return PROFILE_RETURN_DB_ERROR;
}
/* modify structures, user if user exist, UserName cannot be changed
* return PROFILE_RETURN_ALLOK if successfull, otherwise standart error codes
* also if Fui is NULL, ModifyUser() will not modify Full User Information
*/
int CProfiles::ModifyUser(SProfile_UserInfo *newprf, SProfile_FullUserInfo *FullUI, DWORD *ui_index)
{
DWORD idx;
SProfile_UserInfo pi;
Fp_i = NULL;
Fp_b = NULL;
if (!IsLoginValid(newprf->username)) {
return PROFILE_RETURN_INVALID_LOGIN;
}
int ret = GetIndexOfString(newprf->username, &idx);
switch(ret) {
case HASHINDEX_ER_NOT_FOUND:
return PROFILE_RETURN_INVALID_LOGIN;
case HASHINDEX_ER_OK:
// index exist - try to read block
if((Fp_i = wcfopen(F_PROF_NINDEX, FILE_ACCESS_MODES_RW)) == NULL)
goto Do_Exit;
if((Fp_b = wcfopen(F_PROF_BODY, FILE_ACCESS_MODES_RW)) == NULL)
goto Do_Exit;
/********* lock Fp_i, Fp_b *********/
lock_file(Fp_i);
lock_file(Fp_b);
if(wcfseek(Fp_i, idx, SEEK_SET) != 0)
goto Do_Exit;
if(!fCheckedRead(&pi, sizeof(pi), Fp_i))
goto Do_Exit;
if(strcmp(pi.username, newprf->username) == 0) {
if(wcfseek(Fp_i, idx, SEEK_SET) != 0) goto Do_Exit;
if(ui_index != NULL) *ui_index = idx;
if(FullUI != NULL) {
// delete old and save new full info
if(DeleteFullInfo(pi.FullInfo_ID) == 0)
goto Do_Exit;
if(WriteFullInfo(&(newprf->FullInfo_ID), FullUI) == 0)
goto Do_Exit;
}
else {
newprf->FullInfo_ID = pi.FullInfo_ID;
}
// save old dinamic board information
newprf->postcount = pi.postcount;
newprf->UniqID = pi.UniqID;
newprf->persmsg = pi.persmsg;
newprf->lastIP = pi.lastIP;
newprf->persmescnt = pi.persmescnt;
newprf->readpersmescnt = pi.readpersmescnt;
newprf->postedmescnt = pi.postedmescnt;
newprf->postedpersmsg = pi.postedpersmsg;
newprf->RefreshCount = pi.RefreshCount;
// and finally save user profile
if(!fCheckedWrite(newprf, sizeof(SProfile_UserInfo), Fp_i))
goto Do_Exit;
unlock_file(Fp_i);
unlock_file(Fp_b);
/******** unlock Fp_i, Fp_b ********/
wcfclose(Fp_i);
wcfclose(Fp_b);
Fp_i = NULL;
Fp_b = NULL;
return PROFILE_RETURN_ALLOK;
}
unlock_file(Fp_i);
unlock_file(Fp_b);
/******** unlock Fp_i, Fp_b ********/
wcfclose(Fp_i);
wcfclose(Fp_b);
Fp_i = NULL;
Fp_b = NULL;
return PROFILE_RETURN_INVALID_LOGIN;
case HASHINDEX_ER_FORMAT:
return PROFILE_RETURN_INVALID_LOGIN;
case HASHINDEX_ER_IO_READ:
case HASHINDEX_ER_IO_CREATE:
goto Do_Exit;
default:
return PROFILE_RETURN_UNKNOWN_ERROR;
}
Do_Exit:
if(Fp_i) {
unlock_file(Fp_i);
wcfclose(Fp_i);
Fp_i = NULL;
}
if(Fp_b) {
unlock_file(Fp_b);
wcfclose(Fp_b);
Fp_b = NULL;
}
return PROFILE_RETURN_DB_ERROR;
}
/* check, if there is user with same name in profile database
* return PROFILE_RETURN_ALLOK if successfull, otherwise standart error codes
* also if ui not NULL return SProfile_UserInfo for found user, and if also Fui not NULL
* return Fui information too
*/
int CProfiles::GetUserByName(const char *name, SProfile_UserInfo *ui, SProfile_FullUserInfo *Fui, DWORD *ui_index)
{
DWORD idx;
SProfile_UserInfo pi;
int ret = GetIndexOfString(name, &idx);
// error code returned - next strings - analizing it
switch(ret) {
case HASHINDEX_ER_NOT_FOUND:
return PROFILE_RETURN_INVALID_LOGIN;
case HASHINDEX_ER_OK:
// index exist - try to read block
if((Fp_i = wcfopen(F_PROF_NINDEX, FILE_ACCESS_MODES_R)) == NULL)
goto Do_Exit;
if((Fp_b = wcfopen(F_PROF_BODY, FILE_ACCESS_MODES_R)) == NULL)
goto Do_Exit;
if(wcfseek(Fp_i, idx, SEEK_SET) != 0)
goto Do_Exit;
if(!fCheckedRead(&pi, sizeof(pi), Fp_i))
goto Do_Exit;
if(strcmp(pi.username, name) == 0) {
// profile found
if(ui != NULL) {
memcpy(ui, &pi, sizeof(pi));
}
if(Fui != NULL) {
// read SProfile_FullUserInfo
if(ReadFullInfo(pi.FullInfo_ID, Fui) == 0)
goto Do_Exit;
}
if(ui_index != NULL) *ui_index = idx;
wcfclose(Fp_b);
wcfclose(Fp_i);
return PROFILE_RETURN_ALLOK;
}
/* name not found - not exist */
wcfclose(Fp_b);
wcfclose(Fp_i);
return PROFILE_RETURN_INVALID_LOGIN;
case HASHINDEX_ER_FORMAT:
return PROFILE_RETURN_INVALID_LOGIN;
case HASHINDEX_ER_IO_READ:
case HASHINDEX_ER_IO_CREATE:
goto Do_Exit;
default:
return PROFILE_RETURN_UNKNOWN_ERROR;
}
Do_Exit:
if (Fp_i) {
wcfclose(Fp_i);
Fp_i = NULL;
}
if (Fp_b) {
wcfclose(Fp_b);
Fp_b = NULL;
}
return PROFILE_RETURN_DB_ERROR;
}
int CProfiles::GetUsersCount(DWORD *uc)
{
if((Fp_i = wcfopen(F_PROF_NINDEX, FILE_ACCESS_MODES_RW)) == NULL)
return 0;
if(wcfseek(Fp_i, sizeof(DWORD), SEEK_SET) != 0)
return 0;
if(!fCheckedRead(uc, sizeof(DWORD), Fp_i))
return 0;
wcfclose(Fp_i);
return 1;
}
int CProfiles::GetUInfo(DWORD idx, SProfile_UserInfo *FI)
{
if((Fp_i = wcfopen(F_PROF_NINDEX, FILE_ACCESS_MODES_R)) == NULL)
return 0;
DWORD x = (ReadUInfo(idx, FI) != 1);
wcfclose(Fp_i);
if(x) return 0;
return 1;
}
int CProfiles::SetUInfo(DWORD idx, const SProfile_UserInfo *FI)
{
if((Fp_i = wcfopen(F_PROF_NINDEX, FILE_ACCESS_MODES_RW)) == NULL)
return 0;
lock_file(Fp_i);
DWORD x = (WriteUInfo(idx, FI) != 1);
unlock_file(Fp_i);
wcfclose(Fp_i);
if(x) return 0;
return 1;
}
int CProfiles::GetFullInfo(DWORD idx, SProfile_FullUserInfo *FI)
{
if((Fp_b = wcfopen(F_PROF_BODY, FILE_ACCESS_MODES_R)) == NULL)
return 0;
DWORD x = (ReadFullInfo(idx, FI) != 1);
wcfclose(Fp_b);
if(x) return 0;
return 1;
}
int CProfiles::GenerateUserList(char ***buf, DWORD *cnt)
{
DWORD *ii;
DWORD readed, i, curii = 0, c = 0, ac /* alloced count */, allc = 0;
#define ULIST_PROFILE_READ_COUNT 400
SProfile_UserInfo pi[ULIST_PROFILE_READ_COUNT];
*buf = NULL;
*cnt = 0;
if(GenerateIndexList(&ii) != HASHINDEX_ER_OK)
return 0;
if (ii[0] != 0xffffffff) {
*buf = (char**)malloc(ULIST_PROFILE_READ_COUNT*sizeof(char**));
if(!(*buf)) return 0;
ac = ULIST_PROFILE_READ_COUNT;
if((Fp_i = wcfopen(F_PROF_NINDEX, FILE_ACCESS_MODES_R)) == NULL) {
free(*buf);
*buf = NULL;
return 0;
}
if(wcfseek(Fp_i, ii[0], SEEK_SET) != 0)
return 0;
do {
readed = (DWORD)wcfread(&pi, 1, sizeof(SProfile_UserInfo)*ULIST_PROFILE_READ_COUNT, Fp_i);
if((readed % sizeof(SProfile_UserInfo)) != 0) {
wcfclose(Fp_i);
for(i = 0; i < c; i++) {
free((*buf)[i]);
}
free(*buf);
*buf = NULL;
return 0;
}
readed = readed / sizeof(SProfile_UserInfo);
for(i = 0; i < readed; i++) {
// find this in our indexes (test for deleted profile)
while(ii[curii] < ((i + allc*ULIST_PROFILE_READ_COUNT)*sizeof(SProfile_UserInfo) + ii[0])) curii++;
if(ii[curii] == ((i + allc*ULIST_PROFILE_READ_COUNT)*sizeof(SProfile_UserInfo) + ii[0])) {
// modify right - reset all right if SUPERUSER
if((pi[i].right & USERRIGHT_SUPERUSER))
pi[i].right = USERRIGHT_SUPERUSER;
// store it
size_t len = strlen(pi[i].username) + 1;
// TODO: replace this monster with structures
(*buf)[c] = (char*)malloc(len + 5*sizeof(DWORD) + 1);
memcpy((*buf)[c], &pi[i].lastIP, sizeof(DWORD));
memcpy((*buf)[c] + 4, &pi[i].postcount, sizeof(DWORD));
memcpy((*buf)[c] + 8, &pi[i].LoginDate, sizeof(DWORD));
memcpy((*buf)[c] + 12, &pi[i].RefreshCount, sizeof(DWORD));
memcpy((*buf)[c] + 16, &pi[i].right, sizeof(DWORD));
memcpy((*buf)[c] + 20, &pi[i].username, len);
c++;
// check for realloc
if(ac == c) {
ac += ULIST_PROFILE_READ_COUNT;
*buf = (char**)realloc(*buf, ac*sizeof(char**));
}
}
// FOR DEBUG !!!
/*else {
if(curii > 3) {
for(int j = -3; j < 4; j++) {
print2log("%d", ii[curii + j]);
}
print2log("i=%d, name=%s, Done\n", (i + allc*ULIST_PROFILE_READ_COUNT)*sizeof(SProfile_UserInfo) + ii[0], pi[i].username);
}
}*/
}
allc++;
} while(readed == ULIST_PROFILE_READ_COUNT);
wcfclose(Fp_i);
// realloc to the real size
*buf = (char**)realloc(*buf, c*sizeof(char**));
*cnt = c;
}
free(ii);
return 1;
}
int CProfiles::PostPersonalMessage(const char *username, DWORD userindex, const char *message, const char *from, DWORD userindexfrom)
{
SProfile_UserInfo ui, poster_ui;
int ret;
WCFILE *fp;
// load recipient user profile
if(username != NULL && strcmp(username, "") != 0) {
// use username
if((ret = GetUserByName(username, &ui, NULL, &userindex)) != PROFILE_RETURN_ALLOK)
return ret;
}
else {
// use userindex
if(!GetUInfo(userindex, &ui))
return PROFILE_RETURN_DB_ERROR;
}
// load sender user profile
if(from != NULL && strcmp(from, "") != 0) {
// use username
if((ret = GetUserByName(from, &poster_ui, NULL, &userindexfrom)) != PROFILE_RETURN_ALLOK)
return ret;
}
else {
// use userindex
if(!GetUInfo(userindexfrom, &poster_ui))
return PROFILE_RETURN_DB_ERROR;
}
// prepare personal message structure
SPersonalMessage mes;
DWORD pos;
mes.Date = time(NULL);
strcpy(mes.NameFrom, poster_ui.username);
mes.UIdFrom = poster_ui.UniqID;
strcpy(mes.NameTo, ui.username);
mes.UIdTo = ui.UniqID;
strcpy(mes.Msg, message);
mes.Prev = ui.persmsg;
mes.PosterPrev = poster_ui.postedpersmsg;
if((fp = wcfopen(F_PROF_PERSMSG, FILE_ACCESS_MODES_RW)) == NULL)
return PROFILE_RETURN_DB_ERROR;
lock_file(fp);
// write to the file
if(wcfseek(fp, 0, SEEK_END) != 0)
goto PostPersMsg_Error;
pos = wcftell(fp);
if(!fCheckedWrite(&mes, sizeof(mes), fp))
goto PostPersMsg_Error;
// update recipient user profile
ui.persmsg = pos;
ui.persmescnt++;
SetUInfo(userindex, &ui);
// to correct bug with post to youself
if(poster_ui.UniqID == ui.UniqID)
memcpy(&poster_ui, &ui, sizeof(SProfile_UserInfo));
// update sender user profile
poster_ui.postedpersmsg = pos;
poster_ui.postedmescnt++;
SetUInfo(userindexfrom, &poster_ui);
unlock_file(fp);
wcfclose(fp);
return PROFILE_RETURN_ALLOK;
PostPersMsg_Error:
unlock_file(fp);
wcfclose(fp);
return PROFILE_RETURN_DB_ERROR;
}
int CProfiles::ReadPersonalMessages(const char *username, DWORD userindex,
SPersonalMessage **tomessages, DWORD *tocount,
SPersonalMessage **frommessages, DWORD *fromcount)
{
SProfile_UserInfo ui;
int ret;
WCFILE *fp;
SPersonalMessage *msg;
DWORD toread, i, fromread, curpos;
if(username != NULL && strcmp(username, "") != 0) {
// use username
if((ret = GetUserByName(username, &ui, NULL, &userindex)) != PROFILE_RETURN_ALLOK)
return ret;
}
else {
// use userindex
if(!GetUInfo(userindex, &ui))
return PROFILE_RETURN_DB_ERROR;
}
msg = *tomessages = *frommessages = NULL;
// if we really need read to messages
if(tocount == NULL) {
// all messages
toread = ui.persmescnt;
}
else {
if(*tocount == 0) {
// only new messages
toread = (DWORD)ui.persmescnt - (DWORD)ui.readpersmescnt;
}
else {
// selected count
if(*tocount > ui.persmescnt)
*tocount = ui.persmescnt;
toread = *tocount;
}
}
if(!toread) {
*tomessages = NULL;
goto skip_to_msg_read;
}
msg = (SPersonalMessage*)malloc(toread*sizeof(SPersonalMessage));
if((fp = wcfopen(F_PROF_PERSMSG, FILE_ACCESS_MODES_R)) == NULL) {
free(msg);
return PROFILE_RETURN_DB_ERROR;
}
curpos = ui.persmsg;
for(i = 0; i < toread; i++) {
if(wcfseek(fp, curpos, SEEK_SET) != 0)
goto PostPersMsg_Error;
if(!fCheckedRead(&(msg[i]), sizeof(SPersonalMessage), fp))
goto PostPersMsg_Error;
curpos = msg[i].Prev;
// this situation should not happen, but...
if(curpos == 0xffffffff) break;
}
msg[toread - 1].Prev = 0xffffffff; // last message mark
wcfclose(fp);
*tomessages = msg;
msg = NULL;
skip_to_msg_read:
// if we really need read from messages
if(fromcount == NULL) {
// all messages
fromread = ui.postedmescnt;
}
else {
// selected count
if(*fromcount > ui.postedmescnt)
*fromcount = ui.postedmescnt;
fromread = *fromcount;
}
if(!fromread) {
*frommessages = NULL;
return PROFILE_RETURN_ALLOK;
}
msg = (SPersonalMessage*)malloc(fromread*sizeof(SPersonalMessage));
if((fp = wcfopen(F_PROF_PERSMSG, FILE_ACCESS_MODES_R)) == NULL) {
free(msg);
return PROFILE_RETURN_DB_ERROR;
}
curpos = ui.postedpersmsg;
for(i = 0; i < fromread; i++) {
if(wcfseek(fp, curpos, SEEK_SET) != 0)
goto PostPersMsg_Error;
if(!fCheckedRead(&(msg[i]), sizeof(SPersonalMessage), fp))
goto PostPersMsg_Error;
curpos = msg[i].PosterPrev;
msg[i].Prev = 0;
// this situation should not happen, but...
if(curpos == 0xffffffff) {
break;
}
}
msg[i].Prev = 0xffffffff; // last message mark
wcfclose(fp);
*frommessages = msg;
return PROFILE_RETURN_ALLOK;
PostPersMsg_Error:
if(msg) free(msg);
if(*tomessages){
free(*tomessages);
*tomessages = NULL;
}
if(*frommessages){
free(*frommessages);
*frommessages = NULL;
}
wcfclose(fp);
return PROFILE_RETURN_DB_ERROR;
}
int CProfiles::ReadPersonalMessagesByDate(const char *username, DWORD userindex,
SPersonalMessage **tomessages, time_t todate,
SPersonalMessage **frommessages, time_t fromdate)
{
SProfile_UserInfo ui;
int ret;
WCFILE *fp;
SPersonalMessage *msg;
DWORD i, curpos;
if(username != NULL && strcmp(username, "") != 0) {
// use username
if((ret = GetUserByName(username, &ui, NULL, &userindex)) != PROFILE_RETURN_ALLOK)
return ret;
}
else {
// use userindex
if(!GetUInfo(userindex, &ui))
return PROFILE_RETURN_DB_ERROR;
}
msg = *tomessages = *frommessages = NULL;
// if we really need read "to" messages ?
if(!todate) {
*tomessages = NULL;
goto skip_to_msg_read;
}
if((fp = wcfopen(F_PROF_PERSMSG, FILE_ACCESS_MODES_R)) == NULL)
return PROFILE_RETURN_DB_ERROR;
msg = (SPersonalMessage*)malloc(sizeof(SPersonalMessage));
if(!msg) {
wcfclose(fp);
return PROFILE_RETURN_UNKNOWN_ERROR;
}
curpos = ui.persmsg;
i = 0;
if(curpos != 0xffffffff) {
for(;;) {
if(wcfseek(fp, curpos, SEEK_SET) != 0)
goto PostPersMsg_Error;
if(!fCheckedRead(&(msg[i]), sizeof(SPersonalMessage), fp))
goto PostPersMsg_Error;
curpos = msg[i].Prev;
if(msg[i].Date < todate)
break;
i++;
if(curpos == 0xffffffff) break;
msg = (SPersonalMessage*)realloc(msg, (i+1)*sizeof(SPersonalMessage));
}