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