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