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