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