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