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.22 2002/01/04 01:21:47 alex Exp $
17 * Revision 1.22 2002/01/04 01:21:47 alex
18 * - Client-Strukruren werden nur noch ueber Funktionen angesprochen.
19 * - Weitere Anpassungen und Erweiterungen der Server-Links.
21 * Revision 1.21 2002/01/03 02:26:51 alex
22 * - neue Befehle SERVER und NJOIN begonnen,
23 * - begonnen, diverse IRC-Befehle an Server-Links anzupassen.
25 * Revision 1.20 2002/01/02 12:46:41 alex
26 * - die Gross- und Kleinschreibung des Nicks kann mit NICK nun geaendert werden.
28 * Revision 1.19 2002/01/02 02:51:39 alex
29 * - Copyright-Texte angepasst.
30 * - neuer Befehl "ERROR".
32 * Revision 1.17 2001/12/31 15:33:13 alex
33 * - neuer Befehl NAMES, kleinere Bugfixes.
34 * - Bug bei PING behoben: war zu restriktiv implementiert :-)
36 * Revision 1.16 2001/12/31 02:18:51 alex
37 * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
38 * - neuen Header "defines.h" mit (fast) allen Konstanten.
39 * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
41 * Revision 1.15 2001/12/30 19:26:11 alex
42 * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
44 * Revision 1.14 2001/12/30 11:42:00 alex
45 * - der Server meldet nun eine ordentliche "Start-Zeit".
47 * Revision 1.13 2001/12/29 03:10:06 alex
48 * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
49 * - neue configure-Optione "--enable-strict-rfc".
51 * Revision 1.12 2001/12/27 19:17:26 alex
52 * - neue Befehle PRIVMSG, NOTICE, PING.
54 * Revision 1.11 2001/12/27 16:55:41 alex
55 * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
57 * Revision 1.10 2001/12/26 22:48:53 alex
58 * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
60 * Revision 1.9 2001/12/26 14:45:37 alex
63 * Revision 1.8 2001/12/26 03:21:46 alex
64 * - PING/PONG-Befehle implementiert,
65 * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
67 * Revision 1.7 2001/12/25 23:25:18 alex
68 * - und nochmal Aenderungen am Logging ;-)
70 * Revision 1.6 2001/12/25 23:13:33 alex
71 * - Debug-Meldungen angepasst.
73 * Revision 1.5 2001/12/25 22:02:42 alex
74 * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
76 * Revision 1.4 2001/12/25 19:19:30 alex
77 * - bessere Fehler-Abfragen, diverse Bugfixes.
78 * - Nicks werden nur einmal vergeben :-)
79 * - /MOTD wird unterstuetzt.
81 * Revision 1.3 2001/12/24 01:34:06 alex
82 * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
83 * - MOTD-Ausgabe begonnen zu implementieren.
85 * Revision 1.2 2001/12/23 21:57:16 alex
86 * - erste IRC-Befehle zu implementieren begonnen.
88 * Revision 1.1 2001/12/14 08:13:43 alex
89 * - neues Modul begonnen :-)
109 #include "messages.h"
117 #define CONNECTED TRUE
118 #define DISCONNECTED FALSE
121 LOCAL BOOLEAN Hello_User( CLIENT *Client );
122 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
124 LOCAL VOID Kill_Nick( CHAR *Nick );
127 GLOBAL VOID IRC_Init( VOID )
132 GLOBAL VOID IRC_Exit( VOID )
137 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
139 /* Text an Clients, lokal bzw. remote, senden. */
142 BOOLEAN ok = CONNECTED;
146 assert( Client != NULL );
147 assert( Format != NULL );
149 va_start( ap, Format );
150 vsnprintf( buffer, 1000, Format, ap );
153 if( Client_Conn( Client ) != NONE ) send_to = Client_Conn( Client );
154 else send_to = Client_Conn( Client_Introducer( Client ));
156 if( Prefix ) ok = Conn_WriteStr( Client_Conn( Client ), ":%s %s", Client_Mask( Prefix ), buffer );
157 else ok = Conn_WriteStr( Client_Conn( Client ), buffer );
160 } /* IRC_WriteStrClient */
163 GLOBAL BOOLEAN IRC_WriteStrRelated( CLIENT *Client, CHAR *Format, ... )
166 BOOLEAN ok = CONNECTED;
169 assert( Client != NULL );
170 assert( Format != NULL );
172 va_start( ap, Format );
173 vsnprintf( buffer, 1000, Format, ap );
176 /* an den Client selber */
177 ok = IRC_WriteStrClient( Client, Client, buffer );
180 } /* IRC_WriteStrRelated */
183 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
185 assert( Client != NULL );
186 assert( Req != NULL );
188 /* Fehler liefern, wenn kein lokaler Client */
189 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
191 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
193 /* noch nicht registrierte unbekannte Verbindung */
194 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
196 /* Passwort speichern */
197 Client_SetPassword( Client, Req->argv[0] );
199 Client_SetType( Client, CLIENT_GOTPASS );
202 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
204 /* noch nicht registrierte Server-Verbindung */
205 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
207 /* Passwort speichern */
208 Client_SetPassword( Client, Req->argv[0] );
210 Client_SetType( Client, CLIENT_GOTPASSSERVER );
213 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
215 /* Falsche Anzahl Parameter? */
216 return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
218 else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
222 GLOBAL BOOLEAN IRC_SERVER( CLIENT *Client, REQUEST *Req )
224 CHAR str[LINE_LEN], *ptr;
227 assert( Client != NULL );
228 assert( Req != NULL );
230 /* Fehler liefern, wenn kein lokaler Client */
231 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
233 if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
235 /* Verbindung soll als Server-Server-Verbindung registriert werden */
236 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
238 /* Falsche Anzahl Parameter? */
239 if( Req->argc != 3 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
241 /* Ist ein Server mit dieser ID bereits registriert? */
242 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
244 /* Server-Strukturen fuellen ;-) */
245 Client_SetID( Client, Req->argv[0] );
246 Client_SetToken( Client, atoi( Req->argv[1] ));
247 Client_SetInfo( Client, Req->argv[2] );
248 Client_SetHops( Client, 1 );
250 Log( LOG_NOTICE, "Server \"%s\" registered (connection %d, 1 hop).", Client_ID( Client ), Client_Conn( Client ));
252 Client_SetType( Client, CLIENT_SERVER );
255 else if( Client_Type( Client ) == CLIENT_SERVER )
257 /* Neuer Server wird im Netz angekuendigt */
259 /* Falsche Anzahl Parameter? */
260 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
262 /* Ist ein Server mit dieser ID bereits registriert? */
263 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
265 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
266 ptr = strchr( Req->argv[3] + 2, '[' );
267 if( ! ptr ) ptr = Req->argv[3];
269 /* Neue Client-Struktur anlegen */
270 c = Client_NewRemoteServer( Client, Req->argv[0], atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr );
273 /* Neue Client-Struktur konnte nicht angelegt werden */
274 Log( LOG_ALERT, "Can't allocate client structure for server! (on connection %d)", Client_Conn( Client ));
275 Conn_Close( Client_Conn( Client ), "Can't allocate client structure for remote server!" );
279 /* Log-Meldung zusammenbauen und ausgeben */
280 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) sprintf( str, "connected to %s, ", Req->prefix );
281 else strcpy( str, "" );
282 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": "" );
286 else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
290 GLOBAL BOOLEAN IRC_NJOIN( CLIENT *Client, REQUEST *Req )
292 assert( Client != NULL );
293 assert( Req != NULL );
295 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTEREDSERVER_MSG, Client_ID( Client ));
301 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
305 assert( Client != NULL );
306 assert( Req != NULL );
308 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
310 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 )
312 if( Client_Type( Client ) == CLIENT_UNKNOWN || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_USER )
315 /* User-Registrierung bzw. Nick-Aenderung */
317 /* Falsche Anzahl Parameter? */
318 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
320 /* Ist der Client "restricted"? */
321 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_RESTRICTED_MSG, Client_ID( Client ));
323 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
324 * wir nichts. So macht es das Original und mind. Snak hat probleme,
325 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
327 if( strcmp( Client_ID( Client ), Req->argv[0] ) == 0 ) return CONNECTED;
330 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
331 * will nur die Gross- und Kleinschreibung aendern. Das darf
332 * er natuerlich machen :-) */
333 if( strcasecmp( Client_ID( Client ), Req->argv[0] ) != 0 )
335 if( ! Client_CheckNick( Client, Req->argv[0] )) return CONNECTED;
338 if( Client_Type( Client ) == CLIENT_USER )
340 /* Nick-Aenderung: allen mitteilen! */
341 Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( Client ), Client_ID( Client ), Req->argv[0] );
342 IRC_WriteStrRelated( Client, "NICK :%s", Req->argv[0] );
345 /* Client-Nick registrieren */
346 Client_SetID( Client, Req->argv[0] );
348 if( Client_Type( Client ) != CLIENT_USER )
351 Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client_Conn( Client ));
352 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
353 else Client_SetType( Client, CLIENT_GOTNICK );
357 else if( Client_Type( Client ) == CLIENT_SERVER )
359 /* Server fuehrt neuen Client ein */
361 /* Falsche Anzahl Parameter? */
362 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
364 /* Nick ueberpruefen */
365 c = Client_GetFromID( Req->argv[0] );
368 /* Der neue Nick ist auf diesem Server bereits registriert:
369 * sowohl der neue, als auch der alte Client muessen nun
370 * disconnectiert werden. */
371 Log( LOG_ALERT, "Server %s introduces already registered nick %s!", Client_ID( Client ), Req->argv[0] );
372 Kill_Nick( Req->argv[0] );
376 /* Server, zu dem der Client connectiert ist, suchen */
377 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
380 Log( LOG_ALERT, "Server %s introduces nick %s with unknown host server!?", Client_ID( Client ), Req->argv[0] );
381 Kill_Nick( Req->argv[0] );
385 /* Neue Client-Struktur anlegen */
386 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], Req->argv[6] );
389 /* Eine neue Client-Struktur konnte nicht angelegt werden.
390 * Der Client muss disconnectiert werden, damit der Netz-
391 * status konsistent bleibt. */
392 Log( LOG_ALERT, "Can't allocate client structure! (on connection %d)", Client_Conn( Client ));
393 Kill_Nick( Req->argv[0] );
397 Log( LOG_NOTICE, "User \"%s\" registered (via %s, on %s, %d hop%s).", Client_ID( c ), Client_ID( Client ), Client_ID( intr_c ), Client_Hops( c ), Client_Hops( c ) > 1 ? "s": "" );
401 else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
405 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
407 assert( Client != NULL );
408 assert( Req != NULL );
411 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
413 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
416 /* Falsche Anzahl Parameter? */
417 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
419 Client_SetUser( Client, Req->argv[0] );
420 Client_SetInfo( Client, Req->argv[3] );
422 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client_Conn( Client ));
423 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
424 else Client_SetType( Client, CLIENT_GOTUSER );
427 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
429 return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
431 else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
435 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
437 assert( Client != NULL );
438 assert( Req != NULL );
440 if( Client_Type( Client ) != CLIENT_SERVER && Client_Type( Client ) != CLIENT_SERVICE )
442 /* Falsche Anzahl Parameter? */
443 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
445 Conn_Close( Client_Conn( Client ), "Client wants to quit." );
448 else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
452 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
454 assert( Client != NULL );
455 assert( Req != NULL );
457 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
459 /* Falsche Anzahl Parameter? */
460 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOORIGIN_MSG, Client_ID( Client ));
461 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
463 return IRC_WriteStrClient( Client, Client_ThisServer( ), "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
467 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
469 assert( Client != NULL );
470 assert( Req != NULL );
472 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
474 /* Falsche Anzahl Parameter? */
475 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOORIGIN_MSG, Client_ID( Client ));
476 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
478 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
479 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
481 Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
486 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
488 assert( Client != NULL );
489 assert( Req != NULL );
491 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
493 /* Falsche Anzahl Parameter? */
494 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
496 return Show_MOTD( Client );
500 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
504 assert( Client != NULL );
505 assert( Req != NULL );
507 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
509 /* Falsche Anzahl Parameter? */
510 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
511 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
512 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
514 to = Client_Search( Req->argv[0] );
517 /* Okay, Ziel ist ein User */
518 if( Client_Conn( Client ) >= 0 ) Conn_UpdateIdle( Client_Conn( Client ));
519 return IRC_WriteStrClient( to, Client, "PRIVMSG %s :%s", Client_ID( to ), Req->argv[1] );
522 return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
526 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
530 assert( Client != NULL );
531 assert( Req != NULL );
533 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
535 /* Falsche Anzahl Parameter? */
536 if( Req->argc != 2 ) return CONNECTED;
538 to = Client_Search( Req->argv[0] );
541 /* Okay, Ziel ist ein User */
542 return IRC_WriteStrClient( to, Client, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
549 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
551 CHAR x[2], new_modes[CLIENT_MODE_LEN], *ptr;
555 assert( Client != NULL );
556 assert( Req != NULL );
558 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
560 /* Falsche Anzahl Parameter? */
561 if(( Req->argc > 2 ) || ( Req->argc < 1 )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
563 /* "Ziel-Client" suchen */
564 c = Client_Search( Req->argv[0] );
566 /* Wer ist der Anfragende? */
567 if( Client_Type( Client ) == CLIENT_USER )
569 /* User: MODE ist nur fuer sich selber zulaessig */
570 if( c != Client ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
574 /* Server: gibt es den Client ueberhaupt? */
575 if( ! c ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
578 /* Werden die Modes erfragt? */
579 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( Client ));
583 /* Sollen Modes gesetzt oder geloescht werden? */
584 if( *ptr == '+' ) set = TRUE;
585 else if( *ptr == '-' ) set = FALSE;
586 else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
588 /* Reply-String mit Aenderungen vorbereiten */
589 if( set ) strcpy( new_modes, "+" );
590 else strcpy( new_modes, "-" );
605 /* restricted (kann nur gesetzt werden) */
606 if( set ) x[0] = 'r';
607 else ok = IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_RESTRICTED_MSG, Client_ID( Client ));
610 /* operator (kann nur geloescht werden) */
613 Client_SetOperByMe( Client, FALSE );
616 else ok = IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
619 ok = IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
625 if( ! x[0] ) continue;
627 /* Okay, gueltigen Mode gefunden */
630 /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
631 if( Client_ModeAdd( Client, x[0] )) strcat( new_modes, x );
635 /* Modes geloescht. Wenn der Client ihn hatte: merken */
636 if( Client_ModeDel( Client, x[0] )) strcat( new_modes, x );
640 /* Geanderte Modes? */
641 if( new_modes[1] && ok )
643 ok = IRC_WriteStrRelated( Client, "MODE %s :%s", Client_ID( Client ), new_modes );
644 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( Client ), Client_Modes( Client ));
650 GLOBAL BOOLEAN IRC_OPER( CLIENT *Client, REQUEST *Req )
654 assert( Client != NULL );
655 assert( Req != NULL );
657 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
659 /* Falsche Anzahl Parameter? */
660 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
662 /* Operator suchen */
663 for( i = 0; i < Conf_Oper_Count; i++)
665 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
667 if( i >= Conf_Oper_Count ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
669 /* Stimmt der Name und das Passwort? */
670 if(( strcmp( Conf_Oper[i].name, Req->argv[0] ) != 0 ) || ( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
672 if( ! Client_HasMode( Client, 'o' ))
674 /* noch kein o-Mode gesetzt */
675 Client_ModeAdd( Client, 'o' );
676 if( ! IRC_WriteStrRelated( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
679 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE, "User \"%s\" is now an IRC Operator.", Client_Mask( Client ));
681 Client_SetOperByMe( Client, TRUE );
682 return IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_YOUREOPER_MSG, Client_ID( Client ));
686 GLOBAL BOOLEAN IRC_DIE( CLIENT *Client, REQUEST *Req )
688 assert( Client != NULL );
689 assert( Req != NULL );
691 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
693 /* Falsche Anzahl Parameter? */
694 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
696 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
698 Log( LOG_NOTICE, "Got DIE command from \"%s\", going down!", Client_Mask( Client ));
704 GLOBAL BOOLEAN IRC_RESTART( CLIENT *Client, REQUEST *Req )
706 assert( Client != NULL );
707 assert( Req != NULL );
709 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
711 /* Falsche Anzahl Parameter? */
712 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
714 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
716 Log( LOG_NOTICE, "Got RESTART command from \"%s\", going down!", Client_Mask( Client ));
717 NGIRCd_Restart = TRUE;
722 GLOBAL BOOLEAN IRC_NAMES( CLIENT *Client, REQUEST *Req )
724 CHAR rpl[COMMAND_LEN];
727 assert( Client != NULL );
728 assert( Req != NULL );
730 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
732 /* Falsche Anzahl Parameter? */
733 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
735 /* Noch alle User ausgeben, die in keinem Channel sind */
740 if( Client_Type( c ) == CLIENT_USER )
742 /* Okay, das ist ein User */
743 strcat( rpl, Client_ID( c ));
747 /* Antwort zu lang? Splitten. */
748 if( strlen( rpl ) > 480 )
750 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
751 if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
755 c = Client_Next( c );
759 /* es wurden User gefunden */
760 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
761 if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
763 return IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_ENDOFNAMES_MSG, Client_ID( Client ), "*" );
767 GLOBAL BOOLEAN IRC_ISON( CLIENT *Client, REQUEST *Req )
769 CHAR rpl[COMMAND_LEN];
774 assert( Client != NULL );
775 assert( Req != NULL );
777 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
779 /* Falsche Anzahl Parameter? */
780 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
782 strcpy( rpl, RPL_ISON_MSG );
783 for( i = 0; i < Req->argc; i++ )
785 ptr = strtok( Req->argv[i], " " );
789 c = Client_GetFromID( ptr );
790 if( c && ( Client_Type( c ) == CLIENT_USER ))
792 /* Dieser Nick ist "online" */
796 ptr = strtok( NULL, " " );
799 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
801 return IRC_WriteStrClient( Client, Client_ThisServer( ), rpl, Client_ID( Client ) );
805 GLOBAL BOOLEAN IRC_WHOIS( CLIENT *Client, REQUEST *Req )
809 assert( Client != NULL );
810 assert( Req != NULL );
812 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
814 /* Falsche Anzahl Parameter? */
815 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
818 c = Client_GetFromID( Req->argv[0] );
819 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
821 /* Nick, User und Name */
822 if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_WHOISUSER_MSG, Client_ID( Client ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
825 if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_WHOISSERVER_MSG, Client_ID( Client ), Client_ID( c ), Client_ID( Client_Introducer( c )), Client_Info( Client_Introducer( c )))) return DISCONNECTED;
828 if( Client_HasMode( c, 'o' ))
830 if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_WHOISOPERATOR_MSG, Client_ID( Client ), Client_ID( c ))) return DISCONNECTED;
833 /* Idle (nur lokale Clients) */
834 if( Client_Conn( c ) > NONE )
836 if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_WHOISIDLE_MSG, Client_ID( Client ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
840 return IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_ENDOFWHOIS_MSG, Client_ID( Client ), Client_ID( c ));
844 GLOBAL BOOLEAN IRC_USERHOST( CLIENT *Client, REQUEST *Req )
846 CHAR rpl[COMMAND_LEN];
850 assert( Client != NULL );
851 assert( Req != NULL );
853 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
855 /* Falsche Anzahl Parameter? */
856 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
858 if( Req->argc > 5 ) max = 5;
859 else max = Req->argc;
861 strcpy( rpl, RPL_USERHOST_MSG );
862 for( i = 0; i < max; i++ )
864 c = Client_GetFromID( Req->argv[i] );
865 if( c && ( Client_Type( c ) == CLIENT_USER ))
867 /* Dieser Nick ist "online" */
868 strcat( rpl, Client_ID( c ));
869 if( Client_HasMode( c, 'o' )) strcat( rpl, "*" );
871 if( Client_HasMode( c, 'a' )) strcat( rpl, "-" );
872 else strcat( rpl, "+" );
873 strcat( rpl, Client_User( c ));
875 strcat( rpl, Client_Hostname( c ));
879 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
881 return IRC_WriteStrClient( Client, Client_ThisServer( ), rpl, Client_ID( Client ) );
885 GLOBAL BOOLEAN IRC_ERROR( CLIENT *Client, REQUEST *Req )
887 assert( Client != NULL );
888 assert( Req != NULL );
890 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
891 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
897 LOCAL BOOLEAN Hello_User( CLIENT *Client )
899 assert( Client != NULL );
901 /* Passwort ueberpruefen */
902 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
904 /* Falsches Passwort */
905 Log( LOG_ALERT, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
906 Conn_Close( Client_Conn( Client ), "Bad password!" );
910 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
912 IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ));
913 IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
914 IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr );
915 IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
917 Client_SetType( Client, CLIENT_USER );
919 return Show_MOTD( Client );
923 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
929 assert( Client != NULL );
931 fd = fopen( Conf_MotdFile, "r" );
934 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
935 return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOMOTD_MSG, Client_ID( Client ) );
938 IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
941 if( ! fgets( line, 126, fd )) break;
942 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
943 IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_MOTD_MSG, Client_ID( Client ), line );
945 ok = IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
953 LOCAL VOID Kill_Nick( CHAR *Nick )
955 Log( LOG_ALERT, "User(s) with nick \"%s\" will be disconnected!", Nick );
957 Log( LOG_ALERT, "[Kill_Nick() not implemented - OOOPS!]" );