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