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.24 2002/01/04 17:58:44 alex Exp $
17 * Revision 1.24 2002/01/04 17:58:44 alex
18 * - IRC_WriteStrXXX()-Funktionen eingefuehrt, groessere Anpassungen daran.
19 * - neuer Befehl SQUIT, QUIT an Server-Links angepasst.
21 * Revision 1.23 2002/01/04 01:36:40 alex
22 * - Loglevel ein wenig angepasst.
24 * Revision 1.22 2002/01/04 01:21:47 alex
25 * - Client-Strukruren werden nur noch ueber Funktionen angesprochen.
26 * - Weitere Anpassungen und Erweiterungen der Server-Links.
28 * Revision 1.21 2002/01/03 02:26:51 alex
29 * - neue Befehle SERVER und NJOIN begonnen,
30 * - begonnen, diverse IRC-Befehle an Server-Links anzupassen.
32 * Revision 1.20 2002/01/02 12:46:41 alex
33 * - die Gross- und Kleinschreibung des Nicks kann mit NICK nun geaendert werden.
35 * Revision 1.19 2002/01/02 02:51:39 alex
36 * - Copyright-Texte angepasst.
37 * - neuer Befehl "ERROR".
39 * Revision 1.17 2001/12/31 15:33:13 alex
40 * - neuer Befehl NAMES, kleinere Bugfixes.
41 * - Bug bei PING behoben: war zu restriktiv implementiert :-)
43 * Revision 1.16 2001/12/31 02:18:51 alex
44 * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
45 * - neuen Header "defines.h" mit (fast) allen Konstanten.
46 * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
48 * Revision 1.15 2001/12/30 19:26:11 alex
49 * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
51 * Revision 1.14 2001/12/30 11:42:00 alex
52 * - der Server meldet nun eine ordentliche "Start-Zeit".
54 * Revision 1.13 2001/12/29 03:10:06 alex
55 * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
56 * - neue configure-Optione "--enable-strict-rfc".
58 * Revision 1.12 2001/12/27 19:17:26 alex
59 * - neue Befehle PRIVMSG, NOTICE, PING.
61 * Revision 1.11 2001/12/27 16:55:41 alex
62 * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
64 * Revision 1.10 2001/12/26 22:48:53 alex
65 * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
67 * Revision 1.9 2001/12/26 14:45:37 alex
70 * Revision 1.8 2001/12/26 03:21:46 alex
71 * - PING/PONG-Befehle implementiert,
72 * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
74 * Revision 1.7 2001/12/25 23:25:18 alex
75 * - und nochmal Aenderungen am Logging ;-)
77 * Revision 1.6 2001/12/25 23:13:33 alex
78 * - Debug-Meldungen angepasst.
80 * Revision 1.5 2001/12/25 22:02:42 alex
81 * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
83 * Revision 1.4 2001/12/25 19:19:30 alex
84 * - bessere Fehler-Abfragen, diverse Bugfixes.
85 * - Nicks werden nur einmal vergeben :-)
86 * - /MOTD wird unterstuetzt.
88 * Revision 1.3 2001/12/24 01:34:06 alex
89 * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
90 * - MOTD-Ausgabe begonnen zu implementieren.
92 * Revision 1.2 2001/12/23 21:57:16 alex
93 * - erste IRC-Befehle zu implementieren begonnen.
95 * Revision 1.1 2001/12/14 08:13:43 alex
96 * - neues Modul begonnen :-)
116 #include "messages.h"
124 #define CONNECTED TRUE
125 #define DISCONNECTED FALSE
128 LOCAL BOOLEAN Hello_User( CLIENT *Client );
129 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
131 LOCAL VOID Kill_Nick( CHAR *Nick );
134 GLOBAL VOID IRC_Init( VOID )
139 GLOBAL VOID IRC_Exit( VOID )
144 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CHAR *Format, ... )
147 BOOLEAN ok = CONNECTED;
150 assert( Client != NULL );
151 assert( Format != NULL );
153 va_start( ap, Format );
154 vsnprintf( buffer, 1000, Format, ap );
157 /* an den Client selber */
158 ok = IRC_WriteStrClientPrefix( Client, Client_ThisServer( ), buffer );
161 } /* IRC_WriteStrClient */
164 GLOBAL BOOLEAN IRC_WriteStrClientPrefix( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
166 /* Text an Clients, lokal bzw. remote, senden. */
169 BOOLEAN ok = CONNECTED;
173 assert( Client != NULL );
174 assert( Format != NULL );
176 va_start( ap, Format );
177 vsnprintf( buffer, 1000, Format, ap );
180 if( Client_Conn( Client ) != NONE ) send_to = Client_Conn( Client );
181 else send_to = Client_Conn( Client_Introducer( Client ));
183 if( Prefix ) ok = Conn_WriteStr( send_to, ":%s %s", Client_Mask( Prefix ), buffer );
184 else ok = Conn_WriteStr( send_to, buffer );
187 } /* IRC_WriteStrClientPrefix */
190 GLOBAL BOOLEAN IRC_WriteStrRelated( CLIENT *Client, CHAR *Format, ... )
193 BOOLEAN ok = CONNECTED;
196 assert( Client != NULL );
197 assert( Format != NULL );
199 va_start( ap, Format );
200 vsnprintf( buffer, 1000, Format, ap );
203 /* an den Client selber */
204 ok = IRC_WriteStrClient( Client, buffer );
207 } /* IRC_WriteStrRelated */
210 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
212 assert( Client != NULL );
213 assert( Req != NULL );
215 /* Fehler liefern, wenn kein lokaler Client */
216 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
218 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
220 /* noch nicht registrierte unbekannte Verbindung */
221 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
223 /* Passwort speichern */
224 Client_SetPassword( Client, Req->argv[0] );
226 Client_SetType( Client, CLIENT_GOTPASS );
229 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
231 /* noch nicht registrierte Server-Verbindung */
232 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
234 /* Passwort speichern */
235 Client_SetPassword( Client, Req->argv[0] );
237 Client_SetType( Client, CLIENT_GOTPASSSERVER );
240 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
242 /* Falsche Anzahl Parameter? */
243 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
245 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
249 GLOBAL BOOLEAN IRC_SERVER( CLIENT *Client, REQUEST *Req )
251 CHAR str[LINE_LEN], *ptr;
254 assert( Client != NULL );
255 assert( Req != NULL );
257 /* Fehler liefern, wenn kein lokaler Client */
258 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
260 if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
262 /* Verbindung soll als Server-Server-Verbindung registriert werden */
263 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
265 /* Falsche Anzahl Parameter? */
266 if( Req->argc != 3 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
268 /* Ist ein Server mit dieser ID bereits registriert? */
269 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
271 /* Server-Strukturen fuellen ;-) */
272 Client_SetID( Client, Req->argv[0] );
273 Client_SetToken( Client, atoi( Req->argv[1] ));
274 Client_SetInfo( Client, Req->argv[2] );
275 Client_SetHops( Client, 1 );
277 Log( LOG_NOTICE, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), Client_Conn( Client ));
279 Client_SetType( Client, CLIENT_SERVER );
282 else if( Client_Type( Client ) == CLIENT_SERVER )
284 /* Neuer Server wird im Netz angekuendigt */
286 /* Falsche Anzahl Parameter? */
287 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
289 /* Ist ein Server mit dieser ID bereits registriert? */
290 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
292 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
293 ptr = strchr( Req->argv[3] + 2, '[' );
294 if( ! ptr ) ptr = Req->argv[3];
296 /* Neue Client-Struktur anlegen */
297 c = Client_NewRemoteServer( Client, Req->argv[0], atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr );
300 /* Neue Client-Struktur konnte nicht angelegt werden */
301 Log( LOG_ALERT, "Can't allocate client structure for server! (on connection %d)", Client_Conn( Client ));
302 Conn_Close( Client_Conn( Client ), "Can't allocate client structure for remote server!" );
306 /* Log-Meldung zusammenbauen und ausgeben */
307 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) sprintf( str, "connected to %s, ", Req->prefix );
308 else strcpy( str, "" );
309 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": "" );
313 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
317 GLOBAL BOOLEAN IRC_NJOIN( CLIENT *Client, REQUEST *Req )
319 assert( Client != NULL );
320 assert( Req != NULL );
322 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTEREDSERVER_MSG, Client_ID( Client ));
328 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
332 assert( Client != NULL );
333 assert( Req != NULL );
335 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
337 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 )
339 if( Client_Type( Client ) == CLIENT_UNKNOWN || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_USER )
342 /* User-Registrierung bzw. Nick-Aenderung */
344 /* Falsche Anzahl Parameter? */
345 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
347 /* Ist der Client "restricted"? */
348 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
350 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
351 * wir nichts. So macht es das Original und mind. Snak hat probleme,
352 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
354 if( strcmp( Client_ID( Client ), Req->argv[0] ) == 0 ) return CONNECTED;
357 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
358 * will nur die Gross- und Kleinschreibung aendern. Das darf
359 * er natuerlich machen :-) */
360 if( strcasecmp( Client_ID( Client ), Req->argv[0] ) != 0 )
362 if( ! Client_CheckNick( Client, Req->argv[0] )) return CONNECTED;
365 if( Client_Type( Client ) == CLIENT_USER )
367 /* Nick-Aenderung: allen mitteilen! */
368 Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( Client ), Client_ID( Client ), Req->argv[0] );
369 IRC_WriteStrRelated( Client, "NICK :%s", Req->argv[0] );
372 /* Client-Nick registrieren */
373 Client_SetID( Client, Req->argv[0] );
375 if( Client_Type( Client ) != CLIENT_USER )
378 Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client_Conn( Client ));
379 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
380 else Client_SetType( Client, CLIENT_GOTNICK );
384 else if( Client_Type( Client ) == CLIENT_SERVER )
386 /* Server fuehrt neuen Client ein */
388 /* Falsche Anzahl Parameter? */
389 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
391 /* Nick ueberpruefen */
392 c = Client_GetFromID( Req->argv[0] );
395 /* Der neue Nick ist auf diesem Server bereits registriert:
396 * sowohl der neue, als auch der alte Client muessen nun
397 * disconnectiert werden. */
398 Log( LOG_ALERT, "Server %s introduces already registered nick %s!", Client_ID( Client ), Req->argv[0] );
399 Kill_Nick( Req->argv[0] );
403 /* Server, zu dem der Client connectiert ist, suchen */
404 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
407 Log( LOG_ALERT, "Server %s introduces nick %s with unknown host server!?", Client_ID( Client ), Req->argv[0] );
408 Kill_Nick( Req->argv[0] );
412 /* Neue Client-Struktur anlegen */
413 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] );
416 /* Eine neue Client-Struktur konnte nicht angelegt werden.
417 * Der Client muss disconnectiert werden, damit der Netz-
418 * status konsistent bleibt. */
419 Log( LOG_ALERT, "Can't allocate client structure! (on connection %d)", Client_Conn( Client ));
420 Kill_Nick( Req->argv[0] );
424 Log( LOG_DEBUG, "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": "" );
428 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
432 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
434 assert( Client != NULL );
435 assert( Req != NULL );
438 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
440 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
443 /* Falsche Anzahl Parameter? */
444 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
446 Client_SetUser( Client, Req->argv[0] );
447 Client_SetInfo( Client, Req->argv[3] );
449 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client_Conn( Client ));
450 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
451 else Client_SetType( Client, CLIENT_GOTUSER );
454 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
456 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
458 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
462 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
466 assert( Client != NULL );
467 assert( Req != NULL );
469 if(( Client_Type( Client ) == CLIENT_USER ) || ( Client_Type( Client ) == CLIENT_SERVICE ))
473 /* Falsche Anzahl Parameter? */
474 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
476 Conn_Close( Client_Conn( Client ), "Client wants to quit." );
479 else if ( Client_Type( Client ) == CLIENT_SERVER )
483 /* Falsche Anzahl Parameter? */
484 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
486 target = Client_Search( Req->prefix );
487 if( ! target ) Log( LOG_ALERT, "Got QUIT from %s for unknown server!?", Client_ID( Client ));
488 else Client_Destroy( target );
491 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
495 GLOBAL BOOLEAN IRC_SQUIT( CLIENT *Client, REQUEST *Req )
499 assert( Client != NULL );
500 assert( Req != NULL );
502 /* SQUIT ist nur fuer Server erlaubt */
503 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
505 /* Falsche Anzahl Parameter? */
506 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
508 target = Client_GetFromID( Req->argv[0] );
511 Log( LOG_ALERT, "Got SQUIT from %s for unknown server \%s\"!?", Client_ID( Client ), Req->argv[0] );
515 if( target == Client ) Log( LOG_NOTICE, "Got SQUIT from %s: %s", Client_ID( Client ), Req->argv[1] );
516 else Log( LOG_NOTICE, "Got SQUIT from %s for %s: %s", Client_ID( Client ), Client_ID( target ), Req->argv[1] );
518 /* FIXME: das SQUIT muss an alle Server weiter-
519 * geleitet werden ... */
521 if( Client_Conn( target ) > NONE ) Conn_Close( Client_Conn( target ), Req->argv[1] );
522 else Client_Destroy( target );
527 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
529 assert( Client != NULL );
530 assert( Req != NULL );
532 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
534 /* Falsche Anzahl Parameter? */
535 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
536 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
538 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
542 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
544 assert( Client != NULL );
545 assert( Req != NULL );
547 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
549 /* Falsche Anzahl Parameter? */
550 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
551 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
553 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
554 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
556 Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
561 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
563 assert( Client != NULL );
564 assert( Req != NULL );
566 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
568 /* Falsche Anzahl Parameter? */
569 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
571 return Show_MOTD( Client );
575 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
579 assert( Client != NULL );
580 assert( Req != NULL );
582 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
584 /* Falsche Anzahl Parameter? */
585 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
586 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
587 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
589 to = Client_Search( Req->argv[0] );
592 /* Okay, Ziel ist ein User */
593 if( Client_Conn( Client ) >= 0 ) Conn_UpdateIdle( Client_Conn( Client ));
594 return IRC_WriteStrClientPrefix( to, Client, "PRIVMSG %s :%s", Client_ID( to ), Req->argv[1] );
597 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
601 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
605 assert( Client != NULL );
606 assert( Req != NULL );
608 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
610 /* Falsche Anzahl Parameter? */
611 if( Req->argc != 2 ) return CONNECTED;
613 to = Client_Search( Req->argv[0] );
616 /* Okay, Ziel ist ein User */
617 return IRC_WriteStrClientPrefix( to, Client, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
624 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
626 CHAR x[2], new_modes[CLIENT_MODE_LEN], *ptr;
630 assert( Client != NULL );
631 assert( Req != NULL );
633 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
635 /* Falsche Anzahl Parameter? */
636 if(( Req->argc > 2 ) || ( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
638 /* "Ziel-Client" suchen */
639 target = Client_Search( Req->argv[0] );
641 /* Wer ist der Anfragende? */
642 if( Client_Type( Client ) == CLIENT_USER )
644 /* User: MODE ist nur fuer sich selber zulaessig */
645 if( target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
649 /* Server: gibt es den Client ueberhaupt? */
650 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
653 /* Werden die Modes erfragt? */
654 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( Client ));
658 /* Sollen Modes gesetzt oder geloescht werden? */
659 if( *ptr == '+' ) set = TRUE;
660 else if( *ptr == '-' ) set = FALSE;
661 else return IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
663 /* Reply-String mit Aenderungen vorbereiten */
664 if( set ) strcpy( new_modes, "+" );
665 else strcpy( new_modes, "-" );
673 if( Client_Type( Client ) == CLIENT_SERVER )
687 /* restricted (kann nur gesetzt werden) */
688 if( set ) x[0] = 'r';
689 else ok = IRC_WriteStrClient( target, ERR_RESTRICTED_MSG, Client_ID( target ));
692 /* operator (kann nur geloescht werden) */
695 Client_SetOperByMe( target, FALSE );
698 else ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
701 ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
708 if( ! x[0] ) continue;
710 /* Okay, gueltigen Mode gefunden */
713 /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
714 if( Client_ModeAdd( target, x[0] )) strcat( new_modes, x );
718 /* Modes geloescht. Wenn der Client ihn hatte: merken */
719 if( Client_ModeDel( target, x[0] )) strcat( new_modes, x );
723 /* Geanderte Modes? */
726 if( Client_Type( Client ) == CLIENT_SERVER )
728 /* Modes an andere Server forwarden */
733 /* Bestaetigung an Client schicken */
734 ok = IRC_WriteStrRelated( Client, "MODE %s :%s", Client_ID( target ), new_modes );
736 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( target ), Client_Modes( target ));
742 GLOBAL BOOLEAN IRC_OPER( CLIENT *Client, REQUEST *Req )
746 assert( Client != NULL );
747 assert( Req != NULL );
749 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
751 /* Falsche Anzahl Parameter? */
752 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
754 /* Operator suchen */
755 for( i = 0; i < Conf_Oper_Count; i++)
757 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
759 if( i >= Conf_Oper_Count ) return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
761 /* Stimmt der Name und das Passwort? */
762 if(( strcmp( Conf_Oper[i].name, Req->argv[0] ) != 0 ) || ( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )) return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
764 if( ! Client_HasMode( Client, 'o' ))
766 /* noch kein o-Mode gesetzt */
767 Client_ModeAdd( Client, 'o' );
768 if( ! IRC_WriteStrRelated( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
771 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE, "User \"%s\" is now an IRC Operator.", Client_Mask( Client ));
773 Client_SetOperByMe( Client, TRUE );
774 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
778 GLOBAL BOOLEAN IRC_DIE( CLIENT *Client, REQUEST *Req )
780 assert( Client != NULL );
781 assert( Req != NULL );
783 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
785 /* Falsche Anzahl Parameter? */
786 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
788 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
790 Log( LOG_NOTICE, "Got DIE command from \"%s\", going down!", Client_Mask( Client ));
796 GLOBAL BOOLEAN IRC_RESTART( CLIENT *Client, REQUEST *Req )
798 assert( Client != NULL );
799 assert( Req != NULL );
801 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
803 /* Falsche Anzahl Parameter? */
804 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
806 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
808 Log( LOG_NOTICE, "Got RESTART command from \"%s\", going down!", Client_Mask( Client ));
809 NGIRCd_Restart = TRUE;
814 GLOBAL BOOLEAN IRC_NAMES( CLIENT *Client, REQUEST *Req )
816 CHAR rpl[COMMAND_LEN];
819 assert( Client != NULL );
820 assert( Req != NULL );
822 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
824 /* Falsche Anzahl Parameter? */
825 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
827 /* Noch alle User ausgeben, die in keinem Channel sind */
832 if( Client_Type( c ) == CLIENT_USER )
834 /* Okay, das ist ein User */
835 strcat( rpl, Client_ID( c ));
839 /* Antwort zu lang? Splitten. */
840 if( strlen( rpl ) > 480 )
842 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
843 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
847 c = Client_Next( c );
851 /* es wurden User gefunden */
852 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
853 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
855 return IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), "*" );
859 GLOBAL BOOLEAN IRC_ISON( CLIENT *Client, REQUEST *Req )
861 CHAR rpl[COMMAND_LEN];
866 assert( Client != NULL );
867 assert( Req != NULL );
869 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
871 /* Falsche Anzahl Parameter? */
872 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
874 strcpy( rpl, RPL_ISON_MSG );
875 for( i = 0; i < Req->argc; i++ )
877 ptr = strtok( Req->argv[i], " " );
881 c = Client_GetFromID( ptr );
882 if( c && ( Client_Type( c ) == CLIENT_USER ))
884 /* Dieser Nick ist "online" */
888 ptr = strtok( NULL, " " );
891 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
893 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
897 GLOBAL BOOLEAN IRC_WHOIS( CLIENT *Client, REQUEST *Req )
901 assert( Client != NULL );
902 assert( Req != NULL );
904 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
906 /* Falsche Anzahl Parameter? */
907 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
910 c = Client_GetFromID( Req->argv[0] );
911 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
913 /* Nick, User und Name */
914 if( ! IRC_WriteStrClient( Client, RPL_WHOISUSER_MSG, Client_ID( Client ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
917 if( ! IRC_WriteStrClient( Client, RPL_WHOISSERVER_MSG, Client_ID( Client ), Client_ID( c ), Client_ID( Client_Introducer( c )), Client_Info( Client_Introducer( c )))) return DISCONNECTED;
920 if( Client_HasMode( c, 'o' ))
922 if( ! IRC_WriteStrClient( Client, RPL_WHOISOPERATOR_MSG, Client_ID( Client ), Client_ID( c ))) return DISCONNECTED;
925 /* Idle (nur lokale Clients) */
926 if( Client_Conn( c ) > NONE )
928 if( ! IRC_WriteStrClient( Client, RPL_WHOISIDLE_MSG, Client_ID( Client ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
932 return IRC_WriteStrClient( Client, RPL_ENDOFWHOIS_MSG, Client_ID( Client ), Client_ID( c ));
936 GLOBAL BOOLEAN IRC_USERHOST( CLIENT *Client, REQUEST *Req )
938 CHAR rpl[COMMAND_LEN];
942 assert( Client != NULL );
943 assert( Req != NULL );
945 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
947 /* Falsche Anzahl Parameter? */
948 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
950 if( Req->argc > 5 ) max = 5;
951 else max = Req->argc;
953 strcpy( rpl, RPL_USERHOST_MSG );
954 for( i = 0; i < max; i++ )
956 c = Client_GetFromID( Req->argv[i] );
957 if( c && ( Client_Type( c ) == CLIENT_USER ))
959 /* Dieser Nick ist "online" */
960 strcat( rpl, Client_ID( c ));
961 if( Client_HasMode( c, 'o' )) strcat( rpl, "*" );
963 if( Client_HasMode( c, 'a' )) strcat( rpl, "-" );
964 else strcat( rpl, "+" );
965 strcat( rpl, Client_User( c ));
967 strcat( rpl, Client_Hostname( c ));
971 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
973 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
977 GLOBAL BOOLEAN IRC_ERROR( CLIENT *Client, REQUEST *Req )
979 assert( Client != NULL );
980 assert( Req != NULL );
982 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
983 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
989 LOCAL BOOLEAN Hello_User( CLIENT *Client )
991 assert( Client != NULL );
993 /* Passwort ueberpruefen */
994 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
996 /* Falsches Passwort */
997 Log( LOG_ALERT, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
998 Conn_Close( Client_Conn( Client ), "Bad password!" );
1002 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
1004 IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ));
1005 IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1006 IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr );
1007 IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1009 Client_SetType( Client, CLIENT_USER );
1011 return Show_MOTD( Client );
1015 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
1021 assert( Client != NULL );
1023 fd = fopen( Conf_MotdFile, "r" );
1026 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1027 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1030 IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1033 if( ! fgets( line, 126, fd )) break;
1034 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
1035 IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line );
1037 ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
1045 LOCAL VOID Kill_Nick( CHAR *Nick )
1047 Log( LOG_ALERT, "User(s) with nick \"%s\" will be disconnected!", Nick );
1049 Log( LOG_ALERT, "[Kill_Nick() not implemented - OOOPS!]" );