]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
- Server nimmt nun Server-Links an: PASS und SERVER entsprechend angepasst.
[ngircd-alex.git] / src / ngircd / irc.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4  *
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.
11  *
12  * $Id: irc.c,v 1.31 2002/01/07 15:39:46 alex Exp $
13  *
14  * irc.c: IRC-Befehle
15  *
16  * $Log: irc.c,v $
17  * Revision 1.31  2002/01/07 15:39:46  alex
18  * - Server nimmt nun Server-Links an: PASS und SERVER entsprechend angepasst.
19  * - MODE und NICK melden nun die Aenderungen an andere Server.
20  *
21  * Revision 1.30  2002/01/06 15:21:29  alex
22  * - Loglevel und Meldungen nochmals ueberarbeitet.
23  * - QUIT und SQUIT forwarden nun den Grund der Trennung,
24  * - WHOIS wird nun immer an den "Original-Server" weitergeleitet.
25  *
26  * Revision 1.29  2002/01/05 23:24:54  alex
27  * - WHOIS erweitert: Anfragen koennen an andere Server weitergeleitet werden.
28  * - Vorbereitungen fuer Ident-Abfragen bei neuen Client-Strukturen.
29  *
30  * Revision 1.28  2002/01/05 20:08:02  alex
31  * - Div. Aenderungen fuer die Server-Links (u.a. WHOIS, QUIT, NICK angepasst).
32  * - Neue Funktionen IRC_WriteStrServer() und IRC_WriteStrServerPrefix().
33  *
34  * Revision 1.27  2002/01/05 19:15:03  alex
35  * - Fehlerpruefung bei select() in der "Hauptschleife" korrigiert.
36  *
37  * Revision 1.26  2002/01/05 16:51:18  alex
38  * - das Passwort von Servern wird nun ueberprueft (PASS- und SERVER-Befehl).
39  *
40  * Revision 1.25  2002/01/05 00:48:33  alex
41  * - bei SQUIT wurde immer die Verbindung getrennt, auch bei Remote-Servern.
42  *
43  * Revision 1.24  2002/01/04 17:58:44  alex
44  * - IRC_WriteStrXXX()-Funktionen eingefuehrt, groessere Anpassungen daran.
45  * - neuer Befehl SQUIT, QUIT an Server-Links angepasst.
46  *
47  * Revision 1.23  2002/01/04 01:36:40  alex
48  * - Loglevel ein wenig angepasst.
49  *
50  * Revision 1.22  2002/01/04 01:21:47  alex
51  * - Client-Strukruren werden nur noch ueber Funktionen angesprochen.
52  * - Weitere Anpassungen und Erweiterungen der Server-Links.
53  *
54  * Revision 1.21  2002/01/03 02:26:51  alex
55  * - neue Befehle SERVER und NJOIN begonnen,
56  * - begonnen, diverse IRC-Befehle an Server-Links anzupassen.
57  *
58  * Revision 1.20  2002/01/02 12:46:41  alex
59  * - die Gross- und Kleinschreibung des Nicks kann mit NICK nun geaendert werden.
60  *
61  * Revision 1.19  2002/01/02 02:51:39  alex
62  * - Copyright-Texte angepasst.
63  * - neuer Befehl "ERROR".
64  *
65  * Revision 1.17  2001/12/31 15:33:13  alex
66  * - neuer Befehl NAMES, kleinere Bugfixes.
67  * - Bug bei PING behoben: war zu restriktiv implementiert :-)
68  *
69  * Revision 1.16  2001/12/31 02:18:51  alex
70  * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
71  * - neuen Header "defines.h" mit (fast) allen Konstanten.
72  * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
73  *
74  * Revision 1.15  2001/12/30 19:26:11  alex
75  * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
76  *
77  * Revision 1.14  2001/12/30 11:42:00  alex
78  * - der Server meldet nun eine ordentliche "Start-Zeit".
79  *
80  * Revision 1.13  2001/12/29 03:10:06  alex
81  * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
82  * - neue configure-Optione "--enable-strict-rfc".
83  *
84  * Revision 1.12  2001/12/27 19:17:26  alex
85  * - neue Befehle PRIVMSG, NOTICE, PING.
86  *
87  * Revision 1.11  2001/12/27 16:55:41  alex
88  * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
89  *
90  * Revision 1.10  2001/12/26 22:48:53  alex
91  * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
92  *
93  * Revision 1.9  2001/12/26 14:45:37  alex
94  * - "Code Cleanups".
95  *
96  * Revision 1.8  2001/12/26 03:21:46  alex
97  * - PING/PONG-Befehle implementiert,
98  * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
99  *
100  * Revision 1.7  2001/12/25 23:25:18  alex
101  * - und nochmal Aenderungen am Logging ;-)
102  *
103  * Revision 1.6  2001/12/25 23:13:33  alex
104  * - Debug-Meldungen angepasst.
105  *
106  * Revision 1.5  2001/12/25 22:02:42  alex
107  * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
108  *
109  * Revision 1.4  2001/12/25 19:19:30  alex
110  * - bessere Fehler-Abfragen, diverse Bugfixes.
111  * - Nicks werden nur einmal vergeben :-)
112  * - /MOTD wird unterstuetzt.
113  *
114  * Revision 1.3  2001/12/24 01:34:06  alex
115  * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
116  * - MOTD-Ausgabe begonnen zu implementieren.
117  *
118  * Revision 1.2  2001/12/23 21:57:16  alex
119  * - erste IRC-Befehle zu implementieren begonnen.
120  *
121  * Revision 1.1  2001/12/14 08:13:43  alex
122  * - neues Modul begonnen :-)
123  */
124
125
126 #include <portab.h>
127 #include "global.h"
128
129 #include <imp.h>
130 #include <assert.h>
131 #include <errno.h>
132 #include <stdarg.h>
133 #include <stdio.h>
134 #include <stdlib.h>
135 #include <string.h>
136
137 #include "ngircd.h"
138 #include "client.h"
139 #include "conf.h"
140 #include "conn.h"
141 #include "log.h"
142 #include "messages.h"
143 #include "parse.h"
144 #include "tool.h"
145
146 #include <exp.h>
147 #include "irc.h"
148
149
150 #define CONNECTED TRUE
151 #define DISCONNECTED FALSE
152
153
154 LOCAL BOOLEAN Hello_User( CLIENT *Client );
155 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
156
157 LOCAL VOID Kill_Nick( CHAR *Nick );
158
159
160 GLOBAL VOID IRC_Init( VOID )
161 {
162 } /* IRC_Init */
163
164
165 GLOBAL VOID IRC_Exit( VOID )
166 {
167 } /* IRC_Exit */
168
169
170 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CHAR *Format, ... )
171 {
172         CHAR buffer[1000];
173         BOOLEAN ok = CONNECTED;
174         va_list ap;
175
176         assert( Client != NULL );
177         assert( Format != NULL );
178
179         va_start( ap, Format );
180         vsnprintf( buffer, 1000, Format, ap );
181         va_end( ap );
182
183         /* an den Client selber */
184         ok = IRC_WriteStrClientPrefix( Client, Client_ThisServer( ), buffer );
185
186         return ok;
187 } /* IRC_WriteStrClient */
188
189
190 GLOBAL BOOLEAN IRC_WriteStrClientPrefix( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
191 {
192         /* Text an Clients, lokal bzw. remote, senden. */
193
194         CHAR buffer[1000];
195         va_list ap;
196
197         assert( Client != NULL );
198         assert( Format != NULL );
199         assert( Prefix != NULL );
200
201         va_start( ap, Format );
202         vsnprintf( buffer, 1000, Format, ap );
203         va_end( ap );
204
205         return Conn_WriteStr( Client_Conn( Client_NextHop( Client )), ":%s %s", Client_ID( Prefix ), buffer );
206 } /* IRC_WriteStrClientPrefix */
207
208
209 GLOBAL BOOLEAN IRC_WriteStrRelated( CLIENT *Client, CHAR *Format, ... )
210 {
211         CHAR buffer[1000];
212         BOOLEAN ok = CONNECTED;
213         va_list ap;
214
215         assert( Client != NULL );
216         assert( Format != NULL );
217
218         va_start( ap, Format );
219         vsnprintf( buffer, 1000, Format, ap );
220         va_end( ap );
221
222         /* an den Client selber */
223         ok = IRC_WriteStrClient( Client, buffer );
224
225         /* FIXME */
226         
227         return ok;
228 } /* IRC_WriteStrRelated */
229
230
231 GLOBAL VOID IRC_WriteStrServers( CLIENT *ExceptOf, CHAR *Format, ... )
232 {
233         CHAR buffer[1000];
234         va_list ap;
235
236         assert( Format != NULL );
237
238         va_start( ap, Format );
239         vsnprintf( buffer, 1000, Format, ap );
240         va_end( ap );
241
242         /* an den Client selber */
243         return IRC_WriteStrServersPrefix( ExceptOf, Client_ThisServer( ), buffer );
244 } /* IRC_WriteStrServers */
245
246
247 GLOBAL VOID IRC_WriteStrServersPrefix( CLIENT *ExceptOf, CLIENT *Prefix, CHAR *Format, ... )
248 {
249         CHAR buffer[1000];
250         CLIENT *c;
251         va_list ap;
252         
253         assert( Format != NULL );
254         assert( Prefix != NULL );
255
256         va_start( ap, Format );
257         vsnprintf( buffer, 1000, Format, ap );
258         va_end( ap );
259         
260         c = Client_First( );
261         while( c )
262         {
263                 if(( Client_Type( c ) == CLIENT_SERVER ) && ( Client_Conn( c ) > NONE ) && ( c != Client_ThisServer( )) && ( c != ExceptOf ))
264                 {
265                         /* Ziel-Server gefunden */
266                         IRC_WriteStrClientPrefix( c, Prefix, buffer );
267                 }
268                 c = Client_Next( c );
269         }
270 } /* IRC_WriteStrServersPrefix */
271
272
273 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
274 {
275         assert( Client != NULL );
276         assert( Req != NULL );
277
278         /* Fehler liefern, wenn kein lokaler Client */
279         if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
280         
281         if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
282         {
283                 /* noch nicht registrierte unbekannte Verbindung */
284                 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
285
286                 /* Passwort speichern */
287                 Client_SetPassword( Client, Req->argv[0] );
288
289                 Client_SetType( Client, CLIENT_GOTPASS );
290                 return CONNECTED;
291         }
292         else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
293         {
294                 /* noch nicht registrierte Server-Verbindung */
295                 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
296
297                 /* Passwort speichern */
298                 Client_SetPassword( Client, Req->argv[0] );
299
300                 Client_SetType( Client, CLIENT_GOTPASSSERVER );
301                 return CONNECTED;
302         }
303         else if(( Client_Type( Client ) == CLIENT_UNKNOWN  ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
304         {
305                 /* Falsche Anzahl Parameter? */
306                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
307         }
308         else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
309 } /* IRC_PASS */
310
311
312 GLOBAL BOOLEAN IRC_SERVER( CLIENT *Client, REQUEST *Req )
313 {
314         CHAR str[LINE_LEN], *ptr;
315         BOOLEAN ok;
316         CLIENT *c;
317         INT i;
318         
319         assert( Client != NULL );
320         assert( Req != NULL );
321
322         /* Fehler liefern, wenn kein lokaler Client */
323         if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
324
325         if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
326         {
327                 /* Verbindung soll als Server-Server-Verbindung registriert werden */
328                 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
329
330                 /* Falsche Anzahl Parameter? */
331                 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
332
333                 /* Ist dieser Server bei uns konfiguriert? */
334                 for( i = 0; i < Conf_Server_Count; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
335                 if( i >= Conf_Server_Count )
336                 {
337                         /* Server ist nicht konfiguriert! */
338                         Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
339                         Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", TRUE );
340                         return DISCONNECTED;
341                 }
342                 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd ) != 0 )
343                 {
344                         /* Falsches Passwort */
345                         Log( LOG_ERR, "Connection %d: Bad password for server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
346                         Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
347                         return DISCONNECTED;
348                 }
349                 
350                 /* Ist ein Server mit dieser ID bereits registriert? */
351                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
352
353                 /* Meldet sich der Server bei uns an? */
354                 if( Req->argc == 2 )
355                 {
356                         /* Unseren SERVER- und PASS-Befehl senden */
357                         ok = TRUE;
358                         if( ! IRC_WriteStrClient( Client, "PASS %s "PASSSERVERADD, Conf_Server[i].pwd )) ok = FALSE;
359                         else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
360                         if( ! ok )
361                         {
362                                 Conn_Close( Client_Conn( Client ), "Unexpected server behavior!", NULL, FALSE );
363                                 return DISCONNECTED;
364                         }
365                 }
366
367                 /* Server-Strukturen fuellen ;-) */
368                 Client_SetID( Client, Req->argv[0] );
369                 Client_SetHops( Client, 1 );
370                 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
371
372                 if( Req->argc == 3 ) Client_SetToken( Client, atoi( Req->argv[1] ));
373                 else Client_SetToken( Client, 0 );
374
375                 Log( LOG_NOTICE, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), Client_Conn( Client ));
376
377                 Client_SetType( Client, CLIENT_SERVER );
378
379                 /* Unsere User und Server bekanntmachen */
380                 /* ... */
381                 
382                 return CONNECTED;
383         }
384         else if( Client_Type( Client ) == CLIENT_SERVER )
385         {
386                 /* Neuer Server wird im Netz angekuendigt */
387
388                 /* Falsche Anzahl Parameter? */
389                 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
390
391                 /* Ist ein Server mit dieser ID bereits registriert? */
392                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
393
394                 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
395                 ptr = strchr( Req->argv[3] + 2, '[' );
396                 if( ! ptr ) ptr = Req->argv[3];
397
398                 /* Neue Client-Struktur anlegen */
399                 c = Client_NewRemoteServer( Client, Req->argv[0], atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, TRUE );
400                 if( ! c )
401                 {
402                         /* Neue Client-Struktur konnte nicht angelegt werden */
403                         Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
404                         Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", TRUE );
405                         return DISCONNECTED;
406                 }
407
408                 /* Log-Meldung zusammenbauen und ausgeben */
409                 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) sprintf( str, "connected to %s, ", Req->prefix );
410                 else strcpy( str, "" );
411                 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": "" );
412                 
413                 return CONNECTED;
414         }
415         else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
416 } /* IRC_SERVER */
417
418
419 GLOBAL BOOLEAN IRC_NJOIN( CLIENT *Client, REQUEST *Req )
420 {
421         assert( Client != NULL );
422         assert( Req != NULL );
423
424         if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTEREDSERVER_MSG, Client_ID( Client ));
425
426         return CONNECTED;
427 } /* IRC_NJOIN */
428
429
430 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
431 {
432         CLIENT *intr_c, *target, *c;
433
434         assert( Client != NULL );
435         assert( Req != NULL );
436
437         /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
438 #ifndef STRICT_RFC
439         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 ))
440 #else
441         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 ))
442 #endif
443         {
444                 /* User-Registrierung bzw. Nick-Aenderung */
445
446                 /* Falsche Anzahl Parameter? */
447                 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
448
449                 /* "Ziel-Client" ermitteln */
450                 if( Client_Type( Client ) == CLIENT_SERVER )
451                 {
452                         target = Client_GetFromID( Req->prefix );
453                         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
454                 }
455                 else
456                 {
457                         /* Ist der Client "restricted"? */
458                         if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
459                         target = Client;
460                 }
461
462                 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
463                  * wir nichts. So macht es das Original und mind. Snak hat probleme,
464                  * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
465 #ifndef STRICT_RFC
466                 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
467 #endif
468                 
469                 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
470                  * will nur die Gross- und Kleinschreibung aendern. Das darf
471                  * er natuerlich machen :-) */
472                 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
473                 {
474                         if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
475                 }
476
477                 if( Client_Type( Client ) == CLIENT_USER )
478                 {
479                         /* Nick-Aenderung: allen mitteilen! */
480                         Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
481                         IRC_WriteStrRelated( Client, "NICK :%s", Req->argv[0] );
482                         IRC_WriteStrServersPrefix( NULL, Client, "NICK :%s", Req->argv[0] );
483                 }
484                 else if( Client_Type( Client ) == CLIENT_SERVER )
485                 {
486                         /* Nick-Aenderung: allen mitteilen! */
487                         Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
488                         IRC_WriteStrServersPrefix( Client, Client, "NICK :%s", Req->argv[0] );
489                 }
490         
491                 /* Client-Nick registrieren */
492                 Client_SetID( target, Req->argv[0] );
493
494                 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
495                 {
496                         /* Neuer Client */
497                         Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client_Conn( Client ));
498                         if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
499                         else Client_SetType( Client, CLIENT_GOTNICK );
500                 }
501                 return CONNECTED;
502         }
503         else if( Client_Type( Client ) == CLIENT_SERVER )
504         {
505                 /* Server fuehrt neuen Client ein */
506
507                 /* Falsche Anzahl Parameter? */
508                 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
509
510                 /* Nick ueberpruefen */
511                 c = Client_GetFromID( Req->argv[0] );
512                 if( c )
513                 {
514                         /* Der neue Nick ist auf diesem Server bereits registriert:
515                          * sowohl der neue, als auch der alte Client muessen nun
516                          * disconnectiert werden. */
517                         Log( LOG_ERR, "Server %s introduces already registered nick %s!", Client_ID( Client ), Req->argv[0] );
518                         Kill_Nick( Req->argv[0] );
519                         return CONNECTED;
520                 }
521
522                 /* Server, zu dem der Client connectiert ist, suchen */
523                 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
524                 if( ! intr_c )
525                 {
526                         Log( LOG_ERR, "Server %s introduces nick %s with unknown host server!?", Client_ID( Client ), Req->argv[0] );
527                         Kill_Nick( Req->argv[0] );
528                         return CONNECTED;
529                 }
530
531                 /* Neue Client-Struktur anlegen */
532                 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], TRUE );
533                 if( ! c )
534                 {
535                         /* Eine neue Client-Struktur konnte nicht angelegt werden.
536                          * Der Client muss disconnectiert werden, damit der Netz-
537                          * status konsistent bleibt. */
538                         Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
539                         Kill_Nick( Req->argv[0] );
540                         return CONNECTED;
541                 }
542
543                 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": "" );
544
545                 return CONNECTED;
546         }
547         else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
548 } /* IRC_NICK */
549
550
551 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
552 {
553         assert( Client != NULL );
554         assert( Req != NULL );
555
556 #ifndef STRICT_RFC
557         if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
558 #else
559         if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
560 #endif
561         {
562                 /* Falsche Anzahl Parameter? */
563                 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
564
565                 Client_SetUser( Client, Req->argv[0], FALSE );
566                 Client_SetInfo( Client, Req->argv[3] );
567
568                 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client_Conn( Client ));
569                 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
570                 else Client_SetType( Client, CLIENT_GOTUSER );
571                 return CONNECTED;
572         }
573         else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
574         {
575                 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
576         }
577         else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
578 } /* IRC_USER */
579
580
581 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
582 {
583         CLIENT *target;
584         
585         assert( Client != NULL );
586         assert( Req != NULL );
587
588         if(( Client_Type( Client ) == CLIENT_USER ) || ( Client_Type( Client ) == CLIENT_SERVICE ))
589         {
590                 /* User / Service */
591                 
592                 /* Falsche Anzahl Parameter? */
593                 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
594
595                 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
596                 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
597                 
598                 return DISCONNECTED;
599         }
600         else if ( Client_Type( Client ) == CLIENT_SERVER )
601         {
602                 /* Server */
603
604                 /* Falsche Anzahl Parameter? */
605                 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
606
607                 target = Client_Search( Req->prefix );
608                 if( ! target )
609                 {
610                         Log( LOG_ERR, "Got QUIT from %s for unknown server!?", Client_ID( Client ));
611                         return CONNECTED;
612                 }
613
614                 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL );
615                 else Client_Destroy( target, "Got QUIT command.", Req->argv[0] );
616
617                 return CONNECTED;
618         }
619         else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
620 } /* IRC_QUIT */
621
622
623 GLOBAL BOOLEAN IRC_SQUIT( CLIENT *Client, REQUEST *Req )
624 {
625         CLIENT *target;
626         
627         assert( Client != NULL );
628         assert( Req != NULL );
629
630         /* SQUIT ist nur fuer Server erlaubt */
631         if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
632
633         /* Falsche Anzahl Parameter? */
634         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
635
636         target = Client_GetFromID( Req->argv[0] );
637         if( ! target )
638         {
639                 Log( LOG_ERR, "Got SQUIT from %s for unknown server \%s\"!?", Client_ID( Client ), Req->argv[0] );
640                 return CONNECTED;
641         }
642
643         if( target == Client ) Log( LOG_DEBUG, "Got SQUIT from %s: %s", Client_ID( Client ), Req->argv[1] );
644         else Log( LOG_DEBUG, "Got SQUIT from %s for %s: %s", Client_ID( Client ), Client_ID( target ), Req->argv[1] );
645
646         /* SQUIT an alle Server weiterleiten */
647         IRC_WriteStrServers( Client, "SQUIT %s :%s", Req->argv[0], Req->argv[1] );
648
649         if( Client_Conn( target ) > NONE )
650         {
651                 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), "Got SQUIT command.", Req->argv[1], TRUE );
652                 else Conn_Close( Client_Conn( target ), "Got SQUIT command.", NULL, TRUE );
653                 return DISCONNECTED;
654         }
655         else
656         {
657                 Client_Destroy( target, "Got SQUIT command.", Req->argv[1] );
658                 return CONNECTED;
659         }
660 } /* IRC_SQUIT */
661
662
663 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
664 {
665         assert( Client != NULL );
666         assert( Req != NULL );
667
668         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
669
670         /* Falsche Anzahl Parameter? */
671         if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
672         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
673
674         return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
675 } /* IRC_PING */
676
677
678 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
679 {
680         assert( Client != NULL );
681         assert( Req != NULL );
682
683         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
684
685         /* Falsche Anzahl Parameter? */
686         if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
687         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
688
689         /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
690          * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
691
692         Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
693         return CONNECTED;
694 } /* IRC_PONG */
695
696
697 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
698 {
699         assert( Client != NULL );
700         assert( Req != NULL );
701
702         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
703
704         /* Falsche Anzahl Parameter? */
705         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
706
707         return Show_MOTD( Client );
708 } /* IRC_MOTD */
709
710
711 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
712 {
713         CLIENT *to, *from;
714         
715         assert( Client != NULL );
716         assert( Req != NULL );
717
718         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
719
720         /* Falsche Anzahl Parameter? */
721         if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
722         if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
723         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
724
725         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
726         else from = Client;
727         
728         to = Client_Search( Req->argv[0] );
729         if( to )
730         {
731                 /* Okay, Ziel ist ein User */
732                 if( Client_Conn( from ) >= 0 ) Conn_UpdateIdle( Client_Conn( from ));
733                 return IRC_WriteStrClientPrefix( to, from, "PRIVMSG %s :%s", Client_ID( to ), Req->argv[1] );
734         }
735         else return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
736 } /* IRC_PRIVMSG */
737
738
739 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
740 {
741         CLIENT *to, *from;
742
743         assert( Client != NULL );
744         assert( Req != NULL );
745
746         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
747
748         /* Falsche Anzahl Parameter? */
749         if( Req->argc != 2 ) return CONNECTED;
750
751         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
752         else from = Client;
753
754         to = Client_Search( Req->argv[0] );
755         if( to )
756         {
757                 /* Okay, Ziel ist ein User */
758                 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
759         }
760         else return CONNECTED;
761 } /* IRC_NOTICE */
762
763
764 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
765 {
766         CHAR x[2], new_modes[CLIENT_MODE_LEN], *ptr;
767         BOOLEAN set, ok;
768         CLIENT *target;
769         
770         assert( Client != NULL );
771         assert( Req != NULL );
772
773         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
774
775         /* Falsche Anzahl Parameter? */
776         if(( Req->argc > 2 ) || ( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
777
778         /* "Ziel-Client" suchen */
779         target = Client_Search( Req->argv[0] );
780
781         /* Wer ist der Anfragende? */
782         if( Client_Type( Client ) == CLIENT_USER )
783         {
784                 /* User: MODE ist nur fuer sich selber zulaessig */
785                 if( target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
786         }
787         else
788         {
789                 /* Server: gibt es den Client ueberhaupt? */
790                 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
791         }
792
793         /* Werden die Modes erfragt? */
794         if( Req->argc == 1 ) return IRC_WriteStrClient( Client, RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( Client ));
795
796         ptr = Req->argv[1];
797
798         /* Sollen Modes gesetzt oder geloescht werden? */
799         if( *ptr == '+' ) set = TRUE;
800         else if( *ptr == '-' ) set = FALSE;
801         else return IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
802
803         /* Reply-String mit Aenderungen vorbereiten */
804         if( set ) strcpy( new_modes, "+" );
805         else strcpy( new_modes, "-" );
806
807         ptr++;
808         ok = TRUE;
809         x[1] = '\0';
810         while( *ptr )
811         {
812                 x[0] = '\0';
813                 if( Client_Type( Client ) == CLIENT_SERVER )
814                 {
815                         x[0] = *ptr;
816                         ok = TRUE;
817                 }
818                 else
819                 {
820                         switch( *ptr )
821                         {
822                                 case 'i':
823                                         /* invisible */
824                                         x[0] = 'i';
825                                         break;
826                                 case 'r':
827                                         /* restricted (kann nur gesetzt werden) */
828                                         if( set ) x[0] = 'r';
829                                         else ok = IRC_WriteStrClient( target, ERR_RESTRICTED_MSG, Client_ID( target ));
830                                         break;
831                                 case 'o':
832                                         /* operator (kann nur geloescht werden) */
833                                         if( ! set )
834                                         {
835                                                 Client_SetOperByMe( target, FALSE );
836                                                 x[0] = 'o';
837                                         }
838                                         else ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
839                                         break;
840                                 default:
841                                         ok = IRC_WriteStrClient( target, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( target ));
842                                         x[0] = '\0';
843                         }
844                 }
845                 if( ! ok ) break;
846
847                 ptr++;
848                 if( ! x[0] ) continue;
849
850                 /* Okay, gueltigen Mode gefunden */
851                 if( set )
852                 {
853                         /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
854                         if( Client_ModeAdd( target, x[0] )) strcat( new_modes, x );
855                 }
856                 else
857                 {
858                         /* Modes geloescht. Wenn der Client ihn hatte: merken */
859                         if( Client_ModeDel( target, x[0] )) strcat( new_modes, x );
860                 }
861         }
862         
863         /* Geanderte Modes? */
864         if( new_modes[1] )
865         {
866                 if( Client_Type( Client ) == CLIENT_SERVER )
867                 {
868                         /* Modes an andere Server forwarden */
869                         IRC_WriteStrServersPrefix( Client, Client, "MODE %s :%s", Client_ID( target ), new_modes );
870                 }
871                 else
872                 {
873                         /* Bestaetigung an Client schicken & andere Server informieren */
874                         ok = IRC_WriteStrRelated( Client, "MODE %s :%s", Client_ID( target ), new_modes );
875                         IRC_WriteStrServers( Client, "MODE %s :%s", Client_ID( target ), new_modes );
876                 }
877                 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( target ), Client_Modes( target ));
878         }
879         return ok;
880 } /* IRC_MODE */
881
882
883 GLOBAL BOOLEAN IRC_OPER( CLIENT *Client, REQUEST *Req )
884 {
885         INT i;
886         
887         assert( Client != NULL );
888         assert( Req != NULL );
889
890         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
891         
892         /* Falsche Anzahl Parameter? */
893         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
894
895         /* Operator suchen */
896         for( i = 0; i < Conf_Oper_Count; i++)
897         {
898                 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
899         }
900         if( i >= Conf_Oper_Count )
901         {
902                 Log( LOG_WARNING, "Got invalid OPER from \"%s\": Name \"%s\" not configured!", Client_Mask( Client ), Req->argv[0] );
903                 return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
904         }
905
906         /* Stimmt das Passwort? */
907         if( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )
908         {
909                 Log( LOG_WARNING, "Got invalid OPER from \"%s\": Bad password for \"%s\"!", Client_Mask( Client ), Conf_Oper[i].name );
910                 return IRC_WriteStrClient( Client, ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
911         }
912         
913         if( ! Client_HasMode( Client, 'o' ))
914         {
915                 /* noch kein o-Mode gesetzt */
916                 Client_ModeAdd( Client, 'o' );
917                 if( ! IRC_WriteStrRelated( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
918         }
919
920         if( ! Client_OperByMe( Client )) Log( LOG_NOTICE, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
921
922         Client_SetOperByMe( Client, TRUE );
923         return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
924 } /* IRC_OPER */
925
926
927 GLOBAL BOOLEAN IRC_DIE( CLIENT *Client, REQUEST *Req )
928 {
929         assert( Client != NULL );
930         assert( Req != NULL );
931
932         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
933
934         /* Falsche Anzahl Parameter? */
935         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
936
937         if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
938
939         Log( LOG_NOTICE, "Got DIE command from \"%s\", going down!", Client_Mask( Client ));
940         NGIRCd_Quit = TRUE;
941         return CONNECTED;
942 } /* IRC_DIE */
943
944
945 GLOBAL BOOLEAN IRC_RESTART( CLIENT *Client, REQUEST *Req )
946 {
947         assert( Client != NULL );
948         assert( Req != NULL );
949
950         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
951
952         /* Falsche Anzahl Parameter? */
953         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
954
955         if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
956
957         Log( LOG_NOTICE, "Got RESTART command from \"%s\", going down!", Client_Mask( Client ));
958         NGIRCd_Restart = TRUE;
959         return CONNECTED;
960 } /* IRC_RESTART */
961
962
963 GLOBAL BOOLEAN IRC_NAMES( CLIENT *Client, REQUEST *Req )
964 {
965         CHAR rpl[COMMAND_LEN];
966         CLIENT *c;
967         
968         assert( Client != NULL );
969         assert( Req != NULL );
970
971         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
972
973         /* Falsche Anzahl Parameter? */
974         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
975
976         /* Noch alle User ausgeben, die in keinem Channel sind */
977         rpl[0] = '\0';
978         c = Client_First( );
979         while( c )
980         {
981                 if( Client_Type( c ) == CLIENT_USER )
982                 {
983                         /* Okay, das ist ein User */
984                         strcat( rpl, Client_ID( c ));
985                         strcat( rpl, " " );
986                 }
987
988                 /* Antwort zu lang? Splitten. */
989                 if( strlen( rpl ) > 480 )
990                 {
991                         if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
992                         if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
993                         rpl[0] = '\0';
994                 }
995                 
996                 c = Client_Next( c );
997         }
998         if( rpl[0] )
999         {
1000                 /* es wurden User gefunden */
1001                 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1002                 if( ! IRC_WriteStrClient( Client, RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
1003         }
1004         return IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), "*" );
1005 } /* IRC_NAMES */
1006
1007
1008 GLOBAL BOOLEAN IRC_ISON( CLIENT *Client, REQUEST *Req )
1009 {
1010         CHAR rpl[COMMAND_LEN];
1011         CLIENT *c;
1012         CHAR *ptr;
1013         INT i;
1014         
1015         assert( Client != NULL );
1016         assert( Req != NULL );
1017
1018         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1019
1020         /* Falsche Anzahl Parameter? */
1021         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1022
1023         strcpy( rpl, RPL_ISON_MSG );
1024         for( i = 0; i < Req->argc; i++ )
1025         {
1026                 ptr = strtok( Req->argv[i], " " );
1027                 while( ptr )
1028                 {
1029                         ngt_TrimStr( ptr );
1030                         c = Client_GetFromID( ptr );
1031                         if( c && ( Client_Type( c ) == CLIENT_USER ))
1032                         {
1033                                 /* Dieser Nick ist "online" */
1034                                 strcat( rpl, ptr );
1035                                 strcat( rpl, " " );
1036                         }
1037                         ptr = strtok( NULL, " " );
1038                 }
1039         }
1040         if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1041
1042         return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1043 } /* IRC_ISON */
1044
1045
1046 GLOBAL BOOLEAN IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1047 {
1048         CLIENT *from, *target, *c;
1049         CHAR *ptr = NULL;
1050         
1051         assert( Client != NULL );
1052         assert( Req != NULL );
1053
1054         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1055
1056         /* Falsche Anzahl Parameter? */
1057         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1058
1059         /* Client suchen */
1060         c = Client_GetFromID( Req->argv[Req->argc - 1] );
1061         if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
1062
1063         /* Empfaenger des WHOIS suchen */
1064         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
1065         else from = Client;
1066         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
1067         
1068         /* Forwarden an anderen Server? */
1069         if( Req->argc > 1 )
1070         {
1071                 /* angegebenen Ziel-Server suchen */
1072                 target = Client_GetFromID( Req->argv[1] );
1073                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
1074                 ptr = Req->argv[1];
1075         }
1076         else if( Client_Conn( c ) == NONE )
1077         {
1078                 /* Client ist nicht von uns. Ziel-Server suchen */
1079                 target = c;
1080                 ptr = Req->argv[0];
1081         }
1082         else target = NULL;
1083         
1084         if( target && ( Client_NextHop( target ) != Client_ThisServer( ))) return IRC_WriteStrClientPrefix( target, from, "WHOIS %s :%s", Req->argv[0], ptr );
1085         
1086         /* Nick, User und Name */
1087         if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
1088
1089         /* Server */
1090         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;
1091
1092         /* IRC-Operator? */
1093         if( Client_HasMode( c, 'o' ))
1094         {
1095                 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
1096         }
1097
1098         /* Idle (nur lokale Clients) */
1099         if( Client_Conn( c ) > NONE )
1100         {
1101                 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
1102         }
1103
1104         /* End of Whois */
1105         return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1106 } /* IRC_WHOIS */
1107
1108
1109 GLOBAL BOOLEAN IRC_USERHOST( CLIENT *Client, REQUEST *Req )
1110 {
1111         CHAR rpl[COMMAND_LEN];
1112         CLIENT *c;
1113         INT max, i;
1114
1115         assert( Client != NULL );
1116         assert( Req != NULL );
1117
1118         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
1119
1120         /* Falsche Anzahl Parameter? */
1121         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
1122
1123         if( Req->argc > 5 ) max = 5;
1124         else max = Req->argc;
1125         
1126         strcpy( rpl, RPL_USERHOST_MSG );
1127         for( i = 0; i < max; i++ )
1128         {
1129                 c = Client_GetFromID( Req->argv[i] );
1130                 if( c && ( Client_Type( c ) == CLIENT_USER ))
1131                 {
1132                         /* Dieser Nick ist "online" */
1133                         strcat( rpl, Client_ID( c ));
1134                         if( Client_HasMode( c, 'o' )) strcat( rpl, "*" );
1135                         strcat( rpl, "=" );
1136                         if( Client_HasMode( c, 'a' )) strcat( rpl, "-" );
1137                         else strcat( rpl, "+" );
1138                         strcat( rpl, Client_User( c ));
1139                         strcat( rpl, "@" );
1140                         strcat( rpl, Client_Hostname( c ));
1141                         strcat( rpl, " " );
1142                 }
1143         }
1144         if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
1145
1146         return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
1147 } /* IRC_USERHOST */
1148
1149
1150 GLOBAL BOOLEAN IRC_ERROR( CLIENT *Client, REQUEST *Req )
1151 {
1152         assert( Client != NULL );
1153         assert( Req != NULL );
1154
1155         if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
1156         else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
1157
1158         return CONNECTED;
1159 } /* IRC_ERROR */
1160
1161
1162 LOCAL BOOLEAN Hello_User( CLIENT *Client )
1163 {
1164         assert( Client != NULL );
1165
1166         /* Passwort ueberpruefen */
1167         if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
1168         {
1169                 /* Falsches Passwort */
1170                 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
1171                 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
1172                 return DISCONNECTED;
1173         }
1174
1175         Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
1176
1177         /* Andere Server informieren */
1178         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 ));
1179
1180         if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
1181         if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
1182         if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
1183         if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
1184
1185         Client_SetType( Client, CLIENT_USER );
1186
1187         return Show_MOTD( Client );
1188 } /* Hello_User */
1189
1190
1191 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
1192 {
1193         BOOLEAN ok;
1194         CHAR line[127];
1195         FILE *fd;
1196         
1197         assert( Client != NULL );
1198
1199         fd = fopen( Conf_MotdFile, "r" );
1200         if( ! fd )
1201         {
1202                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1203                 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1204         }
1205         
1206         IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
1207         while( TRUE )
1208         {
1209                 if( ! fgets( line, 126, fd )) break;
1210                 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
1211                 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
1212                 {
1213                         fclose( fd );
1214                         return FALSE;
1215                 }
1216         }
1217         ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
1218
1219         fclose( fd );
1220         
1221         return ok;
1222 } /* Show_MOTD */
1223
1224
1225 LOCAL VOID Kill_Nick( CHAR *Nick )
1226 {
1227         Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected!", Nick );
1228         /* FIXME */
1229         Log( LOG_ALERT, "[Kill_Nick() not implemented - OOOPS!]" );
1230 } /* Kill_Nick */
1231
1232
1233 /* -eof- */