]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
- neue Defines fuer max. Anzahl Server und Operatoren.
[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.18 2002/01/02 02:44:36 alex Exp $
13  *
14  * irc.c: IRC-Befehle
15  *
16  * $Log: irc.c,v $
17  * Revision 1.18  2002/01/02 02:44:36  alex
18  * - neue Defines fuer max. Anzahl Server und Operatoren.
19  *
20  * Revision 1.17  2001/12/31 15:33:13  alex
21  * - neuer Befehl NAMES, kleinere Bugfixes.
22  * - Bug bei PING behoben: war zu restriktiv implementiert :-)
23  *
24  * Revision 1.16  2001/12/31 02:18:51  alex
25  * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
26  * - neuen Header "defines.h" mit (fast) allen Konstanten.
27  * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
28  *
29  * Revision 1.15  2001/12/30 19:26:11  alex
30  * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
31  *
32  * Revision 1.14  2001/12/30 11:42:00  alex
33  * - der Server meldet nun eine ordentliche "Start-Zeit".
34  *
35  * Revision 1.13  2001/12/29 03:10:06  alex
36  * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
37  * - neue configure-Optione "--enable-strict-rfc".
38  *
39  * Revision 1.12  2001/12/27 19:17:26  alex
40  * - neue Befehle PRIVMSG, NOTICE, PING.
41  *
42  * Revision 1.11  2001/12/27 16:55:41  alex
43  * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
44  *
45  * Revision 1.10  2001/12/26 22:48:53  alex
46  * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
47  *
48  * Revision 1.9  2001/12/26 14:45:37  alex
49  * - "Code Cleanups".
50  *
51  * Revision 1.8  2001/12/26 03:21:46  alex
52  * - PING/PONG-Befehle implementiert,
53  * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
54  *
55  * Revision 1.7  2001/12/25 23:25:18  alex
56  * - und nochmal Aenderungen am Logging ;-)
57  *
58  * Revision 1.6  2001/12/25 23:13:33  alex
59  * - Debug-Meldungen angepasst.
60  *
61  * Revision 1.5  2001/12/25 22:02:42  alex
62  * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
63  *
64  * Revision 1.4  2001/12/25 19:19:30  alex
65  * - bessere Fehler-Abfragen, diverse Bugfixes.
66  * - Nicks werden nur einmal vergeben :-)
67  * - /MOTD wird unterstuetzt.
68  *
69  * Revision 1.3  2001/12/24 01:34:06  alex
70  * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
71  * - MOTD-Ausgabe begonnen zu implementieren.
72  *
73  * Revision 1.2  2001/12/23 21:57:16  alex
74  * - erste IRC-Befehle zu implementieren begonnen.
75  *
76  * Revision 1.1  2001/12/14 08:13:43  alex
77  * - neues Modul begonnen :-)
78  */
79
80
81 #include <portab.h>
82 #include "global.h"
83
84 #include <imp.h>
85 #include <assert.h>
86 #include <errno.h>
87 #include <stdarg.h>
88 #include <stdio.h>
89 #include <string.h>
90
91 #include "ngircd.h"
92 #include "client.h"
93 #include "conf.h"
94 #include "conn.h"
95 #include "log.h"
96 #include "messages.h"
97 #include "parse.h"
98 #include "tool.h"
99
100 #include <exp.h>
101 #include "irc.h"
102
103
104 #define CONNECTED TRUE
105 #define DISCONNECTED FALSE
106
107
108 LOCAL BOOLEAN Check_Valid_User( CLIENT *Client );
109
110 LOCAL BOOLEAN Hello_User( CLIENT *Client );
111 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
112
113
114 GLOBAL VOID IRC_Init( VOID )
115 {
116 } /* IRC_Init */
117
118
119 GLOBAL VOID IRC_Exit( VOID )
120 {
121 } /* IRC_Exit */
122
123
124 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
125 {
126         /* Text an Clients, lokal bzw. remote, senden. */
127
128         CHAR buffer[1000];
129         BOOLEAN ok = CONNECTED;
130         CONN_ID send_to;
131         va_list ap;
132
133         assert( Client != NULL );
134         assert( Format != NULL );
135
136         va_start( ap, Format );
137         vsnprintf( buffer, 1000, Format, ap );
138         va_end( ap );
139
140         if( Client->conn_id != NONE ) send_to = Client->conn_id;
141         else send_to = Client->introducer->conn_id;
142
143         if( Prefix ) ok = Conn_WriteStr( Client->conn_id, ":%s %s", Client_GetID( Prefix ), buffer );
144         else ok = Conn_WriteStr( Client->conn_id, buffer );
145
146         return ok;
147 } /* IRC_WriteStrClient */
148
149
150 GLOBAL BOOLEAN IRC_WriteStrRelated( CLIENT *Client, CHAR *Format, ... )
151 {
152         CHAR buffer[1000];
153         BOOLEAN ok = CONNECTED;
154         va_list ap;
155
156         assert( Client != NULL );
157         assert( Format != NULL );
158
159         va_start( ap, Format );
160         vsnprintf( buffer, 1000, Format, ap );
161         va_end( ap );
162
163         /* an den Client selber */
164         ok = IRC_WriteStrClient( Client, Client, buffer );
165
166         return ok;
167 } /* IRC_WriteStrRelated */
168
169
170 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
171 {
172         assert( Client != NULL );
173         assert( Req != NULL );
174
175         if( Client->type == CLIENT_UNKNOWN )
176         {
177                 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client->conn_id );
178                 return IRC_WriteStrClient( Client, This_Server, ERR_UNKNOWNCOMMAND_MSG, Client_Nick( Client ), Req->command );
179         }
180         else return IRC_WriteStrClient( Client, This_Server, ERR_ALREADYREGISTRED_MSG, Client_Nick( Client ));
181 } /* IRC_PASS */
182
183
184 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
185 {
186         assert( Client != NULL );
187         assert( Req != NULL );
188
189         /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
190 #ifndef STRICT_RFC
191         if( Client->type == CLIENT_UNKNOWN || Client->type == CLIENT_GOTPASS || Client->type == CLIENT_GOTNICK || Client->type == CLIENT_GOTUSER || Client->type == CLIENT_USER )
192 #else
193         if( Client->type == CLIENT_UNKNOWN || Client->type == CLIENT_GOTPASS || Client->type == CLIENT_GOTNICK || Client->type == CLIENT_USER  )
194 #endif
195         {
196                 /* Falsche Anzahl Parameter? */
197                 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
198
199                 /* Ist der Client "restricted"? */
200                 if( strchr( Client->modes, 'r' )) return IRC_WriteStrClient( Client, This_Server, ERR_RESTRICTED_MSG, Client_Nick( Client ));
201
202                 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
203                  * wir nichts. So macht es das Original und mind. Snak hat probleme,
204                  * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
205 #ifndef STRICT_RFC
206                 if( strcasecmp( Client->nick, Req->argv[0] ) == 0 ) return CONNECTED;
207 #endif
208                 
209                 /* pruefen, ob Nick bereits vergeben */
210                 if( ! Client_CheckNick( Client, Req->argv[0] )) return CONNECTED;
211
212                 if( Client->type == CLIENT_USER )
213                 {
214                         /* Nick-Aenderung: allen mitteilen! */
215                         Log( LOG_INFO, "User \"%s!%s@%s\" changed nick: \"%s\" -> \"%s\".", Client->nick, Client->user, Client->host, Client->nick, Req->argv[0] );
216                         IRC_WriteStrRelated( Client, "NICK :%s", Req->argv[0] );
217                 }
218                 
219                 /* Client-Nick registrieren */
220                 strcpy( Client->nick, Req->argv[0] );
221
222                 if( Client->type != CLIENT_USER )
223                 {
224                         /* Neuer Client */
225                         Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client->conn_id );
226                         if( Client->type == CLIENT_GOTUSER ) return Hello_User( Client );
227                         else Client->type = CLIENT_GOTNICK;
228                 }
229                 return CONNECTED;
230         }
231         else return IRC_WriteStrClient( Client, This_Server, ERR_ALREADYREGISTRED_MSG, Client_Nick( Client ));
232 } /* IRC_NICK */
233
234
235 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
236 {
237         assert( Client != NULL );
238         assert( Req != NULL );
239
240 #ifndef STRICT_RFC
241         if( Client->type == CLIENT_GOTNICK || Client->type == CLIENT_GOTPASS || Client->type == CLIENT_UNKNOWN )
242 #else
243         if( Client->type == CLIENT_GOTNICK || Client->type == CLIENT_GOTPASS )
244 #endif
245         {
246                 /* Falsche Anzahl Parameter? */
247                 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
248
249                 strncpy( Client->user, Req->argv[0], CLIENT_USER_LEN );
250                 Client->user[CLIENT_USER_LEN - 1] = '\0';
251                 strncpy( Client->name, Req->argv[3], CLIENT_NAME_LEN );
252                 Client->name[CLIENT_NAME_LEN - 1] = '\0';
253
254                 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client->conn_id );
255                 if( Client->type == CLIENT_GOTNICK ) return Hello_User( Client );
256                 else Client->type = CLIENT_GOTUSER;
257                 return CONNECTED;
258         }
259         else if( Client->type == CLIENT_USER || Client->type == CLIENT_SERVER || Client->type == CLIENT_SERVICE )
260         {
261                 return IRC_WriteStrClient( Client, This_Server, ERR_ALREADYREGISTRED_MSG, Client_Nick( Client ));
262         }
263         else return IRC_WriteStrClient( Client, This_Server, ERR_NOTREGISTERED_MSG, Client_Nick( Client ));
264 } /* IRC_USER */
265
266
267 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
268 {
269         assert( Client != NULL );
270         assert( Req != NULL );
271
272         if( Client->type != CLIENT_SERVER && Client->type != CLIENT_SERVICE )
273         {
274                 /* Falsche Anzahl Parameter? */
275                 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
276
277                 Conn_Close( Client->conn_id, "Client wants to quit." );
278                 return DISCONNECTED;
279         }
280         else return IRC_WriteStrClient( Client, This_Server, ERR_NOTREGISTERED_MSG, Client_Nick( Client ));
281 } /* IRC_QUIT */
282
283
284 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
285 {
286         assert( Client != NULL );
287         assert( Req != NULL );
288
289         if( ! Check_Valid_User( Client )) return CONNECTED;
290
291         /* Falsche Anzahl Parameter? */
292         if( Req->argc < 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NOORIGIN_MSG, Client_Nick( Client ));
293         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
294
295         return IRC_WriteStrClient( Client, This_Server, "PONG %s :%s", Client_Nick( This_Server), Client_Nick( Client ));
296 } /* IRC_PING */
297
298
299 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
300 {
301         assert( Client != NULL );
302         assert( Req != NULL );
303
304         if( ! Check_Valid_User( Client )) return CONNECTED;
305
306         /* Falsche Anzahl Parameter? */
307         if( Req->argc < 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NOORIGIN_MSG, Client_Nick( Client ));
308         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
309
310         /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
311          * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
312
313         Log( LOG_DEBUG, "Connection %d: received PONG.", Client->conn_id );
314         return CONNECTED;
315 } /* IRC_PONG */
316
317
318 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
319 {
320         assert( Client != NULL );
321         assert( Req != NULL );
322
323         if( ! Check_Valid_User( Client )) return CONNECTED;
324
325         /* Falsche Anzahl Parameter? */
326         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
327
328         return Show_MOTD( Client );
329 } /* IRC_MOTD */
330
331
332 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
333 {
334         CLIENT *to;
335         
336         assert( Client != NULL );
337         assert( Req != NULL );
338
339         if( ! Check_Valid_User( Client )) return CONNECTED;
340
341         /* Falsche Anzahl Parameter? */
342         if( Req->argc == 0 ) return IRC_WriteStrClient( Client, This_Server, ERR_NORECIPIENT_MSG, Client_Nick( Client ), Req->command );
343         if( Req->argc == 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NOTEXTTOSEND_MSG, Client_Nick( Client ));
344         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
345
346         to = Client_Search( Req->argv[0] );
347         if( to )
348         {
349                 /* Okay, Ziel ist ein User */
350                 if( Client->conn_id >= 0 ) Conn_UpdateIdle( Client->conn_id );
351                 return IRC_WriteStrClient( to, Client, "PRIVMSG %s :%s", to->nick, Req->argv[1] );
352         }
353
354         return IRC_WriteStrClient( Client, This_Server, ERR_NOSUCHNICK_MSG, Client_Nick( Client ), Req->argv[0] );
355 } /* IRC_PRIVMSG */
356
357
358 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
359 {
360         CLIENT *to;
361
362         assert( Client != NULL );
363         assert( Req != NULL );
364
365         if( ! Check_Valid_User( Client )) return CONNECTED;
366
367         /* Falsche Anzahl Parameter? */
368         if( Req->argc != 2 ) return CONNECTED;
369
370         to = Client_Search( Req->argv[0] );
371         if( to )
372         {
373                 /* Okay, Ziel ist ein User */
374                 return IRC_WriteStrClient( to, Client, "NOTICE %s :%s", to->nick, Req->argv[1] );
375         }
376
377         return CONNECTED;
378 } /* IRC_NOTICE */
379
380
381 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
382 {
383         CHAR x[2], new_modes[CLIENT_MODE_LEN], *ptr, *p;
384         BOOLEAN set, ok;
385         
386         assert( Client != NULL );
387         assert( Req != NULL );
388
389         if( ! Check_Valid_User( Client )) return CONNECTED;
390
391         /* Falsche Anzahl Parameter? */
392         if(( Req->argc > 2 ) || ( Req->argc < 1 )) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
393
394         /* MODE ist nur fuer sich selber zulaessig */
395         if( Client_Search( Req->argv[0] ) != Client ) return IRC_WriteStrClient( Client, This_Server, ERR_USERSDONTMATCH_MSG, Client_Nick( Client ));
396
397         /* Werden die Modes erfragt? */
398         if( Req->argc == 1 ) return IRC_WriteStrClient( Client, This_Server, RPL_UMODEIS_MSG, Client_Nick( Client ), Client->modes );
399
400         ptr = Req->argv[1];
401
402         /* Sollen Modes gesetzt oder geloescht werden? */
403         if( *ptr == '+' ) set = TRUE;
404         else if( *ptr == '-' ) set = FALSE;
405         else return IRC_WriteStrClient( Client, This_Server, ERR_UMODEUNKNOWNFLAG_MSG, Client_Nick( Client ));
406
407         /* Reply-String mit Aenderungen vorbereiten */
408         if( set ) strcpy( new_modes, "+" );
409         else strcpy( new_modes, "-" );
410
411         ptr++;
412         ok = TRUE;
413         x[1] = '\0';
414         while( *ptr )
415         {
416                 x[0] = '\0';
417                 switch( *ptr )
418                 {
419                         case 'i':
420                                 /* invisible */
421                                 x[0] = 'i';
422                                 break;
423                         case 'r':
424                                 /* restricted (kann nur gesetzt werden) */
425                                 if( set ) x[0] = 'r';
426                                 else ok = IRC_WriteStrClient( Client, This_Server, ERR_RESTRICTED_MSG, Client_Nick( Client ));
427                                 break;
428                         case 'o':
429                                 /* operator (kann nur geloescht werden) */
430                                 if( ! set )
431                                 {
432                                         Client->oper_by_me = FALSE;
433                                         x[0] = 'o';
434                                 }
435                                 else ok = IRC_WriteStrClient( Client, This_Server, ERR_UMODEUNKNOWNFLAG_MSG, Client_Nick( Client ));
436                                 break;
437                         default:
438                                 ok = IRC_WriteStrClient( Client, This_Server, ERR_UMODEUNKNOWNFLAG_MSG, Client_Nick( Client ));
439                                 x[0] = '\0';
440                 }
441                 if( ! ok ) break;
442
443                 ptr++;
444                 if( ! x[0] ) continue;
445
446                 /* Okay, gueltigen Mode gefunden */
447                 if( set )
448                 {
449                         /* Modes sollen gesetzt werden */
450                         if( ! strchr( Client->modes, x[0] ))
451                         {
452                                 /* Client hat den Mode noch nicht -> setzen */
453                                 strcat( Client->modes, x );
454                                 strcat( new_modes, x );
455                         }
456                 }
457                 else
458                 {
459                         /* Modes sollen geloescht werden */
460                         p = strchr( Client->modes, x[0] );
461                         if( p )
462                         {
463                                 /* Client hat den Mode -> loeschen */
464                                 while( *p )
465                                 {
466                                         *p = *(p + 1);
467                                         p++;
468                                 }
469                                 strcat( new_modes, x );
470                         }
471                 }
472         }
473         
474         /* Geanderte Modes? */
475         if( new_modes[1] && ok )
476         {
477                 ok = IRC_WriteStrRelated( Client, "MODE %s :%s", Client->nick, new_modes );
478                 Log( LOG_DEBUG, "User \"%s!%s@%s\": Mode change, now \"%s\".", Client->nick, Client->user, Client->host, Client->modes );
479         }
480         return ok;
481 } /* IRC_MODE */
482
483
484 GLOBAL BOOLEAN IRC_OPER( CLIENT *Client, REQUEST *Req )
485 {
486         INT i;
487         
488         assert( Client != NULL );
489         assert( Req != NULL );
490
491         if( ! Check_Valid_User( Client )) return CONNECTED;
492         
493         /* Falsche Anzahl Parameter? */
494         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
495
496         /* Operator suchen */
497         for( i = 0; i < Conf_Oper_Count; i++)
498         {
499                 if( Conf_Oper[i].name[0] && Conf_Oper[i].pwd[0] && ( strcmp( Conf_Oper[i].name, Req->argv[0] ) == 0 )) break;
500         }
501         if( i >= Conf_Oper_Count ) return IRC_WriteStrClient( Client, This_Server, ERR_PASSWDMISMATCH_MSG, Client_Nick( Client ));
502
503         /* Stimmt der Name und das Passwort? */
504         if(( strcmp( Conf_Oper[i].name, Req->argv[0] ) != 0 ) || ( strcmp( Conf_Oper[i].pwd, Req->argv[1] ) != 0 )) return IRC_WriteStrClient( Client, This_Server, ERR_PASSWDMISMATCH_MSG, Client_Nick( Client ));
505         
506         if( ! strchr( Client->modes, 'o' ))
507         {
508                 /* noch kein o-Mode gesetzt */
509                 strcat( Client->modes, "o" );
510                 if( ! IRC_WriteStrRelated( Client, "MODE %s :+o", Client->nick )) return DISCONNECTED;
511         }
512
513         if( ! Client->oper_by_me ) Log( LOG_NOTICE, "User \"%s!%s@%s\" is now an IRC Operator.", Client->nick, Client->user, Client->host );
514
515         Client->oper_by_me = TRUE;
516         return IRC_WriteStrClient( Client, This_Server, RPL_YOUREOPER_MSG, Client_Nick( Client ));
517 } /* IRC_OPER */
518
519
520 GLOBAL BOOLEAN IRC_DIE( CLIENT *Client, REQUEST *Req )
521 {
522         assert( Client != NULL );
523         assert( Req != NULL );
524
525         if( ! Check_Valid_User( Client )) return CONNECTED;
526
527         /* Falsche Anzahl Parameter? */
528         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
529
530         if(( ! strchr( Client->modes, 'o' )) || ( ! Client->oper_by_me )) return IRC_WriteStrClient( Client, This_Server, ERR_NOPRIVILEGES_MSG, Client_Nick( Client ));
531
532         Log( LOG_NOTICE, "Got DIE command from \"%s!%s@%s\", going down!", Client->nick, Client->user, Client->host );
533         NGIRCd_Quit = TRUE;
534         return CONNECTED;
535 } /* IRC_DIE */
536
537
538 GLOBAL BOOLEAN IRC_RESTART( CLIENT *Client, REQUEST *Req )
539 {
540         assert( Client != NULL );
541         assert( Req != NULL );
542
543         if( ! Check_Valid_User( Client )) return CONNECTED;
544
545         /* Falsche Anzahl Parameter? */
546         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
547
548         if(( ! strchr( Client->modes, 'o' )) || ( ! Client->oper_by_me )) return IRC_WriteStrClient( Client, This_Server, ERR_NOPRIVILEGES_MSG, Client_Nick( Client ));
549
550         Log( LOG_NOTICE, "Got RESTART command from \"%s!%s@%s\", going down!", Client->nick, Client->user, Client->host );
551         NGIRCd_Restart = TRUE;
552         return CONNECTED;
553 } /* IRC_RESTART */
554
555
556 GLOBAL BOOLEAN IRC_NAMES( CLIENT *Client, REQUEST *Req )
557 {
558         CHAR rpl[COMMAND_LEN];
559         CLIENT *c;
560         
561         assert( Client != NULL );
562         assert( Req != NULL );
563
564         if( ! Check_Valid_User( Client )) return CONNECTED;
565
566         /* Falsche Anzahl Parameter? */
567         if( Req->argc != 0 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
568
569         /* Noch alle User ausgeben, die in keinem Channel sind */
570         rpl[0] = '\0';
571         c = Client_First( );
572         while( c )
573         {
574                 if( c->type == CLIENT_USER )
575                 {
576                         /* Okay, das ist ein User */
577                         strcat( rpl, Client_Nick( c ));
578                         strcat( rpl, " " );
579                 }
580
581                 /* Antwort zu lang? Splitten. */
582                 if( strlen( rpl ) > 480 )
583                 {
584                         if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
585                         if( ! IRC_WriteStrClient( Client, This_Server, RPL_NAMREPLY_MSG, Client_Nick( Client ), "*", "*", rpl )) return DISCONNECTED;
586                         rpl[0] = '\0';
587                 }
588                 
589                 c = Client_Next( c );
590         }
591         if( rpl[0] )
592         {
593                 /* es wurden User gefunden */
594                 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
595                 if( ! IRC_WriteStrClient( Client, This_Server, RPL_NAMREPLY_MSG, Client_Nick( Client ), "*", "*", rpl )) return DISCONNECTED;
596         }
597         return IRC_WriteStrClient( Client, This_Server, RPL_ENDOFNAMES_MSG, Client_Nick( Client ), "*" );
598 } /* IRC_NAMES */
599
600
601 GLOBAL BOOLEAN IRC_ISON( CLIENT *Client, REQUEST *Req )
602 {
603         CHAR rpl[COMMAND_LEN];
604         CLIENT *c;
605         CHAR *ptr;
606         INT i;
607         
608         assert( Client != NULL );
609         assert( Req != NULL );
610
611         if( ! Check_Valid_User( Client )) return CONNECTED;
612
613         /* Falsche Anzahl Parameter? */
614         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
615
616         strcpy( rpl, RPL_ISON_MSG );
617         for( i = 0; i < Req->argc; i++ )
618         {
619                 ptr = strtok( Req->argv[i], " " );
620                 while( ptr )
621                 {
622                         ngt_TrimStr( ptr );
623                         c = Client_GetFromNick( ptr );
624                         if( c && ( c->type == CLIENT_USER ))
625                         {
626                                 /* Dieser Nick ist "online" */
627                                 strcat( rpl, ptr );
628                                 strcat( rpl, " " );
629                         }
630                         ptr = strtok( NULL, " " );
631                 }
632         }
633         if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
634
635         return IRC_WriteStrClient( Client, This_Server, rpl, Client->nick );
636 } /* IRC_ISON */
637
638
639 GLOBAL BOOLEAN IRC_WHOIS( CLIENT *Client, REQUEST *Req )
640 {
641         CLIENT *c;
642         
643         assert( Client != NULL );
644         assert( Req != NULL );
645
646         if( ! Check_Valid_User( Client )) return CONNECTED;
647
648         /* Falsche Anzahl Parameter? */
649         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
650
651         /* Client suchen */
652         c = Client_GetFromNick( Req->argv[0] );
653         if(( ! c ) || ( c->type != CLIENT_USER )) return IRC_WriteStrClient( Client, This_Server, ERR_NOSUCHNICK_MSG, Client_Nick( Client ), Req->argv[0] );
654         
655         /* Nick, User und Name */
656         if( ! IRC_WriteStrClient( Client, This_Server, RPL_WHOISUSER_MSG, Client_Nick( Client ), c->nick, c->user, c->host, c->name )) return DISCONNECTED;
657
658         /* Server */
659         if( ! IRC_WriteStrClient( Client, This_Server, RPL_WHOISSERVER_MSG, Client_Nick( Client ), c->nick, c->introducer->nick, c->introducer->info )) return DISCONNECTED;
660
661         /* IRC-Operator? */
662         if( strchr( c->modes, 'o' ))
663         {
664                 if( ! IRC_WriteStrClient( Client, This_Server, RPL_WHOISOPERATOR_MSG, Client_Nick( Client ), c->nick )) return DISCONNECTED;
665         }
666
667         /* Idle (nur lokale Clients) */
668         if( c->conn_id >= 0 )
669         {
670                 if( ! IRC_WriteStrClient( Client, This_Server, RPL_WHOISIDLE_MSG, Client_Nick( Client ), c->nick, Conn_GetIdle( c->conn_id ))) return DISCONNECTED;
671         }
672
673         /* End of Whois */
674         return IRC_WriteStrClient( Client, This_Server, RPL_ENDOFWHOIS_MSG, Client_Nick( Client ), c->nick );
675 } /* IRC_WHOIS */
676
677
678 GLOBAL BOOLEAN IRC_USERHOST( CLIENT *Client, REQUEST *Req )
679 {
680         CHAR rpl[COMMAND_LEN];
681         CLIENT *c;
682         INT max, i;
683
684         assert( Client != NULL );
685         assert( Req != NULL );
686
687         if( ! Check_Valid_User( Client )) return CONNECTED;
688
689         /* Falsche Anzahl Parameter? */
690         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Nick( Client ), Req->command );
691
692         if( Req->argc > 5 ) max = 5;
693         else max = Req->argc;
694         
695         strcpy( rpl, RPL_USERHOST_MSG );
696         for( i = 0; i < max; i++ )
697         {
698                 c = Client_GetFromNick( Req->argv[i] );
699                 if( c && ( c->type == CLIENT_USER ))
700                 {
701                         /* Dieser Nick ist "online" */
702                         strcat( rpl, c->nick );
703                         if( strchr( c->modes, 'o' )) strcat( rpl, "*" );
704                         strcat( rpl, "=" );
705                         if( strchr( c->modes, 'a' )) strcat( rpl, "-" );
706                         else strcat( rpl, "+" );
707                         strcat( rpl, c->user );
708                         strcat( rpl, "@" );
709                         strcat( rpl, c->host );
710                         strcat( rpl, " " );
711                 }
712         }
713         if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
714
715         return IRC_WriteStrClient( Client, This_Server, rpl, Client->nick );
716 } /* IRC_USERHOST */
717
718
719 GLOBAL BOOLEAN IRC_ERROR( CLIENT *Client, REQUEST *Req )
720 {
721         assert( Client != NULL );
722         assert( Req != NULL );
723
724         if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s!%s@%s\"!", Client_Nick( Client ), Client->user, Client->host );
725         else Log( LOG_NOTICE, "Got ERROR from \"%s!%s@%s\": %s!", Client_Nick( Client ), Client->user, Client->host, Req->argv[0] );
726
727         return CONNECTED;
728 } /* IRC_ERROR */
729
730
731 LOCAL BOOLEAN Check_Valid_User( CLIENT *Client )
732 {
733         assert( Client != NULL );
734
735         if( Client->type != CLIENT_USER )
736         {
737                 IRC_WriteStrClient( Client, This_Server, ERR_NOTREGISTERED_MSG, Client_Nick( Client ));
738                 return FALSE;
739         }
740         else return TRUE;
741 } /* Check_Valid_User */
742
743
744 LOCAL BOOLEAN Hello_User( CLIENT *Client )
745 {
746         assert( Client != NULL );
747         assert( Client->nick[0] );
748         
749         Log( LOG_NOTICE, "User \"%s!%s@%s\" (%s) registered (connection %d).", Client->nick, Client->user, Client->host, Client->name, Client->conn_id );
750
751         IRC_WriteStrClient( Client, This_Server, RPL_WELCOME_MSG, Client->nick, Client_GetID( Client ));
752         IRC_WriteStrClient( Client, This_Server, RPL_YOURHOST_MSG, Client->nick, This_Server->nick );
753         IRC_WriteStrClient( Client, This_Server, RPL_CREATED_MSG, Client->nick, NGIRCd_StartStr );
754         IRC_WriteStrClient( Client, This_Server, RPL_MYINFO_MSG, Client->nick, This_Server->nick );
755
756         Client->type = CLIENT_USER;
757
758         return Show_MOTD( Client );
759 } /* Hello_User */
760
761
762 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
763 {
764         BOOLEAN ok;
765         CHAR line[127];
766         FILE *fd;
767         
768         assert( Client != NULL );
769         assert( Client->nick[0] );
770
771         fd = fopen( Conf_MotdFile, "r" );
772         if( ! fd )
773         {
774                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
775                 return IRC_WriteStrClient( Client, This_Server, ERR_NOMOTD_MSG, Client->nick );
776         }
777         
778         IRC_WriteStrClient( Client, This_Server, RPL_MOTDSTART_MSG, Client->nick, This_Server->nick );
779         while( TRUE )
780         {
781                 if( ! fgets( line, 126, fd )) break;
782                 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
783                 IRC_WriteStrClient( Client, This_Server, RPL_MOTD_MSG, Client->nick, line );
784         }
785         ok = IRC_WriteStrClient( Client, This_Server, RPL_ENDOFMOTD_MSG, Client->nick );
786
787         fclose( fd );
788         
789         return ok;
790 } /* Show_MOTD */
791
792
793 /* -eof- */