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.23 2002/01/04 01:36:40 alex Exp $
17 * Revision 1.23 2002/01/04 01:36:40 alex
18 * - Loglevel ein wenig angepasst.
20 * Revision 1.22 2002/01/04 01:21:47 alex
21 * - Client-Strukruren werden nur noch ueber Funktionen angesprochen.
22 * - Weitere Anpassungen und Erweiterungen der Server-Links.
24 * Revision 1.21 2002/01/03 02:26:51 alex
25 * - neue Befehle SERVER und NJOIN begonnen,
26 * - begonnen, diverse IRC-Befehle an Server-Links anzupassen.
28 * Revision 1.20 2002/01/02 12:46:41 alex
29 * - die Gross- und Kleinschreibung des Nicks kann mit NICK nun geaendert werden.
31 * Revision 1.19 2002/01/02 02:51:39 alex
32 * - Copyright-Texte angepasst.
33 * - neuer Befehl "ERROR".
35 * Revision 1.17 2001/12/31 15:33:13 alex
36 * - neuer Befehl NAMES, kleinere Bugfixes.
37 * - Bug bei PING behoben: war zu restriktiv implementiert :-)
39 * Revision 1.16 2001/12/31 02:18:51 alex
40 * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
41 * - neuen Header "defines.h" mit (fast) allen Konstanten.
42 * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
44 * Revision 1.15 2001/12/30 19:26:11 alex
45 * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
47 * Revision 1.14 2001/12/30 11:42:00 alex
48 * - der Server meldet nun eine ordentliche "Start-Zeit".
50 * Revision 1.13 2001/12/29 03:10:06 alex
51 * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
52 * - neue configure-Optione "--enable-strict-rfc".
54 * Revision 1.12 2001/12/27 19:17:26 alex
55 * - neue Befehle PRIVMSG, NOTICE, PING.
57 * Revision 1.11 2001/12/27 16:55:41 alex
58 * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
60 * Revision 1.10 2001/12/26 22:48:53 alex
61 * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
63 * Revision 1.9 2001/12/26 14:45:37 alex
66 * Revision 1.8 2001/12/26 03:21:46 alex
67 * - PING/PONG-Befehle implementiert,
68 * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
70 * Revision 1.7 2001/12/25 23:25:18 alex
71 * - und nochmal Aenderungen am Logging ;-)
73 * Revision 1.6 2001/12/25 23:13:33 alex
74 * - Debug-Meldungen angepasst.
76 * Revision 1.5 2001/12/25 22:02:42 alex
77 * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
79 * Revision 1.4 2001/12/25 19:19:30 alex
80 * - bessere Fehler-Abfragen, diverse Bugfixes.
81 * - Nicks werden nur einmal vergeben :-)
82 * - /MOTD wird unterstuetzt.
84 * Revision 1.3 2001/12/24 01:34:06 alex
85 * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
86 * - MOTD-Ausgabe begonnen zu implementieren.
88 * Revision 1.2 2001/12/23 21:57:16 alex
89 * - erste IRC-Befehle zu implementieren begonnen.
91 * Revision 1.1 2001/12/14 08:13:43 alex
92 * - neues Modul begonnen :-)
112 #include "messages.h"
120 #define CONNECTED TRUE
121 #define DISCONNECTED FALSE
124 LOCAL BOOLEAN Hello_User( CLIENT *Client );
125 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
127 LOCAL VOID Kill_Nick( CHAR *Nick );
130 GLOBAL VOID IRC_Init( VOID )
135 GLOBAL VOID IRC_Exit( VOID )
140 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
142 /* Text an Clients, lokal bzw. remote, senden. */
145 BOOLEAN ok = CONNECTED;
149 assert( Client != NULL );
150 assert( Format != NULL );
152 va_start( ap, Format );
153 vsnprintf( buffer, 1000, Format, ap );
156 if( Client_Conn( Client ) != NONE ) send_to = Client_Conn( Client );
157 else send_to = Client_Conn( Client_Introducer( Client ));
159 if( Prefix ) ok = Conn_WriteStr( Client_Conn( Client ), ":%s %s", Client_Mask( Prefix ), buffer );
160 else ok = Conn_WriteStr( Client_Conn( Client ), buffer );
163 } /* IRC_WriteStrClient */
166 GLOBAL BOOLEAN IRC_WriteStrRelated( CLIENT *Client, CHAR *Format, ... )
169 BOOLEAN ok = CONNECTED;
172 assert( Client != NULL );
173 assert( Format != NULL );
175 va_start( ap, Format );
176 vsnprintf( buffer, 1000, Format, ap );
179 /* an den Client selber */
180 ok = IRC_WriteStrClient( Client, Client, buffer );
183 } /* IRC_WriteStrRelated */
186 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
188 assert( Client != NULL );
189 assert( Req != NULL );
191 /* Fehler liefern, wenn kein lokaler Client */
192 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
194 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
196 /* noch nicht registrierte unbekannte Verbindung */
197 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
199 /* Passwort speichern */
200 Client_SetPassword( Client, Req->argv[0] );
202 Client_SetType( Client, CLIENT_GOTPASS );
205 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
207 /* noch nicht registrierte Server-Verbindung */
208 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
210 /* Passwort speichern */
211 Client_SetPassword( Client, Req->argv[0] );
213 Client_SetType( Client, CLIENT_GOTPASSSERVER );
216 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
218 /* Falsche Anzahl Parameter? */
219 return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
221 else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
225 GLOBAL BOOLEAN IRC_SERVER( CLIENT *Client, REQUEST *Req )
227 CHAR str[LINE_LEN], *ptr;
230 assert( Client != NULL );
231 assert( Req != NULL );
233 /* Fehler liefern, wenn kein lokaler Client */
234 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
236 if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
238 /* Verbindung soll als Server-Server-Verbindung registriert werden */
239 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
241 /* Falsche Anzahl Parameter? */
242 if( Req->argc != 3 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
244 /* Ist ein Server mit dieser ID bereits registriert? */
245 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
247 /* Server-Strukturen fuellen ;-) */
248 Client_SetID( Client, Req->argv[0] );
249 Client_SetToken( Client, atoi( Req->argv[1] ));
250 Client_SetInfo( Client, Req->argv[2] );
251 Client_SetHops( Client, 1 );
253 Log( LOG_NOTICE, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), Client_Conn( Client ));
255 Client_SetType( Client, CLIENT_SERVER );
258 else if( Client_Type( Client ) == CLIENT_SERVER )
260 /* Neuer Server wird im Netz angekuendigt */
262 /* Falsche Anzahl Parameter? */
263 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
265 /* Ist ein Server mit dieser ID bereits registriert? */
266 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
268 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
269 ptr = strchr( Req->argv[3] + 2, '[' );
270 if( ! ptr ) ptr = Req->argv[3];
272 /* Neue Client-Struktur anlegen */
273 c = Client_NewRemoteServer( Client, Req->argv[0], atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr );
276 /* Neue Client-Struktur konnte nicht angelegt werden */
277 Log( LOG_ALERT, "Can't allocate client structure for server! (on connection %d)", Client_Conn( Client ));
278 Conn_Close( Client_Conn( Client ), "Can't allocate client structure for remote server!" );
282 /* Log-Meldung zusammenbauen und ausgeben */
283 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) sprintf( str, "connected to %s, ", Req->prefix );
284 else strcpy( str, "" );
285 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": "" );
289 else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
293 GLOBAL BOOLEAN IRC_NJOIN( CLIENT *Client, REQUEST *Req )
295 assert( Client != NULL );
296 assert( Req != NULL );
298 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTEREDSERVER_MSG, Client_ID( Client ));
304 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
308 assert( Client != NULL );
309 assert( Req != NULL );
311 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
313 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 )
315 if( Client_Type( Client ) == CLIENT_UNKNOWN || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_USER )
318 /* User-Registrierung bzw. Nick-Aenderung */
320 /* Falsche Anzahl Parameter? */
321 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
323 /* Ist der Client "restricted"? */
324 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_RESTRICTED_MSG, Client_ID( Client ));
326 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
327 * wir nichts. So macht es das Original und mind. Snak hat probleme,
328 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
330 if( strcmp( Client_ID( Client ), Req->argv[0] ) == 0 ) return CONNECTED;
333 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
334 * will nur die Gross- und Kleinschreibung aendern. Das darf
335 * er natuerlich machen :-) */
336 if( strcasecmp( Client_ID( Client ), Req->argv[0] ) != 0 )
338 if( ! Client_CheckNick( Client, Req->argv[0] )) return CONNECTED;
341 if( Client_Type( Client ) == CLIENT_USER )
343 /* Nick-Aenderung: allen mitteilen! */
344 Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( Client ), Client_ID( Client ), Req->argv[0] );
345 IRC_WriteStrRelated( Client, "NICK :%s", Req->argv[0] );
348 /* Client-Nick registrieren */
349 Client_SetID( Client, Req->argv[0] );
351 if( Client_Type( Client ) != CLIENT_USER )
354 Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client_Conn( Client ));
355 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
356 else Client_SetType( Client, CLIENT_GOTNICK );
360 else if( Client_Type( Client ) == CLIENT_SERVER )
362 /* Server fuehrt neuen Client ein */
364 /* Falsche Anzahl Parameter? */
365 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
367 /* Nick ueberpruefen */
368 c = Client_GetFromID( Req->argv[0] );
371 /* Der neue Nick ist auf diesem Server bereits registriert:
372 * sowohl der neue, als auch der alte Client muessen nun
373 * disconnectiert werden. */
374 Log( LOG_ALERT, "Server %s introduces already registered nick %s!", Client_ID( Client ), Req->argv[0] );
375 Kill_Nick( Req->argv[0] );
379 /* Server, zu dem der Client connectiert ist, suchen */
380 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
383 Log( LOG_ALERT, "Server %s introduces nick %s with unknown host server!?", Client_ID( Client ), Req->argv[0] );
384 Kill_Nick( Req->argv[0] );
388 /* Neue Client-Struktur anlegen */
389 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] );
392 /* Eine neue Client-Struktur konnte nicht angelegt werden.
393 * Der Client muss disconnectiert werden, damit der Netz-
394 * status konsistent bleibt. */
395 Log( LOG_ALERT, "Can't allocate client structure! (on connection %d)", Client_Conn( Client ));
396 Kill_Nick( Req->argv[0] );
400 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": "" );
404 else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
408 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
410 assert( Client != NULL );
411 assert( Req != NULL );
414 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
416 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
419 /* Falsche Anzahl Parameter? */
420 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
422 Client_SetUser( Client, Req->argv[0] );
423 Client_SetInfo( Client, Req->argv[3] );
425 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client_Conn( Client ));
426 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
427 else Client_SetType( Client, CLIENT_GOTUSER );
430 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
432 return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
434 else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
438 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
440 assert( Client != NULL );
441 assert( Req != NULL );
443 if( Client_Type( Client ) != CLIENT_SERVER && Client_Type( Client ) != CLIENT_SERVICE )
445 /* Falsche Anzahl Parameter? */
446 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
448 Conn_Close( Client_Conn( Client ), "Client wants to quit." );
451 else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
455 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
457 assert( Client != NULL );
458 assert( Req != NULL );
460 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
462 /* Falsche Anzahl Parameter? */
463 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOORIGIN_MSG, Client_ID( Client ));
464 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
466 return IRC_WriteStrClient( Client, Client_ThisServer( ), "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
470 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
472 assert( Client != NULL );
473 assert( Req != NULL );
475 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
477 /* Falsche Anzahl Parameter? */
478 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOORIGIN_MSG, Client_ID( Client ));
479 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
481 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
482 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
484 Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
489 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
491 assert( Client != NULL );
492 assert( Req != NULL );
494 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
496 /* Falsche Anzahl Parameter? */
497 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
499 return Show_MOTD( Client );
503 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
507 assert( Client != NULL );
508 assert( Req != NULL );
510 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
512 /* Falsche Anzahl Parameter? */
513 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
514 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
515 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
517 to = Client_Search( Req->argv[0] );
520 /* Okay, Ziel ist ein User */
521 if( Client_Conn( Client ) >= 0 ) Conn_UpdateIdle( Client_Conn( Client ));
522 return IRC_WriteStrClient( to, Client, "PRIVMSG %s :%s", Client_ID( to ), Req->argv[1] );
525 return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
529 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
533 assert( Client != NULL );
534 assert( Req != NULL );
536 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
538 /* Falsche Anzahl Parameter? */
539 if( Req->argc != 2 ) return CONNECTED;
541 to = Client_Search( Req->argv[0] );
544 /* Okay, Ziel ist ein User */
545 return IRC_WriteStrClient( to, Client, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
552 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
554 CHAR x[2], new_modes[CLIENT_MODE_LEN], *ptr;
558 assert( Client != NULL );
559 assert( Req != NULL );
561 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
563 /* Falsche Anzahl Parameter? */
564 if(( Req->argc > 2 ) || ( Req->argc < 1 )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
566 /* "Ziel-Client" suchen */
567 c = Client_Search( Req->argv[0] );
569 /* Wer ist der Anfragende? */
570 if( Client_Type( Client ) == CLIENT_USER )
572 /* User: MODE ist nur fuer sich selber zulaessig */
573 if( c != Client ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
577 /* Server: gibt es den Client ueberhaupt? */
578 if( ! c ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
581 /* Werden die Modes erfragt? */
582 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( Client ));
586 /* Sollen Modes gesetzt oder geloescht werden? */
587 if( *ptr == '+' ) set = TRUE;
588 else if( *ptr == '-' ) set = FALSE;
589 else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
591 /* Reply-String mit Aenderungen vorbereiten */
592 if( set ) strcpy( new_modes, "+" );
593 else strcpy( new_modes, "-" );
608 /* restricted (kann nur gesetzt werden) */
609 if( set ) x[0] = 'r';
610 else ok = IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_RESTRICTED_MSG, Client_ID( Client ));
613 /* operator (kann nur geloescht werden) */
616 Client_SetOperByMe( Client, FALSE );
619 else ok = IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
622 ok = IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
628 if( ! x[0] ) continue;
630 /* Okay, gueltigen Mode gefunden */
633 /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
634 if( Client_ModeAdd( Client, x[0] )) strcat( new_modes, x );
638 /* Modes geloescht. Wenn der Client ihn hatte: merken */
639 if( Client_ModeDel( Client, x[0] )) strcat( new_modes, x );
643 /* Geanderte Modes? */
644 if( new_modes[1] && ok )
646 ok = IRC_WriteStrRelated( Client, "MODE %s :%s", Client_ID( Client ), new_modes );
647 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( Client ), Client_Modes( Client ));
653 GLOBAL BOOLEAN IRC_OPER( CLIENT *Client, REQUEST *Req )
657 assert( Client != NULL );
658 assert( Req != NULL );
660 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
662 /* Falsche Anzahl Parameter? */
663 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
665 /* Operator suchen */
666 for( i = 0; i < Conf_Oper_Count; i++)
668 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
670 if( i >= Conf_Oper_Count ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
672 /* Stimmt der Name und das Passwort? */
673 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 ));
675 if( ! Client_HasMode( Client, 'o' ))
677 /* noch kein o-Mode gesetzt */
678 Client_ModeAdd( Client, 'o' );
679 if( ! IRC_WriteStrRelated( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
682 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE, "User \"%s\" is now an IRC Operator.", Client_Mask( Client ));
684 Client_SetOperByMe( Client, TRUE );
685 return IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_YOUREOPER_MSG, Client_ID( Client ));
689 GLOBAL BOOLEAN IRC_DIE( CLIENT *Client, REQUEST *Req )
691 assert( Client != NULL );
692 assert( Req != NULL );
694 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
696 /* Falsche Anzahl Parameter? */
697 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
699 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
701 Log( LOG_NOTICE, "Got DIE command from \"%s\", going down!", Client_Mask( Client ));
707 GLOBAL BOOLEAN IRC_RESTART( CLIENT *Client, REQUEST *Req )
709 assert( Client != NULL );
710 assert( Req != NULL );
712 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
714 /* Falsche Anzahl Parameter? */
715 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
717 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
719 Log( LOG_NOTICE, "Got RESTART command from \"%s\", going down!", Client_Mask( Client ));
720 NGIRCd_Restart = TRUE;
725 GLOBAL BOOLEAN IRC_NAMES( CLIENT *Client, REQUEST *Req )
727 CHAR rpl[COMMAND_LEN];
730 assert( Client != NULL );
731 assert( Req != NULL );
733 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
735 /* Falsche Anzahl Parameter? */
736 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
738 /* Noch alle User ausgeben, die in keinem Channel sind */
743 if( Client_Type( c ) == CLIENT_USER )
745 /* Okay, das ist ein User */
746 strcat( rpl, Client_ID( c ));
750 /* Antwort zu lang? Splitten. */
751 if( strlen( rpl ) > 480 )
753 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
754 if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
758 c = Client_Next( c );
762 /* es wurden User gefunden */
763 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
764 if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
766 return IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_ENDOFNAMES_MSG, Client_ID( Client ), "*" );
770 GLOBAL BOOLEAN IRC_ISON( CLIENT *Client, REQUEST *Req )
772 CHAR rpl[COMMAND_LEN];
777 assert( Client != NULL );
778 assert( Req != NULL );
780 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
782 /* Falsche Anzahl Parameter? */
783 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
785 strcpy( rpl, RPL_ISON_MSG );
786 for( i = 0; i < Req->argc; i++ )
788 ptr = strtok( Req->argv[i], " " );
792 c = Client_GetFromID( ptr );
793 if( c && ( Client_Type( c ) == CLIENT_USER ))
795 /* Dieser Nick ist "online" */
799 ptr = strtok( NULL, " " );
802 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
804 return IRC_WriteStrClient( Client, Client_ThisServer( ), rpl, Client_ID( Client ) );
808 GLOBAL BOOLEAN IRC_WHOIS( CLIENT *Client, REQUEST *Req )
812 assert( Client != NULL );
813 assert( Req != NULL );
815 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
817 /* Falsche Anzahl Parameter? */
818 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
821 c = Client_GetFromID( Req->argv[0] );
822 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
824 /* Nick, User und Name */
825 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;
828 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;
831 if( Client_HasMode( c, 'o' ))
833 if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_WHOISOPERATOR_MSG, Client_ID( Client ), Client_ID( c ))) return DISCONNECTED;
836 /* Idle (nur lokale Clients) */
837 if( Client_Conn( c ) > NONE )
839 if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_WHOISIDLE_MSG, Client_ID( Client ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
843 return IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_ENDOFWHOIS_MSG, Client_ID( Client ), Client_ID( c ));
847 GLOBAL BOOLEAN IRC_USERHOST( CLIENT *Client, REQUEST *Req )
849 CHAR rpl[COMMAND_LEN];
853 assert( Client != NULL );
854 assert( Req != NULL );
856 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
858 /* Falsche Anzahl Parameter? */
859 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
861 if( Req->argc > 5 ) max = 5;
862 else max = Req->argc;
864 strcpy( rpl, RPL_USERHOST_MSG );
865 for( i = 0; i < max; i++ )
867 c = Client_GetFromID( Req->argv[i] );
868 if( c && ( Client_Type( c ) == CLIENT_USER ))
870 /* Dieser Nick ist "online" */
871 strcat( rpl, Client_ID( c ));
872 if( Client_HasMode( c, 'o' )) strcat( rpl, "*" );
874 if( Client_HasMode( c, 'a' )) strcat( rpl, "-" );
875 else strcat( rpl, "+" );
876 strcat( rpl, Client_User( c ));
878 strcat( rpl, Client_Hostname( c ));
882 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
884 return IRC_WriteStrClient( Client, Client_ThisServer( ), rpl, Client_ID( Client ) );
888 GLOBAL BOOLEAN IRC_ERROR( CLIENT *Client, REQUEST *Req )
890 assert( Client != NULL );
891 assert( Req != NULL );
893 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
894 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
900 LOCAL BOOLEAN Hello_User( CLIENT *Client )
902 assert( Client != NULL );
904 /* Passwort ueberpruefen */
905 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
907 /* Falsches Passwort */
908 Log( LOG_ALERT, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
909 Conn_Close( Client_Conn( Client ), "Bad password!" );
913 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
915 IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ));
916 IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
917 IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr );
918 IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
920 Client_SetType( Client, CLIENT_USER );
922 return Show_MOTD( Client );
926 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
932 assert( Client != NULL );
934 fd = fopen( Conf_MotdFile, "r" );
937 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
938 return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOMOTD_MSG, Client_ID( Client ) );
941 IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
944 if( ! fgets( line, 126, fd )) break;
945 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
946 IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_MOTD_MSG, Client_ID( Client ), line );
948 ok = IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
956 LOCAL VOID Kill_Nick( CHAR *Nick )
958 Log( LOG_ALERT, "User(s) with nick \"%s\" will be disconnected!", Nick );
960 Log( LOG_ALERT, "[Kill_Nick() not implemented - OOOPS!]" );