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"
20 UserMessageHandler umhandlers
[MAXLOCALUSER
+1];
22 typedef struct pendingkill
{
23 nick
*source
, *target
;
25 struct pendingkill
*next
;
28 pendingkill
*pendingkilllist
;
30 void checklocalkill(int hooknum
, void *nick
);
31 void clearpendingkills(int hooknum
, void *arg
);
32 void checkpendingkills(int hooknum
, void *arg
);
33 void _killuser(nick
*source
, nick
*target
, char *reason
);
38 for (i
=0;i
<=MAXLOCALUSER
;i
++) {
43 registerhook(HOOK_IRC_SENDBURSTNICKS
,&sendnickburst
);
44 registerhook(HOOK_NICK_KILL
,&checklocalkill
);
45 registerhook(HOOK_NICK_LOSTNICK
,&checkpendingkills
); /* CHECK ME -> should this hook KILL or LOSTNICK or BOTH */
46 registerhook(HOOK_CORE_ENDOFHOOKSQUEUE
,&clearpendingkills
);
47 registerserverhandler("P",&handleprivatemsgcmd
,2);
48 registerserverhandler("O",&handleprivatenoticecmd
, 2);
54 for (pk
=pendingkilllist
;pk
;pk
=pendingkilllist
) {
55 pendingkilllist
= pk
->next
;
56 freesstring(pk
->reason
);
60 deregisterhook(HOOK_IRC_SENDBURSTNICKS
,&sendnickburst
);
61 deregisterhook(HOOK_NICK_KILL
,&checklocalkill
);
62 deregisterhook(HOOK_NICK_LOSTNICK
,&checkpendingkills
); /* CHECK ME -> should this hook KILL or LOSTNICK or BOTH */
63 deregisterhook(HOOK_CORE_ENDOFHOOKSQUEUE
,&clearpendingkills
);
65 deregisterserverhandler("P",&handleprivatemsgcmd
);
66 deregisterserverhandler("O",&handleprivatenoticecmd
);
70 * registerlocaluserflags:
71 * This function creates a local user, and broadcasts it's existence to the net (if connected).
74 nick
*registerlocaluserflags(char *nickname
, char *ident
, char *host
, char *realname
, char *authname
, unsigned long authid
, flag_t accountflags
, flag_t umodes
, UserMessageHandler handler
) {
77 struct irc_in_addr ipaddress
;
80 currentlocalunum
=(currentlocalunum
+1)%262142
;
81 while (servernicks
[numerictolong(mynumeric
->content
,2)][currentlocalunum
&MAXLOCALUSER
]!=NULL
) {
82 /* Numeric 262143 on our server is used for "nouser" by the channels module, so cannot be allocated */
83 currentlocalunum
=(currentlocalunum
+1)%262142
;
84 if (++i
>MAXLOCALUSER
) {
89 /* This code is very similar to stuff in nick.c... */
91 newuser
->numeric
=(numerictolong(mynumeric
->content
,2)<<18)|(currentlocalunum
);
92 strncpy(newuser
->nick
,nickname
,NICKLEN
);
93 newuser
->nick
[NICKLEN
]='\0';
94 strncpy(newuser
->ident
,ident
,USERLEN
);
95 newuser
->ident
[USERLEN
]='\0';
96 newuser
->host
=findorcreatehost(host
);
97 newuser
->realname
=findorcreaterealname(realname
);
98 newuser
->nextbyhost
=newuser
->host
->nicks
;
99 newuser
->host
->nicks
=newuser
;
100 newuser
->nextbyrealname
=newuser
->realname
->nicks
;
101 newuser
->realname
->nicks
=newuser
;
102 newuser
->umodes
=umodes
;
104 memset(&ipaddress
, 0, sizeof(ipaddress
));
105 ((unsigned short *)(ipaddress
.in6_16
))[5] = 65535;
106 ((unsigned short *)(ipaddress
.in6_16
))[6] = 127;
107 ((unsigned char *)(ipaddress
.in6_16
))[14] = 1;
108 ((unsigned char *)(ipaddress
.in6_16
))[15] = (currentlocalunum%253
)+1;
110 newuser
->ipnode
= refnode(iptree
, &ipaddress
, PATRICIA_MAXBITS
);
111 node_increment_usercount(newuser
->ipnode
);
113 newuser
->timestamp
=getnettime();
114 newuser
->shident
=NULL
;
115 newuser
->sethost
=NULL
;
117 memset(newuser
->exts
, 0, MAXNICKEXTS
* sizeof(void *));
119 if (IsOper(newuser
)) {
120 newuser
->opername
= getsstring("-", ACCOUNTLEN
);
122 newuser
->opername
= NULL
;
125 newuser
->accountts
=0;
127 newuser
->authname
=NULLAUTHNAME
;
128 if (IsAccount(newuser
)) {
129 newuser
->accountts
=newuser
->timestamp
;
131 newuser
->auth
=findorcreateauthname(authid
, authname
);
132 newuser
->authname
=newuser
->auth
->name
;
133 newuser
->auth
->usercount
++;
134 newuser
->nextbyauthname
=newuser
->auth
->nicks
;
135 newuser
->auth
->nicks
=newuser
;
136 newuser
->auth
->flags
=accountflags
;
139 this is done for three reasons:
140 1: so I don't have to change 500 pieces of code
141 2: so services can be authed with special reserved ids
142 3: saves space over the old authname per user method
144 newuser
->authname
=malloc(strlen(authname
) + 1);
145 strcpy(newuser
->authname
,authname
);
150 /* Check for nick collision */
151 if ((np
=getnickbynick(nickname
))!=NULL
) {
152 /* Make sure we will win the collision */
153 newuser
->timestamp
=(np
->timestamp
-1);
154 killuser(NULL
, np
, "Nick collision");
156 sendnickmsg(newuser
);
160 umhandlers
[(currentlocalunum
&MAXLOCALUSER
)]=handler
;
163 *(gethandlebynumeric(newuser
->numeric
))=newuser
;
164 addnicktohash(newuser
);
165 triggerhook(HOOK_NICK_NEWNICK
,newuser
);
172 * This function changes the name of a given local user
175 int renamelocaluser(nick
*np
, char *newnick
) {
178 time_t timestamp
=getnettime();
183 if (strlen(newnick
) > NICKLEN
)
186 if (homeserver(np
->numeric
)!=mylongnum
)
189 if ((np2
=getnickbynick(newnick
))) {
191 /* Case only name change */
192 strncpy(np
->nick
,newnick
,NICKLEN
);
193 np
->nick
[NICKLEN
]='\0';
194 irc_send("%s N %s %d",iptobase64(ipbuf
, &(np
->p_ipaddr
), sizeof(ipbuf
), 1),np
->nick
,np
->timestamp
);
195 triggerhook(HOOK_NICK_RENAME
,np
);
198 /* Kill other user and drop through */
199 timestamp
=np2
->timestamp
-1;
200 killuser(NULL
, np2
, "Nick collision");
204 np
->timestamp
=timestamp
;
205 removenickfromhash(np
);
206 strncpy(np
->nick
,newnick
,NICKLEN
);
207 np
->nick
[NICKLEN
]='\0';
209 irc_send("%s N %s %d",longtonumeric(np
->numeric
,5),np
->nick
,np
->timestamp
);
210 triggerhook(HOOK_NICK_RENAME
,np
);
216 * deregisterlocaluser:
217 * This function removes the given local user from the network
220 int deregisterlocaluser(nick
*np
, char *reason
) {
225 if (np
==NULL
|| (homeserver(np
->numeric
)!=mylongnum
)) {
226 /* Non-existent user, or user not on this server */
230 if (reason
==NULL
|| *reason
=='\0') {
231 sprintf(reasonstr
,"Quit");
233 snprintf(reasonstr
,510,"Quit: %s",reason
);
239 triggerhook(HOOK_NICK_QUIT
, harg
);
242 umhandlers
[np
->numeric
&MAXLOCALUSER
]=NULL
;
245 irc_send("%s Q :%s",longtonumeric(numeric
,5),reasonstr
);
252 * hooklocaluserhandler:
253 * This function adds a new handler to the hook chain for a local user
254 * THIS RELIES ON MODULES BEING UNLOADED IN THE CORRECT ORDER.
256 UserMessageHandler
hooklocaluserhandler(nick
*np
, UserMessageHandler newhandler
) {
257 UserMessageHandler oldhandler
= NULL
;
258 if (np
==NULL
|| (homeserver(np
->numeric
)!=mylongnum
)) {
259 /* Non-existent user, or user not on this server */
262 oldhandler
= umhandlers
[np
->numeric
&MAXLOCALUSER
];
263 umhandlers
[np
->numeric
&MAXLOCALUSER
]=newhandler
;
269 * Sends details of a given local nick to the network.
272 void sendnickmsg(nick
*np
) {
274 char ipbuf
[25], operbuf
[ACCOUNTLEN
+ 5];
275 char accountbuf
[100];
277 strncpy(numericbuf
,longtonumeric(np
->numeric
,5),5);
280 if(IsOper(np
) && (serverlist
[myhub
].flags
& SMODE_OPERNAME
)) {
281 snprintf(operbuf
,sizeof(operbuf
)," %s",np
->opername
?np
->opername
->content
:"-");
289 if(np
->auth
->flags
) {
290 snprintf(accountbuf
,sizeof(accountbuf
)," %s:%ld:%lu:%llu",np
->authname
,np
->accountts
,np
->auth
->userid
,np
->auth
->flags
);
292 snprintf(accountbuf
,sizeof(accountbuf
)," %s:%ld:%lu",np
->authname
,np
->accountts
,np
->auth
->userid
);
294 } else if(np
->authname
) {
295 snprintf(accountbuf
,sizeof(accountbuf
)," %s:%ld:0",np
->authname
,np
->accountts
);
299 irc_send("%s N %s 1 %ld %s %s %s%s%s %s %s :%s",
300 mynumeric
->content
,np
->nick
,np
->timestamp
,np
->ident
,np
->host
->name
->content
,
301 printflags(np
->umodes
,umodeflags
),operbuf
,accountbuf
,iptobase64(ipbuf
,&(np
->p_ipaddr
),
302 sizeof(ipbuf
),1),numericbuf
,np
->realname
->name
->content
);
305 void sendnickburst(int hooknum
, void *arg
) {
306 /* Send nick messages for all local users */
310 nh
=servernicks
[numerictolong(mynumeric
->content
,2)];
311 for (i
=0;i
<=MAXLOCALUSER
;i
++) {
318 /* Check for a kill of a local user */
319 void checklocalkill(int hooknum
, void *arg
) {
321 nick
*target
=args
[0];
322 char *reason
=args
[1];
329 numeric
=((nick
*)target
)->numeric
;
331 if (homeserver(numeric
)==mylongnum
) {
332 if (umhandlers
[(numeric
)&MAXLOCALUSER
]!=NULL
) {
333 (umhandlers
[(numeric
)&MAXLOCALUSER
])((nick
*)target
,LU_KILLED
,myargs
);
338 int handleprivatemsgcmd(void *source
, int cargc
, char **cargv
) {
339 return handlemessageornotice(source
, cargc
, cargv
, 0);
342 int handleprivatenoticecmd(void *source
, int cargc
, char **cargv
) {
343 return handlemessageornotice(source
, cargc
, cargv
, 1);
346 /* Handle privmsg or notice command addressed to user */
347 int handlemessageornotice(void *source
, int cargc
, char **cargv
, int isnotice
) {
351 char targetnick
[NICKLEN
+1];
356 /* Should have target and message */
361 if (cargv
[0][0]=='#' || cargv
[0][0]=='+') {
362 /* Channel message/notice */
366 if ((sender
=getnickbynumericstr((char *)source
))==NULL
) {
367 Error("localuser",ERR_WARNING
,"PRIVMSG from non existant user %s",(char *)source
);
371 /* Check for a "secure" message (foo@bar) */
373 for (i
=0,ch
=cargv
[0];(i
<=NICKLEN
) && (*ch
);i
++,ch
++) {
383 if (!foundat
) { /* Didn't find an @ sign, assume it's a numeric */
384 if ((target
=getnickbynumericstr(cargv
[0]))==NULL
) {
385 Error("localuser",ERR_DEBUG
,"Couldn't find target for %s",cargv
[0]);
388 } else { /* Did find @, do a lookup by nick */
389 if ((target
=getnickbynick(targetnick
))==NULL
) {
390 Error("localuser",ERR_DEBUG
,"Couldn't find target for %s",cargv
[0]);
391 irc_send(":%s 401 %s %s :No such nick",myserver
->content
,sender
->nick
,cargv
[0]);
396 if (homeserver(target
->numeric
)!=mylongnum
) {
397 Error("localuser",ERR_WARNING
,"Got message/notice for someone not on my server");
398 irc_send(":%s 401 %s %s :No such nick",myserver
->content
,sender
->nick
,cargv
[0]);
402 if (foundat
&& !IsService(target
)) {
403 Error("localuser",ERR_DEBUG
,"Received secure message for %s, but user is not a service",cargv
[0]);
404 irc_send(":%s 401 %s %s :No such nick",myserver
->content
,sender
->nick
,cargv
[0]);
408 if (umhandlers
[(target
->numeric
)&MAXLOCALUSER
]==NULL
) {
409 /* No handler anyhow.. */
413 /* Dispatch the message. */
414 nargs
[0]=(void *)sender
;
415 nargs
[1]=(void *)cargv
[1];
416 (umhandlers
[(target
->numeric
)&MAXLOCALUSER
])(target
,isnotice
?LU_PRIVNOTICE
:(foundat
?LU_SECUREMSG
:LU_PRIVMSG
),nargs
);
421 /* Send message to user */
422 void sendmessagetouser(nick
*source
, nick
*target
, char *format
, ... ) {
427 longtonumeric2(source
->numeric
,5,senderstr
);
430 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
431 /* So max sendable message is 495 bytes. Of course, a client won't be able
432 * to receive this.. */
434 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
437 if (homeserver(target
->numeric
)!=mylongnum
)
438 irc_send("%s P %s :%s",senderstr
,longtonumeric(target
->numeric
,5),buf
);
441 /* Send messageto server, we don't check, but noones going to want to put a server pointer in anyway... */
442 void sendsecuremessagetouser(nick
*source
, nick
*target
, char *servername
, char *format
, ... ) {
447 longtonumeric2(source
->numeric
,5,senderstr
);
450 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
451 /* So max sendable message is 495 bytes. Of course, a client won't be able
452 * to receive this.. */
454 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
457 if (homeserver(target
->numeric
)!=mylongnum
)
458 irc_send("%s P %s@%s :%s",senderstr
,target
->nick
,servername
,buf
);
461 /* Send notice to user */
462 void sendnoticetouser(nick
*source
, nick
*target
, char *format
, ... ) {
467 longtonumeric2(source
->numeric
,5,senderstr
);
470 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
471 /* So max sendable message is 495 bytes. Of course, a client won't be able
472 * to receive this.. */
474 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
477 if (homeserver(target
->numeric
)!=mylongnum
)
478 irc_send("%s O %s :%s",senderstr
,longtonumeric(target
->numeric
,5),buf
);
482 void killuser(nick
*source
, nick
*target
, char *format
, ... ) {
487 va_start(va
, format
);
488 vsnprintf(buf
, BUFSIZE
-17, format
, va
);
491 if (hookqueuelength
) {
492 for (pk
= pendingkilllist
; pk
; pk
= pk
->next
)
493 if (pk
->target
== target
)
496 Error("localuser", ERR_DEBUG
, "Adding pending kill for %s", target
->nick
);
497 pk
= (pendingkill
*)malloc(sizeof(pendingkill
));
500 pk
->reason
= getsstring(buf
, BUFSIZE
);
501 pk
->next
= pendingkilllist
;
502 pendingkilllist
= pk
;
504 _killuser(source
, target
, buf
);
508 void sethostuser(nick
*target
, char *ident
, char *host
) {
509 irc_send("%s SH %s %s %s", mynumeric
->content
, longtonumeric(target
->numeric
, 5), ident
, host
);
512 void _killuser(nick
*source
, nick
*target
, char *reason
) {
514 char sourcestring
[HOSTLEN
+NICKLEN
+3];
517 /* If we have a null nick, use the server.. */
518 strcpy(senderstr
, mynumeric
->content
);
519 strcpy(sourcestring
, myserver
->content
);
521 strcpy(senderstr
, longtonumeric(source
->numeric
,5));
522 sprintf(sourcestring
,"%s!%s",source
->host
->name
->content
, source
->nick
);
525 irc_send("%s D %s :%s (%s)",senderstr
,longtonumeric(target
->numeric
,5),sourcestring
,reason
);
529 void clearpendingkills(int hooknum
, void *arg
) {
532 pk
= pendingkilllist
;
534 pendingkilllist
= pk
->next
;
537 Error("localuser", ERR_DEBUG
, "Processing pending kill for %s", pk
->target
->nick
);
538 _killuser(pk
->source
, pk
->target
, pk
->reason
->content
);
541 freesstring(pk
->reason
);
543 pk
= pendingkilllist
;
547 void checkpendingkills(int hooknum
, void *arg
) {
548 nick
*np
= (nick
*)arg
;
551 for (pk
=pendingkilllist
; pk
; pk
= pk
->next
) {
552 if (pk
->source
== np
) {
553 Error("localuser", ERR_INFO
, "Pending kill source %s got deleted, NULL'ing source for pending kill", np
->nick
);
556 if (pk
->target
== np
) {
557 Error("localuser", ERR_INFO
, "Pending kill target %s got deleted, NULL'ing target for pending kill", np
->nick
);
563 void sendaccountmessage(nick
*np
) {
566 if (np
->auth
->flags
) {
567 irc_send("%s AC %s %s %ld %lu %llu",mynumeric
->content
, longtonumeric(np
->numeric
,5), np
->authname
, np
->accountts
, np
->auth
->userid
, np
->auth
->flags
);
569 irc_send("%s AC %s %s %ld %lu",mynumeric
->content
, longtonumeric(np
->numeric
,5), np
->authname
, np
->accountts
, np
->auth
->userid
);
572 irc_send("%s AC %s %s %ld 0",mynumeric
->content
, longtonumeric(np
->numeric
,5), np
->authname
, np
->accountts
);
577 /* Auth user, don't use to set flags after authing */
578 void localusersetaccount(nick
*np
, char *accname
, unsigned long accid
, u_int64_t accountflags
, time_t authTS
) {
580 Error("localuser",ERR_WARNING
,"Tried to set account on user %s already authed", np
->nick
);
585 np
->accountts
=authTS
?authTS
:getnettime();
588 np
->auth
=findorcreateauthname(accid
, accname
);
589 np
->auth
->usercount
++;
590 np
->authname
=np
->auth
->name
;
591 np
->nextbyauthname
=np
->auth
->nicks
;
593 np
->auth
->flags
=accountflags
;
596 np
->authname
=malloc(strlen(accname
) + 1);
597 strcpy(np
->authname
,accname
);
600 sendaccountmessage(np
);
602 triggerhook(HOOK_NICK_ACCOUNT
, np
);
605 void localusersetumodes(nick
*np
, flag_t newmodes
) {
607 irc_send("%s M %s %s", longtonumeric(np
->numeric
,5), np
->nick
, printflagdiff(np
->umodes
, newmodes
, umodeflags
));
610 np
->umodes
= newmodes
;
613 void localusersetaccountflags(authname
*anp
, u_int64_t accountflags
) {
616 u_int64_t oldflags
= anp
->flags
;
620 anp
->flags
= accountflags
;
622 for(np
=anp
->nicks
;np
;np
=np
->next
)
623 sendaccountmessage(np
);
625 triggerhook(HOOK_AUTH_FLAGSUPDATED
, arg
);