]>
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
;
30 struct irc_in_addr ipaddress
;
34 if (cargc
==2) { /* rename */
35 /* Nyklon 1017697578 */
36 timestamp
=strtol(cargv
[1],NULL
,10);
37 np
=getnickbynumericstr(sender
);
39 Error("nick",ERR_ERROR
,"Rename from non-existent sender %s",sender
);
42 np2
=getnickbynick(cargv
[0]);
44 /* The new and old nickname have the same hash, this means a rename to the same name in
45 * different case, e.g. Flash -> flash. In this case the timestamp for the change should
46 * match the existing timestamp, and we can bypass all the collision checking and hash fettling. */
47 if (np
->timestamp
!=timestamp
) {
48 Error("nick",ERR_WARNING
,"Rename to same nickname with different timestamp (%s(%d) -> %s(%d))",
49 np
->nick
,np
->timestamp
,cargv
[0],timestamp
);
50 np
->timestamp
=timestamp
;
52 strncpy(np
->nick
,cargv
[0],NICKLEN
);
53 np
->nick
[NICKLEN
]='\0';
54 triggerhook(HOOK_NICK_RENAME
,np
);
59 if (ircd_strcmp(np
->ident
,np2
->ident
) || (np
->host
!=np2
->host
)) {
60 /* Different user@host */
61 if (np2
->timestamp
< timestamp
) {
62 /* The nick attempting to rename got killed. Guess we don't need to do the rename now :) */
66 /* The other nick got killed */
70 if (np2
->timestamp
< timestamp
) {
71 /* Same user@host: reverse logic. Whose idea was all this anyway? */
79 /* OK, we've survived the collision hazard. Change timestamp and rename */
80 np
->timestamp
=timestamp
;
81 removenickfromhash(np
);
82 strncpy(np
->nick
,cargv
[0],NICKLEN
);
83 np
->nick
[NICKLEN
]='\0';
85 triggerhook(HOOK_NICK_RENAME
,np
);
86 } else if (cargc
>=8) { /* new nick */
87 /* Jupiler 2 1016645147 ~Jupiler www.iglobal.be +ir moo [FUTURE CRAP HERE] DV74O] BNBd7 :Jupiler */
88 timestamp
=strtol(cargv
[2],NULL
,10);
89 np
=getnickbynick(cargv
[0]);
92 if (ircd_strcmp(np
->ident
,cargv
[3]) || ircd_strcmp(np
->host
->name
->content
,cargv
[4])) {
93 /* Different user@host */
94 if (timestamp
>np
->timestamp
) {
95 /* New nick is newer. Ignore this nick message */
98 /* New nick is older. Kill the imposter, and drop through */
102 if (timestamp
>np
->timestamp
) {
103 /* Same user@host, newer timestamp: we're killing off a ghost */
106 /* This nick is the ghost, so ignore it */
112 nh
=gethandlebynumeric(numerictolong(cargv
[cargc
-2],5));
114 /* This isn't a valid numeric */
115 Error("nick",ERR_WARNING
,"Received NICK with invalid numeric %s from %s.",cargv
[cargc
-2],sender
);
119 base64toip(cargv
[cargc
-3], &ipaddress
);
120 if (!irc_in_addr_valid(&ipaddress
)) {
121 Error("nick",ERR_ERROR
,"Received NICK with invalid ipaddress for %s from %s.",cargv
[0],sender
);
125 /* At this stage the nick is cleared to proceed */
127 strncpy(np
->nick
,cargv
[0],NICKLEN
);
128 np
->nick
[NICKLEN
]='\0';
129 np
->numeric
=numerictolong(cargv
[cargc
-2],5);
130 strncpy(np
->ident
,cargv
[3],USERLEN
);
131 np
->ident
[USERLEN
]='\0';
132 np
->host
=findorcreatehost(cargv
[4]);
133 np
->realname
=findorcreaterealname(cargv
[cargc
-1]);
134 np
->nextbyhost
=np
->host
->nicks
;
136 np
->nextbyrealname
=np
->realname
->nicks
;
137 np
->realname
->nicks
=np
;
138 np
->timestamp
=timestamp
;
140 base64toip(cargv
[cargc
-3], &ipaddress
);
141 /* todo: use a single node for /64 prefixes */
142 np
->ipnode
= refnode(iptree
, &ipaddress
, irc_in_addr_is_ipv4(&ipaddress
) ? PATRICIA_MAXBITS
: 64);
148 memset(np
->exts
, 0, MAXNICKEXTS
* sizeof(void *));
149 np
->authname
[0]='\0';
152 setflags(&(np
->umodes
),UMODE_ALL
,cargv
[5],umodeflags
,REJECT_NONE
);
154 if ((accountts
=strchr(cargv
[6],':'))) {
156 np
->accountts
=strtoul(accountts
,&accountid
,10);
158 userid
=strtoul(accountid
+ 1,NULL
,10);
162 np
->auth
=findorcreateauthname(userid
);
163 np
->auth
->usercount
++;
164 np
->nextbyauthname
=np
->auth
->nicks
;
174 strncpy(np
->authname
,cargv
[6],ACCOUNTLEN
);
175 np
->authname
[ACCOUNTLEN
]='\0';
177 if (IsSetHost(np
) && (fakehost
=strchr(cargv
[cargc
-4],'@'))) {
180 np
->shident
=getsstring(cargv
[cargc
-4],USERLEN
);
181 np
->sethost
=getsstring(fakehost
,HOSTLEN
);
185 /* Place this nick in the server nick table. Note that nh is valid from the numeric check above */
187 /* There was a nick there already -- we have a masked numeric collision
188 * This shouldn't happen, but if it does the newer nick takes precedence
189 * (the two nicks are from the same server, and if the server has reissued
190 * the masked numeric it must believe the old user no longer exists).
192 Error("nick",ERR_ERROR
,"Masked numeric collision for %s [%s vs %s]",cargv
[0],cargv
[cargc
-2],longtonumeric((*nh
)->numeric
,5));
197 /* And the nick hash table */
200 /* Trigger the hook */
201 triggerhook(HOOK_NICK_NEWNICK
,np
);
203 Error("nick",ERR_WARNING
,"Nick message with weird number of parameters (%d)",cargc
);
209 int handlequitmsg(void *source
, int cargc
, char **cargv
) {
214 harg
[1]=(void *)cargv
[1];
219 np
=getnickbynumericstr((char *)source
);
222 triggerhook(HOOK_NICK_QUIT
, harg
);
225 Error("nick",ERR_WARNING
,"Quit from non-existant numeric %s",(char *)source
);
230 int handlekillmsg(void *source
, int cargc
, char **cargv
) {
233 #warning Fix me to use source
236 Error("nick",ERR_WARNING
,"Kill message with too few parameters");
241 harg
[1]=(void *)cargv
[1];
246 np
=getnickbynumericstr(cargv
[0]);
249 triggerhook(HOOK_NICK_KILL
, harg
);
252 Error("nick",ERR_WARNING
,"Kill for non-existant numeric %s",cargv
[0]);
257 int handleusermodemsg(void *source
, int cargc
, char **cargv
) {
263 Error("nick",ERR_WARNING
,"Mode message with too few parameters");
264 return CMD_LAST
; /* With <2 params the channels module won't want it either */
267 if (cargv
[0][0]=='#') {
268 /* Channel mode change, we don't care.
269 * We don't bother checking for other channel types here, since & channel mode
270 * changes aren't broadcast and + channels don't have mode changes :) */
274 np
=getnickbynumericstr((char *)source
);
276 if (ircd_strcmp(cargv
[0],np
->nick
)) {
277 Error("nick",ERR_WARNING
,"Attempted mode change on user %s by %s",cargv
[0],np
->nick
);
281 if (strchr(cargv
[1],'o')) {
285 triggerhook(HOOK_NICK_MODEOPER
,harg
);
287 setflags(&(np
->umodes
),UMODE_ALL
,cargv
[1],umodeflags
,REJECT_NONE
);
288 if (strchr(cargv
[1],'h')) { /* Have to allow +h twice.. */
289 /* +-h: just the freesstring() calls for the -h case */
290 freesstring(np
->shident
); /* freesstring(NULL) is OK */
291 freesstring(np
->sethost
);
294 if (IsSetHost(np
) && cargc
>2) { /* +h and mask received */
295 if ((fakehost
=strchr(cargv
[2],'@'))) {
296 /* user@host change */
298 np
->shident
=getsstring(cargv
[2],USERLEN
);
299 np
->sethost
=getsstring(fakehost
,HOSTLEN
);
301 np
->sethost
=getsstring(cargv
[2],HOSTLEN
);
304 triggerhook(HOOK_NICK_SETHOST
, (void *)np
);
307 Error("nick",ERR_WARNING
,"Usermode change by unknown user %s",(char *)source
);
312 int handlewhoismsg(void *source
, int cargc
, char **cargv
) {
313 nick
*sender
,*target
;
319 if (strncmp(cargv
[0],mynumeric
->content
,2)) {
323 /* Find the sender... */
324 if ((sender
=getnickbynumericstr((char *)source
))==NULL
) {
325 Error("localuser",ERR_WARNING
,"WHOIS message from non existent numeric %s",(char *)source
);
329 /* :hub.splidge.netsplit.net 311 moo splidge splidge ground.stbarnab.as * :splidge
330 :hub.splidge.netsplit.net 312 moo splidge splidge.netsplit.net :splidge's netsplit leaf
331 :hub.splidge.netsplit.net 313 moo splidge :is an IRC Operator
332 :hub.splidge.netsplit.net 318 moo splidge :End of /WHOIS list.
335 /* And the target... */
336 if ((target
=getnickbynick(cargv
[1]))==NULL
) {
337 irc_send(":%s 401 %s %s :No such nick",myserver
->content
,sender
->nick
,cargv
[1]);
339 irc_send(":%s 311 %s %s %s %s * :%s",myserver
->content
,sender
->nick
,target
->nick
,target
->ident
,
340 target
->host
->name
->content
, target
->realname
->name
->content
);
341 nicks
[0]=sender
; nicks
[1]=target
;
342 triggerhook(HOOK_NICK_WHOISCHANNELS
,nicks
);
343 if (IsOper(sender
) || !HIS_SERVER
) {
344 irc_send(":%s 312 %s %s %s :%s",myserver
->content
,sender
->nick
,target
->nick
,
345 serverlist
[homeserver(target
->numeric
)].name
->content
,
346 serverlist
[homeserver(target
->numeric
)].description
->content
);
348 irc_send(":%s 312 %s %s " HIS_SERVERNAME
" :" HIS_SERVERDESC
,myserver
->content
,sender
->nick
,target
->nick
);
350 if (IsOper(target
)) {
351 irc_send(":%s 313 %s %s :is an IRC Operator",myserver
->content
,sender
->nick
,target
->nick
);
353 if (IsAccount(target
)) {
354 irc_send(":%s 330 %s %s %s :is authed as",myserver
->content
,sender
->nick
,target
->nick
,target
->authname
);
356 if (homeserver(target
->numeric
)==mylongnum
&& !IsService(target
) && !IsHideIdle(target
)) {
357 irc_send(":%s 317 %s %s %ld %ld :seconds idle, signon time",myserver
->content
,sender
->nick
,target
->nick
,
358 (getnettime() - target
->timestamp
) % (((target
->numeric
+ target
->timestamp
) % 983) + 7),target
->timestamp
);
362 irc_send(":%s 318 %s %s :End of /WHOIS list.",myserver
->content
,sender
->nick
,cargv
[1]);
366 int handleaccountmsg(void *source
, int cargc
, char **cargv
) {
368 unsigned long userid
;
374 if ((target
=getnickbynumericstr(cargv
[0]))==NULL
) {
378 if (IsAccount(target
)) {
383 strncpy(target
->authname
,cargv
[1],ACCOUNTLEN
);
384 target
->authname
[ACCOUNTLEN
]='\0';
387 target
->accountts
=strtoul(cargv
[2],NULL
,10);
389 userid
=strtoul(cargv
[3],NULL
,10);
393 target
->auth
=findorcreateauthname(userid
);
394 target
->auth
->usercount
++;
395 target
->nextbyauthname
= target
->auth
->nicks
;
396 target
->auth
->nicks
= target
;
406 triggerhook(HOOK_NICK_ACCOUNT
, (void *)target
);
411 int handlestatsmsg(void *source
, int cargc
, char **cargv
) {
413 char *sender
=(char *)source
;
419 Error("nick",ERR_WARNING
,"STATS request without enough parameters!");
423 if (strlen(sender
)==5) {
425 np
=getnickbynumericstr(sender
);
427 Error("nick",ERR_WARNING
,"STATS request from unknown client %s",sender
);
430 replytarget
=np
->nick
;
432 Error("nick",ERR_WARNING
,"STATS request from odd source %s",sender
);
436 /* Reply to stats for ANY server.. including any we are juping */
437 sourceserver
=numerictolong(cargv
[1],2);
438 if (serverlist
[sourceserver
].maxusernum
==0) {
439 Error("nick",ERR_WARNING
,"Stats request for bad server %s",cargv
[1]);
442 fromstring
=serverlist
[sourceserver
].name
->content
;
444 switch(cargv
[0][0]) {
446 irc_send(":%s 242 %s :Server Up %s",fromstring
,replytarget
,
447 longtoduration(time(NULL
)-starttime
, 0));
448 irc_send(":%s 250 %s :Highest connection count: 10 (9 clients)",fromstring
,replytarget
);
452 irc_send(":%s 217 %s P none 0 :0x2000",fromstring
,replytarget
);
457 irc_send(":%s 219 %s %c :End of /STATS report",fromstring
,replytarget
,cargv
[0][0]);