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.39 2002/01/21 00:05:11 alex Exp $
17 * Revision 1.39 2002/01/21 00:05:11 alex
18 * - neue Funktionen IRC_JOIN und IRC_PART begonnen, ebenso die Funktionen
19 * IRC_WriteStrRelatedPrefix und IRC_WriteStrRelatedChannelPrefix().
20 * - diverse Aenderungen im Zusammenhang mit Channels.
22 * Revision 1.38 2002/01/18 15:31:32 alex
23 * - die User-Modes bei einem NICK von einem Server wurden falsch uebernommen.
25 * Revision 1.37 2002/01/16 22:10:18 alex
26 * - IRC_LUSERS() implementiert.
28 * Revision 1.36 2002/01/11 23:50:55 alex
29 * - LINKS implementiert, LUSERS begonnen.
31 * Revision 1.35 2002/01/09 21:30:45 alex
32 * - WHOIS wurde faelschlicherweise an User geforwarded statt vom Server beantwortet.
34 * Revision 1.34 2002/01/09 01:09:58 alex
35 * - WHOIS wird im "Strict-RFC-Mode" nicht mehr automatisch geforwarded,
36 * - andere Server werden nun ueber bisherige Server und User informiert.
38 * Revision 1.33 2002/01/07 23:42:12 alex
39 * - Es werden fuer alle Server eigene Token generiert,
40 * - QUIT von einem Server fuer einen User wird an andere Server geforwarded,
41 * - ebenso NICK-Befehle, die "fremde" User einfuehren.
43 * Revision 1.32 2002/01/07 16:02:36 alex
44 * - Loglevel von Remote-Mode-Aenderungen angepasst (nun Debug).
45 * - Im Debug-Mode werden nun auch PING's protokolliert.
47 * Revision 1.31 2002/01/07 15:39:46 alex
48 * - Server nimmt nun Server-Links an: PASS und SERVER entsprechend angepasst.
49 * - MODE und NICK melden nun die Aenderungen an andere Server.
51 * Revision 1.30 2002/01/06 15:21:29 alex
52 * - Loglevel und Meldungen nochmals ueberarbeitet.
53 * - QUIT und SQUIT forwarden nun den Grund der Trennung,
54 * - WHOIS wird nun immer an den "Original-Server" weitergeleitet.
56 * Revision 1.29 2002/01/05 23:24:54 alex
57 * - WHOIS erweitert: Anfragen koennen an andere Server weitergeleitet werden.
58 * - Vorbereitungen fuer Ident-Abfragen bei neuen Client-Strukturen.
60 * Revision 1.28 2002/01/05 20:08:02 alex
61 * - Div. Aenderungen fuer die Server-Links (u.a. WHOIS, QUIT, NICK angepasst).
62 * - Neue Funktionen IRC_WriteStrServer() und IRC_WriteStrServerPrefix().
64 * Revision 1.27 2002/01/05 19:15:03 alex
65 * - Fehlerpruefung bei select() in der "Hauptschleife" korrigiert.
67 * Revision 1.26 2002/01/05 16:51:18 alex
68 * - das Passwort von Servern wird nun ueberprueft (PASS- und SERVER-Befehl).
70 * Revision 1.25 2002/01/05 00:48:33 alex
71 * - bei SQUIT wurde immer die Verbindung getrennt, auch bei Remote-Servern.
73 * Revision 1.24 2002/01/04 17:58:44 alex
74 * - IRC_WriteStrXXX()-Funktionen eingefuehrt, groessere Anpassungen daran.
75 * - neuer Befehl SQUIT, QUIT an Server-Links angepasst.
77 * Revision 1.23 2002/01/04 01:36:40 alex
78 * - Loglevel ein wenig angepasst.
80 * Revision 1.22 2002/01/04 01:21:47 alex
81 * - Client-Strukruren werden nur noch ueber Funktionen angesprochen.
82 * - Weitere Anpassungen und Erweiterungen der Server-Links.
84 * Revision 1.21 2002/01/03 02:26:51 alex
85 * - neue Befehle SERVER und NJOIN begonnen,
86 * - begonnen, diverse IRC-Befehle an Server-Links anzupassen.
88 * Revision 1.20 2002/01/02 12:46:41 alex
89 * - die Gross- und Kleinschreibung des Nicks kann mit NICK nun geaendert werden.
91 * Revision 1.19 2002/01/02 02:51:39 alex
92 * - Copyright-Texte angepasst.
93 * - neuer Befehl "ERROR".
95 * Revision 1.17 2001/12/31 15:33:13 alex
96 * - neuer Befehl NAMES, kleinere Bugfixes.
97 * - Bug bei PING behoben: war zu restriktiv implementiert :-)
99 * Revision 1.16 2001/12/31 02:18:51 alex
100 * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
101 * - neuen Header "defines.h" mit (fast) allen Konstanten.
102 * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
104 * Revision 1.15 2001/12/30 19:26:11 alex
105 * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
107 * Revision 1.14 2001/12/30 11:42:00 alex
108 * - der Server meldet nun eine ordentliche "Start-Zeit".
110 * Revision 1.13 2001/12/29 03:10:06 alex
111 * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
112 * - neue configure-Optione "--enable-strict-rfc".
114 * Revision 1.12 2001/12/27 19:17:26 alex
115 * - neue Befehle PRIVMSG, NOTICE, PING.
117 * Revision 1.11 2001/12/27 16:55:41 alex
118 * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
120 * Revision 1.10 2001/12/26 22:48:53 alex
121 * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
123 * Revision 1.9 2001/12/26 14:45:37 alex
126 * Revision 1.8 2001/12/26 03:21:46 alex
127 * - PING/PONG-Befehle implementiert,
128 * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
130 * Revision 1.7 2001/12/25 23:25:18 alex
131 * - und nochmal Aenderungen am Logging ;-)
133 * Revision 1.6 2001/12/25 23:13:33 alex
134 * - Debug-Meldungen angepasst.
136 * Revision 1.5 2001/12/25 22:02:42 alex
137 * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
139 * Revision 1.4 2001/12/25 19:19:30 alex
140 * - bessere Fehler-Abfragen, diverse Bugfixes.
141 * - Nicks werden nur einmal vergeben :-)
142 * - /MOTD wird unterstuetzt.
144 * Revision 1.3 2001/12/24 01:34:06 alex
145 * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
146 * - MOTD-Ausgabe begonnen zu implementieren.
148 * Revision 1.2 2001/12/23 21:57:16 alex
149 * - erste IRC-Befehle zu implementieren begonnen.
151 * Revision 1.1 2001/12/14 08:13:43 alex
152 * - neues Modul begonnen :-)
173 #include "messages.h"
181 #define CONNECTED TRUE
182 #define DISCONNECTED FALSE
185 LOCAL BOOLEAN Hello_User( CLIENT *Client );
186 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
188 LOCAL VOID Kill_Nick( CHAR *Nick );
191 GLOBAL VOID IRC_Init( VOID )
196 GLOBAL VOID IRC_Exit( VOID )
201 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CHAR *Format, ... )
204 BOOLEAN ok = CONNECTED;
207 assert( Client != NULL );
208 assert( Format != NULL );
210 va_start( ap, Format );
211 vsnprintf( buffer, 1000, Format, ap );
214 /* an den Client selber */
215 ok = IRC_WriteStrClientPrefix( Client, Client_ThisServer( ), buffer );
218 } /* IRC_WriteStrClient */
221 GLOBAL BOOLEAN IRC_WriteStrClientPrefix( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
223 /* Text an Clients, lokal bzw. remote, senden. */
228 assert( Client != NULL );
229 assert( Format != NULL );
230 assert( Prefix != NULL );
232 va_start( ap, Format );
233 vsnprintf( buffer, 1000, Format, ap );
236 return Conn_WriteStr( Client_Conn( Client_NextHop( Client )), ":%s %s", Client_Mask( Prefix ), buffer );
237 } /* IRC_WriteStrClientPrefix */
240 GLOBAL BOOLEAN IRC_WriteStrRelated( CLIENT *Client, CHAR *Format, ... )
245 assert( Client != NULL );
246 assert( Format != NULL );
248 va_start( ap, Format );
249 vsnprintf( buffer, 1000, Format, ap );
252 return IRC_WriteStrRelatedPrefix( Client, Client_ThisServer( ), buffer );
253 } /* IRC_WriteStrRelated */
256 GLOBAL BOOLEAN IRC_WriteStrRelatedPrefix( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
261 assert( Client != NULL );
262 assert( Format != NULL );
264 va_start( ap, Format );
265 vsnprintf( buffer, 1000, Format, ap );
268 return IRC_WriteStrRelatedChannelPrefix( Client, NULL, Client_ThisServer( ), buffer );
269 } /* IRC_WriteStrRelatedPrefix */
272 GLOBAL BOOLEAN IRC_WriteStrRelatedChannelPrefix( CLIENT *Client, CHAR *ChanName, CLIENT *Prefix, CHAR *Format, ... )
275 BOOLEAN ok = CONNECTED;
278 assert( Client != NULL );
279 assert( Format != NULL );
280 assert( Prefix != NULL );
282 va_start( ap, Format );
283 vsnprintf( buffer, 1000, Format, ap );
286 /* An den Client selber, wenn User */
287 if( Client_Type( Client ) == CLIENT_USER ) ok = IRC_WriteStrClientPrefix( Client, Prefix, buffer );
289 /* An alle Clients, die in den selben Channels sind.
290 * Dabei aber nur einmal je Remote-Server */
293 } /* IRC_WriteStrRelatedChannelPrefix */
296 GLOBAL VOID IRC_WriteStrServers( CLIENT *ExceptOf, CHAR *Format, ... )
301 assert( Format != NULL );
303 va_start( ap, Format );
304 vsnprintf( buffer, 1000, Format, ap );
307 /* an den Client selber */
308 return IRC_WriteStrServersPrefix( ExceptOf, Client_ThisServer( ), buffer );
309 } /* IRC_WriteStrServers */
312 GLOBAL VOID IRC_WriteStrServersPrefix( CLIENT *ExceptOf, CLIENT *Prefix, CHAR *Format, ... )
318 assert( Format != NULL );
319 assert( Prefix != NULL );
321 va_start( ap, Format );
322 vsnprintf( buffer, 1000, Format, ap );
328 if(( Client_Type( c ) == CLIENT_SERVER ) && ( Client_Conn( c ) > NONE ) && ( c != Client_ThisServer( )) && ( c != ExceptOf ))
330 /* Ziel-Server gefunden */
331 IRC_WriteStrClientPrefix( c, Prefix, buffer );
333 c = Client_Next( c );
335 } /* IRC_WriteStrServersPrefix */
338 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
340 assert( Client != NULL );
341 assert( Req != NULL );
343 /* Fehler liefern, wenn kein lokaler Client */
344 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
346 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
348 /* noch nicht registrierte unbekannte Verbindung */
349 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
351 /* Passwort speichern */
352 Client_SetPassword( Client, Req->argv[0] );
354 Client_SetType( Client, CLIENT_GOTPASS );
357 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
359 /* noch nicht registrierte Server-Verbindung */
360 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
362 /* Passwort speichern */
363 Client_SetPassword( Client, Req->argv[0] );
365 Client_SetType( Client, CLIENT_GOTPASSSERVER );
368 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
370 /* Falsche Anzahl Parameter? */
371 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
373 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
377 GLOBAL BOOLEAN IRC_SERVER( CLIENT *Client, REQUEST *Req )
379 CHAR str[LINE_LEN], *ptr;
384 assert( Client != NULL );
385 assert( Req != NULL );
387 /* Fehler liefern, wenn kein lokaler Client */
388 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
390 if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
392 /* Verbindung soll als Server-Server-Verbindung registriert werden */
393 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
395 /* Falsche Anzahl Parameter? */
396 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
398 /* Ist dieser Server bei uns konfiguriert? */
399 for( i = 0; i < Conf_Server_Count; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
400 if( i >= Conf_Server_Count )
402 /* Server ist nicht konfiguriert! */
403 Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
404 Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", TRUE );
407 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd ) != 0 )
409 /* Falsches Passwort */
410 Log( LOG_ERR, "Connection %d: Bad password for server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
411 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
415 /* Ist ein Server mit dieser ID bereits registriert? */
416 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
418 /* Server-Strukturen fuellen ;-) */
419 Client_SetID( Client, Req->argv[0] );
420 Client_SetHops( Client, 1 );
421 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
423 /* Meldet sich der Server bei uns an? */
426 /* Unseren SERVER- und PASS-Befehl senden */
428 if( ! IRC_WriteStrClient( Client, "PASS %s "PASSSERVERADD, Conf_Server[i].pwd )) ok = FALSE;
429 else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
432 Conn_Close( Client_Conn( Client ), "Unexpected server behavior!", NULL, FALSE );
435 Client_SetIntroducer( Client, Client );
436 Client_SetToken( Client, 1 );
438 else Client_SetToken( Client, atoi( Req->argv[1] ));
440 Log( LOG_NOTICE, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), Client_Conn( Client ));
442 Client_SetType( Client, CLIENT_SERVER );
444 /* Alle bisherigen Server dem neuen Server bekannt machen,
445 * die bisherigen Server ueber den neuen informierenn */
449 if(( Client_Type( c ) == CLIENT_SERVER ) && ( c != Client ) && ( c != Client_ThisServer( )))
451 if( Client_Conn( c ) > NONE )
453 /* Dem gefundenen Server gleich den neuen
454 * Server bekannt machen */
455 if( ! IRC_WriteStrClient( c, "SERVER %s %d %d :%s", Client_ID( Client ), Client_Hops( Client ) + 1, Client_MyToken( Client ), Client_Info( Client ))) return DISCONNECTED;
458 /* Den neuen Server ueber den alten informieren */
459 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;
461 c = Client_Next( c );
464 /* alle User dem neuen Server bekannt machen */
468 if( Client_Type( c ) == CLIENT_USER )
470 /* User an neuen Server melden */
471 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;
473 c = Client_Next( c );
478 else if( Client_Type( Client ) == CLIENT_SERVER )
480 /* Neuer Server wird im Netz angekuendigt */
482 /* Falsche Anzahl Parameter? */
483 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
485 /* Ist ein Server mit dieser ID bereits registriert? */
486 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
488 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
489 ptr = strchr( Req->argv[3] + 2, '[' );
490 if( ! ptr ) ptr = Req->argv[3];
492 /* Neue Client-Struktur anlegen */
493 c = Client_NewRemoteServer( Client, Req->argv[0], atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, TRUE );
496 /* Neue Client-Struktur konnte nicht angelegt werden */
497 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
498 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", TRUE );
502 /* Log-Meldung zusammenbauen und ausgeben */
503 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) sprintf( str, "connected to %s, ", Req->prefix );
504 else strcpy( str, "" );
505 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": "" );
509 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
513 GLOBAL BOOLEAN IRC_NJOIN( CLIENT *Client, REQUEST *Req )
515 assert( Client != NULL );
516 assert( Req != NULL );
518 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTEREDSERVER_MSG, Client_ID( Client ));
524 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
526 CLIENT *intr_c, *target, *c;
529 assert( Client != NULL );
530 assert( Req != NULL );
532 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
534 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 ))
536 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 ))
539 /* User-Registrierung bzw. Nick-Aenderung */
541 /* Falsche Anzahl Parameter? */
542 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
544 /* "Ziel-Client" ermitteln */
545 if( Client_Type( Client ) == CLIENT_SERVER )
547 target = Client_GetFromID( Req->prefix );
548 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
552 /* Ist der Client "restricted"? */
553 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
557 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
558 * wir nichts. So macht es das Original und mind. Snak hat probleme,
559 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
561 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
564 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
565 * will nur die Gross- und Kleinschreibung aendern. Das darf
566 * er natuerlich machen :-) */
567 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
569 if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
572 if( Client_Type( Client ) == CLIENT_USER )
574 /* Nick-Aenderung: allen mitteilen! */
575 Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
576 IRC_WriteStrRelated( Client, "NICK :%s", Req->argv[0] );
577 IRC_WriteStrServersPrefix( NULL, Client, "NICK :%s", Req->argv[0] );
579 else if( Client_Type( Client ) == CLIENT_SERVER )
581 /* Nick-Aenderung: allen mitteilen! */
582 Log( LOG_DEBUG, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
583 IRC_WriteStrServersPrefix( Client, Client, "NICK :%s", Req->argv[0] );
586 /* Client-Nick registrieren */
587 Client_SetID( target, Req->argv[0] );
589 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
592 Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client_Conn( Client ));
593 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
594 else Client_SetType( Client, CLIENT_GOTNICK );
598 else if( Client_Type( Client ) == CLIENT_SERVER )
600 /* Server fuehrt neuen Client ein */
602 /* Falsche Anzahl Parameter? */
603 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
605 /* Nick ueberpruefen */
606 c = Client_GetFromID( Req->argv[0] );
609 /* Der neue Nick ist auf diesem Server bereits registriert:
610 * sowohl der neue, als auch der alte Client muessen nun
611 * disconnectiert werden. */
612 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
613 Kill_Nick( Req->argv[0] );
617 /* Server, zu dem der Client connectiert ist, suchen */
618 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
621 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
622 Kill_Nick( Req->argv[0] );
626 /* Neue Client-Struktur anlegen */
627 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 );
630 /* Eine neue Client-Struktur konnte nicht angelegt werden.
631 * Der Client muss disconnectiert werden, damit der Netz-
632 * status konsistent bleibt. */
633 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
634 Kill_Nick( Req->argv[0] );
638 modes = Client_Modes( c );
639 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": "" );
640 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": "" );
642 /* Andere Server, ausser dem Introducer, informieren */
643 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] );
647 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
651 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
653 assert( Client != NULL );
654 assert( Req != NULL );
657 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
659 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
662 /* Falsche Anzahl Parameter? */
663 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
665 Client_SetUser( Client, Req->argv[0], FALSE );
666 Client_SetInfo( Client, Req->argv[3] );
668 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client_Conn( Client ));
669 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
670 else Client_SetType( Client, CLIENT_GOTUSER );
673 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
675 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
677 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
681 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
685 assert( Client != NULL );
686 assert( Req != NULL );
688 if(( Client_Type( Client ) == CLIENT_USER ) || ( Client_Type( Client ) == CLIENT_SERVICE ))
692 /* Falsche Anzahl Parameter? */
693 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
695 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
696 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
700 else if ( Client_Type( Client ) == CLIENT_SERVER )
704 /* Falsche Anzahl Parameter? */
705 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
707 target = Client_Search( Req->prefix );
710 Log( LOG_ERR, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
714 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL );
715 else Client_Destroy( target, "Got QUIT command.", Req->argv[0] );
719 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
723 GLOBAL BOOLEAN IRC_SQUIT( CLIENT *Client, REQUEST *Req )
727 assert( Client != NULL );
728 assert( Req != NULL );
730 /* SQUIT ist nur fuer Server erlaubt */
731 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
733 /* Falsche Anzahl Parameter? */
734 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
736 target = Client_GetFromID( Req->argv[0] );
739 Log( LOG_ERR, "Got SQUIT from %s for unknown server \%s\"!?", Client_ID( Client ), Req->argv[0] );
743 if( target == Client ) Log( LOG_DEBUG, "Got SQUIT from %s: %s", Client_ID( Client ), Req->argv[1] );
744 else Log( LOG_DEBUG, "Got SQUIT from %s for %s: %s", Client_ID( Client ), Client_ID( target ), Req->argv[1] );
746 /* SQUIT an alle Server weiterleiten */
747 IRC_WriteStrServers( Client, "SQUIT %s :%s", Req->argv[0], Req->argv[1] );
749 if( Client_Conn( target ) > NONE )
751 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), "Got SQUIT command.", Req->argv[1], TRUE );
752 else Conn_Close( Client_Conn( target ), "Got SQUIT command.", NULL, TRUE );
757 Client_Destroy( target, "Got SQUIT command.", Req->argv[1] );
763 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
765 assert( Client != NULL );
766 assert( Req != NULL );
768 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
770 /* Falsche Anzahl Parameter? */
771 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
772 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
774 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
775 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
779 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
781 assert( Client != NULL );
782 assert( Req != NULL );
784 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
786 /* Falsche Anzahl Parameter? */
787 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
788 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
790 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
791 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
793 Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
798 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
800 assert( Client != NULL );
801 assert( Req != NULL );
803 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
805 /* Falsche Anzahl Parameter? */
806 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
808 return Show_MOTD( Client );
812 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
816 assert( Client != NULL );
817 assert( Req != NULL );
819 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
821 /* Falsche Anzahl Parameter? */
822 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
823 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
824 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
826 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
828 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
830 to = Client_Search( Req->argv[0] );
833 /* Okay, Ziel ist ein User */
834 if( Client_Conn( from ) >= 0 ) Conn_UpdateIdle( Client_Conn( from ));
835 return IRC_WriteStrClientPrefix( to, from, "PRIVMSG %s :%s", Client_ID( to ), Req->argv[1] );
837 else return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
841 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
845 assert( Client != NULL );
846 assert( Req != NULL );
848 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
850 /* Falsche Anzahl Parameter? */
851 if( Req->argc != 2 ) return CONNECTED;
853 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
855 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
857 to = Client_Search( Req->argv[0] );
860 /* Okay, Ziel ist ein User */
861 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
863 else return CONNECTED;
867 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
869 CHAR x[2], new_modes[CLIENT_MODE_LEN], *ptr;
873 assert( Client != NULL );
874 assert( Req != NULL );
876 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
878 /* Falsche Anzahl Parameter? */
879 if(( Req->argc > 2 ) || ( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
881 /* "Ziel-Client" suchen */
882 target = Client_Search( Req->argv[0] );
884 /* Wer ist der Anfragende? */
885 if( Client_Type( Client ) == CLIENT_USER )
887 /* User: MODE ist nur fuer sich selber zulaessig */
888 if( target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
892 /* Server: gibt es den Client ueberhaupt? */
893 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
896 /* Werden die Modes erfragt? */
897 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( Client ));
901 /* Sollen Modes gesetzt oder geloescht werden? */
902 if( *ptr == '+' ) set = TRUE;
903 else if( *ptr == '-' ) set = FALSE;
904 else return IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
906 /* Reply-String mit Aenderungen vorbereiten */
907 if( set ) strcpy( new_modes, "+" );
908 else strcpy( new_modes, "-" );
916 if( Client_Type( Client ) == CLIENT_SERVER )
930 /* restricted (kann nur gesetzt werden) */
931 if( set ) x[0] = 'r';
932 else ok = IRC_WriteStrClient( target, ERR_RESTRICTED_MSG, Client_ID( target ));
935 /* operator (kann nur geloescht werden) */
938 Client_SetOperByMe( target, FALSE );
941 else ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
944 ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
951 if( ! x[0] ) continue;
953 /* Okay, gueltigen Mode gefunden */
956 /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
957 if( Client_ModeAdd( target, x[0] )) strcat( new_modes, x );
961 /* Modes geloescht. Wenn der Client ihn hatte: merken */
962 if( Client_ModeDel( target, x[0] )) strcat( new_modes, x );
966 /* Geanderte Modes? */
969 if( Client_Type( Client ) == CLIENT_SERVER )
971 /* Modes an andere Server forwarden */
972 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :%s", Client_ID( target ), new_modes );
976 /* Bestaetigung an Client schicken & andere Server informieren */
977 ok = IRC_WriteStrClient( Client, "MODE %s :%s", Client_ID( target ), new_modes );
978 IRC_WriteStrServers( Client, "MODE %s :%s", Client_ID( target ), new_modes );
980 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( target ), Client_Modes( target ));
986 GLOBAL BOOLEAN IRC_OPER( CLIENT *Client, REQUEST *Req )
990 assert( Client != NULL );
991 assert( Req != NULL );
993 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
995 /* Falsche Anzahl Parameter? */
996 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
998 /* Operator suchen */
999 for( i = 0; i < Conf_Oper_Count; i++)
1001 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
1003 if( i >= Conf_Oper_Count )
1005 Log( LOG_WARNING, "Got invalid OPER from \"%s\": Name \"%s\" not configured!", Client_Mask( Client ), Req->argv[0] );
1006 return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
1009 /* Stimmt das Passwort? */
1010 if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
1012 Log( LOG_WARNING, "Got invalid OPER from \"%s\": Bad password for \"%s\"!", Client_Mask( Client ), Conf_Oper[i].name );
1013 return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
1016 if( ! Client_HasMode( Client, 'o' ))
1018 /* noch kein o-Mode gesetzt */
1019 Client_ModeAdd( Client, 'o' );
1020 if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
1021 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
1024 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
1026 Client_SetOperByMe( Client, TRUE );
1027 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
1031 GLOBAL BOOLEAN IRC_DIE( CLIENT *Client, REQUEST *Req )
1033 assert( Client != NULL );
1034 assert( Req != NULL );
1036 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1038 /* Falsche Anzahl Parameter? */
1039 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1041 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
1043 Log( LOG_NOTICE, "Got DIE command from \"%s\", going down!", Client_Mask( Client ));
1049 GLOBAL BOOLEAN IRC_RESTART( CLIENT *Client, REQUEST *Req )
1051 assert( Client != NULL );
1052 assert( Req != NULL );
1054 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1056 /* Falsche Anzahl Parameter? */
1057 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1059 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
1061 Log( LOG_NOTICE, "Got RESTART command from \"%s\", going down!", Client_Mask( Client ));
1062 NGIRCd_Restart = TRUE;
1067 GLOBAL BOOLEAN IRC_NAMES( CLIENT *Client, REQUEST *Req )
1069 CHAR rpl[COMMAND_LEN];
1072 assert( Client != NULL );
1073 assert( Req != NULL );
1075 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1077 /* Falsche Anzahl Parameter? */
1078 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1080 /* Noch alle User ausgeben, die in keinem Channel sind */
1082 c = Client_First( );
1085 if( Client_Type( c ) == CLIENT_USER )
1087 /* Okay, das ist ein User */
1088 strcat( rpl, Client_ID( c ));
1092 /* Antwort zu lang? Splitten. */
1093 if( strlen( rpl ) > 480 )
1095 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1096 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
1100 c = Client_Next( c );
1104 /* es wurden User gefunden */
1105 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1106 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
1108 return IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), "*" );
1112 GLOBAL BOOLEAN IRC_ISON( CLIENT *Client, REQUEST *Req )
1114 CHAR rpl[COMMAND_LEN];
1119 assert( Client != NULL );
1120 assert( Req != NULL );
1122 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1124 /* Falsche Anzahl Parameter? */
1125 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1127 strcpy( rpl, RPL_ISON_MSG );
1128 for( i = 0; i < Req->argc; i++ )
1130 ptr = strtok( Req->argv[i], " " );
1134 c = Client_GetFromID( ptr );
1135 if( c && ( Client_Type( c ) == CLIENT_USER ))
1137 /* Dieser Nick ist "online" */
1141 ptr = strtok( NULL, " " );
1144 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1146 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1150 GLOBAL BOOLEAN IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1152 CLIENT *from, *target, *c;
1155 assert( Client != NULL );
1156 assert( Req != NULL );
1158 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1160 /* Falsche Anzahl Parameter? */
1161 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1164 c = Client_GetFromID( Req->argv[Req->argc - 1] );
1165 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
1167 /* Empfaenger des WHOIS suchen */
1168 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1170 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1172 /* Forwarden an anderen Server? */
1175 /* angegebenen Ziel-Server suchen */
1176 target = Client_GetFromID( Req->argv[1] );
1177 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
1181 else if( Client_Conn( c ) == NONE )
1183 /* Client ist nicht von uns. Ziel-Server suchen */
1190 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 );
1192 /* Nick, User und Name */
1193 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
1196 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;
1199 if( Client_HasMode( c, 'o' ))
1201 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
1204 /* Idle (nur lokale Clients) */
1205 if( Client_Conn( c ) > NONE )
1207 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
1211 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1215 GLOBAL BOOLEAN IRC_USERHOST( CLIENT *Client, REQUEST *Req )
1217 CHAR rpl[COMMAND_LEN];
1221 assert( Client != NULL );
1222 assert( Req != NULL );
1224 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1226 /* Falsche Anzahl Parameter? */
1227 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1229 if( Req->argc > 5 ) max = 5;
1230 else max = Req->argc;
1232 strcpy( rpl, RPL_USERHOST_MSG );
1233 for( i = 0; i < max; i++ )
1235 c = Client_GetFromID( Req->argv[i] );
1236 if( c && ( Client_Type( c ) == CLIENT_USER ))
1238 /* Dieser Nick ist "online" */
1239 strcat( rpl, Client_ID( c ));
1240 if( Client_HasMode( c, 'o' )) strcat( rpl, "*" );
1242 if( Client_HasMode( c, 'a' )) strcat( rpl, "-" );
1243 else strcat( rpl, "+" );
1244 strcat( rpl, Client_User( c ));
1246 strcat( rpl, Client_Hostname( c ));
1250 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1252 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1253 } /* IRC_USERHOST */
1256 GLOBAL BOOLEAN IRC_ERROR( CLIENT *Client, REQUEST *Req )
1258 assert( Client != NULL );
1259 assert( Req != NULL );
1261 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
1262 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
1268 GLOBAL BOOLEAN IRC_LUSERS( CLIENT *Client, REQUEST *Req )
1270 CLIENT *target, *from;
1273 assert( Client != NULL );
1274 assert( Req != NULL );
1276 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1278 /* Falsche Anzahl Parameter? */
1279 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1281 /* Absender ermitteln */
1282 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1284 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1286 /* An anderen Server forwarden? */
1287 if( Req->argc == 2 )
1289 target = Client_GetFromID( Req->argv[1] );
1290 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
1291 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
1294 /* Wer ist der Absender? */
1295 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1296 else target = Client;
1297 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1299 /* Users, Services und Serevr im Netz */
1300 if( ! IRC_WriteStrClient( target, RPL_LUSERCLIENT_MSG, Client_ID( target ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1302 /* IRC-Operatoren im Netz */
1303 cnt = Client_OperCount( );
1306 if( ! IRC_WriteStrClient( target, RPL_LUSEROP_MSG, Client_ID( target ), cnt )) return DISCONNECTED;
1309 /* Unbekannt Verbindungen */
1310 cnt = Client_UnknownCount( );
1313 if( ! IRC_WriteStrClient( target, RPL_LUSERUNKNOWN_MSG, Client_ID( target ), cnt )) return DISCONNECTED;
1316 /* Channels im Netz */
1317 if( ! IRC_WriteStrClient( target, RPL_LUSERCHANNELS_MSG, Client_ID( target ), Channel_Count( ))) return DISCONNECTED;
1319 /* Channels im Netz */
1320 if( ! IRC_WriteStrClient( target, RPL_LUSERME_MSG, Client_ID( target ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1326 GLOBAL BOOLEAN IRC_LINKS( CLIENT *Client, REQUEST *Req )
1328 CLIENT *target, *from, *c;
1331 assert( Client != NULL );
1332 assert( Req != NULL );
1334 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1336 /* Falsche Anzahl Parameter? */
1337 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1339 /* Server-Mask ermitteln */
1340 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
1343 /* Absender ermitteln */
1344 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1346 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1348 /* An anderen Server forwarden? */
1349 if( Req->argc == 2 )
1351 target = Client_GetFromID( Req->argv[0] );
1352 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
1353 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
1356 /* Wer ist der Absender? */
1357 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1358 else target = Client;
1359 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1361 c = Client_First( );
1364 if( Client_Type( c ) == CLIENT_SERVER )
1366 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;
1368 c = Client_Next( c );
1371 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
1375 GLOBAL BOOLEAN IRC_JOIN( CLIENT *Client, REQUEST *Req )
1380 assert( Client != NULL );
1381 assert( Req != NULL );
1383 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1385 /* Falsche Anzahl Parameter? */
1386 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1388 /* Wer ist der Absender? */
1389 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1390 else target = Client;
1391 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1393 /* Channel-Namen durchgehen */
1394 chan = strtok( Req->argv[0], "," );
1397 if( ! Channel_Join( target, chan ))
1399 /* naechsten Namen ermitteln */
1400 chan = strtok( NULL, "," );
1404 /* An andere Server weiterleiten, an Client bestaetigen */
1405 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s", chan );
1406 IRC_WriteStrRelatedChannelPrefix( Client, chan, target, "JOIN :%s", chan );
1408 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( target ), Req->argv[0] );
1410 /* naechsten Namen ermitteln */
1411 chan = strtok( NULL, "," );
1417 GLOBAL BOOLEAN IRC_PART( CLIENT *Client, REQUEST *Req )
1422 assert( Client != NULL );
1423 assert( Req != NULL );
1425 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1427 /* Falsche Anzahl Parameter? */
1428 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1430 /* Wer ist der Absender? */
1431 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1432 else target = Client;
1433 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1435 /* Channel-Namen durchgehen */
1436 chan = strtok( Req->argv[0], "," );
1439 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
1441 /* naechsten Namen ermitteln */
1442 chan = strtok( NULL, "," );
1446 /* naechsten Namen ermitteln */
1447 chan = strtok( NULL, "," );
1453 LOCAL BOOLEAN Hello_User( CLIENT *Client )
1455 assert( Client != NULL );
1457 /* Passwort ueberpruefen */
1458 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
1460 /* Falsches Passwort */
1461 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
1462 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
1463 return DISCONNECTED;
1466 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
1468 /* Andere Server informieren */
1469 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 ));
1471 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
1472 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
1473 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
1474 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
1476 Client_SetType( Client, CLIENT_USER );
1478 return Show_MOTD( Client );
1482 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
1488 assert( Client != NULL );
1490 fd = fopen( Conf_MotdFile, "r" );
1493 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1494 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1497 IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1500 if( ! fgets( line, 126, fd )) break;
1501 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
1502 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
1508 ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
1516 LOCAL VOID Kill_Nick( CHAR *Nick )
1518 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected!", Nick );
1520 Log( LOG_ALERT, "[Kill_Nick() not implemented - OOOPS!]" );