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.8 2001/12/26 03:21:46 alex Exp $
17 * Revision 1.8 2001/12/26 03:21:46 alex
18 * - PING/PONG-Befehle implementiert,
19 * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
21 * Revision 1.7 2001/12/25 23:25:18 alex
22 * - und nochmal Aenderungen am Logging ;-)
24 * Revision 1.6 2001/12/25 23:13:33 alex
25 * - Debug-Meldungen angepasst.
27 * Revision 1.5 2001/12/25 22:02:42 alex
28 * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
30 * Revision 1.4 2001/12/25 19:19:30 alex
31 * - bessere Fehler-Abfragen, diverse Bugfixes.
32 * - Nicks werden nur einmal vergeben :-)
33 * - /MOTD wird unterstuetzt.
35 * Revision 1.3 2001/12/24 01:34:06 alex
36 * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
37 * - MOTD-Ausgabe begonnen zu implementieren.
39 * Revision 1.2 2001/12/23 21:57:16 alex
40 * - erste IRC-Befehle zu implementieren begonnen.
42 * Revision 1.1 2001/12/14 08:13:43 alex
43 * - neues Modul begonnen :-)
65 #define CONNECTED TRUE
66 #define DISCONNECTED FALSE
69 LOCAL BOOLEAN Check_Valid_User( CLIENT *Client );
71 LOCAL BOOLEAN Hello_User( CLIENT *Client );
72 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
75 GLOBAL VOID IRC_Init( VOID )
80 GLOBAL VOID IRC_Exit( VOID )
85 GLOBAL BOOLEAN IRC_WriteStr_Client( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
87 /* Text an Clients, lokal bzw. remote, senden. */
90 BOOLEAN ok = CONNECTED;
93 assert( Client != NULL );
94 assert( Format != NULL );
96 va_start( ap, Format );
98 if( Client->conn_id != NONE )
101 vsnprintf( buffer, 1000, Format, ap );
102 if( Prefix ) ok = Conn_WriteStr( Client->conn_id, ":%s %s", Prefix->host, buffer );
103 else ok = Conn_WriteStr( Client->conn_id, buffer );
108 Log( LOG_EMERG, "not implemented: IRC_WriteStr_Client()" );
113 } /* IRC_WriteStr_Client */
116 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
118 assert( Client != NULL );
119 assert( Req != NULL );
121 if( Client->type == CLIENT_UNKNOWN )
123 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client->conn_id );
124 return IRC_WriteStr_Client( Client, This_Server, ERR_UNKNOWNCOMMAND_MSG, Client_Name( Client ), Req->command );
126 else return IRC_WriteStr_Client( Client, This_Server, ERR_ALREADYREGISTRED_MSG, Client_Name( Client ));
130 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
134 assert( Client != NULL );
135 assert( Req != NULL );
137 if( Client->type != CLIENT_SERVER && Client->type != CLIENT_SERVICE )
139 /* Falsche Anzahl Parameter? */
140 if( Req->argc != 1 ) return IRC_WriteStr_Client( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
143 if( strlen( Req->argv[0] ) > CLIENT_NICK_LEN ) return IRC_WriteStr_Client( Client, This_Server, ERR_ERRONEUSNICKNAME_MSG, Client_Name( Client ), Req->argv[0] );
145 /* pruefen, ob Nick bereits vergeben */
149 if( strcasecmp( c->nick, Req->argv[0] ) == 0 )
151 /* den Nick gibt es bereits */
152 return IRC_WriteStr_Client( Client, This_Server, ERR_NICKNAMEINUSE_MSG, Client_Name( Client ), Req->argv[0] );
157 /* Client-Nick registrieren */
158 strcpy( Client->nick, Req->argv[0] );
160 if( Client->type != CLIENT_USER )
163 Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client->conn_id );
164 if( Client->type == CLIENT_GOTUSER ) return Hello_User( Client );
165 else Client->type = CLIENT_GOTNICK;
169 else return IRC_WriteStr_Client( Client, This_Server, ERR_ALREADYREGISTRED_MSG, Client_Name( Client ));
173 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
175 assert( Client != NULL );
176 assert( Req != NULL );
178 if( Client->type == CLIENT_UNKNOWN || Client->type == CLIENT_GOTNICK || Client->type == CLIENT_GOTPASS )
180 /* Falsche Anzahl Parameter? */
181 if( Req->argc != 4 ) return IRC_WriteStr_Client( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
183 strncpy( Client->user, Req->argv[0], CLIENT_USER_LEN );
184 Client->user[CLIENT_USER_LEN] = '\0';
185 strncpy( Client->name, Req->argv[3], CLIENT_NAME_LEN );
186 Client->name[CLIENT_NAME_LEN] = '\0';
188 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client->conn_id );
189 if( Client->type == CLIENT_GOTNICK ) return Hello_User( Client );
190 else Client->type = CLIENT_GOTUSER;
193 else if( Client->type == CLIENT_USER || Client->type == CLIENT_SERVER || Client->type == CLIENT_SERVICE )
195 return IRC_WriteStr_Client( Client, This_Server, ERR_ALREADYREGISTRED_MSG, Client_Name( Client ));
197 else return IRC_WriteStr_Client( Client, This_Server, ERR_NOTREGISTERED_MSG, Client_Name( Client ));
201 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
203 assert( Client != NULL );
204 assert( Req != NULL );
206 if( Client->type != CLIENT_SERVER && Client->type != CLIENT_SERVICE )
208 /* Falsche Anzahl Parameter? */
209 if( Req->argc > 1 ) return IRC_WriteStr_Client( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
211 Conn_Close( Client->conn_id, "Client wants to quit." );
214 else return IRC_WriteStr_Client( Client, This_Server, ERR_NOTREGISTERED_MSG, Client_Name( Client ));
218 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
220 assert( Client != NULL );
221 assert( Req != NULL );
223 return IRC_WriteStr_Client( Client, This_Server, ERR_UNKNOWNCOMMAND_MSG, Client_Name( Client ), Req->command );
227 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
229 assert( Client != NULL );
230 assert( Req != NULL );
232 if( ! Check_Valid_User( Client )) return CONNECTED;
234 /* Falsche Anzahl Parameter? */
235 if( Req->argc < 1 ) return IRC_WriteStr_Client( Client, This_Server, ERR_NOORIGIN_MSG, Client_Name( Client ));
236 if( Req->argc > 1 ) return IRC_WriteStr_Client( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
238 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
239 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
241 Log( LOG_DEBUG, "Connection %d: received PONG.", Client->conn_id );
246 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
248 assert( Client != NULL );
249 assert( Req != NULL );
251 if( ! Check_Valid_User( Client )) return CONNECTED;
253 /* Falsche Anzahl Parameter? */
254 if( Req->argc != 0 ) return IRC_WriteStr_Client( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
256 return Show_MOTD( Client );
260 LOCAL BOOLEAN Check_Valid_User( CLIENT *Client )
262 assert( Client != NULL );
264 if( Client->type != CLIENT_USER )
266 IRC_WriteStr_Client( Client, This_Server, ERR_NOTREGISTERED_MSG, Client_Name( Client ));
270 } /* Check_Valid_User */
273 LOCAL BOOLEAN Hello_User( CLIENT *Client )
275 assert( Client != NULL );
276 assert( Client->nick[0] );
278 Log( LOG_NOTICE, "User \"%s!%s@%s\" (%s) registered.", Client->nick, Client->user, Client->host, Client->name );
280 IRC_WriteStr_Client( Client, This_Server, RPL_WELCOME_MSG, Client->nick, Client->nick, Client->user, Client->host );
281 IRC_WriteStr_Client( Client, This_Server, RPL_YOURHOST_MSG, Client->nick, This_Server->host );
282 IRC_WriteStr_Client( Client, This_Server, RPL_CREATED_MSG, Client->nick );
283 IRC_WriteStr_Client( Client, This_Server, RPL_MYINFO_MSG, Client->nick, This_Server->host );
285 Client->type = CLIENT_USER;
287 return Show_MOTD( Client );
291 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
293 assert( Client != NULL );
294 assert( Client->nick[0] );
296 IRC_WriteStr_Client( Client, This_Server, RPL_MOTDSTART_MSG, Client->nick, This_Server->host );
297 IRC_WriteStr_Client( Client, This_Server, RPL_MOTD_MSG, Client->nick, "Some cool IRC server welcome message ;-)" );
298 return IRC_WriteStr_Client( Client, This_Server, RPL_ENDOFMOTD_MSG, Client->nick );