]>
jfr.im git - irc/quakenet/newserv.git/blob - chanserv/database/chanservdb_hash.c
3 * Handles insertion and retrieval of various data structures
4 * from their respective hashes. Hopefully it's obvious from
5 * the name of each function what it does.
11 #include "../chanserv.h"
12 #include "../../lib/irc_string.h"
14 reguser
*regusernicktable
[REGUSERHASHSIZE
];
16 maildomain
*maildomainnametable
[MAILDOMAINHASHSIZE
];
17 maildomain
*maildomainIDtable
[MAILDOMAINHASHSIZE
];
19 #define regusernickhash(x) ((crc32i(x))%REGUSERHASHSIZE)
20 #define maildomainnamehash(x) ((crc32i(x))%MAILDOMAINHASHSIZE)
21 #define maildomainIDhash(x) ((x)%MAILDOMAINHASHSIZE)
23 void chanservhashinit() {
24 memset(regusernicktable
,0,REGUSERHASHSIZE
*sizeof(reguser
*));
25 memset(maildomainnametable
,0,MAILDOMAINHASHSIZE
*sizeof(maildomain
*));
26 memset(maildomainIDtable
,0,MAILDOMAINHASHSIZE
*sizeof(maildomain
*));
29 void addregusertohash(reguser
*rup
) {
33 hash
=regusernickhash(rup
->username
);
35 rup
->nextbyname
=regusernicktable
[hash
];
36 regusernicktable
[hash
]=rup
;
38 anp
=findorcreateauthname(rup
->ID
, rup
->username
);
39 anp
->exts
[chanservaext
]=rup
;
42 reguser
*findreguserbynick(const char *nick
) {
43 unsigned int hashnick
;
46 hashnick
=regusernickhash(nick
);
47 for (rup
=regusernicktable
[hashnick
];rup
;rup
=rup
->nextbyname
) {
48 if (!ircd_strcmp(nick
,rup
->username
)) {
57 reguser
*findreguserbyID(unsigned int ID
) {
62 return (reguser
*)anp
->exts
[chanservaext
];
68 * findreguserbyemail()
70 reguser
*findreguserbyemail(const char *email
) {
74 for (i
=0;i
<REGUSERHASHSIZE
;i
++) {
75 for (rup
=regusernicktable
[i
];rup
;rup
=rup
->nextbyname
) {
76 if (rup
->email
&& !strcasecmp(email
,rup
->email
->content
)) {
85 void removereguserfromhash(reguser
*rup
) {
92 hash
=regusernickhash(rup
->username
);
95 for (ruh
=&(regusernicktable
[hash
]);*ruh
;ruh
=&((*ruh
)->nextbyname
)) {
97 *ruh
=(rup
->nextbyname
);
104 Error("chanserv",ERR_ERROR
,"Unable to remove reguser %s from nickhash",
108 anp
=findauthname(rup
->ID
);
111 anp
->exts
[chanservaext
]=NULL
;
112 releaseauthname(anp
);
114 Error("chanserv",ERR_ERROR
,"Unable to remove reguser %s from ID hash",rup
->username
);
118 void addregusertochannel(regchanuser
*rcup
) {
119 rcup
->nextbyuser
=(rcup
->user
->knownon
);
120 rcup
->nextbychan
=(rcup
->chan
->regusers
[(rcup
->user
->ID
)%REGCHANUSERHASHSIZE
]);
122 rcup
->user
->knownon
=rcup
;
123 rcup
->chan
->regusers
[(rcup
->user
->ID
)%REGCHANUSERHASHSIZE
]=rcup
;
126 regchanuser
*findreguseronchannel(regchan
*rcp
, reguser
*rup
) {
129 for (rcup
=rcp
->regusers
[(rup
->ID
)%REGCHANUSERHASHSIZE
];rcup
;rcup
=rcup
->nextbychan
) {
130 if (rcup
->user
==rup
) {
139 void delreguserfromchannel(regchan
*rcp
, reguser
*rup
) {
143 for (rcuh
=&(rcp
->regusers
[(rup
->ID
)%REGCHANUSERHASHSIZE
]);*rcuh
;
144 rcuh
=&((*rcuh
)->nextbychan
)) {
145 if ((*rcuh
)->user
==rup
) {
147 freesstring((*rcuh
)->info
);
149 *rcuh
=(*rcuh
)->nextbychan
;
156 Error("chanserv",ERR_ERROR
,"Unable to remove user %s from channel %s",
157 rup
->username
,rcp
->index
->name
->content
);
161 for (rcuh
=&(rup
->knownon
);*rcuh
;rcuh
=&((*rcuh
)->nextbyuser
)) {
162 if ((*rcuh
)->chan
==rcp
) {
163 /* Found the channel */
164 *rcuh
=(*rcuh
)->nextbyuser
;
169 Error("chanserv",ERR_ERROR
,"Unable to remove channel %s from user %s",
170 rcp
->index
->name
->content
,rup
->username
);
173 void addmaildomaintohash(maildomain
*mdp
) {
176 hash
=maildomainnamehash(mdp
->name
->content
);
178 mdp
->nextbyname
=maildomainnametable
[hash
];
179 maildomainnametable
[hash
]=mdp
;
181 hash
=maildomainIDhash(mdp
->ID
);
183 mdp
->nextbyID
=maildomainIDtable
[hash
];
184 maildomainIDtable
[hash
]=mdp
;
187 maildomain
*findmaildomainbyID(unsigned int ID
) {
191 hash
=maildomainIDhash(ID
);
192 for (mdp
=maildomainIDtable
[hash
]; mdp
; mdp
=mdp
->nextbyID
)
199 maildomain
*findmaildomainbydomain(char *domain
) {
203 hash
=maildomainnamehash(domain
);
204 for (mdp
=maildomainnametable
[hash
]; mdp
; mdp
=mdp
->nextbyname
)
205 if (!strcasecmp(mdp
->name
->content
, domain
))
211 maildomain
*findnearestmaildomain(char *domain
) {
212 maildomain
*m
= findmaildomainbydomain(domain
);
215 if(!m
&& (p
=strchr(domain
, '.')))
216 return findnearestmaildomain(++p
);
221 maildomain
*findmaildomainbyemail(char *email
) {
224 if (!(domain
=strchr(email
, '@')))
229 return findmaildomainbydomain(domain
);
232 maildomain
*findorcreatemaildomain(char *email
) {
234 char *domain
,*pdomain
;
237 if (!(domain
=strchr(email
, '@')))
242 if( domain
[0] == '.' )
245 hash
=maildomainnamehash(domain
);
246 for (mdp
=maildomainnametable
[hash
]; mdp
; mdp
=mdp
->nextbyname
)
247 if (!strcasecmp(mdp
->name
->content
, domain
))
252 mdp
->name
=getsstring(domain
, EMAILLEN
);
255 mdp
->actlimit
=MD_DEFAULTACTLIMIT
;
256 mdp
->flags
=MDFLAG_DEFAULT
;
258 addmaildomaintohash(mdp
);
260 pdomain
=strchr(domain
, '.');
264 mdp
->parent
= findorcreatemaildomain(pdomain
);
272 void removemaildomainfromhash(maildomain
*mdp
) {
277 hash
=maildomainnamehash(mdp
->name
->content
);
280 for (mdh
=&(maildomainnametable
[hash
]);*mdh
;mdh
=&((*mdh
)->nextbyname
)) {
282 *mdh
=(mdp
->nextbyname
);
289 Error("chanserv",ERR_ERROR
,"Unable to remove maildomain %s from namehash",
293 hash
=maildomainIDhash(mdp
->ID
);
296 for (mdh
=&(maildomainIDtable
[hash
]);*mdh
;mdh
=&((*mdh
)->nextbyID
)) {
298 *mdh
=(mdp
->nextbyID
);
305 Error("chanserv",ERR_ERROR
,"Unable to remove maildomain %s from ID hash",mdp
->name
->content
);
309 void addregusertomaildomain(reguser
*rup
, maildomain
*mdp
) {
311 rup
->nextbydomain
=mdp
->users
;
313 for(smdp
=mdp
;smdp
;smdp
=smdp
->parent
) {
318 void delreguserfrommaildomain(reguser
*rup
, maildomain
*mdp
) {
319 maildomain
*smdp
, *nmdp
;
320 reguser
*ruh
, *pruh
=NULL
;
326 for (ruh
=mdp
->users
; ruh
; ruh
=ruh
->nextbydomain
) {
330 pruh
->nextbydomain
=ruh
->nextbydomain
;
332 mdp
->users
=ruh
->nextbydomain
;
340 Error("chanserv",ERR_ERROR
,"Unable to remove user %s from maildomain %s",
341 rup
->username
, mdp
->name
->content
);
345 /* Free it from all the parent domains, cleaning up as we go.. */
346 for(smdp
=mdp
;smdp
;smdp
=nmdp
) {
349 /* Keep it if there are users left or we're remembering something about it. */
350 if (--smdp
->count
|| (smdp
->flags
!= MDFLAG_DEFAULT
) || (smdp
->actlimit
!= MD_DEFAULTACTLIMIT
))
353 removemaildomainfromhash(smdp
);
354 freesstring(smdp
->name
);
355 freemaildomain(smdp
);
359 freesstring(rup
->localpart
);