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