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