]> jfr.im git - irc/quakenet/newserv.git/blob - nick/nickhandlers.c
b704f31ab27b417764be1c9a1ed0cf7ee1e29926
[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, ipaddress_canonical;
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 memcpy(&(np->ipaddress), &ipaddress, sizeof(ipaddress));
148
149 ip_canonicalize_tunnel(&ipaddress_canonical, &ipaddress);
150 np->ipnode = refnode(iptree, &ipaddress_canonical, PATRICIA_MAXBITS);
151 node_increment_usercount(np->ipnode);
152
153 np->away=NULL;
154 np->shident=NULL;
155 np->sethost=NULL;
156 np->opername=NULL;
157 np->umodes=0;
158 np->marker=0;
159 memset(np->exts, 0, MAXNICKEXTS * sizeof(void *));
160 np->authname=NULLAUTHNAME;
161 np->auth=NULL;
162 np->accountts=0;
163 np->cloak_count = 0;
164 np->cloak_extra = NULL;
165 if(cargc>=9) {
166 int sethostarg = 6, opernamearg = 6, accountarg = 6;
167
168 setflags(&(np->umodes),UMODE_ALL,cargv[5],umodeflags,REJECT_NONE);
169
170 if(IsOper(np) && (serverlist[myhub].flags & SMODE_OPERNAME)) {
171 accountarg++;
172 sethostarg++;
173
174 np->opername=getsstring(cargv[opernamearg],ACCOUNTLEN);
175 }
176
177 if (IsAccount(np)) {
178 sethostarg++;
179
180 if ((accountts=strchr(cargv[accountarg],':'))) {
181 userid=0;
182 *accountts++='\0';
183 np->accountts=strtoul(accountts,&accountid,10);
184 if(accountid) {
185 userid=strtoul(accountid + 1,&accountflags,10);
186 if(userid) {
187 np->auth=findorcreateauthname(userid, cargv[accountarg]);
188 np->authname=np->auth->name;
189 np->auth->usercount++;
190 np->nextbyauthname=np->auth->nicks;
191 np->auth->nicks=np;
192 if(accountflags)
193 np->auth->flags=strtoull(accountflags + 1,NULL,10);
194 }
195 }
196 if(!userid) {
197 np->authname=malloc(strlen(cargv[accountarg]) + 1);
198 strcpy(np->authname,cargv[accountarg]);
199 }
200 }
201 }
202 if (IsSetHost(np) && (fakehost=strchr(cargv[sethostarg],'@'))) {
203 /* valid sethost */
204 *fakehost++='\0';
205 np->shident=getsstring(cargv[sethostarg],USERLEN);
206 np->sethost=getsstring(fakehost,HOSTLEN);
207 }
208 }
209
210 /* Place this nick in the server nick table. Note that nh is valid from the numeric check above */
211 if (*nh) {
212 /* There was a nick there already -- we have a masked numeric collision
213 * This shouldn't happen, but if it does the newer nick takes precedence
214 * (the two nicks are from the same server, and if the server has reissued
215 * the masked numeric it must believe the old user no longer exists).
216 */
217 Error("nick",ERR_ERROR,"Masked numeric collision for %s [%s vs %s]",cargv[0],cargv[cargc-2],longtonumeric((*nh)->numeric,5));
218 deletenick(*nh);
219 }
220 *nh=np;
221
222 /* And the nick hash table */
223 addnicktohash(np);
224
225 /* Trigger the hook */
226 triggerhook(HOOK_NICK_NEWNICK,np);
227 } else {
228 Error("nick",ERR_WARNING,"Nick message with weird number of parameters (%d)",cargc);
229 }
230
231 return CMD_OK;
232 }
233
234 int handlequitmsg(void *source, int cargc, char **cargv) {
235 nick *np;
236 void *harg[2];
237
238 if (cargc>0) {
239 harg[1]=(void *)cargv[0];
240 } else {
241 harg[1]="";
242 }
243
244 np=getnickbynumericstr((char *)source);
245 if (np) {
246 harg[0]=(void *)np;
247 triggerhook(HOOK_NICK_QUIT, harg);
248 deletenick(np);
249 } else {
250 Error("nick",ERR_WARNING,"Quit from non-existant numeric %s",(char *)source);
251 }
252 return CMD_OK;
253 }
254
255 int handlekillmsg(void *source, int cargc, char **cargv) {
256 nick *np;
257 void *harg[2];
258 #warning Fix me to use source
259
260 if (cargc<1) {
261 Error("nick",ERR_WARNING,"Kill message with too few parameters");
262 return CMD_ERROR;
263 }
264
265 if (cargc>1) {
266 harg[1]=(void *)cargv[1];
267 } else {
268 harg[1]="";
269 }
270
271 np=getnickbynumericstr(cargv[0]);
272 if (np) {
273 harg[0]=(void *)np;
274 triggerhook(HOOK_NICK_KILL, harg);
275 deletenick(np);
276 } else {
277 Error("nick",ERR_WARNING,"Kill for non-existant numeric %s",cargv[0]);
278 }
279 return CMD_OK;
280 }
281
282 int handleusermodemsg(void *source, int cargc, char **cargv) {
283 nick *np;
284 flag_t oldflags;
285 char *fakehost;
286
287 if (cargc<2) {
288 Error("nick",ERR_WARNING,"Mode message with too few parameters");
289 return CMD_LAST; /* With <2 params the channels module won't want it either */
290 }
291
292 if (cargv[0][0]=='#') {
293 /* Channel mode change, we don't care.
294 * We don't bother checking for other channel types here, since & channel mode
295 * changes aren't broadcast and + channels don't have mode changes :) */
296 return CMD_OK;
297 }
298
299 np=getnickbynumericstr((char *)source);
300 if (np!=NULL) {
301 if (ircd_strcmp(cargv[0],np->nick)) {
302 Error("nick",ERR_WARNING,"Attempted mode change on user %s by %s",cargv[0],np->nick);
303 return CMD_OK;
304 }
305 oldflags=np->umodes;
306 setflags(&(np->umodes),UMODE_ALL,cargv[1],umodeflags,REJECT_NONE);
307
308 if (strchr(cargv[1],'o')) { /* o always comes on its own when being set */
309 if(serverlist[myhub].flags & SMODE_OPERNAME) {
310 if((np->umodes & UMODE_OPER)) {
311 np->opername = getsstring(cargv[2], ACCOUNTLEN);
312 } else {
313 freesstring(np->opername);
314 np->opername = NULL;
315 }
316 }
317 if((np->umodes ^ oldflags) & UMODE_OPER)
318 triggerhook(HOOK_NICK_MODEOPER,np);
319 }
320 if (strchr(cargv[1],'h')) { /* Have to allow +h twice.. */
321 /* +-h: just the freesstring() calls for the -h case */
322 freesstring(np->shident); /* freesstring(NULL) is OK */
323 freesstring(np->sethost);
324 np->shident=NULL;
325 np->sethost=NULL;
326 if (IsSetHost(np) && cargc>2) { /* +h and mask received */
327 if ((fakehost=strchr(cargv[2],'@'))) {
328 /* user@host change */
329 *fakehost++='\0';
330 np->shident=getsstring(cargv[2],USERLEN);
331 np->sethost=getsstring(fakehost,HOSTLEN);
332 } else {
333 np->sethost=getsstring(cargv[2],HOSTLEN);
334 }
335 }
336 triggerhook(HOOK_NICK_SETHOST, (void *)np);
337 }
338 } else {
339 Error("nick",ERR_WARNING,"Usermode change by unknown user %s",(char *)source);
340 }
341 return CMD_OK;
342 }
343
344 int handleaccountmsg(void *source, int cargc, char **cargv) {
345 nick *target;
346 unsigned long userid;
347 time_t accountts;
348 u_int64_t accountflags=0, oldflags;
349
350 if (cargc<4) {
351 return CMD_OK;
352 }
353
354 if ((target=getnickbynumericstr(cargv[0]))==NULL) {
355 return CMD_OK;
356 }
357
358 accountts=strtoul(cargv[2],NULL,10);
359 userid=strtoul(cargv[3],NULL,10);
360 if(cargc>=5)
361 accountflags=strtoull(cargv[4],NULL,10);
362
363 /* allow user flags to change if all fields match */
364 if (IsAccount(target)) {
365 void *arg[2];
366
367 if (!target->auth || strcmp(target->auth->name,cargv[1]) || (target->auth->userid != userid) || (target->accountts != accountts)) {
368 return CMD_OK;
369 }
370
371 oldflags = target->auth->flags;
372 arg[0] = target->auth;
373 arg[1] = &oldflags;
374
375 if (cargc>=5)
376 target->auth->flags=accountflags;
377
378 triggerhook(HOOK_AUTH_FLAGSUPDATED, (void *)arg);
379
380 return CMD_OK;
381 }
382
383 SetAccount(target);
384 target->accountts=accountts;
385
386 if(!userid) {
387 target->auth=NULL;
388 target->authname=malloc(strlen(cargv[1]) + 1);
389 strcpy(target->authname,cargv[1]);
390 } else {
391 target->auth=findorcreateauthname(userid, cargv[1]);
392 target->auth->usercount++;
393 target->authname=target->auth->name;
394 target->nextbyauthname = target->auth->nicks;
395 target->auth->nicks = target;
396 if (cargc>=5)
397 target->auth->flags=accountflags;
398 }
399
400 triggerhook(HOOK_NICK_ACCOUNT, (void *)target);
401
402 return CMD_OK;
403 }
404
405 int handleprivmsg(void *source, int cargc, char **cargv) {
406 nick *sender;
407 void *args[3];
408
409 if (cargc<2)
410 return CMD_OK;
411
412 if (cargv[0][0]!='$')
413 return CMD_OK;
414
415 sender=getnickbynumericstr((char *)source);
416
417 if (!match2strings(cargv[0] + 1,myserver->content))
418 return CMD_OK;
419
420 args[0]=sender;
421 args[1]=cargv[0];
422 args[2]=cargv[1];
423
424 triggerhook(HOOK_NICK_MASKPRIVMSG, (void *)args);
425
426 return CMD_OK;
427 }
428
429 int handleawaymsg(void *source, int cargc, char **cargv) {
430 nick *sender;
431
432 /* Check source is a valid user */
433 if (!(sender=getnickbynumericstr(source))) {
434 return CMD_OK;
435 }
436
437 /* Done with the old away message either way */
438 freesstring(sender->away);
439 sender->away=NULL;
440
441 /* If we have an arg and it isn't an empty string, this sets a new message */
442 if (cargc > 0 && *(cargv[0])) {
443 sender->away=getsstring(cargv[0], AWAYLEN);
444 }
445
446 return CMD_OK;
447 }
448
449 int handleaddcloak(void *source, int cargc, char **cargv) {
450 nick *sender, *target;
451
452 /* Check source is a valid user */
453 if (!(sender=getnickbynumericstr(source))) {
454 return CMD_OK;
455 }
456
457 if (cargc < 1)
458 return CMD_OK;
459
460 if (!(target=getnickbynumericstr(cargv[0]))) {
461 return CMD_OK;
462 }
463
464 addcloaktarget(sender, target);
465
466 return CMD_OK;
467 }
468
469 int handleclearcloak(void *source, int cargc, char **cargv) {
470 nick *sender;
471
472 /* Check source is a valid user */
473 if (!(sender=getnickbynumericstr(source))) {
474 return CMD_OK;
475 }
476
477 clearcloaktargets(sender);
478
479 return CMD_OK;
480 }
481