2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2004 Alexander Barton <alex@barton.de>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
17 static char UNUSED id[] = "$Id: irc.c,v 1.132 2008/01/15 22:28:14 fw Exp $";
26 #include "conn-func.h"
31 #include "irc-write.h"
41 static char *Option_String PARAMS((CONN_ID Idx));
42 static bool Send_Message PARAMS((CLIENT *Client, REQUEST *Req, int ForceType, bool SendErrors));
43 static bool Send_Message_Mask PARAMS((CLIENT *from, char *targetMask, char *message, bool SendErrors));
44 static bool MatchCaseInsensitive PARAMS((const char *pattern, const char *searchme));
48 IRC_ERROR( CLIENT *Client, REQUEST *Req )
50 assert( Client != NULL );
51 assert( Req != NULL );
53 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
54 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
61 * Kill client on request.
62 * This function implements the IRC command "KILL" wich is used to selectively
63 * disconnect clients. It can be used by IRC operators and servers, for example
64 * to "solve" nick collisions after netsplits.
65 * Please note that this function is also called internally, without a real
66 * KILL command beeing received over the network! Client is Client_ThisServer()
69 IRC_KILL( CLIENT *Client, REQUEST *Req )
72 char reason[COMMAND_LEN], *msg;
73 CONN_ID my_conn, conn;
75 assert( Client != NULL );
76 assert( Req != NULL );
78 if(( Client_Type( Client ) != CLIENT_SERVER ) &&
79 ( ! Client_OperByMe( Client )))
81 /* The originator of the KILL is neither an IRC operator of
82 * this server nor a server. */
83 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
89 /* This command requires exactly 2 parameters! */
90 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
91 Client_ID( Client ), Req->command );
94 if( Req->prefix ) prefix = Client_Search( Req->prefix );
98 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
100 prefix = Client_ThisServer( );
103 if( Client != Client_ThisServer( ))
105 /* This is a "real" KILL received from the network. */
106 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
107 Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
110 /* Build reason string */
111 if( Client_Type( Client ) == CLIENT_USER )
113 /* Prefix the "reason" if the originator is a regular user,
114 * so users can't spoof KILLs of servers. */
115 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
116 Client_ID( Client ), Req->argv[1] );
119 strlcpy( reason, Req->argv[1], sizeof( reason ));
121 /* Inform other servers */
122 IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
123 Req->argv[0], reason );
125 /* Save ID of this connection */
126 my_conn = Client_Conn( Client );
128 /* Do we host such a client? */
129 c = Client_Search( Req->argv[0] );
132 if(( Client_Type( c ) != CLIENT_USER ) &&
133 ( Client_Type( c ) != CLIENT_GOTNICK ))
135 /* Target of this KILL is not a regular user, this is
136 * invalid! So we ignore this case if we received a
137 * regular KILL from the network and try to kill the
138 * client/connection anyway (but log an error!) if the
139 * origin is the local server. */
141 if( Client != Client_ThisServer( ))
143 /* Invalid KILL received from remote */
144 if( Client_Type( c ) == CLIENT_SERVER )
145 msg = ERR_CANTKILLSERVER_MSG;
147 msg = ERR_NOPRIVILEGES_MSG;
148 return IRC_WriteStrClient( Client, msg,
149 Client_ID( Client ));
152 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
153 Client_Type( c ), Req->argv[0] );
156 /* Kill client NOW! */
157 conn = Client_Conn( c );
158 Client_Destroy( c, NULL, reason, false );
160 Conn_Close( conn, NULL, reason, true );
163 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
165 /* Are we still connected or were we killed, too? */
166 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
174 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
176 return Send_Message(Client, Req, CLIENT_USER, false);
181 * Handler for the IRC command PRIVMSG.
184 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
186 return Send_Message(Client, Req, CLIENT_USER, true);
191 * Handler for the IRC command SQUERY.
194 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
196 return Send_Message(Client, Req, CLIENT_SERVICE, true);
201 IRC_TRACE( CLIENT *Client, REQUEST *Req )
203 CLIENT *from, *target, *c;
205 char user[CLIENT_USER_LEN];
207 assert( Client != NULL );
208 assert( Req != NULL );
210 /* Bad number of arguments? */
211 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
214 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
216 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
219 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
220 else target = Client_ThisServer( );
222 /* Forward command to other server? */
223 if( target != Client_ThisServer( ))
225 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
227 /* Send RPL_TRACELINK back to initiator */
228 idx = Client_Conn( Client ); assert( idx > NONE );
229 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
230 if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), PACKAGE_NAME, PACKAGE_VERSION, Client_ID( target ), Client_ID( Client_NextHop( target )), Option_String( idx2 ), time( NULL ) - Conn_StartTime( idx2 ), Conn_SendQ( idx ), Conn_SendQ( idx2 ))) return DISCONNECTED;
232 /* Forward command */
233 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
237 /* Infos about all connected servers */
241 if( Client_Conn( c ) > NONE )
244 if( Client_Type( c ) == CLIENT_SERVER )
247 strlcpy( user, Client_User( c ), sizeof( user ));
248 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
249 if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Client_ID( c ), user, Client_Hostname( c ), Client_Mask( Client_ThisServer( )), Option_String( Client_Conn( c )))) return DISCONNECTED;
251 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
254 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
257 c = Client_Next( c );
260 IRC_SetPenalty( Client, 3 );
261 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
266 IRC_HELP( CLIENT *Client, REQUEST *Req )
270 assert( Client != NULL );
271 assert( Req != NULL );
273 /* Bad number of arguments? */
274 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
276 cmd = Parse_GetCommandStruct( );
279 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
283 IRC_SetPenalty( Client, 2 );
289 Option_String( CONN_ID Idx )
291 static char option_txt[8];
294 options = Conn_Options(Idx);
296 strcpy(option_txt, "F"); /* No idea what this means, but the
297 * original ircd sends it ... */
299 if(options & CONN_ZIP) /* zlib compression supported. */
300 strcat(option_txt, "z");
304 } /* Option_String */
308 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
312 char *targetList = Req->argv[0];
313 char *currentTarget = Req->argv[0];
314 unsigned int targetCount = 1;
316 assert( Client != NULL );
317 assert( Req != NULL );
319 if (Req->argc == 0) {
322 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
323 Client_ID(Client), Req->command);
325 if (Req->argc == 1) {
328 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
334 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
335 Client_ID(Client), Req->command);
338 if (Client_Type(Client) == CLIENT_SERVER)
339 from = Client_Search(Req->prefix);
343 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
344 Client_ID(Client), Req->prefix);
347 while (*targetList) {
348 if (*targetList == ',') {
355 while (targetCount > 0) {
356 if (strchr(currentTarget, '!') == NULL)
357 cl = Client_Search(currentTarget);
361 char target[513]; // max mesage length plus null terminator
365 char * server = NULL;
367 strncpy(target, currentTarget, 512);
369 server = strchr(target, '@');
374 host = strchr(target, '%');
379 user = strchr(target, '!');
384 host = server; // <msgto> form: nick!user@host
390 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
391 if (Client_Type(cl) != CLIENT_USER)
394 if (strcmp(nick, Client_ID(cl)) == 0 && strcmp(user, Client_User(cl)) == 0 && strcasecmp(host, Client_Hostname(cl)) == 0)
399 if (strcasecmp(user, Client_User(cl)) != 0)
401 if (host != NULL && strcasecmp(host, Client_Hostname(cl)) != 0)
403 if (server != NULL && strcasecmp(server, Client_ID(Client_Introducer(cl))) != 0)
411 /* Target is a user, enforce type */
412 if (Client_Type(cl) != ForceType) {
415 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
419 } else if ((Client_Type(Client) != CLIENT_SERVER
420 && (strchr(Client_Modes(cl), 'a')))) {
424 if (!IRC_WriteStrClient
425 (from, RPL_AWAY_MSG, Client_ID(from), Client_ID(cl),
428 } if (Client_Conn(from) > NONE) {
429 Conn_UpdateIdle(Client_Conn(from));
431 if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s",
432 Client_ID(cl), Req->argv[1]))
434 } else if (strchr("$#", currentTarget[0]) && strchr(currentTarget, '.')) {
435 if (!Send_Message_Mask(from, currentTarget, Req->argv[1], SendErrors))
437 } else if ((chan = Channel_Search(currentTarget))) {
438 if (!Channel_Write(chan, from, Client, Req->argv[1]))
443 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
444 Client_ID(from), currentTarget))
448 while (*currentTarget)
461 Send_Message_Mask(CLIENT * from, char * targetMask, char * message, bool SendErrors)
465 char *mask = targetMask + 1;
469 if (strchr(Client_Modes(from), 'o') == NULL) {
472 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG, Client_ID(from));
475 if (targetMask[0] == '#') {
476 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
477 if (Client_Type(cl) != CLIENT_USER)
479 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
481 if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s", Client_ID(cl), message))
485 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
486 if (Client_Type(cl) != CLIENT_USER)
488 client_match = MatchCaseInsensitive(mask, Client_ID(Client_Introducer(cl)));
490 if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s", Client_ID(cl), message))
495 } /* Send_Message_Mask */
499 MatchCaseInsensitive(const char *pattern, const char *searchme)
501 char haystack[COMMAND_LEN];
503 strlcpy(haystack, searchme, sizeof(haystack));
505 ngt_LowerStr(haystack);
507 return Match(pattern, haystack);