+ channame = Req->argv[0];
+ ptr = strtok( Req->argv[1], "," );
+ while( ptr )
+ {
+ is_op = is_voiced = FALSE;
+
+ /* Prefixe abschneiden */
+ while(( *ptr == '@' ) || ( *ptr == '+' ))
+ {
+ if( *ptr == '@' ) is_op = TRUE;
+ if( *ptr == '+' ) is_voiced = TRUE;
+ ptr++;
+ }
+
+ c = Client_GetFromID( ptr );
+ if( c )
+ {
+ Channel_Join( c, channame );
+ chan = Channel_Search( channame );
+ assert( chan != NULL );
+
+ if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
+ if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
+
+ /* im Channel bekannt machen */
+ IRC_WriteStrChannelPrefix( Client, chan, c, FALSE, "JOIN :%s", channame );
+
+ /* Channel-User-Modes setzen */
+ strcpy( modes, Channel_UserModes( chan, c ));
+ if( modes[0] )
+ {
+ /* Modes im Channel bekannt machen */
+ IRC_WriteStrChannelPrefix( Client, chan, Client, FALSE, "MODE %s +%s %s", channame, modes, Client_ID( c ));
+ }
+ }
+ else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
+
+ /* naechsten Nick suchen */
+ ptr = strtok( NULL, "," );
+ }
+
+ /* an andere Server weiterleiten */
+ IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], Req->argv[1] );
+
+ return CONNECTED;
+} /* IRC_NJOIN */
+
+
+GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
+{
+ CLIENT *intr_c, *target, *c;
+ CHAR *modes;
+
+ assert( Client != NULL );
+ assert( Req != NULL );
+
+ /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
+#ifndef STRICT_RFC
+ 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 ))
+#else
+ 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 ))
+#endif
+ {
+ /* User-Registrierung bzw. Nick-Aenderung */
+
+ /* Falsche Anzahl Parameter? */
+ if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
+
+ /* "Ziel-Client" ermitteln */
+ if( Client_Type( Client ) == CLIENT_SERVER )
+ {
+ target = Client_GetFromID( Req->prefix );
+ if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
+ }
+ else
+ {
+ /* Ist der Client "restricted"? */
+ if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
+ target = Client;
+ }
+
+#ifndef STRICT_RFC
+ /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
+ * wir nichts. So macht es das Original und mind. Snak hat probleme,
+ * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
+ if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
+#endif
+
+ /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
+ * will nur die Gross- und Kleinschreibung aendern. Das darf
+ * er natuerlich machen :-) */
+ if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
+ {
+ if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
+ }
+
+ /* Nick-Aenderung: allen mitteilen! */
+
+ if( Client_Type( Client ) == CLIENT_USER ) IRC_WriteStrClientPrefix( Client, Client, "NICK :%s", Req->argv[0] );
+ IRC_WriteStrServersPrefix( Client, target, "NICK :%s", Req->argv[0] );
+ IRC_WriteStrRelatedPrefix( target, target, FALSE, "NICK :%s", Req->argv[0] );
+
+ /* Client-Nick registrieren */
+ Client_SetID( target, Req->argv[0] );
+
+ if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
+ {
+ /* Neuer Client */
+ Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client_Conn( Client ));
+ if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
+ else Client_SetType( Client, CLIENT_GOTNICK );
+ }
+ else Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
+
+ return CONNECTED;
+ }
+ else if( Client_Type( Client ) == CLIENT_SERVER )
+ {
+ /* Server fuehrt neuen Client ein */
+
+ /* Falsche Anzahl Parameter? */
+ if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
+
+ /* Nick ueberpruefen */
+ c = Client_GetFromID( Req->argv[0] );
+ if( c )
+ {
+ /* Der neue Nick ist auf diesem Server bereits registriert:
+ * sowohl der neue, als auch der alte Client muessen nun
+ * disconnectiert werden. */
+ Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
+ Kill_Nick( Req->argv[0], "Nick collision" );
+ return CONNECTED;
+ }
+
+ /* Server, zu dem der Client connectiert ist, suchen */
+ intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
+ if( ! intr_c )
+ {
+ Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
+ Kill_Nick( Req->argv[0], "Unknown server" );
+ return CONNECTED;
+ }
+
+ /* Neue Client-Struktur anlegen */
+ 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 );
+ if( ! c )
+ {
+ /* Eine neue Client-Struktur konnte nicht angelegt werden.
+ * Der Client muss disconnectiert werden, damit der Netz-
+ * status konsistent bleibt. */
+ Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
+ Kill_Nick( Req->argv[0], "Server error" );
+ return CONNECTED;
+ }
+
+ modes = Client_Modes( c );
+ 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": "" );
+ 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": "" );
+
+ /* Andere Server, ausser dem Introducer, informieren */
+ 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] );
+
+ return CONNECTED;
+ }
+ else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
+} /* IRC_NICK */
+
+
+GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
+{
+ assert( Client != NULL );
+ assert( Req != NULL );
+
+#ifndef STRICT_RFC
+ if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
+#else
+ if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
+#endif
+ {
+ /* Falsche Anzahl Parameter? */
+ if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
+
+ Client_SetUser( Client, Req->argv[0], FALSE );
+ Client_SetInfo( Client, Req->argv[3] );
+
+ Log( LOG_DEBUG, "Connection %d: got USER command ...", Client_Conn( Client ));
+ if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
+ else Client_SetType( Client, CLIENT_GOTUSER );
+ return CONNECTED;
+ }
+ else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
+ {
+ return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
+ }
+ else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
+} /* IRC_USER */
+
+
+GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
+{
+ CLIENT *target;
+
+ assert( Client != NULL );
+ assert( Req != NULL );
+
+ if(( Client_Type( Client ) == CLIENT_USER ) || ( Client_Type( Client ) == CLIENT_SERVICE ))
+ {
+ /* User / Service */
+
+ /* Falsche Anzahl Parameter? */
+ if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
+
+ if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
+ else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
+
+ return DISCONNECTED;
+ }
+ else if ( Client_Type( Client ) == CLIENT_SERVER )
+ {
+ /* Server */
+
+ /* Falsche Anzahl Parameter? */
+ if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
+
+ target = Client_Search( Req->prefix );
+ if( ! target )
+ {
+ Log( LOG_ERR, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
+ return CONNECTED;
+ }
+
+ if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL );
+ else Client_Destroy( target, "Got QUIT command.", Req->argv[0] );
+
+ return CONNECTED;
+ }
+ else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
+} /* IRC_QUIT */
+
+
+GLOBAL BOOLEAN IRC_SQUIT( CLIENT *Client, REQUEST *Req )
+{
+ CLIENT *target;
+
+ assert( Client != NULL );
+ assert( Req != NULL );
+
+ /* SQUIT ist nur fuer Server erlaubt */
+ if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
+
+ /* Falsche Anzahl Parameter? */
+ if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
+
+ target = Client_GetFromID( Req->argv[0] );
+ if( ! target )
+ {
+ Log( LOG_ERR, "Got SQUIT from %s for unknown server \%s\"!?", Client_ID( Client ), Req->argv[0] );
+ return CONNECTED;
+ }
+
+ if( target == Client ) Log( LOG_DEBUG, "Got SQUIT from %s: %s", Client_ID( Client ), Req->argv[1] );
+ else Log( LOG_DEBUG, "Got SQUIT from %s for %s: %s", Client_ID( Client ), Client_ID( target ), Req->argv[1] );
+
+ /* SQUIT an alle Server weiterleiten */
+ IRC_WriteStrServers( Client, "SQUIT %s :%s", Req->argv[0], Req->argv[1] );
+
+ if( Client_Conn( target ) > NONE )
+ {
+ if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), "Got SQUIT command.", Req->argv[1], TRUE );
+ else Conn_Close( Client_Conn( target ), "Got SQUIT command.", NULL, TRUE );
+ return DISCONNECTED;
+ }
+ else
+ {
+ Client_Destroy( target, "Got SQUIT command.", Req->argv[1] );
+ return CONNECTED;
+ }
+} /* IRC_SQUIT */
+
+
+GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
+{
+ CLIENT *target, *from;
+
+ assert( Client != NULL );
+ assert( Req != NULL );
+
+ if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
+
+ /* Falsche Anzahl Parameter? */
+ if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
+ if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
+
+ if( Req->argc == 2 )
+ {
+ /* es wurde ein Ziel-Client angegeben */
+ target = Client_GetFromID( Req->argv[1] );
+ if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
+ if( target != Client_ThisServer( ))
+ {
+ /* ok, forwarden */
+ if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
+ else from = Client;
+ if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
+ return IRC_WriteStrClientPrefix( Client_NextHop( target ), from, "PING %s :%s", Client_ID( from ), Req->argv[1] );
+ }
+ }
+
+ Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
+ return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
+} /* IRC_PING */
+
+
+GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
+{
+ CLIENT *target, *from;
+
+ assert( Client != NULL );
+ assert( Req != NULL );
+
+ if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
+
+ /* Falsche Anzahl Parameter? */
+ if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
+ if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
+
+ /* forwarden? */
+ if( Req->argc == 2 )
+ {
+ target = Client_GetFromID( Req->argv[1] );
+ if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
+ if( target != Client_ThisServer( ))
+ {
+ /* ok, forwarden */
+ if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
+ else from = Client;
+ if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
+ return IRC_WriteStrClientPrefix( Client_NextHop( target ), from, "PONG %s :%s", Client_ID( from ), Req->argv[1] );
+ }
+ }
+
+ /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
+ * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
+
+ if( Client_Conn( Client ) > NONE ) Log( LOG_DEBUG, "Connection %d: received PONG. Lag: %ld seconds.", Client_Conn( Client ), time( NULL ) - Conn_LastPing( Client_Conn( Client )));
+ else Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
+
+ return CONNECTED;
+} /* IRC_PONG */
+
+
+GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
+{
+ assert( Client != NULL );
+ assert( Req != NULL );
+
+ if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
+
+ /* Falsche Anzahl Parameter? */
+ if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
+
+ return Show_MOTD( Client );
+} /* IRC_MOTD */
+
+
+GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
+{
+ CLIENT *to, *from;
+ CHANNEL *chan;
+
+ assert( Client != NULL );
+ assert( Req != NULL );
+
+ if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
+
+ /* Falsche Anzahl Parameter? */
+ if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
+ if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
+ if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
+
+ if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
+ else from = Client;
+ if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
+
+ to = Client_Search( Req->argv[0] );
+ if( to )
+ {
+ /* Okay, Ziel ist ein User */
+ if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
+ return IRC_WriteStrClientPrefix( to, from, "PRIVMSG %s :%s", Client_ID( to ), Req->argv[1] );
+ }
+
+ chan = Channel_Search( Req->argv[0] );
+ if( chan )
+ {
+ /* Okay, Ziel ist ein Channel */
+ if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
+ return IRC_WriteStrChannelPrefix( Client, chan, from, TRUE, "PRIVMSG %s :%s", Req->argv[0], Req->argv[1] );
+ }
+
+ return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
+} /* IRC_PRIVMSG */
+
+
+GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
+{
+ CLIENT *to, *from;
+
+ assert( Client != NULL );
+ assert( Req != NULL );
+
+ if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
+
+ /* Falsche Anzahl Parameter? */
+ if( Req->argc != 2 ) return CONNECTED;
+
+ if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
+ else from = Client;
+ if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
+
+ to = Client_Search( Req->argv[0] );
+ if( to )