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