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