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.44 2002/01/28 00:55:08 alex Exp $
17 * Revision 1.44 2002/01/28 00:55:08 alex
18 * - ein neu connectierender Server wird nun korrekt im Netz bekannt gemacht.
20 * Revision 1.43 2002/01/27 21:56:39 alex
21 * - IRC_WriteStrServersPrefixID() und IRC_WriteStrClientPrefixID() wieder entfernt.
22 * - einige kleinere Fixes bezueglich Channels ...
24 * Revision 1.42 2002/01/27 18:28:01 alex
25 * - bei NICK wurde das falsche Prefix an andere Server weitergegeben.
27 * Revision 1.41 2002/01/27 17:15:49 alex
28 * - anderungen an den Funktions-Prototypen von IRC_WriteStrChannel() und
29 * IRC_WriteStrChannelPrefix(),
30 * - neue: IRC_WriteStrClientPrefixID() und IRC_WriteStrServersPrefixID().
32 * Revision 1.40 2002/01/26 18:43:11 alex
33 * - neue Funktionen IRC_WriteStrChannelPrefix() und IRC_WriteStrChannel(),
34 * die IRC_Write_xxx_Related() sind dafuer entfallen.
35 * - IRC_PRIVMSG() kann nun auch mit Channels als Ziel umgehen.
37 * Revision 1.39 2002/01/21 00:05:11 alex
38 * - neue Funktionen IRC_JOIN und IRC_PART begonnen, ebenso die Funktionen
39 * IRC_WriteStrRelatedPrefix und IRC_WriteStrRelatedChannelPrefix().
40 * - diverse Aenderungen im Zusammenhang mit Channels.
42 * Revision 1.38 2002/01/18 15:31:32 alex
43 * - die User-Modes bei einem NICK von einem Server wurden falsch uebernommen.
45 * Revision 1.37 2002/01/16 22:10:18 alex
46 * - IRC_LUSERS() implementiert.
48 * Revision 1.36 2002/01/11 23:50:55 alex
49 * - LINKS implementiert, LUSERS begonnen.
51 * Revision 1.35 2002/01/09 21:30:45 alex
52 * - WHOIS wurde faelschlicherweise an User geforwarded statt vom Server beantwortet.
54 * Revision 1.34 2002/01/09 01:09:58 alex
55 * - WHOIS wird im "Strict-RFC-Mode" nicht mehr automatisch geforwarded,
56 * - andere Server werden nun ueber bisherige Server und User informiert.
58 * Revision 1.33 2002/01/07 23:42:12 alex
59 * - Es werden fuer alle Server eigene Token generiert,
60 * - QUIT von einem Server fuer einen User wird an andere Server geforwarded,
61 * - ebenso NICK-Befehle, die "fremde" User einfuehren.
63 * Revision 1.32 2002/01/07 16:02:36 alex
64 * - Loglevel von Remote-Mode-Aenderungen angepasst (nun Debug).
65 * - Im Debug-Mode werden nun auch PING's protokolliert.
67 * Revision 1.31 2002/01/07 15:39:46 alex
68 * - Server nimmt nun Server-Links an: PASS und SERVER entsprechend angepasst.
69 * - MODE und NICK melden nun die Aenderungen an andere Server.
71 * Revision 1.30 2002/01/06 15:21:29 alex
72 * - Loglevel und Meldungen nochmals ueberarbeitet.
73 * - QUIT und SQUIT forwarden nun den Grund der Trennung,
74 * - WHOIS wird nun immer an den "Original-Server" weitergeleitet.
76 * Revision 1.29 2002/01/05 23:24:54 alex
77 * - WHOIS erweitert: Anfragen koennen an andere Server weitergeleitet werden.
78 * - Vorbereitungen fuer Ident-Abfragen bei neuen Client-Strukturen.
80 * Revision 1.28 2002/01/05 20:08:02 alex
81 * - Div. Aenderungen fuer die Server-Links (u.a. WHOIS, QUIT, NICK angepasst).
82 * - Neue Funktionen IRC_WriteStrServer() und IRC_WriteStrServerPrefix().
84 * Revision 1.27 2002/01/05 19:15:03 alex
85 * - Fehlerpruefung bei select() in der "Hauptschleife" korrigiert.
87 * Revision 1.26 2002/01/05 16:51:18 alex
88 * - das Passwort von Servern wird nun ueberprueft (PASS- und SERVER-Befehl).
90 * Revision 1.25 2002/01/05 00:48:33 alex
91 * - bei SQUIT wurde immer die Verbindung getrennt, auch bei Remote-Servern.
93 * Revision 1.24 2002/01/04 17:58:44 alex
94 * - IRC_WriteStrXXX()-Funktionen eingefuehrt, groessere Anpassungen daran.
95 * - neuer Befehl SQUIT, QUIT an Server-Links angepasst.
97 * Revision 1.23 2002/01/04 01:36:40 alex
98 * - Loglevel ein wenig angepasst.
100 * Revision 1.22 2002/01/04 01:21:47 alex
101 * - Client-Strukruren werden nur noch ueber Funktionen angesprochen.
102 * - Weitere Anpassungen und Erweiterungen der Server-Links.
104 * Revision 1.21 2002/01/03 02:26:51 alex
105 * - neue Befehle SERVER und NJOIN begonnen,
106 * - begonnen, diverse IRC-Befehle an Server-Links anzupassen.
108 * Revision 1.20 2002/01/02 12:46:41 alex
109 * - die Gross- und Kleinschreibung des Nicks kann mit NICK nun geaendert werden.
111 * Revision 1.19 2002/01/02 02:51:39 alex
112 * - Copyright-Texte angepasst.
113 * - neuer Befehl "ERROR".
115 * Revision 1.17 2001/12/31 15:33:13 alex
116 * - neuer Befehl NAMES, kleinere Bugfixes.
117 * - Bug bei PING behoben: war zu restriktiv implementiert :-)
119 * Revision 1.16 2001/12/31 02:18:51 alex
120 * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
121 * - neuen Header "defines.h" mit (fast) allen Konstanten.
122 * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
124 * Revision 1.15 2001/12/30 19:26:11 alex
125 * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
127 * Revision 1.14 2001/12/30 11:42:00 alex
128 * - der Server meldet nun eine ordentliche "Start-Zeit".
130 * Revision 1.13 2001/12/29 03:10:06 alex
131 * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
132 * - neue configure-Optione "--enable-strict-rfc".
134 * Revision 1.12 2001/12/27 19:17:26 alex
135 * - neue Befehle PRIVMSG, NOTICE, PING.
137 * Revision 1.11 2001/12/27 16:55:41 alex
138 * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
140 * Revision 1.10 2001/12/26 22:48:53 alex
141 * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
143 * Revision 1.9 2001/12/26 14:45:37 alex
146 * Revision 1.8 2001/12/26 03:21:46 alex
147 * - PING/PONG-Befehle implementiert,
148 * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
150 * Revision 1.7 2001/12/25 23:25:18 alex
151 * - und nochmal Aenderungen am Logging ;-)
153 * Revision 1.6 2001/12/25 23:13:33 alex
154 * - Debug-Meldungen angepasst.
156 * Revision 1.5 2001/12/25 22:02:42 alex
157 * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
159 * Revision 1.4 2001/12/25 19:19:30 alex
160 * - bessere Fehler-Abfragen, diverse Bugfixes.
161 * - Nicks werden nur einmal vergeben :-)
162 * - /MOTD wird unterstuetzt.
164 * Revision 1.3 2001/12/24 01:34:06 alex
165 * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
166 * - MOTD-Ausgabe begonnen zu implementieren.
168 * Revision 1.2 2001/12/23 21:57:16 alex
169 * - erste IRC-Befehle zu implementieren begonnen.
171 * Revision 1.1 2001/12/14 08:13:43 alex
172 * - neues Modul begonnen :-)
193 #include "messages.h"
201 #define CONNECTED TRUE
202 #define DISCONNECTED FALSE
205 LOCAL BOOLEAN Hello_User( CLIENT *Client );
206 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
208 LOCAL VOID Kill_Nick( CHAR *Nick );
211 GLOBAL VOID IRC_Init( VOID )
216 GLOBAL VOID IRC_Exit( VOID )
221 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CHAR *Format, ... )
224 BOOLEAN ok = CONNECTED;
227 assert( Client != NULL );
228 assert( Format != NULL );
230 va_start( ap, Format );
231 vsnprintf( buffer, 1000, Format, ap );
234 /* an den Client selber */
235 ok = IRC_WriteStrClientPrefix( Client, Client_ThisServer( ), buffer );
238 } /* IRC_WriteStrClient */
241 GLOBAL BOOLEAN IRC_WriteStrClientPrefix( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
243 /* Text an Clients, lokal bzw. remote, senden. */
248 assert( Client != NULL );
249 assert( Format != NULL );
250 assert( Prefix != NULL );
252 va_start( ap, Format );
253 vsnprintf( buffer, 1000, Format, ap );
256 return Conn_WriteStr( Client_Conn( Client_NextHop( Client )), ":%s %s", Client_ID( Prefix ), buffer );
257 } /* IRC_WriteStrClientPrefix */
260 GLOBAL BOOLEAN IRC_WriteStrChannel( CLIENT *Client, CHANNEL *Chan, BOOLEAN Remote, CHAR *Format, ... )
265 assert( Client != NULL );
266 assert( Format != NULL );
268 va_start( ap, Format );
269 vsnprintf( buffer, 1000, Format, ap );
272 return IRC_WriteStrChannelPrefix( Client, Chan, Client_ThisServer( ), Remote, buffer );
273 } /* IRC_WriteStrChannel */
276 GLOBAL BOOLEAN IRC_WriteStrChannelPrefix( CLIENT *Client, CHANNEL *Chan, CLIENT *Prefix, BOOLEAN Remote, CHAR *Format, ... )
279 BOOLEAN sock[MAX_CONNECTIONS], ok = CONNECTED, i;
285 assert( Client != NULL );
286 assert( Chan != NULL );
287 assert( Prefix != NULL );
288 assert( Format != NULL );
290 va_start( ap, Format );
291 vsnprintf( buffer, 1000, Format, ap );
294 for( i = 0; i < MAX_CONNECTIONS; i++ ) sock[i] = FALSE;
296 /* An alle Clients, die in den selben Channels sind.
297 * Dabei aber nur einmal je Remote-Server */
298 cl2chan = Channel_FirstMember( Chan );
301 c = Channel_GetClient( cl2chan );
304 if( Client_Conn( c ) <= NONE ) c = NULL;
305 else if( Client_Type( c ) == CLIENT_SERVER ) c = NULL;
307 if( c ) c = Client_NextHop( c );
309 if( c && ( c != Client ))
311 /* Ok, anderer Client */
312 s = Client_Conn( c );
314 assert( s < MAX_CONNECTIONS );
317 cl2chan = Channel_NextMember( Chan, cl2chan );
321 for( i = 0; i < MAX_CONNECTIONS; i++ )
325 ok = Conn_WriteStr( i, ":%s %s", Client_ID( Prefix ), buffer );
330 } /* IRC_WriteStrChannelPrefix */
333 GLOBAL VOID IRC_WriteStrServers( CLIENT *ExceptOf, CHAR *Format, ... )
338 assert( Format != NULL );
340 va_start( ap, Format );
341 vsnprintf( buffer, 1000, Format, ap );
344 /* an den Client selber */
345 return IRC_WriteStrServersPrefix( ExceptOf, Client_ThisServer( ), buffer );
346 } /* IRC_WriteStrServers */
349 GLOBAL VOID IRC_WriteStrServersPrefix( CLIENT *ExceptOf, CLIENT *Prefix, CHAR *Format, ... )
355 assert( Format != NULL );
356 assert( Prefix != NULL );
358 va_start( ap, Format );
359 vsnprintf( buffer, 1000, Format, ap );
365 if(( Client_Type( c ) == CLIENT_SERVER ) && ( Client_Conn( c ) > NONE ) && ( c != Client_ThisServer( )) && ( c != ExceptOf ))
367 /* Ziel-Server gefunden */
368 IRC_WriteStrClientPrefix( c, Prefix, buffer );
370 c = Client_Next( c );
372 } /* IRC_WriteStrServersPrefix */
375 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
377 assert( Client != NULL );
378 assert( Req != NULL );
380 /* Fehler liefern, wenn kein lokaler Client */
381 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
383 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
385 /* noch nicht registrierte unbekannte Verbindung */
386 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
388 /* Passwort speichern */
389 Client_SetPassword( Client, Req->argv[0] );
391 Client_SetType( Client, CLIENT_GOTPASS );
394 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
396 /* noch nicht registrierte Server-Verbindung */
397 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
399 /* Passwort speichern */
400 Client_SetPassword( Client, Req->argv[0] );
402 Client_SetType( Client, CLIENT_GOTPASSSERVER );
405 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
407 /* Falsche Anzahl Parameter? */
408 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
410 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
414 GLOBAL BOOLEAN IRC_SERVER( CLIENT *Client, REQUEST *Req )
416 CHAR str[LINE_LEN], *ptr;
421 assert( Client != NULL );
422 assert( Req != NULL );
424 /* Fehler liefern, wenn kein lokaler Client */
425 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
427 if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
429 /* Verbindung soll als Server-Server-Verbindung registriert werden */
430 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
432 /* Falsche Anzahl Parameter? */
433 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
435 /* Ist dieser Server bei uns konfiguriert? */
436 for( i = 0; i < Conf_Server_Count; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
437 if( i >= Conf_Server_Count )
439 /* Server ist nicht konfiguriert! */
440 Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
441 Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", TRUE );
444 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd ) != 0 )
446 /* Falsches Passwort */
447 Log( LOG_ERR, "Connection %d: Bad password for server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
448 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
452 /* Ist ein Server mit dieser ID bereits registriert? */
453 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
455 /* Server-Strukturen fuellen ;-) */
456 Client_SetID( Client, Req->argv[0] );
457 Client_SetHops( Client, 1 );
458 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
460 /* Meldet sich der Server bei uns an? */
463 /* Unseren SERVER- und PASS-Befehl senden */
465 if( ! IRC_WriteStrClient( Client, "PASS %s "PASSSERVERADD, Conf_Server[i].pwd )) ok = FALSE;
466 else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
469 Conn_Close( Client_Conn( Client ), "Unexpected server behavior!", NULL, FALSE );
472 Client_SetIntroducer( Client, Client );
473 Client_SetToken( Client, 1 );
475 else Client_SetToken( Client, atoi( Req->argv[1] ));
477 Log( LOG_NOTICE, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), Client_Conn( Client ));
479 Client_SetType( Client, CLIENT_SERVER );
481 /* Alle bisherigen Server dem neuen Server bekannt machen,
482 * die bisherigen Server ueber den neuen informierenn */
486 if(( Client_Type( c ) == CLIENT_SERVER ) && ( c != Client ) && ( c != Client_ThisServer( )))
488 if( Client_Conn( c ) > NONE )
490 /* Dem gefundenen Server gleich den neuen
491 * Server bekannt machen */
492 if( ! IRC_WriteStrClient( c, "SERVER %s %d %d :%s", Client_ID( Client ), Client_Hops( Client ) + 1, Client_MyToken( Client ), Client_Info( Client ))) return DISCONNECTED;
495 /* Den neuen Server ueber den alten informieren */
496 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;
498 c = Client_Next( c );
501 /* alle User dem neuen Server bekannt machen */
505 if( Client_Type( c ) == CLIENT_USER )
507 /* User an neuen Server melden */
508 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;
510 c = Client_Next( c );
515 else if( Client_Type( Client ) == CLIENT_SERVER )
517 /* Neuer Server wird im Netz angekuendigt */
519 /* Falsche Anzahl Parameter? */
520 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
522 /* Ist ein Server mit dieser ID bereits registriert? */
523 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
525 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
526 ptr = strchr( Req->argv[3] + 2, '[' );
527 if( ! ptr ) ptr = Req->argv[3];
529 from = Client_GetFromID( Req->prefix );
532 /* Hm, Server, der diesen einfuehrt, ist nicht bekannt!? */
533 Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
534 Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", TRUE );
538 /* Neue Client-Struktur anlegen */
539 c = Client_NewRemoteServer( Client, Req->argv[0], atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, TRUE );
542 /* Neue Client-Struktur konnte nicht angelegt werden */
543 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
544 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", TRUE );
548 /* Log-Meldung zusammenbauen und ausgeben */
549 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) sprintf( str, "connected to %s, ", Client_ID( from ));
550 else strcpy( str, "" );
551 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": "" );
553 /* Andere Server informieren */
554 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
558 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
562 GLOBAL BOOLEAN IRC_NJOIN( CLIENT *Client, REQUEST *Req )
567 assert( Client != NULL );
568 assert( Req != NULL );
570 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTEREDSERVER_MSG, Client_ID( Client ));
572 /* Falsche Anzahl Parameter? */
573 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
576 ptr = strtok( Req->argv[1], "," );
579 /* Prefixe abschneiden */
580 while(( *ptr == '@' ) || ( *ptr == '+' )) ptr++;
582 c = Client_GetFromID( ptr );
583 if( c ) Channel_Join( c, chan );
584 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, chan );
586 /* naechsten Nick suchen */
587 ptr = strtok( NULL, "," );
594 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
596 CLIENT *intr_c, *target, *c;
599 assert( Client != NULL );
600 assert( Req != NULL );
602 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
604 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 ))
606 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 ))
609 /* User-Registrierung bzw. Nick-Aenderung */
611 /* Falsche Anzahl Parameter? */
612 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
614 /* "Ziel-Client" ermitteln */
615 if( Client_Type( Client ) == CLIENT_SERVER )
617 target = Client_GetFromID( Req->prefix );
618 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
622 /* Ist der Client "restricted"? */
623 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
627 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
628 * wir nichts. So macht es das Original und mind. Snak hat probleme,
629 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
631 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
634 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
635 * will nur die Gross- und Kleinschreibung aendern. Das darf
636 * er natuerlich machen :-) */
637 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
639 if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
642 if( Client_Type( Client ) == CLIENT_USER )
644 /* Nick-Aenderung: allen mitteilen! */
645 Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
646 IRC_WriteStrClient( Client, "NICK :%s", Req->argv[0] );
647 IRC_WriteStrServersPrefix( NULL, Client, "NICK :%s", Req->argv[0] );
649 else if( Client_Type( Client ) == CLIENT_SERVER )
651 /* Nick-Aenderung: allen mitteilen! */
652 Log( LOG_DEBUG, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
653 IRC_WriteStrServersPrefix( Client, target, "NICK :%s", Req->argv[0] );
656 /* Client-Nick registrieren */
657 Client_SetID( target, Req->argv[0] );
659 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
662 Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client_Conn( Client ));
663 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
664 else Client_SetType( Client, CLIENT_GOTNICK );
668 else if( Client_Type( Client ) == CLIENT_SERVER )
670 /* Server fuehrt neuen Client ein */
672 /* Falsche Anzahl Parameter? */
673 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
675 /* Nick ueberpruefen */
676 c = Client_GetFromID( Req->argv[0] );
679 /* Der neue Nick ist auf diesem Server bereits registriert:
680 * sowohl der neue, als auch der alte Client muessen nun
681 * disconnectiert werden. */
682 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
683 Kill_Nick( Req->argv[0] );
687 /* Server, zu dem der Client connectiert ist, suchen */
688 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
691 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
692 Kill_Nick( Req->argv[0] );
696 /* Neue Client-Struktur anlegen */
697 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 );
700 /* Eine neue Client-Struktur konnte nicht angelegt werden.
701 * Der Client muss disconnectiert werden, damit der Netz-
702 * status konsistent bleibt. */
703 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
704 Kill_Nick( Req->argv[0] );
708 modes = Client_Modes( c );
709 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": "" );
710 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": "" );
712 /* Andere Server, ausser dem Introducer, informieren */
713 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] );
717 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
721 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
723 assert( Client != NULL );
724 assert( Req != NULL );
727 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
729 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
732 /* Falsche Anzahl Parameter? */
733 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
735 Client_SetUser( Client, Req->argv[0], FALSE );
736 Client_SetInfo( Client, Req->argv[3] );
738 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client_Conn( Client ));
739 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
740 else Client_SetType( Client, CLIENT_GOTUSER );
743 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
745 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
747 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
751 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
755 assert( Client != NULL );
756 assert( Req != NULL );
758 if(( Client_Type( Client ) == CLIENT_USER ) || ( Client_Type( Client ) == CLIENT_SERVICE ))
762 /* Falsche Anzahl Parameter? */
763 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
765 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
766 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
770 else if ( Client_Type( Client ) == CLIENT_SERVER )
774 /* Falsche Anzahl Parameter? */
775 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
777 target = Client_Search( Req->prefix );
780 Log( LOG_ERR, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
784 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL );
785 else Client_Destroy( target, "Got QUIT command.", Req->argv[0] );
789 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
793 GLOBAL BOOLEAN IRC_SQUIT( CLIENT *Client, REQUEST *Req )
797 assert( Client != NULL );
798 assert( Req != NULL );
800 /* SQUIT ist nur fuer Server erlaubt */
801 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
803 /* Falsche Anzahl Parameter? */
804 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
806 target = Client_GetFromID( Req->argv[0] );
809 Log( LOG_ERR, "Got SQUIT from %s for unknown server \%s\"!?", Client_ID( Client ), Req->argv[0] );
813 if( target == Client ) Log( LOG_DEBUG, "Got SQUIT from %s: %s", Client_ID( Client ), Req->argv[1] );
814 else Log( LOG_DEBUG, "Got SQUIT from %s for %s: %s", Client_ID( Client ), Client_ID( target ), Req->argv[1] );
816 /* SQUIT an alle Server weiterleiten */
817 IRC_WriteStrServers( Client, "SQUIT %s :%s", Req->argv[0], Req->argv[1] );
819 if( Client_Conn( target ) > NONE )
821 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), "Got SQUIT command.", Req->argv[1], TRUE );
822 else Conn_Close( Client_Conn( target ), "Got SQUIT command.", NULL, TRUE );
827 Client_Destroy( target, "Got SQUIT command.", Req->argv[1] );
833 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
835 assert( Client != NULL );
836 assert( Req != NULL );
838 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
840 /* Falsche Anzahl Parameter? */
841 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
842 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
844 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
845 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
849 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
851 assert( Client != NULL );
852 assert( Req != NULL );
854 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
856 /* Falsche Anzahl Parameter? */
857 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
858 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
860 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
861 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
863 Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
868 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
870 assert( Client != NULL );
871 assert( Req != NULL );
873 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
875 /* Falsche Anzahl Parameter? */
876 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
878 return Show_MOTD( Client );
882 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
887 assert( Client != NULL );
888 assert( Req != NULL );
890 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
892 /* Falsche Anzahl Parameter? */
893 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
894 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
895 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
897 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
899 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
901 to = Client_Search( Req->argv[0] );
904 /* Okay, Ziel ist ein User */
905 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
906 return IRC_WriteStrClientPrefix( to, from, "PRIVMSG %s :%s", Client_ID( to ), Req->argv[1] );
909 chan = Channel_Search( Req->argv[0] );
912 /* Okay, Ziel ist ein Channel */
913 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
914 return IRC_WriteStrChannelPrefix( Client, chan, from, TRUE, "PRIVMSG %s :%s", Req->argv[0], Req->argv[1] );
917 return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
921 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
925 assert( Client != NULL );
926 assert( Req != NULL );
928 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
930 /* Falsche Anzahl Parameter? */
931 if( Req->argc != 2 ) return CONNECTED;
933 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
935 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
937 to = Client_Search( Req->argv[0] );
940 /* Okay, Ziel ist ein User */
941 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
943 else return CONNECTED;
947 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
949 CHAR x[2], new_modes[CLIENT_MODE_LEN], *ptr;
953 assert( Client != NULL );
954 assert( Req != NULL );
956 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
958 /* Falsche Anzahl Parameter? */
959 if(( Req->argc > 2 ) || ( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
961 /* "Ziel-Client" suchen */
962 target = Client_Search( Req->argv[0] );
964 /* Wer ist der Anfragende? */
965 if( Client_Type( Client ) == CLIENT_USER )
967 /* User: MODE ist nur fuer sich selber zulaessig */
968 if( target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
972 /* Server: gibt es den Client ueberhaupt? */
973 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
976 /* Werden die Modes erfragt? */
977 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( Client ));
981 /* Sollen Modes gesetzt oder geloescht werden? */
982 if( *ptr == '+' ) set = TRUE;
983 else if( *ptr == '-' ) set = FALSE;
984 else return IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
986 /* Reply-String mit Aenderungen vorbereiten */
987 if( set ) strcpy( new_modes, "+" );
988 else strcpy( new_modes, "-" );
996 if( Client_Type( Client ) == CLIENT_SERVER )
1010 /* restricted (kann nur gesetzt werden) */
1011 if( set ) x[0] = 'r';
1012 else ok = IRC_WriteStrClient( target, ERR_RESTRICTED_MSG, Client_ID( target ));
1015 /* operator (kann nur geloescht werden) */
1018 Client_SetOperByMe( target, FALSE );
1021 else ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
1024 ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
1031 if( ! x[0] ) continue;
1033 /* Okay, gueltigen Mode gefunden */
1036 /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
1037 if( Client_ModeAdd( target, x[0] )) strcat( new_modes, x );
1041 /* Modes geloescht. Wenn der Client ihn hatte: merken */
1042 if( Client_ModeDel( target, x[0] )) strcat( new_modes, x );
1046 /* Geanderte Modes? */
1049 if( Client_Type( Client ) == CLIENT_SERVER )
1051 /* Modes an andere Server forwarden */
1052 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :%s", Client_ID( target ), new_modes );
1056 /* Bestaetigung an Client schicken & andere Server informieren */
1057 ok = IRC_WriteStrClient( Client, "MODE %s :%s", Client_ID( target ), new_modes );
1058 IRC_WriteStrServers( Client, "MODE %s :%s", Client_ID( target ), new_modes );
1060 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( target ), Client_Modes( target ));
1066 GLOBAL BOOLEAN IRC_OPER( CLIENT *Client, REQUEST *Req )
1070 assert( Client != NULL );
1071 assert( Req != NULL );
1073 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1075 /* Falsche Anzahl Parameter? */
1076 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1078 /* Operator suchen */
1079 for( i = 0; i < Conf_Oper_Count; i++)
1081 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
1083 if( i >= Conf_Oper_Count )
1085 Log( LOG_WARNING, "Got invalid OPER from \"%s\": Name \"%s\" not configured!", Client_Mask( Client ), Req->argv[0] );
1086 return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
1089 /* Stimmt das Passwort? */
1090 if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
1092 Log( LOG_WARNING, "Got invalid OPER from \"%s\": Bad password for \"%s\"!", Client_Mask( Client ), Conf_Oper[i].name );
1093 return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
1096 if( ! Client_HasMode( Client, 'o' ))
1098 /* noch kein o-Mode gesetzt */
1099 Client_ModeAdd( Client, 'o' );
1100 if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
1101 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
1104 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
1106 Client_SetOperByMe( Client, TRUE );
1107 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
1111 GLOBAL BOOLEAN IRC_DIE( CLIENT *Client, REQUEST *Req )
1113 assert( Client != NULL );
1114 assert( Req != NULL );
1116 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1118 /* Falsche Anzahl Parameter? */
1119 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1121 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
1123 Log( LOG_NOTICE, "Got DIE command from \"%s\", going down!", Client_Mask( Client ));
1129 GLOBAL BOOLEAN IRC_RESTART( CLIENT *Client, REQUEST *Req )
1131 assert( Client != NULL );
1132 assert( Req != NULL );
1134 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1136 /* Falsche Anzahl Parameter? */
1137 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1139 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
1141 Log( LOG_NOTICE, "Got RESTART command from \"%s\", going down!", Client_Mask( Client ));
1142 NGIRCd_Restart = TRUE;
1147 GLOBAL BOOLEAN IRC_NAMES( CLIENT *Client, REQUEST *Req )
1149 CHAR rpl[COMMAND_LEN];
1152 assert( Client != NULL );
1153 assert( Req != NULL );
1155 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1157 /* Falsche Anzahl Parameter? */
1158 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1160 /* Noch alle User ausgeben, die in keinem Channel sind */
1162 c = Client_First( );
1165 if( Client_Type( c ) == CLIENT_USER )
1167 /* Okay, das ist ein User */
1168 strcat( rpl, Client_ID( c ));
1172 /* Antwort zu lang? Splitten. */
1173 if( strlen( rpl ) > 480 )
1175 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1176 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
1180 c = Client_Next( c );
1184 /* es wurden User gefunden */
1185 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1186 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
1188 return IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), "*" );
1192 GLOBAL BOOLEAN IRC_ISON( CLIENT *Client, REQUEST *Req )
1194 CHAR rpl[COMMAND_LEN];
1199 assert( Client != NULL );
1200 assert( Req != NULL );
1202 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1204 /* Falsche Anzahl Parameter? */
1205 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1207 strcpy( rpl, RPL_ISON_MSG );
1208 for( i = 0; i < Req->argc; i++ )
1210 ptr = strtok( Req->argv[i], " " );
1214 c = Client_GetFromID( ptr );
1215 if( c && ( Client_Type( c ) == CLIENT_USER ))
1217 /* Dieser Nick ist "online" */
1221 ptr = strtok( NULL, " " );
1224 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1226 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1230 GLOBAL BOOLEAN IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1232 CLIENT *from, *target, *c;
1235 assert( Client != NULL );
1236 assert( Req != NULL );
1238 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1240 /* Falsche Anzahl Parameter? */
1241 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1244 c = Client_GetFromID( Req->argv[Req->argc - 1] );
1245 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
1247 /* Empfaenger des WHOIS suchen */
1248 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1250 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1252 /* Forwarden an anderen Server? */
1255 /* angegebenen Ziel-Server suchen */
1256 target = Client_GetFromID( Req->argv[1] );
1257 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
1261 else if( Client_Conn( c ) == NONE )
1263 /* Client ist nicht von uns. Ziel-Server suchen */
1270 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 );
1272 /* Nick, User und Name */
1273 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
1276 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;
1279 if( Client_HasMode( c, 'o' ))
1281 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
1284 /* Idle (nur lokale Clients) */
1285 if( Client_Conn( c ) > NONE )
1287 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
1291 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1295 GLOBAL BOOLEAN IRC_USERHOST( CLIENT *Client, REQUEST *Req )
1297 CHAR rpl[COMMAND_LEN];
1301 assert( Client != NULL );
1302 assert( Req != NULL );
1304 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1306 /* Falsche Anzahl Parameter? */
1307 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1309 if( Req->argc > 5 ) max = 5;
1310 else max = Req->argc;
1312 strcpy( rpl, RPL_USERHOST_MSG );
1313 for( i = 0; i < max; i++ )
1315 c = Client_GetFromID( Req->argv[i] );
1316 if( c && ( Client_Type( c ) == CLIENT_USER ))
1318 /* Dieser Nick ist "online" */
1319 strcat( rpl, Client_ID( c ));
1320 if( Client_HasMode( c, 'o' )) strcat( rpl, "*" );
1322 if( Client_HasMode( c, 'a' )) strcat( rpl, "-" );
1323 else strcat( rpl, "+" );
1324 strcat( rpl, Client_User( c ));
1326 strcat( rpl, Client_Hostname( c ));
1330 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1332 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1333 } /* IRC_USERHOST */
1336 GLOBAL BOOLEAN IRC_ERROR( CLIENT *Client, REQUEST *Req )
1338 assert( Client != NULL );
1339 assert( Req != NULL );
1341 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
1342 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
1348 GLOBAL BOOLEAN IRC_LUSERS( CLIENT *Client, REQUEST *Req )
1350 CLIENT *target, *from;
1353 assert( Client != NULL );
1354 assert( Req != NULL );
1356 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1358 /* Falsche Anzahl Parameter? */
1359 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1361 /* Absender ermitteln */
1362 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1364 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1366 /* An anderen Server forwarden? */
1367 if( Req->argc == 2 )
1369 target = Client_GetFromID( Req->argv[1] );
1370 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
1371 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
1374 /* Wer ist der Absender? */
1375 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1376 else target = Client;
1377 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1379 /* Users, Services und Serevr im Netz */
1380 if( ! IRC_WriteStrClient( target, RPL_LUSERCLIENT_MSG, Client_ID( target ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1382 /* IRC-Operatoren im Netz */
1383 cnt = Client_OperCount( );
1386 if( ! IRC_WriteStrClient( target, RPL_LUSEROP_MSG, Client_ID( target ), cnt )) return DISCONNECTED;
1389 /* Unbekannt Verbindungen */
1390 cnt = Client_UnknownCount( );
1393 if( ! IRC_WriteStrClient( target, RPL_LUSERUNKNOWN_MSG, Client_ID( target ), cnt )) return DISCONNECTED;
1396 /* Channels im Netz */
1397 if( ! IRC_WriteStrClient( target, RPL_LUSERCHANNELS_MSG, Client_ID( target ), Channel_Count( ))) return DISCONNECTED;
1399 /* Channels im Netz */
1400 if( ! IRC_WriteStrClient( target, RPL_LUSERME_MSG, Client_ID( target ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1406 GLOBAL BOOLEAN IRC_LINKS( CLIENT *Client, REQUEST *Req )
1408 CLIENT *target, *from, *c;
1411 assert( Client != NULL );
1412 assert( Req != NULL );
1414 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1416 /* Falsche Anzahl Parameter? */
1417 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1419 /* Server-Mask ermitteln */
1420 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
1423 /* Absender ermitteln */
1424 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1426 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1428 /* An anderen Server forwarden? */
1429 if( Req->argc == 2 )
1431 target = Client_GetFromID( Req->argv[0] );
1432 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
1433 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
1436 /* Wer ist der Absender? */
1437 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1438 else target = Client;
1439 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1441 c = Client_First( );
1444 if( Client_Type( c ) == CLIENT_SERVER )
1446 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;
1448 c = Client_Next( c );
1451 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
1455 GLOBAL BOOLEAN IRC_JOIN( CLIENT *Client, REQUEST *Req )
1460 assert( Client != NULL );
1461 assert( Req != NULL );
1463 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1465 /* Falsche Anzahl Parameter? */
1466 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1468 /* Wer ist der Absender? */
1469 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1470 else target = Client;
1471 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1473 /* Channel-Namen durchgehen */
1474 chan = strtok( Req->argv[0], "," );
1477 if( Client_Type( Client ) == CLIENT_SERVER )
1479 /* Channel-Flags extrahieren */
1480 flags = strchr( chan, 0x7 );
1481 if( flags ) *flags++ = '\0';
1485 if( ! Channel_Join( target, chan ))
1487 /* naechsten Namen ermitteln */
1488 chan = strtok( NULL, "," );
1492 /* An andere Server weiterleiten */
1493 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s", chan );
1494 IRC_WriteStrChannelPrefix( Client, Channel_Search( chan ), target, FALSE, "JOIN :%s", chan );
1496 if( Client_Type( Client ) == CLIENT_USER )
1498 /* an Client bestaetigen */
1499 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", chan );
1500 /* Topic an Client schicken */
1501 IRC_WriteStrClient( Client, RPL_TOPIC_MSG, Client_ID( Client ), chan, "What a wonderful channel!" );
1504 /* naechsten Namen ermitteln */
1505 chan = strtok( NULL, "," );
1511 GLOBAL BOOLEAN IRC_PART( CLIENT *Client, REQUEST *Req )
1516 assert( Client != NULL );
1517 assert( Req != NULL );
1519 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1521 /* Falsche Anzahl Parameter? */
1522 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1524 /* Wer ist der Absender? */
1525 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1526 else target = Client;
1527 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1529 /* Channel-Namen durchgehen */
1530 chan = strtok( Req->argv[0], "," );
1533 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
1535 /* naechsten Namen ermitteln */
1536 chan = strtok( NULL, "," );
1540 /* naechsten Namen ermitteln */
1541 chan = strtok( NULL, "," );
1547 LOCAL BOOLEAN Hello_User( CLIENT *Client )
1549 assert( Client != NULL );
1551 /* Passwort ueberpruefen */
1552 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
1554 /* Falsches Passwort */
1555 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
1556 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
1557 return DISCONNECTED;
1560 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
1562 /* Andere Server informieren */
1563 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 ));
1565 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
1566 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
1567 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
1568 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
1570 Client_SetType( Client, CLIENT_USER );
1572 return Show_MOTD( Client );
1576 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
1582 assert( Client != NULL );
1584 fd = fopen( Conf_MotdFile, "r" );
1587 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1588 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1591 IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1594 if( ! fgets( line, 126, fd )) break;
1595 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
1596 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
1602 ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
1610 LOCAL VOID Kill_Nick( CHAR *Nick )
1612 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected!", Nick );
1614 Log( LOG_ALERT, "[Kill_Nick() not implemented - OOOPS!]" );