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.26 2002/01/05 16:51:18 alex Exp $
17 * Revision 1.26 2002/01/05 16:51:18 alex
18 * - das Passwort von Servern wird nun ueberprueft (PASS- und SERVER-Befehl).
20 * Revision 1.25 2002/01/05 00:48:33 alex
21 * - bei SQUIT wurde immer die Verbindung getrennt, auch bei Remote-Servern.
23 * Revision 1.24 2002/01/04 17:58:44 alex
24 * - IRC_WriteStrXXX()-Funktionen eingefuehrt, groessere Anpassungen daran.
25 * - neuer Befehl SQUIT, QUIT an Server-Links angepasst.
27 * Revision 1.23 2002/01/04 01:36:40 alex
28 * - Loglevel ein wenig angepasst.
30 * Revision 1.22 2002/01/04 01:21:47 alex
31 * - Client-Strukruren werden nur noch ueber Funktionen angesprochen.
32 * - Weitere Anpassungen und Erweiterungen der Server-Links.
34 * Revision 1.21 2002/01/03 02:26:51 alex
35 * - neue Befehle SERVER und NJOIN begonnen,
36 * - begonnen, diverse IRC-Befehle an Server-Links anzupassen.
38 * Revision 1.20 2002/01/02 12:46:41 alex
39 * - die Gross- und Kleinschreibung des Nicks kann mit NICK nun geaendert werden.
41 * Revision 1.19 2002/01/02 02:51:39 alex
42 * - Copyright-Texte angepasst.
43 * - neuer Befehl "ERROR".
45 * Revision 1.17 2001/12/31 15:33:13 alex
46 * - neuer Befehl NAMES, kleinere Bugfixes.
47 * - Bug bei PING behoben: war zu restriktiv implementiert :-)
49 * Revision 1.16 2001/12/31 02:18:51 alex
50 * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
51 * - neuen Header "defines.h" mit (fast) allen Konstanten.
52 * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
54 * Revision 1.15 2001/12/30 19:26:11 alex
55 * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
57 * Revision 1.14 2001/12/30 11:42:00 alex
58 * - der Server meldet nun eine ordentliche "Start-Zeit".
60 * Revision 1.13 2001/12/29 03:10:06 alex
61 * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
62 * - neue configure-Optione "--enable-strict-rfc".
64 * Revision 1.12 2001/12/27 19:17:26 alex
65 * - neue Befehle PRIVMSG, NOTICE, PING.
67 * Revision 1.11 2001/12/27 16:55:41 alex
68 * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
70 * Revision 1.10 2001/12/26 22:48:53 alex
71 * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
73 * Revision 1.9 2001/12/26 14:45:37 alex
76 * Revision 1.8 2001/12/26 03:21:46 alex
77 * - PING/PONG-Befehle implementiert,
78 * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
80 * Revision 1.7 2001/12/25 23:25:18 alex
81 * - und nochmal Aenderungen am Logging ;-)
83 * Revision 1.6 2001/12/25 23:13:33 alex
84 * - Debug-Meldungen angepasst.
86 * Revision 1.5 2001/12/25 22:02:42 alex
87 * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
89 * Revision 1.4 2001/12/25 19:19:30 alex
90 * - bessere Fehler-Abfragen, diverse Bugfixes.
91 * - Nicks werden nur einmal vergeben :-)
92 * - /MOTD wird unterstuetzt.
94 * Revision 1.3 2001/12/24 01:34:06 alex
95 * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
96 * - MOTD-Ausgabe begonnen zu implementieren.
98 * Revision 1.2 2001/12/23 21:57:16 alex
99 * - erste IRC-Befehle zu implementieren begonnen.
101 * Revision 1.1 2001/12/14 08:13:43 alex
102 * - neues Modul begonnen :-)
122 #include "messages.h"
130 #define CONNECTED TRUE
131 #define DISCONNECTED FALSE
134 LOCAL BOOLEAN Hello_User( CLIENT *Client );
135 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
137 LOCAL VOID Kill_Nick( CHAR *Nick );
140 GLOBAL VOID IRC_Init( VOID )
145 GLOBAL VOID IRC_Exit( VOID )
150 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CHAR *Format, ... )
153 BOOLEAN ok = CONNECTED;
156 assert( Client != NULL );
157 assert( Format != NULL );
159 va_start( ap, Format );
160 vsnprintf( buffer, 1000, Format, ap );
163 /* an den Client selber */
164 ok = IRC_WriteStrClientPrefix( Client, Client_ThisServer( ), buffer );
167 } /* IRC_WriteStrClient */
170 GLOBAL BOOLEAN IRC_WriteStrClientPrefix( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
172 /* Text an Clients, lokal bzw. remote, senden. */
175 BOOLEAN ok = CONNECTED;
179 assert( Client != NULL );
180 assert( Format != NULL );
182 va_start( ap, Format );
183 vsnprintf( buffer, 1000, Format, ap );
186 if( Client_Conn( Client ) != NONE ) send_to = Client_Conn( Client );
187 else send_to = Client_Conn( Client_Introducer( Client ));
189 if( Prefix ) ok = Conn_WriteStr( send_to, ":%s %s", Client_Mask( Prefix ), buffer );
190 else ok = Conn_WriteStr( send_to, buffer );
193 } /* IRC_WriteStrClientPrefix */
196 GLOBAL BOOLEAN IRC_WriteStrRelated( CLIENT *Client, CHAR *Format, ... )
199 BOOLEAN ok = CONNECTED;
202 assert( Client != NULL );
203 assert( Format != NULL );
205 va_start( ap, Format );
206 vsnprintf( buffer, 1000, Format, ap );
209 /* an den Client selber */
210 ok = IRC_WriteStrClient( Client, buffer );
213 } /* IRC_WriteStrRelated */
216 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
218 assert( Client != NULL );
219 assert( Req != NULL );
221 /* Fehler liefern, wenn kein lokaler Client */
222 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
224 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
226 /* noch nicht registrierte unbekannte Verbindung */
227 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
229 /* Passwort speichern */
230 Client_SetPassword( Client, Req->argv[0] );
232 Client_SetType( Client, CLIENT_GOTPASS );
235 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
237 /* noch nicht registrierte Server-Verbindung */
238 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
240 /* Passwort speichern */
241 Client_SetPassword( Client, Req->argv[0] );
243 Client_SetType( Client, CLIENT_GOTPASSSERVER );
246 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
248 /* Falsche Anzahl Parameter? */
249 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
251 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
255 GLOBAL BOOLEAN IRC_SERVER( CLIENT *Client, REQUEST *Req )
257 CHAR str[LINE_LEN], *ptr;
261 assert( Client != NULL );
262 assert( Req != NULL );
264 /* Fehler liefern, wenn kein lokaler Client */
265 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
267 if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
269 /* Verbindung soll als Server-Server-Verbindung registriert werden */
270 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
272 /* Falsche Anzahl Parameter? */
273 if( Req->argc != 3 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
275 /* Ist dieser Server bei uns konfiguriert? */
276 for( i = 0; i < Conf_Server_Count; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
277 if( i >= Conf_Server_Count )
279 /* Server ist nicht konfiguriert! */
280 Log( LOG_ALERT, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
281 Conn_Close( Client_Conn( Client ), "Server not configured here!" );
284 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd ) != 0 )
286 /* Falsches Passwort */
287 Log( LOG_ALERT, "Connection %d: Bad password for server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
288 Conn_Close( Client_Conn( Client ), "Bad password!" );
292 /* Ist ein Server mit dieser ID bereits registriert? */
293 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
295 /* Server-Strukturen fuellen ;-) */
296 Client_SetID( Client, Req->argv[0] );
297 Client_SetToken( Client, atoi( Req->argv[1] ));
298 Client_SetInfo( Client, Req->argv[2] );
299 Client_SetHops( Client, 1 );
301 Log( LOG_NOTICE, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), Client_Conn( Client ));
303 Client_SetType( Client, CLIENT_SERVER );
306 else if( Client_Type( Client ) == CLIENT_SERVER )
308 /* Neuer Server wird im Netz angekuendigt */
310 /* Falsche Anzahl Parameter? */
311 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
313 /* Ist ein Server mit dieser ID bereits registriert? */
314 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
316 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
317 ptr = strchr( Req->argv[3] + 2, '[' );
318 if( ! ptr ) ptr = Req->argv[3];
320 /* Neue Client-Struktur anlegen */
321 c = Client_NewRemoteServer( Client, Req->argv[0], atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr );
324 /* Neue Client-Struktur konnte nicht angelegt werden */
325 Log( LOG_ALERT, "Can't allocate client structure for server! (on connection %d)", Client_Conn( Client ));
326 Conn_Close( Client_Conn( Client ), "Can't allocate client structure for remote server!" );
330 /* Log-Meldung zusammenbauen und ausgeben */
331 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) sprintf( str, "connected to %s, ", Req->prefix );
332 else strcpy( str, "" );
333 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": "" );
337 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
341 GLOBAL BOOLEAN IRC_NJOIN( CLIENT *Client, REQUEST *Req )
343 assert( Client != NULL );
344 assert( Req != NULL );
346 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTEREDSERVER_MSG, Client_ID( Client ));
352 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
356 assert( Client != NULL );
357 assert( Req != NULL );
359 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
361 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 )
363 if( Client_Type( Client ) == CLIENT_UNKNOWN || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_USER )
366 /* User-Registrierung bzw. Nick-Aenderung */
368 /* Falsche Anzahl Parameter? */
369 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
371 /* Ist der Client "restricted"? */
372 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
374 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
375 * wir nichts. So macht es das Original und mind. Snak hat probleme,
376 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
378 if( strcmp( Client_ID( Client ), Req->argv[0] ) == 0 ) return CONNECTED;
381 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
382 * will nur die Gross- und Kleinschreibung aendern. Das darf
383 * er natuerlich machen :-) */
384 if( strcasecmp( Client_ID( Client ), Req->argv[0] ) != 0 )
386 if( ! Client_CheckNick( Client, Req->argv[0] )) return CONNECTED;
389 if( Client_Type( Client ) == CLIENT_USER )
391 /* Nick-Aenderung: allen mitteilen! */
392 Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( Client ), Client_ID( Client ), Req->argv[0] );
393 IRC_WriteStrRelated( Client, "NICK :%s", Req->argv[0] );
396 /* Client-Nick registrieren */
397 Client_SetID( Client, Req->argv[0] );
399 if( Client_Type( Client ) != CLIENT_USER )
402 Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client_Conn( Client ));
403 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
404 else Client_SetType( Client, CLIENT_GOTNICK );
408 else if( Client_Type( Client ) == CLIENT_SERVER )
410 /* Server fuehrt neuen Client ein */
412 /* Falsche Anzahl Parameter? */
413 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
415 /* Nick ueberpruefen */
416 c = Client_GetFromID( Req->argv[0] );
419 /* Der neue Nick ist auf diesem Server bereits registriert:
420 * sowohl der neue, als auch der alte Client muessen nun
421 * disconnectiert werden. */
422 Log( LOG_ALERT, "Server %s introduces already registered nick %s!", Client_ID( Client ), Req->argv[0] );
423 Kill_Nick( Req->argv[0] );
427 /* Server, zu dem der Client connectiert ist, suchen */
428 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
431 Log( LOG_ALERT, "Server %s introduces nick %s with unknown host server!?", Client_ID( Client ), Req->argv[0] );
432 Kill_Nick( Req->argv[0] );
436 /* Neue Client-Struktur anlegen */
437 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] );
440 /* Eine neue Client-Struktur konnte nicht angelegt werden.
441 * Der Client muss disconnectiert werden, damit der Netz-
442 * status konsistent bleibt. */
443 Log( LOG_ALERT, "Can't allocate client structure! (on connection %d)", Client_Conn( Client ));
444 Kill_Nick( Req->argv[0] );
448 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": "" );
452 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
456 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
458 assert( Client != NULL );
459 assert( Req != NULL );
462 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
464 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
467 /* Falsche Anzahl Parameter? */
468 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
470 Client_SetUser( Client, Req->argv[0] );
471 Client_SetInfo( Client, Req->argv[3] );
473 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client_Conn( Client ));
474 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
475 else Client_SetType( Client, CLIENT_GOTUSER );
478 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
480 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
482 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
486 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
490 assert( Client != NULL );
491 assert( Req != NULL );
493 if(( Client_Type( Client ) == CLIENT_USER ) || ( Client_Type( Client ) == CLIENT_SERVICE ))
497 /* Falsche Anzahl Parameter? */
498 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
500 Conn_Close( Client_Conn( Client ), "Client wants to quit." );
503 else if ( Client_Type( Client ) == CLIENT_SERVER )
507 /* Falsche Anzahl Parameter? */
508 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
510 target = Client_Search( Req->prefix );
511 if( ! target ) Log( LOG_ALERT, "Got QUIT from %s for unknown server!?", Client_ID( Client ));
512 else Client_Destroy( target );
515 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
519 GLOBAL BOOLEAN IRC_SQUIT( CLIENT *Client, REQUEST *Req )
523 assert( Client != NULL );
524 assert( Req != NULL );
526 /* SQUIT ist nur fuer Server erlaubt */
527 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
529 /* Falsche Anzahl Parameter? */
530 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
532 target = Client_GetFromID( Req->argv[0] );
535 Log( LOG_ALERT, "Got SQUIT from %s for unknown server \%s\"!?", Client_ID( Client ), Req->argv[0] );
539 if( target == Client ) Log( LOG_NOTICE, "Got SQUIT from %s: %s", Client_ID( Client ), Req->argv[1] );
540 else Log( LOG_NOTICE, "Got SQUIT from %s for %s: %s", Client_ID( Client ), Client_ID( target ), Req->argv[1] );
542 /* FIXME: das SQUIT muss an alle Server weiter-
543 * geleitet werden ... */
545 if( Client_Conn( target ) > NONE )
547 Conn_Close( Client_Conn( target ), Req->argv[1] );
552 Client_Destroy( target );
558 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
560 assert( Client != NULL );
561 assert( Req != NULL );
563 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
565 /* Falsche Anzahl Parameter? */
566 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
567 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
569 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
573 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
575 assert( Client != NULL );
576 assert( Req != NULL );
578 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
580 /* Falsche Anzahl Parameter? */
581 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
582 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
584 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
585 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
587 Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
592 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
594 assert( Client != NULL );
595 assert( Req != NULL );
597 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
599 /* Falsche Anzahl Parameter? */
600 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
602 return Show_MOTD( Client );
606 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
610 assert( Client != NULL );
611 assert( Req != NULL );
613 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
615 /* Falsche Anzahl Parameter? */
616 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
617 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
618 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
620 to = Client_Search( Req->argv[0] );
623 /* Okay, Ziel ist ein User */
624 if( Client_Conn( Client ) >= 0 ) Conn_UpdateIdle( Client_Conn( Client ));
625 return IRC_WriteStrClientPrefix( to, Client, "PRIVMSG %s :%s", Client_ID( to ), Req->argv[1] );
628 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
632 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
636 assert( Client != NULL );
637 assert( Req != NULL );
639 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
641 /* Falsche Anzahl Parameter? */
642 if( Req->argc != 2 ) return CONNECTED;
644 to = Client_Search( Req->argv[0] );
647 /* Okay, Ziel ist ein User */
648 return IRC_WriteStrClientPrefix( to, Client, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
655 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
657 CHAR x[2], new_modes[CLIENT_MODE_LEN], *ptr;
661 assert( Client != NULL );
662 assert( Req != NULL );
664 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
666 /* Falsche Anzahl Parameter? */
667 if(( Req->argc > 2 ) || ( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
669 /* "Ziel-Client" suchen */
670 target = Client_Search( Req->argv[0] );
672 /* Wer ist der Anfragende? */
673 if( Client_Type( Client ) == CLIENT_USER )
675 /* User: MODE ist nur fuer sich selber zulaessig */
676 if( target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
680 /* Server: gibt es den Client ueberhaupt? */
681 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
684 /* Werden die Modes erfragt? */
685 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( Client ));
689 /* Sollen Modes gesetzt oder geloescht werden? */
690 if( *ptr == '+' ) set = TRUE;
691 else if( *ptr == '-' ) set = FALSE;
692 else return IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
694 /* Reply-String mit Aenderungen vorbereiten */
695 if( set ) strcpy( new_modes, "+" );
696 else strcpy( new_modes, "-" );
704 if( Client_Type( Client ) == CLIENT_SERVER )
718 /* restricted (kann nur gesetzt werden) */
719 if( set ) x[0] = 'r';
720 else ok = IRC_WriteStrClient( target, ERR_RESTRICTED_MSG, Client_ID( target ));
723 /* operator (kann nur geloescht werden) */
726 Client_SetOperByMe( target, FALSE );
729 else ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
732 ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
739 if( ! x[0] ) continue;
741 /* Okay, gueltigen Mode gefunden */
744 /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
745 if( Client_ModeAdd( target, x[0] )) strcat( new_modes, x );
749 /* Modes geloescht. Wenn der Client ihn hatte: merken */
750 if( Client_ModeDel( target, x[0] )) strcat( new_modes, x );
754 /* Geanderte Modes? */
757 if( Client_Type( Client ) == CLIENT_SERVER )
759 /* Modes an andere Server forwarden */
764 /* Bestaetigung an Client schicken */
765 ok = IRC_WriteStrRelated( Client, "MODE %s :%s", Client_ID( target ), new_modes );
767 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( target ), Client_Modes( target ));
773 GLOBAL BOOLEAN IRC_OPER( CLIENT *Client, REQUEST *Req )
777 assert( Client != NULL );
778 assert( Req != NULL );
780 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
782 /* Falsche Anzahl Parameter? */
783 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
785 /* Operator suchen */
786 for( i = 0; i < Conf_Oper_Count; i++)
788 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
790 if( i >= Conf_Oper_Count ) return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
792 /* Stimmt der Name und das Passwort? */
793 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 ));
795 if( ! Client_HasMode( Client, 'o' ))
797 /* noch kein o-Mode gesetzt */
798 Client_ModeAdd( Client, 'o' );
799 if( ! IRC_WriteStrRelated( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
802 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE, "User \"%s\" is now an IRC Operator.", Client_Mask( Client ));
804 Client_SetOperByMe( Client, TRUE );
805 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
809 GLOBAL BOOLEAN IRC_DIE( CLIENT *Client, REQUEST *Req )
811 assert( Client != NULL );
812 assert( Req != NULL );
814 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
816 /* Falsche Anzahl Parameter? */
817 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
819 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
821 Log( LOG_NOTICE, "Got DIE command from \"%s\", going down!", Client_Mask( Client ));
827 GLOBAL BOOLEAN IRC_RESTART( CLIENT *Client, REQUEST *Req )
829 assert( Client != NULL );
830 assert( Req != NULL );
832 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
834 /* Falsche Anzahl Parameter? */
835 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
837 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
839 Log( LOG_NOTICE, "Got RESTART command from \"%s\", going down!", Client_Mask( Client ));
840 NGIRCd_Restart = TRUE;
845 GLOBAL BOOLEAN IRC_NAMES( CLIENT *Client, REQUEST *Req )
847 CHAR rpl[COMMAND_LEN];
850 assert( Client != NULL );
851 assert( Req != NULL );
853 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
855 /* Falsche Anzahl Parameter? */
856 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
858 /* Noch alle User ausgeben, die in keinem Channel sind */
863 if( Client_Type( c ) == CLIENT_USER )
865 /* Okay, das ist ein User */
866 strcat( rpl, Client_ID( c ));
870 /* Antwort zu lang? Splitten. */
871 if( strlen( rpl ) > 480 )
873 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
874 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
878 c = Client_Next( c );
882 /* es wurden User gefunden */
883 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
884 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
886 return IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), "*" );
890 GLOBAL BOOLEAN IRC_ISON( CLIENT *Client, REQUEST *Req )
892 CHAR rpl[COMMAND_LEN];
897 assert( Client != NULL );
898 assert( Req != NULL );
900 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
902 /* Falsche Anzahl Parameter? */
903 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
905 strcpy( rpl, RPL_ISON_MSG );
906 for( i = 0; i < Req->argc; i++ )
908 ptr = strtok( Req->argv[i], " " );
912 c = Client_GetFromID( ptr );
913 if( c && ( Client_Type( c ) == CLIENT_USER ))
915 /* Dieser Nick ist "online" */
919 ptr = strtok( NULL, " " );
922 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
924 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
928 GLOBAL BOOLEAN IRC_WHOIS( CLIENT *Client, REQUEST *Req )
932 assert( Client != NULL );
933 assert( Req != NULL );
935 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
937 /* Falsche Anzahl Parameter? */
938 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
941 c = Client_GetFromID( Req->argv[0] );
942 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
944 /* Nick, User und Name */
945 if( ! IRC_WriteStrClient( Client, RPL_WHOISUSER_MSG, Client_ID( Client ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
948 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;
951 if( Client_HasMode( c, 'o' ))
953 if( ! IRC_WriteStrClient( Client, RPL_WHOISOPERATOR_MSG, Client_ID( Client ), Client_ID( c ))) return DISCONNECTED;
956 /* Idle (nur lokale Clients) */
957 if( Client_Conn( c ) > NONE )
959 if( ! IRC_WriteStrClient( Client, RPL_WHOISIDLE_MSG, Client_ID( Client ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
963 return IRC_WriteStrClient( Client, RPL_ENDOFWHOIS_MSG, Client_ID( Client ), Client_ID( c ));
967 GLOBAL BOOLEAN IRC_USERHOST( CLIENT *Client, REQUEST *Req )
969 CHAR rpl[COMMAND_LEN];
973 assert( Client != NULL );
974 assert( Req != NULL );
976 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
978 /* Falsche Anzahl Parameter? */
979 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
981 if( Req->argc > 5 ) max = 5;
982 else max = Req->argc;
984 strcpy( rpl, RPL_USERHOST_MSG );
985 for( i = 0; i < max; i++ )
987 c = Client_GetFromID( Req->argv[i] );
988 if( c && ( Client_Type( c ) == CLIENT_USER ))
990 /* Dieser Nick ist "online" */
991 strcat( rpl, Client_ID( c ));
992 if( Client_HasMode( c, 'o' )) strcat( rpl, "*" );
994 if( Client_HasMode( c, 'a' )) strcat( rpl, "-" );
995 else strcat( rpl, "+" );
996 strcat( rpl, Client_User( c ));
998 strcat( rpl, Client_Hostname( c ));
1002 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1004 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1005 } /* IRC_USERHOST */
1008 GLOBAL BOOLEAN IRC_ERROR( CLIENT *Client, REQUEST *Req )
1010 assert( Client != NULL );
1011 assert( Req != NULL );
1013 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
1014 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
1020 LOCAL BOOLEAN Hello_User( CLIENT *Client )
1022 assert( Client != NULL );
1024 /* Passwort ueberpruefen */
1025 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
1027 /* Falsches Passwort */
1028 Log( LOG_ALERT, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
1029 Conn_Close( Client_Conn( Client ), "Bad password!" );
1030 return DISCONNECTED;
1033 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
1035 IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ));
1036 IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1037 IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr );
1038 IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1040 Client_SetType( Client, CLIENT_USER );
1042 return Show_MOTD( Client );
1046 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
1052 assert( Client != NULL );
1054 fd = fopen( Conf_MotdFile, "r" );
1057 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1058 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1061 IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1064 if( ! fgets( line, 126, fd )) break;
1065 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
1066 IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line );
1068 ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
1076 LOCAL VOID Kill_Nick( CHAR *Nick )
1078 Log( LOG_ALERT, "User(s) with nick \"%s\" will be disconnected!", Nick );
1080 Log( LOG_ALERT, "[Kill_Nick() not implemented - OOOPS!]" );