]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
Convert SQUERY to PRIVMSG on RFC 1459 compliant links.
[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 "match.h"
34 #include "messages.h"
35 #include "parse.h"
36 #include "tool.h"
37
38 #include "exp.h"
39 #include "irc.h"
40
41
42 static char *Option_String PARAMS((CONN_ID Idx));
43 static bool Send_Message PARAMS((CLIENT *Client, REQUEST *Req, int ForceType,
44                                  bool SendErrors));
45 static bool Send_Message_Mask PARAMS((CLIENT *from, char *command,
46                                       char *targetMask, char *message,
47                                       bool SendErrors));
48
49
50 GLOBAL bool
51 IRC_ERROR( CLIENT *Client, REQUEST *Req )
52 {
53         assert( Client != NULL );
54         assert( Req != NULL );
55
56         if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
57         else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
58
59         return CONNECTED;
60 } /* IRC_ERROR */
61
62
63 /**
64  * Kill client on request.
65  * This function implements the IRC command "KILL" wich is used to selectively
66  * disconnect clients. It can be used by IRC operators and servers, for example
67  * to "solve" nick collisions after netsplits.
68  * Please note that this function is also called internally, without a real
69  * KILL command beeing received over the network! Client is Client_ThisServer()
70  * in this case. */
71 GLOBAL bool
72 IRC_KILL( CLIENT *Client, REQUEST *Req )
73 {
74         CLIENT *prefix, *c;
75         char reason[COMMAND_LEN], *msg;
76         CONN_ID my_conn, conn;
77
78         assert( Client != NULL );
79         assert( Req != NULL );
80
81         if(( Client_Type( Client ) != CLIENT_SERVER ) &&
82            ( ! Client_OperByMe( Client )))
83         {
84                 /* The originator of the KILL is neither an IRC operator of
85                  * this server nor a server. */
86                 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
87                                            Client_ID( Client ));
88         }
89
90         if( Req->argc != 2 )
91         {
92                 /* This command requires exactly 2 parameters! */
93                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
94                                            Client_ID( Client ), Req->command );
95         }
96
97         if( Req->prefix ) prefix = Client_Search( Req->prefix );
98         else prefix = Client;
99         if( ! prefix )
100         {
101                 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
102                      Req->prefix );
103                 prefix = Client_ThisServer( );
104         }
105
106         if( Client != Client_ThisServer( ))
107         {
108                 /* This is a "real" KILL received from the network. */
109                 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
110                      Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
111         }
112
113         /* Build reason string */
114         if( Client_Type( Client ) == CLIENT_USER )
115         {
116                 /* Prefix the "reason" if the originator is a regular user,
117                  * so users can't spoof KILLs of servers. */
118                 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
119                           Client_ID( Client ), Req->argv[1] );
120         }
121         else
122                 strlcpy( reason, Req->argv[1], sizeof( reason ));
123
124         /* Inform other servers */
125         IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
126                                    Req->argv[0], reason );
127
128         /* Save ID of this connection */
129         my_conn = Client_Conn( Client );
130
131         /* Do we host such a client? */
132         c = Client_Search( Req->argv[0] );
133         if( c )
134         {
135                 if(( Client_Type( c ) != CLIENT_USER ) &&
136                    ( Client_Type( c ) != CLIENT_GOTNICK ))
137                 {
138                         /* Target of this KILL is not a regular user, this is
139                          * invalid! So we ignore this case if we received a
140                          * regular KILL from the network and try to kill the
141                          * client/connection anyway (but log an error!) if the
142                          * origin is the local server. */
143
144                         if( Client != Client_ThisServer( ))
145                         {
146                                 /* Invalid KILL received from remote */
147                                 if( Client_Type( c ) == CLIENT_SERVER )
148                                         msg = ERR_CANTKILLSERVER_MSG;
149                                 else
150                                         msg = ERR_NOPRIVILEGES_MSG;
151                                 return IRC_WriteStrClient( Client, msg,
152                                         Client_ID( Client ));
153                         }
154
155                         Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
156                              Client_Type( c ), Req->argv[0] );
157                 }
158
159                 /* Kill client NOW! */
160                 conn = Client_Conn( c );
161                 Client_Destroy( c, NULL, reason, false );
162                 if( conn > NONE )
163                         Conn_Close( conn, NULL, reason, true );
164         }
165         else
166                 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
167
168         /* Are we still connected or were we killed, too? */
169         if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
170                 return CONNECTED;
171         else
172                 return DISCONNECTED;
173 } /* IRC_KILL */
174
175
176 /**
177  * Handler for the IRC command NOTICE.
178  */
179 GLOBAL bool
180 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
181 {
182         return Send_Message(Client, Req, CLIENT_USER, false);
183 } /* IRC_NOTICE */
184
185
186 /**
187  * Handler for the IRC command PRIVMSG.
188  */
189 GLOBAL bool
190 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
191 {
192         return Send_Message(Client, Req, CLIENT_USER, true);
193 } /* IRC_PRIVMSG */
194
195
196 /**
197  * Handler for the IRC command SQUERY.
198  */
199 GLOBAL bool
200 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
201 {
202         return Send_Message(Client, Req, CLIENT_SERVICE, true);
203 } /* IRC_SQUERY */
204
205
206 GLOBAL bool
207 IRC_TRACE( CLIENT *Client, REQUEST *Req )
208 {
209         CLIENT *from, *target, *c;
210         CONN_ID idx, idx2;
211         char user[CLIENT_USER_LEN];
212
213         assert( Client != NULL );
214         assert( Req != NULL );
215
216         /* Bad number of arguments? */
217         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
218
219         /* Search sender */
220         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
221         else from = Client;
222         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
223
224         /* Search target */
225         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
226         else target = Client_ThisServer( );
227         
228         /* Forward command to other server? */
229         if( target != Client_ThisServer( ))
230         {
231                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
232
233                 /* Send RPL_TRACELINK back to initiator */
234                 idx = Client_Conn( Client ); assert( idx > NONE );
235                 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
236                 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;
237
238                 /* Forward command */
239                 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
240                 return CONNECTED;
241         }
242
243         /* Infos about all connected servers */
244         c = Client_First( );
245         while( c )
246         {
247                 if( Client_Conn( c ) > NONE )
248                 {
249                         /* Local client */
250                         if( Client_Type( c ) == CLIENT_SERVER )
251                         {
252                                 /* Server link */
253                                 strlcpy( user, Client_User( c ), sizeof( user ));
254                                 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
255                                 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;
256                         }
257                         if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
258                         {
259                                 /* IRC Operator */
260                                 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
261                         }
262                 }
263                 c = Client_Next( c );
264         }
265
266         IRC_SetPenalty( Client, 3 );
267         return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
268 } /* IRC_TRACE */
269
270
271 GLOBAL bool
272 IRC_HELP( CLIENT *Client, REQUEST *Req )
273 {
274         COMMAND *cmd;
275
276         assert( Client != NULL );
277         assert( Req != NULL );
278
279         /* Bad number of arguments? */
280         if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
281
282         cmd = Parse_GetCommandStruct( );
283         while( cmd->name )
284         {
285                 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
286                 cmd++;
287         }
288         
289         IRC_SetPenalty( Client, 2 );
290         return CONNECTED;
291 } /* IRC_HELP */
292
293
294 static char *
295 Option_String( CONN_ID Idx )
296 {
297         static char option_txt[8];
298         UINT16 options;
299
300         options = Conn_Options(Idx);
301
302         strcpy(option_txt, "F");        /* No idea what this means, but the
303                                          * original ircd sends it ... */
304 #ifdef ZLIB
305         if(options & CONN_ZIP)          /* zlib compression supported. */
306                 strcat(option_txt, "z");
307 #endif
308
309         return option_txt;
310 } /* Option_String */
311
312
313 static bool
314 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
315 {
316         CLIENT *cl, *from;
317         CHANNEL *chan;
318         char *currentTarget = Req->argv[0];
319         char *lastCurrentTarget = NULL;
320
321         assert(Client != NULL);
322         assert(Req != NULL);
323
324         if (Req->argc == 0) {
325                 if (!SendErrors)
326                         return CONNECTED;
327                 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
328                                           Client_ID(Client), Req->command);
329         }
330         if (Req->argc == 1) {
331                 if (!SendErrors)
332                         return CONNECTED;
333                 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
334                                           Client_ID(Client));
335         }
336         if (Req->argc > 2) {
337                 if (!SendErrors)
338                         return CONNECTED;
339                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
340                                           Client_ID(Client), Req->command);
341         }
342
343         if (Client_Type(Client) == CLIENT_SERVER)
344                 from = Client_Search(Req->prefix);
345         else
346                 from = Client;
347         if (!from)
348                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
349                                           Client_ID(Client), Req->prefix);
350
351         /* handle msgtarget = msgto *("," msgto) */
352         currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
353         ngt_UpperStr(Req->command);
354
355         while (currentTarget) {
356                 /* Check for and handle valid <msgto> of form:
357                  * RFC 2812 2.3.1:
358                  *   msgto =  channel / ( user [ "%" host ] "@" servername )
359                  *   msgto =/ ( user "%" host ) / targetmask
360                  *   msgto =/ nickname / ( nickname "!" user "@" host )
361                  */
362                 if (strchr(currentTarget, '!') == NULL)
363                         /* nickname */
364                         cl = Client_Search(currentTarget);
365                 else
366                         cl = NULL;
367
368                 if (cl == NULL) {
369                         /* If currentTarget isn't a nickname check for:
370                          * user ["%" host] "@" servername
371                          * user "%" host
372                          * nickname "!" user "@" host
373                          */
374                         char target[COMMAND_LEN];
375                         char * nick = NULL;
376                         char * user = NULL;
377                         char * host = NULL;
378                         char * server = NULL;
379
380                         strlcpy(target, currentTarget, COMMAND_LEN);
381                         server = strchr(target, '@');
382                         if (server) {
383                                 *server = '\0';
384                                 server++;
385                         }
386                         host = strchr(target, '%');
387                         if (host) {
388                                 *host = '\0';
389                                 host++;
390                         }
391                         user = strchr(target, '!');
392                         if (user) {
393                                 /* msgto form: nick!user@host */
394                                 *user = '\0';
395                                 user++;
396                                 nick = target;
397                                 host = server; /* not "@server" but "@host" */
398                         } else {
399                                 user = target;
400                         }
401
402                         for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
403                                 if (Client_Type(cl) != CLIENT_USER)
404                                         continue;
405                                 if (nick != NULL && host != NULL) {
406                                         if (strcmp(nick, Client_ID(cl)) == 0 &&
407                                             strcmp(user, Client_User(cl)) == 0 &&
408                                             strcasecmp(host, Client_Hostname(cl)) == 0)
409                                                 break;
410                                         else
411                                                 continue;
412                                 }
413                                 if (strcasecmp(user, Client_User(cl)) != 0)
414                                         continue;
415                                 if (host != NULL && strcasecmp(host,
416                                                 Client_Hostname(cl)) != 0)
417                                         continue;
418                                 if (server != NULL && strcasecmp(server,
419                                                 Client_ID(Client_Introducer(cl))) != 0)
420                                         continue;
421                                 break;
422                         }
423                 }
424
425                 if (cl) {
426                         /* Target is a user, enforce type */
427 #ifndef STRICT_RFC
428                         if (Client_Type(cl) != ForceType &&
429                             !(ForceType == CLIENT_USER &&
430                               (Client_Type(cl) == CLIENT_USER ||
431                                Client_Type(cl) == CLIENT_SERVICE))) {
432 #else
433                         if (Client_Type(cl) != ForceType) {
434 #endif
435                                 if (!SendErrors)
436                                         return CONNECTED;
437                                 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
438                                                           Client_ID(from),
439                                                           currentTarget);
440                         }
441
442 #ifndef STRICT_RFC
443                         if (ForceType == CLIENT_SERVICE &&
444                             (Conn_Options(Client_Conn(Client_NextHop(cl)))
445                              & CONN_RFC1459)) {
446                                 /* SQUERY command but RFC 1459 link: convert
447                                  * request to PRIVMSG command */
448                                 Req->command = "PRIVMSG";
449                         }
450 #endif
451
452                         if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
453                             && strchr(Client_Modes(cl), 'a')) {
454                                 /* Target is away */
455                                 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
456                                                         Client_ID(from),
457                                                         Client_ID(cl),
458                                                         Client_Away(cl)))
459                                         return DISCONNECTED;
460                         }
461                         if (Client_Conn(from) > NONE) {
462                                 Conn_UpdateIdle(Client_Conn(from));
463                         }
464                         if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
465                                                       Req->command, Client_ID(cl),
466                                                       Req->argv[1]))
467                                 return DISCONNECTED;
468                 } else if (ForceType != CLIENT_SERVICE
469                            && strchr("$#", currentTarget[0])
470                            && strchr(currentTarget, '.')) {
471                         /* targetmask */
472                         if (!Send_Message_Mask(from, Req->command, currentTarget,
473                                                Req->argv[1], SendErrors))
474                                 return DISCONNECTED;
475                 } else if (ForceType != CLIENT_SERVICE
476                            && (chan = Channel_Search(currentTarget))) {
477                         /* channel */
478                         if (!Channel_Write(chan, from, Client, Req->command,
479                                            SendErrors, Req->argv[1]))
480                                         return DISCONNECTED;
481                 } else {
482                         if (!SendErrors)
483                                 return CONNECTED;
484                         if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
485                                                 Client_ID(from), currentTarget))
486                                 return DISCONNECTED;
487                 }
488
489                 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
490         }
491
492         return CONNECTED;
493 } /* Send_Message */
494
495
496 static bool
497 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
498                   char * message, bool SendErrors)
499 {
500         CLIENT *cl;
501         bool client_match;
502         char *mask = targetMask + 1;
503
504         cl = NULL;
505
506         if (strchr(Client_Modes(from), 'o') == NULL) {
507                 if (!SendErrors)
508                         return true;
509                 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
510                                           Client_ID(from));
511         }
512
513         if (targetMask[0] == '#') {
514                 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
515                         if (Client_Type(cl) != CLIENT_USER)
516                                 continue;
517                         client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
518                         if (client_match)
519                                 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
520                                                 command, Client_ID(cl), message))
521                                         return false;
522                 }
523         } else {
524                 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
525                         if (Client_Type(cl) != CLIENT_USER)
526                                 continue;
527                         client_match = MatchCaseInsensitive(mask,
528                                         Client_ID(Client_Introducer(cl)));
529                         if (client_match)
530                                 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
531                                                 command, Client_ID(cl), message))
532                                         return false;
533                 }
534         }
535         return CONNECTED;
536 } /* Send_Message_Mask */
537
538
539 /* -eof- */