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