]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
- Loglevel ein wenig angepasst.
[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.23 2002/01/04 01:36:40 alex Exp $
13  *
14  * irc.c: IRC-Befehle
15  *
16  * $Log: irc.c,v $
17  * Revision 1.23  2002/01/04 01:36:40  alex
18  * - Loglevel ein wenig angepasst.
19  *
20  * Revision 1.22  2002/01/04 01:21:47  alex
21  * - Client-Strukruren werden nur noch ueber Funktionen angesprochen.
22  * - Weitere Anpassungen und Erweiterungen der Server-Links.
23  *
24  * Revision 1.21  2002/01/03 02:26:51  alex
25  * - neue Befehle SERVER und NJOIN begonnen,
26  * - begonnen, diverse IRC-Befehle an Server-Links anzupassen.
27  *
28  * Revision 1.20  2002/01/02 12:46:41  alex
29  * - die Gross- und Kleinschreibung des Nicks kann mit NICK nun geaendert werden.
30  *
31  * Revision 1.19  2002/01/02 02:51:39  alex
32  * - Copyright-Texte angepasst.
33  * - neuer Befehl "ERROR".
34  *
35  * Revision 1.17  2001/12/31 15:33:13  alex
36  * - neuer Befehl NAMES, kleinere Bugfixes.
37  * - Bug bei PING behoben: war zu restriktiv implementiert :-)
38  *
39  * Revision 1.16  2001/12/31 02:18:51  alex
40  * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
41  * - neuen Header "defines.h" mit (fast) allen Konstanten.
42  * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
43  *
44  * Revision 1.15  2001/12/30 19:26:11  alex
45  * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
46  *
47  * Revision 1.14  2001/12/30 11:42:00  alex
48  * - der Server meldet nun eine ordentliche "Start-Zeit".
49  *
50  * Revision 1.13  2001/12/29 03:10:06  alex
51  * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
52  * - neue configure-Optione "--enable-strict-rfc".
53  *
54  * Revision 1.12  2001/12/27 19:17:26  alex
55  * - neue Befehle PRIVMSG, NOTICE, PING.
56  *
57  * Revision 1.11  2001/12/27 16:55:41  alex
58  * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
59  *
60  * Revision 1.10  2001/12/26 22:48:53  alex
61  * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
62  *
63  * Revision 1.9  2001/12/26 14:45:37  alex
64  * - "Code Cleanups".
65  *
66  * Revision 1.8  2001/12/26 03:21:46  alex
67  * - PING/PONG-Befehle implementiert,
68  * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
69  *
70  * Revision 1.7  2001/12/25 23:25:18  alex
71  * - und nochmal Aenderungen am Logging ;-)
72  *
73  * Revision 1.6  2001/12/25 23:13:33  alex
74  * - Debug-Meldungen angepasst.
75  *
76  * Revision 1.5  2001/12/25 22:02:42  alex
77  * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
78  *
79  * Revision 1.4  2001/12/25 19:19:30  alex
80  * - bessere Fehler-Abfragen, diverse Bugfixes.
81  * - Nicks werden nur einmal vergeben :-)
82  * - /MOTD wird unterstuetzt.
83  *
84  * Revision 1.3  2001/12/24 01:34:06  alex
85  * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
86  * - MOTD-Ausgabe begonnen zu implementieren.
87  *
88  * Revision 1.2  2001/12/23 21:57:16  alex
89  * - erste IRC-Befehle zu implementieren begonnen.
90  *
91  * Revision 1.1  2001/12/14 08:13:43  alex
92  * - neues Modul begonnen :-)
93  */
94
95
96 #include <portab.h>
97 #include "global.h"
98
99 #include <imp.h>
100 #include <assert.h>
101 #include <errno.h>
102 #include <stdarg.h>
103 #include <stdio.h>
104 #include <stdlib.h>
105 #include <string.h>
106
107 #include "ngircd.h"
108 #include "client.h"
109 #include "conf.h"
110 #include "conn.h"
111 #include "log.h"
112 #include "messages.h"
113 #include "parse.h"
114 #include "tool.h"
115
116 #include <exp.h>
117 #include "irc.h"
118
119
120 #define CONNECTED TRUE
121 #define DISCONNECTED FALSE
122
123
124 LOCAL BOOLEAN Hello_User( CLIENT *Client );
125 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
126
127 LOCAL VOID Kill_Nick( CHAR *Nick );
128
129
130 GLOBAL VOID IRC_Init( VOID )
131 {
132 } /* IRC_Init */
133
134
135 GLOBAL VOID IRC_Exit( VOID )
136 {
137 } /* IRC_Exit */
138
139
140 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
141 {
142         /* Text an Clients, lokal bzw. remote, senden. */
143
144         CHAR buffer[1000];
145         BOOLEAN ok = CONNECTED;
146         CONN_ID send_to;
147         va_list ap;
148
149         assert( Client != NULL );
150         assert( Format != NULL );
151
152         va_start( ap, Format );
153         vsnprintf( buffer, 1000, Format, ap );
154         va_end( ap );
155
156         if( Client_Conn( Client ) != NONE ) send_to = Client_Conn( Client );
157         else send_to = Client_Conn( Client_Introducer( Client ));
158
159         if( Prefix ) ok = Conn_WriteStr( Client_Conn( Client ), ":%s %s", Client_Mask( Prefix ), buffer );
160         else ok = Conn_WriteStr( Client_Conn( Client ), buffer );
161
162         return ok;
163 } /* IRC_WriteStrClient */
164
165
166 GLOBAL BOOLEAN IRC_WriteStrRelated( CLIENT *Client, CHAR *Format, ... )
167 {
168         CHAR buffer[1000];
169         BOOLEAN ok = CONNECTED;
170         va_list ap;
171
172         assert( Client != NULL );
173         assert( Format != NULL );
174
175         va_start( ap, Format );
176         vsnprintf( buffer, 1000, Format, ap );
177         va_end( ap );
178
179         /* an den Client selber */
180         ok = IRC_WriteStrClient( Client, Client, buffer );
181
182         return ok;
183 } /* IRC_WriteStrRelated */
184
185
186 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
187 {
188         assert( Client != NULL );
189         assert( Req != NULL );
190
191         /* Fehler liefern, wenn kein lokaler Client */
192         if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
193         
194         if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
195         {
196                 /* noch nicht registrierte unbekannte Verbindung */
197                 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
198
199                 /* Passwort speichern */
200                 Client_SetPassword( Client, Req->argv[0] );
201
202                 Client_SetType( Client, CLIENT_GOTPASS );
203                 return CONNECTED;
204         }
205         else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
206         {
207                 /* noch nicht registrierte Server-Verbindung */
208                 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
209
210                 /* Passwort speichern */
211                 Client_SetPassword( Client, Req->argv[0] );
212
213                 Client_SetType( Client, CLIENT_GOTPASSSERVER );
214                 return CONNECTED;
215         }
216         else if(( Client_Type( Client ) == CLIENT_UNKNOWN  ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
217         {
218                 /* Falsche Anzahl Parameter? */
219                 return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
220         }
221         else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
222 } /* IRC_PASS */
223
224
225 GLOBAL BOOLEAN IRC_SERVER( CLIENT *Client, REQUEST *Req )
226 {
227         CHAR str[LINE_LEN], *ptr;
228         CLIENT *c;
229         
230         assert( Client != NULL );
231         assert( Req != NULL );
232
233         /* Fehler liefern, wenn kein lokaler Client */
234         if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
235
236         if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
237         {
238                 /* Verbindung soll als Server-Server-Verbindung registriert werden */
239                 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
240
241                 /* Falsche Anzahl Parameter? */
242                 if( Req->argc != 3 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
243
244                 /* Ist ein Server mit dieser ID bereits registriert? */
245                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
246
247                 /* Server-Strukturen fuellen ;-) */
248                 Client_SetID( Client, Req->argv[0] );
249                 Client_SetToken( Client, atoi( Req->argv[1] ));
250                 Client_SetInfo( Client, Req->argv[2] );
251                 Client_SetHops( Client, 1 );
252
253                 Log( LOG_NOTICE, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), Client_Conn( Client ));
254
255                 Client_SetType( Client, CLIENT_SERVER );
256                 return CONNECTED;
257         }
258         else if( Client_Type( Client ) == CLIENT_SERVER )
259         {
260                 /* Neuer Server wird im Netz angekuendigt */
261
262                 /* Falsche Anzahl Parameter? */
263                 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
264
265                 /* Ist ein Server mit dieser ID bereits registriert? */
266                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
267
268                 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
269                 ptr = strchr( Req->argv[3] + 2, '[' );
270                 if( ! ptr ) ptr = Req->argv[3];
271
272                 /* Neue Client-Struktur anlegen */
273                 c = Client_NewRemoteServer( Client, Req->argv[0], atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr );
274                 if( ! c )
275                 {
276                         /* Neue Client-Struktur konnte nicht angelegt werden */
277                         Log( LOG_ALERT, "Can't allocate client structure for server! (on connection %d)", Client_Conn( Client ));
278                         Conn_Close( Client_Conn( Client ), "Can't allocate client structure for remote server!" );
279                         return DISCONNECTED;
280                 }
281
282                 /* Log-Meldung zusammenbauen und ausgeben */
283                 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) sprintf( str, "connected to %s, ", Req->prefix );
284                 else strcpy( str, "" );
285                 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": "" );
286                 
287                 return CONNECTED;
288         }
289         else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
290 } /* IRC_SERVER */
291
292
293 GLOBAL BOOLEAN IRC_NJOIN( CLIENT *Client, REQUEST *Req )
294 {
295         assert( Client != NULL );
296         assert( Req != NULL );
297
298         if( Client_Type( Client ) != CLIENT_SERVER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTEREDSERVER_MSG, Client_ID( Client ));
299
300         return CONNECTED;
301 } /* IRC_NJOIN */
302
303
304 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
305 {
306         CLIENT *intr_c, *c;
307
308         assert( Client != NULL );
309         assert( Req != NULL );
310
311         /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
312 #ifndef STRICT_RFC
313         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 )
314 #else
315         if( Client_Type( Client ) == CLIENT_UNKNOWN || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_USER  )
316 #endif
317         {
318                 /* User-Registrierung bzw. Nick-Aenderung */
319
320                 /* Falsche Anzahl Parameter? */
321                 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
322
323                 /* Ist der Client "restricted"? */
324                 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_RESTRICTED_MSG, Client_ID( Client ));
325
326                 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
327                  * wir nichts. So macht es das Original und mind. Snak hat probleme,
328                  * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
329 #ifndef STRICT_RFC
330                 if( strcmp( Client_ID( Client ), Req->argv[0] ) == 0 ) return CONNECTED;
331 #endif
332                 
333                 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
334                  * will nur die Gross- und Kleinschreibung aendern. Das darf
335                  * er natuerlich machen :-) */
336                 if( strcasecmp( Client_ID( Client ), Req->argv[0] ) != 0 )
337                 {
338                         if( ! Client_CheckNick( Client, Req->argv[0] )) return CONNECTED;
339                 }
340
341                 if( Client_Type( Client ) == CLIENT_USER )
342                 {
343                         /* Nick-Aenderung: allen mitteilen! */
344                         Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( Client ), Client_ID( Client ), Req->argv[0] );
345                         IRC_WriteStrRelated( Client, "NICK :%s", Req->argv[0] );
346                 }
347                 
348                 /* Client-Nick registrieren */
349                 Client_SetID( Client, Req->argv[0] );
350
351                 if( Client_Type( Client ) != CLIENT_USER )
352                 {
353                         /* Neuer Client */
354                         Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client_Conn( Client ));
355                         if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
356                         else Client_SetType( Client, CLIENT_GOTNICK );
357                 }
358                 return CONNECTED;
359         }
360         else if( Client_Type( Client ) == CLIENT_SERVER )
361         {
362                 /* Server fuehrt neuen Client ein */
363
364                 /* Falsche Anzahl Parameter? */
365                 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
366
367                 /* Nick ueberpruefen */
368                 c = Client_GetFromID( Req->argv[0] );
369                 if( c )
370                 {
371                         /* Der neue Nick ist auf diesem Server bereits registriert:
372                          * sowohl der neue, als auch der alte Client muessen nun
373                          * disconnectiert werden. */
374                         Log( LOG_ALERT, "Server %s introduces already registered nick %s!", Client_ID( Client ), Req->argv[0] );
375                         Kill_Nick( Req->argv[0] );
376                         return CONNECTED;
377                 }
378
379                 /* Server, zu dem der Client connectiert ist, suchen */
380                 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
381                 if( ! intr_c )
382                 {
383                         Log( LOG_ALERT, "Server %s introduces nick %s with unknown host server!?", Client_ID( Client ), Req->argv[0] );
384                         Kill_Nick( Req->argv[0] );
385                         return CONNECTED;
386                 }
387
388                 /* Neue Client-Struktur anlegen */
389                 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] );
390                 if( ! c )
391                 {
392                         /* Eine neue Client-Struktur konnte nicht angelegt werden.
393                          * Der Client muss disconnectiert werden, damit der Netz-
394                          * status konsistent bleibt. */
395                         Log( LOG_ALERT, "Can't allocate client structure! (on connection %d)", Client_Conn( Client ));
396                         Kill_Nick( Req->argv[0] );
397                         return CONNECTED;
398                 }
399
400                 Log( LOG_NOTICE, "User \"%s\" registered (via %s, on %s, %d hop%s).", Client_ID( c ), Client_ID( Client ), Client_ID( intr_c ), Client_Hops( c ), Client_Hops( c ) > 1 ? "s": "" );
401
402                 return CONNECTED;
403         }
404         else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
405 } /* IRC_NICK */
406
407
408 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
409 {
410         assert( Client != NULL );
411         assert( Req != NULL );
412
413 #ifndef STRICT_RFC
414         if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
415 #else
416         if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
417 #endif
418         {
419                 /* Falsche Anzahl Parameter? */
420                 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
421
422                 Client_SetUser( Client, Req->argv[0] );
423                 Client_SetInfo( Client, Req->argv[3] );
424
425                 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client_Conn( Client ));
426                 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
427                 else Client_SetType( Client, CLIENT_GOTUSER );
428                 return CONNECTED;
429         }
430         else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
431         {
432                 return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
433         }
434         else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
435 } /* IRC_USER */
436
437
438 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
439 {
440         assert( Client != NULL );
441         assert( Req != NULL );
442
443         if( Client_Type( Client ) != CLIENT_SERVER && Client_Type( Client ) != CLIENT_SERVICE )
444         {
445                 /* Falsche Anzahl Parameter? */
446                 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
447
448                 Conn_Close( Client_Conn( Client ), "Client wants to quit." );
449                 return DISCONNECTED;
450         }
451         else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
452 } /* IRC_QUIT */
453
454
455 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
456 {
457         assert( Client != NULL );
458         assert( Req != NULL );
459
460         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
461
462         /* Falsche Anzahl Parameter? */
463         if( Req->argc < 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOORIGIN_MSG, Client_ID( Client ));
464         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
465
466         return IRC_WriteStrClient( Client, Client_ThisServer( ), "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
467 } /* IRC_PING */
468
469
470 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
471 {
472         assert( Client != NULL );
473         assert( Req != NULL );
474
475         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
476
477         /* Falsche Anzahl Parameter? */
478         if( Req->argc < 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOORIGIN_MSG, Client_ID( Client ));
479         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
480
481         /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
482          * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
483
484         Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
485         return CONNECTED;
486 } /* IRC_PONG */
487
488
489 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
490 {
491         assert( Client != NULL );
492         assert( Req != NULL );
493
494         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
495
496         /* Falsche Anzahl Parameter? */
497         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
498
499         return Show_MOTD( Client );
500 } /* IRC_MOTD */
501
502
503 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
504 {
505         CLIENT *to;
506         
507         assert( Client != NULL );
508         assert( Req != NULL );
509
510         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
511
512         /* Falsche Anzahl Parameter? */
513         if( Req->argc == 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
514         if( Req->argc == 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
515         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
516
517         to = Client_Search( Req->argv[0] );
518         if( to )
519         {
520                 /* Okay, Ziel ist ein User */
521                 if( Client_Conn( Client ) >= 0 ) Conn_UpdateIdle( Client_Conn( Client ));
522                 return IRC_WriteStrClient( to, Client, "PRIVMSG %s :%s", Client_ID( to ), Req->argv[1] );
523         }
524
525         return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
526 } /* IRC_PRIVMSG */
527
528
529 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
530 {
531         CLIENT *to;
532
533         assert( Client != NULL );
534         assert( Req != NULL );
535
536         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
537
538         /* Falsche Anzahl Parameter? */
539         if( Req->argc != 2 ) return CONNECTED;
540
541         to = Client_Search( Req->argv[0] );
542         if( to )
543         {
544                 /* Okay, Ziel ist ein User */
545                 return IRC_WriteStrClient( to, Client, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
546         }
547
548         return CONNECTED;
549 } /* IRC_NOTICE */
550
551
552 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
553 {
554         CHAR x[2], new_modes[CLIENT_MODE_LEN], *ptr;
555         BOOLEAN set, ok;
556         CLIENT *c;
557         
558         assert( Client != NULL );
559         assert( Req != NULL );
560
561         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
562
563         /* Falsche Anzahl Parameter? */
564         if(( Req->argc > 2 ) || ( Req->argc < 1 )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
565
566         /* "Ziel-Client" suchen */
567         c = Client_Search( Req->argv[0] );
568
569         /* Wer ist der Anfragende? */
570         if( Client_Type( Client ) == CLIENT_USER )
571         {
572                 /* User: MODE ist nur fuer sich selber zulaessig */
573                 if( c != Client ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
574         }
575         else
576         {
577                 /* Server: gibt es den Client ueberhaupt? */
578                 if( ! c ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
579         }
580
581         /* Werden die Modes erfragt? */
582         if( Req->argc == 1 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( Client ));
583
584         ptr = Req->argv[1];
585
586         /* Sollen Modes gesetzt oder geloescht werden? */
587         if( *ptr == '+' ) set = TRUE;
588         else if( *ptr == '-' ) set = FALSE;
589         else return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
590
591         /* Reply-String mit Aenderungen vorbereiten */
592         if( set ) strcpy( new_modes, "+" );
593         else strcpy( new_modes, "-" );
594
595         ptr++;
596         ok = TRUE;
597         x[1] = '\0';
598         while( *ptr )
599         {
600                 x[0] = '\0';
601                 switch( *ptr )
602                 {
603                         case 'i':
604                                 /* invisible */
605                                 x[0] = 'i';
606                                 break;
607                         case 'r':
608                                 /* restricted (kann nur gesetzt werden) */
609                                 if( set ) x[0] = 'r';
610                                 else ok = IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_RESTRICTED_MSG, Client_ID( Client ));
611                                 break;
612                         case 'o':
613                                 /* operator (kann nur geloescht werden) */
614                                 if( ! set )
615                                 {
616                                         Client_SetOperByMe( Client, FALSE );
617                                         x[0] = 'o';
618                                 }
619                                 else ok = IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
620                                 break;
621                         default:
622                                 ok = IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
623                                 x[0] = '\0';
624                 }
625                 if( ! ok ) break;
626
627                 ptr++;
628                 if( ! x[0] ) continue;
629
630                 /* Okay, gueltigen Mode gefunden */
631                 if( set )
632                 {
633                         /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
634                         if( Client_ModeAdd( Client, x[0] )) strcat( new_modes, x );
635                 }
636                 else
637                 {
638                         /* Modes geloescht. Wenn der Client ihn hatte: merken */
639                         if( Client_ModeDel( Client, x[0] )) strcat( new_modes, x );
640                 }
641         }
642         
643         /* Geanderte Modes? */
644         if( new_modes[1] && ok )
645         {
646                 ok = IRC_WriteStrRelated( Client, "MODE %s :%s", Client_ID( Client ), new_modes );
647                 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( Client ), Client_Modes( Client ));
648         }
649         return ok;
650 } /* IRC_MODE */
651
652
653 GLOBAL BOOLEAN IRC_OPER( CLIENT *Client, REQUEST *Req )
654 {
655         INT i;
656         
657         assert( Client != NULL );
658         assert( Req != NULL );
659
660         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
661         
662         /* Falsche Anzahl Parameter? */
663         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
664
665         /* Operator suchen */
666         for( i = 0; i < Conf_Oper_Count; i++)
667         {
668                 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
669         }
670         if( i >= Conf_Oper_Count ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
671
672         /* Stimmt der Name und das Passwort? */
673         if(( strcmp( Conf_Oper[i].name, Req->argv[0] ) != 0 ) || ( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_PASSWDMISMATCH_MSG, Client_ID( Client ));
674         
675         if( ! Client_HasMode( Client, 'o' ))
676         {
677                 /* noch kein o-Mode gesetzt */
678                 Client_ModeAdd( Client, 'o' );
679                 if( ! IRC_WriteStrRelated( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
680         }
681
682         if( ! Client_OperByMe( Client )) Log( LOG_NOTICE, "User \"%s\" is now an IRC Operator.", Client_Mask( Client ));
683
684         Client_SetOperByMe( Client, TRUE );
685         return IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_YOUREOPER_MSG, Client_ID( Client ));
686 } /* IRC_OPER */
687
688
689 GLOBAL BOOLEAN IRC_DIE( CLIENT *Client, REQUEST *Req )
690 {
691         assert( Client != NULL );
692         assert( Req != NULL );
693
694         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
695
696         /* Falsche Anzahl Parameter? */
697         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
698
699         if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
700
701         Log( LOG_NOTICE, "Got DIE command from \"%s\", going down!", Client_Mask( Client ));
702         NGIRCd_Quit = TRUE;
703         return CONNECTED;
704 } /* IRC_DIE */
705
706
707 GLOBAL BOOLEAN IRC_RESTART( CLIENT *Client, REQUEST *Req )
708 {
709         assert( Client != NULL );
710         assert( Req != NULL );
711
712         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
713
714         /* Falsche Anzahl Parameter? */
715         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
716
717         if(( ! Client_HasMode( Client, 'o' )) || ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
718
719         Log( LOG_NOTICE, "Got RESTART command from \"%s\", going down!", Client_Mask( Client ));
720         NGIRCd_Restart = TRUE;
721         return CONNECTED;
722 } /* IRC_RESTART */
723
724
725 GLOBAL BOOLEAN IRC_NAMES( CLIENT *Client, REQUEST *Req )
726 {
727         CHAR rpl[COMMAND_LEN];
728         CLIENT *c;
729         
730         assert( Client != NULL );
731         assert( Req != NULL );
732
733         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
734
735         /* Falsche Anzahl Parameter? */
736         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
737
738         /* Noch alle User ausgeben, die in keinem Channel sind */
739         rpl[0] = '\0';
740         c = Client_First( );
741         while( c )
742         {
743                 if( Client_Type( c ) == CLIENT_USER )
744                 {
745                         /* Okay, das ist ein User */
746                         strcat( rpl, Client_ID( c ));
747                         strcat( rpl, " " );
748                 }
749
750                 /* Antwort zu lang? Splitten. */
751                 if( strlen( rpl ) > 480 )
752                 {
753                         if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
754                         if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
755                         rpl[0] = '\0';
756                 }
757                 
758                 c = Client_Next( c );
759         }
760         if( rpl[0] )
761         {
762                 /* es wurden User gefunden */
763                 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
764                 if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_NAMREPLY_MSG, Client_ID( Client ), "*", "*", rpl )) return DISCONNECTED;
765         }
766         return IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_ENDOFNAMES_MSG, Client_ID( Client ), "*" );
767 } /* IRC_NAMES */
768
769
770 GLOBAL BOOLEAN IRC_ISON( CLIENT *Client, REQUEST *Req )
771 {
772         CHAR rpl[COMMAND_LEN];
773         CLIENT *c;
774         CHAR *ptr;
775         INT i;
776         
777         assert( Client != NULL );
778         assert( Req != NULL );
779
780         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
781
782         /* Falsche Anzahl Parameter? */
783         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
784
785         strcpy( rpl, RPL_ISON_MSG );
786         for( i = 0; i < Req->argc; i++ )
787         {
788                 ptr = strtok( Req->argv[i], " " );
789                 while( ptr )
790                 {
791                         ngt_TrimStr( ptr );
792                         c = Client_GetFromID( ptr );
793                         if( c && ( Client_Type( c ) == CLIENT_USER ))
794                         {
795                                 /* Dieser Nick ist "online" */
796                                 strcat( rpl, ptr );
797                                 strcat( rpl, " " );
798                         }
799                         ptr = strtok( NULL, " " );
800                 }
801         }
802         if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
803
804         return IRC_WriteStrClient( Client, Client_ThisServer( ), rpl, Client_ID( Client ) );
805 } /* IRC_ISON */
806
807
808 GLOBAL BOOLEAN IRC_WHOIS( CLIENT *Client, REQUEST *Req )
809 {
810         CLIENT *c;
811         
812         assert( Client != NULL );
813         assert( Req != NULL );
814
815         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
816
817         /* Falsche Anzahl Parameter? */
818         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
819
820         /* Client suchen */
821         c = Client_GetFromID( Req->argv[0] );
822         if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
823         
824         /* Nick, User und Name */
825         if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_WHOISUSER_MSG, Client_ID( Client ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
826
827         /* Server */
828         if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_WHOISSERVER_MSG, Client_ID( Client ), Client_ID( c ), Client_ID( Client_Introducer( c )), Client_Info( Client_Introducer( c )))) return DISCONNECTED;
829
830         /* IRC-Operator? */
831         if( Client_HasMode( c, 'o' ))
832         {
833                 if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_WHOISOPERATOR_MSG, Client_ID( Client ), Client_ID( c ))) return DISCONNECTED;
834         }
835
836         /* Idle (nur lokale Clients) */
837         if( Client_Conn( c ) > NONE )
838         {
839                 if( ! IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_WHOISIDLE_MSG, Client_ID( Client ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
840         }
841
842         /* End of Whois */
843         return IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_ENDOFWHOIS_MSG, Client_ID( Client ), Client_ID( c ));
844 } /* IRC_WHOIS */
845
846
847 GLOBAL BOOLEAN IRC_USERHOST( CLIENT *Client, REQUEST *Req )
848 {
849         CHAR rpl[COMMAND_LEN];
850         CLIENT *c;
851         INT max, i;
852
853         assert( Client != NULL );
854         assert( Req != NULL );
855
856         if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOTREGISTERED_MSG, Client_ID( Client ));
857
858         /* Falsche Anzahl Parameter? */
859         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
860
861         if( Req->argc > 5 ) max = 5;
862         else max = Req->argc;
863         
864         strcpy( rpl, RPL_USERHOST_MSG );
865         for( i = 0; i < max; i++ )
866         {
867                 c = Client_GetFromID( Req->argv[i] );
868                 if( c && ( Client_Type( c ) == CLIENT_USER ))
869                 {
870                         /* Dieser Nick ist "online" */
871                         strcat( rpl, Client_ID( c ));
872                         if( Client_HasMode( c, 'o' )) strcat( rpl, "*" );
873                         strcat( rpl, "=" );
874                         if( Client_HasMode( c, 'a' )) strcat( rpl, "-" );
875                         else strcat( rpl, "+" );
876                         strcat( rpl, Client_User( c ));
877                         strcat( rpl, "@" );
878                         strcat( rpl, Client_Hostname( c ));
879                         strcat( rpl, " " );
880                 }
881         }
882         if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
883
884         return IRC_WriteStrClient( Client, Client_ThisServer( ), rpl, Client_ID( Client ) );
885 } /* IRC_USERHOST */
886
887
888 GLOBAL BOOLEAN IRC_ERROR( CLIENT *Client, REQUEST *Req )
889 {
890         assert( Client != NULL );
891         assert( Req != NULL );
892
893         if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
894         else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
895
896         return CONNECTED;
897 } /* IRC_ERROR */
898
899
900 LOCAL BOOLEAN Hello_User( CLIENT *Client )
901 {
902         assert( Client != NULL );
903
904         /* Passwort ueberpruefen */
905         if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
906         {
907                 /* Falsches Passwort */
908                 Log( LOG_ALERT, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
909                 Conn_Close( Client_Conn( Client ), "Bad password!" );
910                 return DISCONNECTED;
911         }
912
913         Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
914
915         IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ));
916         IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
917         IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr );
918         IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
919
920         Client_SetType( Client, CLIENT_USER );
921
922         return Show_MOTD( Client );
923 } /* Hello_User */
924
925
926 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
927 {
928         BOOLEAN ok;
929         CHAR line[127];
930         FILE *fd;
931         
932         assert( Client != NULL );
933
934         fd = fopen( Conf_MotdFile, "r" );
935         if( ! fd )
936         {
937                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
938                 return IRC_WriteStrClient( Client, Client_ThisServer( ), ERR_NOMOTD_MSG, Client_ID( Client ) );
939         }
940         
941         IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
942         while( TRUE )
943         {
944                 if( ! fgets( line, 126, fd )) break;
945                 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
946                 IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_MOTD_MSG, Client_ID( Client ), line );
947         }
948         ok = IRC_WriteStrClient( Client, Client_ThisServer( ), RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
949
950         fclose( fd );
951         
952         return ok;
953 } /* Show_MOTD */
954
955
956 LOCAL VOID Kill_Nick( CHAR *Nick )
957 {
958         Log( LOG_ALERT, "User(s) with nick \"%s\" will be disconnected!", Nick );
959         /* FIXME */
960         Log( LOG_ALERT, "[Kill_Nick() not implemented - OOOPS!]" );
961 } /* Kill_Nick */
962
963
964 /* -eof- */