]> jfr.im git - irc/quakenet/newserv.git/blame - nick/nickhelpers.c
TRUSTS: require sqlite
[irc/quakenet/newserv.git] / nick / nickhelpers.c
CommitLineData
c86edd1d
Q
1/* nickhelpers.c */
2
3#include "nick.h"
4#include "../lib/flags.h"
5#include "../lib/irc_string.h"
6#include "../irc/irc_config.h"
7#include "../core/error.h"
8#include "../lib/sstring.h"
9
4ad1cf7a
CP
10#include <string.h>
11
729c4971
GB
12#define hosthash(x) ((irc_crc32i(x))%HOSTHASHSIZE)
13#define realnamehash(x) ((irc_crc32(x))%REALNAMEHASHSIZE)
c86edd1d 14
c86edd1d
Q
15host *hosttable[HOSTHASHSIZE];
16realname *realnametable[REALNAMEHASHSIZE];
17
18void initnickhelpers() {
19 memset(hosttable,0,sizeof(hosttable));
20 memset(realnametable,0,sizeof(realnametable));
21}
22
707c5824
CP
23void fininickhelpers() {
24 host *hnp, *hnpn;
25 realname *rnp, *rnpn;
26 int i;
27
28 for(i=0;i<HOSTHASHSIZE;i++) {
29 for(hnp=hosttable[i];hnp;hnp=hnpn) {
30 hnpn=hnp->next;
31 freesstring(hnp->name);
32 freehost(hnp);
33 }
34 hosttable[i]=NULL;
35 }
36
37 for(i=0;i<REALNAMEHASHSIZE;i++) {
38 for(rnp=realnametable[i];rnp;rnp=rnpn) {
39 rnpn=rnp->next;
40 freesstring(rnp->name);
41 freerealname(rnp);
42 }
43 realnametable[i]=NULL;
44 }
45}
46
c86edd1d
Q
47host *findhost(const char *hostname) {
48 host *hp;
49 for (hp=hosttable[hosthash(hostname)];hp;hp=(host *)hp->next) {
50 if (!ircd_strcmp(hostname,hp->name->content))
51 return hp;
52 }
53 return NULL;
54}
55
56host *findorcreatehost(const char *hostname) {
57 host *hp;
58 unsigned long thehash=hosthash(hostname);
59
60 for (hp=hosttable[thehash];hp;hp=(host *)hp->next)
61 if (!ircd_strcmp(hostname,hp->name->content)) {
62 hp->clonecount++;
63 return hp;
64 }
65
66 hp=newhost();
67 hp->name=getsstring(hostname,HOSTLEN);
68 hp->clonecount=1;
69 hp->marker=0;
70 hp->nicks=NULL;
71 hp->next=(struct host *)hosttable[thehash];
72 hosttable[thehash]=hp;
73
74 return hp;
75}
76
77void releasehost(host *hp) {
78 host **mhp;
79 if (--(hp->clonecount)==0) {
80 for(mhp=&(hosttable[hosthash(hp->name->content)]);*mhp;mhp=(host **)&((*mhp)->next)) {
81 if ((*mhp)==hp) {
82 (*mhp)=(host *)hp->next;
83 freesstring(hp->name);
84 freehost(hp);
85 return;
86 }
87 }
88 Error("nick",ERR_ERROR,"Unable to remove host %s from hashtable",hp->name->content);
89 }
90}
91
92realname *findrealname(const char *name) {
93 realname *rnp;
94
95 for (rnp=realnametable[realnamehash(name)];rnp;rnp=(realname *)rnp->next)
96 if (!strcmp(name,rnp->name->content))
97 return rnp;
98
99 return NULL;
100}
101
102realname *findorcreaterealname(const char *name) {
103 realname *rnp;
104 unsigned int thehash=realnamehash(name);
105
106 for (rnp=realnametable[thehash];rnp;rnp=(realname *)rnp->next)
107 if (!strcmp(name,rnp->name->content)) {
108 rnp->usercount++;
109 return rnp;
110 }
111
112 rnp=newrealname();
113 rnp->name=getsstring(name,REALLEN);
114 rnp->usercount=1;
115 rnp->marker=0;
116 rnp->nicks=NULL;
117 rnp->next=(struct realname *)realnametable[thehash];
118 realnametable[thehash]=rnp;
119
120 return rnp;
121}
122
123void releaserealname(realname *rnp) {
124 realname **mrnp;
125 if (--(rnp->usercount)==0) {
126 for(mrnp=&(realnametable[realnamehash(rnp->name->content)]);*mrnp;mrnp=(realname **)&((*mrnp)->next)) {
127 if ((*mrnp)==rnp) {
128 (*mrnp)=(realname *)rnp->next;
129 freesstring(rnp->name);
130 freerealname(rnp);
131 return;
132 }
133 }
134 Error("nick",ERR_ERROR,"Unable to remove realname %s from hashtable",rnp->name->content);
135 }
136}
137
138unsigned int nexthostmarker() {
139 int i;
140 host *hp;
7dce4989 141 static unsigned int hostmarker=0;
c86edd1d
Q
142
143 hostmarker++;
144 if (!hostmarker) {
145 /* If we wrapped to zero, zap the marker on all hosts */
146 for (i=0;i<HOSTHASHSIZE;i++)
147 for (hp=hosttable[i];hp;hp=hp->next)
148 hp->marker=0;
149 hostmarker++;
150 }
151
152 return hostmarker;
153}
154
155unsigned int nextrealnamemarker() {
156 int i;
157 realname *rnp;
7dce4989 158 static unsigned int realnamemarker=0;
c86edd1d
Q
159
160 realnamemarker++;
161 if (!realnamemarker) {
162 /* If we wrapped to zero, zap the marker on all records */
163 for (i=0;i<REALNAMEHASHSIZE;i++)
164 for (rnp=realnametable[i];rnp;rnp=rnp->next)
165 rnp->marker=0;
166 realnamemarker++;
167 }
168
169 return realnamemarker;
170}
171
172unsigned int nextnickmarker() {
173 int i;
174 nick *np;
7dce4989 175 static unsigned int nickmarker=0;
c86edd1d
Q
176
177 nickmarker++;
178
179 if (!nickmarker) {
180 /* If we wrapped to zero, zap the marker on all records */
181 for (i=0;i<NICKHASHSIZE;i++)
182 for (np=nicktable[i];np;np=np->next)
183 np->marker=0;
184 nickmarker++;
185 }
186
187 return nickmarker;
188}