]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
Implement IRC commands SERVICE, SERVLIST, and SQUERY as dummy functions
[ngircd-alex.git] / src / ngircd / irc.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2004 Alexander Barton <alex@barton.de>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  *
11  * IRC commands
12  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: irc.c,v 1.132 2008/01/15 22:28:14 fw Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "ngircd.h"
25 #include "resolve.h"
26 #include "conn-func.h"
27 #include "conf.h"
28 #include "client.h"
29 #include "channel.h"
30 #include "defines.h"
31 #include "irc-write.h"
32 #include "log.h"
33 #include "messages.h"
34 #include "parse.h"
35
36 #include "exp.h"
37 #include "irc.h"
38
39
40 static char *Option_String PARAMS((CONN_ID Idx));
41 static bool Send_Message PARAMS((CLIENT *Client, REQUEST *Req, int ForceType));
42
43
44 GLOBAL bool
45 IRC_ERROR( CLIENT *Client, REQUEST *Req )
46 {
47         assert( Client != NULL );
48         assert( Req != NULL );
49
50         if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
51         else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
52
53         return CONNECTED;
54 } /* IRC_ERROR */
55
56
57 /**
58  * Kill client on request.
59  * This function implements the IRC command "KILL" wich is used to selectively
60  * disconnect clients. It can be used by IRC operators and servers, for example
61  * to "solve" nick collisions after netsplits.
62  * Please note that this function is also called internally, without a real
63  * KILL command beeing received over the network! Client is Client_ThisServer()
64  * in this case. */
65 GLOBAL bool
66 IRC_KILL( CLIENT *Client, REQUEST *Req )
67 {
68         CLIENT *prefix, *c;
69         char reason[COMMAND_LEN], *msg;
70         CONN_ID my_conn, conn;
71
72         assert( Client != NULL );
73         assert( Req != NULL );
74
75         if(( Client_Type( Client ) != CLIENT_SERVER ) &&
76            ( ! Client_OperByMe( Client )))
77         {
78                 /* The originator of the KILL is neither an IRC operator of
79                  * this server nor a server. */
80                 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
81                                            Client_ID( Client ));
82         }
83
84         if( Req->argc != 2 )
85         {
86                 /* This command requires exactly 2 parameters! */
87                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
88                                            Client_ID( Client ), Req->command );
89         }
90
91         if( Req->prefix ) prefix = Client_Search( Req->prefix );
92         else prefix = Client;
93         if( ! prefix )
94         {
95                 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
96                      Req->prefix );
97                 prefix = Client_ThisServer( );
98         }
99
100         if( Client != Client_ThisServer( ))
101         {
102                 /* This is a "real" KILL received from the network. */
103                 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
104                      Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
105         }
106
107         /* Build reason string */
108         if( Client_Type( Client ) == CLIENT_USER )
109         {
110                 /* Prefix the "reason" if the originator is a regular user,
111                  * so users can't spoof KILLs of servers. */
112                 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
113                           Client_ID( Client ), Req->argv[1] );
114         }
115         else
116                 strlcpy( reason, Req->argv[1], sizeof( reason ));
117
118         /* Inform other servers */
119         IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
120                                    Req->argv[0], reason );
121
122         /* Save ID of this connection */
123         my_conn = Client_Conn( Client );
124
125         /* Do we host such a client? */
126         c = Client_Search( Req->argv[0] );
127         if( c )
128         {
129                 if(( Client_Type( c ) != CLIENT_USER ) &&
130                    ( Client_Type( c ) != CLIENT_GOTNICK ))
131                 {
132                         /* Target of this KILL is not a regular user, this is
133                          * invalid! So we ignore this case if we received a
134                          * regular KILL from the network and try to kill the
135                          * client/connection anyway (but log an error!) if the
136                          * origin is the local server. */
137
138                         if( Client != Client_ThisServer( ))
139                         {
140                                 /* Invalid KILL received from remote */
141                                 if( Client_Type( c ) == CLIENT_SERVER )
142                                         msg = ERR_CANTKILLSERVER_MSG;
143                                 else
144                                         msg = ERR_NOPRIVILEGES_MSG;
145                                 return IRC_WriteStrClient( Client, msg,
146                                         Client_ID( Client ));
147                         }
148
149                         Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
150                              Client_Type( c ), Req->argv[0] );
151                 }
152
153                 /* Kill client NOW! */
154                 conn = Client_Conn( c );
155                 Client_Destroy( c, NULL, reason, false );
156                 if( conn > NONE )
157                         Conn_Close( conn, NULL, reason, true );
158         }
159         else
160                 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
161
162         /* Are we still connected or were we killed, too? */
163         if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
164                 return CONNECTED;
165         else
166                 return DISCONNECTED;
167 } /* IRC_KILL */
168
169
170 GLOBAL bool
171 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
172 {
173         CLIENT *to, *from;
174         CHANNEL *chan;
175
176         assert( Client != NULL );
177         assert( Req != NULL );
178
179         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
180
181         /* Falsche Anzahl Parameter? */
182         if( Req->argc != 2 ) return CONNECTED;
183
184         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
185         else from = Client;
186         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
187
188         to = Client_Search( Req->argv[0] );
189         if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
190         {
191                 /* Okay, Ziel ist ein User */
192                 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
193         }
194         else
195         {
196                 chan = Channel_Search(Req->argv[0]);
197                 if (chan)
198                         return Channel_Notice(chan, from, Client, Req->argv[1]);
199         }
200
201         return CONNECTED;
202 } /* IRC_NOTICE */
203
204
205 /**
206  * Handler for the IRC command PRIVMSG.
207  */
208 GLOBAL bool
209 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
210 {
211         return Send_Message(Client, Req, CLIENT_USER);
212 } /* IRC_PRIVMSG */
213
214
215 /**
216  * Handler for the IRC command SQUERY.
217  */
218 GLOBAL bool
219 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
220 {
221         return Send_Message(Client, Req, CLIENT_SERVICE);
222 } /* IRC_SQUERY */
223
224
225 GLOBAL bool
226 IRC_TRACE( CLIENT *Client, REQUEST *Req )
227 {
228         CLIENT *from, *target, *c;
229         CONN_ID idx, idx2;
230         char user[CLIENT_USER_LEN];
231
232         assert( Client != NULL );
233         assert( Req != NULL );
234
235         /* Bad number of arguments? */
236         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
237
238         /* Search sender */
239         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
240         else from = Client;
241         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
242
243         /* Search target */
244         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
245         else target = Client_ThisServer( );
246         
247         /* Forward command to other server? */
248         if( target != Client_ThisServer( ))
249         {
250                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
251
252                 /* Send RPL_TRACELINK back to initiator */
253                 idx = Client_Conn( Client ); assert( idx > NONE );
254                 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
255                 if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), PACKAGE_NAME, PACKAGE_VERSION, Client_ID( target ), Client_ID( Client_NextHop( target )), Option_String( idx2 ), time( NULL ) - Conn_StartTime( idx2 ), Conn_SendQ( idx ), Conn_SendQ( idx2 ))) return DISCONNECTED;
256
257                 /* Forward command */
258                 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
259                 return CONNECTED;
260         }
261
262         /* Infos about all connected servers */
263         c = Client_First( );
264         while( c )
265         {
266                 if( Client_Conn( c ) > NONE )
267                 {
268                         /* Local client */
269                         if( Client_Type( c ) == CLIENT_SERVER )
270                         {
271                                 /* Server link */
272                                 strlcpy( user, Client_User( c ), sizeof( user ));
273                                 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
274                                 if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Client_ID( c ), user, Client_Hostname( c ), Client_Mask( Client_ThisServer( )), Option_String( Client_Conn( c )))) return DISCONNECTED;
275                         }
276                         if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
277                         {
278                                 /* IRC Operator */
279                                 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
280                         }
281                 }
282                 c = Client_Next( c );
283         }
284
285         IRC_SetPenalty( Client, 3 );
286         return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
287 } /* IRC_TRACE */
288
289
290 GLOBAL bool
291 IRC_HELP( CLIENT *Client, REQUEST *Req )
292 {
293         COMMAND *cmd;
294
295         assert( Client != NULL );
296         assert( Req != NULL );
297
298         /* Bad number of arguments? */
299         if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
300
301         cmd = Parse_GetCommandStruct( );
302         while( cmd->name )
303         {
304                 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
305                 cmd++;
306         }
307         
308         IRC_SetPenalty( Client, 2 );
309         return CONNECTED;
310 } /* IRC_HELP */
311
312
313 static char *
314 Option_String( CONN_ID Idx )
315 {
316         static char option_txt[8];
317         UINT16 options;
318
319         options = Conn_Options(Idx);
320
321         strcpy(option_txt, "F");        /* No idea what this means, but the
322                                          * original ircd sends it ... */
323 #ifdef ZLIB
324         if(options & CONN_ZIP)          /* zlib compression supported. */
325                 strcat(option_txt, "z");
326 #endif
327
328         return option_txt;
329 } /* Option_String */
330
331
332 static bool
333 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType)
334 {
335         CLIENT *cl, *from;
336         CHANNEL *chan;
337
338         assert(Client != NULL);
339         assert(Req != NULL);
340
341         if (Req->argc == 0)
342                 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
343                                           Client_ID(Client), Req->command);
344         if (Req->argc == 1)
345                 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
346                                           Client_ID(Client));
347         if (Req->argc > 2)
348                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
349                                           Client_ID(Client), Req->command);
350
351         if (Client_Type(Client) == CLIENT_SERVER)
352                 from = Client_Search(Req->prefix);
353         else
354                 from = Client;
355         if (!from)
356                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
357                                           Client_ID(Client), Req->prefix);
358
359         cl = Client_Search(Req->argv[0]);
360         if (cl) {
361                 /* Target is a user, enforce type */
362                 if (Client_Type(cl) != ForceType)
363                         return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
364                                                   Client_ID(from),
365                                                   Req->argv[0]);
366
367                 if ((Client_Type(Client) != CLIENT_SERVER)
368                     && (strchr(Client_Modes(cl), 'a'))) {
369                         /* Target is away */
370                         if (!IRC_WriteStrClient
371                             (from, RPL_AWAY_MSG, Client_ID(from), Client_ID(cl),
372                              Client_Away(cl)))
373                                 return DISCONNECTED;
374                 }
375
376                 if (Client_Conn(from) > NONE)
377                         Conn_UpdateIdle(Client_Conn(from));
378                 return IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s",
379                                                 Client_ID(cl), Req->argv[1]);
380         }
381
382         chan = Channel_Search(Req->argv[0]);
383         if (chan)
384                 return Channel_Write(chan, from, Client, Req->argv[1]);
385
386         return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
387                                   Client_ID(from), Req->argv[0]);
388 } /* Send_Message */
389
390
391 /* -eof- */