]> jfr.im git - irc/quakenet/newserv.git/blame - nick/nick.c
Implement patroljoin, patrolpart and patrollist commands.
[irc/quakenet/newserv.git] / nick / nick.c
CommitLineData
c86edd1d
Q
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"
87698d77 14#include "../lib/version.h"
4208eafd 15#include "../lib/ccassert.h"
103521a1 16#include "../core/nsmalloc.h"
17
c86edd1d
Q
18#include <stdlib.h>
19#include <string.h>
20#include <stdio.h>
21
70b0a4e5 22MODULE_VERSION("");
87698d77 23
4208eafd
GB
24CCASSERT(sizeof(host) == sizeof(realname));
25
c86edd1d
Q
26const 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 },
e98d808a 40 { 'P', UMODE_PARANOID },
c9d0dfa3
GB
41 { 'q', UMODE_COMCHANS },
42 { 'Q', UMODE_COMCHANSRESTR },
42d7af35 43 { 'C', UMODE_CLOAKED },
c86edd1d
Q
44 { '\0', 0 } };
45
c153c0dc 46const flag accountflags[] = {
c4ffdb9b 47 { 'q', AFLAG_STAFF },
3294b10b 48 { 'h', AFLAG_SUPPORT },
c4ffdb9b
CP
49 { 'o', AFLAG_OPER },
50 { 'a', AFLAG_ADMIN },
c153c0dc
CP
51 { 'd', AFLAG_DEVELOPER },
52 { '\0', 0 } };
53
c86edd1d
Q
54#define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
55
56nick *nicktable[NICKHASHSIZE];
57nick **servernicks[MAXSERVERS];
58
59sstring *nickextnames[MAXNICKEXTS];
60
61void nickstats(int hooknum, void *arg);
62
3404a8f0
CP
63char *NULLAUTHNAME = "";
64
c86edd1d 65void _init() {
e31727be 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
c86edd1d
Q
74 initnickhelpers();
75 memset(nicktable,0,sizeof(nicktable));
76 memset(servernicks,0,sizeof(servernicks));
86a9e70c
P
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 }
c86edd1d
Q
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);
47657339 95 registerserverhandler("AC",&handleaccountmsg,4);
6885ae9c 96 registerserverhandler("P",&handleprivmsg,2);
5144ddc4 97 registerserverhandler("A",&handleawaymsg,1);
42d7af35
GB
98 registerserverhandler("CA",&handleaddcloak,1);
99 registerserverhandler("CU",&handleclearcloak,0);
c86edd1d
Q
100
101 /* Fake the addition of our own server */
102 handleserverchange(HOOK_SERVER_NEWSERVER,(void *)numerictolong(mynumeric->content,2));
103}
104
103521a1 105void _fini() {
9cf0b03f
CP
106 nick *np;
107 int i;
108
707c5824 109 fininickhelpers();
9cf0b03f
CP
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);
843184e3 115 freesstring(np->opername);
3404a8f0 116 if(!np->auth && np->authname && (np->authname != NULLAUTHNAME))
3294b10b 117 free(np->authname);
9cf0b03f
CP
118 }
119 }
120
df5ab174 121 nsfreeall(POOL_NICK);
707c5824 122
103521a1 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);
7bec4aeb 133 deregisterserverhandler("AC",&handleaccountmsg);
6885ae9c 134 deregisterserverhandler("P",&handleprivmsg);
5144ddc4 135 deregisterserverhandler("A",&handleawaymsg);
42d7af35
GB
136 deregisterserverhandler("CA",&handleaddcloak);
137 deregisterserverhandler("CU",&handleclearcloak);
103521a1 138}
139
c86edd1d
Q
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
146void handleserverchange(int hooknum, void *arg) {
c3db6f7e 147 long servernum;
c86edd1d
Q
148 int i;
149
c3db6f7e 150 servernum=(long)arg;
c86edd1d
Q
151
152 switch(hooknum) {
153 case HOOK_SERVER_NEWSERVER:
103521a1 154 servernicks[servernum]=(nick **)nsmalloc(POOL_NICK,(serverlist[servernum].maxusernum+1)*sizeof(nick **));
c86edd1d
Q
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 }
2a010087 164 nsfree(POOL_NICK,servernicks[servernum]);
c86edd1d
Q
165 break;
166 }
167}
168
169/*
170 * deletenick:
171 *
172 * This function handles the removal of a nick from the network
173 */
174
175void 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
3294b10b
CP
200 if(IsAccount(np)) {
201 if(!np->auth) {
3404a8f0 202 if(np->authname && (np->authname != NULLAUTHNAME))
3294b10b
CP
203 free(np->authname);
204 } else {
205 np->auth->usercount--;
47657339 206
3294b10b
CP
207 for (nh=&(np->auth->nicks);*nh;nh=&((*nh)->nextbyauthname)) {
208 if (*nh==np) {
209 *nh=np->nextbyauthname;
210 break;
211 }
47657339 212 }
47657339 213
3294b10b
CP
214 releaseauthname(np->auth);
215 }
47657339
C
216 }
217
c86edd1d
Q
218 freesstring(np->shident); /* freesstring(NULL) is OK */
219 freesstring(np->sethost);
843184e3 220 freesstring(np->opername);
80e32fcd 221 freesstring(np->message);
c86edd1d 222
96644df6 223 node_decrement_usercount(np->ipnode);
526e7c1d
P
224 derefnode(iptree, np->ipnode);
225
226 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
227
42d7af35
GB
228 /* Remove cloak entries for the user */
229 removecloaktarget(np);
230 clearcloaktargets(np);
231
c86edd1d
Q
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
241void addnicktohash(nick *np) {
242 np->next=nicktable[nickhash(np->nick)];
243 nicktable[nickhash(np->nick)]=np;
244}
245
246void 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
257nick *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
268void 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
c3db6f7e 289 if ((long)arg>5) {
c86edd1d
Q
290 /* Full stats */
291 sprintf(buf,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total,buckets,NICKHASHSIZE,maxchain);
c3db6f7e 292 } else if ((long)arg>2) {
c86edd1d
Q
293 sprintf(buf,"Nick : %6d users on network.",total);
294 }
295
c3db6f7e 296 if ((long)arg>2) {
c86edd1d
Q
297 triggerhook(HOOK_CORE_STATSREPLY,buf);
298 }
299}
300
301int 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
47657339 316 Error("nick",ERR_WARNING,"Tried to register too many nick extensions: %s",name);
c86edd1d
Q
317 return -1;
318}
319
320int 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
332void 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
350char *visiblehostmask(nick *np, char *buf) {
28252fef 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
363char *visibleuserhost(nick *np, char *buf) {
c86edd1d
Q
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
28252fef 382 sprintf(buf,"%s@%s",ident,host);
c86edd1d
Q
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
397nick **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
422nick *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
436nick *getnickbynumericstr(char *numericstr) {
437 return getnickbynumeric(numerictolong(numericstr,5));
438}
439
cebc4cab 440
c86edd1d 441#endif
526e7c1d 442
42d7af35
GB
443int canseeuser(nick *np, nick *cloaked)
444{
445 return (np == cloaked ||
446 !IsCloaked(cloaked) ||
447 np->cloak_extra == cloaked);
448}
449
450void addcloaktarget(nick *cloaked, nick *target)
451{
452 removecloaktarget(target);
453
454 target->cloak_extra = cloaked;
455 cloaked->cloak_count++;
456}
457
458void removecloaktarget(nick *target)
459{
460 if (target->cloak_extra) {
461 target->cloak_extra->cloak_count--;
462 target->cloak_extra = NULL;
463 }
464}
465
466void clearcloaktargets(nick *cloaked)
467{
468 nick *tnp;
469 int j;
470
260e4351
GB
471 if (cloaked->cloak_count == 0)
472 return;
473
42d7af35
GB
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