2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001 by Alexander Barton (alex@barton.de)
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 comBase beteiligten Autoren finden Sie in der Datei AUTHORS.
12 * $Id: irc.c,v 1.13 2001/12/29 03:10:06 alex Exp $
17 * Revision 1.13 2001/12/29 03:10:06 alex
18 * - Neue Funktion IRC_MODE() implementiert, div. Aenderungen.
19 * - neue configure-Optione "--enable-strict-rfc".
21 * Revision 1.12 2001/12/27 19:17:26 alex
22 * - neue Befehle PRIVMSG, NOTICE, PING.
24 * Revision 1.11 2001/12/27 16:55:41 alex
25 * - neu: IRC_WriteStrRelated(), Aenderungen auch in IRC_WriteStrClient().
27 * Revision 1.10 2001/12/26 22:48:53 alex
28 * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
30 * Revision 1.9 2001/12/26 14:45:37 alex
33 * Revision 1.8 2001/12/26 03:21:46 alex
34 * - PING/PONG-Befehle implementiert,
35 * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
37 * Revision 1.7 2001/12/25 23:25:18 alex
38 * - und nochmal Aenderungen am Logging ;-)
40 * Revision 1.6 2001/12/25 23:13:33 alex
41 * - Debug-Meldungen angepasst.
43 * Revision 1.5 2001/12/25 22:02:42 alex
44 * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
46 * Revision 1.4 2001/12/25 19:19:30 alex
47 * - bessere Fehler-Abfragen, diverse Bugfixes.
48 * - Nicks werden nur einmal vergeben :-)
49 * - /MOTD wird unterstuetzt.
51 * Revision 1.3 2001/12/24 01:34:06 alex
52 * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
53 * - MOTD-Ausgabe begonnen zu implementieren.
55 * Revision 1.2 2001/12/23 21:57:16 alex
56 * - erste IRC-Befehle zu implementieren begonnen.
58 * Revision 1.1 2001/12/14 08:13:43 alex
59 * - neues Modul begonnen :-)
83 #define CONNECTED TRUE
84 #define DISCONNECTED FALSE
87 LOCAL BOOLEAN Check_Valid_User( CLIENT *Client );
89 LOCAL BOOLEAN Hello_User( CLIENT *Client );
90 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
93 GLOBAL VOID IRC_Init( VOID )
98 GLOBAL VOID IRC_Exit( VOID )
103 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
105 /* Text an Clients, lokal bzw. remote, senden. */
108 BOOLEAN ok = CONNECTED;
112 assert( Client != NULL );
113 assert( Format != NULL );
115 va_start( ap, Format );
116 vsnprintf( buffer, 1000, Format, ap );
119 if( Client->conn_id != NONE ) send_to = Client->conn_id;
120 else send_to = Client->introducer->conn_id;
122 if( Prefix ) ok = Conn_WriteStr( Client->conn_id, ":%s %s", Client_GetID( Prefix ), buffer );
123 else ok = Conn_WriteStr( Client->conn_id, buffer );
126 } /* IRC_WriteStrClient */
129 GLOBAL BOOLEAN IRC_WriteStrRelated( CLIENT *Client, CHAR *Format, ... )
132 BOOLEAN ok = CONNECTED;
135 assert( Client != NULL );
136 assert( Format != NULL );
138 va_start( ap, Format );
139 vsnprintf( buffer, 1000, Format, ap );
142 /* an den Client selber */
143 ok = IRC_WriteStrClient( Client, Client, buffer );
146 } /* IRC_WriteStrRelated */
149 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
151 assert( Client != NULL );
152 assert( Req != NULL );
154 if( Client->type == CLIENT_UNKNOWN )
156 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client->conn_id );
157 return IRC_WriteStrClient( Client, This_Server, ERR_UNKNOWNCOMMAND_MSG, Client_Name( Client ), Req->command );
159 else return IRC_WriteStrClient( Client, This_Server, ERR_ALREADYREGISTRED_MSG, Client_Name( Client ));
163 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
165 assert( Client != NULL );
166 assert( Req != NULL );
168 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
170 if( Client->type == CLIENT_UNKNOWN || Client->type == CLIENT_GOTPASS || Client->type == CLIENT_GOTNICK || Client->type == CLIENT_GOTUSER || Client->type == CLIENT_USER )
172 if( Client->type == CLIENT_UNKNOWN || Client->type == CLIENT_GOTPASS || Client->type == CLIENT_GOTNICK || Client->type == CLIENT_USER )
175 /* Falsche Anzahl Parameter? */
176 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
178 /* Ist der Client "restricted"? */
179 if( strchr( Client->modes, 'r' )) return IRC_WriteStrClient( Client, This_Server, ERR_RESTRICTED_MSG, Client_Name( Client ));
181 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
182 * wir nichts. So macht es das Original und mind. Snak hat probleme,
183 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
185 if( strcasecmp( Client->nick, Req->argv[0] ) == 0 ) return CONNECTED;
188 /* pruefen, ob Nick bereits vergeben */
189 if( ! Client_CheckNick( Client, Req->argv[0] )) return CONNECTED;
191 if( Client->type == CLIENT_USER )
193 /* Nick-Aenderung: allen mitteilen! */
194 Log( LOG_INFO, "User \"%s!%s@%s\" changed nick: \"%s\" -> \"%s\".", Client->nick, Client->user, Client->host, Client->nick, Req->argv[0] );
195 IRC_WriteStrRelated( Client, "NICK :%s", Req->argv[0] );
198 /* Client-Nick registrieren */
199 strcpy( Client->nick, Req->argv[0] );
201 if( Client->type != CLIENT_USER )
204 Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client->conn_id );
205 if( Client->type == CLIENT_GOTUSER ) return Hello_User( Client );
206 else Client->type = CLIENT_GOTNICK;
210 else return IRC_WriteStrClient( Client, This_Server, ERR_ALREADYREGISTRED_MSG, Client_Name( Client ));
214 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
216 assert( Client != NULL );
217 assert( Req != NULL );
220 if( Client->type == CLIENT_GOTNICK || Client->type == CLIENT_GOTPASS || Client->type == CLIENT_UNKNOWN )
222 if( Client->type == CLIENT_GOTNICK || Client->type == CLIENT_GOTPASS )
225 /* Falsche Anzahl Parameter? */
226 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
228 strncpy( Client->user, Req->argv[0], CLIENT_USER_LEN );
229 Client->user[CLIENT_USER_LEN] = '\0';
230 strncpy( Client->name, Req->argv[3], CLIENT_NAME_LEN );
231 Client->name[CLIENT_NAME_LEN] = '\0';
233 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client->conn_id );
234 if( Client->type == CLIENT_GOTNICK ) return Hello_User( Client );
235 else Client->type = CLIENT_GOTUSER;
238 else if( Client->type == CLIENT_USER || Client->type == CLIENT_SERVER || Client->type == CLIENT_SERVICE )
240 return IRC_WriteStrClient( Client, This_Server, ERR_ALREADYREGISTRED_MSG, Client_Name( Client ));
242 else return IRC_WriteStrClient( Client, This_Server, ERR_NOTREGISTERED_MSG, Client_Name( Client ));
246 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
248 assert( Client != NULL );
249 assert( Req != NULL );
251 if( Client->type != CLIENT_SERVER && Client->type != CLIENT_SERVICE )
253 /* Falsche Anzahl Parameter? */
254 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
256 Conn_Close( Client->conn_id, "Client wants to quit." );
259 else return IRC_WriteStrClient( Client, This_Server, ERR_NOTREGISTERED_MSG, Client_Name( Client ));
263 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
267 assert( Client != NULL );
268 assert( Req != NULL );
270 if( ! Check_Valid_User( Client )) return CONNECTED;
272 /* Falsche Anzahl Parameter? */
273 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NOORIGIN_MSG, Client_Name( Client ));
274 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
276 to = Client_Search( Req->argv[0] );
278 if( to ) return IRC_WriteStrClient( Client, This_Server, "PONG :%s", Client_Name( Client ));
279 else return IRC_WriteStrClient( Client, This_Server, ERR_NOSUCHNICK_MSG, Client_Name( Client ), Req->argv[0] );
283 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
285 assert( Client != NULL );
286 assert( Req != NULL );
288 if( ! Check_Valid_User( Client )) return CONNECTED;
290 /* Falsche Anzahl Parameter? */
291 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NOORIGIN_MSG, Client_Name( Client ));
292 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
294 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
295 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
297 Log( LOG_DEBUG, "Connection %d: received PONG.", Client->conn_id );
302 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
304 assert( Client != NULL );
305 assert( Req != NULL );
307 if( ! Check_Valid_User( Client )) return CONNECTED;
309 /* Falsche Anzahl Parameter? */
310 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
312 return Show_MOTD( Client );
316 GLOBAL BOOLEAN IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
320 assert( Client != NULL );
321 assert( Req != NULL );
323 if( ! Check_Valid_User( Client )) return CONNECTED;
325 /* Falsche Anzahl Parameter? */
326 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, This_Server, ERR_NORECIPIENT_MSG, Client_Name( Client ), Req->command );
327 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NOTEXTTOSEND_MSG, Client_Name( Client ));
328 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
330 to = Client_Search( Req->argv[0] );
333 /* Okay, Ziel ist ein User */
334 return IRC_WriteStrClient( to, Client, "PRIVMSG %s :%s", to->nick, Req->argv[1] );
337 return IRC_WriteStrClient( Client, This_Server, ERR_NOSUCHNICK_MSG, Client_Name( Client ), Req->argv[0] );
341 GLOBAL BOOLEAN IRC_NOTICE( CLIENT *Client, REQUEST *Req )
345 assert( Client != NULL );
346 assert( Req != NULL );
348 if( ! Check_Valid_User( Client )) return CONNECTED;
350 /* Falsche Anzahl Parameter? */
351 if( Req->argc != 2 ) return CONNECTED;
353 to = Client_Search( Req->argv[0] );
356 /* Okay, Ziel ist ein User */
357 return IRC_WriteStrClient( to, Client, "NOTICE %s :%s", to->nick, Req->argv[1] );
364 GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
366 CHAR x[2], new_modes[CLIENT_MODE_LEN], *ptr, *p;
369 assert( Client != NULL );
370 assert( Req != NULL );
372 if( ! Check_Valid_User( Client )) return CONNECTED;
374 /* Falsche Anzahl Parameter? */
375 if(( Req->argc > 2 ) || ( Req->argc < 1 )) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
377 /* MODE ist nur fuer sich selber zulaessig */
378 if( Client_Search( Req->argv[0] ) != Client ) return IRC_WriteStrClient( Client, This_Server, ERR_USERSDONTMATCH_MSG, Client_Name( Client ));
380 /* Werden die Modes erfragt? */
381 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, This_Server, RPL_UMODEIS_MSG, Client_Name( Client ), Client->modes );
385 /* Sollen Modes gesetzt oder geloescht werden? */
386 if( *ptr == '+' ) set = TRUE;
387 else if( *ptr == '-' ) set = FALSE;
388 else return IRC_WriteStrClient( Client, This_Server, ERR_UMODEUNKNOWNFLAG_MSG, Client_Name( Client ));
390 /* Reply-String mit Aenderungen vorbereiten */
391 if( set ) strcpy( new_modes, "+" );
392 else strcpy( new_modes, "-" );
407 /* restricted (kann nur gesetzt werden) */
408 if( set ) x[0] = 'r';
409 else ok = IRC_WriteStrClient( Client, This_Server, ERR_RESTRICTED_MSG, Client_Name( Client ));
412 ok = IRC_WriteStrClient( Client, This_Server, ERR_UMODEUNKNOWNFLAG_MSG, Client_Name( Client ));
418 if( ! x[0] ) continue;
420 /* Okay, gueltigen Mode gefunden */
423 /* Modes sollen gesetzt werden */
424 if( ! strchr( Client->modes, x[0] ))
426 /* Client hat den Mode noch nicht -> setzen */
427 strcat( Client->modes, x );
428 strcat( new_modes, x );
433 /* Modes sollen geloescht werden */
434 p = strchr( Client->modes, x[0] );
437 /* Client hat den Mode -> loeschen */
443 strcat( new_modes, x );
448 /* Geanderte Modes? */
449 if( new_modes[1] && ok )
451 ok = IRC_WriteStrRelated( Client, "MODE %s :%s", Client->nick, new_modes );
452 Log( LOG_DEBUG, "User \"%s!%s@%s\": Mode change, now \"%s\".", Client->nick, Client->user, Client->host, Client->modes );
458 LOCAL BOOLEAN Check_Valid_User( CLIENT *Client )
460 assert( Client != NULL );
462 if( Client->type != CLIENT_USER )
464 IRC_WriteStrClient( Client, This_Server, ERR_NOTREGISTERED_MSG, Client_Name( Client ));
468 } /* Check_Valid_User */
471 LOCAL BOOLEAN Hello_User( CLIENT *Client )
473 assert( Client != NULL );
474 assert( Client->nick[0] );
476 Log( LOG_NOTICE, "User \"%s!%s@%s\" (%s) registered (connection %d).", Client->nick, Client->user, Client->host, Client->name, Client->conn_id );
478 IRC_WriteStrClient( Client, This_Server, RPL_WELCOME_MSG, Client->nick, Client_GetID( Client ));
479 IRC_WriteStrClient( Client, This_Server, RPL_YOURHOST_MSG, Client->nick, This_Server->host );
480 IRC_WriteStrClient( Client, This_Server, RPL_CREATED_MSG, Client->nick );
481 IRC_WriteStrClient( Client, This_Server, RPL_MYINFO_MSG, Client->nick, This_Server->host );
483 Client->type = CLIENT_USER;
485 return Show_MOTD( Client );
489 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
495 assert( Client != NULL );
496 assert( Client->nick[0] );
498 fd = fopen( Conf_MotdFile, "r" );
501 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
502 return IRC_WriteStrClient( Client, This_Server, ERR_NOMOTD_MSG, Client->nick );
505 IRC_WriteStrClient( Client, This_Server, RPL_MOTDSTART_MSG, Client->nick, This_Server->host );
508 if( ! fgets( line, 126, fd )) break;
509 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
510 IRC_WriteStrClient( Client, This_Server, RPL_MOTD_MSG, Client->nick, line );
512 ok = IRC_WriteStrClient( Client, This_Server, RPL_ENDOFMOTD_MSG, Client->nick );