]>
jfr.im git - irc/quakenet/newserv.git/blob - nick/nickhandlers.c
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"
19 * Handle new nicks being introduced to the network.
23 int handlenickmsg(void *source
, int cargc
, char **cargv
) {
24 char *sender
=(char *)source
;
31 struct irc_in_addr ipaddress
;
35 if (cargc
==2) { /* rename */
36 /* Nyklon 1017697578 */
37 timestamp
=strtol(cargv
[1],NULL
,10);
38 np
=getnickbynumericstr(sender
);
40 Error("nick",ERR_ERROR
,"Rename from non-existent sender %s",sender
);
43 np2
=getnickbynick(cargv
[0]);
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
;
53 strncpy(np
->nick
,cargv
[0],NICKLEN
);
54 np
->nick
[NICKLEN
]='\0';
55 triggerhook(HOOK_NICK_RENAME
,np
);
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 :) */
67 /* The other nick got killed */
71 if (np2
->timestamp
< timestamp
) {
72 /* Same user@host: reverse logic. Whose idea was all this anyway? */
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';
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]);
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 */
99 /* New nick is older. Kill the imposter, and drop through */
103 if (timestamp
>np
->timestamp
) {
104 /* Same user@host, newer timestamp: we're killing off a ghost */
107 /* This nick is the ghost, so ignore it */
113 nh
=gethandlebynumeric(numerictolong(cargv
[cargc
-2],5));
115 /* This isn't a valid numeric */
116 Error("nick",ERR_WARNING
,"Received NICK with invalid numeric %s from %s.",cargv
[cargc
-2],sender
);
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
);
126 /* At this stage the nick is cleared to proceed */
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
;
137 np
->nextbyrealname
=np
->realname
->nicks
;
138 np
->realname
->nicks
=np
;
139 np
->timestamp
=timestamp
;
141 base64toip(cargv
[cargc
-3], &ipaddress
);
142 np
->ipnode
= refnode(iptree
, &ipaddress
, PATRICIA_MAXBITS
);
143 node_increment_usercount(np
->ipnode
);
150 memset(np
->exts
, 0, MAXNICKEXTS
* sizeof(void *));
155 int sethostarg
= 6, opernamearg
= 6, accountarg
= 6;
157 setflags(&(np
->umodes
),UMODE_ALL
,cargv
[5],umodeflags
,REJECT_NONE
);
159 if(IsOper(np
) && (serverlist
[myhub
].flags
& SMODE_OPERNAME
)) {
163 np
->opername
=getsstring(cargv
[opernamearg
],ACCOUNTLEN
);
169 if ((accountts
=strchr(cargv
[accountarg
],':'))) {
171 np
->accountts
=strtoul(accountts
,&accountid
,10);
173 userid
=strtoul(accountid
+ 1,&accountflags
,10);
175 np
->auth
=findorcreateauthname(userid
, cargv
[accountarg
]);
176 np
->authname
=np
->auth
->name
;
177 np
->auth
->usercount
++;
178 np
->nextbyauthname
=np
->auth
->nicks
;
181 np
->auth
->flags
=strtoul(accountflags
+ 1,NULL
,10);
184 np
->authname
=malloc(strlen(cargv
[accountarg
]) + 1);
185 strcpy(np
->authname
,cargv
[accountarg
]);
189 if (IsSetHost(np
) && (fakehost
=strchr(cargv
[sethostarg
],'@'))) {
192 np
->shident
=getsstring(cargv
[sethostarg
],USERLEN
);
193 np
->sethost
=getsstring(fakehost
,HOSTLEN
);
197 /* Place this nick in the server nick table. Note that nh is valid from the numeric check above */
199 /* There was a nick there already -- we have a masked numeric collision
200 * This shouldn't happen, but if it does the newer nick takes precedence
201 * (the two nicks are from the same server, and if the server has reissued
202 * the masked numeric it must believe the old user no longer exists).
204 Error("nick",ERR_ERROR
,"Masked numeric collision for %s [%s vs %s]",cargv
[0],cargv
[cargc
-2],longtonumeric((*nh
)->numeric
,5));
209 /* And the nick hash table */
212 /* Trigger the hook */
213 triggerhook(HOOK_NICK_NEWNICK
,np
);
215 Error("nick",ERR_WARNING
,"Nick message with weird number of parameters (%d)",cargc
);
221 int handlequitmsg(void *source
, int cargc
, char **cargv
) {
226 harg
[1]=(void *)cargv
[0];
231 np
=getnickbynumericstr((char *)source
);
234 triggerhook(HOOK_NICK_QUIT
, harg
);
237 Error("nick",ERR_WARNING
,"Quit from non-existant numeric %s",(char *)source
);
242 int handlekillmsg(void *source
, int cargc
, char **cargv
) {
245 #warning Fix me to use source
248 Error("nick",ERR_WARNING
,"Kill message with too few parameters");
253 harg
[1]=(void *)cargv
[1];
258 np
=getnickbynumericstr(cargv
[0]);
261 triggerhook(HOOK_NICK_KILL
, harg
);
264 Error("nick",ERR_WARNING
,"Kill for non-existant numeric %s",cargv
[0]);
269 int handleusermodemsg(void *source
, int cargc
, char **cargv
) {
275 Error("nick",ERR_WARNING
,"Mode message with too few parameters");
276 return CMD_LAST
; /* With <2 params the channels module won't want it either */
279 if (cargv
[0][0]=='#') {
280 /* Channel mode change, we don't care.
281 * We don't bother checking for other channel types here, since & channel mode
282 * changes aren't broadcast and + channels don't have mode changes :) */
286 np
=getnickbynumericstr((char *)source
);
288 if (ircd_strcmp(cargv
[0],np
->nick
)) {
289 Error("nick",ERR_WARNING
,"Attempted mode change on user %s by %s",cargv
[0],np
->nick
);
293 setflags(&(np
->umodes
),UMODE_ALL
,cargv
[1],umodeflags
,REJECT_NONE
);
295 if (strchr(cargv
[1],'o')) { /* o always comes on its own when being set */
296 if(serverlist
[myhub
].flags
& SMODE_OPERNAME
) {
297 if((np
->umodes
& UMODE_OPER
)) {
298 np
->opername
= getsstring(cargv
[2], ACCOUNTLEN
);
300 freesstring(np
->opername
);
304 if((np
->umodes
^ oldflags
) & UMODE_OPER
)
305 triggerhook(HOOK_NICK_MODEOPER
,np
);
307 if (strchr(cargv
[1],'h')) { /* Have to allow +h twice.. */
308 /* +-h: just the freesstring() calls for the -h case */
309 freesstring(np
->shident
); /* freesstring(NULL) is OK */
310 freesstring(np
->sethost
);
313 if (IsSetHost(np
) && cargc
>2) { /* +h and mask received */
314 if ((fakehost
=strchr(cargv
[2],'@'))) {
315 /* user@host change */
317 np
->shident
=getsstring(cargv
[2],USERLEN
);
318 np
->sethost
=getsstring(fakehost
,HOSTLEN
);
320 np
->sethost
=getsstring(cargv
[2],HOSTLEN
);
323 triggerhook(HOOK_NICK_SETHOST
, (void *)np
);
326 Error("nick",ERR_WARNING
,"Usermode change by unknown user %s",(char *)source
);
331 int handlewhoismsg(void *source
, int cargc
, char **cargv
) {
332 nick
*sender
,*target
;
338 if (strncmp(cargv
[0],mynumeric
->content
,2)) {
342 /* Find the sender... */
343 if ((sender
=getnickbynumericstr((char *)source
))==NULL
) {
344 Error("localuser",ERR_WARNING
,"WHOIS message from non existent numeric %s",(char *)source
);
348 /* :hub.splidge.netsplit.net 311 moo splidge splidge ground.stbarnab.as * :splidge
349 :hub.splidge.netsplit.net 312 moo splidge splidge.netsplit.net :splidge's netsplit leaf
350 :hub.splidge.netsplit.net 313 moo splidge :is an IRC Operator
351 :hub.splidge.netsplit.net 318 moo splidge :End of /WHOIS list.
354 /* And the target... */
355 if ((target
=getnickbynick(cargv
[1]))==NULL
) {
356 irc_send(":%s 401 %s %s :No such nick",myserver
->content
,sender
->nick
,cargv
[1]);
358 irc_send(":%s 311 %s %s %s %s * :%s",myserver
->content
,sender
->nick
,target
->nick
,target
->ident
,
359 target
->host
->name
->content
, target
->realname
->name
->content
);
360 nicks
[0]=sender
; nicks
[1]=target
;
361 triggerhook(HOOK_NICK_WHOISCHANNELS
,nicks
);
362 if (IsOper(sender
) || !HIS_SERVER
) {
363 irc_send(":%s 312 %s %s %s :%s",myserver
->content
,sender
->nick
,target
->nick
,
364 serverlist
[homeserver(target
->numeric
)].name
->content
,
365 serverlist
[homeserver(target
->numeric
)].description
->content
);
367 irc_send(":%s 312 %s %s " HIS_SERVERNAME
" :" HIS_SERVERDESC
,myserver
->content
,sender
->nick
,target
->nick
);
369 if (IsOper(target
)) {
370 irc_send(":%s 313 %s %s :is an IRC Operator",myserver
->content
,sender
->nick
,target
->nick
);
372 if (IsAccount(target
)) {
373 irc_send(":%s 330 %s %s %s :is authed as",myserver
->content
,sender
->nick
,target
->nick
,target
->authname
);
375 if (homeserver(target
->numeric
)==mylongnum
&& !IsService(target
) && !IsHideIdle(target
)) {
376 irc_send(":%s 317 %s %s %ld %ld :seconds idle, signon time",myserver
->content
,sender
->nick
,target
->nick
,
377 (getnettime() - target
->timestamp
) % (((target
->numeric
+ target
->timestamp
) % 983) + 7),target
->timestamp
);
381 irc_send(":%s 318 %s %s :End of /WHOIS list.",myserver
->content
,sender
->nick
,cargv
[1]);
385 int handleaccountmsg(void *source
, int cargc
, char **cargv
) {
387 unsigned long userid
;
395 if ((target
=getnickbynumericstr(cargv
[0]))==NULL
) {
399 accountts
=strtoul(cargv
[2],NULL
,10);
400 userid
=strtoul(cargv
[3],NULL
,10);
402 accountflags
=strtoul(cargv
[4],NULL
,10);
404 /* allow user flags to change if all fields match */
405 if (IsAccount(target
)) {
408 if (!target
->auth
|| strcmp(target
->auth
->name
,cargv
[1]) || (target
->auth
->userid
!= userid
) || (target
->accountts
!= accountts
)) {
412 arg
[0] = (void *)target
->auth
;
413 arg
[1] = (void *)(long)target
->auth
->flags
;
416 target
->auth
->flags
=accountflags
;
418 triggerhook(HOOK_AUTH_FLAGSUPDATED
, (void *)arg
);
420 /* TODO: trigger flag update hook */
425 target
->accountts
=accountts
;
429 target
->authname
=malloc(strlen(cargv
[1]) + 1);
430 strcpy(target
->authname
,cargv
[1]);
432 target
->auth
=findorcreateauthname(userid
, target
->authname
);
433 target
->auth
->usercount
++;
434 target
->authname
=target
->auth
->name
;
435 target
->nextbyauthname
= target
->auth
->nicks
;
436 target
->auth
->nicks
= target
;
438 target
->auth
->flags
=accountflags
;
441 triggerhook(HOOK_NICK_ACCOUNT
, (void *)target
);
446 int handlestatsmsg(void *source
, int cargc
, char **cargv
) {
448 char *sender
=(char *)source
;
454 Error("nick",ERR_WARNING
,"STATS request without enough parameters!");
458 if (strlen(sender
)==5) {
460 np
=getnickbynumericstr(sender
);
462 Error("nick",ERR_WARNING
,"STATS request from unknown client %s",sender
);
465 replytarget
=np
->nick
;
467 Error("nick",ERR_WARNING
,"STATS request from odd source %s",sender
);
471 /* Reply to stats for ANY server.. including any we are juping */
472 sourceserver
=numerictolong(cargv
[1],2);
473 if (serverlist
[sourceserver
].maxusernum
==0) {
474 Error("nick",ERR_WARNING
,"Stats request for bad server %s",cargv
[1]);
477 fromstring
=serverlist
[sourceserver
].name
->content
;
479 switch(cargv
[0][0]) {
481 irc_send(":%s 242 %s :Server Up %s",fromstring
,replytarget
,
482 longtoduration(time(NULL
)-starttime
, 0));
483 irc_send(":%s 250 %s :Highest connection count: 10 (9 clients)",fromstring
,replytarget
);
487 irc_send(":%s 217 %s P none 0 :0x2000",fromstring
,replytarget
);
492 irc_send(":%s 219 %s %c :End of /STATS report",fromstring
,replytarget
,cargv
[0][0]);
497 int handleprivmsg(void *source
, int cargc
, char **cargv
) {
505 if (cargv
[0][0]!='$')
508 sender
=getnickbynumericstr((char *)source
);
510 if (!match2strings(cargv
[0] + 1,myserver
->content
))
519 triggerhook(HOOK_NICK_MASKPRIVMSG
, (void *)args
);