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.9 2001/12/26 14:45:37 alex Exp $
17 * Revision 1.9 2001/12/26 14:45:37 alex
20 * Revision 1.8 2001/12/26 03:21:46 alex
21 * - PING/PONG-Befehle implementiert,
22 * - Meldungen ueberarbeitet: enthalten nun (fast) immer den Nick.
24 * Revision 1.7 2001/12/25 23:25:18 alex
25 * - und nochmal Aenderungen am Logging ;-)
27 * Revision 1.6 2001/12/25 23:13:33 alex
28 * - Debug-Meldungen angepasst.
30 * Revision 1.5 2001/12/25 22:02:42 alex
31 * - neuer IRC-Befehl "/QUIT". Verbessertes Logging & Debug-Ausgaben.
33 * Revision 1.4 2001/12/25 19:19:30 alex
34 * - bessere Fehler-Abfragen, diverse Bugfixes.
35 * - Nicks werden nur einmal vergeben :-)
36 * - /MOTD wird unterstuetzt.
38 * Revision 1.3 2001/12/24 01:34:06 alex
39 * - USER und NICK wird nun in beliebiger Reihenfolge akzeptiert (wg. BitchX)
40 * - MOTD-Ausgabe begonnen zu implementieren.
42 * Revision 1.2 2001/12/23 21:57:16 alex
43 * - erste IRC-Befehle zu implementieren begonnen.
45 * Revision 1.1 2001/12/14 08:13:43 alex
46 * - neues Modul begonnen :-)
68 #define CONNECTED TRUE
69 #define DISCONNECTED FALSE
72 LOCAL BOOLEAN Check_Valid_User( CLIENT *Client );
74 LOCAL BOOLEAN Hello_User( CLIENT *Client );
75 LOCAL BOOLEAN Show_MOTD( CLIENT *Client );
78 GLOBAL VOID IRC_Init( VOID )
83 GLOBAL VOID IRC_Exit( VOID )
88 GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
90 /* Text an Clients, lokal bzw. remote, senden. */
93 BOOLEAN ok = CONNECTED;
96 assert( Client != NULL );
97 assert( Format != NULL );
99 va_start( ap, Format );
101 if( Client->conn_id != NONE )
104 vsnprintf( buffer, 1000, Format, ap );
105 if( Prefix ) ok = Conn_WriteStr( Client->conn_id, ":%s %s", Prefix->host, buffer );
106 else ok = Conn_WriteStr( Client->conn_id, buffer );
111 Log( LOG_EMERG, "not implemented: IRC_WriteStrClient()" );
116 } /* IRC_WriteStrClient */
119 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
121 assert( Client != NULL );
122 assert( Req != NULL );
124 if( Client->type == CLIENT_UNKNOWN )
126 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client->conn_id );
127 return IRC_WriteStrClient( Client, This_Server, ERR_UNKNOWNCOMMAND_MSG, Client_Name( Client ), Req->command );
129 else return IRC_WriteStrClient( Client, This_Server, ERR_ALREADYREGISTRED_MSG, Client_Name( Client ));
133 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
135 assert( Client != NULL );
136 assert( Req != NULL );
138 if( Client->type != CLIENT_SERVER && Client->type != CLIENT_SERVICE )
140 /* Falsche Anzahl Parameter? */
141 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
143 /* pruefen, ob Nick bereits vergeben */
144 if( ! Client_CheckNick( Client, Req->argv[0] )) return CONNECTED;
146 /* Client-Nick registrieren */
147 strcpy( Client->nick, Req->argv[0] );
149 if( Client->type != CLIENT_USER )
152 Log( LOG_DEBUG, "Connection %d: got NICK command ...", Client->conn_id );
153 if( Client->type == CLIENT_GOTUSER ) return Hello_User( Client );
154 else Client->type = CLIENT_GOTNICK;
158 else return IRC_WriteStrClient( Client, This_Server, ERR_ALREADYREGISTRED_MSG, Client_Name( Client ));
162 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
164 assert( Client != NULL );
165 assert( Req != NULL );
167 if( Client->type == CLIENT_UNKNOWN || Client->type == CLIENT_GOTNICK || Client->type == CLIENT_GOTPASS )
169 /* Falsche Anzahl Parameter? */
170 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
172 strncpy( Client->user, Req->argv[0], CLIENT_USER_LEN );
173 Client->user[CLIENT_USER_LEN] = '\0';
174 strncpy( Client->name, Req->argv[3], CLIENT_NAME_LEN );
175 Client->name[CLIENT_NAME_LEN] = '\0';
177 Log( LOG_DEBUG, "Connection %d: got USER command ...", Client->conn_id );
178 if( Client->type == CLIENT_GOTNICK ) return Hello_User( Client );
179 else Client->type = CLIENT_GOTUSER;
182 else if( Client->type == CLIENT_USER || Client->type == CLIENT_SERVER || Client->type == CLIENT_SERVICE )
184 return IRC_WriteStrClient( Client, This_Server, ERR_ALREADYREGISTRED_MSG, Client_Name( Client ));
186 else return IRC_WriteStrClient( Client, This_Server, ERR_NOTREGISTERED_MSG, Client_Name( Client ));
190 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
192 assert( Client != NULL );
193 assert( Req != NULL );
195 if( Client->type != CLIENT_SERVER && Client->type != CLIENT_SERVICE )
197 /* Falsche Anzahl Parameter? */
198 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
200 Conn_Close( Client->conn_id, "Client wants to quit." );
203 else return IRC_WriteStrClient( Client, This_Server, ERR_NOTREGISTERED_MSG, Client_Name( Client ));
207 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
209 assert( Client != NULL );
210 assert( Req != NULL );
212 return IRC_WriteStrClient( Client, This_Server, ERR_UNKNOWNCOMMAND_MSG, Client_Name( Client ), Req->command );
216 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
218 assert( Client != NULL );
219 assert( Req != NULL );
221 if( ! Check_Valid_User( Client )) return CONNECTED;
223 /* Falsche Anzahl Parameter? */
224 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NOORIGIN_MSG, Client_Name( Client ));
225 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
227 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
228 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
230 Log( LOG_DEBUG, "Connection %d: received PONG.", Client->conn_id );
235 GLOBAL BOOLEAN IRC_MOTD( CLIENT *Client, REQUEST *Req )
237 assert( Client != NULL );
238 assert( Req != NULL );
240 if( ! Check_Valid_User( Client )) return CONNECTED;
242 /* Falsche Anzahl Parameter? */
243 if( Req->argc != 0 ) return IRC_WriteStrClient( Client, This_Server, ERR_NEEDMOREPARAMS_MSG, Client_Name( Client ), Req->command );
245 return Show_MOTD( Client );
249 LOCAL BOOLEAN Check_Valid_User( CLIENT *Client )
251 assert( Client != NULL );
253 if( Client->type != CLIENT_USER )
255 IRC_WriteStrClient( Client, This_Server, ERR_NOTREGISTERED_MSG, Client_Name( Client ));
259 } /* Check_Valid_User */
262 LOCAL BOOLEAN Hello_User( CLIENT *Client )
264 assert( Client != NULL );
265 assert( Client->nick[0] );
267 Log( LOG_NOTICE, "User \"%s!%s@%s\" (%s) registered.", Client->nick, Client->user, Client->host, Client->name );
269 IRC_WriteStrClient( Client, This_Server, RPL_WELCOME_MSG, Client->nick, Client->nick, Client->user, Client->host );
270 IRC_WriteStrClient( Client, This_Server, RPL_YOURHOST_MSG, Client->nick, This_Server->host );
271 IRC_WriteStrClient( Client, This_Server, RPL_CREATED_MSG, Client->nick );
272 IRC_WriteStrClient( Client, This_Server, RPL_MYINFO_MSG, Client->nick, This_Server->host );
274 Client->type = CLIENT_USER;
276 return Show_MOTD( Client );
280 LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
282 assert( Client != NULL );
283 assert( Client->nick[0] );
285 IRC_WriteStrClient( Client, This_Server, RPL_MOTDSTART_MSG, Client->nick, This_Server->host );
286 IRC_WriteStrClient( Client, This_Server, RPL_MOTD_MSG, Client->nick, "Some cool IRC server welcome message ;-)" );
287 return IRC_WriteStrClient( Client, This_Server, RPL_ENDOFMOTD_MSG, Client->nick );