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.55 2002/02/12 14:40:37 alex Exp $
17 * Revision 1.55 2002/02/12 14:40:37 alex
18 * - via NJOIN gemeldete Benutzer wurden nicht in Channels bekannt gemacht.
20 * Revision 1.54 2002/02/11 23:33:35 alex
21 * - weitere Anpassungen an Channel-Modes und Channel-User-Modes.
23 * Revision 1.53 2002/02/11 16:06:21 alex
24 * - Die Quelle von MODE-Aenderungen wird nun korrekt weitergegeben.
26 * Revision 1.52 2002/02/11 15:52:21 alex
27 * - PONG an den Server selber wurde faelschlicherweise versucht zu forwarden.
28 * - Channel-Modes wurden falsch geliefert (als User-Modes).
30 * Revision 1.51 2002/02/11 15:15:53 alex
31 * - PING und PONG werden nun auch korrekt an andere Server geforwarded.
32 * - bei MODE-Meldungen wird der letzte Parameter nicht mehr mit ":" getrennt.
34 * Revision 1.50 2002/02/11 01:03:20 alex
35 * - Aenderungen und Anpassungen an Channel-Modes und Channel-User-Modes:
36 * Modes werden besser geforwarded, lokale User, fuer die ein Channel
37 * angelegt wird, werden Channel-Operator, etc. pp. ...
38 * - NJOIN's von Servern werden nun korrekt an andere Server weitergeleitet.
40 * Revision 1.49 2002/02/06 16:51:22 alex
41 * - neue Funktion zur MODE-Behandlung, fuer Channel-Modes vorbereitet.
43 * Revision 1.48 2002/01/29 00:13:45 alex
44 * - WHOIS zeigt nun auch die Channels an, in denen der jeweilige User Mitglied ist.
45 * - zu jedem Server wird nun der "Top-Server" gespeichert, somit funktioniert
46 * LINKS wieder korrekt.
48 * Revision 1.47 2002/01/28 13:05:48 alex
49 * - nach einem JOIN wird die Liste der Mitglieder an den Client geschickt.
50 * - MODE fuer Channels wird nun komplett ignoriert (keine Fehlermeldung mehr).
52 * Revision 1.46 2002/01/28 01:45:43 alex
53 * - SERVER-Meldungen an neue Server sind nun in der richtigen Reihenfolge.
55 * Revision 1.45 2002/01/28 01:18:14 alex
56 * - connectierenden Servern werden Channels nun mit NJOIN bekannt gemacht.
58 * Revision 1.44 2002/01/28 00:55:08 alex
59 * - ein neu connectierender Server wird nun korrekt im Netz bekannt gemacht.
61 * Revision 1.43 2002/01/27 21:56:39 alex
62 * - IRC_WriteStrServersPrefixID() und IRC_WriteStrClientPrefixID() wieder entfernt.
63 * - einige kleinere Fixes bezueglich Channels ...
65 * Revision 1.42 2002/01/27 18:28:01 alex
66 * - bei NICK wurde das falsche Prefix an andere Server weitergegeben.
68 * Revision 1.41 2002/01/27 17:15:49 alex
69 * - anderungen an den Funktions-Prototypen von IRC_WriteStrChannel() und
70 * IRC_WriteStrChannelPrefix(),
71 * - neue: IRC_WriteStrClientPrefixID() und IRC_WriteStrServersPrefixID().
73 * Revision 1.40 2002/01/26 18:43:11 alex
74 * - neue Funktionen IRC_WriteStrChannelPrefix() und IRC_WriteStrChannel(),
75 * die IRC_Write_xxx_Related() sind dafuer entfallen.
76 * - IRC_PRIVMSG() kann nun auch mit Channels als Ziel umgehen.
78 * Revision 1.39 2002/01/21 00:05:11 alex
79 * - neue Funktionen IRC_JOIN und IRC_PART begonnen, ebenso die Funktionen
80 * IRC_WriteStrRelatedPrefix und IRC_WriteStrRelatedChannelPrefix().
81 * - diverse Aenderungen im Zusammenhang mit Channels.
83 * Revision 1.38 2002/01/18 15:31:32 alex
84 * - die User-Modes bei einem NICK von einem Server wurden falsch uebernommen.
86 * Revision 1.37 2002/01/16 22:10:18 alex
87 * - IRC_LUSERS() implementiert.
89 * Revision 1.36 2002/01/11 23:50:55 alex
90 * - LINKS implementiert, LUSERS begonnen.
92 * Revision 1.35 2002/01/09 21:30:45 alex
93 * - WHOIS wurde faelschlicherweise an User geforwarded statt vom Server beantwortet.
95 * Revision 1.34 2002/01/09 01:09:58 alex
96 * - WHOIS wird im "Strict-RFC-Mode" nicht mehr automatisch geforwarded,
97 * - andere Server werden nun ueber bisherige Server und User informiert.
99 * Revision 1.33 2002/01/07 23:42:12 alex
100 * - Es werden fuer alle Server eigene Token generiert,
101 * - QUIT von einem Server fuer einen User wird an andere Server geforwarded,
102 * - ebenso NICK-Befehle, die "fremde" User einfuehren.
104 * Revision 1.32 2002/01/07 16:02:36 alex
105 * - Loglevel von Remote-Mode-Aenderungen angepasst (nun Debug).
106 * - Im Debug-Mode werden nun auch PING's protokolliert.
108 * Revision 1.31 2002/01/07 15:39:46 alex
109 * - Server nimmt nun Server-Links an: PASS und SERVER entsprechend angepasst.
110 * - MODE und NICK melden nun die Aenderungen an andere Server.
112 * Revision 1.30 2002/01/06 15:21:29 alex
113 * - Loglevel und Meldungen nochmals ueberarbeitet.
114 * - QUIT und SQUIT forwarden nun den Grund der Trennung,
115 * - WHOIS wird nun immer an den "Original-Server" weitergeleitet.
117 * Revision 1.29 2002/01/05 23:24:54 alex
118 * - WHOIS erweitert: Anfragen koennen an andere Server weitergeleitet werden.
119 * - Vorbereitungen fuer Ident-Abfragen bei neuen Client-Strukturen.
121 * Revision 1.28 2002/01/05 20:08:02 alex
122 * - Div. Aenderungen fuer die Server-Links (u.a. WHOIS, QUIT, NICK angepasst).
123 * - Neue Funktionen IRC_WriteStrServer() und IRC_WriteStrServerPrefix().
125 * Revision 1.27 2002/01/05 19:15:03 alex
126 * - Fehlerpruefung bei select() in der "Hauptschleife" korrigiert.
128 * Revision 1.26 2002/01/05 16:51:18 alex
129 * - das Passwort von Servern wird nun ueberprueft (PASS- und SERVER-Befehl).
131 * Revision 1.25 2002/01/05 00:48:33 alex
132 * - bei SQUIT wurde immer die Verbindung getrennt, auch bei Remote-Servern.
134 * Revision 1.24 2002/01/04 17:58:44 alex
135 * - IRC_WriteStrXXX()-Funktionen eingefuehrt, groessere Anpassungen daran.
136 * - neuer Befehl SQUIT, QUIT an Server-Links angepasst.
138 * Revision 1.23 2002/01/04 01:36:40 alex
139 * - Loglevel ein wenig angepasst.
141 * Revision 1.22 2002/01/04 01:21:47 alex
142 * - Client-Strukruren werden nur noch ueber Funktionen angesprochen.
143 * - Weitere Anpassungen und Erweiterungen der Server-Links.
145 * Revision 1.21 2002/01/03 02:26:51 alex
146 * - neue Befehle SERVER und NJOIN begonnen,
147 * - begonnen, diverse IRC-Befehle an Server-Links anzupassen.
149 * Revision 1.20 2002/01/02 12:46:41 alex
150 * - die Gross- und Kleinschreibung des Nicks kann mit NICK nun geaendert werden.
152 * Revision 1.19 2002/01/02 02:51:39 alex
153 * - Copyright-Texte angepasst.
154 * - neuer Befehl "ERROR".
156 * Revision 1.17 2001/12/31 15:33:13 alex
157 * - neuer Befehl NAMES, kleinere Bugfixes.
158 * - Bug bei PING behoben: war zu restriktiv implementiert :-)
160 * Revision 1.16 2001/12/31 02:18:51 alex
161 * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
162 * - neuen Header "defines.h" mit (fast) allen Konstanten.
163 * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
165 * Revision 1.15 2001/12/30 19:26:11 alex
166 * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
168 * Revision 1.14 2001/12/30 11:42:00 alex
169 * - der Server meldet nun eine ordentliche "Start-Zeit".
171 * Revision 1.13 2001/12/29 03:10:06 alex
172 * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
173 * - neue configure-Optione "--enable-strict-rfc".
175 * Revision 1.12 2001/12/27 19:17:26 alex
176 * - neue Befehle PRIVMSG, NOTICE, PING.
178 * Revision 1.11 2001/12/27 16:55:41 alex
179 * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
181 * Revision 1.10 2001/12/26 22:48:53 alex
182 * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
184 * Revision 1.9 2001/12/26 14:45:37 alex
187 * Revision 1.8 2001/12/26 03:21:46 alex
188 * - PING/PONG-Befehle implementiert,
189 * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
191 * Revision 1.7 2001/12/25 23:25:18 alex
192 * - und nochmal Aenderungen am Logging ;-)
194 * Revision 1.6 2001/12/25 23:13:33 alex
195 * - Debug-Meldungen angepasst.
197 * Revision 1.5 2001/12/25 22:02:42 alex
198 * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
200 * Revision 1.4 2001/12/25 19:19:30 alex
201 * - bessere Fehler-Abfragen, diverse Bugfixes.
202 * - Nicks werden nur einmal vergeben :-)
203 * - /MOTD wird unterstuetzt.
205 * Revision 1.3 2001/12/24 01:34:06 alex
206 * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
207 * - MOTD-Ausgabe begonnen zu implementieren.
209 * Revision 1.2 2001/12/23 21:57:16 alex
210 * - erste IRC-Befehle zu implementieren begonnen.
212 * Revision 1.1 2001/12/14 08:13:43 alex
213 * - neues Modul begonnen :-)
234 #include "messages.h"
242 #define CONNECTED TRUE
243 #define DISCONNECTED FALSE
246 LOCAL BOOLEAN Hello_User( CLIENT *Client );
247 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
249 LOCAL VOID Kill_Nick( CHAR *Nick );
251 LOCAL BOOLEAN Send_NAMES( CLIENT *Client, CHANNEL *Chan );
254 GLOBAL VOID IRC_Init( VOID )
259 GLOBAL VOID IRC_Exit( VOID )
264 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CHAR *Format, ... )
267 BOOLEAN ok = CONNECTED;
270 assert( Client != NULL );
271 assert( Format != NULL );
273 va_start( ap, Format );
274 vsnprintf( buffer, 1000, Format, ap );
277 /* an den Client selber */
278 ok = IRC_WriteStrClientPrefix( Client, Client_ThisServer( ), buffer );
281 } /* IRC_WriteStrClient */
284 GLOBAL BOOLEAN IRC_WriteStrClientPrefix( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
286 /* Text an Clients, lokal bzw. remote, senden. */
291 assert( Client != NULL );
292 assert( Format != NULL );
293 assert( Prefix != NULL );
295 va_start( ap, Format );
296 vsnprintf( buffer, 1000, Format, ap );
299 return Conn_WriteStr( Client_Conn( Client_NextHop( Client )), ":%s %s", Client_ID( Prefix ), buffer );
300 } /* IRC_WriteStrClientPrefix */
303 GLOBAL BOOLEAN IRC_WriteStrChannel( CLIENT *Client, CHANNEL *Chan, BOOLEAN Remote, CHAR *Format, ... )
308 assert( Client != NULL );
309 assert( Format != NULL );
311 va_start( ap, Format );
312 vsnprintf( buffer, 1000, Format, ap );
315 return IRC_WriteStrChannelPrefix( Client, Chan, Client_ThisServer( ), Remote, buffer );
316 } /* IRC_WriteStrChannel */
319 GLOBAL BOOLEAN IRC_WriteStrChannelPrefix( CLIENT *Client, CHANNEL *Chan, CLIENT *Prefix, BOOLEAN Remote, CHAR *Format, ... )
322 BOOLEAN sock[MAX_CONNECTIONS], ok = CONNECTED, i;
328 assert( Client != NULL );
329 assert( Chan != NULL );
330 assert( Prefix != NULL );
331 assert( Format != NULL );
333 va_start( ap, Format );
334 vsnprintf( buffer, 1000, Format, ap );
337 for( i = 0; i < MAX_CONNECTIONS; i++ ) sock[i] = FALSE;
339 /* An alle Clients, die in den selben Channels sind.
340 * Dabei aber nur einmal je Remote-Server */
341 cl2chan = Channel_FirstMember( Chan );
344 c = Channel_GetClient( cl2chan );
347 if( Client_Conn( c ) <= NONE ) c = NULL;
348 else if( Client_Type( c ) == CLIENT_SERVER ) c = NULL;
350 if( c ) c = Client_NextHop( c );
352 if( c && ( c != Client ))
354 /* Ok, anderer Client */
355 s = Client_Conn( c );
357 assert( s < MAX_CONNECTIONS );
360 cl2chan = Channel_NextMember( Chan, cl2chan );
364 for( i = 0; i < MAX_CONNECTIONS; i++ )
368 ok = Conn_WriteStr( i, ":%s %s", Client_ID( Prefix ), buffer );
373 } /* IRC_WriteStrChannelPrefix */
376 GLOBAL VOID IRC_WriteStrServers( CLIENT *ExceptOf, CHAR *Format, ... )
381 assert( Format != NULL );
383 va_start( ap, Format );
384 vsnprintf( buffer, 1000, Format, ap );
387 /* an den Client selber */
388 return IRC_WriteStrServersPrefix( ExceptOf, Client_ThisServer( ), buffer );
389 } /* IRC_WriteStrServers */
392 GLOBAL VOID IRC_WriteStrServersPrefix( CLIENT *ExceptOf, CLIENT *Prefix, CHAR *Format, ... )
398 assert( Format != NULL );
399 assert( Prefix != NULL );
401 va_start( ap, Format );
402 vsnprintf( buffer, 1000, Format, ap );
408 if(( Client_Type( c ) == CLIENT_SERVER ) && ( Client_Conn( c ) > NONE ) && ( c != Client_ThisServer( )) && ( c != ExceptOf ))
410 /* Ziel-Server gefunden */
411 IRC_WriteStrClientPrefix( c, Prefix, buffer );
413 c = Client_Next( c );
415 } /* IRC_WriteStrServersPrefix */
418 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
420 assert( Client != NULL );
421 assert( Req != NULL );
423 /* Fehler liefern, wenn kein lokaler Client */
424 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
426 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
428 /* noch nicht registrierte unbekannte Verbindung */
429 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
431 /* Passwort speichern */
432 Client_SetPassword( Client, Req->argv[0] );
434 Client_SetType( Client, CLIENT_GOTPASS );
437 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
439 /* noch nicht registrierte Server-Verbindung */
440 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
442 /* Passwort speichern */
443 Client_SetPassword( Client, Req->argv[0] );
445 Client_SetType( Client, CLIENT_GOTPASSSERVER );
448 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
450 /* Falsche Anzahl Parameter? */
451 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
453 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
457 GLOBAL BOOLEAN IRC_SERVER( CLIENT *Client, REQUEST *Req )
459 CHAR str[LINE_LEN], *ptr;
460 CLIENT *from, *c, *cl;
466 assert( Client != NULL );
467 assert( Req != NULL );
469 /* Fehler liefern, wenn kein lokaler Client */
470 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
472 if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
474 /* Verbindung soll als Server-Server-Verbindung registriert werden */
475 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
477 /* Falsche Anzahl Parameter? */
478 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
480 /* Ist dieser Server bei uns konfiguriert? */
481 for( i = 0; i < Conf_Server_Count; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
482 if( i >= Conf_Server_Count )
484 /* Server ist nicht konfiguriert! */
485 Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
486 Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", TRUE );
489 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd ) != 0 )
491 /* Falsches Passwort */
492 Log( LOG_ERR, "Connection %d: Bad password for server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
493 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
497 /* Ist ein Server mit dieser ID bereits registriert? */
498 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
500 /* Server-Strukturen fuellen ;-) */
501 Client_SetID( Client, Req->argv[0] );
502 Client_SetHops( Client, 1 );
503 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
505 /* Meldet sich der Server bei uns an? */
508 /* Unseren SERVER- und PASS-Befehl senden */
510 if( ! IRC_WriteStrClient( Client, "PASS %s "PASSSERVERADD, Conf_Server[i].pwd )) ok = FALSE;
511 else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
514 Conn_Close( Client_Conn( Client ), "Unexpected server behavior!", NULL, FALSE );
517 Client_SetIntroducer( Client, Client );
518 Client_SetToken( Client, 1 );
520 else Client_SetToken( Client, atoi( Req->argv[1] ));
522 Log( LOG_NOTICE, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), Client_Conn( Client ));
524 Client_SetType( Client, CLIENT_SERVER );
526 /* maximalen Hop Count ermitteln */
531 if( Client_Hops( c ) > max_hops ) max_hops = Client_Hops( c );
532 c = Client_Next( c );
535 /* Alle bisherigen Server dem neuen Server bekannt machen,
536 * die bisherigen Server ueber den neuen informierenn */
537 for( i = 0; i < ( max_hops + 1 ); i++ )
542 if(( Client_Type( c ) == CLIENT_SERVER ) && ( c != Client ) && ( c != Client_ThisServer( )) && ( Client_Hops( c ) == i ))
544 if( Client_Conn( c ) > NONE )
546 /* Dem gefundenen Server gleich den neuen
547 * Server bekannt machen */
548 if( ! IRC_WriteStrClient( c, "SERVER %s %d %d :%s", Client_ID( Client ), Client_Hops( Client ) + 1, Client_MyToken( Client ), Client_Info( Client ))) return DISCONNECTED;
551 /* Den neuen Server ueber den alten informieren */
552 if( ! IRC_WriteStrClientPrefix( Client, Client_Hops( c ) == 1 ? Client_ThisServer( ) : Client_Introducer( c ), "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ))) return DISCONNECTED;
554 c = Client_Next( c );
558 /* alle User dem neuen Server bekannt machen */
562 if( Client_Type( c ) == CLIENT_USER )
564 /* User an neuen Server melden */
565 if( ! IRC_WriteStrClient( Client, "NICK %s %d %s %s %d +%s :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_User( c ), Client_Hostname( c ), Client_MyToken( Client_Introducer( c )), Client_Modes( c ), Client_Info( c ))) return DISCONNECTED;
567 c = Client_Next( c );
570 /* Channels dem neuen Server bekannt machen */
571 chan = Channel_First( );
574 sprintf( str, "NJOIN %s :", Channel_Name( chan ));
576 /* alle Member suchen */
577 cl2chan = Channel_FirstMember( chan );
580 cl = Channel_GetClient( cl2chan );
581 assert( cl != NULL );
583 /* Nick, ggf. mit Modes, anhaengen */
584 if( str[strlen( str ) - 1] != ':' ) strcat( str, "," );
585 if( strchr( Channel_UserModes( chan, cl ), 'v' )) strcat( str, "+" );
586 if( strchr( Channel_UserModes( chan, cl ), 'o' )) strcat( str, "@" );
587 strcat( str, Client_ID( cl ));
589 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
592 if( ! IRC_WriteStrClient( Client, str )) return DISCONNECTED;
593 sprintf( str, "NJOIN %s :", Channel_Name( chan ));
596 cl2chan = Channel_NextMember( chan, cl2chan );
600 if( str[strlen( str ) - 1] != ':')
602 /* Ja; Also senden ... */
603 if( ! IRC_WriteStrClient( Client, str )) return DISCONNECTED;
606 /* naechsten Channel suchen */
607 chan = Channel_Next( chan );
612 else if( Client_Type( Client ) == CLIENT_SERVER )
614 /* Neuer Server wird im Netz angekuendigt */
616 /* Falsche Anzahl Parameter? */
617 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
619 /* Ist ein Server mit dieser ID bereits registriert? */
620 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
622 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
623 ptr = strchr( Req->argv[3] + 2, '[' );
624 if( ! ptr ) ptr = Req->argv[3];
626 from = Client_GetFromID( Req->prefix );
629 /* Hm, Server, der diesen einfuehrt, ist nicht bekannt!? */
630 Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
631 Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", TRUE );
635 /* Neue Client-Struktur anlegen */
636 c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, TRUE );
639 /* Neue Client-Struktur konnte nicht angelegt werden */
640 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
641 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", TRUE );
645 /* Log-Meldung zusammenbauen und ausgeben */
646 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) sprintf( str, "connected to %s, ", Client_ID( from ));
647 else strcpy( str, "" );
648 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": "" );
650 /* Andere Server informieren */
651 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
655 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
659 GLOBAL BOOLEAN IRC_NJOIN( CLIENT *Client, REQUEST *Req )
661 CHAR *channame, *ptr, modes[8];
662 BOOLEAN is_op, is_voiced;
666 assert( Client != NULL );
667 assert( Req != NULL );
669 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTEREDSERVER_MSG, Client_ID( Client ));
671 /* Falsche Anzahl Parameter? */
672 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
674 channame = Req->argv[0];
675 ptr = strtok( Req->argv[1], "," );
678 is_op = is_voiced = FALSE;
680 /* Prefixe abschneiden */
681 while(( *ptr == '@' ) || ( *ptr == '+' ))
683 if( *ptr == '@' ) is_op = TRUE;
684 if( *ptr == '+' ) is_voiced = TRUE;
688 c = Client_GetFromID( ptr );
691 Channel_Join( c, channame );
692 chan = Channel_Search( channame );
693 assert( chan != NULL );
695 if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
696 if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
698 /* im Channel bekannt machen */
699 IRC_WriteStrChannelPrefix( Client, chan, c, FALSE, "JOIN :%s", channame );
701 /* Channel-User-Modes setzen */
702 strcpy( modes, Channel_UserModes( chan, c ));
705 /* Modes im Channel bekannt machen */
706 IRC_WriteStrChannelPrefix( Client, chan, Client, FALSE, "MODE %s +%s %s", channame, modes, Client_ID( c ));
709 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
711 /* naechsten Nick suchen */
712 ptr = strtok( NULL, "," );
715 /* an andere Server weiterleiten */
716 IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], Req->argv[1] );
722 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
724 CLIENT *intr_c, *target, *c;
727 assert( Client != NULL );
728 assert( Req != NULL );
730 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
732 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 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
734 if( Client_Type( Client ) == CLIENT_UNKNOWN || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_USER || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
737 /* User-Registrierung bzw. Nick-Aenderung */
739 /* Falsche Anzahl Parameter? */
740 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
742 /* "Ziel-Client" ermitteln */
743 if( Client_Type( Client ) == CLIENT_SERVER )
745 target = Client_GetFromID( Req->prefix );
746 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
750 /* Ist der Client "restricted"? */
751 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
756 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
757 * wir nichts. So macht es das Original und mind. Snak hat probleme,
758 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
759 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
762 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
763 * will nur die Gross- und Kleinschreibung aendern. Das darf
764 * er natuerlich machen :-) */
765 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
767 if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
770 if( Client_Type( Client ) == CLIENT_USER )
772 /* Nick-Aenderung: allen mitteilen! */
773 Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
774 IRC_WriteStrClient( Client, "NICK :%s", Req->argv[0] );
775 IRC_WriteStrServersPrefix( NULL, Client, "NICK :%s", Req->argv[0] );
777 else if( Client_Type( Client ) == CLIENT_SERVER )
779 /* Nick-Aenderung: allen mitteilen! */
780 Log( LOG_DEBUG, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
781 IRC_WriteStrServersPrefix( Client, target, "NICK :%s", Req->argv[0] );
784 /* Client-Nick registrieren */
785 Client_SetID( target, Req->argv[0] );
787 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
790 Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client_Conn( Client ));
791 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
792 else Client_SetType( Client, CLIENT_GOTNICK );
796 else if( Client_Type( Client ) == CLIENT_SERVER )
798 /* Server fuehrt neuen Client ein */
800 /* Falsche Anzahl Parameter? */
801 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
803 /* Nick ueberpruefen */
804 c = Client_GetFromID( Req->argv[0] );
807 /* Der neue Nick ist auf diesem Server bereits registriert:
808 * sowohl der neue, als auch der alte Client muessen nun
809 * disconnectiert werden. */
810 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
811 Kill_Nick( Req->argv[0] );
815 /* Server, zu dem der Client connectiert ist, suchen */
816 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
819 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
820 Kill_Nick( Req->argv[0] );
824 /* Neue Client-Struktur anlegen */
825 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] + 1, Req->argv[6], TRUE );
828 /* Eine neue Client-Struktur konnte nicht angelegt werden.
829 * Der Client muss disconnectiert werden, damit der Netz-
830 * status konsistent bleibt. */
831 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
832 Kill_Nick( Req->argv[0] );
836 modes = Client_Modes( c );
837 if( *modes ) Log( LOG_DEBUG, "User \"%s\" (+%s) registered (via %s, on %s, %d hop%s).", Client_Mask( c ), modes, Client_ID( Client ), Client_ID( intr_c ), Client_Hops( c ), Client_Hops( c ) > 1 ? "s": "" );
838 else Log( LOG_DEBUG, "User \"%s\" registered (via %s, on %s, %d hop%s).", Client_Mask( c ), Client_ID( Client ), Client_ID( intr_c ), Client_Hops( c ), Client_Hops( c ) > 1 ? "s": "" );
840 /* Andere Server, ausser dem Introducer, informieren */
841 IRC_WriteStrServersPrefix( Client, Client, "NICK %s %d %s %s %d %s :%s", Req->argv[0], atoi( Req->argv[1] ) + 1, Req->argv[2], Req->argv[3], Client_MyToken( intr_c ), Req->argv[5], Req->argv[6] );
845 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
849 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
851 assert( Client != NULL );
852 assert( Req != NULL );
855 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
857 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
860 /* Falsche Anzahl Parameter? */
861 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
863 Client_SetUser( Client, Req->argv[0], FALSE );
864 Client_SetInfo( Client, Req->argv[3] );
866 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client_Conn( Client ));
867 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
868 else Client_SetType( Client, CLIENT_GOTUSER );
871 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
873 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
875 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
879 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
883 assert( Client != NULL );
884 assert( Req != NULL );
886 if(( Client_Type( Client ) == CLIENT_USER ) || ( Client_Type( Client ) == CLIENT_SERVICE ))
890 /* Falsche Anzahl Parameter? */
891 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
893 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
894 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
898 else if ( Client_Type( Client ) == CLIENT_SERVER )
902 /* Falsche Anzahl Parameter? */
903 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
905 target = Client_Search( Req->prefix );
908 Log( LOG_ERR, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
912 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL );
913 else Client_Destroy( target, "Got QUIT command.", Req->argv[0] );
917 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
921 GLOBAL BOOLEAN IRC_SQUIT( CLIENT *Client, REQUEST *Req )
925 assert( Client != NULL );
926 assert( Req != NULL );
928 /* SQUIT ist nur fuer Server erlaubt */
929 if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
931 /* Falsche Anzahl Parameter? */
932 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
934 target = Client_GetFromID( Req->argv[0] );
937 Log( LOG_ERR, "Got SQUIT from %s for unknown server \%s\"!?", Client_ID( Client ), Req->argv[0] );
941 if( target == Client ) Log( LOG_DEBUG, "Got SQUIT from %s: %s", Client_ID( Client ), Req->argv[1] );
942 else Log( LOG_DEBUG, "Got SQUIT from %s for %s: %s", Client_ID( Client ), Client_ID( target ), Req->argv[1] );
944 /* SQUIT an alle Server weiterleiten */
945 IRC_WriteStrServers( Client, "SQUIT %s :%s", Req->argv[0], Req->argv[1] );
947 if( Client_Conn( target ) > NONE )
949 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), "Got SQUIT command.", Req->argv[1], TRUE );
950 else Conn_Close( Client_Conn( target ), "Got SQUIT command.", NULL, TRUE );
955 Client_Destroy( target, "Got SQUIT command.", Req->argv[1] );
961 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
963 CLIENT *target, *from;
965 assert( Client != NULL );
966 assert( Req != NULL );
968 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
970 /* Falsche Anzahl Parameter? */
971 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
972 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
976 /* es wurde ein Ziel-Client angegeben */
977 target = Client_GetFromID( Req->argv[1] );
978 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
979 if( target != Client_ThisServer( ))
982 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
984 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
985 return IRC_WriteStrClientPrefix( Client_NextHop( target ), from, "PING %s :%s", Client_ID( from ), Req->argv[1] );
989 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
990 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
994 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
996 CLIENT *target, *from;
998 assert( Client != NULL );
999 assert( Req != NULL );
1001 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1003 /* Falsche Anzahl Parameter? */
1004 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
1005 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1008 if( Req->argc == 2 )
1010 target = Client_GetFromID( Req->argv[1] );
1011 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
1012 if( target != Client_ThisServer( ))
1015 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1017 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
1018 return IRC_WriteStrClientPrefix( Client_NextHop( target ), from, "PONG %s :%s", Client_ID( from ), Req->argv[1] );
1022 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
1023 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
1025 if( Client_Conn( Client ) > NONE ) Log( LOG_DEBUG, "Connection %d: received PONG. Lag: %d seconds.", Client_Conn( Client ), time( NULL ) - Conn_LastPing( Client_Conn( Client )));
1026 else Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
1032 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
1034 assert( Client != NULL );
1035 assert( Req != NULL );
1037 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1039 /* Falsche Anzahl Parameter? */
1040 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1042 return Show_MOTD( Client );
1046 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
1051 assert( Client != NULL );
1052 assert( Req != NULL );
1054 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1056 /* Falsche Anzahl Parameter? */
1057 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
1058 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
1059 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1061 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1063 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1065 to = Client_Search( Req->argv[0] );
1068 /* Okay, Ziel ist ein User */
1069 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
1070 return IRC_WriteStrClientPrefix( to, from, "PRIVMSG %s :%s", Client_ID( to ), Req->argv[1] );
1073 chan = Channel_Search( Req->argv[0] );
1076 /* Okay, Ziel ist ein Channel */
1077 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
1078 return IRC_WriteStrChannelPrefix( Client, chan, from, TRUE, "PRIVMSG %s :%s", Req->argv[0], Req->argv[1] );
1081 return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
1085 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
1089 assert( Client != NULL );
1090 assert( Req != NULL );
1092 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1094 /* Falsche Anzahl Parameter? */
1095 if( Req->argc != 2 ) return CONNECTED;
1097 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1099 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1101 to = Client_Search( Req->argv[0] );
1104 /* Okay, Ziel ist ein User */
1105 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
1107 else return CONNECTED;
1111 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
1113 CHAR *mode_ptr, the_modes[CLIENT_MODE_LEN], x[2];
1114 CLIENT *cl, *chan_cl, *prefix;
1118 assert( Client != NULL );
1119 assert( Req != NULL );
1121 cl = chan_cl = prefix = NULL;
1124 /* Valider Client? */
1125 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1127 /* Keine Parameter? */
1128 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1130 /* Ziel suchen: Client bzw. Channel */
1131 if( Client_IsValidNick( Req->argv[0] )) cl = Client_Search( Req->argv[0] );
1132 if( Channel_IsValidName( Req->argv[0] )) chan = Channel_Search( Req->argv[0] );
1134 /* Kein Ziel gefunden? */
1135 if(( ! cl ) && ( ! chan )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
1137 assert(( cl && chan ) != TRUE );
1139 /* Falsche Anzahl Parameter? */
1140 if(( cl ) && ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1141 if(( chan ) && ( Req->argc > 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1143 /* Client ermitteln, wenn bei Channel-Modes mit 3 Parametern */
1144 if(( chan ) && (Req->argc == 3 ))
1146 chan_cl = Client_Search( Req->argv[2] );
1147 if( ! chan_cl ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
1150 /* Wenn Anfragender ein User ist: Zugriff erlaubt? */
1151 if( Client_Type( Client ) == CLIENT_USER )
1155 /* MODE ist nur fuer sich selber zulaessig! */
1156 if( cl != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
1160 /* Darf der User die Channel-Modes ermitteln? */
1164 /* Werden die Modes "nur" erfragt? */
1165 if(( cl ) && ( Req->argc == 1 )) return IRC_WriteStrClient( Client, RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( cl ));
1166 if(( chan ) && ( Req->argc == 1 )) return IRC_WriteStrClient( Client, RPL_CHANNELMODEIS_MSG, Client_ID( Client ), Channel_Name( chan ), Channel_Modes( chan ));
1168 mode_ptr = Req->argv[1];
1170 /* Sollen Modes gesetzt oder geloescht werden? */
1173 if( *mode_ptr == '+' ) set = TRUE;
1174 else if( *mode_ptr == '-' ) set = FALSE;
1175 else return IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
1180 if( *mode_ptr == '-' ) set = FALSE;
1182 if(( *mode_ptr == '-' ) || ( *mode_ptr == '+' )) mode_ptr++;
1185 /* Prefix fuer Antworten etc. ermitteln */
1186 if( Client_Type( Client ) == CLIENT_SERVER )
1188 prefix = Client_GetFromID( Req->prefix );
1189 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1191 else prefix = Client;
1193 /* Reply-String mit Aenderungen vorbereiten */
1194 if( set ) strcpy( the_modes, "+" );
1195 else strcpy( the_modes, "-" );
1202 if( Client_Type( Client ) == CLIENT_SERVER )
1204 /* Befehl kommt von einem Server, daher
1205 * trauen wir ihm "unbesehen" ... */
1210 /* Modes validieren */
1221 /* restricted (kann nur gesetzt werden) */
1222 if( set ) x[0] = 'r';
1223 else ok = IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
1226 /* operator (kann nur geloescht werden) */
1229 Client_SetOperByMe( Client, FALSE );
1232 else ok = IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
1235 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Client ));
1236 ok = IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
1242 /* Ist der User ein Channel Operator? */
1243 if( ! strchr( Channel_UserModes( chan, Client ), 'o' ))
1245 Log( LOG_DEBUG, "Can't change modes: \"%s\" is not operator on %s!", Client_ID( Client ), Channel_Name( chan ));
1246 ok = IRC_WriteStrClient( Client, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Client ), Channel_Name( chan ));
1250 /* Channel-Modes oder Channel-User-Modes */
1253 /* Channel-User-Modes */
1257 Log( LOG_DEBUG, "Unknown channel-user-mode \"%c%c\" from \"%s\" on \"%s\" at %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Client ), Client_ID( chan_cl ), Channel_Name( chan ));
1258 ok = IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
1268 Log( LOG_DEBUG, "Unknown channel-mode \"%c%c\" from \"%s\" at %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Client ), Channel_Name( chan ));
1269 ok = IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
1278 if( ! x[0] ) continue;
1280 /* Okay, gueltigen Mode gefunden */
1283 /* Es geht um User-Modes */
1286 /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
1287 if( Client_ModeAdd( cl, x[0] )) strcat( the_modes, x );
1291 /* Modes geloescht. Wenn der Client ihn hatte: merken */
1292 if( Client_ModeDel( cl, x[0] )) strcat( the_modes, x );
1297 /* Es geht um Channel-Modes oder Channel-User-Modes */
1300 /* Channel-User-Modes */
1303 /* Mode setzen. Wenn der Channel ihn noch nicht hatte: merken */
1304 if( Channel_UserModeAdd( chan, chan_cl, x[0] )) strcat( the_modes, x );
1308 /* Mode setzen. Wenn der Channel ihn noch nicht hatte: merken */
1309 if( Channel_UserModeDel( chan, chan_cl, x[0] )) strcat( the_modes, x );
1317 /* Mode setzen. Wenn der Channel ihn noch nicht hatte: merken */
1318 if( Channel_ModeAdd( chan, x[0] )) strcat( the_modes, x );
1322 /* Mode setzen. Wenn der Channel ihn noch nicht hatte: merken */
1323 if( Channel_ModeDel( chan, x[0] )) strcat( the_modes, x );
1329 /* Wurden Modes geaendert? */
1335 if( Client_Type( Client ) == CLIENT_SERVER )
1337 /* Modes an andere Server forwarden */
1338 IRC_WriteStrServersPrefix( Client, prefix, "MODE %s :%s", Client_ID( cl ), the_modes );
1342 /* Bestaetigung an Client schicken & andere Server informieren */
1343 ok = IRC_WriteStrClientPrefix( Client, prefix, "MODE %s %s", Client_ID( cl ), the_modes );
1344 IRC_WriteStrServersPrefix( Client, prefix, "MODE %s :%s", Client_ID( cl ), the_modes );
1346 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( cl ), Client_Modes( cl ));
1350 /* Channel-Modes oder Channel-User-Mode */
1353 /* Channel-User-Mode */
1354 if( Client_Type( Client ) == CLIENT_SERVER )
1356 /* Modes an andere Server und Channel-User forwarden */
1357 IRC_WriteStrServersPrefix( Client, prefix, "MODE %s %s :%s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
1358 IRC_WriteStrChannelPrefix( Client, chan, prefix, FALSE, "MODE %s %s %s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
1362 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
1363 ok = IRC_WriteStrClientPrefix( Client, prefix, "MODE %s %s %s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
1364 IRC_WriteStrServersPrefix( Client, prefix, "MODE %s %s :%s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
1365 IRC_WriteStrChannelPrefix( Client, chan, prefix, FALSE, "MODE %s %s %s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
1367 Log( LOG_DEBUG, "User \"%s\" on %s: Mode change, now \"%s\".", Client_Mask( chan_cl), Channel_Name( chan ), Channel_UserModes( chan, chan_cl ));
1372 if( Client_Type( Client ) == CLIENT_SERVER )
1374 /* Modes an andere Server und Channel-User forwarden */
1375 IRC_WriteStrServersPrefix( Client, prefix, "MODE %s :%s", Channel_Name( chan ), the_modes );
1376 IRC_WriteStrChannelPrefix( Client, chan, prefix, FALSE, "MODE %s %s", Channel_Name( chan ), the_modes );
1380 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
1381 ok = IRC_WriteStrClientPrefix( Client, prefix, "MODE %s %s", Channel_Name( chan ), the_modes );
1382 IRC_WriteStrServersPrefix( Client, prefix, "MODE %s :%s", Channel_Name( chan ), the_modes );
1383 IRC_WriteStrChannelPrefix( Client, chan, prefix, FALSE, "MODE %s %s", Channel_Name( chan ), the_modes );
1385 Log( LOG_DEBUG, "Channel \"%s\": Mode change, now \"%s\".", Channel_Name( chan ), Channel_Modes( chan ));
1394 GLOBAL BOOLEAN IRC_OPER( CLIENT *Client, REQUEST *Req )
1398 assert( Client != NULL );
1399 assert( Req != NULL );
1401 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1403 /* Falsche Anzahl Parameter? */
1404 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1406 /* Operator suchen */
1407 for( i = 0; i < Conf_Oper_Count; i++)
1409 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
1411 if( i >= Conf_Oper_Count )
1413 Log( LOG_WARNING, "Got invalid OPER from \"%s\": Name \"%s\" not configured!", Client_Mask( Client ), Req->argv[0] );
1414 return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
1417 /* Stimmt das Passwort? */
1418 if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
1420 Log( LOG_WARNING, "Got invalid OPER from \"%s\": Bad password for \"%s\"!", Client_Mask( Client ), Conf_Oper[i].name );
1421 return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
1424 if( ! Client_HasMode( Client, 'o' ))
1426 /* noch kein o-Mode gesetzt */
1427 Client_ModeAdd( Client, 'o' );
1428 if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
1429 IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
1432 if( ! Client_OperByMe( Client )) Log( LOG_NOTICE, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
1434 Client_SetOperByMe( Client, TRUE );
1435 return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
1439 GLOBAL BOOLEAN IRC_DIE( CLIENT *Client, REQUEST *Req )
1441 assert( Client != NULL );
1442 assert( Req != NULL );
1444 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1446 /* Falsche Anzahl Parameter? */
1447 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1449 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
1451 Log( LOG_NOTICE, "Got DIE command from \"%s\", going down!", Client_Mask( Client ));
1457 GLOBAL BOOLEAN IRC_RESTART( CLIENT *Client, REQUEST *Req )
1459 assert( Client != NULL );
1460 assert( Req != NULL );
1462 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1464 /* Falsche Anzahl Parameter? */
1465 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1467 if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
1469 Log( LOG_NOTICE, "Got RESTART command from \"%s\", going down!", Client_Mask( Client ));
1470 NGIRCd_Restart = TRUE;
1475 GLOBAL BOOLEAN IRC_NAMES( CLIENT *Client, REQUEST *Req )
1477 CHAR rpl[COMMAND_LEN];
1480 assert( Client != NULL );
1481 assert( Req != NULL );
1483 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1485 /* Falsche Anzahl Parameter? */
1486 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1488 /* Noch alle User ausgeben, die in keinem Channel sind */
1490 c = Client_First( );
1493 if( Client_Type( c ) == CLIENT_USER )
1495 /* Okay, das ist ein User */
1496 strcat( rpl, Client_ID( c ));
1500 /* Antwort zu lang? Splitten. */
1501 if( strlen( rpl ) > 480 )
1503 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1504 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
1508 c = Client_Next( c );
1512 /* es wurden User gefunden */
1513 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1514 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
1516 return IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), "*" );
1520 GLOBAL BOOLEAN IRC_ISON( CLIENT *Client, REQUEST *Req )
1522 CHAR rpl[COMMAND_LEN];
1527 assert( Client != NULL );
1528 assert( Req != NULL );
1530 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1532 /* Falsche Anzahl Parameter? */
1533 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1535 strcpy( rpl, RPL_ISON_MSG );
1536 for( i = 0; i < Req->argc; i++ )
1538 ptr = strtok( Req->argv[i], " " );
1542 c = Client_GetFromID( ptr );
1543 if( c && ( Client_Type( c ) == CLIENT_USER ))
1545 /* Dieser Nick ist "online" */
1549 ptr = strtok( NULL, " " );
1552 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1554 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1558 GLOBAL BOOLEAN IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1560 CLIENT *from, *target, *c;
1561 CHAR str[LINE_LEN + 1], *ptr = NULL;
1565 assert( Client != NULL );
1566 assert( Req != NULL );
1568 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1570 /* Falsche Anzahl Parameter? */
1571 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1574 c = Client_GetFromID( Req->argv[Req->argc - 1] );
1575 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
1577 /* Empfaenger des WHOIS suchen */
1578 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1580 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1582 /* Forwarden an anderen Server? */
1585 /* angegebenen Ziel-Server suchen */
1586 target = Client_GetFromID( Req->argv[1] );
1587 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
1591 else if( Client_Conn( c ) == NONE )
1593 /* Client ist nicht von uns. Ziel-Server suchen */
1600 if( target && ( Client_NextHop( target ) != Client_ThisServer( )) && ( Client_Type( Client_NextHop( target )) == CLIENT_SERVER )) return IRC_WriteStrClientPrefix( target, from, "WHOIS %s :%s", Req->argv[0], ptr );
1602 /* Nick, User und Name */
1603 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
1606 if( ! IRC_WriteStrClient( from, RPL_WHOISSERVER_MSG, Client_ID( from ), Client_ID( c ), Client_ID( Client_Introducer( c )), Client_Info( Client_Introducer( c )))) return DISCONNECTED;
1609 sprintf( str, RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
1610 cl2chan = Channel_FirstChannelOf( c );
1613 chan = Channel_GetChannel( cl2chan );
1614 assert( chan != NULL );
1616 /* Channel-Name anhaengen */
1617 if( str[strlen( str ) - 1] != ':' ) strcat( str, " " );
1618 if( strchr( Channel_UserModes( chan, c ), 'v' )) strcat( str, "+" );
1619 if( strchr( Channel_UserModes( chan, c ), 'o' )) strcat( str, "@" );
1620 strcat( str, Channel_Name( chan ));
1622 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
1624 /* Zeile wird zu lang: senden! */
1625 if( ! IRC_WriteStrClient( Client, str )) return DISCONNECTED;
1626 sprintf( str, RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
1629 /* naechstes Mitglied suchen */
1630 cl2chan = Channel_NextChannelOf( c, cl2chan );
1632 if( str[strlen( str ) - 1] != ':')
1634 /* Es sind noch Daten da, die gesendet werden muessen */
1635 if( ! IRC_WriteStrClient( Client, str )) return DISCONNECTED;
1639 if( Client_HasMode( c, 'o' ))
1641 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
1644 /* Idle (nur lokale Clients) */
1645 if( Client_Conn( c ) > NONE )
1647 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
1651 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1655 GLOBAL BOOLEAN IRC_USERHOST( CLIENT *Client, REQUEST *Req )
1657 CHAR rpl[COMMAND_LEN];
1661 assert( Client != NULL );
1662 assert( Req != NULL );
1664 if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1666 /* Falsche Anzahl Parameter? */
1667 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1669 if( Req->argc > 5 ) max = 5;
1670 else max = Req->argc;
1672 strcpy( rpl, RPL_USERHOST_MSG );
1673 for( i = 0; i < max; i++ )
1675 c = Client_GetFromID( Req->argv[i] );
1676 if( c && ( Client_Type( c ) == CLIENT_USER ))
1678 /* Dieser Nick ist "online" */
1679 strcat( rpl, Client_ID( c ));
1680 if( Client_HasMode( c, 'o' )) strcat( rpl, "*" );
1682 if( Client_HasMode( c, 'a' )) strcat( rpl, "-" );
1683 else strcat( rpl, "+" );
1684 strcat( rpl, Client_User( c ));
1686 strcat( rpl, Client_Hostname( c ));
1690 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1692 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1693 } /* IRC_USERHOST */
1696 GLOBAL BOOLEAN IRC_ERROR( CLIENT *Client, REQUEST *Req )
1698 assert( Client != NULL );
1699 assert( Req != NULL );
1701 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
1702 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
1708 GLOBAL BOOLEAN IRC_LUSERS( CLIENT *Client, REQUEST *Req )
1710 CLIENT *target, *from;
1713 assert( Client != NULL );
1714 assert( Req != NULL );
1716 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1718 /* Falsche Anzahl Parameter? */
1719 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1721 /* Absender ermitteln */
1722 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1724 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1726 /* An anderen Server forwarden? */
1727 if( Req->argc == 2 )
1729 target = Client_GetFromID( Req->argv[1] );
1730 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
1731 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
1734 /* Wer ist der Absender? */
1735 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1736 else target = Client;
1737 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1739 /* Users, Services und Serevr im Netz */
1740 if( ! IRC_WriteStrClient( target, RPL_LUSERCLIENT_MSG, Client_ID( target ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1742 /* IRC-Operatoren im Netz */
1743 cnt = Client_OperCount( );
1746 if( ! IRC_WriteStrClient( target, RPL_LUSEROP_MSG, Client_ID( target ), cnt )) return DISCONNECTED;
1749 /* Unbekannt Verbindungen */
1750 cnt = Client_UnknownCount( );
1753 if( ! IRC_WriteStrClient( target, RPL_LUSERUNKNOWN_MSG, Client_ID( target ), cnt )) return DISCONNECTED;
1756 /* Channels im Netz */
1757 if( ! IRC_WriteStrClient( target, RPL_LUSERCHANNELS_MSG, Client_ID( target ), Channel_Count( ))) return DISCONNECTED;
1759 /* Channels im Netz */
1760 if( ! IRC_WriteStrClient( target, RPL_LUSERME_MSG, Client_ID( target ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1766 GLOBAL BOOLEAN IRC_LINKS( CLIENT *Client, REQUEST *Req )
1768 CLIENT *target, *from, *c;
1771 assert( Client != NULL );
1772 assert( Req != NULL );
1774 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1776 /* Falsche Anzahl Parameter? */
1777 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1779 /* Server-Mask ermitteln */
1780 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
1783 /* Absender ermitteln */
1784 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1786 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1788 /* An anderen Server forwarden? */
1789 if( Req->argc == 2 )
1791 target = Client_GetFromID( Req->argv[0] );
1792 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
1793 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
1796 /* Wer ist der Absender? */
1797 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1798 else target = Client;
1799 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1801 c = Client_First( );
1804 if( Client_Type( c ) == CLIENT_SERVER )
1806 if( ! IRC_WriteStrClient( target, RPL_LINKS_MSG, Client_ID( target ), Client_ID( c ), Client_ID( Client_TopServer( c ) ? Client_TopServer( c ) : Client_ThisServer( )), Client_Hops( c ), Client_Info( c ))) return DISCONNECTED;
1808 c = Client_Next( c );
1811 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
1815 GLOBAL BOOLEAN IRC_JOIN( CLIENT *Client, REQUEST *Req )
1817 CHAR *channame, *flags, modes[8];
1818 BOOLEAN is_new_chan;
1822 assert( Client != NULL );
1823 assert( Req != NULL );
1825 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1827 /* Falsche Anzahl Parameter? */
1828 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1830 /* Wer ist der Absender? */
1831 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1832 else target = Client;
1833 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1835 /* Channel-Namen durchgehen */
1836 channame = strtok( Req->argv[0], "," );
1839 /* wird der Channel neu angelegt? */
1842 if( Channel_Search( channame )) is_new_chan = FALSE;
1843 else is_new_chan = TRUE;
1845 /* Hat ein Server Channel-User-Modes uebergeben? */
1846 if( Client_Type( Client ) == CLIENT_SERVER )
1848 /* Channel-Flags extrahieren */
1849 flags = strchr( channame, 0x7 );
1850 if( flags ) *flags++ = '\0';
1853 /* neuer Channel udn lokaler Client? */
1854 if( is_new_chan && ( Client_Type( Client ) == CLIENT_USER ))
1856 /* Dann soll der Client Channel-Operator werden! */
1860 /* Channel joinen (und ggf. anlegen) */
1861 if( ! Channel_Join( target, channame ))
1863 /* naechsten Namen ermitteln */
1864 channame = strtok( NULL, "," );
1867 chan = Channel_Search( channame );
1868 assert( chan != NULL );
1870 /* Modes setzen (wenn vorhanden) */
1871 while( flags && *flags )
1873 Channel_UserModeAdd( chan, target, *flags );
1877 /* Muessen Modes an andere Server gemeldet werden? */
1878 strcpy( &modes[1], Channel_UserModes( chan, target ));
1879 if( modes[1] ) modes[0] = 0x7;
1880 else modes[0] = '\0';
1882 /* An andere Server weiterleiten */
1883 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
1885 /* im Channel bekannt machen */
1886 IRC_WriteStrChannelPrefix( Client, chan, target, FALSE, "JOIN :%s", channame );
1889 /* Modes im Channel bekannt machen */
1890 IRC_WriteStrChannelPrefix( Client, chan, target, FALSE, "MODE %s %s :%s", channame, modes, Client_ID( target ));
1893 if( Client_Type( Client ) == CLIENT_USER )
1895 /* an Client bestaetigen */
1896 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
1897 /* Topic an Client schicken */
1898 IRC_WriteStrClient( Client, RPL_TOPIC_MSG, Client_ID( Client ), channame, "What a wonderful channel!" );
1899 /* Mitglieder an Client Melden */
1900 Send_NAMES( Client, chan );
1903 /* naechsten Namen ermitteln */
1904 channame = strtok( NULL, "," );
1910 GLOBAL BOOLEAN IRC_PART( CLIENT *Client, REQUEST *Req )
1915 assert( Client != NULL );
1916 assert( Req != NULL );
1918 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1920 /* Falsche Anzahl Parameter? */
1921 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1923 /* Wer ist der Absender? */
1924 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_GetFromID( Req->prefix );
1925 else target = Client;
1926 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1928 /* Channel-Namen durchgehen */
1929 chan = strtok( Req->argv[0], "," );
1932 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
1934 /* naechsten Namen ermitteln */
1935 chan = strtok( NULL, "," );
1939 /* naechsten Namen ermitteln */
1940 chan = strtok( NULL, "," );
1946 LOCAL BOOLEAN Hello_User( CLIENT *Client )
1948 assert( Client != NULL );
1950 /* Passwort ueberpruefen */
1951 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
1953 /* Falsches Passwort */
1954 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
1955 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
1956 return DISCONNECTED;
1959 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
1961 /* Andere Server informieren */
1962 IRC_WriteStrServers( NULL, "NICK %s 1 %s %s 1 +%s :%s", Client_ID( Client ), Client_User( Client ), Client_Hostname( Client ), Client_Modes( Client ), Client_Info( Client ));
1964 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
1965 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
1966 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
1967 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
1969 Client_SetType( Client, CLIENT_USER );
1971 return Show_MOTD( Client );
1975 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
1981 assert( Client != NULL );
1983 fd = fopen( Conf_MotdFile, "r" );
1986 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1987 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1990 IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1993 if( ! fgets( line, 126, fd )) break;
1994 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
1995 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
2001 ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
2009 LOCAL VOID Kill_Nick( CHAR *Nick )
2011 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected!", Nick );
2013 Log( LOG_ALERT, "[Kill_Nick() not implemented - OOOPS!]" );
2017 LOCAL BOOLEAN Send_NAMES( CLIENT *Client, CHANNEL *Chan )
2019 CHAR str[LINE_LEN + 1];
2023 assert( Client != NULL );
2024 assert( Chan != NULL );
2026 /* Alle Mitglieder suchen */
2027 sprintf( str, RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
2028 cl2chan = Channel_FirstMember( Chan );
2031 cl = Channel_GetClient( cl2chan );
2033 /* Nick anhaengen */
2034 if( str[strlen( str ) - 1] != ':' ) strcat( str, " " );
2035 if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strcat( str, "+" );
2036 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strcat( str, "@" );
2037 strcat( str, Client_ID( cl ));
2039 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
2041 /* Zeile wird zu lang: senden! */
2042 if( ! IRC_WriteStrClient( Client, str )) return DISCONNECTED;
2043 sprintf( str, RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
2046 /* naechstes Mitglied suchen */
2047 cl2chan = Channel_NextMember( Chan, cl2chan );
2049 if( str[strlen( str ) - 1] != ':')
2051 /* Es sind noch Daten da, die gesendet werden muessen */
2052 if( ! IRC_WriteStrClient( Client, str )) return DISCONNECTED;
2056 IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( Chan ));