]>
jfr.im git - irc/quakenet/newserv.git/blob - nick/nickhandlers.c
282139f3070696df4583b82346e34c13b8a08265
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"
20 * Handle new nicks being introduced to the network.
24 int handlenickmsg(void *source
, int cargc
, char **cargv
) {
25 char *sender
=(char *)source
;
32 struct irc_in_addr ipaddress
;
36 if (cargc
==2) { /* rename */
37 /* Nyklon 1017697578 */
38 timestamp
=strtol(cargv
[1],NULL
,10);
39 np
=getnickbynumericstr(sender
);
41 Error("nick",ERR_ERROR
,"Rename from non-existent sender %s",sender
);
44 np2
=getnickbynick(cargv
[0]);
46 /* The new and old nickname have the same hash, this means a rename to the same name in
47 * different case, e.g. Flash -> flash. In this case the timestamp for the change should
48 * match the existing timestamp, and we can bypass all the collision checking and hash fettling. */
49 if (np
->timestamp
!=timestamp
) {
50 Error("nick",ERR_WARNING
,"Rename to same nickname with different timestamp (%s(%jd) -> %s(%jd))",
51 np
->nick
,(intmax_t)np
->timestamp
,cargv
[0], (intmax_t)timestamp
);
52 np
->timestamp
=timestamp
;
54 strncpy(np
->nick
,cargv
[0],NICKLEN
);
55 np
->nick
[NICKLEN
]='\0';
56 triggerhook(HOOK_NICK_RENAME
,np
);
61 if (ircd_strcmp(np
->ident
,np2
->ident
) || (np
->host
!=np2
->host
)) {
62 /* Different user@host */
63 if (np2
->timestamp
< timestamp
) {
64 /* The nick attempting to rename got killed. Guess we don't need to do the rename now :) */
68 /* The other nick got killed */
72 if (np2
->timestamp
< timestamp
) {
73 /* Same user@host: reverse logic. Whose idea was all this anyway? */
81 /* OK, we've survived the collision hazard. Change timestamp and rename */
82 np
->timestamp
=timestamp
;
83 removenickfromhash(np
);
84 strncpy(np
->nick
,cargv
[0],NICKLEN
);
85 np
->nick
[NICKLEN
]='\0';
87 triggerhook(HOOK_NICK_RENAME
,np
);
88 } else if (cargc
>=8) { /* new nick */
89 /* Jupiler 2 1016645147 ~Jupiler www.iglobal.be +ir moo [FUTURE CRAP HERE] DV74O] BNBd7 :Jupiler */
90 timestamp
=strtol(cargv
[2],NULL
,10);
91 np
=getnickbynick(cargv
[0]);
94 if (ircd_strcmp(np
->ident
,cargv
[3]) || ircd_strcmp(np
->host
->name
->content
,cargv
[4])) {
95 /* Different user@host */
96 if (timestamp
>np
->timestamp
) {
97 /* New nick is newer. Ignore this nick message */
100 /* New nick is older. Kill the imposter, and drop through */
104 if (timestamp
>np
->timestamp
) {
105 /* Same user@host, newer timestamp: we're killing off a ghost */
108 /* This nick is the ghost, so ignore it */
114 nh
=gethandlebynumeric(numerictolong(cargv
[cargc
-2],5));
116 /* This isn't a valid numeric */
117 Error("nick",ERR_WARNING
,"Received NICK with invalid numeric %s from %s.",cargv
[cargc
-2],sender
);
121 base64toip(cargv
[cargc
-3], &ipaddress
);
122 if (!irc_in_addr_valid(&ipaddress
)) {
123 Error("nick",ERR_ERROR
,"Received NICK with invalid ipaddress for %s from %s.",cargv
[0],sender
);
127 /* At this stage the nick is cleared to proceed */
129 strncpy(np
->nick
,cargv
[0],NICKLEN
);
130 np
->nick
[NICKLEN
]='\0';
131 np
->numeric
=numerictolong(cargv
[cargc
-2],5);
132 strncpy(np
->ident
,cargv
[3],USERLEN
);
133 np
->ident
[USERLEN
]='\0';
134 np
->host
=findorcreatehost(cargv
[4]);
135 np
->realname
=findorcreaterealname(cargv
[cargc
-1]);
136 np
->nextbyhost
=np
->host
->nicks
;
138 np
->nextbyrealname
=np
->realname
->nicks
;
139 np
->realname
->nicks
=np
;
140 np
->timestamp
=timestamp
;
142 base64toip(cargv
[cargc
-3], &ipaddress
);
143 np
->ipnode
= refnode(iptree
, &ipaddress
, PATRICIA_MAXBITS
);
144 node_increment_usercount(np
->ipnode
);
152 memset(np
->exts
, 0, MAXNICKEXTS
* sizeof(void *));
153 np
->authname
=NULLAUTHNAME
;
157 int sethostarg
= 6, opernamearg
= 6, accountarg
= 6;
159 setflags(&(np
->umodes
),UMODE_ALL
,cargv
[5],umodeflags
,REJECT_NONE
);
161 if(IsOper(np
) && (serverlist
[myhub
].flags
& SMODE_OPERNAME
)) {
165 np
->opername
=getsstring(cargv
[opernamearg
],ACCOUNTLEN
);
171 if ((accountts
=strchr(cargv
[accountarg
],':'))) {
174 np
->accountts
=strtoul(accountts
,&accountid
,10);
176 userid
=strtoul(accountid
+ 1,&accountflags
,10);
178 np
->auth
=findorcreateauthname(userid
, cargv
[accountarg
]);
179 np
->authname
=np
->auth
->name
;
180 np
->auth
->usercount
++;
181 np
->nextbyauthname
=np
->auth
->nicks
;
184 np
->auth
->flags
=strtoull(accountflags
+ 1,NULL
,10);
188 np
->authname
=malloc(strlen(cargv
[accountarg
]) + 1);
189 strcpy(np
->authname
,cargv
[accountarg
]);
193 if (IsSetHost(np
) && (fakehost
=strchr(cargv
[sethostarg
],'@'))) {
196 np
->shident
=getsstring(cargv
[sethostarg
],USERLEN
);
197 np
->sethost
=getsstring(fakehost
,HOSTLEN
);
201 /* Place this nick in the server nick table. Note that nh is valid from the numeric check above */
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).
208 Error("nick",ERR_ERROR
,"Masked numeric collision for %s [%s vs %s]",cargv
[0],cargv
[cargc
-2],longtonumeric((*nh
)->numeric
,5));
213 /* And the nick hash table */
216 /* Trigger the hook */
217 triggerhook(HOOK_NICK_NEWNICK
,np
);
219 Error("nick",ERR_WARNING
,"Nick message with weird number of parameters (%d)",cargc
);
225 int handlequitmsg(void *source
, int cargc
, char **cargv
) {
230 harg
[1]=(void *)cargv
[0];
235 np
=getnickbynumericstr((char *)source
);
238 triggerhook(HOOK_NICK_QUIT
, harg
);
241 Error("nick",ERR_WARNING
,"Quit from non-existant numeric %s",(char *)source
);
246 int handlekillmsg(void *source
, int cargc
, char **cargv
) {
249 #warning Fix me to use source
252 Error("nick",ERR_WARNING
,"Kill message with too few parameters");
257 harg
[1]=(void *)cargv
[1];
262 np
=getnickbynumericstr(cargv
[0]);
265 triggerhook(HOOK_NICK_KILL
, harg
);
268 Error("nick",ERR_WARNING
,"Kill for non-existant numeric %s",cargv
[0]);
273 int handleusermodemsg(void *source
, int cargc
, char **cargv
) {
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 */
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 :) */
290 np
=getnickbynumericstr((char *)source
);
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
);
297 setflags(&(np
->umodes
),UMODE_ALL
,cargv
[1],umodeflags
,REJECT_NONE
);
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
);
304 freesstring(np
->opername
);
308 if((np
->umodes
^ oldflags
) & UMODE_OPER
)
309 triggerhook(HOOK_NICK_MODEOPER
,np
);
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
);
317 if (IsSetHost(np
) && cargc
>2) { /* +h and mask received */
318 if ((fakehost
=strchr(cargv
[2],'@'))) {
319 /* user@host change */
321 np
->shident
=getsstring(cargv
[2],USERLEN
);
322 np
->sethost
=getsstring(fakehost
,HOSTLEN
);
324 np
->sethost
=getsstring(cargv
[2],HOSTLEN
);
327 triggerhook(HOOK_NICK_SETHOST
, (void *)np
);
330 Error("nick",ERR_WARNING
,"Usermode change by unknown user %s",(char *)source
);
335 int handlewhoismsg(void *source
, int cargc
, char **cargv
) {
336 nick
*sender
,*target
;
342 if (strncmp(cargv
[0],mynumeric
->content
,2)) {
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
);
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.
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]);
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
);
371 irc_send(":%s 312 %s %s " HIS_SERVERNAME
" :" HIS_SERVERDESC
,myserver
->content
,sender
->nick
,target
->nick
);
373 if (IsOper(target
)) {
374 irc_send(":%s 313 %s %s :is an IRC Operator",myserver
->content
,sender
->nick
,target
->nick
);
376 if (IsAccount(target
)) {
377 irc_send(":%s 330 %s %s %s :is authed as",myserver
->content
,sender
->nick
,target
->nick
,target
->authname
);
379 if (homeserver(target
->numeric
)==mylongnum
&& !IsService(target
) && !IsHideIdle(target
)) {
380 irc_send(":%s 317 %s %s %ld %ld :seconds idle, signon time",myserver
->content
,sender
->nick
,target
->nick
,
381 (getnettime() - target
->timestamp
) % (((target
->numeric
+ target
->timestamp
) % 983) + 7),target
->timestamp
);
385 irc_send(":%s 318 %s %s :End of /WHOIS list.",myserver
->content
,sender
->nick
,cargv
[1]);
389 int handleaccountmsg(void *source
, int cargc
, char **cargv
) {
391 unsigned long userid
;
393 u_int64_t accountflags
=0, oldflags
;
399 if ((target
=getnickbynumericstr(cargv
[0]))==NULL
) {
403 accountts
=strtoul(cargv
[2],NULL
,10);
404 userid
=strtoul(cargv
[3],NULL
,10);
406 accountflags
=strtoull(cargv
[4],NULL
,10);
408 /* allow user flags to change if all fields match */
409 if (IsAccount(target
)) {
412 if (!target
->auth
|| strcmp(target
->auth
->name
,cargv
[1]) || (target
->auth
->userid
!= userid
) || (target
->accountts
!= accountts
)) {
416 oldflags
= target
->auth
->flags
;
417 arg
[0] = target
->auth
;
421 target
->auth
->flags
=accountflags
;
423 triggerhook(HOOK_AUTH_FLAGSUPDATED
, (void *)arg
);
429 target
->accountts
=accountts
;
433 target
->authname
=malloc(strlen(cargv
[1]) + 1);
434 strcpy(target
->authname
,cargv
[1]);
436 target
->auth
=findorcreateauthname(userid
, cargv
[1]);
437 target
->auth
->usercount
++;
438 target
->authname
=target
->auth
->name
;
439 target
->nextbyauthname
= target
->auth
->nicks
;
440 target
->auth
->nicks
= target
;
442 target
->auth
->flags
=accountflags
;
445 triggerhook(HOOK_NICK_ACCOUNT
, (void *)target
);
450 int handlestatsmsg(void *source
, int cargc
, char **cargv
) {
452 char *sender
=(char *)source
;
458 Error("nick",ERR_WARNING
,"STATS request without enough parameters!");
462 if (strlen(sender
)==5) {
464 np
=getnickbynumericstr(sender
);
466 Error("nick",ERR_WARNING
,"STATS request from unknown client %s",sender
);
469 replytarget
=np
->nick
;
471 Error("nick",ERR_WARNING
,"STATS request from odd source %s",sender
);
475 /* Reply to stats for ANY server.. including any we are juping */
476 sourceserver
=numerictolong(cargv
[1],2);
477 if (serverlist
[sourceserver
].maxusernum
==0) {
478 Error("nick",ERR_WARNING
,"Stats request for bad server %s",cargv
[1]);
481 fromstring
=serverlist
[sourceserver
].name
->content
;
483 switch(cargv
[0][0]) {
485 irc_send(":%s 242 %s :Server Up %s",fromstring
,replytarget
,
486 longtoduration(time(NULL
)-starttime
, 0));
487 irc_send(":%s 250 %s :Highest connection count: 10 (9 clients)",fromstring
,replytarget
);
491 irc_send(":%s 217 %s P none 0 :0x2000",fromstring
,replytarget
);
495 stats_m(fromstring
, replytarget
);
499 irc_send(":%s 219 %s %c :End of /STATS report",fromstring
,replytarget
,cargv
[0][0]);
504 int handleprivmsg(void *source
, int cargc
, char **cargv
) {
512 if (cargv
[0][0]!='$')
515 sender
=getnickbynumericstr((char *)source
);
517 if (!match2strings(cargv
[0] + 1,myserver
->content
))
526 triggerhook(HOOK_NICK_MASKPRIVMSG
, (void *)args
);
531 int handleawaymsg(void *source
, int cargc
, char **cargv
) {
534 /* Check source is a valid user */
535 if (!(sender
=getnickbynumericstr(source
))) {
539 /* Done with the old away message either way */
540 freesstring(sender
->away
);
543 /* If we have an arg and it isn't an empty string, this sets a new message */
544 if (cargc
> 0 && *(cargv
[0])) {
545 sender
->away
=getsstring(cargv
[0], AWAYLEN
);