]> jfr.im git - irc/quakenet/newserv.git/blame - nick/nickhelpers.c
merge
[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
c86edd1d
Q
12#define hosthash(x) ((crc32i(x))%HOSTHASHSIZE)
13#define realnamehash(x) ((crc32(x))%REALNAMEHASHSIZE)
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
23host *findhost(const char *hostname) {
24 host *hp;
25 for (hp=hosttable[hosthash(hostname)];hp;hp=(host *)hp->next) {
26 if (!ircd_strcmp(hostname,hp->name->content))
27 return hp;
28 }
29 return NULL;
30}
31
32host *findorcreatehost(const char *hostname) {
33 host *hp;
34 unsigned long thehash=hosthash(hostname);
35
36 for (hp=hosttable[thehash];hp;hp=(host *)hp->next)
37 if (!ircd_strcmp(hostname,hp->name->content)) {
38 hp->clonecount++;
39 return hp;
40 }
41
42 hp=newhost();
43 hp->name=getsstring(hostname,HOSTLEN);
44 hp->clonecount=1;
45 hp->marker=0;
46 hp->nicks=NULL;
47 hp->next=(struct host *)hosttable[thehash];
48 hosttable[thehash]=hp;
49
50 return hp;
51}
52
53void releasehost(host *hp) {
54 host **mhp;
55 if (--(hp->clonecount)==0) {
56 for(mhp=&(hosttable[hosthash(hp->name->content)]);*mhp;mhp=(host **)&((*mhp)->next)) {
57 if ((*mhp)==hp) {
58 (*mhp)=(host *)hp->next;
59 freesstring(hp->name);
60 freehost(hp);
61 return;
62 }
63 }
64 Error("nick",ERR_ERROR,"Unable to remove host %s from hashtable",hp->name->content);
65 }
66}
67
68realname *findrealname(const char *name) {
69 realname *rnp;
70
71 for (rnp=realnametable[realnamehash(name)];rnp;rnp=(realname *)rnp->next)
72 if (!strcmp(name,rnp->name->content))
73 return rnp;
74
75 return NULL;
76}
77
78realname *findorcreaterealname(const char *name) {
79 realname *rnp;
80 unsigned int thehash=realnamehash(name);
81
82 for (rnp=realnametable[thehash];rnp;rnp=(realname *)rnp->next)
83 if (!strcmp(name,rnp->name->content)) {
84 rnp->usercount++;
85 return rnp;
86 }
87
88 rnp=newrealname();
89 rnp->name=getsstring(name,REALLEN);
90 rnp->usercount=1;
91 rnp->marker=0;
92 rnp->nicks=NULL;
93 rnp->next=(struct realname *)realnametable[thehash];
94 realnametable[thehash]=rnp;
95
96 return rnp;
97}
98
99void releaserealname(realname *rnp) {
100 realname **mrnp;
101 if (--(rnp->usercount)==0) {
102 for(mrnp=&(realnametable[realnamehash(rnp->name->content)]);*mrnp;mrnp=(realname **)&((*mrnp)->next)) {
103 if ((*mrnp)==rnp) {
104 (*mrnp)=(realname *)rnp->next;
105 freesstring(rnp->name);
106 freerealname(rnp);
107 return;
108 }
109 }
110 Error("nick",ERR_ERROR,"Unable to remove realname %s from hashtable",rnp->name->content);
111 }
112}
113
114unsigned int nexthostmarker() {
115 int i;
116 host *hp;
7dce4989 117 static unsigned int hostmarker=0;
c86edd1d
Q
118
119 hostmarker++;
120 if (!hostmarker) {
121 /* If we wrapped to zero, zap the marker on all hosts */
122 for (i=0;i<HOSTHASHSIZE;i++)
123 for (hp=hosttable[i];hp;hp=hp->next)
124 hp->marker=0;
125 hostmarker++;
126 }
127
128 return hostmarker;
129}
130
131unsigned int nextrealnamemarker() {
132 int i;
133 realname *rnp;
7dce4989 134 static unsigned int realnamemarker=0;
c86edd1d
Q
135
136 realnamemarker++;
137 if (!realnamemarker) {
138 /* If we wrapped to zero, zap the marker on all records */
139 for (i=0;i<REALNAMEHASHSIZE;i++)
140 for (rnp=realnametable[i];rnp;rnp=rnp->next)
141 rnp->marker=0;
142 realnamemarker++;
143 }
144
145 return realnamemarker;
146}
147
148unsigned int nextnickmarker() {
149 int i;
150 nick *np;
7dce4989 151 static unsigned int nickmarker=0;
c86edd1d
Q
152
153 nickmarker++;
154
155 if (!nickmarker) {
156 /* If we wrapped to zero, zap the marker on all records */
157 for (i=0;i<NICKHASHSIZE;i++)
158 for (np=nicktable[i];np;np=np->next)
159 np->marker=0;
160 nickmarker++;
161 }
162
163 return nickmarker;
164}