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