]> jfr.im git - irc/quakenet/newserv.git/blob - chanserv/chanservdump.c
BUILD: add require-all build mode
[irc/quakenet/newserv.git] / chanserv / chanservdump.c
1 /* chanservdump.c */
2
3 #include <stdio.h>
4 #include <string.h>
5
6 #include "chanserv.h"
7
8 struct lastjoin {
9 unsigned int chanid;
10 unsigned int userid;
11 time_t lastjoin;
12 };
13
14 int dumplastjoindata(const char *filename) {
15 FILE *fp;
16 chanindex *cip;
17 regchan *rcp;
18 regchanuser *rcup;
19 int i,j,total=0;
20 struct lastjoin lj;
21
22 Error("chanserv",ERR_INFO,"Dumping last join data.");
23
24 if (!(fp=fopen(filename,"w"))) {
25 Error("chanserv",ERR_ERROR,"Error opening lastjoin dump file.");
26 return 1;
27 }
28
29 for (i=0;i<CHANNELHASHSIZE;i++) {
30 for (cip=chantable[i];cip;cip=cip->next) {
31 if (!(rcp=cip->exts[chanservext]))
32 continue;
33
34 lj.chanid=rcp->ID;
35 for (j=0;j<REGCHANUSERHASHSIZE;j++) {
36 for (rcup=rcp->regusers[j];rcup;rcup=rcup->nextbychan) {
37 lj.userid=rcup->user->ID;
38 lj.lastjoin=rcup->usetime;
39 if (!fwrite(&lj,sizeof(struct lastjoin),1,fp)) {
40 Error("chanserv",ERR_ERROR,"Error saving lastjoin data.");
41 fclose(fp);
42 return 1;
43 }
44 total++;
45 }
46 }
47 }
48 }
49
50 Error("chanserv",ERR_INFO,"Dumped %d last join records to file",total);
51
52 fclose(fp);
53
54 return 0;
55 }
56
57 int readlastjoindata(const char *filename) {
58 FILE *fp;
59 reguser *rup=NULL;
60 chanindex *cip;
61 regchan *rcp=NULL;
62 regchan **allchans;
63 regchanuser *rcup=NULL;
64 unsigned int lastuser=0;
65 struct lastjoin lj;
66 int i;
67 int badcount=0,total=0;
68
69 if (!(fp=fopen(filename,"r"))) {
70 Error("chanserv",ERR_ERROR,"Error opening lastjoin dump file.");
71 return 1;
72 }
73
74 /* Set up the allchans and allusers arrays */
75 allchans=(regchan **)malloc((lastchannelID+1)*sizeof(regchan *));
76 memset(allchans,0,(lastchannelID+1)*sizeof(regchan *));
77 for (i=0;i<CHANNELHASHSIZE;i++) {
78 for (cip=chantable[i];cip;cip=cip->next) {
79 if ((rcp=cip->exts[chanservext]))
80 allchans[rcp->ID]=cip->exts[chanservext];
81 }
82 }
83
84 while (fread(&lj, sizeof(struct lastjoin), 1, fp)) {
85 total++;
86 if (lj.userid != lastuser) {
87 rup=findreguserbyID(lj.userid);
88 lastuser=lj.userid;
89 }
90
91 if (lj.chanid > lastchannelID)
92 rcp=NULL;
93 else
94 rcp=allchans[lj.chanid];
95
96 if (!rup || !rcp) {
97 badcount++;
98 continue;
99 }
100
101 if (!(rcup=findreguseronchannel(rcp, rup))) {
102 badcount++;
103 continue;
104 }
105
106 rcup->usetime=lj.lastjoin;
107 }
108
109 Error("chanserv",ERR_INFO,"Retrieved %d last join entries from file (%d bad entries)",total,badcount);
110
111 fclose(fp);
112 free(allchans);
113
114 return 0;
115 }