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