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