3 #include "../nick/nick.h"
4 #include "../lib/base64.h"
5 #include "../irc/irc.h"
6 #include "../irc/irc_config.h"
7 #include "../core/hooks.h"
8 #include "../core/error.h"
16 UserMessageHandler umhandlers
[MAXLOCALUSER
+1];
18 void checklocalkill(int hooknum
, void *nick
);
23 for (i
=0;i
<=MAXLOCALUSER
;i
++) {
27 registerhook(HOOK_IRC_SENDBURSTNICKS
,&sendnickburst
);
28 registerhook(HOOK_NICK_LOSTNICK
,&checklocalkill
);
29 registerserverhandler("P",&handleprivatemsgcmd
,2);
30 registerserverhandler("O",&handleprivatenoticecmd
, 2);
35 * This function creates a local user, and broadcasts it's existence to the net (if connected).
38 nick
*registerlocaluser(char *nickname
, char *ident
, char *host
, char *realname
, char *authname
, flag_t umodes
, UserMessageHandler handler
) {
43 currentlocalunum
=(currentlocalunum
+1)%262142
;
44 while (servernicks
[numerictolong(mynumeric
->content
,2)][currentlocalunum
&MAXLOCALUSER
]!=NULL
) {
45 /* Numeric 262143 on our server is used for "nouser" by the channels module, so cannot be allocated */
46 currentlocalunum
=(currentlocalunum
+1)%262142
;
47 if (++i
>MAXLOCALUSER
) {
52 /* This code is very similar to stuff in nick.c... */
54 newuser
->numeric
=(numerictolong(mynumeric
->content
,2)<<18)|(currentlocalunum
);
55 strncpy(newuser
->nick
,nickname
,NICKLEN
);
56 newuser
->nick
[NICKLEN
]='\0';
57 strncpy(newuser
->ident
,ident
,USERLEN
);
58 newuser
->ident
[USERLEN
]='\0';
59 newuser
->host
=findorcreatehost(host
);
60 newuser
->realname
=findorcreaterealname(realname
);
61 newuser
->nextbyhost
=newuser
->host
->nicks
;
62 newuser
->host
->nicks
=newuser
;
63 newuser
->nextbyrealname
=newuser
->realname
->nicks
;
64 newuser
->realname
->nicks
=newuser
;
65 newuser
->umodes
=umodes
;
66 newuser
->ipaddress
=(127<<24)+(1<<8)+((currentlocalunum%253
)+1); /* Make it look like a valid addr on 127.0.1.0/24 */
67 newuser
->timestamp
=getnettime();
68 newuser
->shident
=NULL
;
69 newuser
->sethost
=NULL
;
71 memset(newuser
->exts
, 0, MAXNICKEXTS
* sizeof(void *));
73 if (IsAccount(newuser
)) {
74 strncpy(newuser
->authname
,authname
,ACCOUNTLEN
);
76 newuser
->authname
[0]='\0';
80 /* Check for nick collision */
81 if ((np
=getnickbynick(nickname
))!=NULL
) {
82 /* Make sure we will win the collision */
83 newuser
->timestamp
=(np
->timestamp
-1);
84 killuser(NULL
, np
, "Nick collision");
90 umhandlers
[(currentlocalunum
&MAXLOCALUSER
)]=handler
;
93 *(gethandlebynumeric(newuser
->numeric
))=newuser
;
94 addnicktohash(newuser
);
95 triggerhook(HOOK_NICK_NEWNICK
,newuser
);
102 * This function changes the name of a given local user
105 int renamelocaluser(nick
*np
, char *newnick
) {
107 time_t timestamp
=getnettime();
112 if (strlen(newnick
) > NICKLEN
)
115 if (homeserver(np
->numeric
)!=mylongnum
)
118 if ((np2
=getnickbynick(newnick
))) {
120 /* Case only name change */
121 strncpy(np
->nick
,newnick
,NICKLEN
);
122 np
->nick
[NICKLEN
]='\0';
123 irc_send("%s N %s %d",longtonumeric(np
->numeric
,5),np
->nick
,np
->timestamp
);
124 triggerhook(HOOK_NICK_RENAME
,np
);
127 /* Kill other user and drop through */
128 timestamp
=np2
->timestamp
-1;
129 killuser(NULL
, np2
, "Nick collision");
133 np
->timestamp
=timestamp
;
134 removenickfromhash(np
);
135 strncpy(np
->nick
,newnick
,NICKLEN
);
136 np
->nick
[NICKLEN
]='\0';
138 irc_send("%s N %s %d",longtonumeric(np
->numeric
,5),np
->nick
,np
->timestamp
);
139 triggerhook(HOOK_NICK_RENAME
,np
);
145 * deregisterlocaluser:
146 * This function removes the given local user from the network
149 int deregisterlocaluser(nick
*np
, char *reason
) {
153 if (np
==NULL
|| (homeserver(np
->numeric
)!=mylongnum
)) {
154 /* Non-existent user, or user not on this server */
158 if (reason
==NULL
|| *reason
=='\0') {
163 umhandlers
[np
->numeric
&MAXLOCALUSER
]=NULL
;
166 irc_send("%s Q :Quit: %s",longtonumeric(numeric
,5),(defaultreason
?"Leaving":reason
));
174 * Sends details of a given local nick to the network.
177 void sendnickmsg(nick
*np
) {
180 strncpy(numericbuf
,longtonumeric(np
->numeric
,5),5);
183 irc_send("%s N %s 1 %ld %s %s %s%s%s %s %s :%s",
184 mynumeric
->content
,np
->nick
,np
->timestamp
,np
->ident
,np
->host
->name
->content
,
185 printflags(np
->umodes
,umodeflags
),IsAccount(np
)?" ":"",
186 np
->authname
,longtonumeric(np
->ipaddress
,6),numericbuf
,
187 np
->realname
->name
->content
);
190 void sendnickburst(int hooknum
, void *arg
) {
191 /* Send nick messages for all local users */
195 nh
=servernicks
[numerictolong(mynumeric
->content
,2)];
196 for (i
=0;i
<=MAXLOCALUSER
;i
++) {
203 /* Check for a kill of a local user */
204 void checklocalkill(int hooknum
, void *target
) {
210 numeric
=((nick
*)target
)->numeric
;
212 if (homeserver(numeric
)==mylongnum
) {
213 if (umhandlers
[(numeric
)&MAXLOCALUSER
]!=NULL
) {
214 (umhandlers
[(numeric
)&MAXLOCALUSER
])((nick
*)target
,LU_KILLED
,args
);
219 int handleprivatemsgcmd(void *source
, int cargc
, char **cargv
) {
220 return handlemessageornotice(source
, cargc
, cargv
, 0);
223 int handleprivatenoticecmd(void *source
, int cargc
, char **cargv
) {
224 return handlemessageornotice(source
, cargc
, cargv
, 1);
227 /* Handle privmsg or notice command addressed to user */
228 int handlemessageornotice(void *source
, int cargc
, char **cargv
, int isnotice
) {
232 char targetnick
[NICKLEN
+1];
237 /* Should have target and message */
242 if (cargv
[0][0]=='#' || cargv
[0][0]=='+') {
243 /* Channel message/notice */
247 if ((sender
=getnickbynumericstr((char *)source
))==NULL
) {
248 Error("localuser",ERR_WARNING
,"PRIVMSG from non existant user %s",(char *)source
);
252 /* Check for a "secure" message (foo@bar) */
254 for (i
=0,ch
=cargv
[0];(i
<=NICKLEN
) && (*ch
);i
++,ch
++) {
264 if (!foundat
) { /* Didn't find an @ sign, assume it's a numeric */
265 if ((target
=getnickbynumericstr(cargv
[0]))==NULL
) {
266 Error("localuser",ERR_DEBUG
,"Couldn't find target for %s",cargv
[0]);
269 } else { /* Did find @, do a lookup by nick */
270 if ((target
=getnickbynick(targetnick
))==NULL
) {
271 Error("localuser",ERR_DEBUG
,"Couldn't find target for %s",cargv
[0]);
276 if (homeserver(target
->numeric
)!=mylongnum
) {
277 Error("localuser",ERR_WARNING
,"Got message/notice for someone not on my server");
281 if (umhandlers
[(target
->numeric
)&MAXLOCALUSER
]==NULL
) {
282 /* No handler anyhow.. */
286 /* Dispatch the message. */
287 nargs
[0]=(void *)sender
;
288 nargs
[1]=(void *)cargv
[1];
289 (umhandlers
[(target
->numeric
)&MAXLOCALUSER
])(target
,isnotice
?LU_PRIVNOTICE
:(foundat
?LU_SECUREMSG
:LU_PRIVMSG
),nargs
);
294 /* Send message to user */
295 void sendmessagetouser(nick
*source
, nick
*target
, char *format
, ... ) {
300 longtonumeric2(source
->numeric
,5,senderstr
);
303 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
304 /* So max sendable message is 495 bytes. Of course, a client won't be able
305 * to receive this.. */
307 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
310 if (homeserver(target
->numeric
)!=mylongnum
)
311 irc_send("%s P %s :%s",senderstr
,longtonumeric(target
->numeric
,5),buf
);
314 /* Send messageto server, we don't check, but noones going to want to put a server pointer in anyway... */
315 void sendsecuremessagetouser(nick
*source
, nick
*target
, char *servername
, char *format
, ... ) {
320 longtonumeric2(source
->numeric
,5,senderstr
);
323 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
324 /* So max sendable message is 495 bytes. Of course, a client won't be able
325 * to receive this.. */
327 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
330 if (homeserver(target
->numeric
)!=mylongnum
)
331 irc_send("%s P %s@%s :%s",senderstr
,target
->nick
,servername
,buf
);
334 /* Send notice to user */
335 void sendnoticetouser(nick
*source
, nick
*target
, char *format
, ... ) {
340 longtonumeric2(source
->numeric
,5,senderstr
);
343 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
344 /* So max sendable message is 495 bytes. Of course, a client won't be able
345 * to receive this.. */
347 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
350 if (homeserver(target
->numeric
)!=mylongnum
)
351 irc_send("%s O %s :%s",senderstr
,longtonumeric(target
->numeric
,5),buf
);
355 void killuser(nick
*source
, nick
*target
, char *format
, ... ) {
358 char sourcestring
[NICKLEN
+USERLEN
+3];
362 /* If we have a null nick, use the server.. */
363 strcpy(senderstr
, mynumeric
->content
);
364 strcpy(sourcestring
, myserver
->content
);
366 strcpy(senderstr
, longtonumeric(source
->numeric
,5));
367 sprintf(sourcestring
,"%s!%s",source
->host
->name
->content
, source
->nick
);
370 va_start(va
, format
);
371 vsnprintf(buf
, BUFSIZE
-17, format
, va
);
374 irc_send("%s D %s :%s (%s)",senderstr
,longtonumeric(target
->numeric
,5),sourcestring
,buf
);
379 void localusersetaccount(nick
*np
, char *accname
) {
381 Error("localuser",ERR_WARNING
,"Tried to set account on user %s already authed", np
->nick
);
386 strncpy(np
->authname
, accname
, ACCOUNTLEN
);
387 np
->authname
[ACCOUNTLEN
]='\0';
390 irc_send("%s AC %s %s %ld",mynumeric
->content
, longtonumeric(np
->numeric
,5), np
->authname
, getnettime());
393 triggerhook(HOOK_NICK_ACCOUNT
, np
);