]> jfr.im git - irc/quakenet/newserv.git/blob - nick/nick.c
9b47eef72b5fb199a2b1bda6620317235530945c
[irc/quakenet/newserv.git] / nick / nick.c
1 /* nick.c */
2
3 #include "nick.h"
4 #include "../lib/flags.h"
5 #include "../lib/irc_string.h"
6 #include "../lib/base64.h"
7 #include "../irc/irc.h"
8 #include "../irc/irc_config.h"
9 #include "../core/error.h"
10 #include "../core/hooks.h"
11 #include "../lib/sstring.h"
12 #include "../server/server.h"
13 #include "../parser/parser.h"
14 #include "../lib/version.h"
15 #include <stdlib.h>
16 #include <string.h>
17 #include <stdio.h>
18
19 MODULE_VERSION("");
20
21 const flag umodeflags[] = {
22 { 'i', UMODE_INV },
23 { 'w', UMODE_WALLOPS },
24 { 'g', UMODE_DEBUG },
25 { 'o', UMODE_OPER },
26 { 'k', UMODE_SERVICE },
27 { 'X', UMODE_XOPER },
28 { 'd', UMODE_DEAF },
29 { 'r', UMODE_ACCOUNT },
30 { 'n', UMODE_HIDECHAN },
31 { 'x', UMODE_HIDEHOST },
32 { 'h', UMODE_SETHOST },
33 { 'R', UMODE_REGPRIV },
34 { 'I', UMODE_HIDEIDLE },
35 { '\0', 0 } };
36
37 #define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
38
39 nick *nicktable[NICKHASHSIZE];
40 nick **servernicks[MAXSERVERS];
41
42 sstring *nickextnames[MAXNICKEXTS];
43 sstring *nodeextnames[PATRICIA_MAXSLOTS];
44 patricia_tree_t *iptree;
45
46 void nickstats(int hooknum, void *arg);
47
48 void _init() {
49 initnickalloc();
50 initnickhelpers();
51 memset(nicktable,0,sizeof(nicktable));
52 memset(servernicks,0,sizeof(servernicks));
53
54 /* Register our hooks */
55 registerhook(HOOK_SERVER_NEWSERVER,&handleserverchange);
56 registerhook(HOOK_SERVER_LOSTSERVER,&handleserverchange);
57 registerhook(HOOK_CORE_STATSREQUEST,&nickstats);
58
59 /* And our server handlers */
60 registerserverhandler("N",&handlenickmsg,10);
61 registerserverhandler("D",&handlekillmsg,2);
62 registerserverhandler("Q",&handlequitmsg,1);
63 registerserverhandler("M",&handleusermodemsg,3);
64 registerserverhandler("W",&handlewhoismsg,2);
65 registerserverhandler("AC",&handleaccountmsg,4);
66 registerserverhandler("R",&handlestatsmsg,2);
67
68 /* Fake the addition of our own server */
69 handleserverchange(HOOK_SERVER_NEWSERVER,(void *)numerictolong(mynumeric->content,2));
70
71 iptree = patricia_new_tree(PATRICIA_MAXBITS);
72 }
73
74 /*
75 * This function handles servers appearing and disappearing.
76 * For a new server, the client table is allocated.
77 * For a disappearing server, all it's clients are killed and the client table is freed.
78 */
79
80 void handleserverchange(int hooknum, void *arg) {
81 long servernum;
82 int i;
83
84 servernum=(long)arg;
85
86 switch(hooknum) {
87 case HOOK_SERVER_NEWSERVER:
88 servernicks[servernum]=(nick **)malloc((serverlist[servernum].maxusernum+1)*sizeof(nick **));
89 memset(servernicks[servernum],0,(serverlist[servernum].maxusernum+1)*sizeof(nick **));
90 break;
91
92 case HOOK_SERVER_LOSTSERVER:
93 for (i=0;i<=serverlist[servernum].maxusernum;i++) {
94 if (servernicks[servernum][i]!=NULL) {
95 deletenick(servernicks[servernum][i]);
96 }
97 }
98 free(servernicks[servernum]);
99 break;
100 }
101 }
102
103 /*
104 * deletenick:
105 *
106 * This function handles the removal of a nick from the network
107 */
108
109 void deletenick(nick *np) {
110 nick **nh;
111
112 /* Fire the hook. This will deal with removal from channels etc. */
113 triggerhook(HOOK_NICK_LOSTNICK, np);
114
115 /* Release the realname and hostname parts */
116
117 for (nh=&(np->realname->nicks);*nh;nh=&((*nh)->nextbyrealname)) {
118 if (*nh==np) {
119 *nh=np->nextbyrealname;
120 break;
121 }
122 }
123
124 for (nh=&(np->host->nicks);*nh;nh=&((*nh)->nextbyhost)) {
125 if (*nh==np) {
126 *nh=np->nextbyhost;
127 break;
128 }
129 }
130
131 releaserealname(np->realname);
132 releasehost(np->host);
133
134 if(IsAccount(np) && np->auth)
135 {
136 np->auth->usercount--;
137
138 for (nh=&(np->auth->nicks);*nh;nh=&((*nh)->nextbyauthname)) {
139 if (*nh==np) {
140 *nh=np->nextbyauthname;
141 break;
142 }
143 }
144
145 releaseauthname(np->auth);
146 }
147
148 freesstring(np->shident); /* freesstring(NULL) is OK */
149 freesstring(np->sethost);
150
151 derefnode(iptree, np->ipnode);
152
153 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
154
155 /* Delete the nick from the servernick table */
156 *(gethandlebynumericunsafe(np->numeric))=NULL;
157
158 /* Remove the nick from the hash table */
159 removenickfromhash(np);
160
161 freenick(np);
162 }
163
164 void addnicktohash(nick *np) {
165 np->next=nicktable[nickhash(np->nick)];
166 nicktable[nickhash(np->nick)]=np;
167 }
168
169 void removenickfromhash(nick *np) {
170 nick **nh;
171
172 for (nh=&(nicktable[nickhash(np->nick)]);*nh;nh=&((*nh)->next)) {
173 if ((*nh)==np) {
174 (*nh)=np->next;
175 break;
176 }
177 }
178 }
179
180 nick *getnickbynick(const char *name) {
181 nick *np;
182
183 for (np=nicktable[nickhash(name)];np;np=np->next) {
184 if (!ircd_strcmp(np->nick,name))
185 return np;
186 }
187
188 return NULL;
189 }
190
191 void nickstats(int hooknum, void *arg) {
192 int total,maxchain,curchain,i,buckets;
193 nick *np;
194 char buf[200];
195
196 /* Get nick stats */
197 buckets=total=maxchain=curchain=0;
198 for (i=0;i<NICKHASHSIZE;i++,curchain=0) {
199 np=nicktable[i];
200 if (np!=NULL) {
201 buckets++;
202 for (;np;np=np->next) {
203 total++;
204 curchain++;
205 }
206 }
207 if (curchain>maxchain) {
208 maxchain=curchain;
209 }
210 }
211
212 if ((long)arg>5) {
213 /* Full stats */
214 sprintf(buf,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total,buckets,NICKHASHSIZE,maxchain);
215 } else if ((long)arg>2) {
216 sprintf(buf,"Nick : %6d users on network.",total);
217 }
218
219 if ((long)arg>2) {
220 triggerhook(HOOK_CORE_STATSREPLY,buf);
221 }
222 }
223
224 int registernickext(const char *name) {
225 int i;
226
227 if (findnickext(name)!=-1) {
228 Error("nick",ERR_WARNING,"Tried to register duplicate nick extension %s",name);
229 return -1;
230 }
231
232 for (i=0;i<MAXNICKEXTS;i++) {
233 if (nickextnames[i]==NULL) {
234 nickextnames[i]=getsstring(name,100);
235 return i;
236 }
237 }
238
239 Error("nick",ERR_WARNING,"Tried to register too many nick extensions: %s",name);
240 return -1;
241 }
242
243 int findnickext(const char *name) {
244 int i;
245
246 for (i=0;i<MAXNICKEXTS;i++) {
247 if (nickextnames[i]!=NULL && !ircd_strcmp(name,nickextnames[i]->content)) {
248 return i;
249 }
250 }
251
252 return -1;
253 }
254
255 void releasenickext(int index) {
256 int i;
257 nick *np;
258
259 freesstring(nickextnames[index]);
260 nickextnames[index]=NULL;
261
262 for (i=0;i<NICKHASHSIZE;i++) {
263 for (np=nicktable[i];np;np=np->next) {
264 np->exts[index]=NULL;
265 }
266 }
267 }
268
269 /* visiblehostmask
270 * Produces the "apparent" hostmask as seen by network users.
271 */
272
273 char *visiblehostmask(nick *np, char *buf) {
274 char hostbuf[HOSTLEN+1];
275 char *ident, *host;
276
277 ident=np->ident;
278 host=np->host->name->content;
279
280 if (IsSetHost(np)) {
281 if (np->shident) {
282 ident=np->shident->content;
283 }
284 if (np->sethost) {
285 host=np->sethost->content;
286 }
287 } else if (IsAccount(np) && IsHideHost(np)) {
288 sprintf(hostbuf,"%s.%s", np->authname, HIS_HIDDENHOST);
289 host=hostbuf;
290 }
291
292 sprintf(buf,"%s!%s@%s",np->nick,ident,host);
293
294 return buf;
295 }
296
297 #if 0
298
299 /*
300 * gethandlebynumeric:
301 * Given a numeric, gives the location in the servernicks table
302 * where it should be. Does not check that the nick currently found
303 * there (if any) has the correct numeric; this is left to the
304 * calling function to figure out.
305 */
306
307 nick **gethandlebynumeric(long numeric) {
308 int servernum;
309 int maskednum;
310 server *serv;
311
312 /* Shift off the client identifier part of the numeric to get the server ID */
313 servernum=(numeric>>18);
314
315 if ((serv=getserverdata(servernum))==NULL) {
316 Error("nick",ERR_WARNING,"Numeric %ld refers to non-existent server %d",numeric,servernum);
317 return NULL;
318 }
319
320 /* Compute the masked numeric */
321 maskednum=numeric&(serv->maxusernum);
322
323 return (servernicks[servernum])+maskednum;
324 }
325
326 /*
327 * getnickbynumeric[str]()
328 * These functions retrieve a nick based on it's numeric on the network
329 * Use the approriate function depending on how your numeric is expressed..
330 */
331
332 nick *getnickbynumeric(long numeric) {
333 nick **nh;
334
335 nh=gethandlebynumeric(numeric);
336
337 if ((*nh) && ((*nh)->numeric!=numeric)) {
338 /* We found a masked numeric match, but the actual numeric
339 * is different. This counts as a miss. */
340 return NULL;
341 }
342
343 return (*nh);
344 }
345
346 nick *getnickbynumericstr(char *numericstr) {
347 return getnickbynumeric(numerictolong(numericstr,5));
348 }
349
350 #endif
351
352 int registernodeext(const char *name) {
353 int i;
354
355 if (findnodeext(name)!=-1) {
356 Error("nick",ERR_WARNING,"Tried to register duplicate node extension %s",name);
357 return -1;
358 }
359
360 for (i=0;i<PATRICIA_MAXSLOTS;i++) {
361 if (nodeextnames[i]==NULL) {
362 nodeextnames[i]=getsstring(name,100);
363 return i;
364 }
365 }
366
367 Error("nick",ERR_WARNING,"Tried to register too many extensions: %s",name);
368 return -1;
369 }
370
371 int findnodeext(const char *name) {
372 int i;
373
374 for (i=0;i<PATRICIA_MAXSLOTS;i++) {
375 if (nodeextnames[i]!=NULL && !ircd_strcmp(name,nodeextnames[i]->content)) {
376 return i;
377 }
378 }
379
380 return -1;
381 }
382
383 void releasenodeext(int index) {
384 patricia_node_t *head, *node;
385
386 freesstring(nodeextnames[index]);
387 nodeextnames[index]=NULL;
388
389 head = iptree->head;
390
391 PATRICIA_WALK_ALL(head, node)
392 {
393 node->slots[index]=NULL;
394 } PATRICIA_WALK_END;
395 }