]> jfr.im git - irc/quakenet/newserv.git/blame - nick/nickhandlers.c
patricia trie changes
[irc/quakenet/newserv.git] / nick / nickhandlers.c
CommitLineData
c86edd1d
Q
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
23int 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;
c153c0dc 30 char *accountflags;
526e7c1d 31 struct irc_in_addr ipaddress;
47657339
C
32 char *accountid;
33 unsigned long userid;
c86edd1d
Q
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 }
1a38afd3
P
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
c86edd1d
Q
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;
526e7c1d
P
140
141 base64toip(cargv[cargc-3], &ipaddress);
142 /* todo: use a single node for /64 prefixes */
143 np->ipnode = refnode(iptree, &ipaddress, irc_in_addr_is_ipv4(&ipaddress) ? PATRICIA_MAXBITS : 64);
96644df6 144 node_increment_usercount(np->ipnode);
526e7c1d 145
c86edd1d
Q
146 np->shident=NULL;
147 np->sethost=NULL;
148 np->umodes=0;
149 np->marker=0;
150 memset(np->exts, 0, MAXNICKEXTS * sizeof(void *));
151 np->authname[0]='\0';
47657339 152 np->auth=NULL;
c153c0dc 153 np->accountflags=0;
c86edd1d
Q
154 if(cargc>=9) {
155 setflags(&(np->umodes),UMODE_ALL,cargv[5],umodeflags,REJECT_NONE);
156 if (IsAccount(np)) {
157 if ((accountts=strchr(cargv[6],':'))) {
158 *accountts++='\0';
47657339
C
159 np->accountts=strtoul(accountts,&accountid,10);
160 if(accountid) {
c153c0dc 161 userid=strtoul(accountid + 1,&accountflags,10);
47657339
C
162 if(!userid) {
163 np->auth=NULL;
164 } else {
165 np->auth=findorcreateauthname(userid);
166 np->auth->usercount++;
167 np->nextbyauthname=np->auth->nicks;
168 np->auth->nicks=np;
169 }
c153c0dc
CP
170 if(accountflags)
171 np->accountflags=strtoul(accountflags + 1,NULL,10);
47657339
C
172 } else {
173 np->auth=NULL;
174 }
c86edd1d
Q
175 } else {
176 np->accountts=0;
47657339 177 np->auth=NULL;
c86edd1d
Q
178 }
179 strncpy(np->authname,cargv[6],ACCOUNTLEN);
180 np->authname[ACCOUNTLEN]='\0';
181 }
182 if (IsSetHost(np) && (fakehost=strchr(cargv[cargc-4],'@'))) {
183 /* valid sethost */
184 *fakehost++='\0';
185 np->shident=getsstring(cargv[cargc-4],USERLEN);
186 np->sethost=getsstring(fakehost,HOSTLEN);
187 }
188 }
189
190 /* Place this nick in the server nick table. Note that nh is valid from the numeric check above */
191 if (*nh) {
192 /* There was a nick there already -- we have a masked numeric collision
193 * This shouldn't happen, but if it does the newer nick takes precedence
194 * (the two nicks are from the same server, and if the server has reissued
195 * the masked numeric it must believe the old user no longer exists).
196 */
197 Error("nick",ERR_ERROR,"Masked numeric collision for %s [%s vs %s]",cargv[0],cargv[cargc-2],longtonumeric((*nh)->numeric,5));
198 deletenick(*nh);
199 }
200 *nh=np;
201
202 /* And the nick hash table */
203 addnicktohash(np);
204
205 /* Trigger the hook */
206 triggerhook(HOOK_NICK_NEWNICK,np);
207 } else {
208 Error("nick",ERR_WARNING,"Nick message with weird number of parameters (%d)",cargc);
209 }
210
211 return CMD_OK;
212}
213
214int handlequitmsg(void *source, int cargc, char **cargv) {
215 nick *np;
216 void *harg[2];
217
85c382d3 218 if (cargc>0) {
219 harg[1]=(void *)cargv[0];
c86edd1d
Q
220 } else {
221 harg[1]="";
222 }
223
224 np=getnickbynumericstr((char *)source);
225 if (np) {
226 harg[0]=(void *)np;
227 triggerhook(HOOK_NICK_QUIT, harg);
228 deletenick(np);
229 } else {
230 Error("nick",ERR_WARNING,"Quit from non-existant numeric %s",(char *)source);
231 }
232 return CMD_OK;
233}
234
235int handlekillmsg(void *source, int cargc, char **cargv) {
236 nick *np;
acd438c7 237 void *harg[2];
e3073692
CP
238#warning Fix me to use source
239
c86edd1d
Q
240 if (cargc<1) {
241 Error("nick",ERR_WARNING,"Kill message with too few parameters");
242 return CMD_ERROR;
243 }
acd438c7
CP
244
245 if (cargc>1) {
246 harg[1]=(void *)cargv[1];
247 } else {
248 harg[1]="";
249 }
250
c86edd1d
Q
251 np=getnickbynumericstr(cargv[0]);
252 if (np) {
acd438c7
CP
253 harg[0]=(void *)np;
254 triggerhook(HOOK_NICK_KILL, harg);
c86edd1d
Q
255 deletenick(np);
256 } else {
257 Error("nick",ERR_WARNING,"Kill for non-existant numeric %s",cargv[0]);
258 }
259 return CMD_OK;
260}
261
262int handleusermodemsg(void *source, int cargc, char **cargv) {
263 nick *np;
264 flag_t oldflags;
265 char *fakehost;
266
267 if (cargc<2) {
268 Error("nick",ERR_WARNING,"Mode message with too few parameters");
269 return CMD_LAST; /* With <2 params the channels module won't want it either */
270 }
271
272 if (cargv[0][0]=='#') {
273 /* Channel mode change, we don't care.
274 * We don't bother checking for other channel types here, since & channel mode
275 * changes aren't broadcast and + channels don't have mode changes :) */
276 return CMD_OK;
277 }
278
279 np=getnickbynumericstr((char *)source);
280 if (np!=NULL) {
281 if (ircd_strcmp(cargv[0],np->nick)) {
282 Error("nick",ERR_WARNING,"Attempted mode change on user %s by %s",cargv[0],np->nick);
283 return CMD_OK;
284 }
285 oldflags=np->umodes;
3e3692bf
CP
286 if (strchr(cargv[1],'o')) {
287 void *harg[2];
288 harg[0]=np;
289 harg[1]=cargv[1];
290 triggerhook(HOOK_NICK_MODEOPER,harg);
291 }
c86edd1d
Q
292 setflags(&(np->umodes),UMODE_ALL,cargv[1],umodeflags,REJECT_NONE);
293 if (strchr(cargv[1],'h')) { /* Have to allow +h twice.. */
294 /* +-h: just the freesstring() calls for the -h case */
295 freesstring(np->shident); /* freesstring(NULL) is OK */
296 freesstring(np->sethost);
297 np->shident=NULL;
298 np->sethost=NULL;
299 if (IsSetHost(np) && cargc>2) { /* +h and mask received */
300 if ((fakehost=strchr(cargv[2],'@'))) {
301 /* user@host change */
302 *fakehost++='\0';
303 np->shident=getsstring(cargv[2],USERLEN);
304 np->sethost=getsstring(fakehost,HOSTLEN);
305 } else {
306 np->sethost=getsstring(cargv[2],HOSTLEN);
307 }
308 }
309 triggerhook(HOOK_NICK_SETHOST, (void *)np);
310 }
311 } else {
312 Error("nick",ERR_WARNING,"Usermode change by unknown user %s",(char *)source);
313 }
314 return CMD_OK;
315}
316
317int handlewhoismsg(void *source, int cargc, char **cargv) {
318 nick *sender,*target;
319 nick *nicks[2];
320
321 if (cargc<2)
322 return CMD_OK;
323
324 if (strncmp(cargv[0],mynumeric->content,2)) {
325 return CMD_OK;
326 }
327
328 /* Find the sender... */
329 if ((sender=getnickbynumericstr((char *)source))==NULL) {
330 Error("localuser",ERR_WARNING,"WHOIS message from non existent numeric %s",(char *)source);
331 return CMD_OK;
332 }
333
334 /* :hub.splidge.netsplit.net 311 moo splidge splidge ground.stbarnab.as * :splidge
335 :hub.splidge.netsplit.net 312 moo splidge splidge.netsplit.net :splidge's netsplit leaf
336 :hub.splidge.netsplit.net 313 moo splidge :is an IRC Operator
337 :hub.splidge.netsplit.net 318 moo splidge :End of /WHOIS list.
338 */
339
340 /* And the target... */
341 if ((target=getnickbynick(cargv[1]))==NULL) {
342 irc_send(":%s 401 %s %s :No such nick",myserver->content,sender->nick,cargv[1]);
343 } else {
344 irc_send(":%s 311 %s %s %s %s * :%s",myserver->content,sender->nick,target->nick,target->ident,
345 target->host->name->content, target->realname->name->content);
346 nicks[0]=sender; nicks[1]=target;
347 triggerhook(HOOK_NICK_WHOISCHANNELS,nicks);
348 if (IsOper(sender) || !HIS_SERVER ) {
349 irc_send(":%s 312 %s %s %s :%s",myserver->content,sender->nick,target->nick,
350 serverlist[homeserver(target->numeric)].name->content,
351 serverlist[homeserver(target->numeric)].description->content);
352 } else {
353 irc_send(":%s 312 %s %s " HIS_SERVERNAME " :" HIS_SERVERDESC,myserver->content,sender->nick,target->nick);
354 }
355 if (IsOper(target)) {
356 irc_send(":%s 313 %s %s :is an IRC Operator",myserver->content,sender->nick,target->nick);
357 }
358 if (IsAccount(target)) {
109ba422 359 irc_send(":%s 330 %s %s %s :is authed as",myserver->content,sender->nick,target->nick,target->authname);
c86edd1d 360 }
109ba422 361 if (homeserver(target->numeric)==mylongnum && !IsService(target) && !IsHideIdle(target)) {
c86edd1d 362 irc_send(":%s 317 %s %s %ld %ld :seconds idle, signon time",myserver->content,sender->nick,target->nick,
80300a20 363 (getnettime() - target->timestamp) % (((target->numeric + target->timestamp) % 983) + 7),target->timestamp);
c86edd1d
Q
364 }
365 }
366
367 irc_send(":%s 318 %s %s :End of /WHOIS list.",myserver->content,sender->nick,cargv[1]);
368 return CMD_OK;
369}
370
371int handleaccountmsg(void *source, int cargc, char **cargv) {
372 nick *target;
47657339 373 unsigned long userid;
c86edd1d
Q
374
375 if (cargc<2) {
376 return CMD_OK;
377 }
378
379 if ((target=getnickbynumericstr(cargv[0]))==NULL) {
380 return CMD_OK;
381 }
382
383 if (IsAccount(target)) {
384 return CMD_OK;
385 }
386
387 SetAccount(target);
388 strncpy(target->authname,cargv[1],ACCOUNTLEN);
389 target->authname[ACCOUNTLEN]='\0';
47657339
C
390
391 if (cargc>=3) {
392 target->accountts=strtoul(cargv[2],NULL,10);
393 if (cargc>=4) {
394 userid=strtoul(cargv[3],NULL,10);
395 if(!userid) {
396 target->auth=NULL;
397 } else {
398 target->auth=findorcreateauthname(userid);
399 target->auth->usercount++;
400 target->nextbyauthname = target->auth->nicks;
401 target->auth->nicks = target;
402 }
c153c0dc
CP
403 if (cargc>=5)
404 target->accountflags=strtoul(cargv[4],NULL,10);
47657339
C
405 } else {
406 target->auth=NULL;
407 }
408 } else {
409 target->accountts=0;
410 target->auth=NULL;
411 }
412
c86edd1d 413 triggerhook(HOOK_NICK_ACCOUNT, (void *)target);
526e7c1d 414
c86edd1d
Q
415 return CMD_OK;
416}
417
418int handlestatsmsg(void *source, int cargc, char **cargv) {
419 int sourceserver;
420 char *sender=(char *)source;
421 char *replytarget;
422 char *fromstring;
423 nick *np;
424
425 if (cargc<2) {
426 Error("nick",ERR_WARNING,"STATS request without enough parameters!");
427 return CMD_OK;
428 }
429
430 if (strlen(sender)==5) {
431 /* client */
432 np=getnickbynumericstr(sender);
433 if (!np) {
434 Error("nick",ERR_WARNING,"STATS request from unknown client %s",sender);
435 return CMD_OK;
436 }
437 replytarget=np->nick;
438 } else {
439 Error("nick",ERR_WARNING,"STATS request from odd source %s",sender);
440 return CMD_OK;
441 }
442
443 /* Reply to stats for ANY server.. including any we are juping */
444 sourceserver=numerictolong(cargv[1],2);
445 if (serverlist[sourceserver].maxusernum==0) {
446 Error("nick",ERR_WARNING,"Stats request for bad server %s",cargv[1]);
447 return CMD_OK;
448 }
449 fromstring=serverlist[sourceserver].name->content;
450
451 switch(cargv[0][0]) {
452 case 'u':
453 irc_send(":%s 242 %s :Server Up %s",fromstring,replytarget,
454 longtoduration(time(NULL)-starttime, 0));
455 irc_send(":%s 250 %s :Highest connection count: 10 (9 clients)",fromstring,replytarget);
456 break;
457
458 case 'P':
459 irc_send(":%s 217 %s P none 0 :0x2000",fromstring,replytarget);
460 break;
461
462 }
463
464 irc_send(":%s 219 %s %c :End of /STATS report",fromstring,replytarget,cargv[0][0]);
465
466 return CMD_OK;
467}