]> jfr.im git - irc/quakenet/newserv.git/blob - nick/nickhandlers.c
c7c3fee27b648b5f6d56fc309e8ec62ef7227ea6
[irc/quakenet/newserv.git] / nick / nickhandlers.c
1 /* nickhandlers.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 <stdlib.h>
15 #include <string.h>
16
17 /*
18 * handlenickmsg:
19 * Handle new nicks being introduced to the network.
20 * Handle renames.
21 */
22
23 int handlenickmsg(void *source, int cargc, char **cargv) {
24 char *sender=(char *)source;
25 time_t timestamp;
26 nick *np,*np2;
27 nick **nh;
28 char *fakehost;
29 char *accountts;
30 char *accountflags;
31 struct irc_in_addr ipaddress;
32 char *accountid;
33 unsigned long userid;
34
35 if (cargc==2) { /* rename */
36 /* Nyklon 1017697578 */
37 timestamp=strtol(cargv[1],NULL,10);
38 np=getnickbynumericstr(sender);
39 if (np==NULL) {
40 Error("nick",ERR_ERROR,"Rename from non-existent sender %s",sender);
41 return CMD_OK;
42 }
43 np2=getnickbynick(cargv[0]);
44 if (np==np2) {
45 /* The new and old nickname have the same hash, this means a rename to the same name in
46 * different case, e.g. Flash -> flash. In this case the timestamp for the change should
47 * match the existing timestamp, and we can bypass all the collision checking and hash fettling. */
48 if (np->timestamp!=timestamp) {
49 Error("nick",ERR_WARNING,"Rename to same nickname with different timestamp (%s(%d) -> %s(%d))",
50 np->nick,np->timestamp,cargv[0],timestamp);
51 np->timestamp=timestamp;
52 }
53 strncpy(np->nick,cargv[0],NICKLEN);
54 np->nick[NICKLEN]='\0';
55 triggerhook(HOOK_NICK_RENAME,np);
56 return CMD_OK;
57 }
58 if (np2!=NULL) {
59 /* Nick collision */
60 if (ircd_strcmp(np->ident,np2->ident) || (np->host!=np2->host)) {
61 /* Different user@host */
62 if (np2->timestamp < timestamp) {
63 /* The nick attempting to rename got killed. Guess we don't need to do the rename now :) */
64 deletenick(np);
65 return CMD_OK;
66 } else {
67 /* The other nick got killed */
68 deletenick(np2);
69 }
70 } else {
71 if (np2->timestamp < timestamp) {
72 /* Same user@host: reverse logic. Whose idea was all this anyway? */
73 deletenick(np2);
74 } else {
75 deletenick(np);
76 return CMD_OK;
77 }
78 }
79 }
80 /* OK, we've survived the collision hazard. Change timestamp and rename */
81 np->timestamp=timestamp;
82 removenickfromhash(np);
83 strncpy(np->nick,cargv[0],NICKLEN);
84 np->nick[NICKLEN]='\0';
85 addnicktohash(np);
86 triggerhook(HOOK_NICK_RENAME,np);
87 } else if (cargc>=8) { /* new nick */
88 /* Jupiler 2 1016645147 ~Jupiler www.iglobal.be +ir moo [FUTURE CRAP HERE] DV74O] BNBd7 :Jupiler */
89 timestamp=strtol(cargv[2],NULL,10);
90 np=getnickbynick(cargv[0]);
91 if (np!=NULL) {
92 /* Nick collision */
93 if (ircd_strcmp(np->ident,cargv[3]) || ircd_strcmp(np->host->name->content,cargv[4])) {
94 /* Different user@host */
95 if (timestamp>np->timestamp) {
96 /* New nick is newer. Ignore this nick message */
97 return CMD_OK;
98 } else {
99 /* New nick is older. Kill the imposter, and drop through */
100 deletenick(np);
101 }
102 } else {
103 if (timestamp>np->timestamp) {
104 /* Same user@host, newer timestamp: we're killing off a ghost */
105 deletenick(np);
106 } else {
107 /* This nick is the ghost, so ignore it */
108 return CMD_OK;
109 }
110 }
111 }
112
113 nh=gethandlebynumeric(numerictolong(cargv[cargc-2],5));
114 if (!nh) {
115 /* This isn't a valid numeric */
116 Error("nick",ERR_WARNING,"Received NICK with invalid numeric %s from %s.",cargv[cargc-2],sender);
117 return CMD_ERROR;
118 }
119
120 base64toip(cargv[cargc-3], &ipaddress);
121 if (!irc_in_addr_valid(&ipaddress)) {
122 Error("nick",ERR_ERROR,"Received NICK with invalid ipaddress for %s from %s.",cargv[0],sender);
123 return CMD_ERROR;
124 }
125
126 /* At this stage the nick is cleared to proceed */
127 np=newnick();
128 strncpy(np->nick,cargv[0],NICKLEN);
129 np->nick[NICKLEN]='\0';
130 np->numeric=numerictolong(cargv[cargc-2],5);
131 strncpy(np->ident,cargv[3],USERLEN);
132 np->ident[USERLEN]='\0';
133 np->host=findorcreatehost(cargv[4]);
134 np->realname=findorcreaterealname(cargv[cargc-1]);
135 np->nextbyhost=np->host->nicks;
136 np->host->nicks=np;
137 np->nextbyrealname=np->realname->nicks;
138 np->realname->nicks=np;
139 np->timestamp=timestamp;
140
141 base64toip(cargv[cargc-3], &ipaddress);
142 np->ipnode = refnode(iptree, &ipaddress, PATRICIA_MAXBITS);
143 node_increment_usercount(np->ipnode);
144
145 np->shident=NULL;
146 np->sethost=NULL;
147 np->opername=NULL;
148 np->umodes=0;
149 np->marker=0;
150 memset(np->exts, 0, MAXNICKEXTS * sizeof(void *));
151 np->authname[0]='\0';
152 np->auth=NULL;
153 np->accountts=0;
154 if(cargc>=9) {
155 int sethostarg = 6, opernamearg = 6, accountarg = 6;
156
157 setflags(&(np->umodes),UMODE_ALL,cargv[5],umodeflags,REJECT_NONE);
158
159 if(IsOper(np) && (serverlist[myhub].flags & SMODE_OPERNAME)) {
160 accountarg++;
161 sethostarg++;
162
163 np->opername=getsstring(cargv[opernamearg],ACCOUNTLEN);
164 }
165
166 if (IsAccount(np)) {
167 sethostarg++;
168
169 if ((accountts=strchr(cargv[accountarg],':'))) {
170 *accountts++='\0';
171 if(accountid) {
172 strncpy(np->authname,cargv[accountarg],ACCOUNTLEN);
173 np->authname[ACCOUNTLEN]='\0';
174 np->accountts=strtoul(accountts,&accountid,10);
175
176 userid=strtoul(accountid + 1,&accountflags,10);
177 if(!userid) {
178 np->auth=NULL;
179 } else {
180 np->auth=findorcreateauthname(userid, cargv[accountarg]);
181 np->auth->usercount++;
182 np->nextbyauthname=np->auth->nicks;
183 np->auth->nicks=np;
184 if(accountflags)
185 np->auth->flags=strtoul(accountflags + 1,NULL,10);
186 }
187 }
188 }
189 }
190 if (IsSetHost(np) && (fakehost=strchr(cargv[sethostarg],'@'))) {
191 /* valid sethost */
192 *fakehost++='\0';
193 np->shident=getsstring(cargv[sethostarg],USERLEN);
194 np->sethost=getsstring(fakehost,HOSTLEN);
195 }
196 }
197
198 /* Place this nick in the server nick table. Note that nh is valid from the numeric check above */
199 if (*nh) {
200 /* There was a nick there already -- we have a masked numeric collision
201 * This shouldn't happen, but if it does the newer nick takes precedence
202 * (the two nicks are from the same server, and if the server has reissued
203 * the masked numeric it must believe the old user no longer exists).
204 */
205 Error("nick",ERR_ERROR,"Masked numeric collision for %s [%s vs %s]",cargv[0],cargv[cargc-2],longtonumeric((*nh)->numeric,5));
206 deletenick(*nh);
207 }
208 *nh=np;
209
210 /* And the nick hash table */
211 addnicktohash(np);
212
213 /* Trigger the hook */
214 triggerhook(HOOK_NICK_NEWNICK,np);
215 } else {
216 Error("nick",ERR_WARNING,"Nick message with weird number of parameters (%d)",cargc);
217 }
218
219 return CMD_OK;
220 }
221
222 int handlequitmsg(void *source, int cargc, char **cargv) {
223 nick *np;
224 void *harg[2];
225
226 if (cargc>0) {
227 harg[1]=(void *)cargv[0];
228 } else {
229 harg[1]="";
230 }
231
232 np=getnickbynumericstr((char *)source);
233 if (np) {
234 harg[0]=(void *)np;
235 triggerhook(HOOK_NICK_QUIT, harg);
236 deletenick(np);
237 } else {
238 Error("nick",ERR_WARNING,"Quit from non-existant numeric %s",(char *)source);
239 }
240 return CMD_OK;
241 }
242
243 int handlekillmsg(void *source, int cargc, char **cargv) {
244 nick *np;
245 void *harg[2];
246 #warning Fix me to use source
247
248 if (cargc<1) {
249 Error("nick",ERR_WARNING,"Kill message with too few parameters");
250 return CMD_ERROR;
251 }
252
253 if (cargc>1) {
254 harg[1]=(void *)cargv[1];
255 } else {
256 harg[1]="";
257 }
258
259 np=getnickbynumericstr(cargv[0]);
260 if (np) {
261 harg[0]=(void *)np;
262 triggerhook(HOOK_NICK_KILL, harg);
263 deletenick(np);
264 } else {
265 Error("nick",ERR_WARNING,"Kill for non-existant numeric %s",cargv[0]);
266 }
267 return CMD_OK;
268 }
269
270 int handleusermodemsg(void *source, int cargc, char **cargv) {
271 nick *np;
272 flag_t oldflags;
273 char *fakehost;
274
275 if (cargc<2) {
276 Error("nick",ERR_WARNING,"Mode message with too few parameters");
277 return CMD_LAST; /* With <2 params the channels module won't want it either */
278 }
279
280 if (cargv[0][0]=='#') {
281 /* Channel mode change, we don't care.
282 * We don't bother checking for other channel types here, since & channel mode
283 * changes aren't broadcast and + channels don't have mode changes :) */
284 return CMD_OK;
285 }
286
287 np=getnickbynumericstr((char *)source);
288 if (np!=NULL) {
289 if (ircd_strcmp(cargv[0],np->nick)) {
290 Error("nick",ERR_WARNING,"Attempted mode change on user %s by %s",cargv[0],np->nick);
291 return CMD_OK;
292 }
293 oldflags=np->umodes;
294 setflags(&(np->umodes),UMODE_ALL,cargv[1],umodeflags,REJECT_NONE);
295
296 if (strchr(cargv[1],'o')) { /* o always comes on its own when being set */
297 if(serverlist[myhub].flags & SMODE_OPERNAME) {
298 if((np->umodes & UMODE_OPER)) {
299 np->opername = getsstring(cargv[2], ACCOUNTLEN);
300 } else {
301 freesstring(np->opername);
302 np->opername = NULL;
303 }
304 }
305 if((np->umodes ^ oldflags) & UMODE_OPER)
306 triggerhook(HOOK_NICK_MODEOPER,np);
307 }
308 if (strchr(cargv[1],'h')) { /* Have to allow +h twice.. */
309 /* +-h: just the freesstring() calls for the -h case */
310 freesstring(np->shident); /* freesstring(NULL) is OK */
311 freesstring(np->sethost);
312 np->shident=NULL;
313 np->sethost=NULL;
314 if (IsSetHost(np) && cargc>2) { /* +h and mask received */
315 if ((fakehost=strchr(cargv[2],'@'))) {
316 /* user@host change */
317 *fakehost++='\0';
318 np->shident=getsstring(cargv[2],USERLEN);
319 np->sethost=getsstring(fakehost,HOSTLEN);
320 } else {
321 np->sethost=getsstring(cargv[2],HOSTLEN);
322 }
323 }
324 triggerhook(HOOK_NICK_SETHOST, (void *)np);
325 }
326 } else {
327 Error("nick",ERR_WARNING,"Usermode change by unknown user %s",(char *)source);
328 }
329 return CMD_OK;
330 }
331
332 int handlewhoismsg(void *source, int cargc, char **cargv) {
333 nick *sender,*target;
334 nick *nicks[2];
335
336 if (cargc<2)
337 return CMD_OK;
338
339 if (strncmp(cargv[0],mynumeric->content,2)) {
340 return CMD_OK;
341 }
342
343 /* Find the sender... */
344 if ((sender=getnickbynumericstr((char *)source))==NULL) {
345 Error("localuser",ERR_WARNING,"WHOIS message from non existent numeric %s",(char *)source);
346 return CMD_OK;
347 }
348
349 /* :hub.splidge.netsplit.net 311 moo splidge splidge ground.stbarnab.as * :splidge
350 :hub.splidge.netsplit.net 312 moo splidge splidge.netsplit.net :splidge's netsplit leaf
351 :hub.splidge.netsplit.net 313 moo splidge :is an IRC Operator
352 :hub.splidge.netsplit.net 318 moo splidge :End of /WHOIS list.
353 */
354
355 /* And the target... */
356 if ((target=getnickbynick(cargv[1]))==NULL) {
357 irc_send(":%s 401 %s %s :No such nick",myserver->content,sender->nick,cargv[1]);
358 } else {
359 irc_send(":%s 311 %s %s %s %s * :%s",myserver->content,sender->nick,target->nick,target->ident,
360 target->host->name->content, target->realname->name->content);
361 nicks[0]=sender; nicks[1]=target;
362 triggerhook(HOOK_NICK_WHOISCHANNELS,nicks);
363 if (IsOper(sender) || !HIS_SERVER ) {
364 irc_send(":%s 312 %s %s %s :%s",myserver->content,sender->nick,target->nick,
365 serverlist[homeserver(target->numeric)].name->content,
366 serverlist[homeserver(target->numeric)].description->content);
367 } else {
368 irc_send(":%s 312 %s %s " HIS_SERVERNAME " :" HIS_SERVERDESC,myserver->content,sender->nick,target->nick);
369 }
370 if (IsOper(target)) {
371 irc_send(":%s 313 %s %s :is an IRC Operator",myserver->content,sender->nick,target->nick);
372 }
373 if (IsAccount(target)) {
374 irc_send(":%s 330 %s %s %s :is authed as",myserver->content,sender->nick,target->nick,target->authname);
375 }
376 if (homeserver(target->numeric)==mylongnum && !IsService(target) && !IsHideIdle(target)) {
377 irc_send(":%s 317 %s %s %ld %ld :seconds idle, signon time",myserver->content,sender->nick,target->nick,
378 (getnettime() - target->timestamp) % (((target->numeric + target->timestamp) % 983) + 7),target->timestamp);
379 }
380 }
381
382 irc_send(":%s 318 %s %s :End of /WHOIS list.",myserver->content,sender->nick,cargv[1]);
383 return CMD_OK;
384 }
385
386 int handleaccountmsg(void *source, int cargc, char **cargv) {
387 nick *target;
388 unsigned long userid;
389 time_t accountts;
390 flag_t accountflags;
391
392 if (cargc<4) {
393 return CMD_OK;
394 }
395
396 if ((target=getnickbynumericstr(cargv[0]))==NULL) {
397 return CMD_OK;
398 }
399
400 accountts=strtoul(cargv[2],NULL,10);
401 userid=strtoul(cargv[3],NULL,10);
402 if(cargv>=5)
403 accountflags=strtoul(cargv[4],NULL,10);
404
405 /* allow user flags to change if all fields match */
406 if (IsAccount(target)) {
407 void *arg[2];
408
409 if (!target->auth || strcmp(target->authname,cargv[1]) || (target->auth->userid != userid) || (target->accountts != accountts)) {
410 return CMD_OK;
411 }
412
413 arg[0] = (void *)target->auth;
414 arg[1] = (void *)(long)target->auth->flags;
415
416 if (cargc>=5)
417 target->auth->flags=accountflags;
418
419 triggerhook(HOOK_AUTH_FLAGSUPDATED, (void *)arg);
420
421 /* TODO: trigger flag update hook */
422 return CMD_OK;
423 }
424
425 SetAccount(target);
426 strncpy(target->authname,cargv[1],ACCOUNTLEN);
427 target->authname[ACCOUNTLEN]='\0';
428 target->accountts=accountts;
429
430 if(!userid) {
431 target->auth=NULL;
432 } else {
433 target->auth=findorcreateauthname(userid, target->authname);
434 target->auth->usercount++;
435 target->nextbyauthname = target->auth->nicks;
436 target->auth->nicks = target;
437 if (cargc>=5)
438 target->auth->flags=accountflags;
439 }
440
441 triggerhook(HOOK_NICK_ACCOUNT, (void *)target);
442
443 return CMD_OK;
444 }
445
446 int handlestatsmsg(void *source, int cargc, char **cargv) {
447 int sourceserver;
448 char *sender=(char *)source;
449 char *replytarget;
450 char *fromstring;
451 nick *np;
452
453 if (cargc<2) {
454 Error("nick",ERR_WARNING,"STATS request without enough parameters!");
455 return CMD_OK;
456 }
457
458 if (strlen(sender)==5) {
459 /* client */
460 np=getnickbynumericstr(sender);
461 if (!np) {
462 Error("nick",ERR_WARNING,"STATS request from unknown client %s",sender);
463 return CMD_OK;
464 }
465 replytarget=np->nick;
466 } else {
467 Error("nick",ERR_WARNING,"STATS request from odd source %s",sender);
468 return CMD_OK;
469 }
470
471 /* Reply to stats for ANY server.. including any we are juping */
472 sourceserver=numerictolong(cargv[1],2);
473 if (serverlist[sourceserver].maxusernum==0) {
474 Error("nick",ERR_WARNING,"Stats request for bad server %s",cargv[1]);
475 return CMD_OK;
476 }
477 fromstring=serverlist[sourceserver].name->content;
478
479 switch(cargv[0][0]) {
480 case 'u':
481 irc_send(":%s 242 %s :Server Up %s",fromstring,replytarget,
482 longtoduration(time(NULL)-starttime, 0));
483 irc_send(":%s 250 %s :Highest connection count: 10 (9 clients)",fromstring,replytarget);
484 break;
485
486 case 'P':
487 irc_send(":%s 217 %s P none 0 :0x2000",fromstring,replytarget);
488 break;
489
490 }
491
492 irc_send(":%s 219 %s %c :End of /STATS report",fromstring,replytarget,cargv[0][0]);
493
494 return CMD_OK;
495 }
496
497 int handleprivmsg(void *source, int cargc, char **cargv) {
498 nick *sender;
499 char *message;
500 void *args[3];
501
502 if (cargc<2)
503 return CMD_OK;
504
505 if (cargv[0][0]!='$')
506 return CMD_OK;
507
508 sender=getnickbynumericstr((char *)source);
509
510 if (!match2strings(cargv[0] + 1,myserver->content))
511 return CMD_OK;
512
513 message=cargv[0];
514
515 args[0]=sender;
516 args[1]=cargv[0];
517 args[2]=cargv[1];
518
519 triggerhook(HOOK_NICK_MASKPRIVMSG, (void *)args);
520
521 return CMD_OK;
522 }
523