3 #include "../nick/nick.h"
4 #include "../lib/base64.h"
5 #include "../lib/sstring.h"
6 #include "../irc/irc.h"
7 #include "../irc/irc_config.h"
8 #include "../core/hooks.h"
9 #include "../core/error.h"
10 #include "../lib/version.h"
11 #include "localuser.h"
21 UserMessageHandler umhandlers
[MAXLOCALUSER
+1];
23 typedef struct pendingkill
{
24 nick
*source
, *target
;
26 struct pendingkill
*next
;
29 pendingkill
*pendingkilllist
;
31 void checklocalkill(int hooknum
, void *nick
);
32 void clearpendingkills(int hooknum
, void *arg
);
33 void checkpendingkills(int hooknum
, void *arg
);
34 void _killuser(nick
*source
, nick
*target
, char *reason
);
39 for (i
=0;i
<=MAXLOCALUSER
;i
++) {
44 registerhook(HOOK_IRC_SENDBURSTNICKS
,&sendnickburst
);
45 registerhook(HOOK_NICK_KILL
,&checklocalkill
);
46 registerhook(HOOK_NICK_LOSTNICK
,&checkpendingkills
); /* CHECK ME -> should this hook KILL or LOSTNICK or BOTH */
47 registerhook(HOOK_CORE_ENDOFHOOKSQUEUE
,&clearpendingkills
);
48 registerserverhandler("P",&handleprivatemsgcmd
,2);
49 registerserverhandler("O",&handleprivatenoticecmd
, 2);
55 for (pk
=pendingkilllist
;pk
;pk
=pendingkilllist
) {
56 pendingkilllist
= pk
->next
;
57 freesstring(pk
->reason
);
61 deregisterhook(HOOK_IRC_SENDBURSTNICKS
,&sendnickburst
);
62 deregisterhook(HOOK_NICK_KILL
,&checklocalkill
);
63 deregisterhook(HOOK_NICK_LOSTNICK
,&checkpendingkills
); /* CHECK ME -> should this hook KILL or LOSTNICK or BOTH */
64 deregisterhook(HOOK_CORE_ENDOFHOOKSQUEUE
,&clearpendingkills
);
66 deregisterserverhandler("P",&handleprivatemsgcmd
);
67 deregisterserverhandler("O",&handleprivatenoticecmd
);
71 * registerlocaluserflags:
72 * This function creates a local user, and broadcasts it's existence to the net (if connected).
75 nick
*registerlocaluserflags(char *nickname
, char *ident
, char *host
, char *realname
, char *authname
, unsigned long authid
, flag_t accountflags
, flag_t umodes
, UserMessageHandler handler
) {
78 struct irc_in_addr ipaddress
;
81 currentlocalunum
=(currentlocalunum
+1)%262142
;
82 while (servernicks
[numerictolong(mynumeric
->content
,2)][currentlocalunum
&MAXLOCALUSER
]!=NULL
) {
83 /* Numeric 262143 on our server is used for "nouser" by the channels module, so cannot be allocated */
84 currentlocalunum
=(currentlocalunum
+1)%262142
;
85 if (++i
>MAXLOCALUSER
) {
90 /* This code is very similar to stuff in nick.c... */
92 newuser
->numeric
=(numerictolong(mynumeric
->content
,2)<<18)|(currentlocalunum
);
93 strncpy(newuser
->nick
,nickname
,NICKLEN
);
94 newuser
->nick
[NICKLEN
]='\0';
95 strncpy(newuser
->ident
,ident
,USERLEN
);
96 newuser
->ident
[USERLEN
]='\0';
97 newuser
->host
=findorcreatehost(host
);
98 newuser
->realname
=findorcreaterealname(realname
);
99 newuser
->nextbyhost
=newuser
->host
->nicks
;
100 newuser
->host
->nicks
=newuser
;
101 newuser
->nextbyrealname
=newuser
->realname
->nicks
;
102 newuser
->realname
->nicks
=newuser
;
103 newuser
->umodes
=umodes
;
105 memset(&ipaddress
, 0, sizeof(ipaddress
));
106 ((unsigned short *)(ipaddress
.in6_16
))[5] = 65535;
107 ((unsigned short *)(ipaddress
.in6_16
))[6] = 127;
108 ((unsigned char *)(ipaddress
.in6_16
))[14] = 1;
109 ((unsigned char *)(ipaddress
.in6_16
))[15] = (currentlocalunum%253
)+1;
111 memcpy(&newuser
->ipaddress
, &ipaddress
, sizeof(ipaddress
));
113 newuser
->ipnode
= refnode(iptree
, &ipaddress
, PATRICIA_MAXBITS
);
114 node_increment_usercount(newuser
->ipnode
);
116 newuser
->timestamp
=getnettime();
117 newuser
->shident
=NULL
;
118 newuser
->sethost
=NULL
;
121 memset(newuser
->exts
, 0, MAXNICKEXTS
* sizeof(void *));
123 if (IsOper(newuser
)) {
124 newuser
->opername
= getsstring("-", ACCOUNTLEN
);
126 newuser
->opername
= NULL
;
129 newuser
->accountts
=0;
131 newuser
->authname
=NULLAUTHNAME
;
132 if (IsAccount(newuser
)) {
133 newuser
->accountts
=newuser
->timestamp
;
135 newuser
->auth
=findorcreateauthname(authid
, authname
);
136 newuser
->authname
=newuser
->auth
->name
;
137 newuser
->auth
->usercount
++;
138 newuser
->nextbyauthname
=newuser
->auth
->nicks
;
139 newuser
->auth
->nicks
=newuser
;
140 newuser
->auth
->flags
=accountflags
;
143 this is done for three reasons:
144 1: so I don't have to change 500 pieces of code
145 2: so services can be authed with special reserved ids
146 3: saves space over the old authname per user method
148 newuser
->authname
=malloc(strlen(authname
) + 1);
149 strcpy(newuser
->authname
,authname
);
153 newuser
->cloak_count
= 0;
154 newuser
->cloak_extra
= NULL
;
155 newuser
->message
= NULL
;
158 /* Check for nick collision */
159 if ((np
=getnickbynick(nickname
))!=NULL
) {
160 /* Make sure we will win the collision */
161 newuser
->timestamp
=(np
->timestamp
-1);
162 killuser(NULL
, np
, "Nick collision");
164 sendnickmsg(newuser
);
168 umhandlers
[(currentlocalunum
&MAXLOCALUSER
)]=handler
;
171 *(gethandlebynumeric(newuser
->numeric
))=newuser
;
172 addnicktohash(newuser
);
173 triggerhook(HOOK_NICK_NEWNICK
,newuser
);
180 * This function changes the name of a given local user
183 int renamelocaluser(nick
*np
, char *newnick
) {
186 time_t timestamp
=getnettime();
188 char oldnick
[NICKLEN
+1];
193 if (strlen(newnick
) > NICKLEN
)
196 if (homeserver(np
->numeric
)!=mylongnum
)
199 strncpy(oldnick
,np
->nick
,NICKLEN
);
200 oldnick
[NICKLEN
]='\0';
202 harg
[1]=(void *)oldnick
;
204 if ((np2
=getnickbynick(newnick
))) {
206 /* Case only name change */
207 strncpy(np
->nick
,newnick
,NICKLEN
);
208 np
->nick
[NICKLEN
]='\0';
209 irc_send("%s N %s %jd",iptobase64(ipbuf
, &(np
->ipaddress
), sizeof(ipbuf
), 1),np
->nick
,(intmax_t)np
->timestamp
);
210 triggerhook(HOOK_NICK_RENAME
,harg
);
213 /* Kill other user and drop through */
214 timestamp
=np2
->timestamp
-1;
215 killuser(NULL
, np2
, "Nick collision");
219 np
->timestamp
=timestamp
;
220 removenickfromhash(np
);
221 strncpy(np
->nick
,newnick
,NICKLEN
);
222 np
->nick
[NICKLEN
]='\0';
224 irc_send("%s N %s %jd",longtonumeric(np
->numeric
,5),np
->nick
,(intmax_t)np
->timestamp
);
225 triggerhook(HOOK_NICK_RENAME
,harg
);
231 * deregisterlocaluser:
232 * This function removes the given local user from the network
235 int deregisterlocaluser(nick
*np
, char *reason
) {
240 if (np
==NULL
|| (homeserver(np
->numeric
)!=mylongnum
)) {
241 /* Non-existent user, or user not on this server */
245 if (reason
==NULL
|| *reason
=='\0') {
246 sprintf(reasonstr
,"Quit");
248 snprintf(reasonstr
,510,"Quit: %s",reason
);
254 triggerhook(HOOK_NICK_QUIT
, harg
);
257 umhandlers
[np
->numeric
&MAXLOCALUSER
]=NULL
;
260 irc_send("%s Q :%s",longtonumeric(numeric
,5),reasonstr
);
267 * hooklocaluserhandler:
268 * This function adds a new handler to the hook chain for a local user
269 * THIS RELIES ON MODULES BEING UNLOADED IN THE CORRECT ORDER.
271 UserMessageHandler
hooklocaluserhandler(nick
*np
, UserMessageHandler newhandler
) {
272 UserMessageHandler oldhandler
= NULL
;
273 if (np
==NULL
|| (homeserver(np
->numeric
)!=mylongnum
)) {
274 /* Non-existent user, or user not on this server */
277 oldhandler
= umhandlers
[np
->numeric
&MAXLOCALUSER
];
278 umhandlers
[np
->numeric
&MAXLOCALUSER
]=newhandler
;
284 * Sends details of a given local nick to the network.
287 void sendnickmsg(nick
*np
) {
289 char ipbuf
[25], operbuf
[ACCOUNTLEN
+ 5];
290 char accountbuf
[100];
292 strncpy(numericbuf
,longtonumeric(np
->numeric
,5),5);
295 if(IsOper(np
) && (serverlist
[myhub
].flags
& SMODE_OPERNAME
)) {
296 snprintf(operbuf
,sizeof(operbuf
)," %s",np
->opername
?np
->opername
->content
:"-");
304 if(np
->auth
->flags
) {
305 snprintf(accountbuf
,sizeof(accountbuf
)," %s:%ld:%lu:%"PRIu64
,np
->authname
,np
->accountts
,np
->auth
->userid
,np
->auth
->flags
);
307 snprintf(accountbuf
,sizeof(accountbuf
)," %s:%ld:%lu",np
->authname
,np
->accountts
,np
->auth
->userid
);
309 } else if(np
->authname
) {
310 snprintf(accountbuf
,sizeof(accountbuf
)," %s:%ld:0",np
->authname
,np
->accountts
);
314 irc_send("%s N %s 1 %ld %s %s %s%s%s %s %s :%s",
315 mynumeric
->content
,np
->nick
,np
->timestamp
,np
->ident
,np
->host
->name
->content
,
316 printflags(np
->umodes
,umodeflags
),operbuf
,accountbuf
,iptobase64(ipbuf
,&(np
->ipaddress
),
317 sizeof(ipbuf
),1),numericbuf
,np
->realname
->name
->content
);
320 void sendnickburst(int hooknum
, void *arg
) {
321 /* Send nick messages for all local users */
325 nh
=servernicks
[numerictolong(mynumeric
->content
,2)];
326 for (i
=0;i
<=MAXLOCALUSER
;i
++) {
333 /* Check for a kill of a local user */
334 void checklocalkill(int hooknum
, void *arg
) {
336 nick
*target
=args
[0];
337 char *reason
=args
[1];
344 numeric
=((nick
*)target
)->numeric
;
346 if (homeserver(numeric
)==mylongnum
) {
347 if (umhandlers
[(numeric
)&MAXLOCALUSER
]!=NULL
) {
348 (umhandlers
[(numeric
)&MAXLOCALUSER
])((nick
*)target
,LU_KILLED
,myargs
);
353 int handleprivatemsgcmd(void *source
, int cargc
, char **cargv
) {
354 return handlemessageornotice(source
, cargc
, cargv
, 0);
357 int handleprivatenoticecmd(void *source
, int cargc
, char **cargv
) {
358 return handlemessageornotice(source
, cargc
, cargv
, 1);
361 /* Handle privmsg or notice command addressed to user */
362 int handlemessageornotice(void *source
, int cargc
, char **cargv
, int isnotice
) {
366 char targetnick
[NICKLEN
+1];
371 /* Should have target and message */
376 if ((sender
=getnickbynumericstr((char *)source
))==NULL
) {
377 Error("localuser",ERR_WARNING
,"PRIVMSG from non existant user %s",(char *)source
);
381 freesstring(sender
->message
);
382 sender
->message
= getsstring(cargv
[1], 512);
386 nargs
[2] = (void *)(uintptr_t)isnotice
;
387 triggerhook(HOOK_NICK_MESSAGE
, nargs
);
389 if (cargv
[0][0]=='#' || cargv
[0][0]=='+') {
390 /* Channel message/notice */
394 /* Check for a "secure" message (foo@bar) */
396 for (i
=0,ch
=cargv
[0];(i
<=NICKLEN
) && (*ch
);i
++,ch
++) {
406 if (!foundat
) { /* Didn't find an @ sign, assume it's a numeric */
407 if ((target
=getnickbynumericstr(cargv
[0]))==NULL
) {
408 Error("localuser",ERR_DEBUG
,"Couldn't find target for %s",cargv
[0]);
411 } else { /* Did find @, do a lookup by nick */
412 if ((target
=getnickbynick(targetnick
))==NULL
) {
413 Error("localuser",ERR_DEBUG
,"Couldn't find target for %s",cargv
[0]);
414 irc_send(":%s 401 %s %s :No such nick",myserver
->content
,sender
->nick
,cargv
[0]);
419 if (homeserver(target
->numeric
)!=mylongnum
) {
420 Error("localuser",ERR_WARNING
,"Got message/notice for someone not on my server");
421 irc_send(":%s 401 %s %s :No such nick",myserver
->content
,sender
->nick
,cargv
[0]);
425 if (foundat
&& !IsService(target
)) {
426 Error("localuser",ERR_DEBUG
,"Received secure message for %s, but user is not a service",cargv
[0]);
427 irc_send(":%s 401 %s %s :No such nick",myserver
->content
,sender
->nick
,cargv
[0]);
431 if (umhandlers
[(target
->numeric
)&MAXLOCALUSER
]==NULL
) {
432 /* No handler anyhow.. */
436 /* Dispatch the message. */
437 nargs
[0]=(void *)sender
;
438 nargs
[1]=(void *)cargv
[1];
439 (umhandlers
[(target
->numeric
)&MAXLOCALUSER
])(target
,isnotice
?LU_PRIVNOTICE
:(foundat
?LU_SECUREMSG
:LU_PRIVMSG
),nargs
);
444 /* Send message to user */
445 void sendmessagetouser(nick
*source
, nick
*target
, char *format
, ... ) {
450 longtonumeric2(source
->numeric
,5,senderstr
);
453 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
454 /* So max sendable message is 495 bytes. Of course, a client won't be able
455 * to receive this.. */
457 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
460 if (homeserver(target
->numeric
)!=mylongnum
)
461 irc_send("%s P %s :%s",senderstr
,longtonumeric(target
->numeric
,5),buf
);
464 /* Send messageto server, we don't check, but noones going to want to put a server pointer in anyway... */
465 void sendsecuremessagetouser(nick
*source
, nick
*target
, char *servername
, char *format
, ... ) {
470 longtonumeric2(source
->numeric
,5,senderstr
);
473 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
474 /* So max sendable message is 495 bytes. Of course, a client won't be able
475 * to receive this.. */
477 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
480 if (homeserver(target
->numeric
)!=mylongnum
)
481 irc_send("%s P %s@%s :%s",senderstr
,target
->nick
,servername
,buf
);
484 /* Send notice to user */
485 void sendnoticetouser(nick
*source
, nick
*target
, char *format
, ... ) {
490 longtonumeric2(source
->numeric
,5,senderstr
);
493 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
494 /* So max sendable message is 495 bytes. Of course, a client won't be able
495 * to receive this.. */
497 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
500 if (homeserver(target
->numeric
)!=mylongnum
)
501 irc_send("%s O %s :%s",senderstr
,longtonumeric(target
->numeric
,5),buf
);
505 void killuser(nick
*source
, nick
*target
, char *format
, ... ) {
510 va_start(va
, format
);
511 vsnprintf(buf
, BUFSIZE
-17, format
, va
);
514 if (hookqueuelength
) {
515 for (pk
= pendingkilllist
; pk
; pk
= pk
->next
)
516 if (pk
->target
== target
)
519 Error("localuser", ERR_DEBUG
, "Adding pending kill for %s", target
->nick
);
520 pk
= (pendingkill
*)malloc(sizeof(pendingkill
));
523 pk
->reason
= getsstring(buf
, BUFSIZE
);
524 pk
->next
= pendingkilllist
;
525 pendingkilllist
= pk
;
527 _killuser(source
, target
, buf
);
531 void sethostuser(nick
*target
, char *ident
, char *host
) {
532 irc_send("%s SH %s %s %s", mynumeric
->content
, longtonumeric(target
->numeric
, 5), ident
, host
);
535 void _killuser(nick
*source
, nick
*target
, char *reason
) {
537 char sourcestring
[HOSTLEN
+NICKLEN
+3];
542 /* If we have a null nick, use the server.. */
543 strcpy(senderstr
, mynumeric
->content
);
544 strcpy(sourcestring
, myserver
->content
);
546 strcpy(senderstr
, longtonumeric(source
->numeric
,5));
547 sprintf(sourcestring
,"%s!%s",source
->host
->name
->content
, source
->nick
);
550 snprintf(reasonstr
,512,"%s (%s)",sourcestring
,reason
);
553 irc_send("%s D %s :%s",senderstr
,longtonumeric(target
->numeric
,5),reasonstr
);
557 triggerhook(HOOK_NICK_KILL
, args
);
562 void clearpendingkills(int hooknum
, void *arg
) {
565 pk
= pendingkilllist
;
567 pendingkilllist
= pk
->next
;
570 Error("localuser", ERR_DEBUG
, "Processing pending kill for %s", pk
->target
->nick
);
571 _killuser(pk
->source
, pk
->target
, pk
->reason
->content
);
574 freesstring(pk
->reason
);
576 pk
= pendingkilllist
;
580 void checkpendingkills(int hooknum
, void *arg
) {
581 nick
*np
= (nick
*)arg
;
584 for (pk
=pendingkilllist
; pk
; pk
= pk
->next
) {
585 if (pk
->source
== np
) {
586 Error("localuser", ERR_INFO
, "Pending kill source %s got deleted, NULL'ing source for pending kill", np
->nick
);
589 if (pk
->target
== np
) {
590 Error("localuser", ERR_INFO
, "Pending kill target %s got deleted, NULL'ing target for pending kill", np
->nick
);
596 void sendaccountmessage(nick
*np
) {
597 if (connected
&& IsAccount(np
)) {
599 if (np
->auth
->flags
) {
600 irc_send("%s AC %s %s %ld %lu %"PRIu64
,mynumeric
->content
, longtonumeric(np
->numeric
,5), np
->authname
, np
->accountts
, np
->auth
->userid
, np
->auth
->flags
);
602 irc_send("%s AC %s %s %ld %lu",mynumeric
->content
, longtonumeric(np
->numeric
,5), np
->authname
, np
->accountts
, np
->auth
->userid
);
605 irc_send("%s AC %s %s %ld 0",mynumeric
->content
, longtonumeric(np
->numeric
,5), np
->authname
, np
->accountts
);
610 /* Auth user, don't use to set flags after authing */
611 void localusersetaccount(nick
*np
, char *accname
, unsigned long accid
, u_int64_t accountflags
, time_t authTS
) {
613 Error("localuser",ERR_WARNING
,"Tried to set account on user %s already authed", np
->nick
);
618 np
->accountts
=authTS
?authTS
:getnettime();
621 np
->auth
=findorcreateauthname(accid
, accname
);
622 np
->auth
->usercount
++;
623 np
->authname
=np
->auth
->name
;
624 np
->nextbyauthname
=np
->auth
->nicks
;
626 np
->auth
->flags
=accountflags
;
629 np
->authname
=malloc(strlen(accname
) + 1);
630 strcpy(np
->authname
,accname
);
633 sendaccountmessage(np
);
635 triggerhook(HOOK_NICK_ACCOUNT
, np
);
638 void localusersetumodes(nick
*np
, flag_t newmodes
) {
640 irc_send("%s M %s %s", longtonumeric(np
->numeric
,5), np
->nick
, printflagdiff(np
->umodes
, newmodes
, umodeflags
));
643 np
->umodes
= newmodes
;
646 void localusersetaccountflags(authname
*anp
, u_int64_t accountflags
) {
649 u_int64_t oldflags
= anp
->flags
;
653 anp
->flags
= accountflags
;
655 for(np
=anp
->nicks
;np
;np
=np
->nextbyauthname
)
656 sendaccountmessage(np
);
658 triggerhook(HOOK_AUTH_FLAGSUPDATED
, arg
);
661 void localuseraddcloaktarget(nick
*np
, nick
*target
) {
662 char snumeric
[10], tnumeric
[10];
664 strcpy(snumeric
, longtonumeric(np
->numeric
,5));
665 strcpy(tnumeric
, longtonumeric(target
->numeric
,5));
667 irc_send("%s CA %s", snumeric
, tnumeric
);
669 addcloaktarget(np
, target
);
672 void localuserclearcloaktargets(nick
*np
) {
673 irc_send("%s CU", longtonumeric(np
->numeric
,5));
674 clearcloaktargets(np
);