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