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.43 2002/01/27 21:56:39 alex Exp $
17 * Revision 1.43 2002/01/27 21:56:39 alex
18 * - IRC_WriteStrServersPrefixID() und IRC_WriteStrClientPrefixID() wieder entfernt.
19 * - einige kleinere Fixes bezueglich Channels ...
21 * Revision 1.42 2002/01/27 18:28:01 alex
22 * - bei NICK wurde das falsche Prefix an andere Server weitergegeben.
24 * Revision 1.41 2002/01/27 17:15:49 alex
25 * - anderungen an den Funktions-Prototypen von IRC_WriteStrChannel() und
26 * IRC_WriteStrChannelPrefix(),
27 * - neue: IRC_WriteStrClientPrefixID() und IRC_WriteStrServersPrefixID().
29 * Revision 1.40 2002/01/26 18:43:11 alex
30 * - neue Funktionen IRC_WriteStrChannelPrefix() und IRC_WriteStrChannel(),
31 * die IRC_Write_xxx_Related() sind dafuer entfallen.
32 * - IRC_PRIVMSG() kann nun auch mit Channels als Ziel umgehen.
34 * Revision 1.39 2002/01/21 00:05:11 alex
35 * - neue Funktionen IRC_JOIN und IRC_PART begonnen, ebenso die Funktionen
36 * IRC_WriteStrRelatedPrefix und IRC_WriteStrRelatedChannelPrefix().
37 * - diverse Aenderungen im Zusammenhang mit Channels.
39 * Revision 1.38 2002/01/18 15:31:32 alex
40 * - die User-Modes bei einem NICK von einem Server wurden falsch uebernommen.
42 * Revision 1.37 2002/01/16 22:10:18 alex
43 * - IRC_LUSERS() implementiert.
45 * Revision 1.36 2002/01/11 23:50:55 alex
46 * - LINKS implementiert, LUSERS begonnen.
48 * Revision 1.35 2002/01/09 21:30:45 alex
49 * - WHOIS wurde faelschlicherweise an User geforwarded statt vom Server beantwortet.
51 * Revision 1.34 2002/01/09 01:09:58 alex
52 * - WHOIS wird im "Strict-RFC-Mode" nicht mehr automatisch geforwarded,
53 * - andere Server werden nun ueber bisherige Server und User informiert.
55 * Revision 1.33 2002/01/07 23:42:12 alex
56 * - Es werden fuer alle Server eigene Token generiert,
57 * - QUIT von einem Server fuer einen User wird an andere Server geforwarded,
58 * - ebenso NICK-Befehle, die "fremde" User einfuehren.
60 * Revision 1.32 2002/01/07 16:02:36 alex
61 * - Loglevel von Remote-Mode-Aenderungen angepasst (nun Debug).
62 * - Im Debug-Mode werden nun auch PING's protokolliert.
64 * Revision 1.31 2002/01/07 15:39:46 alex
65 * - Server nimmt nun Server-Links an: PASS und SERVER entsprechend angepasst.
66 * - MODE und NICK melden nun die Aenderungen an andere Server.
68 * Revision 1.30 2002/01/06 15:21:29 alex
69 * - Loglevel und Meldungen nochmals ueberarbeitet.
70 * - QUIT und SQUIT forwarden nun den Grund der Trennung,
71 * - WHOIS wird nun immer an den "Original-Server" weitergeleitet.
73 * Revision 1.29 2002/01/05 23:24:54 alex
74 * - WHOIS erweitert: Anfragen koennen an andere Server weitergeleitet werden.
75 * - Vorbereitungen fuer Ident-Abfragen bei neuen Client-Strukturen.
77 * Revision 1.28 2002/01/05 20:08:02 alex
78 * - Div. Aenderungen fuer die Server-Links (u.a. WHOIS, QUIT, NICK angepasst).
79 * - Neue Funktionen IRC_WriteStrServer() und IRC_WriteStrServerPrefix().
81 * Revision 1.27 2002/01/05 19:15:03 alex
82 * - Fehlerpruefung bei select() in der "Hauptschleife" korrigiert.
84 * Revision 1.26 2002/01/05 16:51:18 alex
85 * - das Passwort von Servern wird nun ueberprueft (PASS- und SERVER-Befehl).
87 * Revision 1.25 2002/01/05 00:48:33 alex
88 * - bei SQUIT wurde immer die Verbindung getrennt, auch bei Remote-Servern.
90 * Revision 1.24 2002/01/04 17:58:44 alex
91 * - IRC_WriteStrXXX()-Funktionen eingefuehrt, groessere Anpassungen daran.
92 * - neuer Befehl SQUIT, QUIT an Server-Links angepasst.
94 * Revision 1.23 2002/01/04 01:36:40 alex
95 * - Loglevel ein wenig angepasst.
97 * Revision 1.22 2002/01/04 01:21:47 alex
98 * - Client-Strukruren werden nur noch ueber Funktionen angesprochen.
99 * - Weitere Anpassungen und Erweiterungen der Server-Links.
101 * Revision 1.21 2002/01/03 02:26:51 alex
102 * - neue Befehle SERVER und NJOIN begonnen,
103 * - begonnen, diverse IRC-Befehle an Server-Links anzupassen.
105 * Revision 1.20 2002/01/02 12:46:41 alex
106 * - die Gross- und Kleinschreibung des Nicks kann mit NICK nun geaendert werden.
108 * Revision 1.19 2002/01/02 02:51:39 alex
109 * - Copyright-Texte angepasst.
110 * - neuer Befehl "ERROR".
112 * Revision 1.17 2001/12/31 15:33:13 alex
113 * - neuer Befehl NAMES, kleinere Bugfixes.
114 * - Bug bei PING behoben: war zu restriktiv implementiert :-)
116 * Revision 1.16 2001/12/31 02:18:51 alex
117 * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
118 * - neuen Header "defines.h" mit (fast) allen Konstanten.
119 * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
121 * Revision 1.15 2001/12/30 19:26:11 alex
122 * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
124 * Revision 1.14 2001/12/30 11:42:00 alex
125 * - der Server meldet nun eine ordentliche "Start-Zeit".
127 * Revision 1.13 2001/12/29 03:10:06 alex
128 * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
129 * - neue configure-Optione "--enable-strict-rfc".
131 * Revision 1.12 2001/12/27 19:17:26 alex
132 * - neue Befehle PRIVMSG, NOTICE, PING.
134 * Revision 1.11 2001/12/27 16:55:41 alex
135 * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
137 * Revision 1.10 2001/12/26 22:48:53 alex
138 * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
140 * Revision 1.9 2001/12/26 14:45:37 alex
143 * Revision 1.8 2001/12/26 03:21:46 alex
144 * - PING/PONG-Befehle implementiert,
145 * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
147 * Revision 1.7 2001/12/25 23:25:18 alex
148 * - und nochmal Aenderungen am Logging ;-)
150 * Revision 1.6 2001/12/25 23:13:33 alex
151 * - Debug-Meldungen angepasst.
153 * Revision 1.5 2001/12/25 22:02:42 alex
154 * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
156 * Revision 1.4 2001/12/25 19:19:30 alex
157 * - bessere Fehler-Abfragen, diverse Bugfixes.
158 * - Nicks werden nur einmal vergeben :-)
159 * - /MOTD wird unterstuetzt.
161 * Revision 1.3 2001/12/24 01:34:06 alex
162 * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
163 * - MOTD-Ausgabe begonnen zu implementieren.
165 * Revision 1.2 2001/12/23 21:57:16 alex
166 * - erste IRC-Befehle zu implementieren begonnen.
168 * Revision 1.1 2001/12/14 08:13:43 alex
169 * - neues Modul begonnen :-)
190 #include "messages.h"
198 #define CONNECTED TRUE
199 #define DISCONNECTED FALSE
202 LOCAL BOOLEAN Hello_User( CLIENT *Client );
203 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
205 LOCAL VOID Kill_Nick( CHAR *Nick );
208 GLOBAL VOID IRC_Init( VOID )
213 GLOBAL VOID IRC_Exit( VOID )
218 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CHAR *Format, ... )
221 BOOLEAN ok = CONNECTED;
224 assert( Client != NULL );
225 assert( Format != NULL );
227 va_start( ap, Format );
228 vsnprintf( buffer, 1000, Format, ap );
231 /* an den Client selber */
232 ok = IRC_WriteStrClientPrefix( Client, Client_ThisServer( ), buffer );
235 } /* IRC_WriteStrClient */
238 GLOBAL BOOLEAN IRC_WriteStrClientPrefix( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
240 /* Text an Clients, lokal bzw. remote, senden. */
245 assert( Client != NULL );
246 assert( Format != NULL );
247 assert( Prefix != NULL );
249 va_start( ap, Format );
250 vsnprintf( buffer, 1000, Format, ap );
253 return Conn_WriteStr( Client_Conn( Client_NextHop( Client )), ":%s %s", Client_ID( Prefix ), buffer );
254 } /* IRC_WriteStrClientPrefix */
257 GLOBAL BOOLEAN IRC_WriteStrChannel( CLIENT *Client, CHANNEL *Chan, BOOLEAN Remote, CHAR *Format, ... )
262 assert( Client != NULL );
263 assert( Format != NULL );
265 va_start( ap, Format );
266 vsnprintf( buffer, 1000, Format, ap );
269 return IRC_WriteStrChannelPrefix( Client, Chan, Client_ThisServer( ), Remote, buffer );
270 } /* IRC_WriteStrChannel */
273 GLOBAL BOOLEAN IRC_WriteStrChannelPrefix( CLIENT *Client, CHANNEL *Chan, CLIENT *Prefix, BOOLEAN Remote, CHAR *Format, ... )
276 BOOLEAN sock[MAX_CONNECTIONS], ok = CONNECTED, i;
282 assert( Client != NULL );
283 assert( Chan != NULL );
284 assert( Prefix != NULL );
285 assert( Format != NULL );
287 va_start( ap, Format );
288 vsnprintf( buffer, 1000, Format, ap );
291 for( i = 0; i < MAX_CONNECTIONS; i++ ) sock[i] = FALSE;
293 /* An alle Clients, die in den selben Channels sind.
294 * Dabei aber nur einmal je Remote-Server */
295 cl2chan = Channel_FirstMember( Chan );
298 c = Channel_GetClient( cl2chan );
301 if( Client_Conn( c ) <= NONE ) c = NULL;
302 else if( Client_Type( c ) == CLIENT_SERVER ) c = NULL;
304 if( c ) c = Client_NextHop( c );
306 if( c && ( c != Client ))
308 /* Ok, anderer Client */
309 s = Client_Conn( c );
311 assert( s < MAX_CONNECTIONS );
314 cl2chan = Channel_NextMember( Chan, cl2chan );
318 for( i = 0; i < MAX_CONNECTIONS; i++ )
322 ok = Conn_WriteStr( i, ":%s %s", Client_ID( Prefix ), buffer );
327 } /* IRC_WriteStrChannelPrefix */
330 GLOBAL VOID IRC_WriteStrServers( CLIENT *ExceptOf, CHAR *Format, ... )
335 assert( Format != NULL );
337 va_start( ap, Format );
338 vsnprintf( buffer, 1000, Format, ap );
341 /* an den Client selber */
342 return IRC_WriteStrServersPrefix( ExceptOf, Client_ThisServer( ), buffer );
343 } /* IRC_WriteStrServers */
346 GLOBAL VOID IRC_WriteStrServersPrefix( CLIENT *ExceptOf, CLIENT *Prefix, CHAR *Format, ... )
352 assert( Format != NULL );
353 assert( Prefix != NULL );
355 va_start( ap, Format );
356 vsnprintf( buffer, 1000, Format, ap );
362 if(( Client_Type( c ) == CLIENT_SERVER ) && ( Client_Conn( c ) > NONE ) && ( c != Client_ThisServer( )) && ( c != ExceptOf ))
364 /* Ziel-Server gefunden */
365 IRC_WriteStrClientPrefix( c, Prefix, buffer );
367 c = Client_Next( c );
369 } /* IRC_WriteStrServersPrefix */
372 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
374 assert( Client != NULL );
375 assert( Req != NULL );
377 /* Fehler liefern, wenn kein lokaler Client */
378 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
380 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
382 /* noch nicht registrierte unbekannte Verbindung */
383 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
385 /* Passwort speichern */
386 Client_SetPassword( Client, Req->argv[0] );
388 Client_SetType( Client, CLIENT_GOTPASS );
391 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
393 /* noch nicht registrierte Server-Verbindung */
394 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
396 /* Passwort speichern */
397 Client_SetPassword( Client, Req->argv[0] );
399 Client_SetType( Client, CLIENT_GOTPASSSERVER );
402 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
404 /* Falsche Anzahl Parameter? */
405 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
407 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
411 GLOBAL BOOLEAN IRC_SERVER( CLIENT *Client, REQUEST *Req )
413 CHAR str[LINE_LEN], *ptr;
418 assert( Client != NULL );
419 assert( Req != NULL );
421 /* Fehler liefern, wenn kein lokaler Client */
422 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
424 if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
426 /* Verbindung soll als Server-Server-Verbindung registriert werden */
427 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
429 /* Falsche Anzahl Parameter? */
430 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
432 /* Ist dieser Server bei uns konfiguriert? */
433 for( i = 0; i < Conf_Server_Count; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
434 if( i >= Conf_Server_Count )
436 /* Server ist nicht konfiguriert! */
437 Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
438 Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", TRUE );
441 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd ) != 0 )
443 /* Falsches Passwort */
444 Log( LOG_ERR, "Connection %d: Bad password for server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
445 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
449 /* Ist ein Server mit dieser ID bereits registriert? */
450 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
452 /* Server-Strukturen fuellen ;-) */
453 Client_SetID( Client, Req->argv[0] );
454 Client_SetHops( Client, 1 );
455 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
457 /* Meldet sich der Server bei uns an? */
460 /* Unseren SERVER- und PASS-Befehl senden */
462 if( ! IRC_WriteStrClient( Client, "PASS %s "PASSSERVERADD, Conf_Server[i].pwd )) ok = FALSE;
463 else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
466 Conn_Close( Client_Conn( Client ), "Unexpected server behavior!", NULL, FALSE );
469 Client_SetIntroducer( Client, Client );
470 Client_SetToken( Client, 1 );
472 else Client_SetToken( Client, atoi( Req->argv[1] ));
474 Log( LOG_NOTICE, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), Client_Conn( Client ));
476 Client_SetType( Client, CLIENT_SERVER );
478 /* Alle bisherigen Server dem neuen Server bekannt machen,
479 * die bisherigen Server ueber den neuen informierenn */
483 if(( Client_Type( c ) == CLIENT_SERVER ) && ( c != Client ) && ( c != Client_ThisServer( )))
485 if( Client_Conn( c ) > NONE )
487 /* Dem gefundenen Server gleich den neuen
488 * Server bekannt machen */
489 if( ! IRC_WriteStrClient( c, "SERVER %s %d %d :%s", Client_ID( Client ), Client_Hops( Client ) + 1, Client_MyToken( Client ), Client_Info( Client ))) return DISCONNECTED;
492 /* Den neuen Server ueber den alten informieren */
493 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;
495 c = Client_Next( c );
498 /* alle User dem neuen Server bekannt machen */
502 if( Client_Type( c ) == CLIENT_USER )
504 /* User an neuen Server melden */
505 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;
507 c = Client_Next( c );
512 else if( Client_Type( Client ) == CLIENT_SERVER )
514 /* Neuer Server wird im Netz angekuendigt */
516 /* Falsche Anzahl Parameter? */
517 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
519 /* Ist ein Server mit dieser ID bereits registriert? */
520 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
522 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
523 ptr = strchr( Req->argv[3] + 2, '[' );
524 if( ! ptr ) ptr = Req->argv[3];
526 /* Neue Client-Struktur anlegen */
527 c = Client_NewRemoteServer( Client, Req->argv[0], atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, TRUE );
530 /* Neue Client-Struktur konnte nicht angelegt werden */
531 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
532 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", TRUE );
536 /* Log-Meldung zusammenbauen und ausgeben */
537 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) sprintf( str, "connected to %s, ", Req->prefix );
538 else strcpy( str, "" );
539 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": "" );
543 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
547 GLOBAL BOOLEAN IRC_NJOIN( CLIENT *Client, REQUEST *Req )
552 assert( Client != NULL );
553 assert( Req != NULL );
555 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTEREDSERVER_MSG, Client_ID( Client ));
557 /* Falsche Anzahl Parameter? */
558 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
561 ptr = strtok( Req->argv[1], "," );
564 /* Prefixe abschneiden */
565 while(( *ptr == '@' ) || ( *ptr == '+' )) ptr++;
567 c = Client_GetFromID( ptr );
568 if( c ) Channel_Join( c, chan );
569 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, chan );
571 /* naechsten Nick suchen */
572 ptr = strtok( NULL, "," );
579 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
581 CLIENT *intr_c, *target, *c;
584 assert( Client != NULL );
585 assert( Req != NULL );
587 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
589 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 ))
591 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 ))
594 /* User-Registrierung bzw. Nick-Aenderung */
596 /* Falsche Anzahl Parameter? */
597 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
599 /* "Ziel-Client" ermitteln */
600 if( Client_Type( Client ) == CLIENT_SERVER )
602 target = Client_GetFromID( Req->prefix );
603 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
607 /* Ist der Client "restricted"? */
608 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
612 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
613 * wir nichts. So macht es das Original und mind. Snak hat probleme,
614 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
616 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
619 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
620 * will nur die Gross- und Kleinschreibung aendern. Das darf
621 * er natuerlich machen :-) */
622 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
624 if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
627 if( Client_Type( Client ) == CLIENT_USER )
629 /* Nick-Aenderung: allen mitteilen! */
630 Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
631 IRC_WriteStrClient( Client, "NICK :%s", Req->argv[0] );
632 IRC_WriteStrServersPrefix( NULL, Client, "NICK :%s", Req->argv[0] );
634 else if( Client_Type( Client ) == CLIENT_SERVER )
636 /* Nick-Aenderung: allen mitteilen! */
637 Log( LOG_DEBUG, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
638 IRC_WriteStrServersPrefix( Client, target, "NICK :%s", Req->argv[0] );
641 /* Client-Nick registrieren */
642 Client_SetID( target, Req->argv[0] );
644 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
647 Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client_Conn( Client ));
648 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
649 else Client_SetType( Client, CLIENT_GOTNICK );
653 else if( Client_Type( Client ) == CLIENT_SERVER )
655 /* Server fuehrt neuen Client ein */
657 /* Falsche Anzahl Parameter? */
658 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
660 /* Nick ueberpruefen */
661 c = Client_GetFromID( Req->argv[0] );
664 /* Der neue Nick ist auf diesem Server bereits registriert:
665 * sowohl der neue, als auch der alte Client muessen nun
666 * disconnectiert werden. */
667 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
668 Kill_Nick( Req->argv[0] );
672 /* Server, zu dem der Client connectiert ist, suchen */
673 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
676 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
677 Kill_Nick( Req->argv[0] );
681 /* Neue Client-Struktur anlegen */
682 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 );
685 /* Eine neue Client-Struktur konnte nicht angelegt werden.
686 * Der Client muss disconnectiert werden, damit der Netz-
687 * status konsistent bleibt. */
688 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
689 Kill_Nick( Req->argv[0] );
693 modes = Client_Modes( c );
694 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": "" );
695 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": "" );
697 /* Andere Server, ausser dem Introducer, informieren */
698 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] );
702 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
706 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
708 assert( Client != NULL );
709 assert( Req != NULL );
712 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
714 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
717 /* Falsche Anzahl Parameter? */
718 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
720 Client_SetUser( Client, Req->argv[0], FALSE );
721 Client_SetInfo( Client, Req->argv[3] );
723 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client_Conn( Client ));
724 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
725 else Client_SetType( Client, CLIENT_GOTUSER );
728 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
730 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
732 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
736 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
740 assert( Client != NULL );
741 assert( Req != NULL );
743 if(( Client_Type( Client ) == CLIENT_USER ) || ( Client_Type( Client ) == CLIENT_SERVICE ))
747 /* Falsche Anzahl Parameter? */
748 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
750 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
751 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
755 else if ( Client_Type( Client ) == CLIENT_SERVER )
759 /* Falsche Anzahl Parameter? */
760 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
762 target = Client_Search( Req->prefix );
765 Log( LOG_ERR, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
769 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL );
770 else Client_Destroy( target, "Got QUIT command.", Req->argv[0] );
774 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
778 GLOBAL BOOLEAN IRC_SQUIT( CLIENT *Client, REQUEST *Req )
782 assert( Client != NULL );
783 assert( Req != NULL );
785 /* SQUIT ist nur fuer Server erlaubt */
786 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
788 /* Falsche Anzahl Parameter? */
789 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
791 target = Client_GetFromID( Req->argv[0] );
794 Log( LOG_ERR, "Got SQUIT from %s for unknown server \%s\"!?", Client_ID( Client ), Req->argv[0] );
798 if( target == Client ) Log( LOG_DEBUG, "Got SQUIT from %s: %s", Client_ID( Client ), Req->argv[1] );
799 else Log( LOG_DEBUG, "Got SQUIT from %s for %s: %s", Client_ID( Client ), Client_ID( target ), Req->argv[1] );
801 /* SQUIT an alle Server weiterleiten */
802 IRC_WriteStrServers( Client, "SQUIT %s :%s", Req->argv[0], Req->argv[1] );
804 if( Client_Conn( target ) > NONE )
806 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), "Got SQUIT command.", Req->argv[1], TRUE );
807 else Conn_Close( Client_Conn( target ), "Got SQUIT command.", NULL, TRUE );
812 Client_Destroy( target, "Got SQUIT command.", Req->argv[1] );
818 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
820 assert( Client != NULL );
821 assert( Req != NULL );
823 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
825 /* Falsche Anzahl Parameter? */
826 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
827 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
829 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
830 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
834 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
836 assert( Client != NULL );
837 assert( Req != NULL );
839 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
841 /* Falsche Anzahl Parameter? */
842 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
843 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
845 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
846 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
848 Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
853 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
855 assert( Client != NULL );
856 assert( Req != NULL );
858 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
860 /* Falsche Anzahl Parameter? */
861 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
863 return Show_MOTD( Client );
867 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
872 assert( Client != NULL );
873 assert( Req != NULL );
875 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
877 /* Falsche Anzahl Parameter? */
878 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
879 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
880 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
882 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
884 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
886 to = Client_Search( Req->argv[0] );
889 /* Okay, Ziel ist ein User */
890 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
891 return IRC_WriteStrClientPrefix( to, from, "PRIVMSG %s :%s", Client_ID( to ), Req->argv[1] );
894 chan = Channel_Search( Req->argv[0] );
897 /* Okay, Ziel ist ein Channel */
898 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
899 return IRC_WriteStrChannelPrefix( Client, chan, from, TRUE, "PRIVMSG %s :%s", Req->argv[0], Req->argv[1] );
902 return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
906 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
910 assert( Client != NULL );
911 assert( Req != NULL );
913 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
915 /* Falsche Anzahl Parameter? */
916 if( Req->argc != 2 ) return CONNECTED;
918 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
920 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
922 to = Client_Search( Req->argv[0] );
925 /* Okay, Ziel ist ein User */
926 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
928 else return CONNECTED;
932 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
934 CHAR x[2], new_modes[CLIENT_MODE_LEN], *ptr;
938 assert( Client != NULL );
939 assert( Req != NULL );
941 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
943 /* Falsche Anzahl Parameter? */
944 if(( Req->argc > 2 ) || ( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
946 /* "Ziel-Client" suchen */
947 target = Client_Search( Req->argv[0] );
949 /* Wer ist der Anfragende? */
950 if( Client_Type( Client ) == CLIENT_USER )
952 /* User: MODE ist nur fuer sich selber zulaessig */
953 if( target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
957 /* Server: gibt es den Client ueberhaupt? */
958 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
961 /* Werden die Modes erfragt? */
962 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( Client ));
966 /* Sollen Modes gesetzt oder geloescht werden? */
967 if( *ptr == '+' ) set = TRUE;
968 else if( *ptr == '-' ) set = FALSE;
969 else return IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
971 /* Reply-String mit Aenderungen vorbereiten */
972 if( set ) strcpy( new_modes, "+" );
973 else strcpy( new_modes, "-" );
981 if( Client_Type( Client ) == CLIENT_SERVER )
995 /* restricted (kann nur gesetzt werden) */
996 if( set ) x[0] = 'r';
997 else ok = IRC_WriteStrClient( target, ERR_RESTRICTED_MSG, Client_ID( target ));
1000 /* operator (kann nur geloescht werden) */
1003 Client_SetOperByMe( target, FALSE );
1006 else ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
1009 ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
1016 if( ! x[0] ) continue;
1018 /* Okay, gueltigen Mode gefunden */
1021 /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
1022 if( Client_ModeAdd( target, x[0] )) strcat( new_modes, x );
1026 /* Modes geloescht. Wenn der Client ihn hatte: merken */
1027 if( Client_ModeDel( target, x[0] )) strcat( new_modes, x );
1031 /* Geanderte Modes? */
1034 if( Client_Type( Client ) == CLIENT_SERVER )
1036 /* Modes an andere Server forwarden */
1037 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :%s", Client_ID( target ), new_modes );
1041 /* Bestaetigung an Client schicken & andere Server informieren */
1042 ok = IRC_WriteStrClient( Client, "MODE %s :%s", Client_ID( target ), new_modes );
1043 IRC_WriteStrServers( Client, "MODE %s :%s", Client_ID( target ), new_modes );
1045 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( target ), Client_Modes( target ));
1051 GLOBAL BOOLEAN IRC_OPER( CLIENT *Client, REQUEST *Req )
1055 assert( Client != NULL );
1056 assert( Req != NULL );
1058 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1060 /* Falsche Anzahl Parameter? */
1061 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1063 /* Operator suchen */
1064 for( i = 0; i < Conf_Oper_Count; i++)
1066 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
1068 if( i >= Conf_Oper_Count )
1070 Log( LOG_WARNING, "Got invalid OPER from \"%s\": Name \"%s\" not configured!", Client_Mask( Client ), Req->argv[0] );
1071 return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
1074 /* Stimmt das Passwort? */
1075 if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
1077 Log( LOG_WARNING, "Got invalid OPER from \"%s\": Bad password for \"%s\"!", Client_Mask( Client ), Conf_Oper[i].name );
1078 return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
1081 if( ! Client_HasMode( Client, 'o' ))
1083 /* noch kein o-Mode gesetzt */
1084 Client_ModeAdd( Client, 'o' );
1085 if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
1086 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
1089 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
1091 Client_SetOperByMe( Client, TRUE );
1092 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
1096 GLOBAL BOOLEAN IRC_DIE( CLIENT *Client, REQUEST *Req )
1098 assert( Client != NULL );
1099 assert( Req != NULL );
1101 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1103 /* Falsche Anzahl Parameter? */
1104 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1106 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
1108 Log( LOG_NOTICE, "Got DIE command from \"%s\", going down!", Client_Mask( Client ));
1114 GLOBAL BOOLEAN IRC_RESTART( CLIENT *Client, REQUEST *Req )
1116 assert( Client != NULL );
1117 assert( Req != NULL );
1119 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1121 /* Falsche Anzahl Parameter? */
1122 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1124 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
1126 Log( LOG_NOTICE, "Got RESTART command from \"%s\", going down!", Client_Mask( Client ));
1127 NGIRCd_Restart = TRUE;
1132 GLOBAL BOOLEAN IRC_NAMES( CLIENT *Client, REQUEST *Req )
1134 CHAR rpl[COMMAND_LEN];
1137 assert( Client != NULL );
1138 assert( Req != NULL );
1140 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1142 /* Falsche Anzahl Parameter? */
1143 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1145 /* Noch alle User ausgeben, die in keinem Channel sind */
1147 c = Client_First( );
1150 if( Client_Type( c ) == CLIENT_USER )
1152 /* Okay, das ist ein User */
1153 strcat( rpl, Client_ID( c ));
1157 /* Antwort zu lang? Splitten. */
1158 if( strlen( rpl ) > 480 )
1160 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1161 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
1165 c = Client_Next( c );
1169 /* es wurden User gefunden */
1170 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1171 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
1173 return IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), "*" );
1177 GLOBAL BOOLEAN IRC_ISON( CLIENT *Client, REQUEST *Req )
1179 CHAR rpl[COMMAND_LEN];
1184 assert( Client != NULL );
1185 assert( Req != NULL );
1187 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1189 /* Falsche Anzahl Parameter? */
1190 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1192 strcpy( rpl, RPL_ISON_MSG );
1193 for( i = 0; i < Req->argc; i++ )
1195 ptr = strtok( Req->argv[i], " " );
1199 c = Client_GetFromID( ptr );
1200 if( c && ( Client_Type( c ) == CLIENT_USER ))
1202 /* Dieser Nick ist "online" */
1206 ptr = strtok( NULL, " " );
1209 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1211 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1215 GLOBAL BOOLEAN IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1217 CLIENT *from, *target, *c;
1220 assert( Client != NULL );
1221 assert( Req != NULL );
1223 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1225 /* Falsche Anzahl Parameter? */
1226 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1229 c = Client_GetFromID( Req->argv[Req->argc - 1] );
1230 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
1232 /* Empfaenger des WHOIS suchen */
1233 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1235 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1237 /* Forwarden an anderen Server? */
1240 /* angegebenen Ziel-Server suchen */
1241 target = Client_GetFromID( Req->argv[1] );
1242 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
1246 else if( Client_Conn( c ) == NONE )
1248 /* Client ist nicht von uns. Ziel-Server suchen */
1255 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 );
1257 /* Nick, User und Name */
1258 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
1261 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;
1264 if( Client_HasMode( c, 'o' ))
1266 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
1269 /* Idle (nur lokale Clients) */
1270 if( Client_Conn( c ) > NONE )
1272 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
1276 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1280 GLOBAL BOOLEAN IRC_USERHOST( CLIENT *Client, REQUEST *Req )
1282 CHAR rpl[COMMAND_LEN];
1286 assert( Client != NULL );
1287 assert( Req != NULL );
1289 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1291 /* Falsche Anzahl Parameter? */
1292 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1294 if( Req->argc > 5 ) max = 5;
1295 else max = Req->argc;
1297 strcpy( rpl, RPL_USERHOST_MSG );
1298 for( i = 0; i < max; i++ )
1300 c = Client_GetFromID( Req->argv[i] );
1301 if( c && ( Client_Type( c ) == CLIENT_USER ))
1303 /* Dieser Nick ist "online" */
1304 strcat( rpl, Client_ID( c ));
1305 if( Client_HasMode( c, 'o' )) strcat( rpl, "*" );
1307 if( Client_HasMode( c, 'a' )) strcat( rpl, "-" );
1308 else strcat( rpl, "+" );
1309 strcat( rpl, Client_User( c ));
1311 strcat( rpl, Client_Hostname( c ));
1315 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1317 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1318 } /* IRC_USERHOST */
1321 GLOBAL BOOLEAN IRC_ERROR( CLIENT *Client, REQUEST *Req )
1323 assert( Client != NULL );
1324 assert( Req != NULL );
1326 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
1327 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
1333 GLOBAL BOOLEAN IRC_LUSERS( CLIENT *Client, REQUEST *Req )
1335 CLIENT *target, *from;
1338 assert( Client != NULL );
1339 assert( Req != NULL );
1341 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1343 /* Falsche Anzahl Parameter? */
1344 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1346 /* Absender ermitteln */
1347 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1349 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1351 /* An anderen Server forwarden? */
1352 if( Req->argc == 2 )
1354 target = Client_GetFromID( Req->argv[1] );
1355 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
1356 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
1359 /* Wer ist der Absender? */
1360 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1361 else target = Client;
1362 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1364 /* Users, Services und Serevr im Netz */
1365 if( ! IRC_WriteStrClient( target, RPL_LUSERCLIENT_MSG, Client_ID( target ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1367 /* IRC-Operatoren im Netz */
1368 cnt = Client_OperCount( );
1371 if( ! IRC_WriteStrClient( target, RPL_LUSEROP_MSG, Client_ID( target ), cnt )) return DISCONNECTED;
1374 /* Unbekannt Verbindungen */
1375 cnt = Client_UnknownCount( );
1378 if( ! IRC_WriteStrClient( target, RPL_LUSERUNKNOWN_MSG, Client_ID( target ), cnt )) return DISCONNECTED;
1381 /* Channels im Netz */
1382 if( ! IRC_WriteStrClient( target, RPL_LUSERCHANNELS_MSG, Client_ID( target ), Channel_Count( ))) return DISCONNECTED;
1384 /* Channels im Netz */
1385 if( ! IRC_WriteStrClient( target, RPL_LUSERME_MSG, Client_ID( target ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1391 GLOBAL BOOLEAN IRC_LINKS( CLIENT *Client, REQUEST *Req )
1393 CLIENT *target, *from, *c;
1396 assert( Client != NULL );
1397 assert( Req != NULL );
1399 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1401 /* Falsche Anzahl Parameter? */
1402 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1404 /* Server-Mask ermitteln */
1405 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
1408 /* Absender ermitteln */
1409 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1411 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1413 /* An anderen Server forwarden? */
1414 if( Req->argc == 2 )
1416 target = Client_GetFromID( Req->argv[0] );
1417 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
1418 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
1421 /* Wer ist der Absender? */
1422 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1423 else target = Client;
1424 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1426 c = Client_First( );
1429 if( Client_Type( c ) == CLIENT_SERVER )
1431 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;
1433 c = Client_Next( c );
1436 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
1440 GLOBAL BOOLEAN IRC_JOIN( CLIENT *Client, REQUEST *Req )
1445 assert( Client != NULL );
1446 assert( Req != NULL );
1448 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1450 /* Falsche Anzahl Parameter? */
1451 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1453 /* Wer ist der Absender? */
1454 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1455 else target = Client;
1456 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1458 /* Channel-Namen durchgehen */
1459 chan = strtok( Req->argv[0], "," );
1462 if( Client_Type( Client ) == CLIENT_SERVER )
1464 /* Channel-Flags extrahieren */
1465 flags = strchr( chan, 0x7 );
1466 if( flags ) *flags++ = '\0';
1470 if( ! Channel_Join( target, chan ))
1472 /* naechsten Namen ermitteln */
1473 chan = strtok( NULL, "," );
1477 /* An andere Server weiterleiten */
1478 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s", chan );
1479 IRC_WriteStrChannelPrefix( Client, Channel_Search( chan ), target, FALSE, "JOIN :%s", chan );
1481 if( Client_Type( Client ) == CLIENT_USER )
1483 /* an Client bestaetigen */
1484 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", chan );
1485 /* Topic an Client schicken */
1486 IRC_WriteStrClient( Client, RPL_TOPIC_MSG, Client_ID( Client ), chan, "What a wonderful channel!" );
1489 /* naechsten Namen ermitteln */
1490 chan = strtok( NULL, "," );
1496 GLOBAL BOOLEAN IRC_PART( CLIENT *Client, REQUEST *Req )
1501 assert( Client != NULL );
1502 assert( Req != NULL );
1504 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1506 /* Falsche Anzahl Parameter? */
1507 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1509 /* Wer ist der Absender? */
1510 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1511 else target = Client;
1512 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1514 /* Channel-Namen durchgehen */
1515 chan = strtok( Req->argv[0], "," );
1518 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
1520 /* naechsten Namen ermitteln */
1521 chan = strtok( NULL, "," );
1525 /* naechsten Namen ermitteln */
1526 chan = strtok( NULL, "," );
1532 LOCAL BOOLEAN Hello_User( CLIENT *Client )
1534 assert( Client != NULL );
1536 /* Passwort ueberpruefen */
1537 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
1539 /* Falsches Passwort */
1540 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
1541 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
1542 return DISCONNECTED;
1545 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
1547 /* Andere Server informieren */
1548 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 ));
1550 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
1551 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
1552 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
1553 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
1555 Client_SetType( Client, CLIENT_USER );
1557 return Show_MOTD( Client );
1561 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
1567 assert( Client != NULL );
1569 fd = fopen( Conf_MotdFile, "r" );
1572 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1573 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1576 IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1579 if( ! fgets( line, 126, fd )) break;
1580 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
1581 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
1587 ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
1595 LOCAL VOID Kill_Nick( CHAR *Nick )
1597 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected!", Nick );
1599 Log( LOG_ALERT, "[Kill_Nick() not implemented - OOOPS!]" );