2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 * der GNU General Public License (GPL), wie von der Free Software Foundation
7 * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
12 * $Id: irc.c,v 1.38 2002/01/18 15:31:32 alex Exp $
17 * Revision 1.38 2002/01/18 15:31:32 alex
18 * - die User-Modes bei einem NICK von einem Server wurden falsch uebernommen.
20 * Revision 1.37 2002/01/16 22:10:18 alex
21 * - IRC_LUSERS() implementiert.
23 * Revision 1.36 2002/01/11 23:50:55 alex
24 * - LINKS implementiert, LUSERS begonnen.
26 * Revision 1.35 2002/01/09 21:30:45 alex
27 * - WHOIS wurde faelschlicherweise an User geforwarded statt vom Server beantwortet.
29 * Revision 1.34 2002/01/09 01:09:58 alex
30 * - WHOIS wird im "Strict-RFC-Mode" nicht mehr automatisch geforwarded,
31 * - andere Server werden nun ueber bisherige Server und User informiert.
33 * Revision 1.33 2002/01/07 23:42:12 alex
34 * - Es werden fuer alle Server eigene Token generiert,
35 * - QUIT von einem Server fuer einen User wird an andere Server geforwarded,
36 * - ebenso NICK-Befehle, die "fremde" User einfuehren.
38 * Revision 1.32 2002/01/07 16:02:36 alex
39 * - Loglevel von Remote-Mode-Aenderungen angepasst (nun Debug).
40 * - Im Debug-Mode werden nun auch PING's protokolliert.
42 * Revision 1.31 2002/01/07 15:39:46 alex
43 * - Server nimmt nun Server-Links an: PASS und SERVER entsprechend angepasst.
44 * - MODE und NICK melden nun die Aenderungen an andere Server.
46 * Revision 1.30 2002/01/06 15:21:29 alex
47 * - Loglevel und Meldungen nochmals ueberarbeitet.
48 * - QUIT und SQUIT forwarden nun den Grund der Trennung,
49 * - WHOIS wird nun immer an den "Original-Server" weitergeleitet.
51 * Revision 1.29 2002/01/05 23:24:54 alex
52 * - WHOIS erweitert: Anfragen koennen an andere Server weitergeleitet werden.
53 * - Vorbereitungen fuer Ident-Abfragen bei neuen Client-Strukturen.
55 * Revision 1.28 2002/01/05 20:08:02 alex
56 * - Div. Aenderungen fuer die Server-Links (u.a. WHOIS, QUIT, NICK angepasst).
57 * - Neue Funktionen IRC_WriteStrServer() und IRC_WriteStrServerPrefix().
59 * Revision 1.27 2002/01/05 19:15:03 alex
60 * - Fehlerpruefung bei select() in der "Hauptschleife" korrigiert.
62 * Revision 1.26 2002/01/05 16:51:18 alex
63 * - das Passwort von Servern wird nun ueberprueft (PASS- und SERVER-Befehl).
65 * Revision 1.25 2002/01/05 00:48:33 alex
66 * - bei SQUIT wurde immer die Verbindung getrennt, auch bei Remote-Servern.
68 * Revision 1.24 2002/01/04 17:58:44 alex
69 * - IRC_WriteStrXXX()-Funktionen eingefuehrt, groessere Anpassungen daran.
70 * - neuer Befehl SQUIT, QUIT an Server-Links angepasst.
72 * Revision 1.23 2002/01/04 01:36:40 alex
73 * - Loglevel ein wenig angepasst.
75 * Revision 1.22 2002/01/04 01:21:47 alex
76 * - Client-Strukruren werden nur noch ueber Funktionen angesprochen.
77 * - Weitere Anpassungen und Erweiterungen der Server-Links.
79 * Revision 1.21 2002/01/03 02:26:51 alex
80 * - neue Befehle SERVER und NJOIN begonnen,
81 * - begonnen, diverse IRC-Befehle an Server-Links anzupassen.
83 * Revision 1.20 2002/01/02 12:46:41 alex
84 * - die Gross- und Kleinschreibung des Nicks kann mit NICK nun geaendert werden.
86 * Revision 1.19 2002/01/02 02:51:39 alex
87 * - Copyright-Texte angepasst.
88 * - neuer Befehl "ERROR".
90 * Revision 1.17 2001/12/31 15:33:13 alex
91 * - neuer Befehl NAMES, kleinere Bugfixes.
92 * - Bug bei PING behoben: war zu restriktiv implementiert :-)
94 * Revision 1.16 2001/12/31 02:18:51 alex
95 * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
96 * - neuen Header "defines.h" mit (fast) allen Konstanten.
97 * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
99 * Revision 1.15 2001/12/30 19:26:11 alex
100 * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
102 * Revision 1.14 2001/12/30 11:42:00 alex
103 * - der Server meldet nun eine ordentliche "Start-Zeit".
105 * Revision 1.13 2001/12/29 03:10:06 alex
106 * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
107 * - neue configure-Optione "--enable-strict-rfc".
109 * Revision 1.12 2001/12/27 19:17:26 alex
110 * - neue Befehle PRIVMSG, NOTICE, PING.
112 * Revision 1.11 2001/12/27 16:55:41 alex
113 * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
115 * Revision 1.10 2001/12/26 22:48:53 alex
116 * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
118 * Revision 1.9 2001/12/26 14:45:37 alex
121 * Revision 1.8 2001/12/26 03:21:46 alex
122 * - PING/PONG-Befehle implementiert,
123 * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
125 * Revision 1.7 2001/12/25 23:25:18 alex
126 * - und nochmal Aenderungen am Logging ;-)
128 * Revision 1.6 2001/12/25 23:13:33 alex
129 * - Debug-Meldungen angepasst.
131 * Revision 1.5 2001/12/25 22:02:42 alex
132 * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
134 * Revision 1.4 2001/12/25 19:19:30 alex
135 * - bessere Fehler-Abfragen, diverse Bugfixes.
136 * - Nicks werden nur einmal vergeben :-)
137 * - /MOTD wird unterstuetzt.
139 * Revision 1.3 2001/12/24 01:34:06 alex
140 * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
141 * - MOTD-Ausgabe begonnen zu implementieren.
143 * Revision 1.2 2001/12/23 21:57:16 alex
144 * - erste IRC-Befehle zu implementieren begonnen.
146 * Revision 1.1 2001/12/14 08:13:43 alex
147 * - neues Modul begonnen :-)
167 #include "messages.h"
175 #define CONNECTED TRUE
176 #define DISCONNECTED FALSE
179 LOCAL BOOLEAN Hello_User( CLIENT *Client );
180 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
182 LOCAL VOID Kill_Nick( CHAR *Nick );
185 GLOBAL VOID IRC_Init( VOID )
190 GLOBAL VOID IRC_Exit( VOID )
195 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CHAR *Format, ... )
198 BOOLEAN ok = CONNECTED;
201 assert( Client != NULL );
202 assert( Format != NULL );
204 va_start( ap, Format );
205 vsnprintf( buffer, 1000, Format, ap );
208 /* an den Client selber */
209 ok = IRC_WriteStrClientPrefix( Client, Client_ThisServer( ), buffer );
212 } /* IRC_WriteStrClient */
215 GLOBAL BOOLEAN IRC_WriteStrClientPrefix( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
217 /* Text an Clients, lokal bzw. remote, senden. */
222 assert( Client != NULL );
223 assert( Format != NULL );
224 assert( Prefix != NULL );
226 va_start( ap, Format );
227 vsnprintf( buffer, 1000, Format, ap );
230 return Conn_WriteStr( Client_Conn( Client_NextHop( Client )), ":%s %s", Client_ID( Prefix ), buffer );
231 } /* IRC_WriteStrClientPrefix */
234 GLOBAL BOOLEAN IRC_WriteStrRelated( CLIENT *Client, CHAR *Format, ... )
237 BOOLEAN ok = CONNECTED;
240 assert( Client != NULL );
241 assert( Format != NULL );
243 va_start( ap, Format );
244 vsnprintf( buffer, 1000, Format, ap );
247 /* an den Client selber */
248 ok = IRC_WriteStrClient( Client, buffer );
253 } /* IRC_WriteStrRelated */
256 GLOBAL VOID IRC_WriteStrServers( CLIENT *ExceptOf, CHAR *Format, ... )
261 assert( Format != NULL );
263 va_start( ap, Format );
264 vsnprintf( buffer, 1000, Format, ap );
267 /* an den Client selber */
268 return IRC_WriteStrServersPrefix( ExceptOf, Client_ThisServer( ), buffer );
269 } /* IRC_WriteStrServers */
272 GLOBAL VOID IRC_WriteStrServersPrefix( CLIENT *ExceptOf, CLIENT *Prefix, CHAR *Format, ... )
278 assert( Format != NULL );
279 assert( Prefix != NULL );
281 va_start( ap, Format );
282 vsnprintf( buffer, 1000, Format, ap );
288 if(( Client_Type( c ) == CLIENT_SERVER ) && ( Client_Conn( c ) > NONE ) && ( c != Client_ThisServer( )) && ( c != ExceptOf ))
290 /* Ziel-Server gefunden */
291 IRC_WriteStrClientPrefix( c, Prefix, buffer );
293 c = Client_Next( c );
295 } /* IRC_WriteStrServersPrefix */
298 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
300 assert( Client != NULL );
301 assert( Req != NULL );
303 /* Fehler liefern, wenn kein lokaler Client */
304 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
306 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
308 /* noch nicht registrierte unbekannte Verbindung */
309 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
311 /* Passwort speichern */
312 Client_SetPassword( Client, Req->argv[0] );
314 Client_SetType( Client, CLIENT_GOTPASS );
317 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
319 /* noch nicht registrierte Server-Verbindung */
320 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
322 /* Passwort speichern */
323 Client_SetPassword( Client, Req->argv[0] );
325 Client_SetType( Client, CLIENT_GOTPASSSERVER );
328 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
330 /* Falsche Anzahl Parameter? */
331 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
333 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
337 GLOBAL BOOLEAN IRC_SERVER( CLIENT *Client, REQUEST *Req )
339 CHAR str[LINE_LEN], *ptr;
344 assert( Client != NULL );
345 assert( Req != NULL );
347 /* Fehler liefern, wenn kein lokaler Client */
348 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
350 if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
352 /* Verbindung soll als Server-Server-Verbindung registriert werden */
353 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
355 /* Falsche Anzahl Parameter? */
356 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
358 /* Ist dieser Server bei uns konfiguriert? */
359 for( i = 0; i < Conf_Server_Count; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
360 if( i >= Conf_Server_Count )
362 /* Server ist nicht konfiguriert! */
363 Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
364 Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", TRUE );
367 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd ) != 0 )
369 /* Falsches Passwort */
370 Log( LOG_ERR, "Connection %d: Bad password for server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
371 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
375 /* Ist ein Server mit dieser ID bereits registriert? */
376 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
378 /* Server-Strukturen fuellen ;-) */
379 Client_SetID( Client, Req->argv[0] );
380 Client_SetHops( Client, 1 );
381 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
383 /* Meldet sich der Server bei uns an? */
386 /* Unseren SERVER- und PASS-Befehl senden */
388 if( ! IRC_WriteStrClient( Client, "PASS %s "PASSSERVERADD, Conf_Server[i].pwd )) ok = FALSE;
389 else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
392 Conn_Close( Client_Conn( Client ), "Unexpected server behavior!", NULL, FALSE );
395 Client_SetIntroducer( Client, Client );
396 Client_SetToken( Client, 1 );
398 else Client_SetToken( Client, atoi( Req->argv[1] ));
400 Log( LOG_NOTICE, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), Client_Conn( Client ));
402 Client_SetType( Client, CLIENT_SERVER );
404 /* Alle bisherigen Server dem neuen Server bekannt machen,
405 * die bisherigen Server ueber den neuen informierenn */
409 if(( Client_Type( c ) == CLIENT_SERVER ) && ( c != Client ) && ( c != Client_ThisServer( )))
411 if( Client_Conn( c ) > NONE )
413 /* Dem gefundenen Server gleich den neuen
414 * Server bekannt machen */
415 if( ! IRC_WriteStrClient( c, "SERVER %s %d %d :%s", Client_ID( Client ), Client_Hops( Client ) + 1, Client_MyToken( Client ), Client_Info( Client ))) return DISCONNECTED;
418 /* Den neuen Server ueber den alten informieren */
419 if( ! IRC_WriteStrClientPrefix( Client, Client_Introducer( c ), "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ))) return DISCONNECTED;
421 c = Client_Next( c );
424 /* alle User dem neuen Server bekannt machen */
428 if( Client_Type( c ) == CLIENT_USER )
430 /* User an neuen Server melden */
431 if( ! IRC_WriteStrClient( Client, "NICK %s %d %s %s %d +%s :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_User( c ), Client_Hostname( c ), Client_MyToken( Client_Introducer( c )), Client_Modes( c ), Client_Info( c ))) return DISCONNECTED;
433 c = Client_Next( c );
438 else if( Client_Type( Client ) == CLIENT_SERVER )
440 /* Neuer Server wird im Netz angekuendigt */
442 /* Falsche Anzahl Parameter? */
443 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
445 /* Ist ein Server mit dieser ID bereits registriert? */
446 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
448 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
449 ptr = strchr( Req->argv[3] + 2, '[' );
450 if( ! ptr ) ptr = Req->argv[3];
452 /* Neue Client-Struktur anlegen */
453 c = Client_NewRemoteServer( Client, Req->argv[0], atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, TRUE );
456 /* Neue Client-Struktur konnte nicht angelegt werden */
457 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
458 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", TRUE );
462 /* Log-Meldung zusammenbauen und ausgeben */
463 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) sprintf( str, "connected to %s, ", Req->prefix );
464 else strcpy( str, "" );
465 Log( LOG_NOTICE, "Server \"%s\" registered (via %s, %s%d hop%s).", Client_ID( c ), Client_ID( Client ), str, Client_Hops( c ), Client_Hops( c ) > 1 ? "s": "" );
469 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
473 GLOBAL BOOLEAN IRC_NJOIN( CLIENT *Client, REQUEST *Req )
475 assert( Client != NULL );
476 assert( Req != NULL );
478 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTEREDSERVER_MSG, Client_ID( Client ));
484 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
486 CLIENT *intr_c, *target, *c;
489 assert( Client != NULL );
490 assert( Req != NULL );
492 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
494 if( Client_Type( Client ) == CLIENT_UNKNOWN || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTUSER || Client_Type( Client ) == CLIENT_USER || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
496 if( Client_Type( Client ) == CLIENT_UNKNOWN || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_USER || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
499 /* User-Registrierung bzw. Nick-Aenderung */
501 /* Falsche Anzahl Parameter? */
502 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
504 /* "Ziel-Client" ermitteln */
505 if( Client_Type( Client ) == CLIENT_SERVER )
507 target = Client_GetFromID( Req->prefix );
508 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
512 /* Ist der Client "restricted"? */
513 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
517 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
518 * wir nichts. So macht es das Original und mind. Snak hat probleme,
519 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
521 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
524 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
525 * will nur die Gross- und Kleinschreibung aendern. Das darf
526 * er natuerlich machen :-) */
527 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
529 if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
532 if( Client_Type( Client ) == CLIENT_USER )
534 /* Nick-Aenderung: allen mitteilen! */
535 Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
536 IRC_WriteStrRelated( Client, "NICK :%s", Req->argv[0] );
537 IRC_WriteStrServersPrefix( NULL, Client, "NICK :%s", Req->argv[0] );
539 else if( Client_Type( Client ) == CLIENT_SERVER )
541 /* Nick-Aenderung: allen mitteilen! */
542 Log( LOG_DEBUG, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
543 IRC_WriteStrServersPrefix( Client, Client, "NICK :%s", Req->argv[0] );
546 /* Client-Nick registrieren */
547 Client_SetID( target, Req->argv[0] );
549 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
552 Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client_Conn( Client ));
553 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
554 else Client_SetType( Client, CLIENT_GOTNICK );
558 else if( Client_Type( Client ) == CLIENT_SERVER )
560 /* Server fuehrt neuen Client ein */
562 /* Falsche Anzahl Parameter? */
563 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
565 /* Nick ueberpruefen */
566 c = Client_GetFromID( Req->argv[0] );
569 /* Der neue Nick ist auf diesem Server bereits registriert:
570 * sowohl der neue, als auch der alte Client muessen nun
571 * disconnectiert werden. */
572 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
573 Kill_Nick( Req->argv[0] );
577 /* Server, zu dem der Client connectiert ist, suchen */
578 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
581 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
582 Kill_Nick( Req->argv[0] );
586 /* Neue Client-Struktur anlegen */
587 c = Client_NewRemoteUser( intr_c, Req->argv[0], atoi( Req->argv[1] ), Req->argv[2], Req->argv[3], atoi( Req->argv[4] ), Req->argv[5] + 1, Req->argv[6], TRUE );
590 /* Eine neue Client-Struktur konnte nicht angelegt werden.
591 * Der Client muss disconnectiert werden, damit der Netz-
592 * status konsistent bleibt. */
593 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
594 Kill_Nick( Req->argv[0] );
598 modes = Client_Modes( c );
599 if( *modes ) Log( LOG_DEBUG, "User \"%s\" (+%s) registered (via %s, on %s, %d hop%s).", Client_Mask( c ), modes, Client_ID( Client ), Client_ID( intr_c ), Client_Hops( c ), Client_Hops( c ) > 1 ? "s": "" );
600 else Log( LOG_DEBUG, "User \"%s\" registered (via %s, on %s, %d hop%s).", Client_Mask( c ), Client_ID( Client ), Client_ID( intr_c ), Client_Hops( c ), Client_Hops( c ) > 1 ? "s": "" );
602 /* Andere Server, ausser dem Introducer, informieren */
603 IRC_WriteStrServersPrefix( Client, Client, "NICK %s %d %s %s %d %s :%s", Req->argv[0], atoi( Req->argv[1] ) + 1, Req->argv[2], Req->argv[3], Client_MyToken( intr_c ), Req->argv[5], Req->argv[6] );
607 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
611 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
613 assert( Client != NULL );
614 assert( Req != NULL );
617 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
619 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
622 /* Falsche Anzahl Parameter? */
623 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
625 Client_SetUser( Client, Req->argv[0], FALSE );
626 Client_SetInfo( Client, Req->argv[3] );
628 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client_Conn( Client ));
629 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
630 else Client_SetType( Client, CLIENT_GOTUSER );
633 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
635 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
637 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
641 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
645 assert( Client != NULL );
646 assert( Req != NULL );
648 if(( Client_Type( Client ) == CLIENT_USER ) || ( Client_Type( Client ) == CLIENT_SERVICE ))
652 /* Falsche Anzahl Parameter? */
653 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
655 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
656 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
660 else if ( Client_Type( Client ) == CLIENT_SERVER )
664 /* Falsche Anzahl Parameter? */
665 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
667 target = Client_Search( Req->prefix );
670 Log( LOG_ERR, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
674 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL );
675 else Client_Destroy( target, "Got QUIT command.", Req->argv[0] );
679 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
683 GLOBAL BOOLEAN IRC_SQUIT( CLIENT *Client, REQUEST *Req )
687 assert( Client != NULL );
688 assert( Req != NULL );
690 /* SQUIT ist nur fuer Server erlaubt */
691 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
693 /* Falsche Anzahl Parameter? */
694 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
696 target = Client_GetFromID( Req->argv[0] );
699 Log( LOG_ERR, "Got SQUIT from %s for unknown server \%s\"!?", Client_ID( Client ), Req->argv[0] );
703 if( target == Client ) Log( LOG_DEBUG, "Got SQUIT from %s: %s", Client_ID( Client ), Req->argv[1] );
704 else Log( LOG_DEBUG, "Got SQUIT from %s for %s: %s", Client_ID( Client ), Client_ID( target ), Req->argv[1] );
706 /* SQUIT an alle Server weiterleiten */
707 IRC_WriteStrServers( Client, "SQUIT %s :%s", Req->argv[0], Req->argv[1] );
709 if( Client_Conn( target ) > NONE )
711 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), "Got SQUIT command.", Req->argv[1], TRUE );
712 else Conn_Close( Client_Conn( target ), "Got SQUIT command.", NULL, TRUE );
717 Client_Destroy( target, "Got SQUIT command.", Req->argv[1] );
723 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
725 assert( Client != NULL );
726 assert( Req != NULL );
728 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
730 /* Falsche Anzahl Parameter? */
731 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
732 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
734 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
735 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
739 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
741 assert( Client != NULL );
742 assert( Req != NULL );
744 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
746 /* Falsche Anzahl Parameter? */
747 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
748 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
750 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
751 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
753 Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
758 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
760 assert( Client != NULL );
761 assert( Req != NULL );
763 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
765 /* Falsche Anzahl Parameter? */
766 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
768 return Show_MOTD( Client );
772 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
776 assert( Client != NULL );
777 assert( Req != NULL );
779 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
781 /* Falsche Anzahl Parameter? */
782 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
783 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
784 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
786 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
788 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
790 to = Client_Search( Req->argv[0] );
793 /* Okay, Ziel ist ein User */
794 if( Client_Conn( from ) >= 0 ) Conn_UpdateIdle( Client_Conn( from ));
795 return IRC_WriteStrClientPrefix( to, from, "PRIVMSG %s :%s", Client_ID( to ), Req->argv[1] );
797 else return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
801 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
805 assert( Client != NULL );
806 assert( Req != NULL );
808 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
810 /* Falsche Anzahl Parameter? */
811 if( Req->argc != 2 ) return CONNECTED;
813 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
815 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
817 to = Client_Search( Req->argv[0] );
820 /* Okay, Ziel ist ein User */
821 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
823 else return CONNECTED;
827 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
829 CHAR x[2], new_modes[CLIENT_MODE_LEN], *ptr;
833 assert( Client != NULL );
834 assert( Req != NULL );
836 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
838 /* Falsche Anzahl Parameter? */
839 if(( Req->argc > 2 ) || ( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
841 /* "Ziel-Client" suchen */
842 target = Client_Search( Req->argv[0] );
844 /* Wer ist der Anfragende? */
845 if( Client_Type( Client ) == CLIENT_USER )
847 /* User: MODE ist nur fuer sich selber zulaessig */
848 if( target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
852 /* Server: gibt es den Client ueberhaupt? */
853 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
856 /* Werden die Modes erfragt? */
857 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( Client ));
861 /* Sollen Modes gesetzt oder geloescht werden? */
862 if( *ptr == '+' ) set = TRUE;
863 else if( *ptr == '-' ) set = FALSE;
864 else return IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
866 /* Reply-String mit Aenderungen vorbereiten */
867 if( set ) strcpy( new_modes, "+" );
868 else strcpy( new_modes, "-" );
876 if( Client_Type( Client ) == CLIENT_SERVER )
890 /* restricted (kann nur gesetzt werden) */
891 if( set ) x[0] = 'r';
892 else ok = IRC_WriteStrClient( target, ERR_RESTRICTED_MSG, Client_ID( target ));
895 /* operator (kann nur geloescht werden) */
898 Client_SetOperByMe( target, FALSE );
901 else ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
904 ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
911 if( ! x[0] ) continue;
913 /* Okay, gueltigen Mode gefunden */
916 /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
917 if( Client_ModeAdd( target, x[0] )) strcat( new_modes, x );
921 /* Modes geloescht. Wenn der Client ihn hatte: merken */
922 if( Client_ModeDel( target, x[0] )) strcat( new_modes, x );
926 /* Geanderte Modes? */
929 if( Client_Type( Client ) == CLIENT_SERVER )
931 /* Modes an andere Server forwarden */
932 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :%s", Client_ID( target ), new_modes );
936 /* Bestaetigung an Client schicken & andere Server informieren */
937 ok = IRC_WriteStrRelated( Client, "MODE %s :%s", Client_ID( target ), new_modes );
938 IRC_WriteStrServers( Client, "MODE %s :%s", Client_ID( target ), new_modes );
940 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( target ), Client_Modes( target ));
946 GLOBAL BOOLEAN IRC_OPER( CLIENT *Client, REQUEST *Req )
950 assert( Client != NULL );
951 assert( Req != NULL );
953 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
955 /* Falsche Anzahl Parameter? */
956 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
958 /* Operator suchen */
959 for( i = 0; i < Conf_Oper_Count; i++)
961 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
963 if( i >= Conf_Oper_Count )
965 Log( LOG_WARNING, "Got invalid OPER from \"%s\": Name \"%s\" not configured!", Client_Mask( Client ), Req->argv[0] );
966 return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
969 /* Stimmt das Passwort? */
970 if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
972 Log( LOG_WARNING, "Got invalid OPER from \"%s\": Bad password for \"%s\"!", Client_Mask( Client ), Conf_Oper[i].name );
973 return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
976 if( ! Client_HasMode( Client, 'o' ))
978 /* noch kein o-Mode gesetzt */
979 Client_ModeAdd( Client, 'o' );
980 if( ! IRC_WriteStrRelated( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
981 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
984 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
986 Client_SetOperByMe( Client, TRUE );
987 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
991 GLOBAL BOOLEAN IRC_DIE( CLIENT *Client, REQUEST *Req )
993 assert( Client != NULL );
994 assert( Req != NULL );
996 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
998 /* Falsche Anzahl Parameter? */
999 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1001 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
1003 Log( LOG_NOTICE, "Got DIE command from \"%s\", going down!", Client_Mask( Client ));
1009 GLOBAL BOOLEAN IRC_RESTART( CLIENT *Client, REQUEST *Req )
1011 assert( Client != NULL );
1012 assert( Req != NULL );
1014 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1016 /* Falsche Anzahl Parameter? */
1017 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1019 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
1021 Log( LOG_NOTICE, "Got RESTART command from \"%s\", going down!", Client_Mask( Client ));
1022 NGIRCd_Restart = TRUE;
1027 GLOBAL BOOLEAN IRC_NAMES( CLIENT *Client, REQUEST *Req )
1029 CHAR rpl[COMMAND_LEN];
1032 assert( Client != NULL );
1033 assert( Req != NULL );
1035 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1037 /* Falsche Anzahl Parameter? */
1038 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1040 /* Noch alle User ausgeben, die in keinem Channel sind */
1042 c = Client_First( );
1045 if( Client_Type( c ) == CLIENT_USER )
1047 /* Okay, das ist ein User */
1048 strcat( rpl, Client_ID( c ));
1052 /* Antwort zu lang? Splitten. */
1053 if( strlen( rpl ) > 480 )
1055 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1056 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
1060 c = Client_Next( c );
1064 /* es wurden User gefunden */
1065 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1066 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
1068 return IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), "*" );
1072 GLOBAL BOOLEAN IRC_ISON( CLIENT *Client, REQUEST *Req )
1074 CHAR rpl[COMMAND_LEN];
1079 assert( Client != NULL );
1080 assert( Req != NULL );
1082 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1084 /* Falsche Anzahl Parameter? */
1085 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1087 strcpy( rpl, RPL_ISON_MSG );
1088 for( i = 0; i < Req->argc; i++ )
1090 ptr = strtok( Req->argv[i], " " );
1094 c = Client_GetFromID( ptr );
1095 if( c && ( Client_Type( c ) == CLIENT_USER ))
1097 /* Dieser Nick ist "online" */
1101 ptr = strtok( NULL, " " );
1104 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1106 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1110 GLOBAL BOOLEAN IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1112 CLIENT *from, *target, *c;
1115 assert( Client != NULL );
1116 assert( Req != NULL );
1118 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1120 /* Falsche Anzahl Parameter? */
1121 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1124 c = Client_GetFromID( Req->argv[Req->argc - 1] );
1125 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
1127 /* Empfaenger des WHOIS suchen */
1128 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1130 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1132 /* Forwarden an anderen Server? */
1135 /* angegebenen Ziel-Server suchen */
1136 target = Client_GetFromID( Req->argv[1] );
1137 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
1141 else if( Client_Conn( c ) == NONE )
1143 /* Client ist nicht von uns. Ziel-Server suchen */
1150 if( target && ( Client_NextHop( target ) != Client_ThisServer( )) && ( Client_Type( Client_NextHop( target )) == CLIENT_SERVER )) return IRC_WriteStrClientPrefix( target, from, "WHOIS %s :%s", Req->argv[0], ptr );
1152 /* Nick, User und Name */
1153 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
1156 if( ! IRC_WriteStrClient( from, RPL_WHOISSERVER_MSG, Client_ID( from ), Client_ID( c ), Client_ID( Client_Introducer( c )), Client_Info( Client_Introducer( c )))) return DISCONNECTED;
1159 if( Client_HasMode( c, 'o' ))
1161 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
1164 /* Idle (nur lokale Clients) */
1165 if( Client_Conn( c ) > NONE )
1167 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
1171 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1175 GLOBAL BOOLEAN IRC_USERHOST( CLIENT *Client, REQUEST *Req )
1177 CHAR rpl[COMMAND_LEN];
1181 assert( Client != NULL );
1182 assert( Req != NULL );
1184 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1186 /* Falsche Anzahl Parameter? */
1187 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1189 if( Req->argc > 5 ) max = 5;
1190 else max = Req->argc;
1192 strcpy( rpl, RPL_USERHOST_MSG );
1193 for( i = 0; i < max; i++ )
1195 c = Client_GetFromID( Req->argv[i] );
1196 if( c && ( Client_Type( c ) == CLIENT_USER ))
1198 /* Dieser Nick ist "online" */
1199 strcat( rpl, Client_ID( c ));
1200 if( Client_HasMode( c, 'o' )) strcat( rpl, "*" );
1202 if( Client_HasMode( c, 'a' )) strcat( rpl, "-" );
1203 else strcat( rpl, "+" );
1204 strcat( rpl, Client_User( c ));
1206 strcat( rpl, Client_Hostname( c ));
1210 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1212 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1213 } /* IRC_USERHOST */
1216 GLOBAL BOOLEAN IRC_ERROR( CLIENT *Client, REQUEST *Req )
1218 assert( Client != NULL );
1219 assert( Req != NULL );
1221 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
1222 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
1228 GLOBAL BOOLEAN IRC_LUSERS( CLIENT *Client, REQUEST *Req )
1230 CLIENT *target, *from;
1233 assert( Client != NULL );
1234 assert( Req != NULL );
1236 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1238 /* Falsche Anzahl Parameter? */
1239 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1241 /* Absender ermitteln */
1242 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1244 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1246 /* An anderen Server forwarden? */
1247 if( Req->argc == 2 )
1249 target = Client_GetFromID( Req->argv[1] );
1250 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
1251 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
1254 /* Wer ist der Absender? */
1255 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1256 else target = Client;
1257 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1259 /* Users, Services und Serevr im Netz */
1260 if( ! IRC_WriteStrClient( target, RPL_LUSERCLIENT_MSG, Client_ID( target ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1262 /* IRC-Operatoren im Netz */
1263 cnt = Client_OperCount( );
1266 if( ! IRC_WriteStrClient( target, RPL_LUSEROP_MSG, Client_ID( target ), cnt )) return DISCONNECTED;
1269 /* Unbekannt Verbindungen */
1270 cnt = Client_UnknownCount( );
1273 if( ! IRC_WriteStrClient( target, RPL_LUSERUNKNOWN_MSG, Client_ID( target ), cnt )) return DISCONNECTED;
1276 /* Channels im Netz */
1277 if( ! IRC_WriteStrClient( target, RPL_LUSERCHANNELS_MSG, Client_ID( target ), Channel_Count( ))) return DISCONNECTED;
1279 /* Channels im Netz */
1280 if( ! IRC_WriteStrClient( target, RPL_LUSERME_MSG, Client_ID( target ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1286 GLOBAL BOOLEAN IRC_LINKS( CLIENT *Client, REQUEST *Req )
1288 CLIENT *target, *from, *c;
1291 assert( Client != NULL );
1292 assert( Req != NULL );
1294 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1296 /* Falsche Anzahl Parameter? */
1297 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1299 /* Server-Mask ermitteln */
1300 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
1303 /* Absender ermitteln */
1304 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1306 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1308 /* An anderen Server forwarden? */
1309 if( Req->argc == 2 )
1311 target = Client_GetFromID( Req->argv[0] );
1312 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
1313 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
1316 /* Wer ist der Absender? */
1317 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1318 else target = Client;
1319 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1321 c = Client_First( );
1324 if( Client_Type( c ) == CLIENT_SERVER )
1326 if( ! IRC_WriteStrClient( target, RPL_LINKS_MSG, Client_ID( target ), Client_ID( c ), Client_ID( Client_Introducer( c )), Client_Hops( c ), Client_Info( c ))) return DISCONNECTED;
1328 c = Client_Next( c );
1331 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
1335 LOCAL BOOLEAN Hello_User( CLIENT *Client )
1337 assert( Client != NULL );
1339 /* Passwort ueberpruefen */
1340 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
1342 /* Falsches Passwort */
1343 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
1344 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
1345 return DISCONNECTED;
1348 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
1350 /* Andere Server informieren */
1351 IRC_WriteStrServers( NULL, "NICK %s 1 %s %s 1 +%s :%s", Client_ID( Client ), Client_User( Client ), Client_Hostname( Client ), Client_Modes( Client ), Client_Info( Client ));
1353 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
1354 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
1355 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
1356 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
1358 Client_SetType( Client, CLIENT_USER );
1360 return Show_MOTD( Client );
1364 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
1370 assert( Client != NULL );
1372 fd = fopen( Conf_MotdFile, "r" );
1375 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1376 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1379 IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1382 if( ! fgets( line, 126, fd )) break;
1383 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
1384 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
1390 ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
1398 LOCAL VOID Kill_Nick( CHAR *Nick )
1400 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected!", Nick );
1402 Log( LOG_ALERT, "[Kill_Nick() not implemented - OOOPS!]" );