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.42 2002/01/27 18:28:01 alex Exp $
17 * Revision 1.42 2002/01/27 18:28:01 alex
18 * - bei NICK wurde das falsche Prefix an andere Server weitergegeben.
20 * Revision 1.41 2002/01/27 17:15:49 alex
21 * - anderungen an den Funktions-Prototypen von IRC_WriteStrChannel() und
22 * IRC_WriteStrChannelPrefix(),
23 * - neue: IRC_WriteStrClientPrefixID() und IRC_WriteStrServersPrefixID().
25 * Revision 1.40 2002/01/26 18:43:11 alex
26 * - neue Funktionen IRC_WriteStrChannelPrefix() und IRC_WriteStrChannel(),
27 * die IRC_Write_xxx_Related() sind dafuer entfallen.
28 * - IRC_PRIVMSG() kann nun auch mit Channels als Ziel umgehen.
30 * Revision 1.39 2002/01/21 00:05:11 alex
31 * - neue Funktionen IRC_JOIN und IRC_PART begonnen, ebenso die Funktionen
32 * IRC_WriteStrRelatedPrefix und IRC_WriteStrRelatedChannelPrefix().
33 * - diverse Aenderungen im Zusammenhang mit Channels.
35 * Revision 1.38 2002/01/18 15:31:32 alex
36 * - die User-Modes bei einem NICK von einem Server wurden falsch uebernommen.
38 * Revision 1.37 2002/01/16 22:10:18 alex
39 * - IRC_LUSERS() implementiert.
41 * Revision 1.36 2002/01/11 23:50:55 alex
42 * - LINKS implementiert, LUSERS begonnen.
44 * Revision 1.35 2002/01/09 21:30:45 alex
45 * - WHOIS wurde faelschlicherweise an User geforwarded statt vom Server beantwortet.
47 * Revision 1.34 2002/01/09 01:09:58 alex
48 * - WHOIS wird im "Strict-RFC-Mode" nicht mehr automatisch geforwarded,
49 * - andere Server werden nun ueber bisherige Server und User informiert.
51 * Revision 1.33 2002/01/07 23:42:12 alex
52 * - Es werden fuer alle Server eigene Token generiert,
53 * - QUIT von einem Server fuer einen User wird an andere Server geforwarded,
54 * - ebenso NICK-Befehle, die "fremde" User einfuehren.
56 * Revision 1.32 2002/01/07 16:02:36 alex
57 * - Loglevel von Remote-Mode-Aenderungen angepasst (nun Debug).
58 * - Im Debug-Mode werden nun auch PING's protokolliert.
60 * Revision 1.31 2002/01/07 15:39:46 alex
61 * - Server nimmt nun Server-Links an: PASS und SERVER entsprechend angepasst.
62 * - MODE und NICK melden nun die Aenderungen an andere Server.
64 * Revision 1.30 2002/01/06 15:21:29 alex
65 * - Loglevel und Meldungen nochmals ueberarbeitet.
66 * - QUIT und SQUIT forwarden nun den Grund der Trennung,
67 * - WHOIS wird nun immer an den "Original-Server" weitergeleitet.
69 * Revision 1.29 2002/01/05 23:24:54 alex
70 * - WHOIS erweitert: Anfragen koennen an andere Server weitergeleitet werden.
71 * - Vorbereitungen fuer Ident-Abfragen bei neuen Client-Strukturen.
73 * Revision 1.28 2002/01/05 20:08:02 alex
74 * - Div. Aenderungen fuer die Server-Links (u.a. WHOIS, QUIT, NICK angepasst).
75 * - Neue Funktionen IRC_WriteStrServer() und IRC_WriteStrServerPrefix().
77 * Revision 1.27 2002/01/05 19:15:03 alex
78 * - Fehlerpruefung bei select() in der "Hauptschleife" korrigiert.
80 * Revision 1.26 2002/01/05 16:51:18 alex
81 * - das Passwort von Servern wird nun ueberprueft (PASS- und SERVER-Befehl).
83 * Revision 1.25 2002/01/05 00:48:33 alex
84 * - bei SQUIT wurde immer die Verbindung getrennt, auch bei Remote-Servern.
86 * Revision 1.24 2002/01/04 17:58:44 alex
87 * - IRC_WriteStrXXX()-Funktionen eingefuehrt, groessere Anpassungen daran.
88 * - neuer Befehl SQUIT, QUIT an Server-Links angepasst.
90 * Revision 1.23 2002/01/04 01:36:40 alex
91 * - Loglevel ein wenig angepasst.
93 * Revision 1.22 2002/01/04 01:21:47 alex
94 * - Client-Strukruren werden nur noch ueber Funktionen angesprochen.
95 * - Weitere Anpassungen und Erweiterungen der Server-Links.
97 * Revision 1.21 2002/01/03 02:26:51 alex
98 * - neue Befehle SERVER und NJOIN begonnen,
99 * - begonnen, diverse IRC-Befehle an Server-Links anzupassen.
101 * Revision 1.20 2002/01/02 12:46:41 alex
102 * - die Gross- und Kleinschreibung des Nicks kann mit NICK nun geaendert werden.
104 * Revision 1.19 2002/01/02 02:51:39 alex
105 * - Copyright-Texte angepasst.
106 * - neuer Befehl "ERROR".
108 * Revision 1.17 2001/12/31 15:33:13 alex
109 * - neuer Befehl NAMES, kleinere Bugfixes.
110 * - Bug bei PING behoben: war zu restriktiv implementiert :-)
112 * Revision 1.16 2001/12/31 02:18:51 alex
113 * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
114 * - neuen Header "defines.h" mit (fast) allen Konstanten.
115 * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
117 * Revision 1.15 2001/12/30 19:26:11 alex
118 * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
120 * Revision 1.14 2001/12/30 11:42:00 alex
121 * - der Server meldet nun eine ordentliche "Start-Zeit".
123 * Revision 1.13 2001/12/29 03:10:06 alex
124 * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
125 * - neue configure-Optione "--enable-strict-rfc".
127 * Revision 1.12 2001/12/27 19:17:26 alex
128 * - neue Befehle PRIVMSG, NOTICE, PING.
130 * Revision 1.11 2001/12/27 16:55:41 alex
131 * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
133 * Revision 1.10 2001/12/26 22:48:53 alex
134 * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
136 * Revision 1.9 2001/12/26 14:45:37 alex
139 * Revision 1.8 2001/12/26 03:21:46 alex
140 * - PING/PONG-Befehle implementiert,
141 * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
143 * Revision 1.7 2001/12/25 23:25:18 alex
144 * - und nochmal Aenderungen am Logging ;-)
146 * Revision 1.6 2001/12/25 23:13:33 alex
147 * - Debug-Meldungen angepasst.
149 * Revision 1.5 2001/12/25 22:02:42 alex
150 * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
152 * Revision 1.4 2001/12/25 19:19:30 alex
153 * - bessere Fehler-Abfragen, diverse Bugfixes.
154 * - Nicks werden nur einmal vergeben :-)
155 * - /MOTD wird unterstuetzt.
157 * Revision 1.3 2001/12/24 01:34:06 alex
158 * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
159 * - MOTD-Ausgabe begonnen zu implementieren.
161 * Revision 1.2 2001/12/23 21:57:16 alex
162 * - erste IRC-Befehle zu implementieren begonnen.
164 * Revision 1.1 2001/12/14 08:13:43 alex
165 * - neues Modul begonnen :-)
186 #include "messages.h"
194 #define CONNECTED TRUE
195 #define DISCONNECTED FALSE
198 LOCAL BOOLEAN Hello_User( CLIENT *Client );
199 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
201 LOCAL VOID Kill_Nick( CHAR *Nick );
204 GLOBAL VOID IRC_Init( VOID )
209 GLOBAL VOID IRC_Exit( VOID )
214 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CHAR *Format, ... )
217 BOOLEAN ok = CONNECTED;
220 assert( Client != NULL );
221 assert( Format != NULL );
223 va_start( ap, Format );
224 vsnprintf( buffer, 1000, Format, ap );
227 /* an den Client selber */
228 ok = IRC_WriteStrClientPrefix( Client, Client_ThisServer( ), buffer );
231 } /* IRC_WriteStrClient */
234 GLOBAL BOOLEAN IRC_WriteStrClientPrefix( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
236 /* Text an Clients, lokal bzw. remote, senden. */
241 assert( Client != NULL );
242 assert( Format != NULL );
243 assert( Prefix != NULL );
245 va_start( ap, Format );
246 vsnprintf( buffer, 1000, Format, ap );
249 return Conn_WriteStr( Client_Conn( Client_NextHop( Client )), ":%s %s", Client_Mask( Prefix ), buffer );
250 } /* IRC_WriteStrClientPrefix */
253 GLOBAL BOOLEAN IRC_WriteStrClientPrefixID( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
255 /* Text an Clients, lokal bzw. remote, senden. */
260 assert( Client != NULL );
261 assert( Format != NULL );
262 assert( Prefix != NULL );
264 va_start( ap, Format );
265 vsnprintf( buffer, 1000, Format, ap );
268 return Conn_WriteStr( Client_Conn( Client_NextHop( Client )), ":%s %s", Client_ID( Prefix ), buffer );
269 } /* IRC_WriteStrClientPrefixID */
272 GLOBAL BOOLEAN IRC_WriteStrChannel( CLIENT *Client, CHANNEL *Chan, BOOLEAN Remote, CHAR *Format, ... )
277 assert( Client != NULL );
278 assert( Format != NULL );
280 va_start( ap, Format );
281 vsnprintf( buffer, 1000, Format, ap );
284 return IRC_WriteStrChannelPrefix( Client, Chan, Client_ThisServer( ), Remote, buffer );
285 } /* IRC_WriteStrChannel */
288 GLOBAL BOOLEAN IRC_WriteStrChannelPrefix( CLIENT *Client, CHANNEL *Chan, CLIENT *Prefix, BOOLEAN Remote, CHAR *Format, ... )
291 BOOLEAN sock[MAX_CONNECTIONS], ok = CONNECTED, i;
297 assert( Client != NULL );
298 assert( Chan != NULL );
299 assert( Prefix != NULL );
300 assert( Format != NULL );
302 va_start( ap, Format );
303 vsnprintf( buffer, 1000, Format, ap );
306 for( i = 0; i < MAX_CONNECTIONS; i++ ) sock[i] = FALSE;
308 /* An alle Clients, die in den selben Channels sind.
309 * Dabei aber nur einmal je Remote-Server */
310 cl2chan = Channel_FirstMember( Chan );
313 if( Remote ) c = Client_NextHop( Channel_GetClient( cl2chan ));
316 c = Channel_GetClient( cl2chan );
317 if( Client_Conn( c ) <= NONE ) c = NULL;
320 if( c && ( c != Client ))
322 /* Ok, anderer Client */
323 s = Client_Conn( c );
325 assert( s < MAX_CONNECTIONS );
328 cl2chan = Channel_NextMember( Chan, cl2chan );
332 for( i = 0; i < MAX_CONNECTIONS; i++ )
336 ok = Conn_WriteStr( i, ":%s %s", Client_ID( Prefix ), buffer );
341 } /* IRC_WriteStrChannelPrefix */
344 GLOBAL VOID IRC_WriteStrServers( CLIENT *ExceptOf, CHAR *Format, ... )
349 assert( Format != NULL );
351 va_start( ap, Format );
352 vsnprintf( buffer, 1000, Format, ap );
355 /* an den Client selber */
356 return IRC_WriteStrServersPrefix( ExceptOf, Client_ThisServer( ), buffer );
357 } /* IRC_WriteStrServers */
360 GLOBAL VOID IRC_WriteStrServersPrefix( CLIENT *ExceptOf, CLIENT *Prefix, CHAR *Format, ... )
366 assert( Format != NULL );
367 assert( Prefix != NULL );
369 va_start( ap, Format );
370 vsnprintf( buffer, 1000, Format, ap );
376 if(( Client_Type( c ) == CLIENT_SERVER ) && ( Client_Conn( c ) > NONE ) && ( c != Client_ThisServer( )) && ( c != ExceptOf ))
378 /* Ziel-Server gefunden */
379 IRC_WriteStrClientPrefix( c, Prefix, buffer );
381 c = Client_Next( c );
383 } /* IRC_WriteStrServersPrefix */
386 GLOBAL VOID IRC_WriteStrServersPrefixID( CLIENT *ExceptOf, CLIENT *Prefix, CHAR *Format, ... )
392 assert( Format != NULL );
393 assert( Prefix != NULL );
395 va_start( ap, Format );
396 vsnprintf( buffer, 1000, Format, ap );
402 if(( Client_Type( c ) == CLIENT_SERVER ) && ( Client_Conn( c ) > NONE ) && ( c != Client_ThisServer( )) && ( c != ExceptOf ))
404 /* Ziel-Server gefunden */
405 IRC_WriteStrClientPrefixID( c, Prefix, buffer );
407 c = Client_Next( c );
409 } /* IRC_WriteStrServersPrefixID */
412 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
414 assert( Client != NULL );
415 assert( Req != NULL );
417 /* Fehler liefern, wenn kein lokaler Client */
418 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
420 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
422 /* noch nicht registrierte unbekannte Verbindung */
423 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
425 /* Passwort speichern */
426 Client_SetPassword( Client, Req->argv[0] );
428 Client_SetType( Client, CLIENT_GOTPASS );
431 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
433 /* noch nicht registrierte Server-Verbindung */
434 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
436 /* Passwort speichern */
437 Client_SetPassword( Client, Req->argv[0] );
439 Client_SetType( Client, CLIENT_GOTPASSSERVER );
442 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
444 /* Falsche Anzahl Parameter? */
445 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
447 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
451 GLOBAL BOOLEAN IRC_SERVER( CLIENT *Client, REQUEST *Req )
453 CHAR str[LINE_LEN], *ptr;
458 assert( Client != NULL );
459 assert( Req != NULL );
461 /* Fehler liefern, wenn kein lokaler Client */
462 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
464 if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
466 /* Verbindung soll als Server-Server-Verbindung registriert werden */
467 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
469 /* Falsche Anzahl Parameter? */
470 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
472 /* Ist dieser Server bei uns konfiguriert? */
473 for( i = 0; i < Conf_Server_Count; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
474 if( i >= Conf_Server_Count )
476 /* Server ist nicht konfiguriert! */
477 Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
478 Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", TRUE );
481 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd ) != 0 )
483 /* Falsches Passwort */
484 Log( LOG_ERR, "Connection %d: Bad password for server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
485 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
489 /* Ist ein Server mit dieser ID bereits registriert? */
490 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
492 /* Server-Strukturen fuellen ;-) */
493 Client_SetID( Client, Req->argv[0] );
494 Client_SetHops( Client, 1 );
495 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
497 /* Meldet sich der Server bei uns an? */
500 /* Unseren SERVER- und PASS-Befehl senden */
502 if( ! IRC_WriteStrClient( Client, "PASS %s "PASSSERVERADD, Conf_Server[i].pwd )) ok = FALSE;
503 else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
506 Conn_Close( Client_Conn( Client ), "Unexpected server behavior!", NULL, FALSE );
509 Client_SetIntroducer( Client, Client );
510 Client_SetToken( Client, 1 );
512 else Client_SetToken( Client, atoi( Req->argv[1] ));
514 Log( LOG_NOTICE, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), Client_Conn( Client ));
516 Client_SetType( Client, CLIENT_SERVER );
518 /* Alle bisherigen Server dem neuen Server bekannt machen,
519 * die bisherigen Server ueber den neuen informierenn */
523 if(( Client_Type( c ) == CLIENT_SERVER ) && ( c != Client ) && ( c != Client_ThisServer( )))
525 if( Client_Conn( c ) > NONE )
527 /* Dem gefundenen Server gleich den neuen
528 * Server bekannt machen */
529 if( ! IRC_WriteStrClient( c, "SERVER %s %d %d :%s", Client_ID( Client ), Client_Hops( Client ) + 1, Client_MyToken( Client ), Client_Info( Client ))) return DISCONNECTED;
532 /* Den neuen Server ueber den alten informieren */
533 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;
535 c = Client_Next( c );
538 /* alle User dem neuen Server bekannt machen */
542 if( Client_Type( c ) == CLIENT_USER )
544 /* User an neuen Server melden */
545 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;
547 c = Client_Next( c );
552 else if( Client_Type( Client ) == CLIENT_SERVER )
554 /* Neuer Server wird im Netz angekuendigt */
556 /* Falsche Anzahl Parameter? */
557 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
559 /* Ist ein Server mit dieser ID bereits registriert? */
560 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
562 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
563 ptr = strchr( Req->argv[3] + 2, '[' );
564 if( ! ptr ) ptr = Req->argv[3];
566 /* Neue Client-Struktur anlegen */
567 c = Client_NewRemoteServer( Client, Req->argv[0], atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, TRUE );
570 /* Neue Client-Struktur konnte nicht angelegt werden */
571 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
572 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", TRUE );
576 /* Log-Meldung zusammenbauen und ausgeben */
577 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) sprintf( str, "connected to %s, ", Req->prefix );
578 else strcpy( str, "" );
579 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": "" );
583 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
587 GLOBAL BOOLEAN IRC_NJOIN( CLIENT *Client, REQUEST *Req )
592 assert( Client != NULL );
593 assert( Req != NULL );
595 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTEREDSERVER_MSG, Client_ID( Client ));
597 /* Falsche Anzahl Parameter? */
598 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
601 ptr = strtok( Req->argv[1], "," );
604 /* Prefixe abschneiden */
605 while(( *ptr == '@' ) || ( *ptr == '+' )) ptr++;
607 c = Client_GetFromID( ptr );
608 if( c ) Channel_Join( c, chan );
609 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, chan );
611 /* naechsten Nick suchen */
612 ptr = strtok( NULL, "," );
619 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
621 CLIENT *intr_c, *target, *c;
624 assert( Client != NULL );
625 assert( Req != NULL );
627 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
629 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 ))
631 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 ))
634 /* User-Registrierung bzw. Nick-Aenderung */
636 /* Falsche Anzahl Parameter? */
637 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
639 /* "Ziel-Client" ermitteln */
640 if( Client_Type( Client ) == CLIENT_SERVER )
642 target = Client_GetFromID( Req->prefix );
643 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
647 /* Ist der Client "restricted"? */
648 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
652 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
653 * wir nichts. So macht es das Original und mind. Snak hat probleme,
654 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
656 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
659 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
660 * will nur die Gross- und Kleinschreibung aendern. Das darf
661 * er natuerlich machen :-) */
662 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
664 if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
667 if( Client_Type( Client ) == CLIENT_USER )
669 /* Nick-Aenderung: allen mitteilen! */
670 Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
671 IRC_WriteStrClient( Client, "NICK :%s", Req->argv[0] );
672 IRC_WriteStrServersPrefix( NULL, Client, "NICK :%s", Req->argv[0] );
674 else if( Client_Type( Client ) == CLIENT_SERVER )
676 /* Nick-Aenderung: allen mitteilen! */
677 Log( LOG_DEBUG, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
678 IRC_WriteStrServersPrefix( Client, target, "NICK :%s", Req->argv[0] );
681 /* Client-Nick registrieren */
682 Client_SetID( target, Req->argv[0] );
684 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
687 Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client_Conn( Client ));
688 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
689 else Client_SetType( Client, CLIENT_GOTNICK );
693 else if( Client_Type( Client ) == CLIENT_SERVER )
695 /* Server fuehrt neuen Client ein */
697 /* Falsche Anzahl Parameter? */
698 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
700 /* Nick ueberpruefen */
701 c = Client_GetFromID( Req->argv[0] );
704 /* Der neue Nick ist auf diesem Server bereits registriert:
705 * sowohl der neue, als auch der alte Client muessen nun
706 * disconnectiert werden. */
707 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
708 Kill_Nick( Req->argv[0] );
712 /* Server, zu dem der Client connectiert ist, suchen */
713 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
716 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
717 Kill_Nick( Req->argv[0] );
721 /* Neue Client-Struktur anlegen */
722 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 );
725 /* Eine neue Client-Struktur konnte nicht angelegt werden.
726 * Der Client muss disconnectiert werden, damit der Netz-
727 * status konsistent bleibt. */
728 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
729 Kill_Nick( Req->argv[0] );
733 modes = Client_Modes( c );
734 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": "" );
735 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": "" );
737 /* Andere Server, ausser dem Introducer, informieren */
738 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] );
742 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
746 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
748 assert( Client != NULL );
749 assert( Req != NULL );
752 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
754 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
757 /* Falsche Anzahl Parameter? */
758 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
760 Client_SetUser( Client, Req->argv[0], FALSE );
761 Client_SetInfo( Client, Req->argv[3] );
763 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client_Conn( Client ));
764 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
765 else Client_SetType( Client, CLIENT_GOTUSER );
768 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
770 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
772 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
776 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
780 assert( Client != NULL );
781 assert( Req != NULL );
783 if(( Client_Type( Client ) == CLIENT_USER ) || ( Client_Type( Client ) == CLIENT_SERVICE ))
787 /* Falsche Anzahl Parameter? */
788 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
790 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
791 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
795 else if ( Client_Type( Client ) == CLIENT_SERVER )
799 /* Falsche Anzahl Parameter? */
800 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
802 target = Client_Search( Req->prefix );
805 Log( LOG_ERR, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
809 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL );
810 else Client_Destroy( target, "Got QUIT command.", Req->argv[0] );
814 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
818 GLOBAL BOOLEAN IRC_SQUIT( CLIENT *Client, REQUEST *Req )
822 assert( Client != NULL );
823 assert( Req != NULL );
825 /* SQUIT ist nur fuer Server erlaubt */
826 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
828 /* Falsche Anzahl Parameter? */
829 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
831 target = Client_GetFromID( Req->argv[0] );
834 Log( LOG_ERR, "Got SQUIT from %s for unknown server \%s\"!?", Client_ID( Client ), Req->argv[0] );
838 if( target == Client ) Log( LOG_DEBUG, "Got SQUIT from %s: %s", Client_ID( Client ), Req->argv[1] );
839 else Log( LOG_DEBUG, "Got SQUIT from %s for %s: %s", Client_ID( Client ), Client_ID( target ), Req->argv[1] );
841 /* SQUIT an alle Server weiterleiten */
842 IRC_WriteStrServers( Client, "SQUIT %s :%s", Req->argv[0], Req->argv[1] );
844 if( Client_Conn( target ) > NONE )
846 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), "Got SQUIT command.", Req->argv[1], TRUE );
847 else Conn_Close( Client_Conn( target ), "Got SQUIT command.", NULL, TRUE );
852 Client_Destroy( target, "Got SQUIT command.", Req->argv[1] );
858 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
860 assert( Client != NULL );
861 assert( Req != NULL );
863 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
865 /* Falsche Anzahl Parameter? */
866 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
867 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
869 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
870 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
874 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
876 assert( Client != NULL );
877 assert( Req != NULL );
879 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
881 /* Falsche Anzahl Parameter? */
882 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
883 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
885 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
886 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
888 Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
893 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
895 assert( Client != NULL );
896 assert( Req != NULL );
898 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
900 /* Falsche Anzahl Parameter? */
901 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
903 return Show_MOTD( Client );
907 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
912 assert( Client != NULL );
913 assert( Req != NULL );
915 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
917 /* Falsche Anzahl Parameter? */
918 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
919 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
920 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
922 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
924 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
926 to = Client_Search( Req->argv[0] );
929 /* Okay, Ziel ist ein User */
930 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
931 return IRC_WriteStrClientPrefix( to, from, "PRIVMSG %s :%s", Client_ID( to ), Req->argv[1] );
934 chan = Channel_Search( Req->argv[0] );
937 /* Okay, Ziel ist ein Channel */
938 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
939 return IRC_WriteStrChannelPrefix( Client, chan, from, TRUE, "PRIVMSG %s :%s", Req->argv[0], Req->argv[1] );
942 return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
946 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
950 assert( Client != NULL );
951 assert( Req != NULL );
953 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
955 /* Falsche Anzahl Parameter? */
956 if( Req->argc != 2 ) return CONNECTED;
958 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
960 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
962 to = Client_Search( Req->argv[0] );
965 /* Okay, Ziel ist ein User */
966 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
968 else return CONNECTED;
972 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
974 CHAR x[2], new_modes[CLIENT_MODE_LEN], *ptr;
978 assert( Client != NULL );
979 assert( Req != NULL );
981 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
983 /* Falsche Anzahl Parameter? */
984 if(( Req->argc > 2 ) || ( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
986 /* "Ziel-Client" suchen */
987 target = Client_Search( Req->argv[0] );
989 /* Wer ist der Anfragende? */
990 if( Client_Type( Client ) == CLIENT_USER )
992 /* User: MODE ist nur fuer sich selber zulaessig */
993 if( target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
997 /* Server: gibt es den Client ueberhaupt? */
998 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
1001 /* Werden die Modes erfragt? */
1002 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( Client ));
1006 /* Sollen Modes gesetzt oder geloescht werden? */
1007 if( *ptr == '+' ) set = TRUE;
1008 else if( *ptr == '-' ) set = FALSE;
1009 else return IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
1011 /* Reply-String mit Aenderungen vorbereiten */
1012 if( set ) strcpy( new_modes, "+" );
1013 else strcpy( new_modes, "-" );
1021 if( Client_Type( Client ) == CLIENT_SERVER )
1035 /* restricted (kann nur gesetzt werden) */
1036 if( set ) x[0] = 'r';
1037 else ok = IRC_WriteStrClient( target, ERR_RESTRICTED_MSG, Client_ID( target ));
1040 /* operator (kann nur geloescht werden) */
1043 Client_SetOperByMe( target, FALSE );
1046 else ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
1049 ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
1056 if( ! x[0] ) continue;
1058 /* Okay, gueltigen Mode gefunden */
1061 /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
1062 if( Client_ModeAdd( target, x[0] )) strcat( new_modes, x );
1066 /* Modes geloescht. Wenn der Client ihn hatte: merken */
1067 if( Client_ModeDel( target, x[0] )) strcat( new_modes, x );
1071 /* Geanderte Modes? */
1074 if( Client_Type( Client ) == CLIENT_SERVER )
1076 /* Modes an andere Server forwarden */
1077 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :%s", Client_ID( target ), new_modes );
1081 /* Bestaetigung an Client schicken & andere Server informieren */
1082 ok = IRC_WriteStrClient( Client, "MODE %s :%s", Client_ID( target ), new_modes );
1083 IRC_WriteStrServers( Client, "MODE %s :%s", Client_ID( target ), new_modes );
1085 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( target ), Client_Modes( target ));
1091 GLOBAL BOOLEAN IRC_OPER( CLIENT *Client, REQUEST *Req )
1095 assert( Client != NULL );
1096 assert( Req != NULL );
1098 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1100 /* Falsche Anzahl Parameter? */
1101 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1103 /* Operator suchen */
1104 for( i = 0; i < Conf_Oper_Count; i++)
1106 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
1108 if( i >= Conf_Oper_Count )
1110 Log( LOG_WARNING, "Got invalid OPER from \"%s\": Name \"%s\" not configured!", Client_Mask( Client ), Req->argv[0] );
1111 return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
1114 /* Stimmt das Passwort? */
1115 if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
1117 Log( LOG_WARNING, "Got invalid OPER from \"%s\": Bad password for \"%s\"!", Client_Mask( Client ), Conf_Oper[i].name );
1118 return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
1121 if( ! Client_HasMode( Client, 'o' ))
1123 /* noch kein o-Mode gesetzt */
1124 Client_ModeAdd( Client, 'o' );
1125 if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
1126 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
1129 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
1131 Client_SetOperByMe( Client, TRUE );
1132 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
1136 GLOBAL BOOLEAN IRC_DIE( CLIENT *Client, REQUEST *Req )
1138 assert( Client != NULL );
1139 assert( Req != NULL );
1141 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1143 /* Falsche Anzahl Parameter? */
1144 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1146 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
1148 Log( LOG_NOTICE, "Got DIE command from \"%s\", going down!", Client_Mask( Client ));
1154 GLOBAL BOOLEAN IRC_RESTART( CLIENT *Client, REQUEST *Req )
1156 assert( Client != NULL );
1157 assert( Req != NULL );
1159 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1161 /* Falsche Anzahl Parameter? */
1162 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1164 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
1166 Log( LOG_NOTICE, "Got RESTART command from \"%s\", going down!", Client_Mask( Client ));
1167 NGIRCd_Restart = TRUE;
1172 GLOBAL BOOLEAN IRC_NAMES( CLIENT *Client, REQUEST *Req )
1174 CHAR rpl[COMMAND_LEN];
1177 assert( Client != NULL );
1178 assert( Req != NULL );
1180 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1182 /* Falsche Anzahl Parameter? */
1183 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1185 /* Noch alle User ausgeben, die in keinem Channel sind */
1187 c = Client_First( );
1190 if( Client_Type( c ) == CLIENT_USER )
1192 /* Okay, das ist ein User */
1193 strcat( rpl, Client_ID( c ));
1197 /* Antwort zu lang? Splitten. */
1198 if( strlen( rpl ) > 480 )
1200 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1201 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
1205 c = Client_Next( c );
1209 /* es wurden User gefunden */
1210 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1211 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
1213 return IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), "*" );
1217 GLOBAL BOOLEAN IRC_ISON( CLIENT *Client, REQUEST *Req )
1219 CHAR rpl[COMMAND_LEN];
1224 assert( Client != NULL );
1225 assert( Req != NULL );
1227 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1229 /* Falsche Anzahl Parameter? */
1230 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1232 strcpy( rpl, RPL_ISON_MSG );
1233 for( i = 0; i < Req->argc; i++ )
1235 ptr = strtok( Req->argv[i], " " );
1239 c = Client_GetFromID( ptr );
1240 if( c && ( Client_Type( c ) == CLIENT_USER ))
1242 /* Dieser Nick ist "online" */
1246 ptr = strtok( NULL, " " );
1249 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1251 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1255 GLOBAL BOOLEAN IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1257 CLIENT *from, *target, *c;
1260 assert( Client != NULL );
1261 assert( Req != NULL );
1263 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1265 /* Falsche Anzahl Parameter? */
1266 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1269 c = Client_GetFromID( Req->argv[Req->argc - 1] );
1270 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
1272 /* Empfaenger des WHOIS suchen */
1273 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1275 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1277 /* Forwarden an anderen Server? */
1280 /* angegebenen Ziel-Server suchen */
1281 target = Client_GetFromID( Req->argv[1] );
1282 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
1286 else if( Client_Conn( c ) == NONE )
1288 /* Client ist nicht von uns. Ziel-Server suchen */
1295 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 );
1297 /* Nick, User und Name */
1298 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
1301 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;
1304 if( Client_HasMode( c, 'o' ))
1306 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
1309 /* Idle (nur lokale Clients) */
1310 if( Client_Conn( c ) > NONE )
1312 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
1316 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1320 GLOBAL BOOLEAN IRC_USERHOST( CLIENT *Client, REQUEST *Req )
1322 CHAR rpl[COMMAND_LEN];
1326 assert( Client != NULL );
1327 assert( Req != NULL );
1329 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1331 /* Falsche Anzahl Parameter? */
1332 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1334 if( Req->argc > 5 ) max = 5;
1335 else max = Req->argc;
1337 strcpy( rpl, RPL_USERHOST_MSG );
1338 for( i = 0; i < max; i++ )
1340 c = Client_GetFromID( Req->argv[i] );
1341 if( c && ( Client_Type( c ) == CLIENT_USER ))
1343 /* Dieser Nick ist "online" */
1344 strcat( rpl, Client_ID( c ));
1345 if( Client_HasMode( c, 'o' )) strcat( rpl, "*" );
1347 if( Client_HasMode( c, 'a' )) strcat( rpl, "-" );
1348 else strcat( rpl, "+" );
1349 strcat( rpl, Client_User( c ));
1351 strcat( rpl, Client_Hostname( c ));
1355 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1357 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1358 } /* IRC_USERHOST */
1361 GLOBAL BOOLEAN IRC_ERROR( CLIENT *Client, REQUEST *Req )
1363 assert( Client != NULL );
1364 assert( Req != NULL );
1366 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
1367 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
1373 GLOBAL BOOLEAN IRC_LUSERS( CLIENT *Client, REQUEST *Req )
1375 CLIENT *target, *from;
1378 assert( Client != NULL );
1379 assert( Req != NULL );
1381 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1383 /* Falsche Anzahl Parameter? */
1384 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1386 /* Absender ermitteln */
1387 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1389 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1391 /* An anderen Server forwarden? */
1392 if( Req->argc == 2 )
1394 target = Client_GetFromID( Req->argv[1] );
1395 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
1396 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
1399 /* Wer ist der Absender? */
1400 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1401 else target = Client;
1402 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1404 /* Users, Services und Serevr im Netz */
1405 if( ! IRC_WriteStrClient( target, RPL_LUSERCLIENT_MSG, Client_ID( target ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1407 /* IRC-Operatoren im Netz */
1408 cnt = Client_OperCount( );
1411 if( ! IRC_WriteStrClient( target, RPL_LUSEROP_MSG, Client_ID( target ), cnt )) return DISCONNECTED;
1414 /* Unbekannt Verbindungen */
1415 cnt = Client_UnknownCount( );
1418 if( ! IRC_WriteStrClient( target, RPL_LUSERUNKNOWN_MSG, Client_ID( target ), cnt )) return DISCONNECTED;
1421 /* Channels im Netz */
1422 if( ! IRC_WriteStrClient( target, RPL_LUSERCHANNELS_MSG, Client_ID( target ), Channel_Count( ))) return DISCONNECTED;
1424 /* Channels im Netz */
1425 if( ! IRC_WriteStrClient( target, RPL_LUSERME_MSG, Client_ID( target ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1431 GLOBAL BOOLEAN IRC_LINKS( CLIENT *Client, REQUEST *Req )
1433 CLIENT *target, *from, *c;
1436 assert( Client != NULL );
1437 assert( Req != NULL );
1439 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1441 /* Falsche Anzahl Parameter? */
1442 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1444 /* Server-Mask ermitteln */
1445 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
1448 /* Absender ermitteln */
1449 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1451 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1453 /* An anderen Server forwarden? */
1454 if( Req->argc == 2 )
1456 target = Client_GetFromID( Req->argv[0] );
1457 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
1458 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
1461 /* Wer ist der Absender? */
1462 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1463 else target = Client;
1464 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1466 c = Client_First( );
1469 if( Client_Type( c ) == CLIENT_SERVER )
1471 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;
1473 c = Client_Next( c );
1476 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
1480 GLOBAL BOOLEAN IRC_JOIN( CLIENT *Client, REQUEST *Req )
1485 assert( Client != NULL );
1486 assert( Req != NULL );
1488 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1490 /* Falsche Anzahl Parameter? */
1491 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1493 /* Wer ist der Absender? */
1494 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1495 else target = Client;
1496 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1498 /* Channel-Namen durchgehen */
1499 chan = strtok( Req->argv[0], "," );
1502 if( Client_Type( Client ) == CLIENT_SERVER )
1504 /* Channel-Flags extrahieren */
1505 flags = strchr( chan, 0x7 );
1506 if( flags ) *flags++ = '\0';
1510 if( ! Channel_Join( target, chan ))
1512 /* naechsten Namen ermitteln */
1513 chan = strtok( NULL, "," );
1517 /* An andere Server weiterleiten, an Client bestaetigen */
1518 IRC_WriteStrServersPrefixID( Client, target, "JOIN :%s", chan );
1519 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", chan );
1520 IRC_WriteStrChannelPrefix( Client, Channel_Search( chan ), target, FALSE, "JOIN :%s", chan );
1522 if( Client_Type( Client ) == CLIENT_USER )
1524 /* Topic an Client schicken */
1525 IRC_WriteStrClient( Client, RPL_TOPIC_MSG, Client_ID( Client ), chan, "What a wonderful channel!" );
1528 /* naechsten Namen ermitteln */
1529 chan = strtok( NULL, "," );
1535 GLOBAL BOOLEAN IRC_PART( CLIENT *Client, REQUEST *Req )
1540 assert( Client != NULL );
1541 assert( Req != NULL );
1543 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1545 /* Falsche Anzahl Parameter? */
1546 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1548 /* Wer ist der Absender? */
1549 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1550 else target = Client;
1551 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1553 /* Channel-Namen durchgehen */
1554 chan = strtok( Req->argv[0], "," );
1557 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
1559 /* naechsten Namen ermitteln */
1560 chan = strtok( NULL, "," );
1564 /* naechsten Namen ermitteln */
1565 chan = strtok( NULL, "," );
1571 LOCAL BOOLEAN Hello_User( CLIENT *Client )
1573 assert( Client != NULL );
1575 /* Passwort ueberpruefen */
1576 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
1578 /* Falsches Passwort */
1579 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
1580 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
1581 return DISCONNECTED;
1584 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
1586 /* Andere Server informieren */
1587 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 ));
1589 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
1590 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
1591 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
1592 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
1594 Client_SetType( Client, CLIENT_USER );
1596 return Show_MOTD( Client );
1600 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
1606 assert( Client != NULL );
1608 fd = fopen( Conf_MotdFile, "r" );
1611 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1612 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1615 IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1618 if( ! fgets( line, 126, fd )) break;
1619 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
1620 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
1626 ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
1634 LOCAL VOID Kill_Nick( CHAR *Nick )
1636 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected!", Nick );
1638 Log( LOG_ALERT, "[Kill_Nick() not implemented - OOOPS!]" );