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