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.25 2002/01/05 00:48:33 alex Exp $
17 * Revision 1.25 2002/01/05 00:48:33 alex
18 * - bei SQUIT wurde immer die Verbindung getrennt, auch bei Remote-Servern.
20 * Revision 1.24 2002/01/04 17:58:44 alex
21 * - IRC_WriteStrXXX()-Funktionen eingefuehrt, groessere Anpassungen daran.
22 * - neuer Befehl SQUIT, QUIT an Server-Links angepasst.
24 * Revision 1.23 2002/01/04 01:36:40 alex
25 * - Loglevel ein wenig angepasst.
27 * Revision 1.22 2002/01/04 01:21:47 alex
28 * - Client-Strukruren werden nur noch ueber Funktionen angesprochen.
29 * - Weitere Anpassungen und Erweiterungen der Server-Links.
31 * Revision 1.21 2002/01/03 02:26:51 alex
32 * - neue Befehle SERVER und NJOIN begonnen,
33 * - begonnen, diverse IRC-Befehle an Server-Links anzupassen.
35 * Revision 1.20 2002/01/02 12:46:41 alex
36 * - die Gross- und Kleinschreibung des Nicks kann mit NICK nun geaendert werden.
38 * Revision 1.19 2002/01/02 02:51:39 alex
39 * - Copyright-Texte angepasst.
40 * - neuer Befehl "ERROR".
42 * Revision 1.17 2001/12/31 15:33:13 alex
43 * - neuer Befehl NAMES, kleinere Bugfixes.
44 * - Bug bei PING behoben: war zu restriktiv implementiert :-)
46 * Revision 1.16 2001/12/31 02:18:51 alex
47 * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
48 * - neuen Header "defines.h" mit (fast) allen Konstanten.
49 * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
51 * Revision 1.15 2001/12/30 19:26:11 alex
52 * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
54 * Revision 1.14 2001/12/30 11:42:00 alex
55 * - der Server meldet nun eine ordentliche "Start-Zeit".
57 * Revision 1.13 2001/12/29 03:10:06 alex
58 * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
59 * - neue configure-Optione "--enable-strict-rfc".
61 * Revision 1.12 2001/12/27 19:17:26 alex
62 * - neue Befehle PRIVMSG, NOTICE, PING.
64 * Revision 1.11 2001/12/27 16:55:41 alex
65 * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
67 * Revision 1.10 2001/12/26 22:48:53 alex
68 * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
70 * Revision 1.9 2001/12/26 14:45:37 alex
73 * Revision 1.8 2001/12/26 03:21:46 alex
74 * - PING/PONG-Befehle implementiert,
75 * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
77 * Revision 1.7 2001/12/25 23:25:18 alex
78 * - und nochmal Aenderungen am Logging ;-)
80 * Revision 1.6 2001/12/25 23:13:33 alex
81 * - Debug-Meldungen angepasst.
83 * Revision 1.5 2001/12/25 22:02:42 alex
84 * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
86 * Revision 1.4 2001/12/25 19:19:30 alex
87 * - bessere Fehler-Abfragen, diverse Bugfixes.
88 * - Nicks werden nur einmal vergeben :-)
89 * - /MOTD wird unterstuetzt.
91 * Revision 1.3 2001/12/24 01:34:06 alex
92 * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
93 * - MOTD-Ausgabe begonnen zu implementieren.
95 * Revision 1.2 2001/12/23 21:57:16 alex
96 * - erste IRC-Befehle zu implementieren begonnen.
98 * Revision 1.1 2001/12/14 08:13:43 alex
99 * - neues Modul begonnen :-)
119 #include "messages.h"
127 #define CONNECTED TRUE
128 #define DISCONNECTED FALSE
131 LOCAL BOOLEAN Hello_User( CLIENT *Client );
132 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
134 LOCAL VOID Kill_Nick( CHAR *Nick );
137 GLOBAL VOID IRC_Init( VOID )
142 GLOBAL VOID IRC_Exit( VOID )
147 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CHAR *Format, ... )
150 BOOLEAN ok = CONNECTED;
153 assert( Client != NULL );
154 assert( Format != NULL );
156 va_start( ap, Format );
157 vsnprintf( buffer, 1000, Format, ap );
160 /* an den Client selber */
161 ok = IRC_WriteStrClientPrefix( Client, Client_ThisServer( ), buffer );
164 } /* IRC_WriteStrClient */
167 GLOBAL BOOLEAN IRC_WriteStrClientPrefix( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
169 /* Text an Clients, lokal bzw. remote, senden. */
172 BOOLEAN ok = CONNECTED;
176 assert( Client != NULL );
177 assert( Format != NULL );
179 va_start( ap, Format );
180 vsnprintf( buffer, 1000, Format, ap );
183 if( Client_Conn( Client ) != NONE ) send_to = Client_Conn( Client );
184 else send_to = Client_Conn( Client_Introducer( Client ));
186 if( Prefix ) ok = Conn_WriteStr( send_to, ":%s %s", Client_Mask( Prefix ), buffer );
187 else ok = Conn_WriteStr( send_to, buffer );
190 } /* IRC_WriteStrClientPrefix */
193 GLOBAL BOOLEAN IRC_WriteStrRelated( CLIENT *Client, CHAR *Format, ... )
196 BOOLEAN ok = CONNECTED;
199 assert( Client != NULL );
200 assert( Format != NULL );
202 va_start( ap, Format );
203 vsnprintf( buffer, 1000, Format, ap );
206 /* an den Client selber */
207 ok = IRC_WriteStrClient( Client, buffer );
210 } /* IRC_WriteStrRelated */
213 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
215 assert( Client != NULL );
216 assert( Req != NULL );
218 /* Fehler liefern, wenn kein lokaler Client */
219 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
221 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
223 /* noch nicht registrierte unbekannte Verbindung */
224 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
226 /* Passwort speichern */
227 Client_SetPassword( Client, Req->argv[0] );
229 Client_SetType( Client, CLIENT_GOTPASS );
232 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
234 /* noch nicht registrierte Server-Verbindung */
235 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
237 /* Passwort speichern */
238 Client_SetPassword( Client, Req->argv[0] );
240 Client_SetType( Client, CLIENT_GOTPASSSERVER );
243 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
245 /* Falsche Anzahl Parameter? */
246 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
248 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
252 GLOBAL BOOLEAN IRC_SERVER( CLIENT *Client, REQUEST *Req )
254 CHAR str[LINE_LEN], *ptr;
257 assert( Client != NULL );
258 assert( Req != NULL );
260 /* Fehler liefern, wenn kein lokaler Client */
261 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
263 if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
265 /* Verbindung soll als Server-Server-Verbindung registriert werden */
266 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
268 /* Falsche Anzahl Parameter? */
269 if( Req->argc != 3 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
271 /* Ist ein Server mit dieser ID bereits registriert? */
272 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
274 /* Server-Strukturen fuellen ;-) */
275 Client_SetID( Client, Req->argv[0] );
276 Client_SetToken( Client, atoi( Req->argv[1] ));
277 Client_SetInfo( Client, Req->argv[2] );
278 Client_SetHops( Client, 1 );
280 Log( LOG_NOTICE, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), Client_Conn( Client ));
282 Client_SetType( Client, CLIENT_SERVER );
285 else if( Client_Type( Client ) == CLIENT_SERVER )
287 /* Neuer Server wird im Netz angekuendigt */
289 /* Falsche Anzahl Parameter? */
290 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
292 /* Ist ein Server mit dieser ID bereits registriert? */
293 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
295 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
296 ptr = strchr( Req->argv[3] + 2, '[' );
297 if( ! ptr ) ptr = Req->argv[3];
299 /* Neue Client-Struktur anlegen */
300 c = Client_NewRemoteServer( Client, Req->argv[0], atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr );
303 /* Neue Client-Struktur konnte nicht angelegt werden */
304 Log( LOG_ALERT, "Can't allocate client structure for server! (on connection %d)", Client_Conn( Client ));
305 Conn_Close( Client_Conn( Client ), "Can't allocate client structure for remote server!" );
309 /* Log-Meldung zusammenbauen und ausgeben */
310 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) sprintf( str, "connected to %s, ", Req->prefix );
311 else strcpy( str, "" );
312 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": "" );
316 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
320 GLOBAL BOOLEAN IRC_NJOIN( CLIENT *Client, REQUEST *Req )
322 assert( Client != NULL );
323 assert( Req != NULL );
325 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTEREDSERVER_MSG, Client_ID( Client ));
331 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
335 assert( Client != NULL );
336 assert( Req != NULL );
338 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
340 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 )
342 if( Client_Type( Client ) == CLIENT_UNKNOWN || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_USER )
345 /* User-Registrierung bzw. Nick-Aenderung */
347 /* Falsche Anzahl Parameter? */
348 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
350 /* Ist der Client "restricted"? */
351 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
353 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
354 * wir nichts. So macht es das Original und mind. Snak hat probleme,
355 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
357 if( strcmp( Client_ID( Client ), Req->argv[0] ) == 0 ) return CONNECTED;
360 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
361 * will nur die Gross- und Kleinschreibung aendern. Das darf
362 * er natuerlich machen :-) */
363 if( strcasecmp( Client_ID( Client ), Req->argv[0] ) != 0 )
365 if( ! Client_CheckNick( Client, Req->argv[0] )) return CONNECTED;
368 if( Client_Type( Client ) == CLIENT_USER )
370 /* Nick-Aenderung: allen mitteilen! */
371 Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( Client ), Client_ID( Client ), Req->argv[0] );
372 IRC_WriteStrRelated( Client, "NICK :%s", Req->argv[0] );
375 /* Client-Nick registrieren */
376 Client_SetID( Client, Req->argv[0] );
378 if( Client_Type( Client ) != CLIENT_USER )
381 Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client_Conn( Client ));
382 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
383 else Client_SetType( Client, CLIENT_GOTNICK );
387 else if( Client_Type( Client ) == CLIENT_SERVER )
389 /* Server fuehrt neuen Client ein */
391 /* Falsche Anzahl Parameter? */
392 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
394 /* Nick ueberpruefen */
395 c = Client_GetFromID( Req->argv[0] );
398 /* Der neue Nick ist auf diesem Server bereits registriert:
399 * sowohl der neue, als auch der alte Client muessen nun
400 * disconnectiert werden. */
401 Log( LOG_ALERT, "Server %s introduces already registered nick %s!", Client_ID( Client ), Req->argv[0] );
402 Kill_Nick( Req->argv[0] );
406 /* Server, zu dem der Client connectiert ist, suchen */
407 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
410 Log( LOG_ALERT, "Server %s introduces nick %s with unknown host server!?", Client_ID( Client ), Req->argv[0] );
411 Kill_Nick( Req->argv[0] );
415 /* Neue Client-Struktur anlegen */
416 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] );
419 /* Eine neue Client-Struktur konnte nicht angelegt werden.
420 * Der Client muss disconnectiert werden, damit der Netz-
421 * status konsistent bleibt. */
422 Log( LOG_ALERT, "Can't allocate client structure! (on connection %d)", Client_Conn( Client ));
423 Kill_Nick( Req->argv[0] );
427 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": "" );
431 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
435 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
437 assert( Client != NULL );
438 assert( Req != NULL );
441 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
443 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
446 /* Falsche Anzahl Parameter? */
447 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
449 Client_SetUser( Client, Req->argv[0] );
450 Client_SetInfo( Client, Req->argv[3] );
452 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client_Conn( Client ));
453 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
454 else Client_SetType( Client, CLIENT_GOTUSER );
457 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
459 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
461 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
465 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
469 assert( Client != NULL );
470 assert( Req != NULL );
472 if(( Client_Type( Client ) == CLIENT_USER ) || ( Client_Type( Client ) == CLIENT_SERVICE ))
476 /* Falsche Anzahl Parameter? */
477 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
479 Conn_Close( Client_Conn( Client ), "Client wants to quit." );
482 else if ( Client_Type( Client ) == CLIENT_SERVER )
486 /* Falsche Anzahl Parameter? */
487 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
489 target = Client_Search( Req->prefix );
490 if( ! target ) Log( LOG_ALERT, "Got QUIT from %s for unknown server!?", Client_ID( Client ));
491 else Client_Destroy( target );
494 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
498 GLOBAL BOOLEAN IRC_SQUIT( CLIENT *Client, REQUEST *Req )
502 assert( Client != NULL );
503 assert( Req != NULL );
505 /* SQUIT ist nur fuer Server erlaubt */
506 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
508 /* Falsche Anzahl Parameter? */
509 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
511 target = Client_GetFromID( Req->argv[0] );
514 Log( LOG_ALERT, "Got SQUIT from %s for unknown server \%s\"!?", Client_ID( Client ), Req->argv[0] );
518 if( target == Client ) Log( LOG_NOTICE, "Got SQUIT from %s: %s", Client_ID( Client ), Req->argv[1] );
519 else Log( LOG_NOTICE, "Got SQUIT from %s for %s: %s", Client_ID( Client ), Client_ID( target ), Req->argv[1] );
521 /* FIXME: das SQUIT muss an alle Server weiter-
522 * geleitet werden ... */
524 if( Client_Conn( target ) > NONE )
526 Conn_Close( Client_Conn( target ), Req->argv[1] );
531 Client_Destroy( target );
537 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
539 assert( Client != NULL );
540 assert( Req != NULL );
542 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
544 /* Falsche Anzahl Parameter? */
545 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
546 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
548 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
552 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
554 assert( Client != NULL );
555 assert( Req != NULL );
557 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
559 /* Falsche Anzahl Parameter? */
560 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
561 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
563 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
564 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
566 Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
571 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
573 assert( Client != NULL );
574 assert( Req != NULL );
576 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
578 /* Falsche Anzahl Parameter? */
579 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
581 return Show_MOTD( Client );
585 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
589 assert( Client != NULL );
590 assert( Req != NULL );
592 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
594 /* Falsche Anzahl Parameter? */
595 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
596 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
597 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
599 to = Client_Search( Req->argv[0] );
602 /* Okay, Ziel ist ein User */
603 if( Client_Conn( Client ) >= 0 ) Conn_UpdateIdle( Client_Conn( Client ));
604 return IRC_WriteStrClientPrefix( to, Client, "PRIVMSG %s :%s", Client_ID( to ), Req->argv[1] );
607 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
611 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
615 assert( Client != NULL );
616 assert( Req != NULL );
618 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
620 /* Falsche Anzahl Parameter? */
621 if( Req->argc != 2 ) return CONNECTED;
623 to = Client_Search( Req->argv[0] );
626 /* Okay, Ziel ist ein User */
627 return IRC_WriteStrClientPrefix( to, Client, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
634 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
636 CHAR x[2], new_modes[CLIENT_MODE_LEN], *ptr;
640 assert( Client != NULL );
641 assert( Req != NULL );
643 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
645 /* Falsche Anzahl Parameter? */
646 if(( Req->argc > 2 ) || ( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
648 /* "Ziel-Client" suchen */
649 target = Client_Search( Req->argv[0] );
651 /* Wer ist der Anfragende? */
652 if( Client_Type( Client ) == CLIENT_USER )
654 /* User: MODE ist nur fuer sich selber zulaessig */
655 if( target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
659 /* Server: gibt es den Client ueberhaupt? */
660 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
663 /* Werden die Modes erfragt? */
664 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( Client ));
668 /* Sollen Modes gesetzt oder geloescht werden? */
669 if( *ptr == '+' ) set = TRUE;
670 else if( *ptr == '-' ) set = FALSE;
671 else return IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
673 /* Reply-String mit Aenderungen vorbereiten */
674 if( set ) strcpy( new_modes, "+" );
675 else strcpy( new_modes, "-" );
683 if( Client_Type( Client ) == CLIENT_SERVER )
697 /* restricted (kann nur gesetzt werden) */
698 if( set ) x[0] = 'r';
699 else ok = IRC_WriteStrClient( target, ERR_RESTRICTED_MSG, Client_ID( target ));
702 /* operator (kann nur geloescht werden) */
705 Client_SetOperByMe( target, FALSE );
708 else ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
711 ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
718 if( ! x[0] ) continue;
720 /* Okay, gueltigen Mode gefunden */
723 /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
724 if( Client_ModeAdd( target, x[0] )) strcat( new_modes, x );
728 /* Modes geloescht. Wenn der Client ihn hatte: merken */
729 if( Client_ModeDel( target, x[0] )) strcat( new_modes, x );
733 /* Geanderte Modes? */
736 if( Client_Type( Client ) == CLIENT_SERVER )
738 /* Modes an andere Server forwarden */
743 /* Bestaetigung an Client schicken */
744 ok = IRC_WriteStrRelated( Client, "MODE %s :%s", Client_ID( target ), new_modes );
746 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( target ), Client_Modes( target ));
752 GLOBAL BOOLEAN IRC_OPER( CLIENT *Client, REQUEST *Req )
756 assert( Client != NULL );
757 assert( Req != NULL );
759 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
761 /* Falsche Anzahl Parameter? */
762 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
764 /* Operator suchen */
765 for( i = 0; i < Conf_Oper_Count; i++)
767 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
769 if( i >= Conf_Oper_Count ) return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
771 /* Stimmt der Name und das Passwort? */
772 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 ));
774 if( ! Client_HasMode( Client, 'o' ))
776 /* noch kein o-Mode gesetzt */
777 Client_ModeAdd( Client, 'o' );
778 if( ! IRC_WriteStrRelated( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
781 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE, "User \"%s\" is now an IRC Operator.", Client_Mask( Client ));
783 Client_SetOperByMe( Client, TRUE );
784 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
788 GLOBAL BOOLEAN IRC_DIE( CLIENT *Client, REQUEST *Req )
790 assert( Client != NULL );
791 assert( Req != NULL );
793 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
795 /* Falsche Anzahl Parameter? */
796 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
798 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
800 Log( LOG_NOTICE, "Got DIE command from \"%s\", going down!", Client_Mask( Client ));
806 GLOBAL BOOLEAN IRC_RESTART( CLIENT *Client, REQUEST *Req )
808 assert( Client != NULL );
809 assert( Req != NULL );
811 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
813 /* Falsche Anzahl Parameter? */
814 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
816 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
818 Log( LOG_NOTICE, "Got RESTART command from \"%s\", going down!", Client_Mask( Client ));
819 NGIRCd_Restart = TRUE;
824 GLOBAL BOOLEAN IRC_NAMES( CLIENT *Client, REQUEST *Req )
826 CHAR rpl[COMMAND_LEN];
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 /* Noch alle User ausgeben, die in keinem Channel sind */
842 if( Client_Type( c ) == CLIENT_USER )
844 /* Okay, das ist ein User */
845 strcat( rpl, Client_ID( c ));
849 /* Antwort zu lang? Splitten. */
850 if( strlen( rpl ) > 480 )
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;
857 c = Client_Next( c );
861 /* es wurden User gefunden */
862 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
863 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
865 return IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), "*" );
869 GLOBAL BOOLEAN IRC_ISON( CLIENT *Client, REQUEST *Req )
871 CHAR rpl[COMMAND_LEN];
876 assert( Client != NULL );
877 assert( Req != NULL );
879 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
881 /* Falsche Anzahl Parameter? */
882 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
884 strcpy( rpl, RPL_ISON_MSG );
885 for( i = 0; i < Req->argc; i++ )
887 ptr = strtok( Req->argv[i], " " );
891 c = Client_GetFromID( ptr );
892 if( c && ( Client_Type( c ) == CLIENT_USER ))
894 /* Dieser Nick ist "online" */
898 ptr = strtok( NULL, " " );
901 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
903 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
907 GLOBAL BOOLEAN IRC_WHOIS( CLIENT *Client, REQUEST *Req )
911 assert( Client != NULL );
912 assert( Req != NULL );
914 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
916 /* Falsche Anzahl Parameter? */
917 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
920 c = Client_GetFromID( Req->argv[0] );
921 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
923 /* Nick, User und Name */
924 if( ! IRC_WriteStrClient( Client, RPL_WHOISUSER_MSG, Client_ID( Client ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
927 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;
930 if( Client_HasMode( c, 'o' ))
932 if( ! IRC_WriteStrClient( Client, RPL_WHOISOPERATOR_MSG, Client_ID( Client ), Client_ID( c ))) return DISCONNECTED;
935 /* Idle (nur lokale Clients) */
936 if( Client_Conn( c ) > NONE )
938 if( ! IRC_WriteStrClient( Client, RPL_WHOISIDLE_MSG, Client_ID( Client ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
942 return IRC_WriteStrClient( Client, RPL_ENDOFWHOIS_MSG, Client_ID( Client ), Client_ID( c ));
946 GLOBAL BOOLEAN IRC_USERHOST( CLIENT *Client, REQUEST *Req )
948 CHAR rpl[COMMAND_LEN];
952 assert( Client != NULL );
953 assert( Req != NULL );
955 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
957 /* Falsche Anzahl Parameter? */
958 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
960 if( Req->argc > 5 ) max = 5;
961 else max = Req->argc;
963 strcpy( rpl, RPL_USERHOST_MSG );
964 for( i = 0; i < max; i++ )
966 c = Client_GetFromID( Req->argv[i] );
967 if( c && ( Client_Type( c ) == CLIENT_USER ))
969 /* Dieser Nick ist "online" */
970 strcat( rpl, Client_ID( c ));
971 if( Client_HasMode( c, 'o' )) strcat( rpl, "*" );
973 if( Client_HasMode( c, 'a' )) strcat( rpl, "-" );
974 else strcat( rpl, "+" );
975 strcat( rpl, Client_User( c ));
977 strcat( rpl, Client_Hostname( c ));
981 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
983 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
987 GLOBAL BOOLEAN IRC_ERROR( CLIENT *Client, REQUEST *Req )
989 assert( Client != NULL );
990 assert( Req != NULL );
992 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
993 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
999 LOCAL BOOLEAN Hello_User( CLIENT *Client )
1001 assert( Client != NULL );
1003 /* Passwort ueberpruefen */
1004 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
1006 /* Falsches Passwort */
1007 Log( LOG_ALERT, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
1008 Conn_Close( Client_Conn( Client ), "Bad password!" );
1009 return DISCONNECTED;
1012 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
1014 IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ));
1015 IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1016 IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr );
1017 IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1019 Client_SetType( Client, CLIENT_USER );
1021 return Show_MOTD( Client );
1025 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
1031 assert( Client != NULL );
1033 fd = fopen( Conf_MotdFile, "r" );
1036 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1037 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1040 IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1043 if( ! fgets( line, 126, fd )) break;
1044 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
1045 IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line );
1047 ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
1055 LOCAL VOID Kill_Nick( CHAR *Nick )
1057 Log( LOG_ALERT, "User(s) with nick \"%s\" will be disconnected!", Nick );
1059 Log( LOG_ALERT, "[Kill_Nick() not implemented - OOOPS!]" );