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